Relax visibility on FrontendReadWriteTransaction methods
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / FrontendReadWriteTransaction.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.controller.cluster.datastore;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.primitives.UnsignedLong;
13 import com.google.common.util.concurrent.FutureCallback;
14 import java.util.Collection;
15 import javax.annotation.Nullable;
16 import javax.annotation.concurrent.NotThreadSafe;
17 import org.opendaylight.controller.cluster.access.commands.CommitLocalTransactionRequest;
18 import org.opendaylight.controller.cluster.access.commands.ExistsTransactionRequest;
19 import org.opendaylight.controller.cluster.access.commands.ExistsTransactionSuccess;
20 import org.opendaylight.controller.cluster.access.commands.ModifyTransactionRequest;
21 import org.opendaylight.controller.cluster.access.commands.ModifyTransactionSuccess;
22 import org.opendaylight.controller.cluster.access.commands.PersistenceProtocol;
23 import org.opendaylight.controller.cluster.access.commands.ReadTransactionRequest;
24 import org.opendaylight.controller.cluster.access.commands.ReadTransactionSuccess;
25 import org.opendaylight.controller.cluster.access.commands.TransactionAbortRequest;
26 import org.opendaylight.controller.cluster.access.commands.TransactionAbortSuccess;
27 import org.opendaylight.controller.cluster.access.commands.TransactionCanCommitSuccess;
28 import org.opendaylight.controller.cluster.access.commands.TransactionCommitSuccess;
29 import org.opendaylight.controller.cluster.access.commands.TransactionDelete;
30 import org.opendaylight.controller.cluster.access.commands.TransactionDoCommitRequest;
31 import org.opendaylight.controller.cluster.access.commands.TransactionMerge;
32 import org.opendaylight.controller.cluster.access.commands.TransactionModification;
33 import org.opendaylight.controller.cluster.access.commands.TransactionPreCommitRequest;
34 import org.opendaylight.controller.cluster.access.commands.TransactionPreCommitSuccess;
35 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
36 import org.opendaylight.controller.cluster.access.commands.TransactionSuccess;
37 import org.opendaylight.controller.cluster.access.commands.TransactionWrite;
38 import org.opendaylight.controller.cluster.access.concepts.RequestEnvelope;
39 import org.opendaylight.controller.cluster.access.concepts.RequestException;
40 import org.opendaylight.controller.cluster.access.concepts.RuntimeRequestException;
41 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
42 import org.opendaylight.controller.cluster.access.concepts.UnsupportedRequestException;
43 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
45 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 /**
50  * Frontend read-write transaction state as observed by the shard leader.
51  *
52  * @author Robert Varga
53  */
54 @NotThreadSafe
55 final class FrontendReadWriteTransaction extends FrontendTransaction {
56     private static final Logger LOG = LoggerFactory.getLogger(FrontendReadWriteTransaction.class);
57
58     private ReadWriteShardDataTreeTransaction openTransaction;
59     private DataTreeModification sealedModification;
60     private ShardDataTreeCohort readyCohort;
61
62     private FrontendReadWriteTransaction(final AbstractFrontendHistory history, final TransactionIdentifier id,
63             final ReadWriteShardDataTreeTransaction transaction) {
64         super(history, id);
65         this.openTransaction = Preconditions.checkNotNull(transaction);
66     }
67
68     private FrontendReadWriteTransaction(final AbstractFrontendHistory history, final TransactionIdentifier id,
69             final DataTreeModification mod) {
70         super(history, id);
71         this.sealedModification = Preconditions.checkNotNull(mod);
72     }
73
74     static FrontendReadWriteTransaction createOpen(final AbstractFrontendHistory history,
75             final ReadWriteShardDataTreeTransaction transaction) {
76         return new FrontendReadWriteTransaction(history, transaction.getIdentifier(), transaction);
77     }
78
79     static FrontendReadWriteTransaction createReady(final AbstractFrontendHistory history,
80             final TransactionIdentifier id, final DataTreeModification mod) {
81         return new FrontendReadWriteTransaction(history, id, mod);
82     }
83
84     // Sequence has already been checked
85     @Override
86     @Nullable TransactionSuccess<?> handleRequest(final TransactionRequest<?> request, final RequestEnvelope envelope,
87             final long now) throws RequestException {
88         if (request instanceof ModifyTransactionRequest) {
89             return handleModifyTransaction((ModifyTransactionRequest) request, envelope, now);
90         } else if (request instanceof CommitLocalTransactionRequest) {
91             handleCommitLocalTransaction((CommitLocalTransactionRequest) request, envelope, now);
92             return null;
93         } else if (request instanceof ExistsTransactionRequest) {
94             return handleExistsTransaction((ExistsTransactionRequest) request);
95         } else if (request instanceof ReadTransactionRequest) {
96             return handleReadTransaction((ReadTransactionRequest) request);
97         } else if (request instanceof TransactionPreCommitRequest) {
98             handleTransactionPreCommit((TransactionPreCommitRequest) request, envelope, now);
99             return null;
100         } else if (request instanceof TransactionDoCommitRequest) {
101             handleTransactionDoCommit((TransactionDoCommitRequest) request, envelope, now);
102             return null;
103         } else if (request instanceof TransactionAbortRequest) {
104             handleTransactionAbort((TransactionAbortRequest) request, envelope, now);
105             return null;
106         } else {
107             LOG.warn("Rejecting unsupported request {}", request);
108             throw new UnsupportedRequestException(request);
109         }
110     }
111
112     private void handleTransactionPreCommit(final TransactionPreCommitRequest request,
113             final RequestEnvelope envelope, final long now) throws RequestException {
114         readyCohort.preCommit(new FutureCallback<DataTreeCandidate>() {
115             @Override
116             public void onSuccess(final DataTreeCandidate result) {
117                 recordAndSendSuccess(envelope, now, new TransactionPreCommitSuccess(readyCohort.getIdentifier(),
118                     request.getSequence()));
119             }
120
121             @Override
122             public void onFailure(final Throwable failure) {
123                 recordAndSendFailure(envelope, now, new RuntimeRequestException("Precommit failed", failure));
124                 readyCohort = null;
125             }
126         });
127     }
128
129     private void handleTransactionDoCommit(final TransactionDoCommitRequest request, final RequestEnvelope envelope,
130             final long now) throws RequestException {
131         readyCohort.commit(new FutureCallback<UnsignedLong>() {
132             @Override
133             public void onSuccess(final UnsignedLong result) {
134                 successfulCommit(envelope, now);
135             }
136
137             @Override
138             public void onFailure(final Throwable failure) {
139                 recordAndSendFailure(envelope, now, new RuntimeRequestException("Commit failed", failure));
140                 readyCohort = null;
141             }
142         });
143     }
144
145     private void handleTransactionAbort(final TransactionAbortRequest request,
146             final RequestEnvelope envelope, final long now) throws RequestException {
147         if (readyCohort == null) {
148             openTransaction.abort(() -> recordAndSendSuccess(envelope, now,
149                 new TransactionAbortSuccess(getIdentifier(), request.getSequence())));
150             return;
151         }
152
153         readyCohort.abort(new FutureCallback<Void>() {
154             @Override
155             public void onSuccess(final Void result) {
156                 readyCohort = null;
157                 recordAndSendSuccess(envelope, now, new TransactionAbortSuccess(getIdentifier(),
158                     request.getSequence()));
159                 LOG.debug("Transaction {} aborted", getIdentifier());
160             }
161
162             @Override
163             public void onFailure(final Throwable failure) {
164                 readyCohort = null;
165                 LOG.warn("Transaction {} abort failed", getIdentifier(), failure);
166                 recordAndSendFailure(envelope, now, new RuntimeRequestException("Abort failed", failure));
167             }
168         });
169     }
170
171     private void coordinatedCommit(final RequestEnvelope envelope, final long now) {
172         readyCohort.canCommit(new FutureCallback<Void>() {
173             @Override
174             public void onSuccess(final Void result) {
175                 recordAndSendSuccess(envelope, now, new TransactionCanCommitSuccess(readyCohort.getIdentifier(),
176                     envelope.getMessage().getSequence()));
177             }
178
179             @Override
180             public void onFailure(final Throwable failure) {
181                 recordAndSendFailure(envelope, now, new RuntimeRequestException("CanCommit failed", failure));
182                 readyCohort = null;
183             }
184         });
185     }
186
187     private void directCommit(final RequestEnvelope envelope, final long now) {
188         readyCohort.canCommit(new FutureCallback<Void>() {
189             @Override
190             public void onSuccess(final Void result) {
191                 successfulDirectCanCommit(envelope, now);
192             }
193
194             @Override
195             public void onFailure(final Throwable failure) {
196                 recordAndSendFailure(envelope, now, new RuntimeRequestException("CanCommit failed", failure));
197                 readyCohort = null;
198             }
199         });
200     }
201
202     void successfulDirectCanCommit(final RequestEnvelope envelope, final long startTime) {
203         readyCohort.preCommit(new FutureCallback<DataTreeCandidate>() {
204             @Override
205             public void onSuccess(final DataTreeCandidate result) {
206                 successfulDirectPreCommit(envelope, startTime);
207             }
208
209             @Override
210             public void onFailure(final Throwable failure) {
211                 recordAndSendFailure(envelope, startTime, new RuntimeRequestException("PreCommit failed", failure));
212                 readyCohort = null;
213             }
214         });
215     }
216
217     void successfulDirectPreCommit(final RequestEnvelope envelope, final long startTime) {
218         readyCohort.commit(new FutureCallback<UnsignedLong>() {
219             @Override
220             public void onSuccess(final UnsignedLong result) {
221                 successfulCommit(envelope, startTime);
222             }
223
224             @Override
225             public void onFailure(final Throwable failure) {
226                 recordAndSendFailure(envelope, startTime, new RuntimeRequestException("DoCommit failed", failure));
227                 readyCohort = null;
228             }
229         });
230     }
231
232     void successfulCommit(final RequestEnvelope envelope, final long startTime) {
233         recordAndSendSuccess(envelope, startTime, new TransactionCommitSuccess(readyCohort.getIdentifier(),
234             envelope.getMessage().getSequence()));
235         readyCohort = null;
236     }
237
238     private void handleCommitLocalTransaction(final CommitLocalTransactionRequest request,
239             final RequestEnvelope envelope, final long now) throws RequestException {
240         if (!sealedModification.equals(request.getModification())) {
241             LOG.warn("Expecting modification {}, commit request has {}", sealedModification, request.getModification());
242             throw new UnsupportedRequestException(request);
243         }
244
245         final java.util.Optional<Exception> optFailure = request.getDelayedFailure();
246         if (optFailure.isPresent()) {
247             readyCohort = history().createFailedCohort(getIdentifier(), sealedModification, optFailure.get());
248         } else {
249             readyCohort = history().createReadyCohort(getIdentifier(), sealedModification);
250         }
251
252         if (request.isCoordinated()) {
253             coordinatedCommit(envelope, now);
254         } else {
255             directCommit(envelope, now);
256         }
257     }
258
259     private ExistsTransactionSuccess handleExistsTransaction(final ExistsTransactionRequest request)
260             throws RequestException {
261         final Optional<NormalizedNode<?, ?>> data = openTransaction.getSnapshot().readNode(request.getPath());
262         return recordSuccess(request.getSequence(), new ExistsTransactionSuccess(getIdentifier(), request.getSequence(),
263             data.isPresent()));
264     }
265
266     private ReadTransactionSuccess handleReadTransaction(final ReadTransactionRequest request)
267             throws RequestException {
268         final Optional<NormalizedNode<?, ?>> data = openTransaction.getSnapshot().readNode(request.getPath());
269         return recordSuccess(request.getSequence(), new ReadTransactionSuccess(getIdentifier(), request.getSequence(),
270             data));
271     }
272
273     private ModifyTransactionSuccess replyModifySuccess(final long sequence) {
274         return recordSuccess(sequence, new ModifyTransactionSuccess(getIdentifier(), sequence));
275     }
276
277     private @Nullable TransactionSuccess<?> handleModifyTransaction(final ModifyTransactionRequest request,
278             final RequestEnvelope envelope, final long now) throws RequestException {
279
280         final Collection<TransactionModification> mods = request.getModifications();
281         if (!mods.isEmpty()) {
282             final DataTreeModification modification = openTransaction.getSnapshot();
283             for (TransactionModification m : mods) {
284                 if (m instanceof TransactionDelete) {
285                     modification.delete(m.getPath());
286                 } else if (m instanceof TransactionWrite) {
287                     modification.write(m.getPath(), ((TransactionWrite) m).getData());
288                 } else if (m instanceof TransactionMerge) {
289                     modification.merge(m.getPath(), ((TransactionMerge) m).getData());
290                 } else {
291                     LOG.warn("{}: ignoring unhandled modification {}", history().persistenceId(), m);
292                 }
293             }
294         }
295
296         final java.util.Optional<PersistenceProtocol> maybeProto = request.getPersistenceProtocol();
297         if (!maybeProto.isPresent()) {
298             return replyModifySuccess(request.getSequence());
299         }
300
301         switch (maybeProto.get()) {
302             case ABORT:
303                 openTransaction.abort(() -> replyModifySuccess(request.getSequence()));
304                 openTransaction = null;
305                 return null;
306             case READY:
307                 ensureReady();
308                 return replyModifySuccess(request.getSequence());
309             case SIMPLE:
310                 ensureReady();
311                 directCommit(envelope, now);
312                 return null;
313             case THREE_PHASE:
314                 ensureReady();
315                 coordinatedCommit(envelope, now);
316                 return null;
317             default:
318                 LOG.warn("{}: rejecting unsupported protocol {}", history().persistenceId(), maybeProto.get());
319                 throw new UnsupportedRequestException(request);
320         }
321     }
322
323     private void ensureReady() {
324         // We may have a combination of READY + SIMPLE/THREE_PHASE , in which case we want to ready the transaction
325         // only once.
326         if (readyCohort == null) {
327             readyCohort = openTransaction.ready();
328             LOG.debug("{}: transitioned {} to ready", history().persistenceId(), openTransaction.getIdentifier());
329             openTransaction = null;
330         }
331     }
332 }

©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.