blob: e9e75a595ae3bfe91a6f248ff34191506478f12b [file] [log] [blame]
Madan Jampani7e55c662016-02-15 21:13:53 -08001/*
2 * Copyright 2016 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 */
16package org.onosproject.store.primitives.impl;
17
18import java.util.Set;
Madan Jampani3780d4b2016-04-04 18:18:24 -070019import java.util.concurrent.CompletableFuture;
Madan Jampani7e55c662016-02-15 21:13:53 -080020import java.util.concurrent.atomic.AtomicBoolean;
21
22import org.onosproject.store.primitives.DistributedPrimitiveCreator;
23import org.onosproject.store.primitives.TransactionId;
Madan Jampani3780d4b2016-04-04 18:18:24 -070024import org.onosproject.store.service.CommitStatus;
Madan Jampani7e55c662016-02-15 21:13:53 -080025import org.onosproject.store.service.Serializer;
26import org.onosproject.store.service.TransactionContext;
27import org.onosproject.store.service.TransactionalMap;
Madan Jampani3780d4b2016-04-04 18:18:24 -070028import org.onosproject.utils.MeteringAgent;
Madan Jampani7e55c662016-02-15 21:13:53 -080029
30import com.google.common.collect.Sets;
31
32/**
33 * Default implementation of transaction context.
34 */
35public class NewDefaultTransactionContext implements TransactionContext {
36
37 private final AtomicBoolean isOpen = new AtomicBoolean(false);
38 private final DistributedPrimitiveCreator creator;
39 private final TransactionId transactionId;
40 private final TransactionCoordinator transactionCoordinator;
41 private final Set<TransactionParticipant> txParticipants = Sets.newConcurrentHashSet();
Madan Jampani3780d4b2016-04-04 18:18:24 -070042 private final MeteringAgent monitor;
Madan Jampani7e55c662016-02-15 21:13:53 -080043
44 public NewDefaultTransactionContext(TransactionId transactionId,
45 DistributedPrimitiveCreator creator,
46 TransactionCoordinator transactionCoordinator) {
47 this.transactionId = transactionId;
48 this.creator = creator;
49 this.transactionCoordinator = transactionCoordinator;
Madan Jampani3780d4b2016-04-04 18:18:24 -070050 this.monitor = new MeteringAgent("transactionContext", "*", true);
Madan Jampani7e55c662016-02-15 21:13:53 -080051 }
52
53 @Override
54 public String name() {
55 return transactionId.toString();
56 }
57
58 @Override
59 public TransactionId transactionId() {
60 return transactionId;
61 }
62
63 @Override
64 public boolean isOpen() {
65 return isOpen.get();
66 }
67
68 @Override
69 public void begin() {
70 if (!isOpen.compareAndSet(false, true)) {
71 throw new IllegalStateException("TransactionContext is already open");
72 }
73 }
74
75 @Override
Madan Jampani3780d4b2016-04-04 18:18:24 -070076 public CompletableFuture<CommitStatus> commit() {
77 final MeteringAgent.Context timer = monitor.startTimer("commit");
78 return transactionCoordinator.commit(transactionId, txParticipants)
79 .whenComplete((r, e) -> timer.stop(e));
Madan Jampani7e55c662016-02-15 21:13:53 -080080 }
81
82 @Override
83 public void abort() {
84 isOpen.set(false);
85 }
86
87 @Override
88 public <K, V> TransactionalMap<K, V> getTransactionalMap(String mapName,
89 Serializer serializer) {
90 // FIXME: Do not create duplicates.
91 DefaultTransactionalMap<K, V> txMap = new DefaultTransactionalMap<K, V>(mapName,
92 creator.<K, V>newAsyncConsistentMap(mapName, serializer),
93 this,
94 serializer);
95 txParticipants.add(txMap);
96 return txMap;
97 }
98}