11#include " auto_config_initializer.h"
22#include " run.h"
3+ #include " grpc_servers_manager.h"
34#include " service_initializer.h"
45#include " kikimr_services_initializers.h"
56
173174
174175namespace NKikimr {
175176
177+ namespace {
178+
179+ void StopGRpcServers (std::weak_ptr<TGRpcServersWrapper> grpcServersWrapper) {
180+ auto wrapper = grpcServersWrapper.lock ();
181+ if (!wrapper) {
182+ return ;
183+ }
184+ TGuard<TMutex> guard = wrapper->Guard ();
185+ for (auto & [name, server] : wrapper->Servers ) {
186+ if (!server) {
187+ continue ;
188+ }
189+ server->Stop ();
190+ }
191+ wrapper->Servers .clear ();
192+ }
193+
194+ }
195+
176196class TGRpcServersManager : public TActorBootstrapped <TGRpcServersManager> {
177- TGRpcServersFactory GRpcServersFactory;
178- TGRpcServers GRpcServers;
197+ std::weak_ptr<TGRpcServersWrapper> GRpcServersWrapper;
179198 TIntrusivePtr<NMemory::IProcessMemoryInfoProvider> ProcessMemoryInfoProvider;
199+ bool Started = false ;
200+ bool StopScheduled = false ;
201+ bool WaitingForDisconnectRequest = false ;
180202
181203public:
182204 enum {
@@ -192,9 +214,9 @@ class TGRpcServersManager : public TActorBootstrapped<TGRpcServersManager> {
192214 };
193215
194216public:
195- TGRpcServersManager (TGRpcServersFactory grpcServersFactory ,
217+ TGRpcServersManager (std::weak_ptr<TGRpcServersWrapper> grpcServersWrapper ,
196218 TIntrusivePtr<NMemory::IProcessMemoryInfoProvider> processMemoryInfoProvider)
197- : GRpcServersFactory (std::move(grpcServersFactory ))
219+ : GRpcServersWrapper (std::move(grpcServersWrapper ))
198220 , ProcessMemoryInfoProvider(std::move(processMemoryInfoProvider))
199221 {}
200222
@@ -208,18 +230,43 @@ class TGRpcServersManager : public TActorBootstrapped<TGRpcServersManager> {
208230 if (const auto & bridgeInfo = ev->Get ()->BridgeInfo ) {
209231 if (NBridge::PileStateTraits (bridgeInfo->SelfNodePile ->State ).RequiresConfigQuorum ) {
210232 Start ();
211- } else {
212- Stop ();
233+ } else if (!StopScheduled) {
234+ StopScheduled = true ;
235+ CheckAndExecuteStop ();
213236 }
214237 }
215238 }
216239
240+ void HandleDisconnectRequestStarted () {
241+ WaitingForDisconnectRequest = true ;
242+ }
243+
244+ void HandleDisconnectRequestFinished () {
245+ WaitingForDisconnectRequest = false ;
246+ CheckAndExecuteStop ();
247+ }
248+
249+ void CheckAndExecuteStop () {
250+ if (StopScheduled && !WaitingForDisconnectRequest) {
251+ StopScheduled = false ;
252+ Stop ();
253+ }
254+ }
255+
217256 void Start () {
218- if (GRpcServers) {
257+ if (Started) {
258+ return ;
259+ }
260+ Started = true ;
261+ StopScheduled = false ;
262+ WaitingForDisconnectRequest = false ;
263+ auto wrapper = GRpcServersWrapper.lock ();
264+ if (!wrapper) {
219265 return ;
220266 }
221- GRpcServers = GRpcServersFactory ();
222- for (auto & [name, server] : GRpcServers) {
267+ TGuard<TMutex> guard = wrapper->Guard ();
268+ wrapper->Servers = wrapper->GrpcServersFactory ();
269+ for (auto & [name, server] : wrapper->Servers ) {
223270 if (!server) {
224271 continue ;
225272 }
@@ -251,12 +298,11 @@ class TGRpcServersManager : public TActorBootstrapped<TGRpcServersManager> {
251298 }
252299
253300 void Stop () {
254- for (auto & [name, server] : GRpcServers) {
255- if (server) {
256- server->Stop ();
257- }
301+ if (!Started) {
302+ return ;
258303 }
259- GRpcServers.clear ();
304+ Started = false ;
305+ StopGRpcServers (GRpcServersWrapper);
260306 }
261307
262308 void HandleStop (TEvStop::TPtr ev) {
@@ -268,6 +314,8 @@ class TGRpcServersManager : public TActorBootstrapped<TGRpcServersManager> {
268314 STRICT_STFUNC (StateFunc,
269315 hFunc (TEvNodeWardenStorageConfig, Handle)
270316 hFunc(TEvStop, HandleStop)
317+ cFunc(TEvGRpcServersManager::EvDisconnectRequestStarted, HandleDisconnectRequestStarted)
318+ cFunc(TEvGRpcServersManager::EvDisconnectRequestFinished, HandleDisconnectRequestFinished)
271319 )
272320};
273321
@@ -645,7 +693,10 @@ void TKikimrRunner::InitializeKqpController(const TKikimrRunConfig& runConfig) {
645693}
646694
647695void TKikimrRunner::InitializeGRpc (const TKikimrRunConfig& runConfig) {
648- GRpcServersFactory = [runConfig, this ] { return CreateGRpcServers (runConfig); };
696+ if (!GRpcServersWrapper) {
697+ GRpcServersWrapper = std::make_shared<TGRpcServersWrapper>();
698+ }
699+ GRpcServersWrapper->GrpcServersFactory = [runConfig, this ] { return CreateGRpcServers (runConfig); };
649700}
650701
651702TGRpcServers TKikimrRunner::CreateGRpcServers (const TKikimrRunConfig& runConfig) {
@@ -1990,8 +2041,10 @@ void TKikimrRunner::KikimrStart() {
19902041 Monitoring->Start (ActorSystem.Get ());
19912042 }
19922043
1993- if (GRpcServersFactory) {
1994- GRpcServersManager = ActorSystem->Register (new TGRpcServersManager (std::move (GRpcServersFactory), ProcessMemoryInfoProvider));
2044+ if (GRpcServersWrapper) {
2045+ GRpcServersWrapper->Servers = GRpcServersWrapper->GrpcServersFactory ();
2046+ GRpcServersManager = ActorSystem->Register (new TGRpcServersManager (GRpcServersWrapper, ProcessMemoryInfoProvider));
2047+ ActorSystem->RegisterLocalService (NKikimr::MakeGRpcServersManagerId (ActorSystem->NodeId ), GRpcServersManager);
19952048 }
19962049
19972050 if (SqsHttp) {
@@ -2096,23 +2149,18 @@ void TKikimrRunner::KikimrStop(bool graceful) {
20962149 SqsHttp.Destroy ();
20972150 }
20982151
2099- // stop processing grpc requests/response - we must stop feeding ActorSystem
2100- if (GRpcServersManager) {
2101- TManualEvent event;
2102- ActorSystem->Send (new IEventHandle (GRpcServersManager, {}, new TGRpcServersManager::TEvStop (&event)));
2103- event.WaitI ();
2104- }
2105-
21062152 if (ActorSystem) {
21072153 ActorSystem->Stop ();
21082154 }
21092155
2110- if (YqSharedResources) {
2111- YqSharedResources->Stop ();
2156+ // stop processing grpc requests/response - we must stop feeding ActorSystem
2157+ if (GRpcServersManager) {
2158+ StopGRpcServers (GRpcServersWrapper);
2159+ GRpcServersWrapper->Servers .clear ();
21122160 }
21132161
2114- if (ActorSystem ) {
2115- ActorSystem-> Cleanup ();
2162+ if (YqSharedResources ) {
2163+ YqSharedResources-> Stop ();
21162164 }
21172165
21182166 if (ModuleFactories) {
@@ -2121,12 +2169,17 @@ void TKikimrRunner::KikimrStop(bool graceful) {
21212169 }
21222170 }
21232171
2124- if (YdbDriver) {
2125- YdbDriver->Stop (true );
2126- }
21272172 for (auto plugin: Plugins) {
21282173 plugin->Stop ();
21292174 }
2175+
2176+ if (ActorSystem) {
2177+ ActorSystem->Cleanup ();
2178+ }
2179+
2180+ if (YdbDriver) {
2181+ YdbDriver->Stop (true );
2182+ }
21302183}
21312184
21322185void TKikimrRunner::BusyLoop () {
0 commit comments