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

feat: Added SELECT redis command #482

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

tysion
Copy link

@tysion tysion commented Jan 24, 2024

Added the ability to select a database for redis (see SELECT).
It seems to me that this feature will be useful to many, since people often use “communal” redis sentinel setup.

This can be done by adding the database_index key to the redis config

{
  "redis_settings": {
    "taxi-tmp": {
      "password": "",
      "database_index": 3, /* <---- `SELECT 3` will be executed on connection. If 0 or no "database_index" key at all - noop */
      "secure_connection": false,
      "shards": [
        {
          "name": "mymaster"
        }
      ],
      "sentinels": [
        {
          "host": "127.0.0.1",
          "port": 26379
        }
      ]
    }
  }
}

@tysion tysion marked this pull request as ready for review February 4, 2024 18:25
@tysion tysion requested a review from segoon as a code owner February 4, 2024 18:25
@tysion
Copy link
Author

tysion commented Mar 5, 2024

@apolukhin, any chance of being reviewed?

@apolukhin
Copy link
Member

Thanks for the ping! Will look at the PR in a few days

Copy link

@VyacheslavVanin VyacheslavVanin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.
It's only worth adding a new option here
https://github.com/userver-framework/userver/blob/develop/samples/redis_service/tests/conftest.py#L17
to document it.

@@ -27,16 +27,19 @@ struct ConnectionInfo {
bool read_only = false;
ConnectionSecurity connection_security = ConnectionSecurity::kNone;
using HostVector = std::vector<std::string>;
std::optional<size_t> database_index{};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New connections always use database index 0 so it is not really an optional:

Suggested change
std::optional<size_t> database_index{};
std::size_t database_index = 0;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -267,6 +270,7 @@ class Redis::RedisImpl : public std::enable_shared_from_this<Redis::RedisImpl> {
uint16_t port_ = 0;
std::string server_;
Password password_{std::string()};
std::optional<size_t> database_index_;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not optional and 0 by default

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -1099,6 +1106,47 @@ void Redis::RedisImpl::SendReadOnly() {
}));
}

void Redis::RedisImpl::SelectDatabase() {
if (!database_index_) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better yet, if database index equals 0.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 44 to 48
if (settings.database_index && *settings.database_index == 0) {
// To get rid of the redundant `SELECT 0` command
// since 0 is the default database index and it will be set automatically
settings.database_index = std::nullopt;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better check on connect in RedisImpl

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -38,6 +39,14 @@ RedisMapSettings::RedisMapSettings(const formats::json::Value& doc) {
? USERVER_NAMESPACE::redis::ConnectionSecurity::kTLS
: USERVER_NAMESPACE::redis::ConnectionSecurity::kNone;

settings.database_index =
client_settings["database_index"].As<std::optional<size_t>>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will not work in cluster mode, would be great if you add a check in redis/component.cpp

Copy link
Author

@tysion tysion Apr 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you clarify this a little?

  1. Do you want me to extend method Redis::Connect with something like this:
auto sentinel = redis::Sentinel::CreateSentinel(
        thread_pools_, settings, redis_group.config_name, config_source,
        redis_group.db, redis::KeyShardFactory{redis_group.sharding_strategy},
        cc, testsuite_redis_control);
if (sentinel) {
      if (sentinel->IsInClusterMode() && settings.database_index != 0) {
        // handle it somehow
      }
      ...
}

Is it the only one place I need to insert the check or there are some other places as well?
2. How do you want to handle it? Throw an exception or just skip it with the corresponding log message like when sentinel pointer turns out to be null?
3. Are you sure you want to add checks in this case? As you have mentioned, cluster setup does not support selecting databases, so service will shut down with an error message like SELECT failed: unknown command SELECT``anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants