ClickHouse/src/Storages/StorageMemory.cpp

321 lines
9.7 KiB
C++
Raw Normal View History

2020-11-23 17:45:59 +00:00
#include <cassert>
#include <Common/Exception.h>
2011-10-31 17:55:06 +00:00
#include <DataStreams/IBlockInputStream.h>
2015-01-18 08:25:56 +00:00
2020-09-22 09:23:46 +00:00
#include <Interpreters/MutationsInterpreter.h>
#include <Storages/StorageFactory.h>
2020-09-22 09:23:46 +00:00
#include <Storages/StorageMemory.h>
2011-10-31 17:55:06 +00:00
2018-06-05 19:46:49 +00:00
#include <IO/WriteHelpers.h>
#include <Processors/Sources/SourceWithProgress.h>
#include <Processors/Pipe.h>
2018-06-05 19:46:49 +00:00
2011-10-31 17:55:06 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
}
class MemorySource : public SourceWithProgress
2011-10-31 17:55:06 +00:00
{
2020-10-08 11:27:13 +00:00
using InitializerFunc = std::function<void(BlocksList::const_iterator &, size_t &, std::shared_ptr<const BlocksList> &)>;
2015-01-18 08:25:56 +00:00
public:
2020-08-07 13:02:28 +00:00
/// Blocks are stored in std::list which may be appended in another thread.
/// We use pointer to the beginning of the list and its current size.
/// We don't need synchronisation in this reader, because while we hold SharedLock on storage,
/// only new elements can be added to the back of the list, so our iterators remain valid
MemorySource(
Names column_names_,
2020-10-04 16:28:36 +00:00
BlocksList::const_iterator first_,
2020-08-25 17:54:44 +00:00
size_t num_blocks_,
const StorageMemory & storage,
const StorageMetadataPtr & metadata_snapshot,
2020-10-08 11:27:13 +00:00
std::shared_ptr<const BlocksList> data_,
InitializerFunc initializer_func_ = [](BlocksList::const_iterator &, size_t &, std::shared_ptr<const BlocksList> &) {})
2020-06-19 17:17:13 +00:00
: SourceWithProgress(metadata_snapshot->getSampleBlockForColumns(column_names_, storage.getVirtuals(), storage.getStorageID()))
, column_names(std::move(column_names_))
2020-08-25 17:54:44 +00:00
, current_it(first_)
, num_blocks(num_blocks_)
2020-10-08 11:27:13 +00:00
, data(data_)
, initializer_func(std::move(initializer_func_))
{
}
2011-10-31 17:55:06 +00:00
String getName() const override { return "Memory"; }
2011-10-31 17:55:06 +00:00
2015-01-18 08:25:56 +00:00
protected:
Chunk generate() override
{
if (!postponed_init_done)
2020-09-04 08:36:47 +00:00
{
2020-10-08 11:27:13 +00:00
initializer_func(current_it, num_blocks, data);
postponed_init_done = true;
2020-09-04 08:36:47 +00:00
}
if (current_block_idx == num_blocks)
return {};
const Block & src = *current_it;
Columns columns;
columns.reserve(column_names.size());
/// Add only required columns to `res`.
for (const auto & name : column_names)
columns.push_back(src.getByName(name).column);
2020-08-25 19:46:47 +00:00
if (++current_block_idx < num_blocks)
++current_it;
2020-08-25 19:46:47 +00:00
return Chunk(std::move(columns), src.rows());
}
2015-01-18 08:25:56 +00:00
private:
const Names column_names;
2020-10-04 16:28:36 +00:00
BlocksList::const_iterator current_it;
2020-09-04 08:36:47 +00:00
size_t num_blocks;
size_t current_block_idx = 0;
2020-09-04 08:36:47 +00:00
2020-10-08 11:27:13 +00:00
std::shared_ptr<const BlocksList> data;
bool postponed_init_done = false;
InitializerFunc initializer_func;
2015-01-18 08:25:56 +00:00
};
2011-10-31 17:55:06 +00:00
2015-01-18 08:25:56 +00:00
class MemoryBlockOutputStream : public IBlockOutputStream
2011-10-31 17:55:06 +00:00
{
2015-01-18 08:25:56 +00:00
public:
explicit MemoryBlockOutputStream(
StorageMemory & storage_,
const StorageMetadataPtr & metadata_snapshot_)
: storage(storage_)
, metadata_snapshot(metadata_snapshot_)
{}
Block getHeader() const override { return metadata_snapshot->getSampleBlock(); }
void write(const Block & block) override
{
const auto size_bytes_diff = block.allocatedBytes();
const auto size_rows_diff = block.rows();
2020-06-17 14:32:25 +00:00
metadata_snapshot->check(block, true);
{
std::lock_guard lock(storage.mutex);
2020-10-08 07:19:37 +00:00
auto new_data = std::make_unique<BlocksList>(*(storage.data.get()));
new_data->push_back(block);
storage.data.set(std::move(new_data));
storage.total_size_bytes.fetch_add(size_bytes_diff, std::memory_order_relaxed);
storage.total_size_rows.fetch_add(size_rows_diff, std::memory_order_relaxed);
}
}
2015-01-18 08:25:56 +00:00
private:
StorageMemory & storage;
StorageMetadataPtr metadata_snapshot;
2015-01-18 08:25:56 +00:00
};
2011-10-31 17:55:06 +00:00
2019-12-04 16:06:55 +00:00
StorageMemory::StorageMemory(const StorageID & table_id_, ColumnsDescription columns_description_, ConstraintsDescription constraints_)
2020-10-04 16:28:36 +00:00
: IStorage(table_id_), data(std::make_unique<const BlocksList>())
{
2020-06-19 15:39:41 +00:00
StorageInMemoryMetadata storage_metadata;
storage_metadata.setColumns(std::move(columns_description_));
storage_metadata.setConstraints(std::move(constraints_));
setInMemoryMetadata(storage_metadata);
}
2020-08-03 13:54:14 +00:00
Pipe StorageMemory::read(
const Names & column_names,
const StorageMetadataPtr & metadata_snapshot,
SelectQueryInfo & /*query_info*/,
const Context & /*context*/,
QueryProcessingStage::Enum /*processed_stage*/,
2017-12-01 21:13:25 +00:00
size_t /*max_block_size*/,
2017-06-02 15:54:39 +00:00
unsigned num_streams)
2011-10-31 17:55:06 +00:00
{
2020-06-19 17:17:13 +00:00
metadata_snapshot->check(column_names, getVirtuals(), getStorageID());
2012-05-30 04:45:49 +00:00
2020-09-04 08:36:47 +00:00
if (delay_read_for_global_subqueries)
{
/// Note: for global subquery we use single source.
/// Mainly, the reason is that at this point table is empty,
/// and we don't know the number of blocks are going to be inserted into it.
///
/// It may seem to be not optimal, but actually data from such table is used to fill
/// set for IN or hash table for JOIN, which can't be done concurrently.
/// Since no other manipulation with data is done, multiple sources shouldn't give any profit.
2020-10-08 07:19:37 +00:00
return Pipe(std::make_shared<MemorySource>(
column_names,
2020-10-08 11:15:11 +00:00
data.get()->end(),
2020-10-08 07:19:37 +00:00
0,
*this,
metadata_snapshot,
2020-10-08 11:27:13 +00:00
data.get(),
[this](BlocksList::const_iterator & current_it, size_t & num_blocks, std::shared_ptr<const BlocksList> & current_data)
2020-10-08 11:16:53 +00:00
{
2020-10-08 11:27:13 +00:00
current_data = data.get();
current_it = current_data->begin();
num_blocks = current_data->size();
2020-10-08 07:19:37 +00:00
}));
2020-09-04 08:36:47 +00:00
}
2020-10-08 11:15:11 +00:00
auto current_data = data.get();
2020-10-04 16:28:36 +00:00
size_t size = current_data->size();
2017-06-02 15:54:39 +00:00
if (num_streams > size)
num_streams = size;
2012-05-30 04:45:49 +00:00
Pipes pipes;
2012-05-30 04:45:49 +00:00
2020-10-08 07:19:37 +00:00
BlocksList::const_iterator it = current_data->begin();
2020-08-25 17:42:35 +00:00
size_t offset = 0;
2017-06-02 15:54:39 +00:00
for (size_t stream = 0; stream < num_streams; ++stream)
{
2020-08-25 19:46:47 +00:00
size_t next_offset = (stream + 1) * size / num_streams;
2020-08-25 17:54:44 +00:00
size_t num_blocks = next_offset - offset;
2020-08-25 17:42:35 +00:00
2020-08-25 17:54:44 +00:00
assert(num_blocks > 0);
2020-08-06 12:34:16 +00:00
2020-10-08 11:27:13 +00:00
pipes.emplace_back(std::make_shared<MemorySource>(column_names, it, num_blocks, *this, metadata_snapshot, current_data));
2020-08-25 17:42:35 +00:00
2020-08-25 17:54:44 +00:00
while (offset < next_offset)
{
++it;
++offset;
}
}
2020-08-06 12:24:05 +00:00
return Pipe::unitePipes(std::move(pipes));
2011-10-31 17:55:06 +00:00
}
BlockOutputStreamPtr StorageMemory::write(const ASTPtr & /*query*/, const StorageMetadataPtr & metadata_snapshot, const Context & /*context*/)
2011-10-31 17:55:06 +00:00
{
return std::make_shared<MemoryBlockOutputStream>(*this, metadata_snapshot);
2011-10-31 17:55:06 +00:00
}
2011-11-05 23:31:19 +00:00
2020-01-22 11:30:11 +00:00
void StorageMemory::drop()
2011-11-05 23:31:19 +00:00
{
2020-10-08 11:15:11 +00:00
data.set(std::make_unique<BlocksList>());
total_size_bytes.store(0, std::memory_order_relaxed);
total_size_rows.store(0, std::memory_order_relaxed);
2011-11-05 23:31:19 +00:00
}
2020-10-04 16:28:36 +00:00
static inline void updateBlockData(Block & old_block, const Block & new_block)
2020-09-22 09:23:46 +00:00
{
for (const auto & it : new_block)
{
auto col_name = it.name;
auto & col_with_type_name = old_block.getByName(col_name);
col_with_type_name.column = it.column;
}
}
void StorageMemory::mutate(const MutationCommands & commands, const Context & context)
{
2020-10-08 07:19:37 +00:00
std::lock_guard lock(mutex);
2020-09-22 09:29:57 +00:00
auto metadata_snapshot = getInMemoryMetadataPtr();
auto storage = getStorageID();
auto storage_ptr = DatabaseCatalog::instance().getTable(storage, context);
auto interpreter = std::make_unique<MutationsInterpreter>(storage_ptr, metadata_snapshot, commands, context, true);
2020-09-22 09:23:46 +00:00
auto in = interpreter->execute();
in->readPrefix();
BlocksList out;
Block block;
2020-09-22 09:29:57 +00:00
while ((block = in->read()))
2020-09-22 09:23:46 +00:00
{
out.push_back(block);
}
in->readSuffix();
2020-11-23 05:30:36 +00:00
std::unique_ptr<BlocksList> new_data;
2020-09-22 09:23:46 +00:00
// all column affected
if (interpreter->isAffectingAllColumns())
{
2020-11-23 05:30:36 +00:00
new_data = std::make_unique<BlocksList>(out);
2020-09-22 09:23:46 +00:00
}
else
{
2020-11-23 05:30:36 +00:00
/// just some of the column affected, we need update it with new column
new_data = std::make_unique<BlocksList>(*(data.get()));
2020-10-04 16:28:36 +00:00
auto data_it = new_data->begin();
2020-09-22 09:23:46 +00:00
auto out_it = out.begin();
2020-11-23 17:45:59 +00:00
while (data_it != new_data->end())
2020-09-22 09:23:46 +00:00
{
2020-11-23 17:45:59 +00:00
/// Mutation does not change the number of blocks
assert(out_it != out.end());
2020-10-04 16:28:36 +00:00
updateBlockData(*data_it, *out_it);
2020-09-22 09:23:46 +00:00
++data_it;
++out_it;
}
2020-11-23 17:45:59 +00:00
assert(out_it == out.end());
2020-09-22 09:23:46 +00:00
}
2020-11-23 05:30:36 +00:00
size_t rows = 0;
size_t bytes = 0;
for (const auto & buffer : *new_data)
{
rows += buffer.rows();
bytes += buffer.bytes();
}
total_size_bytes.store(rows, std::memory_order_relaxed);
total_size_rows.store(bytes, std::memory_order_relaxed);
data.set(std::move(new_data));
2020-09-22 09:23:46 +00:00
}
2020-06-18 10:29:13 +00:00
void StorageMemory::truncate(
2020-06-18 16:10:47 +00:00
const ASTPtr &, const StorageMetadataPtr &, const Context &, TableExclusiveLockHolder &)
2018-04-21 00:35:20 +00:00
{
2020-10-04 16:28:36 +00:00
data.set(std::make_unique<BlocksList>());
total_size_bytes.store(0, std::memory_order_relaxed);
total_size_rows.store(0, std::memory_order_relaxed);
2018-04-21 00:35:20 +00:00
}
std::optional<UInt64> StorageMemory::totalRows() const
{
/// All modifications of these counters are done under mutex which automatically guarantees synchronization/consistency
/// When run concurrently we are fine with any value: "before" or "after"
return total_size_rows.load(std::memory_order_relaxed);
}
std::optional<UInt64> StorageMemory::totalBytes() const
{
return total_size_bytes.load(std::memory_order_relaxed);
}
void registerStorageMemory(StorageFactory & factory)
{
factory.registerStorage("Memory", [](const StorageFactory::Arguments & args)
{
if (!args.engine_args.empty())
throw Exception(
"Engine " + args.engine_name + " doesn't support any arguments (" + toString(args.engine_args.size()) + " given)",
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
2019-12-04 16:06:55 +00:00
return StorageMemory::create(args.table_id, args.columns, args.constraints);
});
}
2011-10-31 17:55:06 +00:00
}