-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
fix: PkPatternMatchDel inconsistent between rediscache and db #2777
Changes from 3 commits
ae7a228
3f5ed1f
15895f6
8cf7a61
c2b9c69
d0430ac
889c5d5
681a79f
6f83927
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3129,14 +3129,46 @@ void PKPatternMatchDelCmd::DoInitial() { | |||||||||||||||||||||||||||||
pattern_ = argv_[1]; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
//TODO: may lead to inconsistent between rediscache and db, because currently it only cleans db | ||||||||||||||||||||||||||||||
void PKPatternMatchDelCmd::Do() { | ||||||||||||||||||||||||||||||
int ret = 0; | ||||||||||||||||||||||||||||||
rocksdb::Status s = db_->storage()->PKPatternMatchDel(type_, pattern_, &ret); | ||||||||||||||||||||||||||||||
if (s.ok()) { | ||||||||||||||||||||||||||||||
res_.AppendInteger(ret); | ||||||||||||||||||||||||||||||
int64_t count = 0; | ||||||||||||||||||||||||||||||
rocksdb::Status s = db_->storage()->PKPatternMatchDelWithRemoveKeys(type_, pattern_, &count, &remove_keys_); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if(s.ok()){ | ||||||||||||||||||||||||||||||
res_.AppendInteger(count); | ||||||||||||||||||||||||||||||
s_ = rocksdb::Status::OK(); | ||||||||||||||||||||||||||||||
std::vector<std::string>::const_iterator it; | ||||||||||||||||||||||||||||||
for (it = remove_keys_.begin(); it != remove_keys_.end(); it++) { | ||||||||||||||||||||||||||||||
RemSlotKey(*it, db_); | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||
res_.SetRes(CmdRes::kErrOther, s.ToString()); | ||||||||||||||||||||||||||||||
if (count >= 0) { | ||||||||||||||||||||||||||||||
s_ = rocksdb::Status::OK(); | ||||||||||||||||||||||||||||||
std::vector<std::string>::const_iterator it; | ||||||||||||||||||||||||||||||
for (it = remove_keys_.begin(); it != remove_keys_.end(); it++) { | ||||||||||||||||||||||||||||||
RemSlotKey(*it, db_); | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid duplicate code for removing slot keys. The code for removing slot keys is duplicated. Consider refactoring to avoid duplication. - } else {
- res_.SetRes(CmdRes::kErrOther, s.ToString());
- if (count >= 0) {
- s_ = rocksdb::Status::OK();
- std::vector<std::string>::const_iterator it;
- for (it = remove_keys_.begin(); it != remove_keys_.end(); it++) {
- RemSlotKey(*it, db_);
- }
- }
- }
+ } else if (count >= 0) {
+ s_ = rocksdb::Status::OK();
+ } else {
+ res_.SetRes(CmdRes::kErrOther, s.ToString());
+ }
+ for (const auto& key : remove_keys_) {
+ RemSlotKey(key, db_);
+ }
|
||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
void PKPatternMatchDelCmd::DoThroughDB() { | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 既然记录了删除的key,那是不是binlog内容也可以修改下?直接删除指定的key即可?省去了slave遍历的开销。 |
||||||||||||||||||||||||||||||
Do(); | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
void PKPatternMatchDelCmd::DoUpdateCache() { | ||||||||||||||||||||||||||||||
if(s_.ok()) { | ||||||||||||||||||||||||||||||
db_->cache()->Del(remove_keys_); | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 如果remove_key特别多的话 这个地方 remove_keys_ 会不会有放不下的情况,如果redis有按照字符匹配去删除的命令是不是直接执行命令会好一点 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
我感觉有几个问题
|
||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
void PKPatternMatchDelCmd::DoBinlog() { | ||||||||||||||||||||||||||||||
std::string opt = "del"; | ||||||||||||||||||||||||||||||
for(auto& key: remove_keys_) { | ||||||||||||||||||||||||||||||
argv_.clear(); | ||||||||||||||||||||||||||||||
argv_.emplace_back(opt); | ||||||||||||||||||||||||||||||
argv_.emplace_back(key); | ||||||||||||||||||||||||||||||
Cmd::DoBinlog(); | ||||||||||||||||||||||||||||||
Comment on lines
+3179
to
+3185
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optimize binlog operations. The binlog operations can be optimized by reducing the number of - std::string opt = "del";
- for(auto& key: remove_keys_) {
- argv_.clear();
- argv_.emplace_back(opt);
- argv_.emplace_back(key);
- Cmd::DoBinlog();
- }
+ argv_.clear();
+ argv_.emplace_back("del");
+ for (const auto& key : remove_keys_) {
+ argv_.resize(2);
+ argv_[1] = key;
+ Cmd::DoBinlog();
+ } Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1748,6 +1748,7 @@ rocksdb::Status Redis::PKPatternMatchDel(const std::string& pattern, int32_t* re | |
batch.Clear(); | ||
} else { | ||
*ret = total_delete; | ||
delete iter; | ||
return s; | ||
} | ||
} | ||
|
@@ -1762,6 +1763,93 @@ rocksdb::Status Redis::PKPatternMatchDel(const std::string& pattern, int32_t* re | |
} | ||
|
||
*ret = total_delete; | ||
delete iter; | ||
return s; | ||
} | ||
|
||
rocksdb::Status Redis::PKPatternMatchDelWithRemoveKeys(const std::string& pattern, int64_t* ret, std::vector<std::string>* remove_keys) { | ||
rocksdb::ReadOptions iterator_options; | ||
const rocksdb::Snapshot* snapshot; | ||
ScopeSnapshot ss(db_, &snapshot); | ||
iterator_options.snapshot = snapshot; | ||
iterator_options.fill_cache = false; | ||
|
||
std::string key; | ||
std::string meta_value; | ||
int32_t total_delete = 0; | ||
rocksdb::Status s; | ||
rocksdb::WriteBatch batch; | ||
rocksdb::Iterator* iter = db_->NewIterator(iterator_options, handles_[kMetaCF]); | ||
iter->SeekToFirst(); | ||
while (iter->Valid()) { | ||
auto meta_type = static_cast<enum DataType>(static_cast<uint8_t>(iter->value()[0])); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check for null pointer before dereferencing. Ensure if (iter == nullptr) {
return rocksdb::Status::InvalidArgument("Iterator creation failed.");
} |
||
ParsedBaseMetaKey parsed_meta_key(iter->key().ToString()); | ||
key = iter->key().ToString(); | ||
meta_value = iter->value().ToString(); | ||
|
||
if (meta_type == DataType::kStrings) { | ||
ParsedStringsValue parsed_strings_value(&meta_value); | ||
if (!parsed_strings_value.IsStale() && | ||
(StringMatch(pattern.data(), pattern.size(), parsed_meta_key.Key().data(), parsed_meta_key.Key().size(), 0) != 0)) { | ||
batch.Delete(key); | ||
remove_keys->push_back(parsed_meta_key.Key().data()); | ||
} | ||
} else if (meta_type == DataType::kLists) { | ||
ParsedListsMetaValue parsed_lists_meta_value(&meta_value); | ||
if (!parsed_lists_meta_value.IsStale() && (parsed_lists_meta_value.Count() != 0U) && | ||
(StringMatch(pattern.data(), pattern.size(), parsed_meta_key.Key().data(), parsed_meta_key.Key().size(), 0) != | ||
0)) { | ||
parsed_lists_meta_value.InitialMetaValue(); | ||
batch.Put(handles_[kMetaCF], iter->key(), meta_value); | ||
remove_keys->push_back(parsed_meta_key.Key().data()); | ||
} | ||
} else if (meta_type == DataType::kStreams) { | ||
StreamMetaValue stream_meta_value; | ||
stream_meta_value.ParseFrom(meta_value); | ||
if ((stream_meta_value.length() != 0) && | ||
(StringMatch(pattern.data(), pattern.size(), parsed_meta_key.Key().data(), parsed_meta_key.Key().size(), 0) != 0)) { | ||
stream_meta_value.InitMetaValue(); | ||
batch.Put(handles_[kMetaCF], key, stream_meta_value.value()); | ||
remove_keys->push_back(parsed_meta_key.Key().data()); | ||
} | ||
} else { | ||
ParsedBaseMetaValue parsed_meta_value(&meta_value); | ||
if (!parsed_meta_value.IsStale() && (parsed_meta_value.Count() != 0) && | ||
(StringMatch(pattern.data(), pattern.size(), parsed_meta_key.Key().data(), parsed_meta_key.Key().size(), 0) != | ||
0)) { | ||
parsed_meta_value.InitialMetaValue(); | ||
batch.Put(handles_[kMetaCF], iter->key(), meta_value); | ||
remove_keys->push_back(parsed_meta_key.Key().data()); | ||
} | ||
} | ||
|
||
if (static_cast<size_t>(batch.Count()) >= BATCH_DELETE_LIMIT) { | ||
s = db_->Write(default_write_options_, &batch); | ||
if (s.ok()) { | ||
total_delete += static_cast<int32_t>(batch.Count()); | ||
batch.Clear(); | ||
} else { | ||
remove_keys->erase(remove_keys->end() - batch.Count(), remove_keys->end()); | ||
delete iter; | ||
*ret = total_delete; | ||
return s; | ||
AlexStocks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
iter->Next(); | ||
} | ||
if (batch.Count() != 0U) { | ||
s = db_->Write(default_write_options_, &batch); | ||
if (s.ok()) { | ||
total_delete += static_cast<int32_t>(batch.Count()); | ||
batch.Clear(); | ||
} else { | ||
remove_keys->erase(remove_keys->end() - batch.Count(), remove_keys->end()); | ||
} | ||
} | ||
|
||
*ret = total_delete; | ||
baixin01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
delete iter; | ||
return s; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 释放iter迭代器 |
||
} | ||
|
||
} // namespace storage |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
)和{中间加空格。