2 * Copyright (c) 2014 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.md.sal.dom.store.impl;
10 import static com.google.common.base.Preconditions.checkState;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import com.google.common.util.concurrent.ListeningExecutorService;
19 import java.util.concurrent.Callable;
20 import java.util.concurrent.ExecutorService;
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.atomic.AtomicLong;
24 import javax.annotation.concurrent.GuardedBy;
26 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
27 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
28 import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException;
29 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
30 import org.opendaylight.controller.md.sal.dom.store.impl.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
31 import org.opendaylight.controller.md.sal.dom.store.impl.tree.ListenerTree;
32 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
33 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
34 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
35 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
36 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
37 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
38 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
39 import org.opendaylight.yangtools.concepts.Identifiable;
40 import org.opendaylight.yangtools.concepts.ListenerRegistration;
41 import org.opendaylight.yangtools.util.ExecutorServiceUtil;
42 import org.opendaylight.yangtools.util.concurrent.NotificationManager;
43 import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager;
44 import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager.Invoker;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
46 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
48 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
49 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
50 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
51 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
52 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
53 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
54 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
55 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
60 * In-memory DOM Data Store
62 * Implementation of {@link DOMStore} which uses {@link DataTree} and other
63 * classes such as {@link SnapshotBackedWriteTransaction}.
64 * {@link SnapshotBackedReadTransaction} and {@link ResolveDataChangeEventsTask}
65 * to implement {@link DOMStore} contract.
68 public class InMemoryDOMDataStore implements DOMStore, Identifiable<String>, SchemaContextListener,
69 TransactionReadyPrototype,AutoCloseable {
70 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMDataStore.class);
71 private static final ListenableFuture<Void> SUCCESSFUL_FUTURE = Futures.immediateFuture(null);
73 private static final Invoker<DataChangeListenerRegistration<?>, DOMImmutableDataChangeEvent> DCL_NOTIFICATION_MGR_INVOKER =
74 new Invoker<DataChangeListenerRegistration<?>, DOMImmutableDataChangeEvent>() {
76 public void invokeListener(final DataChangeListenerRegistration<?> listener,
77 final DOMImmutableDataChangeEvent notification ) {
78 final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> inst = listener.getInstance();
80 inst.onDataChanged(notification);
85 private final DataTree dataTree = InMemoryDataTreeFactory.getInstance().create();
86 private final ListenerTree listenerTree = ListenerTree.create();
87 private final AtomicLong txCounter = new AtomicLong(0);
88 private final ListeningExecutorService listeningExecutor;
90 private final NotificationManager<DataChangeListenerRegistration<?>, DOMImmutableDataChangeEvent> dataChangeListenerNotificationManager;
91 private final ExecutorService dataChangeListenerExecutor;
93 private final String name;
95 public InMemoryDOMDataStore(final String name, final ListeningExecutorService listeningExecutor,
96 final ExecutorService dataChangeListenerExecutor) {
97 this(name, listeningExecutor, dataChangeListenerExecutor,
98 InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_LISTENER_QUEUE_SIZE);
101 public InMemoryDOMDataStore(final String name, final ListeningExecutorService listeningExecutor,
102 final ExecutorService dataChangeListenerExecutor, final int maxDataChangeListenerQueueSize) {
103 this.name = Preconditions.checkNotNull(name);
104 this.listeningExecutor = Preconditions.checkNotNull(listeningExecutor);
105 this.dataChangeListenerExecutor = Preconditions.checkNotNull(dataChangeListenerExecutor);
107 dataChangeListenerNotificationManager =
108 new QueuedNotificationManager<>(this.dataChangeListenerExecutor,
109 DCL_NOTIFICATION_MGR_INVOKER, maxDataChangeListenerQueueSize,
110 "DataChangeListenerQueueMgr");
114 public final String getIdentifier() {
119 public DOMStoreReadTransaction newReadOnlyTransaction() {
120 return new SnapshotBackedReadTransaction(nextIdentifier(), dataTree.takeSnapshot());
124 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
125 return new SnapshotBackedReadWriteTransaction(nextIdentifier(), dataTree.takeSnapshot(), this);
129 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
130 return new SnapshotBackedWriteTransaction(nextIdentifier(), dataTree.takeSnapshot(), this);
134 public DOMStoreTransactionChain createTransactionChain() {
135 return new DOMStoreTransactionChainImpl();
139 public synchronized void onGlobalContextUpdated(final SchemaContext ctx) {
140 dataTree.setSchemaContext(ctx);
144 public void close() {
145 ExecutorServiceUtil.tryGracefulShutdown(listeningExecutor, 30, TimeUnit.SECONDS);
146 ExecutorServiceUtil.tryGracefulShutdown(dataChangeListenerExecutor, 30, TimeUnit.SECONDS);
149 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> ListenerRegistration<L> registerChangeListener(
150 final YangInstanceIdentifier path, final L listener, final DataChangeScope scope) {
153 * Make sure commit is not occurring right now. Listener has to be
154 * registered and its state capture enqueued at a consistent point.
156 * FIXME: improve this to read-write lock, such that multiple listener
157 * registrations can occur simultaneously
159 final DataChangeListenerRegistration<L> reg;
160 synchronized (this) {
161 LOG.debug("{}: Registering data change listener {} for {}", name, listener, path);
163 reg = listenerTree.registerDataChangeListener(path, listener, scope);
165 Optional<NormalizedNode<?, ?>> currentState = dataTree.takeSnapshot().readNode(path);
166 if (currentState.isPresent()) {
167 final NormalizedNode<?, ?> data = currentState.get();
169 final DOMImmutableDataChangeEvent event = DOMImmutableDataChangeEvent.builder(DataChangeScope.BASE) //
171 .addCreated(path, data) //
174 dataChangeListenerNotificationManager.submitNotification(reg, event);
178 return new AbstractListenerRegistration<L>(listener) {
180 protected void removeRegistration() {
181 synchronized (InMemoryDOMDataStore.this) {
189 public DOMStoreThreePhaseCommitCohort ready(final SnapshotBackedWriteTransaction writeTx) {
190 LOG.debug("Tx: {} is submitted. Modifications: {}", writeTx.getIdentifier(), writeTx.getMutatedView());
191 return new ThreePhaseCommitImpl(writeTx);
194 private Object nextIdentifier() {
195 return name + "-" + txCounter.getAndIncrement();
198 private class DOMStoreTransactionChainImpl implements DOMStoreTransactionChain, TransactionReadyPrototype {
201 private SnapshotBackedWriteTransaction latestOutstandingTx;
203 private boolean chainFailed = false;
205 private void checkFailed() {
206 Preconditions.checkState(!chainFailed, "Transaction chain is failed.");
210 public synchronized DOMStoreReadTransaction newReadOnlyTransaction() {
211 final DataTreeSnapshot snapshot;
213 if (latestOutstandingTx != null) {
214 checkState(latestOutstandingTx.isReady(), "Previous transaction in chain must be ready.");
215 snapshot = latestOutstandingTx.getMutatedView();
217 snapshot = dataTree.takeSnapshot();
219 return new SnapshotBackedReadTransaction(nextIdentifier(), snapshot);
223 public synchronized DOMStoreReadWriteTransaction newReadWriteTransaction() {
224 final DataTreeSnapshot snapshot;
226 if (latestOutstandingTx != null) {
227 checkState(latestOutstandingTx.isReady(), "Previous transaction in chain must be ready.");
228 snapshot = latestOutstandingTx.getMutatedView();
230 snapshot = dataTree.takeSnapshot();
232 final SnapshotBackedReadWriteTransaction ret = new SnapshotBackedReadWriteTransaction(nextIdentifier(),
234 latestOutstandingTx = ret;
239 public synchronized DOMStoreWriteTransaction newWriteOnlyTransaction() {
240 final DataTreeSnapshot snapshot;
242 if (latestOutstandingTx != null) {
243 checkState(latestOutstandingTx.isReady(), "Previous transaction in chain must be ready.");
244 snapshot = latestOutstandingTx.getMutatedView();
246 snapshot = dataTree.takeSnapshot();
248 final SnapshotBackedWriteTransaction ret = new SnapshotBackedWriteTransaction(nextIdentifier(), snapshot,
250 latestOutstandingTx = ret;
255 public DOMStoreThreePhaseCommitCohort ready(final SnapshotBackedWriteTransaction tx) {
256 DOMStoreThreePhaseCommitCohort storeCohort = InMemoryDOMDataStore.this.ready(tx);
257 return new ChainedTransactionCommitImpl(tx, storeCohort, this);
261 public void close() {
263 // FIXME: this call doesn't look right here - listeningExecutor is shared and owned
264 // by the outer class.
265 //listeningExecutor.shutdownNow();
268 protected synchronized void onTransactionFailed(final SnapshotBackedWriteTransaction transaction,
274 public synchronized void onTransactionCommited(final SnapshotBackedWriteTransaction transaction) {
275 // If committed transaction is latestOutstandingTx we clear
276 // latestOutstandingTx
277 // field in order to base new transactions on Datastore Data Tree
279 if (transaction.equals(latestOutstandingTx)) {
280 latestOutstandingTx = null;
286 private static class ChainedTransactionCommitImpl implements DOMStoreThreePhaseCommitCohort {
288 private final SnapshotBackedWriteTransaction transaction;
289 private final DOMStoreThreePhaseCommitCohort delegate;
291 private final DOMStoreTransactionChainImpl txChain;
293 protected ChainedTransactionCommitImpl(final SnapshotBackedWriteTransaction transaction,
294 final DOMStoreThreePhaseCommitCohort delegate, final DOMStoreTransactionChainImpl txChain) {
296 this.transaction = transaction;
297 this.delegate = delegate;
298 this.txChain = txChain;
302 public ListenableFuture<Boolean> canCommit() {
303 return delegate.canCommit();
307 public ListenableFuture<Void> preCommit() {
308 return delegate.preCommit();
312 public ListenableFuture<Void> abort() {
313 return delegate.abort();
317 public ListenableFuture<Void> commit() {
318 ListenableFuture<Void> commitFuture = delegate.commit();
319 Futures.addCallback(commitFuture, new FutureCallback<Void>() {
321 public void onFailure(final Throwable t) {
322 txChain.onTransactionFailed(transaction, t);
326 public void onSuccess(final Void result) {
327 txChain.onTransactionCommited(transaction);
336 private class ThreePhaseCommitImpl implements DOMStoreThreePhaseCommitCohort {
338 private final SnapshotBackedWriteTransaction transaction;
339 private final DataTreeModification modification;
341 private ResolveDataChangeEventsTask listenerResolver;
342 private DataTreeCandidate candidate;
344 public ThreePhaseCommitImpl(final SnapshotBackedWriteTransaction writeTransaction) {
345 this.transaction = writeTransaction;
346 this.modification = transaction.getMutatedView();
350 public ListenableFuture<Boolean> canCommit() {
351 return listeningExecutor.submit(new Callable<Boolean>() {
353 public Boolean call() throws TransactionCommitFailedException {
355 dataTree.validate(modification);
356 LOG.debug("Store Transaction: {} can be committed", transaction.getIdentifier());
358 } catch (ConflictingModificationAppliedException e) {
359 LOG.warn("Store Tx: {} Conflicting modification for {}.", transaction.getIdentifier(),
361 throw new OptimisticLockFailedException("Optimistic lock failed.",e);
362 } catch (DataValidationFailedException e) {
363 LOG.warn("Store Tx: {} Data Precondition failed for {}.", transaction.getIdentifier(),
365 throw new TransactionCommitFailedException("Data did not pass validation.",e);
372 public ListenableFuture<Void> preCommit() {
373 return listeningExecutor.submit(new Callable<Void>() {
376 candidate = dataTree.prepare(modification);
377 listenerResolver = ResolveDataChangeEventsTask.create(candidate, listenerTree);
384 public ListenableFuture<Void> abort() {
386 return SUCCESSFUL_FUTURE;
390 public ListenableFuture<Void> commit() {
391 checkState(candidate != null, "Proposed subtree must be computed");
394 * The commit has to occur atomically with regard to listener
397 synchronized (this) {
398 dataTree.commit(candidate);
399 listenerResolver.resolve(dataChangeListenerNotificationManager);
402 return SUCCESSFUL_FUTURE;