blob: 908a35dd88ff3ee89c5fe62b2e7caee5d74ba5c8 [file] [log] [blame]
Madan Jampani7e55c662016-02-15 21:13:53 -08001/*
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 */
16package org.onosproject.store.primitives.impl;
17
18import java.util.Set;
19import java.util.concurrent.CompletableFuture;
20import java.util.stream.Collectors;
21
22import org.onlab.util.Tools;
23import org.onosproject.store.primitives.TransactionId;
24import org.onosproject.store.service.AsyncConsistentMap;
25
26/**
27 * Coordinator for a two-phase commit protocol.
28 */
29public class TransactionCoordinator {
30
31 private final AsyncConsistentMap<TransactionId, Transaction.State> transactions;
32
33 public TransactionCoordinator(AsyncConsistentMap<TransactionId, Transaction.State> transactions) {
34 this.transactions = transactions;
35 }
36
37 /**
38 * Commits a transaction.
Thomas Vachuska708d3032016-02-18 11:11:46 -080039 *
40 * @param transactionId transaction
41 * @param transactionParticipants set of transaction participants
Madan Jampani7e55c662016-02-15 21:13:53 -080042 * @return future for commit result
43 */
44 CompletableFuture<Void> commit(TransactionId transactionId, Set<TransactionParticipant> transactionParticipants) {
45 if (!transactionParticipants.stream().anyMatch(t -> t.hasPendingUpdates())) {
46 return CompletableFuture.completedFuture(null);
47 }
48
49 return transactions.put(transactionId, Transaction.State.PREPARING)
50 .thenCompose(v -> this.doPrepare(transactionParticipants))
51 .thenCompose(result -> result
52 ? transactions.put(transactionId, Transaction.State.COMMITTING)
53 .thenCompose(v -> doCommit(transactionParticipants))
54 .thenApply(v -> null)
55 : transactions.put(transactionId, Transaction.State.ROLLINGBACK)
56 .thenCompose(v -> doRollback(transactionParticipants))
57 .thenApply(v -> null))
Madan Jampani28a69742016-02-17 18:36:59 -080058 .thenCompose(v -> transactions.remove(transactionId))
Madan Jampani7e55c662016-02-15 21:13:53 -080059 .thenApply(v -> null);
60 }
61
62 private CompletableFuture<Boolean> doPrepare(Set<TransactionParticipant> transactionParticipants) {
63 return Tools.allOf(transactionParticipants
64 .stream()
65 .map(TransactionParticipant::prepare)
66 .collect(Collectors.toList()))
67 .thenApply(list -> list.stream().reduce(Boolean::logicalAnd).orElse(true));
68 }
69
70 private CompletableFuture<Void> doCommit(Set<TransactionParticipant> transactionParticipants) {
71 return CompletableFuture.allOf(transactionParticipants.stream()
72 .map(p -> p.commit())
73 .toArray(CompletableFuture[]::new));
74 }
75
76 private CompletableFuture<Void> doRollback(Set<TransactionParticipant> transactionParticipants) {
77 return CompletableFuture.allOf(transactionParticipants.stream()
78 .map(p -> p.rollback())
79 .toArray(CompletableFuture[]::new));
80 }
81}