public class KeyValueStateMachine extends RaftStateMachine
| Modifier and Type | Class and Description |
|---|---|
static interface |
KeyValueStateMachine.ChangeListener
This holds a locally registered callback that will be called whenever the state machine changes.
|
| Modifier and Type | Field and Description |
|---|---|
java.util.Optional<java.lang.String> |
serverName
The server that this state machine is running on.
|
| Constructor and Description |
|---|
KeyValueStateMachine(java.lang.String serverName)
Create a new state machine, with knowledge of what node it's running on
|
| Modifier and Type | Method and Description |
|---|---|
void |
addChangeListener(KeyValueStateMachine.ChangeListener changeListener)
This registers a listener that will be called whenever the key-value store changes.
|
protected void |
addErrorListener(RaftErrorListener errorListener)
Keeps track of an additional
RaftErrorListener in this class |
void |
applyTransition(byte[] transition,
long now,
java.util.concurrent.ExecutorService pool)
This is responsible for applying a transition to the state machine.
|
void |
applyTransition(KeyValueStateMachineProto.Transition serializedTransition,
long now,
java.util.concurrent.ExecutorService pool) |
protected void |
clearErrorListeners()
Clears all the
RaftErrorListeners attached to this class. |
static byte[] |
createClearTransition(java.lang.String owner)
This creates a serialized ClearTransient transition.
|
static byte[] |
createGroupedTransition(byte[]... transitions)
Creates a grouped transition, which executes several transitions atomically.
|
static byte[] |
createReleaseLockTransition(java.lang.String lock,
java.lang.String requester,
java.lang.String uniqueHash)
This creates a serialized ReleaseLock transition.
|
static byte[] |
createRemoveValueTransition(java.lang.String key)
This creates a serialized RemoveValue transition that will delete an entry in the map, if it's currently present.
|
static byte[] |
createRequestLockTransition(java.lang.String lock,
java.lang.String requester,
java.lang.String uniqueHash)
This creates a serialized RequestLock transition.
|
static byte[] |
createSetValueTransition(java.lang.String key,
byte[] value)
This creates a serialized SetValue transition that will set an entry in the values map.
|
static byte[] |
createSetValueTransitionWithOwner(java.lang.String key,
byte[] value,
java.lang.String owner)
This creates a serialized SetValue transition that will set an entry in the values map, with an "owner" who is
responsible for the value, which will be automatically cleaned up when the owner disconnects from the cluster.
|
static byte[] |
createTryLockTransition(java.lang.String lock,
java.lang.String requester,
java.lang.String uniqueHash)
This creates a serialized TryLock transition.
|
java.lang.String |
debugTransition(byte[] transition)
This is used for debugging log entries.
|
java.util.Collection<java.util.Map.Entry<java.lang.String,ai.eloquent.raft.KeyValueStateMachine.ValueWithOptionalOwner>> |
entries()
This returns the full list of entries in the state machine.
|
boolean |
equals(java.lang.Object o) |
java.util.Optional<byte[]> |
get(java.lang.String key,
long now)
This gets a value from the values map, if it's present.
|
int |
hashCode() |
java.util.Collection<java.lang.String> |
keys()
This gets the current set of keys in the state machine.
|
java.util.Set<java.lang.String> |
keysIdleSince(java.time.Duration idleTime,
long now)
Returns entries which have not been modified in at least |age| amount of time.
|
java.util.Set<java.lang.String> |
keysPresentSince(java.time.Duration timeInRaft,
long now)
Returns entries which have not been modified in at least |age| amount of time.
|
java.util.Map<java.lang.String,byte[]> |
map()
This returns a copy of the key->value map in the state machine.
|
void |
overwriteWithSerializedImpl(byte[] serialized,
long now,
java.util.concurrent.ExecutorService pool)
This overwrites the current state of the state machine with a serialized proto.
|
java.util.Set<java.lang.String> |
owners()
The set of nodes that own anything in the state machine.
|
void |
removeChangeListener(KeyValueStateMachine.ChangeListener changeListener)
This removes a listener that will be called whenever the key-value store changes.
|
protected void |
removeErrorListener(RaftErrorListener errorListener)
Stop listening from a specific
RaftErrorListener |
com.google.protobuf.ByteString |
serializeImpl()
This serializes the state machine's current state into a proto that can be read from
RaftStateMachine.overwriteWithSerialized(byte[], long, ExecutorService). |
protected void |
throwRaftError(java.lang.String incidentKey,
java.lang.String debugMessage)
Alert each of the
RaftErrorListeners attached to this class. |
getHospice, overwriteWithSerialized, serializepublic final java.util.Optional<java.lang.String> serverName
RaftState.serverNamepublic KeyValueStateMachine(java.lang.String serverName)
protected void addErrorListener(RaftErrorListener errorListener)
RaftErrorListener in this classerrorListener - The error listener to add.protected void removeErrorListener(RaftErrorListener errorListener)
RaftErrorListenererrorListener - The error listener to be removedprotected void clearErrorListeners()
RaftErrorListeners attached to this class.protected void throwRaftError(java.lang.String incidentKey,
java.lang.String debugMessage)
RaftErrorListeners attached to this class.public com.google.protobuf.ByteString serializeImpl()
RaftStateMachine.overwriteWithSerialized(byte[], long, ExecutorService).serializeImpl in class RaftStateMachinepublic void overwriteWithSerializedImpl(byte[] serialized,
long now,
java.util.concurrent.ExecutorService pool)
overwriteWithSerializedImpl in class RaftStateMachineserialized - the state machine to overwrite this one with, in serialized formnow - the current time, for mocking.pool - an executor pool to run lock future commits on.public void applyTransition(byte[] transition,
long now,
java.util.concurrent.ExecutorService pool)
applyTransition in class RaftStateMachinetransition - the transition to apply, in serialized formnow - the current timestamppool - the pool to run any listeners onpublic void applyTransition(KeyValueStateMachineProto.Transition serializedTransition, long now, java.util.concurrent.ExecutorService pool)
public void addChangeListener(KeyValueStateMachine.ChangeListener changeListener)
changeListener - the listener to registerpublic void removeChangeListener(KeyValueStateMachine.ChangeListener changeListener)
changeListener - the listener to deregisterpublic java.util.Optional<byte[]> get(java.lang.String key,
long now)
key - The key to retrieve.now - The current time, so that we can mock transport time if appropriate.public java.util.Collection<java.lang.String> keys()
public java.util.Map<java.lang.String,byte[]> map()
public java.util.Set<java.lang.String> keysIdleSince(java.time.Duration idleTime,
long now)
idleTime - The amount of time an entry must have been idle in the state machine.now - The current time.public java.util.Set<java.lang.String> keysPresentSince(java.time.Duration timeInRaft,
long now)
timeInRaft - The amount of time an entry must have been in the state machine.now - The current time.public java.util.Collection<java.util.Map.Entry<java.lang.String,ai.eloquent.raft.KeyValueStateMachine.ValueWithOptionalOwner>> entries()
ConcurrentHashMap.public java.util.Set<java.lang.String> owners()
owners in class RaftStateMachinepublic java.lang.String debugTransition(byte[] transition)
debugTransition in class RaftStateMachinetransition - the transition to debugpublic boolean equals(java.lang.Object o)
equals in class java.lang.Objectpublic int hashCode()
hashCode in class java.lang.Objectpublic static byte[] createRequestLockTransition(java.lang.String lock,
java.lang.String requester,
java.lang.String uniqueHash)
lock - the lock namerequester - the requester of the lockuniqueHash - the unique hash to deduplicate requests on the same machinepublic static byte[] createTryLockTransition(java.lang.String lock,
java.lang.String requester,
java.lang.String uniqueHash)
lock - the lock namerequester - the requester of the lockuniqueHash - the unique hash to deduplicate requests on the same machinepublic static byte[] createReleaseLockTransition(java.lang.String lock,
java.lang.String requester,
java.lang.String uniqueHash)
lock - the lock namerequester - the requester of the lockuniqueHash - the unique hash to deduplicate requests on the same machinepublic static byte[] createGroupedTransition(byte[]... transitions)
transitions - the transitions (serialized) to grouppublic static byte[] createSetValueTransitionWithOwner(java.lang.String key,
byte[] value,
java.lang.String owner)
key - the key to setvalue - the value, as a raw byte arrayowner - the owner of this key-value pairpublic static byte[] createSetValueTransition(java.lang.String key,
byte[] value)
key - the key to setvalue - the value, as a raw byte arraypublic static byte[] createRemoveValueTransition(java.lang.String key)
key - the key to removepublic static byte[] createClearTransition(java.lang.String owner)
owner - the owner we should clear transient values for.