Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 1 | /* |
| 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 Vachuska | b6d3167 | 2018-07-27 17:03:46 -0700 | [diff] [blame] | 16 | package org.onosproject.store.atomix.primitives.impl; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 17 | |
| 18 | import java.util.concurrent.CompletableFuture; |
| 19 | import java.util.concurrent.ExecutionException; |
| 20 | import java.util.concurrent.TimeUnit; |
| 21 | import java.util.concurrent.TimeoutException; |
| 22 | |
| 23 | import io.atomix.core.transaction.AsyncTransaction; |
| 24 | import io.atomix.primitive.Recovery; |
| 25 | import io.atomix.protocols.raft.MultiRaftProtocol; |
| 26 | import org.onosproject.store.primitives.TransactionId; |
| 27 | import org.onosproject.store.service.CommitStatus; |
| 28 | import org.onosproject.store.service.DistributedPrimitive; |
| 29 | import org.onosproject.store.service.Serializer; |
| 30 | import org.onosproject.store.service.StorageException; |
| 31 | import org.onosproject.store.service.TransactionContext; |
| 32 | import org.onosproject.store.service.TransactionalMap; |
| 33 | |
| 34 | /** |
| 35 | * Atomix transaction context. |
| 36 | */ |
| 37 | public 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 | } |