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.dom.api;
10 import com.google.common.util.concurrent.FluentFuture;
11 import javax.annotation.CheckReturnValue;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.mdsal.common.api.CommitInfo;
14 import org.opendaylight.mdsal.common.api.DataValidationFailedException;
15 import org.opendaylight.mdsal.common.api.OptimisticLockFailedException;
16 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
19 * Write transaction provides mutation capabilities for a data tree.
22 * Initial state of write transaction is a stable snapshot of the current data tree.
23 * The state is captured when the transaction is created and its state and underlying
24 * data tree are not affected by other concurrently running transactions.
27 * Write transactions are isolated from other concurrent write transactions. All
28 * writes are local to the transaction and represent only a proposal of state
29 * change for the data tree and it is not visible to any other concurrently running
33 * Applications make changes to the local data tree in the transaction by via the
34 * <b>put</b>, <b>merge</b>, and <b>delete</b> operations.
36 * <h2>Put operation</h2>
37 * Stores a piece of data at a specified path. This acts as an add / replace
38 * operation, which is to say that whole subtree will be replaced by the
42 * Performing the following put operations:
45 * 1) container { list [ a ] }
46 * 2) container { list [ b ] }
48 * will result in the following data being present:
51 * container { list [ b ] }
53 * <h2>Merge operation</h2>
54 * Merges a piece of data with the existing data at a specified path. Any pre-existing data
55 * which is not explicitly overwritten will be preserved. This means that if you store a container,
56 * its child lists will be merged.
59 * Performing the following merge operations:
62 * 1) container { list [ a ] }
63 * 2) container { list [ b ] }
65 * will result in the following data being present:
68 * container { list [ a, b ] }
70 * This also means that storing the container will preserve any
71 * augmentations which have been attached to it.
73 * <h2>Delete operation</h2>
74 * Removes a piece of data from a specified path.
77 * After applying changes to the local data tree, applications publish the changes proposed in the
78 * transaction by calling {@link #commit} on the transaction. This seals the transaction
79 * (preventing any further writes using this transaction) and commits it to be
80 * processed and applied to global conceptual data tree.
83 * The transaction commit may fail due to a concurrent transaction modifying and committing data in
84 * an incompatible way. See {@link #commit} for more concrete commit failure examples.
87 * <b>Implementation Note:</b> This interface is not intended to be implemented
88 * by users of MD-SAL, but only to be consumed by them.
90 public interface DOMDataTreeWriteTransaction extends DOMDataTreeTransaction, DOMDataTreeWriteOperations {
92 * Commits this transaction to be asynchronously applied to update the logical data tree. The returned
93 * {@link FluentFuture} conveys the result of applying the data changes.
96 * This call logically seals the transaction, which prevents the client from further changing the data tree using
97 * this transaction. Any subsequent calls to <code>put(LogicalDatastoreType, Path, Object)</code>,
98 * <code>merge(LogicalDatastoreType, Path, Object)</code>, <code>delete(LogicalDatastoreType, Path)</code> will fail
99 * with {@link IllegalStateException}. The transaction is marked as committed and enqueued into the data store
100 * back-end for processing.
103 * Whether or not the commit is successful is determined by versioning of the data tree and validation of registered
104 * commit participants if the transaction changes the data tree.
107 * The effects of a successful commit of data depends on listeners and commit participants that are registered with
110 * <h3>Example usage:</h3>
112 * private void doWrite(final int tries) {
113 * WriteTransaction writeTx = dataBroker.newWriteOnlyTransaction();
114 * MyDataObject data = ...;
115 * InstanceIdentifier<MyDataObject> path = ...;
116 * writeTx.put(LogicalDatastoreType.OPERATIONAL, path, data);
117 * Futures.addCallback(writeTx.commit(), new FutureCallback<CommitInfo>() {
118 * public void onSuccess(CommitInfo result) {
121 * public void onFailure(Throwable t) {
122 * if (t instanceof OptimisticLockFailedException) {
123 * if(( tries - 1) > 0 ) {
125 * doWrite(tries - 1);
130 * // failed due to another type of TransactionCommitFailedException.
138 * <h2>Failure scenarios</h2>
141 * Transaction may fail because of multiple reasons, such as
144 * Another transaction finished earlier and modified the same node in a non-compatible way (see below). In this
145 * case the returned future will fail with an {@link OptimisticLockFailedException}. It is the responsibility
146 * of the caller to create a new transaction and commit the same modification again in order to update data
149 * <b>Warning</b>: In most cases, retrying after an OptimisticLockFailedException will result in a high
150 * probability of success. However, there are scenarios, albeit unusual, where any number of retries will
151 * not succeed. Therefore it is strongly recommended to limit the number of retries (2 or 3) to avoid
155 * <li>Data change introduced by this transaction did not pass validation by commit handlers or data was
156 * incorrectly structured. Returned future will fail with a {@link DataValidationFailedException}. User
157 * should not retry to create new transaction with same data, since it probably will fail again.
161 * <h3>Change compatibility</h3>
162 * There are several sets of changes which could be considered incompatible between two transactions which are
163 * derived from same initial state. Rules for conflict detection applies recursively for each subtree level.
165 * <h4>Change compatibility of leafs, leaf-list items</h4>
166 * Following table shows state changes and failures between two concurrent transactions, which are based on same
167 * initial state, Tx 1 completes successfully before Tx 2 is committed.
169 * <table summary="Change compatibility of leaf values">
171 * <th>Initial state</th>
180 * <td>Tx 2 will fail, state is A=1</td>
185 * <td>merge(A,2)</td>
191 * <td>merge(A,1)</td>
193 * <td>Tx 2 will fail, state is A=1</td>
197 * <td>merge(A,1)</td>
198 * <td>merge(A,2)</td>
207 * <td>Tx 2 will fail, A=1</td>
212 * <td>merge(A,2)</td>
217 * <td>merge(A,1)</td>
219 * <td>Tx 2 will fail, A=1</td>
223 * <td>merge(A,1)</td>
224 * <td>merge(A,2)</td>
232 * <td>Tx 2 will fail, A does not exists</td>
237 * <td>merge(A,2)</td>
242 * <h4>Change compatibility of subtrees</h4>
243 * Following table shows state changes and failures between two concurrent transactions, which are based on same
244 * initial state, Tx 1 completes successfully before Tx 2 is committed.
246 * <table summary="Change compatibility of containers">
248 * <th>Initial state</th>
256 * <td>put(TOP,[])</td>
257 * <td>put(TOP,[])</td>
258 * <td>Tx 2 will fail, state is TOP=[]</td>
262 * <td>put(TOP,[])</td>
263 * <td>merge(TOP,[])</td>
269 * <td>put(TOP,[FOO=1])</td>
270 * <td>put(TOP,[BAR=1])</td>
271 * <td>Tx 2 will fail, state is TOP=[FOO=1]</td>
275 * <td>put(TOP,[FOO=1])</td>
276 * <td>merge(TOP,[BAR=1])</td>
277 * <td>TOP=[FOO=1,BAR=1]</td>
282 * <td>merge(TOP,[FOO=1])</td>
283 * <td>put(TOP,[BAR=1])</td>
284 * <td>Tx 2 will fail, state is TOP=[FOO=1]</td>
288 * <td>merge(TOP,[FOO=1])</td>
289 * <td>merge(TOP,[BAR=1])</td>
290 * <td>TOP=[FOO=1,BAR=1]</td>
295 * <td>put(TOP,[FOO=1])</td>
296 * <td>put(TOP,[BAR=1])</td>
297 * <td>Tx 2 will fail, state is TOP=[FOO=1]</td>
301 * <td>put(TOP,[FOO=1])</td>
302 * <td>merge(TOP,[BAR=1])</td>
303 * <td>state is TOP=[FOO=1,BAR=1]</td>
307 * <td>merge(TOP,[FOO=1])</td>
308 * <td>put(TOP,[BAR=1])</td>
309 * <td>Tx 2 will fail, state is TOP=[FOO=1]</td>
313 * <td>merge(TOP,[FOO=1])</td>
314 * <td>merge(TOP,[BAR=1])</td>
315 * <td>state is TOP=[FOO=1,BAR=1]</td>
319 * <td>delete(TOP)</td>
320 * <td>put(TOP,[BAR=1])</td>
321 * <td>Tx 2 will fail, state is empty store</td>
325 * <td>delete(TOP)</td>
326 * <td>merge(TOP,[BAR=1])</td>
327 * <td>state is TOP=[BAR=1]</td>
332 * <td>put(TOP/FOO,1)</td>
333 * <td>put(TOP/BAR,1])</td>
334 * <td>state is TOP=[FOO=1,BAR=1]</td>
338 * <td>put(TOP/FOO,1)</td>
339 * <td>merge(TOP/BAR,1)</td>
340 * <td>state is TOP=[FOO=1,BAR=1]</td>
344 * <td>merge(TOP/FOO,1)</td>
345 * <td>put(TOP/BAR,1)</td>
346 * <td>state is TOP=[FOO=1,BAR=1]</td>
350 * <td>merge(TOP/FOO,1)</td>
351 * <td>merge(TOP/BAR,1)</td>
352 * <td>state is TOP=[FOO=1,BAR=1]</td>
356 * <td>delete(TOP)</td>
357 * <td>put(TOP/BAR,1)</td>
358 * <td>Tx 2 will fail, state is empty store</td>
362 * <td>delete(TOP)</td>
363 * <td>merge(TOP/BAR,1]</td>
364 * <td>Tx 2 will fail, state is empty store</td>
368 * <td>TOP=[FOO=1]</td>
369 * <td>put(TOP/FOO,2)</td>
370 * <td>put(TOP/BAR,1)</td>
371 * <td>state is TOP=[FOO=2,BAR=1]</td>
374 * <td>TOP=[FOO=1]</td>
375 * <td>put(TOP/FOO,2)</td>
376 * <td>merge(TOP/BAR,1)</td>
377 * <td>state is TOP=[FOO=2,BAR=1]</td>
380 * <td>TOP=[FOO=1]</td>
381 * <td>merge(TOP/FOO,2)</td>
382 * <td>put(TOP/BAR,1)</td>
383 * <td>state is TOP=[FOO=2,BAR=1]</td>
386 * <td>TOP=[FOO=1]</td>
387 * <td>merge(TOP/FOO,2)</td>
388 * <td>merge(TOP/BAR,1)</td>
389 * <td>state is TOP=[FOO=2,BAR=1]</td>
392 * <td>TOP=[FOO=1]</td>
393 * <td>delete(TOP/FOO)</td>
394 * <td>put(TOP/BAR,1)</td>
395 * <td>state is TOP=[BAR=1]</td>
398 * <td>TOP=[FOO=1]</td>
399 * <td>delete(TOP/FOO)</td>
400 * <td>merge(TOP/BAR,1]</td>
401 * <td>state is TOP=[BAR=1]</td>
406 * <h3>Examples of failure scenarios</h3>
408 * <h4>Conflict of two transactions</h4>
409 * This example illustrates two concurrent transactions, which derived from same initial state
410 * of data tree and proposes conflicting modifications.
413 * txA = broker.newWriteTransaction(); // allocates new transaction, data tree is empty
414 * txB = broker.newWriteTransaction(); // allocates new transaction, data tree is empty
415 * txA.put(CONFIGURATION, PATH, A); // writes to PATH value A
416 * txB.put(CONFIGURATION, PATH, B) // writes to PATH value B
417 * ListenableFuture futureA = txA.commit(); // transaction A is sealed and committed
418 * ListenebleFuture futureB = txB.commit(); // transaction B is sealed and committed
420 * Commit of transaction A will be processed asynchronously and data tree will be updated to
421 * contain value <code>A</code> for <code>PATH</code>. Returned {@link FluentFuture} will
422 * successfully complete once state is applied to data tree.
423 * Commit of Transaction B will fail, because previous transaction also modified path in a
424 * concurrent way. The state introduced by transaction B will not be applied. Returned
425 * {@link FluentFuture} object will fail with {@link OptimisticLockFailedException}
426 * exception, which indicates to client that concurrent transaction prevented the committed
427 * transaction from being applied. <br>
430 * A successful commit produces implementation-specific {@link CommitInfo} structure, which is used to communicate
431 * post-condition information to the caller. Such information can contain commit-id, timing information or any
432 * other information the implementation wishes to share.
434 * @return a FluentFuture containing the result of the commit information. The Future blocks until the commit
435 * operation is complete. A successful commit returns nothing. On failure, the Future will fail with a
436 * {@link TransactionCommitFailedException} or an exception derived from TransactionCommitFailedException.
437 * @throws IllegalStateException if the transaction is already committed or was canceled.
440 @NonNull FluentFuture<? extends @NonNull CommitInfo> commit();
443 * Cancels the transaction. Transactions can only be cancelled if it was not yet committed.
444 * Invoking cancel() on failed or already canceled will have no effect, and transaction is considered cancelled.
445 * Invoking cancel() on finished transaction (future returned by {@link #commit()} already successfully completed)
446 * will always fail (return false).
448 * @return <tt>false</tt> if the task could not be cancelled, typically because it has already completed normally;
449 * <tt>true</tt> otherwise