mirror of
https://github.com/overte-org/overte.git
synced 2025-08-10 09:08:37 +02:00
implement persist backup
This commit is contained in:
parent
11058355a0
commit
cd4b267732
4 changed files with 101 additions and 19 deletions
|
@ -1020,7 +1020,7 @@ void OctreeServer::readConfiguration() {
|
||||||
bool noPersist;
|
bool noPersist;
|
||||||
readOptionBool(QString("NoPersist"), settingsSectionObject, noPersist);
|
readOptionBool(QString("NoPersist"), settingsSectionObject, noPersist);
|
||||||
_wantPersist = !noPersist;
|
_wantPersist = !noPersist;
|
||||||
qDebug("wantPersist=%s", debug::valueOf(_wantPersist));
|
qDebug() << "wantPersist=" << _wantPersist;
|
||||||
|
|
||||||
if (_wantPersist) {
|
if (_wantPersist) {
|
||||||
QString persistFilename;
|
QString persistFilename;
|
||||||
|
@ -1029,6 +1029,26 @@ void OctreeServer::readConfiguration() {
|
||||||
}
|
}
|
||||||
strcpy(_persistFilename, qPrintable(persistFilename));
|
strcpy(_persistFilename, qPrintable(persistFilename));
|
||||||
qDebug("persistFilename=%s", _persistFilename);
|
qDebug("persistFilename=%s", _persistFilename);
|
||||||
|
|
||||||
|
_persistInterval = OctreePersistThread::DEFAULT_PERSIST_INTERVAL;
|
||||||
|
readOptionInt(QString("persistInterval"), settingsSectionObject, _persistInterval);
|
||||||
|
qDebug() << "persistInterval=" << _persistInterval;
|
||||||
|
|
||||||
|
bool noBackup;
|
||||||
|
readOptionBool(QString("NoBackup"), settingsSectionObject, noBackup);
|
||||||
|
_wantBackup = !noBackup;
|
||||||
|
qDebug() << "wantBackup=" << _wantBackup;
|
||||||
|
|
||||||
|
if (_wantBackup) {
|
||||||
|
_backupExtensionFormat = OctreePersistThread::DEFAULT_BACKUP_EXTENSION_FORMAT;
|
||||||
|
readOptionString(QString("backupExtensionFormat"), settingsSectionObject, _backupExtensionFormat);
|
||||||
|
qDebug() << "backupExtensionFormat=" << _backupExtensionFormat;
|
||||||
|
|
||||||
|
_backupInterval = OctreePersistThread::DEFAULT_BACKUP_INTERVAL;
|
||||||
|
readOptionInt(QString("backupInterval"), settingsSectionObject, _backupInterval);
|
||||||
|
qDebug() << "backupInterval=" << _backupInterval;
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
qDebug("persistFilename= DISABLED");
|
qDebug("persistFilename= DISABLED");
|
||||||
}
|
}
|
||||||
|
@ -1112,7 +1132,8 @@ void OctreeServer::run() {
|
||||||
if (_wantPersist) {
|
if (_wantPersist) {
|
||||||
|
|
||||||
// now set up PersistThread
|
// now set up PersistThread
|
||||||
_persistThread = new OctreePersistThread(_tree, _persistFilename);
|
_persistThread = new OctreePersistThread(_tree, _persistFilename, _persistInterval,
|
||||||
|
_wantBackup, _backupInterval, _backupExtensionFormat);
|
||||||
if (_persistThread) {
|
if (_persistThread) {
|
||||||
_persistThread->initialize(true);
|
_persistThread->initialize(true);
|
||||||
}
|
}
|
||||||
|
|
|
@ -167,6 +167,11 @@ protected:
|
||||||
JurisdictionSender* _jurisdictionSender;
|
JurisdictionSender* _jurisdictionSender;
|
||||||
OctreeInboundPacketProcessor* _octreeInboundPacketProcessor;
|
OctreeInboundPacketProcessor* _octreeInboundPacketProcessor;
|
||||||
OctreePersistThread* _persistThread;
|
OctreePersistThread* _persistThread;
|
||||||
|
|
||||||
|
int _persistInterval;
|
||||||
|
bool _wantBackup;
|
||||||
|
QString _backupExtensionFormat;
|
||||||
|
int _backupInterval;
|
||||||
|
|
||||||
static OctreeServer* _instance;
|
static OctreeServer* _instance;
|
||||||
|
|
||||||
|
|
|
@ -15,12 +15,23 @@
|
||||||
|
|
||||||
#include "OctreePersistThread.h"
|
#include "OctreePersistThread.h"
|
||||||
|
|
||||||
OctreePersistThread::OctreePersistThread(Octree* tree, const QString& filename, int persistInterval) :
|
const int OctreePersistThread::DEFAULT_PERSIST_INTERVAL = 1000 * 30; // every 30 seconds
|
||||||
|
const int OctreePersistThread::DEFAULT_BACKUP_INTERVAL = 1000 * 60 * 1; // every 1 minutes
|
||||||
|
const QString OctreePersistThread::DEFAULT_BACKUP_EXTENSION_FORMAT(".backup.%Y-%m-%d.%H:%M:%S.%z");
|
||||||
|
|
||||||
|
|
||||||
|
OctreePersistThread::OctreePersistThread(Octree* tree, const QString& filename, int persistInterval,
|
||||||
|
bool wantBackup, int backupInterval, const QString& backupExtensionFormat) :
|
||||||
_tree(tree),
|
_tree(tree),
|
||||||
_filename(filename),
|
_filename(filename),
|
||||||
|
_backupExtensionFormat(backupExtensionFormat),
|
||||||
_persistInterval(persistInterval),
|
_persistInterval(persistInterval),
|
||||||
|
_backupInterval(backupInterval),
|
||||||
_initialLoadComplete(false),
|
_initialLoadComplete(false),
|
||||||
_loadTimeUSecs(0)
|
_loadTimeUSecs(0),
|
||||||
|
_lastCheck(0),
|
||||||
|
_lastBackup(0),
|
||||||
|
_wantBackup(wantBackup)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,16 +62,22 @@ bool OctreePersistThread::process() {
|
||||||
unsigned long leafNodeCount = OctreeElement::getLeafNodeCount();
|
unsigned long leafNodeCount = OctreeElement::getLeafNodeCount();
|
||||||
qDebug("Nodes after loading scene %lu nodes %lu internal %lu leaves", nodeCount, internalNodeCount, leafNodeCount);
|
qDebug("Nodes after loading scene %lu nodes %lu internal %lu leaves", nodeCount, internalNodeCount, leafNodeCount);
|
||||||
|
|
||||||
double usecPerGet = (double)OctreeElement::getGetChildAtIndexTime() / (double)OctreeElement::getGetChildAtIndexCalls();
|
bool wantDebug = false;
|
||||||
qDebug() << "getChildAtIndexCalls=" << OctreeElement::getGetChildAtIndexCalls()
|
if (wantDebug) {
|
||||||
<< " getChildAtIndexTime=" << OctreeElement::getGetChildAtIndexTime() << " perGet=" << usecPerGet;
|
double usecPerGet = (double)OctreeElement::getGetChildAtIndexTime()
|
||||||
|
/ (double)OctreeElement::getGetChildAtIndexCalls();
|
||||||
|
qDebug() << "getChildAtIndexCalls=" << OctreeElement::getGetChildAtIndexCalls()
|
||||||
|
<< " getChildAtIndexTime=" << OctreeElement::getGetChildAtIndexTime() << " perGet=" << usecPerGet;
|
||||||
|
|
||||||
double usecPerSet = (double)OctreeElement::getSetChildAtIndexTime() / (double)OctreeElement::getSetChildAtIndexCalls();
|
double usecPerSet = (double)OctreeElement::getSetChildAtIndexTime()
|
||||||
qDebug() << "setChildAtIndexCalls=" << OctreeElement::getSetChildAtIndexCalls()
|
/ (double)OctreeElement::getSetChildAtIndexCalls();
|
||||||
<< " setChildAtIndexTime=" << OctreeElement::getSetChildAtIndexTime() << " perset=" << usecPerSet;
|
qDebug() << "setChildAtIndexCalls=" << OctreeElement::getSetChildAtIndexCalls()
|
||||||
|
<< " setChildAtIndexTime=" << OctreeElement::getSetChildAtIndexTime() << " perSet=" << usecPerSet;
|
||||||
|
}
|
||||||
|
|
||||||
_initialLoadComplete = true;
|
_initialLoadComplete = true;
|
||||||
_lastCheck = usecTimestampNow(); // we just loaded, no need to save again
|
_lastBackup = _lastCheck = usecTimestampNow(); // we just loaded, no need to save again
|
||||||
|
time(&_lastPersistTime);
|
||||||
|
|
||||||
emit loadCompleted();
|
emit loadCompleted();
|
||||||
}
|
}
|
||||||
|
@ -79,7 +96,7 @@ bool OctreePersistThread::process() {
|
||||||
|
|
||||||
if (sinceLastSave > intervalToCheck) {
|
if (sinceLastSave > intervalToCheck) {
|
||||||
_lastCheck = now;
|
_lastCheck = now;
|
||||||
persistOperation();
|
persist();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return isStillRunning(); // keep running till they terminate us
|
return isStillRunning(); // keep running till they terminate us
|
||||||
|
@ -88,11 +105,11 @@ bool OctreePersistThread::process() {
|
||||||
|
|
||||||
void OctreePersistThread::aboutToFinish() {
|
void OctreePersistThread::aboutToFinish() {
|
||||||
qDebug() << "Persist thread about to finish...";
|
qDebug() << "Persist thread about to finish...";
|
||||||
persistOperation();
|
persist();
|
||||||
qDebug() << "Persist thread done with about to finish...";
|
qDebug() << "Persist thread done with about to finish...";
|
||||||
}
|
}
|
||||||
|
|
||||||
void OctreePersistThread::persistOperation() {
|
void OctreePersistThread::persist() {
|
||||||
if (_tree->isDirty()) {
|
if (_tree->isDirty()) {
|
||||||
_tree->lockForWrite();
|
_tree->lockForWrite();
|
||||||
{
|
{
|
||||||
|
@ -102,9 +119,38 @@ void OctreePersistThread::persistOperation() {
|
||||||
}
|
}
|
||||||
_tree->unlock();
|
_tree->unlock();
|
||||||
|
|
||||||
|
backup(); // handle backup if requested
|
||||||
|
|
||||||
qDebug() << "saving Octree to file " << _filename << "...";
|
qDebug() << "saving Octree to file " << _filename << "...";
|
||||||
_tree->writeToSVOFile(_filename.toLocal8Bit().constData());
|
_tree->writeToSVOFile(qPrintable(_filename));
|
||||||
|
time(&_lastPersistTime);
|
||||||
_tree->clearDirtyBit(); // tree is clean after saving
|
_tree->clearDirtyBit(); // tree is clean after saving
|
||||||
qDebug("DONE saving Octree to file...");
|
qDebug() << "DONE saving Octree to file...";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void OctreePersistThread::backup() {
|
||||||
|
if (_wantBackup) {
|
||||||
|
quint64 now = usecTimestampNow();
|
||||||
|
quint64 sinceLastBackup = now - _lastBackup;
|
||||||
|
quint64 MSECS_TO_USECS = 1000;
|
||||||
|
quint64 intervalToBackup = _backupInterval * MSECS_TO_USECS;
|
||||||
|
|
||||||
|
if (sinceLastBackup > intervalToBackup) {
|
||||||
|
struct tm* localTime = localtime(&_lastPersistTime);
|
||||||
|
|
||||||
|
char backupExtension[256];
|
||||||
|
strftime(backupExtension, sizeof(backupExtension), qPrintable(_backupExtensionFormat), localTime);
|
||||||
|
|
||||||
|
QString backupFileName = _filename + backupExtension;
|
||||||
|
|
||||||
|
qDebug() << "backing up persist file " << _filename << "to" << backupFileName << "...";
|
||||||
|
int result = rename(qPrintable(_filename), qPrintable(backupFileName));
|
||||||
|
if (result == 0) {
|
||||||
|
qDebug() << "DONE backing up persist file...";
|
||||||
|
} else {
|
||||||
|
qDebug() << "ERROR in backing up persist file...";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,9 +22,13 @@
|
||||||
class OctreePersistThread : public GenericThread {
|
class OctreePersistThread : public GenericThread {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
static const int DEFAULT_PERSIST_INTERVAL = 1000 * 30; // every 30 seconds
|
static const int DEFAULT_PERSIST_INTERVAL;
|
||||||
|
static const int DEFAULT_BACKUP_INTERVAL;
|
||||||
|
static const QString DEFAULT_BACKUP_EXTENSION_FORMAT;
|
||||||
|
|
||||||
OctreePersistThread(Octree* tree, const QString& filename, int persistInterval = DEFAULT_PERSIST_INTERVAL);
|
OctreePersistThread(Octree* tree, const QString& filename, int persistInterval = DEFAULT_PERSIST_INTERVAL,
|
||||||
|
bool wantBackup = true, int backupInterval = DEFAULT_BACKUP_INTERVAL,
|
||||||
|
const QString& backupExtensionFormat = DEFAULT_BACKUP_EXTENSION_FORMAT);
|
||||||
|
|
||||||
bool isInitialLoadComplete() const { return _initialLoadComplete; }
|
bool isInitialLoadComplete() const { return _initialLoadComplete; }
|
||||||
quint64 getLoadElapsedTime() const { return _loadTimeUSecs; }
|
quint64 getLoadElapsedTime() const { return _loadTimeUSecs; }
|
||||||
|
@ -38,15 +42,21 @@ protected:
|
||||||
/// Implements generic processing behavior for this thread.
|
/// Implements generic processing behavior for this thread.
|
||||||
virtual bool process();
|
virtual bool process();
|
||||||
|
|
||||||
void persistOperation();
|
void persist();
|
||||||
|
void backup();
|
||||||
private:
|
private:
|
||||||
Octree* _tree;
|
Octree* _tree;
|
||||||
QString _filename;
|
QString _filename;
|
||||||
|
QString _backupExtensionFormat;
|
||||||
int _persistInterval;
|
int _persistInterval;
|
||||||
|
int _backupInterval;
|
||||||
bool _initialLoadComplete;
|
bool _initialLoadComplete;
|
||||||
|
|
||||||
quint64 _loadTimeUSecs;
|
quint64 _loadTimeUSecs;
|
||||||
quint64 _lastCheck;
|
quint64 _lastCheck;
|
||||||
|
quint64 _lastBackup;
|
||||||
|
bool _wantBackup;
|
||||||
|
time_t _lastPersistTime;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // hifi_OctreePersistThread_h
|
#endif // hifi_OctreePersistThread_h
|
||||||
|
|
Loading…
Reference in a new issue