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;
14 import javax.annotation.Nullable;
15 import javax.annotation.concurrent.NotThreadSafe;
16 import org.opendaylight.controller.cluster.access.commands.CreateLocalHistoryRequest;
17 import org.opendaylight.controller.cluster.access.commands.DeadHistoryException;
18 import org.opendaylight.controller.cluster.access.commands.DestroyLocalHistoryRequest;
19 import org.opendaylight.controller.cluster.access.commands.LocalHistoryRequest;
20 import org.opendaylight.controller.cluster.access.commands.LocalHistorySuccess;
21 import org.opendaylight.controller.cluster.access.commands.OutOfSequenceEnvelopeException;
22 import org.opendaylight.controller.cluster.access.commands.PurgeLocalHistoryRequest;
23 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
24 import org.opendaylight.controller.cluster.access.commands.TransactionSuccess;
25 import org.opendaylight.controller.cluster.access.commands.UnknownHistoryException;
26 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
27 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
28 import org.opendaylight.controller.cluster.access.concepts.RequestEnvelope;
29 import org.opendaylight.controller.cluster.access.concepts.RequestException;
30 import org.opendaylight.controller.cluster.access.concepts.UnsupportedRequestException;
31 import org.opendaylight.controller.cluster.datastore.utils.UnsignedLongRangeSet;
32 import org.opendaylight.yangtools.concepts.Identifiable;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Frontend state as observed by the shard leader. This class is responsible for tracking generations and sequencing
38 * in the frontend/backend conversation.
40 * @author Robert Varga
43 final class LeaderFrontendState implements Identifiable<ClientIdentifier> {
44 private static final Logger LOG = LoggerFactory.getLogger(LeaderFrontendState.class);
46 // Histories which have not been purged
47 private final Map<LocalHistoryIdentifier, LocalFrontendHistory> localHistories;
49 // RangeSet performs automatic merging, hence we keep minimal state tracking information
50 private final UnsignedLongRangeSet purgedHistories;
52 // Used for all standalone transactions
53 private final AbstractFrontendHistory standaloneHistory;
54 private final ShardDataTree tree;
55 private final ClientIdentifier clientId;
56 private final String persistenceId;
58 private long expectedTxSequence;
59 private Long lastSeenHistory = null;
61 // TODO: explicit failover notification
62 // Record the ActorRef for the originating actor and when we switch to being a leader send a notification
63 // to the frontend client -- that way it can immediately start sending requests
65 // TODO: add statistics:
66 // - number of requests processed
67 // - number of histories processed
68 // - per-RequestException throw counters
70 LeaderFrontendState(final String persistenceId, final ClientIdentifier clientId, final ShardDataTree tree) {
71 this(persistenceId, clientId, tree, UnsignedLongRangeSet.create(),
72 StandaloneFrontendHistory.create(persistenceId, clientId, tree), new HashMap<>());
75 LeaderFrontendState(final String persistenceId, final ClientIdentifier clientId, final ShardDataTree tree,
76 final UnsignedLongRangeSet purgedHistories, final AbstractFrontendHistory standaloneHistory,
77 final Map<LocalHistoryIdentifier, LocalFrontendHistory> localHistories) {
78 this.persistenceId = Preconditions.checkNotNull(persistenceId);
79 this.clientId = Preconditions.checkNotNull(clientId);
80 this.tree = Preconditions.checkNotNull(tree);
81 this.purgedHistories = Preconditions.checkNotNull(purgedHistories);
82 this.standaloneHistory = Preconditions.checkNotNull(standaloneHistory);
83 this.localHistories = Preconditions.checkNotNull(localHistories);
87 public ClientIdentifier getIdentifier() {
91 private void checkRequestSequence(final RequestEnvelope envelope) throws OutOfSequenceEnvelopeException {
92 if (expectedTxSequence != envelope.getTxSequence()) {
93 throw new OutOfSequenceEnvelopeException(expectedTxSequence);
97 private void expectNextRequest() {
101 @Nullable LocalHistorySuccess handleLocalHistoryRequest(final LocalHistoryRequest<?> request,
102 final RequestEnvelope envelope, final long now) throws RequestException {
103 checkRequestSequence(envelope);
106 if (request instanceof CreateLocalHistoryRequest) {
107 return handleCreateHistory((CreateLocalHistoryRequest) request, envelope, now);
108 } else if (request instanceof DestroyLocalHistoryRequest) {
109 return handleDestroyHistory((DestroyLocalHistoryRequest) request, envelope, now);
110 } else if (request instanceof PurgeLocalHistoryRequest) {
111 return handlePurgeHistory((PurgeLocalHistoryRequest)request, envelope, now);
113 LOG.warn("{}: rejecting unsupported request {}", persistenceId, request);
114 throw new UnsupportedRequestException(request);
121 private LocalHistorySuccess handleCreateHistory(final CreateLocalHistoryRequest request,
122 final RequestEnvelope envelope, final long now) throws RequestException {
123 final LocalHistoryIdentifier historyId = request.getTarget();
124 final AbstractFrontendHistory existing = localHistories.get(historyId);
125 if (existing != null) {
126 // History already exists: report success
127 LOG.debug("{}: history {} already exists", persistenceId, historyId);
128 return new LocalHistorySuccess(historyId, request.getSequence());
131 // We have not found the history. Before we create it we need to check history ID sequencing so that we do not
132 // end up resurrecting a purged history.
133 if (purgedHistories.contains(historyId.getHistoryId())) {
134 LOG.debug("{}: rejecting purged request {}", persistenceId, request);
135 throw new DeadHistoryException(purgedHistories.toImmutable());
138 // Update last history we have seen
139 if (lastSeenHistory == null || Long.compareUnsigned(lastSeenHistory, historyId.getHistoryId()) < 0) {
140 lastSeenHistory = historyId.getHistoryId();
143 // We have to send the response only after persistence has completed
144 final ShardDataTreeTransactionChain chain = tree.ensureTransactionChain(historyId, () -> {
145 LOG.debug("{}: persisted history {}", persistenceId, historyId);
146 envelope.sendSuccess(new LocalHistorySuccess(historyId, request.getSequence()), tree.readTime() - now);
149 localHistories.put(historyId, LocalFrontendHistory.create(persistenceId, tree, chain));
150 LOG.debug("{}: created history {}", persistenceId, historyId);
154 private LocalHistorySuccess handleDestroyHistory(final DestroyLocalHistoryRequest request,
155 final RequestEnvelope envelope, final long now)
156 throws RequestException {
157 final LocalHistoryIdentifier id = request.getTarget();
158 final LocalFrontendHistory existing = localHistories.get(id);
159 if (existing == null) {
160 // History does not exist: report success
161 LOG.debug("{}: history {} does not exist, nothing to destroy", persistenceId, id);
162 return new LocalHistorySuccess(id, request.getSequence());
165 existing.destroy(request.getSequence(), envelope, now);
169 private LocalHistorySuccess handlePurgeHistory(final PurgeLocalHistoryRequest request,
170 final RequestEnvelope envelope, final long now) throws RequestException {
171 final LocalHistoryIdentifier id = request.getTarget();
172 final LocalFrontendHistory existing = localHistories.remove(id);
173 if (existing == null) {
174 LOG.debug("{}: history {} has already been purged", persistenceId, id);
175 return new LocalHistorySuccess(id, request.getSequence());
178 LOG.debug("{}: purging history {}", persistenceId, id);
179 purgedHistories.add(id.getHistoryId());
180 existing.purge(request.getSequence(), envelope, now);
184 @Nullable TransactionSuccess<?> handleTransactionRequest(final TransactionRequest<?> request,
185 final RequestEnvelope envelope, final long now) throws RequestException {
186 checkRequestSequence(envelope);
189 final LocalHistoryIdentifier lhId = request.getTarget().getHistoryId();
190 final AbstractFrontendHistory history;
192 if (lhId.getHistoryId() != 0) {
193 history = localHistories.get(lhId);
194 if (history == null) {
195 if (purgedHistories.contains(lhId.getHistoryId())) {
196 LOG.warn("{}: rejecting request {} to purged history", persistenceId, request);
197 throw new DeadHistoryException(purgedHistories.toImmutable());
200 LOG.warn("{}: rejecting unknown history request {}", persistenceId, request);
201 throw new UnknownHistoryException(lastSeenHistory);
204 history = standaloneHistory;
207 return history.handleTransactionRequest(request, envelope, now);
214 expectedTxSequence = 0;
218 // FIXME: flush all state
222 public String toString() {
223 return MoreObjects.toStringHelper(LeaderFrontendState.class).add("clientId", clientId)
224 .add("purgedHistories", purgedHistories).toString();