Search in sources :

Example 1 with NodeOptions

use of io.dingodb.raft.option.NodeOptions 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 NodeOptions

use of io.dingodb.raft.option.NodeOptions 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)

Example 3 with NodeOptions

use of io.dingodb.raft.option.NodeOptions in project dingo by dingodb.

the class RemotePlacementDriverClient method getStoreMetadata.

@Override
public Store getStoreMetadata(final StoreEngineOptions opts) {
    final Endpoint selfEndpoint = opts.getServerAddress();
    /**
     * for debugger.
     */
    for (RegionEngineOptions opt : opts.getRegionEngineOptionsList()) {
        LOG.info("RegionEngineOptions-before: update from local conf. opt:{}", opt.toString());
    }
    // remote conf is the preferred
    final Store remoteStore = this.metadataRpcClient.getStoreInfo(this.clusterId, selfEndpoint);
    if (!remoteStore.isEmpty()) {
        final List<Region> regions = remoteStore.getRegions();
        Long metricsReportPeriodMs = opts.getMetricsReportPeriod();
        if (opts.getRegionEngineOptionsList() != null && opts.getRegionEngineOptionsList().size() > 0) {
            metricsReportPeriodMs = opts.getRegionEngineOptionsList().get(0).getMetricsReportPeriod();
        }
        opts.getRegionEngineOptionsList().clear();
        for (final Region region : regions) {
            super.regionRouteTable.addOrUpdateRegion(region);
            RegionEngineOptions engineOptions = new RegionEngineOptions();
            engineOptions.setRegionId(region.getId());
            engineOptions.setStartKey(BytesUtil.readUtf8(region.getStartKey()));
            engineOptions.setStartKeyBytes(region.getStartKey());
            engineOptions.setEndKey(BytesUtil.readUtf8(region.getEndKey()));
            engineOptions.setEndKeyBytes(region.getEndKey());
            engineOptions.setNodeOptions(new NodeOptions());
            engineOptions.setRaftGroupId(JRaftHelper.getJRaftGroupId(this.clusterName, region.getId()));
            String raftDataPath = JRaftHelper.getRaftDataPath(opts.getRaftStoreOptions().getDataPath(), region.getId(), opts.getServerAddress().getPort());
            engineOptions.setRaftDataPath(raftDataPath);
            engineOptions.setServerAddress(opts.getServerAddress());
            String initServerList = region.getPeers().stream().map(x -> x.getEndpoint().toString()).collect(Collectors.joining(","));
            engineOptions.setInitialServerList(initServerList);
            engineOptions.setMetricsReportPeriod(metricsReportPeriodMs);
            opts.getRegionEngineOptionsList().add(engineOptions);
        }
        /**
         * for debugger.
         */
        for (RegionEngineOptions opt : opts.getRegionEngineOptionsList()) {
            LOG.info("RegionEngineOptions-After: update from remote PD. opt:{}", opt.toString());
        }
        return remoteStore;
    }
    // local conf
    final Store localStore = new Store();
    final List<RegionEngineOptions> rOptsList = opts.getRegionEngineOptionsList();
    final List<Region> regionList = Lists.newArrayListWithCapacity(rOptsList.size());
    localStore.setId(remoteStore.getId());
    localStore.setEndpoint(selfEndpoint);
    for (final RegionEngineOptions rOpts : rOptsList) {
        regionList.add(getLocalRegionMetadata(rOpts));
    }
    localStore.setRegions(regionList);
    refreshStore(localStore);
    return localStore;
}
Also used : Logger(org.slf4j.Logger) Store(io.dingodb.store.row.metadata.Store) RegionEngineOptions(io.dingodb.store.row.options.RegionEngineOptions) StoreEngineOptions(io.dingodb.store.row.options.StoreEngineOptions) LoggerFactory(org.slf4j.LoggerFactory) Region(io.dingodb.store.row.metadata.Region) RouteTable(io.dingodb.raft.RouteTable) NodeOptions(io.dingodb.raft.option.NodeOptions) Lists(io.dingodb.store.row.util.Lists) Collectors(java.util.stream.Collectors) Cluster(io.dingodb.store.row.metadata.Cluster) Strings(io.dingodb.store.row.util.Strings) PlacementDriverOptions(io.dingodb.store.row.options.PlacementDriverOptions) List(java.util.List) PeerId(io.dingodb.raft.entity.PeerId) RouteTableException(io.dingodb.store.row.errors.RouteTableException) BytesUtil(io.dingodb.raft.util.BytesUtil) Endpoint(io.dingodb.raft.util.Endpoint) JRaftHelper(io.dingodb.store.row.JRaftHelper) Endpoint(io.dingodb.raft.util.Endpoint) Store(io.dingodb.store.row.metadata.Store) Region(io.dingodb.store.row.metadata.Region) NodeOptions(io.dingodb.raft.option.NodeOptions) RegionEngineOptions(io.dingodb.store.row.options.RegionEngineOptions)

Example 4 with NodeOptions

use of io.dingodb.raft.option.NodeOptions in project dingo by dingodb.

the class CoordinatorServer method start.

