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.data.api.schema.NormalizedNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
47 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * Frontend read-write transaction state as observed by the shard leader. This class is NOT thread-safe.
54 * @author Robert Varga
56 final class FrontendReadWriteTransaction extends FrontendTransaction {
57 private enum CommitStage {
66 private abstract static class State {
68 public abstract String toString();
71 private static final class Failed extends State {
72 final RequestException cause;
74 Failed(final RequestException cause) {
75 this.cause = requireNonNull(cause);
79 public String toString() {
80 return "FAILED (" + cause.getMessage() + ")";
84 private static final class Open extends State {
85 final ReadWriteShardDataTreeTransaction openTransaction;
87 Open(final ReadWriteShardDataTreeTransaction openTransaction) {
88 this.openTransaction = requireNonNull(openTransaction);
92 public String toString() {
97 private static final class Ready extends State {
98 final ShardDataTreeCohort readyCohort;
101 Ready(final ShardDataTreeCohort readyCohort) {
102 this.readyCohort = requireNonNull(readyCohort);
103 this.stage = CommitStage.READY;
107 public String toString() {
108 return "READY (" + stage + ")";
112 private static final class Sealed extends State {
113 final DataTreeModification sealedModification;
115 Sealed(final DataTreeModification sealedModification) {
116 this.sealedModification = requireNonNull(sealedModification);
120 public String toString() {
126 * Retired state, needed to catch and suppress callbacks after we have removed associated state.
128 private static final class Retired extends State {
129 private final String prevStateString;
131 Retired(final State prevState) {
132 prevStateString = prevState.toString();
136 public String toString() {
137 return "RETIRED (in " + prevStateString + ")";
141 private static final Logger LOG = LoggerFactory.getLogger(FrontendReadWriteTransaction.class);
142 private static final State ABORTED = new State() {
144 public String toString() {
148 private static final State ABORTING = new State() {
150 public String toString() {
154 private static final State COMMITTED = new State() {
156 public String toString() {
163 private FrontendReadWriteTransaction(final AbstractFrontendHistory history, final TransactionIdentifier id,
164 final ReadWriteShardDataTreeTransaction transaction) {
166 this.state = new Open(transaction);
169 private FrontendReadWriteTransaction(final AbstractFrontendHistory history, final TransactionIdentifier id,
170 final DataTreeModification mod) {
172 this.state = new Sealed(mod);
175 static FrontendReadWriteTransaction createOpen(final AbstractFrontendHistory history,
176 final ReadWriteShardDataTreeTransaction transaction) {
177 return new FrontendReadWriteTransaction(history, transaction.getIdentifier(), transaction);
180 static FrontendReadWriteTransaction createReady(final AbstractFrontendHistory history,
181 final TransactionIdentifier id, final DataTreeModification mod) {
182 return new FrontendReadWriteTransaction(history, id, mod);
185 // Sequence has already been checked
187 TransactionSuccess<?> doHandleRequest(final TransactionRequest<?> request, final RequestEnvelope envelope,
188 final long now) throws RequestException {
189 if (request instanceof ModifyTransactionRequest) {
190 return handleModifyTransaction((ModifyTransactionRequest) request, envelope, now);
191 } else if (request instanceof CommitLocalTransactionRequest) {
192 handleCommitLocalTransaction((CommitLocalTransactionRequest) request, envelope, now);
194 } else if (request instanceof ExistsTransactionRequest) {
195 return handleExistsTransaction((ExistsTransactionRequest) request);
196 } else if (request instanceof ReadTransactionRequest) {
197 return handleReadTransaction((ReadTransactionRequest) request);
198 } else if (request instanceof TransactionPreCommitRequest) {
199 handleTransactionPreCommit((TransactionPreCommitRequest) request, envelope, now);
201 } else if (request instanceof TransactionDoCommitRequest) {
202 handleTransactionDoCommit((TransactionDoCommitRequest) request, envelope, now);
204 } else if (request instanceof TransactionAbortRequest) {
205 return handleTransactionAbort(request.getSequence(), envelope, now);
206 } else if (request instanceof AbortLocalTransactionRequest) {
207 handleLocalTransactionAbort(request.getSequence(), envelope, now);
210 LOG.warn("Rejecting unsupported request {}", request);
211 throw new UnsupportedRequestException(request);
217 state = new Retired(state);
220 private void handleTransactionPreCommit(final TransactionPreCommitRequest request,
221 final RequestEnvelope envelope, final long now) throws RequestException {
224 final Ready ready = checkReady();
225 switch (ready.stage) {
226 case PRE_COMMIT_PENDING:
227 LOG.debug("{}: Transaction {} is already preCommitting", persistenceId(), getIdentifier());
229 case CAN_COMMIT_COMPLETE:
230 ready.stage = CommitStage.PRE_COMMIT_PENDING;
231 LOG.debug("{}: Transaction {} initiating preCommit", persistenceId(), getIdentifier());
232 ready.readyCohort.preCommit(new FutureCallback<DataTreeCandidate>() {
234 public void onSuccess(final DataTreeCandidate result) {
235 successfulPreCommit(envelope, now);
239 public void onFailure(final Throwable failure) {
240 failTransaction(envelope, now, new RuntimeRequestException("Precommit failed", failure));
244 case CAN_COMMIT_PENDING:
246 case PRE_COMMIT_COMPLETE:
248 throw new IllegalStateException("Attempted to preCommit in stage " + ready.stage);
250 throwUnhandledCommitStage(ready);
254 void successfulPreCommit(final RequestEnvelope envelope, final long startTime) {
255 if (state instanceof Retired) {
256 LOG.debug("{}: Suppressing successful preCommit of retired transaction {}", persistenceId(),
261 final Ready ready = checkReady();
262 LOG.debug("{}: Transaction {} completed preCommit", persistenceId(), getIdentifier());
263 recordAndSendSuccess(envelope, startTime, new TransactionPreCommitSuccess(getIdentifier(),
264 envelope.getMessage().getSequence()));
265 ready.stage = CommitStage.PRE_COMMIT_COMPLETE;
268 void failTransaction(final RequestEnvelope envelope, final long now, final RuntimeRequestException cause) {
269 if (state instanceof Retired) {
270 LOG.debug("{}: Suppressing failure of retired transaction {}", persistenceId(), getIdentifier(), cause);
274 recordAndSendFailure(envelope, now, cause);
275 state = new Failed(cause);
276 LOG.debug("{}: Transaction {} failed", persistenceId(), getIdentifier(), cause);
279 private void handleTransactionDoCommit(final TransactionDoCommitRequest request, final RequestEnvelope envelope,
280 final long now) throws RequestException {
283 final Ready ready = checkReady();
284 switch (ready.stage) {
286 LOG.debug("{}: Transaction {} is already committing", persistenceId(), getIdentifier());
288 case PRE_COMMIT_COMPLETE:
289 ready.stage = CommitStage.COMMIT_PENDING;
290 LOG.debug("{}: Transaction {} initiating commit", persistenceId(), getIdentifier());
291 ready.readyCohort.commit(new FutureCallback<UnsignedLong>() {
293 public void onSuccess(final UnsignedLong result) {
294 successfulCommit(envelope, now);
298 public void onFailure(final Throwable failure) {
299 failTransaction(envelope, now, new RuntimeRequestException("Commit failed", failure));
303 case CAN_COMMIT_COMPLETE:
304 case CAN_COMMIT_PENDING:
305 case PRE_COMMIT_PENDING:
307 throw new IllegalStateException("Attempted to doCommit in stage " + ready.stage);
309 throwUnhandledCommitStage(ready);
313 private void handleLocalTransactionAbort(final long sequence, final RequestEnvelope envelope, final long now) {
314 checkOpen().abort(() -> recordAndSendSuccess(envelope, now, new TransactionAbortSuccess(getIdentifier(),
318 private void startAbort() {
320 LOG.debug("{}: Transaction {} aborting", persistenceId(), getIdentifier());
323 private void finishAbort() {
325 LOG.debug("{}: Transaction {} aborted", persistenceId(), getIdentifier());
328 private TransactionAbortSuccess handleTransactionAbort(final long sequence, final RequestEnvelope envelope,
330 if (state instanceof Open) {
331 final ReadWriteShardDataTreeTransaction openTransaction = checkOpen();
333 openTransaction.abort(() -> {
334 recordAndSendSuccess(envelope, now, new TransactionAbortSuccess(getIdentifier(),
340 if (ABORTING.equals(state)) {
341 LOG.debug("{}: Transaction {} already aborting", persistenceId(), getIdentifier());
344 if (ABORTED.equals(state)) {
345 // We should have recorded the reply
346 LOG.warn("{}: Transaction {} already aborted", persistenceId(), getIdentifier());
347 return new TransactionAbortSuccess(getIdentifier(), sequence);
350 final Ready ready = checkReady();
352 ready.readyCohort.abort(new FutureCallback<Void>() {
354 public void onSuccess(final Void result) {
355 recordAndSendSuccess(envelope, now, new TransactionAbortSuccess(getIdentifier(), sequence));
360 public void onFailure(final Throwable failure) {
361 recordAndSendFailure(envelope, now, new RuntimeRequestException("Abort failed", failure));
362 LOG.warn("{}: Transaction {} abort failed", persistenceId(), getIdentifier(), failure);
369 private void coordinatedCommit(final RequestEnvelope envelope, final long now) throws RequestException {
372 final Ready ready = checkReady();
373 switch (ready.stage) {
374 case CAN_COMMIT_PENDING:
375 LOG.debug("{}: Transaction {} is already canCommitting", persistenceId(), getIdentifier());
378 ready.stage = CommitStage.CAN_COMMIT_PENDING;
379 LOG.debug("{}: Transaction {} initiating canCommit", persistenceId(), getIdentifier());
380 checkReady().readyCohort.canCommit(new FutureCallback<Void>() {
382 public void onSuccess(final Void result) {
383 successfulCanCommit(envelope, now);
387 public void onFailure(final Throwable failure) {
388 failTransaction(envelope, now, new RuntimeRequestException("CanCommit failed", failure));
392 case CAN_COMMIT_COMPLETE:
394 case PRE_COMMIT_COMPLETE:
395 case PRE_COMMIT_PENDING:
396 throw new IllegalStateException("Attempted to canCommit in stage " + ready.stage);
398 throwUnhandledCommitStage(ready);
402 void successfulCanCommit(final RequestEnvelope envelope, final long startTime) {
403 if (state instanceof Retired) {
404 LOG.debug("{}: Suppressing successful canCommit of retired transaction {}", persistenceId(),
409 final Ready ready = checkReady();
410 recordAndSendSuccess(envelope, startTime, new TransactionCanCommitSuccess(getIdentifier(),
411 envelope.getMessage().getSequence()));
412 ready.stage = CommitStage.CAN_COMMIT_COMPLETE;
413 LOG.debug("{}: Transaction {} completed canCommit", persistenceId(), getIdentifier());
416 private void directCommit(final RequestEnvelope envelope, final long now) throws RequestException {
419 final Ready ready = checkReady();
420 switch (ready.stage) {
421 case CAN_COMMIT_COMPLETE:
422 case CAN_COMMIT_PENDING:
424 case PRE_COMMIT_COMPLETE:
425 case PRE_COMMIT_PENDING:
426 LOG.debug("{}: Transaction {} in state {}, not initiating direct commit for {}", persistenceId(),
427 getIdentifier(), state, envelope);
430 ready.stage = CommitStage.CAN_COMMIT_PENDING;
431 LOG.debug("{}: Transaction {} initiating direct canCommit", persistenceId(), getIdentifier());
432 ready.readyCohort.canCommit(new FutureCallback<Void>() {
434 public void onSuccess(final Void result) {
435 successfulDirectCanCommit(envelope, now);
439 public void onFailure(final Throwable failure) {
440 failTransaction(envelope, now, new RuntimeRequestException("CanCommit failed", failure));
445 throwUnhandledCommitStage(ready);
449 void successfulDirectCanCommit(final RequestEnvelope envelope, final long startTime) {
450 if (state instanceof Retired) {
451 LOG.debug("{}: Suppressing direct canCommit of retired transaction {}", persistenceId(), getIdentifier());
455 final Ready ready = checkReady();
456 ready.stage = CommitStage.PRE_COMMIT_PENDING;
457 LOG.debug("{}: Transaction {} initiating direct preCommit", persistenceId(), getIdentifier());
458 ready.readyCohort.preCommit(new FutureCallback<DataTreeCandidate>() {
460 public void onSuccess(final DataTreeCandidate result) {
461 successfulDirectPreCommit(envelope, startTime);
465 public void onFailure(final Throwable failure) {
466 failTransaction(envelope, startTime, new RuntimeRequestException("PreCommit failed", failure));
471 void successfulDirectPreCommit(final RequestEnvelope envelope, final long startTime) {
472 if (state instanceof Retired) {
473 LOG.debug("{}: Suppressing direct commit of retired transaction {}", persistenceId(), getIdentifier());
477 final Ready ready = checkReady();
478 ready.stage = CommitStage.COMMIT_PENDING;
479 LOG.debug("{}: Transaction {} initiating direct commit", persistenceId(), getIdentifier());
480 ready.readyCohort.commit(new FutureCallback<UnsignedLong>() {
482 public void onSuccess(final UnsignedLong result) {
483 successfulCommit(envelope, startTime);
487 public void onFailure(final Throwable failure) {
488 failTransaction(envelope, startTime, new RuntimeRequestException("DoCommit failed", failure));
493 void successfulCommit(final RequestEnvelope envelope, final long startTime) {
494 if (state instanceof Retired) {
495 LOG.debug("{}: Suppressing commit response on retired transaction {}", persistenceId(), getIdentifier());
499 recordAndSendSuccess(envelope, startTime, new TransactionCommitSuccess(getIdentifier(),
500 envelope.getMessage().getSequence()));
504 private void handleCommitLocalTransaction(final CommitLocalTransactionRequest request,
505 final RequestEnvelope envelope, final long now) throws RequestException {
506 final DataTreeModification sealedModification = checkSealed();
507 if (!sealedModification.equals(request.getModification())) {
508 LOG.warn("Expecting modification {}, commit request has {}", sealedModification, request.getModification());
509 throw new UnsupportedRequestException(request);
512 final Optional<Exception> optFailure = request.getDelayedFailure();
513 if (optFailure.isPresent()) {
514 state = new Ready(history().createFailedCohort(getIdentifier(), sealedModification, optFailure.get()));
516 state = new Ready(history().createReadyCohort(getIdentifier(), sealedModification, Optional.empty()));
519 if (request.isCoordinated()) {
520 coordinatedCommit(envelope, now);
522 directCommit(envelope, now);
526 private ExistsTransactionSuccess handleExistsTransaction(final ExistsTransactionRequest request) {
527 final Optional<NormalizedNode<?, ?>> data = checkOpen().getSnapshot().readNode(request.getPath());
528 return recordSuccess(request.getSequence(), new ExistsTransactionSuccess(getIdentifier(), request.getSequence(),
532 private ReadTransactionSuccess handleReadTransaction(final ReadTransactionRequest request) {
533 final Optional<NormalizedNode<?, ?>> data = checkOpen().getSnapshot().readNode(request.getPath());
534 return recordSuccess(request.getSequence(), new ReadTransactionSuccess(getIdentifier(), request.getSequence(),
538 private ModifyTransactionSuccess replyModifySuccess(final long sequence) {
539 return recordSuccess(sequence, new ModifyTransactionSuccess(getIdentifier(), sequence));
542 private void applyModifications(final Collection<TransactionModification> modifications) {
543 if (!modifications.isEmpty()) {
544 final DataTreeModification modification = checkOpen().getSnapshot();
545 for (TransactionModification m : modifications) {
546 if (m instanceof TransactionDelete) {
547 modification.delete(m.getPath());
548 } else if (m instanceof TransactionWrite) {
549 modification.write(m.getPath(), ((TransactionWrite) m).getData());
550 } else if (m instanceof TransactionMerge) {
551 modification.merge(m.getPath(), ((TransactionMerge) m).getData());
553 LOG.warn("{}: ignoring unhandled modification {}", persistenceId(), m);
559 private @Nullable TransactionSuccess<?> handleModifyTransaction(final ModifyTransactionRequest request,
560 final RequestEnvelope envelope, final long now) throws RequestException {
561 // We need to examine the persistence protocol first to see if this is an idempotent request. If there is no
562 // protocol, there is nothing for us to do.
563 final Optional<PersistenceProtocol> maybeProto = request.getPersistenceProtocol();
564 if (!maybeProto.isPresent()) {
565 applyModifications(request.getModifications());
566 return replyModifySuccess(request.getSequence());
569 switch (maybeProto.get()) {
571 if (ABORTING.equals(state)) {
572 LOG.debug("{}: Transaction {} already aborting", persistenceId(), getIdentifier());
575 final ReadWriteShardDataTreeTransaction openTransaction = checkOpen();
577 openTransaction.abort(() -> {
578 recordAndSendSuccess(envelope, now, new ModifyTransactionSuccess(getIdentifier(),
579 request.getSequence()));
584 ensureReady(request.getModifications());
585 return replyModifySuccess(request.getSequence());
587 ensureReady(request.getModifications());
588 directCommit(envelope, now);
591 ensureReady(request.getModifications());
592 coordinatedCommit(envelope, now);
595 LOG.warn("{}: rejecting unsupported protocol {}", persistenceId(), maybeProto.get());
596 throw new UnsupportedRequestException(request);
600 private void ensureReady(final Collection<TransactionModification> modifications) {
601 // We may have a combination of READY + SIMPLE/THREE_PHASE , in which case we want to ready the transaction
603 if (state instanceof Ready) {
604 LOG.debug("{}: {} is already in state {}", persistenceId(), getIdentifier(), state);
608 applyModifications(modifications);
609 state = new Ready(checkOpen().ready(Optional.empty()));
610 LOG.debug("{}: transitioned {} to ready", persistenceId(), getIdentifier());
613 private void throwIfFailed() throws RequestException {
614 if (state instanceof Failed) {
615 LOG.debug("{}: {} has failed, rejecting request", persistenceId(), getIdentifier());
616 throw ((Failed) state).cause;
620 private ReadWriteShardDataTreeTransaction checkOpen() {
621 checkState(state instanceof Open, "%s expect to be open, is in state %s", getIdentifier(), state);
622 return ((Open) state).openTransaction;
625 private Ready checkReady() {
626 checkState(state instanceof Ready, "%s expect to be ready, is in state %s", getIdentifier(), state);
627 return (Ready) state;
630 private DataTreeModification checkSealed() {
631 checkState(state instanceof Sealed, "%s expect to be sealed, is in state %s", getIdentifier(), state);
632 return ((Sealed) state).sealedModification;
635 private static void throwUnhandledCommitStage(final Ready ready) {
636 throw new IllegalStateException("Unhandled commit stage " + ready.stage);