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.MoreObjects;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Range;
13 import com.google.common.collect.RangeSet;
14 import com.google.common.collect.TreeRangeSet;
15 import com.google.common.primitives.UnsignedLong;
16 import java.util.HashMap;
17 import java.util.Iterator;
19 import javax.annotation.Nullable;
20 import javax.annotation.concurrent.NotThreadSafe;
21 import org.opendaylight.controller.cluster.access.commands.CreateLocalHistoryRequest;
22 import org.opendaylight.controller.cluster.access.commands.DeadHistoryException;
23 import org.opendaylight.controller.cluster.access.commands.DestroyLocalHistoryRequest;
24 import org.opendaylight.controller.cluster.access.commands.LocalHistoryRequest;
25 import org.opendaylight.controller.cluster.access.commands.LocalHistorySuccess;
26 import org.opendaylight.controller.cluster.access.commands.OutOfSequenceEnvelopeException;
27 import org.opendaylight.controller.cluster.access.commands.PurgeLocalHistoryRequest;
28 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
29 import org.opendaylight.controller.cluster.access.commands.TransactionSuccess;
30 import org.opendaylight.controller.cluster.access.commands.UnknownHistoryException;
31 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
32 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
33 import org.opendaylight.controller.cluster.access.concepts.RequestEnvelope;
34 import org.opendaylight.controller.cluster.access.concepts.RequestException;
35 import org.opendaylight.controller.cluster.access.concepts.UnsupportedRequestException;
36 import org.opendaylight.controller.cluster.datastore.ShardDataTreeCohort.State;
37 import org.opendaylight.yangtools.concepts.Identifiable;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * Frontend state as observed by the shard leader. This class is responsible for tracking generations and sequencing
43 * in the frontend/backend conversation.
45 * @author Robert Varga
48 final class LeaderFrontendState implements Identifiable<ClientIdentifier> {
49 private static final Logger LOG = LoggerFactory.getLogger(LeaderFrontendState.class);
51 // Histories which have not been purged
52 private final Map<LocalHistoryIdentifier, LocalFrontendHistory> localHistories;
54 // RangeSet performs automatic merging, hence we keep minimal state tracking information
55 private final RangeSet<UnsignedLong> purgedHistories;
57 // Used for all standalone transactions
58 private final AbstractFrontendHistory standaloneHistory;
59 private final ShardDataTree tree;
60 private final ClientIdentifier clientId;
61 private final String persistenceId;
63 private long lastConnectTicks;
64 private long lastSeenTicks;
65 private long expectedTxSequence;
66 private Long lastSeenHistory = null;
68 // TODO: explicit failover notification
69 // Record the ActorRef for the originating actor and when we switch to being a leader send a notification
70 // to the frontend client -- that way it can immediately start sending requests
72 // TODO: add statistics:
73 // - number of requests processed
74 // - number of histories processed
75 // - per-RequestException throw counters
77 LeaderFrontendState(final String persistenceId, final ClientIdentifier clientId, final ShardDataTree tree) {
78 this(persistenceId, clientId, tree, TreeRangeSet.create(), StandaloneFrontendHistory.create(persistenceId,
79 clientId, tree), new HashMap<>());
82 LeaderFrontendState(final String persistenceId, final ClientIdentifier clientId, final ShardDataTree tree,
83 final RangeSet<UnsignedLong> purgedHistories, final AbstractFrontendHistory standaloneHistory,
84 final Map<LocalHistoryIdentifier, LocalFrontendHistory> localHistories) {
85 this.persistenceId = Preconditions.checkNotNull(persistenceId);
86 this.clientId = Preconditions.checkNotNull(clientId);
87 this.tree = Preconditions.checkNotNull(tree);
88 this.purgedHistories = Preconditions.checkNotNull(purgedHistories);
89 this.standaloneHistory = Preconditions.checkNotNull(standaloneHistory);
90 this.localHistories = Preconditions.checkNotNull(localHistories);
91 this.lastSeenTicks = tree.readTime();
95 public ClientIdentifier getIdentifier() {
99 private void checkRequestSequence(final RequestEnvelope envelope) throws OutOfSequenceEnvelopeException {
100 if (expectedTxSequence != envelope.getTxSequence()) {
101 throw new OutOfSequenceEnvelopeException(expectedTxSequence);
105 private void expectNextRequest() {
106 expectedTxSequence++;
109 @Nullable LocalHistorySuccess handleLocalHistoryRequest(final LocalHistoryRequest<?> request,
110 final RequestEnvelope envelope, final long now) throws RequestException {
111 checkRequestSequence(envelope);
114 if (request instanceof CreateLocalHistoryRequest) {
115 return handleCreateHistory((CreateLocalHistoryRequest) request, envelope, now);
116 } else if (request instanceof DestroyLocalHistoryRequest) {
117 return handleDestroyHistory((DestroyLocalHistoryRequest) request, envelope, now);
118 } else if (request instanceof PurgeLocalHistoryRequest) {
119 return handlePurgeHistory((PurgeLocalHistoryRequest)request, envelope, now);
121 LOG.warn("{}: rejecting unsupported request {}", persistenceId, request);
122 throw new UnsupportedRequestException(request);
129 private LocalHistorySuccess handleCreateHistory(final CreateLocalHistoryRequest request,
130 final RequestEnvelope envelope, final long now) throws RequestException {
131 final LocalHistoryIdentifier historyId = request.getTarget();
132 final AbstractFrontendHistory existing = localHistories.get(historyId);
133 if (existing != null) {
134 // History already exists: report success
135 LOG.debug("{}: history {} already exists", persistenceId, historyId);
136 return new LocalHistorySuccess(historyId, request.getSequence());
139 // We have not found the history. Before we create it we need to check history ID sequencing so that we do not
140 // end up resurrecting a purged history.
141 if (purgedHistories.contains(UnsignedLong.fromLongBits(historyId.getHistoryId()))) {
142 LOG.debug("{}: rejecting purged request {}", persistenceId, request);
143 throw new DeadHistoryException(purgedHistories);
146 // Update last history we have seen
147 if (lastSeenHistory == null || Long.compareUnsigned(lastSeenHistory, historyId.getHistoryId()) < 0) {
148 lastSeenHistory = historyId.getHistoryId();
151 // We have to send the response only after persistence has completed
152 final ShardDataTreeTransactionChain chain = tree.ensureTransactionChain(historyId, () -> {
153 LOG.debug("{}: persisted history {}", persistenceId, historyId);
154 envelope.sendSuccess(new LocalHistorySuccess(historyId, request.getSequence()), tree.readTime() - now);
157 localHistories.put(historyId, LocalFrontendHistory.create(persistenceId, tree, chain));
158 LOG.debug("{}: created history {}", persistenceId, historyId);
162 private LocalHistorySuccess handleDestroyHistory(final DestroyLocalHistoryRequest request,
163 final RequestEnvelope envelope, final long now)
164 throws RequestException {
165 final LocalHistoryIdentifier id = request.getTarget();
166 final LocalFrontendHistory existing = localHistories.get(id);
167 if (existing == null) {
168 // History does not exist: report success
169 LOG.debug("{}: history {} does not exist, nothing to destroy", persistenceId, id);
170 return new LocalHistorySuccess(id, request.getSequence());
173 existing.destroy(request.getSequence(), envelope, now);
177 private LocalHistorySuccess handlePurgeHistory(final PurgeLocalHistoryRequest request,
178 final RequestEnvelope envelope, final long now) throws RequestException {
179 final LocalHistoryIdentifier id = request.getTarget();
180 final LocalFrontendHistory existing = localHistories.remove(id);
181 if (existing == null) {
182 LOG.debug("{}: history {} has already been purged", persistenceId, id);
183 return new LocalHistorySuccess(id, request.getSequence());
186 LOG.debug("{}: purging history {}", persistenceId, id);
187 final UnsignedLong ul = UnsignedLong.fromLongBits(id.getHistoryId());
188 purgedHistories.add(Range.closedOpen(ul, UnsignedLong.ONE.plus(ul)));
189 existing.purge(request.getSequence(), envelope, now);
193 @Nullable TransactionSuccess<?> handleTransactionRequest(final TransactionRequest<?> request,
194 final RequestEnvelope envelope, final long now) throws RequestException {
195 checkRequestSequence(envelope);
198 final LocalHistoryIdentifier lhId = request.getTarget().getHistoryId();
199 final AbstractFrontendHistory history;
201 if (lhId.getHistoryId() != 0) {
202 history = localHistories.get(lhId);
203 if (history == null) {
204 if (purgedHistories.contains(UnsignedLong.fromLongBits(lhId.getHistoryId()))) {
205 LOG.warn("{}: rejecting request {} to purged history", persistenceId, request);
206 throw new DeadHistoryException(purgedHistories);
209 LOG.warn("{}: rejecting unknown history request {}", persistenceId, request);
210 throw new UnknownHistoryException(lastSeenHistory);
213 history = standaloneHistory;
216 return history.handleTransactionRequest(request, envelope, now);
223 expectedTxSequence = 0;
224 lastConnectTicks = tree.readTime();
228 // Hunt down any transactions associated with this frontend
229 final Iterator<SimpleShardDataTreeCohort> it = tree.cohortIterator();
230 while (it.hasNext()) {
231 final SimpleShardDataTreeCohort cohort = it.next();
232 if (clientId.equals(cohort.getIdentifier().getHistoryId().getClientId())) {
233 if (cohort.getState() != State.COMMIT_PENDING) {
234 LOG.debug("{}: Retiring transaction {}", persistenceId, cohort.getIdentifier());
237 LOG.debug("{}: Transaction {} already committing, not retiring it", persistenceId,
238 cohort.getIdentifier());
243 // Clear out all transaction chains
244 localHistories.values().forEach(AbstractFrontendHistory::retire);
245 localHistories.clear();
246 standaloneHistory.retire();
249 long getLastConnectTicks() {
250 return lastConnectTicks;
253 long getLastSeenTicks() {
254 return lastSeenTicks;
258 this.lastSeenTicks = tree.readTime();
262 public String toString() {
263 return MoreObjects.toStringHelper(LeaderFrontendState.class)
264 .add("clientId", clientId)
265 .add("nanosAgo", tree.readTime() - lastSeenTicks)
266 .add("purgedHistories", purgedHistories)