1
1
#include " auto_config_initializer.h"
2
2
#include " run.h"
3
+ #include " grpc_servers_manager.h"
3
4
#include " service_initializer.h"
4
5
#include " kikimr_services_initializers.h"
5
6
173
174
174
175
namespace NKikimr {
175
176
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
+
176
196
class TGRpcServersManager : public TActorBootstrapped <TGRpcServersManager> {
177
- TGRpcServersFactory GRpcServersFactory;
178
- TGRpcServers GRpcServers;
197
+ std::weak_ptr<TGRpcServersWrapper> GRpcServersWrapper;
179
198
TIntrusivePtr<NMemory::IProcessMemoryInfoProvider> ProcessMemoryInfoProvider;
199
+ bool Started = false ;
200
+ bool StopScheduled = false ;
201
+ bool WaitingForDisconnectRequest = false ;
180
202
181
203
public:
182
204
enum {
@@ -192,9 +214,9 @@ class TGRpcServersManager : public TActorBootstrapped<TGRpcServersManager> {
192
214
};
193
215
194
216
public:
195
- TGRpcServersManager (TGRpcServersFactory grpcServersFactory ,
217
+ TGRpcServersManager (std::weak_ptr<TGRpcServersWrapper> grpcServersWrapper ,
196
218
TIntrusivePtr<NMemory::IProcessMemoryInfoProvider> processMemoryInfoProvider)
197
- : GRpcServersFactory (std::move(grpcServersFactory ))
219
+ : GRpcServersWrapper (std::move(grpcServersWrapper ))
198
220
, ProcessMemoryInfoProvider(std::move(processMemoryInfoProvider))
199
221
{}
200
222
@@ -208,18 +230,43 @@ class TGRpcServersManager : public TActorBootstrapped<TGRpcServersManager> {
208
230
if (const auto & bridgeInfo = ev->Get ()->BridgeInfo ) {
209
231
if (NBridge::PileStateTraits (bridgeInfo->SelfNodePile ->State ).RequiresConfigQuorum ) {
210
232
Start ();
211
- } else {
212
- Stop ();
233
+ } else if (!StopScheduled) {
234
+ StopScheduled = true ;
235
+ CheckAndExecuteStop ();
213
236
}
214
237
}
215
238
}
216
239
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
+
217
256
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) {
219
265
return ;
220
266
}
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 ) {
223
270
if (!server) {
224
271
continue ;
225
272
}
@@ -251,12 +298,11 @@ class TGRpcServersManager : public TActorBootstrapped<TGRpcServersManager> {
251
298
}
252
299
253
300
void Stop () {
254
- for (auto & [name, server] : GRpcServers) {
255
- if (server) {
256
- server->Stop ();
257
- }
301
+ if (!Started) {
302
+ return ;
258
303
}
259
- GRpcServers.clear ();
304
+ Started = false ;
305
+ StopGRpcServers (GRpcServersWrapper);
260
306
}
261
307
262
308
void HandleStop (TEvStop::TPtr ev) {
@@ -268,6 +314,8 @@ class TGRpcServersManager : public TActorBootstrapped<TGRpcServersManager> {
268
314
STRICT_STFUNC (StateFunc,
269
315
hFunc (TEvNodeWardenStorageConfig, Handle)
270
316
hFunc(TEvStop, HandleStop)
317
+ cFunc(TEvGRpcServersManager::EvDisconnectRequestStarted, HandleDisconnectRequestStarted)
318
+ cFunc(TEvGRpcServersManager::EvDisconnectRequestFinished, HandleDisconnectRequestFinished)
271
319
)
272
320
};
273
321
@@ -645,7 +693,10 @@ void TKikimrRunner::InitializeKqpController(const TKikimrRunConfig& runConfig) {
645
693
}
646
694
647
695
void 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); };
649
700
}
650
701
651
702
TGRpcServers TKikimrRunner::CreateGRpcServers (const TKikimrRunConfig& runConfig) {
@@ -1990,8 +2041,10 @@ void TKikimrRunner::KikimrStart() {
1990
2041
Monitoring->Start (ActorSystem.Get ());
1991
2042
}
1992
2043
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);
1995
2048
}
1996
2049
1997
2050
if (SqsHttp) {
@@ -2096,23 +2149,18 @@ void TKikimrRunner::KikimrStop(bool graceful) {
2096
2149
SqsHttp.Destroy ();
2097
2150
}
2098
2151
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
-
2106
2152
if (ActorSystem) {
2107
2153
ActorSystem->Stop ();
2108
2154
}
2109
2155
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 ();
2112
2160
}
2113
2161
2114
- if (ActorSystem ) {
2115
- ActorSystem-> Cleanup ();
2162
+ if (YqSharedResources ) {
2163
+ YqSharedResources-> Stop ();
2116
2164
}
2117
2165
2118
2166
if (ModuleFactories) {
@@ -2121,12 +2169,17 @@ void TKikimrRunner::KikimrStop(bool graceful) {
2121
2169
}
2122
2170
}
2123
2171
2124
- if (YdbDriver) {
2125
- YdbDriver->Stop (true );
2126
- }
2127
2172
for (auto plugin: Plugins) {
2128
2173
plugin->Stop ();
2129
2174
}
2175
+
2176
+ if (ActorSystem) {
2177
+ ActorSystem->Cleanup ();
2178
+ }
2179
+
2180
+ if (YdbDriver) {
2181
+ YdbDriver->Stop (true );
2182
+ }
2130
2183
}
2131
2184
2132
2185
void TKikimrRunner::BusyLoop () {
0 commit comments