aboutsummaryrefslogtreecommitdiffstats
path: root/ydb/core/cms/cms_impl.h
blob: c5fc9c168422bd90b8628f4ceb58517aaff4fb6b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#pragma once

#include "audit_log.h"
#include "cms.h"
#include "config.h"
#include "logger.h"
#include "services.h"
#include "walle.h"

#include <library/cpp/actors/core/hfunc.h>
#include <library/cpp/actors/core/interconnect.h>
#include <ydb/core/base/tablet_pipe.h>
#include <ydb/core/base/statestorage_impl.h>
#include <ydb/core/cms/console/console.h>
#include <ydb/core/protos/counters_cms.pb.h>
#include <ydb/core/tablet/tablet_counters_protobuf.h>
#include <ydb/core/tablet_flat/tablet_flat_executed.h>
#include <ydb/core/engine/minikql/flat_local_tx_factory.h>


#include <util/generic/stack.h>
#include <util/generic/queue.h>

namespace NKikimr::NCms {

using NConsole::TEvConsole;
using NTabletFlatExecutor::TTabletExecutedFlat;
using NTabletFlatExecutor::ITransaction;
using NTabletFlatExecutor::TTransactionBase;
using NTabletFlatExecutor::TTransactionContext;

class TCms : public TActor<TCms>, public TTabletExecutedFlat {
public:
    struct TEvPrivate {
        enum EEv {
            EvClusterInfo = EventSpaceBegin(TKikimrEvents::ES_PRIVATE),
            EvUpdateClusterInfo,
            EvCleanupExpired,
            EvCleanupWalle,
            EvLogAndSend,
            EvCleanupLog,
            EvStartCollecting,

            EvEnd
        };

        static_assert(EvEnd < EventSpaceEnd(TKikimrEvents::ES_PRIVATE), "expect EvEnd < EventSpaceEnd(TKikimrEvents::ES_PRIVATE)");

        struct TEvClusterInfo : public TEventLocal<TEvClusterInfo, EvClusterInfo> {
            bool Success = true;
            TClusterInfoPtr Info;

            TString ToString() const override
            {
                return "TEvClusterInfo";
            }
        };

        struct TEvUpdateClusterInfo : public TEventLocal<TEvUpdateClusterInfo, EvUpdateClusterInfo> {};

        struct TEvStartCollecting : public TEventLocal<TEvStartCollecting, EvStartCollecting> {};

        struct TEvCleanupExpired : public TEventLocal<TEvCleanupExpired, EvCleanupExpired> {};

        struct TEvCleanupWalle : public TEventLocal<TEvCleanupWalle, EvCleanupWalle> {};

        struct TEvLogAndSend : public TEventLocal<TEvLogAndSend, EvLogAndSend> {
            NKikimrCms::TLogRecordData LogData;
            THolder<IEventHandle> Event;
        };

        struct TEvCleanupLog : public TEventLocal<TEvCleanupLog, EvCleanupLog> {};
    };

    void PersistNodeTenants(TTransactionContext& txc, const TActorContext& ctx);

    static void AddHostState(const TClusterInfoPtr &clusterInfo, const TNodeInfo &node, NKikimrCms::TClusterStateResponse &resp, TInstant timestamp);

private:
    using TActorBase = TActor<TCms>;
    using EStatusCode = NKikimrCms::TStatus::ECode;

    class TTxGetLogTail;
    class TTxInitScheme;
    class TTxLoadState;
    class TTxLogAndSend;
    class TTxLogCleanup;
    class TTxProcessNotification;
    class TTxRejectNotification;
    class TTxRemoveExpiredNotifications;
    class TTxRemoveRequest;
    class TTxRemovePermissions;
    class TTxRemoveWalleTask;
    class TTxStorePermissions;
    class TTxStoreWalleTask;
    class TTxUpdateConfig;
    class TTxUpdateDowntimes;

    struct TActionOptions {
        TDuration PermissionDuration;
        NKikimrCms::ETenantPolicy TenantPolicy;
        NKikimrCms::EAvailabilityMode AvailabilityMode;
        bool PartialPermissionAllowed;

        TActionOptions(TDuration dur)
            : PermissionDuration(dur)
            , TenantPolicy(NKikimrCms::DEFAULT)
            , AvailabilityMode(NKikimrCms::MODE_MAX_AVAILABILITY)
            , PartialPermissionAllowed(false)
        {}
    };

