blob: 9f8c5bd6878f6672311d9401dec0a1bb9a4bf5e2 [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
Ray Milkey8dc82082015-02-20 16:22:38 -080059 @SuppressWarnings("unchecked")
Madan Jampani64689552015-02-17 10:00:27 -080060 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 Jampani7c521002015-03-23 12:23:01 -070066 ConsistentMap<K, V> backingMap = new DefaultConsistentMap<>(mapName, databaseProxy, serializer);
Madan Jampani64689552015-02-17 10:00:27 -080067 DefaultTransactionalMap<K, V> txMap = new DefaultTransactionalMap<>(mapName, backingMap, this, serializer);
68 txMaps.put(mapName, txMap);
69 }
70 return txMaps.get(mapName);
71 }
72
Ray Milkey8dc82082015-02-20 16:22:38 -080073 @SuppressWarnings("unchecked")
Madan Jampani64689552015-02-17 10:00:27 -080074 @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}