Search in sources :

Example 6 with PeerId

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

the class CliServiceImpl method transferLeader.

@Override
public Status transferLeader(final String groupId, final Configuration conf, final PeerId peer) {
    Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
    Requires.requireNonNull(conf, "Null configuration");
    Requires.requireNonNull(peer, "Null peer");
    final PeerId leaderId = new PeerId();
    final Status st = checkLeaderAndConnect(groupId, conf, leaderId);
    if (!st.isOk()) {
        return st;
    }
    final CliRequests.TransferLeaderRequest.Builder rb = // 
    CliRequests.TransferLeaderRequest.newBuilder().setGroupId(// 
    groupId).setLeaderId(leaderId.toString());
    if (!peer.isEmpty()) {
        rb.setPeerId(peer.toString());
    }
    try {
        final Message result = this.cliClientService.transferLeader(leaderId.getEndpoint(), rb.build(), null).get();
        return statusFromResponse(result);
    } catch (final Exception e) {
        return new Status(-1, e.getMessage());
    }
}
Also used : Status(io.dingodb.raft.Status) Message(com.google.protobuf.Message) JRaftException(io.dingodb.raft.error.JRaftException) PeerId(io.dingodb.raft.entity.PeerId)

Example 7 with PeerId

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

the class RouteTable method selectLeader.

/**
 * Get the cached leader of the group, return it when found, null otherwise.
 * Make sure calls {@link #refreshLeader(CliClientService, String, int)} already
 * before invoke this method.
 *
 * @param groupId raft group id
 * @return peer of leader
 */
public PeerId selectLeader(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();
    PeerId leader = gc.leader;
    if (!stampedLock.validate(stamp)) {
        stamp = stampedLock.readLock();
        try {
            leader = gc.leader;
        } finally {
            stampedLock.unlockRead(stamp);
        }
    }
    return leader;
}
Also used : StampedLock(java.util.concurrent.locks.StampedLock) PeerId(io.dingodb.raft.entity.PeerId)

Example 8 with PeerId

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

the class CliServiceImpl method resetPeer.

@Override
public Status resetPeer(final String groupId, final PeerId peerId, final Configuration newPeers) {
    Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
    Requires.requireNonNull(peerId, "Null peerId");
    Requires.requireNonNull(newPeers, "Null new peers");
    if (!this.cliClientService.connect(peerId.getEndpoint())) {
        return new Status(-1, "Fail to init channel to %s", peerId);
    }
    final CliRequests.ResetPeerRequest.Builder rb = // 
    CliRequests.ResetPeerRequest.newBuilder().setGroupId(// 
    groupId).setPeerId(peerId.toString());
    for (final PeerId peer : newPeers) {
        rb.addNewPeers(peer.toString());
    }
    try {
        final Message result = this.cliClientService.resetPeer(peerId.getEndpoint(), rb.build(), null).get();
        return statusFromResponse(result);
    } catch (final Exception e) {
        return new Status(-1, e.getMessage());
    }
}
Also used : Status(io.dingodb.raft.Status) Message(com.google.protobuf.Message) JRaftException(io.dingodb.raft.error.JRaftException) PeerId(io.dingodb.raft.entity.PeerId)

Example 9 with PeerId

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

the class CliServiceImpl method recordConfigurationChange.

private void recordConfigurationChange(final String groupId, final List<String> oldPeersList, final List<String> newPeersList) {
    final Configuration oldConf = new Configuration();
    for (final String peerIdStr : oldPeersList) {
        final PeerId oldPeer = new PeerId();
        oldPeer.parse(peerIdStr);
        oldConf.addPeer(oldPeer);
    }
    final Configuration newConf = new Configuration();
    for (final String peerIdStr : newPeersList) {
        final PeerId newPeer = new PeerId();
        newPeer.parse(peerIdStr);
        newConf.addPeer(newPeer);
    }
    LOG.info("Configuration of replication group {} changed from {} to {}.", groupId, oldConf, newConf);
}
Also used : Configuration(io.dingodb.raft.conf.Configuration) PeerId(io.dingodb.raft.entity.PeerId)

Example 10 with PeerId

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

the class CliServiceImpl method changePeers.

@Override
public Status changePeers(final String groupId, final Configuration conf, final Configuration newPeers) {
    Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
    Requires.requireNonNull(conf, "Null configuration");
    Requires.requireNonNull(newPeers, "Null new peers");
    final PeerId leaderId = new PeerId();
    final Status st = checkLeaderAndConnect(groupId, conf, leaderId);
    if (!st.isOk()) {
        return st;
    }
    final CliRequests.ChangePeersRequest.Builder rb = // 
    CliRequests.ChangePeersRequest.newBuilder().setGroupId(// 
    groupId).setLeaderId(leaderId.toString());
    for (final PeerId peer : newPeers) {
        rb.addNewPeers(peer.toString());
    }
    try {
        final Message result = this.cliClientService.changePeers(leaderId.getEndpoint(), rb.build(), null).get();
        if (result instanceof CliRequests.ChangePeersResponse) {
            final CliRequests.ChangePeersResponse resp = (CliRequests.ChangePeersResponse) result;
            recordConfigurationChange(groupId, resp.getOldPeersList(), resp.getNewPeersList());
            return Status.OK();
        } else {
            return statusFromResponse(result);
        }
    } catch (final Exception e) {
        return new Status(-1, e.getMessage());
    }
}
Also used : Status(io.dingodb.raft.Status) Message(com.google.protobuf.Message) CliRequests(io.dingodb.raft.rpc.CliRequests) JRaftException(io.dingodb.raft.error.JRaftException) 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