Signed-off-by: Lloyd-Pottiger <yan1579196623@gamil.com>
This commit is contained in:
Lloyd-Pottiger 2022-08-26 20:00:17 +08:00
parent 0d33327eda
commit 59dccd6e49
5 changed files with 46 additions and 23 deletions

View File

@ -21,7 +21,7 @@ CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
Engine parameters:
- `ttl` - time to live for values. TTL is accepted in seconds. If TTL is 0, regular RocksDB instance is used (without TTL).
- `rocksdb_dir` - path to the directory of an existed RocksDB. Open the table with the specified `rocksdb_dir`.
- `rocksdb_dir` - path to the directory of an existed RocksDB or the destination path of the created RocksDB. Open the table with the specified `rocksdb_dir`.
- `read_only` - when `read_only` is set to true, read-only mode is used. For storage with TTL, compaction will not be triggered (neither manual nor automatic), so no expired entries are removed.
- `primary_key_name` any column name in the column list.
- `primary key` must be specified, it supports only one column in the primary key. The primary key will be serialized in binary as a `rocksdb key`.

View File

@ -29,6 +29,7 @@
#include <cstddef>
#include <filesystem>
#include <shared_mutex>
#include <utility>
namespace fs = std::filesystem;
@ -172,18 +173,15 @@ StorageEmbeddedRocksDB::StorageEmbeddedRocksDB(const StorageID & table_id_,
: IStorage(table_id_)
, WithContext(context_->getGlobalContext())
, primary_key{primary_key_}
, rocksdb_dir(std::move(rocksdb_dir_))
, ttl(ttl_)
, read_only(read_only_)
{
setInMemoryMetadata(metadata_);
if (rocksdb_dir_.empty())
if (rocksdb_dir.empty())
{
rocksdb_dir = context_->getPath() + relative_data_path_;
}
else
{
rocksdb_dir = rocksdb_dir_;
}
if (!attach)
{
fs::create_directories(rocksdb_dir);
@ -397,7 +395,7 @@ static StoragePtr create(const StorageFactory::Arguments & args)
{
throw Exception("StorageEmbeddedRocksDB must require one column in primary key", ErrorCodes::BAD_ARGUMENTS);
}
return std::make_shared<StorageEmbeddedRocksDB>(args.table_id, args.relative_data_path, metadata, args.attach, args.getContext(), primary_key_names[0], ttl, rocksdb_dir, read_only);
return std::make_shared<StorageEmbeddedRocksDB>(args.table_id, args.relative_data_path, metadata, args.attach, args.getContext(), primary_key_names[0], ttl, std::move(rocksdb_dir), read_only);
}
std::shared_ptr<rocksdb::Statistics> StorageEmbeddedRocksDB::getRocksDBStatistics() const

View File

@ -54,10 +54,13 @@ def test_valid_options(start_cluster):
DROP TABLE test;
"""
)
def test_dirctory_missing_after_stop(start_cluster):
# for read_only = false
node.query(
"""
CREATE TABLE test (key UInt64, value String) Engine=EmbeddedRocksDB(0, '/var/lib/clickhouse/store/test_rocksdb_read_only') PRIMARY KEY(key);
INSERT INTO test (key, value) VALUES (0, 'a');
CREATE TABLE test (key UInt64, value String) Engine=EmbeddedRocksDB(0, '/var/lib/clickhouse/store/test_rocksdb_read_only_missing') PRIMARY KEY(key);
"""
)
node.stop_clickhouse()
@ -65,16 +68,40 @@ def test_valid_options(start_cluster):
[
"bash",
"-c",
"rm -r /var/lib/clickhouse/store/test_rocksdb_read_only",
"rm -r /var/lib/clickhouse/store/test_rocksdb_read_only_missing",
]
)
node.start_clickhouse()
result = node.query(
"""SELECT * FROM test;
"""INSERT INTO test (key, value) VALUES (0, 'a');
SELECT * FROM test;
"""
)
assert result.strip() == "0\ta"
result = node.query(
node.query(
"""DROP TABLE test;
"""
)
# for read_only = true
node.query(
"""
CREATE TABLE test (key UInt64, value String) Engine=EmbeddedRocksDB(0, '/var/lib/clickhouse/store/test_rocksdb_read_only_missing', 1) PRIMARY KEY(key);
"""
)
node.stop_clickhouse()
node.exec_in_container(
[
"bash",
"-c",
"rm -r /var/lib/clickhouse/store/test_rocksdb_read_only_missing",
]
)
node.start_clickhouse()
with pytest.raises(QueryRuntimeException):
node.query("""INSERT INTO test (key, value) VALUES (1, 'b');""")
result = node.query("""SELECT * FROM test;""")
assert result.strip() == ""
node.query(
"""DROP TABLE test;
"""
)

View File

@ -9,8 +9,7 @@ FAIL
OK
OK
----
3
FAIL
OK
----
FAIL
FAIL

17
tests/queries/0_stateless/02404_rocksdb_read_only.sh Normal file → Executable file
View File

@ -1,26 +1,25 @@
#!/usr/bin/env bash
# Tags: no-fasttest, no-parallel, no-replicated-database
# Tags: no-ordinary-database, no-fasttest
CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck source=../shell_config.sh
. "$CURDIR"/../shell_config.sh
ROCKSDB_DIR="/var/lib/clickhouse/store/test_rocksdb_read_only"
ROCKSDB_DIR="$CLICKHOUSE_TMP/test_rocksdb_read_only"
$CLICKHOUSE_CLIENT --query="DROP TABLE IF EXISTS test_02404;" 2>&1 | grep -F -q "OK" && echo 'OK' || echo 'FAIL';
$CLICKHOUSE_CLIENT --query="DROP TABLE IF EXISTS test_02404;" && echo 'OK' || echo 'FAIL';
echo "----"
$CLICKHOUSE_CLIENT --query="CREATE TABLE test_02404 (key UInt64, value String) Engine=EmbeddedRocksDB(0, '${ROCKSDB_DIR}', 1) PRIMARY KEY(key);" 2>&1 | grep -F -q "OK" && echo 'OK' || echo 'FAIL';
echo "----"
$CLICKHOUSE_CLIENT --query="CREATE TABLE test_02404 (key UInt64, value String) Engine=EmbeddedRocksDB(0, '${ROCKSDB_DIR}') PRIMARY KEY(key);" 2>&1 | grep -F -q "OK" && echo 'OK' || echo 'FAIL';
$CLICKHOUSE_CLIENT --query="INSERT INTO test_02404 (key, value) VALUES (0, 'a'), (1, 'b'), (3, 'c');" 2>&1 | grep -F -q "OK" && echo 'OK' || echo 'FAIL';
$CLICKHOUSE_CLIENT --query="CREATE TABLE test_02404 (key UInt64, value String) Engine=EmbeddedRocksDB(0, '${ROCKSDB_DIR}') PRIMARY KEY(key);" && echo 'OK' || echo 'FAIL';
$CLICKHOUSE_CLIENT --query="INSERT INTO test_02404 (key, value) VALUES (0, 'a'), (1, 'b'), (3, 'c');" && echo 'OK' || echo 'FAIL';
echo "----"
$CLICKHOUSE_CLIENT --query="CREATE TABLE test_02404_fail (key UInt64, value String) Engine=EmbeddedRocksDB(10, '${ROCKSDB_DIR}', 1) PRIMARY KEY(key);" 2>&1 | grep -F -q "OK" && echo 'OK' || echo 'FAIL';
$CLICKHOUSE_CLIENT --query="DROP TABLE test_02404;" 2>&1 | grep -F -q "OK" && echo 'OK' || echo 'FAIL';
$CLICKHOUSE_CLIENT --query="CREATE TABLE test_02404 (key UInt64, value String) Engine=EmbeddedRocksDB(10, '${ROCKSDB_DIR}', 1) PRIMARY KEY(key);" 2>&1 | grep -F -q "OK" && echo 'OK' || echo 'FAIL';
$CLICKHOUSE_CLIENT --query="DROP TABLE test_02404;" && echo 'OK' || echo 'FAIL';
$CLICKHOUSE_CLIENT --query="CREATE TABLE test_02404 (key UInt64, value String) Engine=EmbeddedRocksDB(10, '${ROCKSDB_DIR}', 1) PRIMARY KEY(key);" && echo 'OK' || echo 'FAIL';
echo "----"
$CLICKHOUSE_CLIENT --query="SELECT * FROM test_02404;" 2>&1 | grep -F "rows in set." | awk '{print $1}';
$CLICKHOUSE_CLIENT --query="INSERT INTO test_02404 (key, value) VALUES (4, 'd');" 2>&1 | grep -F -q "OK" && echo 'OK' || echo 'FAIL';
$CLICKHOUSE_CLIENT --query="DROP TABLE test_02404;" 2>&1 | grep -F -q "OK" && echo 'OK' || echo 'FAIL';
$CLICKHOUSE_CLIENT --query="DROP TABLE test_02404;" && echo 'OK' || echo 'FAIL';
echo "----"
rm -r ${ROCKSDB_DIR}
$CLICKHOUSE_CLIENT --query="CREATE TABLE test_02404 (key UInt64, value String) Engine=EmbeddedRocksDB(10, '${ROCKSDB_DIR}', 1) PRIMARY KEY(key);" 2>&1 | grep -F -q "OK" && echo 'OK' || echo 'FAIL';