/** * 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()); }
/** * 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()); }
@Override public void encode(OutputStream out, TransactionSnapshot snapshot) { try { BinaryEncoder encoder = new BinaryEncoder(out); encoder.writeLong(snapshot.getTimestamp()); encoder.writeLong(snapshot.getReadPointer()); encoder.writeLong(snapshot.getWritePointer()); encodeInvalid(encoder, snapshot.getInvalid()); encodeInProgress(encoder, snapshot.getInProgress()); encodeChangeSets(encoder, snapshot.getCommittingChangeSets()); encodeChangeSets(encoder, snapshot.getCommittedChangeSets()); } catch (IOException e) { LOG.error("Unable to serialize transaction state: ", e); throw Throwables.propagate(e); } }
@Override public void encode(OutputStream out, TransactionSnapshot snapshot) { try { BinaryEncoder encoder = new BinaryEncoder(out); encoder.writeLong(snapshot.getTimestamp()); encoder.writeLong(snapshot.getReadPointer()); encoder.writeLong(snapshot.getWritePointer()); encodeInvalid(encoder, snapshot.getInvalid()); encodeInProgress(encoder, snapshot.getInProgress()); encodeChangeSets(encoder, snapshot.getCommittingChangeSets()); encodeChangeSets(encoder, snapshot.getCommittedChangeSets()); } catch (IOException e) { LOG.error("Unable to serialize transaction state: ", e); throw Throwables.propagate(e); } }
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(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 {
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);
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();
@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()); }
txm1.getCurrentState().getInProgress().keySet()); try { txm1.truncateInvalidTxBefore(timeBeforeTx5); txm1.getCurrentState().getInProgress().keySet()); txm2.getCurrentState().getInvalid()); Assert.assertEquals(ImmutableSet.of(tx3.getTransactionId(), tx4.getTransactionId()), txm2.getCurrentState().getInProgress().keySet()); } finally { txm1.stopAndWait();
Assert.assertEquals(0, snapshot1.getInProgress().size()); Assert.assertEquals(0, snapshot1.getCommittedChangeSets().size()); Assert.assertEquals(0, snapshot1.getCommittingChangeSets().size());
txm1.getCurrentState().getInProgress().keySet()); Assert.assertFalse(txm1.truncateInvalidTx(ImmutableSet.of(tx3.getTransactionId(), tx4.getTransactionId()))); txm1.getCurrentState().getInProgress().keySet()); txm2.getCurrentState().getInvalid()); Assert.assertEquals(ImmutableSet.of(tx3.getTransactionId(), tx4.getTransactionId()), txm2.getCurrentState().getInProgress().keySet()); } finally { txm1.stopAndWait();
Assert.assertEquals(0, snapshot1.getInProgress().size()); Assert.assertEquals(0, snapshot1.getCommittedChangeSets().size()); Assert.assertEquals(0, snapshot1.getCommittedChangeSets().size());
Assert.assertEquals(0, snapshot.getInProgress().size()); Assert.assertEquals(0, snapshot.getCommittedChangeSets().size()); Assert.assertEquals(0, snapshot.getCommittedChangeSets().size());