Search in sources :

Example 6 with RpcServer

use of com.alipay.sofa.jraft.rpc.RpcServer in project RICE by gaojiayi.

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 7 with RpcServer

use of com.alipay.sofa.jraft.rpc.RpcServer in project nacos by alibaba.

the class JRaftUtils method initRpcServer.

public static RpcServer initRpcServer(JRaftServer server, PeerId peerId) {
    GrpcRaftRpcFactory raftRpcFactory = (GrpcRaftRpcFactory) RpcFactoryHelper.rpcFactory();
    raftRpcFactory.registerProtobufSerializer(Log.class.getName(), Log.getDefaultInstance());
    raftRpcFactory.registerProtobufSerializer(GetRequest.class.getName(), GetRequest.getDefaultInstance());
    raftRpcFactory.registerProtobufSerializer(WriteRequest.class.getName(), WriteRequest.getDefaultInstance());
    raftRpcFactory.registerProtobufSerializer(ReadRequest.class.getName(), ReadRequest.getDefaultInstance());
    raftRpcFactory.registerProtobufSerializer(Response.class.getName(), Response.getDefaultInstance());
    MarshallerRegistry registry = raftRpcFactory.getMarshallerRegistry();
    registry.registerResponseInstance(Log.class.getName(), Response.getDefaultInstance());
    registry.registerResponseInstance(GetRequest.class.getName(), Response.getDefaultInstance());
    registry.registerResponseInstance(WriteRequest.class.getName(), Response.getDefaultInstance());
    registry.registerResponseInstance(ReadRequest.class.getName(), Response.getDefaultInstance());
    final RpcServer rpcServer = raftRpcFactory.createRpcServer(peerId.getEndpoint());
    RaftRpcServerFactory.addRaftRequestProcessors(rpcServer, RaftExecutor.getRaftCoreExecutor(), RaftExecutor.getRaftCliServiceExecutor());
    // Deprecated
    rpcServer.registerProcessor(new NacosLogProcessor(server, SerializeFactory.getDefault()));
    // Deprecated
    rpcServer.registerProcessor(new NacosGetRequestProcessor(server, SerializeFactory.getDefault()));
    rpcServer.registerProcessor(new NacosWriteRequestProcessor(server, SerializeFactory.getDefault()));
    rpcServer.registerProcessor(new NacosReadRequestProcessor(server, SerializeFactory.getDefault()));
    return rpcServer;
}
Also used : Response(com.alibaba.nacos.consistency.entity.Response) NacosWriteRequestProcessor(com.alibaba.nacos.core.distributed.raft.processor.NacosWriteRequestProcessor) MarshallerRegistry(com.alipay.sofa.jraft.rpc.impl.MarshallerRegistry) NacosReadRequestProcessor(com.alibaba.nacos.core.distributed.raft.processor.NacosReadRequestProcessor) NacosGetRequestProcessor(com.alibaba.nacos.core.distributed.raft.processor.NacosGetRequestProcessor) Log(com.alibaba.nacos.consistency.entity.Log) WriteRequest(com.alibaba.nacos.consistency.entity.WriteRequest) GetRequest(com.alibaba.nacos.consistency.entity.GetRequest) RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) NacosLogProcessor(com.alibaba.nacos.core.distributed.raft.processor.NacosLogProcessor) GrpcRaftRpcFactory(com.alipay.sofa.jraft.rpc.impl.GrpcRaftRpcFactory) ReadRequest(com.alibaba.nacos.consistency.entity.ReadRequest)

Example 8 with RpcServer

use of com.alipay.sofa.jraft.rpc.RpcServer in project sofa-jraft by sofastack.

the class BoltRaftRpcFactory method createRpcServer.

@Override
public RpcServer createRpcServer(final Endpoint endpoint, final ConfigHelper<RpcServer> helper) {
    final int port = Requires.requireNonNull(endpoint, "endpoint").getPort();
    Requires.requireTrue(port > 0 && port < 0xFFFF, "port out of range:" + port);
    final com.alipay.remoting.rpc.RpcServer boltImpl = new com.alipay.remoting.rpc.RpcServer(port, true, false);
    final RpcServer rpcServer = new BoltRpcServer(boltImpl);
    if (helper != null) {
        helper.config(rpcServer);
    }
    return rpcServer;
}
Also used : RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) Endpoint(com.alipay.sofa.jraft.util.Endpoint)

Example 9 with RpcServer

use of com.alipay.sofa.jraft.rpc.RpcServer in project sofa-jraft by sofastack.

the class GrpcRaftRpcFactory method createRpcServer.

@Override
public RpcServer createRpcServer(final Endpoint endpoint, final ConfigHelper<RpcServer> helper) {
    final int port = Requires.requireNonNull(endpoint, "endpoint").getPort();
    Requires.requireTrue(port > 0 && port < 0xFFFF, "port out of range:" + port);
    final MutableHandlerRegistry handlerRegistry = new MutableHandlerRegistry();
    final Server server = // 
    ServerBuilder.forPort(port).fallbackHandlerRegistry(// 
    handlerRegistry).directExecutor().maxInboundMessageSize(// 
    RPC_MAX_INBOUND_MESSAGE_SIZE).build();
    final RpcServer rpcServer = new GrpcServer(server, handlerRegistry, this.parserClasses, getMarshallerRegistry());
    if (helper != null) {
        helper.config(rpcServer);
    }
    return rpcServer;
}
Also used : RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) Server(io.grpc.Server) RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) MutableHandlerRegistry(io.grpc.util.MutableHandlerRegistry) Endpoint(com.alipay.sofa.jraft.util.Endpoint)

