public abstract class ResourceStateMachine extends StateMachine implements SessionListener
Resource implementations should extend the resource state machine for support in Atomix core. This state machine implements functionality necessary to multiplexing the state machine on a single Copycat log and provides facilities for configuring and cleaning up the resource upon deletion.
To implement a resource state machine, simply extend this class. State machines must adhere
to the same rules as those outlined in the Copycat documentation. See the StateMachine
documentation for more information.
public class MapStateMachine extends ResourceStateMachine {
private final Map<Object, Object> map = new HashMap<>();
public void put(Commit<PutCommand> commit) {
try {
map.put(commit.operation().key(), commit.operation().value());
} finally {
commit.close();
}
}
}
Resource state machines should override the delete() method to perform cleanup of
the resource state upon deletion of the resource. Cleanup tasks might include notifying clients
of the deletion of the resource or releasing commits held by the state machine.
public class MapStateMachine extends ResourceStateMachine {
private final Map<Object, Commit<PutCommand>> map = new HashMap<>();
public void put(Commit<PutCommand> commit) {
map.put(commit.operation().key(), commit);
}
public void delete() {
for (Map.Entry<Object, Commit<PutCommand>> entry : map.entrySet()) {
entry.getValue().close();
}
map.clear();
}
}
Resource state machines implement Copycat's SessionListener interface to allow state
machines to listen for changes in client sessions. When used in an Atomix cluster, sessions
are specific to a state machine and not the entire cluster. When a client or replica opens
a resource, a new logical session to that resource is opened and the session listener's
SessionListener.register(ServerSession) method will be called. When a client or replica
closes a resource, the SessionListener.close(ServerSession) method will be called.
In other words, sessions in resource state machines represent a client's open resource instance,
and event messages published to that session will
be received by the specific client side Resource instance.
public class MapStateMachine extends ResourceStateMachine {
private final Map<Object, Object> map = new HashMap<>();
private final Set<ServerSession> listeners = new HashSet<>();
public void listen(Commit<ListenCommand> commit) {
listeners.add(commit.session());
}
public void put(Commit<PutCommand> commit) {
map.put(commit.operation().key(), commit.operation().value());
for (ServerSession listener : listeners) {
listener.publish(commit.operation().key(), commit.operation().value());
}
}
public void close(ServerSession session) {
listeners.remove(session);
}
}
| Modifier and Type | Method and Description |
|---|---|
void |
close(ServerSession session) |
void |
delete()
Cleans up deleted state machine state.
|
void |
expire(ServerSession session) |
void |
init(StateMachineExecutor executor) |
void |
register(ServerSession session) |
void |
unregister(ServerSession session) |
closepublic final void init(StateMachineExecutor executor)
init in class StateMachinepublic void register(ServerSession session)
register in interface SessionListenerpublic void unregister(ServerSession session)
unregister in interface SessionListenerpublic void expire(ServerSession session)
expire in interface SessionListenerpublic void close(ServerSession session)
close in interface SessionListenerpublic void delete()
This method is called when a client deletes a resource in the cluster.
This method is guaranteed to eventually be called on all servers at the same point in logical time
(i.e. index). State machines should override this method to clean up state machine state,
including releasing Commits held by the state machine.
public class MapStateMachine extends ResourceStateMachine {
private final Map<Object, Commit<PutCommand>> map = new HashMap<>();
public void put(Commit<PutCommand> commit) {
map.put(commit.operation().key(), commit);
}
public void delete() {
for (Map.Entry<Object, Commit<PutCommand>> entry : map.entrySet()) {
entry.getValue().close();
}
map.clear();
}
}
Failing to override this method to clean up commits held by a resource state machine is considered
a bug and will eventually result in disk filling up.Copyright © 2013–2016. All rights reserved.