blob: 19048947c171f7796fc937ff0a2b117707e7acda [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;
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 Jampani7e55c662016-02-15 21:13:53 -080047 if (!transactionParticipants.stream().anyMatch(t -> t.hasPendingUpdates())) {
Madan Jampani3780d4b2016-04-04 18:18:24 -070048 return CompletableFuture.completedFuture(CommitStatus.SUCCESS);
Madan Jampani7e55c662016-02-15 21:13:53 -080049 }
50
Madan Jampani3780d4b2016-04-04 18:18:24 -070051 CompletableFuture<CommitStatus> status = transactions.put(transactionId, Transaction.State.PREPARING)
Madan Jampani7e55c662016-02-15 21:13:53 -080052 .thenCompose(v -> this.doPrepare(transactionParticipants))
53 .thenCompose(result -> result
54 ? transactions.put(transactionId, Transaction.State.COMMITTING)
55 .thenCompose(v -> doCommit(transactionParticipants))
Madan Jampani3780d4b2016-04-04 18:18:24 -070056 .thenApply(v -> CommitStatus.SUCCESS)
Madan Jampani7e55c662016-02-15 21:13:53 -080057 : transactions.put(transactionId, Transaction.State.ROLLINGBACK)
58 .thenCompose(v -> doRollback(transactionParticipants))
Madan Jampani3780d4b2016-04-04 18:18:24 -070059 .thenApply(v -> CommitStatus.FAILURE));
60 return status.thenCompose(v -> transactions.remove(transactionId).thenApply(u -> v));
Madan Jampani7e55c662016-02-15 21:13:53 -080061 }
62
63 private CompletableFuture<Boolean> doPrepare(Set<TransactionParticipant> transactionParticipants) {
Madan Jampani3780d4b2016-04-04 18:18:24 -070064 return Tools.allOf(transactionParticipants.stream()
65 .filter(TransactionParticipant::hasPendingUpdates)
66 .map(TransactionParticipant::prepare)
67 .collect(Collectors.toList()))
Madan Jampani7e55c662016-02-15 21:13:53 -080068 .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 Jampani3780d4b2016-04-04 18:18:24 -070073 .filter(TransactionParticipant::hasPendingUpdates)
74 .map(TransactionParticipant::commit)
Madan Jampani7e55c662016-02-15 21:13:53 -080075 .toArray(CompletableFuture[]::new));
76 }
77
78 private CompletableFuture<Void> doRollback(Set<TransactionParticipant> transactionParticipants) {
79 return CompletableFuture.allOf(transactionParticipants.stream()
Madan Jampani3780d4b2016-04-04 18:18:24 -070080 .filter(TransactionParticipant::hasPendingUpdates)
81 .map(TransactionParticipant::rollback)
Madan Jampani7e55c662016-02-15 21:13:53 -080082 .toArray(CompletableFuture[]::new));
83 }
84}