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 java.util.HashMap;
13 import java.util.Iterator;
15 import javax.annotation.Nullable;
16 import javax.annotation.concurrent.NotThreadSafe;
17 import org.opendaylight.controller.cluster.access.commands.CreateLocalHistoryRequest;
18 import org.opendaylight.controller.cluster.access.commands.DeadHistoryException;
19 import org.opendaylight.controller.cluster.access.commands.DestroyLocalHistoryRequest;
20 import org.opendaylight.controller.cluster.access.commands.LocalHistoryRequest;
21 import org.opendaylight.controller.cluster.access.commands.LocalHistorySuccess;
22 import org.opendaylight.controller.cluster.access.commands.OutOfSequenceEnvelopeException;
23 import org.opendaylight.controller.cluster.access.commands.PurgeLocalHistoryRequest;
24 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
25 import org.opendaylight.controller.cluster.access.commands.TransactionSuccess;
26 import org.opendaylight.controller.cluster.access.commands.UnknownHistoryException;
27 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
28 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
29 import org.opendaylight.controller.cluster.access.concepts.RequestEnvelope;
30 import org.opendaylight.controller.cluster.access.concepts.RequestException;
31 import org.opendaylight.controller.cluster.access.concepts.UnsupportedRequestException;
32 import org.opendaylight.controller.cluster.datastore.ShardDataTreeCohort.State;
33 import org.opendaylight.controller.cluster.datastore.utils.UnsignedLongRangeSet;
34 import org.opendaylight.yangtools.concepts.Identifiable;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * Frontend state as observed by the shard leader. This class is responsible for tracking generations and sequencing
40 * in the frontend/backend conversation.
42 * @author Robert Varga
45 final class LeaderFrontendState implements Identifiable<ClientIdentifier> {
46 private static final Logger LOG = LoggerFactory.getLogger(LeaderFrontendState.class);
48 // Histories which have not been purged
49 private final Map<LocalHistoryIdentifier, LocalFrontendHistory> localHistories;
51 // RangeSet performs automatic merging, hence we keep minimal state tracking information
52 private final UnsignedLongRangeSet purgedHistories;
54 // Used for all standalone transactions
55 private final AbstractFrontendHistory standaloneHistory;
56 private final ShardDataTree tree;
57 private final ClientIdentifier clientId;
58 private final String persistenceId;
60 private long expectedTxSequence;
61 private Long lastSeenHistory = null;
63 // TODO: explicit failover notification
64 // Record the ActorRef for the originating actor and when we switch to being a leader send a notification
65 // to the frontend client -- that way it can immediately start sending requests
67 // TODO: add statistics:
68 // - number of requests processed
69 // - number of histories processed
70 // - per-RequestException throw counters
72 LeaderFrontendState(final String persistenceId, final ClientIdentifier clientId, final ShardDataTree tree) {
73 this(persistenceId, clientId, tree, UnsignedLongRangeSet.create(),
74 StandaloneFrontendHistory.create(persistenceId, clientId, tree), new HashMap<>());
77 LeaderFrontendState(final String persistenceId, final ClientIdentifier clientId, final ShardDataTree tree,
78 final UnsignedLongRangeSet purgedHistories, final AbstractFrontendHistory standaloneHistory,
79 final Map<LocalHistoryIdentifier, LocalFrontendHistory> localHistories) {
80 this.persistenceId = Preconditions.checkNotNull(persistenceId);
81 this.clientId = Preconditions.checkNotNull(clientId);
82 this.tree = Preconditions.checkNotNull(tree);
83 this.purgedHistories = Preconditions.checkNotNull(purgedHistories);
84 this.standaloneHistory = Preconditions.checkNotNull(standaloneHistory);
85 this.localHistories = Preconditions.checkNotNull(localHistories);
89 public ClientIdentifier getIdentifier() {
93 private void checkRequestSequence(final RequestEnvelope envelope) throws OutOfSequenceEnvelopeException {
94 if (expectedTxSequence != envelope.getTxSequence()) {
95 throw new OutOfSequenceEnvelopeException(expectedTxSequence);
99 private void expectNextRequest() {
100 expectedTxSequence++;
103 @Nullable LocalHistorySuccess handleLocalHistoryRequest(final LocalHistoryRequest<?> request,
104 final RequestEnvelope envelope, final long now) throws RequestException {
105 checkRequestSequence(envelope);
108 if (request instanceof CreateLocalHistoryRequest) {
109 return handleCreateHistory((CreateLocalHistoryRequest) request, envelope, now);
110 } else if (request instanceof DestroyLocalHistoryRequest) {
111 return handleDestroyHistory((DestroyLocalHistoryRequest) request, envelope, now);
112 } else if (request instanceof PurgeLocalHistoryRequest) {
113 return handlePurgeHistory((PurgeLocalHistoryRequest)request, envelope, now);
115 LOG.warn("{}: rejecting unsupported request {}", persistenceId, request);
116 throw new UnsupportedRequestException(request);
123 private LocalHistorySuccess handleCreateHistory(final CreateLocalHistoryRequest request,
124 final RequestEnvelope envelope, final long now) throws RequestException {
125 final LocalHistoryIdentifier historyId = request.getTarget();
126 final AbstractFrontendHistory existing = localHistories.get(historyId);
127 if (existing != null) {
128 // History already exists: report success
129 LOG.debug("{}: history {} already exists", persistenceId, historyId);
130 return new LocalHistorySuccess(historyId, request.getSequence());
133 // We have not found the history. Before we create it we need to check history ID sequencing so that we do not
134 // end up resurrecting a purged history.
135 if (purgedHistories.contains(historyId.getHistoryId())) {
136 LOG.debug("{}: rejecting purged request {}", persistenceId, request);
137 throw new DeadHistoryException(purgedHistories.toImmutable());
140 // Update last history we have seen
141 if (lastSeenHistory == null || Long.compareUnsigned(lastSeenHistory, historyId.getHistoryId()) < 0) {
142 lastSeenHistory = historyId.getHistoryId();
145 // We have to send the response only after persistence has completed
146 final ShardDataTreeTransactionChain chain = tree.ensureTransactionChain(historyId, () -> {
147 LOG.debug("{}: persisted history {}", persistenceId, historyId);
148 envelope.sendSuccess(new LocalHistorySuccess(historyId, request.getSequence()), tree.readTime() - now);
151 localHistories.put(historyId, LocalFrontendHistory.create(persistenceId, tree, chain));
152 LOG.debug("{}: created history {}", persistenceId, historyId);
156 private LocalHistorySuccess handleDestroyHistory(final DestroyLocalHistoryRequest request,
157 final RequestEnvelope envelope, final long now)
158 throws RequestException {
159 final LocalHistoryIdentifier id = request.getTarget();
160 final LocalFrontendHistory existing = localHistories.get(id);
161 if (existing == null) {
162 // History does not exist: report success
163 LOG.debug("{}: history {} does not exist, nothing to destroy", persistenceId, id);
164 return new LocalHistorySuccess(id, request.getSequence());
167 existing.destroy(request.getSequence(), envelope, now);
171 private LocalHistorySuccess handlePurgeHistory(final PurgeLocalHistoryRequest request,
172 final RequestEnvelope envelope, final long now) throws RequestException {
173 final LocalHistoryIdentifier id = request.getTarget();
174 final LocalFrontendHistory existing = localHistories.remove(id);
175 if (existing == null) {
176 LOG.debug("{}: history {} has already been purged", persistenceId, id);
177 return new LocalHistorySuccess(id, request.getSequence());
180 LOG.debug("{}: purging history {}", persistenceId, id);
181 purgedHistories.add(id.getHistoryId());
182 existing.purge(request.getSequence(), envelope, now);
186 @Nullable TransactionSuccess<?> handleTransactionRequest(final TransactionRequest<?> request,
187 final RequestEnvelope envelope, final long now) throws RequestException {
188 checkRequestSequence(envelope);
191 final LocalHistoryIdentifier lhId = request.getTarget().getHistoryId();
192 final AbstractFrontendHistory history;
194 if (lhId.getHistoryId() != 0) {
195 history = localHistories.get(lhId);
196 if (history == null) {
197 if (purgedHistories.contains(lhId.getHistoryId())) {
198 LOG.warn("{}: rejecting request {} to purged history", persistenceId, request);
199 throw new DeadHistoryException(purgedHistories.toImmutable());
202 LOG.warn("{}: rejecting unknown history request {}", persistenceId, request);
203 throw new UnknownHistoryException(lastSeenHistory);
206 history = standaloneHistory;
209 return history.handleTransactionRequest(request, envelope, now);
216 expectedTxSequence = 0;
220 // Hunt down any transactions associated with this frontend
221 final Iterator<SimpleShardDataTreeCohort> it = tree.cohortIterator();
222 while (it.hasNext()) {
223 final SimpleShardDataTreeCohort cohort = it.next();
224 if (clientId.equals(cohort.getIdentifier().getHistoryId().getClientId())) {
225 if (cohort.getState() != State.COMMIT_PENDING) {
226 LOG.debug("{}: Retiring transaction {}", persistenceId, cohort.getIdentifier());
229 LOG.debug("{}: Transaction {} already committing, not retiring it", persistenceId,
230 cohort.getIdentifier());
235 // Clear out all transaction chains
236 localHistories.values().forEach(AbstractFrontendHistory::retire);
237 localHistories.clear();
238 standaloneHistory.retire();
242 public String toString() {
243 return MoreObjects.toStringHelper(LeaderFrontendState.class).add("clientId", clientId)
244 .add("purgedHistories", purgedHistories).toString();