Search in sources :

Example 1 with StoreEngineOptions

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

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

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

the class DefaultRheaKVStore method init.

@Override
public synchronized boolean init(final RheaKVStoreOptions opts) {
    if (this.started) {
        LOG.info("[DefaultRheaKVStore] already started.");
        return true;
    }
    DescriberManager.getInstance().addDescriber(RouteTable.getInstance());
    this.opts = opts;
    // init placement driver
    final PlacementDriverOptions pdOpts = opts.getPlacementDriverOptions();
    final String clusterName = opts.getClusterName();
    Requires.requireNonNull(pdOpts, "opts.placementDriverOptions");
    Requires.requireNonNull(clusterName, "opts.clusterName");
    if (Strings.isBlank(pdOpts.getInitialServerList())) {
        // if blank, extends parent's value
        pdOpts.setInitialServerList(opts.getInitialServerList());
    }
    if (pdOpts.isFake()) {
        this.pdClient = new FakePlacementDriverClient(opts.getClusterId(), clusterName);
    } else {
        this.pdClient = new RemotePlacementDriverClient(opts.getClusterId(), clusterName);
    }
    if (!this.pdClient.init(pdOpts)) {
        LOG.error("Fail to init [PlacementDriverClient].");
        return false;
    }
    // init compress strategies
    ZipStrategyManager.init(opts);
    // init store engine
    final StoreEngineOptions stOpts = opts.getStoreEngineOptions();
    if (stOpts != null) {
        stOpts.setInitialServerList(opts.getInitialServerList());
        this.storeEngine = new StoreEngine(this.pdClient, this.stateListenerContainer);
        if (!this.storeEngine.init(stOpts)) {
            LOG.error("Fail to init [StoreEngine].");
            return false;
        }
    }
    final Endpoint selfEndpoint = this.storeEngine == null ? null : this.storeEngine.getSelfEndpoint();
    final RpcOptions rpcOpts = opts.getRpcOptions();
    Requires.requireNonNull(rpcOpts, "opts.rpcOptions");
    this.rheaKVRpcService = new DefaultRheaKVRpcService(this.pdClient, selfEndpoint) {

        @Override
        public Endpoint getLeader(final long regionId, final boolean forceRefresh, final long timeoutMillis) {
            final Endpoint leader = getLeaderByRegionEngine(regionId);
            if (leader != null) {
                return leader;
            }
            return super.getLeader(regionId, forceRefresh, timeoutMillis);
        }
    };
    if (!this.rheaKVRpcService.init(rpcOpts)) {
        LOG.error("Fail to init [RheaKVRpcService].");
        return false;
    }
    this.failoverRetries = opts.getFailoverRetries();
    this.futureTimeoutMillis = opts.getFutureTimeoutMillis();
    this.onlyLeaderRead = opts.isOnlyLeaderRead();
    if (opts.isUseParallelKVExecutor()) {
        final int numWorkers = Utils.cpus();
        final int bufSize = numWorkers << 4;
        final String name = "parallel-kv-executor";
        final ThreadFactory threadFactory = Constants.THREAD_AFFINITY_ENABLED ? new AffinityNamedThreadFactory(name, true) : new NamedThreadFactory(name, true);
        this.kvDispatcher = new TaskDispatcher(bufSize, numWorkers, WaitStrategyType.LITE_BLOCKING_WAIT, threadFactory);
    }
    this.batchingOpts = opts.getBatchingOptions();
    if (this.batchingOpts.isAllowBatching()) {
        this.getBatching = new GetBatching(KeyEvent::new, "get_batching", new GetBatchingHandler("get", false));
        this.getBatchingOnlySafe = new GetBatching(KeyEvent::new, "get_batching_only_safe", new GetBatchingHandler("get_only_safe", true));
        this.putBatching = new PutBatching(KVEvent::new, "put_batching", new PutBatchingHandler("put"));
    }
    LOG.info("[DefaultRheaKVStore] start successfully, options: {}.", opts);
    return this.started = true;
}
Also used : NamedThreadFactory(com.alipay.sofa.jraft.rhea.util.concurrent.NamedThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) AffinityNamedThreadFactory(com.alipay.sofa.jraft.rhea.util.concurrent.AffinityNamedThreadFactory) PlacementDriverOptions(com.alipay.sofa.jraft.rhea.options.PlacementDriverOptions) NamedThreadFactory(com.alipay.sofa.jraft.rhea.util.concurrent.NamedThreadFactory) AffinityNamedThreadFactory(com.alipay.sofa.jraft.rhea.util.concurrent.AffinityNamedThreadFactory) RemotePlacementDriverClient(com.alipay.sofa.jraft.rhea.client.pd.RemotePlacementDriverClient) Endpoint(com.alipay.sofa.jraft.util.Endpoint) StoreEngine(com.alipay.sofa.jraft.rhea.StoreEngine) RpcOptions(com.alipay.sofa.jraft.rhea.options.RpcOptions) Endpoint(com.alipay.sofa.jraft.util.Endpoint) TaskDispatcher(com.alipay.sofa.jraft.rhea.util.concurrent.disruptor.TaskDispatcher) FakePlacementDriverClient(com.alipay.sofa.jraft.rhea.client.pd.FakePlacementDriverClient) StoreEngineOptions(com.alipay.sofa.jraft.rhea.options.StoreEngineOptions) AffinityNamedThreadFactory(com.alipay.sofa.jraft.rhea.util.concurrent.AffinityNamedThreadFactory)

