Search in sources :

Example 1 with RheaKVStore

use of com.alipay.sofa.jraft.rhea.client.RheaKVStore 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 RheaKVStore

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

the class ChaosTestCluster method start.

public synchronized void start() {
    deleteFiles();
    final Configuration conf = new Configuration(this.peerIds);
    final String initialServerList = conf.toString();
    for (final PeerId p : conf.listPeers()) {
        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(// 
        p.getEndpoint()).withLeastKeysOnSplit(// 
        10).config();
        final RheaKVStoreOptions opts = // 
        RheaKVStoreOptionsConfigured.newConfigured().withClusterName(// 
        CLUSTER_NAME).withInitialServerList(initialServerList).withOnlyLeaderRead(// 
        this.onlyLeaderRead).withStoreEngineOptions(// 
        storeOpts).withPlacementDriverOptions(// 
        pdOpts).withFailoverRetries(// 
        30).withFutureTimeoutMillis(// 
        TimeUnit.SECONDS.toMillis(60)).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);
        }
        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) StoreEngineOptions(com.alipay.sofa.jraft.rhea.options.StoreEngineOptions) DefaultRheaKVStore(com.alipay.sofa.jraft.rhea.client.DefaultRheaKVStore) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 3 with RheaKVStore

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

the class ChaosTestCluster method getByStorePeer.

public synchronized RheaKVStore getByStorePeer(final PeerId peerId) {
    awaitLeader();
    final Endpoint endpoint = JRaftHelper.toPeer(peerId).getEndpoint();
    for (final RheaKVStore store : this.stores) {
        if (endpoint.equals(getSelfEndpoint(store))) {
            return store;
        }
    }
    throw new RuntimeException("fail to get peer: " + peerId);
}
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)

Example 4 with RheaKVStore

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

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

the class RheaHeartbeatTest method putAndGetValue.

private void putAndGetValue() {
    final RheaKVStore store = getLeaderStore(ThreadLocalRandom.current().nextInt(1, 2));
    final String key = UUID.randomUUID().toString();
    final byte[] value = makeValue(UUID.randomUUID().toString());
    store.bPut(key, value);
    final byte[] newValue = store.bGet(key);
    Assert.assertArrayEquals(value, newValue);
}
Also used : RheaKVStore(com.alipay.sofa.jraft.rhea.client.RheaKVStore)

Aggregations

RheaKVStore (com.alipay.sofa.jraft.rhea.client.RheaKVStore)18 DefaultRheaKVStore (com.alipay.sofa.jraft.rhea.client.DefaultRheaKVStore)12 RheaKVStoreOptions (com.alipay.sofa.jraft.rhea.options.RheaKVStoreOptions)6 Endpoint (com.alipay.sofa.jraft.util.Endpoint)6 PlacementDriverClient (com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient)5 Test (org.junit.Test)5 Configuration (com.alipay.sofa.jraft.conf.Configuration)4 File (java.io.File)4 PeerId (com.alipay.sofa.jraft.entity.PeerId)3 Status (com.alipay.sofa.jraft.Status)2 CliOptions (com.alipay.sofa.jraft.option.CliOptions)2 RheaKVCliService (com.alipay.sofa.jraft.rhea.client.RheaKVCliService)2 BatchingOptions (com.alipay.sofa.jraft.rhea.options.BatchingOptions)2 PlacementDriverOptions (com.alipay.sofa.jraft.rhea.options.PlacementDriverOptions)2 StoreEngineOptions (com.alipay.sofa.jraft.rhea.options.StoreEngineOptions)2 NamedThreadFactory (com.alipay.sofa.jraft.util.NamedThreadFactory)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2