Merge "Cleanup: Remove passing around of DataPersistenceProvider"
[controller.git] / opendaylight / md-sal / sal-inmemory-datastore / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / InMemoryDOMDataStore.java
1 /*
2  * Copyright (c) 2014 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.md.sal.dom.store.impl;
9
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.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.concurrent.ExecutorService;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.atomic.AtomicLong;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
20 import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
23 import org.opendaylight.controller.md.sal.dom.store.impl.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
24 import org.opendaylight.controller.md.sal.dom.store.impl.tree.ListenerTree;
25 import org.opendaylight.controller.sal.core.spi.data.AbstractDOMStoreTransaction;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
28 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
29 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
30 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
31 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTreeChangePublisher;
32 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
33 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
34 import org.opendaylight.yangtools.concepts.Identifiable;
35 import org.opendaylight.yangtools.concepts.ListenerRegistration;
36 import org.opendaylight.yangtools.util.ExecutorServiceUtil;
37 import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager;
38 import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager.Invoker;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
42 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
43 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
44 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
45 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
46 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
47 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
48 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
49 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 /**
54  * In-memory DOM Data Store
55  *
56  * Implementation of {@link DOMStore} which uses {@link DataTree} and other
57  * classes such as {@link SnapshotBackedWriteTransaction}.
58  * {@link SnapshotBackedReadTransaction} and {@link ResolveDataChangeEventsTask}
59  * to implement {@link DOMStore} contract.
60  *
61  */
62 public class InMemoryDOMDataStore extends TransactionReadyPrototype implements DOMStore, Identifiable<String>, SchemaContextListener, AutoCloseable, DOMStoreTreeChangePublisher {
63     private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMDataStore.class);
64     private static final ListenableFuture<Void> SUCCESSFUL_FUTURE = Futures.immediateFuture(null);
65     private static final ListenableFuture<Boolean> CAN_COMMIT_FUTURE = Futures.immediateFuture(Boolean.TRUE);
66
67     private static final Invoker<DataChangeListenerRegistration<?>, DOMImmutableDataChangeEvent> DCL_NOTIFICATION_MGR_INVOKER =
68             new Invoker<DataChangeListenerRegistration<?>, DOMImmutableDataChangeEvent>() {
69                 @Override
70                 public void invokeListener(final DataChangeListenerRegistration<?> listener,
71                                            final DOMImmutableDataChangeEvent notification ) {
72                     final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> inst = listener.getInstance();
73                     if (inst != null) {
74                         inst.onDataChanged(notification);
75                     }
76                 }
77             };
78
79     private final DataTree dataTree = InMemoryDataTreeFactory.getInstance().create();
80     private final ListenerTree listenerTree = ListenerTree.create();
81     private final AtomicLong txCounter = new AtomicLong(0);
82
83     private final QueuedNotificationManager<DataChangeListenerRegistration<?>, DOMImmutableDataChangeEvent> dataChangeListenerNotificationManager;
84     private final InMemoryDOMStoreTreeChangePublisher changePublisher;
85     private final ExecutorService dataChangeListenerExecutor;
86     private final boolean debugTransactions;
87     private final String name;
88
89     private volatile AutoCloseable closeable;
90
91     public InMemoryDOMDataStore(final String name, final ExecutorService dataChangeListenerExecutor) {
92         this(name, dataChangeListenerExecutor, InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_LISTENER_QUEUE_SIZE, false);
93     }
94
95     public InMemoryDOMDataStore(final String name, final ExecutorService dataChangeListenerExecutor,
96             final int maxDataChangeListenerQueueSize, final boolean debugTransactions) {
97         this.name = Preconditions.checkNotNull(name);
98         this.dataChangeListenerExecutor = Preconditions.checkNotNull(dataChangeListenerExecutor);
99         this.debugTransactions = debugTransactions;
100
101         dataChangeListenerNotificationManager =
102                 new QueuedNotificationManager<>(this.dataChangeListenerExecutor,
103                         DCL_NOTIFICATION_MGR_INVOKER, maxDataChangeListenerQueueSize,
104                         "DataChangeListenerQueueMgr");
105         changePublisher = new InMemoryDOMStoreTreeChangePublisher(this.dataChangeListenerExecutor, maxDataChangeListenerQueueSize);
106     }
107
108     public void setCloseable(final AutoCloseable closeable) {
109         this.closeable = closeable;
110     }
111
112     public QueuedNotificationManager<?, ?> getDataChangeListenerNotificationManager() {
113         return dataChangeListenerNotificationManager;
114     }
115
116     @Override
117     public final String getIdentifier() {
118         return name;
119     }
120
121     @Override
122     public DOMStoreReadTransaction newReadOnlyTransaction() {
123         return new SnapshotBackedReadTransaction(nextIdentifier(), debugTransactions, dataTree.takeSnapshot());
124     }
125
126     @Override
127     public DOMStoreReadWriteTransaction newReadWriteTransaction() {
128         return new SnapshotBackedReadWriteTransaction(nextIdentifier(), debugTransactions, dataTree.takeSnapshot(), this);
129     }
130
131     @Override
132     public DOMStoreWriteTransaction newWriteOnlyTransaction() {
133         return new SnapshotBackedWriteTransaction(nextIdentifier(), debugTransactions, dataTree.takeSnapshot(), this);
134     }
135
136     @Override
137     public DOMStoreTransactionChain createTransactionChain() {
138         return new DOMStoreTransactionChainImpl(this);
139     }
140
141     @Override
142     public synchronized void onGlobalContextUpdated(final SchemaContext ctx) {
143         dataTree.setSchemaContext(ctx);
144     }
145
146     @Override
147     public void close() {
148         ExecutorServiceUtil.tryGracefulShutdown(dataChangeListenerExecutor, 30, TimeUnit.SECONDS);
149
150         if(closeable != null) {
151             try {
152                 closeable.close();
153             } catch(Exception e) {
154                 LOG.debug("Error closing instance", e);
155             }
156         }
157     }
158
159     public final boolean getDebugTransactions() {
160         return debugTransactions;
161     }
162
163     final DataTreeSnapshot takeSnapshot() {
164         return dataTree.takeSnapshot();
165     }
166
167     @Override
168     public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> ListenerRegistration<L> registerChangeListener(
169             final YangInstanceIdentifier path, final L listener, final DataChangeScope scope) {
170
171         /*
172          * Make sure commit is not occurring right now. Listener has to be
173          * registered and its state capture enqueued at a consistent point.
174          *
175          * FIXME: improve this to read-write lock, such that multiple listener
176          * registrations can occur simultaneously
177          */
178         final DataChangeListenerRegistration<L> reg;
179         synchronized (this) {
180             LOG.debug("{}: Registering data change listener {} for {}", name, listener, path);
181
182             reg = listenerTree.registerDataChangeListener(path, listener, scope);
183
184             Optional<NormalizedNode<?, ?>> currentState = dataTree.takeSnapshot().readNode(path);
185             if (currentState.isPresent()) {
186                 final NormalizedNode<?, ?> data = currentState.get();
187
188                 final DOMImmutableDataChangeEvent event = DOMImmutableDataChangeEvent.builder(DataChangeScope.BASE) //
189                         .setAfter(data) //
190                         .addCreated(path, data) //
191                         .build();
192
193                 dataChangeListenerNotificationManager.submitNotification(reg, event);
194             }
195         }
196
197         return new AbstractListenerRegistration<L>(listener) {
198             @Override
199             protected void removeRegistration() {
200                 synchronized (InMemoryDOMDataStore.this) {
201                     reg.close();
202                 }
203             }
204         };
205     }
206
207     @Override
208     public synchronized <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerTreeChangeListener(final YangInstanceIdentifier treeId, final L listener) {
209         /*
210          * Make sure commit is not occurring right now. Listener has to be
211          * registered and its state capture enqueued at a consistent point.
212          */
213         return changePublisher.registerTreeChangeListener(treeId, listener, dataTree.takeSnapshot());
214     }
215
216     @Override
217     protected void transactionAborted(final SnapshotBackedWriteTransaction tx) {
218         LOG.debug("Tx: {} is closed.", tx.getIdentifier());
219     }
220
221     @Override
222     protected DOMStoreThreePhaseCommitCohort transactionReady(final SnapshotBackedWriteTransaction tx, final DataTreeModification tree) {
223         LOG.debug("Tx: {} is submitted. Modifications: {}", tx.getIdentifier(), tree);
224         return new ThreePhaseCommitImpl(tx, tree);
225     }
226
227     Object nextIdentifier() {
228         return name + "-" + txCounter.getAndIncrement();
229     }
230
231     private static void warnDebugContext(final AbstractDOMStoreTransaction<?> transaction) {
232         final Throwable ctx = transaction.getDebugContext();
233         if (ctx != null) {
234             LOG.warn("Transaction {} has been allocated in the following context", transaction.getIdentifier(), ctx);
235         }
236     }
237
238     private final class ThreePhaseCommitImpl implements DOMStoreThreePhaseCommitCohort {
239         private final SnapshotBackedWriteTransaction transaction;
240         private final DataTreeModification modification;
241
242         private ResolveDataChangeEventsTask listenerResolver;
243         private DataTreeCandidate candidate;
244
245         public ThreePhaseCommitImpl(final SnapshotBackedWriteTransaction writeTransaction, final DataTreeModification modification) {
246             this.transaction = writeTransaction;
247             this.modification = modification;
248         }
249
250         @Override
251         public ListenableFuture<Boolean> canCommit() {
252             try {
253                 dataTree.validate(modification);
254                 LOG.debug("Store Transaction: {} can be committed", transaction.getIdentifier());
255                 return CAN_COMMIT_FUTURE;
256             } catch (ConflictingModificationAppliedException e) {
257                 LOG.warn("Store Tx: {} Conflicting modification for {}.", transaction.getIdentifier(),
258                         e.getPath());
259                 warnDebugContext(transaction);
260                 return Futures.immediateFailedFuture(new OptimisticLockFailedException("Optimistic lock failed.", e));
261             } catch (DataValidationFailedException e) {
262                 LOG.warn("Store Tx: {} Data Precondition failed for {}.", transaction.getIdentifier(),
263                         e.getPath(), e);
264                 warnDebugContext(transaction);
265
266                 // For debugging purposes, allow dumping of the modification. Coupled with the above
267                 // precondition log, it should allow us to understand what went on.
268                 LOG.trace("Store Tx: {} modifications: {} tree: {}", modification, dataTree);
269
270                 return Futures.immediateFailedFuture(new TransactionCommitFailedException("Data did not pass validation.", e));
271             } catch (Exception e) {
272                 LOG.warn("Unexpected failure in validation phase", e);
273                 return Futures.immediateFailedFuture(e);
274             }
275         }
276
277         @Override
278         public ListenableFuture<Void> preCommit() {
279             try {
280                 candidate = dataTree.prepare(modification);
281                 listenerResolver = ResolveDataChangeEventsTask.create(candidate, listenerTree);
282                 return SUCCESSFUL_FUTURE;
283             } catch (Exception e) {
284                 LOG.warn("Unexpected failure in pre-commit phase", e);
285                 return Futures.immediateFailedFuture(e);
286             }
287         }
288
289         @Override
290         public ListenableFuture<Void> abort() {
291             candidate = null;
292             return SUCCESSFUL_FUTURE;
293         }
294
295         @Override
296         public ListenableFuture<Void> commit() {
297             checkState(candidate != null, "Proposed subtree must be computed");
298
299             /*
300              * The commit has to occur atomically with regard to listener
301              * registrations.
302              */
303             synchronized (InMemoryDOMDataStore.this) {
304                 dataTree.commit(candidate);
305                 changePublisher.publishChange(candidate);
306                 listenerResolver.resolve(dataChangeListenerNotificationManager);
307             }
308
309             return SUCCESSFUL_FUTURE;
310         }
311     }
312 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.