Search in sources :

Example 1 with Endpoint

use of io.dingodb.raft.util.Endpoint in project dingo by dingodb.

the class StoreEngine method init.

@Override
public synchronized boolean init(final StoreEngineOptions opts) {
    if (this.started) {
        LOG.info("[StoreEngine] already started.");
        return true;
    }
    DescriberManager.getInstance().addDescriber(this);
    this.storeOpts = Requires.requireNonNull(opts, "opts");
    Endpoint serverAddress = Requires.requireNonNull(opts.getServerAddress(), "opts.serverAddress");
    final int port = serverAddress.getPort();
    final String ip = serverAddress.getIp();
    if (ip == null || Utils.IP_ANY.equals(ip)) {
        serverAddress = new Endpoint(NetUtil.getLocalCanonicalHostName(), port);
        opts.setServerAddress(serverAddress);
    }
    final long metricsReportPeriod = opts.getMetricsReportPeriod();
    // init region options
    List<RegionEngineOptions> rOptsList = opts.getRegionEngineOptionsList();
    if (rOptsList == null || rOptsList.isEmpty()) {
        // -1 region
        final RegionEngineOptions rOpts = new RegionEngineOptions();
        rOpts.setRegionId(Constants.DEFAULT_REGION_ID);
        rOptsList = Lists.newArrayList();
        rOptsList.add(rOpts);
        opts.setRegionEngineOptionsList(rOptsList);
    }
    final String clusterName = this.pdClient.getClusterName();
    for (final RegionEngineOptions rOpts : rOptsList) {
        rOpts.setRaftGroupId(JRaftHelper.getJRaftGroupId(clusterName, rOpts.getRegionId()));
        rOpts.setServerAddress(serverAddress);
        if (Strings.isBlank(rOpts.getInitialServerList())) {
            // if blank, extends parent's value
            rOpts.setInitialServerList(opts.getInitialServerList());
        }
        if (rOpts.getNodeOptions() == null) {
            // copy common node options
            rOpts.setNodeOptions(opts.getCommonNodeOptions() == null ? new NodeOptions() : opts.getCommonNodeOptions().copy());
        }
        if (rOpts.getMetricsReportPeriod() <= 0 && metricsReportPeriod > 0) {
            // extends store opts
            rOpts.setMetricsReportPeriod(metricsReportPeriod);
        }
    }
    // init store
    final Store store = this.pdClient.getStoreMetadata(opts);
    if (store == null || store.getRegions() == null || store.getRegions().isEmpty()) {
        LOG.error("Empty store metadata: {}.", store);
        return false;
    }
    this.storeId = store.getId();
    // init executors
    if (this.readIndexExecutor == null) {
        this.readIndexExecutor = StoreEngineHelper.createReadIndexExecutor(opts.getReadIndexCoreThreads());
    }
    if (this.raftStateTrigger == null) {
        this.raftStateTrigger = StoreEngineHelper.createRaftStateTrigger(opts.getLeaderStateTriggerCoreThreads());
    }
    if (this.snapshotExecutor == null) {
        this.snapshotExecutor = StoreEngineHelper.createSnapshotExecutor(opts.getSnapshotCoreThreads(), opts.getSnapshotMaxThreads());
    }
    // init rpc executors
    final boolean useSharedRpcExecutor = opts.isUseSharedRpcExecutor();
    if (!useSharedRpcExecutor) {
        if (this.cliRpcExecutor == null) {
            this.cliRpcExecutor = StoreEngineHelper.createCliRpcExecutor(opts.getCliRpcCoreThreads());
        }
        if (this.raftRpcExecutor == null) {
            this.raftRpcExecutor = StoreEngineHelper.createRaftRpcExecutor(opts.getRaftRpcCoreThreads());
        }
        if (this.kvRpcExecutor == null) {
            this.kvRpcExecutor = StoreEngineHelper.createKvRpcExecutor(opts.getKvRpcCoreThreads());
        }
    }
    // init metrics
    startMetricReporters(metricsReportPeriod);
    // init rpc server
    this.rpcServer = RaftRpcServerFactory.createRaftRpcServer(serverAddress, this.raftRpcExecutor, this.cliRpcExecutor);
    StoreEngineHelper.addKvStoreRequestProcessor(this.rpcServer, this);
    if (!this.rpcServer.init(null)) {
        LOG.error("Fail to init [RpcServer].");
        return false;
    }
    // init db store
    if (!initRawKVStore(opts)) {
        return false;
    }
    if (this.rawKVStore instanceof Describer) {
        DescriberManager.getInstance().addDescriber((Describer) this.rawKVStore);
    }
    // init all region engine
    if (!initAllRegionEngine(opts, store)) {
        LOG.error("Fail to init all [RegionEngine].");
        return false;
    }
    this.startTime = System.currentTimeMillis();
    LOG.info("[StoreEngine] start successfully: {}.", this);
    return this.started = true;
}
Also used : Endpoint(io.dingodb.raft.util.Endpoint) Store(io.dingodb.store.row.metadata.Store) MemoryRawKVStore(io.dingodb.store.row.storage.MemoryRawKVStore) BatchRawKVStore(io.dingodb.store.row.storage.BatchRawKVStore) RocksRawKVStore(io.dingodb.store.row.storage.RocksRawKVStore) NodeOptions(io.dingodb.raft.option.NodeOptions) Endpoint(io.dingodb.raft.util.Endpoint) Describer(io.dingodb.raft.util.Describer)

