From fc2e1e4cad358de37758fb95a432c8778765f5f1 Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Mon, 1 Feb 2021 16:57:01 +0000 Subject: Use conditional notify instead of sleep. --- .../filestorage/filestormanagertest.cpp | 1 + .../persistence/filestorage/filestormanager.cpp | 27 ++++++++++++---------- .../persistence/filestorage/filestormanager.h | 5 ++-- 3 files changed, 19 insertions(+), 14 deletions(-) (limited to 'storage') diff --git a/storage/src/tests/persistence/filestorage/filestormanagertest.cpp b/storage/src/tests/persistence/filestorage/filestormanagertest.cpp index beecaddf5e4..5a7a4394da4 100644 --- a/storage/src/tests/persistence/filestorage/filestormanagertest.cpp +++ b/storage/src/tests/persistence/filestorage/filestormanagertest.cpp @@ -482,6 +482,7 @@ TEST_F(FileStorManagerTest, sync_waits_for_already_started_tasks) { executor->sync(); syncComplete = true; }); + std::this_thread::sleep_for(100us); EXPECT_FALSE(syncComplete); gate.countDown(); thread.join(); diff --git a/storage/src/vespa/storage/persistence/filestorage/filestormanager.cpp b/storage/src/vespa/storage/persistence/filestorage/filestormanager.cpp index 38def4f775e..42f9e876eee 100644 --- a/storage/src/vespa/storage/persistence/filestorage/filestormanager.cpp +++ b/storage/src/vespa/storage/persistence/filestorage/filestormanager.cpp @@ -82,6 +82,8 @@ FileStorManager(const config::ConfigUri & configUri, spi::PersistenceProvider& p _filestorHandler(), _sequencedExecutor(), _executeLock(), + _syncCond(), + _notifyAfterExecute(false), _executeCount(0), _tasksInExecute(), _closed(false), @@ -1003,6 +1005,9 @@ void FileStorManager::TrackExecutedTasks::run() { std::lock_guard guard(_manager._executeLock); _manager._tasksInExecute.erase(_serialNum); + if (_manager._notifyAfterExecute) { + _manager._syncCond.notify_all(); + } } std::unique_ptr @@ -1016,28 +1021,26 @@ FileStorManager::execute(const spi::Bucket &bucket, std::unique_ptr &inFlight, size_t limit) { + for (size_t serial : inFlight) { if (serial < limit) { return false; } } return true; } +} void FileStorManager::sync() { - size_t serialNumLimit; - { - std::lock_guard guard(_executeLock); - serialNumLimit = _executeCount; - } - while ( ! areTaskCompleteUntil(serialNumLimit)) { - std::this_thread::sleep_for(100us); - } + std::unique_lock guard(_executeLock); + _notifyAfterExecute = true; + _syncCond.wait(guard, [this, limit=_executeCount]() { + return areTasksCompleteUntil(_tasksInExecute, limit); + }); + _notifyAfterExecute = false; } } // storage diff --git a/storage/src/vespa/storage/persistence/filestorage/filestormanager.h b/storage/src/vespa/storage/persistence/filestorage/filestormanager.h index da47ed64f4a..fea1b9c7ea6 100644 --- a/storage/src/vespa/storage/persistence/filestorage/filestormanager.h +++ b/storage/src/vespa/storage/persistence/filestorage/filestormanager.h @@ -72,7 +72,9 @@ class FileStorManager : public StorageLinkQueued, std::shared_ptr _metrics; std::unique_ptr _filestorHandler; std::unique_ptr _sequencedExecutor; - mutable std::mutex _executeLock; + std::mutex _executeLock; + std::condition_variable _syncCond; + bool _notifyAfterExecute; size_t _executeCount; vespalib::hash_set _tasksInExecute; @@ -82,7 +84,6 @@ class FileStorManager : public StorageLinkQueued, std::unique_ptr _bucketExecutorRegistration; ServiceLayerHostInfoReporter _host_info_reporter; std::unique_ptr _resource_usage_listener_registration; - bool areTaskCompleteUntil(size_t serialNum) const; class TrackExecutedTasks; public: FileStorManager(const config::ConfigUri &, spi::PersistenceProvider&, -- cgit v1.2.3