Delay snapshot backed transaction ready error
[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 com.google.common.base.Preconditions;
11 import java.util.Optional;
12 import java.util.concurrent.ExecutorService;
13 import java.util.concurrent.TimeUnit;
14 import java.util.concurrent.atomic.AtomicLong;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
19 import org.opendaylight.controller.md.sal.dom.store.impl.tree.ListenerTree;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTreeChangePublisher;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
27 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedTransactions;
28 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction;
29 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
30 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
31 import org.opendaylight.yangtools.concepts.Identifiable;
32 import org.opendaylight.yangtools.concepts.ListenerRegistration;
33 import org.opendaylight.yangtools.util.ExecutorServiceUtil;
34 import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager;
35 import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager.Invoker;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
39 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
40 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
41 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
42 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
43 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
44 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
45 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
46 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 /**
51  * In-memory DOM Data Store
52  *
53  * Implementation of {@link DOMStore} which uses {@link DataTree} and other
54  * classes such as {@link SnapshotBackedWriteTransaction}.
55  * {@link org.opendaylight.controller.sal.core.spi.data.SnapshotBackedReadTransaction} and {@link ResolveDataChangeEventsTask}
56  * to implement {@link DOMStore} contract.
57  *
58  */
59 public class InMemoryDOMDataStore extends TransactionReadyPrototype<String> implements DOMStore, Identifiable<String>, SchemaContextListener, AutoCloseable, DOMStoreTreeChangePublisher {
60     private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMDataStore.class);
61
62     private static final Invoker<DataChangeListenerRegistration<?>, DOMImmutableDataChangeEvent> DCL_NOTIFICATION_MGR_INVOKER =
63             (listener, notification) -> {
64                 final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> inst = listener.getInstance();
65                 if (inst != null) {
66                     inst.onDataChanged(notification);
67                 }
68             };
69
70     private final DataTree dataTree;
71     private final ListenerTree listenerTree = ListenerTree.create();
72     private final AtomicLong txCounter = new AtomicLong(0);
73
74     private final QueuedNotificationManager<DataChangeListenerRegistration<?>, DOMImmutableDataChangeEvent> dataChangeListenerNotificationManager;
75     private final InMemoryDOMStoreTreeChangePublisher changePublisher;
76     private final ExecutorService dataChangeListenerExecutor;
77     private final boolean debugTransactions;
78     private final String name;
79
80     private volatile AutoCloseable closeable;
81
82     public InMemoryDOMDataStore(final String name, final ExecutorService dataChangeListenerExecutor) {
83         this(name, LogicalDatastoreType.OPERATIONAL, dataChangeListenerExecutor,
84             InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_LISTENER_QUEUE_SIZE, false);
85     }
86
87     public InMemoryDOMDataStore(final String name, final LogicalDatastoreType type,
88             final ExecutorService dataChangeListenerExecutor,
89             final int maxDataChangeListenerQueueSize, final boolean debugTransactions) {
90         this.name = Preconditions.checkNotNull(name);
91         this.dataChangeListenerExecutor = Preconditions.checkNotNull(dataChangeListenerExecutor);
92         this.debugTransactions = debugTransactions;
93
94         dataChangeListenerNotificationManager =
95                 new QueuedNotificationManager<>(this.dataChangeListenerExecutor,
96                         DCL_NOTIFICATION_MGR_INVOKER, maxDataChangeListenerQueueSize,
97                         "DataChangeListenerQueueMgr");
98         changePublisher = new InMemoryDOMStoreTreeChangePublisher(this.dataChangeListenerExecutor, maxDataChangeListenerQueueSize);
99
100         switch (type) {
101             case CONFIGURATION:
102                 dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION);
103                 break;
104             case OPERATIONAL:
105                 dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL);
106                 break;
107             default:
108                 throw new IllegalArgumentException("Data store " + type + " not supported");
109         }
110     }
111
112     public void setCloseable(final AutoCloseable closeable) {
113         this.closeable = closeable;
114     }
115
116     public QueuedNotificationManager<?, ?> getDataChangeListenerNotificationManager() {
117         return dataChangeListenerNotificationManager;
118     }
119
120     @Override
121     public final String getIdentifier() {
122         return name;
123     }
124
125     @Override
126     public DOMStoreReadTransaction newReadOnlyTransaction() {
127         return SnapshotBackedTransactions.newReadTransaction(nextIdentifier(), debugTransactions, dataTree.takeSnapshot());
128     }
129
130     @Override
131     public DOMStoreReadWriteTransaction newReadWriteTransaction() {
132         return SnapshotBackedTransactions.newReadWriteTransaction(nextIdentifier(), debugTransactions, dataTree.takeSnapshot(), this);
133     }
134
135     @Override
136     public DOMStoreWriteTransaction newWriteOnlyTransaction() {
137         return SnapshotBackedTransactions.newWriteTransaction(nextIdentifier(), debugTransactions, dataTree.takeSnapshot(), this);
138     }
139
140     @Override
141     public DOMStoreTransactionChain createTransactionChain() {
142         return new DOMStoreTransactionChainImpl(this);
143     }
144
145     @Override
146     public synchronized void onGlobalContextUpdated(final SchemaContext ctx) {
147         dataTree.setSchemaContext(ctx);
148     }
149
150     @Override
151     public void close() {
152         ExecutorServiceUtil.tryGracefulShutdown(dataChangeListenerExecutor, 30, TimeUnit.SECONDS);
153
154         if(closeable != null) {
155             try {
156                 closeable.close();
157             } catch(Exception e) {
158                 LOG.debug("Error closing instance", e);
159             }
160         }
161     }
162
163     public final boolean getDebugTransactions() {
164         return debugTransactions;
165     }
166
167     final DataTreeSnapshot takeSnapshot() {
168         return dataTree.takeSnapshot();
169     }
170
171     @Override
172     public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> ListenerRegistration<L> registerChangeListener(
173             final YangInstanceIdentifier path, final L listener, final DataChangeScope scope) {
174
175         /*
176          * Make sure commit is not occurring right now. Listener has to be
177          * registered and its state capture enqueued at a consistent point.
178          *
179          * FIXME: improve this to read-write lock, such that multiple listener
180          * registrations can occur simultaneously
181          */
182         final DataChangeListenerRegistration<L> reg;
183         synchronized (this) {
184             LOG.debug("{}: Registering data change listener {} for {}", name, listener, path);
185
186             reg = listenerTree.registerDataChangeListener(path, listener, scope);
187
188             Optional<NormalizedNode<?, ?>> currentState = dataTree.takeSnapshot().readNode(path);
189             if (currentState.isPresent()) {
190                 final NormalizedNode<?, ?> data = currentState.get();
191
192                 final DOMImmutableDataChangeEvent event = DOMImmutableDataChangeEvent.builder(DataChangeScope.BASE) //
193                         .setAfter(data) //
194                         .addCreated(path, data) //
195                         .build();
196
197                 dataChangeListenerNotificationManager.submitNotification(reg, event);
198             }
199         }
200
201         return new AbstractListenerRegistration<L>(listener) {
202             @Override
203             protected void removeRegistration() {
204                 synchronized (InMemoryDOMDataStore.this) {
205                     reg.close();
206                 }
207             }
208         };
209     }
210
211     @Override
212     public synchronized <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerTreeChangeListener(final YangInstanceIdentifier treeId, final L listener) {
213         /*
214          * Make sure commit is not occurring right now. Listener has to be
215          * registered and its state capture enqueued at a consistent point.
216          */
217         return changePublisher.registerTreeChangeListener(treeId, listener, dataTree.takeSnapshot());
218     }
219
220     @Override
221     protected void transactionAborted(final SnapshotBackedWriteTransaction<String> tx) {
222         LOG.debug("Tx: {} is closed.", tx.getIdentifier());
223     }
224
225     @Override
226     protected DOMStoreThreePhaseCommitCohort transactionReady(final SnapshotBackedWriteTransaction<String> tx,
227                                                               final DataTreeModification modification,
228                                                               final Exception readyError) {
229         LOG.debug("Tx: {} is submitted. Modifications: {}", tx.getIdentifier(), modification);
230         return new InMemoryDOMStoreThreePhaseCommitCohort(this, tx, modification, readyError);
231     }
232
233     String nextIdentifier() {
234         return name + "-" + txCounter.getAndIncrement();
235     }
236
237     void validate(final DataTreeModification modification) throws DataValidationFailedException {
238         dataTree.validate(modification);
239     }
240
241     DataTreeCandidate prepare(final DataTreeModification modification) {
242         return dataTree.prepare(modification);
243     }
244
245     synchronized void commit(final DataTreeCandidate candidate) {
246         dataTree.commit(candidate);
247         changePublisher.publishChange(candidate);
248         ResolveDataChangeEventsTask.create(candidate, listenerTree).resolve(dataChangeListenerNotificationManager);
249     }
250 }