Skip to content

[CH] Refactor off heap memory management, clean shuffle write code - #6558

Merged
liuneng1994 merged 2 commits into
apache:mainfrom
liuneng1994:new-memory-management
Jul 30, 2024
Merged

liuneng1994 merged 2 commits into
apache:mainfrom
liuneng1994:new-memory-management

Conversation

@liuneng1994

@liuneng1994 liuneng1994 commented Jul 23, 2024 •

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

将现有的通过spark管理内存并触发spill的机制改为由ClickHouse托管。shuffle spill触发由当前内存使用量和固定阈值决定。

修改原因:

  1. 统计不准确,代码运行过程中如果存在异步线程,容易出现内存使用量统计失真
  2. 无法统一管理各个算子的spill,目前已有sort ,aggregate,join,shuffle支持spill,spark task memory manager无法管理众多算子的spill触发
  3. shuffle通过spark触发spill容易出错,因为内存分配会出现在任何时候,但不是任何时机都可以触发spill,容易导致运行错误。难以根本解决。

image

在task线程初始化时创建对应的ThreadGroup,将task线程 attach至thread group,使得在task线程创建的pipeline executor也会加入到当前的thread group。通过thread Group的内存统计来控制整个task所需的offheap内存。
在task结束时,清理相应的thread group,并更新spark统计信息.

image

删除多余的配置项

  1. spark.gluten.sql.columnar.backend.ch.shuffle.preferSpill
    不再需要选择不同的模式,保留preferSpill=false的模式,对celeborn没有影响
  2. spark.gluten.sql.columnar.backend.ch.throwIfMemoryExceed
    offheap内存管理切换至clickhouse MemoryTracker,不在需要该配置
  3. spark.gluten.sql.columnar.backend.ch.flushBlockBufferBeforeEvict
    保留flushBlockBufferBeforeEvict=true的行为,移除配置项
  4. spark.gluten.sql.columnar.backend.ch.spillFirstlyBeforeStop
  5. spark.gluten.sql.columnar.backend.ch.forceExternalSortShuffle
    移除冗余的external sort shuffle 及配置

新增配置

  1. spark.gluten.sql.columnar.backend.ch.runtime_config.spill_mem_ratio=0.9
    shuffle spill触发控制,默认当前task内存使用量超过90%时触发
  2. spark.gluten.sql.columnar.backend.ch.runtime_config.extra_memory_hard_limit=0
    为task内存增加额外的hard limit限制,单位为byte,当task内存使用超过hardlimit时会产生异常。如果希望task能适度超过offheap配置可增大该配置项
    内存的hard_limit = soft_limit + extra_memory_hard_limit, soft_limit为spark配置的offheap size。

依然可以通过spark ui查看task的peak memory信息
image

新增performance counter 日志输出,方便对task进行性能分析。
image

How was this patch tested?

unit tests

(If this patch involves UI changes, please attach a screenshot; otherwise, remove this)

@github-actions

Copy link
Copy Markdown

Thanks for opening a pull request!

Could you open an issue for this pull request on Github Issues?

https://github.com/apache/incubator-gluten/issues

Then could you also rename commit message and pull request title in the following format?

[GLUTEN-${ISSUES_ID}][COMPONENT]feat/fix: ${detailed message}

See also:

@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI

@liuneng1994
liuneng1994 force-pushed the new-memory-management branch from 63f47c0 to b9dc0a0 Compare July 23, 2024 08:28
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI

@liuneng1994
liuneng1994 force-pushed the new-memory-management branch from b9dc0a0 to 2f69cba Compare July 23, 2024 08:57
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI

@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI

@liuneng1994
liuneng1994 force-pushed the new-memory-management branch from edc79e1 to 4ef241d Compare July 23, 2024 10:37
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI

@liuneng1994
liuneng1994 force-pushed the new-memory-management branch from 4ef241d to 04bf0cd Compare July 24, 2024 02:08
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI

@liuneng1994
liuneng1994 force-pushed the new-memory-management branch from 04bf0cd to c0bd70e Compare July 24, 2024 02:56
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI

2 similar comments
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI

@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI

@liuneng1994
liuneng1994 force-pushed the new-memory-management branch from bdf0116 to 04c8ec6 Compare July 24, 2024 10:05
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI

1 similar comment
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI

@liuneng1994
liuneng1994 force-pushed the new-memory-management branch from bc8b2d1 to a94d045 Compare July 25, 2024 03:25
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI

@liuneng1994
liuneng1994 force-pushed the new-memory-management branch from a94d045 to b117819 Compare July 25, 2024 07:23
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI

@liuneng1994
liuneng1994 force-pushed the new-memory-management branch from b117819 to 48653d8 Compare July 26, 2024 06:29
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI

@liuneng1994
liuneng1994 force-pushed the new-memory-management branch from 48653d8 to 32ceddd Compare July 26, 2024 08:04
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI

@liuneng1994
liuneng1994 force-pushed the new-memory-management branch from 32ceddd to a4f9ca6 Compare July 26, 2024 08:49
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI

@liuneng1994
liuneng1994 force-pushed the new-memory-management branch from a4f9ca6 to aabc77d Compare July 26, 2024 09:13
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI

@liuneng1994
liuneng1994 force-pushed the new-memory-management branch from aabc77d to afc5ba0 Compare July 26, 2024 10:18
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI

1 similar comment
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI

@liuneng1994
liuneng1994 force-pushed the new-memory-management branch from 77751d5 to 351142e Compare July 29, 2024 12:41
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI

@liuneng1994
liuneng1994 force-pushed the new-memory-management branch from 351142e to e1b197e Compare July 30, 2024 02:18
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI

@liuneng1994
liuneng1994 requested a review from lgbo-ustc July 30, 2024 06:40
};

std::unordered_map<int64_t, std::shared_ptr<QueryContext>> query_map;
std::mutex query_map_mutex;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个需要锁?

@liuneng1994 liuneng1994 Jul 30, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该需要,这里会有多个task并发初始化和释放的情况,在一些频繁调用的接口,例如currentMemoryUsage上是不需要锁的,总的来说应该不会造成影响。

if (logger->information())
{
throw DB::Exception(ErrorCodes::LOGICAL_ERROR, "allocator {} not found", allocator_id);
std::ostringstream msg;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ostringstream is not recommended in CH

@liuneng1994 liuneng1994 Jul 30, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里主要是需要利用到setw 和 setfill来实现一个输出内容的对齐,提高可读性。没有找到其他更好的方式

@liuneng1994
liuneng1994 merged commit e8dd172 into apache:main Jul 30, 2024
baibaichen added a commit to Kyligence/gluten that referenced this pull request Jul 30, 2024
baibaichen added a commit to Kyligence/gluten that referenced this pull request Jul 30, 2024
baibaichen added a commit that referenced this pull request Jul 30, 2024
* [GLUTEN-1632][CH]Daily Update Clickhouse Version (20240730)

* Fix build due to ClickHouse/ClickHouse#65414
* FIXME: very slow after #6558

---------

Co-authored-by: kyligence-git <[email protected]>
Co-authored-by: Chang Chen <[email protected]>
baibaichen added a commit to Kyligence/gluten that referenced this pull request Aug 3, 2024
baibaichen added a commit that referenced this pull request Aug 3, 2024
* [GLUTEN-1632][CH]Daily Update Clickhouse Version (20240803)

* fix build due to ClickHouse/ClickHouse#65751

* Fix build due to #6558

---------

Co-authored-by: kyligence-git <[email protected]>
Co-authored-by: Chang Chen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants