提交 b1ba69d1 编写于 作者: S superjom

rename InformationMaintainer to IM

上级 d709252b
...@@ -25,20 +25,18 @@ int ReserviorSample(int num_samples, int num_records) { ...@@ -25,20 +25,18 @@ int ReserviorSample(int num_samples, int num_records) {
return -1; return -1;
} }
void InformationMaintainer::SetPersistDest(const std::string &path) { void IM::SetPersistDest(const std::string &path) {
CHECK(storage_->mutable_data()->dir().empty()) CHECK(storage_->mutable_data()->dir().empty())
<< "duplicate set storage's path"; << "duplicate set storage's path";
storage_->mutable_data()->set_dir(path); storage_->mutable_data()->set_dir(path);
} }
storage::Tablet *InformationMaintainer::AddTablet(const std::string &tag, storage::Tablet *IM::AddTablet(const std::string &tag, int num_samples) {
int num_samples) {
auto tablet = storage_->NewTablet(tag, num_samples); auto tablet = storage_->NewTablet(tag, num_samples);
return tablet; return tablet;
} }
void InformationMaintainer::AddRecord(const std::string &tag, void IM::AddRecord(const std::string &tag, const storage::Record &data) {
const storage::Record &data) {
auto *tablet = storage_->tablet(tag); auto *tablet = storage_->tablet(tag);
CHECK(tablet) << "no tablet called " << tag; CHECK(tablet) << "no tablet called " << tag;
...@@ -65,14 +63,14 @@ void InformationMaintainer::AddRecord(const std::string &tag, ...@@ -65,14 +63,14 @@ void InformationMaintainer::AddRecord(const std::string &tag,
tablet->set_total_records(num_records + 1); tablet->set_total_records(num_records + 1);
} }
void InformationMaintainer::Clear() { void IM::Clear() {
auto *data = storage().mutable_data(); auto *data = storage().mutable_data();
data->clear_tablets(); data->clear_tablets();
data->clear_dir(); data->clear_dir();
data->clear_timestamp(); data->clear_timestamp();
} }
void InformationMaintainer::PersistToDisk() { void IM::PersistToDisk() {
CHECK(!storage_->data().dir().empty()) << "path of storage should be set"; CHECK(!storage_->data().dir().empty()) << "path of storage should be set";
// TODO make dir first // TODO make dir first
// MakeDir(storage_.data().dir()); // MakeDir(storage_.data().dir());
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
namespace visualdl { 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. * pre-compute some the statistical information to help visualizaton.
* *
* There should be two processes and each have an IM, one is the web server * There should be two processes and each have an IM, one is the web server
...@@ -24,9 +24,9 @@ namespace visualdl { ...@@ -24,9 +24,9 @@ namespace visualdl {
* The SDK's IM will maintain the changes and periodically write to disk, and * 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. * the web server's IM will periodically read latest storage from disk.
*/ */
class InformationMaintainer final { class IM final {
public: public:
InformationMaintainer(StorageBase::Type type = StorageBase::Type::kMemory) { IM(StorageBase::Type type = StorageBase::Type::kMemory) {
switch (type) { switch (type) {
case StorageBase::Type::kMemory: case StorageBase::Type::kMemory:
storage_.reset(new MemoryStorage); storage_.reset(new MemoryStorage);
...@@ -36,8 +36,8 @@ public: ...@@ -36,8 +36,8 @@ public:
} }
} }
static InformationMaintainer &Global() { static IM &Global() {
static InformationMaintainer *x = new InformationMaintainer(); static IM *x = new IM();
return *x; return *x;
} }
......
...@@ -8,7 +8,7 @@ class ImTester : public ::testing::Test { ...@@ -8,7 +8,7 @@ class ImTester : public ::testing::Test {
protected: protected:
void SetUp() override {} void SetUp() override {}
InformationMaintainer &im = InformationMaintainer::Global(); IM &im = IM::Global();
}; };
TEST_F(ImTester, AddTablet) { TEST_F(ImTester, AddTablet) {
......
...@@ -60,9 +60,7 @@ std::string TabletHelper::human_readable_buffer() const { ...@@ -60,9 +60,7 @@ std::string TabletHelper::human_readable_buffer() const {
return buffer; return buffer;
} }
void ImHelper::PersistToDisk() const { void ImHelper::PersistToDisk() const { IM::Global().PersistToDisk(); }
InformationMaintainer::Global().PersistToDisk();
}
// implementations for components // implementations for components
namespace components { namespace components {
......
...@@ -94,18 +94,16 @@ public: ...@@ -94,18 +94,16 @@ public:
ImHelper() {} ImHelper() {}
StorageHelper storage() { StorageHelper storage() {
return StorageHelper( return StorageHelper(IM::Global().storage().mutable_data());
InformationMaintainer::Global().storage().mutable_data());
} }
TabletHelper tablet(const std::string &tag) { 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) { TabletHelper AddTablet(const std::string &tag, int num_samples) {
return TabletHelper( return TabletHelper(IM::Global().AddTablet(tag, num_samples));
InformationMaintainer::Global().AddTablet(tag, num_samples));
} }
void ClearTablets() { void ClearTablets() {
InformationMaintainer::Global().storage().mutable_data()->clear_tablets(); IM::Global().storage().mutable_data()->clear_tablets();
} }
void PersistToDisk() const; void PersistToDisk() const;
......
#include <glog/logging.h> #include <glog/logging.h>
#include <visualdl/backend/utils/concurrency.h>
#include <fstream> #include <fstream>
#include "visualdl/backend/storage/storage.h" #include "visualdl/backend/storage/storage.h"
...@@ -72,4 +73,21 @@ void MemoryStorage::LoadFromDisk(const std::string &dir) { ...@@ -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 } // namespace visualdl
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册