Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.store.consistent.impl; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 19 | |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 20 | import java.util.Arrays; |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 21 | import java.util.Collection; |
| 22 | import java.util.concurrent.CompletableFuture; |
| 23 | import java.util.stream.Collectors; |
| 24 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 25 | import org.onlab.util.KryoNamespace; |
| 26 | import org.onosproject.store.serializers.KryoNamespaces; |
| 27 | import org.onosproject.store.service.AsyncConsistentMap; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 28 | import org.onosproject.store.service.ConsistentMapBuilder; |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 29 | import org.onosproject.store.service.DatabaseUpdate; |
| 30 | import org.onosproject.store.service.Serializer; |
| 31 | import org.onosproject.store.service.Transaction; |
| 32 | import org.onosproject.store.service.Versioned; |
| 33 | import org.onosproject.store.service.Transaction.State; |
| 34 | |
Madan Jampani | bab51a4 | 2015-08-10 13:53:35 -0700 | [diff] [blame] | 35 | import com.google.common.collect.ImmutableList; |
| 36 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Agent that runs the two phase commit protocol. |
| 39 | */ |
| 40 | public class TransactionManager { |
| 41 | |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 42 | private static final KryoNamespace KRYO_NAMESPACE = KryoNamespace.newBuilder() |
| 43 | .register(KryoNamespaces.BASIC) |
| 44 | .nextId(KryoNamespace.FLOATING_ID) |
| 45 | .register(Versioned.class) |
| 46 | .register(DatabaseUpdate.class) |
| 47 | .register(DatabaseUpdate.Type.class) |
| 48 | .register(DefaultTransaction.class) |
| 49 | .register(Transaction.State.class) |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 50 | .build(); |
| 51 | |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 52 | private final Serializer serializer = Serializer.using(Arrays.asList(KRYO_NAMESPACE)); |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 53 | private final Database database; |
| 54 | private final AsyncConsistentMap<Long, Transaction> transactions; |
| 55 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 56 | /** |
| 57 | * Constructs a new TransactionManager for the specified database instance. |
| 58 | * |
| 59 | * @param database database |
Madan Jampani | 0dbac7a | 2015-06-25 10:37:45 -0700 | [diff] [blame] | 60 | * @param mapBuilder builder for ConsistentMap instances |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 61 | */ |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 62 | public TransactionManager(Database database, ConsistentMapBuilder<Long, Transaction> mapBuilder) { |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 63 | this.database = checkNotNull(database, "database cannot be null"); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 64 | this.transactions = mapBuilder.withName("onos-transactions") |
| 65 | .withSerializer(serializer) |
| 66 | .buildAsyncMap(); |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Executes the specified transaction by employing a two phase commit protocol. |
| 71 | * |
| 72 | * @param transaction transaction to commit |
| 73 | * @return transaction result. Result value true indicates a successful commit, false |
| 74 | * indicates abort |
| 75 | */ |
Madan Jampani | bab51a4 | 2015-08-10 13:53:35 -0700 | [diff] [blame] | 76 | public CompletableFuture<CommitResponse> execute(Transaction transaction) { |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 77 | // clean up if this transaction in already in a terminal state. |
| 78 | if (transaction.state() == Transaction.State.COMMITTED || |
| 79 | transaction.state() == Transaction.State.ROLLEDBACK) { |
Madan Jampani | bab51a4 | 2015-08-10 13:53:35 -0700 | [diff] [blame] | 80 | return transactions.remove(transaction.id()).thenApply(v -> CommitResponse.success(ImmutableList.of())); |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 81 | } else if (transaction.state() == Transaction.State.COMMITTING) { |
| 82 | return commit(transaction); |
| 83 | } else if (transaction.state() == Transaction.State.ROLLINGBACK) { |
Madan Jampani | bab51a4 | 2015-08-10 13:53:35 -0700 | [diff] [blame] | 84 | return rollback(transaction).thenApply(v -> CommitResponse.success(ImmutableList.of())); |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 85 | } else { |
| 86 | return prepare(transaction).thenCompose(v -> v ? commit(transaction) : rollback(transaction)); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | |
| 91 | /** |
| 92 | * Returns all transactions in the system. |
| 93 | * |
| 94 | * @return future for a collection of transactions |
| 95 | */ |
| 96 | public CompletableFuture<Collection<Transaction>> getTransactions() { |
| 97 | return transactions.values().thenApply(c -> { |
| 98 | Collection<Transaction> txns = c.stream().map(v -> v.value()).collect(Collectors.toList()); |
| 99 | return txns; |
| 100 | }); |
| 101 | } |
| 102 | |
| 103 | private CompletableFuture<Boolean> prepare(Transaction transaction) { |
| 104 | return transactions.put(transaction.id(), transaction) |
| 105 | .thenCompose(v -> database.prepare(transaction)) |
| 106 | .thenCompose(status -> transactions.put( |
| 107 | transaction.id(), |
| 108 | transaction.transition(status ? State.COMMITTING : State.ROLLINGBACK)) |
| 109 | .thenApply(v -> status)); |
| 110 | } |
| 111 | |
Madan Jampani | bab51a4 | 2015-08-10 13:53:35 -0700 | [diff] [blame] | 112 | private CompletableFuture<CommitResponse> commit(Transaction transaction) { |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 113 | return database.commit(transaction) |
Madan Jampani | bab51a4 | 2015-08-10 13:53:35 -0700 | [diff] [blame] | 114 | .whenComplete((r, e) -> transactions.put( |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 115 | transaction.id(), |
Madan Jampani | bab51a4 | 2015-08-10 13:53:35 -0700 | [diff] [blame] | 116 | transaction.transition(Transaction.State.COMMITTED))); |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Madan Jampani | bab51a4 | 2015-08-10 13:53:35 -0700 | [diff] [blame] | 119 | private CompletableFuture<CommitResponse> rollback(Transaction transaction) { |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 120 | return database.rollback(transaction) |
| 121 | .thenCompose(v -> transactions.put( |
| 122 | transaction.id(), |
| 123 | transaction.transition(Transaction.State.ROLLEDBACK))) |
Madan Jampani | bab51a4 | 2015-08-10 13:53:35 -0700 | [diff] [blame] | 124 | .thenApply(v -> CommitResponse.failure()); |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 125 | } |
Madan Jampani | 02b7fb8 | 2015-05-01 13:01:20 -0700 | [diff] [blame] | 126 | } |