Madan Jampani
Committed by Gerrit Code Review

Support for reacting to underlying copycat client session state changes

Change-Id: If8af43f81963653da3584167d7a9813456ce3773
...@@ -18,9 +18,22 @@ package org.onosproject.store.primitives.impl; ...@@ -18,9 +18,22 @@ package org.onosproject.store.primitives.impl;
18 import static org.slf4j.LoggerFactory.getLogger; 18 import static org.slf4j.LoggerFactory.getLogger;
19 import io.atomix.Atomix; 19 import io.atomix.Atomix;
20 import io.atomix.AtomixClient; 20 import io.atomix.AtomixClient;
21 +import io.atomix.catalyst.transport.Address;
21 import io.atomix.catalyst.transport.Transport; 22 import io.atomix.catalyst.transport.Transport;
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;
22 import io.atomix.variables.DistributedLong; 34 import io.atomix.variables.DistributedLong;
23 35
36 +import java.util.Collection;
24 import java.util.Set; 37 import java.util.Set;
25 import java.util.concurrent.CompletableFuture; 38 import java.util.concurrent.CompletableFuture;
26 39
...@@ -53,6 +66,7 @@ public class StoragePartitionClient implements DistributedPrimitiveCreator, Mana ...@@ -53,6 +66,7 @@ public class StoragePartitionClient implements DistributedPrimitiveCreator, Mana
53 private final Transport transport; 66 private final Transport transport;
54 private final io.atomix.catalyst.serializer.Serializer serializer; 67 private final io.atomix.catalyst.serializer.Serializer serializer;
55 private Atomix client; 68 private Atomix client;
69 + private CopycatClient copycatClient;
56 private static final String ATOMIC_VALUES_CONSISTENT_MAP_NAME = "onos-atomic-values"; 70 private static final String ATOMIC_VALUES_CONSISTENT_MAP_NAME = "onos-atomic-values";
57 private final Supplier<AsyncConsistentMap<String, byte[]>> onosAtomicValuesMap = 71 private final Supplier<AsyncConsistentMap<String, byte[]>> onosAtomicValuesMap =
58 Suppliers.memoize(() -> newAsyncConsistentMap(ATOMIC_VALUES_CONSISTENT_MAP_NAME, 72 Suppliers.memoize(() -> newAsyncConsistentMap(ATOMIC_VALUES_CONSISTENT_MAP_NAME,
...@@ -72,11 +86,12 @@ public class StoragePartitionClient implements DistributedPrimitiveCreator, Mana ...@@ -72,11 +86,12 @@ public class StoragePartitionClient implements DistributedPrimitiveCreator, Mana
72 return CompletableFuture.completedFuture(null); 86 return CompletableFuture.completedFuture(null);
73 } 87 }
74 synchronized (StoragePartitionClient.this) { 88 synchronized (StoragePartitionClient.this) {
75 - client = AtomixClient.builder(partition.getMemberAddresses()) 89 + copycatClient = newCopycatClient(partition.getMemberAddresses(),
76 - .withResourceTypes(StoragePartition.RESOURCE_TYPES) 90 + transport,
77 - .withSerializer(serializer.clone()) 91 + serializer.clone(),
78 - .withTransport(transport) 92 + StoragePartition.RESOURCE_TYPES);
79 - .build(); 93 + copycatClient.onStateChange(state -> log.info("Client state {}", state));
94 + client = new AtomixClient(new ResourceClient(copycatClient));
80 } 95 }
81 return client.open().whenComplete((r, e) -> { 96 return client.open().whenComplete((r, e) -> {
82 if (e == null) { 97 if (e == null) {
...@@ -154,4 +169,30 @@ public class StoragePartitionClient implements DistributedPrimitiveCreator, Mana ...@@ -154,4 +169,30 @@ public class StoragePartitionClient implements DistributedPrimitiveCreator, Mana
154 public boolean isOpen() { 169 public boolean isOpen() {
155 return client.isOpen(); 170 return client.isOpen();
156 } 171 }
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 + }
157 } 198 }
......