8a0ce605dfd18bb7bcd205c8cdebce9b6691f356
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / FrontendClientMetadataBuilder.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.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;
18 import java.util.Map;
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;
30
31 @NotThreadSafe
32 final class FrontendClientMetadataBuilder implements Builder<FrontendClientMetadata>, Identifiable<ClientIdentifier> {
33     private static final Logger LOG = LoggerFactory.getLogger(FrontendClientMetadataBuilder.class);
34
35     private final Map<LocalHistoryIdentifier, FrontendHistoryMetadataBuilder> currentHistories = new HashMap<>();
36     private final RangeSet<UnsignedLong> purgedHistories;
37     private final ClientIdentifier identifier;
38
39     FrontendClientMetadataBuilder(final ClientIdentifier identifier) {
40         this.identifier = Preconditions.checkNotNull(identifier);
41         purgedHistories = TreeRangeSet.create();
42     }
43
44     FrontendClientMetadataBuilder(final FrontendClientMetadata meta) {
45         this.identifier = Preconditions.checkNotNull(meta.getIdentifier());
46         purgedHistories = TreeRangeSet.create(meta.getPurgedHistories());
47
48         for (FrontendHistoryMetadata h : meta.getCurrentHistories()) {
49             final FrontendHistoryMetadataBuilder b = new FrontendHistoryMetadataBuilder(identifier, h);
50             currentHistories.put(b.getIdentifier(), b);
51         }
52     }
53
54     @Override
55     public FrontendClientMetadata build() {
56         return new FrontendClientMetadata(identifier, purgedHistories,
57             Collections2.transform(currentHistories.values(), FrontendHistoryMetadataBuilder::build));
58     }
59
60     @Override
61     public ClientIdentifier getIdentifier() {
62         return identifier;
63     }
64
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);
71         } else {
72             LOG.debug("Created local history {}", historyId);
73         }
74     }
75
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);
81         } else {
82             LOG.warn("Closed unknown history {}, ignoring", historyId);
83         }
84     }
85
86     void onHistoryPurged(final LocalHistoryIdentifier historyId) {
87         final FrontendHistoryMetadataBuilder history = currentHistories.remove(historyId);
88         if (history == null) {
89             LOG.warn("Purging unknown history {}", historyId);
90         }
91
92         // XXX: do we need to account for cookies?
93         purgedHistories.add(Range.singleton(UnsignedLong.fromLongBits(historyId.getHistoryId())));
94         LOG.debug("Purged history {}", historyId);
95     }
96
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);
102         } else {
103             LOG.warn("Unknown history for aborted transaction {}, ignoring", txId);
104         }
105     }
106
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);
112         } else {
113             LOG.warn("Unknown history for commited transaction {}, ignoring", txId);
114         }
115     }
116
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);
122         } else {
123             LOG.warn("Unknown history for purged transaction {}, ignoring", txId);
124         }
125     }
126
127     /**
128      * Transform frontend metadata for a particular client into its {@link LeaderFrontendState} counterpart.
129      *
130      * @param shard parent shard
131      * @return Leader frontend state
132      */
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);
142             }
143         }
144
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);
151         } else {
152             singleHistory = singleHistoryMeta.toLeaderState(shard);
153         }
154
155         return new LeaderFrontendState(shard.persistenceId(), getIdentifier(), shard.getDataStore(),
156             TreeRangeSet.create(purgedHistories), singleHistory, histories);
157     }
158
159     private FrontendHistoryMetadataBuilder getHistory(final TransactionIdentifier txId) {
160         return currentHistories.get(txId.getHistoryId());
161     }
162 }