aboutsummaryrefslogtreecommitdiffstats
path: root/ydb/core/kesus/tablet/tablet_impl.h
blob: c279eaa27f3d3b4111234ad7a11c4f7eff6e9f4c (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
#pragma once

#include "defs.h"

#include "events.h"
#include "quoter_resource_tree.h"
#include "schema.h"

#include <ydb/core/base/path.h>
#include <ydb/core/base/tablet_pipe.h>
#include <ydb/core/protos/counters_kesus.pb.h>
#include <ydb/core/tablet/tablet_counters_aggregator.h>
#include <ydb/core/tablet/tablet_counters_protobuf.h>
#include <ydb/core/tablet_flat/flat_executor_counters.h>
#include <ydb/core/tablet_flat/tablet_flat_executed.h>

#include <library/cpp/actors/core/interconnect.h>
#include <library/cpp/actors/core/scheduler_cookie.h>

#include <util/generic/hash.h>

namespace NKikimr {
namespace NKesus {

class TKesusTablet : public TActor<TKesusTablet>, public NTabletFlatExecutor::TTabletExecutedFlat {
private:
    using Schema = TKesusSchema;
    using TTxBase = NTabletFlatExecutor::TTransactionBase<TKesusTablet>;

    struct TTxInitSchema;
    struct TTxInit;
    struct TTxSelfCheck;

    struct TTxDummy;

    struct TTxConfigGet;
    struct TTxConfigSet;

    struct TTxSessionAttach;
    struct TTxSessionDestroy;
    struct TTxSessionDetach;
    struct TTxSessionTimeout;
    struct TTxSessionsDescribe;

    struct TTxSemaphoreAcquire;
    struct TTxSemaphoreCreate;
    struct TTxSemaphoreDescribe;
    struct TTxSemaphoreDelete;
    struct TTxSemaphoreRelease;
    struct TTxSemaphoreTimeout;
    struct TTxSemaphoreUpdate;

    struct TTxQuoterResourceAdd;
    struct TTxQuoterResourceUpdate;
    struct TTxQuoterResourceDelete;
    struct TTxQuoterResourceDescribe;
    class TQuoterResourceSink;

    static constexpr ui64 QUOTER_TICK_PROCESSING_WAKEUP_TAG = 1;

    static constexpr size_t MAX_SESSIONS_LIMIT = 1000000; // 1 million
    static constexpr size_t MAX_DESCRIPTION_SIZE = 1024;
    static constexpr size_t MAX_PROTECTION_KEY_SIZE = 16;

    static constexpr size_t MAX_SEMAPHORE_NAME = 1024; // 1KB
    static constexpr size_t MAX_SEMAPHORE_DATA = 1024 * 1024; // 1MB
    static constexpr size_t MAX_SEMAPHORES_LIMIT = 1000000; // 1 million

    static constexpr TDuration MIN_SELF_CHECK_PERIOD = TDuration::MilliSeconds(500);
    static constexpr TDuration MAX_SELF_CHECK_PERIOD = TDuration::Seconds(10);
    static constexpr TDuration MIN_SESSION_GRACE_PERIOD = TDuration::Seconds(1);
    static constexpr TDuration MAX_SESSION_GRACE_PERIOD = TDuration::Seconds(30);

    static constexpr TDuration MAX_SESSION_TIMEOUT = TDuration::Minutes(30);
    static constexpr TDuration MAX_ACQUIRE_TIMEOUT = TDuration::Days(1);

    struct THtmlRenderer;

    struct TDelayedEvent {
        const TActorId Recipient;
        const ui64 Cookie;
        THolder<IEventBase> Event;

        TDelayedEvent(const TActorId& recipient, ui64 cookie, IEventBase* event)
            : Recipient(recipient)
            , Cookie(cookie)
            , Event(event)
        {}
    };

    struct TProxyInfo {
        TActorId ActorID;
        ui64 Generation = 0;
        THashSet<ui64> AttachedSessions;
    };

    struct TSemaphoreInfo;

    struct TSemaphoreOwnerInfo {
        ui64 OrderId;
        ui64 SessionId;
        ui64 Count;
        TString Data;
    };

    struct TSemaphoreWaiterInfo {
        ui64 OrderId;
        ui64 SessionId;
        ui64 TimeoutMillis;
        ui64 Count;
        TString Data;
        TSchedulerCookieHolder TimeoutCookie;
        TInstant ScheduledTimeoutDeadline;
    };

    struct TSessionInfo {
        ui64 Id;
        ui64 TimeoutMillis;
        TString Description;
        TString ProtectionKey;
        THashMap<ui64, TSemaphoreOwnerInfo> OwnedSemaphores;
        THashMap<ui64, TSemaphoreWaiterInfo> WaitingSemaphores;
        TSchedulerCookieHolder TimeoutCookie;
        TInstant ScheduledTimeoutDeadline;

        TProxyInfo* OwnerProxy;
        ui64 OwnerCookie = 0;
        THashMap<TSemaphoreInfo*, ui64> SemaphoreWaitCookie;
        THashMap<TSemaphoreInfo*, ui64> SemaphoreWatchCookie;

        TActorId LastOwnerProxy;
        ui64 LastOwnerSeqNo = 0;

        void ClearWatchCookies();

        bool DetachProxy() {
            SemaphoreWaitCookie.clear();
            ClearWatchCookies();
            if (OwnerProxy) {
                OwnerProxy->AttachedSessions.erase(Id);
                OwnerProxy = nullptr;
                OwnerCookie = 0;
                return true;
            } else {
                return false;
            }
        }

        void AttachProxy(TProxyInfo* proxy, ui64 cookie, ui64 seqNo) {
            Y_ABORT_UNLESS(proxy);
            if (OwnerProxy != proxy) {
                if (OwnerProxy) {
                    OwnerProxy->AttachedSessions.erase(Id);
                }
                OwnerProxy = proxy;
                OwnerProxy->AttachedSessions.insert(Id);
            }
            OwnerProxy = proxy;
            OwnerCookie = cookie;
            SemaphoreWaitCookie.clear();
            ClearWatchCookies();
            LastOwnerProxy = OwnerProxy->ActorID;
            LastOwnerSeqNo = seqNo;
            TimeoutCookie.Detach();
        }

        template<class Callback>
        void ConsumeSemaphoreWaitCookie(TSemaphoreInfo* semaphore, Callback&& callback) {
            auto it = SemaphoreWaitCookie.find(semaphore);
            if (it != SemaphoreWaitCookie.end()) {
                ui64 cookie = it->second;
                SemaphoreWaitCookie.erase(it);
                callback(cookie);
            }
        }

        ui64 RemoveSemaphoreWatchCookie(TSemaphoreInfo* semaphore) {
            auto it = SemaphoreWatchCookie.find(semaphore);
            Y_ABORT_UNLESS(it != SemaphoreWatchCookie.end());
            ui64 cookie = it->second;
            SemaphoreWatchCookie.erase(it);
            return cookie;
        }
    };

    struct TSemaphoreInfo {
        ui64 Id;
        TString Name;
        TString Data;
        ui64 Limit;
        ui64 Count;
        bool Ephemeral;

        THashSet<TSemaphoreOwnerInfo*> Owners;
        TMap<ui64, TSemaphoreWaiterInfo*> Waiters;

        THashSet<TSessionInfo*> DataWatchers;
        THashSet<TSessionInfo*> OwnersWatchers;

        bool IsEmpty() const {
            return Owners.empty() && Waiters.empty();
        }

        ui64 GetFirstOrderId() const {
            if (!Waiters.empty()) {
                return Waiters.begin()->first;
            }
            return 0;
        }

        bool CanAcquire(ui64 count) const {
            Y_ABORT_UNLESS(count <= Limit);
            return count <= (Limit - Count);
        }

        void NotifyWatchers(TVector<TDelayedEvent>& events, bool dataChanged, bool ownerChanged);
    };

    struct TQuoterResourceSessionsAccumulator {
        void Accumulate(const TActorId& recipient, ui64 resourceId, double amount, const NKikimrKesus::TStreamingQuoterResource* props);
        void Sync(const TActorId& recipient, ui64 resourceId, ui32 lastReportId, double amount);
        void SendAll(const TActorContext& ctx, ui64 tabletId);

        struct TSendInfo {
            THolder<TEvKesus::TEvResourcesAllocated> Event;
            THashMap<ui64, size_t> ResIdIndex;
        };

        struct TSendSyncInfo {
            THolder<TEvKesus::TEvSyncResources> Event;
            THashMap<ui64, size_t> ResIdIndex;
        };

        THashMap<TActorId, TSendInfo> SendInfos;
        THashMap<TActorId, TSendSyncInfo> SendSyncInfos;
    };

    struct TEvPrivate {
        enum EEv {
            EvSessionTimeout = EventSpaceBegin(TEvents::ES_PRIVATE),
            EvAcquireSemaphoreTimeout,
            EvSelfCheckStart,
            EvSelfCheckTimeout,

            EvEnd
        };

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

        struct TEvSessionTimeout : public TEventLocal<TEvSessionTimeout, EvSessionTimeout> {
            const ui64 SessionId;
            TSchedulerCookieHolder Cookie;

            TEvSessionTimeout(ui64 sessionId, ISchedulerCookie* cookie)
                : SessionId(sessionId)
                , Cookie(cookie)
            {}
        };

        struct TEvAcquireSemaphoreTimeout : public TEventLocal<TEvAcquireSemaphoreTimeout, EvAcquireSemaphoreTimeout> {
            const ui64 SessionId;
            const ui64 SemaphoreId;
            TSchedulerCookieHolder Cookie;

            TEvAcquireSemaphoreTimeout(ui64 sessionId, ui64 semaphoreId, ISchedulerCookie* cookie)
                : SessionId(sessionId)
                , SemaphoreId(semaphoreId)
                , Cookie(cookie)
            {}
        };

        struct TEvSelfCheckStart : public TEventLocal<TEvSelfCheckStart, EvSelfCheckStart> {
            const TInstant Deadline;

            explicit TEvSelfCheckStart(TInstant deadline)
                : Deadline(deadline)
            {}
        };

        struct TEvSelfCheckTimeout : public TEventLocal<TEvSelfCheckTimeout, EvSelfCheckTimeout> {
            TSchedulerCookieHolder Cookie;

            explicit TEvSelfCheckTimeout(ISchedulerCookie* cookie)
                : Cookie(cookie)
            {}
        };
    };

private:
    TString KesusPath;
    ui64 ConfigVersion;
    ui64 NextSessionId;
    ui64 NextSemaphoreId;
    ui64 NextSemaphoreOrderId;
    TDuration SelfCheckPeriod;
    TDuration SessionGracePeriod;
    Ydb::Coordination::ConsistencyMode ReadConsistencyMode;
    Ydb::Coordination::ConsistencyMode AttachConsistencyMode;
    THashMap<ui64, TSessionInfo> Sessions;
    THashMap<ui64, TSemaphoreInfo> Semaphores;
    THashMap<TString, TSemaphoreInfo*> SemaphoresByName;
    THashMap<TActorId, TProxyInfo> Proxies;
    THashMap<ui32, THashSet<TProxyInfo*>> ProxiesByNode;
    THashMap<ui64, ui64> SessionsTxCount;

    // Quoter support
    ui64 NextQuoterResourceId;
    TQuoterResources QuoterResources;
    TInstant NextQuoterTickTime = TInstant::Max();
    TTickProcessorQueue QuoterTickProcessorQueue;
    TQuoterResourceSessionsAccumulator QuoterResourceSessionsAccumulator;
    Ydb::Coordination::RateLimiterCountersMode RateLimiterCountersMode;
    bool QuoterTickProcessingIsScheduled = false;

    // Counters support
    THolder<TTabletCountersBase> TabletCountersPtr;
    TTabletCountersBase* TabletCounters;

    // Self check support
    ui64 SelfCheckCounter = 0;
    bool SelfCheckPending = false;

    ui64 StrictMarkerCounter = 0;

public:
    TKesusTablet(const TActorId& tablet, TTabletStorageInfo* info);
    virtual ~TKesusTablet();

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

private:
    void OnDetach(const TActorContext& ctx) override;
    void OnTabletDead(TEvTablet::TEvTabletDead::TPtr& ev, const TActorContext& ctx) override;
    void OnActivateExecutor(const TActorContext& ctx) override;
    void DefaultSignalTabletActive(const TActorContext& ctx) override;
    bool OnRenderAppHtmlPage(NMon::TEvRemoteHttpInfo::TPtr ev, const TActorContext& ctx) override;

private:
    NTabletFlatExecutor::ITransaction* CreateTxInitSchema();
    NTabletFlatExecutor::ITransaction* CreateTxInit();

private:
    void ResetState();
    void ResetCounters();

    bool UseStrictRead() const {
        return ReadConsistencyMode == Ydb::Coordination::CONSISTENCY_MODE_STRICT;
    }

    bool UseStrictAttach() const {
        return AttachConsistencyMode == Ydb::Coordination::CONSISTENCY_MODE_STRICT;
    }

    void PersistSysParam(NIceDb::TNiceDb& db, ui64 id, const TString& value);
    void PersistDeleteSession(NIceDb::TNiceDb& db, ui64 sessionId);
    void PersistDeleteSessionSemaphore(NIceDb::TNiceDb& db, ui64 sessionId, ui64 semaphoreId);
    void PersistDeleteSemaphore(NIceDb::TNiceDb& db, ui64 semaphoreId);

    void PersistStrictMarker(NIceDb::TNiceDb& db);

    void DoDeleteSession(
        NIceDb::TNiceDb& db, TSessionInfo* session, TVector<TDelayedEvent>& events);
    void DoDeleteSemaphore(
        NIceDb::TNiceDb& db, TSemaphoreInfo* semaphore, TVector<TDelayedEvent>& events);
    void DoDeleteSessionSemaphore(
        NIceDb::TNiceDb& db, TSemaphoreInfo* semaphore, TSemaphoreOwnerInfo* owner, TVector<TDelayedEvent>& events);
    void DoDeleteSessionSemaphore(
        NIceDb::TNiceDb& db, TSemaphoreInfo* semaphore, TSemaphoreWaiterInfo* waiter, TVector<TDelayedEvent>& events);
    void DoProcessSemaphoreQueue(
        TSemaphoreInfo* semaphore, TVector<TDelayedEvent>& events, bool ownersChanged = false);

private:
    bool ScheduleSelfCheck(const TActorContext& ctx);
    bool ScheduleWaiterTimeout(ui64 semaphoreId, TSemaphoreWaiterInfo* waiter, const TActorContext& ctx);
    bool ScheduleSessionTimeout(TSessionInfo* session, const TActorContext& ctx, TDuration gracePeriod = TDuration::Zero());
    void ClearProxy(TProxyInfo* proxy, const TActorContext& ctx);
    void ForgetProxy(TProxyInfo* proxy);
    void ScheduleQuoterTick();

private:
    void VerifyKesusPath(const TString& kesusPath);

    void Handle(TEvents::TEvUndelivered::TPtr& ev);
    void Handle(TEvInterconnect::TEvNodeConnected::TPtr& ev);
    void Handle(TEvInterconnect::TEvNodeDisconnected::TPtr& ev);
    void Handle(TEvents::TEvWakeup::TPtr& ev);

    void Handle(TEvKesus::TEvDummyRequest::TPtr& ev);
    void Handle(TEvKesus::TEvSetConfig::TPtr& ev);
    void Handle(TEvKesus::TEvGetConfig::TPtr& ev);
    void Handle(TEvKesus::TEvDescribeSemaphore::TPtr& ev);
    void Handle(TEvKesus::TEvDescribeProxies::TPtr& ev);
    void Handle(TEvKesus::TEvDescribeSessions::TPtr& ev);
    void Handle(TEvKesus::TEvRegisterProxy::TPtr& ev);
    void Handle(TEvKesus::TEvUnregisterProxy::TPtr& ev);
    void Handle(TEvKesus::TEvAttachSession::TPtr& ev);
    void Handle(TEvKesus::TEvDetachSession::TPtr& ev);
    void Handle(TEvKesus::TEvDestroySession::TPtr& ev);
    void Handle(TEvKesus::TEvAcquireSemaphore::TPtr& ev);
    void Handle(TEvKesus::TEvReleaseSemaphore::TPtr& ev);
    void Handle(TEvKesus::TEvCreateSemaphore::TPtr& ev);
    void Handle(TEvKesus::TEvUpdateSemaphore::TPtr& ev);
    void Handle(TEvKesus::TEvDeleteSemaphore::TPtr& ev);

    // Quoter API
    void Handle(TEvKesus::TEvDescribeQuoterResources::TPtr& ev);
    void Handle(TEvKesus::TEvAddQuoterResource::TPtr& ev);
    void Handle(TEvKesus::TEvUpdateQuoterResource::TPtr& ev);
    void Handle(TEvKesus::TEvDeleteQuoterResource::TPtr& ev);
    // Quoter runtime
    void Handle(TEvKesus::TEvSubscribeOnResources::TPtr& ev);
    void Handle(TEvKesus::TEvUpdateConsumptionState::TPtr& ev);
    void Handle(TEvKesus::TEvAccountResources::TPtr& ev);
    void Handle(TEvKesus::TEvReportResources::TPtr& ev);
    void Handle(TEvKesus::TEvResourcesAllocatedAck::TPtr& ev);
    void Handle(TEvKesus::TEvGetQuoterResourceCounters::TPtr& ev);
    void Handle(TEvTabletPipe::TEvServerDisconnected::TPtr& ev);
    void HandleQuoterTick();

    void Handle(TEvPrivate::TEvSessionTimeout::TPtr& ev);
    void Handle(TEvPrivate::TEvAcquireSemaphoreTimeout::TPtr& ev);
    void Handle(TEvPrivate::TEvSelfCheckStart::TPtr& ev);
    void Handle(TEvPrivate::TEvSelfCheckTimeout::TPtr& ev);

    // Ignored messages for compatibility
    void HandleIgnored();

private:
    STFUNC(StateInit);
    STFUNC(StateWork);

private:
    void AddSessionTx(ui64 sessionId) {
        ++SessionsTxCount[sessionId];
    }

    ui64 GetSessionTxCount(ui64 sessionId) {
        return SessionsTxCount.Value(sessionId, 0);
    }

    void RemoveSessionTx(ui64 sessionId) {
        auto it = SessionsTxCount.find(sessionId);
        Y_ABORT_UNLESS(it != SessionsTxCount.end());
        if (!--it->second) {
            SessionsTxCount.erase(it);
        }
    }

    bool IsSessionFastPathAllowed(ui64 sessionId) {
        return sessionId > 0 && sessionId < NextSessionId && GetSessionTxCount(sessionId) == 0;
    }
};

}
}