1b15c728a8e14106f182b1933f628d2657e201e5
[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             handleTransactionAbort((TransactionAbortRequest) request, envelope, now);
174             return null;
175         } else {
176             throw new UnsupportedRequestException(request);
177         }
178     }
179
180     private void recordResponse(final long sequence, final Object response) {
181         if (replayQueue.isEmpty()) {
182             firstReplaySequence = sequence;
183         }
184         replayQueue.add(response);
185         expectedSequence++;
186     }
187
188     private <T extends TransactionSuccess<?>> T recordSuccess(final long sequence, final T success) {
189         recordResponse(sequence, success);
190         return success;
191     }
192
193     private long executionTime(final long startTime) {
194         return history.readTime() - startTime;
195     }
196
197     private void recordAndSendSuccess(final RequestEnvelope envelope, final long startTime,
198             final TransactionSuccess<?> success) {
199         recordResponse(success.getSequence(), success);
200         envelope.sendSuccess(success, executionTime(startTime));
201     }
202
203     private void recordAndSendFailure(final RequestEnvelope envelope, final long startTime,
204             final RuntimeRequestException failure) {
205         recordResponse(envelope.getMessage().getSequence(), failure);
206         envelope.sendFailure(failure, executionTime(startTime));
207     }
208
209     private void handleTransactionPreCommit(final TransactionPreCommitRequest request,
210             final RequestEnvelope envelope, final long now) throws RequestException {
211         readyCohort.preCommit(new FutureCallback<DataTreeCandidate>() {
212             @Override
213             public void onSuccess(final DataTreeCandidate result) {
214                 recordAndSendSuccess(envelope, now, new TransactionPreCommitSuccess(readyCohort.getIdentifier(),
215                     request.getSequence()));
216             }
217
218             @Override
219             public void onFailure(final Throwable failure) {
220                 recordAndSendFailure(envelope, now, new RuntimeRequestException("Precommit failed", failure));
221                 readyCohort = null;
222             }
223         });
224     }
225
226     private void handleTransactionDoCommit(final TransactionDoCommitRequest request, final RequestEnvelope envelope,
227             final long now) throws RequestException {
228         readyCohort.commit(new FutureCallback<UnsignedLong>() {
229             @Override
230             public void onSuccess(final UnsignedLong result) {
231                 successfulCommit(envelope, now);
232             }
233
234             @Override
235             public void onFailure(final Throwable failure) {
236                 recordAndSendFailure(envelope, now, new RuntimeRequestException("Commit failed", failure));
237                 readyCohort = null;
238             }
239         });
240     }
241
242     private void handleTransactionAbort(final TransactionAbortRequest request, final RequestEnvelope envelope,
243             final long now) throws RequestException {
244         readyCohort.abort(new FutureCallback<Void>() {
245             @Override
246             public void onSuccess(final Void result) {
247                 readyCohort = null;
248                 recordAndSendSuccess(envelope, now, new TransactionAbortSuccess(id, request.getSequence()));
249                 LOG.debug("Transaction {} aborted", id);
250             }
251
252             @Override
253             public void onFailure(final Throwable failure) {
254                 readyCohort = null;
255                 LOG.warn("Transaction {} abort failed", id, failure);
256                 recordAndSendFailure(envelope, now, new RuntimeRequestException("Abort failed", failure));
257             }
258         });
259     }
260
261     private void coordinatedCommit(final RequestEnvelope envelope, final long now) {
262         readyCohort.canCommit(new FutureCallback<Void>() {
263             @Override
264             public void onSuccess(final Void result) {
265                 recordAndSendSuccess(envelope, now, new TransactionCanCommitSuccess(readyCohort.getIdentifier(),
266                     envelope.getMessage().getSequence()));
267             }
268
269             @Override
270             public void onFailure(final Throwable failure) {
271                 recordAndSendFailure(envelope, now, new RuntimeRequestException("CanCommit failed", failure));
272                 readyCohort = null;
273             }
274         });
275     }
276
277     private void directCommit(final RequestEnvelope envelope, final long now) {
278         readyCohort.canCommit(new FutureCallback<Void>() {
279             @Override
280             public void onSuccess(final Void result) {
281                 successfulDirectCanCommit(envelope, now);
282             }
283
284             @Override
285             public void onFailure(final Throwable failure) {
286                 recordAndSendFailure(envelope, now, new RuntimeRequestException("CanCommit failed", failure));
287                 readyCohort = null;
288             }
289         });
290
291     }
292
293     private void successfulDirectCanCommit(final RequestEnvelope envelope, final long startTime) {
294         readyCohort.preCommit(new FutureCallback<DataTreeCandidate>() {
295             @Override
296             public void onSuccess(final DataTreeCandidate result) {
297                 successfulDirectPreCommit(envelope, startTime);
298             }
299
300             @Override
301             public void onFailure(final Throwable failure) {
302                 recordAndSendFailure(envelope, startTime, new RuntimeRequestException("PreCommit failed", failure));
303                 readyCohort = null;
304             }
305         });
306     }
307
308     private void successfulDirectPreCommit(final RequestEnvelope envelope, final long startTime) {
309         readyCohort.commit(new FutureCallback<UnsignedLong>() {
310
311             @Override
312             public void onSuccess(final UnsignedLong result) {
313                 successfulCommit(envelope, startTime);
314             }
315
316             @Override
317             public void onFailure(final Throwable failure) {
318                 recordAndSendFailure(envelope, startTime, new RuntimeRequestException("DoCommit failed", failure));
319                 readyCohort = null;
320             }
321         });
322     }
323
324     private void successfulCommit(final RequestEnvelope envelope, final long startTime) {
325         recordAndSendSuccess(envelope, startTime, new TransactionCommitSuccess(readyCohort.getIdentifier(),
326             envelope.getMessage().getSequence()));
327         readyCohort = null;
328     }
329
330     private void handleCommitLocalTransaction(final CommitLocalTransactionRequest request,
331             final RequestEnvelope envelope, final long now) throws RequestException {
332         if (sealedModification.equals(request.getModification())) {
333             readyCohort = history.createReadyCohort(id, sealedModification);
334
335             if (request.isCoordinated()) {
336                 coordinatedCommit(envelope, now);
337             } else {
338                 directCommit(envelope, now);
339             }
340         } else {
341             throw new UnsupportedRequestException(request);
342         }
343     }
344
345     private ExistsTransactionSuccess handleExistsTransaction(final ExistsTransactionRequest request)
346             throws RequestException {
347         final Optional<NormalizedNode<?, ?>> data = openTransaction.getSnapshot().readNode(request.getPath());
348         return recordSuccess(request.getSequence(), new ExistsTransactionSuccess(id, request.getSequence(),
349             data.isPresent()));
350     }
351
352     private ReadTransactionSuccess handleReadTransaction(final ReadTransactionRequest request)
353             throws RequestException {
354         final Optional<NormalizedNode<?, ?>> data = openTransaction.getSnapshot().readNode(request.getPath());
355         return recordSuccess(request.getSequence(), new ReadTransactionSuccess(id, request.getSequence(), data));
356     }
357
358     private ModifyTransactionSuccess replyModifySuccess(final long sequence) {
359         if (cachedModifySuccess == null) {
360             cachedModifySuccess = new ModifyTransactionSuccess(id, sequence);
361         }
362
363         return recordSuccess(sequence, cachedModifySuccess);
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 }