TransactionStateStorage storage = null;
TransactionStateStorage storage2 = null;
TransactionStateStorage storage3 = null;
try {
storage = getStorage(conf);
TransactionManager txManager = new TransactionManager
(conf, storage, new TxMetricsCollector());
txManager.startAndWait();
// TODO: replace with new persistence tests
final byte[] a = { 'a' };
final byte[] b = { 'b' };
// start a tx1, add a change A and commit
Transaction tx1 = txManager.startShort();
Assert.assertTrue(txManager.canCommit(tx1, Collections.singleton(a)));
Assert.assertTrue(txManager.commit(tx1));
// start a tx2 and add a change B
Transaction tx2 = txManager.startShort();
Assert.assertTrue(txManager.canCommit(tx2, Collections.singleton(b)));
// start a tx3
Transaction tx3 = txManager.startShort();
// restart
txManager.stopAndWait();
TransactionSnapshot origState = txManager.getCurrentState();
LOG.info("Orig state: " + origState);
Thread.sleep(100);
// starts a new tx manager
storage2 = getStorage(conf);
txManager = new TransactionManager(conf, storage2, new TxMetricsCollector());
txManager.startAndWait();
// check that the reloaded state matches the old
TransactionSnapshot newState = txManager.getCurrentState();
LOG.info("New state: " + newState);
assertEquals(origState, newState);
// commit tx2
Assert.assertTrue(txManager.commit(tx2));
// start another transaction, must be greater than tx3
Transaction tx4 = txManager.startShort();
Assert.assertTrue(tx4.getWritePointer() > tx3.getWritePointer());
// tx1 must be visble from tx2, but tx3 and tx4 must not
Assert.assertTrue(tx2.isVisible(tx1.getWritePointer()));
Assert.assertFalse(tx2.isVisible(tx3.getWritePointer()));
Assert.assertFalse(tx2.isVisible(tx4.getWritePointer()));
// add same change for tx3
Assert.assertFalse(txManager.canCommit(tx3, Collections.singleton(b)));
// check visibility with new xaction
Transaction tx5 = txManager.startShort();
Assert.assertTrue(tx5.isVisible(tx1.getWritePointer()));
Assert.assertTrue(tx5.isVisible(tx2.getWritePointer()));
Assert.assertFalse(tx5.isVisible(tx3.getWritePointer()));
Assert.assertFalse(tx5.isVisible(tx4.getWritePointer()));
// can commit tx3?
txManager.abort(tx3);
txManager.abort(tx4);
txManager.abort(tx5);
// start new tx and verify its exclude list is empty
Transaction tx6 = txManager.startShort();
Assert.assertFalse(tx6.hasExcludes());
txManager.abort(tx6);
// now start 5 x claim size transactions
Transaction tx = txManager.startShort();
for (int i = 1; i < 50; i++) {
tx = txManager.startShort();
}
origState = txManager.getCurrentState();
Thread.sleep(100);
// simulate crash by starting a new tx manager without a stopAndWait
storage3 = getStorage(conf);
txManager = new TransactionManager(conf, storage3, new TxMetricsCollector());
txManager.startAndWait();
// verify state again matches (this time should include WAL replay)
newState = txManager.getCurrentState();
assertEquals(origState, newState);
// get a new transaction and verify it is greater
Transaction txAfter = txManager.startShort();
Assert.assertTrue(txAfter.getWritePointer() > tx.getWritePointer());
} finally {
if (storage != null) {
storage.stopAndWait();
}