StateT
- The type of the object whose updates are being synchronized.public interface StateSynchronizer<StateT extends Revisioned>
extends java.lang.AutoCloseable
RevisionedStreamClient
.
The pattern is to have an object of type StateT that can be updated by objects of type UpdateT.
Each host can perform logic based on its current StateT object and apply updates by supplying a
function to create UpdateT objects. Updates from other hosts can be obtained by calling
fetchUpdates()
The applying of updates can be conditional on the state that was provided to their generator being the most recent revision, and retrying if it is not. This provides a strong consistency through optimistic concurrency.
As with any optimistic concurrency system, this works best when optimism is justified: i.e. The odds are good another host is not updating the state at the exact same time.
Because they are held in memory and transmitted over the network, state objects are updates should be relatively compact. Implementations might explicitly enforce size limits.
Modifier and Type | Interface and Description |
---|---|
static interface |
StateSynchronizer.UpdateGenerator<StateT extends Revisioned>
A function which given a state object populates a list of updates that should be applied.
|
static interface |
StateSynchronizer.UpdateGeneratorFunction<StateT extends Revisioned,ReturnT>
Similar to
StateSynchronizer.UpdateGenerator but it also returns a result for the caller. |
Modifier and Type | Method and Description |
---|---|
long |
bytesWrittenSinceCompaction()
Calculates the number of bytes that have been written since the state has last been compacted by calling
compact(Function)
This may be useful when calculating when a compaction should occur. |
void |
close()
Closes the StateSynchronizer and frees any resources associated with it.
|
void |
compact(java.util.function.Function<StateT,InitialUpdate<StateT>> compactor)
Provide a function that generates compacted version of localState so that we can drop some of the
history updates.
|
void |
fetchUpdates()
Fetch and apply all updates needed to the state object held locally up to date.
|
StateT |
getState()
Gets the state object currently held in memory.
|
void |
initialize(InitialUpdate<StateT> initial)
This method can be used to provide an initial value for a new stream if the stream has not
been previously initialized.
|
void |
updateState(StateSynchronizer.UpdateGenerator<StateT> updateGenerator)
Creates a new update for the latest state object and applies it atomically.
|
<ReturnT> ReturnT |
updateState(StateSynchronizer.UpdateGeneratorFunction<StateT,ReturnT> updateGenerator)
Similar to
updateState(UpdateGenerator) but this version returns a result object
supplied by the StateSynchronizer.UpdateGeneratorFunction . |
void |
updateStateUnconditionally(java.util.List<? extends Update<StateT>> update)
Same as
updateStateUnconditionally(Update) , except it persists multiple updates at
the same time so they will not be interleaved with other updates. |
void |
updateStateUnconditionally(Update<StateT> update)
Persists the provided update.
|
StateT getState()
void fetchUpdates()
void updateState(StateSynchronizer.UpdateGenerator<StateT> updateGenerator)
updateGenerator
- A function that given the current state can supply updates that should
be applied.<ReturnT> ReturnT updateState(StateSynchronizer.UpdateGeneratorFunction<StateT,ReturnT> updateGenerator)
updateState(UpdateGenerator)
but this version returns a result object
supplied by the StateSynchronizer.UpdateGeneratorFunction
. This is useful if the calling code wishes to
do something in response to the update.
As an example suppose the update type was MyUpdate and each update and an associated key.
Then it might be useful to return the updated keys:
List updated = stateSynchronizer.updateState((state, updates) -> {
List toAdd = findUpdatesForState(state);
updates.addAll(toAdd);
return toAdd.stream().map(a -> a.getKey()).collect(Collectors.toList());
});
ReturnT
- They type of the result returned by the updateGeneratorupdateGenerator
- A function which give the state can supply updates that should be
applied.void updateStateUnconditionally(Update<StateT> update)
fetchUpdates()
is called.update
- The update that all other processes should receive.void updateStateUnconditionally(java.util.List<? extends Update<StateT>> update)
updateStateUnconditionally(Update)
, except it persists multiple updates at
the same time so they will not be interleaved with other updates.update
- The updates that all other processes should receive.void initialize(InitialUpdate<StateT> initial)
fetchUpdates()
initial
- The initializer for the statelong bytesWrittenSinceCompaction()
compact(Function)
This may be useful when calculating when a compaction should occur.compact(Function)
void compact(java.util.function.Function<StateT,InitialUpdate<StateT>> compactor)
NOTE: If InitialUpdate returned does not generate local state exactly corruption will occur.
compactor
- An generator of InitialUpdates given a state.void close()
close
in interface java.lang.AutoCloseable
AutoCloseable.close()