ba2bdb3c7bfc87ba0dcc479ec754d7ff8dd7a751
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / LeaderFrontendState.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
10 import com.google.common.base.MoreObjects;
11 import com.google.common.base.Preconditions;
12 import java.util.HashMap;
13 import java.util.Map;
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;
35
36 /**
37  * Frontend state as observed by the shard leader. This class is responsible for tracking generations and sequencing
38  * in the frontend/backend conversation.
39  *
40  * @author Robert Varga
41  */
42 @NotThreadSafe
43 final class LeaderFrontendState implements Identifiable<ClientIdentifier> {
44     private static final Logger LOG = LoggerFactory.getLogger(LeaderFrontendState.class);
45
46     // Histories which have not been purged
47     private final Map<LocalHistoryIdentifier, LocalFrontendHistory> localHistories;
48
49     // RangeSet performs automatic merging, hence we keep minimal state tracking information
50     private final UnsignedLongRangeSet purgedHistories;
51
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;
57
58     private long expectedTxSequence;
59     private Long lastSeenHistory = null;
60
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
64
65     // TODO: add statistics:
66     // - number of requests processed
67     // - number of histories processed
68     // - per-RequestException throw counters
69
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<>());
73     }
74
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);
84     }
85
86     @Override
87     public ClientIdentifier getIdentifier() {
88         return clientId;
89     }
90
91     private void checkRequestSequence(final RequestEnvelope envelope) throws OutOfSequenceEnvelopeException {
92         if (expectedTxSequence != envelope.getTxSequence()) {
93             throw new OutOfSequenceEnvelopeException(expectedTxSequence);
94         }
95     }
96
97     private void expectNextRequest() {
98         expectedTxSequence++;
99     }
100
101     @Nullable LocalHistorySuccess handleLocalHistoryRequest(final LocalHistoryRequest<?> request,
102             final RequestEnvelope envelope, final long now) throws RequestException {
103         checkRequestSequence(envelope);
104
105         try {
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);
112             } else {
113                 LOG.warn("{}: rejecting unsupported request {}", persistenceId, request);
114                 throw new UnsupportedRequestException(request);
115             }
116         } finally {
117             expectNextRequest();
118         }
119     }
120
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());
129         }
130
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());
136         }
137
138         // Update last history we have seen
139         if (lastSeenHistory == null || Long.compareUnsigned(lastSeenHistory, historyId.getHistoryId()) < 0) {
140             lastSeenHistory = historyId.getHistoryId();
141         }
142
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);
147         });
148
149         localHistories.put(historyId, LocalFrontendHistory.create(persistenceId, tree, chain));
150         LOG.debug("{}: created history {}", persistenceId, historyId);
151         return null;
152     }
153
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());
163         }
164
165         existing.destroy(request.getSequence(), envelope, now);
166         return null;
167     }
168
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());
176         }
177
178         LOG.debug("{}: purging history {}", persistenceId, id);
179         purgedHistories.add(id.getHistoryId());
180         existing.purge(request.getSequence(), envelope, now);
181         return null;
182     }
183
184     @Nullable TransactionSuccess<?> handleTransactionRequest(final TransactionRequest<?> request,
185             final RequestEnvelope envelope, final long now) throws RequestException {
186         checkRequestSequence(envelope);
187
188         try {
189             final LocalHistoryIdentifier lhId = request.getTarget().getHistoryId();
190             final AbstractFrontendHistory history;
191
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());
198                     }
199
200                     LOG.warn("{}: rejecting unknown history request {}", persistenceId, request);
201                     throw new UnknownHistoryException(lastSeenHistory);
202                 }
203             } else {
204                 history = standaloneHistory;
205             }
206
207             return history.handleTransactionRequest(request, envelope, now);
208         } finally {
209             expectNextRequest();
210         }
211     }
212
213     void reconnect() {
214         expectedTxSequence = 0;
215     }
216
217     void retire() {
218         // FIXME: flush all state
219     }
220
221     @Override
222     public String toString() {
223         return MoreObjects.toStringHelper(LeaderFrontendState.class).add("clientId", clientId)
224                 .add("purgedHistories", purgedHistories).toString();
225     }
226 }