return new TransactionSnapshot(snapshotTime, readPointer, writePointer, invalidCopy, inProgressCopy, committingCopy, committedCopy);
/** * Restore the initial in-memory transaction state from a snapshot. */ private void restoreSnapshot(TransactionSnapshot snapshot) { LOG.info("Restoring transaction state from snapshot at " + snapshot.getTimestamp()); Preconditions.checkState(lastSnapshotTime == 0, "lastSnapshotTime has been set!"); Preconditions.checkState(readPointer == 0, "readPointer has been set!"); Preconditions.checkState(lastWritePointer == 0, "lastWritePointer has been set!"); Preconditions.checkState(invalid.isEmpty(), "invalid list should be empty!"); Preconditions.checkState(inProgress.isEmpty(), "inProgress map should be empty!"); Preconditions.checkState(committingChangeSets.isEmpty(), "committingChangeSets should be empty!"); Preconditions.checkState(committedChangeSets.isEmpty(), "committedChangeSets should be empty!"); LOG.info("Restoring snapshot of state: " + snapshot); lastSnapshotTime = snapshot.getTimestamp(); readPointer = snapshot.getReadPointer(); lastWritePointer = snapshot.getWritePointer(); invalid.addAll(snapshot.getInvalid()); inProgress.putAll(txnBackwardsCompatCheck(defaultLongTimeout, longTimeoutTolerance, snapshot.getInProgress())); committingChangeSets.putAll(snapshot.getCommittingChangeSets()); committedChangeSets.putAll(snapshot.getCommittedChangeSets()); }
public synchronized TransactionSnapshot getCurrentState() { return TransactionSnapshot.copyFrom(System.currentTimeMillis(), readPointer, lastWritePointer, invalid, inProgress, committingChangeSets, committedChangeSets); }
private static void printSnapshot(TransactionSnapshot snapshot) { Date snapshotDate = new Date(snapshot.getTimestamp()); System.out.println("TransactionSnapshot at " + snapshotDate.toString()); System.out.println("\t" + snapshot.toString()); }
tShort, new TransactionManager.InProgressTx(readPtr, now + 1000, TransactionType.SHORT))); TransactionSnapshot snapshot = new TransactionSnapshot(now, readPtr, nowWritePointer, assertTransactionVisibilityStateEquals(snapshot2, minTxSnapshot); assertEquals(snapshot.getReadPointer(), snapshot2.getReadPointer()); assertEquals(snapshot.getWritePointer(), snapshot2.getWritePointer()); assertEquals(snapshot.getInvalid(), snapshot2.getInvalid()); assertNotEquals(snapshot.getInProgress(), snapshot2.getInProgress()); assertEquals(snapshot.getCommittingChangeSets(), snapshot2.getCommittingChangeSets()); assertEquals(snapshot.getCommittedChangeSets(), snapshot2.getCommittedChangeSets()); TxConstants.Manager.DEFAULT_TX_LONG_TIMEOUT, 10000L, snapshot2.getInProgress()); assertEquals(snapshot.getInProgress(), fixedInProgress); assertEquals(snapshot, snapshot2);
@Test public void testResetState() throws Exception { // have tx in progress, committing and committed then reset, // get the last snapshot and see that it is empty TransactionSystemClient client = getClient(); TransactionStateStorage stateStorage = getStateStorage(); Transaction tx1 = client.startShort(); Transaction tx2 = client.startShort(); client.canCommit(tx1, asList(C1, C2)); client.commit(tx1); client.canCommit(tx2, asList(C3, C4)); Transaction txPreReset = client.startShort(); long currentTs = System.currentTimeMillis(); client.resetState(); TransactionSnapshot snapshot = stateStorage.getLatestSnapshot(); Assert.assertTrue(snapshot.getTimestamp() >= currentTs); Assert.assertEquals(0, snapshot.getInvalid().size()); Assert.assertEquals(0, snapshot.getInProgress().size()); Assert.assertEquals(0, snapshot.getCommittingChangeSets().size()); Assert.assertEquals(0, snapshot.getCommittedChangeSets().size()); // confirm that transaction IDs are not reset Transaction txPostReset = client.startShort(); Assert.assertTrue("New tx ID should be greater than last ID before reset", txPostReset.getTransactionId() > txPreReset.getTransactionId()); }
Assert.assertEquals(ImmutableList.of(wp1, wp2, wp3), snapshot1.getInvalid()); Assert.assertEquals(0, snapshot1.getInProgress().size()); Assert.assertEquals(0, snapshot1.getCommittedChangeSets().size()); Assert.assertEquals(0, snapshot1.getCommittedChangeSets().size()); } finally { txm.stopAndWait();
Map<Long, Set<ChangeId>> committing = Maps.newHashMap(); Map<Long, Set<ChangeId>> committed = Maps.newHashMap(); TransactionSnapshot snapshot = new TransactionSnapshot(time2, 0, wp2, invalid, inProgress, committing, committed); long time3 = time1 + 200; Assert.assertEquals(ImmutableSortedSet.of(wp1, wp2, wp3, wp4), snapshot1.getInProgress().keySet()); verifyInProgress(snapshot1.getInProgress().get(wp1), TransactionType.LONG, time1 + longTimeout); verifyInProgress(snapshot1.getInProgress().get(wp2), TransactionType.SHORT, time2 + 1000); verifyInProgress(snapshot1.getInProgress().get(wp3), TransactionType.LONG, time3 + longTimeout); verifyInProgress(snapshot1.getInProgress().get(wp4), TransactionType.SHORT, time4 + 1000); Assert.assertEquals(0, snapshot1.getInvalid().size()); } finally { txm.stopAndWait();
@Override public long deleteOldSnapshots(int numberToKeep) throws IOException { // always only keep the last snapshot return lastSnapshot.getTimestamp(); }
ImmutableList.of(tx1.getTransactionId(), tx2.getTransactionId(), tx5.getTransactionId(), tx6.getTransactionId()), txm1.getCurrentState().getInvalid() ); txm1.getCurrentState().getInvalid()); txm1.getCurrentState().getInProgress().keySet()); try { txm1.truncateInvalidTxBefore(timeBeforeTx5); txm1.getCurrentState().getInProgress().keySet()); txm1.getCurrentState().getInvalid()); txm2.startAndWait(); Assert.assertEquals(ImmutableList.of(tx5.getTransactionId(), tx6.getTransactionId()), txm2.getCurrentState().getInvalid()); Assert.assertEquals(ImmutableSet.of(tx3.getTransactionId(), tx4.getTransactionId()), txm2.getCurrentState().getInProgress().keySet()); } finally { txm1.stopAndWait();
assertTransactionVisibilityStateEquals(snapshot, txVisibilityState); assertNotNull(snapshot); assertEquals(1, snapshot.getInProgress().size()); Map.Entry<Long, TransactionManager.InProgressTx> entry = snapshot.getInProgress().entrySet().iterator().next(); assertNull(entry.getValue().getType()); txStorage.stopAndWait(); assertEquals(1, snapshot2.getInProgress().size()); Map.Entry<Long, TransactionManager.InProgressTx> inProgressTx = snapshot2.getInProgress().entrySet().iterator().next(); assertEquals(TransactionType.LONG, inProgressTx.getValue().getType()); assertEquals(snapshot2.getInProgress(), snapshot3.getInProgress()); assertEquals(snapshot2, snapshot3); txStorage2.stopAndWait();
Assert.assertEquals(tx3.getTransactionId(), (long) txm.getCurrentState().getInvalid().iterator().next()); Assert.assertEquals(1, txm.getExcludedListSize()); } finally {
@Override public InternalScanner preFlush(ObserverContext<RegionCoprocessorEnvironment> e, Store store, InternalScanner scanner) throws IOException { TransactionSnapshot snapshot = cache.getLatestState(); if (snapshot != null) { return new IncrementSummingScanner(region, BATCH_UNLIMITED, scanner, snapshot.getVisibilityUpperBound()); } return new IncrementSummingScanner(region, BATCH_UNLIMITED, scanner); }
Assert.assertEquals(ImmutableList.of(wp1, wp2), snapshot1.getInvalid()); Assert.assertEquals(0, snapshot1.getInProgress().size()); Assert.assertEquals(0, snapshot1.getCommittedChangeSets().size()); Assert.assertEquals(0, snapshot1.getCommittingChangeSets().size()); } finally { txm.stopAndWait();
Assert.assertEquals(ImmutableList.of(wp4), snapshot.getInvalid()); Assert.assertEquals(0, snapshot.getInProgress().size()); Assert.assertEquals(0, snapshot.getCommittedChangeSets().size()); Assert.assertEquals(0, snapshot.getCommittedChangeSets().size()); } finally { txm.stopAndWait();
@Override public void writeSnapshot(TransactionSnapshot snapshot) throws IOException { // create a temporary file, and save the snapshot Path snapshotTmpFile = new Path(snapshotDir, TMP_SNAPSHOT_FILE_PREFIX + snapshot.getTimestamp()); LOG.debug("Writing snapshot to temporary file {}", snapshotTmpFile); FSDataOutputStream out = fs.create(snapshotTmpFile, false, BUFFER_SIZE); // encode the snapshot and stream the serialized version to the file try { codecProvider.encode(out, snapshot); } finally { out.close(); } // move the temporary file into place with the correct filename Path finalFile = new Path(snapshotDir, SNAPSHOT_FILE_PREFIX + snapshot.getTimestamp()); fs.rename(snapshotTmpFile, finalFile); LOG.debug("Completed snapshot to file {}", finalFile); }
ImmutableList.of(tx1.getTransactionId(), tx2.getTransactionId(), tx5.getTransactionId(), tx6.getTransactionId()), txm1.getCurrentState().getInvalid() ); txm1.getCurrentState().getInvalid()); txm1.getCurrentState().getInProgress().keySet()); Assert.assertFalse(txm1.truncateInvalidTx(ImmutableSet.of(tx3.getTransactionId(), tx4.getTransactionId()))); txm1.getCurrentState().getInProgress().keySet()); txm1.getCurrentState().getInvalid()); txm2.startAndWait(); Assert.assertEquals(ImmutableList.of(tx2.getTransactionId(), tx5.getTransactionId()), txm2.getCurrentState().getInvalid()); Assert.assertEquals(ImmutableSet.of(tx3.getTransactionId(), tx4.getTransactionId()), txm2.getCurrentState().getInProgress().keySet()); } finally { txm1.stopAndWait();
private static void printSnapshot(TransactionSnapshot snapshot) { Date snapshotDate = new Date(snapshot.getTimestamp()); System.out.println("TransactionSnapshot at " + snapshotDate.toString()); System.out.println("\t" + snapshot.toString()); }
assertTransactionVisibilityStateEquals(snapshot, txVisibilityState); Map<Long, TransactionManager.InProgressTx> inProgress = snapshot.getInProgress(); Assert.assertEquals(1, inProgress.size()); inProgress = snapshot.getInProgress(); Assert.assertEquals(1, inProgress.size()); Assert.assertTrue(snapshot.getInProgress().isEmpty()); txStorage2.stopAndWait();
@Override public InternalScanner preCompact(ObserverContext<RegionCoprocessorEnvironment> e, Store store, InternalScanner scanner, ScanType scanType) throws IOException { TransactionSnapshot snapshot = cache.getLatestState(); if (snapshot != null) { return new IncrementSummingScanner(region, BATCH_UNLIMITED, scanner, snapshot.getVisibilityUpperBound()); } return new IncrementSummingScanner(region, BATCH_UNLIMITED, scanner); }