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
9 package org.opendaylight.controller.cluster.datastore;
11 import akka.actor.Status;
12 import akka.actor.Status.Failure;
13 import akka.dispatch.ExecutionContexts;
14 import akka.dispatch.Futures;
15 import akka.dispatch.Recover;
16 import akka.japi.Function;
17 import akka.pattern.Patterns;
18 import akka.util.Timeout;
19 import com.google.common.base.Preconditions;
20 import com.google.common.base.Throwables;
21 import com.google.common.collect.Iterables;
22 import java.util.Collection;
23 import java.util.Iterator;
24 import java.util.Optional;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.TimeoutException;
27 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
28 import org.opendaylight.controller.cluster.datastore.DataTreeCohortActor.CanCommit;
29 import org.opendaylight.controller.cluster.datastore.DataTreeCohortActor.Success;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import scala.concurrent.Await;
33 import scala.concurrent.Future;
37 * Composite cohort, which coordinates multiple user-provided cohorts as if it was only one cohort.
39 * It tracks current operation and list of cohorts which successfuly finished previous phase in
40 * case, if abort is necessary to invoke it only on cohort steps which are still active.
43 class CompositeDataTreeCohort {
47 * Cohorts are idle, no messages were sent.
51 * CanCommit message was sent to all participating cohorts.
55 * Successful canCommit responses were received from every participating cohort.
57 CAN_COMMIT_SUCCESSFUL,
59 * PreCommit message was sent to all participating cohorts.
63 * Successful preCommit responses were received from every participating cohort.
65 PRE_COMMIT_SUCCESSFUL,
67 * Commit message was send to all participating cohorts.
71 * Successful commit responses were received from all participating cohorts.
75 * Some of cohorts responsed back with unsuccessful message.
81 * Abort message was send to all cohorts which responded with success previously.
87 protected static final Recover<Object> EXCEPTION_TO_MESSAGE = new Recover<Object>() {
89 public Failure recover(final Throwable error) throws Throwable {
90 return new Failure(error);
95 private final DataTreeCohortActorRegistry registry;
96 private final TransactionIdentifier txId;
97 private final SchemaContext schema;
98 private final Timeout timeout;
99 private Iterable<Success> successfulFromPrevious;
100 private State state = State.IDLE;
102 CompositeDataTreeCohort(final DataTreeCohortActorRegistry registry, final TransactionIdentifier transactionID,
103 final SchemaContext schema, final Timeout timeout) {
104 this.registry = Preconditions.checkNotNull(registry);
105 this.txId = Preconditions.checkNotNull(transactionID);
106 this.schema = Preconditions.checkNotNull(schema);
107 this.timeout = Preconditions.checkNotNull(timeout);
110 void canCommit(final DataTreeCandidate tip) throws ExecutionException, TimeoutException {
111 Collection<CanCommit> messages = registry.createCanCommitMessages(txId, tip, schema);
112 // FIXME: Optimize empty collection list with pre-created futures, containing success.
113 Future<Iterable<Object>> canCommitsFuture =
114 Futures.traverse(messages, new Function<CanCommit, Future<Object>>() {
116 public Future<Object> apply(final CanCommit input) {
117 return Patterns.ask(input.getCohort(), input, timeout).recover(EXCEPTION_TO_MESSAGE,
118 ExecutionContexts.global());
120 }, ExecutionContexts.global());
121 changeStateFrom(State.IDLE, State.CAN_COMMIT_SENT);
122 processResponses(canCommitsFuture, State.CAN_COMMIT_SENT, State.CAN_COMMIT_SUCCESSFUL);
125 void preCommit() throws ExecutionException, TimeoutException {
126 Preconditions.checkState(successfulFromPrevious != null);
127 Future<Iterable<Object>> preCommitFutures = sendMesageToSuccessful(new DataTreeCohortActor.PreCommit(txId));
128 changeStateFrom(State.CAN_COMMIT_SUCCESSFUL, State.PRE_COMMIT_SENT);
129 processResponses(preCommitFutures, State.PRE_COMMIT_SENT, State.PRE_COMMIT_SUCCESSFUL);
132 void commit() throws ExecutionException, TimeoutException {
133 Preconditions.checkState(successfulFromPrevious != null);
134 Future<Iterable<Object>> commitsFuture = sendMesageToSuccessful(new DataTreeCohortActor.Commit(txId));
135 changeStateFrom(State.PRE_COMMIT_SUCCESSFUL, State.COMMIT_SENT);
136 processResponses(commitsFuture, State.COMMIT_SENT, State.COMMITED);
139 Optional<Future<Iterable<Object>>> abort() {
140 if (successfulFromPrevious != null) {
141 return Optional.of(sendMesageToSuccessful(new DataTreeCohortActor.Abort(txId)));
144 return Optional.empty();
147 private Future<Iterable<Object>> sendMesageToSuccessful(final Object message) {
148 return Futures.traverse(successfulFromPrevious, new Function<DataTreeCohortActor.Success, Future<Object>>() {
151 public Future<Object> apply(final DataTreeCohortActor.Success cohortResponse) throws Exception {
152 return Patterns.ask(cohortResponse.getCohort(), message, timeout);
155 }, ExecutionContexts.global());
158 private void processResponses(final Future<Iterable<Object>> resultsFuture, final State currentState, final State afterState)
159 throws TimeoutException, ExecutionException {
160 final Iterable<Object> results;
162 results = Await.result(resultsFuture, timeout.duration());
163 } catch (Exception e) {
164 successfulFromPrevious = null;
165 Throwables.propagateIfInstanceOf(e, TimeoutException.class);
166 throw Throwables.propagate(e);
168 Iterable<Failure> failed = Iterables.filter(results, Status.Failure.class);
169 Iterable<Success> successful = Iterables.filter(results, DataTreeCohortActor.Success.class);
170 successfulFromPrevious = successful;
171 if (!Iterables.isEmpty(failed)) {
172 changeStateFrom(currentState, State.FAILED);
173 Iterator<Failure> it = failed.iterator();
174 Throwable firstEx = it.next().cause();
175 while (it.hasNext()) {
176 firstEx.addSuppressed(it.next().cause());
178 Throwables.propagateIfPossible(firstEx, ExecutionException.class);
179 Throwables.propagateIfPossible(firstEx, TimeoutException.class);
180 throw Throwables.propagate(firstEx);
182 changeStateFrom(currentState, afterState);
185 void changeStateFrom(final State expected, final State followup) {
186 Preconditions.checkState(state == expected);