Search in sources :

Example 31 with Configuration

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);
    }
}
Also used : LearnersOpResponse(com.alipay.sofa.jraft.rpc.CliRequests.LearnersOpResponse) Configuration(com.alipay.sofa.jraft.conf.Configuration) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 32 with Configuration

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);
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 33 with Configuration

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));
        }
    }
}
Also used : RegionRouteTableOptions(com.alipay.sofa.jraft.rhea.options.RegionRouteTableOptions) Configuration(com.alipay.sofa.jraft.conf.Configuration) Endpoint(com.alipay.sofa.jraft.util.Endpoint) PlacementDriverClient(com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient) Endpoint(com.alipay.sofa.jraft.util.Endpoint) ArrayDeque(java.util.ArrayDeque) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 34 with Configuration

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);
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) CliClientServiceImpl(com.alipay.sofa.jraft.rpc.impl.cli.CliClientServiceImpl) CountDownLatch(java.util.concurrent.CountDownLatch) CliOptions(com.alipay.sofa.jraft.option.CliOptions) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 35 with Configuration

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());
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) Test(org.junit.Test)

Aggregations

Configuration (com.alipay.sofa.jraft.conf.Configuration)81 PeerId (com.alipay.sofa.jraft.entity.PeerId)54 Test (org.junit.Test)28 Node (com.alipay.sofa.jraft.Node)20 Endpoint (com.alipay.sofa.jraft.util.Endpoint)20 Status (com.alipay.sofa.jraft.Status)18 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)18 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)9 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)8 File (java.io.File)8 ArrayList (java.util.ArrayList)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)7 ConfigurationEntry (com.alipay.sofa.jraft.conf.ConfigurationEntry)5 Task (com.alipay.sofa.jraft.entity.Task)5 CliOptions (com.alipay.sofa.jraft.option.CliOptions)5 LogId (com.alipay.sofa.jraft.entity.LogId)4 RheaKVStore (com.alipay.sofa.jraft.rhea.client.RheaKVStore)4 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)3 RaftException (com.alipay.sofa.jraft.error.RaftException)3