Search in sources :

Example 6 with Configuration

use of io.atomix.protocols.raft.storage.system.Configuration in project atomix by atomix.

the class RaftClusterContext method listen.

@Override
public synchronized CompletableFuture<Void> listen(Collection<NodeId> cluster) {
    if (joinFuture != null)
        return joinFuture;
    // If no configuration was loaded from disk, create a new configuration.
    if (configuration == null) {
        member.setType(RaftMember.Type.PASSIVE);
        // Create a set of cluster members, excluding the local member which is joining a cluster.
        Set<RaftMember> activeMembers = cluster.stream().filter(m -> !m.equals(member.nodeId())).map(m -> new DefaultRaftMember(m, RaftMember.Type.ACTIVE, member.getLastUpdated())).collect(Collectors.toSet());
        // fail the join.
        if (activeMembers.isEmpty()) {
            return Futures.exceptionalFuture(new IllegalStateException("cannot join empty cluster"));
        }
        // Create a new configuration and configure the cluster. Once the cluster is configured, the configuration
        // will be stored on disk to ensure the cluster can fall back to the provided configuration if necessary.
        configure(new Configuration(0, 0, member.getLastUpdated().toEpochMilli(), activeMembers));
    }
    return join();
}
Also used : RaftClusterEvent(io.atomix.protocols.raft.cluster.RaftClusterEvent) NodeId(io.atomix.cluster.NodeId) RaftMember(io.atomix.protocols.raft.cluster.RaftMember) RaftCluster(io.atomix.protocols.raft.cluster.RaftCluster) RaftClusterEventListener(io.atomix.protocols.raft.cluster.RaftClusterEventListener) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) JoinRequest(io.atomix.protocols.raft.protocol.JoinRequest) RaftServer(io.atomix.protocols.raft.RaftServer) ContextualLoggerFactory(io.atomix.utils.logging.ContextualLoggerFactory) LeaveRequest(io.atomix.protocols.raft.protocol.LeaveRequest) ArrayList(java.util.ArrayList) RaftResponse(io.atomix.protocols.raft.protocol.RaftResponse) Map(java.util.Map) RaftError(io.atomix.protocols.raft.RaftError) Scheduled(io.atomix.utils.concurrent.Scheduled) Futures(io.atomix.utils.concurrent.Futures) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) LoggerContext(io.atomix.utils.logging.LoggerContext) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Set(java.util.Set) Instant(java.time.Instant) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) Collectors(java.util.stream.Collectors) Configuration(io.atomix.protocols.raft.storage.system.Configuration) Consumer(java.util.function.Consumer) List(java.util.List) RaftContext(io.atomix.protocols.raft.impl.RaftContext) Comparator(java.util.Comparator) Collections(java.util.Collections) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) RaftMember(io.atomix.protocols.raft.cluster.RaftMember) Configuration(io.atomix.protocols.raft.storage.system.Configuration)

Example 7 with Configuration

use of io.atomix.protocols.raft.storage.system.Configuration in project atomix by atomix.

the class InactiveRole method onConfigure.

@Override
public CompletableFuture<ConfigureResponse> onConfigure(ConfigureRequest request) {
    raft.checkThread();
    logRequest(request);
    updateTermAndLeader(request.term(), request.leader());
    Configuration configuration = new Configuration(request.index(), request.term(), request.timestamp(), request.members());
    // Configure the cluster membership. This will cause this server to transition to the
    // appropriate state if its type has changed.
    raft.getCluster().configure(configuration);
    // case the received configuration was an older configuration that was not applied.
    if (raft.getCommitIndex() >= raft.getCluster().getConfiguration().index()) {
        raft.getCluster().commit();
    }
    return CompletableFuture.completedFuture(logResponse(ConfigureResponse.builder().withStatus(RaftResponse.Status.OK).build()));
}
Also used : Configuration(io.atomix.protocols.raft.storage.system.Configuration)

Example 8 with Configuration

use of io.atomix.protocols.raft.storage.system.Configuration in project atomix by atomix.

the class LeaderRole method configure.

/**
 * Commits the given configuration.
 */
protected CompletableFuture<Long> configure(Collection<RaftMember> members) {
    raft.checkThread();
    final long term = raft.getTerm();
    return appendAndCompact(new ConfigurationEntry(term, System.currentTimeMillis(), members)).thenComposeAsync(entry -> {
        // Store the index of the configuration entry in order to prevent other configurations from
        // being logged and committed concurrently. This is an important safety property of Raft.
        configuring = entry.index();
        raft.getCluster().configure(new Configuration(entry.index(), entry.entry().term(), entry.entry().timestamp(), entry.entry().members()));
        return appender.appendEntries(entry.index()).whenComplete((commitIndex, commitError) -> {
            raft.checkThread();
            if (isRunning() && commitError == null) {
                raft.getServiceManager().<OperationResult>apply(entry.index());
            }
            configuring = 0;
        });
    }, raft.getThreadContext());
}
Also used : Configuration(io.atomix.protocols.raft.storage.system.Configuration) OperationResult(io.atomix.protocols.raft.impl.OperationResult) ConfigurationEntry(io.atomix.protocols.raft.storage.log.entry.ConfigurationEntry)

