mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
Limit file descriptors num by adding archive slice lru (#892)
* --max-archive-fd option limits open files in archive manager * Don't close the latest archives + bugfix * Delete temp packages early --------- Co-authored-by: SpyCheese <mikle98@yandex.ru>
This commit is contained in:
parent
e723213d5c
commit
12c1b1a2e6
12 changed files with 316 additions and 88 deletions
|
@ -21,6 +21,7 @@
|
|||
#include "validator/interfaces/db.h"
|
||||
#include "package.hpp"
|
||||
#include "fileref.hpp"
|
||||
#include <map>
|
||||
|
||||
namespace ton {
|
||||
|
||||
|
@ -44,7 +45,7 @@ struct PackageId {
|
|||
std::string path() const;
|
||||
std::string name() const;
|
||||
|
||||
bool is_empty() {
|
||||
bool is_empty() const {
|
||||
return id == std::numeric_limits<td::uint32>::max();
|
||||
}
|
||||
static PackageId empty(bool key, bool temp) {
|
||||
|
@ -54,26 +55,33 @@ struct PackageId {
|
|||
|
||||
class PackageWriter : public td::actor::Actor {
|
||||
public:
|
||||
PackageWriter(std::shared_ptr<Package> package) : package_(std::move(package)) {
|
||||
PackageWriter(std::weak_ptr<Package> package, bool async_mode = false)
|
||||
: package_(std::move(package)), async_mode_(async_mode) {
|
||||
}
|
||||
|
||||
void append(std::string filename, td::BufferSlice data, td::Promise<std::pair<td::uint64, td::uint64>> promise);
|
||||
void set_async_mode(bool mode, td::Promise<td::Unit> promise) {
|
||||
async_mode_ = mode;
|
||||
if (!async_mode_) {
|
||||
package_->sync();
|
||||
auto p = package_.lock();
|
||||
if (p) {
|
||||
p->sync();
|
||||
}
|
||||
}
|
||||
promise.set_value(td::Unit());
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr<Package> package_;
|
||||
std::weak_ptr<Package> package_;
|
||||
bool async_mode_ = false;
|
||||
};
|
||||
|
||||
class ArchiveLru;
|
||||
|
||||
class ArchiveSlice : public td::actor::Actor {
|
||||
public:
|
||||
ArchiveSlice(td::uint32 archive_id, bool key_blocks_only, bool temp, bool finalized, std::string db_root);
|
||||
ArchiveSlice(td::uint32 archive_id, bool key_blocks_only, bool temp, bool finalized, std::string db_root,
|
||||
td::actor::ActorId<ArchiveLru> archive_lru);
|
||||
|
||||
void get_archive_id(BlockSeqno masterchain_seqno, td::Promise<td::uint64> promise);
|
||||
|
||||
|
@ -95,16 +103,23 @@ class ArchiveSlice : public td::actor::Actor {
|
|||
|
||||
void get_slice(td::uint64 archive_id, td::uint64 offset, td::uint32 limit, td::Promise<td::BufferSlice> promise);
|
||||
|
||||
void start_up() override;
|
||||
void destroy(td::Promise<td::Unit> promise);
|
||||
void truncate(BlockSeqno masterchain_seqno, ConstBlockHandle handle, td::Promise<td::Unit> promise);
|
||||
|
||||
void begin_transaction();
|
||||
void commit_transaction();
|
||||
void set_async_mode(bool mode, td::Promise<td::Unit> promise);
|
||||
|
||||
void close_files();
|
||||
|
||||
private:
|
||||
void written_data(BlockHandle handle, td::Promise<td::Unit> promise);
|
||||
void before_query();
|
||||
void do_close();
|
||||
template<typename T>
|
||||
td::Promise<T> begin_async_query(td::Promise<T> promise);
|
||||
void end_async_query();
|
||||
|
||||
void begin_transaction();
|
||||
void commit_transaction();
|
||||
|
||||
void add_file_cont(size_t idx, FileReference ref_id, td::uint64 offset, td::uint64 size,
|
||||
td::Promise<td::Unit> promise);
|
||||
|
||||
|
@ -112,13 +127,14 @@ class ArchiveSlice : public td::actor::Actor {
|
|||
td::BufferSlice get_db_key_lt_desc(ShardIdFull shard);
|
||||
td::BufferSlice get_db_key_lt_el(ShardIdFull shard, td::uint32 idx);
|
||||
td::BufferSlice get_db_key_block_info(BlockIdExt block_id);
|
||||
td::BufferSlice get_lt_from_db(ShardIdFull shard, td::uint32 idx);
|
||||
|
||||
td::uint32 archive_id_;
|
||||
|
||||
bool key_blocks_only_;
|
||||
bool temp_;
|
||||
bool finalized_;
|
||||
PackageId p_id_;
|
||||
std::string db_path_;
|
||||
|
||||
bool destroyed_ = false;
|
||||
bool async_mode_ = false;
|
||||
|
@ -127,8 +143,14 @@ class ArchiveSlice : public td::actor::Actor {
|
|||
td::uint32 huge_transaction_size_ = 0;
|
||||
td::uint32 slice_size_{100};
|
||||
|
||||
enum Status {
|
||||
st_closed, st_open, st_want_close
|
||||
} status_ = st_closed;
|
||||
size_t active_queries_ = 0;
|
||||
|
||||
std::string db_root_;
|
||||
std::shared_ptr<td::KeyValue> kv_;
|
||||
td::actor::ActorId<ArchiveLru> archive_lru_;
|
||||
std::unique_ptr<td::KeyValue> kv_;
|
||||
|
||||
struct PackageInfo {
|
||||
PackageInfo(std::shared_ptr<Package> package, td::actor::ActorOwn<PackageWriter> writer, BlockSeqno id,
|
||||
|
@ -164,6 +186,32 @@ class ArchiveSlice : public td::actor::Actor {
|
|||
static constexpr td::uint32 default_package_version() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const size_t ESTIMATED_DB_OPEN_FILES = 5;
|
||||
};
|
||||
|
||||
class ArchiveLru : public td::actor::Actor {
|
||||
public:
|
||||
explicit ArchiveLru(size_t max_total_files) : max_total_files_(max_total_files) {
|
||||
CHECK(max_total_files_ > 0);
|
||||
}
|
||||
void on_query(td::actor::ActorId<ArchiveSlice> slice, PackageId id, size_t files_count);
|
||||
void set_permanent_slices(std::vector<PackageId> ids);
|
||||
private:
|
||||
size_t current_idx_ = 1;
|
||||
struct SliceInfo {
|
||||
td::actor::ActorId<ArchiveSlice> actor;
|
||||
size_t files_count = 0;
|
||||
size_t opened_idx = 0; // 0 - not opened
|
||||
bool is_permanent = false;
|
||||
};
|
||||
std::map<std::tuple<td::uint32, bool, bool>, SliceInfo> slices_;
|
||||
std::map<size_t, PackageId> lru_;
|
||||
size_t total_files_ = 0;
|
||||
size_t max_total_files_ = 0;
|
||||
std::vector<PackageId> permanent_slices_;
|
||||
|
||||
void enforce_limit();
|
||||
};
|
||||
|
||||
} // namespace validator
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue