BUG-5280: add executionTimeNanos
[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.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;
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 = new HashMap<>();
51
52     // RangeSet performs automatic merging, hence we keep minimal state tracking information
53     private final RangeSet<UnsignedLong> purgedHistories = TreeRangeSet.create();
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
65     // TODO: explicit failover notification
66     //       Record the ActorRef for the originating actor and when we switch to being a leader send a notification
67     //       to the frontend client -- that way it can immediately start sending requests
68
69     // TODO: add statistics:
70     // - number of requests processed
71     // - number of histories processed
72     // - per-RequestException throw counters
73
74     LeaderFrontendState(final String persistenceId, final ClientIdentifier clientId, final ShardDataTree tree) {
75         this.persistenceId = Preconditions.checkNotNull(persistenceId);
76         this.clientId = Preconditions.checkNotNull(clientId);
77         this.tree = Preconditions.checkNotNull(tree);
78         standaloneHistory = new StandaloneFrontendHistory(persistenceId, tree.ticker(), clientId, tree);
79     }
80
81     @Override
82     public ClientIdentifier getIdentifier() {
83         return clientId;
84     }
85
86     private void checkRequestSequence(final RequestEnvelope envelope) throws OutOfOrderRequestException {
87         if (expectedTxSequence != envelope.getTxSequence()) {
88             throw new OutOfOrderRequestException(expectedTxSequence);
89         }
90     }
91
92     private void expectNextRequest() {
93         expectedTxSequence++;
94     }
95
96     @Nullable LocalHistorySuccess handleLocalHistoryRequest(final LocalHistoryRequest<?> request,
97             final RequestEnvelope envelope, final long now) throws RequestException {
98         checkRequestSequence(envelope);
99
100         try {
101             if (request instanceof CreateLocalHistoryRequest) {
102                 return handleCreateHistory((CreateLocalHistoryRequest) request);
103             } else if (request instanceof DestroyLocalHistoryRequest) {
104                 return handleDestroyHistory((DestroyLocalHistoryRequest) request, now);
105             } else if (request instanceof PurgeLocalHistoryRequest) {
106                 return handlePurgeHistory((PurgeLocalHistoryRequest)request, now);
107             } else {
108                 throw new UnsupportedRequestException(request);
109             }
110         } finally {
111             expectNextRequest();
112         }
113     }
114
115     private LocalHistorySuccess handleCreateHistory(final CreateLocalHistoryRequest request) throws RequestException {
116         final LocalHistoryIdentifier id = request.getTarget();
117         final AbstractFrontendHistory existing = localHistories.get(id);
118         if (existing != null) {
119             // History already exists: report success
120             LOG.debug("{}: history {} already exists", persistenceId, id);
121             return new LocalHistorySuccess(id, request.getSequence());
122         }
123
124         // We have not found the history. Before we create it we need to check history ID sequencing so that we do not
125         // end up resurrecting a purged history.
126         if (purgedHistories.contains(UnsignedLong.fromLongBits(id.getHistoryId()))) {
127             LOG.debug("{}: rejecting purged request {}", persistenceId, request);
128             throw new DeadHistoryException(lastSeenHistory.longValue());
129         }
130
131         // Update last history we have seen
132         if (lastSeenHistory != null && Long.compareUnsigned(lastSeenHistory, id.getHistoryId()) < 0) {
133             lastSeenHistory = id.getHistoryId();
134         }
135
136         localHistories.put(id, new LocalFrontendHistory(persistenceId, tree.ticker(), tree.ensureTransactionChain(id)));
137         LOG.debug("{}: created history {}", persistenceId, id);
138         return new LocalHistorySuccess(id, request.getSequence());
139     }
140
141     private LocalHistorySuccess handleDestroyHistory(final DestroyLocalHistoryRequest request, final long now)
142             throws RequestException {
143         final LocalHistoryIdentifier id = request.getTarget();
144         final LocalFrontendHistory existing = localHistories.get(id);
145         if (existing == null) {
146             // History does not exist: report success
147             LOG.debug("{}: history {} does not exist, nothing to destroy", persistenceId, id);
148             return new LocalHistorySuccess(id, request.getSequence());
149         }
150
151         return existing.destroy(request.getSequence(), now);
152     }
153
154     private LocalHistorySuccess handlePurgeHistory(final PurgeLocalHistoryRequest request, final long now)
155             throws RequestException {
156         final LocalHistoryIdentifier id = request.getTarget();
157         final LocalFrontendHistory existing = localHistories.remove(id);
158         if (existing != null) {
159             purgedHistories.add(Range.singleton(UnsignedLong.fromLongBits(id.getHistoryId())));
160
161             if (!existing.isDestroyed()) {
162                 LOG.warn("{}: purging undestroyed history {}", persistenceId, id);
163                 existing.destroy(request.getSequence(), now);
164             }
165
166             // FIXME: record a PURGE tombstone in the journal
167
168             LOG.debug("{}: purged history {}", persistenceId, id);
169         } else {
170             LOG.debug("{}: history {} has already been purged", persistenceId, id);
171         }
172
173         return new LocalHistorySuccess(id, request.getSequence());
174     }
175
176     @Nullable TransactionSuccess<?> handleTransactionRequest(final TransactionRequest<?> request,
177             final RequestEnvelope envelope, final long now) throws RequestException {
178         checkRequestSequence(envelope);
179
180         try {
181             final LocalHistoryIdentifier lhId = request.getTarget().getHistoryId();
182             final AbstractFrontendHistory history;
183
184             if (lhId.getHistoryId() != 0) {
185                 history = localHistories.get(lhId);
186                 if (history == null) {
187                     LOG.debug("{}: rejecting unknown history request {}", persistenceId, request);
188                     throw new UnknownHistoryException(lastSeenHistory);
189                 }
190             } else {
191                 history = standaloneHistory;
192             }
193
194             return history.handleTransactionRequest(request, envelope, now);
195         } finally {
196             expectNextRequest();
197         }
198     }
199
200     void reconnect() {
201         expectedTxSequence = 0;
202     }
203
204     void retire() {
205         // FIXME: flush all state
206     }
207
208     @Override
209     public String toString() {
210         return MoreObjects.toStringHelper(LeaderFrontendState.class).add("clientId", clientId)
211                 .add("purgedHistories", purgedHistories).toString();
212     }
213 }