2016-10-25 06:49:24 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2018-05-09 04:22:30 +00:00
|
|
|
#include <errno.h>
|
2016-10-25 06:49:24 +00:00
|
|
|
|
2015-08-16 07:01:41 +00:00
|
|
|
#include <map>
|
2017-11-20 04:15:43 +00:00
|
|
|
#include <optional>
|
2015-08-16 07:01:41 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/escapeForFileName.h>
|
2015-08-16 07:01:41 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Exception.h>
|
2015-08-16 07:01:41 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadBufferFromFile.h>
|
|
|
|
#include <IO/WriteBufferFromFile.h>
|
|
|
|
#include <IO/CompressedReadBufferFromFile.h>
|
|
|
|
#include <IO/CompressedWriteBuffer.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2015-08-16 07:01:41 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataStreams/IProfilingBlockInputStream.h>
|
|
|
|
#include <DataStreams/IBlockOutputStream.h>
|
|
|
|
#include <DataStreams/NativeBlockInputStream.h>
|
|
|
|
#include <DataStreams/NativeBlockOutputStream.h>
|
|
|
|
#include <DataStreams/NullBlockInputStream.h>
|
2015-08-16 07:01:41 +00:00
|
|
|
|
2018-02-18 03:23:48 +00:00
|
|
|
#include <DataTypes/DataTypeFactory.h>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnArray.h>
|
2015-08-16 07:01:41 +00:00
|
|
|
|
2017-05-24 21:06:29 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2017-01-21 04:24:28 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Storages/StorageStripeLog.h>
|
2017-12-30 00:36:06 +00:00
|
|
|
#include <Storages/StorageFactory.h>
|
2018-04-21 00:35:20 +00:00
|
|
|
#include <Poco/DirectoryIterator.h>
|
2015-08-16 07:01:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
#define INDEX_BUFFER_SIZE 4096
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int EMPTY_LIST_OF_COLUMNS_PASSED;
|
|
|
|
extern const int CANNOT_CREATE_DIRECTORY;
|
2017-12-30 00:36:06 +00:00
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
2017-11-03 19:53:10 +00:00
|
|
|
extern const int INCORRECT_FILE_NAME;
|
2018-06-09 15:48:22 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-16 07:01:41 +00:00
|
|
|
|
2017-09-08 04:58:57 +00:00
|
|
|
class StripeLogBlockInputStream final : public IProfilingBlockInputStream
|
2015-08-16 07:01:41 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-12-01 21:13:25 +00:00
|
|
|
StripeLogBlockInputStream(StorageStripeLog & storage_, size_t max_read_buffer_size_,
|
2017-04-01 07:20:54 +00:00
|
|
|
std::shared_ptr<const IndexForNativeFormat> & index_,
|
|
|
|
IndexForNativeFormat::Blocks::const_iterator index_begin_,
|
|
|
|
IndexForNativeFormat::Blocks::const_iterator index_end_)
|
|
|
|
: storage(storage_), max_read_buffer_size(max_read_buffer_size_),
|
|
|
|
index(index_), index_begin(index_begin_), index_end(index_end_)
|
|
|
|
{
|
2018-02-21 04:38:26 +00:00
|
|
|
if (index_begin != index_end)
|
|
|
|
{
|
|
|
|
for (const auto & column : index_begin->columns)
|
|
|
|
{
|
|
|
|
auto type = DataTypeFactory::instance().get(column.type);
|
|
|
|
header.insert(ColumnWithTypeAndName{ type, column.name });
|
|
|
|
}
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override { return "StripeLog"; }
|
|
|
|
|
2018-02-18 03:23:48 +00:00
|
|
|
Block getHeader() const override
|
2018-01-09 01:51:08 +00:00
|
|
|
{
|
2018-02-18 03:23:48 +00:00
|
|
|
return header;
|
2018-01-09 01:51:08 +00:00
|
|
|
};
|
2018-01-06 18:10:44 +00:00
|
|
|
|
2015-08-16 07:01:41 +00:00
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
Block readImpl() override
|
|
|
|
{
|
|
|
|
Block res;
|
2018-01-09 01:51:08 +00:00
|
|
|
start();
|
2015-12-16 02:32:49 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (block_in)
|
|
|
|
{
|
|
|
|
res = block_in->read();
|
2015-10-01 03:30:50 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Freeing memory before destroying the object.
|
|
|
|
if (!res)
|
|
|
|
{
|
2017-11-20 04:41:56 +00:00
|
|
|
block_in.reset();
|
|
|
|
data_in.reset();
|
2017-04-01 07:20:54 +00:00
|
|
|
index.reset();
|
|
|
|
}
|
|
|
|
}
|
2015-10-01 03:30:50 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
|
|
|
}
|
2015-08-16 07:01:41 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
StorageStripeLog & storage;
|
|
|
|
size_t max_read_buffer_size;
|
|
|
|
|
|
|
|
std::shared_ptr<const IndexForNativeFormat> index;
|
|
|
|
IndexForNativeFormat::Blocks::const_iterator index_begin;
|
|
|
|
IndexForNativeFormat::Blocks::const_iterator index_end;
|
2018-02-21 04:38:26 +00:00
|
|
|
Block header;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/** optional - to create objects only on first reading
|
|
|
|
* and delete objects (release buffers) after the source is exhausted
|
|
|
|
* - to save RAM when using a large number of sources.
|
|
|
|
*/
|
|
|
|
bool started = false;
|
2017-11-20 04:15:43 +00:00
|
|
|
std::optional<CompressedReadBufferFromFile> data_in;
|
|
|
|
std::optional<NativeBlockInputStream> block_in;
|
2018-01-09 01:51:08 +00:00
|
|
|
|
|
|
|
void start()
|
|
|
|
{
|
|
|
|
if (!started)
|
|
|
|
{
|
|
|
|
started = true;
|
|
|
|
|
|
|
|
data_in.emplace(
|
|
|
|
storage.full_path() + "data.bin", 0, 0,
|
|
|
|
std::min(static_cast<Poco::File::FileSize>(max_read_buffer_size), Poco::File(storage.full_path() + "data.bin").getSize()));
|
|
|
|
|
2018-02-18 02:46:39 +00:00
|
|
|
block_in.emplace(*data_in, 0, index_begin, index_end);
|
2018-01-09 01:51:08 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-16 07:01:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-09-08 04:58:57 +00:00
|
|
|
class StripeLogBlockOutputStream final : public IBlockOutputStream
|
2015-08-16 07:01:41 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-09-07 21:04:48 +00:00
|
|
|
explicit StripeLogBlockOutputStream(StorageStripeLog & storage_)
|
2017-04-01 07:20:54 +00:00
|
|
|
: storage(storage_), lock(storage.rwlock),
|
|
|
|
data_out_compressed(storage.full_path() + "data.bin", DBMS_DEFAULT_BUFFER_SIZE, O_WRONLY | O_APPEND | O_CREAT),
|
2017-10-13 01:02:16 +00:00
|
|
|
data_out(data_out_compressed, CompressionSettings(CompressionMethod::LZ4), storage.max_compress_block_size),
|
2017-04-01 07:20:54 +00:00
|
|
|
index_out_compressed(storage.full_path() + "index.mrk", INDEX_BUFFER_SIZE, O_WRONLY | O_APPEND | O_CREAT),
|
|
|
|
index_out(index_out_compressed),
|
2018-02-19 00:45:32 +00:00
|
|
|
block_out(data_out, 0, storage.getSampleBlock(), &index_out, Poco::File(storage.full_path() + "data.bin").getSize())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~StripeLogBlockOutputStream()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
writeSuffix();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-19 00:45:32 +00:00
|
|
|
Block getHeader() const override { return storage.getSampleBlock(); }
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void write(const Block & block) override
|
|
|
|
{
|
|
|
|
block_out.write(block);
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeSuffix() override
|
|
|
|
{
|
|
|
|
if (done)
|
|
|
|
return;
|
|
|
|
|
|
|
|
block_out.writeSuffix();
|
|
|
|
data_out.next();
|
|
|
|
data_out_compressed.next();
|
|
|
|
index_out.next();
|
|
|
|
index_out_compressed.next();
|
|
|
|
|
|
|
|
FileChecker::Files files{ data_out_compressed.getFileName(), index_out_compressed.getFileName() };
|
|
|
|
storage.file_checker.update(files.begin(), files.end());
|
|
|
|
|
|
|
|
done = true;
|
|
|
|
}
|
2015-08-16 07:01:41 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
StorageStripeLog & storage;
|
2017-07-28 17:34:02 +00:00
|
|
|
std::unique_lock<std::shared_mutex> lock;
|
2015-08-16 07:01:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
WriteBufferFromFile data_out_compressed;
|
|
|
|
CompressedWriteBuffer data_out;
|
|
|
|
WriteBufferFromFile index_out_compressed;
|
|
|
|
CompressedWriteBuffer index_out;
|
|
|
|
NativeBlockOutputStream block_out;
|
2015-08-16 07:01:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
bool done = false;
|
2015-08-16 07:01:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
StorageStripeLog::StorageStripeLog(
|
2017-04-01 07:20:54 +00:00
|
|
|
const std::string & path_,
|
|
|
|
const std::string & name_,
|
2018-03-06 20:18:34 +00:00
|
|
|
const ColumnsDescription & columns_,
|
2017-04-01 07:20:54 +00:00
|
|
|
bool attach,
|
|
|
|
size_t max_compress_block_size_)
|
2018-03-06 20:18:34 +00:00
|
|
|
: IStorage{columns_},
|
2018-01-25 14:42:39 +00:00
|
|
|
path(path_), name(name_),
|
2017-04-01 07:20:54 +00:00
|
|
|
max_compress_block_size(max_compress_block_size_),
|
|
|
|
file_checker(path + escapeForFileName(name) + '/' + "sizes.json"),
|
|
|
|
log(&Logger::get("StorageStripeLog"))
|
2015-08-16 07:01:41 +00:00
|
|
|
{
|
2017-11-03 19:53:10 +00:00
|
|
|
if (path.empty())
|
|
|
|
throw Exception("Storage " + getName() + " requires data path", ErrorCodes::INCORRECT_FILE_NAME);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
String full_path = path + escapeForFileName(name) + '/';
|
|
|
|
if (!attach)
|
|
|
|
{
|
|
|
|
/// create files if they do not exist
|
|
|
|
if (0 != mkdir(full_path.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) && errno != EEXIST)
|
|
|
|
throwFromErrno("Cannot create directory " + full_path, ErrorCodes::CANNOT_CREATE_DIRECTORY);
|
|
|
|
}
|
2015-08-16 07:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-01 21:13:25 +00:00
|
|
|
void StorageStripeLog::rename(const String & new_path_to_db, const String & /*new_database_name*/, const String & new_table_name)
|
2015-08-16 07:01:41 +00:00
|
|
|
{
|
2017-07-28 17:34:02 +00:00
|
|
|
std::unique_lock<std::shared_mutex> lock(rwlock);
|
2015-08-16 08:18:34 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Rename directory with data.
|
|
|
|
Poco::File(path + escapeForFileName(name)).renameTo(new_path_to_db + escapeForFileName(new_table_name));
|
2015-08-16 07:01:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
path = new_path_to_db;
|
|
|
|
name = new_table_name;
|
|
|
|
file_checker.setPath(path + escapeForFileName(name) + "/" + "sizes.json");
|
2015-08-16 07:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BlockInputStreams StorageStripeLog::read(
|
2017-04-01 07:20:54 +00:00
|
|
|
const Names & column_names,
|
2017-12-01 21:13:25 +00:00
|
|
|
const SelectQueryInfo & /*query_info*/,
|
2017-04-01 07:20:54 +00:00
|
|
|
const Context & context,
|
|
|
|
QueryProcessingStage::Enum & processed_stage,
|
2017-12-01 21:13:25 +00:00
|
|
|
const size_t /*max_block_size*/,
|
2017-06-02 15:54:39 +00:00
|
|
|
unsigned num_streams)
|
2015-08-16 07:01:41 +00:00
|
|
|
{
|
2017-07-28 17:34:02 +00:00
|
|
|
std::shared_lock<std::shared_mutex> lock(rwlock);
|
2015-08-16 08:18:34 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
check(column_names);
|
|
|
|
processed_stage = QueryProcessingStage::FetchColumns;
|
2015-08-16 08:18:34 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
NameSet column_names_set(column_names.begin(), column_names.end());
|
2015-08-16 08:18:34 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!Poco::File(full_path() + "index.mrk").exists())
|
2018-01-09 02:09:08 +00:00
|
|
|
return { std::make_shared<NullBlockInputStream>(getSampleBlockForColumns(column_names)) };
|
2015-12-08 20:04:11 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
CompressedReadBufferFromFile index_in(full_path() + "index.mrk", 0, 0, INDEX_BUFFER_SIZE);
|
|
|
|
std::shared_ptr<const IndexForNativeFormat> index{std::make_shared<IndexForNativeFormat>(index_in, column_names_set)};
|
2015-08-16 08:18:34 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
BlockInputStreams res;
|
2015-08-16 08:18:34 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t size = index->blocks.size();
|
2017-06-02 15:54:39 +00:00
|
|
|
if (num_streams > size)
|
|
|
|
num_streams = size;
|
2015-08-16 08:18:34 +00:00
|
|
|
|
2017-06-02 15:54:39 +00:00
|
|
|
for (size_t stream = 0; stream < num_streams; ++stream)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
IndexForNativeFormat::Blocks::const_iterator begin = index->blocks.begin();
|
|
|
|
IndexForNativeFormat::Blocks::const_iterator end = index->blocks.begin();
|
2015-08-16 08:18:34 +00:00
|
|
|
|
2017-06-02 15:54:39 +00:00
|
|
|
std::advance(begin, stream * size / num_streams);
|
|
|
|
std::advance(end, (stream + 1) * size / num_streams);
|
2015-08-16 08:18:34 +00:00
|
|
|
|
2017-05-24 21:06:29 +00:00
|
|
|
res.emplace_back(std::make_shared<StripeLogBlockInputStream>(
|
2017-12-01 21:13:25 +00:00
|
|
|
*this, context.getSettingsRef().max_read_buffer_size, index, begin, end));
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2015-08-16 08:18:34 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// We do not keep read lock directly at the time of reading, because we read ranges of data that do not change.
|
2015-08-16 08:18:34 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2015-08-16 07:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BlockOutputStreamPtr StorageStripeLog::write(
|
2017-12-01 21:13:25 +00:00
|
|
|
const ASTPtr & /*query*/, const Settings & /*settings*/)
|
2015-08-16 07:01:41 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_shared<StripeLogBlockOutputStream>(*this);
|
2015-08-16 07:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool StorageStripeLog::checkData() const
|
|
|
|
{
|
2017-07-28 17:34:02 +00:00
|
|
|
std::shared_lock<std::shared_mutex> lock(rwlock);
|
2017-04-01 07:20:54 +00:00
|
|
|
return file_checker.check();
|
2015-08-16 07:01:41 +00:00
|
|
|
}
|
|
|
|
|
2018-06-09 15:48:22 +00:00
|
|
|
void StorageStripeLog::truncate(const ASTPtr &)
|
2018-04-21 00:35:20 +00:00
|
|
|
{
|
2018-06-09 15:48:22 +00:00
|
|
|
if (name.empty())
|
|
|
|
throw Exception("Logical error: table name is empty", ErrorCodes::LOGICAL_ERROR);
|
2018-04-21 00:35:20 +00:00
|
|
|
|
2018-06-09 15:48:22 +00:00
|
|
|
std::shared_lock<std::shared_mutex> lock(rwlock);
|
2018-04-21 00:35:20 +00:00
|
|
|
|
2018-06-09 16:09:37 +00:00
|
|
|
auto file = Poco::File(path + escapeForFileName(name));
|
|
|
|
file.remove(true);
|
|
|
|
file.createDirectories();
|
|
|
|
|
2018-06-09 15:48:22 +00:00
|
|
|
file_checker = FileChecker{path + escapeForFileName(name) + '/' + "sizes.json"};
|
2018-04-21 00:35:20 +00:00
|
|
|
}
|
|
|
|
|
2017-12-30 00:36:06 +00:00
|
|
|
|
|
|
|
void registerStorageStripeLog(StorageFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerStorage("StripeLog", [](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);
|
|
|
|
|
|
|
|
return StorageStripeLog::create(
|
|
|
|
args.data_path, args.table_name, args.columns,
|
2017-12-30 03:49:02 +00:00
|
|
|
args.attach, args.context.getSettings().max_compress_block_size);
|
2017-12-30 00:36:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-08-16 07:01:41 +00:00
|
|
|
}
|