Skip to content

Commit 3f06de9

Browse files
committed
[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 155d2f8 commit 3f06de9

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
@@ -47,6 +47,13 @@ namespace api
4747
//! are that a lot of memory is being used by the temporary
4848
//! copy of the data to be persisted.
4949
//!
50+
//! Persistence happens in a background thread and further
51+
//! persistence requests via the startBackgroundPersist*() methods
52+
//! will be rejected if the background thread is executing.
53+
//! However, calls to startBackgroundPersist() and
54+
//! startBackgroundPersistIfAppropriate() are not thread safe and
55+
//! must not be made concurrently.
56+
//!
5057
//! IMPLEMENTATION DECISIONS:\n
5158
//! This class expects to call a persistence function taking
5259
//! just the data adder as an argument. It's easy to wrap up
@@ -107,9 +114,15 @@ class API_EXPORT CBackgroundPersister : private core::CNonCopyable
107114
//! called.
108115
bool firstProcessorPeriodicPersistFunc(const TFirstProcessorPeriodicPersistFunc &firstProcessorPeriodicPersistFunc);
109116

110-
//! Check whether a background persist is appropriate now, and if it is
111-
//! then start it by calling the first processor periodic persist
112-
//! function.
117+
//! Start a background persist is one is not running.
118+
//! Calls the first processor periodic persist function first.
119+
//! Concurrent calls to this method are not threadsafe.
120+
bool startBackgroundPersist(void);
121+
122+
//! If the periodic persist interval has passed since the last persist
123+
//! then it is appropriate to persist now. Start it by calling the
124+
//! first processor periodic persist function.
125+
//! Concurrent calls to this method are not threadsafe.
113126
bool startBackgroundPersistIfAppropriate(void);
114127

115128
private:
@@ -130,6 +143,10 @@ class API_EXPORT CBackgroundPersister : private core::CNonCopyable
130143
};
131144

132145
private:
146+
//! Persist in the background setting the last persist time
147+
//! to timeOfPersistence
148+
bool startBackgroundPersist(core_t::TTime timeOfPersistence);
149+
133150
//! When this function is called a background persistence will be
134151
//! triggered unless there is already one in progress.
135152
bool startPersist(void);

lib/api/CAnomalyJob.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,14 @@ bool CAnomalyJob::handleControlMessage(const std::string &controlMessage)
397397
case 'p':
398398
this->doForecast(controlMessage);
399399
break;
400+
case 'w':
401+
{
402+
if (m_PeriodicPersister != nullptr)
403+
{
404+
m_PeriodicPersister->startBackgroundPersist();
405+
}
406+
}
407+
break;
400408
default:
401409
LOG_WARN("Ignoring unknown control message of length " <<
402410
controlMessage.length() << " beginning with '" <<

lib/api/CBackgroundPersister.cc

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,17 @@ bool CBackgroundPersister::firstProcessorPeriodicPersistFunc(const TFirstProcess
178178
return true;
179179
}
180180

181+
bool CBackgroundPersister::startBackgroundPersist(void)
182+
{
183+
if (this->isBusy())
184+
{
185+
LOG_WARN("Cannot start background persist as a previous "
186+
"persist is still in progress");
187+
return false;
188+
}
189+
return this->startBackgroundPersist(core::CTimeUtils::now());
190+
}
191+
181192
bool CBackgroundPersister::startBackgroundPersistIfAppropriate(void)
182193
{
183194
core_t::TTime due(m_LastPeriodicPersistTime + m_PeriodicPersistInterval);
@@ -201,6 +212,11 @@ bool CBackgroundPersister::startBackgroundPersistIfAppropriate(void)
201212
return false;
202213
}
203214

215+
return this->startBackgroundPersist(now);
216+
}
217+
218+
bool CBackgroundPersister::startBackgroundPersist(core_t::TTime timeOfPersistence)
219+
{
204220
bool backgroundPersistSetupOk = m_FirstProcessorPeriodicPersistFunc(*this);
205221
if (!backgroundPersistSetupOk)
206222
{
@@ -211,7 +227,7 @@ bool CBackgroundPersister::startBackgroundPersistIfAppropriate(void)
211227
return false;
212228
}
213229

214-
m_LastPeriodicPersistTime = now;
230+
m_LastPeriodicPersistTime = timeOfPersistence;
215231

216232
LOG_INFO("Background persist starting background thread");
217233

0 commit comments

Comments
 (0)