Search in sources :

Example 1 with PlacementDriverClient

use of com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient in project sofa-jraft by sofastack.

the class ChaosTestCluster method addPeer.

public synchronized void addPeer(final PeerId peerId) {
    if (this.peerIds.contains(peerId)) {
        throw new RuntimeException("peerId is exist: " + peerId);
    }
    this.peerIds.add(peerId);
    final Configuration conf = new Configuration(this.peerIds);
    final String initialServerList = conf.toString();
    final PlacementDriverOptions pdOpts = // use a fake pd
    PlacementDriverOptionsConfigured.newConfigured().withFake(true).config();
    final StoreEngineOptions storeOpts = // 
    StoreEngineOptionsConfigured.newConfigured().withStorageType(// 
    this.storageType).withRocksDBOptions(// 
    RocksDBOptionsConfigured.newConfigured().withDbPath(DB_PATH).config()).withRaftDataPath(// 
    RAFT_DATA_PATH).withServerAddress(// 
    peerId.getEndpoint()).config();
    final RheaKVStoreOptions opts = // 
    RheaKVStoreOptionsConfigured.newConfigured().withClusterName(// 
    "chaos_test").withInitialServerList(initialServerList).withStoreEngineOptions(// 
    storeOpts).withPlacementDriverOptions(// 
    pdOpts).config();
    BatchingOptions batchingOptions = opts.getBatchingOptions();
    if (batchingOptions == null) {
        batchingOptions = new BatchingOptions();
    }
    batchingOptions.setAllowBatching(this.allowBatching);
    opts.setBatchingOptions(batchingOptions);
    final RheaKVStore store = new DefaultRheaKVStore();
    if (!store.init(opts)) {
        throw new IllegalStateException("fail to init store with options: " + opts);
    }
    final RheaKVStore leader = getLeaderStore();
    final PlacementDriverClient pdClient = leader.getPlacementDriverClient();
    if (!pdClient.addReplica(Constants.DEFAULT_REGION_ID, JRaftHelper.toPeer(peerId), true)) {
        throw new RuntimeException("fail to add peer: " + peerId);
    }
    this.stores.add(store);
    awaitLeader();
}
Also used : RheaKVStoreOptions(com.alipay.sofa.jraft.rhea.options.RheaKVStoreOptions) RheaKVStore(com.alipay.sofa.jraft.rhea.client.RheaKVStore) DefaultRheaKVStore(com.alipay.sofa.jraft.rhea.client.DefaultRheaKVStore) BatchingOptions(com.alipay.sofa.jraft.rhea.options.BatchingOptions) PlacementDriverOptions(com.alipay.sofa.jraft.rhea.options.PlacementDriverOptions) Configuration(com.alipay.sofa.jraft.conf.Configuration) PlacementDriverClient(com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient) StoreEngineOptions(com.alipay.sofa.jraft.rhea.options.StoreEngineOptions) DefaultRheaKVStore(com.alipay.sofa.jraft.rhea.client.DefaultRheaKVStore)

Example 2 with PlacementDriverClient

use of com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient in project sofa-jraft by sofastack.

the class ChaosTestCluster method randomTransferLeader.

public synchronized void randomTransferLeader() {
    final RheaKVStore leader = getLeaderStore();
    final Endpoint leaderEndpoint = getSelfEndpoint(leader);
    final PlacementDriverClient pdClient = leader.getPlacementDriverClient();
    final Peer randomPeer = JRaftHelper.toPeer(getRandomPeer());
    boolean result = pdClient.transferLeader(Constants.DEFAULT_REGION_ID, randomPeer, false);
    if (!result) {
        throw new RuntimeException("fail to transfer leader [" + leaderEndpoint + " --> " + randomPeer);
    }
    LOG.info("Transfer leader from {} to {}", leaderEndpoint, randomPeer.getEndpoint());
}
Also used : RheaKVStore(com.alipay.sofa.jraft.rhea.client.RheaKVStore) DefaultRheaKVStore(com.alipay.sofa.jraft.rhea.client.DefaultRheaKVStore) Endpoint(com.alipay.sofa.jraft.util.Endpoint) Peer(com.alipay.sofa.jraft.rhea.metadata.Peer) PlacementDriverClient(com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient)

Example 3 with PlacementDriverClient

use of com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient in project sofa-jraft by sofastack.

the class RheaBenchmarkCluster method start.

