+ private <T> FutureCallback<T> switchState(final State newState) {
+ @SuppressWarnings("unchecked")
+ final FutureCallback<T> ret = (FutureCallback<T>) this.callback;
+ this.callback = null;
+ LOG.debug("Transaction {} changing state from {} to {}", transactionId, state, newState);
+ this.state = newState;
+ return ret;
+ }
+
+ void setNewCandidate(final DataTreeCandidateTip dataTreeCandidate) {
+ checkState(State.PRE_COMMIT_COMPLETE);
+ this.candidate = Verify.verifyNotNull(dataTreeCandidate);
+ }
+
+ void successfulCanCommit() {
+ switchState(State.CAN_COMMIT_COMPLETE).onSuccess(null);
+ }
+
+ void failedCanCommit(final Exception cause) {
+ switchState(State.FAILED).onFailure(cause);
+ }
+
+ /**
+ * Run user-defined canCommit and preCommit hooks. We want to run these before we initiate persistence so that
+ * any failure to validate is propagated before we record the transaction.
+ *
+ * @param dataTreeCandidate {@link DataTreeCandidate} under consideration
+ * @throws ExecutionException if the operation fails
+ * @throws TimeoutException if the operation times out
+ */
+ // FIXME: this should be asynchronous
+ void userPreCommit(final DataTreeCandidate dataTreeCandidate) throws ExecutionException, TimeoutException {
+ userCohorts.reset();
+ userCohorts.canCommit(dataTreeCandidate);
+ userCohorts.preCommit();
+ }
+
+ void successfulPreCommit(final DataTreeCandidateTip dataTreeCandidate) {
+ LOG.trace("Transaction {} prepared candidate {}", transaction, dataTreeCandidate);
+ this.candidate = Verify.verifyNotNull(dataTreeCandidate);
+ switchState(State.PRE_COMMIT_COMPLETE).onSuccess(dataTreeCandidate);
+ }
+
+ void failedPreCommit(final Exception cause) {
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("Transaction {} failed to prepare", transaction, cause);
+ } else {
+ LOG.error("Transaction {} failed to prepare", transactionId, cause);
+ }
+
+ userCohorts.abort();
+ switchState(State.FAILED).onFailure(cause);
+ }
+
+ void successfulCommit(final UnsignedLong journalIndex) {