Class CriticalDataService
- All Implemented Interfaces:
CtxEventListener
,ContextHandleAware
,HandleAware
,Ready
,ReadyListener
,BoardLinkListener
,HardwareListener
Critical data is encoded as json and compressed with a CRC32 header for actual
storage. This allows arbitrary json data to be stored in critical data while
still attempting to minimize the space required. The CRC32 also allows junk
data to be detected and flagged as invalid. Individual CriticalDataSource
implementations are expected to know the length of critical data and return
the correct number of bytes. A typical solution for this is to write the length
of the array before writing the array itself.
Critical data is namespaced and is effectively a map with the key being the name of the critical data to operate on. This allows multiple services to share critical data without requiring a global format for all data.
Care should be taken to minimize the size of json data where possible. Critical data is often a very limited resource, sometimes providing as little as 100 bytes of storage depending on the hardware backing the source. All critical data is stored on every source so the upper limit for data is the smallest source capacity.
As critical data is inherently distributed across different hardware, it can take
some time for critical data to become available, if it becomes available at all.
The criticalDataReady
indicator can be used to determine when critical
data becomes ready and even get a callback when ready. The
CriticalDataListener
interface provides additional events such as when
critical data is changed or if critical data is unavailable.
- Version:
- 2024-06-24
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addSource
(CriticalDataSource source) Add a new source to the service.<T> T
getCriticalData
(String name, Class<T> clazz) Return the named critical data as an instance of the specified class.Return theReadyIndicator
that can be used to detect if critical data is already ready, or can generate a callback when critical data is ready for use.boolean
Return true if the critical data is ready for use.boolean
Override in subclasses to be notified when all dependencies are ready.void
onBoardLink
(Board board) Called when a board is linked.void
onBoardUnlink
(Board board) Called when a board is unlinked.void
ready
(CriticalDataSource source) Called when aCriticalDataSource
becomes ready for use.void
removeCriticalData
(String name) Remove the named critical data and write back to the source.void
removeSource
(CriticalDataSource source) Remove a source from the service.void
setAllCriticalData
(JsonNode json) Set all of critical data with the specified json data.void
setCriticalData
(String name, Object data, Class<?> view) Set the new critical data and write back to all the sources.void
Update all the sources with critical data if needed.Methods inherited from class com.tccc.kos.commons.core.service.AbstractService
getHandle, getHandlePrefix
Methods inherited from class com.tccc.kos.commons.util.ready.ReadyBean
getReady, onDependenciesReady
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.tccc.kos.commons.core.context.CtxEventListener
onCtxAutowiringCompleted, onCtxDestroyed, onCtxPhaseCompleted
Methods inherited from interface com.tccc.kos.commons.core.service.handle.HandleAware
addHandleChild, getName, getPath
Methods inherited from interface com.tccc.kos.commons.util.ready.ReadyListener
onDependenciesGroupReady
-
Field Details
-
ALL_DATA
- See Also:
-
-
Constructor Details
-
CriticalDataService
public CriticalDataService()
-
-
Method Details
-
onBeanReady
public boolean onBeanReady()Description copied from class:ReadyBean
Override in subclasses to be notified when all dependencies are ready. Return true to indicate that this bean is also ready. If the bean needs to callsetReady()
later due to some async processing, return false to preventsetReady()
from being called automatically.It is recommended to override this method rather than
onDependenciesReady()
as a majority of the time it requires callingsuper.onDependneciesReady()
or explicitly callingsetReady()
which defaults to a failure mode.- Overrides:
onBeanReady
in classReadyBean
- Returns:
-
getCriticalDataReady
Return theReadyIndicator
that can be used to detect if critical data is already ready, or can generate a callback when critical data is ready for use. -
isCriticalDataReady
public boolean isCriticalDataReady()Return true if the critical data is ready for use. This is simply a wrapper forgetCriticalDataReady().isReady()
.- Returns:
- true if critical data is ready for use
-
getCriticalData
Return the named critical data as an instance of the specified class. This will deserialize the named section from json format back to the specified class.- Parameters:
name
- the name of the data to return- Returns:
- the named critical data
- Throws:
CriticalDataException
- if critical data not available or can't be parsed
-
setCriticalData
Set the new critical data and write back to all the sources.- Parameters:
name
- the name of the data to setdata
- the new critical data (null will remove the name entirely)view
- optional view to json conversion- Throws:
CriticalDataException
- if critical data not available or can't be set
-
removeCriticalData
Remove the named critical data and write back to the source.- Parameters:
name
- the name of the data to remove
-
setAllCriticalData
Set all of critical data with the specified json data. This is typically used during testing or manufacturing to set critical data when none already exists. This will replace the current critical data if any exists.- Parameters:
json
- json representation of all critical data- Throws:
CriticalDataException
- if critical data can't be set
-
updateAllSourcesIfNeeded
public void updateAllSourcesIfNeeded()Update all the sources with critical data if needed. -
ready
Called when aCriticalDataSource
becomes ready for use. Normally a source will be checked when it is added, but if not ready at that point, this should be called to indicate that it is ready and should be checked again. Even if critical data has been selected, this will force the data to be replicated to the source. -
addSource
Add a new source to the service. This only needs to be called if the source is not in aBeanContext
which will cause it to be autowired. Calling this for a source that is already added will do nothing.- Parameters:
source
- the new source
-
removeSource
Remove a source from the service. This only needs to be called if the source is not in aBeanContext
as it will be automatically removed when the context is destroyed.- Parameters:
source
- the source to remove
-
onBoardLink
Description copied from interface:BoardLinkListener
Called when a board is linked.- Specified by:
onBoardLink
in interfaceBoardLinkListener
- Parameters:
board
- the board that was linked
-
onBoardUnlink
Description copied from interface:BoardLinkListener
Called when a board is unlinked.- Specified by:
onBoardUnlink
in interfaceBoardLinkListener
- Parameters:
board
- the board that was unlinked
-