/** * Write log. * * @param bytes the bytes * @throws GondolaException thrown if the local member is not the leader * @throws InterruptedException the interrupted exception */ public void writeLog(byte[] bytes) throws GondolaException, InterruptedException { Command command = shard.checkoutCommand(); Timer.Context time = commitTimer.time(); command.commit(bytes, 0, bytes.length); time.stop(); } /**
/** * Retrieves a command object from the command pool. Blocks until there are commands in the pool. * * @return non-null command object */ public Command checkoutCommand() throws InterruptedException { Command command = pool.poll(); if (command == null) { command = new Command(gondola, this, cmember); } return command; }
try { byte[] buffer = line.substring(2).getBytes("UTF-8"); if (buffer.length > command.getCapacity()) { String msg = String.format("ERROR command too long. size=%d, max=%d", buffer.length, command.getCapacity()); logger.error(msg); return msg; command.commit(buffer, 0, buffer.length); } finally { command.release(); return String.format("SUCCESS: index %d", command.getIndex()); case "F": Role role = cluster.getLocalRole(); command = cluster.getCommittedCommand(index, timeout); try { return String.format("SUCCESS: %s", command.getString()); } finally { command.release();
/** * 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; } } }
/** * 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 GondolaException, InterruptedException, TimeoutException { if (index <= 0) { throw new IllegalStateException(String.format("Index %d must be > 0", index)); } Command command = checkoutCommand(); cmember.getCommittedLogEntry(command.ccmd, index, timeout); try { cmember.getCommittedLogEntry(command.ccmd, index, timeout); return command; } catch (GondolaException e) { command.release(); throw e; } }
/** * Retrieves a command object from the command pool. * Blocks until there are commands in the pool. * * @return non-null command object */ public Command checkoutCommand() throws InterruptedException { Command command = pool.poll(); if (command == null) { command = new Command(gondola, this, cmember); } return command; }