Skip to content

Instantly share code, notes, and snippets.

@davengeo
Last active March 31, 2018 22:14
Show Gist options
  • Save davengeo/0ee4a328993c5ba9b87affd63929d810 to your computer and use it in GitHub Desktop.
Save davengeo/0ee4a328993c5ba9b87affd63929d810 to your computer and use it in GitHub Desktop.
//trivial means trivial
@Test
public void transactionInDlog() throws SystemException, IOException {
final Namespace namespace = Factories.namespace(NAMESPACE).get();
final DistributedLogManager managerOne = namespace.openLog("stream-1");
final DistributedLogManager managerTwo = namespace.openLog("stream-2");
managerOne.delete();
managerTwo.delete();
final LogWriter logWriterOne = managerOne.openLogWriter();
final LogWriter logWriterTwo = managerTwo.openLogWriter();
LogRecord controlOne = new LogRecord(1, "ONE".getBytes());
controlOne.setControl();
LogRecord recordOne = new LogRecord(1, "ONE".getBytes());
LogRecord controlTwo = new LogRecord(1, "TWO".getBytes());
controlTwo.setControl();
LogRecord recordTwo = new LogRecord(1, "TWO".getBytes());
final UserTransactionManager transactionManager = new UserTransactionManager();
try {
transactionManager.begin();
log.info("begin");
logWriterOne.write(controlOne);
logWriterTwo.write(controlTwo);
logWriterOne.flush();
logWriterTwo.flush();
transactionManager.commit();
log.info("commit");
logWriterOne.write(recordOne);
logWriterTwo.write(recordTwo);
logWriterOne.flush();
logWriterTwo.flush();
} catch (NotSupportedException | HeuristicMixedException | HeuristicRollbackException | IOException | RollbackException e) {
log.warn("rollback after {}", e.getMessage());
transactionManager.rollback();
logWriterOne.abort();
logWriterTwo.abort();
}
logWriterOne.close();
logWriterTwo.close();
transactionManager.close();
final LogRecordWithDLSN lastRecordOne = managerOne.getLastLogRecord();
final LogRecordWithDLSN lastRecordTwo = managerTwo.getLastLogRecord();
assertThat(lastRecordOne.getPayload()).isEqualTo("ONE".getBytes());
assertThat(lastRecordTwo.getPayload()).isEqualTo("TWO".getBytes());
assertThat(Try.of(managerOne::getLogRecordCount).get()).isEqualTo(1L);
assertThat(Try.of(managerTwo::getLogRecordCount).get()).isEqualTo(1L);
}
@Test
public void transactionInDlogFailing() throws SystemException, IOException {
final Namespace namespace = Factories.namespace(NAMESPACE).get();
final DistributedLogManager managerOne = namespace.openLog("stream-1");
final DistributedLogManager managerTwo = namespace.openLog("stream-2");
managerOne.delete();
managerTwo.delete();
final LogWriter logWriterOne = managerOne.openLogWriter();
final LogWriter logWriterTwo = managerTwo.openLogWriter();
LogRecord controlOne = new LogRecord(1, "ONE".getBytes());
controlOne.setControl();
LogRecord recordOne = new LogRecord(1, "ONE".getBytes());
LogRecord controlTwo = new LogRecord(1, "TWO".getBytes());
controlTwo.setControl();
LogRecord recordTwo = new LogRecord(1, "TWO".getBytes());
final UserTransactionManager transactionManager = new UserTransactionManager();
boolean fail = true;
try {
transactionManager.begin();
log.info("****************************************** begin");
logWriterOne.write(controlOne);
logWriterTwo.write(controlTwo);
logWriterOne.flush();
logWriterTwo.flush();
if (fail) {
throw new IOException("to fail");
}
transactionManager.commit();
log.info("****************************************** commit");
logWriterOne.write(recordOne);
logWriterTwo.write(recordTwo);
logWriterOne.flush();
logWriterTwo.flush();
} catch (Exception e) {
transactionManager.rollback();
logWriterOne.abort();
logWriterTwo.abort();
log.info("****************************************** rollback");
}
log.warn("{}", managerOne.getLogRecordCount());
logWriterOne.close();
logWriterTwo.close();
transactionManager.close();
assertThat(Try.of(managerOne::getLastLogRecord).isFailure()).isTrue();
assertThat(Try.of(managerTwo::getLastLogRecord).isFailure()).isTrue();
assertThat(Try.of(managerOne::getLogRecordCount).get()).isZero();
assertThat(Try.of(managerTwo::getLogRecordCount).get()).isZero();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment