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 static java.util.Objects.requireNonNull;
12 import com.google.common.base.MoreObjects;
13 import java.util.HashMap;
14 import java.util.Iterator;
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;
40 * Frontend state as observed by the shard leader. This class is responsible for tracking generations and sequencing
41 * in the frontend/backend conversation.
43 * @author Robert Varga
46 final class LeaderFrontendState implements Identifiable<ClientIdentifier> {
47 private static final Logger LOG = LoggerFactory.getLogger(LeaderFrontendState.class);
49 // Histories which have not been purged
50 private final Map<LocalHistoryIdentifier, LocalFrontendHistory> localHistories;
52 // RangeSet performs automatic merging, hence we keep minimal state tracking information
53 private final UnsignedLongRangeSet purgedHistories;
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;
61 private long lastConnectTicks;
62 private long lastSeenTicks;
63 private long expectedTxSequence;
64 private Long lastSeenHistory = null;
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
70 // TODO: add statistics:
71 // - number of requests processed
72 // - number of histories processed
73 // - per-RequestException throw counters
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<>());
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();
93 public ClientIdentifier getIdentifier() {
97 private void checkRequestSequence(final RequestEnvelope envelope) throws OutOfSequenceEnvelopeException {
98 if (expectedTxSequence != envelope.getTxSequence()) {
99 throw new OutOfSequenceEnvelopeException(expectedTxSequence);
103 private void expectNextRequest() {
104 expectedTxSequence++;
107 @Nullable LocalHistorySuccess handleLocalHistoryRequest(final LocalHistoryRequest<?> request,
108 final RequestEnvelope envelope, final long now) throws RequestException {
109 checkRequestSequence(envelope);
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);
119 LOG.warn("{}: rejecting unsupported request {}", persistenceId, request);
120 throw new UnsupportedRequestException(request);
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());
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());
144 // Update last history we have seen
145 if (lastSeenHistory == null || Long.compareUnsigned(lastSeenHistory, historyId.getHistoryId()) < 0) {
146 lastSeenHistory = historyId.getHistoryId();
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);
155 localHistories.put(historyId, LocalFrontendHistory.create(persistenceId, tree, chain));
156 LOG.debug("{}: created history {}", persistenceId, historyId);
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());
170 existing.destroy(request.getSequence(), envelope, now);
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());
183 LOG.debug("{}: purging history {}", persistenceId, id);
184 purgedHistories.add(id.getHistoryId());
185 existing.purge(request.getSequence(), envelope, now);
189 @Nullable TransactionSuccess<?> handleTransactionRequest(final TransactionRequest<?> request,
190 final RequestEnvelope envelope, final long now) throws RequestException {
191 checkRequestSequence(envelope);
194 final LocalHistoryIdentifier lhId = request.getTarget().getHistoryId();
195 final AbstractFrontendHistory history;
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());
205 LOG.warn("{}: rejecting unknown history request {}", persistenceId, request);
206 throw new UnknownHistoryException(lastSeenHistory);
209 history = standaloneHistory;
212 return history.handleTransactionRequest(request, envelope, now);
219 expectedTxSequence = 0;
220 lastConnectTicks = tree.readTime();
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());
233 LOG.debug("{}: Transaction {} already committing, not retiring it", persistenceId,
234 cohort.getIdentifier());
239 // Clear out all transaction chains
240 localHistories.values().forEach(AbstractFrontendHistory::retire);
241 localHistories.clear();
242 standaloneHistory.retire();
245 long getLastConnectTicks() {
246 return lastConnectTicks;
249 long getLastSeenTicks() {
250 return lastSeenTicks;
254 this.lastSeenTicks = tree.readTime();
258 public String toString() {
259 return MoreObjects.toStringHelper(LeaderFrontendState.class)
260 .add("clientId", clientId)
261 .add("nanosAgo", tree.readTime() - lastSeenTicks)
262 .add("purgedHistories", purgedHistories)