Skip to content
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

Do not use more then 98K of memory for uniqCombined* #7236

Merged
merged 1 commit into from Oct 9, 2019

Conversation

azat
Copy link
Collaborator

@azat azat commented Oct 8, 2019

For changelog. Remove if this is non-significant change.

Category (leave one):

  • Improvement

Short description (up to few sentences):
Do not use more then 98K of memory for uniqCombined*

Detailed description (optional):

uniqCombined() uses hashtable for medium cardinality, and since
HashTable resize by the power of 2 (well actually HashTableGrower grows
double by the power of 2, hence HashTableGrower::increaseSize() should
be overwritten to change this), with 1<<13 (default for uniqCombined)
and UInt64 HashValueType, the HashTable will takes:

  getBufferSizeInBytes() == 131072

While it should be not greater then sizeof(HLL) ~= 98K, so reduce the
maximum cardinality for hashtable to 1<<12 with UInt64 HashValueType and
to 1<13 with UInt32, overwrite HashTableGrower::increaseSize() and cover
this using max_memory_usage.

Refs: #7221 (comment)

@azat azat force-pushed the uniqCombined-memory-usage branch 2 times, most recently from f13c32d to d745e7f Compare October 8, 2019 22:21
@alexey-milovidov alexey-milovidov added can be tested pr-improvement Pull request with some product improvements labels Oct 8, 2019
16,
13,
12 + (sizeof(Key) == sizeof(UInt32)),
Copy link
Member

Choose a reason for hiding this comment

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

Here we have lowered max size degree by one when type is UInt64 and keep as before for UInt32. It's Ok.

Copy link
Contributor

@akuzm akuzm Oct 9, 2019

Choose a reason for hiding this comment

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

This + (sizeof(Key) == sizeof(UInt32)) looks totally puzzling. Why not just write it out:

template <size_t key_size> constexpr int hll_threshold = 12;
template <> constexpr int hll_threshold<sizeof(UInt32)> = 13; 

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Well it doesn't look puzzlingly to me, but I agree with you.
However, this bit is in two places, and writing this in your way will overly complicate the code
Although I don't have any strong feeling about this and can go your way

Copy link
Contributor

Choose a reason for hiding this comment

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

If I'm not missing anything, it should reduce duplication, you just write hll_threshold<sizeof(Key)> instead of 12 + sizeof....

Copy link
Member

Choose a reason for hiding this comment

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

@akuzm Yes, it will be more clear. I'd like if you will do this modification.

@azat azat force-pushed the uniqCombined-memory-usage branch from d745e7f to bf5945f Compare October 8, 2019 22:50
uniqCombined() uses hashtable for medium cardinality, and since
HashTable resize by the power of 2 (well actually HashTableGrower grows
double by the power of 2, hence HashTableGrower::increaseSize() should
be overwritten to change this), with 1<<13 (default for uniqCombined)
and UInt64 HashValueType, the HashTable will takes:

  getBufferSizeInBytes() == 131072

While it should be not greater then sizeof(HLL) ~= 98K, so reduce the
maximum cardinality for hashtable to 1<<12 with UInt64 HashValueType and
to 1<13 with UInt32, overwrite HashTableGrower::increaseSize() and cover
this using max_memory_usage.

Refs: ClickHouse#7221 (comment)

v2: cover uniqCombined() with non-default K
@azat azat force-pushed the uniqCombined-memory-usage branch from bf5945f to e373862 Compare October 8, 2019 23:39
@alexey-milovidov alexey-milovidov merged commit 81a9c81 into ClickHouse:master Oct 9, 2019
@azat azat deleted the uniqCombined-memory-usage branch October 9, 2019 21:44
@akuzm
Copy link
Contributor

akuzm commented Oct 10, 2019

Apparently some tests were broken in this PR, but we didn't notice because I broke the CI.
Now it's failing in master: https://clickhouse-test-reports.s3.yandex.net/0/81a9c81ba7fecd2747ecf1f5050f23e5d5ff771c/functional_stateless_tests_(release).html

@azat could you look into this? It's 01017_uniqCombined_memory_usage and 00212_shard_aggregate_function_uniq

@azat
Copy link
Collaborator Author

azat commented Oct 10, 2019

Sure, still take a look into this today

@azat
Copy link
Collaborator Author

azat commented Oct 10, 2019

01017_uniqCombined_memory_usage

Cannot reproduce the failure of this test, is there any chance on getting logs from the build on CI?

@akuzm
Copy link
Contributor

akuzm commented Oct 10, 2019

Do you mean the test log or the build log itself?

For tests, there are links to logs at the bottom of this test page:
https://clickhouse-test-reports.s3.yandex.net/0/0769f80f4ea2516cc301257af27352c27a93df6c/functional_stateless_tests_(release).html

In the list of checks, this is Details of 'Functional stateless tests (release)'.

The log of clickhouse-test:
https://clickhouse-test-reports.s3.yandex.net/0/0769f80f4ea2516cc301257af27352c27a93df6c/functional_stateless_tests_(release)/test_run.txt.out.log

The build logs and packages can be found here:
https://clickhouse-builds.s3.yandex.net/0/0769f80f4ea2516cc301257af27352c27a93df6c/report.html

In the list of checks, this is Details for 'ClickHouse build check'.

@akuzm
Copy link
Contributor

akuzm commented Oct 10, 2019

The list of checks for each commit in master can be seen if you press a cross or a tick to the right of the commit time at this page:
https://github.com/ClickHouse/ClickHouse/commits/master

@azat
Copy link
Collaborator Author

azat commented Oct 10, 2019

Thanks, got it!

