Search in sources :

Example 31 with PeerId

use of io.dingodb.raft.entity.PeerId 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 32 with PeerId

use of io.dingodb.raft.entity.PeerId 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 33 with PeerId

use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.

the class RegionEngine method transferLeadershipTo.

public boolean transferLeadershipTo(final Endpoint endpoint) {
    final PeerId peerId = new PeerId(endpoint, 0);
    final Status status = this.node.transferLeadershipTo(peerId);
    final boolean isOk = status.isOk();
    if (isOk) {
        LOG.info("Transfer-leadership succeeded: [{} --> {}].", this.storeEngine.getSelfEndpoint(), endpoint);
    } else {
        LOG.error("Transfer-leadership failed: {}, [{} --> {}].", status, this.storeEngine.getSelfEndpoint(), endpoint);
    }
    return isOk;
}
Also used : Status(io.dingodb.raft.Status) PeerId(io.dingodb.raft.entity.PeerId)

Example 34 with PeerId

use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.

the class AbstractPlacementDriverClient method getLeader.

@Override
public Endpoint getLeader(final String regionId, final boolean forceRefresh, final long timeoutMillis) {
    final String raftGroupId = JRaftHelper.getJRaftGroupId(this.clusterName, regionId);
    PeerId leader = getLeaderForRaftGroupId(raftGroupId, forceRefresh, timeoutMillis);
    if (leader == null && !forceRefresh) {
        // Could not found leader from cache, try again and force refresh cache
        leader = getLeaderForRaftGroupId(raftGroupId, true, timeoutMillis);
    }
    if (leader == null) {
        throw new RouteTableException("no leader in group: " + raftGroupId);
    }
    return leader.getEndpoint();
}
Also used : RouteTableException(io.dingodb.store.row.errors.RouteTableException) PeerId(io.dingodb.raft.entity.PeerId)

Example 35 with PeerId

use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.

the class RemovePeerRequestProcessor method processRequest0.

@Override
protected Message processRequest0(final CliRequestContext ctx, final CliRequests.RemovePeerRequest request, final RpcRequestClosure done) {
    final List<PeerId> oldPeers = ctx.node.listPeers();
    final String removingPeerIdStr = request.getPeerId();
    final PeerId removingPeer = new PeerId();
    if (removingPeer.parse(removingPeerIdStr)) {
        LOG.info("Receive RemovePeerRequest to {} from {}, removing {}", ctx.node.getNodeId(), done.getRpcCtx().getRemoteAddress(), removingPeerIdStr);
        ctx.node.removePeer(removingPeer, status -> {
            if (!status.isOk()) {
                done.run(status);
            } else {
                final CliRequests.RemovePeerResponse.Builder rb = CliRequests.RemovePeerResponse.newBuilder();
                for (final PeerId oldPeer : oldPeers) {
                    rb.addOldPeers(oldPeer.toString());
                    if (!oldPeer.equals(removingPeer)) {
                        rb.addNewPeers(oldPeer.toString());
                    }
                }
                done.sendResponse(rb.build());
            }
        });
    } else {
        return // 
        RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peer id %s", removingPeerIdStr);
    }
    return null;
}
Also used : PeerId(io.dingodb.raft.entity.PeerId)

Aggregations

PeerId (io.dingodb.raft.entity.PeerId)72 Status (io.dingodb.raft.Status)27 Configuration (io.dingodb.raft.conf.Configuration)15 Message (com.google.protobuf.Message)12 ArrayList (java.util.ArrayList)12 JRaftException (io.dingodb.raft.error.JRaftException)10 LogId (io.dingodb.raft.entity.LogId)9 CliRequests (io.dingodb.raft.rpc.CliRequests)8 Node (io.dingodb.raft.Node)6 RpcRequests (io.dingodb.raft.rpc.RpcRequests)4 Endpoint (io.dingodb.raft.util.Endpoint)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)4 LogEntry (io.dingodb.raft.entity.LogEntry)3 NodeOptions (io.dingodb.raft.option.NodeOptions)3 ByteBuffer (java.nio.ByteBuffer)3 TimeoutException (java.util.concurrent.TimeoutException)3 RaftOutter (io.dingodb.raft.entity.RaftOutter)2 RaftException (io.dingodb.raft.error.RaftException)2 ThreadId (io.dingodb.raft.util.ThreadId)2 ExecutionException (java.util.concurrent.ExecutionException)2