protected void start() throws IOException, InterruptedException {
    SystemPropertyUtil.setProperty(Configs.NETTY_BUFFER_LOW_WATERMARK, Integer.toString(256 * 1024));
    SystemPropertyUtil.setProperty(Configs.NETTY_BUFFER_HIGH_WATERMARK, Integer.toString(512 * 1024));
    File file = new File("benchmark_rhea_db");
    if (file.exists()) {
        FileUtils.forceDelete(file);
    }
    file = new File("benchmark_rhea_db");
    if (file.mkdir()) {
        this.tempDbPath = file.getAbsolutePath();
        System.out.println("make dir: " + this.tempDbPath);
    }
    file = new File("benchmark_rhea_raft");
    if (file.exists()) {
        FileUtils.forceDelete(file);
    }
    file = new File("benchmark_rhea_raft");
    if (file.mkdir()) {
        this.tempRaftPath = file.getAbsolutePath();
        System.out.println("make dir: " + this.tempRaftPath);
    }
    for (String c : CONF) {
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        final RheaKVStoreOptions opts = mapper.readValue(new File(c), RheaKVStoreOptions.class);
        RheaKVStore rheaKVStore = new DefaultRheaKVStore();
        if (rheaKVStore.init(opts)) {
            stores.add(rheaKVStore);
        } else {
            throw new RuntimeException("Fail to init rhea kv store witch conf: " + c);
        }
    }
    PlacementDriverClient pdClient = stores.get(0).getPlacementDriverClient();
    Endpoint leader1 = pdClient.getLeader(1, true, 10000);
    System.out.println("The region 1 leader is: " + leader1);
// Endpoint leader2 = pdClient.getLeader(2, true, 10000);
// System.out.println("The region 2 leader is: " + leader2);
// Endpoint leader3 = pdClient.getLeader(3, true, 10000);
// System.out.println("The region 3 leader is: " + leader3);
}
Also used : RheaKVStoreOptions(com.alipay.sofa.jraft.rhea.options.RheaKVStoreOptions) DefaultRheaKVStore(com.alipay.sofa.jraft.rhea.client.DefaultRheaKVStore) RheaKVStore(com.alipay.sofa.jraft.rhea.client.RheaKVStore) Endpoint(com.alipay.sofa.jraft.util.Endpoint) PlacementDriverClient(com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) File(java.io.File) DefaultRheaKVStore(com.alipay.sofa.jraft.rhea.client.DefaultRheaKVStore) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with PlacementDriverClient

use of com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient 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 5 with PlacementDriverClient

use of com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient in project sofa-jraft by sofastack.

the class RheaKVTestCluster method start.

protected void start() throws IOException, InterruptedException {
    System.out.println("RheaKVTestCluster init ...");
    File file = new File("rhea_pd_db");
    if (file.exists()) {
        FileUtils.forceDelete(file);
    }
    file = new File("rhea_pd_db");
    if (file.mkdir()) {
        this.tempDbPath = file.getAbsolutePath();
        System.out.println("make dir: " + this.tempDbPath);
    }
    file = new File("rhea_pd_raft");
    if (file.exists()) {
        FileUtils.forceDelete(file);
    }
    file = new File("rhea_pd_raft");
    if (file.mkdir()) {
        this.tempRaftPath = file.getAbsolutePath();
        System.out.println("make dir: " + this.tempRaftPath);
    }
    final Set<Long> regionIds = new HashSet<>();
    for (final String c : CONF) {
        final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        final InputStream in = RheaKVTestCluster.class.getResourceAsStream(c);
        final RheaKVStoreOptions opts = mapper.readValue(in, RheaKVStoreOptions.class);
        for (final RegionEngineOptions rOpts : opts.getStoreEngineOptions().getRegionEngineOptionsList()) {
            regionIds.add(rOpts.getRegionId());
        }
        final RheaKVStore rheaKVStore = new DefaultRheaKVStore();
        if (rheaKVStore.init(opts)) {
            stores.add(rheaKVStore);
        } else {
            System.err.println("Fail to init rhea kv store witch conf: " + c);
        }
    }
    final PlacementDriverClient pdClient = stores.get(0).getPlacementDriverClient();
    for (final Long regionId : regionIds) {
        final Endpoint leader = pdClient.getLeader(regionId, true, 10000);
        System.out.println("The region " + regionId + " leader is: " + leader);
    }
}
Also used : DefaultRheaKVStore(com.alipay.sofa.jraft.rhea.client.DefaultRheaKVStore) RheaKVStore(com.alipay.sofa.jraft.rhea.client.RheaKVStore) InputStream(java.io.InputStream) PlacementDriverClient(com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient) RegionEngineOptions(com.alipay.sofa.jraft.rhea.options.RegionEngineOptions) DefaultRheaKVStore(com.alipay.sofa.jraft.rhea.client.DefaultRheaKVStore) RheaKVStoreOptions(com.alipay.sofa.jraft.rhea.options.RheaKVStoreOptions) Endpoint(com.alipay.sofa.jraft.util.Endpoint) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashSet(java.util.HashSet)

Aggregations

PlacementDriverClient (com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient)7 Endpoint (com.alipay.sofa.jraft.util.Endpoint)6 DefaultRheaKVStore (com.alipay.sofa.jraft.rhea.client.DefaultRheaKVStore)5 RheaKVStore (com.alipay.sofa.jraft.rhea.client.RheaKVStore)5 RheaKVStoreOptions (com.alipay.sofa.jraft.rhea.options.RheaKVStoreOptions)3 Configuration (com.alipay.sofa.jraft.conf.Configuration)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)2 File (java.io.File)2 PeerId (com.alipay.sofa.jraft.entity.PeerId)1 Peer (com.alipay.sofa.jraft.rhea.metadata.Peer)1 BatchingOptions (com.alipay.sofa.jraft.rhea.options.BatchingOptions)1 PlacementDriverOptions (com.alipay.sofa.jraft.rhea.options.PlacementDriverOptions)1 RegionEngineOptions (com.alipay.sofa.jraft.rhea.options.RegionEngineOptions)1 RegionRouteTableOptions (com.alipay.sofa.jraft.rhea.options.RegionRouteTableOptions)1 StoreEngineOptions (com.alipay.sofa.jraft.rhea.options.StoreEngineOptions)1 InputStream (java.io.InputStream)1 ArrayDeque (java.util.ArrayDeque)1 HashSet (java.util.HashSet)1