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

Fix data race for accessing database in some commands #253

Merged
merged 6 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion src/redis_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <map>
#include <iostream>
#include <memory>

namespace Redis {

Expand Down Expand Up @@ -168,7 +169,9 @@ rocksdb::Status Set::Take(const Slice &user_key, std::vector<std::string> *membe
std::string ns_key;
AppendNamespacePrefix(user_key, &ns_key);

if (pop) LockGuard guard(storage_->GetLockManager(), ns_key);
ShooterIT marked this conversation as resolved.
Show resolved Hide resolved
std::unique_ptr<LockGuard> lock_guard;
if (pop) lock_guard = std::unique_ptr<LockGuard>(new LockGuard(storage_->GetLockManager(), ns_key));
ShooterIT marked this conversation as resolved.
Show resolved Hide resolved

SetMetadata metadata(false);
rocksdb::Status s = GetMetadata(ns_key, &metadata);
if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;
Expand Down
8 changes: 6 additions & 2 deletions src/redis_zset.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <map>
#include <limits>
#include <cmath>
#include <memory>

namespace Redis {

Expand Down Expand Up @@ -168,7 +169,9 @@ rocksdb::Status ZSet::Range(const Slice &user_key, int start, int stop, uint8_t

bool removed = (flags & (uint8_t)ZSET_REMOVED) != 0;
bool reversed = (flags & (uint8_t)ZSET_REVERSED) != 0;
if (removed) LockGuard guard(storage_->GetLockManager(), ns_key);

std::unique_ptr<LockGuard> lock_guard;
if (removed) lock_guard = std::unique_ptr<LockGuard>(new LockGuard(storage_->GetLockManager(), ns_key));
ZSetMetadata metadata(false);
rocksdb::Status s = GetMetadata(ns_key, &metadata);
if (!s.ok()) return s.IsNotFound()? rocksdb::Status::OK():s;
Expand Down Expand Up @@ -239,7 +242,8 @@ rocksdb::Status ZSet::RangeByScore(const Slice &user_key,
std::string ns_key;
AppendNamespacePrefix(user_key, &ns_key);

if (spec.removed) LockGuard guard(storage_->GetLockManager(), ns_key);
std::unique_ptr<LockGuard> lock_guard;
if (spec.removed) lock_guard = std::unique_ptr<LockGuard>(new LockGuard(storage_->GetLockManager(), ns_key));
ZSetMetadata metadata(false);
rocksdb::Status s = GetMetadata(ns_key, &metadata);
if (!s.ok()) return s.IsNotFound()? rocksdb::Status::OK():s;
Expand Down
6 changes: 4 additions & 2 deletions src/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ Status Server::Start() {
if (is_loading_ == false && ++counter % 600 == 0 // check every minute
&& config_->compaction_checker_range.Enabled()) {
auto now = std::time(nullptr);
auto local_time = std::localtime(&now);
std::tm *local_time;
localtime_r(&now, local_time);
if (local_time->tm_hour >= config_->compaction_checker_range.Start
&& local_time->tm_hour <= config_->compaction_checker_range.Stop) {
std::vector<std::string> cf_names = {Engine::kMetadataColumnFamilyName,
Expand Down Expand Up @@ -460,7 +461,8 @@ void Server::cron() {
// check every 20s (use 20s instead of 60s so that cron will execute in critical condition)
if (is_loading_ == false && counter != 0 && counter % 200 == 0) {
auto t = std::time(nullptr);
auto now = std::localtime(&t);
std::tm *now;
localtime_r(&t, now);
// disable compaction cron when the compaction checker was enabled
if (!config_->compaction_checker_range.Enabled()
&& config_->compact_cron.IsEnabled()
Expand Down
2 changes: 1 addition & 1 deletion src/worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ void WorkerThread::Start() {
} else {
Util::ThreadSetName("worker");
}
this->worker_->Run(t_.get_id());
this->worker_->Run(std::this_thread::get_id());
});
} catch (const std::system_error &e) {
LOG(ERROR) << "[worker] Failed to start worker thread, err: " << e.what();
Expand Down