    struct TNodeCounter {
        ui32 Total = 0;
        ui32 Down = 0;
        ui32 Locked = 0;
        NKikimrCms::TStatus::ECode Code = NKikimrCms::TStatus::DISALLOW_TEMP;

        void CountNode(const TNodeInfo &node, bool ignoreDownState);
        // Check if we can lock one more node and stay in lock limits.
        bool CheckLimit(ui32 limit, NKikimrCms::EAvailabilityMode mode) const;
        // Check if we can lock one more node and stay in lock ration limits.
        bool CheckRatio(ui32 ratio, NKikimrCms::EAvailabilityMode mode) const;
    };

    ITransaction *CreateTxGetLogTail(TEvCms::TEvGetLogTailRequest::TPtr &ev);
    ITransaction *CreateTxInitScheme();
    ITransaction *CreateTxLoadState();
    ITransaction *CreateTxLogAndSend(TEvPrivate::TEvLogAndSend::TPtr &ev);
    ITransaction *CreateTxLogCleanup();
    ITransaction *CreateTxProcessNotification(TEvCms::TEvNotification::TPtr &ev);
    ITransaction *CreateTxRejectNotification(TEvCms::TEvManageNotificationRequest::TPtr &ev);
    ITransaction *CreateTxRemoveExpiredNotifications();
    ITransaction *CreateTxRemoveRequest(const TString &id, THolder<IEventBase> req, TAutoPtr<IEventHandle> resp);
    ITransaction *CreateTxRemovePermissions(TVector<TString> ids, THolder<IEventBase> req, TAutoPtr<IEventHandle> resp, bool expired = false);
    ITransaction *CreateTxRemoveWalleTask(const TString &id);
    ITransaction *CreateTxStorePermissions(THolder<IEventBase> req, TAutoPtr<IEventHandle> resp,
                                           const TString &owner, TAutoPtr<TRequestInfo> scheduled);
    ITransaction *CreateTxStoreWalleTask(const TWalleTaskInfo &task, THolder<IEventBase> req, TAutoPtr<IEventHandle> resp);
    ITransaction *CreateTxUpdateConfig(TEvCms::TEvSetConfigRequest::TPtr &ev);
    ITransaction *CreateTxUpdateConfig(TEvConsole::TEvConfigNotificationRequest::TPtr &ev);
    ITransaction *CreateTxUpdateDowntimes();

    static void AuditLog(const TActorContext& ctx, const TString& message) {
        NCms::AuditLog("CMS tablet", message, ctx);
    }

    static void AuditLog(const IEventBase* request, const IEventBase* response, const TActorContext& ctx) {
        return AuditLog(ctx, TStringBuilder() << "Reply"
            << ": request# " << request->ToString()
            << ", response# " << response->ToString());
    }

    static void Reply(const IEventBase* request, TAutoPtr<IEventHandle> response, const TActorContext& ctx) {
        AuditLog(request, response->GetBase(), ctx);
        ctx.Send(response);
    }

    template <typename TEvRequestPtr>
    static void Reply(TEvRequestPtr& request, THolder<IEventBase> response, const TActorContext& ctx) {
        AuditLog(request->Get(), response.Get(), ctx);
        ctx.Send(request->Sender, response.Release(), 0, request->Cookie);
    }

    template <typename TEvResponse, typename TEvRequestPtr>
    static void ReplyWithError(TEvRequestPtr& ev, EStatusCode code, const TString& reason, const TActorContext& ctx) {
        auto response = MakeHolder<TEvResponse>();
        response->Record.MutableStatus()->SetCode(code);
        response->Record.MutableStatus()->SetReason(reason);

        Reply<TEvRequestPtr>(ev, std::move(response), ctx);
    }

    STFUNC(StateInit)
    {
        LOG_DEBUG(ctx, NKikimrServices::CMS, "StateInit event type: %" PRIx32 " event: %s",
                  ev->GetTypeRewrite(), ev->HasEvent() ? ev->GetBase()->ToString().data() : "serialized?");
        StateInitImpl(ev, ctx);
    }

    template <typename TEvRequest, typename TEvResponse>
    void HandleNotSupported(typename TEvRequest::TPtr& ev, const TActorContext& ctx) {
        ReplyWithError<TEvResponse, typename TEvRequest::TPtr>(ev, NKikimrCms::TStatus::ERROR, NotSupportedReason, ctx);
    }

