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;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.ListeningExecutorService;
17 import com.google.common.util.concurrent.MoreExecutors;
18 import java.util.concurrent.Callable;
19 import java.util.concurrent.ExecutorService;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.atomic.AtomicLong;
22 import javax.annotation.concurrent.GuardedBy;
23 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
24 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
25 import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException;
26 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
27 import org.opendaylight.controller.md.sal.dom.store.impl.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
28 import org.opendaylight.controller.md.sal.dom.store.impl.tree.ListenerTree;
29 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
30 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
31 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
32 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
33 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
34 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
35 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
36 import org.opendaylight.yangtools.concepts.Identifiable;
37 import org.opendaylight.yangtools.concepts.ListenerRegistration;
38 import org.opendaylight.yangtools.util.ExecutorServiceUtil;
39 import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager;
40 import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager.Invoker;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
44 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
45 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
46 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
47 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
48 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
49 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
50 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
51 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 * In-memory DOM Data Store
58 * Implementation of {@link DOMStore} which uses {@link DataTree} and other
59 * classes such as {@link SnapshotBackedWriteTransaction}.
60 * {@link SnapshotBackedReadTransaction} and {@link ResolveDataChangeEventsTask}
61 * to implement {@link DOMStore} contract.
64 public class InMemoryDOMDataStore extends TransactionReadyPrototype implements DOMStore, Identifiable<String>, SchemaContextListener, AutoCloseable {
65 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMDataStore.class);
66 private static final ListenableFuture<Void> SUCCESSFUL_FUTURE = Futures.immediateFuture(null);
68 private static final Invoker<DataChangeListenerRegistration<?>, DOMImmutableDataChangeEvent> DCL_NOTIFICATION_MGR_INVOKER =
69 new Invoker<DataChangeListenerRegistration<?>, DOMImmutableDataChangeEvent>() {
71 public void invokeListener(final DataChangeListenerRegistration<?> listener,
72 final DOMImmutableDataChangeEvent notification ) {
73 final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> inst = listener.getInstance();
75 inst.onDataChanged(notification);
80 private final DataTree dataTree = InMemoryDataTreeFactory.getInstance().create();
81 private final ListenerTree listenerTree = ListenerTree.create();
82 private final AtomicLong txCounter = new AtomicLong(0);
83 private final ListeningExecutorService listeningExecutor;
85 private final QueuedNotificationManager<DataChangeListenerRegistration<?>, DOMImmutableDataChangeEvent> dataChangeListenerNotificationManager;
86 private final ExecutorService dataChangeListenerExecutor;
88 private final ExecutorService domStoreExecutor;
89 private final boolean debugTransactions;
90 private final String name;
92 private volatile AutoCloseable closeable;
94 public InMemoryDOMDataStore(final String name, final ExecutorService domStoreExecutor,
95 final ExecutorService dataChangeListenerExecutor) {
96 this(name, domStoreExecutor, dataChangeListenerExecutor,
97 InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_LISTENER_QUEUE_SIZE, false);
100 public InMemoryDOMDataStore(final String name, final ExecutorService domStoreExecutor,
101 final ExecutorService dataChangeListenerExecutor, final int maxDataChangeListenerQueueSize,
102 final boolean debugTransactions) {
103 this.name = Preconditions.checkNotNull(name);
104 this.domStoreExecutor = Preconditions.checkNotNull(domStoreExecutor);
105 this.listeningExecutor = MoreExecutors.listeningDecorator(this.domStoreExecutor);
106 this.dataChangeListenerExecutor = Preconditions.checkNotNull(dataChangeListenerExecutor);
107 this.debugTransactions = debugTransactions;
109 dataChangeListenerNotificationManager =
110 new QueuedNotificationManager<>(this.dataChangeListenerExecutor,
111 DCL_NOTIFICATION_MGR_INVOKER, maxDataChangeListenerQueueSize,
112 "DataChangeListenerQueueMgr");
115 public void setCloseable(final AutoCloseable closeable) {
116 this.closeable = closeable;
119 public QueuedNotificationManager<?, ?> getDataChangeListenerNotificationManager() {
120 return dataChangeListenerNotificationManager;
123 public ExecutorService getDomStoreExecutor() {
124 return domStoreExecutor;
128 public final String getIdentifier() {
133 public DOMStoreReadTransaction newReadOnlyTransaction() {
134 return new SnapshotBackedReadTransaction(nextIdentifier(), debugTransactions, dataTree.takeSnapshot());
138 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
139 return new SnapshotBackedReadWriteTransaction(nextIdentifier(), debugTransactions, dataTree.takeSnapshot(), this);
143 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
144 return new SnapshotBackedWriteTransaction(nextIdentifier(), debugTransactions, dataTree.takeSnapshot(), this);
148 public DOMStoreTransactionChain createTransactionChain() {
149 return new DOMStoreTransactionChainImpl();
153 public synchronized void onGlobalContextUpdated(final SchemaContext ctx) {
154 dataTree.setSchemaContext(ctx);
158 public void close() {
159 ExecutorServiceUtil.tryGracefulShutdown(listeningExecutor, 30, TimeUnit.SECONDS);
160 ExecutorServiceUtil.tryGracefulShutdown(dataChangeListenerExecutor, 30, TimeUnit.SECONDS);
162 if(closeable != null) {
165 } catch(Exception e) {
166 LOG.debug("Error closing instance", e);
171 boolean getDebugTransactions() {
172 return debugTransactions;
176 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> ListenerRegistration<L> registerChangeListener(
177 final YangInstanceIdentifier path, final L listener, final DataChangeScope scope) {
180 * Make sure commit is not occurring right now. Listener has to be
181 * registered and its state capture enqueued at a consistent point.
183 * FIXME: improve this to read-write lock, such that multiple listener
184 * registrations can occur simultaneously
186 final DataChangeListenerRegistration<L> reg;
187 synchronized (this) {
188 LOG.debug("{}: Registering data change listener {} for {}", name, listener, path);
190 reg = listenerTree.registerDataChangeListener(path, listener, scope);
192 Optional<NormalizedNode<?, ?>> currentState = dataTree.takeSnapshot().readNode(path);
193 if (currentState.isPresent()) {
194 final NormalizedNode<?, ?> data = currentState.get();
196 final DOMImmutableDataChangeEvent event = DOMImmutableDataChangeEvent.builder(DataChangeScope.BASE) //
198 .addCreated(path, data) //
201 dataChangeListenerNotificationManager.submitNotification(reg, event);
205 return new AbstractListenerRegistration<L>(listener) {
207 protected void removeRegistration() {
208 synchronized (InMemoryDOMDataStore.this) {
216 protected void transactionAborted(final SnapshotBackedWriteTransaction tx) {
217 LOG.debug("Tx: {} is closed.", tx.getIdentifier());
221 protected DOMStoreThreePhaseCommitCohort transactionReady(final SnapshotBackedWriteTransaction tx, final DataTreeModification tree) {
222 LOG.debug("Tx: {} is submitted. Modifications: {}", tx.getIdentifier(), tree);
223 return new ThreePhaseCommitImpl(tx, tree);
226 private Object nextIdentifier() {
227 return name + "-" + txCounter.getAndIncrement();
230 private class DOMStoreTransactionChainImpl extends TransactionReadyPrototype implements DOMStoreTransactionChain {
232 private SnapshotBackedWriteTransaction allocatedTransaction;
234 private DataTreeSnapshot readySnapshot;
236 private boolean chainFailed = false;
239 private void checkFailed() {
240 Preconditions.checkState(!chainFailed, "Transaction chain is failed.");
244 private DataTreeSnapshot getSnapshot() {
247 if (allocatedTransaction != null) {
248 Preconditions.checkState(readySnapshot != null, "Previous transaction %s is not ready yet", allocatedTransaction.getIdentifier());
249 return readySnapshot;
251 return dataTree.takeSnapshot();
256 private <T extends SnapshotBackedWriteTransaction> T recordTransaction(final T transaction) {
257 allocatedTransaction = transaction;
258 readySnapshot = null;
263 public synchronized DOMStoreReadTransaction newReadOnlyTransaction() {
264 final DataTreeSnapshot snapshot = getSnapshot();
265 return new SnapshotBackedReadTransaction(nextIdentifier(), getDebugTransactions(), snapshot);
269 public synchronized DOMStoreReadWriteTransaction newReadWriteTransaction() {
270 final DataTreeSnapshot snapshot = getSnapshot();
271 return recordTransaction(new SnapshotBackedReadWriteTransaction(nextIdentifier(),
272 getDebugTransactions(), snapshot, this));
276 public synchronized DOMStoreWriteTransaction newWriteOnlyTransaction() {
277 final DataTreeSnapshot snapshot = getSnapshot();
278 return recordTransaction(new SnapshotBackedWriteTransaction(nextIdentifier(),
279 getDebugTransactions(), snapshot, this));
283 protected synchronized void transactionAborted(final SnapshotBackedWriteTransaction tx) {
284 if (tx.equals(allocatedTransaction)) {
285 Preconditions.checkState(readySnapshot == null, "Unexpected abort of transaction %s with ready snapshot %s", tx, readySnapshot);
286 allocatedTransaction = null;
291 protected synchronized DOMStoreThreePhaseCommitCohort transactionReady(final SnapshotBackedWriteTransaction tx, final DataTreeModification tree) {
292 Preconditions.checkState(tx.equals(allocatedTransaction), "Mis-ordered ready transaction %s last allocated was %s", tx, allocatedTransaction);
293 if (readySnapshot != null) {
294 // The snapshot should have been cleared
295 LOG.warn("Uncleared snapshot {} encountered, overwritten with transaction {} snapshot {}", readySnapshot, tx, tree);
298 final DOMStoreThreePhaseCommitCohort cohort = InMemoryDOMDataStore.this.transactionReady(tx, tree);
299 readySnapshot = tree;
300 return new ChainedTransactionCommitImpl(tx, cohort, this);
304 public void close() {
305 // FIXME: this call doesn't look right here - listeningExecutor is shared and owned
306 // by the outer class.
307 //listeningExecutor.shutdownNow();
310 protected synchronized void onTransactionFailed(final SnapshotBackedWriteTransaction transaction,
315 public synchronized void onTransactionCommited(final SnapshotBackedWriteTransaction transaction) {
316 // If the committed transaction was the one we allocated last,
317 // we clear it and the ready snapshot, so the next transaction
318 // allocated refers to the data tree directly.
319 if (transaction.equals(allocatedTransaction)) {
320 if (readySnapshot == null) {
321 LOG.warn("Transaction {} committed while no ready snapshot present", transaction);
324 allocatedTransaction = null;
325 readySnapshot = null;
330 private static class ChainedTransactionCommitImpl implements DOMStoreThreePhaseCommitCohort {
331 private final SnapshotBackedWriteTransaction transaction;
332 private final DOMStoreThreePhaseCommitCohort delegate;
333 private final DOMStoreTransactionChainImpl txChain;
335 protected ChainedTransactionCommitImpl(final SnapshotBackedWriteTransaction transaction,
336 final DOMStoreThreePhaseCommitCohort delegate, final DOMStoreTransactionChainImpl txChain) {
337 this.transaction = transaction;
338 this.delegate = delegate;
339 this.txChain = txChain;
343 public ListenableFuture<Boolean> canCommit() {
344 return delegate.canCommit();
348 public ListenableFuture<Void> preCommit() {
349 return delegate.preCommit();
353 public ListenableFuture<Void> abort() {
354 return delegate.abort();
358 public ListenableFuture<Void> commit() {
359 ListenableFuture<Void> commitFuture = delegate.commit();
360 Futures.addCallback(commitFuture, new FutureCallback<Void>() {
362 public void onFailure(final Throwable t) {
363 txChain.onTransactionFailed(transaction, t);
367 public void onSuccess(final Void result) {
368 txChain.onTransactionCommited(transaction);
375 private class ThreePhaseCommitImpl implements DOMStoreThreePhaseCommitCohort {
376 private final SnapshotBackedWriteTransaction transaction;
377 private final DataTreeModification modification;
379 private ResolveDataChangeEventsTask listenerResolver;
380 private DataTreeCandidate candidate;
382 public ThreePhaseCommitImpl(final SnapshotBackedWriteTransaction writeTransaction, final DataTreeModification modification) {
383 this.transaction = writeTransaction;
384 this.modification = modification;
388 public ListenableFuture<Boolean> canCommit() {
389 return listeningExecutor.submit(new Callable<Boolean>() {
391 public Boolean call() throws TransactionCommitFailedException {
393 dataTree.validate(modification);
394 LOG.debug("Store Transaction: {} can be committed", transaction.getIdentifier());
396 } catch (ConflictingModificationAppliedException e) {
397 LOG.warn("Store Tx: {} Conflicting modification for {}.", transaction.getIdentifier(),
399 transaction.warnDebugContext(LOG);
400 throw new OptimisticLockFailedException("Optimistic lock failed.",e);
401 } catch (DataValidationFailedException e) {
402 LOG.warn("Store Tx: {} Data Precondition failed for {}.", transaction.getIdentifier(),
404 transaction.warnDebugContext(LOG);
405 throw new TransactionCommitFailedException("Data did not pass validation.",e);
412 public ListenableFuture<Void> preCommit() {
413 return listeningExecutor.submit(new Callable<Void>() {
416 candidate = dataTree.prepare(modification);
417 listenerResolver = ResolveDataChangeEventsTask.create(candidate, listenerTree);
424 public ListenableFuture<Void> abort() {
426 return SUCCESSFUL_FUTURE;
430 public ListenableFuture<Void> commit() {
431 checkState(candidate != null, "Proposed subtree must be computed");
434 * The commit has to occur atomically with regard to listener
437 synchronized (InMemoryDOMDataStore.this) {
438 dataTree.commit(candidate);
439 listenerResolver.resolve(dataChangeListenerNotificationManager);
442 return SUCCESSFUL_FUTURE;