6c7ae07a3cf890c8cba9db65eba9e3fec349a405
[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.MoreObjects;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Verify;
13 import java.util.ArrayDeque;
14 import java.util.Iterator;
15 import java.util.Queue;
16 import javax.annotation.Nullable;
17 import javax.annotation.concurrent.NotThreadSafe;
18 import org.opendaylight.controller.cluster.access.commands.IncrementTransactionSequenceRequest;
19 import org.opendaylight.controller.cluster.access.commands.IncrementTransactionSequenceSuccess;
20 import org.opendaylight.controller.cluster.access.commands.OutOfOrderRequestException;
21 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
22 import org.opendaylight.controller.cluster.access.commands.TransactionSuccess;
23 import org.opendaylight.controller.cluster.access.concepts.RequestEnvelope;
24 import org.opendaylight.controller.cluster.access.concepts.RequestException;
25 import org.opendaylight.controller.cluster.access.concepts.RuntimeRequestException;
26 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
27 import org.opendaylight.yangtools.concepts.Identifiable;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * Frontend common transaction state as observed by the shard leader.
33  *
34  * @author Robert Varga
35  */
36 @NotThreadSafe
37 abstract class FrontendTransaction implements Identifiable<TransactionIdentifier> {
38     private static final Logger LOG = LoggerFactory.getLogger(FrontendTransaction.class);
39
40     private final AbstractFrontendHistory history;
41     private final TransactionIdentifier id;
42
43     /**
44      * It is possible that after we process a request and send a response that response gets lost and the client
45      * initiates a retry. Since subsequent requests can mutate transaction state we need to retain the response until
46      * it is acknowledged by the client.
47      */
48     private final Queue<Object> replayQueue = new ArrayDeque<>();
49     private long firstReplaySequence;
50     private Long lastPurgedSequence;
51     private long expectedSequence;
52
53     private RequestException previousFailure;
54
55     FrontendTransaction(final AbstractFrontendHistory history, final TransactionIdentifier id) {
56         this.history = Preconditions.checkNotNull(history);
57         this.id = Preconditions.checkNotNull(id);
58     }
59
60     @Override
61     public final TransactionIdentifier getIdentifier() {
62         return id;
63     }
64
65     final AbstractFrontendHistory history() {
66         return history;
67     }
68
69     final String persistenceId() {
70         return history().persistenceId();
71     }
72
73     final java.util.Optional<TransactionSuccess<?>> replaySequence(final long sequence) throws RequestException {
74         // Fast path check: if the requested sequence is the next request, bail early
75         if (expectedSequence == sequence) {
76             return java.util.Optional.empty();
77         }
78
79         // Check sequencing: we do not need to bother with future requests
80         if (Long.compareUnsigned(expectedSequence, sequence) < 0) {
81             throw new OutOfOrderRequestException(expectedSequence);
82         }
83
84         // Sanity check: if we have purged sequences, this has to be newer
85         if (lastPurgedSequence != null && Long.compareUnsigned(lastPurgedSequence.longValue(), sequence) >= 0) {
86             // Client has sent a request sequence, which has already been purged. This is a hard error, which should
87             // never occur. Throwing an IllegalArgumentException will cause it to be wrapped in a
88             // RuntimeRequestException (which is not retriable) and report it back to the client.
89             throw new IllegalArgumentException(String.format("Invalid purged sequence %s (last purged is %s)",
90                 sequence, lastPurgedSequence));
91         }
92
93         // At this point we have established that the requested sequence lies in the open interval
94         // (lastPurgedSequence, expectedSequence). That does not actually mean we have a response, as the commit
95         // machinery is asynchronous, hence a reply may be in the works and not available.
96
97         long replaySequence = firstReplaySequence;
98         final Iterator<?> it = replayQueue.iterator();
99         while (it.hasNext()) {
100             final Object replay = it.next();
101             if (replaySequence == sequence) {
102                 if (replay instanceof RequestException) {
103                     throw (RequestException) replay;
104                 }
105
106                 Verify.verify(replay instanceof TransactionSuccess);
107                 return java.util.Optional.of((TransactionSuccess<?>) replay);
108             }
109
110             replaySequence++;
111         }
112
113         // Not found
114         return java.util.Optional.empty();
115     }
116
117     final void purgeSequencesUpTo(final long sequence) {
118         // FIXME: implement this
119
120         lastPurgedSequence = sequence;
121     }
122
123     // Request order has already been checked by caller and replaySequence()
124     @SuppressWarnings("checkstyle:IllegalCatch")
125     final @Nullable TransactionSuccess<?> handleRequest(final TransactionRequest<?> request,
126             final RequestEnvelope envelope, final long now) throws RequestException {
127         if (request instanceof IncrementTransactionSequenceRequest) {
128             final IncrementTransactionSequenceRequest incr = (IncrementTransactionSequenceRequest) request;
129             expectedSequence += incr.getIncrement();
130
131             return recordSuccess(incr.getSequence(), new IncrementTransactionSequenceSuccess(incr.getTarget(),
132                 incr.getSequence()));
133         }
134
135         if (previousFailure != null) {
136             LOG.debug("{}: Rejecting request {} due to previous failure", persistenceId(), request, previousFailure);
137             throw previousFailure;
138         }
139
140         try {
141             return doHandleRequest(request, envelope, now);
142         } catch (RuntimeException e) {
143             /*
144              * The request failed to process, we should not attempt to ever apply it again. Furthermore we cannot
145              * accept any further requests from this connection, simply because the transaction state is undefined.
146              */
147             LOG.debug("{}: Request {} failed to process", persistenceId(), request, e);
148             previousFailure = new RuntimeRequestException("Request " + request + " failed to process", e);
149             throw previousFailure;
150         }
151     }
152
153     abstract @Nullable TransactionSuccess<?> doHandleRequest(TransactionRequest<?> request, RequestEnvelope envelope,
154             long now) throws RequestException;
155
156     private void recordResponse(final long sequence, final Object response) {
157         if (replayQueue.isEmpty()) {
158             firstReplaySequence = sequence;
159         }
160         replayQueue.add(response);
161         expectedSequence++;
162     }
163
164     final <T extends TransactionSuccess<?>> T recordSuccess(final long sequence, final T success) {
165         recordResponse(sequence, success);
166         return success;
167     }
168
169     private long executionTime(final long startTime) {
170         return history.readTime() - startTime;
171     }
172
173     final void recordAndSendSuccess(final RequestEnvelope envelope, final long startTime,
174             final TransactionSuccess<?> success) {
175         recordResponse(success.getSequence(), success);
176         envelope.sendSuccess(success, executionTime(startTime));
177     }
178
179     final void recordAndSendFailure(final RequestEnvelope envelope, final long startTime,
180             final RuntimeRequestException failure) {
181         recordResponse(envelope.getMessage().getSequence(), failure);
182         envelope.sendFailure(failure, executionTime(startTime));
183     }
184
185     @Override
186     public final String toString() {
187         return MoreObjects.toStringHelper(this).omitNullValues().add("identifier", getIdentifier())
188                 .add("expectedSequence", expectedSequence).add("firstReplaySequence", firstReplaySequence)
189                 .add("lastPurgedSequence", lastPurgedSequence)
190                 .toString();
191     }
192
193 }