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