2 * Copyright (c) 2015 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 akka.actor.ActorSelection;
11 import akka.dispatch.OnComplete;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
16 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
17 import org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction;
18 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
20 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import scala.concurrent.Future;
27 * Fake {@link DOMStoreThreePhaseCommitCohort} instantiated for local transactions to conform with the DOM
28 * transaction APIs. It is only used to hold the data from a local DOM transaction ready operation and to
29 * initiate direct or coordinated commits from the front-end by sending the ReadyLocalTransaction message.
30 * It is not actually called by the front-end to perform 3PC thus the canCommit/preCommit/commit methods
33 abstract class LocalThreePhaseCommitCohort implements DOMStoreThreePhaseCommitCohort {
34 private static final Logger LOG = LoggerFactory.getLogger(LocalThreePhaseCommitCohort.class);
36 private final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction;
37 private final DataTreeModification modification;
38 private final ActorContext actorContext;
39 private final ActorSelection leader;
41 protected LocalThreePhaseCommitCohort(final ActorContext actorContext, final ActorSelection leader,
42 final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction, final DataTreeModification modification) {
43 this.actorContext = Preconditions.checkNotNull(actorContext);
44 this.leader = Preconditions.checkNotNull(leader);
45 this.transaction = Preconditions.checkNotNull(transaction);
46 this.modification = Preconditions.checkNotNull(modification);
49 private Future<Object> initiateCommit(final boolean immediate) {
50 final ReadyLocalTransaction message = new ReadyLocalTransaction(transaction.getIdentifier().toString(),
51 modification, immediate);
52 return actorContext.executeOperationAsync(leader, message, actorContext.getTransactionCommitOperationTimeout());
56 * Return the {@link ActorContext} associated with this object.
58 * @return An actor context instance.
60 @Nonnull ActorContext getActorContext() {
64 Future<ActorSelection> initiateCoordinatedCommit() {
65 final Future<Object> messageFuture = initiateCommit(false);
66 final Future<ActorSelection> ret = TransactionReadyReplyMapper.transform(messageFuture, actorContext,
67 transaction.getIdentifier());
68 ret.onComplete(new OnComplete<ActorSelection>() {
70 public void onComplete(final Throwable failure, final ActorSelection success) throws Throwable {
71 if (failure != null) {
72 LOG.info("Failed to prepare transaction {} on backend", transaction.getIdentifier(), failure);
73 transactionAborted(transaction);
77 LOG.debug("Transaction {} resolved to actor {}", transaction.getIdentifier(), success);
79 }, actorContext.getClientDispatcher());
84 Future<Object> initiateDirectCommit() {
85 final Future<Object> messageFuture = initiateCommit(true);
86 messageFuture.onComplete(new OnComplete<Object>() {
88 public void onComplete(final Throwable failure, final Object message) throws Throwable {
89 if (failure != null) {
90 LOG.error("Failed to prepare transaction {} on backend", transaction.getIdentifier(), failure);
91 transactionAborted(transaction);
92 } else if (CommitTransactionReply.SERIALIZABLE_CLASS.isInstance(message)) {
93 LOG.debug("Transaction {} committed successfully", transaction.getIdentifier());
94 transactionCommitted(transaction);
96 LOG.error("Transaction {} resulted in unhandled message type {}, aborting", message.getClass());
97 transactionAborted(transaction);
100 }, actorContext.getClientDispatcher());
102 return messageFuture;
106 public final ListenableFuture<Boolean> canCommit() {
108 throw new UnsupportedOperationException();
112 public final ListenableFuture<Void> preCommit() {
114 throw new UnsupportedOperationException();
118 public final ListenableFuture<Void> abort() {
120 throw new UnsupportedOperationException();
124 public final ListenableFuture<Void> commit() {
126 throw new UnsupportedOperationException();
129 protected abstract void transactionAborted(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction);
130 protected abstract void transactionCommitted(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction);