@Override public long append(LogEntry logEntry) { try{ PreparedStatement ps = this.connection.prepareStatement(INSERT_ENTRY_SQL); ps.setLong(1, logEntry.getTerm()); ps.setByte(2, logEntry.getValueType().toByte()); ps.setBytes(3, logEntry.getValue()); ps.execute(); this.connection.commit(); this.lastEntry = logEntry; return this.nextIndex.getAndIncrement(); }catch(Throwable error){ this.logger.error("failed to insert a new entry", error); throw new RuntimeException("log store error", error); } }
@Override public long append(LogEntry logEntry) { try{ PreparedStatement ps = this.connection.prepareStatement(INSERT_ENTRY_SQL); ps.setLong(1, logEntry.getTerm()); ps.setByte(2, logEntry.getValueType().toByte()); ps.setBytes(3, logEntry.getValue()); ps.execute(); this.connection.commit(); this.lastEntry = logEntry; return this.nextIndex.getAndIncrement(); }catch(Throwable error){ this.logger.error("failed to insert a new entry", error); throw new RuntimeException("log store error", error); } }
@Override public void writeAt(long index, LogEntry logEntry) { if(index >= this.nextIndex.get() || index < this.startIndex.get()){ throw new IllegalArgumentException("index out of range"); } try{ PreparedStatement ps = this.connection.prepareStatement(UPDATE_ENTRY_SQL); ps.setLong(1, logEntry.getTerm()); ps.setByte(2, logEntry.getValueType().toByte()); ps.setBytes(3, logEntry.getValue()); ps.setLong(4, index); ps.execute(); ps = this.connection.prepareStatement(TRIM_TABLE_SQL); ps.setLong(1, index); ps.execute(); ps = this.connection.prepareStatement(UPDATE_SEQUENCE_SQL); ps.setLong(1, index + 1); ps.execute(); this.connection.commit(); this.nextIndex.set(index + 1); this.lastEntry = logEntry; }catch(Throwable error){ this.logger.error("failed to write an entry at a specific index", error); throw new RuntimeException("log store error", error); } }
@Override public void writeAt(long index, LogEntry logEntry) { if(index >= this.nextIndex.get() || index < this.startIndex.get()){ throw new IllegalArgumentException("index out of range"); } try{ PreparedStatement ps = this.connection.prepareStatement(UPDATE_ENTRY_SQL); ps.setLong(1, logEntry.getTerm()); ps.setByte(2, logEntry.getValueType().toByte()); ps.setBytes(3, logEntry.getValue()); ps.setLong(4, index); ps.execute(); ps = this.connection.prepareStatement(TRIM_TABLE_SQL); ps.setLong(1, index); ps.execute(); ps = this.connection.prepareStatement(UPDATE_SEQUENCE_SQL); ps.setLong(1, index + 1); ps.execute(); this.connection.commit(); this.nextIndex.set(index + 1); this.lastEntry = logEntry; }catch(Throwable error){ this.logger.error("failed to write an entry at a specific index", error); throw new RuntimeException("log store error", error); } }
public static byte[] logEntryToBytes(LogEntry logEntry){ ByteArrayOutputStream output = new ByteArrayOutputStream(); try{ output.write(longToBytes(logEntry.getTerm())); output.write(logEntry.getValueType().toByte()); output.write(intToBytes(logEntry.getValue().length)); output.write(logEntry.getValue()); output.flush(); return output.toByteArray(); }catch(IOException exception){ LogManager.getLogger("BinaryUtil").error("failed to serialize LogEntry to memory", exception); throw new RuntimeException("Running into bad situation, where memory may not be sufficient", exception); } }
@Override public long append(LogEntry logEntry) { try{ this.storeWriteLock.lock(); this.indexFile.seek(this.indexFile.length()); long dataFileLength = this.dataFile.length(); this.indexFile.writeLong(dataFileLength); this.dataFile.seek(dataFileLength); ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES + 1 + logEntry.getValue().length); buffer.putLong(logEntry.getTerm()); buffer.put(logEntry.getValueType().toByte()); buffer.put(logEntry.getValue()); this.dataFile.write(buffer.array()); this.entriesInStore += 1; this.buffer.append(logEntry); return this.entriesInStore + this.startIndex - 1; }catch(IOException exception){ this.logger.error("failed to append a log entry to store", exception); throw new RuntimeException(exception.getMessage(), exception); }finally{ this.storeWriteLock.unlock(); } }
public static byte[] logEntryToBytes(LogEntry logEntry){ ByteArrayOutputStream output = new ByteArrayOutputStream(); try{ output.write(longToBytes(logEntry.getTerm())); output.write(logEntry.getValueType().toByte()); output.write(intToBytes(logEntry.getValue().length)); output.write(logEntry.getValue()); output.flush(); return output.toByteArray(); }catch(IOException exception){ LogManager.getLogger("BinaryUtil").error("failed to serialize LogEntry to memory", exception); throw new RuntimeException("Running into bad situation, where memory may not be sufficient", exception); } }
@Override public long append(LogEntry logEntry) { try{ this.storeWriteLock.lock(); this.indexFile.seek(this.indexFile.length()); long dataFileLength = this.dataFile.length(); this.indexFile.writeLong(dataFileLength); this.dataFile.seek(dataFileLength); ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES + 1 + logEntry.getValue().length); buffer.putLong(logEntry.getTerm()); buffer.put(logEntry.getValueType().toByte()); buffer.put(logEntry.getValue()); this.dataFile.write(buffer.array()); this.entriesInStore += 1; this.buffer.append(logEntry); return this.entriesInStore + this.startIndex - 1; }catch(IOException exception){ this.logger.error("failed to append a log entry to store", exception); throw new RuntimeException(exception.getMessage(), exception); }finally{ this.storeWriteLock.unlock(); } }
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES + 1 + logEntry.getValue().length); buffer.putLong(logEntry.getTerm()); buffer.put(logEntry.getValueType().toByte()); buffer.put(logEntry.getValue());
currentCommitIndex += 1; LogEntry logEntry = server.logStore.getLogEntryAt(currentCommitIndex); if(logEntry.getValueType() == LogValueType.Application){ server.stateMachine.commit(currentCommitIndex, logEntry.getValue()); }else if(logEntry.getValueType() == LogValueType.Configuration){ synchronized(server){ ClusterConfiguration newConfig = ClusterConfiguration.fromBytes(logEntry.getValue());
currentCommitIndex += 1; LogEntry logEntry = server.logStore.getLogEntryAt(currentCommitIndex); if(logEntry.getValueType() == LogValueType.Application){ server.stateMachine.commit(currentCommitIndex, logEntry.getValue()); }else if(logEntry.getValueType() == LogValueType.Configuration){ synchronized(server){ ClusterConfiguration newConfig = ClusterConfiguration.fromBytes(logEntry.getValue());
response.setNextIndex(this.logStore.getFirstAvailableIndex()); response.setAccepted(false); if(logEntries.length != 1 || logEntries[0].getValueType() != LogValueType.ClusterServer){ this.logger.info("bad add server request as we are expecting one log entry with value type of ClusterServer"); return response;
response.setNextIndex(this.logStore.getFirstAvailableIndex()); response.setAccepted(false); if(logEntries.length != 1 || logEntries[0].getValueType() != LogValueType.ClusterServer){ this.logger.info("bad add server request as we are expecting one log entry with value type of ClusterServer"); return response;
response.setNextIndex(this.logStore.getFirstAvailableIndex()); response.setAccepted(false); if(logEntries.length != 1 || logEntries[0].getValueType() != LogValueType.ClusterServer){ this.logger.info("bad add server request as we are expecting one log entry with value type of ClusterServer"); return response;
private RaftResponseMessage handleLogSyncRequest(RaftRequestMessage request){ LogEntry[] logEntries = request.getLogEntries(); RaftResponseMessage response = new RaftResponseMessage(); response.setSource(this.id); response.setDestination(request.getSource()); response.setTerm(this.state.getTerm()); response.setMessageType(RaftMessageType.SyncLogResponse); response.setNextIndex(this.logStore.getFirstAvailableIndex()); response.setAccepted(false); if(logEntries == null || logEntries.length != 1 || logEntries[0].getValueType() != LogValueType.LogPack || logEntries[0].getValue() == null || logEntries[0].getValue().length == 0){ this.logger.info("receive an invalid LogSyncRequest as the log entry value doesn't meet the requirements"); return response; } if(!this.catchingUp){ this.logger.debug("This server is ready for cluster, ignore the request"); return response; } this.logStore.applyLogPack(request.getLastLogIndex() + 1, logEntries[0].getValue()); this.commit(this.logStore.getFirstAvailableIndex() -1); response.setNextIndex(this.logStore.getFirstAvailableIndex()); response.setAccepted(true); return response; }
private RaftResponseMessage handleLogSyncRequest(RaftRequestMessage request){ LogEntry[] logEntries = request.getLogEntries(); RaftResponseMessage response = new RaftResponseMessage(); response.setSource(this.id); response.setDestination(request.getSource()); response.setTerm(this.state.getTerm()); response.setMessageType(RaftMessageType.SyncLogResponse); response.setNextIndex(this.logStore.getFirstAvailableIndex()); response.setAccepted(false); if(logEntries == null || logEntries.length != 1 || logEntries[0].getValueType() != LogValueType.LogPack || logEntries[0].getValue() == null || logEntries[0].getValue().length == 0){ this.logger.info("receive an invalid LogSyncRequest as the log entry value doesn't meet the requirements"); return response; } if(!this.catchingUp){ this.logger.debug("This server is ready for cluster, ignore the request"); return response; } this.logStore.applyLogPack(request.getLastLogIndex() + 1, logEntries[0].getValue()); this.commit(this.logStore.getFirstAvailableIndex() -1); response.setNextIndex(this.logStore.getFirstAvailableIndex()); response.setAccepted(true); return response; }
private RaftResponseMessage handleLogSyncRequest(RaftRequestMessage request){ LogEntry[] logEntries = request.getLogEntries(); RaftResponseMessage response = new RaftResponseMessage(); response.setSource(this.id); response.setDestination(request.getSource()); response.setTerm(this.state.getTerm()); response.setMessageType(RaftMessageType.SyncLogResponse); response.setNextIndex(this.logStore.getFirstAvailableIndex()); response.setAccepted(false); if(logEntries == null || logEntries.length != 1 || logEntries[0].getValueType() != LogValueType.LogPack || logEntries[0].getValue() == null || logEntries[0].getValue().length == 0){ this.logger.info("receive an invalid LogSyncRequest as the log entry value doesn't meet the requirements"); return response; } if(!this.catchingUp){ this.logger.debug("This server is ready for cluster, ignore the request"); return response; } this.logStore.applyLogPack(request.getLastLogIndex() + 1, logEntries[0].getValue()); this.commit(this.logStore.getFirstAvailableIndex() -1); response.setNextIndex(this.logStore.getFirstAvailableIndex()); response.setAccepted(true); return response; }
if(logEntries == null || logEntries.length != 1 || logEntries[0].getValueType() != LogValueType.Configuration || logEntries[0].getValue() == null || logEntries[0].getValue().length == 0){
if(logEntries == null || logEntries.length != 1 || logEntries[0].getValueType() != LogValueType.Configuration || logEntries[0].getValue() == null || logEntries[0].getValue().length == 0){
if(logEntries == null || logEntries.length != 1 || logEntries[0].getValueType() != LogValueType.Configuration || logEntries[0].getValue() == null || logEntries[0].getValue().length == 0){