Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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 | |
| 22 | import org.onlab.util.Tools; |
| 23 | import org.onosproject.store.primitives.TransactionId; |
| 24 | import org.onosproject.store.service.AsyncConsistentMap; |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame] | 25 | import org.onosproject.store.service.CommitStatus; |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Coordinator for a two-phase commit protocol. |
| 29 | */ |
| 30 | public class TransactionCoordinator { |
| 31 | |
| 32 | private final AsyncConsistentMap<TransactionId, Transaction.State> transactions; |
| 33 | |
| 34 | public TransactionCoordinator(AsyncConsistentMap<TransactionId, Transaction.State> transactions) { |
| 35 | this.transactions = transactions; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Commits a transaction. |
Thomas Vachuska | 708d303 | 2016-02-18 11:11:46 -0800 | [diff] [blame] | 40 | * |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame] | 41 | * @param transactionId transaction identifier |
Thomas Vachuska | 708d303 | 2016-02-18 11:11:46 -0800 | [diff] [blame] | 42 | * @param transactionParticipants set of transaction participants |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 43 | * @return future for commit result |
| 44 | */ |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame] | 45 | CompletableFuture<CommitStatus> commit(TransactionId transactionId, |
| 46 | Set<TransactionParticipant> transactionParticipants) { |
Madan Jampani | 542d9e2 | 2016-04-05 15:39:55 -0700 | [diff] [blame] | 47 | int totalUpdates = transactionParticipants.stream() |
| 48 | .map(TransactionParticipant::totalUpdates) |
| 49 | .reduce(Math::addExact) |
| 50 | .orElse(0); |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 51 | |
Madan Jampani | 542d9e2 | 2016-04-05 15:39:55 -0700 | [diff] [blame] | 52 | if (totalUpdates == 0) { |
| 53 | return CompletableFuture.completedFuture(CommitStatus.SUCCESS); |
| 54 | } else if (totalUpdates == 1) { |
| 55 | return transactionParticipants.stream() |
| 56 | .filter(p -> p.totalUpdates() == 1) |
| 57 | .findFirst() |
| 58 | .get() |
| 59 | .prepareAndCommit() |
| 60 | .thenApply(v -> v ? CommitStatus.SUCCESS : CommitStatus.FAILURE); |
| 61 | } else { |
| 62 | CompletableFuture<CommitStatus> status = transactions.put(transactionId, Transaction.State.PREPARING) |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 63 | .thenCompose(v -> this.doPrepare(transactionParticipants)) |
| 64 | .thenCompose(result -> result |
Madan Jampani | 542d9e2 | 2016-04-05 15:39:55 -0700 | [diff] [blame] | 65 | ? transactions.put(transactionId, Transaction.State.COMMITTING) |
| 66 | .thenCompose(v -> doCommit(transactionParticipants)) |
| 67 | .thenApply(v -> CommitStatus.SUCCESS) |
| 68 | : transactions.put(transactionId, Transaction.State.ROLLINGBACK) |
| 69 | .thenCompose(v -> doRollback(transactionParticipants)) |
| 70 | .thenApply(v -> CommitStatus.FAILURE)); |
| 71 | return status.thenCompose(v -> transactions.remove(transactionId).thenApply(u -> v)); |
| 72 | } |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | private CompletableFuture<Boolean> doPrepare(Set<TransactionParticipant> transactionParticipants) { |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame] | 76 | return Tools.allOf(transactionParticipants.stream() |
| 77 | .filter(TransactionParticipant::hasPendingUpdates) |
| 78 | .map(TransactionParticipant::prepare) |
| 79 | .collect(Collectors.toList())) |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 80 | .thenApply(list -> list.stream().reduce(Boolean::logicalAnd).orElse(true)); |
| 81 | } |
| 82 | |
| 83 | private CompletableFuture<Void> doCommit(Set<TransactionParticipant> transactionParticipants) { |
| 84 | return CompletableFuture.allOf(transactionParticipants.stream() |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame] | 85 | .filter(TransactionParticipant::hasPendingUpdates) |
| 86 | .map(TransactionParticipant::commit) |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 87 | .toArray(CompletableFuture[]::new)); |
| 88 | } |
| 89 | |
| 90 | private CompletableFuture<Void> doRollback(Set<TransactionParticipant> transactionParticipants) { |
| 91 | return CompletableFuture.allOf(transactionParticipants.stream() |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame] | 92 | .filter(TransactionParticipant::hasPendingUpdates) |
| 93 | .map(TransactionParticipant::rollback) |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 94 | .toArray(CompletableFuture[]::new)); |
| 95 | } |
| 96 | } |