Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.store.primitives.impl; |
| 17 | |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 18 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 19 | import io.atomix.Atomix; |
| 20 | import io.atomix.AtomixClient; |
| 21 | import io.atomix.catalyst.transport.Transport; |
| 22 | import io.atomix.resource.ResourceType; |
| 23 | import io.atomix.variables.DistributedLong; |
| 24 | |
| 25 | import java.util.Collection; |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 26 | import java.util.Set; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 27 | import java.util.concurrent.CompletableFuture; |
| 28 | |
| 29 | import org.onlab.util.HexString; |
| 30 | import org.onosproject.store.primitives.DistributedPrimitiveCreator; |
| 31 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMap; |
| 32 | import org.onosproject.store.primitives.resources.impl.AtomixCounter; |
Madan Jampani | 39fff10 | 2016-02-14 13:17:28 -0800 | [diff] [blame] | 33 | import org.onosproject.store.primitives.resources.impl.AtomixLeaderElector; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 34 | import org.onosproject.store.serializers.KryoNamespaces; |
| 35 | import org.onosproject.store.service.AsyncAtomicCounter; |
| 36 | import org.onosproject.store.service.AsyncAtomicValue; |
| 37 | import org.onosproject.store.service.AsyncConsistentMap; |
| 38 | import org.onosproject.store.service.AsyncDistributedSet; |
| 39 | import org.onosproject.store.service.AsyncLeaderElector; |
| 40 | import org.onosproject.store.service.DistributedQueue; |
| 41 | import org.onosproject.store.service.Serializer; |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 42 | import org.slf4j.Logger; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 43 | |
| 44 | import com.google.common.base.Supplier; |
| 45 | import com.google.common.base.Suppliers; |
| 46 | import com.google.common.collect.ImmutableSet; |
| 47 | |
| 48 | /** |
| 49 | * StoragePartition client. |
| 50 | */ |
| 51 | public class StoragePartitionClient implements DistributedPrimitiveCreator, Managed<StoragePartitionClient> { |
| 52 | |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 53 | private final Logger log = getLogger(getClass()); |
| 54 | |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 55 | 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 Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 89 | 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 Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 96 | } |
| 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 Jampani | 39fff10 | 2016-02-14 13:17:28 -0800 | [diff] [blame] | 147 | return client.get(name, AtomixLeaderElector.class).join(); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Override |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 151 | 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 Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 161 | public boolean isOpen() { |
| 162 | return client.isOpen(); |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public boolean isClosed() { |
| 167 | return client.isClosed(); |
| 168 | } |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 169 | } |