Fix shard deadlock in 3 nodes
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / StandaloneFrontendHistory.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.collect.ImmutableMap;
12 import com.google.common.collect.RangeSet;
13 import com.google.common.collect.TreeRangeSet;
14 import com.google.common.primitives.UnsignedLong;
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.Optional;
18 import java.util.SortedSet;
19 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
20 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
21 import org.opendaylight.controller.cluster.access.concepts.RequestException;
22 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
24
25 /**
26  * Standalone transaction specialization of {@link AbstractFrontendHistory}. There can be multiple open transactions
27  * and they are submitted in any order.
28  *
29  * @author Robert Varga
30  */
31 final class StandaloneFrontendHistory extends AbstractFrontendHistory {
32     private final LocalHistoryIdentifier identifier;
33     private final ShardDataTree tree;
34
35     private StandaloneFrontendHistory(final String persistenceId, final ClientIdentifier clientId,
36             final ShardDataTree tree, final Map<UnsignedLong, Boolean> closedTransactions,
37             final RangeSet<UnsignedLong> purgedTransactions) {
38         super(persistenceId, tree, closedTransactions, purgedTransactions);
39         this.identifier = new LocalHistoryIdentifier(clientId, 0);
40         this.tree = Preconditions.checkNotNull(tree);
41     }
42
43     static StandaloneFrontendHistory create(final String persistenceId, final ClientIdentifier clientId,
44             final ShardDataTree tree) {
45         return new StandaloneFrontendHistory(persistenceId, clientId, tree, ImmutableMap.of(),
46             TreeRangeSet.create());
47     }
48
49     static StandaloneFrontendHistory recreate(final String persistenceId, final ClientIdentifier clientId,
50             final ShardDataTree tree, final Map<UnsignedLong, Boolean> closedTransactions,
51             final RangeSet<UnsignedLong> purgedTransactions) {
52         return new StandaloneFrontendHistory(persistenceId, clientId, tree, new HashMap<>(closedTransactions),
53             purgedTransactions);
54     }
55
56     @Override
57     public LocalHistoryIdentifier getIdentifier() {
58         return identifier;
59     }
60
61     @Override
62     FrontendTransaction createOpenSnapshot(final TransactionIdentifier id) throws RequestException {
63         return FrontendReadOnlyTransaction.create(this, tree.newReadOnlyTransaction(id));
64     }
65
66     @Override
67     FrontendTransaction createOpenTransaction(final TransactionIdentifier id) throws RequestException {
68         return FrontendReadWriteTransaction.createOpen(this, tree.newReadWriteTransaction(id));
69     }
70
71     @Override
72     FrontendTransaction createReadyTransaction(final TransactionIdentifier id, final DataTreeModification mod)
73             throws RequestException {
74         return FrontendReadWriteTransaction.createReady(this, id, mod);
75     }
76
77     @Override
78     ShardDataTreeCohort createFailedCohort(final TransactionIdentifier id, final DataTreeModification mod,
79             final Exception failure) {
80         return tree.createFailedCohort(id, mod, failure);
81     }
82
83     @Override
84     ShardDataTreeCohort createReadyCohort(final TransactionIdentifier id, final DataTreeModification mod,
85             final Optional<SortedSet<String>> participatingShardNames) {
86         return tree.createReadyCohort(id, mod, participatingShardNames);
87     }
88 }