From 0cc465d39f79c5ace6c16f570d6f1cacfa879060 Mon Sep 17 00:00:00 2001 From: James Turner <james@flightgear.org> Date: Fri, 5 Feb 2021 11:14:34 +0000 Subject: [PATCH] Tweak Natural-earth loading to reduce DB contention MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reduce ‘SQLITE BUSY’ issues with multiple running copies of FG --- src/GUI/QtLauncher.cxx | 8 ++++++-- src/Navaids/NavDataCache.cxx | 5 ++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/GUI/QtLauncher.cxx b/src/GUI/QtLauncher.cxx index a9bbe10b0..baf5de35f 100644 --- a/src/GUI/QtLauncher.cxx +++ b/src/GUI/QtLauncher.cxx @@ -218,14 +218,18 @@ private: return; flightgear::PolyLineList::const_iterator begin = m_parsedLines.begin() + m_lineInsertCount; - unsigned int numToAdd = std::min<unsigned int>(1000U, static_cast<unsigned int>(m_parsedLines.size()) - m_lineInsertCount); + + // add lines in batches of 100; this is a tradeoff to ensure we don't lock + // the database for too long. (Which triggers SQLITE_BUSY) + const int lineInsertBatchSize = 100U; + unsigned int numToAdd = std::min<unsigned int>(100U, static_cast<unsigned int>(m_parsedLines.size()) - m_lineInsertCount); flightgear::PolyLineList::const_iterator end = begin + numToAdd; flightgear::PolyLine::bulkAddToSpatialIndex(begin, end); if (end == m_parsedLines.end()) { deleteLater(); // commit suicide } else { - m_lineInsertCount += 1000; + m_lineInsertCount += lineInsertBatchSize; QTimer::singleShot(50, this, SLOT(onFinished())); } } diff --git a/src/Navaids/NavDataCache.cxx b/src/Navaids/NavDataCache.cxx index 1e28d691e..e5f1d4f63 100644 --- a/src/Navaids/NavDataCache.cxx +++ b/src/Navaids/NavDataCache.cxx @@ -458,6 +458,8 @@ public: { int retries = 0; int result; + int retryMSec = 10; + while (retries < MAX_RETRIES) { result = sqlite3_step(stmt); if (result == SQLITE_ROW) { @@ -473,7 +475,8 @@ public: } SG_LOG(SG_NAVCACHE, SG_ALERT, "NavCache contention on select, will retry:" << retries); - SGTimeStamp::sleepForMSec(++retries * 10); + SGTimeStamp::sleepForMSec(retryMSec); + retryMSec *= 2; // double the back-off time, each time } // of retry loop for DB locked if (retries >= MAX_RETRIES) {