未验证 提交 2267c4e8 编写于 作者: J Jeff Wang 提交者: GitHub

Adjust sync cycle (#328)

* Adjust the sync_cycle automatically.

* Change the multiplier type to double.

* Adjust the sync_period.

* Fix comment.

* Add the pre-commit

* Run pre-commit with other files.
上级 b9fa3ba8
...@@ -22,6 +22,15 @@ limitations under the License. */ ...@@ -22,6 +22,15 @@ limitations under the License. */
namespace visualdl { namespace visualdl {
const int minimun_sync_cycle = 100;
// Expect sync happens every 15~25 seconds
const int sync_period = 20;
const int period_range = 5;
const double slower_multiplier = 1.4;
const double faster_multiplier = 0.5;
static time_t last_sync_time = time(NULL);
template <typename T> template <typename T>
void SimpleWriteSyncGuard<T>::Start() { void SimpleWriteSyncGuard<T>::Start() {
CHECK(data_); CHECK(data_);
...@@ -33,6 +42,23 @@ void SimpleWriteSyncGuard<T>::End() { ...@@ -33,6 +42,23 @@ void SimpleWriteSyncGuard<T>::End() {
CHECK(data_); CHECK(data_);
if (data_->parent()->meta.ToSync()) { if (data_->parent()->meta.ToSync()) {
Sync(); Sync();
time_t current_time = time(NULL);
time_t interval = current_time - last_sync_time;
// If last sync happens more than 25 seconds ago, the system needs to make
// the sync-up faster
if (interval > sync_period + period_range) {
data_->parent()->meta.cycle =
std::max(long(data_->parent()->meta.cycle * faster_multiplier),
long(minimun_sync_cycle));
} else if (interval < sync_period - period_range) {
// If the last sync happens less than 15 seconds ago, the system needs to
// make the sync-up slower.
data_->parent()->meta.cycle = std::min(
long(data_->parent()->meta.cycle * slower_multiplier), LONG_MAX);
}
last_sync_time = current_time;
} }
} }
......
...@@ -32,9 +32,7 @@ Storage::Storage(const Storage& other) ...@@ -32,9 +32,7 @@ Storage::Storage(const Storage& other)
dir_ = other.dir_; dir_ = other.dir_;
} }
Storage::~Storage() { Storage::~Storage() { PersistToDisk(); }
PersistToDisk();
}
void Storage::AddMode(const std::string& x) { void Storage::AddMode(const std::string& x) {
// avoid duplicate modes. // avoid duplicate modes.
...@@ -54,13 +52,9 @@ Tablet Storage::AddTablet(const std::string& x) { ...@@ -54,13 +52,9 @@ Tablet Storage::AddTablet(const std::string& x) {
return Tablet(&(*tablets_)[x], this); return Tablet(&(*tablets_)[x], this);
} }
void Storage::SetDir(const std::string& dir) { void Storage::SetDir(const std::string& dir) { *dir_ = dir; }
*dir_ = dir;
}
std::string Storage::dir() const { std::string Storage::dir() const { return *dir_; }
return *dir_;
}
void Storage::PersistToDisk() { PersistToDisk(*dir_); } void Storage::PersistToDisk() { PersistToDisk(*dir_); }
...@@ -70,7 +64,7 @@ void Storage::PersistToDisk(const std::string& dir) { ...@@ -70,7 +64,7 @@ void Storage::PersistToDisk(const std::string& dir) {
fs::SerializeToFile(*data_, meta_path(dir)); fs::SerializeToFile(*data_, meta_path(dir));
for (auto tag : data_->tags()) { for (auto tag : data_->tags()) {
if (modified_tablet_set_.count(tag) > 0){ if (modified_tablet_set_.count(tag) > 0) {
auto it = tablets_->find(tag); auto it = tablets_->find(tag);
CHECK(it != tablets_->end()) << "tag " << tag << " not exist."; CHECK(it != tablets_->end()) << "tag " << tag << " not exist.";
fs::SerializeToFile(it->second, tablet_path(dir, tag)); fs::SerializeToFile(it->second, tablet_path(dir, tag));
...@@ -79,9 +73,7 @@ void Storage::PersistToDisk(const std::string& dir) { ...@@ -79,9 +73,7 @@ void Storage::PersistToDisk(const std::string& dir) {
modified_tablet_set_.clear(); modified_tablet_set_.clear();
} }
Storage* Storage::parent() { Storage* Storage::parent() { return this; }
return this;
}
void Storage::MarkTabletModified(const std::string tag) { void Storage::MarkTabletModified(const std::string tag) {
modified_tablet_set_.insert(tag); modified_tablet_set_.insert(tag);
...@@ -90,7 +82,7 @@ void Storage::MarkTabletModified(const std::string tag) { ...@@ -90,7 +82,7 @@ void Storage::MarkTabletModified(const std::string tag) {
void Storage::AddTag(const std::string& x) { void Storage::AddTag(const std::string& x) {
*data_->add_tags() = x; *data_->add_tags() = x;
WRITE_GUARD WRITE_GUARD
} }
// StorageReader // StorageReader
std::vector<std::string> StorageReader::all_tags() { std::vector<std::string> StorageReader::all_tags() {
......
...@@ -29,11 +29,12 @@ struct TabletReader; ...@@ -29,11 +29,12 @@ struct TabletReader;
* Tablet is a helper for operations on storage::Tablet. * Tablet is a helper for operations on storage::Tablet.
*/ */
struct Tablet { struct Tablet {
enum Type { kScalar = 0, kHistogram = 1, kImage = 2, kUnknown = -1}; enum Type { kScalar = 0, kHistogram = 1, kImage = 2, kUnknown = -1 };
DECL_GUARD(Tablet); DECL_GUARD(Tablet);
Tablet(storage::Tablet* x, Storage* parent) : data_(x), x_(parent), internal_encoded_tag_("") {} Tablet(storage::Tablet* x, Storage* parent)
: data_(x), x_(parent), internal_encoded_tag_("") {}
static Type type(const std::string& name) { static Type type(const std::string& name) {
if (name == "scalar") { if (name == "scalar") {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册