Command(Gondola gondola, Shard shard, CoreMember cmember) { this.shard = shard; ccmd = new CoreCmd(gondola, shard, cmember); }
/** * Blocks until the command in buffer has been committed or the timeout has expired. * After this call, this command object can be used again. * * @param timeout If -1, timeout is disabled. * @throws NotLeaderException if the member is not currently the leader. */ public void commit(byte[] buf, int bufOffset, int bufLen, int timeout) throws InterruptedException, GondolaException, TimeoutException { ccmd.commit(buf, bufOffset, bufLen, timeout); }
public CoreCmd(Gondola gondola, Cluster cluster, CoreMember cmember) { this.gondola = gondola; this.cluster = cluster; this.cmember = cmember; createdCount.incrementAndGet(); stats = gondola.getStats(); buffer = new byte[maxCommandSize]; reset(); }
/** * Throws an exception to any waiters for log entries. */ public void enterSlaveMode() { // Clear out get queue CoreCmd ccmd = getQueue.peek(); while (ccmd != null) { ccmd = getQueue.remove(); ccmd.update(Command.STATUS_SLAVE_MODE, cmember.leaderId); ccmd = getQueue.peek(); } }
CoreCmd.initConfig(config); Message.initConfig(config); Peer.initConfig(config);
ccmd.waitForLogEntry(index, timeout); if (ccmd.status == Command.STATUS_TIMEOUT) { getQueue.remove(ccmd);
/** * Called by SaveQueue and Peer whenever savedIndex or matchIndex increases. */ public void updateCommitIndex(int index) { /* int index = commitIndex; if (cmember.isFollower()) { saveQueue.getLatest(savedRid); index = Math.min(commitIndex, savedRid.index); } */ // Signal waiters for committed commands CoreCmd ccmd = getQueue.peek(); while (ccmd != null && ccmd.index <= index) { ccmd.update(Command.STATUS_OK, cmember.leaderId); getQueue.poll(); ccmd = getQueue.peek(); } }
/** * @param hostId the non-null id of the current host on which to start Gondola. */ public Gondola(Config config, String hostId) throws GondolaException { this.config = config; this.hostId = hostId; try { // Initialize static config values CoreCmd.initConfig(config); Message.initConfig(config); Peer.initConfig(config); ExceptionLogger.initConfig(config); messagePool = new MessagePool(config, stats); init(); } catch (Exception e) { throw new GondolaException(e); } }
ccmd.waitForLogEntry(index, timeout); if (ccmd.status == Command.STATUS_TIMEOUT) { getQueue.remove(ccmd);
/** * Called by SaveQueue and Peer whenever savedIndex or matchIndex increases. */ public void updateCommitIndex(int index) { /* int index = commitIndex; if (cmember.isFollower()) { saveQueue.getLatest(savedRid); index = Math.min(commitIndex, savedRid.index); } */ // Signal waiters for committed commands CoreCmd ccmd = getQueue.peek(); while (ccmd != null && ccmd.index <= index) { ccmd.update(Command.STATUS_OK, cmember.leaderId); getQueue.poll(); ccmd = getQueue.peek(); } }
public CoreCmd(Gondola gondola, Shard shard, CoreMember cmember) { this.gondola = gondola; this.shard = shard; this.cmember = cmember; createdCount.incrementAndGet(); stats = gondola.getStats(); buffer = new byte[maxCommandSize]; reset(); }
Command(Gondola gondola, Cluster cluster, CoreMember cmember) { this.cluster = cluster; ccmd = new CoreCmd(gondola, cluster, cmember); }
/** * Blocks until the command in buffer has been committed or the timeout has expired. * After this call, this command object can be used again. * * @param timeout If -1, timeout is disabled. * @return 0 if successful. * @throws NotLeaderException if the member is not currently the leader. */ public void commit(byte[] buf, int bufOffset, int bufLen, int timeout) throws InterruptedException, NotLeaderException, TimeoutException { ccmd.commit(buf, bufOffset, bufLen, timeout); }
/** * Called as a result of incoming append entry reply messages. Attempts to release as many waiting commands as * possible. */ void updateWaitingCommands() { int index = commitIndex; if (isLeader()) { // Commit CoreCmd ccmd = waitQueue.peek(); while (ccmd != null && ccmd.index <= index) { latency.tail(ccmd.index); ccmd.update(Command.STATUS_OK, leaderId); waitQueue.poll(); ccmd = waitQueue.peek(); } } // Update the getters saveQueue.getLatest(savedRid); index = Math.min(commitIndex, savedRid.index); // The min is needed for followers commitQueue.updateCommitIndex(index); }
reset(); this.index = index;
public void becomeLeader() throws Exception { logger.info("[{}-{}] Becomes LEADER for term {} {}", gondola.getHostId(), memberId, currentTerm, isPrimary ? "(primary)" : ""); become(Role.LEADER, memberId); // Initialize raft variables for (Peer peer : peers) { int nextIndex = sentRid.index + 1; peer.setNextIndex(nextIndex, nextIndex); peer.lastReceivedTs = clock.now(); } // If command queue is empty, add a no-op command to commit entries from the previous term if (commandQueue.size() == 0 && sentRid.term > 0 && sentRid.term < currentTerm) { commandQueue.add(new CoreCmd(gondola, cluster, this)); } }
/** * Equivalent to commit(buf, buffOffset, bufLen, -1). * * @throws NotLeaderException if the member is not currently the leader. */ public void commit(byte[] buf, int bufOffset, int bufLen) throws InterruptedException, GondolaException { try { ccmd.commit(buf, bufOffset, bufLen, -1); } catch (TimeoutException e) { throw new IllegalStateException(e); // Can't happen } }
/** * Called as a result of incoming append entry reply messages. * Attempts to release as many waiting commands as possible. */ void updateWaitingCommands() throws Exception { int index = commitIndex; if (isLeader()) { // Commit CoreCmd ccmd = waitQueue.peek(); while (ccmd != null && ccmd.index <= index) { latency.tail(ccmd.index); ccmd.update(Command.STATUS_OK, leaderId); waitQueue.poll(); ccmd = waitQueue.peek(); } } // Update the getters saveQueue.getLatest(savedRid, false); index = Math.min(commitIndex, savedRid.index); // The min is needed for followers commitQueue.updateCommitIndex(index); }
reset(); this.index = index;
public void becomeLeader() throws GondolaException { logger.info("[{}-{}] Becomes LEADER for term {} {}", gondola.getHostId(), memberId, currentTerm, isPrimary ? "(primary)" : ""); become(Role.LEADER, memberId); // Initialize raft variables for (Peer peer : peers.values()) { int nextIndex = sentRid.index + 1; peer.setNextIndex(nextIndex, nextIndex); peer.lastReceivedTs = clock.now(); } for (Peer slave : slaves) { int nextIndex = sentRid.index + 1; slave.setNextIndex(nextIndex, nextIndex); slave.lastReceivedTs = clock.now(); } // If command queue is empty, add a no-op command to commit entries from the previous term if (commandQueue.size() == 0 && sentRid.term < currentTerm) { commandQueue.add(new CoreCmd(gondola, shard, this)); } }