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 static com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.base.MoreObjects;
14 import com.google.common.base.MoreObjects.ToStringHelper;
15 import com.google.common.collect.Collections2;
16 import com.google.common.collect.ImmutableList;
17 import com.google.common.collect.ImmutableRangeSet;
18 import com.google.common.collect.RangeSet;
19 import com.google.common.primitives.UnsignedLong;
20 import java.util.Collection;
21 import java.util.HashMap;
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
25 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
26 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
27 import org.opendaylight.controller.cluster.datastore.persisted.FrontendClientMetadata;
28 import org.opendaylight.controller.cluster.datastore.persisted.FrontendHistoryMetadata;
29 import org.opendaylight.controller.cluster.datastore.utils.UnsignedLongSet;
30 import org.opendaylight.yangtools.concepts.Builder;
31 import org.opendaylight.yangtools.concepts.Identifiable;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * This class is NOT thread-safe.
38 abstract class FrontendClientMetadataBuilder implements Builder<FrontendClientMetadata>,
39 Identifiable<ClientIdentifier> {
40 static final class Disabled extends FrontendClientMetadataBuilder {
41 Disabled(final String shardName, final ClientIdentifier identifier) {
42 super(shardName, identifier);
46 public FrontendClientMetadata build() {
47 return new FrontendClientMetadata(getIdentifier(), ImmutableRangeSet.of(), ImmutableList.of());
51 void onHistoryCreated(final LocalHistoryIdentifier historyId) {
56 void onHistoryClosed(final LocalHistoryIdentifier historyId) {
61 void onHistoryPurged(final LocalHistoryIdentifier historyId) {
66 void onTransactionAborted(final TransactionIdentifier txId) {
71 void onTransactionCommitted(final TransactionIdentifier txId) {
76 void onTransactionPurged(final TransactionIdentifier txId) {
81 LeaderFrontendState toLeaderState(final Shard shard) {
82 return new LeaderFrontendState.Disabled(shard.persistenceId(), getIdentifier(), shard.getDataStore());
86 static final class Enabled extends FrontendClientMetadataBuilder {
87 private final Map<LocalHistoryIdentifier, FrontendHistoryMetadataBuilder> currentHistories = new HashMap<>();
88 private final LocalHistoryIdentifier standaloneId;
89 private final UnsignedLongSet purgedHistories;
91 Enabled(final String shardName, final ClientIdentifier identifier) {
92 super(shardName, identifier);
94 purgedHistories = UnsignedLongSet.of();
96 // History for stand-alone transactions is always present
97 standaloneId = standaloneHistoryId();
98 currentHistories.put(standaloneId, new FrontendHistoryMetadataBuilder(standaloneId));
101 Enabled(final String shardName, final FrontendClientMetadata meta) {
102 super(shardName, meta.getIdentifier());
104 purgedHistories = UnsignedLongSet.of(meta.getPurgedHistories());
105 for (FrontendHistoryMetadata h : meta.getCurrentHistories()) {
106 final FrontendHistoryMetadataBuilder b = new FrontendHistoryMetadataBuilder(getIdentifier(), h);
107 currentHistories.put(b.getIdentifier(), b);
110 // Sanity check and recovery
111 standaloneId = standaloneHistoryId();
112 if (!currentHistories.containsKey(standaloneId)) {
113 LOG.warn("{}: Client {} recovered histories {} do not contain stand-alone history, attempting recovery",
114 shardName, getIdentifier(), currentHistories);
115 currentHistories.put(standaloneId, new FrontendHistoryMetadataBuilder(standaloneId));
120 public FrontendClientMetadata build() {
121 return new FrontendClientMetadata(getIdentifier(), purgedHistories.toRangeSet(),
122 Collections2.transform(currentHistories.values(), FrontendHistoryMetadataBuilder::build));
126 void onHistoryCreated(final LocalHistoryIdentifier historyId) {
127 final FrontendHistoryMetadataBuilder newMeta = new FrontendHistoryMetadataBuilder(historyId);
128 final FrontendHistoryMetadataBuilder oldMeta = currentHistories.putIfAbsent(historyId, newMeta);
129 if (oldMeta != null) {
130 // This should not be happening, warn about it
131 LOG.warn("{}: Reused local history {}", shardName(), historyId);
133 LOG.debug("{}: Created local history {}", shardName(), historyId);
138 void onHistoryClosed(final LocalHistoryIdentifier historyId) {
139 final FrontendHistoryMetadataBuilder builder = currentHistories.get(historyId);
140 if (builder != null) {
141 builder.onHistoryClosed();
142 LOG.debug("{}: Closed history {}", shardName(), historyId);
144 LOG.warn("{}: Closed unknown history {}, ignoring", shardName(), historyId);
149 void onHistoryPurged(final LocalHistoryIdentifier historyId) {
150 final FrontendHistoryMetadataBuilder history = currentHistories.remove(historyId);
151 final long historyBits = historyId.getHistoryId();
152 if (history == null) {
153 if (!purgedHistories.contains(historyBits)) {
154 purgedHistories.add(historyBits);
155 LOG.warn("{}: Purging unknown history {}", shardName(), historyId);
157 LOG.warn("{}: Duplicate purge of history {}", shardName(), historyId);
160 purgedHistories.add(historyBits);
161 LOG.debug("{}: Purged history {}", shardName(), historyId);
166 void onTransactionAborted(final TransactionIdentifier txId) {
167 final FrontendHistoryMetadataBuilder history = getHistory(txId);
168 if (history != null) {
169 history.onTransactionAborted(txId);
170 LOG.debug("{}: Aborted transaction {}", shardName(), txId);
172 LOG.warn("{}: Unknown history for aborted transaction {}, ignoring", shardName(), txId);
177 void onTransactionCommitted(final TransactionIdentifier txId) {
178 final FrontendHistoryMetadataBuilder history = getHistory(txId);
179 if (history != null) {
180 history.onTransactionCommitted(txId);
181 LOG.debug("{}: Committed transaction {}", shardName(), txId);
183 LOG.warn("{}: Unknown history for commited transaction {}, ignoring", shardName(), txId);
188 void onTransactionPurged(final TransactionIdentifier txId) {
189 final FrontendHistoryMetadataBuilder history = getHistory(txId);
190 if (history != null) {
191 history.onTransactionPurged(txId);
192 LOG.debug("{}: Purged transaction {}", shardName(), txId);
194 LOG.warn("{}: Unknown history for purged transaction {}, ignoring", shardName(), txId);
199 LeaderFrontendState toLeaderState(final Shard shard) {
200 // Note: we have to make sure to *copy* all current state and not leak any views, otherwise leader/follower
201 // interactions would get intertwined leading to inconsistencies.
202 final Map<LocalHistoryIdentifier, LocalFrontendHistory> histories = new HashMap<>();
203 for (FrontendHistoryMetadataBuilder e : currentHistories.values()) {
204 if (e.getIdentifier().getHistoryId() != 0) {
205 final AbstractFrontendHistory state = e.toLeaderState(shard);
206 verify(state instanceof LocalFrontendHistory, "Unexpected state %s", state);
207 histories.put(e.getIdentifier(), (LocalFrontendHistory) state);
211 final AbstractFrontendHistory singleHistory;
212 final FrontendHistoryMetadataBuilder singleHistoryMeta = currentHistories.get(
213 new LocalHistoryIdentifier(getIdentifier(), 0));
214 if (singleHistoryMeta == null) {
215 final ShardDataTree tree = shard.getDataStore();
216 singleHistory = StandaloneFrontendHistory.create(shard.persistenceId(), getIdentifier(), tree);
218 singleHistory = singleHistoryMeta.toLeaderState(shard);
221 return new LeaderFrontendState.Enabled(shard.persistenceId(), getIdentifier(), shard.getDataStore(),
222 purgedHistories.copy(), singleHistory, histories);
226 ToStringHelper addToStringAttributes(final ToStringHelper helper) {
227 return super.addToStringAttributes(helper).add("current", currentHistories).add("purged", purgedHistories);
230 private FrontendHistoryMetadataBuilder getHistory(final TransactionIdentifier txId) {
231 LocalHistoryIdentifier historyId = txId.getHistoryId();
232 if (historyId.getHistoryId() == 0 && historyId.getCookie() != 0) {
233 // We are pre-creating the history for free-standing transactions with a zero cookie, hence our lookup
234 // needs to account for that.
235 LOG.debug("{}: looking up {} instead of {}", shardName(), standaloneId, historyId);
236 historyId = standaloneId;
239 return currentHistories.get(historyId);
242 private LocalHistoryIdentifier standaloneHistoryId() {
243 return new LocalHistoryIdentifier(getIdentifier(), 0);
247 private static final Logger LOG = LoggerFactory.getLogger(FrontendClientMetadataBuilder.class);
249 private final @NonNull ClientIdentifier identifier;
250 private final @NonNull String shardName;
252 FrontendClientMetadataBuilder(final String shardName, final ClientIdentifier identifier) {
253 this.shardName = requireNonNull(shardName);
254 this.identifier = requireNonNull(identifier);
257 static FrontendClientMetadataBuilder of(final String shardName, final FrontendClientMetadata meta) {
258 final Collection<FrontendHistoryMetadata> current = meta.getCurrentHistories();
259 final RangeSet<UnsignedLong> purged = meta.getPurgedHistories();
261 // Completely empty histories imply disabled state, as otherwise we'd have a record of the single history --
262 // either purged or active
263 return current.isEmpty() && purged.isEmpty() ? new Disabled(shardName, meta.getIdentifier())
264 : new Enabled(shardName, meta);
268 public final ClientIdentifier getIdentifier() {
272 final String shardName() {
276 abstract void onHistoryCreated(LocalHistoryIdentifier historyId);
278 abstract void onHistoryClosed(LocalHistoryIdentifier historyId);
280 abstract void onHistoryPurged(LocalHistoryIdentifier historyId);
282 abstract void onTransactionAborted(TransactionIdentifier txId);
284 abstract void onTransactionCommitted(TransactionIdentifier txId);
286 abstract void onTransactionPurged(TransactionIdentifier txId);
289 * Transform frontend metadata for a particular client into its {@link LeaderFrontendState} counterpart.
291 * @param shard parent shard
292 * @return Leader frontend state
294 abstract @NonNull LeaderFrontendState toLeaderState(@NonNull Shard shard);
297 public final String toString() {
298 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
301 ToStringHelper addToStringAttributes(final ToStringHelper helper) {
302 return helper.add("identifier", identifier);