Cleanup comments and process structure in OctreePersistThread

This commit is contained in:
Ryan Huffman 2018-06-08 16:00:31 -07:00
parent d0af06939f
commit 9afc9d19c5
3 changed files with 22 additions and 44 deletions

View file

@ -1053,7 +1053,7 @@ void OctreeServer::readConfiguration() {
int result { -1 }; int result { -1 };
readOptionInt(QString("persistInterval"), settingsSectionObject, result); readOptionInt(QString("persistInterval"), settingsSectionObject, result);
if (result != -1) { if (result != -1) {
_persistInterval = std::chrono::seconds(result); _persistInterval = std::chrono::milliseconds(result);
} }
qDebug() << "persistInterval=" << _persistInterval.count(); qDebug() << "persistInterval=" << _persistInterval.count();

View file

@ -36,7 +36,8 @@
#include "OctreeUtils.h" #include "OctreeUtils.h"
#include "OctreeDataUtils.h" #include "OctreeDataUtils.h"
const std::chrono::seconds OctreePersistThread::DEFAULT_PERSIST_INTERVAL { 30 }; constexpr std::chrono::seconds OctreePersistThread::DEFAULT_PERSIST_INTERVAL { 30 };
constexpr std::chrono::milliseconds TIME_BETWEEN_PROCESSING { 10 };
constexpr int MAX_OCTREE_REPLACEMENT_BACKUP_FILES_COUNT { 20 }; constexpr int MAX_OCTREE_REPLACEMENT_BACKUP_FILES_COUNT { 20 };
constexpr size_t MAX_OCTREE_REPLACEMENT_BACKUP_FILES_SIZE_BYTES { 50 * 1000 * 1000 }; constexpr size_t MAX_OCTREE_REPLACEMENT_BACKUP_FILES_SIZE_BYTES { 50 * 1000 * 1000 };
@ -48,7 +49,7 @@ OctreePersistThread::OctreePersistThread(OctreePointer tree, const QString& file
_persistInterval(persistInterval), _persistInterval(persistInterval),
_initialLoadComplete(false), _initialLoadComplete(false),
_loadTimeUSecs(0), _loadTimeUSecs(0),
_lastCheck(0), _lastPersistCheck(std::chrono::steady_clock::now()),
_debugTimestampNow(debugTimestampNow), _debugTimestampNow(debugTimestampNow),
_lastTimeDebug(0), _lastTimeDebug(0),
_persistAsFileType(persistAsFileType) _persistAsFileType(persistAsFileType)
@ -95,7 +96,7 @@ void OctreePersistThread::start() {
packet->writePrimitive(false); packet->writePrimitive(false);
} }
qCDebug(octree) << "Sending request for octree data to DS"; qCDebug(octree) << "Sending OctreeDataFileRequest to DS";
nodeList->sendPacket(std::move(packet), domainHandler.getSockAddr()); nodeList->sendPacket(std::move(packet), domainHandler.getSockAddr());
} }
@ -111,9 +112,9 @@ void OctreePersistThread::handleOctreeDataFileReply(QSharedPointer<ReceivedMessa
if (includesNewData) { if (includesNewData) {
replacementData = message->readAll(); replacementData = message->readAll();
replaceData(replacementData); replaceData(replacementData);
qDebug() << "Got reply to octree data file request, new data sent"; qDebug() << "Got OctreeDataFileReply, new data sent";
} else { } else {
qDebug() << "Got reply to octree data file request, current entity data is sufficient"; qDebug() << "Got OctreeDataFileReply, current entity data is sufficient";
OctreeUtils::RawEntityData data; OctreeUtils::RawEntityData data;
qCDebug(octree) << "Reading octree data from" << _filename; qCDebug(octree) << "Reading octree data from" << _filename;
@ -178,16 +179,14 @@ void OctreePersistThread::handleOctreeDataFileReply(QSharedPointer<ReceivedMessa
_initialLoadComplete = true; _initialLoadComplete = true;
// Since we just loaded the persistent file, we can consider ourselves as having "just checked" for persistance. // Since we just loaded the persistent file, we can consider ourselves as having just persist
_lastCheck = usecTimestampNow(); // we just loaded, no need to save again _lastPersistCheck = std::chrono::steady_clock::now();
if (replacementData.isNull()) { if (replacementData.isNull()) {
sendLatestEntityDataToDS(); sendLatestEntityDataToDS();
} }
qDebug() << "Starting timer"; QTimer::singleShot(TIME_BETWEEN_PROCESSING.count(), this, &OctreePersistThread::process);
QTimer::singleShot(std::chrono::milliseconds(_persistInterval).count(), this, &OctreePersistThread::process);
QTimer::singleShot(std::chrono::milliseconds(1000).count(), this, &OctreePersistThread::process);
emit loadCompleted(); emit loadCompleted();
} }
@ -233,41 +232,21 @@ bool OctreePersistThread::backupCurrentFile() {
return true; return true;
} }
bool OctreePersistThread::process() { void OctreePersistThread::process() {
qDebug() << "Processing..."; auto startedProcessingAt = std::chrono::steady_clock::now();
if (true) { //isStillRunning()) { _tree->update();
quint64 MSECS_TO_USECS = 1000;
quint64 USECS_TO_SLEEP = 10 * MSECS_TO_USECS; // every 10ms
std::this_thread::sleep_for(std::chrono::microseconds(USECS_TO_SLEEP));
// do our updates then check to save... auto now = std::chrono::steady_clock::now();
_tree->update(); auto timeSinceLastPersist = now - _lastPersistCheck;
quint64 now = usecTimestampNow(); qDebug() << "Seconds since last persist: " << std::chrono::duration_cast<std::chrono::seconds>(timeSinceLastPersist).count();
quint64 sinceLastSave = now - _lastCheck; if (timeSinceLastPersist > _persistInterval) {
quint64 intervalToCheck = std::chrono::microseconds(_persistInterval).count(); _lastPersistCheck = now;
persist();
if (sinceLastSave > intervalToCheck) {
_lastCheck = now;
persist();
}
} }
// if we were asked to debugTimestampNow do that now...
if (_debugTimestampNow) {
quint64 now = usecTimestampNow();
quint64 sinceLastDebug = now - _lastTimeDebug;
quint64 DEBUG_TIMESTAMP_INTERVAL = 600000000; // every 10 minutes
if (sinceLastDebug > DEBUG_TIMESTAMP_INTERVAL) { QTimer::singleShot(TIME_BETWEEN_PROCESSING.count(), this, &OctreePersistThread::process);
_lastTimeDebug = usecTimestampNow(true); // ask for debug output
}
}
//return isStillRunning(); // keep running till they terminate us
QTimer::singleShot(1000, this, &OctreePersistThread::process);
return true;
} }
void OctreePersistThread::aboutToFinish() { void OctreePersistThread::aboutToFinish() {

View file

@ -54,7 +54,7 @@ signals:
void loadCompleted(); void loadCompleted();
protected slots: protected slots:
bool process(); void process();
void handleOctreeDataFileReply(QSharedPointer<ReceivedMessage> message); void handleOctreeDataFileReply(QSharedPointer<ReceivedMessage> message);
protected: protected:
@ -71,12 +71,11 @@ private:
OctreePointer _tree; OctreePointer _tree;
QString _filename; QString _filename;
std::chrono::milliseconds _persistInterval; std::chrono::milliseconds _persistInterval;
std::chrono::steady_clock::time_point _lastPersistCheck;
bool _initialLoadComplete; bool _initialLoadComplete;
quint64 _loadTimeUSecs; quint64 _loadTimeUSecs;
quint64 _lastCheck;
bool _debugTimestampNow; bool _debugTimestampNow;
quint64 _lastTimeDebug; quint64 _lastTimeDebug;