java.lang.Object
org.jgroups.raft.util.CounterStateMachine
- All Implemented Interfaces:
StateMachine
Sample state machine accepting additions and subtractions
- Since:
- 1.0.5
- Author:
- Bela Ban
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final AtomicIntegerprotected final AtomicIntegerprotected final AtomicInteger -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintbyte[]apply(byte[] data, int offset, int length, boolean serialize_response) Applies a command to the state machine.intcounter()static StringvoidReads the contents of the state machine from an input stream.static Stringreset()inttoString()voidwriteContentTo(DataOutput out) Writes the contents of the state machine to an output stream.
-
Field Details
-
counter
-
additions
-
subtractions
-
-
Constructor Details
-
CounterStateMachine
public CounterStateMachine()
-
-
Method Details
-
counter
public int counter() -
additions
public int additions() -
subtractions
public int subtractions() -
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
-
readAndDumpSnapshot
-
reader
-
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
-
reset
-
toString
-