@azat
Copy link
Collaborator Author

azat commented Oct 10, 2019

01017_uniqCombined_memory_usage

Hm, interesting client fails with "Attempt to read after eof" error first time (after server restart) while the server indeed reports "Memory limit (for query) exceeded", and I can reproduce it with packages from this build, but not in my local setup (and I'm not surprised)

And this is not related to my changes I guess, since every first error SELECT foo (syntax error) produces EOF error on the client.

Looking into this more and the problem is in address resolving, look:

And indeed:

2019.10.10 15:17:23.712268 [ 27 ] {} <Error> ServerErrorHandler: Code: 241, e.displayText() = DB::Exception: Memory limit (for query) exceeded: would use 4.16 MiB (attempt to allocate chunk of 4367069 bytes), maximum: 3.81 MiB, Stack trace:

0. 0x55c55645e300 StackTrace::StackTrace() /build/obj-x86_64-linux-gnu/../dbms/src/Common/StackTrace.cpp:228
1. 0x55c55645e0d5 DB::Exception::Exception(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int) /build/obj-x86_64-linux-gnu/../dbms/src/Common/Exception.h:27
2. 0x55c555facf43 MemoryTracker::alloc(long) [clone .cold] /usr/include/c++/9/bits/basic_string.h:222
3. 0x55c55645cf5c MemoryTracker::alloc(long) /build/obj-x86_64-linux-gnu/../dbms/src/Common/MemoryTracker.cpp:134
4. 0x55c556404111 operator new(unsigned long) /build/obj-x86_64-linux-gnu/../libs/libcommon/include/common/memory.h:44
5. 0x55c5564647b6 void std::vector<DB::SymbolIndex::Symbol, std::allocator<DB::SymbolIndex::Symbol> >::_M_realloc_insert<DB::SymbolIndex::Symbol>(__gnu_cxx::__normal_iterator<DB::SymbolIndex::Symbol*, std::vector<DB::SymbolIndex::Symbol, std::allocator<DB::SymbolIndex::Symb
6. 0x55c5564630cd DB::(anonymous namespace)::collectSymbols(dl_phdr_info*, unsigned long, void*) /usr/include/c++/9/bits/vector.tcc:121
7. 0x55c55b98da47 dl_iterate_phdr /build/obj-x86_64-linux-gnu/../libs/libcommon/src/phdr_cache.cpp:84
8. 0x55c5564643cd DB::SymbolIndex::update() /usr/include/c++/9/bits/stl_iterator.h:807
9. 0x55c5564646ad DB::SymbolIndex::instance() /build/obj-x86_64-linux-gnu/../dbms/src/Common/SymbolIndex.cpp:366
10. 0x55c55645ed8d toStringEveryLineImpl(std::array<void*, 32ul> const&, unsigned long, unsigned long, std::function<void (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)>) /usr/include/c++/9/bits/basic_ios.h:462
11. 0x55c55645fccc std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > SimpleCache<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > (std::array<void*, 32ul> const&, unsigned long, unsigned long), &(toStringImpl(std:
12. 0x55c556460191 StackTrace::toString[abi:cxx11]() const /build/obj-x86_64-linux-gnu/../dbms/src/Common/StackTrace.cpp:327
13. 0x55c5561b4b43 DB::setExceptionStackTrace(DB::QueryLogElement&) /build/obj-x86_64-linux-gnu/../dbms/src/Interpreters/executeQuery.cpp:129
14. 0x55c5561b504e DB::executeQueryImpl(char const*, char const*, DB::Context&, bool, DB::QueryProcessingStage::Enum, bool, DB::ReadBuffer*, bool)::{lambda()#3}::operator()() [clone .cold] /build/obj-x86_64-linux-gnu/../dbms/src/Interpreters/executeQuery.cpp:508
15. 0x55c555fc0e96 DB::TCPHandler::runImpl() [clone .cold] /usr/include/c++/9/bits/std_function.h:690
16. 0x55c5564e4b7b DB::TCPHandler::run() /build/obj-x86_64-linux-gnu/../dbms/programs/server/TCPHandler.cpp:1225
17. 0x55c55a350ac0 Poco::Net::TCPServerConnection::start() /build/obj-x86_64-linux-gnu/../contrib/poco/Net/src/TCPServerConnection.cpp:57
18. 0x55c55a3511dd Poco::Net::TCPServerDispatcher::run() /usr/include/c++/9/bits/atomic_base.h:327
19. 0x55c55ba273b1 Poco::PooledThread::run() /usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:748
20. 0x55c55ba2515c Poco::ThreadImpl::runnableEntry(void*) /build/obj-x86_64-linux-gnu/../contrib/poco/Foundation/include/Poco/AutoPtr.h:205
21. 0x55c55c196ce0 execute_native_thread_routine /usr/lib/debug/usr/bin/clickhouse
22. 0x7f0c3877f6db start_thread /lib/x86_64-linux-gnu/libpthread-2.27.so
23. 0x7f0c3809c88f clone /lib/x86_64-linux-gnu/libc-2.27.so

Will submit patch set shortly (that will have details).

azat added a commit to azat/ClickHouse that referenced this pull request Oct 10, 2019
…ined(12)(String)

In e373862 the maxium size for HashTable in
CombinedCardinalityEstimator had been reduced for power of 2 for String
(since otherwise the size of the hashtable becames bigger then the
sizeof HLL).

Refs: ClickHouse#7236 (comment)
Fixes: e373862 ("Do not use more then 98K of memory for uniqCombined*")
@azat
Copy link
Collaborator Author

azat commented Oct 10, 2019

@akuzm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-improvement Pull request with some product improvements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants