-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
memtable flush reserve process mem and improve logs #45743
base: spill_and_reserve
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
#include "common/signal_handler.h" | ||
#include "olap/memtable.h" | ||
#include "olap/rowset/rowset_writer.h" | ||
#include "olap/storage_engine.h" | ||
#include "util/debug_points.h" | ||
#include "util/doris_metrics.h" | ||
#include "util/metrics.h" | ||
|
@@ -140,6 +141,36 @@ Status FlushToken::wait() { | |
return Status::OK(); | ||
} | ||
|
||
Status FlushToken::_try_reserve_memory(int64_t size) { | ||
auto* thread_context = doris::thread_context(); | ||
auto* memtable_flush_executor = | ||
ExecEnv::GetInstance()->storage_engine().memtable_flush_executor(); | ||
Status st; | ||
do { | ||
// only try to reserve process memory | ||
st = thread_context->reserve_memory(size); | ||
if (st.ok()) { | ||
memtable_flush_executor->inc_flushing_task(); | ||
break; | ||
} | ||
if (_is_shutdown()) { | ||
st = Status::Cancelled("flush memtable already cancelled"); | ||
break; | ||
} | ||
// Make sure at least one memtable is flushing even reserve memory failed. | ||
if (memtable_flush_executor->check_and_inc_has_any_flushing_task()) { | ||
// If there are already any flushing task, Wait for some time and retry. | ||
LOG(INFO) << fmt::format( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个每1min 打印一次吧,否则内存满的时候刷屏了 |
||
"Failed to reserve memory {} for flush memtable, retry after 100ms", size); | ||
std::this_thread::sleep_for(std::chrono::milliseconds(100)); | ||
} else { | ||
st = Status::OK(); | ||
break; | ||
} | ||
} while (true); | ||
return st; | ||
} | ||
|
||
Status FlushToken::_do_flush_memtable(MemTable* memtable, int32_t segment_id, int64_t* flush_size) { | ||
VLOG_CRITICAL << "begin to flush memtable for tablet: " << memtable->tablet_id() | ||
<< ", memsize: " << memtable->memory_usage() | ||
|
@@ -150,10 +181,18 @@ Status FlushToken::_do_flush_memtable(MemTable* memtable, int32_t segment_id, in | |
SCOPED_ATTACH_TASK(memtable->query_thread_context()); | ||
signal::set_signal_task_id(_rowset_writer->load_id()); | ||
signal::tablet_id = memtable->tablet_id(); | ||
|
||
DEFER_RELEASE_RESERVED(); | ||
|
||
{ | ||
SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER( | ||
memtable->query_thread_context().query_mem_tracker->write_tracker()); | ||
SCOPED_CONSUME_MEM_TRACKER(memtable->mem_tracker()); | ||
auto reserve_size = memtable->get_flush_reserve_memory_size(); | ||
RETURN_IF_ERROR(_try_reserve_memory(reserve_size)); | ||
Defer defer {[&]() { | ||
ExecEnv::GetInstance()->storage_engine().memtable_flush_executor()->dec_flushing_task(); | ||
}}; | ||
std::unique_ptr<vectorized::Block> block; | ||
RETURN_IF_ERROR(memtable->to_block(&block)); | ||
RETURN_IF_ERROR(_rowset_writer->flush_memtable(block.get(), segment_id, flush_size)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,7 +83,7 @@ class ThreadMemTrackerMgr { | |
void consume(int64_t size, int skip_large_memory_check = 0); | ||
void flush_untracked_mem(); | ||
|
||
doris::Status try_reserve(int64_t size); | ||
doris::Status try_reserve(int64_t size, bool only_check_process_memory); | ||
|
||
void release_reserved(); | ||
|
||
|
@@ -278,34 +278,44 @@ inline void ThreadMemTrackerMgr::flush_untracked_mem() { | |
_stop_consume = false; | ||
} | ||
|
||
inline doris::Status ThreadMemTrackerMgr::try_reserve(int64_t size) { | ||
inline doris::Status ThreadMemTrackerMgr::try_reserve(int64_t size, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 把这个API 的单测补充一下 |
||
bool only_check_process_memory) { | ||
DCHECK(_limiter_tracker); | ||
DCHECK(size >= 0); | ||
CHECK(init()); | ||
// if _reserved_mem not equal to 0, repeat reserve, | ||
// _untracked_mem store bytes that not synchronized to process reserved memory. | ||
flush_untracked_mem(); | ||
auto wg_ptr = _wg_wptr.lock(); | ||
if (!_limiter_tracker->try_reserve(size)) { | ||
auto err_msg = fmt::format( | ||
"reserve memory failed, size: {}, because query memory exceeded, memory tracker " | ||
"consumption: {}, limit: {}", | ||
PrettyPrinter::print(size, TUnit::BYTES), | ||
PrettyPrinter::print(_limiter_tracker->consumption(), TUnit::BYTES), | ||
PrettyPrinter::print(_limiter_tracker->limit(), TUnit::BYTES)); | ||
return doris::Status::Error<ErrorCode::QUERY_MEMORY_EXCEEDED>(err_msg); | ||
} | ||
if (wg_ptr) { | ||
if (!wg_ptr->add_wg_refresh_interval_memory_growth(size)) { | ||
if (only_check_process_memory) { | ||
_limiter_tracker->reserve(size); | ||
if (wg_ptr) { | ||
wg_ptr->add_wg_refresh_interval_memory_growth(size); | ||
} | ||
} else { | ||
if (!_limiter_tracker->try_reserve(size)) { | ||
auto err_msg = fmt::format( | ||
"reserve memory failed, size: {}, because workload group memory exceeded, " | ||
"workload group: {}", | ||
PrettyPrinter::print(size, TUnit::BYTES), wg_ptr->memory_debug_string()); | ||
_limiter_tracker->release(size); // rollback | ||
_limiter_tracker->release_reserved(size); // rollback | ||
return doris::Status::Error<ErrorCode::WORKLOAD_GROUP_MEMORY_EXCEEDED>(err_msg); | ||
"reserve memory failed, size: {}, because query memory exceeded, memory " | ||
"tracker " | ||
"consumption: {}, limit: {}", | ||
PrettyPrinter::print(size, TUnit::BYTES), | ||
PrettyPrinter::print(_limiter_tracker->consumption(), TUnit::BYTES), | ||
PrettyPrinter::print(_limiter_tracker->limit(), TUnit::BYTES)); | ||
return doris::Status::Error<ErrorCode::QUERY_MEMORY_EXCEEDED>(err_msg); | ||
} | ||
if (wg_ptr) { | ||
if (!wg_ptr->try_add_wg_refresh_interval_memory_growth(size)) { | ||
auto err_msg = fmt::format( | ||
"reserve memory failed, size: {}, because workload group memory exceeded, " | ||
"workload group: {}", | ||
PrettyPrinter::print(size, TUnit::BYTES), wg_ptr->memory_debug_string()); | ||
_limiter_tracker->release(size); // rollback | ||
_limiter_tracker->release_reserved(size); // rollback | ||
return doris::Status::Error<ErrorCode::WORKLOAD_GROUP_MEMORY_EXCEEDED>(err_msg); | ||
} | ||
} | ||
} | ||
|
||
if (!doris::GlobalMemoryArbitrator::try_reserve_process_memory(size)) { | ||
auto err_msg = | ||
fmt::format("reserve memory failed, size: {}, because proccess memory exceeded, {}", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -249,13 +249,22 @@ class ThreadContext { | |
thread_mem_tracker_mgr->consume(size, skip_large_memory_check); | ||
} | ||
|
||
doris::Status reserve_memory(const int64_t size) const { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. xxx 和 try_xxx 区分不清楚是否要检查query和wg的limit。 |
||
#ifdef USE_MEM_TRACKER | ||
DCHECK(doris::k_doris_exit || !doris::config::enable_memory_orphan_check || | ||
thread_mem_tracker()->label() != "Orphan") | ||
<< doris::memory_orphan_check_msg; | ||
#endif | ||
return thread_mem_tracker_mgr->try_reserve(size, true); | ||
} | ||
|
||
doris::Status try_reserve_memory(const int64_t size) const { | ||
#ifdef USE_MEM_TRACKER | ||
DCHECK(doris::k_doris_exit || !doris::config::enable_memory_orphan_check || | ||
thread_mem_tracker()->label() != "Orphan") | ||
<< doris::memory_orphan_check_msg; | ||
#endif | ||
return thread_mem_tracker_mgr->try_reserve(size); | ||
return thread_mem_tracker_mgr->try_reserve(size, false); | ||
} | ||
|
||
void release_reserved_memory() const { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果在等待期间,要flush的这个memtable 关联的导入任务呗cancel了,怎么处理?