Example 2 with Endpoint

use of io.dingodb.raft.util.Endpoint in project dingo by dingodb.

the class StoreEngine method initAllRegionEngine.

private boolean initAllRegionEngine(final StoreEngineOptions opts, final Store store) {
    Requires.requireNonNull(opts, "opts");
    Requires.requireNonNull(store, "store");
    Requires.requireNonNull(opts.getRaftStoreOptions(), "raftDBOptions is Null");
    String baseRaftDataPath = opts.getRaftStoreOptions().getDataPath();
    if (baseRaftDataPath != null && 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 {
        LOG.error("Init Region found region raft path is empty. store:{}, raftStoreOpt:{}", store.getId(), opts.getRaftStoreOptions());
        return false;
    }
    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);
        boolean isOK = inConfiguration(rOpts.getServerAddress().toString(), rOpts.getInitialServerList());
        if (!isOK) {
            LOG.warn("Invalid serverAddress:{} not in initialServerList:{}, whole options:{}", rOpts.getServerAddress(), rOpts.getInitialServerList(), rOpts);
            continue;
        }
        final Region region = regionList.get(i);
        if (Strings.isBlank(rOpts.getRaftDataPath())) {
            final String raftDataPath = JRaftHelper.getRaftDataPath(baseRaftDataPath, region.getId(), serverAddress.getPort());
            rOpts.setRaftDataPath(raftDataPath);
        }
        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(io.dingodb.raft.util.Endpoint) Endpoint(io.dingodb.raft.util.Endpoint) Region(io.dingodb.store.row.metadata.Region) File(java.io.File)

Example 3 with Endpoint

use of io.dingodb.raft.util.Endpoint in project dingo by dingodb.

the class DefaultDingoRowStoreRpcService method callAsyncWithRpc.

@Override
public <V> CompletableFuture<V> callAsyncWithRpc(final BaseRequest request, final FailoverClosure<V> closure, final Errors lastCause, final boolean requireLeader) {
    final boolean forceRefresh = ErrorsHelper.isInvalidPeer(lastCause);
    final Endpoint endpoint = getRpcEndpoint(request.getRegionId(), forceRefresh, this.rpcTimeoutMillis, requireLeader);
    internalCallAsyncWithRpc(endpoint, request, closure);
    return closure.future();
}
Also used : Endpoint(io.dingodb.raft.util.Endpoint)

Example 4 with Endpoint

use of io.dingodb.raft.util.Endpoint in project dingo by dingodb.

the class JRaftHelper method toJRaftPeerId.

public static PeerId toJRaftPeerId(final Peer peer) {
    Requires.requireNonNull(peer, "peer");
    final Endpoint endpoint = peer.getEndpoint();
    Requires.requireNonNull(endpoint, "peer.endpoint");
    return new PeerId(endpoint, 0);
}
Also used : Endpoint(io.dingodb.raft.util.Endpoint) PeerId(io.dingodb.raft.entity.PeerId)

Example 5 with Endpoint

use of io.dingodb.raft.util.Endpoint in project dingo by dingodb.

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);
    this.storeEngine.getStateListenerContainer().addStateListener(this.region.getId(), this);
    // 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());
    }
    String storeOptionStr = "Null";
    RaftStoreOptions raftStoreOptions = opts.getRaftStoreOptions();
    if (raftStoreOptions != null) {
        RaftLogStorageOptions raftLogStorageOptions = raftStoreOptions.getRaftLogStorageOptions();
        nodeOpts.setRaftLogStorageOptions(raftLogStorageOptions);
        storeOptionStr = raftStoreOptions.toString();
    }
    LOG.info("[RegionEngine: {}], log uri: {}, raft meta uri: {}, snapshot uri: {}. raftDBOptions:{}", this.region, nodeOpts.getLogUri(), nodeOpts.getRaftMetaUri(), nodeOpts.getSnapshotUri(), storeOptionStr);
    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);
            }
        }
        if (this.storeEngine.getPlacementDriverClient() instanceof RemotePlacementDriverClient) {
            HeartbeatOptions heartbeatOpts = opts.getHeartbeatOptions();
            if (heartbeatOpts == null) {
                heartbeatOpts = new HeartbeatOptions();
            }
            this.heartbeatSender = new RegionHeartbeatSender(this);
            if (!this.heartbeatSender.init(heartbeatOpts)) {
                LOG.error("Fail to init [HeartbeatSender].");
                return false;
            }
        }
        this.started = true;
        LOG.info("[RegionEngine] start successfully: {}.", this);
    }
    return this.started;
}
Also used : Path(java.nio.file.Path) RaftStoreOptions(io.dingodb.store.row.options.RaftStoreOptions) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Configuration(io.dingodb.raft.conf.Configuration) RaftGroupService(io.dingodb.raft.RaftGroupService) MetricRegistry(com.codahale.metrics.MetricRegistry) NodeOptions(io.dingodb.raft.option.NodeOptions) KVStoreStateMachine(io.dingodb.store.row.storage.KVStoreStateMachine) MetricsRawKVStore(io.dingodb.store.row.storage.MetricsRawKVStore) RemotePlacementDriverClient(io.dingodb.store.row.client.pd.RemotePlacementDriverClient) RaftLogStorageOptions(io.dingodb.raft.option.RaftLogStorageOptions) RaftRawKVStore(io.dingodb.store.row.storage.RaftRawKVStore) MetricsRawKVStore(io.dingodb.store.row.storage.MetricsRawKVStore) RawKVStore(io.dingodb.store.row.storage.RawKVStore) RegionHeartbeatSender(io.dingodb.store.row.client.pd.RegionHeartbeatSender) Executor(java.util.concurrent.Executor) Endpoint(io.dingodb.raft.util.Endpoint) HeartbeatOptions(io.dingodb.store.row.options.HeartbeatOptions) RpcServer(io.dingodb.raft.rpc.RpcServer) File(java.io.File) RaftRawKVStore(io.dingodb.store.row.storage.RaftRawKVStore) PeerId(io.dingodb.raft.entity.PeerId)

