deallocate all

This commit is contained in:
andrew (from workstation) 2020-05-21 23:59:36 +02:00
parent fbbf1ea907
commit 050ad6283e
3 changed files with 28 additions and 18 deletions

View File

@ -41,10 +41,13 @@ class FileId {
} }
int32 get() const { int32 get() const {
const_cast<FileId*>(this)->time_ = std::time(nullptr);
return id; return id;
} }
void set_time() {
time_ = std::time(nullptr);
}
int64 get_time() const { int64 get_time() const {
return time_; return time_;
} }

View File

@ -981,6 +981,7 @@ bool FileManager::try_fix_partial_local_location(FileNodePtr node) {
} }
FileManager::FileIdInfo *FileManager::get_file_id_info(FileId file_id) { FileManager::FileIdInfo *FileManager::get_file_id_info(FileId file_id) {
file_id.set_time();
LOG_CHECK(0 <= file_id.get() && file_id.get() < static_cast<int32>(file_id_info_.size())) LOG_CHECK(0 <= file_id.get() && file_id.get() < static_cast<int32>(file_id_info_.size()))
<< file_id << " " << file_id_info_.size(); << file_id << " " << file_id_info_.size();
return &file_id_info_[file_id.get()]; return &file_id_info_[file_id.get()];
@ -3194,12 +3195,15 @@ FileId FileManager::next_file_id() {
if (!empty_file_ids_.empty()) { if (!empty_file_ids_.empty()) {
auto res = empty_file_ids_.back(); auto res = empty_file_ids_.back();
empty_file_ids_.pop_back(); empty_file_ids_.pop_back();
return FileId{res, 0}; auto ret = FileId{res, 0};
ret.set_time();
return ret;
} }
auto id = (int32) file_id_info_.size(); auto id = (int32) file_id_info_.size();
FileId res(static_cast<int32>(id), 0); FileId res(static_cast<int32>(id), 0);
file_id_info_.push_back({}); file_id_info_.push_back({});
res.set_time();
return res; return res;
} }
@ -3739,7 +3743,7 @@ void FileManager::memory_cleanup() {
if (main_node_id != 0) { if (main_node_id != 0) {
auto &node = file_nodes_[main_node_id]; auto &node = file_nodes_[main_node_id];
if (node != nullptr && ((int32) i) == node->main_file_id_.fast_get()) { if (node != nullptr && ((int32) i) == node->main_file_id_.fast_get()) {
if (time - node->main_file_id_.get_time() > 20 /* MAIN FILE TTL */) { if (time - node->main_file_id_.get_time() > 60 * 5 /* MAIN FILE TTL */) {
node->main_file_id_.reset_time(); node->main_file_id_.reset_time();
for (auto &file_id : node->file_ids_) { for (auto &file_id : node->file_ids_) {
@ -3831,7 +3835,7 @@ void FileManager::memory_cleanup() {
/* DESTROY INVALID file_hash_to_file_id_ */ /* DESTROY INVALID file_hash_to_file_id_ */
for (const auto &pair : file_hash_to_file_id_) { for (const auto &pair : file_hash_to_file_id_) {
auto &file = file_id_info_[pair.second.fast_get()]; auto &file = file_id_info_[pair.second.fast_get()];
if (file_nodes_[file.node_id_] == nullptr) { if (&file == nullptr || file_nodes_[file.node_id_] == nullptr) {
file_hash_to_file_id_.erase(pair.first); file_hash_to_file_id_.erase(pair.first);
} }
} }
@ -3839,7 +3843,7 @@ void FileManager::memory_cleanup() {
/* DESTROY INVALID local_location_to_file_id_ */ /* DESTROY INVALID local_location_to_file_id_ */
for (const auto &pair : local_location_to_file_id_) { for (const auto &pair : local_location_to_file_id_) {
auto &file = file_id_info_[pair.second.fast_get()]; auto &file = file_id_info_[pair.second.fast_get()];
if (file_nodes_[file.node_id_] == nullptr) { if (&file == nullptr || file_nodes_[file.node_id_] == nullptr) {
local_location_to_file_id_.erase(pair.first); local_location_to_file_id_.erase(pair.first);
} }
} }
@ -3847,7 +3851,7 @@ void FileManager::memory_cleanup() {
/* DESTROY INVALID generate_location_to_file_id_ */ /* DESTROY INVALID generate_location_to_file_id_ */
for (const auto &pair : generate_location_to_file_id_) { for (const auto &pair : generate_location_to_file_id_) {
auto &file = file_id_info_[pair.second.fast_get()]; auto &file = file_id_info_[pair.second.fast_get()];
if (file_nodes_[file.node_id_] == nullptr) { if (&file == nullptr || file_nodes_[file.node_id_] == nullptr) {
generate_location_to_file_id_.erase(pair.first); generate_location_to_file_id_.erase(pair.first);
} }
} }
@ -3855,7 +3859,7 @@ void FileManager::memory_cleanup() {
/* DESTROY INVALID remote_location_info_ */ /* DESTROY INVALID remote_location_info_ */
for (const auto &pair : remote_location_info_.get_map()) { for (const auto &pair : remote_location_info_.get_map()) {
auto &file = file_id_info_[pair.first.file_id_.fast_get()]; auto &file = file_id_info_[pair.first.file_id_.fast_get()];
if (file_nodes_[file.node_id_] == nullptr) { if (&file == nullptr || file_nodes_[file.node_id_] == nullptr) {
remote_location_info_.erase_map_key(pair.first, pair.second); remote_location_info_.erase_map_key(pair.first, pair.second);
} }
} }
@ -3866,7 +3870,7 @@ void FileManager::memory_cleanup() {
empty_file_ids_.shrink_to_fit(); empty_file_ids_.shrink_to_fit();
empty_node_ids_.shrink_to_fit(); empty_node_ids_.shrink_to_fit();
LOG(INFO) << empty_file_ids_.size() << " empty ids and " << queries_container_.size() << " running queries"; LOG(ERROR) << empty_file_ids_.size() << " empty ids and " << queries_container_.size() << " running queries";
is_closed_ = false; is_closed_ = false;
} }
} }

View File

@ -23,37 +23,40 @@ class Enumerator {
return map_; return map_;
} }
Key next() { std::pair<Key, bool> next() {
if (!empty_id_.empty()) { if (!empty_id_.empty()) {
auto res = empty_id_.back(); auto res = empty_id_.back();
empty_id_.pop_back(); empty_id_.pop_back();
return res; return std::make_pair(res, true);
} }
return (Key) (arr_.size() + 1); return std::make_pair((Key) (arr_.size() + 1), false);
} }
void erase_map_key(ValueT key_x, Key key_y) { void erase_map_key(ValueT key_x, Key key_y) {
auto pos_y = static_cast<Key>(key_y - 1);
empty_id_.push_back(key_y); empty_id_.push_back(key_y);
map_.erase(key_x); map_.erase(key_x);
arr_[key_y] = nullptr; arr_[pos_y] = nullptr;
} }
Key add(ValueT v) { Key add(ValueT v) {
CHECK(arr_.size() < static_cast<size_t>(std::numeric_limits<int32>::max() - 1)); auto next_id = next();
Key next_id = next();
bool was_inserted; bool was_inserted;
decltype(map_.begin()) it; decltype(map_.begin()) it;
std::tie(it, was_inserted) = map_.emplace(std::move(v), next_id); std::tie(it, was_inserted) = map_.emplace(std::move(v), next_id.first);
if (was_inserted) { if (was_inserted && next_id.second) {
arr_[next_id.first - 1] = &it->first;
} else if (was_inserted) {
arr_.push_back(&it->first); arr_.push_back(&it->first);
} else if (next_id.second) {
empty_id_.push_back(next_id.first);
} }
return it->second; return it->second;
} }
const ValueT &get(Key key) const { const ValueT &get(Key key) const {
auto pos = static_cast<size_t>(key - 1); auto pos = static_cast<Key>(key - 1);
CHECK(pos < arr_.size());
return *arr_[pos]; return *arr_[pos];
} }