Search in sources :

Example 1 with StoreEngine

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

the class KVStateMachineTest method setup.

@Before
public void setup() throws IOException, InterruptedException {
    final Region region = new Region();
    region.setId(1);
    final StoreEngine storeEngine = new MockStoreEngine();
    final KVStoreStateMachine fsm = new KVStoreStateMachine(region, storeEngine);
    final NodeOptions nodeOpts = new NodeOptions();
    final Configuration conf = new Configuration();
    conf.addPeer(PeerId.parsePeer("127.0.0.1:8081"));
    nodeOpts.setInitialConf(conf);
    nodeOpts.setFsm(fsm);
    final String raftDataPath = "raft_st_test";
    this.raftDataPath = new File(raftDataPath);
    if (this.raftDataPath.exists()) {
        FileUtils.forceDelete(this.raftDataPath);
    }
    FileUtils.forceMkdir(this.raftDataPath);
    final Path logUri = Paths.get(raftDataPath, "log");
    nodeOpts.setLogUri(logUri.toString());
    final Path meteUri = Paths.get(raftDataPath, "meta");
    nodeOpts.setRaftMetaUri(meteUri.toString());
    final Path snapshotUri = Paths.get(raftDataPath, "snapshot");
    nodeOpts.setSnapshotUri(snapshotUri.toString());
    final Endpoint serverAddress = new Endpoint("127.0.0.1", 8081);
    final PeerId serverId = new PeerId(serverAddress, 0);
    this.raftGroupService = new RaftGroupService("st_test", serverId, nodeOpts, null, true);
    final Node node = this.raftGroupService.start(false);
    for (int i = 0; i < 100; i++) {
        if (node.isLeader()) {
            break;
        }
        Thread.sleep(100);
    }
    final RawKVStore rawKVStore = storeEngine.getRawKVStore();
    this.raftRawKVStore = new RaftRawKVStore(node, rawKVStore, null);
}
Also used : Path(java.nio.file.Path) Configuration(com.alipay.sofa.jraft.conf.Configuration) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) Node(com.alipay.sofa.jraft.Node) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) Endpoint(com.alipay.sofa.jraft.util.Endpoint) StoreEngine(com.alipay.sofa.jraft.rhea.StoreEngine) Endpoint(com.alipay.sofa.jraft.util.Endpoint) Region(com.alipay.sofa.jraft.rhea.metadata.Region) File(java.io.File) PeerId(com.alipay.sofa.jraft.entity.PeerId) Before(org.junit.Before)

Example 2 with StoreEngine

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

Aggregations

StoreEngine (com.alipay.sofa.jraft.rhea.StoreEngine)2 Endpoint (com.alipay.sofa.jraft.util.Endpoint)2 Node (com.alipay.sofa.jraft.Node)1 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)1 Configuration (com.alipay.sofa.jraft.conf.Configuration)1 PeerId (com.alipay.sofa.jraft.entity.PeerId)1 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)1 FakePlacementDriverClient (com.alipay.sofa.jraft.rhea.client.pd.FakePlacementDriverClient)1 RemotePlacementDriverClient (com.alipay.sofa.jraft.rhea.client.pd.RemotePlacementDriverClient)1 Region (com.alipay.sofa.jraft.rhea.metadata.Region)1 PlacementDriverOptions (com.alipay.sofa.jraft.rhea.options.PlacementDriverOptions)1 RpcOptions (com.alipay.sofa.jraft.rhea.options.RpcOptions)1 StoreEngineOptions (com.alipay.sofa.jraft.rhea.options.StoreEngineOptions)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 File (java.io.File)1 Path (java.nio.file.Path)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 Before (org.junit.Before)1