public void start(final CoordinatorOptions opts) throws Exception {
    this.svrOpts = opts;
    log.info("Coordinator all configuration: {}.", this.svrOpts);
    log.info("instance configuration: {}.", DingoOptions.instance());
    this.context = new CoordinatorContext();
    final String raftId = svrOpts.getRaft().getGroup();
    final Endpoint endpoint = new Endpoint(svrOpts.getIp(), svrOpts.getRaft().getPort());
    final RocksRawKVStore rawKVStore = createRocksDB();
    final CoordinatorStateMachine stateMachine = createStateMachine(raftId, rawKVStore, context);
    final Node node = RaftServiceFactory.createRaftNode(raftId, new PeerId(endpoint, 0));
    final AsyncKeyValueStore keyValueStore = createStore(rawKVStore, node);
    final ScheduleMetaAdaptor scheduleMetaAdaptor = createScheduleMetaAdaptor(keyValueStore);
    final TableMetaAdaptor tableMetaAdaptor = createTableMetaAdaptor(keyValueStore, scheduleMetaAdaptor);
    final CoordinatorMetaService metaService = createMetaService();
    final RowStoreMetaAdaptor rowStoreMetaAdaptor = createRowStoreMetaAdaptor(scheduleMetaAdaptor);
    context.coordOpts(svrOpts).endpoint(endpoint).netService(createNetService()).rocksKVStore(rawKVStore).stateMachine(stateMachine).keyValueStore(keyValueStore).node(node).scheduleMetaAdaptor(scheduleMetaAdaptor).serviceProvider(createServiceProvider()).tableMetaAdaptor(tableMetaAdaptor).rowStoreMetaAdaptor(rowStoreMetaAdaptor).metaService(metaService);
    NodeManager.getInstance().addAddress(endpoint);
    stateMachine.init();
    final NodeOptions nodeOptions = initNodeOptions(stateMachine);
    node.init(nodeOptions);
    keyValueStore.init();
}
Also used : RowStoreMetaAdaptor(io.dingodb.server.coordinator.meta.RowStoreMetaAdaptor) CoordinatorStateMachine(io.dingodb.server.coordinator.state.CoordinatorStateMachine) Node(io.dingodb.raft.Node) RocksRawKVStore(io.dingodb.store.row.storage.RocksRawKVStore) ScheduleMetaAdaptor(io.dingodb.server.coordinator.meta.ScheduleMetaAdaptor) NodeOptions(io.dingodb.raft.option.NodeOptions) CoordinatorContext(io.dingodb.server.coordinator.context.CoordinatorContext) AsyncKeyValueStore(io.dingodb.server.coordinator.store.AsyncKeyValueStore) RaftAsyncKeyValueStore(io.dingodb.server.coordinator.store.RaftAsyncKeyValueStore) TableMetaAdaptor(io.dingodb.server.coordinator.meta.TableMetaAdaptor) Endpoint(io.dingodb.raft.util.Endpoint) CoordinatorMetaService(io.dingodb.server.coordinator.meta.service.CoordinatorMetaService) PeerId(io.dingodb.raft.entity.PeerId)

Example 5 with NodeOptions

use of io.dingodb.raft.option.NodeOptions in project dingo by dingodb.

the class CoordinatorServer method initNodeOptions.

private NodeOptions initNodeOptions(StateMachine stateMachine) {
    final Configuration initialConf = new Configuration();
    String svrList = svrOpts.getRaft().getInitCoordRaftSvrList();
    if (!initialConf.parse(svrList)) {
        throw new RuntimeException("configuration parse error, initCoordSrvList: " + svrList);
    }
    NodeOptions nodeOpts = new NodeOptions();
    nodeOpts.setInitialConf(initialConf);
    nodeOpts.setFsm(stateMachine);
    final String dbPath = svrOpts.getOptions().getStoreDBOptions().getDataPath();
    if (Strings.isBlank(nodeOpts.getLogUri())) {
        final String logUri = Paths.get(dbPath, COORDINATOR, "log").toString();
        try {
            FileUtils.forceMkdir(new File(logUri));
            log.info("data path created: {}", logUri);
        } catch (final Throwable t) {
            throw new RuntimeException("Fail to make dir for dbPath: " + logUri);
        }
        nodeOpts.setLogUri(logUri);
    }
    if (Strings.isBlank(nodeOpts.getRaftMetaUri())) {
        final String meteUri = Paths.get(dbPath, COORDINATOR, "meta").toString();
        nodeOpts.setRaftMetaUri(meteUri);
    }
    if (Strings.isBlank(nodeOpts.getSnapshotUri())) {
        final String snapshotUri = Paths.get(dbPath, COORDINATOR, "snapshot").toString();
        nodeOpts.setSnapshotUri(snapshotUri);
    }
    return nodeOpts;
}
Also used : Configuration(io.dingodb.raft.conf.Configuration) NodeOptions(io.dingodb.raft.option.NodeOptions) File(java.io.File)

Aggregations

NodeOptions (io.dingodb.raft.option.NodeOptions)6 PeerId (io.dingodb.raft.entity.PeerId)4 Endpoint (io.dingodb.raft.util.Endpoint)4 Configuration (io.dingodb.raft.conf.Configuration)2 Store (io.dingodb.store.row.metadata.Store)2 RocksRawKVStore (io.dingodb.store.row.storage.RocksRawKVStore)2 File (java.io.File)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 Node (io.dingodb.raft.Node)1 RaftGroupService (io.dingodb.raft.RaftGroupService)1 RouteTable (io.dingodb.raft.RouteTable)1 SynchronizedClosure (io.dingodb.raft.closure.SynchronizedClosure)1 ConfigurationManager (io.dingodb.raft.conf.ConfigurationManager)1 LogEntry (io.dingodb.raft.entity.LogEntry)1 LogId (io.dingodb.raft.entity.LogId)1 RaftLogStorageOptions (io.dingodb.raft.option.RaftLogStorageOptions)1 RpcServer (io.dingodb.raft.rpc.RpcServer)1 BytesUtil (io.dingodb.raft.util.BytesUtil)1 Describer (io.dingodb.raft.util.Describer)1 CoordinatorContext (io.dingodb.server.coordinator.context.CoordinatorContext)1