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;
}
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;
}
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;
}
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;
}
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);
}
}
Aggregations