Search in sources :

Example 11 with Endpoint

use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.

the class RemoteFileCopierTest method testInit.

@Test
public void testInit() {
    Mockito.when(rpcService.connect(new Endpoint("localhost", 8081))).thenReturn(true);
    assertTrue(copier.init("remote://localhost:8081/999", null, new SnapshotCopierOptions(rpcService, timerManager, new RaftOptions(), new NodeOptions())));
    assertEquals(999, copier.getReaderId());
    Assert.assertEquals("localhost", copier.getEndpoint().getIp());
    Assert.assertEquals(8081, copier.getEndpoint().getPort());
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) SnapshotCopierOptions(com.alipay.sofa.jraft.option.SnapshotCopierOptions) Endpoint(com.alipay.sofa.jraft.util.Endpoint) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) Test(org.junit.Test)

Example 12 with Endpoint

use of com.alipay.sofa.jraft.util.Endpoint 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 13 with Endpoint

use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.

the class StoreEngine method initAllRegionEngine.

private boolean initAllRegionEngine(final StoreEngineOptions opts, final Store store) {
    Requires.requireNonNull(opts, "opts");
    Requires.requireNonNull(store, "store");
    String baseRaftDataPath = opts.getRaftDataPath();
    if (Strings.isNotBlank(baseRaftDataPath)) {
        try {
            FileUtils.forceMkdir(new File(baseRaftDataPath));
        } catch (final Throwable t) {
            LOG.error("Fail to make dir for raftDataPath: {}.", baseRaftDataPath);
            return false;
        }
    } else {
        baseRaftDataPath = "";
    }
    final Endpoint serverAddress = opts.getServerAddress();
    final List<RegionEngineOptions> rOptsList = opts.getRegionEngineOptionsList();
    final List<Region> regionList = store.getRegions();
    Requires.requireTrue(rOptsList.size() == regionList.size());
    for (int i = 0; i < rOptsList.size(); i++) {
        final RegionEngineOptions rOpts = rOptsList.get(i);
        if (!inConfiguration(rOpts.getServerAddress().toString(), rOpts.getInitialServerList())) {
            continue;
        }
        final Region region = regionList.get(i);
        if (Strings.isBlank(rOpts.getRaftDataPath())) {
            final String childPath = "raft_data_region_" + region.getId() + "_" + serverAddress.getPort();
            rOpts.setRaftDataPath(Paths.get(baseRaftDataPath, childPath).toString());
        }
        Requires.requireNonNull(region.getRegionEpoch(), "regionEpoch");
        final RegionEngine engine = new RegionEngine(region, this);
        if (engine.init(rOpts)) {
            final RegionKVService regionKVService = new DefaultRegionKVService(engine);
            registerRegionKVService(regionKVService);
            this.regionEngineTable.put(region.getId(), engine);
        } else {
            LOG.error("Fail to init [RegionEngine: {}].", region);
            return false;
        }
    }
    return true;
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Region(com.alipay.sofa.jraft.rhea.metadata.Region) RegionEngineOptions(com.alipay.sofa.jraft.rhea.options.RegionEngineOptions) File(java.io.File) Endpoint(com.alipay.sofa.jraft.util.Endpoint)

Example 14 with Endpoint

use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.

the class RegionEngine method init.

@Override
public synchronized boolean init(final RegionEngineOptions opts) {
    if (this.started) {
        LOG.info("[RegionEngine: {}] already started.", this.region);
        return true;
    }
    this.regionOpts = Requires.requireNonNull(opts, "opts");
    this.fsm = new KVStoreStateMachine(this.region, this.storeEngine);
    // node options
    NodeOptions nodeOpts = opts.getNodeOptions();
    if (nodeOpts == null) {
        nodeOpts = new NodeOptions();
    }
    final long metricsReportPeriod = opts.getMetricsReportPeriod();
    if (metricsReportPeriod > 0) {
        // metricsReportPeriod > 0 means enable metrics
        nodeOpts.setEnableMetrics(true);
    }
    final Configuration initialConf = new Configuration();
    if (!initialConf.parse(opts.getInitialServerList())) {
        LOG.error("Fail to parse initial configuration {}.", opts.getInitialServerList());
        return false;
    }
    nodeOpts.setInitialConf(initialConf);
    nodeOpts.setFsm(this.fsm);
    final String raftDataPath = opts.getRaftDataPath();
    try {
        FileUtils.forceMkdir(new File(raftDataPath));
    } catch (final Throwable t) {
        LOG.error("Fail to make dir for raftDataPath {}.", raftDataPath);
        return false;
    }
    if (Strings.isBlank(nodeOpts.getLogUri())) {
        final Path logUri = Paths.get(raftDataPath, "log");
        nodeOpts.setLogUri(logUri.toString());
    }
    if (Strings.isBlank(nodeOpts.getRaftMetaUri())) {
        final Path meteUri = Paths.get(raftDataPath, "meta");
        nodeOpts.setRaftMetaUri(meteUri.toString());
    }
    if (Strings.isBlank(nodeOpts.getSnapshotUri())) {
        final Path snapshotUri = Paths.get(raftDataPath, "snapshot");
        nodeOpts.setSnapshotUri(snapshotUri.toString());
    }
    LOG.info("[RegionEngine: {}], log uri: {}, raft meta uri: {}, snapshot uri: {}.", this.region, nodeOpts.getLogUri(), nodeOpts.getRaftMetaUri(), nodeOpts.getSnapshotUri());
    final Endpoint serverAddress = opts.getServerAddress();
    final PeerId serverId = new PeerId(serverAddress, 0);
    final RpcServer rpcServer = this.storeEngine.getRpcServer();
    this.raftGroupService = new RaftGroupService(opts.getRaftGroupId(), serverId, nodeOpts, rpcServer, true);
    this.node = this.raftGroupService.start(false);
    RouteTable.getInstance().updateConfiguration(this.raftGroupService.getGroupId(), nodeOpts.getInitialConf());
    if (this.node != null) {
        final RawKVStore rawKVStore = this.storeEngine.getRawKVStore();
        final Executor readIndexExecutor = this.storeEngine.getReadIndexExecutor();
        this.raftRawKVStore = new RaftRawKVStore(this.node, rawKVStore, readIndexExecutor);
        this.metricsRawKVStore = new MetricsRawKVStore(this.region.getId(), this.raftRawKVStore);
        // metrics config
        if (this.regionMetricsReporter == null && metricsReportPeriod > 0) {
            final MetricRegistry metricRegistry = this.node.getNodeMetrics().getMetricRegistry();
            if (metricRegistry != null) {
                final ScheduledExecutorService scheduler = this.storeEngine.getMetricsScheduler();
                // start raft node metrics reporter
                this.regionMetricsReporter = // 
                Slf4jReporter.forRegistry(metricRegistry).prefixedWith(// 
                "region_" + this.region.getId()).withLoggingLevel(// 
                Slf4jReporter.LoggingLevel.INFO).outputTo(// 
                LOG).scheduleOn(// 
                scheduler).shutdownExecutorOnStop(// 
                scheduler != null).build();
                this.regionMetricsReporter.start(metricsReportPeriod, TimeUnit.SECONDS);
            }
        }
        this.started = true;
        LOG.info("[RegionEngine] start successfully: {}.", this);
    }
    return this.started;
}
Also used : Path(java.nio.file.Path) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Configuration(com.alipay.sofa.jraft.conf.Configuration) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) MetricRegistry(com.codahale.metrics.MetricRegistry) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) KVStoreStateMachine(com.alipay.sofa.jraft.rhea.storage.KVStoreStateMachine) MetricsRawKVStore(com.alipay.sofa.jraft.rhea.storage.MetricsRawKVStore) RaftRawKVStore(com.alipay.sofa.jraft.rhea.storage.RaftRawKVStore) RawKVStore(com.alipay.sofa.jraft.rhea.storage.RawKVStore) MetricsRawKVStore(com.alipay.sofa.jraft.rhea.storage.MetricsRawKVStore) Executor(java.util.concurrent.Executor) Endpoint(com.alipay.sofa.jraft.util.Endpoint) RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) File(java.io.File) RaftRawKVStore(com.alipay.sofa.jraft.rhea.storage.RaftRawKVStore) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 15 with Endpoint

