Search in sources :

Example 1 with ConfigurationException

use of io.atomix.utils.config.ConfigurationException in project atomix by atomix.

the class MultiRaftProtocol 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 -> ((RaftPartition) partition).getClient().sessionBuilder(primitiveName, primitiveType, serviceConfig).withMinTimeout(config.getMinTimeout()).withMaxTimeout(config.getMaxTimeout()).withReadConsistency(config.getReadConsistency()).withCommunicationStrategy(config.getCommunicationStrategy()).withRecoveryStrategy(config.getRecoveryStrategy()).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) Collection(java.util.Collection) RaftPartition(io.atomix.protocols.raft.partition.RaftPartition) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) 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) DefaultProxyClient(io.atomix.primitive.proxy.impl.DefaultProxyClient) ConfigurationException(io.atomix.utils.config.ConfigurationException) SessionClient(io.atomix.primitive.session.SessionClient)

Example 2 with ConfigurationException

use of io.atomix.utils.config.ConfigurationException in project atomix by atomix.

the class RaftServiceContext method installSnapshot.

/**
 * Installs a snapshot.
 */
public void installSnapshot(SnapshotReader reader) {
    log.debug("Installing snapshot {}", reader.snapshot().index());
    // Skip the service ID
    reader.skip(Bytes.LONG);
    PrimitiveType primitiveType;
    try {
        primitiveType = raft.getPrimitiveTypes().getPrimitiveType(reader.readString());
    } catch (ConfigurationException e) {
        log.error(e.getMessage(), e);
        return;
    }
    String serviceName = reader.readString();
    currentIndex = reader.readLong();
    currentTimestamp = reader.readLong();
    timestampDelta = reader.readLong();
    int sessionCount = reader.readInt();
    for (int i = 0; i < sessionCount; i++) {
        SessionId sessionId = SessionId.from(reader.readLong());
        MemberId node = MemberId.from(reader.readString());
        ReadConsistency readConsistency = ReadConsistency.valueOf(reader.readString());
        long minTimeout = reader.readLong();
        long maxTimeout = reader.readLong();
        long sessionTimestamp = reader.readLong();
        // Only create a new session if one does not already exist. This is necessary to ensure only a single session
        // is ever opened and exposed to the state machine.
        RaftSession session = raft.getSessions().addSession(new RaftSession(sessionId, node, serviceName, primitiveType, readConsistency, minTimeout, maxTimeout, sessionTimestamp, service.serializer(), this, raft, threadContextFactory));
        session.setRequestSequence(reader.readLong());
        session.setCommandSequence(reader.readLong());
        session.setEventIndex(reader.readLong());
        session.setLastCompleted(reader.readLong());
        session.setLastApplied(reader.snapshot().index());
        session.setLastUpdated(sessionTimestamp);
        session.open();
        service.register(sessions.addSession(session));
    }
    service.restore(new DefaultBackupInput(reader, service.serializer()));
}
Also used : ReadConsistency(io.atomix.protocols.raft.ReadConsistency) MemberId(io.atomix.cluster.MemberId) RaftSession(io.atomix.protocols.raft.session.RaftSession) ConfigurationException(io.atomix.utils.config.ConfigurationException) PrimitiveType(io.atomix.primitive.PrimitiveType) SessionId(io.atomix.primitive.session.SessionId) DefaultBackupInput(io.atomix.primitive.service.impl.DefaultBackupInput)

Example 3 with ConfigurationException

use of io.atomix.utils.config.ConfigurationException in project atomix by atomix.

the class RaftServiceManager method installService.

/**
 * Restores the service associated with the given snapshot.
 *
 * @param reader the snapshot reader
 */
private void installService(SnapshotReader reader) {
    PrimitiveId primitiveId = PrimitiveId.from(reader.readLong());
    try {
        PrimitiveType primitiveType = raft.getPrimitiveTypes().getPrimitiveType(reader.readString());
        String serviceName = reader.readString();
        byte[] serviceConfig = reader.readBytes(reader.readInt());
        // Get or create the service associated with the snapshot.
        logger.debug("Installing service {} {}", primitiveId, serviceName);
        RaftServiceContext service = initializeService(primitiveId, primitiveType, serviceName, serviceConfig);
        if (service != null) {
            try {
                service.installSnapshot(reader);
            } catch (Exception e) {
                logger.error("Failed to install snapshot for service {}", serviceName, e);
            }
        }
    } catch (ConfigurationException e) {
        logger.error(e.getMessage(), e);
    }
}
Also used : ConfigurationException(io.atomix.utils.config.ConfigurationException) RaftServiceContext(io.atomix.protocols.raft.service.RaftServiceContext) PrimitiveType(io.atomix.primitive.PrimitiveType) AtomixIOException(io.atomix.utils.AtomixIOException) ConfigurationException(io.atomix.utils.config.ConfigurationException) RaftException(io.atomix.protocols.raft.RaftException) PrimitiveId(io.atomix.primitive.PrimitiveId)

