BUG-8639: always invalidate primary info cache
[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 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;
18 import java.util.Map;
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;
40
41 /**
42  * Frontend state as observed by the shard leader. This class is responsible for tracking generations and sequencing
43  * in the frontend/backend conversation.
44  *
45  * @author Robert Varga
46  */
47 @NotThreadSafe
48 final class LeaderFrontendState implements Identifiable<ClientIdentifier> {
49     private static final Logger LOG = LoggerFactory.getLogger(LeaderFrontendState.class);
50
51     // Histories which have not been purged
52     private final Map<LocalHistoryIdentifier, LocalFrontendHistory> localHistories;
53
54     // RangeSet performs automatic merging, hence we keep minimal state tracking information
55     private final RangeSet<UnsignedLong> purgedHistories;
56
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;
62
63     private long lastConnectTicks;
64     private long lastSeenTicks;
65     private long expectedTxSequence;
66     private Long lastSeenHistory = null;
67
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
71
72     // TODO: add statistics:
73     // - number of requests processed
74     // - number of histories processed
75     // - per-RequestException throw counters
76
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<>());
80     }
81
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();
92     }
93
94     @Override
95     public ClientIdentifier getIdentifier() {
96         return clientId;
97     }
98
99     private void checkRequestSequence(final RequestEnvelope envelope) throws OutOfSequenceEnvelopeException {
100         if (expectedTxSequence != envelope.getTxSequence()) {
101             throw new OutOfSequenceEnvelopeException(expectedTxSequence);
102         }
103     }
104
105     private void expectNextRequest() {
106         expectedTxSequence++;
107     }
108
109     @Nullable LocalHistorySuccess handleLocalHistoryRequest(final LocalHistoryRequest<?> request,
110             final RequestEnvelope envelope, final long now) throws RequestException {
111         checkRequestSequence(envelope);
112
113         try {
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);
120             } else {
121                 LOG.warn("{}: rejecting unsupported request {}", persistenceId, request);
122                 throw new UnsupportedRequestException(request);
123             }
124         } finally {
125             expectNextRequest();
126         }
127     }
128
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());
137         }
138
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);
144         }
145
146         // Update last history we have seen
147         if (lastSeenHistory == null || Long.compareUnsigned(lastSeenHistory, historyId.getHistoryId()) < 0) {
148             lastSeenHistory = historyId.getHistoryId();
149         }
150
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);
155         });
156
157         localHistories.put(historyId, LocalFrontendHistory.create(persistenceId, tree, chain));
158         LOG.debug("{}: created history {}", persistenceId, historyId);
159         return null;
160     }
161
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());
171         }
172
173         existing.destroy(request.getSequence(), envelope, now);
174         return null;
175     }
176
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());
184         }
185
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);
190         return null;
191     }
192
193     @Nullable TransactionSuccess<?> handleTransactionRequest(final TransactionRequest<?> request,
194             final RequestEnvelope envelope, final long now) throws RequestException {
195         checkRequestSequence(envelope);
196
197         try {
198             final LocalHistoryIdentifier lhId = request.getTarget().getHistoryId();
199             final AbstractFrontendHistory history;
200
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);
207                     }
208
209                     LOG.warn("{}: rejecting unknown history request {}", persistenceId, request);
210                     throw new UnknownHistoryException(lastSeenHistory);
211                 }
212             } else {
213                 history = standaloneHistory;
214             }
215
216             return history.handleTransactionRequest(request, envelope, now);
217         } finally {
218             expectNextRequest();
219         }
220     }
221
222     void reconnect() {
223         expectedTxSequence = 0;
224         lastConnectTicks = tree.readTime();
225     }
226
227     void retire() {
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());
235                     it.remove();
236                 } else {
237                     LOG.debug("{}: Transaction {} already committing, not retiring it", persistenceId,
238                         cohort.getIdentifier());
239                 }
240             }
241         }
242
243         // Clear out all transaction chains
244         localHistories.values().forEach(AbstractFrontendHistory::retire);
245         localHistories.clear();
246         standaloneHistory.retire();
247     }
248
249     long getLastConnectTicks() {
250         return lastConnectTicks;
251     }
252
253     long getLastSeenTicks() {
254         return lastSeenTicks;
255     }
256
257     void touch() {
258         this.lastSeenTicks = tree.readTime();
259     }
260
261     @Override
262     public String toString() {
263         return MoreObjects.toStringHelper(LeaderFrontendState.class)
264                 .add("clientId", clientId)
265                 .add("nanosAgo", tree.readTime() - lastSeenTicks)
266                 .add("purgedHistories", purgedHistories)
267                 .toString();
268     }
269 }