Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | */ |
| 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 | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 47 | if (!transactionParticipants.stream().anyMatch(t -> t.hasPendingUpdates())) { |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame^] | 48 | return CompletableFuture.completedFuture(CommitStatus.SUCCESS); |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame^] | 51 | CompletableFuture<CommitStatus> status = transactions.put(transactionId, Transaction.State.PREPARING) |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 52 | .thenCompose(v -> this.doPrepare(transactionParticipants)) |
| 53 | .thenCompose(result -> result |
| 54 | ? transactions.put(transactionId, Transaction.State.COMMITTING) |
| 55 | .thenCompose(v -> doCommit(transactionParticipants)) |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame^] | 56 | .thenApply(v -> CommitStatus.SUCCESS) |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 57 | : transactions.put(transactionId, Transaction.State.ROLLINGBACK) |
| 58 | .thenCompose(v -> doRollback(transactionParticipants)) |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame^] | 59 | .thenApply(v -> CommitStatus.FAILURE)); |
| 60 | return status.thenCompose(v -> transactions.remove(transactionId).thenApply(u -> v)); |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | private CompletableFuture<Boolean> doPrepare(Set<TransactionParticipant> transactionParticipants) { |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame^] | 64 | return Tools.allOf(transactionParticipants.stream() |
| 65 | .filter(TransactionParticipant::hasPendingUpdates) |
| 66 | .map(TransactionParticipant::prepare) |
| 67 | .collect(Collectors.toList())) |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 68 | .thenApply(list -> list.stream().reduce(Boolean::logicalAnd).orElse(true)); |
| 69 | } |
| 70 | |
| 71 | private CompletableFuture<Void> doCommit(Set<TransactionParticipant> transactionParticipants) { |
| 72 | return CompletableFuture.allOf(transactionParticipants.stream() |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame^] | 73 | .filter(TransactionParticipant::hasPendingUpdates) |
| 74 | .map(TransactionParticipant::commit) |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 75 | .toArray(CompletableFuture[]::new)); |
| 76 | } |
| 77 | |
| 78 | private CompletableFuture<Void> doRollback(Set<TransactionParticipant> transactionParticipants) { |
| 79 | return CompletableFuture.allOf(transactionParticipants.stream() |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame^] | 80 | .filter(TransactionParticipant::hasPendingUpdates) |
| 81 | .map(TransactionParticipant::rollback) |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 82 | .toArray(CompletableFuture[]::new)); |
| 83 | } |
| 84 | } |