public byte[] toBytes(){ byte[] configData = this.snapshot.getLastConfig().toBytes(); int size = Long.BYTES * 3 + configData.length + Integer.BYTES * 2 + data.length + 1; ByteBuffer buffer = ByteBuffer.allocate(size); buffer.putLong(snapshot.getLastLogIndex()); buffer.putLong(snapshot.getLastLogTerm()); buffer.putInt(configData.length); buffer.put(configData); buffer.putLong(this.offset); buffer.putInt(this.data.length); buffer.put(this.data); buffer.put(this.done ? (byte)1 : (byte)0); return buffer.array(); }
public byte[] toBytes(){ byte[] configData = this.snapshot.getLastConfig().toBytes(); int size = Long.BYTES * 3 + configData.length + Integer.BYTES * 2 + data.length + 1; ByteBuffer buffer = ByteBuffer.allocate(size); buffer.putLong(snapshot.getLastLogIndex()); buffer.putLong(snapshot.getLastLogTerm()); buffer.putInt(configData.length); buffer.put(configData); buffer.putLong(this.offset); buffer.putInt(this.data.length); buffer.put(this.data); buffer.put(this.done ? (byte)1 : (byte)0); return buffer.array(); }
public byte[] toBytes(){ byte[] configData = this.snapshot.getLastConfig().toBytes(); int size = Long.BYTES * 3 + configData.length + Integer.BYTES * 2 + data.length + 1; ByteBuffer buffer = ByteBuffer.allocate(size); buffer.putLong(snapshot.getLastLogIndex()); buffer.putLong(snapshot.getLastLogTerm()); buffer.putInt(configData.length); buffer.put(configData); buffer.putLong(this.offset); buffer.putInt(this.data.length); buffer.put(this.data); buffer.put(this.done ? (byte)1 : (byte)0); return buffer.array(); }
@Override public void saveSnapshotData(Snapshot snapshot, long offset, byte[] data) { Path filePath = this.snapshotStore.resolve(String.format("%d-%d.s", snapshot.getLastLogIndex(), snapshot.getLastLogTerm())); try{ if(!Files.exists(filePath)){ Files.write(this.snapshotStore.resolve(String.format("%d.cnf", snapshot.getLastLogIndex())), snapshot.getLastConfig().toBytes(), StandardOpenOption.CREATE); } RandomAccessFile snapshotFile = new RandomAccessFile(filePath.toString(), "rw"); snapshotFile.seek(offset); snapshotFile.write(data); snapshotFile.close(); }catch(Exception error){ throw new RuntimeException(error.getMessage()); } }
@Override public void saveSnapshotData(Snapshot snapshot, long offset, byte[] data) { Path filePath = this.snapshotStore.resolve(String.format("%d-%d.s", snapshot.getLastLogIndex(), snapshot.getLastLogTerm())); try{ if(!Files.exists(filePath)){ Files.write(this.snapshotStore.resolve(String.format("%d.cnf", snapshot.getLastLogIndex())), snapshot.getLastConfig().toBytes(), StandardOpenOption.CREATE); } RandomAccessFile snapshotFile = new RandomAccessFile(filePath.toString(), "rw"); snapshotFile.seek(offset); snapshotFile.write(data); snapshotFile.close(); }catch(Exception error){ throw new RuntimeException(error.getMessage()); } }
try{ if(!Files.exists(filePath)){ Files.write(this.snapshotStore.resolve(String.format("%d.cnf", snapshot.getLastLogIndex())), snapshot.getLastConfig().toBytes(), StandardOpenOption.CREATE);
try{ if(!Files.exists(filePath)){ Files.write(this.snapshotStore.resolve(String.format("%d.cnf", snapshot.getLastLogIndex())), snapshot.getLastConfig().toBytes(), StandardOpenOption.CREATE);
private void removeServerFromCluster(int serverId){ ClusterConfiguration newConfig = new ClusterConfiguration(); newConfig.setLastLogIndex(this.config.getLogIndex()); newConfig.setLogIndex(this.logStore.getFirstAvailableIndex()); for(ClusterServer server: this.config.getServers()){ if(server.getId() != serverId){ newConfig.getServers().add(server); } } this.logger.info("removed a server from configuration and save the configuration to log store at %d", newConfig.getLogIndex()); this.configChanging = true; this.logStore.append(new LogEntry(this.state.getTerm(), newConfig.toBytes(), LogValueType.Configuration)); this.requestAppendEntries(); }
private void removeServerFromCluster(int serverId){ ClusterConfiguration newConfig = new ClusterConfiguration(); newConfig.setLastLogIndex(this.config.getLogIndex()); newConfig.setLogIndex(this.logStore.getFirstAvailableIndex()); for(ClusterServer server: this.config.getServers()){ if(server.getId() != serverId){ newConfig.getServers().add(server); } } this.logger.info("removed a server from configuration and save the configuration to log store at %d", newConfig.getLogIndex()); this.configChanging = true; this.logStore.append(new LogEntry(this.state.getTerm(), newConfig.toBytes(), LogValueType.Configuration)); this.requestAppendEntries(); }
private void removeServerFromCluster(int serverId){ ClusterConfiguration newConfig = new ClusterConfiguration(); newConfig.setLastLogIndex(this.config.getLogIndex()); newConfig.setLogIndex(this.logStore.getFirstAvailableIndex()); for(ClusterServer server: this.config.getServers()){ if(server.getId() != serverId){ newConfig.getServers().add(server); } } this.logger.info("removed a server from configuration and save the configuration to log store at %d", newConfig.getLogIndex()); this.configChanging = true; this.logStore.append(new LogEntry(this.state.getTerm(), newConfig.toBytes(), LogValueType.Configuration)); this.requestAppendEntries(); }
private void becomeLeader(){ this.stopElectionTimer(); this.role = ServerRole.Leader; this.leader = this.id; this.serverToJoin = null; for(PeerServer server : this.peers.values()){ server.setNextLogIndex(this.logStore.getFirstAvailableIndex()); server.setSnapshotInSync(null); server.setFree(); this.enableHeartbeatForPeer(server); } // if current config is not committed, try to commit it if(this.config.getLogIndex() == 0){ this.config.setLogIndex(this.logStore.getFirstAvailableIndex()); this.logStore.append(new LogEntry(this.state.getTerm(), this.config.toBytes(), LogValueType.Configuration)); this.logger.info("add initial configuration to log store"); this.configChanging = true; } this.requestAppendEntries(); }
private void becomeLeader(){ this.stopElectionTimer(); this.role = ServerRole.Leader; this.leader = this.id; this.serverToJoin = null; for(PeerServer server : this.peers.values()){ server.setNextLogIndex(this.logStore.getFirstAvailableIndex()); server.setSnapshotInSync(null); server.setFree(); this.enableHeartbeatForPeer(server); } // if current config is not committed, try to commit it if(this.config.getLogIndex() == 0){ this.config.setLogIndex(this.logStore.getFirstAvailableIndex()); this.logStore.append(new LogEntry(this.state.getTerm(), this.config.toBytes(), LogValueType.Configuration)); this.logger.info("add initial configuration to log store"); this.configChanging = true; } this.requestAppendEntries(); }
private void becomeLeader(){ this.stopElectionTimer(); this.role = ServerRole.Leader; this.leader = this.id; this.serverToJoin = null; for(PeerServer server : this.peers.values()){ server.setNextLogIndex(this.logStore.getFirstAvailableIndex()); server.setSnapshotInSync(null); server.setFree(); this.enableHeartbeatForPeer(server); } // if current config is not committed, try to commit it if(this.config.getLogIndex() == 0){ this.config.setLogIndex(this.logStore.getFirstAvailableIndex()); this.logStore.append(new LogEntry(this.state.getTerm(), this.config.toBytes(), LogValueType.Configuration)); this.logger.info("add initial configuration to log store"); this.configChanging = true; } this.requestAppendEntries(); }
private void inviteServerToJoinCluster(){ RaftRequestMessage request = new RaftRequestMessage(); request.setCommitIndex(this.quickCommitIndex); request.setDestination(this.serverToJoin.getId()); request.setSource(this.id); request.setTerm(this.state.getTerm()); request.setMessageType(RaftMessageType.JoinClusterRequest); request.setLastLogIndex(this.logStore.getFirstAvailableIndex() - 1); request.setLogEntries(new LogEntry[] { new LogEntry(this.state.getTerm(), this.config.toBytes(), LogValueType.Configuration) }); this.serverToJoin.SendRequest(request).whenCompleteAsync((RaftResponseMessage response, Throwable error) -> { this.handleExtendedResponse(response, error); }, this.context.getScheduledExecutor()); }
private void inviteServerToJoinCluster(){ RaftRequestMessage request = new RaftRequestMessage(); request.setCommitIndex(this.quickCommitIndex); request.setDestination(this.serverToJoin.getId()); request.setSource(this.id); request.setTerm(this.state.getTerm()); request.setMessageType(RaftMessageType.JoinClusterRequest); request.setLastLogIndex(this.logStore.getFirstAvailableIndex() - 1); request.setLogEntries(new LogEntry[] { new LogEntry(this.state.getTerm(), this.config.toBytes(), LogValueType.Configuration) }); this.serverToJoin.SendRequest(request).whenCompleteAsync((RaftResponseMessage response, Throwable error) -> { this.handleExtendedResponse(response, error); }, this.context.getScheduledExecutor()); }
private void inviteServerToJoinCluster(){ RaftRequestMessage request = new RaftRequestMessage(); request.setCommitIndex(this.quickCommitIndex); request.setDestination(this.serverToJoin.getId()); request.setSource(this.id); request.setTerm(this.state.getTerm()); request.setMessageType(RaftMessageType.JoinClusterRequest); request.setLastLogIndex(this.logStore.getFirstAvailableIndex() - 1); request.setLogEntries(new LogEntry[] { new LogEntry(this.state.getTerm(), this.config.toBytes(), LogValueType.Configuration) }); this.serverToJoin.SendRequest(request).whenCompleteAsync((RaftResponseMessage response, Throwable error) -> { this.handleExtendedResponse(response, error); }, this.context.getScheduledExecutor()); }
newConfig.getServers().addAll(this.config.getServers()); newConfig.getServers().add(this.serverToJoin.getClusterConfig()); LogEntry configEntry = new LogEntry(this.state.getTerm(), newConfig.toBytes(), LogValueType.Configuration); this.logStore.append(configEntry); this.configChanging = true;
newConfig.getServers().addAll(this.config.getServers()); newConfig.getServers().add(this.serverToJoin.getClusterConfig()); LogEntry configEntry = new LogEntry(this.state.getTerm(), newConfig.toBytes(), LogValueType.Configuration); this.logStore.append(configEntry); this.configChanging = true;
newConfig.getServers().addAll(this.config.getServers()); newConfig.getServers().add(this.serverToJoin.getClusterConfig()); LogEntry configEntry = new LogEntry(this.state.getTerm(), newConfig.toBytes(), LogValueType.Configuration); this.logStore.append(configEntry); this.configChanging = true;