Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support DISK Command #882

Merged
merged 50 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
23a6730
Support DISK Command
tanruixiang Sep 17, 2022
4605365
fix lint error
tanruixiang Sep 17, 2022
ddc683a
fix lint error
tanruixiang Sep 17, 2022
53e3735
fix lint error
tanruixiang Sep 17, 2022
039333f
Compact code
tanruixiang Sep 17, 2022
eb6b4b4
Reduce test set size to fit github machine
tanruixiang Sep 17, 2022
08313a5
fix lint error
tanruixiang Sep 17, 2022
fe25463
fix typo
tanruixiang Sep 17, 2022
5b57472
Reduce test set size to fit github machine
tanruixiang Sep 17, 2022
5aed18a
delete this->
tanruixiang Sep 17, 2022
0df43a5
change indentation
tanruixiang Sep 17, 2022
4f4ec9a
Reduce test set size to fit github machine
tanruixiang Sep 17, 2022
b874856
Compact code
tanruixiang Sep 17, 2022
56b9e98
fix typo
tanruixiang Sep 17, 2022
63612c0
fix typo
tanruixiang Sep 17, 2022
e0f9504
fix missing whitespace
tanruixiang Sep 17, 2022
cbbb9b4
Reduce test cases that take up less space
tanruixiang Sep 18, 2022
6b1567c
Merge branch 'unstable' into support_disk_command
tanruixiang Sep 18, 2022
666dcaa
Reduce redundant code
tanruixiang Sep 18, 2022
54def1b
Merge branch 'support_disk_command' of github.com:tanruixiang/incubat…
tanruixiang Sep 18, 2022
cd533f2
add gocase test
tanruixiang Sep 18, 2022
7c800fa
Merge branch 'unstable' into support_disk_command
tanruixiang Sep 18, 2022
8b13e15
add commands number because of unstable branch's change
tanruixiang Sep 18, 2022
f6e25f1
Merge branch 'support_disk_command' of github.com:tanruixiang/incubat…
tanruixiang Sep 18, 2022
474dc0c
adjust cpp ut test
tanruixiang Sep 18, 2022
3d13b17
Merge remote-tracking branch 'origin_kv/unstable' into support_disk_c…
tanruixiang Sep 20, 2022
324f110
add sync=true
tanruixiang Sep 20, 2022
4909af1
update command number
tanruixiang Sep 20, 2022
616e236
Merge branch 'unstable' into support_disk_command
git-hulk Sep 21, 2022
85cddbc
test compression=no
tanruixiang Sep 21, 2022
01a0a88
Merge branch 'support_disk_command' of github.com:tanruixiang/incubat…
tanruixiang Sep 21, 2022
05f3360
Merge branch 'unstable' into support_disk_command
tanruixiang Sep 21, 2022
17455d8
update command number
tanruixiang Sep 21, 2022
bf05baa
Merge branch 'support_disk_command' of github.com:tanruixiang/incubat…
tanruixiang Sep 21, 2022
db735b1
adjust cpp test config
tanruixiang Sep 21, 2022
1b4cea1
adjust test cases
tanruixiang Sep 27, 2022
6172305
adjust test cases
tanruixiang Sep 27, 2022
29d0acd
Merge branch 'unstable' into support_disk_command
tanruixiang Sep 27, 2022
8adabac
add stream
tanruixiang Sep 29, 2022
6747559
Merge branch 'support_disk_command' of github.com:tanruixiang/incubat…
tanruixiang Sep 29, 2022
15a5289
Merge branch 'unstable' into support_disk_command
tanruixiang Sep 29, 2022
b1b8932
Merge branch 'unstable' into support_disk_command
tanruixiang Sep 30, 2022
21f18fe
adjust input size
tanruixiang Sep 30, 2022
29ad3ad
adjust input size
tanruixiang Sep 30, 2022
79ea7ee
Modified based on review comments
tanruixiang Oct 1, 2022
0ebc36e
adjust ut(use range check)
tanruixiang Oct 3, 2022
c16f6bc
Merge branch 'unstable' into support_disk_command
tanruixiang Oct 7, 2022
6710be2
Merge branch 'unstable' into support_disk_command
tanruixiang Oct 7, 2022
fef56b7
delete useless check
tanruixiang Oct 8, 2022
68b5543
Merge branch 'unstable' into support_disk_command
git-hulk Oct 11, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/redis_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "redis_hash.h"
#include "redis_bitmap.h"
#include "redis_list.h"
#include "redis_disk.h"
#include "redis_reply.h"
#include "redis_request.h"
#include "redis_connection.h"
Expand Down Expand Up @@ -3371,6 +3372,58 @@ class CommandInfo : public Commander {
}
};

