97f3e7444d3ee564c88845256e7448209036fe09
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / TransactionChainProxy.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 com.google.common.base.Preconditions;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.concurrent.atomic.AtomicInteger;
15 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChain;
16 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
17 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainClosedException;
18 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
22 import scala.concurrent.Future;
23
24 /**
25  * TransactionChainProxy acts as a proxy for a DOMStoreTransactionChain created on a remote shard
26  */
27 public class TransactionChainProxy implements DOMStoreTransactionChain {
28
29     private interface State {
30         boolean isReady();
31
32         List<Future<Object>> getPreviousReadyFutures();
33     }
34
35     private static class Allocated implements State {
36         private final ChainedTransactionProxy transaction;
37
38         Allocated(ChainedTransactionProxy transaction) {
39             this.transaction = transaction;
40         }
41
42         @Override
43         public boolean isReady() {
44             return transaction.isReady();
45         }
46
47         @Override
48         public List<Future<Object>> getPreviousReadyFutures() {
49             return transaction.getReadyFutures();
50         }
51     }
52
53     private static abstract class AbstractDefaultState implements State {
54         @Override
55         public List<Future<Object>> getPreviousReadyFutures() {
56             return Collections.emptyList();
57         }
58     }
59
60     private static final State IDLE_STATE = new AbstractDefaultState() {
61         @Override
62         public boolean isReady() {
63             return true;
64         }
65     };
66
67     private static final State CLOSED_STATE = new AbstractDefaultState() {
68         @Override
69         public boolean isReady() {
70             throw new TransactionChainClosedException("Transaction chain has been closed");
71         }
72     };
73
74     private static final AtomicInteger counter = new AtomicInteger(0);
75
76     private final ActorContext actorContext;
77     private final String transactionChainId;
78     private volatile State currentState = IDLE_STATE;
79
80     public TransactionChainProxy(ActorContext actorContext) {
81         this.actorContext = actorContext;
82         transactionChainId = actorContext.getCurrentMemberName() + "-txn-chain-" + counter.incrementAndGet();
83     }
84
85     public String getTransactionChainId() {
86         return transactionChainId;
87     }
88
89     @Override
90     public DOMStoreReadTransaction newReadOnlyTransaction() {
91         State localState = currentState;
92         checkReadyState(localState);
93
94         return new ChainedTransactionProxy(actorContext, TransactionType.READ_ONLY,
95                 transactionChainId, localState.getPreviousReadyFutures());
96     }
97
98     @Override
99     public DOMStoreReadWriteTransaction newReadWriteTransaction() {
100         actorContext.acquireTxCreationPermit();
101         return allocateWriteTransaction(TransactionType.READ_WRITE);
102     }
103
104     @Override
105     public DOMStoreWriteTransaction newWriteOnlyTransaction() {
106         actorContext.acquireTxCreationPermit();
107         return allocateWriteTransaction(TransactionType.WRITE_ONLY);
108     }
109
110     @Override
111     public void close() {
112         currentState = CLOSED_STATE;
113
114         // Send a close transaction chain request to each and every shard
115         actorContext.broadcast(new CloseTransactionChain(transactionChainId).toSerializable());
116     }
117
118     private ChainedTransactionProxy allocateWriteTransaction(TransactionType type) {
119         State localState = currentState;
120
121         checkReadyState(localState);
122
123         // Pass the ready Futures from the previous Tx.
124         ChainedTransactionProxy txProxy = new ChainedTransactionProxy(actorContext, type,
125                 transactionChainId, localState.getPreviousReadyFutures());
126
127         currentState = new Allocated(txProxy);
128
129         return txProxy;
130     }
131
132     private void checkReadyState(State state) {
133         Preconditions.checkState(state.isReady(), "Previous transaction is not ready yet");
134     }
135 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.