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;
18 import javax.annotation.Nullable;
19 import javax.annotation.concurrent.NotThreadSafe;
20 import org.opendaylight.controller.cluster.access.commands.CreateLocalHistoryRequest;
21 import org.opendaylight.controller.cluster.access.commands.DeadHistoryException;
22 import org.opendaylight.controller.cluster.access.commands.DestroyLocalHistoryRequest;
23 import org.opendaylight.controller.cluster.access.commands.LocalHistoryRequest;
24 import org.opendaylight.controller.cluster.access.commands.LocalHistorySuccess;
25 import org.opendaylight.controller.cluster.access.commands.OutOfOrderRequestException;
26 import org.opendaylight.controller.cluster.access.commands.PurgeLocalHistoryRequest;
27 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
28 import org.opendaylight.controller.cluster.access.commands.TransactionSuccess;
29 import org.opendaylight.controller.cluster.access.commands.UnknownHistoryException;
30 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
31 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
32 import org.opendaylight.controller.cluster.access.concepts.RequestEnvelope;
33 import org.opendaylight.controller.cluster.access.concepts.RequestException;
34 import org.opendaylight.controller.cluster.access.concepts.UnsupportedRequestException;
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 RangeSet<UnsignedLong> 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 expectedTxSequence;
62 private Long lastSeenHistory = null;
64 // TODO: explicit failover notification
65 // Record the ActorRef for the originating actor and when we switch to being a leader send a notification
66 // to the frontend client -- that way it can immediately start sending requests
68 // TODO: add statistics:
69 // - number of requests processed
70 // - number of histories processed
71 // - per-RequestException throw counters
73 LeaderFrontendState(final String persistenceId, final ClientIdentifier clientId, final ShardDataTree tree) {
74 this(persistenceId, clientId, tree, TreeRangeSet.create(), StandaloneFrontendHistory.create(persistenceId,
75 clientId, tree), new HashMap<>());
78 LeaderFrontendState(final String persistenceId, final ClientIdentifier clientId, final ShardDataTree tree,
79 final RangeSet<UnsignedLong> purgedHistories, final AbstractFrontendHistory standaloneHistory,
80 final Map<LocalHistoryIdentifier, LocalFrontendHistory> localHistories) {
81 this.persistenceId = Preconditions.checkNotNull(persistenceId);
82 this.clientId = Preconditions.checkNotNull(clientId);
83 this.tree = Preconditions.checkNotNull(tree);
84 this.purgedHistories = Preconditions.checkNotNull(purgedHistories);
85 this.standaloneHistory = Preconditions.checkNotNull(standaloneHistory);
86 this.localHistories = Preconditions.checkNotNull(localHistories);
90 public ClientIdentifier getIdentifier() {
94 private void checkRequestSequence(final RequestEnvelope envelope) throws OutOfOrderRequestException {
95 if (expectedTxSequence != envelope.getTxSequence()) {
96 throw new OutOfOrderRequestException(expectedTxSequence);
100 private void expectNextRequest() {
101 expectedTxSequence++;
104 @Nullable LocalHistorySuccess handleLocalHistoryRequest(final LocalHistoryRequest<?> request,
105 final RequestEnvelope envelope, final long now) throws RequestException {
106 checkRequestSequence(envelope);
109 if (request instanceof CreateLocalHistoryRequest) {
110 return handleCreateHistory((CreateLocalHistoryRequest) request);
111 } else if (request instanceof DestroyLocalHistoryRequest) {
112 return handleDestroyHistory((DestroyLocalHistoryRequest) request, envelope, now);
113 } else if (request instanceof PurgeLocalHistoryRequest) {
114 return handlePurgeHistory((PurgeLocalHistoryRequest)request, envelope, now);
116 throw new UnsupportedRequestException(request);
123 private LocalHistorySuccess handleCreateHistory(final CreateLocalHistoryRequest request) throws RequestException {
124 final LocalHistoryIdentifier id = request.getTarget();
125 final AbstractFrontendHistory existing = localHistories.get(id);
126 if (existing != null) {
127 // History already exists: report success
128 LOG.debug("{}: history {} already exists", persistenceId, id);
129 return new LocalHistorySuccess(id, request.getSequence());
132 // We have not found the history. Before we create it we need to check history ID sequencing so that we do not
133 // end up resurrecting a purged history.
134 if (purgedHistories.contains(UnsignedLong.fromLongBits(id.getHistoryId()))) {
135 LOG.debug("{}: rejecting purged request {}", persistenceId, request);
136 throw new DeadHistoryException(lastSeenHistory.longValue());
139 // Update last history we have seen
140 if (lastSeenHistory != null && Long.compareUnsigned(lastSeenHistory, id.getHistoryId()) < 0) {
141 lastSeenHistory = id.getHistoryId();
144 localHistories.put(id, LocalFrontendHistory.create(persistenceId, tree, id));
145 LOG.debug("{}: created history {}", persistenceId, id);
146 return new LocalHistorySuccess(id, request.getSequence());
149 private LocalHistorySuccess handleDestroyHistory(final DestroyLocalHistoryRequest request,
150 final RequestEnvelope envelope, final long now)
151 throws RequestException {
152 final LocalHistoryIdentifier id = request.getTarget();
153 final LocalFrontendHistory existing = localHistories.get(id);
154 if (existing == null) {
155 // History does not exist: report success
156 LOG.debug("{}: history {} does not exist, nothing to destroy", persistenceId, id);
157 return new LocalHistorySuccess(id, request.getSequence());
160 existing.destroy(request.getSequence(), envelope, now);
164 private LocalHistorySuccess handlePurgeHistory(final PurgeLocalHistoryRequest request,
165 final RequestEnvelope envelope, final long now) throws RequestException {
166 final LocalHistoryIdentifier id = request.getTarget();
167 final LocalFrontendHistory existing = localHistories.remove(id);
168 if (existing == null) {
169 LOG.debug("{}: history {} has already been purged", persistenceId, id);
170 return new LocalHistorySuccess(id, request.getSequence());
173 LOG.debug("{}: purging history {}", persistenceId, id);
174 purgedHistories.add(Range.singleton(UnsignedLong.fromLongBits(id.getHistoryId())));
175 existing.purge(request.getSequence(), envelope, now);
179 @Nullable TransactionSuccess<?> handleTransactionRequest(final TransactionRequest<?> request,
180 final RequestEnvelope envelope, final long now) throws RequestException {
181 checkRequestSequence(envelope);
184 final LocalHistoryIdentifier lhId = request.getTarget().getHistoryId();
185 final AbstractFrontendHistory history;
187 if (lhId.getHistoryId() != 0) {
188 history = localHistories.get(lhId);
189 if (history == null) {
190 LOG.debug("{}: rejecting unknown history request {}", persistenceId, request);
191 throw new UnknownHistoryException(lastSeenHistory);
194 history = standaloneHistory;
197 return history.handleTransactionRequest(request, envelope, now);
204 expectedTxSequence = 0;
208 // FIXME: flush all state
212 public String toString() {
213 return MoreObjects.toStringHelper(LeaderFrontendState.class).add("clientId", clientId)
214 .add("purgedHistories", purgedHistories).toString();