Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
2dot5
ClickHouse
提交
2619efad
C
ClickHouse
项目概览
2dot5
/
ClickHouse
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
ClickHouse
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
2619efad
编写于
10月 21, 2020
作者:
A
Alexey Milovidov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix multiple issues with memory tracking
上级
b26e979e
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
49 addition
and
37 deletion
+49
-37
src/Common/MemoryTracker.cpp
src/Common/MemoryTracker.cpp
+20
-23
src/Common/MemoryTracker.h
src/Common/MemoryTracker.h
+12
-7
src/Interpreters/SystemLog.h
src/Interpreters/SystemLog.h
+1
-1
src/Interpreters/executeQuery.cpp
src/Interpreters/executeQuery.cpp
+1
-1
src/Storages/MergeTree/IMergeTreeDataPart.cpp
src/Storages/MergeTree/IMergeTreeDataPart.cpp
+1
-1
src/Storages/MergeTree/MergeTreeDataPartWriterOnDisk.cpp
src/Storages/MergeTree/MergeTreeDataPartWriterOnDisk.cpp
+1
-1
src/Storages/MergeTree/MergeTreeMarksLoader.cpp
src/Storages/MergeTree/MergeTreeMarksLoader.cpp
+1
-1
src/Storages/StorageBuffer.cpp
src/Storages/StorageBuffer.cpp
+2
-2
tests/queries/0_stateless/01529_bad_memory_tracking.reference
...s/queries/0_stateless/01529_bad_memory_tracking.reference
+0
-0
tests/queries/0_stateless/01529_bad_memory_tracking.sh
tests/queries/0_stateless/01529_bad_memory_tracking.sh
+10
-0
未找到文件。
src/Common/MemoryTracker.cpp
浏览文件 @
2619efad
...
...
@@ -30,6 +30,8 @@ namespace ProfileEvents
static
constexpr
size_t
log_peak_memory_usage_every
=
1ULL
<<
30
;
thread_local
bool
MemoryTracker
::
BlockerInThread
::
is_blocked
=
false
;
MemoryTracker
total_memory_tracker
(
nullptr
,
VariableContext
::
Global
);
...
...
@@ -56,13 +58,15 @@ MemoryTracker::~MemoryTracker()
void
MemoryTracker
::
logPeakMemoryUsage
()
const
{
const
auto
*
description
=
description_ptr
.
load
(
std
::
memory_order_relaxed
);
LOG_DEBUG
(
&
Poco
::
Logger
::
get
(
"MemoryTracker"
),
"Peak memory usage{}: {}."
,
(
description
?
" "
+
std
::
string
(
description
)
:
""
),
ReadableSize
(
peak
));
LOG_DEBUG
(
&
Poco
::
Logger
::
get
(
"MemoryTracker"
),
"Peak memory usage{}: {}."
,
(
description
?
" "
+
std
::
string
(
description
)
:
""
),
ReadableSize
(
peak
));
}
void
MemoryTracker
::
logMemoryUsage
(
Int64
current
)
const
{
const
auto
*
description
=
description_ptr
.
load
(
std
::
memory_order_relaxed
);
LOG_DEBUG
(
&
Poco
::
Logger
::
get
(
"MemoryTracker"
),
"Current memory usage{}: {}."
,
(
description
?
" "
+
std
::
string
(
description
)
:
""
),
ReadableSize
(
current
));
LOG_DEBUG
(
&
Poco
::
Logger
::
get
(
"MemoryTracker"
),
"Current memory usage{}: {}."
,
(
description
?
" "
+
std
::
string
(
description
)
:
""
),
ReadableSize
(
current
));
}
...
...
@@ -71,7 +75,7 @@ void MemoryTracker::alloc(Int64 size)
if
(
size
<
0
)
throw
DB
::
Exception
(
DB
::
ErrorCodes
::
LOGICAL_ERROR
,
"Negative size ({}) is passed to MemoryTracker. It is a bug."
,
size
);
if
(
blocker
.
isCancell
ed
())
if
(
BlockerInThread
::
isBlock
ed
())
return
;
/** Using memory_order_relaxed means that if allocations are done simultaneously,
...
...
@@ -86,12 +90,15 @@ void MemoryTracker::alloc(Int64 size)
Int64
current_hard_limit
=
hard_limit
.
load
(
std
::
memory_order_relaxed
);
Int64
current_profiler_limit
=
profiler_limit
.
load
(
std
::
memory_order_relaxed
);
/// Cap the limit to the total_memory_tracker, since it may include some drift.
/// Cap the limit to the total_memory_tracker, since it may include some drift
/// for user-level memory tracker.
///
/// And since total_memory_tracker is reset to the process resident
/// memory peridically (in AsynchronousMetrics::update()), any limit can be
/// capped to it, to avoid possible drift.
if
(
unlikely
(
current_hard_limit
&&
will_be
>
current_hard_limit
))
if
(
unlikely
(
current_hard_limit
&&
will_be
>
current_hard_limit
&&
level
==
VariableContext
::
User
))
{
Int64
total_amount
=
total_memory_tracker
.
get
();
if
(
amount
>
total_amount
)
...
...
@@ -104,10 +111,8 @@ void MemoryTracker::alloc(Int64 size)
std
::
bernoulli_distribution
fault
(
fault_probability
);
if
(
unlikely
(
fault_probability
&&
fault
(
thread_local_rng
)))
{
free
(
size
);
/// Prevent recursion. Exception::ctor -> std::string -> new[] -> MemoryTracker::alloc
auto
untrack_lock
=
blocker
.
cancel
();
// NOLINT
BlockerInThread
untrack_lock
;
ProfileEvents
::
increment
(
ProfileEvents
::
QueryMemoryLimitExceeded
);
std
::
stringstream
message
;
...
...
@@ -118,12 +123,13 @@ void MemoryTracker::alloc(Int64 size)
<<
" (attempt to allocate chunk of "
<<
size
<<
" bytes)"
<<
", maximum: "
<<
formatReadableSizeWithBinarySuffix
(
current_hard_limit
);
amount
.
fetch_sub
(
size
,
std
::
memory_order_relaxed
);
throw
DB
::
Exception
(
message
.
str
(),
DB
::
ErrorCodes
::
MEMORY_LIMIT_EXCEEDED
);
}
if
(
unlikely
(
current_profiler_limit
&&
will_be
>
current_profiler_limit
))
{
auto
no_track
=
blocker
.
cancel
()
;
BlockerInThread
untrack_lock
;
DB
::
TraceCollector
::
collect
(
DB
::
TraceType
::
Memory
,
StackTrace
(),
size
);
setOrRaiseProfilerLimit
((
will_be
+
profiler_step
-
1
)
/
profiler_step
*
profiler_step
);
}
...
...
@@ -131,16 +137,14 @@ void MemoryTracker::alloc(Int64 size)
std
::
bernoulli_distribution
sample
(
sample_probability
);
if
(
unlikely
(
sample_probability
&&
sample
(
thread_local_rng
)))
{
auto
no_track
=
blocker
.
cancel
()
;
BlockerInThread
untrack_lock
;
DB
::
TraceCollector
::
collect
(
DB
::
TraceType
::
MemorySample
,
StackTrace
(),
size
);
}
if
(
unlikely
(
current_hard_limit
&&
will_be
>
current_hard_limit
))
{
free
(
size
);
/// Prevent recursion. Exception::ctor -> std::string -> new[] -> MemoryTracker::alloc
auto
no_track
=
blocker
.
cancel
();
// NOLINT
BlockerInThread
untrack_lock
;
ProfileEvents
::
increment
(
ProfileEvents
::
QueryMemoryLimitExceeded
);
std
::
stringstream
message
;
...
...
@@ -151,6 +155,7 @@ void MemoryTracker::alloc(Int64 size)
<<
" (attempt to allocate chunk of "
<<
size
<<
" bytes)"
<<
", maximum: "
<<
formatReadableSizeWithBinarySuffix
(
current_hard_limit
);
amount
.
fetch_sub
(
size
,
std
::
memory_order_relaxed
);
throw
DB
::
Exception
(
message
.
str
(),
DB
::
ErrorCodes
::
MEMORY_LIMIT_EXCEEDED
);
}
...
...
@@ -177,13 +182,13 @@ void MemoryTracker::updatePeak(Int64 will_be)
void
MemoryTracker
::
free
(
Int64
size
)
{
if
(
blocker
.
isCancell
ed
())
if
(
BlockerInThread
::
isBlock
ed
())
return
;
std
::
bernoulli_distribution
sample
(
sample_probability
);
if
(
unlikely
(
sample_probability
&&
sample
(
thread_local_rng
)))
{
auto
no_track
=
blocker
.
cancel
()
;
BlockerInThread
untrack_lock
;
DB
::
TraceCollector
::
collect
(
DB
::
TraceType
::
MemorySample
,
StackTrace
(),
-
size
);
}
...
...
@@ -298,11 +303,3 @@ namespace CurrentMemoryTracker
}
}
}
DB
::
SimpleActionLock
getCurrentMemoryTrackerActionLock
()
{
auto
*
memory_tracker
=
DB
::
CurrentThread
::
getMemoryTracker
();
if
(
!
memory_tracker
)
return
{};
return
memory_tracker
->
blocker
.
cancel
();
}
src/Common/MemoryTracker.h
浏览文件 @
2619efad
...
...
@@ -3,7 +3,6 @@
#include <atomic>
#include <common/types.h>
#include <Common/CurrentMetrics.h>
#include <Common/SimpleActionBlocker.h>
#include <Common/VariableContext.h>
...
...
@@ -131,8 +130,18 @@ public:
/// Prints info about peak memory consumption into log.
void
logPeakMemoryUsage
()
const
;
/// To be able to temporarily stop memory tracker
DB
::
SimpleActionBlocker
blocker
;
/// To be able to temporarily stop memory tracking from current thread.
struct
BlockerInThread
{
private:
BlockerInThread
(
const
BlockerInThread
&
)
=
delete
;
BlockerInThread
&
operator
=
(
const
BlockerInThread
&
)
=
delete
;
static
thread_local
bool
is_blocked
;
public:
BlockerInThread
()
{
is_blocked
=
true
;
}
~
BlockerInThread
()
{
is_blocked
=
false
;
}
static
bool
isBlocked
()
{
return
is_blocked
;
}
};
};
extern
MemoryTracker
total_memory_tracker
;
...
...
@@ -145,7 +154,3 @@ namespace CurrentMemoryTracker
void
realloc
(
Int64
old_size
,
Int64
new_size
);
void
free
(
Int64
size
);
}
/// Holding this object will temporarily disable memory tracking.
DB
::
SimpleActionLock
getCurrentMemoryTrackerActionLock
();
src/Interpreters/SystemLog.h
浏览文件 @
2619efad
...
...
@@ -233,7 +233,7 @@ void SystemLog<LogElement>::add(const LogElement & element)
/// The size of allocation can be in order of a few megabytes.
/// But this should not be accounted for query memory usage.
/// Otherwise the tests like 01017_uniqCombined_memory_usage.sql will be flacky.
auto
temporarily_disable_memory_tracker
=
getCurrentMemoryTrackerActionLock
()
;
MemoryTracker
::
BlockerInThread
temporarily_disable_memory_tracker
;
/// Should not log messages under mutex.
bool
queue_is_half_full
=
false
;
...
...
src/Interpreters/executeQuery.cpp
浏览文件 @
2619efad
...
...
@@ -157,7 +157,7 @@ static void setExceptionStackTrace(QueryLogElement & elem)
{
/// Disable memory tracker for stack trace.
/// Because if exception is "Memory limit (for query) exceed", then we probably can't allocate another one string.
auto
temporarily_disable_memory_tracker
=
getCurrentMemoryTrackerActionLock
()
;
MemoryTracker
::
BlockerInThread
temporarily_disable_memory_tracker
;
try
{
...
...
src/Storages/MergeTree/IMergeTreeDataPart.cpp
浏览文件 @
2619efad
...
...
@@ -408,7 +408,7 @@ void IMergeTreeDataPart::loadColumnsChecksumsIndexes(bool require_columns_checks
/// Memory should not be limited during ATTACH TABLE query.
/// This is already true at the server startup but must be also ensured for manual table ATTACH.
/// Motivation: memory for index is shared between queries - not belong to the query itself.
auto
temporarily_disable_memory_tracker
=
getCurrentMemoryTrackerActionLock
()
;
MemoryTracker
::
BlockerInThread
temporarily_disable_memory_tracker
;
loadColumns
(
require_columns_checksums
);
loadChecksums
(
require_columns_checksums
);
...
...
src/Storages/MergeTree/MergeTreeDataPartWriterOnDisk.cpp
浏览文件 @
2619efad
...
...
@@ -212,7 +212,7 @@ void MergeTreeDataPartWriterOnDisk::calculateAndSerializePrimaryIndex(const Bloc
* And otherwise it will look like excessively growing memory consumption in context of query.
* (observed in long INSERT SELECTs)
*/
auto
temporarily_disable_memory_tracker
=
getCurrentMemoryTrackerActionLock
()
;
MemoryTracker
::
BlockerInThread
temporarily_disable_memory_tracker
;
/// Write index. The index contains Primary Key value for each `index_granularity` row.
...
...
src/Storages/MergeTree/MergeTreeMarksLoader.cpp
浏览文件 @
2619efad
...
...
@@ -48,7 +48,7 @@ const MarkInCompressedFile & MergeTreeMarksLoader::getMark(size_t row_index, siz
MarkCache
::
MappedPtr
MergeTreeMarksLoader
::
loadMarksImpl
()
{
/// Memory for marks must not be accounted as memory usage for query, because they are stored in shared cache.
auto
temporarily_disable_memory_tracker
=
getCurrentMemoryTrackerActionLock
()
;
MemoryTracker
::
BlockerInThread
temporarily_disable_memory_tracker
;
size_t
file_size
=
disk
->
getFileSize
(
mrk_path
);
size_t
mark_size
=
index_granularity_info
.
getMarkSizeInBytes
(
columns_in_mark
);
...
...
src/Storages/StorageBuffer.cpp
浏览文件 @
2619efad
...
...
@@ -315,7 +315,7 @@ static void appendBlock(const Block & from, Block & to)
size_t
old_rows
=
to
.
rows
();
auto
temporarily_disable_memory_tracker
=
getCurrentMemoryTrackerActionLock
()
;
MemoryTracker
::
BlockerInThread
temporarily_disable_memory_tracker
;
try
{
...
...
@@ -693,7 +693,7 @@ void StorageBuffer::writeBlockToDestination(const Block & block, StoragePtr tabl
}
auto
destination_metadata_snapshot
=
table
->
getInMemoryMetadataPtr
();
auto
temporarily_disable_memory_tracker
=
getCurrentMemoryTrackerActionLock
()
;
MemoryTracker
::
BlockerInThread
temporarily_disable_memory_tracker
;
auto
insert
=
std
::
make_shared
<
ASTInsertQuery
>
();
insert
->
table_id
=
destination_id
;
...
...
tests/queries/0_stateless/01529_bad_memory_tracking.reference
0 → 100644
浏览文件 @
2619efad
tests/queries/0_stateless/01529_bad_memory_tracking.sh
0 → 100755
浏览文件 @
2619efad
#!/usr/bin/env bash
CLICKHOUSE_CLIENT_SERVER_LOGS_LEVEL
=
fatal
CURDIR
=
$(
cd
"
$(
dirname
"
${
BASH_SOURCE
[0]
}
"
)
"
&&
pwd
)
.
"
$CURDIR
"
/../shell_config.sh
for
_
in
{
1..10
}
;
do
${
CLICKHOUSE_CLIENT
}
--max_memory_usage
'10G'
--query
"SELECT i FROM generateRandom('i Array(Int8)', 1, 1, 1048577) LIMIT 65536"
2>&1 |
grep
-v
-P
'^(Received exception from server|Code: 241)'
done
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录