Search in sources :

Example 96 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class GrpcClient method notifyReady.

private void notifyReady(final Endpoint endpoint) {
    LOG.info("The channel {} has successfully established.", endpoint);
    clearConnFailuresCount(endpoint);
    final ReplicatorGroup rpGroup = this.replicatorGroup;
    if (rpGroup != null) {
        try {
            RpcUtils.runInThread(() -> {
                final PeerId peer = new PeerId();
                if (peer.parse(endpoint.toString())) {
                    LOG.info("Peer {} is connected.", peer);
                    rpGroup.checkReplicator(peer, true);
                } else {
                    LOG.error("Fail to parse peer: {}.", endpoint);
                }
            });
        } catch (final Throwable t) {
            LOG.error("Fail to check replicator {}.", endpoint, t);
        }
    }
}
Also used : ReplicatorGroup(com.alipay.sofa.jraft.ReplicatorGroup) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 97 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class ElectionNode method init.

@Override
public boolean init(final ElectionNodeOptions opts) {
    if (this.started) {
        LOG.info("[ElectionNode: {}] already started.", opts.getServerAddress());
        return true;
    }
    // node options
    NodeOptions nodeOpts = opts.getNodeOptions();
    if (nodeOpts == null) {
        nodeOpts = new NodeOptions();
    }
    this.fsm = new ElectionOnlyStateMachine(this.listeners);
    nodeOpts.setFsm(this.fsm);
    final Configuration initialConf = new Configuration();
    if (!initialConf.parse(opts.getInitialServerAddressList())) {
        throw new IllegalArgumentException("Fail to parse initConf: " + opts.getInitialServerAddressList());
    }
    // Set the initial cluster configuration
    nodeOpts.setInitialConf(initialConf);
    final String dataPath = opts.getDataPath();
    try {
        FileUtils.forceMkdir(new File(dataPath));
    } catch (final IOException e) {
        LOG.error("Fail to make dir for dataPath {}.", dataPath);
        return false;
    }
    // Set the data path
    // Log, required
    nodeOpts.setLogUri(Paths.get(dataPath, "log").toString());
    // Metadata, required
    nodeOpts.setRaftMetaUri(Paths.get(dataPath, "meta").toString());
    // nodeOpts.setSnapshotUri(Paths.get(dataPath, "snapshot").toString());
    final String groupId = opts.getGroupId();
    final PeerId serverId = new PeerId();
    if (!serverId.parse(opts.getServerAddress())) {
        throw new IllegalArgumentException("Fail to parse serverId: " + opts.getServerAddress());
    }
    final RpcServer rpcServer = RaftRpcServerFactory.createRaftRpcServer(serverId.getEndpoint());
    this.raftGroupService = new RaftGroupService(groupId, serverId, nodeOpts, rpcServer);
    this.node = this.raftGroupService.start();
    if (this.node != null) {
        this.started = true;
    }
    return this.started;
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) IOException(java.io.IOException) File(java.io.File) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 98 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class PriorityElectionNode method init.

@Override
public boolean init(final PriorityElectionNodeOptions opts) {
    if (this.started) {
        LOG.info("[PriorityElectionNode: {}] already started.", opts.getServerAddress());
        return true;
    }
    // node options
    NodeOptions nodeOpts = opts.getNodeOptions();
    if (nodeOpts == null) {
        nodeOpts = new NodeOptions();
    }
    this.fsm = new PriorityElectionOnlyStateMachine(this.listeners);
    // Set the initial PriorityElectionOnlyStateMachine
    nodeOpts.setFsm(this.fsm);
    final Configuration initialConf = new Configuration();
    if (!initialConf.parse(opts.getInitialServerAddressList())) {
        throw new IllegalArgumentException("Fail to parse initConf: " + opts.getInitialServerAddressList());
    }
    // Set the initial cluster configuration
    nodeOpts.setInitialConf(initialConf);
    final String dataPath = opts.getDataPath();
    try {
        FileUtils.forceMkdir(new File(dataPath));
    } catch (final IOException e) {
        LOG.error("Fail to make dir for dataPath {}.", dataPath);
        return false;
    }
    // Set the data path
    // Log, required
    nodeOpts.setLogUri(Paths.get(dataPath, "log").toString());
    // Metadata, required
    nodeOpts.setRaftMetaUri(Paths.get(dataPath, "meta").toString());
    // nodeOpts.setSnapshotUri(Paths.get(dataPath, "snapshot").toString());
    final String groupId = opts.getGroupId();
    final PeerId serverId = new PeerId();
    if (!serverId.parse(opts.getServerAddress())) {
        throw new IllegalArgumentException("Fail to parse serverId: " + opts.getServerAddress());
    }
    /**
     * Set priority value, required for priority-based election, it must be a positive value when
     * enable the feature, some special value meaning:
     * <ul>
     * <li>-1 : disable priority-based election.</li>
     * <li>0: will never participate in election.</li>
     * <li>1: minimum value</li>
     * </ul>
     * value.
     */
    nodeOpts.setElectionPriority(serverId.getPriority());
    final RpcServer rpcServer = RaftRpcServerFactory.createRaftRpcServer(serverId.getEndpoint());
    this.raftGroupService = new RaftGroupService(groupId, serverId, nodeOpts, rpcServer);
    this.node = this.raftGroupService.start();
    if (this.node != null) {
        this.started = true;
    }
    return this.started;
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) IOException(java.io.IOException) File(java.io.File) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 99 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class LogManagerImpl method oldConfFromMeta.

private Configuration oldConfFromMeta(final SnapshotMeta meta) {
    final Configuration oldConf = new Configuration();
    for (int i = 0; i < meta.getOldPeersCount(); i++) {
        final PeerId peer = new PeerId();
        peer.parse(meta.getOldPeers(i));
        oldConf.addPeer(peer);
    }
    for (int i = 0; i < meta.getOldLearnersCount(); i++) {
        final PeerId peer = new PeerId();
        peer.parse(meta.getOldLearners(i));
        oldConf.addLearner(peer);
    }
    return oldConf;
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 100 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class LogManagerImpl method confFromMeta.

private Configuration confFromMeta(final SnapshotMeta meta) {
    final Configuration conf = new Configuration();
    for (int i = 0; i < meta.getPeersCount(); i++) {
        final PeerId peer = new PeerId();
        peer.parse(meta.getPeers(i));
        conf.addPeer(peer);
    }
    for (int i = 0; i < meta.getLearnersCount(); i++) {
        final PeerId peer = new PeerId();
        peer.parse(meta.getLearners(i));
        conf.addLearner(peer);
    }
    return conf;
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Aggregations

PeerId (com.alipay.sofa.jraft.entity.PeerId)236 Test (org.junit.Test)107 Node (com.alipay.sofa.jraft.Node)70 Configuration (com.alipay.sofa.jraft.conf.Configuration)54 Status (com.alipay.sofa.jraft.Status)49 Endpoint (com.alipay.sofa.jraft.util.Endpoint)43 ArrayList (java.util.ArrayList)32 CountDownLatch (java.util.concurrent.CountDownLatch)28 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)24 LogId (com.alipay.sofa.jraft.entity.LogId)17 Message (com.google.protobuf.Message)15 ByteBuffer (java.nio.ByteBuffer)15 Task (com.alipay.sofa.jraft.entity.Task)13 File (java.io.File)12 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)11 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)11 JRaftException (com.alipay.sofa.jraft.error.JRaftException)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)9 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)8