/** * Returns this command object back to the pool. */ public void release() { cluster.checkinCommand(this); } }
/** * Equivalent to getCommittedCommand(index, -1); * * @param index must be > 0. * @return the non-null Command at index. */ public Command getCommittedCommand(int index) throws Exception { return getCommittedCommand(index, -1); }
/** * Returns the current leader. * * @return null if the leader is not known. */ public Member getLeader() { return getMember(cmember.getLeaderId()); }
Command command = cluster.checkoutCommand(); try { byte[] buffer = line.substring(2).getBytes("UTF-8"); Role role = cluster.getLocalRole(); if (cluster.getLocalRole() == Role.LEADER) { logger.info("This member is already a leader"); } else { cluster.forceLeader(timeout); return "SUCCESS: This member is now " + cluster.getLocalRole(); case "m": return String.format("SUCCESS: %s", cluster.getLocalRole()); case "g": command = cluster.getCommittedCommand(index, timeout); try { return String.format("SUCCESS: %s", command.getString()); LogEntry le = gondola.getStorage().getLastLogEntry(cluster.getLocalMember().getMemberId()); int savedIndex = 0; if (le != null) { cluster.getLocalRole(), cluster.getLastSavedIndex(), savedIndex); case "n": if (args.length < 2) {
if (cluster.getLocalMember().isLeader()) { Command command = cluster.checkoutCommand(); bbuffer.clear(); bbuffer.putInt(id);
index = Math.max(1, cluster.getLastSavedIndex()); } catch (Exception e) { e.printStackTrace(); while (true) { try { command = cluster.getCommittedCommand(index, 5000); System.arraycopy(command.getBuffer(), 0, buffer, 0, command.getSize()); command.release();
public void start() throws Exception { logger.info("Starting up Gondola..."); clock.start(); network.start(); storage.start(); for (Cluster c : clusters) { c.start(); } // Start local threads assert threads.size() == 0; threads.add(new RoleChangeNotifier()); threads.forEach(t -> t.start()); }
/** * Returns the command at the specified index. This method blocks until index has been committed. * An empty command can be returned. This is an artifact of the raft protocol to avoid deadlock when * used with a finite thread pool. Empty commands will be inserted right after a leader election when * the new leader discovers that it has uncommitted commands. * The leader inserts an empty command to commit these immediately. * * @param index Must be > 0. * @param timeout Returns after timeout milliseconds, even if the command is not yet available. -1 means there is no timeout. * @return non-null Command */ public Command getCommittedCommand(int index, int timeout) throws Exception { if (index <= 0) { throw new IllegalStateException(String.format("Index %d must be > 0", index)); } Command command = checkoutCommand(); try { cmember.getCommittedLogEntry(command.ccmd, index, timeout); return command; } catch (Exception e) { command.release(); throw e; } } }
Cluster cluster = new Cluster(this, clusterId);
Thread.sleep(5000); double avgLatency = 1.0 * waitTime.get() / requests.get(); if (cluster.getLocalMember().isLeader()) { logger.info(String.format("commits: %.2f/s latency: %.2f ms (%.2fms)", (requests.get() - start) / 5.0, avgLatency, latency.get()));
/** * ***************************** role ********************************* */ void become(Role role, int leaderId) throws Exception { // Clear peers and reset storage state peers.forEach(p -> p.reset()); saveQueue.settle(savedRid); sentRid.set(savedRid); Role oldRole = this.role; this.role = role; this.leaderId = leaderId; // Clear any waiting committers if (role != Role.LEADER) { CoreCmd ccmd = waitQueue.peek(); while (ccmd != null) { ccmd.update(Command.STATUS_NOT_LEADER, leaderId); waitQueue.poll(); ccmd = waitQueue.peek(); } } // Notify gondola listeners of role change. if (role != oldRole) { gondola.notifyRoleChange( new RoleChangeEvent(cluster, cluster.getMember(memberId), cluster.getMember(leaderId), oldRole, role)); } }