Merge pull request #60139 from ClickHouse/revert-60093-lazy-primary-key-loading

Fix data race in `IMergeTreeDataPart`
This commit is contained in:
Alexey Milovidov 2024-02-20 06:44:28 +01:00 committed by GitHub
commit b216223aa0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 5 deletions

View File

@ -347,7 +347,7 @@ const IMergeTreeDataPart::Index & IMergeTreeDataPart::getIndex() const
{
std::scoped_lock lock(index_mutex);
if (!index_loaded)
loadIndex(lock);
loadIndex();
index_loaded = true;
return index;
}
@ -569,6 +569,7 @@ void IMergeTreeDataPart::removeIfNeeded()
UInt64 IMergeTreeDataPart::getIndexSizeInBytes() const
{
std::scoped_lock lock(index_mutex);
UInt64 res = 0;
for (const ColumnPtr & column : index)
res += column->byteSize();
@ -577,6 +578,7 @@ UInt64 IMergeTreeDataPart::getIndexSizeInBytes() const
UInt64 IMergeTreeDataPart::getIndexSizeInAllocatedBytes() const
{
std::scoped_lock lock(index_mutex);
UInt64 res = 0;
for (const ColumnPtr & column : index)
res += column->allocatedBytes();
@ -828,7 +830,7 @@ void IMergeTreeDataPart::appendFilesOfIndexGranularity(Strings & /* files */) co
{
}
void IMergeTreeDataPart::loadIndex(std::scoped_lock<std::mutex> &) const
void IMergeTreeDataPart::loadIndex() const
{
/// Memory for index must not be accounted as memory usage for query, because it belongs to a table.
MemoryTrackerBlockerInThread temporarily_disable_memory_tracker;

View File

@ -3,6 +3,7 @@
#include <IO/WriteSettings.h>
#include <Core/Block.h>
#include <base/types.h>
#include <base/defines.h>
#include <Core/NamesAndTypes.h>
#include <Storages/IStorage.h>
#include <Storages/LightweightDeleteDescription.h>
@ -565,8 +566,8 @@ protected:
/// Lazily loaded in RAM. Contains each index_granularity-th value of primary key tuple.
/// Note that marks (also correspond to primary key) are not always in RAM, but cached. See MarkCache.h.
mutable std::mutex index_mutex;
mutable Index index;
mutable bool index_loaded = false;
mutable Index index TSA_GUARDED_BY(index_mutex);
mutable bool index_loaded TSA_GUARDED_BY(index_mutex) = false;
/// Total size of all columns, calculated once in calcuateColumnSizesOnDisk
ColumnSize total_columns_size;
@ -664,7 +665,7 @@ private:
virtual void appendFilesOfIndexGranularity(Strings & files) const;
/// Loads the index file.
void loadIndex(std::scoped_lock<std::mutex> &) const;
void loadIndex() const TSA_REQUIRES(index_mutex);
void appendFilesOfIndex(Strings & files) const;