Ref T285, Ref T292 improved background updater

* use atomic members (precaution)
* refactoring
This commit is contained in:
Klaus Basan
2018-07-27 23:31:29 +02:00
parent a8a36591f8
commit 106ef6709e
2 changed files with 42 additions and 24 deletions

View File

@@ -54,10 +54,13 @@ namespace BlackCore
void CBackgroundDataUpdater::doWork()
{
if (!this->doWorkCheck()) { return; }
if (m_inWork) { return; }
m_inWork = true;
emit this->consolidating(true);
const int cycle = m_cycle;
int cycle = m_cycle;
this->addHistory(CLogMessage(this).info("Background consolidation cycle %1") << cycle);
switch (cycle)
{
case 0:
@@ -75,18 +78,19 @@ namespace BlackCore
this->syncDbEntity(CEntityFlags::AircraftIcaoEntity);
break;
case 2:
this->addHistory(CLogMessage(this).info("Synchronize %1") << m_modelCaches.getDescription());
this->syncModelOrModelSetCacheWithDbData(m_modelCaches);
this->addHistory(CLogMessage(this).info("Synchronize %1") << this->modelCaches(true).getDescription());
this->syncModelOrModelSetCacheWithDbData(true); // set
break;
case 3:
this->addHistory(CLogMessage(this).info("Synchronize %1") << m_modelSetCaches.getDescription());
this->syncModelOrModelSetCacheWithDbData(m_modelSetCaches);
this->addHistory(CLogMessage(this).info("Synchronize %1") << this->modelCaches(false).getDescription());
this->syncModelOrModelSetCacheWithDbData(false);
break;
default:
break;
}
++m_cycle %= 4;
++cycle %= 4;
m_cycle = cycle;
m_inWork = false;
emit this->consolidating(false);
}
@@ -98,20 +102,21 @@ namespace BlackCore
sApp->getWebDataServices()->triggerReadOfSharedInfoObjects();
}
void CBackgroundDataUpdater::syncModelOrModelSetCacheWithDbData(IMultiSimulatorModelCaches &cache, const CAircraftModelList &dbModelsConsidered)
void CBackgroundDataUpdater::syncModelOrModelSetCacheWithDbData(bool modelSetFlag, const CAircraftModelList &dbModelsConsidered)
{
if (!this->doWorkCheck()) { return; }
IMultiSimulatorModelCaches &modelCaches = this->modelCaches(modelSetFlag);
const QDateTime latestDbModelsTs = dbModelsConsidered.isEmpty() ?
sApp->getWebDataServices()->getCacheTimestamp(CEntityFlags::ModelEntity) :
dbModelsConsidered.latestTimestamp();
if (!latestDbModelsTs.isValid()) { return; }
// newer DB models as cache
const QDateTime dbModelsLatestSync = m_syncedModelsLatestChange.value(cache.getDescription());
const QDateTime dbModelsLatestSync = m_syncedModelsLatestChange.value(modelCaches.getDescription());
if (dbModelsLatestSync.isValid() && latestDbModelsTs <= dbModelsLatestSync) { return; }
m_syncedModelsLatestChange[cache.getDescription()] = latestDbModelsTs;
const CSimulatorInfo simulators = cache.simulatorsWithInitializedCache(); // simulators ever used
m_syncedModelsLatestChange[modelCaches.getDescription()] = latestDbModelsTs;
const CSimulatorInfo simulators = modelCaches.simulatorsWithInitializedCache(); // simulators ever used
if (simulators.isNoSimulator()) { return; }
const CAircraftModelList dbModels = dbModelsConsidered.isEmpty() ?
@@ -122,7 +127,7 @@ namespace BlackCore
for (const CSimulatorInfo &singleSimulator : simulatorsSet)
{
if (!this->doWorkCheck()) { return; }
CAircraftModelList simulatorModels = cache.getSynchronizedCachedModels(singleSimulator);
CAircraftModelList simulatorModels = modelCaches.getSynchronizedCachedModels(singleSimulator);
if (simulatorModels.isEmpty()) { continue; }
const CAircraftModelList dbModelsForSimulator = dbModels.matchesSimulator(singleSimulator);
if (dbModelsForSimulator.isEmpty()) { continue; }
@@ -132,7 +137,7 @@ namespace BlackCore
if (c > 0)
{
this->addHistory(CLogMessage(this).info("Consolidated %1 models for '%2'") << c << singleSimulator.convertToQString());
const CStatusMessage m = cache.setCachedModels(simulatorModels, singleSimulator);
const CStatusMessage m = modelCaches.setCachedModels(simulatorModels, singleSimulator);
CLogMessage::preformatted(m);
this->addHistory(m);
}
@@ -184,13 +189,20 @@ namespace BlackCore
if (modelsPublished.isEmpty()) { return; }
if (!m_updatePublishedModels) { return; }
if (!directWrite) { return; } // those models are CRs and have to be released first
if (m_inWork) { return; } // no 2 updates at the same time
emit this->consolidating(true);
this->syncModelOrModelSetCacheWithDbData(m_modelCaches, modelsPublished);
this->syncModelOrModelSetCacheWithDbData(m_modelSetCaches, modelsPublished);
this->syncModelOrModelSetCacheWithDbData(true, modelsPublished);
this->syncModelOrModelSetCacheWithDbData(false, modelsPublished);
emit this->consolidating(false);
}
IMultiSimulatorModelCaches &CBackgroundDataUpdater::modelCaches(bool modelSetFlag)
{
if (modelSetFlag) { return m_modelSets; }
return m_modelCaches;
}
void CBackgroundDataUpdater::addHistory(const CStatusMessage &msg)
{
QWriteLocker l(&m_lockMsg);

View File

@@ -14,12 +14,14 @@
#include "blackcore/data/dbcaches.h"
#include "blackcore/blackcoreexport.h"
#include "blackmisc/simulation/data/modelcaches.h"
#include "blackmisc/network/entityflags.h"
#include "blackmisc/simulation/data/modelcaches.h"
#include "blackmisc/statusmessagelist.h"
#include "blackmisc/worker.h"
#include <QMap>
#include <QReadWriteLock>
#include <atomic>
namespace BlackCore
{
@@ -46,16 +48,17 @@ namespace BlackCore
void consolidating(bool running);
private:
int m_cycle = 0; //!< cycle
bool m_inWork = false; //!< indicates a running update
bool m_updatePublishedModels = true; //!< update when models have been updated
mutable QReadWriteLock m_lockMsg; //!< lock snapshot
BlackMisc::Simulation::Data::CModelCaches m_modelCaches { false, this }; //!< caches
BlackMisc::Simulation::Data::CModelSetCaches m_modelSetCaches { false, this }; //!< caches
std::atomic_int m_cycle { 0 }; //!< cycle
std::atomic_bool m_inWork { false }; //!< indicates a running update
std::atomic_bool m_updatePublishedModels { true }; //!< update when models have been updated
QMap<QString, QDateTime> m_syncedModelsLatestChange; //! timestamp per cache when last synced
BlackMisc::CStatusMessageList m_messageHistory;
// set/caches as member as we are in own thread, central instance will not work
BlackMisc::Simulation::Data::CModelCaches m_modelCaches { false, this };
BlackMisc::Simulation::Data::CModelSetCaches m_modelSets { false, this };
//! Do the update checks
void doWork();
@@ -63,9 +66,9 @@ namespace BlackCore
void triggerInfoReads();
//! Sync the model cache, normally model set or simulator models cache
//! \remark if no DB models are passed all DB models are used
void syncModelOrModelSetCacheWithDbData(BlackMisc::Simulation::Data::IMultiSimulatorModelCaches &cache,
const BlackMisc::Simulation::CAircraftModelList &dbModelsConsidered = {});
//! \param modelSetFlag true means model set, false means model cach
//! \param dbModelsConsidered if no DB models are passed all DB models are used
void syncModelOrModelSetCacheWithDbData(bool modelSetFlag, const BlackMisc::Simulation::CAircraftModelList &dbModelsConsidered = {});
//! Sync DB entity
void syncDbEntity(BlackMisc::Network::CEntityFlags::Entity entity);
@@ -76,6 +79,9 @@ namespace BlackCore
//! Models have been published
void onModelsPublished(const BlackMisc::Simulation::CAircraftModelList &modelsPublished, bool directWrite);
//! Model or model set instance
BlackMisc::Simulation::Data::IMultiSimulatorModelCaches &modelCaches(bool modelSetFlag);
//! Add history message
//! \threadsafe
void addHistory(const BlackMisc::CStatusMessage &msg);