2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore;
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;
54 * Frontend transaction state as observed by the shard leader.
56 * @author Robert Varga
59 final class FrontendTransaction {
60 private static final Logger LOG = LoggerFactory.getLogger(FrontendTransaction.class);
62 private final AbstractFrontendHistory history;
63 private final TransactionIdentifier id;
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.
70 private final Queue<Object> replayQueue = new ArrayDeque<>();
71 private long firstReplaySequence;
72 private Long lastPurgedSequence;
73 private long expectedSequence;
75 private ReadWriteShardDataTreeTransaction openTransaction;
76 private DataTreeModification sealedModification;
77 private ShardDataTreeCohort readyCohort;
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);
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);
93 static FrontendTransaction createOpen(final AbstractFrontendHistory history,
94 final ReadWriteShardDataTreeTransaction transaction) {
95 return new FrontendTransaction(history, transaction.getIdentifier(), transaction);
98 static FrontendTransaction createReady(final AbstractFrontendHistory history, final TransactionIdentifier id,
99 final DataTreeModification mod) {
100 return new FrontendTransaction(history, id, mod);
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();
109 // Check sequencing: we do not need to bother with future requests
110 if (Long.compareUnsigned(expectedSequence, sequence) < 0) {
111 throw new OutOfOrderRequestException(expectedSequence);
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));
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.
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;
136 Verify.verify(replay instanceof TransactionSuccess);
137 return java.util.Optional.of((TransactionSuccess<?>) replay);
144 return java.util.Optional.empty();
147 void purgeSequencesUpTo(final long sequence) {
148 // FIXME: implement this
150 lastPurgedSequence = sequence;
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);
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);
168 } else if (request instanceof TransactionDoCommitRequest) {
169 handleTransactionDoCommit((TransactionDoCommitRequest) request, envelope, now);
171 } else if (request instanceof TransactionAbortRequest) {
172 return handleTransactionAbort((TransactionAbortRequest) request, envelope, now);
174 throw new UnsupportedRequestException(request);
178 private void recordResponse(final long sequence, final Object response) {
179 if (replayQueue.isEmpty()) {
180 firstReplaySequence = sequence;
182 replayQueue.add(response);
186 private <T extends TransactionSuccess<?>> T recordSuccess(final long sequence, final T success) {
187 recordResponse(sequence, success);
191 private long executionTime(final long startTime) {
192 return history.readTime() - startTime;
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));
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));
207 private void handleTransactionPreCommit(final TransactionPreCommitRequest request,
208 final RequestEnvelope envelope, final long now) throws RequestException {
209 readyCohort.preCommit(new FutureCallback<DataTreeCandidate>() {
211 public void onSuccess(final DataTreeCandidate result) {
212 recordAndSendSuccess(envelope, now, new TransactionPreCommitSuccess(readyCohort.getIdentifier(),
213 request.getSequence()));
217 public void onFailure(final Throwable failure) {
218 recordAndSendFailure(envelope, now, new RuntimeRequestException("Precommit failed", failure));
224 private void handleTransactionDoCommit(final TransactionDoCommitRequest request, final RequestEnvelope envelope,
225 final long now) throws RequestException {
226 readyCohort.commit(new FutureCallback<UnsignedLong>() {
228 public void onSuccess(final UnsignedLong result) {
229 successfulCommit(envelope, now);
233 public void onFailure(final Throwable failure) {
234 recordAndSendFailure(envelope, now, new RuntimeRequestException("Commit failed", failure));
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());
247 readyCohort.abort(new FutureCallback<Void>() {
249 public void onSuccess(final Void result) {
251 recordAndSendSuccess(envelope, now, new TransactionAbortSuccess(id, request.getSequence()));
252 LOG.debug("Transaction {} aborted", id);
256 public void onFailure(final Throwable failure) {
258 LOG.warn("Transaction {} abort failed", id, failure);
259 recordAndSendFailure(envelope, now, new RuntimeRequestException("Abort failed", failure));
265 private void coordinatedCommit(final RequestEnvelope envelope, final long now) {
266 readyCohort.canCommit(new FutureCallback<Void>() {
268 public void onSuccess(final Void result) {
269 recordAndSendSuccess(envelope, now, new TransactionCanCommitSuccess(readyCohort.getIdentifier(),
270 envelope.getMessage().getSequence()));
274 public void onFailure(final Throwable failure) {
275 recordAndSendFailure(envelope, now, new RuntimeRequestException("CanCommit failed", failure));
281 private void directCommit(final RequestEnvelope envelope, final long now) {
282 readyCohort.canCommit(new FutureCallback<Void>() {
284 public void onSuccess(final Void result) {
285 successfulDirectCanCommit(envelope, now);
289 public void onFailure(final Throwable failure) {
290 recordAndSendFailure(envelope, now, new RuntimeRequestException("CanCommit failed", failure));
297 private void successfulDirectCanCommit(final RequestEnvelope envelope, final long startTime) {
298 readyCohort.preCommit(new FutureCallback<DataTreeCandidate>() {
300 public void onSuccess(final DataTreeCandidate result) {
301 successfulDirectPreCommit(envelope, startTime);
305 public void onFailure(final Throwable failure) {
306 recordAndSendFailure(envelope, startTime, new RuntimeRequestException("PreCommit failed", failure));
312 private void successfulDirectPreCommit(final RequestEnvelope envelope, final long startTime) {
313 readyCohort.commit(new FutureCallback<UnsignedLong>() {
316 public void onSuccess(final UnsignedLong result) {
317 successfulCommit(envelope, startTime);
321 public void onFailure(final Throwable failure) {
322 recordAndSendFailure(envelope, startTime, new RuntimeRequestException("DoCommit failed", failure));
328 private void successfulCommit(final RequestEnvelope envelope, final long startTime) {
329 recordAndSendSuccess(envelope, startTime, new TransactionCommitSuccess(readyCohort.getIdentifier(),
330 envelope.getMessage().getSequence()));
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);
339 if (request.isCoordinated()) {
340 coordinatedCommit(envelope, now);
342 directCommit(envelope, now);
345 throw new UnsupportedRequestException(request);
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(),
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));
362 private ModifyTransactionSuccess replyModifySuccess(final long sequence) {
363 return recordSuccess(sequence, new ModifyTransactionSuccess(id, sequence));
366 private @Nullable TransactionSuccess<?> handleModifyTransaction(final ModifyTransactionRequest request,
367 final RequestEnvelope envelope, final long now) throws RequestException {
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());
378 LOG.warn("{}: ignoring unhandled modification {}", history.persistenceId(), m);
382 final java.util.Optional<PersistenceProtocol> maybeProto = request.getPersistenceProtocol();
383 if (!maybeProto.isPresent()) {
384 return replyModifySuccess(request.getSequence());
387 switch (maybeProto.get()) {
389 openTransaction.abort();
390 openTransaction = null;
391 return replyModifySuccess(request.getSequence());
393 readyCohort = openTransaction.ready();
394 openTransaction = null;
395 directCommit(envelope, now);
398 readyCohort = openTransaction.ready();
399 openTransaction = null;
400 coordinatedCommit(envelope, now);
403 throw new UnsupportedRequestException(request);