blob: cd2768f1a81701df20869f7f6caa88cac7a97555 [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.
39 * @param transactionId transaction
40 * @return future for commit result
41 */
42 CompletableFuture<Void> commit(TransactionId transactionId, Set<TransactionParticipant> transactionParticipants) {
43 if (!transactionParticipants.stream().anyMatch(t -> t.hasPendingUpdates())) {
44 return CompletableFuture.completedFuture(null);
45 }
46
47 return transactions.put(transactionId, Transaction.State.PREPARING)
48 .thenCompose(v -> this.doPrepare(transactionParticipants))
49 .thenCompose(result -> result
50 ? transactions.put(transactionId, Transaction.State.COMMITTING)
51 .thenCompose(v -> doCommit(transactionParticipants))
52 .thenApply(v -> null)
53 : transactions.put(transactionId, Transaction.State.ROLLINGBACK)
54 .thenCompose(v -> doRollback(transactionParticipants))
55 .thenApply(v -> null))
56 .thenCompose(v -> transactions.remove(transactionId).thenApply(u -> null))
57 .thenApply(v -> null);
58 }
59
60 private CompletableFuture<Boolean> doPrepare(Set<TransactionParticipant> transactionParticipants) {
61 return Tools.allOf(transactionParticipants
62 .stream()
63 .map(TransactionParticipant::prepare)
64 .collect(Collectors.toList()))
65 .thenApply(list -> list.stream().reduce(Boolean::logicalAnd).orElse(true));
66 }
67
68 private CompletableFuture<Void> doCommit(Set<TransactionParticipant> transactionParticipants) {
69 return CompletableFuture.allOf(transactionParticipants.stream()
70 .map(p -> p.commit())
71 .toArray(CompletableFuture[]::new));
72 }
73
74 private CompletableFuture<Void> doRollback(Set<TransactionParticipant> transactionParticipants) {
75 return CompletableFuture.allOf(transactionParticipants.stream()
76 .map(p -> p.rollback())
77 .toArray(CompletableFuture[]::new));
78 }
79}