Skip to content

Commit

Permalink
add log
Browse files Browse the repository at this point in the history
  • Loading branch information
oker authored and oker committed Nov 1, 2024
1 parent eed3153 commit de953a9
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 41 deletions.
86 changes: 50 additions & 36 deletions crypto/vm/db/DynamicBagOfCellsDb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Copyright 2017-2020 Telegram Systems LLP
*/
#include "vm/db/DynamicBagOfCellsDb.h"
#include "td/utils/logging.h"
#include "vm/db/CellStorage.h"
#include "vm/db/CellHashTable.h"

Expand Down Expand Up @@ -66,16 +67,25 @@ struct CellInfo {

struct Eq {
using is_transparent = void; // Pred to use
bool operator()(const CellInfo &info, const CellInfo &other_info) const { return info.key() == other_info.key();}
bool operator()(const CellInfo &info, td::Slice hash) const { return info.key().as_slice() == hash;}
bool operator()(td::Slice hash, const CellInfo &info) const { return info.key().as_slice() == hash;}

bool operator()(const CellInfo &info, const CellInfo &other_info) const {
return info.key() == other_info.key();
}
bool operator()(const CellInfo &info, td::Slice hash) const {
return info.key().as_slice() == hash;
}
bool operator()(td::Slice hash, const CellInfo &info) const {
return info.key().as_slice() == hash;
}
};
struct Hash {
using is_transparent = void; // Pred to use
using transparent_key_equal = Eq;
size_t operator()(td::Slice hash) const { return cell_hash_slice_hash(hash); }
size_t operator()(const CellInfo &info) const { return cell_hash_slice_hash(info.key().as_slice());}
size_t operator()(td::Slice hash) const {
return cell_hash_slice_hash(hash);
}
size_t operator()(const CellInfo &info) const {
return cell_hash_slice_hash(info.key().as_slice());
}
};
};

