aboutsummaryrefslogtreecommitdiffstats
path: root/ydb/core/mind/hive/tablet_info.h
blob: c32ae342eba8a43e35050c1460a2d0cdaf6e3065 (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
#pragma once

#include "hive.h"
#include "metrics.h"

namespace NKikimr {
namespace NHive {

struct TNodeInfo;
struct TLeaderTabletInfo;
struct TFollowerTabletInfo;

struct TCounters {
    TVector<ui64> Simple;
    TVector<i64> SimpleDelta;
    TVector<ui64> Cumulative;
    TVector<ui64> CumulativeDelta;

    void UpdateCounters(const NKikimrTabletCountersAggregator::TTabletCounters& counters) {
        {
            size_t size = counters.SimpleCountersSize();
            Simple.resize(size);
            SimpleDelta.resize(size);
            for (size_t i = 0; i < size; ++i) {
                ui64 newValue = counters.GetSimpleCounters(i);
                i64 deltaValue = newValue - Simple[i];
                Simple[i] = newValue;
                SimpleDelta[i] = deltaValue;
            }
        }
        {
            size_t size = counters.CumulativeCountersSize();
            Cumulative.resize(Max(size, Cumulative.size()), 0);
            CumulativeDelta.resize(Max(size, CumulativeDelta.size()), 0);
            for (size_t i = 0; i < size; ++i) {
                ui64 newValue = counters.GetCumulativeCounters(i);
                ui64 deltaValue = Cumulative[i] <= newValue ? newValue - Cumulative[i] : newValue;
                Cumulative[i] = newValue;
                CumulativeDelta[i] = deltaValue;
            }
        }
    }

    void UpdateCounters(const TCounters& counters) {
        {
            size_t size = counters.Simple.size();
            Simple.resize(size);
            SimpleDelta.resize(size);
            for (size_t i = 0; i < size; ++i) {
                i64 deltaValue = counters.SimpleDelta[i];
                Simple[i] += deltaValue;
                SimpleDelta[i] = deltaValue;
            }
        }
        {
            size_t size = counters.CumulativeDelta.size();
            Cumulative.resize(Max(size, Cumulative.size()), 0);
            CumulativeDelta.resize(Max(size, CumulativeDelta.size()), 0);
            for (size_t i = 0; i < size; ++i) {
                ui64 deltaValue = counters.CumulativeDelta[i];
                Cumulative[i] += deltaValue;
                CumulativeDelta[i] = deltaValue;
            }
        }
    }
};

struct TTabletCountersInfo {
    TCounters ExecutorCounters;
    TCounters AppCounters;

    void UpdateCounters(const NKikimrTabletCountersAggregator::TTabletCountersInfo& countersInfo) {
        if (countersInfo.HasExecutorCounters())
            ExecutorCounters.UpdateCounters(countersInfo.GetExecutorCounters());
        if (countersInfo.HasAppCounters())
            AppCounters.UpdateCounters(countersInfo.GetAppCounters());
    }
};

struct TTabletDebugState {
    ui32 NodesDead = 0;
    ui32 NodesDown = 0;
    ui32 NodesNotAllowed = 0;
    ui32 NodesInDatacentersNotAllowed = 0;
    bool LeaderNotRunning = false;
    ui32 NodesWithLeaderNotLocal = 0;
    ui32 NodesWithoutDomain = 0;
    ui32 NodesFilledWithDatacenterFollowers = 0;
    ui32 NodesWithoutResources = 0;
    ui32 NodesWithSomeoneFromOurFamily = 0;
    ui32 NodesWithoutLocation = 0;
};

struct TTabletInfo {
    friend class TTxMonEvent_TabletInfo;
public:
    using EVolatileState = NKikimrHive::ETabletVolatileState;
    using EBalancerPolicy = NKikimrHive::EBalancerPolicy;

    enum class ETabletRole {
        Leader,
        Follower
    };

protected:
    EVolatileState VolatileState;
    ETabletRole TabletRole;
    TInstant VolatileStateChangeTime;
    TInstant LastBalancerDecisionTime;

public:
    static TString EVolatileStateName(EVolatileState value) {
        switch(value) {
        case EVolatileState::TABLET_VOLATILE_STATE_UNKNOWN: return "Unknown";
        case EVolatileState::TABLET_VOLATILE_STATE_STOPPED: return "Stopped";
        case EVolatileState::TABLET_VOLATILE_STATE_BOOTING: return "Booting";
        case EVolatileState::TABLET_VOLATILE_STATE_STARTING: return "Starting";
        case EVolatileState::TABLET_VOLATILE_STATE_RUNNING: return "Running";
        case EVolatileState::_TABLET_VOLATILE_STATE_BLOCKED: return "Blocked";
        default: return Sprintf("%d", static_cast<int>(value));
        }
    }

    static TString ETabletRoleName(ETabletRole value) {
        switch(value) {
        case ETabletRole::Leader: return "Leader";
        case ETabletRole::Follower: return "Follower";
        default: return Sprintf("%d", static_cast<int>(value));
        }
    }

    TInstant GetVolatileStateChangeTime() const {
        return VolatileStateChangeTime;
    }

    bool IsGoodForBalancer(TInstant now) const;

    void MakeBalancerDecision(TInstant now) {
        LastBalancerDecisionTime = now;
    }

    THive& Hive;
    TNodeId PreferredNodeId;
    TNodeId NodeId;
    TNodeInfo* Node;
    TNodeId LastNodeId;
    TTabletCountersInfo Counters;
    NKikimrHive::TTabletStatistics Statistics;

    TString GetLogPrefix() const;

protected:
    NKikimrTabletBase::TMetrics ResourceValues; // current values of various metrics
    TTabletMetricsAggregates ResourceMetricsAggregates;
    TResourceNormalizedValues ResourceNormalizedValues;

public:
    TVector<TActorId> ActorsToNotify; // ...OnCreation persistent
    TVector<TActorId> ActorsToNotifyOnRestart; // volatile
    double Weight;
    mutable TString BootState;
    TInstant PostponedStart;
    EBalancerPolicy BalancerPolicy;

    TTabletInfo(ETabletRole role, THive& hive);
    TTabletInfo(const TTabletInfo&) = delete;
    TTabletInfo(TTabletInfo&&) = delete;
    TTabletInfo& operator =(const TTabletInfo&) = delete;
    TTabletInfo& operator =(TTabletInfo&&) = delete;

    bool operator ==(const TTabletInfo& tablet) const {
        return this == &tablet;
    }

    EVolatileState GetVolatileState() const {
        return VolatileState;
    }

    bool IsLeader() const {
        return TabletRole == ETabletRole::Leader;
    }

    bool IsFollower() const {
        return TabletRole == ETabletRole::Follower;
    }

    const TLeaderTabletInfo& GetLeader() const;
    TLeaderTabletInfo& GetLeader();
    TLeaderTabletInfo& AsLeader();
    const TLeaderTabletInfo& AsLeader() const;
    TFollowerTabletInfo& AsFollower();
    const TFollowerTabletInfo& AsFollower() const;
    std::pair<TTabletId, TFollowerId> GetFullTabletId() const;
    TFullObjectId GetObjectId() const;
    TTabletTypes::EType GetTabletType() const;
    TString ToString() const;
    TString StateString() const;
    TString FamilyString() const;
    void ChangeVolatileState(EVolatileState state);

    bool IsReadyToBoot() const {
        return NodeId == 0 && VolatileState == EVolatileState::TABLET_VOLATILE_STATE_STOPPED;
    }

    bool IsReadyToStart(TInstant now) const;
    bool IsStarting() const;
    bool IsStartingOnNode(TNodeId nodeId) const;
    bool IsRunning() const;
    bool IsBooting() const;
    bool IsAlive() const;
    bool CanBeAlive() const; // IsAlive() + <Unknown>

    bool IsAliveOnLocal(const TActorId& local) const;
    bool IsStopped() const;
    bool InitiateBoot();
    bool BecomeStarting(TNodeId nodeId);
    bool BecomeRunning(TNodeId nodeId);
    bool BecomeStopped();

    TNodeInfo* GetNode() const;
    TActorId GetLocal() const;
    void SendStopTablet(TSideEffects& sideEffects);
    void SendStopTablet(const TActorId& local, TSideEffects& sideEffects);
    bool InitiateStop(TSideEffects& sideEffects);

    void BecomeUnknown(TNodeInfo* node);
    bool Kick();
    const TVector<i64>& GetTabletAllowedMetricIds() const;

    void UpdateResourceUsage(const NKikimrTabletBase::TMetrics& metrics);
    TResourceRawValues GetResourceCurrentValues() const;
    TResourceRawValues GetResourceMaximumValues() const;
    static i64 GetCounterValue(const NKikimrTabletBase::TMetrics& metrics, const TVector<i64>& allowedMetricIds);
    void FilterRawValues(TResourceRawValues& values) const;
    void FilterRawValues(TResourceNormalizedValues& values) const;
    void ActualizeCounter();

    template <typename ResourcesType>
    static double GetUsage(const ResourcesType& current, const ResourcesType& maximum, EResourceToBalance resource = EResourceToBalance::Dominant) {
        auto normValues = NormalizeRawValues(current, maximum);
        return ExtractResourceUsage(normValues, resource);
    }

    static double ExtractResourceUsage(const TResourceNormalizedValues& normValues, EResourceToBalance resource = EResourceToBalance::Dominant) {
        switch (resource) {
        case EResourceToBalance::CPU: return std::get<NMetrics::EResource::CPU>(normValues);
        case EResourceToBalance::Memory: return std::get<NMetrics::EResource::Memory>(normValues);
        case EResourceToBalance::Network: return std::get<NMetrics::EResource::Network>(normValues);
        case EResourceToBalance::Counter: return std::get<NMetrics::EResource::Counter>(normValues);
        case EResourceToBalance::Dominant: return max(normValues);
        }
    }

    void UpdateWeight() {
        TResourceRawValues current = GetResourceCurrentValues();
        TResourceRawValues maximum = GetResourceMaximumValues();
        FilterRawValues(current);
        FilterRawValues(maximum);

        ResourceNormalizedValues = NormalizeRawValues(current, maximum);
        Weight = ExtractResourceUsage(ResourceNormalizedValues);
    }

    double GetWeight(EResourceToBalance resourceToBalance) const {
        return ExtractResourceUsage(ResourceNormalizedValues, resourceToBalance);
    }

    void PostponeStart(TInstant nextStart) {
        PostponedStart = nextStart;
    }

    const TVector<TNodeId>& GetAllowedNodes() const;
    const TVector<TDataCenterId>& GetAllowedDataCenters() const;
    bool InitiateStart(TNodeInfo* node);

    const NKikimrTabletBase::TMetrics& GetResourceValues() const {
        return ResourceValues;
    }

    void InitTabletMetrics() {
        UpdateResourceUsage({});
    }

    const TTabletMetricsAggregates& GetResourceMetricsAggregates() const {
        return ResourceMetricsAggregates;
    }

    TTabletMetricsAggregates& MutableResourceMetricsAggregates() {
        return ResourceMetricsAggregates;
    }

    // ONLY for use in unit tests
    NKikimrTabletBase::TMetrics& GetMutableResourceValues() {
        return ResourceValues;
    }

    void ActualizeTabletStatistics(TInstant now);
    ui64 GetRestartsPerPeriod(TInstant barrier);
    bool RestartsOften() const;

    bool HasCounter() {
        return std::get<NMetrics::EResource::Counter>(GetResourceCurrentValues()) > 0;
    }
};


} // NHive
} // NKikimr