com.alipay.sofa.jraft.RouteTable Java Examples
The following examples show how to use
com.alipay.sofa.jraft.RouteTable.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractPlacementDriverClient.java From sofa-jraft with Apache License 2.0 | 6 votes |
@Override public boolean transferLeader(final long regionId, final Peer peer, final boolean refreshConf) { Requires.requireNonNull(peer, "peer"); Requires.requireNonNull(peer.getEndpoint(), "peer.endpoint"); final String raftGroupId = JRaftHelper.getJRaftGroupId(this.clusterName, regionId); final Configuration conf = RouteTable.getInstance().getConfiguration(raftGroupId); final Status status = this.cliService.transferLeader(raftGroupId, conf, JRaftHelper.toJRaftPeerId(peer)); if (status.isOk()) { if (refreshConf) { refreshRouteConfiguration(regionId); } return true; } LOG.error("Fail to [transferLeader], [regionId: {}, peer: {}], status: {}.", regionId, peer, status); return false; }
Example #2
Source File: AbstractPlacementDriverClient.java From sofa-jraft with Apache License 2.0 | 6 votes |
@Override public boolean addReplica(final long regionId, final Peer peer, final boolean refreshConf) { Requires.requireNonNull(peer, "peer"); Requires.requireNonNull(peer.getEndpoint(), "peer.endpoint"); final String raftGroupId = JRaftHelper.getJRaftGroupId(this.clusterName, regionId); final Configuration conf = RouteTable.getInstance().getConfiguration(raftGroupId); final Status status = this.cliService.addPeer(raftGroupId, conf, JRaftHelper.toJRaftPeerId(peer)); if (status.isOk()) { if (refreshConf) { refreshRouteConfiguration(regionId); } return true; } LOG.error("Fail to [addReplica], [regionId: {}, peer: {}], status: {}.", regionId, peer, status); return false; }
Example #3
Source File: AbstractPlacementDriverClient.java From sofa-jraft with Apache License 2.0 | 6 votes |
@Override public boolean removeReplica(final long regionId, final Peer peer, final boolean refreshConf) { Requires.requireNonNull(peer, "peer"); Requires.requireNonNull(peer.getEndpoint(), "peer.endpoint"); final String raftGroupId = JRaftHelper.getJRaftGroupId(this.clusterName, regionId); final Configuration conf = RouteTable.getInstance().getConfiguration(raftGroupId); final Status status = this.cliService.removePeer(raftGroupId, conf, JRaftHelper.toJRaftPeerId(peer)); if (status.isOk()) { if (refreshConf) { refreshRouteConfiguration(regionId); } return true; } LOG.error("Fail to [removeReplica], [regionId: {}, peer: {}], status: {}.", regionId, peer, status); return false; }
Example #4
Source File: AbstractPlacementDriverClient.java From sofa-jraft with Apache License 2.0 | 6 votes |
protected void initRouteTableByRegion(final RegionRouteTableOptions opts) { final long regionId = Requires.requireNonNull(opts.getRegionId(), "opts.regionId"); final byte[] startKey = opts.getStartKeyBytes(); final byte[] endKey = opts.getEndKeyBytes(); final String initialServerList = opts.getInitialServerList(); final Region region = new Region(); final Configuration conf = new Configuration(); // region region.setId(regionId); region.setStartKey(startKey); region.setEndKey(endKey); region.setRegionEpoch(new RegionEpoch(-1, -1)); // peers Requires.requireTrue(Strings.isNotBlank(initialServerList), "opts.initialServerList is blank"); conf.parse(initialServerList); region.setPeers(JRaftHelper.toPeerList(conf.listPeers())); // update raft route table RouteTable.getInstance().updateConfiguration(JRaftHelper.getJRaftGroupId(clusterName, regionId), conf); this.regionRouteTable.addOrUpdateRegion(region); }
Example #5
Source File: RemotePlacementDriverClient.java From sofa-jraft with Apache License 2.0 | 6 votes |
@Override public synchronized boolean init(final PlacementDriverOptions opts) { if (this.started) { LOG.info("[RemotePlacementDriverClient] already started."); return true; } super.init(opts); this.pdGroupId = opts.getPdGroupId(); if (Strings.isBlank(this.pdGroupId)) { throw new IllegalArgumentException("opts.pdGroup id must not be blank"); } final String initialPdServers = opts.getInitialPdServerList(); if (Strings.isBlank(initialPdServers)) { throw new IllegalArgumentException("opts.initialPdServerList must not be blank"); } RouteTable.getInstance().updateConfiguration(this.pdGroupId, initialPdServers); this.metadataRpcClient = new MetadataRpcClient(super.pdRpcService, 3); refreshRouteTable(); LOG.info("[RemotePlacementDriverClient] start successfully, options: {}.", opts); return this.started = true; }
Example #6
Source File: RaftClient.java From sofa-registry with Apache License 2.0 | 6 votes |
public static PeerId refreshLeader(CliClientService cliClientService, String groupId, int timeout) { try { Status status = RouteTable.getInstance().refreshLeader(cliClientService, groupId, timeout); if (!status.isOk()) { throw new IllegalStateException(String.format("Refresh leader failed,error=%s", status.getErrorMsg())); } PeerId leader = RouteTable.getInstance().selectLeader(groupId); LOGGER.info("Leader is {}", leader); //avoid refresh leader config ip list must be current list,list maybe change by manage status = RouteTable.getInstance().refreshConfiguration(cliClientService, groupId, timeout); if (!status.isOk()) { throw new IllegalStateException(String.format( "Refresh configuration failed, error=%s", status.getErrorMsg())); } return leader; } catch (Exception e) { LOGGER.error("Refresh leader failed", e); throw new IllegalStateException("Refresh leader failed", e); } }
Example #7
Source File: RaftClient.java From sofa-registry with Apache License 2.0 | 6 votes |
private Object redirectRequest(ProcessRequest request, String redirect) { try { PeerId redirectLead = new PeerId(); if (!redirectLead.parse(redirect)) { throw new IllegalArgumentException("Fail to parse serverId:" + redirect); } //wait for onLeaderStart TimeUnit.MILLISECONDS.sleep(1000); LOGGER.info("Redirect request send to return peer {},request {}", redirect, request); Object response = this.rpcClient.invokeSync(redirectLead.getEndpoint().toString(), request, cliOptions.getRpcDefaultTimeout()); ProcessResponse cmd = (ProcessResponse) response; if (cmd.getSuccess()) { RouteTable.getInstance().updateLeader(groupId, redirectLead); return cmd.getEntity(); } else { refreshLeader(); throw new IllegalStateException("Redirect request server error:" + cmd.getEntity()); } } catch (Exception e) { LOGGER.error("Redirect process request error!", e); throw new RuntimeException("Redirect process request error!" + e.getMessage(), e); } }
Example #8
Source File: RaftClient.java From sofa-registry with Apache License 2.0 | 5 votes |
/** * raft client start */ public void start() { if (started.compareAndSet(false, true)) { RouteTable.getInstance().updateConfiguration(groupId, conf); cliClientService.init(cliOptions); rpcClient = cliClientService.getRpcClient(); RaftClientConnectionHandler raftClientConnectionHandler = new RaftClientConnectionHandler( this); rpcClient.addConnectionEventProcessor(ConnectionEventType.CONNECT, new ConnectionEventAdapter(ConnectionEventType.CONNECT, raftClientConnectionHandler, null)); rpcClient.addConnectionEventProcessor(ConnectionEventType.CLOSE, new ConnectionEventAdapter(ConnectionEventType.CLOSE, raftClientConnectionHandler, null)); rpcClient.addConnectionEventProcessor(ConnectionEventType.EXCEPTION, new ConnectionEventAdapter(ConnectionEventType.EXCEPTION, raftClientConnectionHandler, null)); //reset leader notify NotifyLeaderChangeHandler notifyLeaderChangeHandler = new NotifyLeaderChangeHandler( groupId, cliClientService); rpcClient .registerUserProcessor(new SyncUserProcessorAdapter(notifyLeaderChangeHandler)); } }
Example #9
Source File: RaftClientConnectionHandler.java From sofa-registry with Apache License 2.0 | 5 votes |
@Override public void disconnected(Channel channel) { if (channel != null && !channel.isConnected()) { LOGGER.info("Raft Client disconnected,remote address:" + channel.getRemoteAddress() + " localAddress:" + channel.getLocalAddress()); } //reset leader if (RouteTable.getInstance().updateLeader(raftClient.getGroupId(), (PeerId) null)) { LOGGER.info("Reset leader for raft group {}", raftClient.getGroupId()); } }
Example #10
Source File: CounterClient.java From sofa-jraft with Apache License 2.0 | 5 votes |
public static void main(final String[] args) throws Exception { if (args.length != 2) { System.out.println("Useage : java com.alipay.sofa.jraft.example.counter.CounterClient {groupId} {conf}"); System.out .println("Example: java com.alipay.sofa.jraft.example.counter.CounterClient counter 127.0.0.1:8081,127.0.0.1:8082,127.0.0.1:8083"); System.exit(1); } final String groupId = args[0]; final String confStr = args[1]; final Configuration conf = new Configuration(); if (!conf.parse(confStr)) { throw new IllegalArgumentException("Fail to parse conf:" + confStr); } RouteTable.getInstance().updateConfiguration(groupId, conf); final CliClientServiceImpl cliClientService = new CliClientServiceImpl(); cliClientService.init(new CliOptions()); if (!RouteTable.getInstance().refreshLeader(cliClientService, groupId, 1000).isOk()) { throw new IllegalStateException("Refresh leader failed"); } final PeerId leader = RouteTable.getInstance().selectLeader(groupId); System.out.println("Leader is " + leader); final int n = 1000; final CountDownLatch latch = new CountDownLatch(n); final long start = System.currentTimeMillis(); for (int i = 0; i < n; i++) { incrementAndGet(cliClientService, leader, i, latch); } latch.await(); System.out.println(n + " ops, cost : " + (System.currentTimeMillis() - start) + " ms."); System.exit(0); }
Example #11
Source File: CliServiceTest.java From sofa-jraft with Apache License 2.0 | 5 votes |
@After public void teardown() throws Exception { this.cliService.shutdown(); this.cluster.stopAll(); if (NodeImpl.GLOBAL_NUM_NODES.get() > 0) { Thread.sleep(1000); assertEquals(NodeImpl.GLOBAL_NUM_NODES.get(), 0); } FileUtils.deleteDirectory(new File(this.dataPath)); NodeManager.getInstance().clear(); RouteTable.getInstance().reset(); System.out.println(">>>>>>>>>>>>>>> End test method: " + this.testName.getMethodName()); }
Example #12
Source File: RaftClient.java From sofa-registry with Apache License 2.0 | 5 votes |
/** * get leader * @return */ public PeerId getLeader() { PeerId leader = RouteTable.getInstance().selectLeader(groupId); if (leader == null) { leader = refreshLeader(); } return leader; }
Example #13
Source File: DmetaClient.java From distkv with BSD 3-Clause "New" or "Revised" License | 5 votes |
public DmetaClient(String confStr) { final Configuration conf = JRaftUtils.getConfiguration(confStr); RouteTable.getInstance().updateConfiguration(RAFT_GROUP_ID, conf); //init RPC client and update Routing table cliClientService = new BoltCliClientService(); cliClientService.init(new CliOptions()); refreshLeader(); }
Example #14
Source File: AtomicClient.java From sofa-jraft with Apache License 2.0 | 5 votes |
public void start() throws InterruptedException, TimeoutException { cliOptions = new CliOptions(); this.cliClientService.init(cliOptions); this.rpcClient = this.cliClientService.getRpcClient(); if (conf != null) { final Set<PeerId> peers = conf.getPeerSet(); for (final PeerId peer : peers) { try { final BooleanCommand cmd = (BooleanCommand) this.rpcClient.invokeSync(peer.getEndpoint(), new GetSlotsCommand(), cliOptions.getRpcDefaultTimeout()); if (cmd instanceof SlotsResponseCommand) { groups = ((SlotsResponseCommand) cmd).getMap(); break; } else { LOG.warn("Fail to get slots from peer {}, error: {}", peer, cmd.getErrorMsg()); } } catch (final Throwable t) { LOG.warn("Fail to get slots from peer {}, error: {}", peer, t.getMessage()); //continue; } } if (groups == null || groups.isEmpty()) { throw new IllegalArgumentException("Can't get slots from any peers"); } else { LOG.info("All groups is {}", groups); } for (final String groupId : groups.values()) { RouteTable.getInstance().updateConfiguration(groupId, conf); refreshLeader(groupId); refreshConf(groupId); } } }
Example #15
Source File: DefaultRheaKVStore.java From sofa-jraft with Apache License 2.0 | 5 votes |
private Endpoint getLeaderByRegionEngine(final long regionId) { final RegionEngine regionEngine = getRegionEngine(regionId); if (regionEngine != null) { final PeerId leader = regionEngine.getLeaderId(); if (leader != null) { final String raftGroupId = JRaftHelper.getJRaftGroupId(this.pdClient.getClusterName(), regionId); RouteTable.getInstance().updateLeader(raftGroupId, leader); return leader.getEndpoint(); } } return null; }
Example #16
Source File: CliServiceTest.java From sofa-jraft with Apache License 2.0 | 5 votes |
@After public void teardown() throws Exception { this.cliService.shutdown(); this.cluster.stopAll(); if (NodeImpl.GLOBAL_NUM_NODES.get() > 0) { Thread.sleep(1000); assertEquals(NodeImpl.GLOBAL_NUM_NODES.get(), 0); } FileUtils.deleteDirectory(new File(this.dataPath)); NodeManager.getInstance().clear(); RouteTable.getInstance().reset(); System.out.println(">>>>>>>>>>>>>>> End test method: " + this.testName.getMethodName()); }
Example #17
Source File: AbstractPlacementDriverClient.java From sofa-jraft with Apache License 2.0 | 5 votes |
@Override public void refreshRouteConfiguration(final long regionId) { final String raftGroupId = JRaftHelper.getJRaftGroupId(this.clusterName, regionId); try { getLeader(raftGroupId, true, 5000); RouteTable.getInstance().refreshConfiguration(this.cliClientService, raftGroupId, 5000); } catch (final Exception e) { LOG.error("Fail to refresh route configuration for {}, {}.", regionId, StackTraceUtil.stackTrace(e)); } }
Example #18
Source File: App.java From KitDB with Apache License 2.0 | 4 votes |
public static void main(String[] args) throws IOException, KitDBException { if (args.length != 6) { System.out .println("Useage : java com.alipay.jraft.example.counter.CounterServer {dataPath} {groupId} {serverId} {initConf}"); System.out .println("Example: java com.alipay.jraft.example.counter.CounterServer /tmp/server1 counter 127.0.0.1:8081 127.0.0.1:8081,127.0.0.1:8082,127.0.0.1:8083"); System.exit(1); } String dataPath = args[0]; String groupId = args[1]; String serverIdStr = args[2]; String initConfStr = args[3]; String dnName = args[4]; String portStr = args[5]; // 解析参数 // 设置初始集群配置 DB db = DB.build("D:\\temp\\" + dnName, false); GroupConfig groupConfig = new GroupConfig(); groupConfig.setGroup("test"); groupConfig.setInitNodes(initConfStr); NodeConfig nodeConfig = new NodeConfig(); nodeConfig.setNode(serverIdStr); nodeConfig.setRaftDir(dataPath); KitRaft counterServer = new KitRaft(groupConfig, nodeConfig, db); try { App app = new App(Integer.parseInt(portStr)); app.kitRaft = counterServer; } catch (IOException ioe) { System.err.println("Couldn't start server:\n" + ioe); } rt = RouteTable.getInstance(); }
Example #19
Source File: AtomicClient.java From sofa-jraft with Apache License 2.0 | 4 votes |
private PeerId getPeer(String key) throws InterruptedException, TimeoutException { final String groupId = getGroupId(key); this.refreshConf(groupId); final List<PeerId> peers = RouteTable.getInstance().getConfiguration(groupId).getPeers(); return peers.get(ThreadLocalRandom.current().nextInt(peers.size())); }
Example #20
Source File: AtomicClient.java From sofa-jraft with Apache License 2.0 | 4 votes |
private PeerId getLeader(String key) throws InterruptedException, TimeoutException { final String groupId = getGroupId(key); refreshLeader(groupId); return RouteTable.getInstance().selectLeader(groupId); }
Example #21
Source File: AtomicClient.java From sofa-jraft with Apache License 2.0 | 4 votes |
private void refreshLeader(String groupId) throws InterruptedException, TimeoutException { RouteTable.getInstance().refreshLeader(cliClientService, groupId, cliOptions.getRpcDefaultTimeout()); }
Example #22
Source File: AtomicClient.java From sofa-jraft with Apache License 2.0 | 4 votes |
private void refreshConf(String groupId) throws InterruptedException, TimeoutException { RouteTable.getInstance().refreshConfiguration(cliClientService, groupId, cliOptions.getRpcDefaultTimeout()); }
Example #23
Source File: AtomicClient.java From sofa-jraft with Apache License 2.0 | 4 votes |
public void shutdown() { this.cliClientService.shutdown(); for (final String groupId : groups.values()) { RouteTable.getInstance().removeGroup(groupId); } }