use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class CliServiceImpl method processLearnersOpResponse.
private Status processLearnersOpResponse(final String groupId, final Message result, final String fmt, final Object... formatArgs) {
if (result instanceof LearnersOpResponse) {
final LearnersOpResponse resp = (LearnersOpResponse) result;
final Configuration oldConf = new Configuration();
for (final String peerIdStr : resp.getOldLearnersList()) {
final PeerId oldPeer = new PeerId();
oldPeer.parse(peerIdStr);
oldConf.addLearner(oldPeer);
}
final Configuration newConf = new Configuration();
for (final String peerIdStr : resp.getNewLearnersList()) {
final PeerId newPeer = new PeerId();
newPeer.parse(peerIdStr);
newConf.addLearner(newPeer);
}
LOG.info("Learners of replication group {} changed from {} to {} after {}.", groupId, oldConf, newConf, String.format(fmt, formatArgs));
return Status.OK();
} else {
return statusFromResponse(result);
}
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class CliServiceImpl method recordConfigurationChange.
private void recordConfigurationChange(final String groupId, final List<String> oldPeersList, final List<String> newPeersList) {
final Configuration oldConf = new Configuration();
for (final String peerIdStr : oldPeersList) {
final PeerId oldPeer = new PeerId();
oldPeer.parse(peerIdStr);
oldConf.addPeer(oldPeer);
}
final Configuration newConf = new Configuration();
for (final String peerIdStr : newPeersList) {
final PeerId newPeer = new PeerId();
newPeer.parse(peerIdStr);
newConf.addPeer(newPeer);
}
LOG.info("Configuration of replication group {} changed from {} to {}.", groupId, oldConf, newConf);
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class BenchmarkClient method rebalance.
// Because we use fake PD, so we need manual rebalance
public static void rebalance(final RheaKVStore rheaKVStore, final String initialServerList, final List<RegionRouteTableOptions> regionRouteTableOptionsList) {
final PlacementDriverClient pdClient = rheaKVStore.getPlacementDriverClient();
final Configuration configuration = new Configuration();
configuration.parse(initialServerList);
final int serverSize = configuration.size();
final int regionSize = regionRouteTableOptionsList.size();
final int regionSizePerServer = regionSize / serverSize;
final Queue<Long> regions = new ArrayDeque<>();
for (final RegionRouteTableOptions r : regionRouteTableOptionsList) {
regions.add(r.getRegionId());
}
final Map<PeerId, Integer> peerMap = Maps.newHashMap();
for (; ; ) {
final Long regionId = regions.poll();
if (regionId == null) {
break;
}
PeerId peerId;
try {
final Endpoint endpoint = pdClient.getLeader(regionId, true, 10000);
if (endpoint == null) {
continue;
}
peerId = new PeerId(endpoint, 0);
LOG.info("Region {} leader is {}", regionId, peerId);
} catch (final Exception e) {
regions.add(regionId);
continue;
}
final Integer size = peerMap.get(peerId);
if (size == null) {
peerMap.put(peerId, 1);
continue;
}
if (size < regionSizePerServer) {
peerMap.put(peerId, size + 1);
continue;
}
for (final PeerId p : configuration.listPeers()) {
final Integer pSize = peerMap.get(p);
if (pSize != null && pSize >= regionSizePerServer) {
continue;
}
try {
pdClient.transferLeader(regionId, JRaftHelper.toPeer(p), true);
LOG.info("Region {} transfer leader to {}", regionId, p);
regions.add(regionId);
break;
} catch (final Exception e) {
LOG.error("Fail to transfer leader to {}", p);
}
}
}
for (final RegionRouteTableOptions r : regionRouteTableOptionsList) {
final Long regionId = r.getRegionId();
try {
final Endpoint endpoint = pdClient.getLeader(regionId, true, 10000);
LOG.info("Finally, the region: {} leader is: {}", regionId, endpoint);
} catch (final Exception e) {
LOG.error("Fail to get leader: {}", StackTraceUtil.stackTrace(e));
}
}
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class CounterClient method main.
public static void main(final String[] args) throws Exception {
if (args.length != 2) {
System.out.println("Usage : 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];
CounterGrpcHelper.initGRpc();
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);
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class RouteTableTest method testRefreshConfigurationFail.
@Test
public void testRefreshConfigurationFail() throws Exception {
cluster.stopAll();
final RouteTable rt = RouteTable.getInstance();
rt.updateConfiguration(groupId, new Configuration(cluster.getPeers()));
final Status st = rt.refreshConfiguration(cliClientService, groupId, 10000);
assertFalse(st.isOk());
}
Aggregations