Example 4 with StoreEngineOptions

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

the class Server2 method main.

public static void main(final String[] args) throws Exception {
    final PlacementDriverOptions pdOpts = PlacementDriverOptionsConfigured.newConfigured().withFake(// use a fake pd
    true).config();
    final StoreEngineOptions storeOpts = // 
    StoreEngineOptionsConfigured.newConfigured().withStorageType(StorageType.RocksDB).withRocksDBOptions(RocksDBOptionsConfigured.newConfigured().withDbPath(Configs.DB_PATH).config()).withRaftDataPath(Configs.RAFT_DATA_PATH).withServerAddress(new Endpoint("127.0.0.1", 8182)).config();
    final RheaKVStoreOptions opts = // 
    RheaKVStoreOptionsConfigured.newConfigured().withClusterName(// 
    Configs.CLUSTER_NAME).withUseParallelCompress(// 
    true).withInitialServerList(Configs.ALL_NODE_ADDRESSES).withStoreEngineOptions(// 
    storeOpts).withPlacementDriverOptions(// 
    pdOpts).config();
    System.out.println(opts);
    final Node node = new Node(opts);
    node.start();
    Runtime.getRuntime().addShutdownHook(new Thread(node::stop));
    System.out.println("server2 start OK");
}
Also used : RheaKVStoreOptions(com.alipay.sofa.jraft.rhea.options.RheaKVStoreOptions) PlacementDriverOptions(com.alipay.sofa.jraft.rhea.options.PlacementDriverOptions) Endpoint(com.alipay.sofa.jraft.util.Endpoint) StoreEngineOptions(com.alipay.sofa.jraft.rhea.options.StoreEngineOptions)

Example 5 with StoreEngineOptions

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

the class Server3 method main.

public static void main(final String[] args) throws Exception {
    final PlacementDriverOptions pdOpts = PlacementDriverOptionsConfigured.newConfigured().withFake(// use a fake pd
    true).config();
    final StoreEngineOptions storeOpts = // 
    StoreEngineOptionsConfigured.newConfigured().withStorageType(StorageType.RocksDB).withRocksDBOptions(RocksDBOptionsConfigured.newConfigured().withDbPath(Configs.DB_PATH).config()).withRaftDataPath(Configs.RAFT_DATA_PATH).withServerAddress(new Endpoint("127.0.0.1", 8183)).config();
    final RheaKVStoreOptions opts = // 
    RheaKVStoreOptionsConfigured.newConfigured().withClusterName(// 
    Configs.CLUSTER_NAME).withUseParallelCompress(// 
    true).withInitialServerList(Configs.ALL_NODE_ADDRESSES).withStoreEngineOptions(// 
    storeOpts).withPlacementDriverOptions(// 
    pdOpts).config();
    System.out.println(opts);
    final Node node = new Node(opts);
    node.start();
    Runtime.getRuntime().addShutdownHook(new Thread(node::stop));
    System.out.println("server3 start OK");
}
Also used : RheaKVStoreOptions(com.alipay.sofa.jraft.rhea.options.RheaKVStoreOptions) PlacementDriverOptions(com.alipay.sofa.jraft.rhea.options.PlacementDriverOptions) Endpoint(com.alipay.sofa.jraft.util.Endpoint) StoreEngineOptions(com.alipay.sofa.jraft.rhea.options.StoreEngineOptions)

Aggregations

PlacementDriverOptions (com.alipay.sofa.jraft.rhea.options.PlacementDriverOptions)6 StoreEngineOptions (com.alipay.sofa.jraft.rhea.options.StoreEngineOptions)6 RheaKVStoreOptions (com.alipay.sofa.jraft.rhea.options.RheaKVStoreOptions)5 Endpoint (com.alipay.sofa.jraft.util.Endpoint)4 Configuration (com.alipay.sofa.jraft.conf.Configuration)2 DefaultRheaKVStore (com.alipay.sofa.jraft.rhea.client.DefaultRheaKVStore)2 RheaKVStore (com.alipay.sofa.jraft.rhea.client.RheaKVStore)2 BatchingOptions (com.alipay.sofa.jraft.rhea.options.BatchingOptions)2 PeerId (com.alipay.sofa.jraft.entity.PeerId)1 StoreEngine (com.alipay.sofa.jraft.rhea.StoreEngine)1 FakePlacementDriverClient (com.alipay.sofa.jraft.rhea.client.pd.FakePlacementDriverClient)1 PlacementDriverClient (com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient)1 RemotePlacementDriverClient (com.alipay.sofa.jraft.rhea.client.pd.RemotePlacementDriverClient)1 RpcOptions (com.alipay.sofa.jraft.rhea.options.RpcOptions)1 AffinityNamedThreadFactory (com.alipay.sofa.jraft.rhea.util.concurrent.AffinityNamedThreadFactory)1 NamedThreadFactory (com.alipay.sofa.jraft.rhea.util.concurrent.NamedThreadFactory)1 TaskDispatcher (com.alipay.sofa.jraft.rhea.util.concurrent.disruptor.TaskDispatcher)1 ThreadFactory (java.util.concurrent.ThreadFactory)1