Java Code Examples for com.alipay.sofa.jraft.entity.PeerId#parse()

The following examples show how to use com.alipay.sofa.jraft.entity.PeerId#parse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RaftClient.java    From sofa-registry with Apache License 2.0 6 votes vote down vote up
private Object redirectRequest(ProcessRequest request, String redirect) {
    try {
        PeerId redirectLead = new PeerId();
        if (!redirectLead.parse(redirect)) {
            throw new IllegalArgumentException("Fail to parse serverId:" + redirect);
        }

        //wait for onLeaderStart
        TimeUnit.MILLISECONDS.sleep(1000);

        LOGGER.info("Redirect request send to return peer {},request {}", redirect, request);
        Object response = this.rpcClient.invokeSync(redirectLead.getEndpoint().toString(),
            request, cliOptions.getRpcDefaultTimeout());
        ProcessResponse cmd = (ProcessResponse) response;
        if (cmd.getSuccess()) {
            RouteTable.getInstance().updateLeader(groupId, redirectLead);
            return cmd.getEntity();
        } else {
            refreshLeader();
            throw new IllegalStateException("Redirect request server error:" + cmd.getEntity());
        }
    } catch (Exception e) {
        LOGGER.error("Redirect process request error!", e);
        throw new RuntimeException("Redirect process request error!" + e.getMessage(), e);
    }
}
 
Example 2
Source File: NodeRequestProcessor.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Override
public Message processRequest(final T request, final RpcRequestClosure done) {
    final PeerId peer = new PeerId();
    final String peerIdStr = getPeerId(request);
    if (peer.parse(peerIdStr)) {
        final String groupId = getGroupId(request);
        final Node node = NodeManager.getInstance().get(groupId, peer);
        if (node != null) {
            return processRequest0((RaftServerService) node, request, done);
        } else {
            return RpcFactoryHelper //
                .responseFactory() //
                .newResponse(defaultResp(), RaftError.ENOENT, "Peer id not found: %s, group: %s", peerIdStr,
                    groupId);
        }
    } else {
        return RpcFactoryHelper //
            .responseFactory() //
            .newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peerId: %s", peerIdStr);
    }
}
 
Example 3
Source File: AppendEntriesRequestProcessor.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Override
public void onClosed(final String remoteAddress, final Connection conn) {
    final PeerId peer = new PeerId();
    final String peerAttr = (String) conn.getAttribute(PEER_ATTR);

    if (!StringUtils.isBlank(peerAttr) && peer.parse(peerAttr)) {
        // Clear request context when connection disconnected.
        for (final Map.Entry<String, ConcurrentMap<String, PeerRequestContext>> entry : this.peerRequestContexts
            .entrySet()) {
            final ConcurrentMap<String, PeerRequestContext> groupCtxs = entry.getValue();
            synchronized (Utils.withLockObject(groupCtxs)) {
                final PeerRequestContext ctx = groupCtxs.remove(peer.toString());
                if (ctx != null) {
                    ctx.destroy();
                }
            }
        }
    } else {
        LOG.info("Connection disconnected: {}", remoteAddress);
    }
}
 
Example 4
Source File: AbstractCliRequestProcessorTest.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Test
public void testHandleRequest() {
    this.mockNodes(3);
    Mockito.when(this.node.getGroupId()).thenReturn(this.groupId);
    PeerId peerId = new PeerId();
    peerId.parse(this.peerIdStr);
    Mockito.when(this.node.getOptions()).thenReturn(new NodeOptions());
    Mockito.when(this.node.getNodeId()).thenReturn(new NodeId("test", peerId));
    NodeManager.getInstance().addAddress(peerId.getEndpoint());
    NodeManager.getInstance().add(this.node);

    BaseCliRequestProcessor<T> processor = newProcessor();
    processor.handleRequest(this.asyncContext, createRequest(this.groupId, peerId));
    ArgumentCaptor<Closure> doneArg = ArgumentCaptor.forClass(Closure.class);
    verify(processor.interest(), this.node, doneArg);
}
 
Example 5
Source File: CliServiceTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public Status getLeader(final String groupId, final Configuration conf, final PeerId leaderId) {
    final PeerId ret = this.rebalancedLeaderIds.get(groupId);
    if (ret != null) {
        leaderId.parse(ret.toString());
    } else {
        leaderId.parse(this.initialLeaderId.toString());
    }
    return Status.OK();
}
 