Aggregations

Endpoint (io.dingodb.raft.util.Endpoint)25 Store (io.dingodb.store.row.metadata.Store)6 PeerId (io.dingodb.raft.entity.PeerId)5 Cluster (io.dingodb.store.row.metadata.Cluster)5 NodeOptions (io.dingodb.raft.option.NodeOptions)4 Status (io.dingodb.raft.Status)3 FutureHelper (io.dingodb.store.row.client.FutureHelper)3 FailoverClosure (io.dingodb.store.row.client.failover.FailoverClosure)3 RetryRunner (io.dingodb.store.row.client.failover.RetryRunner)3 FailoverClosureImpl (io.dingodb.store.row.client.failover.impl.FailoverClosureImpl)3 CreateRegionIdRequest (io.dingodb.store.row.cmd.pd.CreateRegionIdRequest)3 GetClusterInfoRequest (io.dingodb.store.row.cmd.pd.GetClusterInfoRequest)3 GetStoreIdRequest (io.dingodb.store.row.cmd.pd.GetStoreIdRequest)3 GetStoreInfoRequest (io.dingodb.store.row.cmd.pd.GetStoreInfoRequest)3 SetStoreInfoRequest (io.dingodb.store.row.cmd.pd.SetStoreInfoRequest)3 Region (io.dingodb.store.row.metadata.Region)3 StoreStats (io.dingodb.store.row.metadata.StoreStats)3 RouteTable (io.dingodb.raft.RouteTable)2 Configuration (io.dingodb.raft.conf.Configuration)2 RpcServer (io.dingodb.raft.rpc.RpcServer)2