Example 10 with RpcServer

use of com.alipay.sofa.jraft.rpc.RpcServer in project sofa-jraft by sofastack.

the class AtomicServer method start.

public void start() throws IOException {
    PeerId serverId = new PeerId();
    if (!serverId.parse(conf.getServerAddress())) {
        throw new IllegalArgumentException("Fail to parse serverId:" + conf.getServerAddress());
    }
    FileUtils.forceMkdir(new File(conf.getDataPath()));
    // The same in-process raft group shares the same RPC Server.
    RpcServer rpcServer = RaftRpcServerFactory.createRaftRpcServer(serverId.getEndpoint());
    // Register biz handler
    rpcServer.registerProcessor(new GetSlotsCommandProcessor(this));
    rpcServer.registerProcessor(new GetCommandProcessor(this));
    rpcServer.registerProcessor(new IncrementAndGetCommandProcessor(this));
    rpcServer.registerProcessor(new CompareAndSetCommandProcessor(this));
    rpcServer.registerProcessor(new SetCommandProcessor(this));
    long step = conf.getMaxSlot() / totalSlots;
    if (conf.getMaxSlot() % totalSlots > 0) {
        step = step + 1;
    }
    for (int i = 0; i < totalSlots; i++) {
        long min = i * step;
        long mayMax = (i + 1) * step;
        long max = mayMax > conf.getMaxSlot() || mayMax <= 0 ? conf.getMaxSlot() : mayMax;
        StartupConf nodeConf = new StartupConf();
        String nodeDataPath = conf.getDataPath() + File.separator + i;
        nodeConf.setDataPath(nodeDataPath);
        String nodeGroup = conf.getGroupId() + "_" + i;
        nodeConf.setGroupId(nodeGroup);
        nodeConf.setMaxSlot(max);
        nodeConf.setMinSlot(min);
        nodeConf.setConf(conf.getConf());
        nodeConf.setServerAddress(conf.getServerAddress());
        nodeConf.setTotalSlots(conf.getTotalSlots());
        LOG.info("Starting range node {}-{} with conf {}", min, max, nodeConf);
        nodes.put(i * step, AtomicRangeGroup.start(nodeConf, rpcServer));
        groups.put(i * step, nodeGroup);
    }
}
Also used : IncrementAndGetCommandProcessor(com.alipay.sofa.jraft.test.atomic.server.processor.IncrementAndGetCommandProcessor) CompareAndSetCommandProcessor(com.alipay.sofa.jraft.test.atomic.server.processor.CompareAndSetCommandProcessor) SetCommandProcessor(com.alipay.sofa.jraft.test.atomic.server.processor.SetCommandProcessor) CompareAndSetCommandProcessor(com.alipay.sofa.jraft.test.atomic.server.processor.CompareAndSetCommandProcessor) RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) GetSlotsCommandProcessor(com.alipay.sofa.jraft.test.atomic.server.processor.GetSlotsCommandProcessor) GetCommandProcessor(com.alipay.sofa.jraft.test.atomic.server.processor.GetCommandProcessor) IncrementAndGetCommandProcessor(com.alipay.sofa.jraft.test.atomic.server.processor.IncrementAndGetCommandProcessor) File(java.io.File) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Aggregations

RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)12 PeerId (com.alipay.sofa.jraft.entity.PeerId)8 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)6 Configuration (com.alipay.sofa.jraft.conf.Configuration)6 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)6 File (java.io.File)6 Endpoint (com.alipay.sofa.jraft.util.Endpoint)4 IOException (java.io.IOException)3 Node (com.alipay.sofa.jraft.Node)2 GetRequest (com.alibaba.nacos.consistency.entity.GetRequest)1 Log (com.alibaba.nacos.consistency.entity.Log)1 ReadRequest (com.alibaba.nacos.consistency.entity.ReadRequest)1 Response (com.alibaba.nacos.consistency.entity.Response)1 WriteRequest (com.alibaba.nacos.consistency.entity.WriteRequest)1 NacosGetRequestProcessor (com.alibaba.nacos.core.distributed.raft.processor.NacosGetRequestProcessor)1 NacosLogProcessor (com.alibaba.nacos.core.distributed.raft.processor.NacosLogProcessor)1 NacosReadRequestProcessor (com.alibaba.nacos.core.distributed.raft.processor.NacosReadRequestProcessor)1 NacosWriteRequestProcessor (com.alibaba.nacos.core.distributed.raft.processor.NacosWriteRequestProcessor)1 KVStoreStateMachine (com.alipay.sofa.jraft.rhea.storage.KVStoreStateMachine)1 MetricsRawKVStore (com.alipay.sofa.jraft.rhea.storage.MetricsRawKVStore)1