blob: 02a9fde83549edea3ab82382c5f84ad31d431437 [file] [log] [blame]
Madan Jampanibff6d8f2015-03-31 16:53:47 -07001/*
2 * Copyright 2015 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 */
Madan Jampanif4c88502016-01-21 12:35:36 -080016package org.onosproject.store.primitives.impl;
Madan Jampanibff6d8f2015-03-31 16:53:47 -070017
18import static com.google.common.base.Preconditions.checkNotNull;
Ayaka Koshibee114f042015-05-01 11:43:00 -070019
Madan Jampanibff6d8f2015-03-31 16:53:47 -070020import java.util.Collection;
21import java.util.concurrent.CompletableFuture;
22import java.util.stream.Collectors;
23
Madan Jampanicadd70b2016-02-08 13:45:43 -080024import org.onosproject.store.primitives.TransactionId;
25import org.onosproject.store.primitives.resources.impl.CommitResult;
Madan Jampanibff6d8f2015-03-31 16:53:47 -070026import org.onosproject.store.service.AsyncConsistentMap;
Madan Jampanibff6d8f2015-03-31 16:53:47 -070027
Madan Jampanicadd70b2016-02-08 13:45:43 -080028import static org.onosproject.store.primitives.impl.Transaction.State.COMMITTED;
29import static org.onosproject.store.primitives.impl.Transaction.State.COMMITTING;
30import static org.onosproject.store.primitives.impl.Transaction.State.ROLLEDBACK;
31import static org.onosproject.store.primitives.impl.Transaction.State.ROLLINGBACK;
Madan Jampanibab51a42015-08-10 13:53:35 -070032
Madan Jampanibff6d8f2015-03-31 16:53:47 -070033/**
34 * Agent that runs the two phase commit protocol.
35 */
36public class TransactionManager {
37
38 private final Database database;
Madan Jampanicadd70b2016-02-08 13:45:43 -080039 private final AsyncConsistentMap<TransactionId, Transaction> transactions;
Madan Jampanibff6d8f2015-03-31 16:53:47 -070040
Madan Jampanicadd70b2016-02-08 13:45:43 -080041 public TransactionManager(Database database, AsyncConsistentMap<TransactionId, Transaction> transactions) {
Madan Jampanibff6d8f2015-03-31 16:53:47 -070042 this.database = checkNotNull(database, "database cannot be null");
Madan Jampanicadd70b2016-02-08 13:45:43 -080043 this.transactions = transactions;
Madan Jampanibff6d8f2015-03-31 16:53:47 -070044 }
45
46 /**
47 * Executes the specified transaction by employing a two phase commit protocol.
48 *
49 * @param transaction transaction to commit
Madan Jampanicadd70b2016-02-08 13:45:43 -080050 * @return transaction commit result
Madan Jampanibff6d8f2015-03-31 16:53:47 -070051 */
Madan Jampanicadd70b2016-02-08 13:45:43 -080052 public CompletableFuture<CommitResult> execute(Transaction transaction) {
Madan Jampani92545012016-02-09 13:30:47 -080053 // short-circuit if there is only a single update
54 if (transaction.updates().size() <= 1) {
55 return database.prepareAndCommit(transaction)
56 .thenApply(response -> response.success()
57 ? CommitResult.OK : CommitResult.FAILURE_DURING_COMMIT);
58 }
Madan Jampanibff6d8f2015-03-31 16:53:47 -070059 // clean up if this transaction in already in a terminal state.
Madan Jampanicadd70b2016-02-08 13:45:43 -080060 if (transaction.state() == COMMITTED || transaction.state() == ROLLEDBACK) {
61 return transactions.remove(transaction.id()).thenApply(v -> CommitResult.OK);
62 } else if (transaction.state() == COMMITTING) {
Madan Jampanibff6d8f2015-03-31 16:53:47 -070063 return commit(transaction);
Madan Jampanicadd70b2016-02-08 13:45:43 -080064 } else if (transaction.state() == ROLLINGBACK) {
65 return rollback(transaction).thenApply(v -> CommitResult.FAILURE_TO_PREPARE);
Madan Jampanibff6d8f2015-03-31 16:53:47 -070066 } else {
67 return prepare(transaction).thenCompose(v -> v ? commit(transaction) : rollback(transaction));
68 }
69 }
70
Madan Jampanibff6d8f2015-03-31 16:53:47 -070071 /**
Madan Jampanicadd70b2016-02-08 13:45:43 -080072 * Returns all pending transaction identifiers.
Madan Jampanibff6d8f2015-03-31 16:53:47 -070073 *
Madan Jampanicadd70b2016-02-08 13:45:43 -080074 * @return future for a collection of transaction identifiers.
Madan Jampanibff6d8f2015-03-31 16:53:47 -070075 */
Madan Jampanicadd70b2016-02-08 13:45:43 -080076 public CompletableFuture<Collection<TransactionId>> getPendingTransactionIds() {
77 return transactions.values().thenApply(c -> c.stream()
78 .map(v -> v.value())
79 .filter(v -> v.state() != COMMITTED && v.state() != ROLLEDBACK)
80 .map(Transaction::id)
81 .collect(Collectors.toList()));
Madan Jampanibff6d8f2015-03-31 16:53:47 -070082 }
83
84 private CompletableFuture<Boolean> prepare(Transaction transaction) {
85 return transactions.put(transaction.id(), transaction)
86 .thenCompose(v -> database.prepare(transaction))
87 .thenCompose(status -> transactions.put(
88 transaction.id(),
Madan Jampanicadd70b2016-02-08 13:45:43 -080089 transaction.transition(status ? COMMITTING : ROLLINGBACK))
Madan Jampanibff6d8f2015-03-31 16:53:47 -070090 .thenApply(v -> status));
91 }
92
Madan Jampanicadd70b2016-02-08 13:45:43 -080093 private CompletableFuture<CommitResult> commit(Transaction transaction) {
Madan Jampanibff6d8f2015-03-31 16:53:47 -070094 return database.commit(transaction)
Madan Jampanicadd70b2016-02-08 13:45:43 -080095 .thenCompose(r -> {
96 if (r.success()) {
97 return transactions.put(transaction.id(), transaction.transition(COMMITTED))
98 .thenApply(v -> CommitResult.OK);
99 } else {
100 return CompletableFuture.completedFuture(CommitResult.FAILURE_DURING_COMMIT);
101 }
102 });
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700103 }
104
Madan Jampanicadd70b2016-02-08 13:45:43 -0800105 private CompletableFuture<CommitResult> rollback(Transaction transaction) {
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700106 return database.rollback(transaction)
Madan Jampanicadd70b2016-02-08 13:45:43 -0800107 .thenCompose(v -> transactions.put(transaction.id(), transaction.transition(ROLLEDBACK)))
108 .thenApply(v -> CommitResult.FAILURE_TO_PREPARE);
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700109 }
Madan Jampani02b7fb82015-05-01 13:01:20 -0700110}