Allow manual compaction of rocksdb via OPTIMIZE query

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
This commit is contained in:
Azat Khuzhin 2023-11-01 21:42:34 +01:00
parent 7d4c97e8f3
commit 09c1e76982
4 changed files with 46 additions and 0 deletions

View File

@ -47,6 +47,7 @@ namespace ErrorCodes
extern const int LOGICAL_ERROR;
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
extern const int ROCKSDB_ERROR;
extern const int NOT_IMPLEMENTED;
}
using FieldVectorPtr = std::shared_ptr<FieldVector>;
@ -310,6 +311,36 @@ void StorageEmbeddedRocksDB::drop()
rocksdb_ptr = nullptr;
}
bool StorageEmbeddedRocksDB::optimize(
const ASTPtr & /*query*/,
const StorageMetadataPtr & /*metadata_snapshot*/,
const ASTPtr & partition,
bool final,
bool deduplicate,
const Names & /* deduplicate_by_columns */,
bool cleanup,
ContextPtr /*context*/)
{
if (partition)
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Partition cannot be specified when optimizing table of type EmbeddedRocksDB");
if (final)
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "FINAL cannot be specified when optimizing table of type EmbeddedRocksDB");
if (deduplicate)
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "DEDUPLICATE cannot be specified when optimizing table of type EmbeddedRocksDB");
if (cleanup)
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "CLEANUP cannot be specified when optimizing table of type EmbeddedRocksDB");
std::shared_lock lock(rocksdb_ptr_mx);
rocksdb::CompactRangeOptions compact_options;
auto status = rocksdb_ptr->CompactRange(compact_options, nullptr, nullptr);
if (!status.ok())
throw Exception(ErrorCodes::ROCKSDB_ERROR, "Compaction failed: {}", status.ToString());
return true;
}
void StorageEmbeddedRocksDB::initDB()
{
rocksdb::Status status;

View File

@ -55,6 +55,16 @@ public:
void mutate(const MutationCommands &, ContextPtr) override;
void drop() override;
bool optimize(
const ASTPtr & query,
const StorageMetadataPtr & metadata_snapshot,
const ASTPtr & partition,
bool final,
bool deduplicate,
const Names & deduplicate_by_columns,
bool cleanup,
ContextPtr context) override;
bool supportsParallelInsert() const override { return true; }
bool supportsIndexForIn() const override { return true; }
bool mayBenefitFromIndexForIn(

View File

@ -0,0 +1,5 @@
-- Tags: use-rocksdb
CREATE TABLE dict (key UInt64, value String) ENGINE = EmbeddedRocksDB PRIMARY KEY key;
INSERT INTO dict SELECT number, toString(number) FROM numbers(1e3);
OPTIMIZE TABLE dict;