+ /**
+ * Base class for representing logical state of this proxy. See individual instantiations and {@link SuccessorState}
+ * for details.
+ */
+ private static class State {
+ private final String string;
+
+ State(final String string) {
+ this.string = Preconditions.checkNotNull(string);
+ }
+
+ @Override
+ public final String toString() {
+ return string;
+ }
+ }
+
+ /**
+ * State class used when a successor has interfered. Contains coordinator latch, the successor and previous state.
+ * This is a temporary state introduced during reconnection process and is necessary for correct state hand-off
+ * between the old connection (potentially being accessed by the user) and the new connection (being cleaned up
+ * by the actor.
+ *
+ * <p>
+ * When a user operation encounters this state, it synchronizes on the it and wait until reconnection completes,
+ * at which point the request is routed to the successor transaction. This is a relatively heavy-weight solution
+ * to the problem of state transfer, but the user will observe it only if the race condition is hit.
+ */
+ private static final class SuccessorState extends State {
+ private final CountDownLatch latch = new CountDownLatch(1);
+ private AbstractProxyTransaction successor;
+ private State prevState;
+
+ SuccessorState() {
+ super("SUCCESSOR");
+ }
+
+ // Synchronize with succession process and return the successor
+ AbstractProxyTransaction await() {
+ try {
+ latch.await();
+ } catch (InterruptedException e) {
+ LOG.warn("Interrupted while waiting for latch of {}", successor);
+ throw Throwables.propagate(e);
+ }
+ return successor;
+ }
+
+ void finish() {
+ latch.countDown();
+ }
+
+ State getPrevState() {
+ return prevState;
+ }
+
+ void setPrevState(final State prevState) {
+ Verify.verify(this.prevState == null, "Attempted to set previous state to %s when we already have %s",
+ prevState, this.prevState);
+ this.prevState = Preconditions.checkNotNull(prevState);
+ }
+
+ // To be called from safe contexts, where successor is known to be completed
+ AbstractProxyTransaction getSuccessor() {
+ return Verify.verifyNotNull(successor);
+ }
+
+ void setSuccessor(final AbstractProxyTransaction successor) {
+ Verify.verify(this.successor == null, "Attempted to set successor to %s when we already have %s",
+ successor, this.successor);
+ this.successor = Preconditions.checkNotNull(successor);
+ }