use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.

the class JRaftHelper method toPeer.

public static Peer toPeer(final PeerId peerId) {
    Requires.requireNonNull(peerId, "peerId");
    final Endpoint endpoint = peerId.getEndpoint();
    Requires.requireNonNull(endpoint, "peerId.endpoint");
    final Peer peer = new Peer();
    peer.setId(-1);
    peer.setStoreId(-1);
    peer.setEndpoint(endpoint.copy());
    return peer;
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Peer(com.alipay.sofa.jraft.rhea.metadata.Peer)

Aggregations

Endpoint (com.alipay.sofa.jraft.util.Endpoint)80 Test (org.junit.Test)34 PeerId (com.alipay.sofa.jraft.entity.PeerId)28 Node (com.alipay.sofa.jraft.Node)23 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)18 Configuration (com.alipay.sofa.jraft.conf.Configuration)12 CountDownLatch (java.util.concurrent.CountDownLatch)11 Status (com.alipay.sofa.jraft.Status)8 File (java.io.File)7 ByteBuffer (java.nio.ByteBuffer)7 RaftOptions (com.alipay.sofa.jraft.option.RaftOptions)6 Region (com.alipay.sofa.jraft.rhea.metadata.Region)6 PlacementDriverClient (com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient)5 RheaKVStoreOptions (com.alipay.sofa.jraft.rhea.options.RheaKVStoreOptions)5 RpcResponseClosure (com.alipay.sofa.jraft.rpc.RpcResponseClosure)5 RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)5 FutureImpl (com.alipay.sofa.jraft.rpc.impl.FutureImpl)5 SnapshotReader (com.alipay.sofa.jraft.storage.snapshot.SnapshotReader)5 Message (com.google.protobuf.Message)5 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)4