Search in sources :

Example 1 with RegionRouteTableOptions

use of com.alipay.sofa.jraft.rhea.options.RegionRouteTableOptions in project sofa-jraft by sofastack.

the class MultiRegionRouteTableOptionsConfigured method getOrCreateOptsById.

private RegionRouteTableOptions getOrCreateOptsById(final Long regionId) {
    Requires.requireNonNull(regionId, "regionId");
    RegionRouteTableOptions opts = this.optsTable.get(regionId);
    if (opts != null) {
        return opts;
    }
    opts = new RegionRouteTableOptions();
    opts.setRegionId(regionId);
    this.optsTable.put(regionId, opts);
    return opts;
}
Also used : RegionRouteTableOptions(com.alipay.sofa.jraft.rhea.options.RegionRouteTableOptions)

Example 2 with RegionRouteTableOptions

use of com.alipay.sofa.jraft.rhea.options.RegionRouteTableOptions in project sofa-jraft by sofastack.

the class BenchmarkClient method main.

public static void main(final String[] args) {
    if (args.length < 7) {
        LOG.error("Args: [initialServerList], [configPath], [threads], [writeRatio], [readRatio], [valueSize] are needed.");
        System.exit(-1);
    }
    final String initialServerList = args[1];
    final String configPath = args[2];
    final int threads = Integer.parseInt(args[3]);
    final int writeRatio = Integer.parseInt(args[4]);
    final int readRatio = Integer.parseInt(args[5]);
    final int valueSize = Integer.parseInt(args[6]);
    final RheaKVStoreOptions opts = Yaml.readConfig(configPath);
    opts.setInitialServerList(initialServerList);
    final RheaKVStore rheaKVStore = new DefaultRheaKVStore();
    if (!rheaKVStore.init(opts)) {
        LOG.error("Fail to init [RheaKVStore]");
        System.exit(-1);
    }
    final List<RegionRouteTableOptions> regionRouteTableOptionsList = opts.getPlacementDriverOptions().getRegionRouteTableOptionsList();
    rebalance(rheaKVStore, initialServerList, regionRouteTableOptionsList);
    rheaKVStore.bPut("benchmark", BytesUtil.writeUtf8("benchmark start at: " + new Date()));
    LOG.info(BytesUtil.readUtf8(rheaKVStore.bGet("benchmark")));
    // 
    ConsoleReporter.forRegistry(KVMetrics.metricRegistry()).build().start(30, TimeUnit.SECONDS);
    LOG.info("Start benchmark...");
    startBenchmark(rheaKVStore, threads, writeRatio, readRatio, valueSize, regionRouteTableOptionsList);
}
Also used : RegionRouteTableOptions(com.alipay.sofa.jraft.rhea.options.RegionRouteTableOptions) RheaKVStoreOptions(com.alipay.sofa.jraft.rhea.options.RheaKVStoreOptions) RheaKVStore(com.alipay.sofa.jraft.rhea.client.RheaKVStore) DefaultRheaKVStore(com.alipay.sofa.jraft.rhea.client.DefaultRheaKVStore) DefaultRheaKVStore(com.alipay.sofa.jraft.rhea.client.DefaultRheaKVStore) Endpoint(com.alipay.sofa.jraft.util.Endpoint) Date(java.util.Date)

Example 3 with RegionRouteTableOptions

use of com.alipay.sofa.jraft.rhea.options.RegionRouteTableOptions 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 4 with RegionRouteTableOptions

use of com.alipay.sofa.jraft.rhea.options.RegionRouteTableOptions in project sofa-jraft by sofastack.

the class Client method init.

public void init() {
    final List<RegionRouteTableOptions> regionRouteTableOptionsList = MultiRegionRouteTableOptionsConfigured.newConfigured().withInitialServerList(-1L, /* default id */
    Configs.ALL_NODE_ADDRESSES).config();
    final PlacementDriverOptions pdOpts = // 
    PlacementDriverOptionsConfigured.newConfigured().withFake(// 
    true).withRegionRouteTableOptionsList(// 
    regionRouteTableOptionsList).config();
    final RheaKVStoreOptions opts = // 
    RheaKVStoreOptionsConfigured.newConfigured().withClusterName(// 
    Configs.CLUSTER_NAME).withPlacementDriverOptions(// 
    pdOpts).config();
    System.out.println(opts);
    rheaKVStore.init(opts);
}
Also used : RegionRouteTableOptions(com.alipay.sofa.jraft.rhea.options.RegionRouteTableOptions) RheaKVStoreOptions(com.alipay.sofa.jraft.rhea.options.RheaKVStoreOptions) PlacementDriverOptions(com.alipay.sofa.jraft.rhea.options.PlacementDriverOptions)

Example 5 with RegionRouteTableOptions

use of com.alipay.sofa.jraft.rhea.options.RegionRouteTableOptions in project sofa-jraft by sofastack.

the class AbstractPlacementDriverClient method init.

@Override
public synchronized boolean init(final PlacementDriverOptions opts) {
    initCli(opts.getCliOptions());
    this.pdRpcService = new DefaultPlacementDriverRpcService(this);
    RpcOptions rpcOpts = opts.getPdRpcOptions();
    if (rpcOpts == null) {
        rpcOpts = RpcOptionsConfigured.newDefaultConfig();
        rpcOpts.setCallbackExecutorCorePoolSize(0);
        rpcOpts.setCallbackExecutorMaximumPoolSize(0);
    }
    if (!this.pdRpcService.init(rpcOpts)) {
        LOG.error("Fail to init [PlacementDriverRpcService].");
        return false;
    }
    // region route table
    final List<RegionRouteTableOptions> regionRouteTableOptionsList = opts.getRegionRouteTableOptionsList();
    if (regionRouteTableOptionsList != null) {
        final String initialServerList = opts.getInitialServerList();
        for (final RegionRouteTableOptions regionRouteTableOpts : regionRouteTableOptionsList) {
            if (Strings.isBlank(regionRouteTableOpts.getInitialServerList())) {
                // if blank, extends parent's value
                regionRouteTableOpts.setInitialServerList(initialServerList);
            }
            initRouteTableByRegion(regionRouteTableOpts);
        }
    }
    return true;
}
Also used : RegionRouteTableOptions(com.alipay.sofa.jraft.rhea.options.RegionRouteTableOptions) RpcOptions(com.alipay.sofa.jraft.rhea.options.RpcOptions)

Aggregations

RegionRouteTableOptions (com.alipay.sofa.jraft.rhea.options.RegionRouteTableOptions)5 RheaKVStoreOptions (com.alipay.sofa.jraft.rhea.options.RheaKVStoreOptions)2 Endpoint (com.alipay.sofa.jraft.util.Endpoint)2 Configuration (com.alipay.sofa.jraft.conf.Configuration)1 PeerId (com.alipay.sofa.jraft.entity.PeerId)1 DefaultRheaKVStore (com.alipay.sofa.jraft.rhea.client.DefaultRheaKVStore)1 RheaKVStore (com.alipay.sofa.jraft.rhea.client.RheaKVStore)1 PlacementDriverClient (com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient)1 PlacementDriverOptions (com.alipay.sofa.jraft.rhea.options.PlacementDriverOptions)1 RpcOptions (com.alipay.sofa.jraft.rhea.options.RpcOptions)1 ArrayDeque (java.util.ArrayDeque)1 Date (java.util.Date)1