Search in sources :

Example 21 with Configuration

use of io.dingodb.raft.conf.Configuration 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)

Example 22 with Configuration

use of io.dingodb.raft.conf.Configuration in project dingo by dingodb.

the class RouteTable method refreshLeader.

/**
 * Blocking the thread until query_leader finishes.
 *
 * @param groupId   raft group id
 * @param timeoutMs timeout millis
 * @return operation status
 */
public Status refreshLeader(final CliClientService cliClientService, final String groupId, final int timeoutMs) throws InterruptedException, TimeoutException {
    Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
    Requires.requireTrue(timeoutMs > 0, "Invalid timeout: " + timeoutMs);
    final Configuration conf = getConfiguration(groupId);
    if (conf == null) {
        return new Status(RaftError.ENOENT, "Group %s is not registered in RouteTable, forgot to call updateConfiguration?", groupId);
    }
    final Status st = Status.OK();
    final CliRequests.GetLeaderRequest.Builder rb = CliRequests.GetLeaderRequest.newBuilder();
    rb.setGroupId(groupId);
    final CliRequests.GetLeaderRequest request = rb.build();
    TimeoutException timeoutException = null;
    for (final PeerId peer : conf) {
        if (!cliClientService.connect(peer.getEndpoint())) {
            if (st.isOk()) {
                st.setError(-1, "Fail to init channel to %s", peer);
            } else {
                final String savedMsg = st.getErrorMsg();
                st.setError(-1, "%s, Fail to init channel to %s", savedMsg, peer);
            }
            continue;
        }
        final Future<Message> result = cliClientService.getLeader(peer.getEndpoint(), request, null);
        try {
            final Message msg = result.get(timeoutMs, TimeUnit.MILLISECONDS);
            if (msg instanceof RpcRequests.ErrorResponse) {
                if (st.isOk()) {
                    st.setError(-1, ((RpcRequests.ErrorResponse) msg).getErrorMsg());
                } else {
                    final String savedMsg = st.getErrorMsg();
                    st.setError(-1, "%s, %s", savedMsg, ((RpcRequests.ErrorResponse) msg).getErrorMsg());
                }
            } else {
                final CliRequests.GetLeaderResponse response = (CliRequests.GetLeaderResponse) msg;
                updateLeader(groupId, response.getLeaderId());
                return Status.OK();
            }
        } catch (final TimeoutException e) {
            timeoutException = e;
        } catch (final ExecutionException e) {
            if (st.isOk()) {
                st.setError(-1, e.getMessage());
            } else {
                final String savedMsg = st.getErrorMsg();
                st.setError(-1, "%s, %s", savedMsg, e.getMessage());
            }
        }
    }
    if (timeoutException != null) {
        throw timeoutException;
    }
    return st;
}
Also used : Configuration(io.dingodb.raft.conf.Configuration) Message(com.google.protobuf.Message) RpcRequests(io.dingodb.raft.rpc.RpcRequests) CliRequests(io.dingodb.raft.rpc.CliRequests) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) PeerId(io.dingodb.raft.entity.PeerId)

Example 23 with Configuration

use of io.dingodb.raft.conf.Configuration in project dingo by dingodb.

the class RouteTable method updateConfiguration.

/**
 * Update configuration of group in route table.
 *
 * @param groupId raft group id
 * @param confStr configuration string
 * @return true on success
 */
public boolean updateConfiguration(final String groupId, final String confStr) {
    Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
    Requires.requireTrue(!StringUtils.isBlank(confStr), "Blank configuration");
    final Configuration conf = new Configuration();
    if (conf.parse(confStr)) {
        return updateConfiguration(groupId, conf);
    } else {
        LOG.error("Fail to parse confStr: {}", confStr);
        return false;
    }
}
Also used : Configuration(io.dingodb.raft.conf.Configuration)

Example 24 with Configuration

use of io.dingodb.raft.conf.Configuration in project dingo by dingodb.

the class RouteTable method getConfiguration.

/**
 * Get the configuration by groupId, returns null when not found.
 *
 * @param groupId raft group id
 * @return configuration of the group id
 */
