Skip to content

Commit 143da4c

Browse files
kardymondsGrigoriyPA
authored andcommitted
YQ-4723 Use own driver in leader election (#25770)
1 parent 22c701e commit 143da4c

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

ydb/core/fq/libs/row_dispatcher/leader_election.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
#include <ydb/library/actors/core/actor_bootstrapped.h>
1010
#include <ydb/library/actors/core/hfunc.h>
1111
#include <ydb/library/actors/protos/actors.pb.h>
12+
#include <ydb/library/logger/actor.h>
1213

1314
#include <ydb/core/base/path.h>
1415
#include <ydb/core/protos/config.pb.h>
1516

17+
#include <memory>
18+
1619
namespace NFq {
1720

1821
using namespace NActors;
@@ -83,7 +86,11 @@ struct TLeaderElectionMetrics {
8386
::NMonitoring::TDynamicCounters::TCounterPtr LeaderChanged;
8487
};
8588

86-
class TLeaderElection: public TActorBootstrapped<TLeaderElection> {
89+
struct TActorSystemPtrMixin {
90+
NKikimr::TDeferredActorLogBackend::TSharedAtomicActorSystemPtr ActorSystemPtr = std::make_shared<NKikimr::TDeferredActorLogBackend::TAtomicActorSystemPtr>(nullptr);
91+
};
92+
93+
class TLeaderElection: public TActorBootstrapped<TLeaderElection>, public TActorSystemPtrMixin {
8794

8895
enum class EState {
8996
Init,
@@ -93,8 +100,8 @@ class TLeaderElection: public TActorBootstrapped<TLeaderElection> {
93100
Started
94101
};
95102
NKikimrConfig::TSharedReadingConfig::TCoordinatorConfig Config;
96-
const NKikimr::TYdbCredentialsProviderFactory& CredentialsProviderFactory;
97-
NYdb::TDriver Driver;
103+
NKikimr::TYdbCredentialsProviderFactory CredentialsProviderFactory;
104+
std::unique_ptr<NYdb::TDriver> Driver;
98105
TYdbConnectionPtr YdbConnection;
99106
TString TablePathPrefix;
100107
const TString TenantId;
@@ -165,20 +172,19 @@ class TLeaderElection: public TActorBootstrapped<TLeaderElection> {
165172
void ProcessState();
166173
void ResetState();
167174
void SetTimeout();
175+
NYdb::TDriverConfig GetYdbDriverConfig() const;
168176
};
169177

170178
TLeaderElection::TLeaderElection(
171179
NActors::TActorId parentId,
172180
NActors::TActorId coordinatorId,
173181
const NKikimrConfig::TSharedReadingConfig::TCoordinatorConfig& config,
174182
const NKikimr::TYdbCredentialsProviderFactory& credentialsProviderFactory,
175-
NYdb::TDriver driver,
183+
NYdb::TDriver /*driver*/,
176184
const TString& tenant,
177185
const ::NMonitoring::TDynamicCounterPtr& counters)
178186
: Config(config)
179187
, CredentialsProviderFactory(credentialsProviderFactory)
180-
, Driver(driver)
181-
, YdbConnection(config.GetLocalMode() ? nullptr : NewYdbConnection(config.GetDatabase(), credentialsProviderFactory, Driver))
182188
, TablePathPrefix(JoinPath(config.GetDatabase().GetDatabase(), config.GetCoordinationNodePath()))
183189
, TenantId(JoinSeq(":", NKikimr::SplitPath(tenant)))
184190
, CoordinationNodePath(JoinPath(TablePathPrefix, TenantId))
@@ -218,13 +224,19 @@ TYdbSdkRetryPolicy::TPtr MakeSchemaRetryPolicy() {
218224

219225
void TLeaderElection::Bootstrap() {
220226
Become(&TLeaderElection::StateFunc);
227+
Y_ABORT_UNLESS(!ActorSystemPtr->load(std::memory_order_relaxed), "Double ActorSystemPtr init");
228+
ActorSystemPtr->store(TActivationContext::ActorSystem(), std::memory_order_relaxed);
229+
221230
LogPrefix = "TLeaderElection " + SelfId().ToString() + " ";
222231
LOG_ROW_DISPATCHER_DEBUG("Successfully bootstrapped, local coordinator id " << CoordinatorId.ToString()
223232
<< ", tenant id " << TenantId << ", local mode " << Config.GetLocalMode() << ", coordination node path " << CoordinationNodePath);
224233
if (Config.GetLocalMode()) {
225234
TActivationContext::ActorSystem()->Send(ParentId, new NFq::TEvRowDispatcher::TEvCoordinatorChanged(CoordinatorId, 0));
226235
return;
227236
}
237+
238+
Driver = std::make_unique<NYdb::TDriver>(GetYdbDriverConfig());
239+
YdbConnection = NewYdbConnection(Config.GetDatabase(), CredentialsProviderFactory, *Driver);
228240
ProcessState();
229241
}
230242

@@ -469,6 +481,13 @@ void TLeaderElection::HandleException(const std::exception& e) {
469481
ResetState();
470482
}
471483

484+
NYdb::TDriverConfig TLeaderElection::GetYdbDriverConfig() const {
485+
NYdb::TDriverConfig cfg;
486+
cfg.SetDiscoveryMode(NYdb::EDiscoveryMode::Async);
487+
cfg.SetLog(std::make_unique<NKikimr::TDeferredActorLogBackend>(ActorSystemPtr, NKikimrServices::EServiceKikimr::YDB_SDK));
488+
return cfg;
489+
}
490+
472491
} // anonymous namespace
473492

474493
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)