Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | */ |
Madan Jampani | f4c8850 | 2016-01-21 12:35:36 -0800 | [diff] [blame] | 16 | package org.onosproject.store.primitives.impl; |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 19 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 20 | import java.util.Collection; |
| 21 | import java.util.concurrent.CompletableFuture; |
| 22 | import java.util.stream.Collectors; |
| 23 | |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 24 | import org.onosproject.store.primitives.TransactionId; |
| 25 | import org.onosproject.store.primitives.resources.impl.CommitResult; |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 26 | import org.onosproject.store.service.AsyncConsistentMap; |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 27 | |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 28 | import static org.onosproject.store.primitives.impl.Transaction.State.COMMITTED; |
| 29 | import static org.onosproject.store.primitives.impl.Transaction.State.COMMITTING; |
| 30 | import static org.onosproject.store.primitives.impl.Transaction.State.ROLLEDBACK; |
| 31 | import static org.onosproject.store.primitives.impl.Transaction.State.ROLLINGBACK; |
Madan Jampani | bab51a4 | 2015-08-10 13:53:35 -0700 | [diff] [blame] | 32 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Agent that runs the two phase commit protocol. |
| 35 | */ |
| 36 | public class TransactionManager { |
| 37 | |
| 38 | private final Database database; |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 39 | private final AsyncConsistentMap<TransactionId, Transaction> transactions; |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 40 | |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 41 | public TransactionManager(Database database, AsyncConsistentMap<TransactionId, Transaction> transactions) { |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 42 | this.database = checkNotNull(database, "database cannot be null"); |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 43 | this.transactions = transactions; |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Executes the specified transaction by employing a two phase commit protocol. |
| 48 | * |
| 49 | * @param transaction transaction to commit |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 50 | * @return transaction commit result |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 51 | */ |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 52 | public CompletableFuture<CommitResult> execute(Transaction transaction) { |
Madan Jampani | 9254501 | 2016-02-09 13:30:47 -0800 | [diff] [blame] | 53 | // short-circuit if there is only a single update |
| 54 | if (transaction.updates().size() <= 1) { |
| 55 | return database.prepareAndCommit(transaction) |
| 56 | .thenApply(response -> response.success() |
| 57 | ? CommitResult.OK : CommitResult.FAILURE_DURING_COMMIT); |
| 58 | } |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 59 | // clean up if this transaction in already in a terminal state. |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 60 | if (transaction.state() == COMMITTED || transaction.state() == ROLLEDBACK) { |
| 61 | return transactions.remove(transaction.id()).thenApply(v -> CommitResult.OK); |
| 62 | } else if (transaction.state() == COMMITTING) { |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 63 | return commit(transaction); |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 64 | } else if (transaction.state() == ROLLINGBACK) { |
| 65 | return rollback(transaction).thenApply(v -> CommitResult.FAILURE_TO_PREPARE); |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 66 | } else { |
| 67 | return prepare(transaction).thenCompose(v -> v ? commit(transaction) : rollback(transaction)); |
| 68 | } |
| 69 | } |
| 70 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 71 | /** |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 72 | * Returns all pending transaction identifiers. |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 73 | * |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 74 | * @return future for a collection of transaction identifiers. |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 75 | */ |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 76 | public CompletableFuture<Collection<TransactionId>> getPendingTransactionIds() { |
| 77 | return transactions.values().thenApply(c -> c.stream() |
| 78 | .map(v -> v.value()) |
| 79 | .filter(v -> v.state() != COMMITTED && v.state() != ROLLEDBACK) |
| 80 | .map(Transaction::id) |
| 81 | .collect(Collectors.toList())); |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | private CompletableFuture<Boolean> prepare(Transaction transaction) { |
| 85 | return transactions.put(transaction.id(), transaction) |
| 86 | .thenCompose(v -> database.prepare(transaction)) |
| 87 | .thenCompose(status -> transactions.put( |
| 88 | transaction.id(), |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 89 | transaction.transition(status ? COMMITTING : ROLLINGBACK)) |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 90 | .thenApply(v -> status)); |
| 91 | } |
| 92 | |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 93 | private CompletableFuture<CommitResult> commit(Transaction transaction) { |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 94 | return database.commit(transaction) |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 95 | .thenCompose(r -> { |
| 96 | if (r.success()) { |
| 97 | return transactions.put(transaction.id(), transaction.transition(COMMITTED)) |
| 98 | .thenApply(v -> CommitResult.OK); |
| 99 | } else { |
| 100 | return CompletableFuture.completedFuture(CommitResult.FAILURE_DURING_COMMIT); |
| 101 | } |
| 102 | }); |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 105 | private CompletableFuture<CommitResult> rollback(Transaction transaction) { |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 106 | return database.rollback(transaction) |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 107 | .thenCompose(v -> transactions.put(transaction.id(), transaction.transition(ROLLEDBACK))) |
| 108 | .thenApply(v -> CommitResult.FAILURE_TO_PREPARE); |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 109 | } |
Madan Jampani | 02b7fb8 | 2015-05-01 13:01:20 -0700 | [diff] [blame] | 110 | } |