Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [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 | |
| 17 | package org.onosproject.store.consistent.impl; |
| 18 | |
| 19 | import java.util.List; |
| 20 | import java.util.Map; |
| 21 | import java.util.concurrent.CompletableFuture; |
| 22 | import java.util.concurrent.ExecutionException; |
| 23 | import java.util.concurrent.TimeUnit; |
| 24 | import java.util.concurrent.TimeoutException; |
| 25 | |
| 26 | import static com.google.common.base.Preconditions.*; |
| 27 | |
| 28 | import org.onosproject.store.service.ConsistentMap; |
| 29 | import org.onosproject.store.service.Serializer; |
| 30 | import org.onosproject.store.service.TransactionContext; |
| 31 | import org.onosproject.store.service.TransactionException; |
| 32 | import org.onosproject.store.service.TransactionalMap; |
| 33 | import org.onosproject.store.service.UpdateOperation; |
| 34 | |
| 35 | import com.google.common.collect.Lists; |
| 36 | import com.google.common.collect.Maps; |
| 37 | |
| 38 | /** |
| 39 | * Default TransactionContext implementation. |
| 40 | */ |
| 41 | public class DefaultTransactionContext implements TransactionContext { |
| 42 | |
| 43 | private final Map<String, DefaultTransactionalMap> txMaps = Maps.newHashMap(); |
| 44 | private boolean isOpen = false; |
| 45 | DatabaseProxy<String, byte[]> databaseProxy; |
| 46 | private static final String TX_NOT_OPEN_ERROR = "Transaction is not open"; |
| 47 | private static final int TRANSACTION_TIMEOUT_MILLIS = 2000; |
| 48 | |
| 49 | DefaultTransactionContext(DatabaseProxy<String, byte[]> proxy) { |
| 50 | this.databaseProxy = proxy; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public void begin() { |
| 55 | isOpen = true; |
| 56 | } |
| 57 | |
| 58 | @Override |
Ray Milkey | 8dc8208 | 2015-02-20 16:22:38 -0800 | [diff] [blame] | 59 | @SuppressWarnings("unchecked") |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 60 | public <K, V> TransactionalMap<K, V> createTransactionalMap(String mapName, |
| 61 | Serializer serializer) { |
| 62 | checkNotNull(mapName, "map name is null"); |
| 63 | checkNotNull(serializer, "serializer is null"); |
| 64 | checkState(isOpen, TX_NOT_OPEN_ERROR); |
| 65 | if (!txMaps.containsKey(mapName)) { |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame^] | 66 | ConsistentMap<K, V> backingMap = new DefaultConsistentMap<>(mapName, databaseProxy, serializer); |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 67 | DefaultTransactionalMap<K, V> txMap = new DefaultTransactionalMap<>(mapName, backingMap, this, serializer); |
| 68 | txMaps.put(mapName, txMap); |
| 69 | } |
| 70 | return txMaps.get(mapName); |
| 71 | } |
| 72 | |
Ray Milkey | 8dc8208 | 2015-02-20 16:22:38 -0800 | [diff] [blame] | 73 | @SuppressWarnings("unchecked") |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 74 | @Override |
| 75 | public void commit() { |
| 76 | checkState(isOpen, TX_NOT_OPEN_ERROR); |
| 77 | List<UpdateOperation<String, byte[]>> allUpdates = |
| 78 | Lists.newLinkedList(); |
| 79 | try { |
| 80 | txMaps.values() |
| 81 | .stream() |
| 82 | .forEach(m -> { |
| 83 | allUpdates.addAll(m.prepareDatabaseUpdates()); |
| 84 | }); |
| 85 | |
| 86 | if (!complete(databaseProxy.atomicBatchUpdate(allUpdates))) { |
| 87 | throw new TransactionException.OptimisticConcurrencyFailure(); |
| 88 | } |
| 89 | } finally { |
| 90 | isOpen = false; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void rollback() { |
| 96 | checkState(isOpen, TX_NOT_OPEN_ERROR); |
| 97 | txMaps.values() |
| 98 | .stream() |
| 99 | .forEach(m -> m.rollback()); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public boolean isOpen() { |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | private static <T> T complete(CompletableFuture<T> future) { |
| 108 | try { |
| 109 | return future.get(TRANSACTION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); |
| 110 | } catch (InterruptedException e) { |
| 111 | Thread.currentThread().interrupt(); |
| 112 | throw new TransactionException.Interrupted(); |
| 113 | } catch (TimeoutException e) { |
| 114 | throw new TransactionException.Timeout(); |
| 115 | } catch (ExecutionException e) { |
| 116 | throw new TransactionException(e.getCause()); |
| 117 | } |
| 118 | } |
| 119 | } |