36b6efa2f7091d9f178d682aded4fc4888f42dff
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ThreePhaseCommitCohortProxy.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.datastore;
10
11 import akka.actor.ActorPath;
12 import akka.actor.ActorSelection;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.ListenableFutureTask;
15 import org.opendaylight.controller.cluster.datastore.messages.AbortTransaction;
16 import org.opendaylight.controller.cluster.datastore.messages.AbortTransactionReply;
17 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction;
18 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply;
19 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
20 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
21 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransaction;
22 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransactionReply;
23 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.concurrent.Callable;
31 import java.util.concurrent.ExecutorService;
32 import java.util.concurrent.Executors;
33
34 /**
35  * ThreePhaseCommitCohortProxy represents a set of remote cohort proxies
36  */
37 public class ThreePhaseCommitCohortProxy implements
38     DOMStoreThreePhaseCommitCohort{
39
40     private static final Logger
41         LOG = LoggerFactory.getLogger(DistributedDataStore.class);
42
43     private final ActorContext actorContext;
44     private final List<ActorPath> cohortPaths;
45     //FIXME : Use a thread pool here
46     private final ExecutorService executorService = Executors.newSingleThreadExecutor();
47
48
49     public ThreePhaseCommitCohortProxy(ActorContext actorContext, List<ActorPath> cohortPaths) {
50         this.actorContext = actorContext;
51         this.cohortPaths = cohortPaths;
52     }
53
54     @Override public ListenableFuture<Boolean> canCommit() {
55         Callable<Boolean> call = new Callable() {
56
57             @Override public Boolean call() throws Exception {
58             for(ActorPath actorPath : cohortPaths){
59                 ActorSelection cohort = actorContext.actorSelection(actorPath);
60
61                 try {
62                     Object response =
63                         actorContext.executeRemoteOperation(cohort,
64                             new CanCommitTransaction(),
65                             ActorContext.ASK_DURATION);
66
67                     if (response instanceof CanCommitTransactionReply) {
68                         CanCommitTransactionReply reply =
69                             (CanCommitTransactionReply) response;
70                         if (!reply.getCanCommit()) {
71                             return false;
72                         }
73                     }
74                 } catch(RuntimeException e){
75                     LOG.error("Unexpected Exception", e);
76                     return false;
77                 }
78
79
80             }
81             return true;
82             }
83         };
84
85         ListenableFutureTask<Boolean>
86             future = ListenableFutureTask.create(call);
87
88         executorService.submit(future);
89
90         return future;
91     }
92
93     @Override public ListenableFuture<Void> preCommit() {
94         return voidOperation(new PreCommitTransaction(), PreCommitTransactionReply.class);
95     }
96
97     @Override public ListenableFuture<Void> abort() {
98         return voidOperation(new AbortTransaction(), AbortTransactionReply.class);
99     }
100
101     @Override public ListenableFuture<Void> commit() {
102         return voidOperation(new CommitTransaction(), CommitTransactionReply.class);
103     }
104
105     private ListenableFuture<Void> voidOperation(final Object message, final Class expectedResponseClass){
106         Callable<Void> call = new Callable<Void>() {
107
108             @Override public Void call() throws Exception {
109                 for(ActorPath actorPath : cohortPaths){
110                     ActorSelection cohort = actorContext.actorSelection(actorPath);
111
112                     Object response = actorContext.executeRemoteOperation(cohort,
113                         message,
114                         ActorContext.ASK_DURATION);
115
116                     if(response != null && !response.getClass().equals(expectedResponseClass)){
117                         throw new RuntimeException(
118                             String.format(
119                                 "did not get the expected response \n\t\t expected : %s \n\t\t actual   : %s",
120                                 expectedResponseClass.toString(),
121                                 response.getClass().toString()));
122                     }
123                 }
124                 return null;
125             }
126         };
127
128         ListenableFutureTask<Void>
129             future = ListenableFutureTask.create(call);
130
131         executorService.submit(future);
132
133         return future;
134
135     }
136
137     public List<ActorPath> getCohortPaths() {
138         return Collections.unmodifiableList(this.cohortPaths);
139     }
140 }