aboutsummaryrefslogtreecommitdiffstats
path: root/ydb/core/base/statestorage.h
blob: 3a3c06c3b812bea36ef556d2f5ad7e04386227ff (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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#pragma once
#include "defs.h"
#include "events.h"
#include <ydb/core/protos/statestorage.pb.h>
#include <ydb/core/protos/config.pb.h>
#include <library/cpp/actors/interconnect/event_filter.h>
#include <util/stream/str.h>
#include <util/generic/list.h>
#include <util/generic/map.h>

namespace NKikimr {

struct TEvStateStorage {
    enum EEv {
        // requests (local, to proxy)
        EvLookup = EventSpaceBegin(TKikimrEvents::ES_STATESTORAGE),
        EvUpdate,
        EvLock,
        EvResolveReplicas,
        EvRequestReplicasDumps,
        EvDelete,
        EvCleanup,
        EvResolveBoard,
        EvBoardInfo,
        EvResolveSchemeBoard, // subset (by hash)
        EvListSchemeBoard, // all
        EvUpdateGroupConfig,
        EvListStateStorage,

        // replies (local, from proxy)
        EvInfo = EvLookup + 512,
        EvUpdateSignature,
        EvResolveReplicasList,
        EvResponseReplicasDumps,
        EvDeleteResult,
        EvListSchemeBoardResult,
        EvListStateStorageResult,

        // replicas interface
        EvReplicaLookup = EvLock + 2 * 512,
        EvReplicaUpdate,
        EvReplicaLock,
        EvReplicaLeaderDemoted,
        EvReplicaDumpRequest,
        EvReplicaDump,
        EvReplicaRegFollower,
        EvReplicaUnregFollower,
        EvReplicaDelete,
        EvReplicaCleanup,

        EvReplicaInfo = EvLock + 3 * 512,
        EvReplicaShutdown,

        EvReplicaBoardPublish = EvLock + 4 * 512,
        EvReplicaBoardLookup,
        EvReplicaBoardCleanup,

        EvReplicaBoardPublishAck = EvLock + 5 * 512,
        EvReplicaBoardInfo,

        EvReplicaProbeSubscribe = EvLock + 6 * 512,
        EvReplicaProbeUnsubscribe,
        EvReplicaProbeConnected,
        EvReplicaProbeDisconnected,

        EvEnd
    };

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

    struct TProxyOptions {
        enum ESigWaitMode {
            SigNone,
            SigAsync,
            SigSync,
        };

        ESigWaitMode SigWaitMode;

        TProxyOptions(ESigWaitMode sigWaitMode = SigNone)
            : SigWaitMode(sigWaitMode)
        {}

        TString ToString() const {
            switch (SigWaitMode) {
            case SigNone: return "SigNone";
            case SigAsync: return "SigAsync";
            case SigSync: return "SigSync";
            default: return "Unknown";
            }
        }
    };

    struct TEvLookup : public TEventLocal<TEvLookup, EvLookup> {
        const ui64 TabletID;
        const ui64 Cookie;
        const TProxyOptions ProxyOptions;

        TEvLookup(ui64 tabletId, ui64 cookie, const TProxyOptions &proxyOptions = TProxyOptions())
            : TabletID(tabletId)
            , Cookie(cookie)
            , ProxyOptions(proxyOptions)
        {}

        TString ToString() const {
            TStringStream str;
            str << "{EvLookup TabletID: " << TabletID;
            str << " Cookie: " << Cookie;
            str << " ProxyOptions: " << ProxyOptions.ToString();
            str << "}";
            return str.Str();
        }
    };

    struct TEvUpdate : public TEventLocal<TEvUpdate, EvUpdate> {
        const ui64 TabletID;
        const ui64 Cookie;
        const TActorId ProposedLeader;
        const TActorId ProposedLeaderTablet;
        const ui32 ProposedGeneration;
        const ui32 ProposedStep;
        const ui32 SignatureSz;
        const TArrayHolder<ui64> Signature;
        const TProxyOptions ProxyOptions;

        TEvUpdate(ui64 tabletId, ui64 cookie, const TActorId &leader, const TActorId &leaderTablet, ui32 gen, ui32 step, const ui64 *sig, ui32 sigsz, const TProxyOptions &proxyOptions = TProxyOptions())
            : TabletID(tabletId)
            , Cookie(cookie)
            , ProposedLeader(leader)
            , ProposedLeaderTablet(leaderTablet)
            , ProposedGeneration(gen)
            , ProposedStep(step)
            , SignatureSz(sigsz)
            , Signature(new ui64[sigsz])
            , ProxyOptions(proxyOptions)
        {
            Copy(sig, sig + sigsz, Signature.Get());
        }

        TString ToString() const {
            TStringStream str;
            str << "{EvUpdate TabletID: " << TabletID;
            str << " Cookie: " << Cookie;
            str << " ProposedLeader: " << ProposedLeader.ToString();
            str << " ProposedLeaderTablet: " << ProposedLeaderTablet.ToString();
            str << " ProposedGeneration: " << ProposedGeneration;
            str << " ProposedStep: " << ProposedStep;
            str << " SignatureSz: " << SignatureSz;
            if (SignatureSz) {
                str << " Signature: {" << Signature[0];
                for (size_t i = 1; i < SignatureSz; ++i) {
                    str << ", " << Signature[i];
                }
                str << "}";
            }
            str << " ProxyOptions: " << ProxyOptions.ToString();
            str << "}";
            return str.Str();
        }
    };

    struct TEvDelete : TEventLocal<TEvDelete, EvDelete> {
        const ui64 TabletID;
        const ui64 Cookie;

        TEvDelete(ui64 tabletId, ui64 cookie = 0)
            : TabletID(tabletId)
            , Cookie(cookie)
        {}

        TString ToString() const {
            TStringStream str;
            str << "{EvDelete TabletID: " << TabletID;
            str << " Cookie: " << Cookie;
            str << "}";
            return str.Str();
        }
    };

    struct TEvCleanup : TEventLocal<TEvCleanup, EvCleanup> {
        const ui64 TabletID;
        const TActorId ProposedLeader;

        TEvCleanup(ui64 tabletId, TActorId proposedLeader)
            : TabletID(tabletId)
            , ProposedLeader(proposedLeader)
        {}
    };

    struct TEvDeleteResult : TEventLocal<TEvDeleteResult, EvDeleteResult> {
        const ui64 TabletID;
        const NKikimrProto::EReplyStatus Status;

        TEvDeleteResult(ui64 tabletId, NKikimrProto::EReplyStatus status)
            : TabletID(tabletId)
            , Status(status)
        {}

        TString ToString() const {
            TStringStream str;
            str << "{EvDeleteResult TabletID: " << TabletID;
            str << " Status: " << (ui32)Status;
            str << "}";
            return str.Str();
        }
    };

    struct TEvLock : public TEventLocal<TEvLock, EvLock> {
        const ui64 TabletID;
        const ui64 Cookie;
        const TActorId ProposedLeader;
        const ui32 ProposedGeneration;
        const ui32 SignatureSz;
        const TArrayHolder<ui64> Signature;
        const TProxyOptions ProxyOptions;

        TEvLock(ui64 tabletId, ui64 cookie, const TActorId &leader, ui32 gen, const ui64 *sig, ui32 sigsz, const TProxyOptions &proxyOptions = TProxyOptions())
            : TabletID(tabletId)
            , Cookie(cookie)
            , ProposedLeader(leader)
            , ProposedGeneration(gen)
            , SignatureSz(sigsz)
            , Signature(new ui64[sigsz])
            , ProxyOptions(proxyOptions)
        {
            Copy(sig, sig + sigsz, Signature.Get());
        }

        TString ToString() const {
            TStringStream str;
            str << "{EvLock TabletID: " << TabletID;
            str << " Cookie: " << Cookie;
            str << " ProposedLeader: " << ProposedLeader.ToString();
            str << " ProposedGeneration: " << ProposedGeneration;
            str << " SignatureSz: " << SignatureSz;
            if (SignatureSz) {
                str << " Signature: {" << Signature[0];
                for (size_t i = 1; i < SignatureSz; ++i) {
                    str << ", " << Signature[i];
                }
                str << "}";
            }
            str << " ProxyOptions: " << ProxyOptions.ToString();
            str << "}";
            return str.Str();
        }
    };

    inline static void MakeFilteredSignatureCopy(const ui64 *sig, ui32 sigsz, ui64 *target) {
        for (ui32 i = 0; i != sigsz; ++i) {
            if (sig[i] != Max<ui64>())
                target[i] = sig[i];
            else
                target[i] = 0;
        }
    }

    struct TEvInfo : public TEventLocal<TEvInfo, EvInfo> {
        const NKikimrProto::EReplyStatus Status;
        const ui64 TabletID;
        const ui64 Cookie;
        const TActorId CurrentLeader;
        const TActorId CurrentLeaderTablet;
        const ui32 CurrentGeneration;
        const ui32 CurrentStep;
        const bool Locked;
        const ui64 LockedFor;
        const ui32 SignatureSz;
        TArrayHolder<ui64> Signature;
        TVector<std::pair<TActorId, TActorId>> Followers;

        TEvInfo(NKikimrProto::EReplyStatus status, ui64 tabletId, ui64 cookie, const TActorId &leader, const TActorId &leaderTablet, ui32 gen, ui32 step, bool locked, ui64 lockedFor, const ui64 *sig, ui32 sigsz, const TMap<TActorId, TActorId> &followers)
            : Status(status)
            , TabletID(tabletId)
            , Cookie(cookie)
            , CurrentLeader(leader)
            , CurrentLeaderTablet(leaderTablet)
            , CurrentGeneration(gen)
            , CurrentStep(step)
            , Locked(locked)
            , LockedFor(lockedFor)
            , SignatureSz(sigsz)
            , Signature(new ui64[sigsz])
            , Followers(followers.begin(), followers.end())
        {
            MakeFilteredSignatureCopy(sig, sigsz, Signature.Get());
        }

        TString ToString() const {
            TStringStream str;
            str << "{EvInfo Status: " << (ui32)Status;
            str << " TabletID: " << TabletID;
            str << " Cookie: " << Cookie;
            str << " CurrentLeader: " << CurrentLeader.ToString();
            str << " CurrentLeaderTablet: " << CurrentLeaderTablet.ToString();
            str << " CurrentGeneration: " << CurrentGeneration;
            str << " CurrentStep: " << CurrentStep;
            str << " Locked: " << (Locked ? "true" : "false");
            str << " LockedFor: " << LockedFor;
            str << " SignatureSz: " << SignatureSz;
            if (SignatureSz) {
                str << " Signature: {" << Signature[0];
                for (size_t i = 1; i < SignatureSz; ++i) {
                    str << ", " << Signature[i];
                }
                str << "}";
            }
            if (!Followers.empty()) {
                str << " Followers: [";
                for (auto it = Followers.begin(); it != Followers.end(); ++it) {
                    if (it != Followers.begin()) {
                        str << ',';
                    }
                    str << '{' << it->first.ToString() << ',' << it->second.ToString() << '}';
                }
                str << "]";
            }
            str << "}";
            return str.Str();
        }
    };

    struct TEvUpdateSignature : public TEventLocal<TEvUpdateSignature, EvUpdateSignature> {
        const ui64 TabletID;
        const ui32 Sz;
        const TArrayHolder<ui64> Signature;

        TEvUpdateSignature(ui64 tabletId, ui64 *sig, ui32 sigsz)
            : TabletID(tabletId)
            , Sz(sigsz)
            , Signature(new ui64[sigsz])
        {
            MakeFilteredSignatureCopy(sig, sigsz, Signature.Get());
        }

        TString ToString() const {
            TStringStream str;
            str << "{EvUpdateSignature TabletID: " << TabletID;
            str << " Sz: " << Sz;
            if (Sz) {
                str << " Signature: {" << Signature[0];
                for (size_t i = 1; i < Sz; ++i) {
                    str << ", " << Signature[i];
                }
                str << "}";
            }
            str << "}";
            return str.Str();
        }
    };

    struct TEvReplicaLeaderDemoted : public TEventPB<TEvReplicaLeaderDemoted, NKikimrStateStorage::TEvReplicaLeaderDemoted, EvReplicaLeaderDemoted> {
        TEvReplicaLeaderDemoted() {}
        TEvReplicaLeaderDemoted(ui64 tabletId, ui64 signature)
        {
            Record.SetTabletID(tabletId);
            Record.SetSignature(signature);
        }
    };

    struct TEvResolveReplicas;
    struct TEvResolveBoard;
    struct TEvResolveSchemeBoard;
    struct TEvResolveReplicasList;
    struct TEvReplicaLookup;
    struct TEvReplicaInfo;
    struct TEvReplicaUpdate;
    struct TEvReplicaLock;
    struct TEvReplicaDelete;
    struct TEvReplicaCleanup;
    struct TEvReplicaBoardPublish;
    struct TEvReplicaBoardLookup;
    struct TEvReplicaBoardCleanup;
    struct TEvReplicaBoardPublishAck;
    struct TEvReplicaBoardInfo;
    struct TEvListSchemeBoard;
    struct TEvListSchemeBoardResult;
    struct TEvListStateStorage;
    struct TEvListStateStorageResult;
    struct TEvUpdateGroupConfig;
    struct TEvReplicaProbeSubscribe;
    struct TEvReplicaProbeUnsubscribe;
    struct TEvReplicaProbeConnected;
    struct TEvReplicaProbeDisconnected;

    struct TEvReplicaShutdown : public TEventPB<TEvStateStorage::TEvReplicaShutdown, NKikimrStateStorage::TEvReplicaShutdown, TEvStateStorage::EvReplicaShutdown> {
    };

    struct TEvReplicaDumpRequest : public TEventPB<TEvReplicaDumpRequest, NKikimrStateStorage::TEvDumpRequest, EvReplicaDumpRequest> {
    };

    struct TEvReplicaDump : public TEventPB<TEvReplicaDump, NKikimrStateStorage::TEvDump, EvReplicaDump> {
    };

    struct TEvRequestReplicasDumps : public TEventLocal<TEvRequestReplicasDumps, EvRequestReplicasDumps> {

    };

    struct TEvResponseReplicasDumps : public TEventLocal<TEvResponseReplicasDumps, EvResponseReplicasDumps> {
        TVector<std::pair<TActorId, TAutoPtr<TEvReplicaDump>>> ReplicasDumps;
    };

    struct TEvReplicaRegFollower : public TEventPB<TEvReplicaRegFollower, NKikimrStateStorage::TEvRegisterFollower, EvReplicaRegFollower> {
        TEvReplicaRegFollower()
        {}

        TEvReplicaRegFollower(ui64 tabletId, TActorId follower, TActorId tablet, bool isCandidate)
        {
            Record.SetTabletID(tabletId);
            ActorIdToProto(follower, Record.MutableFollower());
            ActorIdToProto(tablet, Record.MutableFollowerTablet());
            Record.SetCandidate(isCandidate);
        }
    };

    struct TEvReplicaUnregFollower : public TEventPB<TEvReplicaUnregFollower, NKikimrStateStorage::TEvUnregisterFollower, EvReplicaUnregFollower> {
        TEvReplicaUnregFollower()
        {}

        TEvReplicaUnregFollower(ui64 tabletId, const TActorId &follower)
        {
            Record.SetTabletID(tabletId);
            ActorIdToProto(follower, Record.MutableFollower());
        }
    };

    struct TEvBoardInfo : public TEventLocal<TEvBoardInfo, EvBoardInfo> {
        enum class EStatus {
            Unknown,
            Ok,
            NotAvailable,
        };

        struct TInfoEntry {
            TString Payload;
        };

        const EStatus Status;
        const TString Path;
        TMap<TActorId, TInfoEntry> InfoEntries;

        TEvBoardInfo(EStatus status, const TString &path)
            : Status(status)
            , Path(path)
        {}

        TEvBoardInfo(const TEvBoardInfo &x)
            : Status(x.Status)
            , Path(x.Path)
            , InfoEntries(x.InfoEntries)
        {}
    };
};

struct TStateStorageInfo : public TThrRefBase {
    struct TSelection {
        enum EStatus {
            StatusUnknown,
            StatusOk,
            StatusNoInfo,
            StatusOutdated,
        };

        ui32 Sz;
        TArrayHolder<TActorId> SelectedReplicas;
        TArrayHolder<EStatus> Status;

        TSelection()
            : Sz(0)
        {}

        void MergeReply(EStatus status, EStatus *owner, ui64 targetCookie, bool resetOld);

        const TActorId* begin() const { return SelectedReplicas.Get(); }
        const TActorId* end() const { return SelectedReplicas.Get() + Sz; }
    };

    struct TRing {
        bool UseRingSpecificNodeSelection;
        TVector<TActorId> Replicas;

        TActorId SelectReplica(ui32 hash) const;
        ui32 ContentHash() const;
    };

    ui32 StateStorageGroup;
    ui32 NToSelect;
    TVector<TRing> Rings;

    void SelectReplicas(ui64 tabletId, TSelection *selection) const;
    TList<TActorId> SelectAllReplicas() const;
    ui32 ContentHash() const;

    TStateStorageInfo()
        : StateStorageGroup(Max<ui32>())
        , NToSelect(0)
        , Hash(Max<ui64>())
    {}

private:
    mutable ui64 Hash;
};

enum class EBoardLookupMode {
    First,
    Second,
    Majority,
    FirstNonEmptyDoubleTime,
    SecondNonEmptyDoubleTime,
    MajorityDoubleTime,
};

TIntrusivePtr<TStateStorageInfo> BuildStateStorageInfo(char (&namePrefix)[TActorId::MaxServiceIDLength], const NKikimrConfig::TDomainsConfig::TStateStorage& config);
void BuildStateStorageInfos(const NKikimrConfig::TDomainsConfig::TStateStorage& config,
    TIntrusivePtr<TStateStorageInfo> &stateStorageInfo,
    TIntrusivePtr<TStateStorageInfo> &boardInfo,
    TIntrusivePtr<TStateStorageInfo> &schemeBoardInfo);

IActor* CreateStateStorageWarden(const TIntrusivePtr<TStateStorageInfo> &info, const TIntrusivePtr<TStateStorageInfo> &board, const TIntrusivePtr<TStateStorageInfo> &schemeBoard);

IActor* CreateStateStorageProxy(const TIntrusivePtr<TStateStorageInfo> &info, const TIntrusivePtr<TStateStorageInfo> &board, const TIntrusivePtr<TStateStorageInfo> &schemeBoard);
IActor* CreateStateStorageProxyStub();
IActor* CreateStateStorageReplica(const TIntrusivePtr<TStateStorageInfo> &info, ui32 replicaIndex);
IActor* CreateStateStorageMonitoringActor(ui64 targetTablet, const TActorId &sender, const TString &query);
IActor* CreateStateStorageTabletGuardian(ui64 tabletId, const TActorId &leader, const TActorId &tabletLeader, ui32 generation);
IActor* CreateStateStorageFollowerGuardian(ui64 tabletId, const TActorId &follower); // created as followerCandidate
IActor* CreateStateStorageBoardReplica(const TIntrusivePtr<TStateStorageInfo> &, ui32);
IActor* CreateSchemeBoardReplica(const TIntrusivePtr<TStateStorageInfo>&, ui32);
IActor* CreateBoardLookupActor(const TString &path, const TActorId &owner, ui32 groupId, EBoardLookupMode mode, bool sub, bool useNodeSubscriptions);
IActor* CreateBoardPublishActor(const TString &path, const TString &payload, const TActorId &owner, ui32 groupId, ui32 ttlMs, bool reg);

TString MakeEndpointsBoardPath(const TString &database);

void RegisterStateStorageEventScopes(const std::shared_ptr<TEventFilter>& filter);

}