d860bfa289e39f7b9a144aa652a5a030ec4c4546
[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.Map;
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.OutOfSequenceEnvelopeException;
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;
38
39 /**
40  * Frontend state as observed by the shard leader. This class is responsible for tracking generations and sequencing
41  * in the frontend/backend conversation.
42  *
43  * @author Robert Varga
44  */
45 @NotThreadSafe
46 final class LeaderFrontendState implements Identifiable<ClientIdentifier> {
47     private static final Logger LOG = LoggerFactory.getLogger(LeaderFrontendState.class);
48
49     // Histories which have not been purged
50     private final Map<LocalHistoryIdentifier, LocalFrontendHistory> localHistories;
51
52     // RangeSet performs automatic merging, hence we keep minimal state tracking information
53     private final RangeSet<UnsignedLong> purgedHistories;
54
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;
60
61     private long expectedTxSequence;
62     private Long lastSeenHistory = null;
63
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
67
68     // TODO: add statistics:
69     // - number of requests processed
70     // - number of histories processed
71     // - per-RequestException throw counters
72
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<>());
76     }
77
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);
87     }
88
89     @Override
90     public ClientIdentifier getIdentifier() {
91         return clientId;
92     }
93
94     private void checkRequestSequence(final RequestEnvelope envelope) throws OutOfSequenceEnvelopeException {
95         if (expectedTxSequence != envelope.getTxSequence()) {
96             throw new OutOfSequenceEnvelopeException(expectedTxSequence);
97         }
98     }
99
100     private void expectNextRequest() {
101         expectedTxSequence++;
102     }
103
104     @Nullable LocalHistorySuccess handleLocalHistoryRequest(final LocalHistoryRequest<?> request,
105             final RequestEnvelope envelope, final long now) throws RequestException {
106         checkRequestSequence(envelope);
107
108         try {
109             if (request instanceof CreateLocalHistoryRequest) {
110                 return handleCreateHistory((CreateLocalHistoryRequest) request, envelope, now);
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);
115             } else {
116                 LOG.warn("{}: rejecting unsupported request {}", persistenceId, request);
117                 throw new UnsupportedRequestException(request);
118             }
119         } finally {
120             expectNextRequest();
121         }
122     }
123
124     private LocalHistorySuccess handleCreateHistory(final CreateLocalHistoryRequest request,
125             final RequestEnvelope envelope, final long now) throws RequestException {
126         final LocalHistoryIdentifier historyId = request.getTarget();
127         final AbstractFrontendHistory existing = localHistories.get(historyId);
128         if (existing != null) {
129             // History already exists: report success
130             LOG.debug("{}: history {} already exists", persistenceId, historyId);
131             return new LocalHistorySuccess(historyId, request.getSequence());
132         }
133
134         // We have not found the history. Before we create it we need to check history ID sequencing so that we do not
135         // end up resurrecting a purged history.
136         if (purgedHistories.contains(UnsignedLong.fromLongBits(historyId.getHistoryId()))) {
137             LOG.debug("{}: rejecting purged request {}", persistenceId, request);
138             throw new DeadHistoryException(purgedHistories);
139         }
140
141         // Update last history we have seen
142         if (lastSeenHistory == null || Long.compareUnsigned(lastSeenHistory, historyId.getHistoryId()) < 0) {
143             lastSeenHistory = historyId.getHistoryId();
144         }
145
146         // We have to send the response only after persistence has completed
147         final ShardDataTreeTransactionChain chain = tree.ensureTransactionChain(historyId, () -> {
148             LOG.debug("{}: persisted history {}", persistenceId, historyId);
149             envelope.sendSuccess(new LocalHistorySuccess(historyId, request.getSequence()), tree.readTime() - now);
150         });
151
152         localHistories.put(historyId, LocalFrontendHistory.create(persistenceId, tree, chain));
153         LOG.debug("{}: created history {}", persistenceId, historyId);
154         return null;
155     }
156
157     private LocalHistorySuccess handleDestroyHistory(final DestroyLocalHistoryRequest request,
158             final RequestEnvelope envelope, final long now)
159             throws RequestException {
160         final LocalHistoryIdentifier id = request.getTarget();
161         final LocalFrontendHistory existing = localHistories.get(id);
162         if (existing == null) {
163             // History does not exist: report success
164             LOG.debug("{}: history {} does not exist, nothing to destroy", persistenceId, id);
165             return new LocalHistorySuccess(id, request.getSequence());
166         }
167
168         existing.destroy(request.getSequence(), envelope, now);
169         return null;
170     }
171
172     private LocalHistorySuccess handlePurgeHistory(final PurgeLocalHistoryRequest request,
173             final RequestEnvelope envelope, final long now) throws RequestException {
174         final LocalHistoryIdentifier id = request.getTarget();
175         final LocalFrontendHistory existing = localHistories.remove(id);
176         if (existing == null) {
177             LOG.debug("{}: history {} has already been purged", persistenceId, id);
178             return new LocalHistorySuccess(id, request.getSequence());
179         }
180
181         LOG.debug("{}: purging history {}", persistenceId, id);
182         final UnsignedLong ul = UnsignedLong.fromLongBits(id.getHistoryId());
183         purgedHistories.add(Range.closedOpen(ul, UnsignedLong.ONE.plus(ul)));
184         existing.purge(request.getSequence(), envelope, now);
185         return null;
186     }
187
188     @Nullable TransactionSuccess<?> handleTransactionRequest(final TransactionRequest<?> request,
189             final RequestEnvelope envelope, final long now) throws RequestException {
190         checkRequestSequence(envelope);
191
192         try {
193             final LocalHistoryIdentifier lhId = request.getTarget().getHistoryId();
194             final AbstractFrontendHistory history;
195
196             if (lhId.getHistoryId() != 0) {
197                 history = localHistories.get(lhId);
198                 if (history == null) {
199                     if (purgedHistories.contains(UnsignedLong.fromLongBits(lhId.getHistoryId()))) {
200                         LOG.warn("{}: rejecting request {} to purged history", persistenceId, request);
201                         throw new DeadHistoryException(purgedHistories);
202                     }
203
204                     LOG.warn("{}: rejecting unknown history request {}", persistenceId, request);
205                     throw new UnknownHistoryException(lastSeenHistory);
206                 }
207             } else {
208                 history = standaloneHistory;
209             }
210
211             return history.handleTransactionRequest(request, envelope, now);
212         } finally {
213             expectNextRequest();
214         }
215     }
216
217     void reconnect() {
218         expectedTxSequence = 0;
219     }
220
221     void retire() {
222         // FIXME: flush all state
223     }
224
225     @Override
226     public String toString() {
227         return MoreObjects.toStringHelper(LeaderFrontendState.class).add("clientId", clientId)
228                 .add("purgedHistories", purgedHistories).toString();
229     }
230 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.