Example 9 with Configuration

use of io.atomix.protocols.raft.storage.system.Configuration in project atomix by atomix.

the class LeaderRole method onReconfigure.

@Override
public CompletableFuture<ReconfigureResponse> onReconfigure(final ReconfigureRequest request) {
    raft.checkThread();
    logRequest(request);
    // See https://groups.google.com/forum/#!topic/raft-dev/t4xj6dJTP6E
    if (configuring() || initializing()) {
        return CompletableFuture.completedFuture(logResponse(ReconfigureResponse.builder().withStatus(RaftResponse.Status.ERROR).build()));
    }
    // If the member is not a known member of the cluster, fail the promotion.
    DefaultRaftMember existingMember = raft.getCluster().getMember(request.member().nodeId());
    if (existingMember == null) {
        return CompletableFuture.completedFuture(logResponse(ReconfigureResponse.builder().withStatus(RaftResponse.Status.ERROR).withError(RaftError.Type.UNKNOWN_SESSION).build()));
    }
    // the leader, fail the request to ensure servers can't reconfigure an old configuration.
    if (request.index() > 0 && request.index() < raft.getCluster().getConfiguration().index() || request.term() != raft.getCluster().getConfiguration().term()) {
        return CompletableFuture.completedFuture(logResponse(ReconfigureResponse.builder().withStatus(RaftResponse.Status.ERROR).withError(RaftError.Type.CONFIGURATION_ERROR).build()));
    }
    // If the member type has not changed, complete the configuration change successfully.
    if (existingMember.getType() == request.member().getType()) {
        Configuration configuration = raft.getCluster().getConfiguration();
        return CompletableFuture.completedFuture(logResponse(ReconfigureResponse.builder().withStatus(RaftResponse.Status.OK).withIndex(configuration.index()).withTerm(raft.getCluster().getConfiguration().term()).withTime(raft.getCluster().getConfiguration().time()).withMembers(configuration.members()).build()));
    }
    // Update the member type.
    existingMember.update(request.member().getType(), Instant.now());
    Collection<RaftMember> members = raft.getCluster().getMembers();
    CompletableFuture<ReconfigureResponse> future = new CompletableFuture<>();
    configure(members).whenComplete((index, error) -> {
        if (error == null) {
            future.complete(logResponse(ReconfigureResponse.builder().withStatus(RaftResponse.Status.OK).withIndex(index).withTerm(raft.getCluster().getConfiguration().term()).withTime(raft.getCluster().getConfiguration().time()).withMembers(members).build()));
        } else {
            future.complete(logResponse(ReconfigureResponse.builder().withStatus(RaftResponse.Status.ERROR).withError(RaftError.Type.PROTOCOL_ERROR).build()));
        }
    });
    return future;
}
Also used : RaftMember(io.atomix.protocols.raft.cluster.RaftMember) DefaultRaftMember(io.atomix.protocols.raft.cluster.impl.DefaultRaftMember) CompletableFuture(java.util.concurrent.CompletableFuture) DefaultRaftMember(io.atomix.protocols.raft.cluster.impl.DefaultRaftMember) Configuration(io.atomix.protocols.raft.storage.system.Configuration) ReconfigureResponse(io.atomix.protocols.raft.protocol.ReconfigureResponse)

Aggregations

Configuration (io.atomix.protocols.raft.storage.system.Configuration)9 RaftMember (io.atomix.protocols.raft.cluster.RaftMember)4 JoinRequest (io.atomix.protocols.raft.protocol.JoinRequest)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 MoreObjects.toStringHelper (com.google.common.base.MoreObjects.toStringHelper)3 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)3 NodeId (io.atomix.cluster.NodeId)3 RaftError (io.atomix.protocols.raft.RaftError)3 RaftServer (io.atomix.protocols.raft.RaftServer)3 RaftCluster (io.atomix.protocols.raft.cluster.RaftCluster)3 RaftClusterEvent (io.atomix.protocols.raft.cluster.RaftClusterEvent)3 RaftClusterEventListener (io.atomix.protocols.raft.cluster.RaftClusterEventListener)3 RaftContext (io.atomix.protocols.raft.impl.RaftContext)3 LeaveRequest (io.atomix.protocols.raft.protocol.LeaveRequest)3 RaftResponse (io.atomix.protocols.raft.protocol.RaftResponse)3 Futures (io.atomix.utils.concurrent.Futures)3 Scheduled (io.atomix.utils.concurrent.Scheduled)3 ContextualLoggerFactory (io.atomix.utils.logging.ContextualLoggerFactory)3 LoggerContext (io.atomix.utils.logging.LoggerContext)3 Instant (java.time.Instant)3