Skip to content

Commit

Permalink
fix: allow lock_on_hashtags with any cluster mode (#2443)
Browse files Browse the repository at this point in the history
Motivation - after we submitted #2429 some smart-ass clients
prevent users from accessing single-node commands like "SELECT".
This PR fixes it by allowing consistent sharding based on hashtags
even with cluster mode disabled.

Signed-off-by: Roman Gershman <[email protected]>
  • Loading branch information
romange authored Jan 19, 2024
1 parent a11b2a9 commit 8f454b2
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 18 deletions.
4 changes: 0 additions & 4 deletions src/server/cluster/cluster_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ bool ClusterConfig::IsEmulated() {
return cluster_mode == ClusterMode::kEmulatedCluster;
}

bool ClusterConfig::IsEnabledOrEmulated() {
return IsEnabled() || IsEmulated();
}

string_view ClusterConfig::KeyTag(string_view key) {
size_t start = key.find('{');
if (start == key.npos) {
Expand Down
11 changes: 9 additions & 2 deletions src/server/cluster/cluster_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#pragma once

#include <absl/base/thread_annotations.h>
#include <absl/container/flat_hash_set.h>

#include <array>
Expand All @@ -15,6 +14,7 @@

#include "core/json_object.h"
#include "src/core/fibers.h"
#include "src/server/common.h"

namespace dfly {

Expand Down Expand Up @@ -50,7 +50,14 @@ class ClusterConfig {
static void Initialize();
static bool IsEnabled();
static bool IsEmulated();
static bool IsEnabledOrEmulated();

static bool IsEnabledOrEmulated() {
return IsEnabled() || IsEmulated();
}

static bool IsShardedByTag() {
return IsEnabledOrEmulated() || KeyLockArgs::IsLockHashTagEnabled();
}

// If the key contains the {...} pattern, return only the part between { and }
static std::string_view KeyTag(std::string_view key);
Expand Down
3 changes: 1 addition & 2 deletions src/server/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ extern "C" {
#include "strings/human_readable.h"

ABSL_FLAG(bool, lock_on_hashtags, false,
"When true, locks are done in the {hashtag} level instead of key level. "
"Only use this with --cluster_mode=emulated|yes.");
"When true, locks are done in the {hashtag} level instead of key level.");

namespace dfly {

Expand Down
7 changes: 2 additions & 5 deletions src/server/engine_shard_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -859,11 +859,8 @@ void EngineShardSet::TEST_EnableCacheMode() {
}

ShardId Shard(string_view v, ShardId shard_num) {
if (ClusterConfig::IsEnabledOrEmulated()) {
string_view v_hash_tag = ClusterConfig::KeyTag(v);
if (v_hash_tag.size() != v.size()) {
v = v_hash_tag;
}
if (ClusterConfig::IsShardedByTag()) {
v = ClusterConfig::KeyTag(v);
}

XXH64_hash_t hash = XXH64(v.data(), v.size(), 120577240643ULL);
Expand Down
5 changes: 0 additions & 5 deletions src/server/main_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -727,11 +727,6 @@ Service::Service(ProactorPool* pp)
CHECK(pp);
CHECK(shard_set == NULL);

if (KeyLockArgs::IsLockHashTagEnabled() && !ClusterConfig::IsEnabledOrEmulated()) {
LOG(ERROR) << "Setting --lock_on_hashtags without --cluster_mode is unsupported";
exit(1);
}

#ifdef PRINT_STACKTRACES_ON_SIGNAL
LOG(INFO) << "PRINT STACKTRACES REGISTERED";
pp_.GetNextProactor()->RegisterSignal({SIGUSR1}, [this](int signal) {
Expand Down

0 comments on commit 8f454b2

Please sign in to comment.