aboutsummaryrefslogtreecommitdiff
path: root/zenstore
diff options
context:
space:
mode:
Diffstat (limited to 'zenstore')
-rw-r--r--zenstore/gc.cpp79
-rw-r--r--zenstore/include/zenstore/gc.h12
2 files changed, 52 insertions, 39 deletions
diff --git a/zenstore/gc.cpp b/zenstore/gc.cpp
index bf04543b5..86f215b97 100644
--- a/zenstore/gc.cpp
+++ b/zenstore/gc.cpp
@@ -289,20 +289,19 @@ GcScheduler::Initialize(const GcSchedulerConfig& Config)
std::filesystem::create_directories(Config.RootDirectory);
- Timepoint CurrentTime = Clock::now();
+ GcClock::TimePoint LastGcTime = GcClock::Now();
if (CbObject SchedulerState = LoadCompactBinaryObject(Config.RootDirectory / "gc_state"))
{
- const int64_t LastGcTime = SchedulerState["LastGcTime"sv].AsInt64();
- CurrentTime = Timepoint(Clock::duration(LastGcTime));
+ LastGcTime = GcClock::TimePoint(GcClock::Duration(SchedulerState["LastGcTime"sv].AsInt64()));
- if (CurrentTime + m_Config.Interval < Clock::now())
+ if (LastGcTime + m_Config.Interval < GcClock::Now())
{
- CurrentTime = Clock::now();
+ LastGcTime = GcClock::Now();
}
}
- m_NextGcTime = CurrentTime + m_Config.Interval;
+ m_NextGcTime = NextGcTime(LastGcTime);
m_GcThread = std::jthread(&GcScheduler::SchedulerThread, this);
}
@@ -336,41 +335,57 @@ GcScheduler::SchedulerThread()
}
}
- if (m_Config.Enabled)
+ if (!m_Config.Enabled)
{
- Stopwatch Timer;
-
- DiskSpace Space;
- DiskSpaceInfo(m_Config.RootDirectory, Space);
- ZEN_INFO("garbage collection STARTING, disk space {}/{} (free/total)", NiceBytes(Space.Free), NiceBytes(Space.Total));
+ ZEN_INFO("disabled");
+ continue;
+ }
- GcContext GcCtx;
- GcCtx.SetDeletionMode(true);
- GcCtx.CollectSmallObjects(m_Config.CollectSmallObjects);
- GcCtx.MaxCacheDuration(m_Config.MaxCacheDuration);
+ Stopwatch Timer;
- ZEN_DEBUG("collecting small objects {}, max cache duration {}s",
- m_Config.CollectSmallObjects ? "YES"sv : "NO"sv,
- m_Config.MaxCacheDuration.count());
+ DiskSpace Space;
+ DiskSpaceInfo(m_Config.RootDirectory, Space);
+ ZEN_INFO("garbage collection STARTING, disk space {}/{} (free/total)", NiceBytes(Space.Free), NiceBytes(Space.Total));
- m_CasGc.CollectGarbage(GcCtx);
+ GcContext GcCtx;
+ GcCtx.SetDeletionMode(true);
+ GcCtx.CollectSmallObjects(m_Config.CollectSmallObjects);
+ GcCtx.MaxCacheDuration(m_Config.MaxCacheDuration);
- m_Status = static_cast<uint32_t>(GcSchedulerStatus::kIdle);
- m_NextGcTime = Clock::now() + m_Config.Interval;
+ ZEN_DEBUG("collecting small objects {}, max cache duration {}s",
+ m_Config.CollectSmallObjects ? "YES"sv : "NO"sv,
+ m_Config.MaxCacheDuration.count());
- {
- CbObjectWriter SchedulderState;
- SchedulderState << "LastGcTime"sv << static_cast<int64_t>(Clock::now().time_since_epoch().count());
- SaveCompactBinaryObject(m_Config.RootDirectory / "gc_state", SchedulderState.Save());
- }
+ m_CasGc.CollectGarbage(GcCtx);
- DiskSpaceInfo(m_Config.RootDirectory, Space);
+ m_Status = static_cast<uint32_t>(GcSchedulerStatus::kIdle);
+ m_NextGcTime = NextGcTime(GcClock::Now());
- ZEN_INFO("garbage collection DONE after {}, disk space {}/{} (free/total)",
- NiceTimeSpanMs(Timer.GetElapsedTimeMs()),
- NiceBytes(Space.Free),
- NiceBytes(Space.Total));
+ {
+ CbObjectWriter SchedulderState;
+ SchedulderState << "LastGcTime"sv << static_cast<int64_t>(GcClock::Now().time_since_epoch().count());
+ SaveCompactBinaryObject(m_Config.RootDirectory / "gc_state", SchedulderState.Save());
}
+
+ DiskSpaceInfo(m_Config.RootDirectory, Space);
+
+ ZEN_INFO("garbage collection DONE after {}, disk space {}/{} (free/total)",
+ NiceTimeSpanMs(Timer.GetElapsedTimeMs()),
+ NiceBytes(Space.Free),
+ NiceBytes(Space.Total));
+ }
+}
+
+GcClock::TimePoint
+GcScheduler::NextGcTime(GcClock::TimePoint CurrentTime)
+{
+ if (m_Config.Interval.count())
+ {
+ return CurrentTime + m_Config.Interval;
+ }
+ else
+ {
+ return GcClock::TimePoint::max();
}
}
diff --git a/zenstore/include/zenstore/gc.h b/zenstore/include/zenstore/gc.h
index 3c03c7feb..5a6afc8da 100644
--- a/zenstore/include/zenstore/gc.h
+++ b/zenstore/include/zenstore/gc.h
@@ -150,7 +150,7 @@ enum class GcSchedulerStatus : uint32_t
struct GcSchedulerConfig
{
std::filesystem::path RootDirectory;
- std::chrono::seconds Interval{3600};
+ std::chrono::seconds Interval{};
std::chrono::seconds MaxCacheDuration{86400};
bool CollectSmallObjects = false;
bool Enabled = false;
@@ -158,9 +158,6 @@ struct GcSchedulerConfig
class GcScheduler
{
- using Clock = std::chrono::system_clock;
- using Timepoint = std::chrono::time_point<Clock>;
-
public:
GcScheduler(CasGc& CasGc);
~GcScheduler();
@@ -171,13 +168,14 @@ public:
void Shutdown();
private:
- void SchedulerThread();
- spdlog::logger& Log() { return m_Log; }
+ void SchedulerThread();
+ GcClock::TimePoint NextGcTime(GcClock::TimePoint CurrentTime);
+ spdlog::logger& Log() { return m_Log; }
spdlog::logger& m_Log;
CasGc& m_CasGc;
GcSchedulerConfig m_Config;
- Timepoint m_NextGcTime{};
+ GcClock::TimePoint m_NextGcTime{};
std::atomic_uint32_t m_Status{};
std::jthread m_GcThread;
std::mutex m_GcMutex;