blob: 6be5be0d473bc1a4c41930be2e5dbceb83970492 [file] [log] [blame]
Jordan Halterman00e92da2018-05-22 23:05:52 -07001/*
2 * Copyright 2018-present Open Networking Foundation
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 */
Thomas Vachuskab6d31672018-07-27 17:03:46 -070016package org.onosproject.store.atomix.primitives.impl;
Jordan Halterman00e92da2018-05-22 23:05:52 -070017
18import java.util.concurrent.CompletableFuture;
19import java.util.concurrent.ExecutionException;
20import java.util.concurrent.TimeUnit;
21import java.util.concurrent.TimeoutException;
22
23import io.atomix.core.transaction.AsyncTransaction;
24import io.atomix.primitive.Recovery;
25import io.atomix.protocols.raft.MultiRaftProtocol;
26import org.onosproject.store.primitives.TransactionId;
27import org.onosproject.store.service.CommitStatus;
28import org.onosproject.store.service.DistributedPrimitive;
29import org.onosproject.store.service.Serializer;
30import org.onosproject.store.service.StorageException;
31import org.onosproject.store.service.TransactionContext;
32import org.onosproject.store.service.TransactionalMap;
33
34/**
35 * Atomix transaction context.
36 */
37public class AtomixTransactionContext implements TransactionContext {
38 private static final int MAX_RETRIES = 5;
39
40 private final AsyncTransaction atomixTransaction;
41 private final String group;
42
43 public AtomixTransactionContext(AsyncTransaction atomixTransaction, String group) {
44 this.atomixTransaction = atomixTransaction;
45 this.group = group;
46 }
47
48 @Override
49 public String name() {
50 return atomixTransaction.name();
51 }
52
53 @Override
54 public TransactionId transactionId() {
55 return TransactionId.from(atomixTransaction.transactionId().id());
56 }
57
58 @Override
59 public boolean isOpen() {
60 return atomixTransaction.isOpen();
61 }
62
63 @Override
64 public void begin() {
65 try {
66 atomixTransaction.begin().get(DistributedPrimitive.DEFAULT_OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
67 } catch (InterruptedException e) {
68 throw new StorageException.Interrupted();
69 } catch (TimeoutException e) {
70 throw new StorageException.Timeout();
71 } catch (ExecutionException e) {
72 throw new StorageException.Unavailable();
73 }
74 }
75
76 @Override
77 public CompletableFuture<CommitStatus> commit() {
78 return atomixTransaction.commit().thenApply(status -> CommitStatus.valueOf(status.name()));
79 }
80
81 @Override
82 public void abort() {
83 try {
84 atomixTransaction.abort().get(DistributedPrimitive.DEFAULT_OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
85 } catch (InterruptedException e) {
86 throw new StorageException.Interrupted();
87 } catch (TimeoutException e) {
88 throw new StorageException.Timeout();
89 } catch (ExecutionException e) {
90 throw new StorageException.Unavailable();
91 }
92 }
93
94 @Override
95 public <K, V> TransactionalMap<K, V> getTransactionalMap(String mapName, Serializer serializer) {
96 return new AtomixTransactionalMap<>(atomixTransaction.<K, V>mapBuilder(mapName)
97 .withProtocol(MultiRaftProtocol.builder(group)
98 .withRecoveryStrategy(Recovery.RECOVER)
99 .withMaxRetries(MAX_RETRIES)
100 .build())
101 .withSerializer(new AtomixSerializerAdapter(serializer))
102 .build());
103 }
104}