2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore;
10 import com.google.common.base.MoreObjects;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.Range;
14 import com.google.common.collect.RangeSet;
15 import com.google.common.primitives.UnsignedLong;
16 import java.util.HashMap;
18 import java.util.Optional;
19 import javax.annotation.Nullable;
20 import org.opendaylight.controller.cluster.access.commands.AbstractReadTransactionRequest;
21 import org.opendaylight.controller.cluster.access.commands.ClosedTransactionException;
22 import org.opendaylight.controller.cluster.access.commands.CommitLocalTransactionRequest;
23 import org.opendaylight.controller.cluster.access.commands.DeadTransactionException;
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.TransactionPurgeRequest;
27 import org.opendaylight.controller.cluster.access.commands.TransactionPurgeResponse;
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.concepts.LocalHistoryIdentifier;
31 import org.opendaylight.controller.cluster.access.concepts.RequestEnvelope;
32 import org.opendaylight.controller.cluster.access.concepts.RequestException;
33 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
34 import org.opendaylight.yangtools.concepts.Identifiable;
35 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * Abstract class for providing logical tracking of frontend local histories. This class is specialized for
41 * standalone transactions and chained transactions.
43 * @author Robert Varga
45 abstract class AbstractFrontendHistory implements Identifiable<LocalHistoryIdentifier> {
46 private static final Logger LOG = LoggerFactory.getLogger(AbstractFrontendHistory.class);
47 private static final OutOfOrderRequestException UNSEQUENCED_START = new OutOfOrderRequestException(0);
49 private final Map<TransactionIdentifier, FrontendTransaction> transactions = new HashMap<>();
50 private final RangeSet<UnsignedLong> purgedTransactions;
51 private final String persistenceId;
52 private final ShardDataTree tree;
55 * Transactions closed by the previous leader. Boolean indicates whether the transaction was committed (true) or
56 * aborted (false). We only ever shrink these.
58 private Map<UnsignedLong, Boolean> closedTransactions;
60 AbstractFrontendHistory(final String persistenceId, final ShardDataTree tree,
61 final Map<UnsignedLong, Boolean> closedTransactions, final RangeSet<UnsignedLong> purgedTransactions) {
62 this.persistenceId = Preconditions.checkNotNull(persistenceId);
63 this.tree = Preconditions.checkNotNull(tree);
64 this.closedTransactions = Preconditions.checkNotNull(closedTransactions);
65 this.purgedTransactions = Preconditions.checkNotNull(purgedTransactions);
68 final String persistenceId() {
72 final long readTime() {
73 return tree.ticker().read();
76 final @Nullable TransactionSuccess<?> handleTransactionRequest(final TransactionRequest<?> request,
77 final RequestEnvelope envelope, final long now) throws RequestException {
78 final TransactionIdentifier id = request.getTarget();
79 final UnsignedLong ul = UnsignedLong.fromLongBits(id.getTransactionId());
81 if (request instanceof TransactionPurgeRequest) {
82 if (purgedTransactions.contains(ul)) {
83 // Retransmitted purge request: nothing to do
84 LOG.debug("{}: transaction {} already purged", persistenceId, id);
85 return new TransactionPurgeResponse(id, request.getSequence());
88 // We perform two lookups instead of a straight remove, because once the map becomes empty we switch it
89 // to an ImmutableMap, which does not allow remove().
90 if (closedTransactions.containsKey(ul)) {
91 tree.purgeTransaction(id, () -> {
92 closedTransactions.remove(ul);
93 if (closedTransactions.isEmpty()) {
94 closedTransactions = ImmutableMap.of();
97 purgedTransactions.add(Range.singleton(ul));
98 LOG.debug("{}: finished purging inherited transaction {}", persistenceId(), id);
99 envelope.sendSuccess(new TransactionPurgeResponse(id, request.getSequence()), readTime() - now);
104 final FrontendTransaction tx = transactions.get(id);
106 // This should never happen because the purge callback removes the transaction and puts it into
107 // purged transactions in one go. If it does, we warn about the situation and
108 LOG.warn("{}: transaction {} not tracked in {}, but not present in active transactions", persistenceId,
109 id, purgedTransactions);
110 purgedTransactions.add(Range.singleton(ul));
111 return new TransactionPurgeResponse(id, request.getSequence());
114 tree.purgeTransaction(id, () -> {
115 purgedTransactions.add(Range.singleton(ul));
116 transactions.remove(id);
117 LOG.debug("{}: finished purging transaction {}", persistenceId(), id);
118 envelope.sendSuccess(new TransactionPurgeResponse(id, request.getSequence()), readTime() - now);
123 if (purgedTransactions.contains(ul)) {
124 LOG.warn("{}: Request {} is contained purged transactions {}", persistenceId, request, purgedTransactions);
125 throw new DeadTransactionException(purgedTransactions);
127 final Boolean closed = closedTransactions.get(ul);
128 if (closed != null) {
129 final boolean successful = closed.booleanValue();
130 LOG.debug("{}: Request {} refers to a {} transaction", persistenceId, request, successful ? "successful"
132 throw new ClosedTransactionException(successful);
135 FrontendTransaction tx = transactions.get(id);
137 // The transaction does not exist and we are about to create it, check sequence number
138 if (request.getSequence() != 0) {
139 LOG.debug("{}: no transaction state present, unexpected request {}", persistenceId(), request);
140 throw UNSEQUENCED_START;
143 tx = createTransaction(request, id);
144 transactions.put(id, tx);
146 final Optional<TransactionSuccess<?>> maybeReplay = tx.replaySequence(request.getSequence());
147 if (maybeReplay.isPresent()) {
148 final TransactionSuccess<?> replay = maybeReplay.get();
149 LOG.debug("{}: envelope {} replaying response {}", persistenceId(), envelope, replay);
154 return tx.handleRequest(request, envelope, now);
157 void destroy(final long sequence, final RequestEnvelope envelope, final long now) {
158 LOG.debug("{}: closing history {}", persistenceId(), getIdentifier());
159 tree.closeTransactionChain(getIdentifier(), () -> {
160 envelope.sendSuccess(new LocalHistorySuccess(getIdentifier(), sequence), readTime() - now);
164 void purge(final long sequence, final RequestEnvelope envelope, final long now) {
165 LOG.debug("{}: purging history {}", persistenceId(), getIdentifier());
166 tree.purgeTransactionChain(getIdentifier(), () -> {
167 envelope.sendSuccess(new LocalHistorySuccess(getIdentifier(), sequence), readTime() - now);
171 private FrontendTransaction createTransaction(final TransactionRequest<?> request, final TransactionIdentifier id)
172 throws RequestException {
173 if (request instanceof CommitLocalTransactionRequest) {
174 LOG.debug("{}: allocating new ready transaction {}", persistenceId(), id);
175 tree.getStats().incrementReadWriteTransactionCount();
176 return createReadyTransaction(id, ((CommitLocalTransactionRequest) request).getModification());
178 if (request instanceof AbstractReadTransactionRequest) {
179 if (((AbstractReadTransactionRequest<?>) request).isSnapshotOnly()) {
180 LOG.debug("{}: allocatint new open snapshot {}", persistenceId(), id);
181 tree.getStats().incrementReadOnlyTransactionCount();
182 return createOpenSnapshot(id);
186 LOG.debug("{}: allocating new open transaction {}", persistenceId(), id);
187 tree.getStats().incrementReadWriteTransactionCount();
188 return createOpenTransaction(id);
191 abstract FrontendTransaction createOpenSnapshot(TransactionIdentifier id) throws RequestException;
193 abstract FrontendTransaction createOpenTransaction(TransactionIdentifier id) throws RequestException;
195 abstract FrontendTransaction createReadyTransaction(TransactionIdentifier id, DataTreeModification mod)
196 throws RequestException;
198 abstract ShardDataTreeCohort createFailedCohort(TransactionIdentifier id, DataTreeModification mod,
201 abstract ShardDataTreeCohort createReadyCohort(TransactionIdentifier id, DataTreeModification mod);
204 public String toString() {
205 return MoreObjects.toStringHelper(this).omitNullValues().add("identifier", getIdentifier())
206 .add("persistenceId", persistenceId).add("transactions", transactions).toString();