Speed up DatastoreContextIntrospector a bit
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / AbstractFrontendHistory.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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects;
13 import com.google.common.collect.ImmutableMap;
14 import com.google.common.collect.Range;
15 import com.google.common.collect.RangeSet;
16 import com.google.common.primitives.UnsignedLong;
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Optional;
20 import java.util.SortedSet;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.opendaylight.controller.cluster.access.commands.AbstractReadTransactionRequest;
23 import org.opendaylight.controller.cluster.access.commands.ClosedTransactionException;
24 import org.opendaylight.controller.cluster.access.commands.CommitLocalTransactionRequest;
25 import org.opendaylight.controller.cluster.access.commands.DeadTransactionException;
26 import org.opendaylight.controller.cluster.access.commands.IncrementTransactionSequenceRequest;
27 import org.opendaylight.controller.cluster.access.commands.LocalHistorySuccess;
28 import org.opendaylight.controller.cluster.access.commands.OutOfOrderRequestException;
29 import org.opendaylight.controller.cluster.access.commands.TransactionPurgeRequest;
30 import org.opendaylight.controller.cluster.access.commands.TransactionPurgeResponse;
31 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
32 import org.opendaylight.controller.cluster.access.commands.TransactionSuccess;
33 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
34 import org.opendaylight.controller.cluster.access.concepts.RequestEnvelope;
35 import org.opendaylight.controller.cluster.access.concepts.RequestException;
36 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
37 import org.opendaylight.yangtools.concepts.Identifiable;
38 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * Abstract class for providing logical tracking of frontend local histories. This class is specialized for
44  * standalone transactions and chained transactions.
45  *
46  * @author Robert Varga
47  */
48 abstract class AbstractFrontendHistory implements Identifiable<LocalHistoryIdentifier> {
49     private static final Logger LOG = LoggerFactory.getLogger(AbstractFrontendHistory.class);
50
51     private final Map<TransactionIdentifier, FrontendTransaction> transactions = new HashMap<>();
52     private final RangeSet<UnsignedLong> purgedTransactions;
53     private final String persistenceId;
54     private final ShardDataTree tree;
55
56     /**
57      * Transactions closed by the previous leader. Boolean indicates whether the transaction was committed (true) or
58      * aborted (false). We only ever shrink these.
59      */
60     private Map<UnsignedLong, Boolean> closedTransactions;
61
62     AbstractFrontendHistory(final String persistenceId, final ShardDataTree tree,
63         final Map<UnsignedLong, Boolean> closedTransactions, final RangeSet<UnsignedLong> purgedTransactions) {
64         this.persistenceId = requireNonNull(persistenceId);
65         this.tree = requireNonNull(tree);
66         this.closedTransactions = requireNonNull(closedTransactions);
67         this.purgedTransactions = requireNonNull(purgedTransactions);
68     }
69
70     final String persistenceId() {
71         return persistenceId;
72     }
73
74     final long readTime() {
75         return tree.readTime();
76     }
77
78     final @Nullable TransactionSuccess<?> handleTransactionRequest(final TransactionRequest<?> request,
79             final RequestEnvelope envelope, final long now) throws RequestException {
80         if (request instanceof TransactionPurgeRequest) {
81             return handleTransactionPurgeRequest(request, envelope, now);
82         }
83
84         final TransactionIdentifier id = request.getTarget();
85         final UnsignedLong ul = UnsignedLong.fromLongBits(id.getTransactionId());
86         if (purgedTransactions.contains(ul)) {
87             LOG.warn("{}: Request {} is contained purged transactions {}", persistenceId, request, purgedTransactions);
88             throw new DeadTransactionException(purgedTransactions);
89         }
90         final Boolean closed = closedTransactions.get(ul);
91         if (closed != null) {
92             final boolean successful = closed.booleanValue();
93             LOG.debug("{}: Request {} refers to a {} transaction", persistenceId, request, successful ? "successful"
94                     : "failed");
95             throw new ClosedTransactionException(successful);
96         }
97
98         FrontendTransaction tx = transactions.get(id);
99         if (tx == null) {
100             // The transaction does not exist and we are about to create it, check sequence number
101             if (request.getSequence() != 0) {
102                 LOG.warn("{}: no transaction state present, unexpected request {}", persistenceId(), request);
103                 throw new OutOfOrderRequestException(0);
104             }
105
106             tx = createTransaction(request, id);
107             transactions.put(id, tx);
108         } else if (!(request instanceof IncrementTransactionSequenceRequest)) {
109             final Optional<TransactionSuccess<?>> maybeReplay = tx.replaySequence(request.getSequence());
110             if (maybeReplay.isPresent()) {
111                 final TransactionSuccess<?> replay = maybeReplay.get();
112                 LOG.debug("{}: envelope {} replaying response {}", persistenceId(), envelope, replay);
113                 return replay;
114             }
115         }
116
117         return tx.handleRequest(request, envelope, now);
118     }
119
120     private TransactionSuccess<?> handleTransactionPurgeRequest(final TransactionRequest<?> request,
121             final RequestEnvelope envelope, final long now) {
122         final TransactionIdentifier id = request.getTarget();
123         final UnsignedLong ul = UnsignedLong.fromLongBits(id.getTransactionId());
124         if (purgedTransactions.contains(ul)) {
125             // Retransmitted purge request: nothing to do
126             LOG.debug("{}: transaction {} already purged", persistenceId, id);
127             return new TransactionPurgeResponse(id, request.getSequence());
128         }
129
130         // We perform two lookups instead of a straight remove, because once the map becomes empty we switch it
131         // to an ImmutableMap, which does not allow remove().
132         if (closedTransactions.containsKey(ul)) {
133             tree.purgeTransaction(id, () -> {
134                 closedTransactions.remove(ul);
135                 if (closedTransactions.isEmpty()) {
136                     closedTransactions = ImmutableMap.of();
137                 }
138
139                 purgedTransactions.add(Range.closedOpen(ul, UnsignedLong.ONE.plus(ul)));
140                 LOG.debug("{}: finished purging inherited transaction {}", persistenceId(), id);
141                 envelope.sendSuccess(new TransactionPurgeResponse(id, request.getSequence()), readTime() - now);
142             });
143             return null;
144         }
145
146         final FrontendTransaction tx = transactions.get(id);
147         if (tx == null) {
148             // This should never happen because the purge callback removes the transaction and puts it into
149             // purged transactions in one go. If it does, we warn about the situation and
150             LOG.warn("{}: transaction {} not tracked in {}, but not present in active transactions", persistenceId,
151                 id, purgedTransactions);
152             purgedTransactions.add(Range.closedOpen(ul, UnsignedLong.ONE.plus(ul)));
153             return new TransactionPurgeResponse(id, request.getSequence());
154         }
155
156         tree.purgeTransaction(id, () -> {
157             purgedTransactions.add(Range.closedOpen(ul, UnsignedLong.ONE.plus(ul)));
158             transactions.remove(id);
159             LOG.debug("{}: finished purging transaction {}", persistenceId(), id);
160             envelope.sendSuccess(new TransactionPurgeResponse(id, request.getSequence()), readTime() - now);
161         });
162
163         return null;
164     }
165
166     final void destroy(final long sequence, final RequestEnvelope envelope, final long now) {
167         LOG.debug("{}: closing history {}", persistenceId(), getIdentifier());
168         tree.closeTransactionChain(getIdentifier(),
169             () -> envelope.sendSuccess(new LocalHistorySuccess(getIdentifier(), sequence), readTime() - now));
170     }
171
172     final void purge(final long sequence, final RequestEnvelope envelope, final long now) {
173         LOG.debug("{}: purging history {}", persistenceId(), getIdentifier());
174         tree.purgeTransactionChain(getIdentifier(),
175             () -> envelope.sendSuccess(new LocalHistorySuccess(getIdentifier(), sequence), readTime() - now));
176     }
177
178     final void retire() {
179         transactions.values().forEach(FrontendTransaction::retire);
180         tree.removeTransactionChain(getIdentifier());
181     }
182
183     private FrontendTransaction createTransaction(final TransactionRequest<?> request, final TransactionIdentifier id) {
184         if (request instanceof CommitLocalTransactionRequest) {
185             LOG.debug("{}: allocating new ready transaction {}", persistenceId(), id);
186             tree.getStats().incrementReadWriteTransactionCount();
187             return createReadyTransaction(id, ((CommitLocalTransactionRequest) request).getModification());
188         }
189         if (request instanceof AbstractReadTransactionRequest
190                 && ((AbstractReadTransactionRequest<?>) request).isSnapshotOnly()) {
191             LOG.debug("{}: allocating new open snapshot {}", persistenceId(), id);
192             tree.getStats().incrementReadOnlyTransactionCount();
193             return createOpenSnapshot(id);
194         }
195
196         LOG.debug("{}: allocating new open transaction {}", persistenceId(), id);
197         tree.getStats().incrementReadWriteTransactionCount();
198         return createOpenTransaction(id);
199     }
200
201     abstract FrontendTransaction createOpenSnapshot(TransactionIdentifier id);
202
203     abstract FrontendTransaction createOpenTransaction(TransactionIdentifier id);
204
205     abstract FrontendTransaction createReadyTransaction(TransactionIdentifier id, DataTreeModification mod)
206         ;
207
208     abstract ShardDataTreeCohort createFailedCohort(TransactionIdentifier id, DataTreeModification mod,
209             Exception failure);
210
211     abstract ShardDataTreeCohort createReadyCohort(TransactionIdentifier id, DataTreeModification mod,
212             Optional<SortedSet<String>> participatingShardNames);
213
214     @Override
215     public String toString() {
216         return MoreObjects.toStringHelper(this).omitNullValues().add("identifier", getIdentifier())
217                 .add("persistenceId", persistenceId).add("transactions", transactions).toString();
218     }
219 }