    STFUNC(StateNotSupported)
    {
        switch (ev->GetTypeRewrite()) {
            HFunc(TEvCms::TEvClusterStateRequest, (HandleNotSupported<TEvCms::TEvClusterStateRequest,
                                                                      TEvCms::TEvClusterStateResponse>));
            HFunc(TEvCms::TEvPermissionRequest, (HandleNotSupported<TEvCms::TEvPermissionRequest,
                                                                    TEvCms::TEvPermissionResponse>));
            HFunc(TEvCms::TEvManageRequestRequest, (HandleNotSupported<TEvCms::TEvManageRequestRequest,
                                                                       TEvCms::TEvManageRequestResponse>));
            HFunc(TEvCms::TEvCheckRequest, (HandleNotSupported<TEvCms::TEvCheckRequest,
                                                               TEvCms::TEvPermissionResponse>));
            HFunc(TEvCms::TEvManagePermissionRequest, (HandleNotSupported<TEvCms::TEvManagePermissionRequest,
                                                                          TEvCms::TEvManagePermissionResponse>));
            HFunc(TEvCms::TEvConditionalPermissionRequest, (HandleNotSupported<TEvCms::TEvConditionalPermissionRequest,
                                                                               TEvCms::TEvPermissionResponse>));
            HFunc(TEvCms::TEvNotification, (HandleNotSupported<TEvCms::TEvNotification,
                                                               TEvCms::TEvNotificationResponse>));
            HFunc(TEvCms::TEvManageNotificationRequest, (HandleNotSupported<TEvCms::TEvManageNotificationRequest,
                                                                            TEvCms::TEvManageNotificationResponse>));
            IgnoreFunc(TEvTabletPipe::TEvServerConnected);
            IgnoreFunc(TEvTabletPipe::TEvServerDisconnected);

        default:
            if (!HandleDefaultEvents(ev, ctx)) {
                LOG_DEBUG(ctx, NKikimrServices::CMS, "StateNotSupported unexpected event type: %" PRIx32 " event: %s",
                          ev->GetTypeRewrite(), ev->HasEvent() ? ev->GetBase()->ToString().data() : "serialized?");
            }
        }
    }

    STFUNC(StateWork)
    {
        switch (ev->GetTypeRewrite()) {
            HFunc(TEvPrivate::TEvClusterInfo, Handle);
            HFunc(TEvPrivate::TEvLogAndSend, Handle);
            FFunc(TEvPrivate::EvUpdateClusterInfo, EnqueueRequest);
            CFunc(TEvPrivate::EvCleanupExpired, CleanupExpired);
            CFunc(TEvPrivate::EvCleanupLog, CleanupLog);
            CFunc(TEvPrivate::EvCleanupWalle, CleanupWalleTasks);
            CFunc(TEvPrivate::EvStartCollecting, StartCollecting); 
            FFunc(TEvCms::EvClusterStateRequest, EnqueueRequest);
            HFunc(TEvCms::TEvPermissionRequest, CheckAndEnqueueRequest);
            HFunc(TEvCms::TEvManageRequestRequest, Handle);
            HFunc(TEvCms::TEvCheckRequest, CheckAndEnqueueRequest);
            HFunc(TEvCms::TEvManagePermissionRequest, Handle);
            HFunc(TEvCms::TEvConditionalPermissionRequest, CheckAndEnqueueRequest);
            HFunc(TEvCms::TEvNotification, CheckAndEnqueueRequest);
            HFunc(TEvCms::TEvManageNotificationRequest, Handle);
            HFunc(TEvCms::TEvWalleCreateTaskRequest, Handle);
            HFunc(TEvCms::TEvWalleListTasksRequest, Handle);
            HFunc(TEvCms::TEvWalleCheckTaskRequest, Handle);
            HFunc(TEvCms::TEvWalleRemoveTaskRequest, Handle);
            HFunc(TEvCms::TEvStoreWalleTask, Handle);
            HFunc(TEvCms::TEvRemoveWalleTask, Handle);
            HFunc(TEvCms::TEvGetConfigRequest, Handle);
            HFunc(TEvCms::TEvSetConfigRequest, Handle);
            HFunc(TEvCms::TEvResetMarkerRequest, Handle);
            HFunc(TEvCms::TEvSetMarkerRequest, Handle);
            HFunc(TEvCms::TEvGetLogTailRequest, Handle);
            HFunc(TEvCms::TEvGetSentinelStateRequest, Handle);
            FFunc(TEvCms::EvGetClusterInfoRequest, EnqueueRequest);
            HFunc(TEvConsole::TEvConfigNotificationRequest, Handle);
            HFunc(TEvConsole::TEvReplaceConfigSubscriptionsResponse, Handle);
            HFunc(TEvents::TEvPoisonPill, Handle);
            HFunc(TEvTabletPipe::TEvClientDestroyed, Handle);
            HFunc(TEvTabletPipe::TEvClientConnected, Handle);
            IgnoreFunc(TEvTabletPipe::TEvServerConnected);
            IgnoreFunc(TEvTabletPipe::TEvServerDisconnected);

        default:
            if (!HandleDefaultEvents(ev, ctx)) {
                LOG_DEBUG(ctx, NKikimrServices::CMS, "StateWork unexpected event type: %" PRIx32 " event: %s",
                          ev->GetTypeRewrite(), ev->HasEvent() ? ev->GetBase()->ToString().data() : "serialized?");
            }
        }
    }

