a825a4ddee58842318270aaa94269e7c2b37e5bf
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / FrontendTransaction.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.base.Verify;
13 import com.google.common.primitives.UnsignedLong;
14 import com.google.common.util.concurrent.FutureCallback;
15 import java.util.ArrayDeque;
16 import java.util.Iterator;
17 import java.util.Queue;
18 import javax.annotation.Nullable;
19 import javax.annotation.concurrent.NotThreadSafe;
20 import org.opendaylight.controller.cluster.access.commands.CommitLocalTransactionRequest;
21 import org.opendaylight.controller.cluster.access.commands.ExistsTransactionRequest;
22 import org.opendaylight.controller.cluster.access.commands.ExistsTransactionSuccess;
23 import org.opendaylight.controller.cluster.access.commands.ModifyTransactionRequest;
24 import org.opendaylight.controller.cluster.access.commands.ModifyTransactionSuccess;
25 import org.opendaylight.controller.cluster.access.commands.OutOfOrderRequestException;
26 import org.opendaylight.controller.cluster.access.commands.PersistenceProtocol;
27 import org.opendaylight.controller.cluster.access.commands.ReadTransactionRequest;
28 import org.opendaylight.controller.cluster.access.commands.ReadTransactionSuccess;
29 import org.opendaylight.controller.cluster.access.commands.TransactionAbortRequest;
30 import org.opendaylight.controller.cluster.access.commands.TransactionAbortSuccess;
31 import org.opendaylight.controller.cluster.access.commands.TransactionCanCommitSuccess;
32 import org.opendaylight.controller.cluster.access.commands.TransactionCommitSuccess;
33 import org.opendaylight.controller.cluster.access.commands.TransactionDelete;
34 import org.opendaylight.controller.cluster.access.commands.TransactionDoCommitRequest;
35 import org.opendaylight.controller.cluster.access.commands.TransactionMerge;
36 import org.opendaylight.controller.cluster.access.commands.TransactionModification;
37 import org.opendaylight.controller.cluster.access.commands.TransactionPreCommitRequest;
38 import org.opendaylight.controller.cluster.access.commands.TransactionPreCommitSuccess;
39 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
40 import org.opendaylight.controller.cluster.access.commands.TransactionSuccess;
41 import org.opendaylight.controller.cluster.access.commands.TransactionWrite;
42 import org.opendaylight.controller.cluster.access.concepts.RequestEnvelope;
43 import org.opendaylight.controller.cluster.access.concepts.RequestException;
44 import org.opendaylight.controller.cluster.access.concepts.RuntimeRequestException;
45 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
46 import org.opendaylight.controller.cluster.access.concepts.UnsupportedRequestException;
47 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
49 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 /**
54  * Frontend transaction state as observed by the shard leader.
55  *
56  * @author Robert Varga
57  */
58 @NotThreadSafe
59 final class FrontendTransaction {
60     private static final Logger LOG = LoggerFactory.getLogger(FrontendTransaction.class);
61
62     private final AbstractFrontendHistory history;
63     private final TransactionIdentifier id;
64
65     /**
66      * It is possible that after we process a request and send a response that response gets lost and the client
67      * initiates a retry. Since subsequent requests can mutate transaction state we need to retain the response until
68      * it is acknowledged by the client.
69      */
70     private final Queue<Object> replayQueue = new ArrayDeque<>();
71     private long firstReplaySequence;
72     private Long lastPurgedSequence;
73     private long expectedSequence;
74
75     private ReadWriteShardDataTreeTransaction openTransaction;
76     private ModifyTransactionSuccess cachedModifySuccess;
77     private DataTreeModification sealedModification;
78     private ShardDataTreeCohort readyCohort;
79
80     private FrontendTransaction(final AbstractFrontendHistory history, final TransactionIdentifier id,
81             final ReadWriteShardDataTreeTransaction transaction) {
82         this.history = Preconditions.checkNotNull(history);
83         this.id = Preconditions.checkNotNull(id);
84         this.openTransaction = Preconditions.checkNotNull(transaction);
85     }
86
87     private FrontendTransaction(final AbstractFrontendHistory history, final TransactionIdentifier id,
88             final DataTreeModification mod) {
89         this.history = Preconditions.checkNotNull(history);
90         this.id = Preconditions.checkNotNull(id);
91         this.sealedModification = Preconditions.checkNotNull(mod);
92     }
93
94     static FrontendTransaction createOpen(final AbstractFrontendHistory history,
95             final ReadWriteShardDataTreeTransaction transaction) {
96         return new FrontendTransaction(history, transaction.getIdentifier(), transaction);
97     }
98
99     static FrontendTransaction createReady(final AbstractFrontendHistory history, final TransactionIdentifier id,
100             final DataTreeModification mod) {
101         return new FrontendTransaction(history, id, mod);
102     }
103
104     java.util.Optional<TransactionSuccess<?>> replaySequence(final long sequence) throws RequestException {
105         // Fast path check: if the requested sequence is the next request, bail early
106         if (expectedSequence == sequence) {
107             return java.util.Optional.empty();
108         }
109
110         // Check sequencing: we do not need to bother with future requests
111         if (Long.compareUnsigned(expectedSequence, sequence) < 0) {
112             throw new OutOfOrderRequestException(expectedSequence);
113         }
114
115         // Sanity check: if we have purged sequences, this has to be newer
116         if (lastPurgedSequence != null && Long.compareUnsigned(lastPurgedSequence.longValue(), sequence) >= 0) {
117             // Client has sent a request sequence, which has already been purged. This is a hard error, which should
118             // never occur. Throwing an IllegalArgumentException will cause it to be wrapped in a
119             // RuntimeRequestException (which is not retriable) and report it back to the client.
120             throw new IllegalArgumentException(String.format("Invalid purged sequence %s (last purged is %s)",
121                 sequence, lastPurgedSequence));
122         }
123
124         // At this point we have established that the requested sequence lies in the open interval
125         // (lastPurgedSequence, expectedSequence). That does not actually mean we have a response, as the commit
126         // machinery is asynchronous, hence a reply may be in the works and not available.
127
128         long replaySequence = firstReplaySequence;
129         final Iterator<?> it = replayQueue.iterator();
130         while (it.hasNext()) {
131             final Object replay = it.next();
132             if (replaySequence == sequence) {
133                 if (replay instanceof RequestException) {
134                     throw (RequestException) replay;
135                 }
136
137                 Verify.verify(replay instanceof TransactionSuccess);
138                 return java.util.Optional.of((TransactionSuccess<?>) replay);
139             }
140
141             replaySequence++;
142         }
143
144         // Not found
145         return java.util.Optional.empty();
146     }
147
148     void purgeSequencesUpTo(final long sequence) {
149         // FIXME: implement this
150
151         lastPurgedSequence = sequence;
152     }
153
154     // Sequence has already been checked
155     @Nullable TransactionSuccess<?> handleRequest(final TransactionRequest<?> request, final RequestEnvelope envelope,
156             final long now) throws RequestException {
157         if (request instanceof ModifyTransactionRequest) {
158             return handleModifyTransaction((ModifyTransactionRequest) request, envelope, now);
159         } else if (request instanceof CommitLocalTransactionRequest) {
160             handleCommitLocalTransaction((CommitLocalTransactionRequest) request, envelope, now);
161             return null;
162         } else if (request instanceof ExistsTransactionRequest) {
163             return handleExistsTransaction((ExistsTransactionRequest) request);
164         } else if (request instanceof ReadTransactionRequest) {
165             return handleReadTransaction((ReadTransactionRequest) request);
166         } else if (request instanceof TransactionPreCommitRequest) {
167             handleTransactionPreCommit((TransactionPreCommitRequest) request, envelope, now);
168             return null;
169         } else if (request instanceof TransactionDoCommitRequest) {
170             handleTransactionDoCommit((TransactionDoCommitRequest) request, envelope, now);
171             return null;
172         } else if (request instanceof TransactionAbortRequest) {
173             return handleTransactionAbort((TransactionAbortRequest) request, envelope, now);
174         } else {
175             throw new UnsupportedRequestException(request);
176         }
177     }
178
179     private void recordResponse(final long sequence, final Object response) {
180         if (replayQueue.isEmpty()) {
181             firstReplaySequence = sequence;
182         }
183         replayQueue.add(response);
184         expectedSequence++;
185     }
186
187     private <T extends TransactionSuccess<?>> T recordSuccess(final long sequence, final T success) {
188         recordResponse(sequence, success);
189         return success;
190     }
191
192     private long executionTime(final long startTime) {
193         return history.readTime() - startTime;
194     }
195
196     private void recordAndSendSuccess(final RequestEnvelope envelope, final long startTime,
197             final TransactionSuccess<?> success) {
198         recordResponse(success.getSequence(), success);
199         envelope.sendSuccess(success, executionTime(startTime));
200     }
201
202     private void recordAndSendFailure(final RequestEnvelope envelope, final long startTime,
203             final RuntimeRequestException failure) {
204         recordResponse(envelope.getMessage().getSequence(), failure);
205         envelope.sendFailure(failure, executionTime(startTime));
206     }
207
208     private void handleTransactionPreCommit(final TransactionPreCommitRequest request,
209             final RequestEnvelope envelope, final long now) throws RequestException {
210         readyCohort.preCommit(new FutureCallback<DataTreeCandidate>() {
211             @Override
212             public void onSuccess(final DataTreeCandidate result) {
213                 recordAndSendSuccess(envelope, now, new TransactionPreCommitSuccess(readyCohort.getIdentifier(),
214                     request.getSequence()));
215             }
216
217             @Override
218             public void onFailure(final Throwable failure) {
219                 recordAndSendFailure(envelope, now, new RuntimeRequestException("Precommit failed", failure));
220                 readyCohort = null;
221             }
222         });
223     }
224
225     private void handleTransactionDoCommit(final TransactionDoCommitRequest request, final RequestEnvelope envelope,
226             final long now) throws RequestException {
227         readyCohort.commit(new FutureCallback<UnsignedLong>() {
228             @Override
229             public void onSuccess(final UnsignedLong result) {
230                 successfulCommit(envelope, now);
231             }
232
233             @Override
234             public void onFailure(final Throwable failure) {
235                 recordAndSendFailure(envelope, now, new RuntimeRequestException("Commit failed", failure));
236                 readyCohort = null;
237             }
238         });
239     }
240
241     private TransactionSuccess<?> handleTransactionAbort(final TransactionAbortRequest request,
242             final RequestEnvelope envelope, final long now) throws RequestException {
243         if (readyCohort == null) {
244             openTransaction.abort();
245             return new TransactionAbortSuccess(id, request.getSequence());
246         }
247
248         readyCohort.abort(new FutureCallback<Void>() {
249             @Override
250             public void onSuccess(final Void result) {
251                 readyCohort = null;
252                 recordAndSendSuccess(envelope, now, new TransactionAbortSuccess(id, request.getSequence()));
253                 LOG.debug("Transaction {} aborted", id);
254             }
255
256             @Override
257             public void onFailure(final Throwable failure) {
258                 readyCohort = null;
259                 LOG.warn("Transaction {} abort failed", id, failure);
260                 recordAndSendFailure(envelope, now, new RuntimeRequestException("Abort failed", failure));
261             }
262         });
263         return null;
264     }
265
266     private void coordinatedCommit(final RequestEnvelope envelope, final long now) {
267         readyCohort.canCommit(new FutureCallback<Void>() {
268             @Override
269             public void onSuccess(final Void result) {
270                 recordAndSendSuccess(envelope, now, new TransactionCanCommitSuccess(readyCohort.getIdentifier(),
271                     envelope.getMessage().getSequence()));
272             }
273
274             @Override
275             public void onFailure(final Throwable failure) {
276                 recordAndSendFailure(envelope, now, new RuntimeRequestException("CanCommit failed", failure));
277                 readyCohort = null;
278             }
279         });
280     }
281
282     private void directCommit(final RequestEnvelope envelope, final long now) {
283         readyCohort.canCommit(new FutureCallback<Void>() {
284             @Override
285             public void onSuccess(final Void result) {
286                 successfulDirectCanCommit(envelope, now);
287             }
288
289             @Override
290             public void onFailure(final Throwable failure) {
291                 recordAndSendFailure(envelope, now, new RuntimeRequestException("CanCommit failed", failure));
292                 readyCohort = null;
293             }
294         });
295
296     }
297
298     private void successfulDirectCanCommit(final RequestEnvelope envelope, final long startTime) {
299         readyCohort.preCommit(new FutureCallback<DataTreeCandidate>() {
300             @Override
301             public void onSuccess(final DataTreeCandidate result) {
302                 successfulDirectPreCommit(envelope, startTime);
303             }
304
305             @Override
306             public void onFailure(final Throwable failure) {
307                 recordAndSendFailure(envelope, startTime, new RuntimeRequestException("PreCommit failed", failure));
308                 readyCohort = null;
309             }
310         });
311     }
312
313     private void successfulDirectPreCommit(final RequestEnvelope envelope, final long startTime) {
314         readyCohort.commit(new FutureCallback<UnsignedLong>() {
315
316             @Override
317             public void onSuccess(final UnsignedLong result) {
318                 successfulCommit(envelope, startTime);
319             }
320
321             @Override
322             public void onFailure(final Throwable failure) {
323                 recordAndSendFailure(envelope, startTime, new RuntimeRequestException("DoCommit failed", failure));
324                 readyCohort = null;
325             }
326         });
327     }
328
329     private void successfulCommit(final RequestEnvelope envelope, final long startTime) {
330         recordAndSendSuccess(envelope, startTime, new TransactionCommitSuccess(readyCohort.getIdentifier(),
331             envelope.getMessage().getSequence()));
332         readyCohort = null;
333     }
334
335     private void handleCommitLocalTransaction(final CommitLocalTransactionRequest request,
336             final RequestEnvelope envelope, final long now) throws RequestException {
337         if (sealedModification.equals(request.getModification())) {
338             readyCohort = history.createReadyCohort(id, sealedModification);
339
340             if (request.isCoordinated()) {
341                 coordinatedCommit(envelope, now);
342             } else {
343                 directCommit(envelope, now);
344             }
345         } else {
346             throw new UnsupportedRequestException(request);
347         }
348     }
349
350     private ExistsTransactionSuccess handleExistsTransaction(final ExistsTransactionRequest request)
351             throws RequestException {
352         final Optional<NormalizedNode<?, ?>> data = openTransaction.getSnapshot().readNode(request.getPath());
353         return recordSuccess(request.getSequence(), new ExistsTransactionSuccess(id, request.getSequence(),
354             data.isPresent()));
355     }
356
357     private ReadTransactionSuccess handleReadTransaction(final ReadTransactionRequest request)
358             throws RequestException {
359         final Optional<NormalizedNode<?, ?>> data = openTransaction.getSnapshot().readNode(request.getPath());
360         return recordSuccess(request.getSequence(), new ReadTransactionSuccess(id, request.getSequence(), data));
361     }
362
363     private ModifyTransactionSuccess replyModifySuccess(final long sequence) {
364         if (cachedModifySuccess == null) {
365             cachedModifySuccess = new ModifyTransactionSuccess(id, sequence);
366         }
367
368         return recordSuccess(sequence, cachedModifySuccess);
369     }
370
371     private @Nullable TransactionSuccess<?> handleModifyTransaction(final ModifyTransactionRequest request,
372             final RequestEnvelope envelope, final long now) throws RequestException {
373
374         final DataTreeModification modification = openTransaction.getSnapshot();
375         for (TransactionModification m : request.getModifications()) {
376             if (m instanceof TransactionDelete) {
377                 modification.delete(m.getPath());
378             } else if (m instanceof TransactionWrite) {
379                 modification.write(m.getPath(), ((TransactionWrite) m).getData());
380             } else if (m instanceof TransactionMerge) {
381                 modification.merge(m.getPath(), ((TransactionMerge) m).getData());
382             } else {
383                 LOG.warn("{}: ignoring unhandled modification {}", history.persistenceId(), m);
384             }
385         }
386
387         final java.util.Optional<PersistenceProtocol> maybeProto = request.getPersistenceProtocol();
388         if (!maybeProto.isPresent()) {
389             return replyModifySuccess(request.getSequence());
390         }
391
392         switch (maybeProto.get()) {
393             case ABORT:
394                 openTransaction.abort();
395                 openTransaction = null;
396                 return replyModifySuccess(request.getSequence());
397             case SIMPLE:
398                 readyCohort = openTransaction.ready();
399                 openTransaction = null;
400                 directCommit(envelope, now);
401                 return null;
402             case THREE_PHASE:
403                 readyCohort = openTransaction.ready();
404                 openTransaction = null;
405                 coordinatedCommit(envelope, now);
406                 return null;
407             default:
408                 throw new UnsupportedRequestException(request);
409         }
410     }
411 }