aboutsummaryrefslogtreecommitdiffstats
path: root/ydb/services/persqueue_cluster_discovery/cluster_discovery_service.cpp
blob: 23bf9636db7ed34d61925f6e939bf9d0c259c678 (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
#include "cluster_discovery_service.h"
#include "cluster_discovery_worker.h"

#include "counters.h"

#include <ydb/core/base/appdata.h>
#include <ydb/core/grpc_services/grpc_request_proxy.h>
#include <ydb/core/mind/address_classification/net_classifier.h>
#include <ydb/core/mon/mon.h>
#include <ydb/core/persqueue/cluster_tracker.h>
#include <ydb/core/util/address_classifier.h>

#include <library/cpp/actors/core/actor_bootstrapped.h>

#include <algorithm>


namespace NKikimr::NPQ::NClusterDiscovery {

using namespace NCounters;

inline auto& Ctx() {
    return TActivationContext::AsActorContext();
}

class TClusterDiscoveryServiceActor: public TActorBootstrapped<TClusterDiscoveryServiceActor> {
public:
    TClusterDiscoveryServiceActor(TIntrusivePtr<::NMonitoring::TDynamicCounters> counters)
        : RawCounters(counters)
        , Counters(BuildCounters())
    {
    }

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

    const auto& Cfg() const {
        return AppData(Ctx())->PQClusterDiscoveryConfig;
    }

    void Bootstrap() {
        Become(&TThis::Initing);

        SubscribeToNetClassifier();
        SubscribeToClusterTracker();

        InitCloudNetData();

        RegisterMonitoringPage();

        StartPeriodicalMonitoring();
    }

private:
    void InitCloudNetData() {
        if (Cfg().HasCloudNetData()) {
            CloudNetworksClassifier = NAddressClassifier::BuildLabeledAddressClassifierFromNetData(Cfg().GetCloudNetData());

            Y_VERIFY(CloudNetworksClassifier); // the config is expected to be correct if specified
        }
    }

    void SubscribeToNetClassifier() {
        Send(NNetClassifier::MakeNetClassifierID(), new NNetClassifier::TEvNetClassifier::TEvSubscribe);
    }

    void SubscribeToClusterTracker() {
        Send(NPQ::NClusterTracker::MakeClusterTrackerID(), new NPQ::NClusterTracker::TEvClusterTracker::TEvSubscribe);
    }

    static TString FormatTs(const TMaybe<TInstant>& time) {
        if (time) {
            return time->ToRfc822String();
        }
        return "NULL";
    }

    bool IsHealthy(bool useLocalEnabled = false) const {
        bool isLocalEnabled = false;
        if (ClustersList) {
            for (const auto& cluster : ClustersList->Clusters) {
                if (cluster.IsLocal)
                    isLocalEnabled = cluster.IsEnabled;
            }

        }
        return ClustersList && DatacenterClassifier && (isLocalEnabled || !useLocalEnabled);
    }

    TString MakeReport() const {
        TStringBuilder message;
        message << "PersQueue Cluster Discovery Service info: \n";
        message << "\tOverall status: " << (IsHealthy() ? "GOOD" : "BAD") << "\n\n";

        message << "\tInitial NetClassifier response ts: " << FormatTs(InitialNetClassifierResponseTs) << "\n";
        message << "\tInitial ClusterTracker response ts: " << FormatTs(InitialClusterTrackerResponseTs)  << "\n";
        message << "\tFully initialized at: " << FormatTs(FullyInitializedTs) << "\n\n";

        message << "\tNetClassifier: " << (DatacenterClassifier ? "OK" : "NULL") << "\n";
        message << "\tNetClassifier NetData update ts: " << FormatTs(NetDataUpdateTs) << "\n\n";

        message << "\tClusters list: " << (ClustersList ? "OK" : "NULL") << "\n";
        message << "\tClusters list update ts: " << FormatTs(ClustersListUpdateTs) << "\n";
        if (ClustersList && ClustersList->Clusters.size()) {
            message << "\tClusters " << "(" << ClustersList->Clusters.size() << "): " << "\n";
            for (const auto& cluster : ClustersList->Clusters) {
                message << "\t\t" << cluster.DebugString() << "\n";
            }
        }

        message << "\n";
        message << "\tNetClassifier updates count: " << NetClassifierUpdatesCount << "\n";
        message << "\tClusters list updates count: " << ClustersListUpdatesCount << "\n";

        return TString(message);
    }

    void HandleHttpRequest(NMon::TEvHttpInfo::TPtr& ev) {
        const TStringBuf path = ev->Get()->Request.GetPath();

        TStringBuilder responseContent;
        if (path.EndsWith("/health")) {
            static const char HTTPNOTAVAIL_H[] = "HTTP/1.1 418 I'm a teapot\r\nConnection: Close\r\n\r\nDiscovery service is disabled on the node\r\n";
            responseContent << (IsHealthy() ? NMonitoring::HTTPOKTEXT : HTTPNOTAVAIL_H) << "Service statuses: 200 - OK, 418 - DISABLED";
        } else if (path.EndsWith("/ping")) {
            static const char HTTPNOTAVAIL_P[] = "HTTP/1.1 418 I'm a teapot\r\nConnection: Close\r\n\r\nDiscovery service is disabled on the node and local cluster is disabled\r\n";
            responseContent << (IsHealthy(true) ? NMonitoring::HTTPOKTEXT : HTTPNOTAVAIL_P) << "Service statuses: 200 - OK, 418 - DISABLED";
        } else{
            responseContent << NMonitoring::HTTPOKTEXT << MakeReport();
        }

        Send(ev->Sender, new NMon::TEvHttpInfoRes(TString(responseContent), 0, NMon::IEvHttpInfoRes::EContentType::Custom));
    }

    void UpdateNetClassifier(NNetClassifier::TEvNetClassifier::TEvClassifierUpdate::TPtr& ev) {
        DatacenterClassifier = ev->Get()->Classifier;
        NetDataUpdateTs = ev->Get()->NetDataUpdateTimestamp;

        Counters = BuildCounters();

        ++NetClassifierUpdatesCount;

        UpdateHealthProbe();
    }

    void HandleClassifierUpdateWhileIniting(NNetClassifier::TEvNetClassifier::TEvClassifierUpdate::TPtr& ev) {
        UpdateNetClassifier(ev);

        InitialNetClassifierResponseTs = Ctx().Now();

        CompleteInitialization();
    }

    void UpdateClustersList(NClusterTracker::TEvClusterTracker::TEvClustersUpdate::TPtr& ev) {
        ClustersListUpdateTs = ev->Get()->ClustersListUpdateTimestamp;

        if (!ClustersList || !(*ClustersList == *ev->Get()->ClustersList)) {
            ClustersList = ev->Get()->ClustersList;
            Counters = BuildCounters();
        }

        ++ClustersListUpdatesCount;

        UpdateHealthProbe();
    }

    void HandleClustersUpdateWhileIniting(NClusterTracker::TEvClusterTracker::TEvClustersUpdate::TPtr& ev) {
        UpdateClustersList(ev);

        InitialClusterTrackerResponseTs = Ctx().Now();

        CompleteInitialization();
    }

    void CompleteInitialization() {
        if (!InitialNetClassifierResponseTs || !InitialClusterTrackerResponseTs) {
            return;
        }

        FullyInitializedTs = Ctx().Now();
        Become(&TThis::Working);
    }

    void RegisterMonitoringPage() const {
        NActors::TMon* mon = AppData(Ctx())->Mon;
        if (mon) {
            NMonitoring::TIndexMonPage * page = mon->RegisterIndexPage("actors", "Actors");
            mon->RegisterActorPage(page, "pqcd", "PersQueue Cluster Discovery", false, Ctx().ExecutorThread.ActorSystem, Ctx().SelfID);
        }
    }

    STATEFN(Initing) {
        switch (ev->GetTypeRewrite()) {
            hFunc(NNetClassifier::TEvNetClassifier::TEvClassifierUpdate, HandleClassifierUpdateWhileIniting);
            hFunc(NClusterTracker::TEvClusterTracker::TEvClustersUpdate, HandleClustersUpdateWhileIniting);
            hFunc(NGRpcService::TEvDiscoverPQClustersRequest, HandleDiscoverPQClustersRequestWhileIniting);
            hFunc(NMon::TEvHttpInfo, HandleHttpRequest);
            hFunc(TEvents::TEvWakeup, UpdateTimedCounters);
        }
    }

    void RespondServiceUnavailable(NGRpcService::TEvDiscoverPQClustersRequest::TPtr& ev) {
        Counters->DroppedRequestsCount->Inc();

        ev->Get()->ReplyWithYdbStatus(Ydb::StatusIds::UNAVAILABLE);
    }

    void HandleDiscoverPQClustersRequestWhileIniting(NGRpcService::TEvDiscoverPQClustersRequest::TPtr& ev) {
        Counters->TotalRequestsCount->Inc();

        RespondServiceUnavailable(ev);
    }

    void HandleClassifierUpdateWhileWorking(NNetClassifier::TEvNetClassifier::TEvClassifierUpdate::TPtr& ev) {
        UpdateNetClassifier(ev);
    }

    void HandleClustersUpdateWhileWorking(NClusterTracker::TEvClusterTracker::TEvClustersUpdate::TPtr& ev) {
        UpdateClustersList(ev);
    }

    void HandleDiscoverPQClustersRequestWhileWorking(NGRpcService::TEvDiscoverPQClustersRequest::TPtr& ev) {
        Counters->TotalRequestsCount->Inc();

        if (!IsHealthy()) {
            RespondServiceUnavailable(ev);
            return;
        }

        IActor* actor = NWorker::CreateClusterDiscoveryWorker(ev, DatacenterClassifier, CloudNetworksClassifier, ClustersList, Counters);
        Register(actor, TMailboxType::HTSwap, AppData(Ctx())->UserPoolId);
    }

    STATEFN(Working) {
        switch (ev->GetTypeRewrite()) {
            hFunc(NNetClassifier::TEvNetClassifier::TEvClassifierUpdate, HandleClassifierUpdateWhileWorking);
            hFunc(NClusterTracker::TEvClusterTracker::TEvClustersUpdate, HandleClustersUpdateWhileWorking);
            hFunc(NGRpcService::TEvDiscoverPQClustersRequest, HandleDiscoverPQClustersRequestWhileWorking);
            hFunc(NMon::TEvHttpInfo, HandleHttpRequest);
            hFunc(TEvents::TEvWakeup, UpdateTimedCounters);
        }
    }

    // Monitoring features

    void StartPeriodicalMonitoring() {
        Y_VERIFY(Cfg().GetTimedCountersUpdateIntervalSeconds());

        Send(Ctx().SelfID, new TEvents::TEvWakeup);
    }

    void UpdateHealthProbe() {
        (*Counters->HealthProbe) = IsHealthy();
    }

    void UpdateTimedCounters(TEvents::TEvWakeup::TPtr&) {
        if (NetDataUpdateTs) {
            (*Counters->NetDataUpdateLagSeconds) = Ctx().Now().Seconds() - NetDataUpdateTs->Seconds();
        }

        if (ClustersListUpdateTs) {
            (*Counters->ClustersListUpdateLagSeconds) = Ctx().Now().Seconds() - ClustersListUpdateTs->Seconds();
        }

        UpdateHealthProbe();

        Schedule(TDuration::Seconds(Cfg().GetTimedCountersUpdateIntervalSeconds()), new TEvents::TEvWakeup);
    }

    TClusterDiscoveryCounters::TPtr BuildCounters() {
        return MakeIntrusive<TClusterDiscoveryCounters>(RawCounters, ClustersList, DatacenterClassifier);
    }

private:
    TActorId NetClassifierActorId;
    TActorId ClusterTrackerActorId;
    NAddressClassifier::TLabeledAddressClassifier::TConstPtr DatacenterClassifier;
    NAddressClassifier::TLabeledAddressClassifier::TConstPtr CloudNetworksClassifier;
    TMaybe<TInstant> NetDataUpdateTs;

    NClusterTracker::TClustersList::TConstPtr ClustersList;
    TMaybe<TInstant> ClustersListUpdateTs;

    TMaybe<TInstant> InitialNetClassifierResponseTs;
    TMaybe<TInstant> InitialClusterTrackerResponseTs;

    TMaybe<TInstant> FullyInitializedTs;

    size_t NetClassifierUpdatesCount = 0;
    size_t ClustersListUpdatesCount = 0;

    TIntrusivePtr<::NMonitoring::TDynamicCounters> RawCounters;
    TClusterDiscoveryCounters::TPtr Counters;
};

NActors::IActor* CreateClusterDiscoveryService(TIntrusivePtr<::NMonitoring::TDynamicCounters> counters) {
    return new TClusterDiscoveryServiceActor(counters);
}

} // namespace NKikimr::NPQ::NClusterDiscovery