    void OnActivateExecutor(const TActorContext &ctx) override;
    void OnDetach(const TActorContext &ctx) override;
    void OnTabletDead(TEvTablet::TEvTabletDead::TPtr &ev, const TActorContext &ctx) override;

    void Enqueue(TAutoPtr<IEventHandle> &ev,
                 const TActorContext &ctx) override;
    void ProcessInitQueue(const TActorContext &ctx);

    void SubscribeForConfig(const TActorContext &ctx);
    void AdjustInfo(TClusterInfoPtr &info, const TActorContext &ctx) const;
    bool CheckPermissionRequest(const NKikimrCms::TPermissionRequest &request,
                                NKikimrCms::TPermissionResponse &response,
                                NKikimrCms::TPermissionRequest &scheduled,
                                const TActorContext &ctx);
    bool IsActionHostValid(const NKikimrCms::TAction &action, TErrorInfo &error) const;
    bool ParseServices(const NKikimrCms::TAction &action, TServices &services,
                       TErrorInfo &error) const;

    bool CheckAccess(const TString &token,
                     NKikimrCms::TStatus::ECode &code,
                     TString &error,
                     const TActorContext &ctx);
    bool CheckAction(const NKikimrCms::TAction &action, const TActionOptions &options,
                     TErrorInfo &error, const TActorContext &ctx) const;
    bool CheckActionShutdownNode(const NKikimrCms::TAction &action,
                                 const TActionOptions &options,
                                 const TNodeInfo &node,
                                 TErrorInfo &error,
                                 const TActorContext &ctx) const;
    bool CheckActionRestartServices(const NKikimrCms::TAction &action,
                                    const TActionOptions &options,
                                    TErrorInfo &error,
                                    const TActorContext &ctx) const;
    bool CheckActionShutdownHost(const NKikimrCms::TAction &action,
                                 const TActionOptions &options,
                                 TErrorInfo &error,
                                 const TActorContext &ctx) const;
    bool CheckActionReplaceDevices(const NKikimrCms::TAction &action,
                                   const TActionOptions &options,
                                   TErrorInfo &error) const;
    bool CheckSysTabletsNode(const NKikimrCms::TAction &action,
                             const TActionOptions &opts,
                             const TNodeInfo &node, 
                             TErrorInfo &error) const;
    bool TryToLockNode(const NKikimrCms::TAction &action,
                       const TActionOptions &options,
                       const TNodeInfo &node,
                       TErrorInfo& error) const;
    bool TryToLockPDisk(const NKikimrCms::TAction &action,
                        const TActionOptions &options,
                        const TPDiskInfo &pdisk,
                        TErrorInfo &error) const;
    bool TryToLockVDisks(const NKikimrCms::TAction &action,
                         const TActionOptions &options,
                         const TSet<TVDiskID> &vdisks,
                         TErrorInfo &error) const;
    bool TryToLockVDisk(const TActionOptions &options,
                        const TVDiskInfo &vdisk,
                        TDuration duration,
                        TErrorInfo &error) const;
    bool TryToLockStateStorageReplica(const NKikimrCms::TAction &action,
                                      const TActionOptions &options,
                                      const TNodeInfo &node, 
                                      TErrorInfo& error,
                                      const TActorContext &ctx) const;
    void AcceptPermissions(NKikimrCms::TPermissionResponse &resp, const TString &requestId,
                           const TString &owner, const TActorContext &ctx, bool check = false);
    void ScheduleUpdateClusterInfo(const TActorContext &ctx, bool now = false);
    void ScheduleCleanup(TInstant time, const TActorContext &ctx);
    void SchedulePermissionsCleanup(const TActorContext &ctx);
    void ScheduleNotificationsCleanup(const TActorContext &ctx);
    void CleanupExpired(const TActorContext &ctx);
    void CleanupLog(const TActorContext &ctx);
    void ScheduleLogCleanup(const TActorContext &ctx);
    void DoPermissionsCleanup(const TActorContext &ctx);
    void CleanupWalleTasks(const TActorContext &ctx);
    void RemoveEmptyWalleTasks(const TActorContext &ctx);
    void StartCollecting(const TActorContext &ctx);
    bool CheckNotificationDeadline(const NKikimrCms::TAction &action, TInstant time,
                                   TErrorInfo &error, const TActorContext &ctx) const;
    bool CheckNotificationRestartServices(const NKikimrCms::TAction &action, TInstant time,
                                          TErrorInfo &error, const TActorContext &ctx) const;
    bool CheckNotificationShutdownHost(const NKikimrCms::TAction &action, TInstant time,
                                       TErrorInfo &error, const TActorContext &ctx) const;
    bool CheckNotificationReplaceDevices(const NKikimrCms::TAction &action, TInstant time,
                                         TErrorInfo &error, const TActorContext &ctx) const;
    bool IsValidNotificationAction(const NKikimrCms::TAction &action, TInstant time,
                                   TErrorInfo &error, const TActorContext &ctx) const;
    TString AcceptNotification(const NKikimrCms::TNotification &notification,
                               const TActorContext &ctx);
    bool CheckNotification(const NKikimrCms::TNotification &notification,
                           NKikimrCms::TNotificationResponse &resp,
                           const TActorContext &ctx) const;

