Search in sources :

Example 11 with SessionClient

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;
}
Also used : SessionClient(io.atomix.primitive.session.SessionClient)

Example 12 with SessionClient

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);
        }
    };
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RecoveringSessionClient(io.atomix.primitive.session.impl.RecoveringSessionClient) BlockingAwareSessionClient(io.atomix.primitive.session.impl.BlockingAwareSessionClient) BlockingAwareSessionClient(io.atomix.primitive.session.impl.BlockingAwareSessionClient) RetryingSessionClient(io.atomix.primitive.session.impl.RetryingSessionClient) PrimaryBackupSessionClient(io.atomix.protocols.backup.session.PrimaryBackupSessionClient) RecoveringSessionClient(io.atomix.primitive.session.impl.RecoveringSessionClient) SessionClient(io.atomix.primitive.session.SessionClient) ThreadContext(io.atomix.utils.concurrent.ThreadContext) PrimaryBackupSessionClient(io.atomix.protocols.backup.session.PrimaryBackupSessionClient) PrimitiveDescriptor(io.atomix.protocols.backup.protocol.PrimitiveDescriptor) RetryingSessionClient(io.atomix.primitive.session.impl.RetryingSessionClient)

Example 13 with SessionClient

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);
    }
}
Also used : SessionClient(io.atomix.primitive.session.SessionClient)

Example 14 with SessionClient

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);
    }
}
Also used : SessionClient(io.atomix.primitive.session.SessionClient)

Example 15 with SessionClient

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());
}
Also used : PartitionGroup(io.atomix.primitive.partition.PartitionGroup) ProxyProtocol(io.atomix.primitive.protocol.ProxyProtocol) ProxyClient(io.atomix.primitive.proxy.ProxyClient) PrimaryBackupPartition(io.atomix.protocols.backup.partition.PrimaryBackupPartition) Collection(java.util.Collection) PartitionGroup(io.atomix.primitive.partition.PartitionGroup) Collectors(java.util.stream.Collectors) ConfigurationException(io.atomix.utils.config.ConfigurationException) ServiceConfig(io.atomix.primitive.service.ServiceConfig) PrimitiveType(io.atomix.primitive.PrimitiveType) PartitionService(io.atomix.primitive.partition.PartitionService) PrimitiveProtocol(io.atomix.primitive.protocol.PrimitiveProtocol) DefaultProxyClient(io.atomix.primitive.proxy.impl.DefaultProxyClient) SessionClient(io.atomix.primitive.session.SessionClient) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) DefaultProxyClient(io.atomix.primitive.proxy.impl.DefaultProxyClient) ConfigurationException(io.atomix.utils.config.ConfigurationException) SessionClient(io.atomix.primitive.session.SessionClient) PrimaryBackupPartition(io.atomix.protocols.backup.partition.PrimaryBackupPartition)

Aggregations

SessionClient (io.atomix.primitive.session.SessionClient)19 PrimitiveType (io.atomix.primitive.PrimitiveType)3 DefaultProxyClient (io.atomix.primitive.proxy.impl.DefaultProxyClient)3 ThreadContext (io.atomix.utils.concurrent.ThreadContext)3 PartitionGroup (io.atomix.primitive.partition.PartitionGroup)2 PartitionService (io.atomix.primitive.partition.PartitionService)2 PrimitiveProtocol (io.atomix.primitive.protocol.PrimitiveProtocol)2 ProxyProtocol (io.atomix.primitive.protocol.ProxyProtocol)2 ProxyClient (io.atomix.primitive.proxy.ProxyClient)2 ServiceConfig (io.atomix.primitive.service.ServiceConfig)2 BlockingAwareSessionClient (io.atomix.primitive.session.impl.BlockingAwareSessionClient)2 RecoveringSessionClient (io.atomix.primitive.session.impl.RecoveringSessionClient)2 RetryingSessionClient (io.atomix.primitive.session.impl.RetryingSessionClient)2 ConfigurationException (io.atomix.utils.config.ConfigurationException)2 Collection (java.util.Collection)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Collectors (java.util.stream.Collectors)2 MoreObjects.toStringHelper (com.google.common.base.MoreObjects.toStringHelper)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1