diff --git a/visualdl/backend/logic/im.cc b/visualdl/backend/logic/im.cc index e33ac2768bd5325051f9a71ec6be880389c06dcd..971a9f48f7355a802200341540a3aada82eb464a 100644 --- a/visualdl/backend/logic/im.cc +++ b/visualdl/backend/logic/im.cc @@ -25,20 +25,18 @@ int ReserviorSample(int num_samples, int num_records) { return -1; } -void InformationMaintainer::SetPersistDest(const std::string &path) { +void IM::SetPersistDest(const std::string &path) { CHECK(storage_->mutable_data()->dir().empty()) << "duplicate set storage's path"; storage_->mutable_data()->set_dir(path); } -storage::Tablet *InformationMaintainer::AddTablet(const std::string &tag, - int num_samples) { +storage::Tablet *IM::AddTablet(const std::string &tag, int num_samples) { auto tablet = storage_->NewTablet(tag, num_samples); return tablet; } -void InformationMaintainer::AddRecord(const std::string &tag, - const storage::Record &data) { +void IM::AddRecord(const std::string &tag, const storage::Record &data) { auto *tablet = storage_->tablet(tag); CHECK(tablet) << "no tablet called " << tag; @@ -65,14 +63,14 @@ void InformationMaintainer::AddRecord(const std::string &tag, tablet->set_total_records(num_records + 1); } -void InformationMaintainer::Clear() { +void IM::Clear() { auto *data = storage().mutable_data(); data->clear_tablets(); data->clear_dir(); data->clear_timestamp(); } -void InformationMaintainer::PersistToDisk() { +void IM::PersistToDisk() { CHECK(!storage_->data().dir().empty()) << "path of storage should be set"; // TODO make dir first // MakeDir(storage_.data().dir()); diff --git a/visualdl/backend/logic/im.h b/visualdl/backend/logic/im.h index 588540c7eac79b8bf224ab209db7cbdc3585cd60..603f4294d61828b5ad2de5f73bb8b4c7bf9588cb 100644 --- a/visualdl/backend/logic/im.h +++ b/visualdl/backend/logic/im.h @@ -10,7 +10,7 @@ namespace visualdl { /* - * InformationMaintainer(IM) maintain the Storage singleton in memory, + * IM(IM) maintain the Storage singleton in memory, * pre-compute some the statistical information to help visualizaton. * * There should be two processes and each have an IM, one is the web server @@ -24,9 +24,9 @@ namespace visualdl { * The SDK's IM will maintain the changes and periodically write to disk, and * the web server's IM will periodically read latest storage from disk. */ -class InformationMaintainer final { +class IM final { public: - InformationMaintainer(StorageBase::Type type = StorageBase::Type::kMemory) { + IM(StorageBase::Type type = StorageBase::Type::kMemory) { switch (type) { case StorageBase::Type::kMemory: storage_.reset(new MemoryStorage); @@ -36,8 +36,8 @@ public: } } - static InformationMaintainer &Global() { - static InformationMaintainer *x = new InformationMaintainer(); + static IM &Global() { + static IM *x = new IM(); return *x; } diff --git a/visualdl/backend/logic/im_test.cc b/visualdl/backend/logic/im_test.cc index 183853e501143ffd33714b69c1c02162bf1d6f57..c387565477ee9df0ae8d2872ae6416f79e08ca7e 100644 --- a/visualdl/backend/logic/im_test.cc +++ b/visualdl/backend/logic/im_test.cc @@ -8,7 +8,7 @@ class ImTester : public ::testing::Test { protected: void SetUp() override {} - InformationMaintainer &im = InformationMaintainer::Global(); + IM &im = IM::Global(); }; TEST_F(ImTester, AddTablet) { diff --git a/visualdl/backend/logic/sdk.cc b/visualdl/backend/logic/sdk.cc index c676c5505ab215ca6e601b8307af443520238f2f..4ce6362fbcf1e9b5f07d987e7a3988d11888d64e 100644 --- a/visualdl/backend/logic/sdk.cc +++ b/visualdl/backend/logic/sdk.cc @@ -60,9 +60,7 @@ std::string TabletHelper::human_readable_buffer() const { return buffer; } -void ImHelper::PersistToDisk() const { - InformationMaintainer::Global().PersistToDisk(); -} +void ImHelper::PersistToDisk() const { IM::Global().PersistToDisk(); } // implementations for components namespace components { diff --git a/visualdl/backend/logic/sdk.h b/visualdl/backend/logic/sdk.h index 5162fee8c0bcae538cabd790d9c68e2361a79dff..963834e825dfca94b7d95164586e18b83b06a89d 100644 --- a/visualdl/backend/logic/sdk.h +++ b/visualdl/backend/logic/sdk.h @@ -94,18 +94,16 @@ public: ImHelper() {} StorageHelper storage() { - return StorageHelper( - InformationMaintainer::Global().storage().mutable_data()); + return StorageHelper(IM::Global().storage().mutable_data()); } TabletHelper tablet(const std::string &tag) { - return TabletHelper(InformationMaintainer::Global().storage().tablet(tag)); + return TabletHelper(IM::Global().storage().tablet(tag)); } TabletHelper AddTablet(const std::string &tag, int num_samples) { - return TabletHelper( - InformationMaintainer::Global().AddTablet(tag, num_samples)); + return TabletHelper(IM::Global().AddTablet(tag, num_samples)); } void ClearTablets() { - InformationMaintainer::Global().storage().mutable_data()->clear_tablets(); + IM::Global().storage().mutable_data()->clear_tablets(); } void PersistToDisk() const; diff --git a/visualdl/backend/storage/storage.cc b/visualdl/backend/storage/storage.cc index 0350b5c3f26b4414fe644cb723e6bc6a328df030..b74840d14db20ec6e6bd1a10ec55ee09b6526e27 100644 --- a/visualdl/backend/storage/storage.cc +++ b/visualdl/backend/storage/storage.cc @@ -1,4 +1,5 @@ #include +#include #include #include "visualdl/backend/storage/storage.h" @@ -72,4 +73,21 @@ void MemoryStorage::LoadFromDisk(const std::string &dir) { } } +void MemoryStorage::StartReadService() { + cc::PeriodExector::task_t task = [this] { + LOG(INFO) << "loading from " << storage_.dir(); + LoadFromDisk(storage_.dir()); + return true; + }; + cc::PeriodExector::Global()(std::move(task), 2512); +} + +void MemoryStorage::StartWriteSerice() { + cc::PeriodExector::task_t task = [this] { + LOG(INFO) << "writing to " << storage_.dir(); + PersistToDisk(); + return true; + }; + cc::PeriodExector::Global()(std::move(task), 2000); +} } // namespace visualdl