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.mdsal.dom.store.inmemory;
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.Collection;
15 import java.util.concurrent.atomic.AtomicLong;
16 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
18 import org.opendaylight.mdsal.dom.spi.shard.DOMDataTreeShardProducer;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 class InMemoryDOMDataTreeShardProducer implements DOMDataTreeShardProducer {
26 private abstract static class State {
28 * Allocate a new snapshot.
30 * @return A new snapshot
32 protected abstract DataTreeSnapshot getSnapshot(Object transactionId);
35 private static final class Idle extends State {
36 private final InMemoryDOMDataTreeShardProducer producer;
38 Idle(final InMemoryDOMDataTreeShardProducer producer) {
39 this.producer = requireNonNull(producer);
43 protected DataTreeSnapshot getSnapshot(final Object transactionId) {
44 return producer.takeSnapshot();
49 * We have a transaction out there.
51 private static final class Allocated extends State {
52 private static final AtomicReferenceFieldUpdater<Allocated, DataTreeSnapshot> SNAPSHOT_UPDATER =
53 AtomicReferenceFieldUpdater.newUpdater(Allocated.class, DataTreeSnapshot.class, "snapshot");
54 private final InmemoryDOMDataTreeShardWriteTransaction transaction;
55 private volatile DataTreeSnapshot snapshot;
57 Allocated(final InmemoryDOMDataTreeShardWriteTransaction transaction) {
58 this.transaction = requireNonNull(transaction);
61 InmemoryDOMDataTreeShardWriteTransaction getTransaction() {
66 protected DataTreeSnapshot getSnapshot(final Object transactionId) {
67 final DataTreeSnapshot ret = snapshot;
68 checkState(ret != null,
69 "Could not get snapshot for transaction %s - previous transaction %s is not ready yet",
70 transactionId, transaction.getIdentifier());
74 void setSnapshot(final DataTreeSnapshot snapshot) {
75 final boolean success = SNAPSHOT_UPDATER.compareAndSet(this, null, snapshot);
76 checkState(success, "Transaction %s has already been marked as ready",
77 transaction.getIdentifier());
82 * Producer is logically shut down, no further allocation allowed.
84 private static final class Shutdown extends State {
85 private final String message;
87 Shutdown(final String message) {
88 this.message = requireNonNull(message);
92 protected DataTreeSnapshot getSnapshot(final Object transactionId) {
93 throw new IllegalStateException(message);
97 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMDataTreeShardProducer.class);
98 private static final AtomicLong COUNTER = new AtomicLong();
100 private final InMemoryDOMDataTreeShard parentShard;
101 private final Collection<DOMDataTreeIdentifier> prefixes;
102 private final Idle idleState = new Idle(this);
104 private static final AtomicReferenceFieldUpdater<InMemoryDOMDataTreeShardProducer, State> STATE_UPDATER =
105 AtomicReferenceFieldUpdater.newUpdater(InMemoryDOMDataTreeShardProducer.class, State.class, "state");
106 private volatile State state;
108 private InMemoryShardDataModificationFactory modificationFactory;
110 InMemoryDOMDataTreeShardProducer(final InMemoryDOMDataTreeShard parentShard,
111 final Collection<DOMDataTreeIdentifier> prefixes,
112 final InMemoryShardDataModificationFactory modificationFactory) {
113 this.parentShard = requireNonNull(parentShard);
114 this.prefixes = ImmutableSet.copyOf(prefixes);
115 this.modificationFactory = requireNonNull(modificationFactory);
120 public InmemoryDOMDataTreeShardWriteTransaction createTransaction() {
121 final String transactionId = nextIdentifier();
124 InmemoryDOMDataTreeShardWriteTransaction ret;
127 ret = parentShard.createTransaction(transactionId, this, localState.getSnapshot(transactionId));
128 } while (!STATE_UPDATER.compareAndSet(this, localState, new Allocated(ret)));
133 void transactionReady(final InmemoryDOMDataTreeShardWriteTransaction tx, final DataTreeModification modification) {
134 final State localState = state;
135 LOG.debug("Transaction was readied {}, current state {}", tx.getIdentifier(), localState);
137 if (localState instanceof Allocated) {
138 final Allocated allocated = (Allocated) localState;
139 final InmemoryDOMDataTreeShardWriteTransaction transaction = allocated.getTransaction();
140 checkState(tx.equals(transaction), "Mis-ordered ready transaction %s last allocated was %s", tx,
142 allocated.setSnapshot(modification);
144 LOG.debug("Ignoring transaction {} readiness due to state {}", tx, localState);
149 * Notify the base logic that a previously-submitted transaction has been committed successfully.
151 * @param transaction Transaction which completed successfully.
153 void onTransactionCommited(final InmemoryDOMDataTreeShardWriteTransaction transaction) {
154 // If the committed transaction was the one we allocated last,
155 // we clear it and the ready snapshot, so the next transaction
156 // allocated refers to the data tree directly.
157 final State localState = state;
158 LOG.debug("Transaction {} commit done, current state {}", transaction.getIdentifier(), localState);
160 if (!(localState instanceof Allocated)) {
161 // This can legally happen if the chain is shut down before the transaction was committed
163 LOG.debug("Ignoring successful transaction {} in state {}", transaction, localState);
167 final Allocated allocated = (Allocated) localState;
168 final InmemoryDOMDataTreeShardWriteTransaction tx = allocated.getTransaction();
169 if (!tx.equals(transaction)) {
170 LOG.debug("Ignoring non-latest successful transaction {} in state {}", transaction, allocated);
174 if (!STATE_UPDATER.compareAndSet(this, localState, idleState)) {
175 LOG.debug("Producer {} has already transitioned from {} to {}, not making it idle", this,
180 void transactionAborted(final InmemoryDOMDataTreeShardWriteTransaction tx) {
181 final State localState = state;
182 if (localState instanceof Allocated) {
183 final Allocated allocated = (Allocated) localState;
184 if (allocated.getTransaction().equals(tx)) {
185 final boolean success = STATE_UPDATER.compareAndSet(this, localState, idleState);
187 LOG.warn("Transaction {} aborted, but producer {} state already transitioned from {} to {}",
188 tx, this, localState, state);
194 private static String nextIdentifier() {
195 return "INMEMORY-SHARD-TX-" + COUNTER.getAndIncrement();
198 DataTreeSnapshot takeSnapshot() {
199 return parentShard.takeSnapshot();
203 public Collection<DOMDataTreeIdentifier> getPrefixes() {
207 InMemoryShardDataModificationFactory getModificationFactory() {
208 return modificationFactory;
211 void setModificationFactory(final InMemoryShardDataModificationFactory modificationFactory) {
212 this.modificationFactory = requireNonNull(modificationFactory);