blob: 5103348747f8c8e8f7295a8bec861e23af06556a [file] [log] [blame]
Madan Jampani64689552015-02-17 10:00:27 -08001/*
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
17package org.onosproject.store.consistent.impl;
18
19import java.util.List;
20import java.util.Map;
21import java.util.concurrent.CompletableFuture;
22import java.util.concurrent.ExecutionException;
23import java.util.concurrent.TimeUnit;
24import java.util.concurrent.TimeoutException;
25
26import static com.google.common.base.Preconditions.*;
27
28import org.onosproject.store.service.ConsistentMap;
29import org.onosproject.store.service.Serializer;
30import org.onosproject.store.service.TransactionContext;
31import org.onosproject.store.service.TransactionException;
32import org.onosproject.store.service.TransactionalMap;
33import org.onosproject.store.service.UpdateOperation;
34
35import com.google.common.collect.Lists;
36import com.google.common.collect.Maps;
37
38/**
39 * Default TransactionContext implementation.
40 */
41public 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
59 public <K, V> TransactionalMap<K, V> createTransactionalMap(String mapName,
60 Serializer serializer) {
61 checkNotNull(mapName, "map name is null");
62 checkNotNull(serializer, "serializer is null");
63 checkState(isOpen, TX_NOT_OPEN_ERROR);
64 if (!txMaps.containsKey(mapName)) {
65 ConsistentMap<K, V> backingMap = new ConsistentMapImpl<>(mapName, databaseProxy, serializer);
66 DefaultTransactionalMap<K, V> txMap = new DefaultTransactionalMap<>(mapName, backingMap, this, serializer);
67 txMaps.put(mapName, txMap);
68 }
69 return txMaps.get(mapName);
70 }
71
72 @Override
73 public void commit() {
74 checkState(isOpen, TX_NOT_OPEN_ERROR);
75 List<UpdateOperation<String, byte[]>> allUpdates =
76 Lists.newLinkedList();
77 try {
78 txMaps.values()
79 .stream()
80 .forEach(m -> {
81 allUpdates.addAll(m.prepareDatabaseUpdates());
82 });
83
84 if (!complete(databaseProxy.atomicBatchUpdate(allUpdates))) {
85 throw new TransactionException.OptimisticConcurrencyFailure();
86 }
87 } finally {
88 isOpen = false;
89 }
90 }
91
92 @Override
93 public void rollback() {
94 checkState(isOpen, TX_NOT_OPEN_ERROR);
95 txMaps.values()
96 .stream()
97 .forEach(m -> m.rollback());
98 }
99
100 @Override
101 public boolean isOpen() {
102 return false;
103 }
104
105 private static <T> T complete(CompletableFuture<T> future) {
106 try {
107 return future.get(TRANSACTION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
108 } catch (InterruptedException e) {
109 Thread.currentThread().interrupt();
110 throw new TransactionException.Interrupted();
111 } catch (TimeoutException e) {
112 throw new TransactionException.Timeout();
113 } catch (ExecutionException e) {
114 throw new TransactionException(e.getCause());
115 }
116 }
117}