3628efb4acec7d427d03085ba32cd6eb64bc85c2
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / LeaderFrontendState.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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects;
13 import java.util.HashMap;
14 import java.util.Iterator;
15 import java.util.Map;
16 import javax.annotation.concurrent.NotThreadSafe;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.controller.cluster.access.commands.CreateLocalHistoryRequest;
19 import org.opendaylight.controller.cluster.access.commands.DeadHistoryException;
20 import org.opendaylight.controller.cluster.access.commands.DestroyLocalHistoryRequest;
21 import org.opendaylight.controller.cluster.access.commands.LocalHistoryRequest;
22 import org.opendaylight.controller.cluster.access.commands.LocalHistorySuccess;
23 import org.opendaylight.controller.cluster.access.commands.OutOfSequenceEnvelopeException;
24 import org.opendaylight.controller.cluster.access.commands.PurgeLocalHistoryRequest;
25 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
26 import org.opendaylight.controller.cluster.access.commands.TransactionSuccess;
27 import org.opendaylight.controller.cluster.access.commands.UnknownHistoryException;
28 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
29 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
30 import org.opendaylight.controller.cluster.access.concepts.RequestEnvelope;
31 import org.opendaylight.controller.cluster.access.concepts.RequestException;
32 import org.opendaylight.controller.cluster.access.concepts.UnsupportedRequestException;
33 import org.opendaylight.controller.cluster.datastore.ShardDataTreeCohort.State;
34 import org.opendaylight.controller.cluster.datastore.utils.UnsignedLongRangeSet;
35 import org.opendaylight.yangtools.concepts.Identifiable;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * Frontend state as observed by the shard leader. This class is responsible for tracking generations and sequencing
41  * in the frontend/backend conversation.
42  *
43  * @author Robert Varga
44  */
45 @NotThreadSafe
46 final class LeaderFrontendState implements Identifiable<ClientIdentifier> {
47     private static final Logger LOG = LoggerFactory.getLogger(LeaderFrontendState.class);
48
49     // Histories which have not been purged
50     private final Map<LocalHistoryIdentifier, LocalFrontendHistory> localHistories;
51
52     // RangeSet performs automatic merging, hence we keep minimal state tracking information
53     private final UnsignedLongRangeSet purgedHistories;
54
55     // Used for all standalone transactions
56     private final AbstractFrontendHistory standaloneHistory;
57     private final ShardDataTree tree;
58     private final ClientIdentifier clientId;
59     private final String persistenceId;
60
61     private long lastConnectTicks;
62     private long lastSeenTicks;
63     private long expectedTxSequence;
64     private Long lastSeenHistory = null;
65
66     // TODO: explicit failover notification
67     //       Record the ActorRef for the originating actor and when we switch to being a leader send a notification
68     //       to the frontend client -- that way it can immediately start sending requests
69
70     // TODO: add statistics:
71     // - number of requests processed
72     // - number of histories processed
73     // - per-RequestException throw counters
74
75     LeaderFrontendState(final String persistenceId, final ClientIdentifier clientId, final ShardDataTree tree) {
76         this(persistenceId, clientId, tree, UnsignedLongRangeSet.create(),
77             StandaloneFrontendHistory.create(persistenceId, clientId, tree), new HashMap<>());
78     }
79
80     LeaderFrontendState(final String persistenceId, final ClientIdentifier clientId, final ShardDataTree tree,
81         final UnsignedLongRangeSet purgedHistories, final AbstractFrontendHistory standaloneHistory,
82         final Map<LocalHistoryIdentifier, LocalFrontendHistory> localHistories) {
83         this.persistenceId = requireNonNull(persistenceId);
84         this.clientId = requireNonNull(clientId);
85         this.tree = requireNonNull(tree);
86         this.purgedHistories = requireNonNull(purgedHistories);
87         this.standaloneHistory = requireNonNull(standaloneHistory);
88         this.localHistories = requireNonNull(localHistories);
89         this.lastSeenTicks = tree.readTime();
90     }
91
92     @Override
93     public ClientIdentifier getIdentifier() {
94         return clientId;
95     }
96
97     private void checkRequestSequence(final RequestEnvelope envelope) throws OutOfSequenceEnvelopeException {
98         if (expectedTxSequence != envelope.getTxSequence()) {
99             throw new OutOfSequenceEnvelopeException(expectedTxSequence);
100         }
101     }
102
103     private void expectNextRequest() {
104         expectedTxSequence++;
105     }
106
107     @Nullable LocalHistorySuccess handleLocalHistoryRequest(final LocalHistoryRequest<?> request,
108             final RequestEnvelope envelope, final long now) throws RequestException {
109         checkRequestSequence(envelope);
110
111         try {
112             if (request instanceof CreateLocalHistoryRequest) {
113                 return handleCreateHistory((CreateLocalHistoryRequest) request, envelope, now);
114             } else if (request instanceof DestroyLocalHistoryRequest) {
115                 return handleDestroyHistory((DestroyLocalHistoryRequest) request, envelope, now);
116             } else if (request instanceof PurgeLocalHistoryRequest) {
117                 return handlePurgeHistory((PurgeLocalHistoryRequest)request, envelope, now);
118             } else {
119                 LOG.warn("{}: rejecting unsupported request {}", persistenceId, request);
120                 throw new UnsupportedRequestException(request);
121             }
122         } finally {
123             expectNextRequest();
124         }
125     }
126
127     private LocalHistorySuccess handleCreateHistory(final CreateLocalHistoryRequest request,
128             final RequestEnvelope envelope, final long now) throws RequestException {
129         final LocalHistoryIdentifier historyId = request.getTarget();
130         final AbstractFrontendHistory existing = localHistories.get(historyId);
131         if (existing != null) {
132             // History already exists: report success
133             LOG.debug("{}: history {} already exists", persistenceId, historyId);
134             return new LocalHistorySuccess(historyId, request.getSequence());
135         }
136
137         // We have not found the history. Before we create it we need to check history ID sequencing so that we do not
138         // end up resurrecting a purged history.
139         if (purgedHistories.contains(historyId.getHistoryId())) {
140             LOG.debug("{}: rejecting purged request {}", persistenceId, request);
141             throw new DeadHistoryException(purgedHistories.toImmutable());
142         }
143
144         // Update last history we have seen
145         if (lastSeenHistory == null || Long.compareUnsigned(lastSeenHistory, historyId.getHistoryId()) < 0) {
146             lastSeenHistory = historyId.getHistoryId();
147         }
148
149         // We have to send the response only after persistence has completed
150         final ShardDataTreeTransactionChain chain = tree.ensureTransactionChain(historyId, () -> {
151             LOG.debug("{}: persisted history {}", persistenceId, historyId);
152             envelope.sendSuccess(new LocalHistorySuccess(historyId, request.getSequence()), tree.readTime() - now);
153         });
154
155         localHistories.put(historyId, LocalFrontendHistory.create(persistenceId, tree, chain));
156         LOG.debug("{}: created history {}", persistenceId, historyId);
157         return null;
158     }
159
160     private LocalHistorySuccess handleDestroyHistory(final DestroyLocalHistoryRequest request,
161             final RequestEnvelope envelope, final long now) {
162         final LocalHistoryIdentifier id = request.getTarget();
163         final LocalFrontendHistory existing = localHistories.get(id);
164         if (existing == null) {
165             // History does not exist: report success
166             LOG.debug("{}: history {} does not exist, nothing to destroy", persistenceId, id);
167             return new LocalHistorySuccess(id, request.getSequence());
168         }
169
170         existing.destroy(request.getSequence(), envelope, now);
171         return null;
172     }
173
174     private LocalHistorySuccess handlePurgeHistory(final PurgeLocalHistoryRequest request,
175             final RequestEnvelope envelope, final long now) {
176         final LocalHistoryIdentifier id = request.getTarget();
177         final LocalFrontendHistory existing = localHistories.remove(id);
178         if (existing == null) {
179             LOG.debug("{}: history {} has already been purged", persistenceId, id);
180             return new LocalHistorySuccess(id, request.getSequence());
181         }
182
183         LOG.debug("{}: purging history {}", persistenceId, id);
184         purgedHistories.add(id.getHistoryId());
185         existing.purge(request.getSequence(), envelope, now);
186         return null;
187     }
188
189     @Nullable TransactionSuccess<?> handleTransactionRequest(final TransactionRequest<?> request,
190             final RequestEnvelope envelope, final long now) throws RequestException {
191         checkRequestSequence(envelope);
192
193         try {
194             final LocalHistoryIdentifier lhId = request.getTarget().getHistoryId();
195             final AbstractFrontendHistory history;
196
197             if (lhId.getHistoryId() != 0) {
198                 history = localHistories.get(lhId);
199                 if (history == null) {
200                     if (purgedHistories.contains(lhId.getHistoryId())) {
201                         LOG.warn("{}: rejecting request {} to purged history", persistenceId, request);
202                         throw new DeadHistoryException(purgedHistories.toImmutable());
203                     }
204
205                     LOG.warn("{}: rejecting unknown history request {}", persistenceId, request);
206                     throw new UnknownHistoryException(lastSeenHistory);
207                 }
208             } else {
209                 history = standaloneHistory;
210             }
211
212             return history.handleTransactionRequest(request, envelope, now);
213         } finally {
214             expectNextRequest();
215         }
216     }
217
218     void reconnect() {
219         expectedTxSequence = 0;
220         lastConnectTicks = tree.readTime();
221     }
222
223     void retire() {
224         // Hunt down any transactions associated with this frontend
225         final Iterator<SimpleShardDataTreeCohort> it = tree.cohortIterator();
226         while (it.hasNext()) {
227             final SimpleShardDataTreeCohort cohort = it.next();
228             if (clientId.equals(cohort.getIdentifier().getHistoryId().getClientId())) {
229                 if (cohort.getState() != State.COMMIT_PENDING) {
230                     LOG.debug("{}: Retiring transaction {}", persistenceId, cohort.getIdentifier());
231                     it.remove();
232                 } else {
233                     LOG.debug("{}: Transaction {} already committing, not retiring it", persistenceId,
234                         cohort.getIdentifier());
235                 }
236             }
237         }
238
239         // Clear out all transaction chains
240         localHistories.values().forEach(AbstractFrontendHistory::retire);
241         localHistories.clear();
242         standaloneHistory.retire();
243     }
244
245     long getLastConnectTicks() {
246         return lastConnectTicks;
247     }
248
249     long getLastSeenTicks() {
250         return lastSeenTicks;
251     }
252
253     void touch() {
254         this.lastSeenTicks = tree.readTime();
255     }
256
257     @Override
258     public String toString() {
259         return MoreObjects.toStringHelper(LeaderFrontendState.class)
260                 .add("clientId", clientId)
261                 .add("nanosAgo", tree.readTime() - lastSeenTicks)
262                 .add("purgedHistories", purgedHistories)
263                 .toString();
264     }
265 }