Skip to content

Commit 1f5b4e9

Browse files
committed
feat redis: reload secdist
commit_hash:761c62562a9bf66c6f973e00a5594d239b1b02db
1 parent 1e8b3e3 commit 1f5b4e9

File tree

8 files changed

+64
-1
lines changed

8 files changed

+64
-1
lines changed

redis/include/userver/storages/redis/component.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <userver/storages/redis/base.hpp>
1515
#include <userver/storages/redis/fwd.hpp>
1616
#include <userver/storages/redis/wait_connected_mode.hpp>
17+
#include <userver/storages/secdist/secdist.hpp>
1718
#include <userver/testsuite/redis_control.hpp>
1819
#include <userver/utils/statistics/entry.hpp>
1920

@@ -145,6 +146,7 @@ class Redis : public ComponentBase {
145146

146147
private:
147148
void OnConfigUpdate(const dynamic_config::Snapshot& cfg);
149+
void OnSecdistUpdate(const storages::secdist::SecdistConfig& cfg);
148150

149151
void Connect(
150152
const ComponentConfig& config,
@@ -162,6 +164,7 @@ class Redis : public ComponentBase {
162164

163165
dynamic_config::Source config_;
164166
concurrent::AsyncEventSubscriberScope config_subscription_;
167+
concurrent::AsyncEventSubscriberScope secdist_subscription_;
165168

166169
utils::statistics::Entry statistics_holder_;
167170
utils::statistics::Entry subscribe_statistics_holder_;

redis/src/storages/redis/component.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ Redis::Redis(const ComponentConfig& config, const ComponentContext& component_co
128128

129129
config_subscription_ = config_.UpdateAndListen(this, "redis", &Redis::OnConfigUpdate);
130130

131+
auto& secdist = component_context.FindComponent<Secdist>();
132+
secdist_subscription_ = secdist.GetStorage().UpdateAndListen(this, "redis", &Redis::OnSecdistUpdate);
133+
131134
auto& statistics_storage = component_context.FindComponent<components::StatisticsStorage>().GetStorage();
132135

133136
statistics_holder_ = statistics_storage.RegisterWriter(kStatisticsName, [this](utils::statistics::Writer& writer) {
@@ -323,6 +326,22 @@ void Redis::OnConfigUpdate(const dynamic_config::Snapshot& cfg) {
323326
}
324327
}
325328

329+
void Redis::OnSecdistUpdate(const storages::secdist::SecdistConfig& cfg) {
330+
for (auto& [db, sentinel] : sentinels_) {
331+
const auto& config_name = sentinel->ShardGroupName();
332+
const auto& settings = cfg.Get<storages::secdist::RedisMapSettings>().GetSettings(config_name);
333+
334+
std::vector<storages::redis::ConnectionInfo> cii;
335+
for (const auto& host_port : settings.sentinels) {
336+
storages::redis::ConnectionInfo ci(host_port.host, host_port.port, settings.password);
337+
338+
cii.push_back(ci);
339+
}
340+
341+
sentinels_.at(db)->SetConnectionInfo(cii);
342+
}
343+
}
344+
326345
yaml_config::Schema Redis::GetStaticConfigSchema() {
327346
return yaml_config::MergeSchemas<ComponentBase>(R"(
328347
type: object

redis/src/storages/redis/impl/cluster_sentinel_impl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,10 @@ class ClusterTopologyHolder : public std::enable_shared_from_this<ClusterTopolog
348348
}
349349
}
350350

351+
void SetConnectionInfo(const std::vector<ConnectionInfoInt>& info_array) {
352+
sentinels_->SetConnectionInfo(info_array);
353+
}
354+
351355
static size_t GetClusterSlotsCalledCounter() { return cluster_slots_call_counter_.load(std::memory_order_relaxed); }
352356

353357
boost::signals2::signal<void(HostPort, Redis::State)>& GetSignalNodeStateChanged() {
@@ -1051,6 +1055,10 @@ size_t ClusterSentinelImpl::GetClusterSlotsCalledCounter() {
10511055
return ClusterTopologyHolder::GetClusterSlotsCalledCounter();
10521056
}
10531057

1058+
void ClusterSentinelImpl::SetConnectionInfo(const std::vector<ConnectionInfoInt>& info_array) {
1059+
topology_holder_->SetConnectionInfo(info_array);
1060+
}
1061+
10541062
PublishSettings ClusterSentinelImpl::GetPublishSettings() {
10551063
return PublishSettings{kUnknownShard, false, CommandControl::Strategy::kEveryDc};
10561064
}

redis/src/storages/redis/impl/cluster_sentinel_impl.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class ClusterSentinelImpl : public SentinelImplBase {
6969

7070
static size_t GetClusterSlotsCalledCounter();
7171

72+
void SetConnectionInfo(const std::vector<ConnectionInfoInt>& info_array) override;
73+
7274
private:
7375
void AsyncCommandFailed(const SentinelCommand& scommand);
7476
void EnqueueCommand(const SentinelCommand& command);

redis/src/storages/redis/impl/sentinel.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ Sentinel::Sentinel(
7373
const testsuite::RedisControl& testsuite_redis_control,
7474
ConnectionMode mode
7575
)
76-
: thread_pools_(thread_pools),
76+
: shard_group_name_(shard_group_name),
77+
thread_pools_(thread_pools),
7778
secdist_default_command_control_(command_control),
7879
testsuite_redis_control_(testsuite_redis_control) {
7980
config_default_command_control_.Set(std::make_shared<CommandControl>(secdist_default_command_control_));
@@ -414,6 +415,16 @@ void Sentinel::SetConfigDefaultCommandControl(const std::shared_ptr<CommandContr
414415
config_default_command_control_.Set(cc);
415416
}
416417

418+
const std::string& Sentinel::ShardGroupName() const { return shard_group_name_; }
419+
420+
void Sentinel::SetConnectionInfo(std::vector<ConnectionInfo> info_array) {
421+
std::vector<ConnectionInfoInt> cii;
422+
cii.reserve(info_array.size());
423+
for (const auto& ci : info_array) cii.emplace_back(ci);
424+
425+
impl_->SetConnectionInfo(cii);
426+
}
427+
417428
std::vector<std::shared_ptr<const Shard>> Sentinel::GetMasterShards() const { return impl_->GetMasterShards(); }
418429

419430
void Sentinel::CheckRenameParams(const std::string& key, const std::string& newkey) const {

redis/src/storages/redis/impl/sentinel.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ class Sentinel {
184184

185185
virtual void SetConfigDefaultCommandControl(const std::shared_ptr<CommandControl>& cc);
186186

187+
void SetConnectionInfo(std::vector<ConnectionInfo> info_array);
188+
const std::string& ShardGroupName() const;
189+
187190
using UserMessageCallback = std::function<Outcome(const std::string& channel, const std::string& message)>;
188191
using UserPmessageCallback =
189192
std::function<Outcome(const std::string& pattern, const std::string& channel, const std::string& message)>;
@@ -228,6 +231,7 @@ class Sentinel {
228231

229232
friend class Transaction;
230233

234+
const std::string shard_group_name_;
231235
std::shared_ptr<ThreadPools> thread_pools_;
232236
std::unique_ptr<engine::ev::ThreadControl> sentinel_thread_control_;
233237
CommandControl secdist_default_command_control_;

redis/src/storages/redis/impl/sentinel_impl.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ SentinelImpl::SentinelImpl(
9999

100100
SentinelImpl::~SentinelImpl() { Stop(); }
101101

102+
void SentinelImpl::SetSentinelConnectionInfo(const std::vector<ConnectionInfo>& sentinel_conns) {
103+
std::vector<ConnectionInfoInt> cii;
104+
cii.reserve(sentinel_conns.size());
105+
for (const auto& conn : sentinel_conns) cii.emplace_back(ConnectionInfoInt{conn});
106+
sentinels_->SetConnectionInfo(cii);
107+
}
108+
102109
std::unordered_map<ServerId, size_t, ServerIdHasher>
103110
SentinelImpl::GetAvailableServersWeighted(size_t shard_idx, bool with_master, const CommandControl& cc) const {
104111
return master_shards_.at(shard_idx)->GetAvailableServersWeighted(with_master, cc);
@@ -1022,6 +1029,10 @@ bool SentinelImpl::ConnectedStatus::Wait(engine::Deadline deadline, const Pred&
10221029
return cv_.WaitUntil(lock, deadline, pred);
10231030
}
10241031

1032+
void SentinelImpl::SetConnectionInfo(const std::vector<ConnectionInfoInt>& info_array) {
1033+
sentinels_->SetConnectionInfo(info_array);
1034+
}
1035+
10251036
} // namespace storages::redis::impl
10261037

10271038
USERVER_NAMESPACE_END

redis/src/storages/redis/impl/sentinel_impl.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class SentinelImplBase {
8585
virtual void SetRetryBudgetSettings(const utils::RetryBudgetSettings& retry_budget_settings) = 0;
8686

8787
virtual PublishSettings GetPublishSettings() = 0;
88+
virtual void SetConnectionInfo(const std::vector<ConnectionInfoInt>& info_array) = 0;
8889
};
8990

9091
bool AdjustDeadline(const SentinelImplBase::SentinelCommand& scommand, const dynamic_config::Snapshot& config);
@@ -110,6 +111,8 @@ class SentinelImpl : public SentinelImplBase {
110111
);
111112
~SentinelImpl() override;
112113

114+
void SetSentinelConnectionInfo(const std::vector<ConnectionInfo>& sentinel_conns);
115+
113116
std::unordered_map<ServerId, size_t, ServerIdHasher>
114117
GetAvailableServersWeighted(size_t shard_idx, bool with_master, const CommandControl& cc) const override;
115118

@@ -139,6 +142,8 @@ class SentinelImpl : public SentinelImplBase {
139142
void SetRetryBudgetSettings(const utils::RetryBudgetSettings& retry_budget_settings) override;
140143
PublishSettings GetPublishSettings() override;
141144

145+
void SetConnectionInfo(const std::vector<ConnectionInfoInt>& info_array) override;
146+
142147
private:
143148
static constexpr const std::chrono::milliseconds cluster_slots_timeout_ = std::chrono::milliseconds(4000);
144149

0 commit comments

Comments
 (0)