blob: 30aebb1bc0eaaeebee06d0eac00a1253569ad0fb [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 Jampanibff6d8f2015-03-31 16:53:47 -070053 // clean up if this transaction in already in a terminal state.
Madan Jampanicadd70b2016-02-08 13:45:43 -080054 if (transaction.state() == COMMITTED || transaction.state() == ROLLEDBACK) {
55 return transactions.remove(transaction.id()).thenApply(v -> CommitResult.OK);
56 } else if (transaction.state() == COMMITTING) {
Madan Jampanibff6d8f2015-03-31 16:53:47 -070057 return commit(transaction);
Madan Jampanicadd70b2016-02-08 13:45:43 -080058 } else if (transaction.state() == ROLLINGBACK) {
59 return rollback(transaction).thenApply(v -> CommitResult.FAILURE_TO_PREPARE);
Madan Jampanibff6d8f2015-03-31 16:53:47 -070060 } else {
61 return prepare(transaction).thenCompose(v -> v ? commit(transaction) : rollback(transaction));
62 }
63 }
64
Madan Jampanibff6d8f2015-03-31 16:53:47 -070065 /**
Madan Jampanicadd70b2016-02-08 13:45:43 -080066 * Returns all pending transaction identifiers.
Madan Jampanibff6d8f2015-03-31 16:53:47 -070067 *
Madan Jampanicadd70b2016-02-08 13:45:43 -080068 * @return future for a collection of transaction identifiers.
Madan Jampanibff6d8f2015-03-31 16:53:47 -070069 */
Madan Jampanicadd70b2016-02-08 13:45:43 -080070 public CompletableFuture<Collection<TransactionId>> getPendingTransactionIds() {
71 return transactions.values().thenApply(c -> c.stream()
72 .map(v -> v.value())
73 .filter(v -> v.state() != COMMITTED && v.state() != ROLLEDBACK)
74 .map(Transaction::id)
75 .collect(Collectors.toList()));
Madan Jampanibff6d8f2015-03-31 16:53:47 -070076 }
77
78 private CompletableFuture<Boolean> prepare(Transaction transaction) {
79 return transactions.put(transaction.id(), transaction)
80 .thenCompose(v -> database.prepare(transaction))
81 .thenCompose(status -> transactions.put(
82 transaction.id(),
Madan Jampanicadd70b2016-02-08 13:45:43 -080083 transaction.transition(status ? COMMITTING : ROLLINGBACK))
Madan Jampanibff6d8f2015-03-31 16:53:47 -070084 .thenApply(v -> status));
85 }
86
Madan Jampanicadd70b2016-02-08 13:45:43 -080087 private CompletableFuture<CommitResult> commit(Transaction transaction) {
Madan Jampanibff6d8f2015-03-31 16:53:47 -070088 return database.commit(transaction)
Madan Jampanicadd70b2016-02-08 13:45:43 -080089 .thenCompose(r -> {
90 if (r.success()) {
91 return transactions.put(transaction.id(), transaction.transition(COMMITTED))
92 .thenApply(v -> CommitResult.OK);
93 } else {
94 return CompletableFuture.completedFuture(CommitResult.FAILURE_DURING_COMMIT);
95 }
96 });
Madan Jampanibff6d8f2015-03-31 16:53:47 -070097 }
98
Madan Jampanicadd70b2016-02-08 13:45:43 -080099 private CompletableFuture<CommitResult> rollback(Transaction transaction) {
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700100 return database.rollback(transaction)
Madan Jampanicadd70b2016-02-08 13:45:43 -0800101 .thenCompose(v -> transactions.put(transaction.id(), transaction.transition(ROLLEDBACK)))
102 .thenApply(v -> CommitResult.FAILURE_TO_PREPARE);
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700103 }
Madan Jampani02b7fb82015-05-01 13:01:20 -0700104}