Example 6
Source File: DmetaServer.java    From distkv with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(final String[] args) throws IOException {
  if (args.length != 4) {
    System.out
        .println("Useage :  {dataPath} {groupId} {serverId} {initConf}");
    System.out
        .println("Example:  /tmp/server1 counter 127.0.0.1:8081 " +
            "127.0.0.1:8081,127.0.0.1:8082,127.0.0.1:8083");
    System.exit(1);
  }
  final String dataPath = args[0];
  final String groupId = args[1];
  final String serverIdStr = args[2];
  final String initConfStr = args[3];

  final NodeOptions nodeOptions = new NodeOptions();
  // Set the election timeout to 1 second.
  nodeOptions.setElectionTimeoutMs(1000);
  // close CLI service.
  nodeOptions.setDisableCli(false);
  //30s snapshot
  nodeOptions.setSnapshotIntervalSecs(30);
  // parser
  final PeerId serverId = new PeerId();
  if (!serverId.parse(serverIdStr)) {
    throw new IllegalArgumentException("Fail to parse serverId:" + serverIdStr);
  }
  final Configuration initConf = new Configuration();
  if (!initConf.parse(initConfStr)) {
    throw new IllegalArgumentException("Fail to parse initConf:" + initConfStr);
  }
  // set origin conf
  nodeOptions.setInitialConf(initConf);

  // start
  final DmetaServer counterServer = new DmetaServer(dataPath, groupId, serverId, nodeOptions);
  System.out.println("Started DMeta server at port:"
      + counterServer.getNode().getNodeId().getPeerId().getPort());
}
 
Example 7
Source File: ClientServiceConnectionEventProcessor.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public void onEvent(final String remoteAddr, final Connection conn) {
    final PeerId peer = new PeerId();
    if (peer.parse(remoteAddr)) {
        LOG.info("Peer {} is connected", peer);
        this.rgGroup.checkReplicator(peer, true);
    } else {
        LOG.error("Fail to parse peer: {}", remoteAddr);
    }
}
 
Example 8
Source File: AppendEntriesRequestProcessor.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
PeerRequestContext getPeerRequestContext(final String groupId, final String peerId, final Connection conn) {
    ConcurrentMap<String/* peerId */, PeerRequestContext> groupContexts = this.peerRequestContexts.get(groupId);
    if (groupContexts == null) {
        groupContexts = new ConcurrentHashMap<>();
        final ConcurrentMap<String, PeerRequestContext> existsCtxs = this.peerRequestContexts.putIfAbsent(groupId,
            groupContexts);
        if (existsCtxs != null) {
            groupContexts = existsCtxs;
        }
    }

    PeerRequestContext peerCtx = groupContexts.get(peerId);
    if (peerCtx == null) {
        synchronized (Utils.withLockObject(groupContexts)) {
            peerCtx = groupContexts.get(peerId);
            // double check in lock
            if (peerCtx == null) {
                // only one thread to process append entries for every jraft node
                final PeerId peer = new PeerId();
                final boolean parsed = peer.parse(peerId);
                assert (parsed);
                final Node node = NodeManager.getInstance().get(groupId, peer);
                assert (node != null);
                peerCtx = new PeerRequestContext(groupId, peerId, node.getRaftOptions()
                    .getMaxReplicatorInflightMsgs());
                groupContexts.put(peerId, peerCtx);
            }
        }
    }
    // Set peer attribute into connection if absent
    if (conn != null && conn.getAttribute(PEER_ATTR) == null) {
        conn.setAttribute(PEER_ATTR, peerId);
    }
    return peerCtx;
}
 
Example 9
Source File: BaseNodeRequestProcessorTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
protected PeerId mockNode() {
    Mockito.when(node.getGroupId()).thenReturn(this.groupId);
    final PeerId peerId = new PeerId();
    peerId.parse(this.peerIdStr);
    Mockito.when(node.getNodeId()).thenReturn(new NodeId(groupId, peerId));
    NodeManager.getInstance().addAddress(peerId.getEndpoint());
    NodeManager.getInstance().add(node);
    return peerId;
}
 
Example 10
Source File: KitRaft.java    From KitDB with Apache License 2.0 5 votes vote down vote up
public void transferLeader(String nodeConf) {
    PeerId serverId = new PeerId();
    if (!serverId.parse(nodeConf)) {
        throw new IllegalArgumentException("Fail to parse Conf:" + nodeConf);
    }
    node.transferLeadershipTo(serverId);
}
 
