X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-common-api%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fmd%2Fsal%2Fcommon%2Fapi%2Fdata%2FAsyncWriteTransaction.java;h=9aaa77ca61f1c5f16df1650a696899d90c96e5f5;hb=refs%2Fchanges%2F91%2F9091%2F3;hp=e2734eaddc4845396e3d866dec8d918ac9fe4bca;hpb=80aa861b74f7b0b3574f0962cdb45740ff71946c;p=controller.git diff --git a/opendaylight/md-sal/sal-common-api/src/main/java/org/opendaylight/controller/md/sal/common/api/data/AsyncWriteTransaction.java b/opendaylight/md-sal/sal-common-api/src/main/java/org/opendaylight/controller/md/sal/common/api/data/AsyncWriteTransaction.java index e2734eaddc..9aaa77ca61 100644 --- a/opendaylight/md-sal/sal-common-api/src/main/java/org/opendaylight/controller/md/sal/common/api/data/AsyncWriteTransaction.java +++ b/opendaylight/md-sal/sal-common-api/src/main/java/org/opendaylight/controller/md/sal/common/api/data/AsyncWriteTransaction.java @@ -11,6 +11,7 @@ import org.opendaylight.controller.md.sal.common.api.TransactionStatus; import org.opendaylight.yangtools.concepts.Path; import org.opendaylight.yangtools.yang.common.RpcResult; +import com.google.common.util.concurrent.CheckedFuture; import com.google.common.util.concurrent.ListenableFuture; /** @@ -53,13 +54,19 @@ public interface AsyncWriteTransaction
, D> extends AsyncTransa * {@link TransactionStatus#NEW} or {@link TransactionStatus#SUBMITED} * * Invoking cancel() on {@link TransactionStatus#FAILED} or - * {@link TransactionStatus#CANCELED} will have no effect. + * {@link TransactionStatus#CANCELED} will have no effect, and transaction + * is considered cancelled. * - * @throws IllegalStateException - * If transaction status is {@link TransactionStatus#COMMITED} + * Invoking cancel() on finished transaction (future returned by {@link #commit()} + * already completed with {@link TransactionStatus#COMMITED}) will always + * fail (return false). + * + * @return false if the task could not be cancelled, + * typically because it has already completed normally; + * true otherwise * */ - public void cancel(); + boolean cancel(); /** * Store a piece of data at specified path. This acts as an add / replace @@ -91,7 +98,7 @@ public interface AsyncWriteTransaction
, D> extends AsyncTransa * @throws IllegalStateException * if the transaction is no longer {@link TransactionStatus#NEW} */ - public void put(LogicalDatastoreType store, P path, D data); + void put(LogicalDatastoreType store, P path, D data); /** * Store a piece of data at the specified path. This acts as a merge operation, @@ -126,7 +133,7 @@ public interface AsyncWriteTransaction
, D> extends AsyncTransa * @throws IllegalStateException * if the transaction is no longer {@link TransactionStatus#NEW} */ - public void merge(LogicalDatastoreType store, P path, D data); + void merge(LogicalDatastoreType store, P path, D data); /** * Remove a piece of data from specified path. This operation does not fail @@ -139,24 +146,14 @@ public interface AsyncWriteTransaction
, D> extends AsyncTransa * @throws IllegalStateException * if the transaction is no longer {@link TransactionStatus#NEW} */ - public void delete(LogicalDatastoreType store, P path); + void delete(LogicalDatastoreType store, P path); /** - * - * Closes transaction and resources allocated to the transaction. - * - * This call does not change Transaction status. Client SHOULD explicitly - * {@link #commit()} or {@link #cancel()} transaction. - * - * @throws IllegalStateException - * if the transaction has not been updated by invoking - * {@link #commit()} or {@link #cancel()}. - */ - @Override - public void close(); - - /** - * Submits transaction to be applied to update logical data tree. + * Submits this transaction to be asynchronously applied to update the logical data tree. + * The returned CheckedFuture conveys the result of applying the data changes. + *
+ * Note: It is strongly recommended to process the CheckedFuture result in an asynchronous + * manner rather than using the blocking get() method. See example usage below. *
* This call logically seals the transaction, which prevents the client from * further changing data tree using this transaction. Any subsequent calls to @@ -166,31 +163,65 @@ public interface AsyncWriteTransaction
, D> extends AsyncTransa * {@link IllegalStateException}. * * The transaction is marked as {@link TransactionStatus#SUBMITED} and - * enqueued into the data store backed for processing. + * enqueued into the data store back-end for processing. * *
* Whether or not the commit is successful is determined by versioning - * of data tree and validation of registered commit participants - * {@link AsyncConfigurationCommitHandler} - * if transaction changes {@link LogicalDatastoreType#CONFIGURATION} data tree. - *
- * The effects of successful commit of data depends on - * other data change listeners {@link AsyncDataChangeListener} and - * {@link AsyncConfigurationCommitHandler}, which was registered to the - * same {@link AsyncDataBroker}, to which this transaction belongs. - * + * of the data tree and validation of registered commit participants + * ({@link AsyncConfigurationCommitHandler}) + * if the transaction changes the data tree. + *
+ * The effects of a successful commit of data depends on data change listeners + * ({@link AsyncDataChangeListener}) and commit participants + * ({@link AsyncConfigurationCommitHandler}) that are registered with the data broker. + *
+ *
+ * private void doWrite( final int tries ) { + * WriteTransaction writeTx = dataBroker.newWriteOnlyTransaction(); + * + * MyDataObject data = ...; + * InstanceIdentifier*path = ...; + * writeTx.put( LogicalDatastoreType.OPERATIONAL, path, data ); + * + * Futures.addCallback( writeTx.commit(), new FutureCallback () { + * public void onSuccess( Void result ) { + * // succeeded + * } + * + * public void onFailure( Throwable t ) { + * if( t instanceof OptimisticLockFailedException ) { + * if( ( tries - 1 ) > 0 ) { + * // do retry + * doWrite( tries - 1 ); + * } else { + * // out of retries + * } + * } else { + * // failed due to another type of TransactionCommitFailedException. + * } + * } ); + * } + * ... + * doWrite( 2 ); + *
* Transaction may fail because of multiple reasons, such as *
, D> extends AsyncTransa
* with {@link OptimisticLockFailedException} exception, which indicates to
* client that concurrent transaction prevented the submitted transaction from being
* applied.
- *
- * @return Result of the Commit, containing success information or list of
- * encountered errors, if commit was not successful. The Future
- * blocks until {@link TransactionStatus#COMMITED} is reached.
- * Future will fail with {@link TransactionCommitFailedException} if
- * Commit of this transaction failed. TODO: Usability: Consider
- * change from ListenableFuture to
- * {@link com.google.common.util.concurrent.CheckedFuture} which
- * will throw {@link TransactionCommitFailedException}.
+ *
+ * @return a CheckFuture containing the result of the commit. The Future blocks until the
+ * commit operation is complete. A successful commit returns nothing. On failure,
+ * the Future will fail with a {@link TransactionCommitFailedException} or an exception
+ * derived from TransactionCommitFailedException.
*
* @throws IllegalStateException
* if the transaction is not {@link TransactionStatus#NEW}
*/
- public ListenableFuture