    void Cleanup(const TActorContext &ctx);
    void Die(const TActorContext& ctx) override;

    void GetPermission(TEvCms::TEvManagePermissionRequest::TPtr &ev, bool all, const TActorContext &ctx);
    void RemovePermission(TEvCms::TEvManagePermissionRequest::TPtr &ev, bool done, const TActorContext &ctx);
    void GetRequest(TEvCms::TEvManageRequestRequest::TPtr &ev, bool all, const TActorContext &ctx);
    void RemoveRequest(TEvCms::TEvManageRequestRequest::TPtr &ev, const TActorContext &ctx);
    void GetNotifications(TEvCms::TEvManageNotificationRequest::TPtr &ev, bool all,
                          const TActorContext &ctx);
    bool RemoveNotification(const TString &id, const TString &user, bool remove, TErrorInfo &error);

    void EnqueueRequest(TAutoPtr<IEventHandle> ev, const TActorContext &ctx);
    void CheckAndEnqueueRequest(TEvCms::TEvPermissionRequest::TPtr &ev, const TActorContext &ctx);
    void CheckAndEnqueueRequest(TEvCms::TEvCheckRequest::TPtr &ev, const TActorContext &ctx);
    void CheckAndEnqueueRequest(TEvCms::TEvConditionalPermissionRequest::TPtr &ev, const TActorContext &ctx);
    void CheckAndEnqueueRequest(TEvCms::TEvNotification::TPtr &ev, const TActorContext &ctx);
    void ProcessQueue(const TActorContext &ctx);
    void ProcessRequest(TAutoPtr<IEventHandle> &ev, const TActorContext &ctx);

    void AddPermissionExtensions(const NKikimrCms::TAction& action, NKikimrCms::TPermission& perm) const;
    void AddHostExtensions(const TString& host, NKikimrCms::TPermission& perm) const;

    void OnBSCPipeDestroyed(const TActorContext &ctx);