Example 11
Source File: AddPeerRequestProcessor.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
protected Message processRequest0(final CliRequestContext ctx, final AddPeerRequest request, final RpcRequestClosure done) {
    final List<PeerId> oldPeers = ctx.node.listPeers();
    final String addingPeerIdStr = request.getPeerId();
    final PeerId addingPeer = new PeerId();
    if (addingPeer.parse(addingPeerIdStr)) {
        LOG.info("Receive AddPeerRequest to {} from {}, adding {}", ctx.node.getNodeId(), done.getRpcCtx()
            .getRemoteAddress(), addingPeerIdStr);
        ctx.node.addPeer(addingPeer, status -> {
            if (!status.isOk()) {
                done.run(status);
            } else {
                final AddPeerResponse.Builder rb = AddPeerResponse.newBuilder();
                boolean alreadyExists = false;
                for (final PeerId oldPeer : oldPeers) {
                    rb.addOldPeers(oldPeer.toString());
                    rb.addNewPeers(oldPeer.toString());
                    if (oldPeer.equals(addingPeer)) {
                        alreadyExists = true;
                    }
                }
                if (!alreadyExists) {
                    rb.addNewPeers(addingPeerIdStr);
                }
                done.sendResponse(rb.build());
            }
        });
    } else {
        return RpcFactoryHelper //
            .responseFactory() //
            .newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peer id %s", addingPeerIdStr);
    }

    return null;
}
 
Example 12
Source File: CliServiceTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public Status getLeader(final String groupId, final Configuration conf, final PeerId leaderId) {
    final PeerId ret = this.rebalancedLeaderIds.get(groupId);
    if (ret != null) {
        leaderId.parse(ret.toString());
    } else {
        leaderId.parse(this.initialLeaderId.toString());
    }
    return Status.OK();
}
 
Example 13
Source File: RaftServer.java    From sofa-registry with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param dataPath    Example: /tmp/server1
 * @param groupId
 * @param serverIdStr Example: 127.0.0.1:8081
 * @param initConfStr Example: 127.0.0.1:8081,127.0.0.1:8082,127.0.0.1:8083
 * @throws IOException
 */
public RaftServer(String dataPath, String groupId, String serverIdStr, String initConfStr) {
    this.dataPath = dataPath;
    this.groupId = groupId;
    serverId = new PeerId();
    if (!serverId.parse(serverIdStr)) {
        throw new IllegalArgumentException("Fail to parse serverId:" + serverIdStr);
    }

    initConf = new Configuration();
    if (!initConf.parse(initConfStr)) {
        throw new IllegalArgumentException("Fail to parse initConf:" + initConfStr);
    }
}
 
Example 14
Source File: AtomicServer.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
public void start() throws IOException {
    PeerId serverId = new PeerId();
    if (!serverId.parse(conf.getServerAddress())) {
        throw new IllegalArgumentException("Fail to parse serverId:" + conf.getServerAddress());
    }

    FileUtils.forceMkdir(new File(conf.getDataPath()));
    // The same in-process raft group shares the same RPC Server.
    RpcServer rpcServer = RaftRpcServerFactory.createRaftRpcServer(serverId.getEndpoint());
    // Register biz handler
    rpcServer.registerProcessor(new GetSlotsCommandProcessor(this));
    rpcServer.registerProcessor(new GetCommandProcessor(this));
    rpcServer.registerProcessor(new IncrementAndGetCommandProcessor(this));
    rpcServer.registerProcessor(new CompareAndSetCommandProcessor(this));
    rpcServer.registerProcessor(new SetCommandProcessor(this));

    long step = conf.getMaxSlot() / totalSlots;
    if (conf.getMaxSlot() % totalSlots > 0) {
        step = step + 1;
    }
    for (int i = 0; i < totalSlots; i++) {
        long min = i * step;
        long mayMax = (i + 1) * step;
        long max = mayMax > conf.getMaxSlot() || mayMax <= 0 ? conf.getMaxSlot() : mayMax;
        StartupConf nodeConf = new StartupConf();
        String nodeDataPath = conf.getDataPath() + File.separator + i;
        nodeConf.setDataPath(nodeDataPath);
        String nodeGroup = conf.getGroupId() + "_" + i;
        nodeConf.setGroupId(nodeGroup);
        nodeConf.setMaxSlot(max);
        nodeConf.setMinSlot(min);
        nodeConf.setConf(conf.getConf());
        nodeConf.setServerAddress(conf.getServerAddress());
        nodeConf.setTotalSlots(conf.getTotalSlots());
        LOG.info("Starting range node {}-{} with conf {}", min, max, nodeConf);
        nodes.put(i * step, AtomicRangeGroup.start(nodeConf, rpcServer));
        groups.put(i * step, nodeGroup);
    }
}
 
