DetectorGraph
2.0
|
Basic Counter Graph with persistent storage. More...
Go to the source code of this file.
Basic Counter Graph with persistent storage.
This examples cover the most basic way to preserve state across Graph instances.
This covers how to resume state and a neat way to define initial state.
The example uses a minimal Graph with a single Detector and two topics. The Detector counts EventHappened
and publishes the total in EventCount
.
This example shows how GraphStateStore and StateSnapshot can be used in conjunction with ResumeFromSnapshotTopicState to provide an extensible, robust and transparent state persistence mechanism.
The diagram below shows the life cycle of StateSnapshots during boot.
This life cycle can be seen in the main
of this example:
PrimeSnapshot
is a StateSnapshot that contains the initial state/data that should always be used - regardless of file-based state persistence. In this example we synthesize PrimeSnapshot
this way:
ResumeSnapshot
is the aggregation of TopicStates contained in PrimeSnapshot
and what is deserialized from storage in ReadSnapshot()
:
ResumeSnapshot
is then used to construct a ResumeFromSnapshotTopicState
which is then posted to the graph to allow Detectors to resume/initialize their state.
From then on mStateStore
in ResumingGraph
is continually updated from within ResumingGraph::ProcessOutput()
:
The up-to-date latestSnapshot
can then be flushed to disk as necessary with WriteSnapshot()
:
WriteSnapshot()
for each new processed input. Some applications may choose to only call WriteSnapshot()
during shutdown. A more sophisticated approach is to call WriteSnapshot()
only when a TopicState
in a set of critical ones changes. This can be done by iterating through the list in mGraph.GetOutputList()
as it only contains the changed TopicStates
.Running the program produces:
$ ./resuminggraph.out DetectorGraph: Graph Initialized EventCount = 1001 EventCount = 1002 EventCount = 1003 EventCount = 1004 EventCount = 1005 EventCount = 1006 EventCount = 1007 $ ./resuminggraph.out DetectorGraph: Graph Initialized EventCount = 1008 EventCount = 1009 EventCount = 1010 EventCount = 1011 EventCount = 1012 EventCount = 1013 EventCount = 1014 $ cat resumingGraphSnapshot.txt 1014
Definition in file resuminggraph.cpp.