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

MSET change it to batch write instead of batch per key #1634

Merged
merged 9 commits into from
Aug 7, 2023
27 changes: 16 additions & 11 deletions src/types/redis_string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -371,28 +371,33 @@ rocksdb::Status String::MSet(const std::vector<StringPair> &pairs, uint64_t ttl,
expire = now + ttl;
}

// Data race, key string maybe overwrite by other key while didn't lock the key here,
// Data race, key string maybe overwrite by other key while didn't lock the keys here,
// to improve the set performance
std::string ns_key;
git-hulk marked this conversation as resolved.
Show resolved Hide resolved
std::optional<MultiLockGuard> guard;
if (lock) {
std::vector<std::string> lock_keys;
lock_keys.reserve(pairs.size());
for (const StringPair &pair : pairs) {
AppendNamespacePrefix(pair.key, &ns_key);
lock_keys.emplace_back(ns_key);
}
guard.emplace(storage_->GetLockManager(), lock_keys);
}

auto batch = storage_->GetWriteBatchBase();
WriteBatchLogData log_data(kRedisString);
batch->PutLogData(log_data.Encode());
for (const auto &pair : pairs) {
std::string bytes;
Metadata metadata(kRedisString, false);
metadata.expire = expire;
metadata.Encode(&bytes);
bytes.append(pair.value.data(), pair.value.size());
auto batch = storage_->GetWriteBatchBase();
WriteBatchLogData log_data(kRedisString);
batch->PutLogData(log_data.Encode());
AppendNamespacePrefix(pair.key, &ns_key);
batch->Put(metadata_cf_handle_, ns_key, bytes);
std::optional<LockGuard> guard;
if (lock) {
guard.emplace(storage_->GetLockManager(), ns_key);
}
auto s = storage_->Write(storage_->DefaultWriteOptions(), batch->GetWriteBatch());
if (!s.ok()) return s;
}
return rocksdb::Status::OK();
return storage_->Write(storage_->DefaultWriteOptions(), batch->GetWriteBatch());
}

rocksdb::Status String::MSetNX(const std::vector<StringPair> &pairs, uint64_t ttl, bool *flag) {
Expand Down