Example 15
Source File: LogEntryV2CodecFactoryTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
private List<PeerId> createLearners(final String... peers) {
    List<PeerId> ret = new ArrayList<>();
    for (String s : peers) {
        PeerId e = new PeerId();
        e.parse(s);
        ret.add(e);
    }
    return ret;
}
 
Example 16
Source File: KitRaft.java    From KitDB with Apache License 2.0 4 votes vote down vote up
public void addNode(String nodeConf) {
    PeerId peer = new PeerId();
    peer.parse(nodeConf);
    node.addPeer(peer, s -> LOG.error("addNode error", s.getErrorMsg()));
}
 
Example 17
Source File: KitRaft.java    From KitDB with Apache License 2.0 4 votes vote down vote up
public KitRaft(GroupConfig groupConfig, NodeConfig nodeConfig, DB db) throws IOException {

        NodeOptions nodeOptions = new NodeOptions();

        RaftOptions raftOptions = new RaftOptions();
        raftOptions.setDisruptorBufferSize(16 * 16384);
        raftOptions.setApplyBatch(128);
        raftOptions.setSync(false);
        nodeOptions.setRaftOptions(raftOptions);

        nodeOptions.setElectionTimeoutMs(groupConfig.getElectionTimeoutMs());
        nodeOptions.setDisableCli(true);
        nodeOptions.setSnapshotIntervalSecs(groupConfig.getSnapshotIntervalSecs());

        PeerId serverId = new PeerId();
        if (!serverId.parse(nodeConfig.getNode())) {
            throw new IllegalArgumentException("Fail to parse serverId:" + nodeConfig.getNode());
        }

        Configuration initConf = new Configuration();
        if (!initConf.parse(groupConfig.getInitNodes())) {
            throw new IllegalArgumentException("Fail to parse initConf:" + groupConfig.getInitNodes());
        }

        nodeOptions.setInitialConf(initConf);

        String raftDir = nodeConfig.getRaftDir();
        FileUtils.forceMkdir(new File(raftDir));

        RpcServer rpcServer = new RpcServer(serverId.getPort());
        RaftRpcServerFactory.addRaftRequestProcessors(rpcServer);

        this.dbsm = new DBStateMachine();
        dbsm.setDbRequestProcessor(new DBRequestProcessor(this));
        dbsm.setDB(db);
        nodeOptions.setFsm(this.dbsm);

        nodeOptions.setLogUri(raftDir + File.separator + "log");
        nodeOptions.setRaftMetaUri(raftDir + File.separator + "raft_meta");
        nodeOptions.setSnapshotUri(raftDir + File.separator + "snapshot");

        this.raftGroupService = new RaftGroupService(groupConfig.getGroup(), serverId, nodeOptions, rpcServer);
        // 启动
        this.node = this.raftGroupService.start();
    }
 
Example 18
Source File: NodeImpl.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@Override
public Message handleRequestVoteRequest(final RequestVoteRequest request) {
    boolean doUnlock = true;
    this.writeLock.lock();
    try {
        if (!this.state.isActive()) {
            LOG.warn("Node {} is not in active state, currTerm={}.", getNodeId(), this.currTerm);
            return RpcFactoryHelper //
                .responseFactory() //
                .newResponse(RequestVoteResponse.getDefaultInstance(), RaftError.EINVAL,
                    "Node %s is not in active state, state %s.", getNodeId(), this.state.name());
        }
        final PeerId candidateId = new PeerId();
        if (!candidateId.parse(request.getServerId())) {
            LOG.warn("Node {} received RequestVoteRequest from {} serverId bad format.", getNodeId(),
                request.getServerId());
            return RpcFactoryHelper //
                .responseFactory() //
                .newResponse(RequestVoteResponse.getDefaultInstance(), RaftError.EINVAL,
                    "Parse candidateId failed: %s.", request.getServerId());
        }

        // noinspection ConstantConditions
        do {
            // check term
            if (request.getTerm() >= this.currTerm) {
                LOG.info("Node {} received RequestVoteRequest from {}, term={}, currTerm={}.", getNodeId(),
                    request.getServerId(), request.getTerm(), this.currTerm);
                // increase current term, change state to follower
                if (request.getTerm() > this.currTerm) {
                    stepDown(request.getTerm(), false, new Status(RaftError.EHIGHERTERMRESPONSE,
                        "Raft node receives higher term RequestVoteRequest."));
                }
            } else {
                // ignore older term
                LOG.info("Node {} ignore RequestVoteRequest from {}, term={}, currTerm={}.", getNodeId(),
                    request.getServerId(), request.getTerm(), this.currTerm);
                break;
            }
            doUnlock = false;
            this.writeLock.unlock();

            final LogId lastLogId = this.logManager.getLastLogId(true);

            doUnlock = true;
            this.writeLock.lock();
            // vote need ABA check after unlock&writeLock
            if (request.getTerm() != this.currTerm) {
                LOG.warn("Node {} raise term {} when get lastLogId.", getNodeId(), this.currTerm);
                break;
            }

            final boolean logIsOk = new LogId(request.getLastLogIndex(), request.getLastLogTerm())
                .compareTo(lastLogId) >= 0;

            if (logIsOk && (this.votedId == null || this.votedId.isEmpty())) {
                stepDown(request.getTerm(), false, new Status(RaftError.EVOTEFORCANDIDATE,
                    "Raft node votes for some candidate, step down to restart election_timer."));
                this.votedId = candidateId.copy();
                this.metaStorage.setVotedFor(candidateId);
            }
        } while (false);

        return RequestVoteResponse.newBuilder() //
            .setTerm(this.currTerm) //
            .setGranted(request.getTerm() == this.currTerm && candidateId.equals(this.votedId)) //
            .build();
    } finally {
        if (doUnlock) {
            this.writeLock.unlock();
        }
    }
}
 
