- All Implemented Interfaces:
Settable
Provides methods to make changes, register a state machine,
get commit-index and last-applied, register RAFT.RoleChange listeners etc.
JChannel ch=createChannel();
RaftHandle handle=new RaftHandle(ch, new StateMachineImpl()); // implements StateMachine
handle.addRoleListener(this);
handle.setAsync(buf, 0, buf.length).whenComplete((buf,ex) -> ...);
- Since:
- 0.2
- Author:
- Bela Ban
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionRaftHandle(org.jgroups.JChannel ch, StateMachine sm) Creates a RaftHandle instance. -
Method Summary
Modifier and TypeMethodDescriptionaddRoleListener(RAFT.RoleChange listener) CompletableFuture<byte[]> Asynchronously adds a new server to the RAFT cluster.org.jgroups.JChannelchannel()longlongCompletableFuture<byte[]> Asynchronous get operation that returns immediately without blocking.booleanisLeader()longorg.jgroups.Addressleader()log()voidlogEntries(ObjLongConsumer<LogEntry> func) longlogSize()raft()raftId()removeRoleListener(RAFT.RoleChange listener) CompletableFuture<byte[]> removeServer(String server) Asynchronously removes a server from the RAFT cluster.CompletableFuture<byte[]> Asynchronous set, returns immediately with a CompletableFuture.voidsnapshot()
-
Field Details
-
ch
protected org.jgroups.JChannel ch -
raft
-
settable
-
-
Constructor Details
-
RaftHandle
Creates a RaftHandle instance.- Parameters:
ch- The channel over which to create the RaftHandle. Must be non-null, but doesn't yet need to be connectedsm- An implementation ofStateMachine. Can be null, ie. if it is set later viastateMachine(StateMachine).
-
-
Method Details
-
channel
public org.jgroups.JChannel channel() -
raft
-
raftId
-
raftId
-
leader
public org.jgroups.Address leader() -
isLeader
public boolean isLeader() -
stateMachine
-
stateMachine
-
addRoleListener
-
removeRoleListener
-
currentTerm
public long currentTerm() -
lastApplied
public long lastApplied() -
commitIndex
public long commitIndex() -
snapshot
- Throws:
Exception
-
log
-
logSize
public long logSize() -
addServer
Asynchronously adds a new server to the RAFT cluster.Only a single membership change executes at any given time. Multiple concurrent changes are chained and executed serially. The membership change adds an entry to the log, meaning it tolerates crashes and restarts and only has an effect after a majority of members commit it.
- Parameters:
server- The server RAFT ID to add.- Returns:
- A
CompletableFuturethat completes once the command is committed. - Throws:
Exception- if dynamic view changes are not enabled.IllegalStateException- if the node is not the leader.
-
removeServer
Asynchronously removes a server from the RAFT cluster.- Parameters:
server- The server RAFT ID to remove.- Returns:
- A
CompletableFuturethat completes once the command is committed. - Throws:
Exception- if dynamic view changes are not enabled.IllegalStateException- if the node is not the leader.- See Also:
-
logEntries
-
setAsync
public CompletableFuture<byte[]> setAsync(byte[] buf, int offset, int length, Options options) throws Exception Description copied from interface:SettableAsynchronous set, returns immediately with a CompletableFuture. To wait for the result,CompletableFuture.get()orCompletableFuture.get(long, TimeUnit)can be called.- Specified by:
setAsyncin interfaceSettable- Parameters:
buf- The buffer (usually a serialized command) which represent the change to be applied to all state machinesoffset- The offset into the bufferlength- he number of bytes to be used in the buffer, starting at offsetoptions- Options to pass to the call, may be null- Returns:
- A CompletableFuture which can be used to fetch the result.
- Throws:
Exception
-
getAsync
public CompletableFuture<byte[]> getAsync(byte[] buf, int offset, int length, Options options) throws Exception Description copied from interface:SettableAsynchronous get operation that returns immediately without blocking.This method submits a read-only operation to the state machine. Read-only operations are treated differently by the replication algorithm. Since read-only operations do not change the state-machine state, these operations are not appended to the replicated log.
Warning: Do not change the state-machine state by operations submitted through this method. Otherwise, the state-machine will diverge and lead to an undefined state.
- Specified by:
getAsyncin interfaceSettable- Parameters:
buf- The buffer representing the read-only operation to submit to the state machine.offset- The offset to skip the bytes in the buffer.length- The number of bytes to use from the buffer starting at offset.options- Options to pass along in the call chain, may be null.- Returns:
- A buffer representing the result after submitting the read-only operation.
- Throws:
Exception- Thrown if the operation could not be submitted.
-