    void Handle(TEvPrivate::TEvClusterInfo::TPtr &ev, const TActorContext &ctx);
    void Handle(TEvPrivate::TEvLogAndSend::TPtr &ev, const TActorContext &ctx);
    void Handle(TEvPrivate::TEvUpdateClusterInfo::TPtr &ev, const TActorContext &ctx);
    void Handle(TEvCms::TEvManageRequestRequest::TPtr &ev, const TActorContext &ctx);
    void Handle(TEvCms::TEvManagePermissionRequest::TPtr &ev, const TActorContext &ctx);
    void Handle(TEvCms::TEvClusterStateRequest::TPtr &ev, const TActorContext &ctx);
    void Handle(TEvCms::TEvPermissionRequest::TPtr &ev, const TActorContext &ctx);
    void Handle(TEvCms::TEvCheckRequest::TPtr &ev, const TActorContext &ctx);
    void Handle(TEvCms::TEvNotification::TPtr &ev, const TActorContext &ctx);
    void Handle(TEvCms::TEvManageNotificationRequest::TPtr &ev, const TActorContext &ctx);
    void Handle(TEvCms::TEvWalleCreateTaskRequest::TPtr &ev, const TActorContext &ctx);
    void Handle(TEvCms::TEvWalleListTasksRequest::TPtr &ev, const TActorContext &ctx);
    void Handle(TEvCms::TEvWalleCheckTaskRequest::TPtr &ev, const TActorContext &ctx);
    void Handle(TEvCms::TEvWalleRemoveTaskRequest::TPtr &ev, const TActorContext &ctx);
    void Handle(TEvCms::TEvStoreWalleTask::TPtr &ev, const TActorContext &ctx);
    void Handle(TEvCms::TEvRemoveWalleTask::TPtr &ev, const TActorContext &ctx);
    void Handle(TEvCms::TEvGetConfigRequest::TPtr &ev, const TActorContext &ctx);
    void Handle(TEvCms::TEvSetConfigRequest::TPtr &ev, const TActorContext &ctx);
    void Handle(TEvCms::TEvResetMarkerRequest::TPtr &ev, const TActorContext &ctx);
    void Handle(TEvCms::TEvSetMarkerRequest::TPtr &ev, const TActorContext &ctx);
    void Handle(TEvCms::TEvGetLogTailRequest::TPtr &ev, const TActorContext &ctx);
    void Handle(TEvCms::TEvGetSentinelStateRequest::TPtr &ev, const TActorContext &ctx);
    void Handle(TEvCms::TEvGetClusterInfoRequest::TPtr &ev, const TActorContext &ctx);
    void Handle(TEvConsole::TEvConfigNotificationRequest::TPtr &ev,
                const TActorContext &ctx);
    void Handle(TEvConsole::TEvReplaceConfigSubscriptionsResponse::TPtr &ev,
                const TActorContext &ctx);
    void Handle(TEvents::TEvPoisonPill::TPtr &ev,
                const TActorContext &ctx);
    void Handle(TEvTabletPipe::TEvClientDestroyed::TPtr &ev, const TActorContext &ctx);
    void Handle(TEvTabletPipe::TEvClientConnected::TPtr &ev, const TActorContext &ctx);

private:
    TStack<TInstant> ScheduledCleanups;
    TString NotSupportedReason;
    TQueue<TAutoPtr<IEventHandle>> InitQueue;

    TQueue<TAutoPtr<IEventHandle>> Queue;
    TQueue<TAutoPtr<IEventHandle>> NextQueue;

    TCmsStatePtr State;
    TLogger Logger;
    // Shortcut to State->ClusterInfo.
    TClusterInfoPtr ClusterInfo;
    ui64 ConfigSubscriptionId;
    TSchedulerCookieHolder WalleCleanupTimerCookieHolder;
    TSchedulerCookieHolder LogCleanupTimerCookieHolder;

    // State Storage
    TIntrusiveConstPtr<TStateStorageInfo> StateStorageInfo;
    THashMap<ui32, ui32> NodeToRing;
    THashSet<ui32> StateStorageNodes;

    // Monitoring
    THolder<class NKikimr::TTabletCountersBase> TabletCountersPtr;
    TTabletCountersBase* TabletCounters;

public:
    TCms(const TActorId &tablet, TTabletStorageInfo *info)
        : TActor(&TThis::StateInit)
        , TTabletExecutedFlat(info, tablet, new NMiniKQL::TMiniKQLFactory)
        , State(new TCmsState)
        , Logger(State)
        , ConfigSubscriptionId(0)
    {
        TabletCountersPtr.Reset(new TProtobufTabletCounters<
            ESimpleCounters_descriptor,
            ECumulativeCounters_descriptor,
            EPercentileCounters_descriptor,
            ETxTypes_descriptor>());
        TabletCounters = TabletCountersPtr.Get();
    }

    static constexpr NKikimrServices::TActivity::EType ActorActivityType()
    {
        return NKikimrServices::TActivity::CMS_SERVICE;
    }

};

} // namespace NKikimr::NCms