Expand Down Expand Up @@ -119,29 +129,28 @@ class DynamicBagOfCellsDbImpl : public DynamicBagOfCellsDb, private ExtCellCreat
}
SimpleExtCellCreator ext_cell_creator(cell_db_reader_);
auto promise_ptr = std::make_shared<td::Promise<Ref<DataCell>>>(std::move(promise));
executor->execute_async(
[executor, loader = *loader_, hash = CellHash::from_slice(hash), db = this,
ext_cell_creator = std::move(ext_cell_creator), promise = std::move(promise_ptr)]() mutable {
TRY_RESULT_PROMISE((*promise), res, loader.load(hash.as_slice(), true, ext_cell_creator));
if (res.status != CellLoader::LoadResult::Ok) {
promise->set_error(td::Status::Error("cell not found"));
return;
}
Ref<Cell> cell = res.cell();
executor->execute_sync([hash, db, res = std::move(res),
ext_cell_creator = std::move(ext_cell_creator)]() mutable {
db->hash_table_.apply(hash.as_slice(), [&](CellInfo &info) {
db->update_cell_info_loaded(info, hash.as_slice(), std::move(res));
});
for (auto &ext_cell : ext_cell_creator.get_created_cells()) {
auto ext_cell_hash = ext_cell->get_hash();
db->hash_table_.apply(ext_cell_hash.as_slice(), [&](CellInfo &info) {
db->update_cell_info_created_ext(info, std::move(ext_cell));
});
}
});
promise->set_result(std::move(cell));
executor->execute_async([executor, loader = *loader_, hash = CellHash::from_slice(hash), db = this,
ext_cell_creator = std::move(ext_cell_creator),
promise = std::move(promise_ptr)]() mutable {
TRY_RESULT_PROMISE((*promise), res, loader.load(hash.as_slice(), true, ext_cell_creator));
if (res.status != CellLoader::LoadResult::Ok) {
promise->set_error(td::Status::Error("cell not found"));
return;
}
Ref<Cell> cell = res.cell();
executor->execute_sync([hash, db, res = std::move(res),
ext_cell_creator = std::move(ext_cell_creator)]() mutable {
db->hash_table_.apply(hash.as_slice(), [&](CellInfo &info) {
db->update_cell_info_loaded(info, hash.as_slice(), std::move(res));
});
for (auto &ext_cell : ext_cell_creator.get_created_cells()) {
auto ext_cell_hash = ext_cell->get_hash();
db->hash_table_.apply(ext_cell_hash.as_slice(),
[&](CellInfo &info) { db->update_cell_info_created_ext(info, std::move(ext_cell)); });
}
});
promise->set_result(std::move(cell));
});
}
CellInfo &get_cell_info_force(td::Slice hash) {
return hash_table_.apply(hash, [&](CellInfo &info) { update_cell_info_force(info, hash); });
Expand Down Expand Up @@ -240,7 +249,7 @@ class DynamicBagOfCellsDbImpl : public DynamicBagOfCellsDb, private ExtCellCreat
celldb_compress_depth_ = value;
}

vm::ExtCellCreator& as_ext_cell_creator() override {
vm::ExtCellCreator &as_ext_cell_creator() override {
return *this;
}

Expand All @@ -260,8 +269,9 @@ class DynamicBagOfCellsDbImpl : public DynamicBagOfCellsDb, private ExtCellCreat

class SimpleExtCellCreator : public ExtCellCreator {
public:
explicit SimpleExtCellCreator(std::shared_ptr<CellDbReader> cell_db_reader) :
cell_db_reader_(std::move(cell_db_reader)) {}
explicit SimpleExtCellCreator(std::shared_ptr<CellDbReader> cell_db_reader)
: cell_db_reader_(std::move(cell_db_reader)) {
}

td::Result<Ref<Cell>> ext_cell(Cell::LevelMask level_mask, td::Slice hash, td::Slice depth) override {
TRY_RESULT(ext_cell, DynamicBocExtCell::create(PrunnedCellInfo{level_mask, hash, depth},
Expand All @@ -270,7 +280,7 @@ class DynamicBagOfCellsDbImpl : public DynamicBagOfCellsDb, private ExtCellCreat
return std::move(ext_cell);
}

std::vector<Ref<Cell>>& get_created_cells() {
std::vector<Ref<Cell>> &get_created_cells() {
return created_cells_;
}

Expand Down Expand Up @@ -373,8 +383,7 @@ class DynamicBagOfCellsDbImpl : public DynamicBagOfCellsDb, private ExtCellCreat
}

bool not_in_db = false;
for_each(
info, [&not_in_db, this](auto &child_info) { not_in_db |= !dfs_new_cells_in_db(child_info); }, false);
for_each(info, [&not_in_db, this](auto &child_info) { not_in_db |= !dfs_new_cells_in_db(child_info); }, false);

if (not_in_db) {
CHECK(!info.in_db);
Expand Down Expand Up @@ -642,7 +651,7 @@ class DynamicBagOfCellsDbImpl : public DynamicBagOfCellsDb, private ExtCellCreat

CellHashTable<CellInfo2> cells_;

std::queue<CellInfo2*> load_queue_;
std::queue<CellInfo2 *> load_queue_;
td::uint32 active_load_ = 0;
td::uint32 max_parallel_load_ = 4;
};
Expand All @@ -655,12 +664,14 @@ class DynamicBagOfCellsDbImpl : public DynamicBagOfCellsDb, private ExtCellCreat
return;
}
if (is_prepared_for_commit()) {
LOG(INFO) << "prepare_commit_async: ";
promise.set_result(td::Unit());
return;
}
pca_state_ = std::make_unique<PrepareCommitAsyncState>();
pca_state_->executor_ = std::move(executor);
pca_state_->promise_ = std::move(promise);
LOG(INFO) << "yus prepare_commit_async: to_inc=" << to_inc_.size() << " to_dec=" << to_dec_.size();
for (auto &new_cell : to_inc_) {
dfs_new_cells_in_db_async(new_cell);
}
Expand Down Expand Up @@ -705,10 +716,13 @@ class DynamicBagOfCellsDbImpl : public DynamicBagOfCellsDb, private ExtCellCreat
return;
}
++pca_state_->active_load_;
LOG(INFO) << "yus pca_load_from_db 0: " << info->info->cell->get_hash().to_hex();
pca_state_->executor_->execute_async(
[db = this, info, executor = pca_state_->executor_, loader = *loader_]() mutable {
auto res = loader.load_refcnt(info->info->cell->get_hash().as_slice()).move_as_ok();
LOG(INFO) << "yus pca_load_from_db 1: " << info->info->cell->get_hash().to_hex() << " " << res.refcnt();
executor->execute_sync([db, info, res = std::move(res)]() {
LOG(INFO) << "yus pca_load_from_db 2: active_load_" << db->pca_state_->active_load_;
--db->pca_state_->active_load_;
db->pca_process_load_queue();
db->pca_set_in_db(info, std::move(res));
Expand All @@ -725,6 +739,7 @@ class DynamicBagOfCellsDbImpl : public DynamicBagOfCellsDb, private ExtCellCreat
}

void pca_set_in_db(PrepareCommitAsyncState::CellInfo2 *info, CellLoader::LoadResult result) {
LOG(INFO) << " yus pca_set_in_db: " << info->info->cell->get_hash().to_hex();
info->info->sync_with_db = true;
if (result.status == CellLoader::LoadResult::Ok) {
info->info->in_db = true;
Expand Down Expand Up @@ -805,7 +820,6 @@ class DynamicBagOfCellsDbImpl : public DynamicBagOfCellsDb, private ExtCellCreat
pca_state_->promise_.set_result(td::Unit());
pca_state_ = {};
}

};
} // namespace

Expand Down
12 changes: 7 additions & 5 deletions validator/db/celldb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "td/db/RocksDb.h"
#include "rocksdb/utilities/optimistic_transaction_db.h"

#include "td/utils/logging.h"
#include "ton/ton-tl.hpp"
#include "ton/ton-io.hpp"
#include "common/delay.h"
Expand Down Expand Up @@ -198,6 +199,7 @@ void CellDbIn::store_cell(BlockIdExt block_id, td::Ref<vm::Cell> cell, td::Promi
}

boc_->inc(cell);
LOG(INFO) << "yus Storing state " << block_id.to_str();
db_busy_ = true;
boc_->prepare_commit_async(async_executor, [=, this, SelfId = actor_id(this), timer = std::move(timer),
timer_prepare = td::Timer{}, promise = std::move(promise),
Expand Down Expand Up @@ -255,11 +257,10 @@ void CellDbIn::store_cell(BlockIdExt block_id, td::Ref<vm::Cell> cell, td::Promi

void CellDbIn::get_cell_db_reader(td::Promise<std::shared_ptr<vm::CellDbReader>> promise) {
if (db_busy_) {
action_queue_.push(
[self = this, promise = std::move(promise)](td::Result<td::Unit> R) mutable {
R.ensure();
self->get_cell_db_reader(std::move(promise));
});
action_queue_.push([self = this, promise = std::move(promise)](td::Result<td::Unit> R) mutable {
R.ensure();
self->get_cell_db_reader(std::move(promise));
});
return;
}
promise.set_result(boc_->get_cell_db_reader());
Expand Down Expand Up @@ -432,6 +433,7 @@ void CellDbIn::gc_cont2(BlockHandle handle) {
auto cell = boc_->load_cell(F.root_hash.as_slice()).move_as_ok();

boc_->dec(cell);
LOG(INFO) << "yus GC " << handle->id().to_str() << " " << cell->get_hash().to_hex();
db_busy_ = true;
boc_->prepare_commit_async(
async_executor, [this, SelfId = actor_id(this), timer_boc = std::move(timer_boc), F = std::move(F), key_hash,
Expand Down
2 changes: 2 additions & 0 deletions validator/db/celldb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,10 @@ class CellDbIn : public CellDbBase {
std::queue<td::Promise<td::Unit>> action_queue_;

void release_db() {
LOG(INFO) << "yus release_db";
db_busy_ = false;
while (!db_busy_ && !action_queue_.empty()) {
LOG(INFO) << "yus release_db: action_queue_.size()=" << action_queue_.size();
auto action = std::move(action_queue_.front());
action_queue_.pop();
action.set_value(td::Unit());
Expand Down

0 comments on commit de953a9

Please sign in to comment.