class CommandDisk : public Commander {
public:
Status Parse(const std::vector<std::string> &args) override {
std::string opname = Util::ToLower(args[1]);
if (opname != "usage")
return Status(Status::RedisInvalidCmd, "Unknown operation");
if (args.size() != 3)
return Status(Status::RedisInvalidCmd, "Incorrect number of parameters");
git-hulk marked this conversation as resolved.
Show resolved Hide resolved
return Commander::Parse(args);
}

Status Execute(Server *svr, Connection *conn, std::string *output) override {
RedisType type;
Redis::Disk disk_db(svr->storage_, conn->GetNamespace());
rocksdb::Status s = disk_db.Type(args_[2], &type);
if (!s.ok())return Status(Status::RedisExecErr, s.ToString());

uint64_t result = 0;
switch (type) {
case RedisType::kRedisString:
disk_db.GetStringSize(args_[2], &result);
break;
case RedisType::kRedisHash:
disk_db.GetHashSize(args_[2], &result);
break;
case RedisType::kRedisBitmap:
disk_db.GetBitmapSize(args_[2], &result);
break;
case RedisType::kRedisList:
disk_db.GetListSize(args_[2], &result);
break;
case RedisType::kRedisSet:
disk_db.GetSetSize(args_[2], &result);
break;
case RedisType::kRedisSortedint:
disk_db.GetSortedintSize(args_[2], &result);
break;
case RedisType::kRedisZSet:
disk_db.GetZsetSize(args_[2], &result);
break;
case RedisType::kRedisNone:
return Status(Status::NotFound, " Not found " + args_[2]);
break;
case RedisType::kRedisStream:
return Status(Status::RedisInvalidCmd, "not support stream");
break;
}
*output = Redis::Integer(result);
return Status::OK();
}
};

