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.mdsal.common.api;
10 import com.google.common.util.concurrent.FluentFuture;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import javax.annotation.CheckReturnValue;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.concepts.Path;
17 * Write transaction provides mutation capabilities for a data tree.
20 * Initial state of write transaction is a stable snapshot of the current data tree.
21 * The state is captured when the transaction is created and its state and underlying
22 * data tree are not affected by other concurrently running transactions.
25 * Write transactions are isolated from other concurrent write transactions. All
26 * writes are local to the transaction and represent only a proposal of state
27 * change for the data tree and it is not visible to any other concurrently running
31 * Applications make changes to the local data tree in the transaction by via the
32 * <b>put</b>, <b>merge</b>, and <b>delete</b> operations.
34 * <h2>Put operation</h2>
35 * Stores a piece of data at a specified path. This acts as an add / replace
36 * operation, which is to say that whole subtree will be replaced by the
40 * Performing the following put operations:
43 * 1) container { list [ a ] }
44 * 2) container { list [ b ] }
46 * will result in the following data being present:
49 * container { list [ b ] }
51 * <h2>Merge operation</h2>
52 * Merges a piece of data with the existing data at a specified path. Any pre-existing data
53 * which is not explicitly overwritten will be preserved. This means that if you store a container,
54 * its child lists will be merged.
57 * Performing the following merge operations:
60 * 1) container { list [ a ] }
61 * 2) container { list [ b ] }
63 * will result in the following data being present:
66 * container { list [ a, b ] }
68 * This also means that storing the container will preserve any
69 * augmentations which have been attached to it.
71 * <h2>Delete operation</h2>
72 * Removes a piece of data from a specified path.
75 * After applying changes to the local data tree, applications publish the changes proposed in the
76 * transaction by calling {@link #commit} on the transaction. This seals the transaction
77 * (preventing any further writes using this transaction) and commits it to be
78 * processed and applied to global conceptual data tree.
81 * The transaction commit may fail due to a concurrent transaction modifying and committing data in
82 * an incompatible way. See {@link #commit} for more concrete commit failure examples.
85 * <b>Implementation Note:</b> This interface is not intended to be implemented
86 * by users of MD-SAL, but only to be consumed by them.
89 * Type of path (subtree identifier), which represents location in
92 * Type of data (payload), which represents data payload
94 public interface AsyncWriteTransaction<P extends Path<P>, D> extends AsyncTransaction<P, D> {
96 * Cancels the transaction.
97 * Transactions can only be cancelled if it was not yet committed.
98 * Invoking cancel() on failed or already canceled will have no effect, and transaction is
99 * considered cancelled.
100 * Invoking cancel() on finished transaction (future returned by {@link #commit()} already
101 * successfully completed) will always fail (return false).
103 * @return <tt>false</tt> if the task could not be cancelled, typically because it has already
104 * completed normally; <tt>true</tt> otherwise
110 * Removes a piece of data from specified path. This operation does not fail if the specified
111 * path does not exist.
113 * @param store Logical data store which should be modified
114 * @param path Data object path
115 * @throws IllegalStateException if the transaction was committed or canceled.
117 void delete(LogicalDatastoreType store, P path);
120 * Commits this transaction to be asynchronously applied to update the logical data tree. The returned
121 * {@link FluentFuture} conveys the result of applying the data changes.
124 * This call logically seals the transaction, which prevents the client from further changing the data tree using
125 * this transaction. Any subsequent calls to <code>put(LogicalDatastoreType, Path, Object)</code>,
126 * <code>merge(LogicalDatastoreType, Path, Object)</code>, <code>delete(LogicalDatastoreType, Path)</code> will fail
127 * with {@link IllegalStateException}. The transaction is marked as committed and enqueued into the data store
128 * back-end for processing.
131 * Whether or not the commit is successful is determined by versioning of the data tree and validation of registered
132 * commit participants if the transaction changes the data tree.
135 * The effects of a successful commit of data depends on listeners and commit participants that are registered with
139 * <h3>Example usage:</h3>
141 * private void doWrite(final int tries) {
142 * WriteTransaction writeTx = dataBroker.newWriteOnlyTransaction();
143 * MyDataObject data = ...;
144 * InstanceIdentifier<MyDataObject> path = ...;
145 * writeTx.put(LogicalDatastoreType.OPERATIONAL, path, data);
146 * Futures.addCallback(writeTx.commit(), new FutureCallback<CommitInfo>() {
147 * public void onSuccess(CommitInfo result) {
150 * public void onFailure(Throwable t) {
151 * if(t instanceof OptimisticLockFailedException) {
152 * if(( tries - 1) > 0 ) {
154 * doWrite(tries - 1);
159 * // failed due to another type of TransactionCommitFailedException.
167 * <h2>Failure scenarios</h2>
170 * Transaction may fail because of multiple reasons, such as
173 * Another transaction finished earlier and modified the same node in a non-compatible way (see below). In this
174 * case the returned future will fail with an {@link OptimisticLockFailedException}. It is the responsibility
175 * of the caller to create a new transaction and commit the same modification again in order to update data
178 * <b>Warning</b>: In most cases, retrying after an OptimisticLockFailedException will result in a high
179 * probability of success. However, there are scenarios, albeit unusual, where any number of retries will
180 * not succeed. Therefore it is strongly recommended to limit the number of retries (2 or 3) to avoid
184 * <li>Data change introduced by this transaction did not pass validation by commit handlers or data was
185 * incorrectly structured. Returned future will fail with a {@link DataValidationFailedException}. User
186 * should not retry to create new transaction with same data, since it probably will fail again.
190 * <h3>Change compatibility</h3>
191 * There are several sets of changes which could be considered incompatible between two transactions which are
192 * derived from same initial state. Rules for conflict detection applies recursively for each subtree level.
194 * <h4>Change compatibility of leafs, leaf-list items</h4>
195 * Following table shows state changes and failures between two concurrent transactions, which are based on same
196 * initial state, Tx 1 completes successfully before Tx 2 is committed.
198 * <table summary="Change compatibility of leaf values">
200 * <th>Initial state</th>
209 * <td>Tx 2 will fail, state is A=1</td>
214 * <td>merge(A,2)</td>
220 * <td>merge(A,1)</td>
222 * <td>Tx 2 will fail, state is A=1</td>
226 * <td>merge(A,1)</td>
227 * <td>merge(A,2)</td>
236 * <td>Tx 2 will fail, A=1</td>
241 * <td>merge(A,2)</td>
246 * <td>merge(A,1)</td>
248 * <td>Tx 2 will fail, A=1</td>
252 * <td>merge(A,1)</td>
253 * <td>merge(A,2)</td>
261 * <td>Tx 2 will fail, A does not exists</td>
266 * <td>merge(A,2)</td>
271 * <h4>Change compatibility of subtrees</h4>
272 * Following table shows state changes and failures between two concurrent transactions, which are based on same
273 * initial state, Tx 1 completes successfully before Tx 2 is committed.
275 * <table summary="Change compatibility of containers">
277 * <th>Initial state</th>
285 * <td>put(TOP,[])</td>
286 * <td>put(TOP,[])</td>
287 * <td>Tx 2 will fail, state is TOP=[]</td>
291 * <td>put(TOP,[])</td>
292 * <td>merge(TOP,[])</td>
298 * <td>put(TOP,[FOO=1])</td>
299 * <td>put(TOP,[BAR=1])</td>
300 * <td>Tx 2 will fail, state is TOP=[FOO=1]</td>
304 * <td>put(TOP,[FOO=1])</td>
305 * <td>merge(TOP,[BAR=1])</td>
306 * <td>TOP=[FOO=1,BAR=1]</td>
311 * <td>merge(TOP,[FOO=1])</td>
312 * <td>put(TOP,[BAR=1])</td>
313 * <td>Tx 2 will fail, state is TOP=[FOO=1]</td>
317 * <td>merge(TOP,[FOO=1])</td>
318 * <td>merge(TOP,[BAR=1])</td>
319 * <td>TOP=[FOO=1,BAR=1]</td>
324 * <td>put(TOP,[FOO=1])</td>
325 * <td>put(TOP,[BAR=1])</td>
326 * <td>Tx 2 will fail, state is TOP=[FOO=1]</td>
330 * <td>put(TOP,[FOO=1])</td>
331 * <td>merge(TOP,[BAR=1])</td>
332 * <td>state is TOP=[FOO=1,BAR=1]</td>
336 * <td>merge(TOP,[FOO=1])</td>
337 * <td>put(TOP,[BAR=1])</td>
338 * <td>Tx 2 will fail, state is TOP=[FOO=1]</td>
342 * <td>merge(TOP,[FOO=1])</td>
343 * <td>merge(TOP,[BAR=1])</td>
344 * <td>state is TOP=[FOO=1,BAR=1]</td>
348 * <td>delete(TOP)</td>
349 * <td>put(TOP,[BAR=1])</td>
350 * <td>Tx 2 will fail, state is empty store</td>
354 * <td>delete(TOP)</td>
355 * <td>merge(TOP,[BAR=1])</td>
356 * <td>state is TOP=[BAR=1]</td>
361 * <td>put(TOP/FOO,1)</td>
362 * <td>put(TOP/BAR,1])</td>
363 * <td>state is TOP=[FOO=1,BAR=1]</td>
367 * <td>put(TOP/FOO,1)</td>
368 * <td>merge(TOP/BAR,1)</td>
369 * <td>state is TOP=[FOO=1,BAR=1]</td>
373 * <td>merge(TOP/FOO,1)</td>
374 * <td>put(TOP/BAR,1)</td>
375 * <td>state is TOP=[FOO=1,BAR=1]</td>
379 * <td>merge(TOP/FOO,1)</td>
380 * <td>merge(TOP/BAR,1)</td>
381 * <td>state is TOP=[FOO=1,BAR=1]</td>
385 * <td>delete(TOP)</td>
386 * <td>put(TOP/BAR,1)</td>
387 * <td>Tx 2 will fail, state is empty store</td>
391 * <td>delete(TOP)</td>
392 * <td>merge(TOP/BAR,1]</td>
393 * <td>Tx 2 will fail, state is empty store</td>
397 * <td>TOP=[FOO=1]</td>
398 * <td>put(TOP/FOO,2)</td>
399 * <td>put(TOP/BAR,1)</td>
400 * <td>state is TOP=[FOO=2,BAR=1]</td>
403 * <td>TOP=[FOO=1]</td>
404 * <td>put(TOP/FOO,2)</td>
405 * <td>merge(TOP/BAR,1)</td>
406 * <td>state is TOP=[FOO=2,BAR=1]</td>
409 * <td>TOP=[FOO=1]</td>
410 * <td>merge(TOP/FOO,2)</td>
411 * <td>put(TOP/BAR,1)</td>
412 * <td>state is TOP=[FOO=2,BAR=1]</td>
415 * <td>TOP=[FOO=1]</td>
416 * <td>merge(TOP/FOO,2)</td>
417 * <td>merge(TOP/BAR,1)</td>
418 * <td>state is TOP=[FOO=2,BAR=1]</td>
421 * <td>TOP=[FOO=1]</td>
422 * <td>delete(TOP/FOO)</td>
423 * <td>put(TOP/BAR,1)</td>
424 * <td>state is TOP=[BAR=1]</td>
427 * <td>TOP=[FOO=1]</td>
428 * <td>delete(TOP/FOO)</td>
429 * <td>merge(TOP/BAR,1]</td>
430 * <td>state is TOP=[BAR=1]</td>
435 * <h3>Examples of failure scenarios</h3>
437 * <h4>Conflict of two transactions</h4>
438 * This example illustrates two concurrent transactions, which derived from same initial state
439 * of data tree and proposes conflicting modifications.
442 * txA = broker.newWriteTransaction(); // allocates new transaction, data tree is empty
443 * txB = broker.newWriteTransaction(); // allocates new transaction, data tree is empty
444 * txA.put(CONFIGURATION, PATH, A); // writes to PATH value A
445 * txB.put(CONFIGURATION, PATH, B) // writes to PATH value B
446 * ListenableFuture futureA = txA.commit(); // transaction A is sealed and committed
447 * ListenebleFuture futureB = txB.commit(); // transaction B is sealed and committed
449 * Commit of transaction A will be processed asynchronously and data tree will be updated to
450 * contain value <code>A</code> for <code>PATH</code>. Returned {@link ListenableFuture} will
451 * successfully complete once state is applied to data tree.
452 * Commit of Transaction B will fail, because previous transaction also modified path in a
453 * concurrent way. The state introduced by transaction B will not be applied. Returned
454 * {@link ListenableFuture} object will fail with {@link OptimisticLockFailedException}
455 * exception, which indicates to client that concurrent transaction prevented the committed
456 * transaction from being applied. <br>
459 * A successful commit produces implementation-specific {@link CommitInfo} structure, which is used to communicate
460 * post-condition information to the caller. Such information can contain commit-id, timing information or any
461 * other information the implementation wishes to share.
463 * @return a FluentFuture containing the result of the commit information. The Future blocks until the commit
464 * operation is complete. A successful commit returns nothing. On failure, the Future will fail with a
465 * {@link TransactionCommitFailedException} or an exception derived from TransactionCommitFailedException.
466 * @throws IllegalStateException if the transaction is already committed or was canceled.
469 @NonNull FluentFuture<? extends @NonNull CommitInfo> commit();