*/
package org.opendaylight.controller.cluster.datastore;
-import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.primitives.UnsignedLong;
import com.google.common.util.concurrent.FutureCallback;
import java.util.Collection;
+import java.util.Optional;
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;
import org.opendaylight.controller.cluster.access.commands.AbortLocalTransactionRequest;
}
}
+ /**
+ * Retired state, needed to catch and suppress callbacks after we have removed associated state.
+ */
+ private static final class Retired extends State {
+ private final String prevStateString;
+
+ Retired(final State prevState) {
+ prevStateString = prevState.toString();
+ }
+
+ @Override
+ public String toString() {
+ return "RETIRED (in " + prevStateString + ")";
+ }
+ }
+
private static final Logger LOG = LoggerFactory.getLogger(FrontendReadWriteTransaction.class);
private static final State ABORTED = new State() {
@Override
}
}
+ @Override
+ void retire() {
+ state = new Retired(state);
+ }
+
private void handleTransactionPreCommit(final TransactionPreCommitRequest request,
final RequestEnvelope envelope, final long now) throws RequestException {
throwIfFailed();
ready.readyCohort.preCommit(new FutureCallback<DataTreeCandidate>() {
@Override
public void onSuccess(final DataTreeCandidate result) {
- LOG.debug("{}: Transaction {} completed preCommit", persistenceId(), getIdentifier());
- recordAndSendSuccess(envelope, now, new TransactionPreCommitSuccess(getIdentifier(),
- request.getSequence()));
- ready.stage = CommitStage.PRE_COMMIT_COMPLETE;
+ successfulPreCommit(envelope, now);
}
@Override
case READY:
throw new IllegalStateException("Attempted to preCommit in stage " + ready.stage);
default:
- throw new IllegalStateException("Unhandled commit stage " + ready.stage);
+ throwUnhandledCommitStage(ready);
+ }
+ }
+
+ void successfulPreCommit(final RequestEnvelope envelope, final long startTime) {
+ if (state instanceof Retired) {
+ LOG.debug("{}: Suppressing successful preCommit of retired transaction {}", persistenceId(),
+ getIdentifier());
+ return;
}
+
+ final Ready ready = checkReady();
+ LOG.debug("{}: Transaction {} completed preCommit", persistenceId(), getIdentifier());
+ recordAndSendSuccess(envelope, startTime, new TransactionPreCommitSuccess(getIdentifier(),
+ envelope.getMessage().getSequence()));
+ ready.stage = CommitStage.PRE_COMMIT_COMPLETE;
}
- private void failTransaction(final RequestEnvelope envelope, final long now, final RuntimeRequestException cause) {
+ void failTransaction(final RequestEnvelope envelope, final long now, final RuntimeRequestException cause) {
+ if (state instanceof Retired) {
+ LOG.debug("{}: Suppressing failure of retired transaction {}", persistenceId(), getIdentifier(), cause);
+ return;
+ }
+
recordAndSendFailure(envelope, now, cause);
state = new Failed(cause);
LOG.debug("{}: Transaction {} failed", persistenceId(), getIdentifier(), cause);
case READY:
throw new IllegalStateException("Attempted to doCommit in stage " + ready.stage);
default:
- throw new IllegalStateException("Unhandled commit stage " + ready.stage);
+ throwUnhandledCommitStage(ready);
}
}
checkReady().readyCohort.canCommit(new FutureCallback<Void>() {
@Override
public void onSuccess(final Void result) {
- recordAndSendSuccess(envelope, now, new TransactionCanCommitSuccess(getIdentifier(),
- envelope.getMessage().getSequence()));
- ready.stage = CommitStage.CAN_COMMIT_COMPLETE;
- LOG.debug("{}: Transaction {} completed canCommit", persistenceId(), getIdentifier());
+ successfulCanCommit(envelope, now);
}
@Override
case PRE_COMMIT_PENDING:
throw new IllegalStateException("Attempted to canCommit in stage " + ready.stage);
default:
- throw new IllegalStateException("Unhandled commit stage " + ready.stage);
+ throwUnhandledCommitStage(ready);
+ }
+ }
+
+ void successfulCanCommit(final RequestEnvelope envelope, final long startTime) {
+ if (state instanceof Retired) {
+ LOG.debug("{}: Suppressing successful canCommit of retired transaction {}", persistenceId(),
+ getIdentifier());
+ return;
}
+
+ final Ready ready = checkReady();
+ recordAndSendSuccess(envelope, startTime, new TransactionCanCommitSuccess(getIdentifier(),
+ envelope.getMessage().getSequence()));
+ ready.stage = CommitStage.CAN_COMMIT_COMPLETE;
+ LOG.debug("{}: Transaction {} completed canCommit", persistenceId(), getIdentifier());
}
private void directCommit(final RequestEnvelope envelope, final long now) throws RequestException {
});
break;
default:
- throw new IllegalStateException("Unhandled commit stage " + ready.stage);
+ throwUnhandledCommitStage(ready);
}
}
void successfulDirectCanCommit(final RequestEnvelope envelope, final long startTime) {
+ if (state instanceof Retired) {
+ LOG.debug("{}: Suppressing direct canCommit of retired transaction {}", persistenceId(), getIdentifier());
+ return;
+ }
+
final Ready ready = checkReady();
ready.stage = CommitStage.PRE_COMMIT_PENDING;
LOG.debug("{}: Transaction {} initiating direct preCommit", persistenceId(), getIdentifier());
}
void successfulDirectPreCommit(final RequestEnvelope envelope, final long startTime) {
+ if (state instanceof Retired) {
+ LOG.debug("{}: Suppressing direct commit of retired transaction {}", persistenceId(), getIdentifier());
+ return;
+ }
+
final Ready ready = checkReady();
ready.stage = CommitStage.COMMIT_PENDING;
LOG.debug("{}: Transaction {} initiating direct commit", persistenceId(), getIdentifier());
}
void successfulCommit(final RequestEnvelope envelope, final long startTime) {
+ if (state instanceof Retired) {
+ LOG.debug("{}: Suppressing commit response on retired transaction {}", persistenceId(), getIdentifier());
+ return;
+ }
+
recordAndSendSuccess(envelope, startTime, new TransactionCommitSuccess(getIdentifier(),
envelope.getMessage().getSequence()));
state = COMMITTED;
if (optFailure.isPresent()) {
state = new Ready(history().createFailedCohort(getIdentifier(), sealedModification, optFailure.get()));
} else {
- state = new Ready(history().createReadyCohort(getIdentifier(), sealedModification));
+ state = new Ready(history().createReadyCohort(getIdentifier(), sealedModification,
+ java.util.Optional.empty()));
}
if (request.isCoordinated()) {
}
}
- private ExistsTransactionSuccess handleExistsTransaction(final ExistsTransactionRequest request)
- throws RequestException {
+ private ExistsTransactionSuccess handleExistsTransaction(final ExistsTransactionRequest request) {
final Optional<NormalizedNode<?, ?>> data = checkOpen().getSnapshot().readNode(request.getPath());
return recordSuccess(request.getSequence(), new ExistsTransactionSuccess(getIdentifier(), request.getSequence(),
data.isPresent()));
}
- private ReadTransactionSuccess handleReadTransaction(final ReadTransactionRequest request)
- throws RequestException {
+ private ReadTransactionSuccess handleReadTransaction(final ReadTransactionRequest request) {
final Optional<NormalizedNode<?, ?>> data = checkOpen().getSnapshot().readNode(request.getPath());
return recordSuccess(request.getSequence(), new ReadTransactionSuccess(getIdentifier(), request.getSequence(),
data));
}
}
- private @Nullable TransactionSuccess<?> handleModifyTransaction(final ModifyTransactionRequest request,
+ @Nullable
+ private TransactionSuccess<?> handleModifyTransaction(final ModifyTransactionRequest request,
final RequestEnvelope envelope, final long now) throws RequestException {
// We need to examine the persistence protocol first to see if this is an idempotent request. If there is no
// protocol, there is nothing for us to do.
}
applyModifications(modifications);
- state = new Ready(checkOpen().ready());
+ state = new Ready(checkOpen().ready(java.util.Optional.empty()));
LOG.debug("{}: transitioned {} to ready", persistenceId(), getIdentifier());
}
state);
return ((Sealed) state).sealedModification;
}
+
+ private static void throwUnhandledCommitStage(final Ready ready) {
+ throw new IllegalStateException("Unhandled commit stage " + ready.stage);
+ }
}