2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.datastore;
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.primitives.UnsignedLong;
14 import com.google.common.util.concurrent.FutureCallback;
15 import java.util.Collection;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.controller.cluster.access.commands.AbortLocalTransactionRequest;
19 import org.opendaylight.controller.cluster.access.commands.CommitLocalTransactionRequest;
20 import org.opendaylight.controller.cluster.access.commands.ExistsTransactionRequest;
21 import org.opendaylight.controller.cluster.access.commands.ExistsTransactionSuccess;
22 import org.opendaylight.controller.cluster.access.commands.ModifyTransactionRequest;
23 import org.opendaylight.controller.cluster.access.commands.ModifyTransactionSuccess;
24 import org.opendaylight.controller.cluster.access.commands.PersistenceProtocol;
25 import org.opendaylight.controller.cluster.access.commands.ReadTransactionRequest;
26 import org.opendaylight.controller.cluster.access.commands.ReadTransactionSuccess;
27 import org.opendaylight.controller.cluster.access.commands.TransactionAbortRequest;
28 import org.opendaylight.controller.cluster.access.commands.TransactionAbortSuccess;
29 import org.opendaylight.controller.cluster.access.commands.TransactionCanCommitSuccess;
30 import org.opendaylight.controller.cluster.access.commands.TransactionCommitSuccess;
31 import org.opendaylight.controller.cluster.access.commands.TransactionDelete;
32 import org.opendaylight.controller.cluster.access.commands.TransactionDoCommitRequest;
33 import org.opendaylight.controller.cluster.access.commands.TransactionMerge;
34 import org.opendaylight.controller.cluster.access.commands.TransactionModification;
35 import org.opendaylight.controller.cluster.access.commands.TransactionPreCommitRequest;
36 import org.opendaylight.controller.cluster.access.commands.TransactionPreCommitSuccess;
37 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
38 import org.opendaylight.controller.cluster.access.commands.TransactionSuccess;
39 import org.opendaylight.controller.cluster.access.commands.TransactionWrite;
40 import org.opendaylight.controller.cluster.access.concepts.RequestEnvelope;
41 import org.opendaylight.controller.cluster.access.concepts.RequestException;
42 import org.opendaylight.controller.cluster.access.concepts.RuntimeRequestException;
43 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
44 import org.opendaylight.controller.cluster.access.concepts.UnsupportedRequestException;
45 import org.opendaylight.yangtools.yang.common.Empty;
46 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
47 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
48 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
53 * Frontend read-write transaction state as observed by the shard leader. This class is NOT thread-safe.
55 * @author Robert Varga
57 final class FrontendReadWriteTransaction extends FrontendTransaction {
58 private enum CommitStage {
67 private abstract static class State {
69 public abstract String toString();
72 private static final class Failed extends State {
73 final RequestException cause;
75 Failed(final RequestException cause) {
76 this.cause = requireNonNull(cause);
80 public String toString() {
81 return "FAILED (" + cause.getMessage() + ")";
85 private static final class Open extends State {
86 final ReadWriteShardDataTreeTransaction openTransaction;
88 Open(final ReadWriteShardDataTreeTransaction openTransaction) {
89 this.openTransaction = requireNonNull(openTransaction);
93 public String toString() {
98 private static final class Ready extends State {
99 final ShardDataTreeCohort readyCohort;
102 Ready(final ShardDataTreeCohort readyCohort) {
103 this.readyCohort = requireNonNull(readyCohort);
104 stage = CommitStage.READY;
108 public String toString() {
109 return "READY (" + stage + ")";
113 private static final class Sealed extends State {
114 final DataTreeModification sealedModification;
116 Sealed(final DataTreeModification sealedModification) {
117 this.sealedModification = requireNonNull(sealedModification);
121 public String toString() {
127 * Retired state, needed to catch and suppress callbacks after we have removed associated state.
129 private static final class Retired extends State {
130 private final String prevStateString;
132 Retired(final State prevState) {
133 prevStateString = prevState.toString();
137 public String toString() {
138 return "RETIRED (in " + prevStateString + ")";
142 private static final Logger LOG = LoggerFactory.getLogger(FrontendReadWriteTransaction.class);
143 private static final State ABORTED = new State() {
145 public String toString() {
149 private static final State ABORTING = new State() {
151 public String toString() {
155 private static final State COMMITTED = new State() {
157 public String toString() {
164 private FrontendReadWriteTransaction(final AbstractFrontendHistory history, final TransactionIdentifier id,
165 final ReadWriteShardDataTreeTransaction transaction) {
167 state = new Open(transaction);
170 private FrontendReadWriteTransaction(final AbstractFrontendHistory history, final TransactionIdentifier id,
171 final DataTreeModification mod) {
173 state = new Sealed(mod);
176 static FrontendReadWriteTransaction createOpen(final AbstractFrontendHistory history,
177 final ReadWriteShardDataTreeTransaction transaction) {
178 return new FrontendReadWriteTransaction(history, transaction.getIdentifier(), transaction);
181 static FrontendReadWriteTransaction createReady(final AbstractFrontendHistory history,
182 final TransactionIdentifier id, final DataTreeModification mod) {
183 return new FrontendReadWriteTransaction(history, id, mod);
186 // Sequence has already been checked
188 TransactionSuccess<?> doHandleRequest(final TransactionRequest<?> request, final RequestEnvelope envelope,
189 final long now) throws RequestException {
190 if (request instanceof ModifyTransactionRequest) {
191 return handleModifyTransaction((ModifyTransactionRequest) request, envelope, now);
192 } else if (request instanceof CommitLocalTransactionRequest) {
193 handleCommitLocalTransaction((CommitLocalTransactionRequest) request, envelope, now);
195 } else if (request instanceof ExistsTransactionRequest) {
196 return handleExistsTransaction((ExistsTransactionRequest) request);
197 } else if (request instanceof ReadTransactionRequest) {
198 return handleReadTransaction((ReadTransactionRequest) request);
199 } else if (request instanceof TransactionPreCommitRequest) {
200 handleTransactionPreCommit((TransactionPreCommitRequest) request, envelope, now);
202 } else if (request instanceof TransactionDoCommitRequest) {
203 handleTransactionDoCommit((TransactionDoCommitRequest) request, envelope, now);
205 } else if (request instanceof TransactionAbortRequest) {
206 return handleTransactionAbort(request.getSequence(), envelope, now);
207 } else if (request instanceof AbortLocalTransactionRequest) {
208 handleLocalTransactionAbort(request.getSequence(), envelope, now);
211 LOG.warn("Rejecting unsupported request {}", request);
212 throw new UnsupportedRequestException(request);
218 state = new Retired(state);
221 private void handleTransactionPreCommit(final TransactionPreCommitRequest request,
222 final RequestEnvelope envelope, final long now) throws RequestException {
225 final Ready ready = checkReady();
226 switch (ready.stage) {
227 case PRE_COMMIT_PENDING:
228 LOG.debug("{}: Transaction {} is already preCommitting", persistenceId(), getIdentifier());
230 case CAN_COMMIT_COMPLETE:
231 ready.stage = CommitStage.PRE_COMMIT_PENDING;
232 LOG.debug("{}: Transaction {} initiating preCommit", persistenceId(), getIdentifier());
233 ready.readyCohort.preCommit(new FutureCallback<DataTreeCandidate>() {
235 public void onSuccess(final DataTreeCandidate result) {
236 successfulPreCommit(envelope, now);
240 public void onFailure(final Throwable failure) {
241 failTransaction(envelope, now, new RuntimeRequestException("Precommit failed", failure));
245 case CAN_COMMIT_PENDING:
247 case PRE_COMMIT_COMPLETE:
249 throw new IllegalStateException("Attempted to preCommit in stage " + ready.stage);
251 throwUnhandledCommitStage(ready);
255 void successfulPreCommit(final RequestEnvelope envelope, final long startTime) {
256 if (state instanceof Retired) {
257 LOG.debug("{}: Suppressing successful preCommit of retired transaction {}", persistenceId(),
262 final Ready ready = checkReady();
263 LOG.debug("{}: Transaction {} completed preCommit", persistenceId(), getIdentifier());
264 recordAndSendSuccess(envelope, startTime, new TransactionPreCommitSuccess(getIdentifier(),
265 envelope.getMessage().getSequence()));
266 ready.stage = CommitStage.PRE_COMMIT_COMPLETE;
269 void failTransaction(final RequestEnvelope envelope, final long now, final RuntimeRequestException cause) {
270 if (state instanceof Retired) {
271 LOG.debug("{}: Suppressing failure of retired transaction {}", persistenceId(), getIdentifier(), cause);
275 recordAndSendFailure(envelope, now, cause);
276 state = new Failed(cause);
277 LOG.debug("{}: Transaction {} failed", persistenceId(), getIdentifier(), cause);
280 private void handleTransactionDoCommit(final TransactionDoCommitRequest request, final RequestEnvelope envelope,
281 final long now) throws RequestException {
284 final Ready ready = checkReady();
285 switch (ready.stage) {
287 LOG.debug("{}: Transaction {} is already committing", persistenceId(), getIdentifier());
289 case PRE_COMMIT_COMPLETE:
290 ready.stage = CommitStage.COMMIT_PENDING;
291 LOG.debug("{}: Transaction {} initiating commit", persistenceId(), getIdentifier());
292 ready.readyCohort.commit(new FutureCallback<UnsignedLong>() {
294 public void onSuccess(final UnsignedLong result) {
295 successfulCommit(envelope, now);
299 public void onFailure(final Throwable failure) {
300 failTransaction(envelope, now, new RuntimeRequestException("Commit failed", failure));
304 case CAN_COMMIT_COMPLETE:
305 case CAN_COMMIT_PENDING:
306 case PRE_COMMIT_PENDING:
308 throw new IllegalStateException("Attempted to doCommit in stage " + ready.stage);
310 throwUnhandledCommitStage(ready);
314 private void handleLocalTransactionAbort(final long sequence, final RequestEnvelope envelope, final long now) {
315 checkOpen().abort(() -> recordAndSendSuccess(envelope, now, new TransactionAbortSuccess(getIdentifier(),
319 private void startAbort() {
321 LOG.debug("{}: Transaction {} aborting", persistenceId(), getIdentifier());
324 private void finishAbort() {
326 LOG.debug("{}: Transaction {} aborted", persistenceId(), getIdentifier());
329 private TransactionAbortSuccess handleTransactionAbort(final long sequence, final RequestEnvelope envelope,
331 if (state instanceof Open) {
332 final ReadWriteShardDataTreeTransaction openTransaction = checkOpen();
334 openTransaction.abort(() -> {
335 recordAndSendSuccess(envelope, now, new TransactionAbortSuccess(getIdentifier(),
341 if (ABORTING.equals(state)) {
342 LOG.debug("{}: Transaction {} already aborting", persistenceId(), getIdentifier());
345 if (ABORTED.equals(state)) {
346 // We should have recorded the reply
347 LOG.warn("{}: Transaction {} already aborted", persistenceId(), getIdentifier());
348 return new TransactionAbortSuccess(getIdentifier(), sequence);
351 final Ready ready = checkReady();
353 ready.readyCohort.abort(new FutureCallback<>() {
355 public void onSuccess(final Empty result) {
356 recordAndSendSuccess(envelope, now, new TransactionAbortSuccess(getIdentifier(), sequence));
361 public void onFailure(final Throwable failure) {
362 recordAndSendFailure(envelope, now, new RuntimeRequestException("Abort failed", failure));
363 LOG.warn("{}: Transaction {} abort failed", persistenceId(), getIdentifier(), failure);
370 private void coordinatedCommit(final RequestEnvelope envelope, final long now) throws RequestException {
373 final Ready ready = checkReady();
374 switch (ready.stage) {
375 case CAN_COMMIT_PENDING:
376 LOG.debug("{}: Transaction {} is already canCommitting", persistenceId(), getIdentifier());
379 ready.stage = CommitStage.CAN_COMMIT_PENDING;
380 LOG.debug("{}: Transaction {} initiating canCommit", persistenceId(), getIdentifier());
381 checkReady().readyCohort.canCommit(new FutureCallback<>() {
383 public void onSuccess(final Empty result) {
384 successfulCanCommit(envelope, now);
388 public void onFailure(final Throwable failure) {
389 failTransaction(envelope, now, new RuntimeRequestException("CanCommit failed", failure));
393 case CAN_COMMIT_COMPLETE:
395 case PRE_COMMIT_COMPLETE:
396 case PRE_COMMIT_PENDING:
397 throw new IllegalStateException("Attempted to canCommit in stage " + ready.stage);
399 throwUnhandledCommitStage(ready);
403 void successfulCanCommit(final RequestEnvelope envelope, final long startTime) {
404 if (state instanceof Retired) {
405 LOG.debug("{}: Suppressing successful canCommit of retired transaction {}", persistenceId(),
410 final Ready ready = checkReady();
411 recordAndSendSuccess(envelope, startTime, new TransactionCanCommitSuccess(getIdentifier(),
412 envelope.getMessage().getSequence()));
413 ready.stage = CommitStage.CAN_COMMIT_COMPLETE;
414 LOG.debug("{}: Transaction {} completed canCommit", persistenceId(), getIdentifier());
417 private void directCommit(final RequestEnvelope envelope, final long now) throws RequestException {
420 final Ready ready = checkReady();
421 switch (ready.stage) {
422 case CAN_COMMIT_COMPLETE:
423 case CAN_COMMIT_PENDING:
425 case PRE_COMMIT_COMPLETE:
426 case PRE_COMMIT_PENDING:
427 LOG.debug("{}: Transaction {} in state {}, not initiating direct commit for {}", persistenceId(),
428 getIdentifier(), state, envelope);
431 ready.stage = CommitStage.CAN_COMMIT_PENDING;
432 LOG.debug("{}: Transaction {} initiating direct canCommit", persistenceId(), getIdentifier());
433 ready.readyCohort.canCommit(new FutureCallback<>() {
435 public void onSuccess(final Empty result) {
436 successfulDirectCanCommit(envelope, now);
440 public void onFailure(final Throwable failure) {
441 failTransaction(envelope, now, new RuntimeRequestException("CanCommit failed", failure));
446 throwUnhandledCommitStage(ready);
450 void successfulDirectCanCommit(final RequestEnvelope envelope, final long startTime) {
451 if (state instanceof Retired) {
452 LOG.debug("{}: Suppressing direct canCommit of retired transaction {}", persistenceId(), getIdentifier());
456 final Ready ready = checkReady();
457 ready.stage = CommitStage.PRE_COMMIT_PENDING;
458 LOG.debug("{}: Transaction {} initiating direct preCommit", persistenceId(), getIdentifier());
459 ready.readyCohort.preCommit(new FutureCallback<DataTreeCandidate>() {
461 public void onSuccess(final DataTreeCandidate result) {
462 successfulDirectPreCommit(envelope, startTime);
466 public void onFailure(final Throwable failure) {
467 failTransaction(envelope, startTime, new RuntimeRequestException("PreCommit failed", failure));
472 void successfulDirectPreCommit(final RequestEnvelope envelope, final long startTime) {
473 if (state instanceof Retired) {
474 LOG.debug("{}: Suppressing direct commit of retired transaction {}", persistenceId(), getIdentifier());
478 final Ready ready = checkReady();
479 ready.stage = CommitStage.COMMIT_PENDING;
480 LOG.debug("{}: Transaction {} initiating direct commit", persistenceId(), getIdentifier());
481 ready.readyCohort.commit(new FutureCallback<UnsignedLong>() {
483 public void onSuccess(final UnsignedLong result) {
484 successfulCommit(envelope, startTime);
488 public void onFailure(final Throwable failure) {
489 failTransaction(envelope, startTime, new RuntimeRequestException("DoCommit failed", failure));
494 void successfulCommit(final RequestEnvelope envelope, final long startTime) {
495 if (state instanceof Retired) {
496 LOG.debug("{}: Suppressing commit response on retired transaction {}", persistenceId(), getIdentifier());
500 recordAndSendSuccess(envelope, startTime, new TransactionCommitSuccess(getIdentifier(),
501 envelope.getMessage().getSequence()));
505 private void handleCommitLocalTransaction(final CommitLocalTransactionRequest request,
506 final RequestEnvelope envelope, final long now) throws RequestException {
507 final DataTreeModification sealedModification = checkSealed();
508 if (!sealedModification.equals(request.getModification())) {
509 LOG.warn("Expecting modification {}, commit request has {}", sealedModification, request.getModification());
510 throw new UnsupportedRequestException(request);
513 final Optional<Exception> optFailure = request.getDelayedFailure();
514 if (optFailure.isPresent()) {
515 state = new Ready(history().createFailedCohort(getIdentifier(), sealedModification, optFailure.get()));
517 state = new Ready(history().createReadyCohort(getIdentifier(), sealedModification, Optional.empty()));
520 if (request.isCoordinated()) {
521 coordinatedCommit(envelope, now);
523 directCommit(envelope, now);
527 private ExistsTransactionSuccess handleExistsTransaction(final ExistsTransactionRequest request) {
528 final Optional<NormalizedNode> data = checkOpen().getSnapshot().readNode(request.getPath());
529 return recordSuccess(request.getSequence(), new ExistsTransactionSuccess(getIdentifier(), request.getSequence(),
533 private ReadTransactionSuccess handleReadTransaction(final ReadTransactionRequest request) {
534 final Optional<NormalizedNode> data = checkOpen().getSnapshot().readNode(request.getPath());
535 return recordSuccess(request.getSequence(), new ReadTransactionSuccess(getIdentifier(), request.getSequence(),
539 private ModifyTransactionSuccess replyModifySuccess(final long sequence) {
540 return recordSuccess(sequence, new ModifyTransactionSuccess(getIdentifier(), sequence));
543 private void applyModifications(final Collection<TransactionModification> modifications) {
544 if (!modifications.isEmpty()) {
545 final DataTreeModification modification = checkOpen().getSnapshot();
546 for (TransactionModification m : modifications) {
547 if (m instanceof TransactionDelete) {
548 modification.delete(m.getPath());
549 } else if (m instanceof TransactionWrite) {
550 modification.write(m.getPath(), ((TransactionWrite) m).getData());
551 } else if (m instanceof TransactionMerge) {
552 modification.merge(m.getPath(), ((TransactionMerge) m).getData());
554 LOG.warn("{}: ignoring unhandled modification {}", persistenceId(), m);
560 private @Nullable TransactionSuccess<?> handleModifyTransaction(final ModifyTransactionRequest request,
561 final RequestEnvelope envelope, final long now) throws RequestException {
562 // We need to examine the persistence protocol first to see if this is an idempotent request. If there is no
563 // protocol, there is nothing for us to do.
564 final Optional<PersistenceProtocol> maybeProto = request.getPersistenceProtocol();
565 if (!maybeProto.isPresent()) {
566 applyModifications(request.getModifications());
567 return replyModifySuccess(request.getSequence());
570 switch (maybeProto.get()) {
572 if (ABORTING.equals(state)) {
573 LOG.debug("{}: Transaction {} already aborting", persistenceId(), getIdentifier());
576 final ReadWriteShardDataTreeTransaction openTransaction = checkOpen();
578 openTransaction.abort(() -> {
579 recordAndSendSuccess(envelope, now, new ModifyTransactionSuccess(getIdentifier(),
580 request.getSequence()));
585 ensureReady(request.getModifications());
586 return replyModifySuccess(request.getSequence());
588 ensureReady(request.getModifications());
589 directCommit(envelope, now);
592 ensureReady(request.getModifications());
593 coordinatedCommit(envelope, now);
596 LOG.warn("{}: rejecting unsupported protocol {}", persistenceId(), maybeProto.get());
597 throw new UnsupportedRequestException(request);
601 private void ensureReady(final Collection<TransactionModification> modifications) {
602 // We may have a combination of READY + SIMPLE/THREE_PHASE , in which case we want to ready the transaction
604 if (state instanceof Ready) {
605 LOG.debug("{}: {} is already in state {}", persistenceId(), getIdentifier(), state);
609 applyModifications(modifications);
610 state = new Ready(checkOpen().ready(Optional.empty()));
611 LOG.debug("{}: transitioned {} to ready", persistenceId(), getIdentifier());
614 private void throwIfFailed() throws RequestException {
615 if (state instanceof Failed) {
616 LOG.debug("{}: {} has failed, rejecting request", persistenceId(), getIdentifier());
617 throw ((Failed) state).cause;
621 private ReadWriteShardDataTreeTransaction checkOpen() {
622 checkState(state instanceof Open, "%s expect to be open, is in state %s", getIdentifier(), state);
623 return ((Open) state).openTransaction;
626 private Ready checkReady() {
627 checkState(state instanceof Ready, "%s expect to be ready, is in state %s", getIdentifier(), state);
628 return (Ready) state;
631 private DataTreeModification checkSealed() {
632 checkState(state instanceof Sealed, "%s expect to be sealed, is in state %s", getIdentifier(), state);
633 return ((Sealed) state).sealedModification;
636 private static void throwUnhandledCommitStage(final Ready ready) {
637 throw new IllegalStateException("Unhandled commit stage " + ready.stage);