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.Preconditions;
11 import com.google.common.base.Verify;
12 import com.google.common.collect.Collections2;
13 import com.google.common.collect.Range;
14 import com.google.common.collect.RangeSet;
15 import com.google.common.collect.TreeRangeSet;
16 import com.google.common.primitives.UnsignedLong;
17 import java.util.HashMap;
19 import javax.annotation.Nonnull;
20 import javax.annotation.concurrent.NotThreadSafe;
21 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
22 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
23 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
24 import org.opendaylight.controller.cluster.datastore.persisted.FrontendClientMetadata;
25 import org.opendaylight.controller.cluster.datastore.persisted.FrontendHistoryMetadata;
26 import org.opendaylight.yangtools.concepts.Builder;
27 import org.opendaylight.yangtools.concepts.Identifiable;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 final class FrontendClientMetadataBuilder implements Builder<FrontendClientMetadata>, Identifiable<ClientIdentifier> {
33 private static final Logger LOG = LoggerFactory.getLogger(FrontendClientMetadataBuilder.class);
35 private final Map<LocalHistoryIdentifier, FrontendHistoryMetadataBuilder> currentHistories = new HashMap<>();
36 private final RangeSet<UnsignedLong> purgedHistories;
37 private final ClientIdentifier identifier;
39 FrontendClientMetadataBuilder(final ClientIdentifier identifier) {
40 this.identifier = Preconditions.checkNotNull(identifier);
41 purgedHistories = TreeRangeSet.create();
44 FrontendClientMetadataBuilder(final FrontendClientMetadata meta) {
45 this.identifier = Preconditions.checkNotNull(meta.getIdentifier());
46 purgedHistories = TreeRangeSet.create(meta.getPurgedHistories());
48 for (FrontendHistoryMetadata h : meta.getCurrentHistories()) {
49 final FrontendHistoryMetadataBuilder b = new FrontendHistoryMetadataBuilder(identifier, h);
50 currentHistories.put(b.getIdentifier(), b);
55 public FrontendClientMetadata build() {
56 return new FrontendClientMetadata(identifier, purgedHistories,
57 Collections2.transform(currentHistories.values(), FrontendHistoryMetadataBuilder::build));
61 public ClientIdentifier getIdentifier() {
65 void onHistoryCreated(final LocalHistoryIdentifier historyId) {
66 final FrontendHistoryMetadataBuilder newMeta = new FrontendHistoryMetadataBuilder(historyId);
67 final FrontendHistoryMetadataBuilder oldMeta = currentHistories.putIfAbsent(historyId, newMeta);
68 if (oldMeta != null) {
69 // This should not be happening, warn about it
70 LOG.warn("Reused local history {}", historyId);
72 LOG.debug("Created local history {}", historyId);
76 void onHistoryClosed(final LocalHistoryIdentifier historyId) {
77 final FrontendHistoryMetadataBuilder builder = currentHistories.get(historyId);
78 if (builder != null) {
79 builder.onHistoryClosed();
80 LOG.debug("Closed history {}", historyId);
82 LOG.warn("Closed unknown history {}, ignoring", historyId);
86 void onHistoryPurged(final LocalHistoryIdentifier historyId) {
87 final FrontendHistoryMetadataBuilder history = currentHistories.remove(historyId);
88 if (history == null) {
89 LOG.warn("Purging unknown history {}", historyId);
92 // XXX: do we need to account for cookies?
93 purgedHistories.add(Range.singleton(UnsignedLong.fromLongBits(historyId.getHistoryId())));
94 LOG.debug("Purged history {}", historyId);
97 void onTransactionAborted(final TransactionIdentifier txId) {
98 final FrontendHistoryMetadataBuilder history = getHistory(txId);
99 if (history != null) {
100 history.onTransactionAborted(txId);
101 LOG.debug("Committed transaction {}", txId);
103 LOG.warn("Unknown history for aborted transaction {}, ignoring", txId);
107 void onTransactionCommitted(final TransactionIdentifier txId) {
108 final FrontendHistoryMetadataBuilder history = getHistory(txId);
109 if (history != null) {
110 history.onTransactionCommitted(txId);
111 LOG.debug("Aborted transaction {}", txId);
113 LOG.warn("Unknown history for commited transaction {}, ignoring", txId);
117 void onTransactionPurged(final TransactionIdentifier txId) {
118 final FrontendHistoryMetadataBuilder history = getHistory(txId);
119 if (history != null) {
120 history.onTransactionPurged(txId);
121 LOG.debug("Purged transaction {}", txId);
123 LOG.warn("Unknown history for purged transaction {}, ignoring", txId);
128 * Transform frontend metadata for a particular client into its {@link LeaderFrontendState} counterpart.
130 * @param shard parent shard
131 * @return Leader frontend state
133 @Nonnull LeaderFrontendState toLeaderState(@Nonnull final Shard shard) {
134 // Note: we have to make sure to *copy* all current state and not leak any views, otherwise leader/follower
135 // interactions would get intertwined leading to inconsistencies.
136 final Map<LocalHistoryIdentifier, LocalFrontendHistory> histories = new HashMap<>();
137 for (FrontendHistoryMetadataBuilder e : currentHistories.values()) {
138 if (e.getIdentifier().getHistoryId() != 0) {
139 final AbstractFrontendHistory state = e.toLeaderState(shard);
140 Verify.verify(state instanceof LocalFrontendHistory);
141 histories.put(e.getIdentifier(), (LocalFrontendHistory) state);
145 final AbstractFrontendHistory singleHistory;
146 final FrontendHistoryMetadataBuilder singleHistoryMeta = currentHistories.get(
147 new LocalHistoryIdentifier(identifier, 0));
148 if (singleHistoryMeta == null) {
149 final ShardDataTree tree = shard.getDataStore();
150 singleHistory = StandaloneFrontendHistory.create(shard.persistenceId(), getIdentifier(), tree);
152 singleHistory = singleHistoryMeta.toLeaderState(shard);
155 return new LeaderFrontendState(shard.persistenceId(), getIdentifier(), shard.getDataStore(),
156 TreeRangeSet.create(purgedHistories), singleHistory, histories);
159 private FrontendHistoryMetadataBuilder getHistory(final TransactionIdentifier txId) {
160 return currentHistories.get(txId.getHistoryId());