aboutsummaryrefslogtreecommitdiffstats
path: root/ydb/core/base/tablet_resolver.h
blob: 24744cef478fdb1e17272e7149bf2a90eb261834 (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
#pragma once
#include "defs.h"
#include "events.h"

#include <ydb/core/protos/base.pb.h>
#include <library/cpp/actors/core/event_local.h>
#include <util/stream/str.h>
#include <util/string/builder.h>

namespace NKikimr {

struct TEvTabletResolver {
    enum EEv {
        EvForward = EventSpaceBegin(TKikimrEvents::ES_TABLETRESOLVER),
        EvTabletProblem,
        EvNodeProblem,

        EvForwardResult = EvForward + 1 * 512,

        EvEnd
    };

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

    struct TEvForward : public TEventLocal<TEvForward, EvForward> {
        ///
        enum class EResolvePrio : ui8 {
            ResPrioDisallow,
            ResPrioAllow,
            ResPrioPrefer,
            ResPrioForce,
        };

        ///
        struct TResolveFlags {
            EResolvePrio LocalNodePrio;
            EResolvePrio LocalDcPrio;
            EResolvePrio FollowerPrio;

            TResolveFlags()
                : LocalNodePrio(EResolvePrio::ResPrioAllow)
                , LocalDcPrio(EResolvePrio::ResPrioPrefer)
                , FollowerPrio(EResolvePrio::ResPrioDisallow)
            {}

            static constexpr ui32 MaxTabletPriority() { return 4; }

            ui32 GetTabletPriority(bool isLocalNode, bool isLocalDc, bool isFollower) const {
                if (isLocalNode && (LocalNodePrio == EResolvePrio::ResPrioDisallow) ||
                    isLocalDc && (LocalDcPrio == EResolvePrio::ResPrioDisallow) ||
                    isFollower && (FollowerPrio == EResolvePrio::ResPrioDisallow) ||
                    !isLocalNode && (LocalNodePrio == EResolvePrio::ResPrioForce) ||
                    !isLocalDc && (LocalDcPrio == EResolvePrio::ResPrioForce) ||
                    !isFollower && (FollowerPrio == EResolvePrio::ResPrioForce))
                {
                    return 0;
                }

                ui32 prio = 1;
                if (isLocalNode && (LocalNodePrio == EResolvePrio::ResPrioPrefer))
                    prio |= 2;
                if (isLocalDc && (LocalDcPrio == EResolvePrio::ResPrioPrefer))
                    prio |= 8;
                if (isFollower && (FollowerPrio == EResolvePrio::ResPrioPrefer))
                    prio |= 4;
                return prio;
            }

            bool AllowFollower() const { return FollowerPrio != EResolvePrio::ResPrioDisallow; }

            void SetAllowFollower(bool flag, bool allowMeansPrefer = true) {
                FollowerPrio = EResolvePrio::ResPrioDisallow;
                if (flag)
                    FollowerPrio = (allowMeansPrefer ? EResolvePrio::ResPrioPrefer : EResolvePrio::ResPrioAllow);
            }

            void SetForceFollower(bool flag) {
                if (flag)
                    FollowerPrio = EResolvePrio::ResPrioForce;
            }

            void SetPreferLocal(bool flag) {
                if (flag && LocalNodePrio < EResolvePrio::ResPrioPrefer)
                    LocalNodePrio = EResolvePrio::ResPrioPrefer;
            }

            void SetForceLocal(bool flag) {
                if (flag)
                    LocalNodePrio = EResolvePrio::ResPrioForce;
            }

            TString ToString() const {
                TStringStream str;
                str << (ui32) LocalNodePrio << ':'
                    << (ui32) LocalDcPrio << ':'
                    << (ui32) FollowerPrio;
                return str.Str();
            }
        };

        ///
        enum class EActor : ui8 {
            Tablet,
            SysTablet,
        };

        const ui64 TabletID;
        THolder<IEventHandle> Ev;
        TResolveFlags ResolveFlags;
        EActor Actor;

        TEvForward(ui64 tabletId, IEventHandle *ev, TResolveFlags flags = TResolveFlags(), EActor actor = EActor::Tablet)
            : TabletID(tabletId)
            , Ev(ev)
            , ResolveFlags(flags)
            , Actor(actor)
        {}

        TString ToString() const {
            TStringStream str;
            str << "{EvForward TabletID: " << TabletID;
            str << " Ev: " << (Ev ? Ev->GetBase()->ToString().data() : "nullptr");
            str << " Flags: " << ResolveFlags.ToString();
            str << "}";
            return str.Str();
        }

        TActorId SelectActor(const TActorId& tablet, const TActorId& sysTablet) const {
            switch (Actor) {
                case EActor::Tablet:
                    return tablet;
                case EActor::SysTablet:
                    return sysTablet;
            }
        }
    };

    struct TEvTabletProblem : public TEventLocal<TEvTabletProblem, EvTabletProblem> {
        const ui64 TabletID;
        const TActorId TabletActor;

        TEvTabletProblem(ui64 tabletId, const TActorId &tabletActor)
            : TabletID(tabletId)
            , TabletActor(tabletActor)
        {}

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

    struct TEvNodeProblem : public TEventLocal<TEvNodeProblem, EvNodeProblem> {
        const ui32 NodeId;
        const ui64 CacheEpoch;

        TEvNodeProblem(ui32 nodeId, ui64 cacheEpoch)
            : NodeId(nodeId)
            , CacheEpoch(cacheEpoch)
        {}

        TString ToString() const {
            return TStringBuilder()
                << "{EvNodeProblem NodeId: " << NodeId
                << " CacheEpoch: " << CacheEpoch
                << "}";
        }
    };

    struct TEvForwardResult : public TEventLocal<TEvForwardResult, EvForwardResult> {
        const NKikimrProto::EReplyStatus Status;

        ui64 TabletID;
        TActorId TabletActor;
        TActorId Tablet;
        ui64 CacheEpoch;

        TEvForwardResult(NKikimrProto::EReplyStatus status, ui64 tabletId)
            : Status(status)
            , TabletID(tabletId)
            , CacheEpoch(0)
        {}

        TEvForwardResult(ui64 tabletId, const TActorId &tabletActor, const TActorId &tablet, ui64 cacheEpoch)
            : Status(NKikimrProto::OK)
            , TabletID(tabletId)
            , TabletActor(tabletActor)
            , Tablet(tablet)
            , CacheEpoch(cacheEpoch)
        {}

        TString ToString() const {
            TStringStream str;
            str << "{EvForwardResult Status: " << (ui32)Status;
            str << " TabletID: " << TabletID;
            str << " TabletActor: " << TabletActor.ToString();
            str << " Tablet: " << Tablet.ToString();
            str << " CacheEpoch: " << CacheEpoch;
            str << "}";
            return str.Str();
        }
    };
};

struct TTabletResolverConfig : public TThrRefBase {
    ui64 TabletCacheLimit;

    TTabletResolverConfig()
        : TabletCacheLimit(500000)
    {}
};

IActor* CreateTabletResolver(const TIntrusivePtr<TTabletResolverConfig> &config);
TActorId MakeTabletResolverID();

}