Skip to content

Commit 34fef02

Browse files
authored
[ML] Add control message to start background persistence (#19)
* Add control message for back ground persist * Add isBusy check to public startBackgroundPersist function * Document thread safety aspects
1 parent b9991eb commit 34fef02

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

include/api/CBackgroundPersister.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ namespace api
3838
//! are that a lot of memory is being used by the temporary
3939
//! copy of the data to be persisted.
4040
//!
41+
//! Persistence happens in a background thread and further
42+
//! persistence requests via the startBackgroundPersist*() methods
43+
//! will be rejected if the background thread is executing.
44+
//! However, calls to startBackgroundPersist() and
45+
//! startBackgroundPersistIfAppropriate() are not thread safe and
46+
//! must not be made concurrently.
47+
//!
4148
//! IMPLEMENTATION DECISIONS:\n
4249
//! This class expects to call a persistence function taking
4350
//! just the data adder as an argument. It's easy to wrap up
@@ -98,9 +105,15 @@ class API_EXPORT CBackgroundPersister : private core::CNonCopyable
98105
//! called.
99106
bool firstProcessorPeriodicPersistFunc(const TFirstProcessorPeriodicPersistFunc &firstProcessorPeriodicPersistFunc);
100107

101-
//! Check whether a background persist is appropriate now, and if it is
102-
//! then start it by calling the first processor periodic persist
103-
//! function.
108+
//! Start a background persist is one is not running.
109+
//! Calls the first processor periodic persist function first.
110+
//! Concurrent calls to this method are not threadsafe.
111+
bool startBackgroundPersist(void);
112+
113+
//! If the periodic persist interval has passed since the last persist
114+
//! then it is appropriate to persist now. Start it by calling the
115+
//! first processor periodic persist function.
116+
//! Concurrent calls to this method are not threadsafe.
104117
bool startBackgroundPersistIfAppropriate(void);
105118

106119
private:
@@ -121,6 +134,10 @@ class API_EXPORT CBackgroundPersister : private core::CNonCopyable
121134
};
122135

123136
private:
137+
//! Persist in the background setting the last persist time
138+
//! to timeOfPersistence
139+
bool startBackgroundPersist(core_t::TTime timeOfPersistence);
140+
124141
//! When this function is called a background persistence will be
125142
//! triggered unless there is already one in progress.
126143
bool startPersist(void);

lib/api/CAnomalyJob.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,14 @@ bool CAnomalyJob::handleControlMessage(const std::string &controlMessage)
388388
case 'p':
389389
this->doForecast(controlMessage);
390390
break;
391+
case 'w':
392+
{
393+
if (m_PeriodicPersister != nullptr)
394+
{
395+
m_PeriodicPersister->startBackgroundPersist();
396+
}
397+
}
398+
break;
391399
default:
392400
LOG_WARN("Ignoring unknown control message of length " <<
393401
controlMessage.length() << " beginning with '" <<

lib/api/CBackgroundPersister.cc

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,17 @@ bool CBackgroundPersister::firstProcessorPeriodicPersistFunc(const TFirstProcess
169169
return true;
170170
}
171171

172+
bool CBackgroundPersister::startBackgroundPersist(void)
173+
{
174+
if (this->isBusy())
175+
{
176+
LOG_WARN("Cannot start background persist as a previous "
177+
"persist is still in progress");
178+
return false;
179+
}
180+
return this->startBackgroundPersist(core::CTimeUtils::now());
181+
}
182+
172183
bool CBackgroundPersister::startBackgroundPersistIfAppropriate(void)
173184
{
174185
core_t::TTime due(m_LastPeriodicPersistTime + m_PeriodicPersistInterval);
@@ -192,6 +203,11 @@ bool CBackgroundPersister::startBackgroundPersistIfAppropriate(void)
192203
return false;
193204
}
194205

206+
return this->startBackgroundPersist(now);
207+
}
208+
209+
bool CBackgroundPersister::startBackgroundPersist(core_t::TTime timeOfPersistence)
210+
{
195211
bool backgroundPersistSetupOk = m_FirstProcessorPeriodicPersistFunc(*this);
196212
if (!backgroundPersistSetupOk)
197213
{
@@ -202,7 +218,7 @@ bool CBackgroundPersister::startBackgroundPersistIfAppropriate(void)
202218
return false;
203219
}
204220

205-
m_LastPeriodicPersistTime = now;
221+
m_LastPeriodicPersistTime = timeOfPersistence;
206222

207223
LOG_INFO("Background persist starting background thread");
208224

0 commit comments

Comments
 (0)