class CommandRole : public Commander {
public:
Status Execute(Server *svr, Connection *conn, std::string *output) override {
Expand Down Expand Up @@ -5680,6 +5733,7 @@ CommandAttributes redisCommandTable[] = {
ADD_CMD("debug", -2, "read-only exclusive", 0, 0, 0, CommandDebug),
ADD_CMD("command", -1, "read-only", 0, 0, 0, CommandCommand),
ADD_CMD("echo", 2, "read-only", 0, 0, 0, CommandEcho),
ADD_CMD("disk", 3, "read-only", 0, 0, 0, CommandDisk),

ADD_CMD("ttl", 2, "read-only", 1, 1, 1, CommandTTL),
ADD_CMD("pttl", 2, "read-only", 1, 1, 1, CommandPTTL),
Expand Down
146 changes: 146 additions & 0 deletions src/redis_disk.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/

tanruixiang marked this conversation as resolved.
Show resolved Hide resolved
#include <vector>
#include <memory>
#include <utility>
#include <algorithm>
#include <string>

#include "db_util.h"
#include "redis_metadata.h"
#include "redis_sortedint.h"
#include "rocksdb/status.h"
#include "redis_zset.h"
#include "redis_bitmap.h"
#include "redis_disk.h"
#include "status.h"

namespace Redis {
rocksdb::Status Disk::GetApproximateSizes(const Metadata &metadata, const Slice &ns_key,
rocksdb::ColumnFamilyHandle *column_family,
uint64_t *key_size, Slice subkeyleft ,
tanruixiang marked this conversation as resolved.
Show resolved Hide resolved
Slice subkeyright) {
std::string prefix_key, next_version_prefix_key;
InternalKey(ns_key, subkeyleft, metadata.version,
storage_->IsSlotIdEncoded()).Encode(&prefix_key);
InternalKey(ns_key, subkeyright, metadata.version + 1,
storage_->IsSlotIdEncoded()).Encode(&next_version_prefix_key);
auto key_range = rocksdb::Range(prefix_key, next_version_prefix_key);
uint64_t tmp_size = 0;
rocksdb::Status s = db_->GetApproximateSizes(this->option, column_family,
&key_range, 1, &tmp_size);
if (!s.ok()) return s;
*key_size += tmp_size;
return rocksdb::Status::OK();
}
rocksdb::Status Disk::GetStringSize(const Slice &user_key, uint64_t *key_size) {
std::string ns_key;
AppendNamespacePrefix(user_key, &ns_key);
auto key_range = rocksdb::Range(Slice(ns_key), Slice(ns_key + static_cast<char>(0)));
return db_->GetApproximateSizes(this->option, metadata_cf_handle_, &key_range, 1, key_size);
}

rocksdb::Status Disk::GetHashSize(const Slice &user_key, uint64_t *key_size) {
*key_size = 0;
std::string ns_key;
AppendNamespacePrefix(user_key, &ns_key);
HashMetadata metadata(false);
rocksdb::Status s = Database::GetMetadata(kRedisHash, ns_key, &metadata);
if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;
return this->GetApproximateSizes(metadata, ns_key,
storage_->GetCFHandle(Engine::kSubkeyColumnFamilyName),
key_size);
}


rocksdb::Status Disk::GetSetSize(const Slice &user_key, uint64_t *key_size) {
*key_size = 0;
std::string ns_key;
AppendNamespacePrefix(user_key, &ns_key);
HashMetadata metadata(false);
rocksdb::Status s = Database::GetMetadata(kRedisSet, ns_key, &metadata);
if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;
return this->GetApproximateSizes(metadata, ns_key,
storage_->GetCFHandle(Engine::kSubkeyColumnFamilyName),
key_size);
}

rocksdb::Status Disk::GetListSize(const Slice &user_key, uint64_t *key_size) {
*key_size = 0;
std::string ns_key;
AppendNamespacePrefix(user_key, &ns_key);
ListMetadata metadata(false);
rocksdb::Status s = Database::GetMetadata(kRedisList, ns_key, &metadata);
if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;
std::string buf;
PutFixed64(&buf, metadata.head);
return this->GetApproximateSizes(metadata, ns_key,
storage_->GetCFHandle(Engine::kSubkeyColumnFamilyName),
key_size, buf);
}

rocksdb::Status Disk::GetZsetSize(const Slice &user_key, uint64_t *key_size) {
*key_size = 0;
std::string ns_key;
AppendNamespacePrefix(user_key, &ns_key);
tanruixiang marked this conversation as resolved.
Show resolved Hide resolved
ZSetMetadata metadata(false);
rocksdb::Status s = Database::GetMetadata(kRedisZSet, ns_key, &metadata);
if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;
std::string score_bytes;
PutDouble(&score_bytes, kMinScore);
s = this->GetApproximateSizes(metadata, ns_key,
storage_->GetCFHandle(Engine::kZSetScoreColumnFamilyName),
key_size, score_bytes, score_bytes);
if (!s.ok()) return s;
return this->GetApproximateSizes(metadata, ns_key,
storage_->GetCFHandle(Engine::kSubkeyColumnFamilyName),
key_size);
}

rocksdb::Status Disk::GetBitmapSize(const Slice &user_key, uint64_t *key_size) {
*key_size = 0;
std::string ns_key;
AppendNamespacePrefix(user_key, &ns_key);
BitmapMetadata metadata(false);
rocksdb::Status s = Database::GetMetadata(kRedisBitmap, ns_key, &metadata);
if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;
std::string score_bytes;
PutDouble(&score_bytes, kMinScore);
return this->GetApproximateSizes(metadata, ns_key,
storage_->GetCFHandle(Engine::kSubkeyColumnFamilyName),
key_size, std::to_string(0), std::to_string(0));
}

rocksdb::Status Disk::GetSortedintSize(const Slice &user_key, uint64_t *key_size) {
*key_size = 0;
std::string ns_key;
AppendNamespacePrefix(user_key, &ns_key);
SortedintMetadata metadata(false);
rocksdb::Status s = Database::GetMetadata(kRedisSortedint, ns_key, &metadata);
if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;
std::string prefix_key, next_version_prefix_key, start_buf;
PutFixed64(&start_buf, 0);
return this->GetApproximateSizes(metadata, ns_key,
tanruixiang marked this conversation as resolved.
Show resolved Hide resolved
storage_->GetCFHandle(Engine::kSubkeyColumnFamilyName),
key_size, start_buf, start_buf);
}

} // namespace Redis
50 changes: 50 additions & 0 deletions src/redis_disk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/

#pragma once

#include <string>
#include "redis_db.h"
#include "redis_metadata.h"

namespace Redis {

class Disk : public Database {
public:
explicit Disk(Engine::Storage *storage, const std::string &ns): Database(storage, ns) {
this->option.include_memtabtles = true;
this->option.include_files = true;
}
rocksdb::Status GetApproximateSizes(const Metadata &metadata, const Slice &ns_key,
rocksdb::ColumnFamilyHandle *column_family,
uint64_t *key_size, Slice subkeyleft = Slice(),
Slice subkeyright = Slice());
rocksdb::Status GetStringSize(const Slice &user_key, uint64_t *key_size);
caipengbo marked this conversation as resolved.
Show resolved Hide resolved
rocksdb::Status GetHashSize(const Slice &user_key, uint64_t *key_size);
rocksdb::Status GetSetSize(const Slice &user_key, uint64_t *key_size);
rocksdb::Status GetListSize(const Slice &user_key, uint64_t *key_size);
rocksdb::Status GetZsetSize(const Slice &user_key, uint64_t *key_size);
rocksdb::Status GetBitmapSize(const Slice &user_key, uint64_t *key_size);
rocksdb::Status GetSortedintSize(const Slice &user_key, uint64_t *key_size);
private:
rocksdb::SizeApproximationOptions option;
tanruixiang marked this conversation as resolved.
Show resolved Hide resolved
};

} // namespace Redis
Loading