use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class CounterServer method main.
public static void main(final String[] args) throws IOException {
if (args.length != 4) {
System.out.println("Usage : java com.alipay.sofa.jraft.example.counter.CounterServer {dataPath} {groupId} {serverId} {initConf}");
System.out.println("Example: java com.alipay.sofa.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);
}
final String dataPath = args[0];
final String groupId = args[1];
final String serverIdStr = args[2];
final String initConfStr = args[3];
final NodeOptions nodeOptions = new NodeOptions();
// for test, modify some params
// set election timeout to 1s
nodeOptions.setElectionTimeoutMs(1000);
// disable CLI service。
nodeOptions.setDisableCli(false);
// do snapshot every 30s
nodeOptions.setSnapshotIntervalSecs(30);
// parse server address
final PeerId serverId = new PeerId();
if (!serverId.parse(serverIdStr)) {
throw new IllegalArgumentException("Fail to parse serverId:" + serverIdStr);
}
final Configuration initConf = new Configuration();
if (!initConf.parse(initConfStr)) {
throw new IllegalArgumentException("Fail to parse initConf:" + initConfStr);
}
// set cluster configuration
nodeOptions.setInitialConf(initConf);
// start raft server
final CounterServer counterServer = new CounterServer(dataPath, groupId, serverId, nodeOptions);
System.out.println("Started counter server at port:" + counterServer.getNode().getNodeId().getPeerId().getPort());
// GrpcServer need block to prevent process exit
CounterGrpcHelper.blockUntilShutdown();
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class AbstractPlacementDriverClient method initRouteTableByRegion.
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);
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class AbstractPlacementDriverClient method getLuckyPeer.
@Override
public Endpoint getLuckyPeer(final long regionId, final boolean forceRefresh, final long timeoutMillis, final Endpoint unExpect) {
final String raftGroupId = JRaftHelper.getJRaftGroupId(this.clusterName, regionId);
final RouteTable routeTable = RouteTable.getInstance();
if (forceRefresh) {
final long deadline = System.currentTimeMillis() + timeoutMillis;
final StringBuilder error = new StringBuilder();
// or in the 'leader-transfer' state, it needs to be re-tried
for (; ; ) {
try {
final Status st = routeTable.refreshConfiguration(this.cliClientService, raftGroupId, 5000);
if (st.isOk()) {
break;
}
error.append(st.toString());
} catch (final InterruptedException e) {
ThrowUtil.throwException(e);
} catch (final TimeoutException e) {
error.append(e.getMessage());
}
if (System.currentTimeMillis() < deadline) {
LOG.debug("Fail to get peers, retry again, {}.", error);
error.append(", ");
try {
Thread.sleep(5);
} catch (final InterruptedException e) {
ThrowUtil.throwException(e);
}
} else {
throw new RouteTableException(error.toString());
}
}
}
final Configuration configs = routeTable.getConfiguration(raftGroupId);
if (configs == null) {
throw new RouteTableException("empty configs in group: " + raftGroupId);
}
final List<PeerId> peerList = configs.getPeers();
if (peerList == null || peerList.isEmpty()) {
throw new RouteTableException("empty peers in group: " + raftGroupId);
}
final int size = peerList.size();
if (size == 1) {
return peerList.get(0).getEndpoint();
}
final RoundRobinLoadBalancer balancer = RoundRobinLoadBalancer.getInstance(regionId);
for (int i = 0; i < size; i++) {
final PeerId candidate = balancer.select(peerList);
final Endpoint luckyOne = candidate.getEndpoint();
if (!luckyOne.equals(unExpect)) {
return luckyOne;
}
}
throw new RouteTableException("have no choice in group(peers): " + raftGroupId);
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class RouteTableTest method testRefreshLeaderWhenFirstPeerDown.
@Test
public void testRefreshLeaderWhenFirstPeerDown() throws Exception {
final RouteTable rt = RouteTable.getInstance();
rt.updateConfiguration(groupId, new Configuration(cluster.getPeers()));
assertTrue(rt.refreshLeader(cliClientService, groupId, 10000).isOk());
cluster.stop(cluster.getPeers().get(0).getEndpoint());
Thread.sleep(1000);
this.cluster.waitLeader();
assertTrue(rt.refreshLeader(cliClientService, groupId, 10000).isOk());
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class RouteTableTest method testUpdateConfSelectLeader.
@Test
public void testUpdateConfSelectLeader() throws Exception {
final RouteTable rt = RouteTable.getInstance();
assertNull(rt.getConfiguration(groupId));
rt.updateConfiguration(groupId, new Configuration(cluster.getPeers()));
assertEquals(rt.getConfiguration(groupId), new Configuration(cluster.getPeers()));
assertNull(rt.selectLeader(groupId));
assertTrue(rt.refreshLeader(cliClientService, groupId, 10000).isOk());
final PeerId leader = rt.selectLeader(groupId);
assertEquals(leader, cluster.getLeader().getNodeId().getPeerId());
}
Aggregations