Example 19
Source File: NodeImpl.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@Override
public Message handleInstallSnapshot(final InstallSnapshotRequest request, final RpcRequestClosure done) {
    if (this.snapshotExecutor == null) {
        return RpcFactoryHelper //
            .responseFactory() //
            .newResponse(InstallSnapshotResponse.getDefaultInstance(), RaftError.EINVAL, "Not supported snapshot");
    }
    final PeerId serverId = new PeerId();
    if (!serverId.parse(request.getServerId())) {
        LOG.warn("Node {} ignore InstallSnapshotRequest from {} bad server id.", getNodeId(), request.getServerId());
        return RpcFactoryHelper //
            .responseFactory() //
            .newResponse(InstallSnapshotResponse.getDefaultInstance(), RaftError.EINVAL,
                "Parse serverId failed: %s", request.getServerId());
    }

    this.writeLock.lock();
    try {
        if (!this.state.isActive()) {
            LOG.warn("Node {} ignore InstallSnapshotRequest as it is not in active state {}.", getNodeId(),
                this.state);
            return RpcFactoryHelper //
                .responseFactory() //
                .newResponse(InstallSnapshotResponse.getDefaultInstance(), RaftError.EINVAL,
                    "Node %s:%s is not in active state, state %s.", this.groupId, this.serverId, this.state.name());
        }

        if (request.getTerm() < this.currTerm) {
            LOG.warn("Node {} ignore stale InstallSnapshotRequest from {}, term={}, currTerm={}.", getNodeId(),
                request.getPeerId(), request.getTerm(), this.currTerm);
            return InstallSnapshotResponse.newBuilder() //
                .setTerm(this.currTerm) //
                .setSuccess(false) //
                .build();
        }

        checkStepDown(request.getTerm(), serverId);

        if (!serverId.equals(this.leaderId)) {
            LOG.error("Another peer {} declares that it is the leader at term {} which was occupied by leader {}.",
                serverId, this.currTerm, this.leaderId);
            // Increase the term by 1 and make both leaders step down to minimize the
            // loss of split brain
            stepDown(request.getTerm() + 1, false, new Status(RaftError.ELEADERCONFLICT,
                "More than one leader in the same term."));
            return InstallSnapshotResponse.newBuilder() //
                .setTerm(request.getTerm() + 1) //
                .setSuccess(false) //
                .build();
        }

    } finally {
        this.writeLock.unlock();
    }
    final long startMs = Utils.monotonicMs();
    try {
        if (LOG.isInfoEnabled()) {
            LOG.info(
                "Node {} received InstallSnapshotRequest from {}, lastIncludedLogIndex={}, lastIncludedLogTerm={}, lastLogId={}.",
                getNodeId(), request.getServerId(), request.getMeta().getLastIncludedIndex(), request.getMeta()
                    .getLastIncludedTerm(), this.logManager.getLastLogId(false));
        }
        this.snapshotExecutor.installSnapshot(request, InstallSnapshotResponse.newBuilder(), done);
        return null;
    } finally {
        this.metrics.recordLatency("install-snapshot", Utils.monotonicMs() - startMs);
    }
}
 
Example 20
Source File: KitRaft.java    From KitDB with Apache License 2.0 4 votes vote down vote up
public void removeNode(String nodeConf) {
    PeerId peer = new PeerId();
    peer.parse(nodeConf);
    node.removePeer(peer, s -> LOG.error("removeNode error", s.getErrorMsg()));
}