diff --git a/phoenix-core-server/src/main/java/org/apache/phoenix/hbase/index/IndexRegionObserver.java b/phoenix-core-server/src/main/java/org/apache/phoenix/hbase/index/IndexRegionObserver.java index b1145c721bb..1bd04d725b2 100644 --- a/phoenix-core-server/src/main/java/org/apache/phoenix/hbase/index/IndexRegionObserver.java +++ b/phoenix-core-server/src/main/java/org/apache/phoenix/hbase/index/IndexRegionObserver.java @@ -151,7 +151,6 @@ import org.slf4j.LoggerFactory; import org.xerial.snappy.Snappy; -import org.apache.phoenix.thirdparty.com.google.common.base.Preconditions; import org.apache.phoenix.thirdparty.com.google.common.collect.ArrayListMultimap; import org.apache.phoenix.thirdparty.com.google.common.collect.ListMultimap; import org.apache.phoenix.thirdparty.com.google.common.collect.Lists; @@ -242,6 +241,8 @@ private class PendingRow { private int count; private boolean usable; private ImmutableBytesPtr rowKey; + // The batch that the next batch on this row chains behind, or null to read the row from disk. + // It is only changed while holding the row lock private BatchMutateContext lastContext; PendingRow(ImmutableBytesPtr rowKey, BatchMutateContext context) { @@ -251,16 +252,30 @@ private class PendingRow { this.rowKey = rowKey; } - public BatchMutateContext addAndGetPrevCtx(BatchMutateContext context) { + /** + * Adds a batch to this row and returns true, or returns false if this row has been removed from + * the pending rows. The caller must hold the row lock. + */ + public boolean add() { synchronized (this) { - if (usable) { - BatchMutateContext previousContext = lastContext; - count++; - lastContext = context; - return previousContext; + if (!usable) { + return false; } + count++; + return true; + } + } + + public BatchMutateContext getLastContext() { + synchronized (this) { + return lastContext; + } + } + + public void setLastContext(BatchMutateContext context) { + synchronized (this) { + lastContext = context; } - return null; } public void remove() { @@ -273,6 +288,20 @@ public void remove() { } } + /** + * Removes a batch that is releasing this row without updating it. The batch it was chained + * behind becomes the last context again. The caller must hold the row lock. + */ + public void removeAndRestorePrevCtx(BatchMutateContext context, + BatchMutateContext prevContext) { + synchronized (this) { + if (lastContext == context) { + lastContext = prevContext; + } + remove(); + } + } + public int getCount() { return count; } @@ -355,8 +384,11 @@ public static class BatchMutateContext { private Set rowsToLock = new TreeSet<>(); // The current and next states of the data rows corresponding to the pending mutations private HashMap> dataRowStates; - // The previous concurrent batch contexts + // For each row, the pending batch this batch was chained behind. Rows read from disk have no + // entry private HashMap lastConcurrentBatchContext = null; + // True if the rows of this batch have been added to pendingRows + private boolean addedToPendingRows = false; // The latches of the threads waiting for this batch to complete private List waitList = null; private Map multiMutationMap; @@ -1189,6 +1221,28 @@ private boolean isPartialUncoveredIndexMutation(PhoenixIndexMetaData indexMetaDa return false; } + /** + * Returns the pending batch whose update of the given row has not been written yet, starting from + * the given batch and skipping the failed batches it was chained behind. Returns null if the row + * should be read from disk. The row is locked by the caller, so a batch in the PRE phase cannot + * complete successfully while this runs. + */ + private static BatchMutateContext getPendingContext(BatchMutateContext context, + ImmutableBytesPtr rowKey) { + while (context != null) { + BatchMutatePhase phase = context.getCurrentPhase(); + if (phase == BatchMutatePhase.PRE) { + return context; + } + if (phase != BatchMutatePhase.FAILED || context.lastConcurrentBatchContext == null) { + // Either the batch completed and its update is on disk, or nothing is pending before it + return null; + } + context = context.lastConcurrentBatchContext.get(rowKey); + } + return null; + } + /** * Retrieve the data row state either from memory or disk. The rows are locked by the caller. *

@@ -1215,41 +1269,41 @@ private void getCurrentRowStates(ObserverContext c // Add the data table rows in the mini batch to the per region collection of pending // rows. This will be used to detect concurrent updates PendingRow existingPendingRow = pendingRows.putIfAbsent(rowKeyPtr, pendingRow); - if (existingPendingRow == null) { - // There was no pending row for this row key. We need to retrieve this row from disk - keys.add(PVarbinary.INSTANCE.getKeyRange(rowKeyPtr.get(), SortOrder.ASC)); - } else { - // There is a pending row for this row key. We need to retrieve the row from memory - BatchMutateContext lastContext = existingPendingRow.addAndGetPrevCtx(context); - if (lastContext != null) { - BatchMutatePhase phase = lastContext.getCurrentPhase(); - Preconditions.checkArgument(phase != BatchMutatePhase.POST, - "the phase of the last batch cannot be POST"); - if (phase == BatchMutatePhase.PRE) { - if (context.lastConcurrentBatchContext == null) { - context.lastConcurrentBatchContext = new HashMap<>(); - } - context.lastConcurrentBatchContext.put(rowKeyPtr, lastContext); - if (context.maxPendingRowCount < existingPendingRow.getCount()) { - context.maxPendingRowCount = existingPendingRow.getCount(); - } - Put put = lastContext.getNextDataRowState(rowKeyPtr); - if (put != null) { - // We have detected a concurrent update so do a deep copy of the - // previous update but we can skip the attributes - Put copy = MutationUtil.copyPut(put, true); - context.dataRowStates.put(rowKeyPtr, new Pair<>(copy, new Put(copy))); - } - } else { - // The last batch for this row key failed. We cannot use the memory state. - // So we need to retrieve this row from disk - keys.add(PVarbinary.INSTANCE.getKeyRange(rowKeyPtr.get(), SortOrder.ASC)); - } + BatchMutateContext lastContext = null; + if (existingPendingRow != null) { + if (existingPendingRow.add()) { + lastContext = getPendingContext(existingPendingRow.getLastContext(), rowKeyPtr); + existingPendingRow.setLastContext(context); } else { // The existing pending row is removed from the map. That means there is no // pending row for this row key anymore. We need to add the new one to the map pendingRows.put(rowKeyPtr, pendingRow); - keys.add(PVarbinary.INSTANCE.getKeyRange(rowKeyPtr.get(), SortOrder.ASC)); + } + } + if (lastContext == null) { + // There is no pending update for this row key. We need to retrieve this row from disk + keys.add(PVarbinary.INSTANCE.getKeyRange(rowKeyPtr.get(), SortOrder.ASC)); + continue; + } + // There is a pending update for this row key. We need to retrieve the row from memory + if (context.lastConcurrentBatchContext == null) { + context.lastConcurrentBatchContext = new HashMap<>(); + } + context.lastConcurrentBatchContext.put(rowKeyPtr, lastContext); + if (context.maxPendingRowCount < existingPendingRow.getCount()) { + context.maxPendingRowCount = existingPendingRow.getCount(); + } + } + // Set before anything below can throw, so that the rows are removed from pendingRows + context.addedToPendingRows = true; + if (context.lastConcurrentBatchContext != null) { + for (ImmutableBytesPtr rowKeyPtr : context.lastConcurrentBatchContext.keySet()) { + Put put = context.lastConcurrentBatchContext.get(rowKeyPtr).getNextDataRowState(rowKeyPtr); + if (put != null) { + // We have detected a concurrent update so do a deep copy of the + // previous update but we can skip the attributes + Put copy = MutationUtil.copyPut(put, true); + context.dataRowStates.put(rowKeyPtr, new Pair<>(copy, new Put(copy))); } } } @@ -2013,6 +2067,11 @@ && isPartialUncoveredIndexMutation(indexMetaData, miniBatchOp) // early exit if we are not changing any rows if (context.rowsToLock.isEmpty()) { + // The result of this batch may be based on the uncommitted state of a previous concurrent + // batch, so wait for that batch to complete before returning + if (context.lastConcurrentBatchContext != null) { + waitForPreviousConcurrentBatch(table, context); + } return; } } @@ -2070,9 +2129,14 @@ private void releaseLocksForOnDupIgnoreMutations( RowLock rowLock = rowLockIterator.next(); ImmutableBytesPtr rowKey = rowLock.getRowKey(); if (row.equals(rowKey)) { - PendingRow pendingRow = pendingRows.get(rowKey); + PendingRow pendingRow = context.addedToPendingRows ? pendingRows.get(rowKey) : null; if (pendingRow != null) { - pendingRow.remove(); + // This batch will not update the row, so the batch it was chained behind becomes the + // last batch for the row again + BatchMutateContext prevContext = context.lastConcurrentBatchContext == null + ? null + : context.lastConcurrentBatchContext.get(rowKey); + pendingRow.removeAndRestorePrevCtx(context, prevContext); } rowLock.release(); rowLockIterator.remove(); @@ -2140,13 +2204,13 @@ public void postBatchMutateIndispensably(ObserverContext props = Maps.newHashMapWithExpectedSize(4); + props.put(IndexRegionObserver.INDEX_LAZY_POST_BATCH_WRITE, Boolean.TRUE.toString()); + props.put(IndexRegionObserver.PHOENIX_INDEX_CDC_MUTATION_SERIALIZE, Boolean.FALSE.toString()); + // Batches waiting on the parked batch must not time out + props.put("phoenix.index.concurrent.wait.duration.ms", Integer.toString(30000)); + // More than one batch can be parked in its index write at a time + props.put(AbstractParallelWriterIndexCommitter.NUM_CONCURRENT_INDEX_WRITER_THREADS_CONF_KEY, + Integer.toString(4)); + setUpTestDriver(new ReadOnlyProps(props.entrySet().iterator())); + } + + private final boolean eventual; + + public ConcurrentNoOpAtomicUpsertIndexIT(boolean eventual) { + this.eventual = eventual; + } + + @Parameterized.Parameters(name = "eventual={0}") + public static synchronized Collection data() { + return Arrays.asList(new Object[][] { { true }, { false } }); + } + + /** Parks the next write to an armed table until released, and optionally fails it. */ + public static class BlockingIndexWriteObserver extends SimpleRegionObserver { + private static final Map ARMED = new ConcurrentHashMap<>(); + + static void arm(String indexName, CountDownLatch reached, CountDownLatch proceed) { + arm(indexName, reached, proceed, false); + } + + static void arm(String indexName, CountDownLatch reached, CountDownLatch proceed, + boolean fail) { + ARMED.put(indexName, new Object[] { reached, proceed, fail }); + } + + @Override + public void preBatchMutate(ObserverContext c, + MiniBatchOperationInProgress miniBatchOp) throws IOException { + String tableName = c.getEnvironment().getRegionInfo().getTable().getNameAsString(); + Object[] armed = ARMED.remove(tableName); + if (armed == null) { + return; + } + ((CountDownLatch) armed[0]).countDown(); + try { + ((CountDownLatch) armed[1]).await(60, TimeUnit.SECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IOException(e); + } + if ((Boolean) armed[2]) { + throw new DoNotRetryIOException("Simulating an index write failure"); + } + } + } + + /** Delays every write to the table it is added to, and fails some of them at random. */ + public static class RandomIndexWriteObserver extends SimpleRegionObserver { + private static volatile int failurePercent = 0; + private static volatile int maxDelayMs = 0; + + static void configure(int failurePercent, int maxDelayMs) { + RandomIndexWriteObserver.failurePercent = failurePercent; + RandomIndexWriteObserver.maxDelayMs = maxDelayMs; + } + + @Override + public void preBatchMutate(ObserverContext c, + MiniBatchOperationInProgress miniBatchOp) throws IOException { + ThreadLocalRandom random = ThreadLocalRandom.current(); + try { + if (maxDelayMs > 0) { + Thread.sleep(random.nextInt(maxDelayMs + 1)); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IOException(e); + } + if (random.nextInt(100) < failurePercent) { + throw new DoNotRetryIOException("Simulating a random index write failure"); + } + } + } + + private static int conditionalUpdate(Connection conn, String tableName, String writer, + int expected) throws Exception { + return conditionalUpdate(conn, tableName, "r1", writer, expected).getFirst(); + } + + /** Returns the update count and the row returned by the update. */ + private static Pair conditionalUpdate(Connection conn, String tableName, + String rowKey, String writer, int expected) throws Exception { + BsonDocument condition = new BsonDocument(); + condition.put("$EXPR", new BsonString("#v = :v")); + condition.put("$VAL", new BsonDocument().append(":v", new BsonInt32(expected))); + condition.put("$KEYS", new BsonDocument().append("#v", new BsonString("ver"))); + BsonDocument update = new BsonDocument().append("$SET", new BsonDocument() + .append("ver", new BsonInt32(expected + 1)).append("by", new BsonString(writer))); + PreparedStatement ps = conn.prepareStatement("UPSERT INTO " + tableName + + " VALUES (?) ON DUPLICATE KEY UPDATE COL = CASE WHEN BSON_CONDITION_EXPRESSION(COL, '" + + condition.toJson() + "') THEN BSON_UPDATE_EXPRESSION(COL, '" + update.toJson() + + "') ELSE COL END"); + ps.setString(1, rowKey); + Pair result = + ps.unwrap(PhoenixPreparedStatement.class).executeAtomicUpdateReturnRow(); + ResultSet rs = result.getSecond(); + return new Pair<>(result.getFirst(), rs == null ? null : (BsonDocument) rs.getObject(2)); + } + + private static Thread runAsync(String name, String tableName, int expected, + AtomicReference count, AtomicReference error) { + Thread t = new Thread(() -> { + try (Connection conn = DriverManager.getConnection(getUrl())) { + conn.setAutoCommit(true); + count.set(conditionalUpdate(conn, tableName, name, expected)); + } catch (Exception e) { + error.set(e); + } + }, name); + t.start(); + return t; + } + + /** Returns true if the error was caused by a simulated index write failure. */ + private static boolean isSimulatedFailure(Throwable e) { + for (Throwable t = e; t != null; t = t.getCause()) { + if (t.getMessage() != null && t.getMessage().contains("Simulating")) { + return true; + } + } + return false; + } + + /** + * Creates the table with row r1 (ver = 1) and its index, and installs the blocking observer. + * Returns the name of the table whose write is blocked. + */ + private String createTable(Connection conn, String tableName) throws Exception { + String indexName = "IDX_" + tableName; + conn.createStatement().execute("CREATE TABLE " + tableName + + " (PK1 VARCHAR NOT NULL, COL BSON CONSTRAINT pk PRIMARY KEY(PK1))"); + conn.createStatement() + .execute("CREATE UNCOVERED INDEX " + indexName + " ON " + tableName + + " (BSON_VALUE(COL, 'idxKey', 'VARCHAR')) " + + "WHERE BSON_VALUE(COL, 'idxKey', 'VARCHAR') IS NOT NULL" + + (eventual ? " CONSISTENCY = EVENTUAL" : "")); + PreparedStatement ps = conn.prepareStatement("UPSERT INTO " + tableName + " VALUES (?, ?)"); + ps.setString(1, "r1"); + ps.setObject(2, RawBsonDocument.parse("{\"idxKey\": \"g1\", \"ver\": 1}")); + ps.executeUpdate(); + + // With only eventually consistent indexes, the CDC index is the synchronous index write + String blockedTable = eventual ? CDCUtil.getCDCIndexName("CDC_" + tableName) : indexName; + TestUtil.addCoprocessor(conn, blockedTable, BlockingIndexWriteObserver.class); + return blockedTable; + } + + private static BsonDocument getRow(Connection conn, String tableName) throws Exception { + ResultSet rs = + conn.createStatement().executeQuery("SELECT COL FROM " + tableName + " WHERE PK1 = 'r1'"); + assertTrue(rs.next()); + return (BsonDocument) rs.getObject(1); + } + + private static int getVer(Connection conn, String tableName) throws Exception { + return getRow(conn, tableName).getInt32("ver").getValue(); + } + + private static String getWriter(Connection conn, String tableName) throws Exception { + return getRow(conn, tableName).getString("by").getValue(); + } + + /** + *

    + *
  1. Batch A: v 1 -> 2, parked in doPre.
  2. + *
  3. Batch B: v 1 -> 2, a no-op on A's pending state. Must not return before A completes.
  4. + *
  5. Batch C: v 2 -> 3 while A is pending. Must chain on A, not on B.
  6. + *
+ */ + @Test + public void testConcurrentConditionalUpdatesWithNoOp() throws Exception { + String tableName = generateUniqueName(); + try (Connection conn = DriverManager.getConnection(getUrl())) { + conn.setAutoCommit(true); + String blockedTable = createTable(conn, tableName); + + // A: 1 -> 2, parks in doPre with the row lock released + CountDownLatch aReached = new CountDownLatch(1); + CountDownLatch aProceed = new CountDownLatch(1); + BlockingIndexWriteObserver.arm(blockedTable, aReached, aProceed); + AtomicReference aCnt = new AtomicReference<>(); + AtomicReference aError = new AtomicReference<>(); + Thread a = runAsync("A", tableName, 1, aCnt, aError); + Thread b = null; + Thread c = null; + try { + assertTrue("batch A did not reach the index write", aReached.await(60, TimeUnit.SECONDS)); + + // B: 1 -> 2, a no-op on A's pending state + AtomicReference bCnt = new AtomicReference<>(); + AtomicReference bError = new AtomicReference<>(); + b = runAsync("B", tableName, 1, bCnt, bError); + b.join(3000); + assertTrue("batch B returned before batch A completed: " + bError.get(), b.isAlive()); + + // C: 2 -> 3 while A is still pending + AtomicReference cCnt = new AtomicReference<>(); + AtomicReference cError = new AtomicReference<>(); + c = runAsync("C", tableName, 2, cCnt, cError); + c.join(3000); + assertTrue("batch C returned before batch A completed: " + cError.get(), c.isAlive()); + + aProceed.countDown(); + a.join(60000); + b.join(60000); + c.join(60000); + assertFalse(a.isAlive()); + assertFalse(b.isAlive()); + assertFalse(c.isAlive()); + assertNull("batch A failed: " + aError.get(), aError.get()); + assertNull("batch B failed: " + bError.get(), bError.get()); + assertNull("batch C failed: " + cError.get(), cError.get()); + assertEquals(Integer.valueOf(1), aCnt.get()); + assertEquals(Integer.valueOf(0), bCnt.get()); + assertEquals(Integer.valueOf(1), cCnt.get()); + } finally { + aProceed.countDown(); + a.join(60000); + if (b != null) { + b.join(60000); + } + if (c != null) { + c.join(60000); + } + TestUtil.removeCoprocessor(conn, blockedTable, BlockingIndexWriteObserver.class); + } + + assertEquals(3, getVer(conn, tableName)); + assertEquals("C", getWriter(conn, tableName)); + } + } + + /** + *
    + *
  1. Batch A: v 1 -> 2, parked in doPre.
  2. + *
  3. Batch X: v 2 -> 3, chained on A, fails in doPre.
  4. + *
  5. Batch B: no-op, must skip X, chain on A and wait for A.
  6. + *
  7. Batch C: no-op, must also chain on A, not on B.
  8. + *
+ */ + @Test + public void testNoOpAfterConcurrentFailedBatch() throws Exception { + String tableName = generateUniqueName(); + try (Connection conn = DriverManager.getConnection(getUrl())) { + conn.setAutoCommit(true); + String blockedTable = createTable(conn, tableName); + + CountDownLatch aReached = new CountDownLatch(1); + CountDownLatch aProceed = new CountDownLatch(1); + BlockingIndexWriteObserver.arm(blockedTable, aReached, aProceed); + AtomicReference aCnt = new AtomicReference<>(); + AtomicReference aError = new AtomicReference<>(); + Thread a = runAsync("A", tableName, 1, aCnt, aError); + Thread b = null; + Thread c = null; + try { + assertTrue("batch A did not reach the index write", aReached.await(60, TimeUnit.SECONDS)); + + IndexRegionObserver.setFailPreIndexUpdatesForTesting(true); + try { + conditionalUpdate(conn, tableName, "X", 2); + fail("batch X should have failed"); + } catch (SQLException e) { + assertTrue("unexpected error: " + e, isSimulatedFailure(e)); + } finally { + IndexRegionObserver.setFailPreIndexUpdatesForTesting(false); + } + + AtomicReference bCnt = new AtomicReference<>(); + AtomicReference bError = new AtomicReference<>(); + b = runAsync("B", tableName, 99, bCnt, bError); + b.join(3000); + assertTrue("batch B returned before batch A completed: " + bError.get(), b.isAlive()); + + AtomicReference cCnt = new AtomicReference<>(); + AtomicReference cError = new AtomicReference<>(); + c = runAsync("C", tableName, 99, cCnt, cError); + c.join(3000); + assertTrue("batch C returned before batch A completed: " + cError.get(), c.isAlive()); + + aProceed.countDown(); + a.join(60000); + b.join(60000); + c.join(60000); + assertFalse(a.isAlive()); + assertFalse(b.isAlive()); + assertFalse(c.isAlive()); + assertNull("batch A failed: " + aError.get(), aError.get()); + assertNull("batch B failed: " + bError.get(), bError.get()); + assertNull("batch C failed: " + cError.get(), cError.get()); + assertEquals(Integer.valueOf(1), aCnt.get()); + assertEquals(Integer.valueOf(0), bCnt.get()); + assertEquals(Integer.valueOf(0), cCnt.get()); + } finally { + IndexRegionObserver.setFailPreIndexUpdatesForTesting(false); + aProceed.countDown(); + a.join(60000); + if (b != null) { + b.join(60000); + } + if (c != null) { + c.join(60000); + } + TestUtil.removeCoprocessor(conn, blockedTable, BlockingIndexWriteObserver.class); + } + + assertEquals(2, getVer(conn, tableName)); + assertEquals("A", getWriter(conn, tableName)); + } + } + + /** + *
    + *
  1. Batch A: v 1 -> 2, parked in doPre.
  2. + *
  3. Batch B: v 1 -> 2, a no-op on A's pending state.
  4. + *
  5. Batch C: v 1 -> 2, also a no-op on A's pending state.
  6. + *
  7. Batch D: v 1 -> 2, must see A's pending state and must not overwrite A.
  8. + *
+ */ + @Test + public void testNoLostUpdateAfterConcurrentNoOp() throws Exception { + String tableName = generateUniqueName(); + try (Connection conn = DriverManager.getConnection(getUrl())) { + conn.setAutoCommit(true); + String blockedTable = createTable(conn, tableName); + + CountDownLatch aReached = new CountDownLatch(1); + CountDownLatch aProceed = new CountDownLatch(1); + BlockingIndexWriteObserver.arm(blockedTable, aReached, aProceed); + Map> counts = new ConcurrentHashMap<>(); + Map> errors = new ConcurrentHashMap<>(); + Map threads = new LinkedHashMap<>(); + try { + for (String name : new String[] { "A", "B", "C", "D" }) { + counts.put(name, new AtomicReference<>()); + errors.put(name, new AtomicReference<>()); + threads.put(name, runAsync(name, tableName, 1, counts.get(name), errors.get(name))); + if (name.equals("A")) { + assertTrue("batch A did not reach the index write", + aReached.await(60, TimeUnit.SECONDS)); + } else { + threads.get(name).join(3000); + } + } + aProceed.countDown(); + for (Thread t : threads.values()) { + t.join(60000); + assertFalse(t.isAlive()); + } + // Check the data before the errors so that a lost update is reported even if a batch + // failed + assertEquals("batch A failed: " + errors.get("A").get(), Integer.valueOf(1), + counts.get("A").get()); + assertEquals("A", getWriter(conn, tableName)); + assertEquals(2, getVer(conn, tableName)); + for (String name : threads.keySet()) { + assertNull("batch " + name + " failed: " + errors.get(name).get(), + errors.get(name).get()); + } + assertEquals(Integer.valueOf(0), counts.get("B").get()); + assertEquals(Integer.valueOf(0), counts.get("C").get()); + assertEquals(Integer.valueOf(0), counts.get("D").get()); + } finally { + aProceed.countDown(); + for (Thread t : threads.values()) { + t.join(60000); + } + TestUtil.removeCoprocessor(conn, blockedTable, BlockingIndexWriteObserver.class); + } + + assertEquals(2, getVer(conn, tableName)); + assertEquals("A", getWriter(conn, tableName)); + } + } + + /** + *
    + *
  1. Batch A: v 1 -> 2, parked in doPre.
  2. + *
  3. Batch X: v 2 -> 3, chained on A, fails in doPre.
  4. + *
  5. Batch D: v 1 -> 2, must see A's pending state and must not overwrite A.
  6. + *
+ */ + @Test + public void testNoLostUpdateAfterConcurrentFailedBatch() throws Exception { + String tableName = generateUniqueName(); + try (Connection conn = DriverManager.getConnection(getUrl())) { + conn.setAutoCommit(true); + String blockedTable = createTable(conn, tableName); + + CountDownLatch aReached = new CountDownLatch(1); + CountDownLatch aProceed = new CountDownLatch(1); + BlockingIndexWriteObserver.arm(blockedTable, aReached, aProceed); + AtomicReference aCnt = new AtomicReference<>(); + AtomicReference aError = new AtomicReference<>(); + Thread a = runAsync("A", tableName, 1, aCnt, aError); + Thread d = null; + try { + assertTrue("batch A did not reach the index write", aReached.await(60, TimeUnit.SECONDS)); + + IndexRegionObserver.setFailPreIndexUpdatesForTesting(true); + try { + conditionalUpdate(conn, tableName, "X", 2); + fail("batch X should have failed"); + } catch (SQLException e) { + assertTrue("unexpected error: " + e, isSimulatedFailure(e)); + } finally { + IndexRegionObserver.setFailPreIndexUpdatesForTesting(false); + } + + AtomicReference dCount = new AtomicReference<>(); + AtomicReference dError = new AtomicReference<>(); + d = runAsync("D", tableName, 1, dCount, dError); + d.join(3000); + + aProceed.countDown(); + a.join(60000); + d.join(60000); + assertFalse(a.isAlive()); + assertFalse(d.isAlive()); + assertNull("batch A failed: " + aError.get(), aError.get()); + assertEquals(Integer.valueOf(1), aCnt.get()); + assertNull("batch D failed: " + dError.get(), dError.get()); + assertEquals("batch D overwrote batch A", Integer.valueOf(0), dCount.get()); + } finally { + IndexRegionObserver.setFailPreIndexUpdatesForTesting(false); + aProceed.countDown(); + a.join(60000); + if (d != null) { + d.join(60000); + } + TestUtil.removeCoprocessor(conn, blockedTable, BlockingIndexWriteObserver.class); + } + + assertEquals(2, getVer(conn, tableName)); + assertEquals("A", getWriter(conn, tableName)); + } + } + + /** + *
    + *
  1. Batch A: v 1 -> 2, parked in doPre.
  2. + *
  3. Batch Y: v 2 -> 3, chained on A, parked in doPre.
  4. + *
  5. Batch X: v 3 -> 4, chained on Y, parked in doPre.
  6. + *
  7. Y fails, then X fails.
  8. + *
  9. Batch D: v 1 -> 2, must skip X and Y, see A's pending state and must not overwrite A.
  10. + *
+ */ + @Test + public void testNoLostUpdateAfterConsecutiveFailedBatches() throws Exception { + String tableName = generateUniqueName(); + try (Connection conn = DriverManager.getConnection(getUrl())) { + conn.setAutoCommit(true); + String blockedTable = createTable(conn, tableName); + + Map> counts = new ConcurrentHashMap<>(); + Map> errors = new ConcurrentHashMap<>(); + Map proceed = new ConcurrentHashMap<>(); + Map threads = new LinkedHashMap<>(); + try { + // A, Y and X park in doPre one after the other. Y and X then fail + String[] names = { "A", "Y", "X" }; + int[] expected = { 1, 2, 3 }; + for (int i = 0; i < names.length; i++) { + String name = names[i]; + counts.put(name, new AtomicReference<>()); + errors.put(name, new AtomicReference<>()); + proceed.put(name, new CountDownLatch(1)); + CountDownLatch reached = new CountDownLatch(1); + BlockingIndexWriteObserver.arm(blockedTable, reached, proceed.get(name), + !name.equals("A")); + threads.put(name, + runAsync(name, tableName, expected[i], counts.get(name), errors.get(name))); + assertTrue("batch " + name + " did not reach the index write", + reached.await(60, TimeUnit.SECONDS)); + } + for (String name : new String[] { "Y", "X" }) { + proceed.get(name).countDown(); + threads.get(name).join(60000); + assertFalse(threads.get(name).isAlive()); + assertTrue("unexpected error: " + errors.get(name).get(), + isSimulatedFailure(errors.get(name).get())); + } + + counts.put("D", new AtomicReference<>()); + errors.put("D", new AtomicReference<>()); + threads.put("D", runAsync("D", tableName, 1, counts.get("D"), errors.get("D"))); + threads.get("D").join(3000); + + proceed.get("A").countDown(); + for (Thread t : threads.values()) { + t.join(60000); + assertFalse(t.isAlive()); + } + assertNull("batch A failed: " + errors.get("A").get(), errors.get("A").get()); + assertEquals(Integer.valueOf(1), counts.get("A").get()); + assertNull("batch D failed: " + errors.get("D").get(), errors.get("D").get()); + assertEquals("batch D overwrote batch A", Integer.valueOf(0), counts.get("D").get()); + } finally { + for (CountDownLatch latch : proceed.values()) { + latch.countDown(); + } + for (Thread t : threads.values()) { + t.join(60000); + } + TestUtil.removeCoprocessor(conn, blockedTable, BlockingIndexWriteObserver.class); + } + + assertEquals(2, getVer(conn, tableName)); + assertEquals("A", getWriter(conn, tableName)); + } + } + + /** + *
    + *
  1. Batch A: v 1 -> 2, parked in doPre.
  2. + *
  3. Batch X: plain upsert of the full row, which is not tracked as pending, fails in + * doPre.
  4. + *
  5. Batch D: v 1 -> 2, must see A's pending state and must not overwrite A.
  6. + *
+ */ + @Test + public void testNoLostUpdateAfterUntrackedBatch() throws Exception { + String tableName = generateUniqueName(); + try (Connection conn = DriverManager.getConnection(getUrl())) { + conn.setAutoCommit(true); + String blockedTable = createTable(conn, tableName); + + CountDownLatch aReached = new CountDownLatch(1); + CountDownLatch aProceed = new CountDownLatch(1); + BlockingIndexWriteObserver.arm(blockedTable, aReached, aProceed); + AtomicReference aCnt = new AtomicReference<>(); + AtomicReference aError = new AtomicReference<>(); + Thread a = runAsync("A", tableName, 1, aCnt, aError); + Thread d = null; + try { + assertTrue("batch A did not reach the index write", aReached.await(60, TimeUnit.SECONDS)); + + IndexRegionObserver.setFailPreIndexUpdatesForTesting(true); + try { + PreparedStatement ps = + conn.prepareStatement("UPSERT INTO " + tableName + " VALUES (?, ?)"); + ps.setString(1, "r1"); + ps.setObject(2, RawBsonDocument.parse("{\"idxKey\": \"g1\", \"ver\": 7, \"by\": \"X\"}")); + ps.executeUpdate(); + fail("batch X should have failed"); + } catch (SQLException e) { + assertTrue("unexpected error: " + e, isSimulatedFailure(e)); + } finally { + IndexRegionObserver.setFailPreIndexUpdatesForTesting(false); + } + + AtomicReference dCount = new AtomicReference<>(); + AtomicReference dError = new AtomicReference<>(); + d = runAsync("D", tableName, 1, dCount, dError); + d.join(3000); + assertTrue("batch D returned before batch A completed: " + dError.get(), d.isAlive()); + + aProceed.countDown(); + a.join(60000); + d.join(60000); + assertFalse(a.isAlive()); + assertFalse(d.isAlive()); + assertNull("batch A failed: " + aError.get(), aError.get()); + assertEquals(Integer.valueOf(1), aCnt.get()); + assertNull("batch D failed: " + dError.get(), dError.get()); + assertEquals("batch D overwrote batch A", Integer.valueOf(0), dCount.get()); + } finally { + IndexRegionObserver.setFailPreIndexUpdatesForTesting(false); + aProceed.countDown(); + a.join(60000); + if (d != null) { + d.join(60000); + } + TestUtil.removeCoprocessor(conn, blockedTable, BlockingIndexWriteObserver.class); + } + + assertEquals(2, getVer(conn, tableName)); + assertEquals("A", getWriter(conn, tableName)); + } + } + + /** + *
    + *
  1. Batch A: v 1 -> 2, parked in doPre, then fails.
  2. + *
  3. Batch B: v 1 -> 2, a no-op on A's pending state. Must not return A's state, which is never + * committed, and must be retried after A fails.
  4. + *
+ */ + @Test + public void testNoOpOnFailedPendingBatch() throws Exception { + String tableName = generateUniqueName(); + try (Connection conn = DriverManager.getConnection(getUrl())) { + conn.setAutoCommit(true); + String blockedTable = createTable(conn, tableName); + + CountDownLatch aReached = new CountDownLatch(1); + CountDownLatch aProceed = new CountDownLatch(1); + BlockingIndexWriteObserver.arm(blockedTable, aReached, aProceed, true); + AtomicReference aCnt = new AtomicReference<>(); + AtomicReference aError = new AtomicReference<>(); + Thread a = runAsync("A", tableName, 1, aCnt, aError); + Thread b = null; + try { + assertTrue("batch A did not reach the index write", aReached.await(60, TimeUnit.SECONDS)); + + AtomicReference bCnt = new AtomicReference<>(); + AtomicReference bError = new AtomicReference<>(); + b = runAsync("B", tableName, 1, bCnt, bError); + b.join(3000); + assertTrue("batch B returned before batch A completed: " + bError.get(), b.isAlive()); + + aProceed.countDown(); + a.join(60000); + b.join(60000); + assertFalse(a.isAlive()); + assertFalse(b.isAlive()); + assertTrue("unexpected error: " + aError.get(), isSimulatedFailure(aError.get())); + assertNull("batch B failed: " + bError.get(), bError.get()); + assertEquals("batch B was a no-op on a state that was never committed", Integer.valueOf(1), + bCnt.get()); + } finally { + aProceed.countDown(); + a.join(60000); + if (b != null) { + b.join(60000); + } + TestUtil.removeCoprocessor(conn, blockedTable, BlockingIndexWriteObserver.class); + } + + assertEquals(2, getVer(conn, tableName)); + assertEquals("B", getWriter(conn, tableName)); + } + } + + /** + *
    + *
  1. Batch A: inserts row r2 with ON DUPLICATE KEY IGNORE, parked in doPre, then fails.
  2. + *
  3. Batch B: inserts row r2 with ON DUPLICATE KEY IGNORE, a no-op on A's pending insert. Must + * be retried after A fails and insert the row.
  4. + *
+ */ + @Test + public void testIgnoreOnFailedPendingInsert() throws Exception { + String tableName = generateUniqueName(); + try (Connection conn = DriverManager.getConnection(getUrl())) { + conn.setAutoCommit(true); + String blockedTable = createTable(conn, tableName); + + CountDownLatch aReached = new CountDownLatch(1); + CountDownLatch aProceed = new CountDownLatch(1); + BlockingIndexWriteObserver.arm(blockedTable, aReached, aProceed, true); + Map> errors = new ConcurrentHashMap<>(); + Map threads = new LinkedHashMap<>(); + try { + for (String name : new String[] { "A", "B" }) { + AtomicReference error = new AtomicReference<>(); + errors.put(name, error); + Thread t = new Thread(() -> { + try (Connection c = DriverManager.getConnection(getUrl())) { + c.setAutoCommit(true); + PreparedStatement ps = c.prepareStatement( + "UPSERT INTO " + tableName + " VALUES (?, ?) ON DUPLICATE KEY IGNORE"); + ps.setString(1, "r2"); + ps.setObject(2, RawBsonDocument + .parse("{\"idxKey\": \"g1\", \"ver\": 1, \"by\": \"" + name + "\"}")); + ps.executeUpdate(); + } catch (Exception e) { + error.set(e); + } + }, name); + threads.put(name, t); + t.start(); + if (name.equals("A")) { + assertTrue("batch A did not reach the index write", + aReached.await(60, TimeUnit.SECONDS)); + } else { + t.join(3000); + assertTrue("batch B returned before batch A completed: " + error.get(), t.isAlive()); + } + } + aProceed.countDown(); + for (Thread t : threads.values()) { + t.join(60000); + assertFalse(t.isAlive()); + } + assertTrue("unexpected error: " + errors.get("A").get(), + isSimulatedFailure(errors.get("A").get())); + assertNull("batch B failed: " + errors.get("B").get(), errors.get("B").get()); + } finally { + aProceed.countDown(); + for (Thread t : threads.values()) { + t.join(60000); + } + TestUtil.removeCoprocessor(conn, blockedTable, BlockingIndexWriteObserver.class); + } + + ResultSet rs = + conn.createStatement().executeQuery("SELECT COL FROM " + tableName + " WHERE PK1 = 'r2'"); + assertTrue("the row inserted by batch B is missing", rs.next()); + assertEquals("B", ((BsonDocument) rs.getObject(1)).getString("by").getValue()); + } + } + + /** + * Many threads run conditional v -> v + 1 updates on a few rows while index writes are delayed + * and fail at random. Checks that no update is lost, that no failed update is written, and that + * every row returned by an update was committed. + */ + @Test + public void testRandomConcurrentConditionalUpdates() throws Exception { + final int numRows = 2; + final int numThreads = 10; + final int updatesPerThread = 60; + String tableName = generateUniqueName(); + try (Connection conn = DriverManager.getConnection(getUrl())) { + conn.setAutoCommit(true); + String blockedTable = createTable(conn, tableName); + TestUtil.removeCoprocessor(conn, blockedTable, BlockingIndexWriteObserver.class); + PreparedStatement ps = conn.prepareStatement("UPSERT INTO " + tableName + " VALUES (?, ?)"); + for (int i = 0; i < numRows; i++) { + ps.setString(1, "s" + i); + ps.setObject(2, RawBsonDocument.parse("{\"idxKey\": \"g1\", \"ver\": 0}")); + ps.executeUpdate(); + } + + // For each row, the writer of each committed version, and the versions returned to clients + List> committed = new ArrayList<>(); + List> returned = new ArrayList<>(); + for (int i = 0; i < numRows; i++) { + committed.add(new ConcurrentHashMap<>()); + returned.add(new ConcurrentHashMap<>()); + } + Map duplicateVersions = new ConcurrentHashMap<>(); + AtomicReference unexpectedError = new AtomicReference<>(); + int[] outcomes = new int[3]; + + TestUtil.addCoprocessor(conn, blockedTable, RandomIndexWriteObserver.class); + RandomIndexWriteObserver.configure(25, 50); + List threads = new ArrayList<>(); + try { + for (int t = 0; t < numThreads; t++) { + final int threadId = t; + Thread thread = new Thread(() -> { + ThreadLocalRandom random = ThreadLocalRandom.current(); + int[] known = new int[numRows]; + try (Connection c = DriverManager.getConnection(getUrl())) { + c.setAutoCommit(true); + for (int i = 0; i < updatesPerThread; i++) { + int row = random.nextInt(numRows); + // Mostly use the last version seen, sometimes a stale one, and sometimes guess a + // version that a pending update may write so that this update chains on it + int choice = random.nextInt(10); + int expected = choice < 2 ? Math.max(0, known[row] - 1) + : choice < 5 ? known[row] + : known[row] + 1 + random.nextInt(2); + String writer = "T" + threadId + "-" + i; + Pair result; + try { + result = conditionalUpdate(c, tableName, "s" + row, writer, expected); + } catch (SQLException e) { + // Only a simulated failure, or a failed wait for a previous batch after the + // retries, is expected + if ( + !isSimulatedFailure(e) + && !e.toString().contains("One of the previous concurrent mutations") + ) { + throw e; + } + synchronized (outcomes) { + outcomes[2]++; + } + continue; + } + BsonDocument doc = result.getSecond(); + int ver = doc.getInt32("ver").getValue(); + String by = doc.containsKey("by") ? doc.getString("by").getValue() : ""; + if (result.getFirst() == 1) { + synchronized (outcomes) { + outcomes[0]++; + } + assertEquals(expected + 1, ver); + assertEquals(writer, by); + String prev = committed.get(row).putIfAbsent(ver, writer); + if (prev != null) { + duplicateVersions.put("s" + row + " v" + ver, prev + ", " + writer); + } + } else { + synchronized (outcomes) { + outcomes[1]++; + } + assertEquals(Integer.valueOf(0), result.getFirst()); + returned.get(row).put(ver, by); + } + known[row] = ver; + } + } catch (Throwable e) { + LOG.error("Unexpected error in thread " + threadId, e); + unexpectedError.compareAndSet(null, e); + } + }, "stress-" + t); + threads.add(thread); + thread.start(); + } + for (Thread thread : threads) { + thread.join(600000); + assertFalse("thread " + thread.getName() + " did not finish", thread.isAlive()); + } + } finally { + RandomIndexWriteObserver.configure(0, 0); + for (Thread thread : threads) { + thread.join(60000); + } + TestUtil.removeCoprocessor(conn, blockedTable, RandomIndexWriteObserver.class); + } + + LOG.info("Updates: {} succeeded, {} no-ops, {} failed", outcomes[0], outcomes[1], + outcomes[2]); + assertNull("unexpected error: " + unexpectedError.get(), unexpectedError.get()); + assertTrue("lost updates: " + duplicateVersions, duplicateVersions.isEmpty()); + assertTrue("no update succeeded", outcomes[0] > 0); + assertTrue("no update was a no-op", outcomes[1] > 0); + assertTrue("no update failed", outcomes[2] > 0); + + for (int row = 0; row < numRows; row++) { + ResultSet rs = conn.createStatement() + .executeQuery("SELECT COL FROM " + tableName + " WHERE PK1 = 's" + row + "'"); + assertTrue(rs.next()); + BsonDocument doc = (BsonDocument) rs.getObject(1); + int ver = doc.getInt32("ver").getValue(); + Map rowCommitted = committed.get(row); + // Every version from 1 to the final one was committed by exactly one successful update, + // so no update was lost and no failed update was written + assertEquals("row s" + row + " versions " + rowCommitted.keySet(), ver, + rowCommitted.size()); + for (int v = 1; v <= ver; v++) { + assertTrue("row s" + row + " version " + v + " has no successful update", + rowCommitted.containsKey(v)); + } + if (ver > 0) { + assertEquals(rowCommitted.get(ver), doc.getString("by").getValue()); + } + // Every row returned by a no-op was committed + for (Map.Entry entry : returned.get(row).entrySet()) { + int v = entry.getKey(); + String expectedWriter = v == 0 ? "" : rowCommitted.get(v); + assertEquals("row s" + row + " version " + v + " returned by a no-op was not committed", + expectedWriter, entry.getValue()); + } + } + } + } +} diff --git a/phoenix-core/src/test/java/org/apache/phoenix/query/BaseTest.java b/phoenix-core/src/test/java/org/apache/phoenix/query/BaseTest.java index fe438ff0f25..11e0b120315 100644 --- a/phoenix-core/src/test/java/org/apache/phoenix/query/BaseTest.java +++ b/phoenix-core/src/test/java/org/apache/phoenix/query/BaseTest.java @@ -597,7 +597,9 @@ public static Configuration setUpConfigForMiniCluster(Configuration conf, conf.setInt("hbase.catalogjanitor.interval", 5000); conf.setInt(QueryServices.TASK_HANDLING_INTERVAL_MS_ATTRIB, 10000); conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2); - conf.setInt(NUM_CONCURRENT_INDEX_WRITER_THREADS_CONF_KEY, 1); + if (overrideProps.get(NUM_CONCURRENT_INDEX_WRITER_THREADS_CONF_KEY) == null) { + conf.setInt(NUM_CONCURRENT_INDEX_WRITER_THREADS_CONF_KEY, 1); + } conf.setInt(GLOBAL_INDEX_ROW_AGE_THRESHOLD_TO_DELETE_MS_ATTRIB, 0); conf.set(IndexManagementUtil.WAL_EDIT_CODEC_CLASS_KEY, "org.apache.hadoop.hbase.regionserver.wal.IndexedWALEditCodec");