public Configuration getConfiguration(final String groupId) {
    Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
    final GroupConf gc = this.groupConfTable.get(groupId);
    if (gc == null) {
        return null;
    }
    final StampedLock stampedLock = gc.stampedLock;
    long stamp = stampedLock.tryOptimisticRead();
    Configuration conf = gc.conf;
    if (!stampedLock.validate(stamp)) {
        stamp = stampedLock.readLock();
        try {
            conf = gc.conf;
        } finally {
            stampedLock.unlockRead(stamp);
        }
    }
    return conf;
}
Also used : Configuration(io.dingodb.raft.conf.Configuration) StampedLock(java.util.concurrent.locks.StampedLock)

Example 25 with Configuration

use of io.dingodb.raft.conf.Configuration in project dingo by dingodb.

the class RouteTable method refreshConfiguration.

public Status refreshConfiguration(final CliClientService cliClientService, final String groupId, final int timeoutMs) throws InterruptedException, TimeoutException {
    Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
    Requires.requireTrue(timeoutMs > 0, "Invalid timeout: " + timeoutMs);
    final Configuration conf = getConfiguration(groupId);
    if (conf == null) {
        return new Status(RaftError.ENOENT, "Group %s is not registered in RouteTable, forgot to call updateConfiguration?", groupId);
    }
    final Status st = Status.OK();
    PeerId leaderId = selectLeader(groupId);
    if (leaderId == null) {
        refreshLeader(cliClientService, groupId, timeoutMs);
        leaderId = selectLeader(groupId);
    }
    if (leaderId == null) {
        st.setError(-1, "Fail to get leader of group %s", groupId);
        return st;
    }
    if (!cliClientService.connect(leaderId.getEndpoint())) {
        st.setError(-1, "Fail to init channel to %s", leaderId);
        return st;
    }
    final CliRequests.GetPeersRequest.Builder rb = CliRequests.GetPeersRequest.newBuilder();
    rb.setGroupId(groupId);
    rb.setLeaderId(leaderId.toString());
    try {
        final Message result = cliClientService.getPeers(leaderId.getEndpoint(), rb.build(), null).get(timeoutMs, TimeUnit.MILLISECONDS);
        if (result instanceof CliRequests.GetPeersResponse) {
            final CliRequests.GetPeersResponse resp = (CliRequests.GetPeersResponse) result;
            final Configuration newConf = new Configuration();
            for (final String peerIdStr : resp.getPeersList()) {
                final PeerId newPeer = new PeerId();
                newPeer.parse(peerIdStr);
                newConf.addPeer(newPeer);
            }
            if (!conf.equals(newConf)) {
                LOG.info("Configuration of replication group {} changed from {} to {}", groupId, conf, newConf);
            }
            updateConfiguration(groupId, newConf);
        } else {
            final RpcRequests.ErrorResponse resp = (RpcRequests.ErrorResponse) result;
            st.setError(resp.getErrorCode(), resp.getErrorMsg());
        }
    } catch (final Exception e) {
        st.setError(-1, e.getMessage());
    }
    return st;
}
Also used : Configuration(io.dingodb.raft.conf.Configuration) Message(com.google.protobuf.Message) RpcRequests(io.dingodb.raft.rpc.RpcRequests) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) CliRequests(io.dingodb.raft.rpc.CliRequests) PeerId(io.dingodb.raft.entity.PeerId)

Aggregations

Configuration (io.dingodb.raft.conf.Configuration)31 PeerId (io.dingodb.raft.entity.PeerId)16 Status (io.dingodb.raft.Status)9 LogId (io.dingodb.raft.entity.LogId)5 ConfigurationEntry (io.dingodb.raft.conf.ConfigurationEntry)3 LogEntry (io.dingodb.raft.entity.LogEntry)3 CliRequests (io.dingodb.raft.rpc.CliRequests)3 TimeoutException (java.util.concurrent.TimeoutException)3 Message (com.google.protobuf.Message)2 RaftException (io.dingodb.raft.error.RaftException)2 NodeOptions (io.dingodb.raft.option.NodeOptions)2 RpcRequests (io.dingodb.raft.rpc.RpcRequests)2 ArrayList (java.util.ArrayList)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 ExecutionException (java.util.concurrent.ExecutionException)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 com.lmax.disruptor (com.lmax.disruptor)1 Disruptor (com.lmax.disruptor.dsl.Disruptor)1 ProducerType (com.lmax.disruptor.dsl.ProducerType)1 Closure (io.dingodb.raft.Closure)1