- All Implemented Interfaces:
StateMachine
- Since:
- 0.1
- Author:
- Bela Ban
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanprotected org.jgroups.JChannelprotected ClassLoaderprotected static final byteprotected final List<ReplicatedStateMachine.Notification<K, V>> protected static final byteprotected RaftHandleprotected static final byteprotected long -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidvoidaddRoleChangeListener(RAFT.RoleChange listener) booleanallowDirtyReads(boolean f) byte[]apply(byte[] data, int offset, int length, boolean serialize_response) Applies a command to the state machine.org.jgroups.JChannelchannel()classLoaderInUse(ClassLoader classLoader) longdumpLog()booleanReturns the value for a given key.inthashCode()protected VlonglonglogSize()protected voidprotected voidprotected voidnotifyRemove(K key, V old_val) Adds a key value pair to the state machine.raftId()voidReads the contents of the state machine from an input stream.Removes a key-value pair from the state machine.voidvoidremoveRoleChangeListener(RAFT.RoleChange listener) intsize()Returns the number of elements in the RSMvoidsnapshot()longtimeout()timeout(long timeout) toString()useClassLoader(ClassLoader classLoader) voidwriteContentTo(DataOutput out) Writes the contents of the state machine to an output stream.
-
Field Details
-
ch
protected org.jgroups.JChannel ch -
raft
-
repl_timeout
protected long repl_timeout -
allow_dirty_reads
protected boolean allow_dirty_reads -
class_loader
-
listeners
-
map
-
PUT
protected static final byte PUT- See Also:
-
REMOVE
protected static final byte REMOVE- See Also:
-
GET
protected static final byte GET- See Also:
-
-
Constructor Details
-
ReplicatedStateMachine
public ReplicatedStateMachine(org.jgroups.JChannel ch)
-
-
Method Details
-
timeout
-
timeout
public long timeout() -
allowDirtyReads
-
allowDirtyReads
public boolean allowDirtyReads() -
addRoleChangeListener
-
addNotificationListener
-
removeNotificationListener
-
removeRoleChangeListener
-
lastApplied
public long lastApplied() -
commitIndex
public long commitIndex() -
channel
public org.jgroups.JChannel channel() -
snapshot
- Throws:
Exception
-
logSize
public long logSize() -
raftId
-
raftId
-
useClassLoader
-
classLoaderInUse
-
dumpLog
-
equals
-
hashCode
public int hashCode() -
put
Adds a key value pair to the state machine. The data is not added directly, but sent to the RAFT leader and only added to the hashmap after the change has been committed (by majority decision). The actual change will be applied with callbackStateMachine.apply(byte[], int, int, boolean).- Parameters:
key- The key to be added.val- The value to be added- Returns:
- Null, or the previous value associated with key (if present)
- Throws:
Exception
-
get
Returns the value for a given key.When
allow_dirty_readsis set, the local value is returned, possibly stale and violating linearizability. Otherwise, the request is sent through RAFT and returns a consistent value.- Parameters:
key- The key- Returns:
- The value associated with key (staleness configurable via
allow_dirty_reads) - Throws:
Exception
-
remove
Removes a key-value pair from the state machine. The data is not removed directly from the hashmap, but an update is sent via RAFT and the actual removal from the hashmap is only done when that change has been committed.- Parameters:
key- The key to be removed- Throws:
Exception
-
size
public int size()Returns the number of elements in the RSM -
apply
public byte[] apply(byte[] data, int offset, int length, boolean serialize_response) throws Exception Description copied from interface:StateMachineApplies a command to the state machine. The contents of the byte[] buffer are interpreted by the state machine. The command could for example be a set(), remove() or clear() command.- Specified by:
applyin interfaceStateMachine- Parameters:
data- The byte[] bufferoffset- The offset at which the data startslength- The length of the dataserialize_response- If true, serialize and return the response, else return null- Returns:
- A serialized response value, or null (e.g. if the method returned void)
- Throws:
Exception- Thrown on deserialization or other failure
-
readContentFrom
Description copied from interface:StateMachineReads the contents of the state machine from an input stream.This can be the case when an InstallSnapshot RPC is used to bootstrap a new node, or a node that's lagging far behind. The parsing depends on the concrete state machine implementation, but the idea is that the stream is a sequence of commands, each of which can be passed to
StateMachine.apply(byte[], int, int, boolean).The state machine may need to block modifications until the contents have been set (unless e.g. copy-on-write is used). The state machine implementation may need to remove all contents before populating itself from the stream.
- Specified by:
readContentFromin interfaceStateMachine- Parameters:
in- The input stream- Throws:
Exception
-
writeContentTo
Description copied from interface:StateMachineWrites the contents of the state machine to an output stream. This is typically called on the leader to provide state to a new node, or a node that's lagging far behind.Updates to the state machine may need to be put on hold while the state is written to the output stream.
- Specified by:
writeContentToin interfaceStateMachine- Parameters:
out- The output stream- Throws:
Exception
-
toString
-
invoke
protected V invoke(byte command, K key, V val, boolean ignore_return_value, boolean isRead) throws Exception - Throws:
Exception
-
notifyPut
-
notifyRemove
-
notifyGet
-