Package io.pravega.client.state.impl
Class StateSynchronizerImpl<StateT extends Revisioned>
- java.lang.Object
-
- io.pravega.client.state.impl.StateSynchronizerImpl<StateT>
-
- All Implemented Interfaces:
StateSynchronizer<StateT>
,java.lang.AutoCloseable
public class StateSynchronizerImpl<StateT extends Revisioned> extends java.lang.Object implements StateSynchronizer<StateT>
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface io.pravega.client.state.StateSynchronizer
StateSynchronizer.UpdateGenerator<StateT extends Revisioned>, StateSynchronizer.UpdateGeneratorFunction<StateT extends Revisioned,ReturnT>
-
-
Constructor Summary
Constructors Constructor Description StateSynchronizerImpl(Segment segment, RevisionedStreamClient<UpdateOrInit<StateT>> client)
Creates a new instance of StateSynchronizer class.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description long
bytesWrittenSinceCompaction()
Calculates the number of bytes that have been written since the state has last been compacted by callingStateSynchronizer.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.java.lang.String
toString()
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 toStateSynchronizer.updateState(UpdateGenerator)
but this version returns a result object supplied by theStateSynchronizer.UpdateGeneratorFunction
.void
updateStateUnconditionally(Update<StateT> update)
Persists the provided update.void
updateStateUnconditionally(java.util.List<? extends Update<StateT>> update)
Same asStateSynchronizer.updateStateUnconditionally(Update)
, except it persists multiple updates at the same time so they will not be interleaved with other updates.
-
-
-
Constructor Detail
-
StateSynchronizerImpl
public StateSynchronizerImpl(Segment segment, RevisionedStreamClient<UpdateOrInit<StateT>> client)
Creates a new instance of StateSynchronizer class.- Parameters:
segment
- The segment.client
- The revisioned stream client this state synchronizer builds upon."
-
-
Method Detail
-
getState
public StateT getState()
Description copied from interface:StateSynchronizer
Gets the state object currently held in memory. This is a non-blocking call.- Specified by:
getState
in interfaceStateSynchronizer<StateT extends Revisioned>
- Returns:
- Revisioned state object
-
fetchUpdates
public void fetchUpdates()
Description copied from interface:StateSynchronizer
Fetch and apply all updates needed to the state object held locally up to date.- Specified by:
fetchUpdates
in interfaceStateSynchronizer<StateT extends Revisioned>
-
updateState
public void updateState(StateSynchronizer.UpdateGenerator<StateT> updateGenerator)
Description copied from interface:StateSynchronizer
Creates a new update for the latest state object and applies it atomically. The UpdateGenerator provided will be passed the latest state object and a list which it can populate with any updates that need to be applied. These updates are recorded and applied conditionally on the state object that was passed to the function being up to date. If another process was applying an update in parallel, the state is updated and updateGenerator will be called again with the new state object so that it may generate new updates. (Which may be different from the one it previously generated) By re-creating the updates in this way, consistency is guaranteed. When this function returns the generated updates will have been applied to the local state.- Specified by:
updateState
in interfaceStateSynchronizer<StateT extends Revisioned>
- Parameters:
updateGenerator
- A function that given the current state can supply updates that should be applied.
-
updateState
public <ReturnT> ReturnT updateState(StateSynchronizer.UpdateGeneratorFunction<StateT,ReturnT> updateGenerator)
Description copied from interface:StateSynchronizer
Similar toStateSynchronizer.updateState(UpdateGenerator)
but this version returns a result object supplied by theStateSynchronizer.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<String> updated = stateSynchronizer.updateState((state, updates) -> { List<MyUpdate> toAdd = findUpdatesForState(state); updates.addAll(toAdd); return toAdd.stream().map(a -> a.getKey()).collect(Collectors.toList()); });
- Specified by:
updateState
in interfaceStateSynchronizer<StateT extends Revisioned>
- Type Parameters:
ReturnT
- They type of the result returned by the updateGenerator- Parameters:
updateGenerator
- A function which give the state can supply updates that should be applied.- Returns:
- the result returned by the updateGenerator.
-
updateStateUnconditionally
public void updateStateUnconditionally(Update<StateT> update)
Description copied from interface:StateSynchronizer
Persists the provided update. To ensure consistent ordering of updates across hosts the update is not applied locally untilStateSynchronizer.fetchUpdates()
is called.- Specified by:
updateStateUnconditionally
in interfaceStateSynchronizer<StateT extends Revisioned>
- Parameters:
update
- The update that all other processes should receive.
-
updateStateUnconditionally
public void updateStateUnconditionally(java.util.List<? extends Update<StateT>> update)
Description copied from interface:StateSynchronizer
Same asStateSynchronizer.updateStateUnconditionally(Update)
, except it persists multiple updates at the same time so they will not be interleaved with other updates.- Specified by:
updateStateUnconditionally
in interfaceStateSynchronizer<StateT extends Revisioned>
- Parameters:
update
- The updates that all other processes should receive.
-
initialize
public void initialize(InitialUpdate<StateT> initial)
Description copied from interface:StateSynchronizer
This method can be used to provide an initial value for a new stream if the stream has not been previously initialized. If the stream was already initialized nothing will be changed, and the local state will be updated as thoughStateSynchronizer.fetchUpdates()
- Specified by:
initialize
in interfaceStateSynchronizer<StateT extends Revisioned>
- Parameters:
initial
- The initializer for the state
-
bytesWrittenSinceCompaction
public long bytesWrittenSinceCompaction()
Description copied from interface:StateSynchronizer
Calculates the number of bytes that have been written since the state has last been compacted by callingStateSynchronizer.compact(Function)
This may be useful when calculating when a compaction should occur.- Specified by:
bytesWrittenSinceCompaction
in interfaceStateSynchronizer<StateT extends Revisioned>
- Returns:
- The number of bytes written since the last call to
StateSynchronizer.compact(Function)
-
compact
public void compact(java.util.function.Function<StateT,InitialUpdate<StateT>> compactor)
Description copied from interface:StateSynchronizer
Provide a function that generates compacted version of localState so that we can drop some of the history updates.NOTE: If InitialUpdate returned does not generate local state exactly corruption will occur.
- Specified by:
compact
in interfaceStateSynchronizer<StateT extends Revisioned>
- Parameters:
compactor
- An generator of InitialUpdates given a state.
-
close
public void close()
Description copied from interface:StateSynchronizer
Closes the StateSynchronizer and frees any resources associated with it. (It may no longer be used)- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfaceStateSynchronizer<StateT extends Revisioned>
- See Also:
AutoCloseable.close()
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-