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
|
#include <library/cpp/actors/core/actorsystem.h>
#include <library/cpp/actors/core/actor_bootstrapped.h>
#include <library/cpp/actors/core/log.h>
#include <ydb/core/node_whiteboard/node_whiteboard.h>
#include <ydb/core/protos/services.pb.h>
#include <library/cpp/json/json_reader.h>
#include <library/cpp/http/io/stream.h>
#include <library/cpp/openssl/init/init.h>
#include <library/cpp/openssl/io/stream.h>
#include <util/network/sock.h>
#include <util/system/hostname.h>
using namespace NActors;
namespace NKikimr {
class TNodeIdentifier : public TActorBootstrapped<TNodeIdentifier> {
using TThis = TNodeIdentifier;
using TBase = TActorBootstrapped<TNodeIdentifier>;
struct TEvPrivate {
enum EEv {
EvUpdateNodeInfo = EventSpaceBegin(TEvents::ES_PRIVATE),
EvEnd
};
static_assert(EvEnd < EventSpaceEnd(TEvents::ES_PRIVATE), "expected EvEnd < EventSpaceEnd");
struct TEvUpdateNodeInfo : TEventLocal<TEvUpdateNodeInfo, EvUpdateNodeInfo> {};
};
static const NJson::TJsonReaderConfig& GetJsonConfig() {
static NJson::TJsonReaderConfig jsonConfig;
return jsonConfig;
}
const TString& GetWallEServiceHost() const {
static const TString host("api.wall-e.yandex-team.ru");
return host;
}
const TNetworkAddress& GetWallENetworkAddress() const {
static const TNetworkAddress address(GetWallEServiceHost(), 443);
return address;
}
const TString& GetConductorServiceHost() const {
static const TString host("c.yandex-team.ru");
return host;
}
const TNetworkAddress& GetConductorNetworkAddress() const {
static const TNetworkAddress address(GetConductorServiceHost(), 443);
return address;
}
TDuration GetTimeout() const {
return TDuration::Seconds(10);
}
void Handle(TEvPrivate::TEvUpdateNodeInfo::TPtr&, const TActorContext& ctx) {
NKikimrWhiteboard::TSystemStateInfo systemStateInfo;
TString hostname = FQDNHostName();
systemStateInfo.SetHost(hostname);
try {
TSocket s(GetWallENetworkAddress(), GetTimeout());
s.SetSocketTimeout(GetTimeout().Seconds());
TSocketOutput so(s);
TSocketInput si(s);
TOpenSslClientIO ssl(&si, &so);
THttpOutput output(&ssl);
output << "GET /v1/hosts/" << hostname << "?fields=location HTTP/1.1\r\n"
<< "Host: " << GetWallEServiceHost() << "\r\n"
<< "\r\n";
output.Finish();
THttpInput input(&ssl);
unsigned status = ParseHttpRetCode(input.FirstLine());
if (status == 200) {
NJson::TJsonValue response;
bool success = NJson::ReadJsonTree(input.ReadAll(), &GetJsonConfig(), &response);
if (success) {
NJson::TJsonValue* jsonLocation;
if (response.GetValuePointer("location", &jsonLocation)) {
NJson::TJsonValue* jsonValue;
if (jsonLocation->GetValuePointer("short_datacenter_name", &jsonValue)) {
if (jsonValue->GetType() == NJson::EJsonValueType::JSON_STRING) {
systemStateInfo.SetDataCenter(jsonValue->GetStringRobust());
}
}
if (jsonLocation->GetValuePointer("queue", &jsonValue)) {
systemStateInfo.SetDataCenterDescription(jsonValue->GetStringRobust());
}
if (jsonLocation->GetValuePointer("rack", &jsonValue)) {
systemStateInfo.SetRack(jsonValue->GetStringRobust());
}
}
}
}
}
catch (const std::exception&) {
}
try {
TSocket s(GetConductorNetworkAddress(), GetTimeout());
s.SetSocketTimeout(GetTimeout().Seconds());
TSocketOutput so(s);
TSocketInput si(s);
TOpenSslClientIO ssl(&si, &so);
THttpOutput output(&ssl);
output << "GET /api/hosts/" << hostname << "?format=json HTTP/1.1\r\n"
<< "Host: " << GetConductorServiceHost() << "\r\n"
<< "\r\n";
output.Finish();
THttpInput input(&ssl);
unsigned status = ParseHttpRetCode(input.FirstLine());
if (status == 200) {
NJson::TJsonValue response;
bool success = NJson::ReadJsonTree(input.ReadAll(), &GetJsonConfig(), &response);
if (success && response.IsArray()) {
const NJson::TJsonValue& first = response.GetArray().front();
const NJson::TJsonValue* jsonGroup;
if (first.GetValuePointer("group", &jsonGroup)) {
systemStateInfo.SetClusterName(jsonGroup->GetStringRobust());
}
const NJson::TJsonValue* jsonRootDatacenter;
if (!systemStateInfo.HasDataCenter() && first.GetValuePointer("root_datacenter", &jsonRootDatacenter)) {
if (jsonRootDatacenter->GetType() == NJson::EJsonValueType::JSON_STRING) {
systemStateInfo.SetDataCenter(jsonRootDatacenter->GetStringRobust());
}
}
}
}
}
catch (const std::exception&) {
}
TActorId whiteboardServiceId = NNodeWhiteboard::MakeNodeWhiteboardServiceId(ctx.SelfID.NodeId());
ctx.Send(whiteboardServiceId, new NNodeWhiteboard::TEvWhiteboard::TEvSystemStateUpdate(systemStateInfo));
}
public:
static constexpr NKikimrServices::TActivity::EType ActorActivityType() { return NKikimrServices::TActivity::NODE_IDENTIFIER; }
void StateWork(TAutoPtr<IEventHandle>& ev, const TActorContext& ctx) {
switch (ev->GetTypeRewrite()) {
HFunc(TEvPrivate::TEvUpdateNodeInfo, Handle);
CFunc(TEvents::TSystem::PoisonPill, Die);
}
}
void Bootstrap(const TActorContext& ctx) {
ctx.Send(ctx.SelfID, new TEvPrivate::TEvUpdateNodeInfo());
Become(&TThis::StateWork);
}
};
IActor* CreateNodeIdentifier() {
InitOpenSSL();
return new TNodeIdentifier();
}
}
|