blob: 160ecf92c2b55ac978a90560d91df6e55747269f [file] [log] [blame]
Madan Jampani7e55c662016-02-15 21:13:53 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Madan Jampani7e55c662016-02-15 21:13:53 -08003 *
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;
Madan Jampani3780d4b2016-04-04 18:18:24 -070025import org.onosproject.store.service.CommitStatus;
Madan Jampani7e55c662016-02-15 21:13:53 -080026
27/**
28 * Coordinator for a two-phase commit protocol.
29 */
30public 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 Vachuska708d3032016-02-18 11:11:46 -080040 *
Madan Jampani3780d4b2016-04-04 18:18:24 -070041 * @param transactionId transaction identifier
Thomas Vachuska708d3032016-02-18 11:11:46 -080042 * @param transactionParticipants set of transaction participants
Madan Jampani7e55c662016-02-15 21:13:53 -080043 * @return future for commit result
44 */
Madan Jampani3780d4b2016-04-04 18:18:24 -070045 CompletableFuture<CommitStatus> commit(TransactionId transactionId,
46 Set<TransactionParticipant> transactionParticipants) {
Madan Jampani542d9e22016-04-05 15:39:55 -070047 int totalUpdates = transactionParticipants.stream()
48 .map(TransactionParticipant::totalUpdates)
49 .reduce(Math::addExact)
50 .orElse(0);
Madan Jampani7e55c662016-02-15 21:13:53 -080051
Madan Jampani542d9e22016-04-05 15:39:55 -070052 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 Jampani7e55c662016-02-15 21:13:53 -080063 .thenCompose(v -> this.doPrepare(transactionParticipants))
64 .thenCompose(result -> result
Madan Jampani542d9e22016-04-05 15:39:55 -070065 ? 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 Jampani7e55c662016-02-15 21:13:53 -080073 }
74
75 private CompletableFuture<Boolean> doPrepare(Set<TransactionParticipant> transactionParticipants) {
Madan Jampani3780d4b2016-04-04 18:18:24 -070076 return Tools.allOf(transactionParticipants.stream()
77 .filter(TransactionParticipant::hasPendingUpdates)
78 .map(TransactionParticipant::prepare)
79 .collect(Collectors.toList()))
Madan Jampani7e55c662016-02-15 21:13:53 -080080 .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 Jampani3780d4b2016-04-04 18:18:24 -070085 .filter(TransactionParticipant::hasPendingUpdates)
86 .map(TransactionParticipant::commit)
Madan Jampani7e55c662016-02-15 21:13:53 -080087 .toArray(CompletableFuture[]::new));
88 }
89
90 private CompletableFuture<Void> doRollback(Set<TransactionParticipant> transactionParticipants) {
91 return CompletableFuture.allOf(transactionParticipants.stream()
Madan Jampani3780d4b2016-04-04 18:18:24 -070092 .filter(TransactionParticipant::hasPendingUpdates)
93 .map(TransactionParticipant::rollback)
Madan Jampani7e55c662016-02-15 21:13:53 -080094 .toArray(CompletableFuture[]::new));
95 }
96}