Java Code Examples for com.alipay.sofa.jraft.error.RaftError#EINVAL
The following examples show how to use
com.alipay.sofa.jraft.error.RaftError#EINVAL .
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: NodeImpl.java From sofa-jraft with Apache License 2.0 | 5 votes |
@Override public Status resetPeers(final Configuration newPeers) { Requires.requireNonNull(newPeers, "Null new peers"); Requires.requireTrue(!newPeers.isEmpty(), "Empty new peers"); Requires.requireTrue(newPeers.isValid(), "Invalid new peers: %s", newPeers); this.writeLock.lock(); try { if (newPeers.isEmpty()) { LOG.warn("Node {} set empty peers.", getNodeId()); return new Status(RaftError.EINVAL, "newPeers is empty"); } if (!this.state.isActive()) { LOG.warn("Node {} is in state {}, can't set peers.", getNodeId(), this.state); return new Status(RaftError.EPERM, "Bad state: %s", this.state); } // bootstrap? if (this.conf.getConf().isEmpty()) { LOG.info("Node {} set peers to {} from empty.", getNodeId(), newPeers); this.conf.setConf(newPeers); stepDown(this.currTerm + 1, false, new Status(RaftError.ESETPEER, "Set peer from empty configuration")); return Status.OK(); } if (this.state == State.STATE_LEADER && this.confCtx.isBusy()) { LOG.warn("Node {} set peers need wait current conf changing.", getNodeId()); return new Status(RaftError.EBUSY, "Changing to another configuration"); } // check equal, maybe retry direct return if (this.conf.getConf().equals(newPeers)) { return Status.OK(); } final Configuration newConf = new Configuration(newPeers); LOG.info("Node {} set peers from {} to {}.", getNodeId(), this.conf.getConf(), newPeers); this.conf.setConf(newConf); this.conf.getOldConf().reset(); stepDown(this.currTerm + 1, false, new Status(RaftError.ESETPEER, "Raft node set peer normally")); return Status.OK(); } finally { this.writeLock.unlock(); } }
Example 2
Source File: NodeImpl.java From sofa-jraft with Apache License 2.0 | 5 votes |
private void doSnapshot(final Closure done) { if (this.snapshotExecutor != null) { this.snapshotExecutor.doSnapshot(done); } else { if (done != null) { final Status status = new Status(RaftError.EINVAL, "Snapshot is not supported"); Utils.runClosureInThread(done, status); } } }
Example 3
Source File: NodeImpl.java From sofa-jraft with Apache License 2.0 | 4 votes |
@Override public Status transferLeadershipTo(final PeerId peer) { Requires.requireNonNull(peer, "Null peer"); this.writeLock.lock(); try { if (this.state != State.STATE_LEADER) { LOG.warn("Node {} can't transfer leadership to peer {} as it is in state {}.", getNodeId(), peer, this.state); return new Status(this.state == State.STATE_TRANSFERRING ? RaftError.EBUSY : RaftError.EPERM, "Not a leader"); } if (this.confCtx.isBusy()) { // It's very messy to deal with the case when the |peer| received // TimeoutNowRequest and increase the term while somehow another leader // which was not replicated with the newest configuration has been // elected. If no add_peer with this very |peer| is to be invoked ever // after nor this peer is to be killed, this peer will spin in the voting // procedure and make the each new leader stepped down when the peer // reached vote timeout and it starts to vote (because it will increase // the term of the group) // To make things simple, refuse the operation and force users to // invoke transfer_leadership_to after configuration changing is // completed so that the peer's configuration is up-to-date when it // receives the TimeOutNowRequest. LOG.warn( "Node {} refused to transfer leadership to peer {} when the leader is changing the configuration.", getNodeId(), peer); return new Status(RaftError.EBUSY, "Changing the configuration"); } PeerId peerId = peer.copy(); // if peer_id is ANY_PEER(0.0.0.0:0:0), the peer with the largest // last_log_id will be selected. if (peerId.equals(PeerId.ANY_PEER)) { LOG.info("Node {} starts to transfer leadership to any peer.", getNodeId()); if ((peerId = this.replicatorGroup.findTheNextCandidate(this.conf)) == null) { return new Status(-1, "Candidate not found for any peer"); } } if (peerId.equals(this.serverId)) { LOG.info("Node {} transferred leadership to self.", this.serverId); return Status.OK(); } if (!this.conf.contains(peerId)) { LOG.info("Node {} refused to transfer leadership to peer {} as it is not in {}.", getNodeId(), peer, this.conf); return new Status(RaftError.EINVAL, "Not in current configuration"); } final long lastLogIndex = this.logManager.getLastLogIndex(); if (!this.replicatorGroup.transferLeadershipTo(peerId, lastLogIndex)) { LOG.warn("No such peer {}.", peer); return new Status(RaftError.EINVAL, "No such peer %s", peer); } this.state = State.STATE_TRANSFERRING; final Status status = new Status(RaftError.ETRANSFERLEADERSHIP, "Raft leader is transferring leadership to %s", peerId); onLeaderStop(status); LOG.info("Node {} starts to transfer leadership to peer {}.", getNodeId(), peer); final StopTransferArg stopArg = new StopTransferArg(this, this.currTerm, peerId); this.stopTransferArg = stopArg; this.transferTimer = this.timerManager.schedule(() -> onTransferTimeout(stopArg), this.options.getElectionTimeoutMs(), TimeUnit.MILLISECONDS); } finally { this.writeLock.unlock(); } return Status.OK(); }