Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import java.util.List; |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 19 | import java.util.concurrent.CompletableFuture; |
| 20 | import java.util.concurrent.atomic.AtomicBoolean; |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 21 | |
| 22 | import org.onosproject.store.primitives.TransactionId; |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 23 | import org.onosproject.store.service.TransactionLog; |
| 24 | import org.onosproject.store.service.Transactional; |
| 25 | import org.onosproject.store.service.Version; |
| 26 | import org.onosproject.store.service.TransactionContext; |
| 27 | import org.onosproject.store.service.TransactionException; |
Jordan Halterman | 03b8318 | 2017-05-09 12:11:22 -0700 | [diff] [blame] | 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 30 | |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 31 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 32 | import static com.google.common.base.Preconditions.checkState; |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 33 | |
| 34 | /** |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 35 | * Manages a transaction within the context of a single primitive. |
| 36 | * <p> |
| 37 | * The {@code Transaction} object is used to manage the transaction for a single partition primitive that implements |
| 38 | * the {@link Transactional} interface. It's used as a proxy for {@link TransactionContext}s to manage the transaction |
| 39 | * as it relates to a single piece of atomic state. |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 40 | */ |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 41 | public class Transaction<T> { |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 42 | |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Transaction state. |
| 45 | * <p> |
| 46 | * The transaction state is used to indicate the phase within which the transaction is currently running. |
| 47 | */ |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 48 | enum State { |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 49 | |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 50 | /** |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 51 | * Active transaction state. |
| 52 | * <p> |
| 53 | * The {@code ACTIVE} state represents a transaction in progress. Active transactions may or may not affect |
| 54 | * concurrently running transactions depending on the transaction's isolation level. |
| 55 | */ |
| 56 | ACTIVE, |
| 57 | |
| 58 | /** |
| 59 | * Preparing transaction state. |
| 60 | * <p> |
| 61 | * Once a transaction commitment begins, it enters the {@code PREPARING} phase of the two-phase commit protocol. |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 62 | */ |
| 63 | PREPARING, |
| 64 | |
| 65 | /** |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 66 | * Prepared transaction state. |
| 67 | * <p> |
| 68 | * Once the first phase of the two-phase commit protocol is complete, the transaction's state is set to |
| 69 | * {@code PREPARED}. |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 70 | */ |
| 71 | PREPARED, |
| 72 | |
| 73 | /** |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 74 | * Committing transaction state. |
| 75 | * <p> |
| 76 | * The {@code COMMITTING} state represents a transaction within the second phase of the two-phase commit |
| 77 | * protocol. |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 78 | */ |
| 79 | COMMITTING, |
| 80 | |
| 81 | /** |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 82 | * Committed transaction state. |
| 83 | * <p> |
| 84 | * Once the second phase of the two-phase commit protocol is complete, the transaction's state is set to |
| 85 | * {@code COMMITTED}. |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 86 | */ |
| 87 | COMMITTED, |
| 88 | |
| 89 | /** |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 90 | * Rolling back transaction state. |
| 91 | * <p> |
| 92 | * In the event of a two-phase lock failure, when the transaction is rolled back it will enter the |
| 93 | * {@code ROLLING_BACK} state while the rollback is in progress. |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 94 | */ |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 95 | ROLLING_BACK, |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 96 | |
| 97 | /** |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 98 | * Rolled back transaction state. |
| 99 | * <p> |
| 100 | * Once a transaction has been rolled back, it will enter the {@code ROLLED_BACK} state. |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 101 | */ |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 102 | ROLLED_BACK, |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 105 | private static final String TX_OPEN_ERROR = "transaction already open"; |
| 106 | private static final String TX_CLOSED_ERROR = "transaction not open"; |
| 107 | private static final String TX_INACTIVE_ERROR = "transaction is not active"; |
| 108 | private static final String TX_UNPREPARED_ERROR = "transaction has not been prepared"; |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 109 | |
Jordan Halterman | 03b8318 | 2017-05-09 12:11:22 -0700 | [diff] [blame] | 110 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 111 | protected final TransactionId transactionId; |
| 112 | protected final Transactional<T> transactionalObject; |
| 113 | private final AtomicBoolean open = new AtomicBoolean(); |
| 114 | private volatile State state = State.ACTIVE; |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 115 | private volatile Version lock; |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 116 | |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 117 | public Transaction(TransactionId transactionId, Transactional<T> transactionalObject) { |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 118 | this.transactionId = transactionId; |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 119 | this.transactionalObject = transactionalObject; |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 122 | /** |
| 123 | * Returns the transaction identifier. |
| 124 | * |
| 125 | * @return the transaction identifier |
| 126 | */ |
| 127 | public TransactionId transactionId() { |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 128 | return transactionId; |
| 129 | } |
| 130 | |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 131 | /** |
| 132 | * Returns the current transaction state. |
| 133 | * |
| 134 | * @return the current transaction state |
| 135 | */ |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 136 | public State state() { |
| 137 | return state; |
| 138 | } |
| 139 | |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 140 | /** |
| 141 | * Returns a boolean indicating whether the transaction is open. |
| 142 | * |
| 143 | * @return indicates whether the transaction is open |
| 144 | */ |
| 145 | public boolean isOpen() { |
| 146 | return open.get(); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Opens the transaction, throwing an {@link IllegalStateException} if it's already open. |
| 151 | */ |
| 152 | protected void open() { |
| 153 | if (!open.compareAndSet(false, true)) { |
| 154 | throw new IllegalStateException(TX_OPEN_ERROR); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Checks that the transaction is open and throws an {@link IllegalStateException} if not. |
| 160 | */ |
| 161 | protected void checkOpen() { |
| 162 | checkState(isOpen(), TX_CLOSED_ERROR); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Checks that the transaction state is {@code ACTIVE} and throws an {@link IllegalStateException} if not. |
| 167 | */ |
| 168 | protected void checkActive() { |
| 169 | checkState(state == State.ACTIVE, TX_INACTIVE_ERROR); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Checks that the transaction state is {@code PREPARED} and throws an {@link IllegalStateException} if not. |
| 174 | */ |
| 175 | protected void checkPrepared() { |
| 176 | checkState(state == State.PREPARED, TX_UNPREPARED_ERROR); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Updates the transaction state. |
| 181 | * |
| 182 | * @param state the updated transaction state |
| 183 | */ |
| 184 | protected void setState(State state) { |
| 185 | this.state = state; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Begins the transaction. |
| 190 | * <p> |
| 191 | * Locks are acquired when the transaction is begun to prevent concurrent transactions from operating on the shared |
| 192 | * resource to which this transaction relates. |
| 193 | * |
| 194 | * @return a completable future to be completed once the transaction has been started |
| 195 | */ |
| 196 | public CompletableFuture<Version> begin() { |
Jordan Halterman | 03b8318 | 2017-05-09 12:11:22 -0700 | [diff] [blame] | 197 | log.debug("Beginning transaction {} for {}", transactionId, transactionalObject); |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 198 | open(); |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 199 | return transactionalObject.begin(transactionId).thenApply(lock -> { |
| 200 | this.lock = lock; |
Jordan Halterman | 03b8318 | 2017-05-09 12:11:22 -0700 | [diff] [blame] | 201 | log.trace("Transaction lock acquired: {}", lock); |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 202 | return lock; |
| 203 | }); |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Prepares the transaction. |
| 208 | * <p> |
| 209 | * When preparing the transaction, the given list of updates for the shared resource will be prepared, and |
| 210 | * concurrent modification checks will be performed. The returned future may be completed with a |
| 211 | * {@link TransactionException} if a concurrent modification is detected for an isolation level that does |
| 212 | * not allow such modifications. |
| 213 | * |
| 214 | * @param updates the transaction updates |
| 215 | * @return a completable future to be completed once the transaction has been prepared |
| 216 | */ |
| 217 | public CompletableFuture<Boolean> prepare(List<T> updates) { |
| 218 | checkOpen(); |
| 219 | checkActive(); |
Jordan Halterman | 03b8318 | 2017-05-09 12:11:22 -0700 | [diff] [blame] | 220 | log.debug("Preparing transaction {} for {}", transactionId, transactionalObject); |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 221 | Version lock = this.lock; |
| 222 | checkState(lock != null, TX_INACTIVE_ERROR); |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 223 | setState(State.PREPARING); |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 224 | return transactionalObject.prepare(new TransactionLog<T>(transactionId, lock.value(), updates)) |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 225 | .thenApply(succeeded -> { |
| 226 | setState(State.PREPARED); |
| 227 | return succeeded; |
| 228 | }); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Prepares and commits the transaction in a single atomic operation. |
| 233 | * <p> |
| 234 | * Both the prepare and commit phases of the protocol must be executed within a single atomic operation. This method |
| 235 | * is used to optimize committing transactions that operate only on a single partition within a single primitive. |
| 236 | * |
| 237 | * @param updates the transaction updates |
| 238 | * @return a completable future to be completed once the transaction has been prepared |
| 239 | */ |
| 240 | public CompletableFuture<Boolean> prepareAndCommit(List<T> updates) { |
| 241 | checkOpen(); |
| 242 | checkActive(); |
Jordan Halterman | 03b8318 | 2017-05-09 12:11:22 -0700 | [diff] [blame] | 243 | log.debug("Preparing and committing transaction {} for {}", transactionId, transactionalObject); |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 244 | Version lock = this.lock; |
| 245 | checkState(lock != null, TX_INACTIVE_ERROR); |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 246 | setState(State.PREPARING); |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 247 | return transactionalObject.prepareAndCommit(new TransactionLog<T>(transactionId, lock.value(), updates)) |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 248 | .thenApply(succeeded -> { |
| 249 | setState(State.COMMITTED); |
| 250 | return succeeded; |
| 251 | }); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Commits the transaction. |
| 256 | * <p> |
| 257 | * Performs the second phase of the two-phase commit protocol, committing the previously |
| 258 | * {@link #prepare(List) prepared} updates. |
| 259 | * |
| 260 | * @return a completable future to be completed once the transaction has been committed |
| 261 | */ |
| 262 | public CompletableFuture<Void> commit() { |
| 263 | checkOpen(); |
| 264 | checkPrepared(); |
Jordan Halterman | 03b8318 | 2017-05-09 12:11:22 -0700 | [diff] [blame] | 265 | log.debug("Committing transaction {} for {}", transactionId, transactionalObject); |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 266 | setState(State.COMMITTING); |
| 267 | return transactionalObject.commit(transactionId).thenRun(() -> { |
| 268 | setState(State.COMMITTED); |
| 269 | }); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Rolls back the transaction. |
| 274 | * <p> |
| 275 | * Rolls back the first phase of the two-phase commit protocol, cancelling prepared updates. |
| 276 | * |
| 277 | * @return a completable future to be completed once the transaction has been rolled back |
| 278 | */ |
| 279 | public CompletableFuture<Void> rollback() { |
| 280 | checkOpen(); |
| 281 | checkPrepared(); |
Jordan Halterman | 03b8318 | 2017-05-09 12:11:22 -0700 | [diff] [blame] | 282 | log.debug("Rolling back transaction {} for {}", transactionId, transactionalObject); |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 283 | setState(State.ROLLING_BACK); |
| 284 | return transactionalObject.rollback(transactionId).thenRun(() -> { |
| 285 | setState(State.ROLLED_BACK); |
| 286 | }); |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | @Override |
| 290 | public String toString() { |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 291 | return toStringHelper(this) |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 292 | .add("transactionId", transactionId) |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 293 | .add("state", state) |
| 294 | .toString(); |
| 295 | } |
| 296 | } |