use of io.atomix.primitive.session.SessionClient in project atomix by atomix.
the class RecoveringSessionClient method close.
private CompletableFuture<Void> close(Function<SessionClient, CompletableFuture<Void>> closeFunction) {
if (closeFuture == null) {
synchronized (this) {
if (closeFuture == null) {
connected = false;
SessionClient session = this.session;
if (session != null) {
closeFuture = closeFunction.apply(session);
} else if (closeFuture != null) {
closeFuture = connectFuture.thenCompose(closeFunction);
} else {
closeFuture = CompletableFuture.completedFuture(null);
}
}
}
}
return closeFuture;
}
use of io.atomix.primitive.session.SessionClient in project atomix by atomix.
the class PrimaryBackupClient method sessionBuilder.
/**
* Creates a new primary backup proxy session builder.
*
* @param primitiveName the primitive name
* @param primitiveType the primitive type
* @param serviceConfig the service configuration
* @return a new primary-backup proxy session builder
*/
public PrimaryBackupSessionClient.Builder sessionBuilder(String primitiveName, PrimitiveType primitiveType, ServiceConfig serviceConfig) {
byte[] configBytes = Serializer.using(primitiveType.namespace()).encode(serviceConfig);
return new PrimaryBackupSessionClient.Builder() {
@Override
public SessionClient build() {
Supplier<CompletableFuture<SessionClient>> proxyBuilder = () -> sessionIdService.nextSessionId().thenApply(sessionId -> new PrimaryBackupSessionClient(clientName, partitionId, sessionId, primitiveType, new PrimitiveDescriptor(primitiveName, primitiveType.name(), configBytes, numBackups, replication), clusterMembershipService, PrimaryBackupClient.this.protocol, primaryElection, threadContextFactory.createContext()));
SessionClient proxy;
ThreadContext context = threadContextFactory.createContext();
if (recovery == Recovery.RECOVER) {
proxy = new RecoveringSessionClient(clientName, partitionId, primitiveName, primitiveType, proxyBuilder, context);
} else {
proxy = Futures.get(proxyBuilder.get());
}
// If max retries is set, wrap the client in a retrying proxy client.
if (maxRetries > 0) {
proxy = new RetryingSessionClient(proxy, context, maxRetries, retryDelay);
}
return new BlockingAwareSessionClient(proxy, context);
}
};
}
use of io.atomix.primitive.session.SessionClient in project atomix by atomix.
the class PrimaryBackupTest method testManyEvents.
/**
* Tests submitting a linearizable event that publishes to all sessions.
*/
private void testManyEvents(int nodes, int backups, Replication replication) throws Throwable {
createServers(nodes);
PrimaryBackupClient client = createClient();
SessionClient session = createProxy(client, backups, replication);
session.addEventListener(CHANGE_EVENT, message -> {
threadAssertNotNull(message);
resume();
});
for (int i = 0; i < 10; i++) {
session.execute(operation(EVENT, SERIALIZER.encode(true))).thenRun(this::resume);
await(5000, 2);
}
}
use of io.atomix.primitive.session.SessionClient in project atomix by atomix.
the class PrimaryBackupTest method testManySessionsManyEvents.
/**
* Tests submitting a linearizable event that publishes to all sessions.
*/
private void testManySessionsManyEvents(Replication replication) throws Throwable {
createServers(3);
PrimaryBackupClient client = createClient();
SessionClient session = createProxy(client, 2, replication);
session.addEventListener(CHANGE_EVENT, event -> {
threadAssertNotNull(event);
resume();
});
SessionClient session1 = createProxy(createClient(), 2, replication);
session1.execute(operation(READ)).thenRun(this::resume);
session1.addEventListener(CHANGE_EVENT, event -> {
threadAssertNotNull(event);
resume();
});
SessionClient session2 = createProxy(createClient(), 2, replication);
session2.execute(operation(READ)).thenRun(this::resume);
session2.addEventListener(CHANGE_EVENT, event -> {
threadAssertNotNull(event);
resume();
});
await(5000, 2);
for (int i = 0; i < 10; i++) {
session.execute(operation(EVENT, SERIALIZER.encode(false))).thenRun(this::resume);
await(10000, 4);
}
}
use of io.atomix.primitive.session.SessionClient in project atomix by atomix.
the class MultiPrimaryProtocol method newProxy.
@Override
public <S> ProxyClient<S> newProxy(String primitiveName, PrimitiveType primitiveType, Class<S> serviceType, ServiceConfig serviceConfig, PartitionService partitionService) {
PartitionGroup partitionGroup = partitionService.getPartitionGroup(this);
if (partitionGroup == null) {
throw new ConfigurationException("No Raft partition group matching the configured protocol exists");
}
Collection<SessionClient> partitions = partitionGroup.getPartitions().stream().map(partition -> ((PrimaryBackupPartition) partition).getClient().sessionBuilder(primitiveName, primitiveType, serviceConfig).withConsistency(config.getConsistency()).withReplication(config.getReplication()).withRecovery(config.getRecovery()).withNumBackups(config.getBackups()).withMaxRetries(config.getMaxRetries()).withRetryDelay(config.getRetryDelay()).build()).collect(Collectors.toList());
return new DefaultProxyClient<>(primitiveName, primitiveType, this, serviceType, partitions, config.getPartitioner());
}
Aggregations