Merge "Fix for possible NPE if Bundle is stopped."
[controller.git] / opendaylight / md-sal / sal-common-api / src / main / java / org / opendaylight / controller / md / sal / common / api / data / TransactionChain.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.common.api.data;
9
10 import org.opendaylight.yangtools.concepts.Path;
11
12 /**
13  * A chain of transactions. Transactions in a chain need to be committed in
14  * sequence and each transaction should see the effects of previous committed transactions
15  * as they occurred. A chain makes no guarantees of atomicity across the chained transactions -
16  * the transactions are committed as soon as possible in the order that they were submitted.
17  *
18  * This behaviour is different from the default AsyncDataBroker, where a
19  * transaction is always created from the current global state, not taking into
20  * account any transactions previously committed by the calling thread. Due to
21  * the asynchronous nature of transaction submission this can lead to surprising
22  * results. If a thread executes the following sequence sufficiently quickly:
23  *
24  * AsyncWriteTransaction t1 = broker.newWriteOnlyTransaction();
25  * t1.put(id, data);
26  * t1.submit();
27  *
28  * AsyncReadTransaction t2 = broker.newReadOnlyTransaction();
29  * Optional<?> maybeData = t2.read(id).get();
30  *
31  * it may happen, that it sees maybeData.isPresent() == false, simply because
32  * t1 has not completed the processes of being applied and t2 is actually
33  * allocated from the previous state. This is obviously bad for users who create
34  * incremental state in the datastore and actually read what they write in
35  * subsequent transactions.
36  *
37  * Using a TransactionChain instead of a broker solves this particular problem,
38  * and leads to expected behavior: t2 will always see the data written in t1
39  * present.
40  */
41 public interface TransactionChain<P extends Path<P>, D> extends AutoCloseable,
42         AsyncDataTransactionFactory<P, D> {
43
44     /**
45      * Create a new read only transaction which will continue the chain.
46      *
47      * <p>
48      * The previous write transaction has to be either SUBMITTED
49      * ({@link AsyncWriteTransaction#submit submit} was invoked) or CANCELLED
50      * ({@link #close close} was invoked).
51      * <p>
52      * The returned read-only transaction presents an isolated view of the data if the previous
53      * write transaction was successful - in other words, this read-only transaction will see the
54      * state changes made by the previous write transaction in the chain. However, state which
55      * was introduced by other transactions outside this transaction chain after creation of
56      * the previous transaction is not visible.
57      *
58      * @return New transaction in the chain.
59      * @throws IllegalStateException
60      *             if the previous transaction was not SUBMITTED or CANCELLED.
61      * @throws TransactionChainClosedException
62      *             if the chain has been closed.
63      */
64     @Override
65     public AsyncReadOnlyTransaction<P, D> newReadOnlyTransaction();
66
67     /**
68      * Create a new read-write transaction which will continue the chain.
69      *
70      * <p>
71      * The previous write transaction has to be either SUBMITTED
72      * ({@link AsyncWriteTransaction#submit submit} was invoked) or CANCELLED
73      * ({@link #close close} was invoked).
74      * <p>
75      * The returned read-write transaction presents an isolated view of the data if the previous
76      * write transaction was successful - in other words, this read-write transaction will see the
77      * state changes made by the previous write transaction in the chain. However, state which
78      * was introduced by other transactions outside this transaction chain after creation of
79      * the previous transaction is not visible.
80      * <p>
81      * Committing this read-write transaction using {@link AsyncWriteTransaction#submit submit}
82      * will submit the state changes in this transaction to be visible to any subsequent
83      * transaction in this chain and also to any transaction outside this chain.
84      *
85      * @return New transaction in the chain.
86      * @throws IllegalStateException
87      *             if the previous transaction was not SUBMITTED or CANCELLED.
88      * @throws TransactionChainClosedException
89      *             if the chain has been closed.
90      */
91     @Override
92     public AsyncReadWriteTransaction<P, D> newReadWriteTransaction();
93
94     /**
95      * Create a new write-only transaction which will continue the chain.
96      *
97      * <p>
98      * The previous write transaction has to be either SUBMITTED
99      * ({@link AsyncWriteTransaction#submit submit} was invoked) or CANCELLED
100      * ({@link #close close} was invoked).
101      * <p>
102      * The returned write-only transaction presents an isolated view of the data if the previous
103      * write transaction was successful - in other words, this write-only transaction will see the
104      * state changes made by the previous write transaction in the chain. However, state which
105      * was introduced by other transactions outside this transaction chain after creation of
106      * the previous transaction is not visible.
107      * <p>
108      * Committing this write-only transaction using {@link AsyncWriteTransaction#submit submit}
109      * will submit the state changes in this transaction to be visible to any subsequent
110      * transaction in this chain and also to any transaction outside this chain.
111      *
112      * @return New transaction in the chain.
113      * @throws IllegalStateException
114      *             if the previous transaction was not SUBMITTED or CANCELLED.
115      * @throws TransactionChainClosedException
116      *             if the chain has been closed.
117      */
118     @Override
119     public AsyncWriteTransaction<P, D> newWriteOnlyTransaction();
120
121     @Override
122     void close();
123 }