Fixed issue where a shard for a defined module is not found
[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
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.ListeningExecutorService;
16
17 import org.opendaylight.controller.cluster.datastore.exceptions.TimeoutException;
18 import org.opendaylight.controller.cluster.datastore.messages.AbortTransaction;
19 import org.opendaylight.controller.cluster.datastore.messages.AbortTransactionReply;
20 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction;
21 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply;
22 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
23 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
24 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransaction;
25 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransactionReply;
26 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.concurrent.Callable;
34
35 /**
36  * ThreePhaseCommitCohortProxy represents a set of remote cohort proxies
37  */
38 public class ThreePhaseCommitCohortProxy implements
39     DOMStoreThreePhaseCommitCohort{
40
41     private static final Logger
42         LOG = LoggerFactory.getLogger(DistributedDataStore.class);
43
44     private final ActorContext actorContext;
45     private final List<ActorPath> cohortPaths;
46     private final ListeningExecutorService executor;
47     private final String transactionId;
48
49
50     public ThreePhaseCommitCohortProxy(ActorContext actorContext,
51         List<ActorPath> cohortPaths,
52         String transactionId,
53         ListeningExecutorService executor) {
54
55         this.actorContext = actorContext;
56         this.cohortPaths = cohortPaths;
57         this.transactionId = transactionId;
58         this.executor = executor;
59     }
60
61     @Override public ListenableFuture<Boolean> canCommit() {
62         Callable<Boolean> call = new Callable<Boolean>() {
63
64             @Override
65             public Boolean call() throws Exception {
66                 for(ActorPath actorPath : cohortPaths){
67                     ActorSelection cohort = actorContext.actorSelection(actorPath);
68
69                     try {
70                         Object response =
71                                 actorContext.executeRemoteOperation(cohort,
72                                         new CanCommitTransaction().toSerializable(),
73                                         ActorContext.ASK_DURATION);
74
75                         if (response.getClass().equals(CanCommitTransactionReply.SERIALIZABLE_CLASS)) {
76                             CanCommitTransactionReply reply =
77                                     CanCommitTransactionReply.fromSerializable(response);
78                             if (!reply.getCanCommit()) {
79                                 System.out.println("**TOM - failed: false");
80                                 return false;
81                             }
82                         }
83                     } catch(RuntimeException e){
84                         LOG.error("Unexpected Exception", e);
85                         return false;
86                     }
87                 }
88
89                 return true;
90             }
91         };
92
93         return executor.submit(call);
94     }
95
96     @Override public ListenableFuture<Void> preCommit() {
97         return voidOperation(new PreCommitTransaction().toSerializable(), PreCommitTransactionReply.SERIALIZABLE_CLASS);
98     }
99
100     @Override public ListenableFuture<Void> abort() {
101         return voidOperation(new AbortTransaction().toSerializable(), AbortTransactionReply.SERIALIZABLE_CLASS);
102     }
103
104     @Override public ListenableFuture<Void> commit() {
105         return voidOperation(new CommitTransaction().toSerializable(), CommitTransactionReply.SERIALIZABLE_CLASS);
106     }
107
108     private ListenableFuture<Void> voidOperation(final Object message, final Class expectedResponseClass){
109         Callable<Void> call = new Callable<Void>() {
110
111             @Override public Void call() throws Exception {
112                 for(ActorPath actorPath : cohortPaths){
113                     ActorSelection cohort = actorContext.actorSelection(actorPath);
114
115                     try {
116                         Object response =
117                             actorContext.executeRemoteOperation(cohort,
118                                 message,
119                                 ActorContext.ASK_DURATION);
120
121                         if (response != null && !response.getClass()
122                             .equals(expectedResponseClass)) {
123                             throw new RuntimeException(
124                                 String.format(
125                                     "did not get the expected response \n\t\t expected : %s \n\t\t actual   : %s",
126                                     expectedResponseClass.toString(),
127                                     response.getClass().toString())
128                             );
129                         }
130                     } catch(TimeoutException e){
131                         LOG.error(String.format("A timeout occurred when processing operation : %s", message));
132                     }
133                 }
134                 return null;
135             }
136         };
137
138         return executor.submit(call);
139     }
140
141     public List<ActorPath> getCohortPaths() {
142         return Collections.unmodifiableList(this.cohortPaths);
143     }
144 }