Example 4 with ConfigurationException

use of io.atomix.utils.config.ConfigurationException in project atomix by atomix.

the class DefaultPartitionService method start.

@Override
@SuppressWarnings("unchecked")
public CompletableFuture<PartitionService> start() {
    groupMembershipService.addListener(groupMembershipEventListener);
    return groupMembershipService.start().thenCompose(v -> {
        PartitionGroupMembership systemGroupMembership = groupMembershipService.getSystemMembership();
        if (systemGroupMembership != null) {
            if (systemGroup == null) {
                systemGroup = ((PartitionGroup.Type) systemGroupMembership.config().getType()).newPartitionGroup(systemGroupMembership.config());
            }
            systemElectionService = new DefaultPrimaryElectionService(systemGroup);
            systemSessionIdService = new ReplicatedSessionIdService(systemGroup);
            electionService = new HashBasedPrimaryElectionService(clusterMembershipService, groupMembershipService, communicationService);
            return electionService.start().thenCompose(s -> {
                PartitionManagementService managementService = new DefaultPartitionManagementService(clusterMembershipService, communicationService, primitiveTypeRegistry, electionService, new DefaultSessionIdService());
                if (systemGroupMembership.members().contains(clusterMembershipService.getLocalMember().id())) {
                    return systemGroup.join(managementService);
                } else {
                    return systemGroup.connect(managementService);
                }
            });
        } else {
            return Futures.exceptionalFuture(new ConfigurationException("No system partition group found"));
        }
    }).thenCompose(v -> systemElectionService.start().thenCompose(v2 -> systemSessionIdService.start()).thenApply(v2 -> new DefaultPartitionManagementService(clusterMembershipService, communicationService, primitiveTypeRegistry, systemElectionService, systemSessionIdService))).thenCompose(managementService -> {
        this.partitionManagementService = (PartitionManagementService) managementService;
        List<CompletableFuture> futures = groupMembershipService.getMemberships().stream().map(membership -> {
            ManagedPartitionGroup group;
            synchronized (groups) {
                group = groups.get(membership.group());
                if (group == null) {
                    group = ((PartitionGroup.Type) membership.config().getType()).newPartitionGroup(membership.config());
                    groups.put(group.name(), group);
                }
            }
            if (membership.members().contains(clusterMembershipService.getLocalMember().id())) {
                return group.join(partitionManagementService);
            } else {
                return group.connect(partitionManagementService);
            }
        }).collect(Collectors.toList());
        return CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])).thenApply(v -> {
            LOGGER.info("Started");
            started.set(true);
            return this;
        });
    });
}
Also used : ManagedPartitionGroup(io.atomix.primitive.partition.ManagedPartitionGroup) PartitionGroup(io.atomix.primitive.partition.PartitionGroup) ManagedPrimaryElectionService(io.atomix.primitive.partition.ManagedPrimaryElectionService) PartitionGroupMembershipEventListener(io.atomix.primitive.partition.PartitionGroupMembershipEventListener) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PrimitiveTypeRegistry(io.atomix.primitive.PrimitiveTypeRegistry) ManagedPartitionService(io.atomix.primitive.partition.ManagedPartitionService) DefaultSessionIdService(io.atomix.primitive.session.impl.DefaultSessionIdService) CompletableFuture(java.util.concurrent.CompletableFuture) ConfigurationException(io.atomix.utils.config.ConfigurationException) ClusterMembershipService(io.atomix.cluster.ClusterMembershipService) PartitionGroupTypeRegistry(io.atomix.primitive.partition.PartitionGroupTypeRegistry) Map(java.util.Map) ClusterCommunicationService(io.atomix.cluster.messaging.ClusterCommunicationService) ManagedPartitionGroup(io.atomix.primitive.partition.ManagedPartitionGroup) Futures(io.atomix.utils.concurrent.Futures) Logger(org.slf4j.Logger) ReplicatedSessionIdService(io.atomix.primitive.session.impl.ReplicatedSessionIdService) ManagedSessionIdService(io.atomix.primitive.session.ManagedSessionIdService) Collection(java.util.Collection) ManagedPartitionGroupMembershipService(io.atomix.primitive.partition.ManagedPartitionGroupMembershipService) Maps(com.google.common.collect.Maps) PartitionGroup(io.atomix.primitive.partition.PartitionGroup) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) PartitionManagementService(io.atomix.primitive.partition.PartitionManagementService) PartitionService(io.atomix.primitive.partition.PartitionService) PartitionGroupMembershipEvent(io.atomix.primitive.partition.PartitionGroupMembershipEvent) PartitionGroupMembership(io.atomix.primitive.partition.PartitionGroupMembership) ReplicatedSessionIdService(io.atomix.primitive.session.impl.ReplicatedSessionIdService) CompletableFuture(java.util.concurrent.CompletableFuture) PartitionGroupMembership(io.atomix.primitive.partition.PartitionGroupMembership) ManagedPartitionGroup(io.atomix.primitive.partition.ManagedPartitionGroup) PartitionManagementService(io.atomix.primitive.partition.PartitionManagementService) ConfigurationException(io.atomix.utils.config.ConfigurationException) DefaultSessionIdService(io.atomix.primitive.session.impl.DefaultSessionIdService)

