blob: 854aa3c3e809b48b875590a83093272322d2424c [file] [log] [blame]
Madan Jampani15b8ef52016-02-02 17:35:05 -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
Madan Jampani2f9cc712016-02-15 19:36:21 -080018import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani15b8ef52016-02-02 17:35:05 -080019import io.atomix.Atomix;
20import io.atomix.AtomixClient;
21import io.atomix.catalyst.transport.Transport;
22import io.atomix.resource.ResourceType;
23import io.atomix.variables.DistributedLong;
24
25import java.util.Collection;
Madan Jampanie14a09c2016-02-11 10:43:21 -080026import java.util.Set;
Madan Jampani15b8ef52016-02-02 17:35:05 -080027import java.util.concurrent.CompletableFuture;
28
29import org.onlab.util.HexString;
30import org.onosproject.store.primitives.DistributedPrimitiveCreator;
31import org.onosproject.store.primitives.resources.impl.AtomixConsistentMap;
32import org.onosproject.store.primitives.resources.impl.AtomixCounter;
Madan Jampani39fff102016-02-14 13:17:28 -080033import org.onosproject.store.primitives.resources.impl.AtomixLeaderElector;
Madan Jampani15b8ef52016-02-02 17:35:05 -080034import org.onosproject.store.serializers.KryoNamespaces;
35import org.onosproject.store.service.AsyncAtomicCounter;
36import org.onosproject.store.service.AsyncAtomicValue;
37import org.onosproject.store.service.AsyncConsistentMap;
38import org.onosproject.store.service.AsyncDistributedSet;
39import org.onosproject.store.service.AsyncLeaderElector;
40import org.onosproject.store.service.DistributedQueue;
41import org.onosproject.store.service.Serializer;
Madan Jampani2f9cc712016-02-15 19:36:21 -080042import org.slf4j.Logger;
Madan Jampani15b8ef52016-02-02 17:35:05 -080043
44import com.google.common.base.Supplier;
45import com.google.common.base.Suppliers;
46import com.google.common.collect.ImmutableSet;
47
48/**
49 * StoragePartition client.
50 */
51public class StoragePartitionClient implements DistributedPrimitiveCreator, Managed<StoragePartitionClient> {
52
Madan Jampani2f9cc712016-02-15 19:36:21 -080053 private final Logger log = getLogger(getClass());
54
Madan Jampani15b8ef52016-02-02 17:35:05 -080055 private final StoragePartition partition;
56 private final Transport transport;
57 private final io.atomix.catalyst.serializer.Serializer serializer;
58 private final Collection<ResourceType> resourceTypes;
59 private Atomix client;
60 private static final String ATOMIC_VALUES_CONSISTENT_MAP_NAME = "onos-atomic-values";
61 private final Supplier<AsyncConsistentMap<String, byte[]>> onosAtomicValuesMap =
62 Suppliers.memoize(() -> newAsyncConsistentMap(ATOMIC_VALUES_CONSISTENT_MAP_NAME,
63 Serializer.using(KryoNamespaces.BASIC)));
64
65 public StoragePartitionClient(StoragePartition partition,
66 io.atomix.catalyst.serializer.Serializer serializer,
67 Transport transport,
68 Collection<ResourceType> resourceTypes) {
69 this.partition = partition;
70 this.serializer = serializer;
71 this.transport = transport;
72 this.resourceTypes = ImmutableSet.copyOf(resourceTypes);
73 }
74
75 @Override
76 public CompletableFuture<Void> open() {
77 if (client != null && client.isOpen()) {
78 return CompletableFuture.completedFuture(null);
79 }
80 synchronized (StoragePartitionClient.this) {
81 client = AtomixClient.builder(partition.getMemberAddresses())
82 .withSerializer(serializer.clone())
83 .withResourceResolver(r -> {
84 resourceTypes.forEach(r::register);
85 })
86 .withTransport(transport)
87 .build();
88 }
Madan Jampani2f9cc712016-02-15 19:36:21 -080089 return client.open().whenComplete((r, e) -> {
90 if (e == null) {
91 log.info("Successfully started client for partition {}", partition.getId());
92 } else {
93 log.info("Failed to start client for partition {}", partition.getId(), e);
94 }
95 }).thenApply(v -> null);
Madan Jampani15b8ef52016-02-02 17:35:05 -080096 }
97
98 @Override
99 public CompletableFuture<Void> close() {
100 return client != null ? client.close() : CompletableFuture.completedFuture(null);
101 }
102
103 @Override
104 public <K, V> AsyncConsistentMap<K, V> newAsyncConsistentMap(String name, Serializer serializer) {
105 AsyncConsistentMap<String, byte[]> rawMap =
106 new DelegatingAsyncConsistentMap<String, byte[]>(client.get(name, AtomixConsistentMap.class).join()) {
107 @Override
108 public String name() {
109 return name;
110 }
111 };
112 AsyncConsistentMap<K, V> transcodedMap = DistributedPrimitives.<K, V, String, byte[]>newTranscodingMap(rawMap,
113 key -> HexString.toHexString(serializer.encode(key)),
114 string -> serializer.decode(HexString.fromHexString(string)),
115 value -> value == null ? null : serializer.encode(value),
116 bytes -> serializer.decode(bytes));
117
118 return DistributedPrimitives.newCachingMap(transcodedMap);
119 }
120
121 @Override
122 public <E> AsyncDistributedSet<E> newAsyncDistributedSet(String name, Serializer serializer) {
123 return DistributedPrimitives.newSetFromMap(this.<E, Boolean>newAsyncConsistentMap(name, serializer));
124 }
125
126 @Override
127 public AsyncAtomicCounter newAsyncCounter(String name) {
128 DistributedLong distributedLong = client.get(name, DistributedLong.class).join();
129 return new AtomixCounter(name, distributedLong);
130 }
131
132 @Override
133 public <V> AsyncAtomicValue<V> newAsyncAtomicValue(String name, Serializer serializer) {
134 return new DefaultAsyncAtomicValue<>(name,
135 serializer,
136 onosAtomicValuesMap.get());
137 }
138
139 @Override
140 public <E> DistributedQueue<E> newDistributedQueue(String name, Serializer serializer) {
141 // TODO: Implement
142 throw new UnsupportedOperationException();
143 }
144
145 @Override
146 public AsyncLeaderElector newAsyncLeaderElector(String name) {
Madan Jampani39fff102016-02-14 13:17:28 -0800147 return client.get(name, AtomixLeaderElector.class).join();
Madan Jampani15b8ef52016-02-02 17:35:05 -0800148 }
149
150 @Override
Madan Jampanie14a09c2016-02-11 10:43:21 -0800151 public Set<String> getAsyncConsistentMapNames() {
152 return client.keys(AtomixConsistentMap.class).join();
153 }
154
155 @Override
156 public Set<String> getAsyncAtomicCounterNames() {
157 return client.keys(DistributedLong.class).join();
158 }
159
160 @Override
Madan Jampani15b8ef52016-02-02 17:35:05 -0800161 public boolean isOpen() {
162 return client.isOpen();
163 }
164
165 @Override
166 public boolean isClosed() {
167 return client.isClosed();
168 }
Madan Jampani2f9cc712016-02-15 19:36:21 -0800169}