Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 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; |
Madan Jampani | 471a7bc | 2016-04-27 15:51:38 -0700 | [diff] [blame^] | 21 | import io.atomix.catalyst.transport.Address; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 22 | import io.atomix.catalyst.transport.Transport; |
Madan Jampani | 471a7bc | 2016-04-27 15:51:38 -0700 | [diff] [blame^] | 23 | import io.atomix.catalyst.util.concurrent.CatalystThreadFactory; |
| 24 | import io.atomix.copycat.client.ConnectionStrategies; |
| 25 | import io.atomix.copycat.client.CopycatClient; |
| 26 | import io.atomix.copycat.client.RecoveryStrategies; |
| 27 | import io.atomix.copycat.client.RetryStrategies; |
| 28 | import io.atomix.copycat.client.ServerSelectionStrategies; |
| 29 | import io.atomix.manager.ResourceClient; |
| 30 | import io.atomix.manager.state.ResourceManagerException; |
| 31 | import io.atomix.manager.util.ResourceManagerTypeResolver; |
| 32 | import io.atomix.resource.ResourceType; |
| 33 | import io.atomix.resource.util.ResourceRegistry; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 34 | import io.atomix.variables.DistributedLong; |
| 35 | |
Madan Jampani | 471a7bc | 2016-04-27 15:51:38 -0700 | [diff] [blame^] | 36 | import java.util.Collection; |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 37 | import java.util.Set; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 38 | import java.util.concurrent.CompletableFuture; |
| 39 | |
| 40 | import org.onlab.util.HexString; |
| 41 | import org.onosproject.store.primitives.DistributedPrimitiveCreator; |
| 42 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMap; |
| 43 | import org.onosproject.store.primitives.resources.impl.AtomixCounter; |
Madan Jampani | 39fff10 | 2016-02-14 13:17:28 -0800 | [diff] [blame] | 44 | import org.onosproject.store.primitives.resources.impl.AtomixLeaderElector; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 45 | import org.onosproject.store.serializers.KryoNamespaces; |
| 46 | import org.onosproject.store.service.AsyncAtomicCounter; |
| 47 | import org.onosproject.store.service.AsyncAtomicValue; |
| 48 | import org.onosproject.store.service.AsyncConsistentMap; |
| 49 | import org.onosproject.store.service.AsyncDistributedSet; |
| 50 | import org.onosproject.store.service.AsyncLeaderElector; |
| 51 | import org.onosproject.store.service.DistributedQueue; |
| 52 | import org.onosproject.store.service.Serializer; |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 53 | import org.slf4j.Logger; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 54 | |
| 55 | import com.google.common.base.Supplier; |
| 56 | import com.google.common.base.Suppliers; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * StoragePartition client. |
| 60 | */ |
| 61 | public class StoragePartitionClient implements DistributedPrimitiveCreator, Managed<StoragePartitionClient> { |
| 62 | |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 63 | private final Logger log = getLogger(getClass()); |
| 64 | |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 65 | private final StoragePartition partition; |
| 66 | private final Transport transport; |
| 67 | private final io.atomix.catalyst.serializer.Serializer serializer; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 68 | private Atomix client; |
Madan Jampani | 471a7bc | 2016-04-27 15:51:38 -0700 | [diff] [blame^] | 69 | private CopycatClient copycatClient; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 70 | private static final String ATOMIC_VALUES_CONSISTENT_MAP_NAME = "onos-atomic-values"; |
| 71 | private final Supplier<AsyncConsistentMap<String, byte[]>> onosAtomicValuesMap = |
| 72 | Suppliers.memoize(() -> newAsyncConsistentMap(ATOMIC_VALUES_CONSISTENT_MAP_NAME, |
| 73 | Serializer.using(KryoNamespaces.BASIC))); |
| 74 | |
| 75 | public StoragePartitionClient(StoragePartition partition, |
| 76 | io.atomix.catalyst.serializer.Serializer serializer, |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 77 | Transport transport) { |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 78 | this.partition = partition; |
| 79 | this.serializer = serializer; |
| 80 | this.transport = transport; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public CompletableFuture<Void> open() { |
| 85 | if (client != null && client.isOpen()) { |
| 86 | return CompletableFuture.completedFuture(null); |
| 87 | } |
| 88 | synchronized (StoragePartitionClient.this) { |
Madan Jampani | 471a7bc | 2016-04-27 15:51:38 -0700 | [diff] [blame^] | 89 | copycatClient = newCopycatClient(partition.getMemberAddresses(), |
| 90 | transport, |
| 91 | serializer.clone(), |
| 92 | StoragePartition.RESOURCE_TYPES); |
| 93 | copycatClient.onStateChange(state -> log.info("Client state {}", state)); |
| 94 | client = new AtomixClient(new ResourceClient(copycatClient)); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 95 | } |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 96 | return client.open().whenComplete((r, e) -> { |
| 97 | if (e == null) { |
| 98 | log.info("Successfully started client for partition {}", partition.getId()); |
| 99 | } else { |
| 100 | log.info("Failed to start client for partition {}", partition.getId(), e); |
| 101 | } |
| 102 | }).thenApply(v -> null); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public CompletableFuture<Void> close() { |
| 107 | return client != null ? client.close() : CompletableFuture.completedFuture(null); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public <K, V> AsyncConsistentMap<K, V> newAsyncConsistentMap(String name, Serializer serializer) { |
| 112 | AsyncConsistentMap<String, byte[]> rawMap = |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 113 | new DelegatingAsyncConsistentMap<String, byte[]>(client.getResource(name, AtomixConsistentMap.class) |
| 114 | .join()) { |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 115 | @Override |
| 116 | public String name() { |
| 117 | return name; |
| 118 | } |
| 119 | }; |
| 120 | AsyncConsistentMap<K, V> transcodedMap = DistributedPrimitives.<K, V, String, byte[]>newTranscodingMap(rawMap, |
| 121 | key -> HexString.toHexString(serializer.encode(key)), |
| 122 | string -> serializer.decode(HexString.fromHexString(string)), |
| 123 | value -> value == null ? null : serializer.encode(value), |
| 124 | bytes -> serializer.decode(bytes)); |
| 125 | |
Madan Jampani | 542d9e2 | 2016-04-05 15:39:55 -0700 | [diff] [blame] | 126 | return transcodedMap; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public <E> AsyncDistributedSet<E> newAsyncDistributedSet(String name, Serializer serializer) { |
| 131 | return DistributedPrimitives.newSetFromMap(this.<E, Boolean>newAsyncConsistentMap(name, serializer)); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public AsyncAtomicCounter newAsyncCounter(String name) { |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 136 | DistributedLong distributedLong = client.getLong(name).join(); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 137 | return new AtomixCounter(name, distributedLong); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public <V> AsyncAtomicValue<V> newAsyncAtomicValue(String name, Serializer serializer) { |
| 142 | return new DefaultAsyncAtomicValue<>(name, |
| 143 | serializer, |
| 144 | onosAtomicValuesMap.get()); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public <E> DistributedQueue<E> newDistributedQueue(String name, Serializer serializer) { |
| 149 | // TODO: Implement |
| 150 | throw new UnsupportedOperationException(); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public AsyncLeaderElector newAsyncLeaderElector(String name) { |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 155 | return client.getResource(name, AtomixLeaderElector.class).join(); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Override |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 159 | public Set<String> getAsyncConsistentMapNames() { |
| 160 | return client.keys(AtomixConsistentMap.class).join(); |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public Set<String> getAsyncAtomicCounterNames() { |
| 165 | return client.keys(DistributedLong.class).join(); |
| 166 | } |
| 167 | |
| 168 | @Override |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 169 | public boolean isOpen() { |
| 170 | return client.isOpen(); |
| 171 | } |
Madan Jampani | 471a7bc | 2016-04-27 15:51:38 -0700 | [diff] [blame^] | 172 | |
| 173 | private CopycatClient newCopycatClient(Collection<Address> members, |
| 174 | Transport transport, |
| 175 | io.atomix.catalyst.serializer.Serializer serializer, |
| 176 | Collection<ResourceType> resourceTypes) { |
| 177 | ResourceRegistry registry = new ResourceRegistry(); |
| 178 | resourceTypes.forEach(registry::register); |
| 179 | CopycatClient client = CopycatClient.builder(members) |
| 180 | .withServerSelectionStrategy(ServerSelectionStrategies.ANY) |
| 181 | .withConnectionStrategy(ConnectionStrategies.FIBONACCI_BACKOFF) |
| 182 | .withRecoveryStrategy(RecoveryStrategies.RECOVER) |
| 183 | .withRetryStrategy(RetryStrategies.FIBONACCI_BACKOFF) |
| 184 | .withTransport(transport) |
| 185 | .withSerializer(serializer) |
| 186 | .withThreadFactory(new CatalystThreadFactory(String.format("copycat-client-%s", partition.getId()))) |
| 187 | .build(); |
| 188 | client.serializer().resolve(new ResourceManagerTypeResolver()); |
| 189 | for (ResourceType type : registry.types()) { |
| 190 | try { |
| 191 | type.factory().newInstance().createSerializableTypeResolver().resolve(client.serializer().registry()); |
| 192 | } catch (InstantiationException | IllegalAccessException e) { |
| 193 | throw new ResourceManagerException(e); |
| 194 | } |
| 195 | } |
| 196 | return client; |
| 197 | } |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 198 | } |