Example 5 with ConfigurationException

use of io.atomix.utils.config.ConfigurationException in project atomix by atomix.

the class DefaultPartitionGroupMembershipService method updatePartitionGroups.

private void updatePartitionGroups(PartitionGroupInfo info) {
    if (systemGroup == null && info.systemGroup != null) {
        systemGroup = info.systemGroup;
        post(new PartitionGroupMembershipEvent(MEMBERS_CHANGED, systemGroup));
        LOGGER.info("{} - Bootstrapped management group {} from {}", membershipService.getLocalMember().id(), systemGroup, info.memberId);
    } else if (systemGroup != null && info.systemGroup != null) {
        if (!systemGroup.group().equals(info.systemGroup.group()) || !systemGroup.config().getType().name().equals(info.systemGroup.config().getType().name())) {
            throw new ConfigurationException("Duplicate system group detected");
        } else {
            Set<MemberId> newMembers = Stream.concat(systemGroup.members().stream(), info.systemGroup.members().stream()).filter(memberId -> membershipService.getMember(memberId) != null).collect(Collectors.toSet());
            if (!Sets.difference(newMembers, systemGroup.members()).isEmpty()) {
                systemGroup = new PartitionGroupMembership(systemGroup.group(), systemGroup.config(), ImmutableSet.copyOf(newMembers), true);
                post(new PartitionGroupMembershipEvent(MEMBERS_CHANGED, systemGroup));
                LOGGER.debug("{} - Updated management group {} from {}", membershipService.getLocalMember().id(), systemGroup, info.memberId);
            }
        }
    }
    for (PartitionGroupMembership newMembership : info.groups) {
        PartitionGroupMembership oldMembership = groups.get(newMembership.group());
        if (oldMembership == null) {
            groups.put(newMembership.group(), newMembership);
            post(new PartitionGroupMembershipEvent(MEMBERS_CHANGED, newMembership));
            LOGGER.info("{} - Bootstrapped partition group {} from {}", membershipService.getLocalMember().id(), newMembership, info.memberId);
        } else if (!oldMembership.group().equals(newMembership.group()) || !oldMembership.config().getType().name().equals(newMembership.config().getType().name())) {
            throw new ConfigurationException("Duplicate partition group " + newMembership.group() + " detected");
        } else {
            Set<MemberId> newMembers = Stream.concat(oldMembership.members().stream(), newMembership.members().stream()).filter(memberId -> membershipService.getMember(memberId) != null).collect(Collectors.toSet());
            if (!Sets.difference(newMembers, oldMembership.members()).isEmpty()) {
                PartitionGroupMembership newGroup = new PartitionGroupMembership(oldMembership.group(), oldMembership.config(), ImmutableSet.copyOf(newMembers), false);
                groups.put(oldMembership.group(), newGroup);
                post(new PartitionGroupMembershipEvent(MEMBERS_CHANGED, newGroup));
                LOGGER.debug("{} - Updated partition group {} from {}", membershipService.getLocalMember().id(), newGroup, info.memberId);
            }
        }
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) PartitionGroupMembership(io.atomix.primitive.partition.PartitionGroupMembership) ConfigurationException(io.atomix.utils.config.ConfigurationException) PartitionGroupMembershipEvent(io.atomix.primitive.partition.PartitionGroupMembershipEvent)

Aggregations

ConfigurationException (io.atomix.utils.config.ConfigurationException)11 Collection (java.util.Collection)5 Collectors (java.util.stream.Collectors)5 PrimitiveType (io.atomix.primitive.PrimitiveType)4 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)3 PartitionGroup (io.atomix.primitive.partition.PartitionGroup)3 PartitionService (io.atomix.primitive.partition.PartitionService)3 List (java.util.List)3 Set (java.util.Set)3 Joiner (com.google.common.base.Joiner)2 Config (com.typesafe.config.Config)2 AtomixRegistry (io.atomix.core.AtomixRegistry)2 PartitionGroupMembership (io.atomix.primitive.partition.PartitionGroupMembership)2 PartitionGroupMembershipEvent (io.atomix.primitive.partition.PartitionGroupMembershipEvent)2 PrimitiveProtocol (io.atomix.primitive.protocol.PrimitiveProtocol)2 ConfigMapper (io.atomix.utils.config.ConfigMapper)2 TypedConfig (io.atomix.utils.config.TypedConfig)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 Map (java.util.Map)2