Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -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.Set; |
| 19 | import java.util.concurrent.CompletableFuture; |
| 20 | import java.util.stream.Collectors; |
| 21 | |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Sets; |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 23 | import org.onlab.util.Tools; |
| 24 | import org.onosproject.store.primitives.TransactionId; |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame] | 25 | import org.onosproject.store.service.CommitStatus; |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 26 | import org.onosproject.store.service.Serializer; |
| 27 | import org.onosproject.store.service.TransactionalMap; |
Jordan Halterman | 03b8318 | 2017-05-09 12:11:22 -0700 | [diff] [blame] | 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 30 | |
| 31 | import static com.google.common.base.MoreObjects.toStringHelper; |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 34 | * Transaction coordinator. |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 35 | */ |
| 36 | public class TransactionCoordinator { |
Jordan Halterman | 03b8318 | 2017-05-09 12:11:22 -0700 | [diff] [blame] | 37 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 38 | protected final TransactionId transactionId; |
| 39 | protected final TransactionManager transactionManager; |
| 40 | protected final Set<TransactionParticipant> transactionParticipants = Sets.newConcurrentHashSet(); |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 41 | |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 42 | public TransactionCoordinator(TransactionId transactionId, TransactionManager transactionManager) { |
| 43 | this.transactionId = transactionId; |
| 44 | this.transactionManager = transactionManager; |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /** |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 48 | * Returns a transactional map for this transaction. |
Thomas Vachuska | 708d303 | 2016-02-18 11:11:46 -0800 | [diff] [blame] | 49 | * |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 50 | * @param name the transactional map name |
| 51 | * @param serializer the serializer |
| 52 | * @param <K> key type |
| 53 | * @param <V> value type |
| 54 | * @return a transactional map for this transaction |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 55 | */ |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 56 | public <K, V> TransactionalMap<K, V> getTransactionalMap(String name, Serializer serializer) { |
| 57 | PartitionedTransactionalMap<K, V> map = transactionManager.getTransactionalMap(name, serializer, this); |
| 58 | transactionParticipants.addAll(map.participants()); |
| 59 | return map; |
| 60 | } |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 61 | |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 62 | /** |
| 63 | * Commits the transaction. |
| 64 | * |
| 65 | * @return the transaction commit status |
| 66 | */ |
| 67 | public CompletableFuture<CommitStatus> commit() { |
| 68 | long totalParticipants = transactionParticipants.stream() |
| 69 | .filter(TransactionParticipant::hasPendingUpdates) |
| 70 | .count(); |
| 71 | |
| 72 | if (totalParticipants == 0) { |
Jordan Halterman | 03b8318 | 2017-05-09 12:11:22 -0700 | [diff] [blame] | 73 | log.debug("No transaction participants, skipping commit", totalParticipants); |
Madan Jampani | 542d9e2 | 2016-04-05 15:39:55 -0700 | [diff] [blame] | 74 | return CompletableFuture.completedFuture(CommitStatus.SUCCESS); |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 75 | } else if (totalParticipants == 1) { |
Jordan Halterman | 03b8318 | 2017-05-09 12:11:22 -0700 | [diff] [blame] | 76 | log.debug("Committing transaction {} via 1 participant", transactionId); |
Madan Jampani | 542d9e2 | 2016-04-05 15:39:55 -0700 | [diff] [blame] | 77 | return transactionParticipants.stream() |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 78 | .filter(TransactionParticipant::hasPendingUpdates) |
| 79 | .findFirst() |
| 80 | .get() |
| 81 | .prepareAndCommit() |
| 82 | .thenApply(v -> v ? CommitStatus.SUCCESS : CommitStatus.FAILURE); |
Madan Jampani | 542d9e2 | 2016-04-05 15:39:55 -0700 | [diff] [blame] | 83 | } else { |
Jordan Halterman | 03b8318 | 2017-05-09 12:11:22 -0700 | [diff] [blame] | 84 | log.debug("Committing transaction {} via {} participants", transactionId, totalParticipants); |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 85 | Set<TransactionParticipant> transactionParticipants = this.transactionParticipants.stream() |
| 86 | .filter(TransactionParticipant::hasPendingUpdates) |
| 87 | .collect(Collectors.toSet()); |
| 88 | |
| 89 | CompletableFuture<CommitStatus> status = transactionManager.updateState( |
| 90 | transactionId, Transaction.State.PREPARING) |
| 91 | .thenCompose(v -> prepare(transactionParticipants)) |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 92 | .thenCompose(result -> result |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 93 | ? transactionManager.updateState(transactionId, Transaction.State.COMMITTING) |
| 94 | .thenCompose(v -> commit(transactionParticipants)) |
| 95 | .thenApply(v -> CommitStatus.SUCCESS) |
| 96 | : transactionManager.updateState(transactionId, Transaction.State.ROLLING_BACK) |
| 97 | .thenCompose(v -> rollback(transactionParticipants)) |
| 98 | .thenApply(v -> CommitStatus.FAILURE)); |
| 99 | return status.thenCompose(v -> transactionManager.remove(transactionId).thenApply(u -> v)); |
Madan Jampani | 542d9e2 | 2016-04-05 15:39:55 -0700 | [diff] [blame] | 100 | } |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 103 | /** |
| 104 | * Performs the prepare phase of the two-phase commit protocol for the given transaction participants. |
| 105 | * |
| 106 | * @param transactionParticipants the transaction participants for which to prepare the transaction |
| 107 | * @return a completable future indicating whether <em>all</em> prepares succeeded |
| 108 | */ |
| 109 | protected CompletableFuture<Boolean> prepare(Set<TransactionParticipant> transactionParticipants) { |
Jordan Halterman | 03b8318 | 2017-05-09 12:11:22 -0700 | [diff] [blame] | 110 | log.trace("Preparing transaction {} via {}", transactionId, transactionParticipants); |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame] | 111 | return Tools.allOf(transactionParticipants.stream() |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 112 | .map(TransactionParticipant::prepare) |
| 113 | .collect(Collectors.toList())) |
| 114 | .thenApply(list -> list.stream().reduce(Boolean::logicalAnd).orElse(true)); |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 117 | /** |
| 118 | * Performs the commit phase of the two-phase commit protocol for the given transaction participants. |
| 119 | * |
| 120 | * @param transactionParticipants the transaction participants for which to commit the transaction |
| 121 | * @return a completable future to be completed once the commits are complete |
| 122 | */ |
| 123 | protected CompletableFuture<Void> commit(Set<TransactionParticipant> transactionParticipants) { |
Jordan Halterman | 03b8318 | 2017-05-09 12:11:22 -0700 | [diff] [blame] | 124 | log.trace("Committing transaction {} via {}", transactionId, transactionParticipants); |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 125 | return CompletableFuture.allOf(transactionParticipants.stream() |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 126 | .map(TransactionParticipant::commit) |
| 127 | .toArray(CompletableFuture[]::new)); |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 130 | /** |
| 131 | * Rolls back transactions for the given participants. |
| 132 | * |
| 133 | * @param transactionParticipants the transaction participants for which to roll back the transaction |
| 134 | * @return a completable future to be completed once the rollbacks are complete |
| 135 | */ |
| 136 | protected CompletableFuture<Void> rollback(Set<TransactionParticipant> transactionParticipants) { |
Jordan Halterman | 03b8318 | 2017-05-09 12:11:22 -0700 | [diff] [blame] | 137 | log.trace("Rolling back transaction {} via {}", transactionId, transactionParticipants); |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 138 | return CompletableFuture.allOf(transactionParticipants.stream() |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 139 | .map(TransactionParticipant::rollback) |
| 140 | .toArray(CompletableFuture[]::new)); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public String toString() { |
| 145 | return toStringHelper(this) |
| 146 | .add("transactionId", transactionId) |
| 147 | .add("participants", transactionParticipants) |
| 148 | .toString(); |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 149 | } |
| 150 | } |