+ }
+
+ /**
+ * Notify the base logic that a previously-submitted transaction has been committed successfully.
+ *
+ * @param transaction Transaction which completed successfully.
+ */
+ synchronized void onTransactionCommited(final InmemoryDOMDataTreeShardWriteTransaction transaction) {
+ // If the committed transaction was the one we allocated last,
+ // we clear it and the ready snapshot, so the next transaction
+ // allocated refers to the data tree directly.
+ final State localState = state;
+ LOG.debug("Transaction {} commit done, current state {}", transaction.getIdentifier(), localState);
+
+ if (!(localState instanceof Allocated)) {
+ // This can legally happen if the chain is shut down before the transaction was committed
+ // by the backend.
+ LOG.debug("Ignoring successful transaction {} in state {}", transaction, localState);
+ return;
+ }
+
+ final Allocated allocated = (Allocated) localState;
+ final InmemoryDOMDataTreeShardWriteTransaction tx = allocated.getTransaction();
+ if (!tx.equals(transaction)) {
+ LOG.debug("Ignoring non-latest successful transaction {} in state {}", transaction, allocated);
+ return;
+ }
+
+ if (!STATE_UPDATER.compareAndSet(this, localState, idleState)) {
+ LOG.debug("Producer {} has already transitioned from {} to {}, not making it idle", this,
+ localState, state);
+ }
+ }
+
+ synchronized void transactionAborted(final InmemoryDOMDataTreeShardWriteTransaction tx) {
+ final State localState = state;
+ if (localState instanceof Allocated) {
+ final Allocated allocated = (Allocated)localState;
+ if (allocated.getTransaction().equals(tx)) {
+ final boolean success = STATE_UPDATER.compareAndSet(this, localState, idleState);
+ if (!success) {
+ LOG.warn("Transaction {} aborted, but producer {} state already transitioned from {} to {}",
+ tx, this, localState, state);
+ }
+ }
+ }
+ }
+
+
+ private Entry<State, DataTreeSnapshot> getSnapshot(String transactionId) {
+ final State localState = state;
+ return new SimpleEntry<>(localState, localState.getSnapshot(transactionId));
+ }
+
+ private boolean recordTransaction(final State expected,
+ final InmemoryDOMDataTreeShardWriteTransaction transaction) {
+ final State state = new Allocated(transaction);
+ return STATE_UPDATER.compareAndSet(this, expected, state);
+ }
+
+ private String nextIdentifier() {
+ return "INMEMORY-SHARD-TX-" + COUNTER.getAndIncrement();
+
+ }
+
+ DataTreeSnapshot takeSnapshot() {
+ return parentShard.takeSnapshot();