Newer
Older
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
local fun = require('fun')
local json = require('json')
-- I believe that we won't maintain clusters over 1k replicasets
-- in the nearest future
local SELECT_LIMIT = 1000
local VERSION_PLACEHOLDER = '??.??'
local DEFAULT_FUTURE_TIMEOUT = 60
local INSTANCE_GRADE = {
ONLINE = 'Online',
OffLINE = 'Offline',
}
local function table_len(table)
local i = 0
for _ in pairs(table) do
i = i + 1
end
return i
end
local function get_instances()
local tuples = box.space._pico_instance:select({}, { limit = SELECT_LIMIT })
return fun.iter(tuples)
:map(function(tuple) return tuple:tomap({ names_only = true }) end)
:totable()
end
local function get_replicasets()
local tuples = box.space._pico_replicaset:select({}, { limit = SELECT_LIMIT })
return fun.iter(tuples)
:map(function(tuple) return tuple:tomap({ names_only = true }) end)
:totable()
end
-- returns leader's box.slab.info
local function get_memory_info()
local replicasets = vshard.router.routeall()
-- we only need to watch for leader's capacity
local leaders = fun.iter(replicasets)
:reduce(function(acc, uuid, replicaset)
acc[uuid] = replicaset.master
return acc
end,{})
local slab_info_futures = fun.iter(leaders)
:reduce(function(acc, uuid, instance)
local future = instance.conn:call('box.slab.info', {}, { is_async = true })
acc[uuid] = future
return acc
end, {})
local results = fun.iter(slab_info_futures)
:reduce(function(acc, uuid, future)
acc[uuid] = future:wait_result(DEFAULT_FUTURE_TIMEOUT)[1]
return acc
end, {})
return results
end
local function is_leader(instance, replicasets)
local replicaset = fun.iter(replicasets)
:filter(function(replicaset) return replicaset.replicaset_uuid == instance.replicaset_uuid end)
:totable()[1]
return instance.instance_id == replicaset.master_id
end
local function cast_instance(instance, is_leader)
return {
name = instance.instance_id,
targetGrade = instance.target_grade[1],
currentGrade = instance.current_grade[1],
failureDomain = instance.failure_domain,
version = VERSION_PLACEHOLDER,
isLeader = is_leader,
}
end
-- the expected response:
-- --------------------------------------------------
-- export interface ReplicasetType {
-- id: string;
-- instanceCount: number;
-- instances: InstanceType[];
-- version: string;
-- grade: string;
-- capacity: string;
-- }
-- export interface InstanceType {
-- name: string;
-- targetGrade: string;
-- currentGrade: string;
-- failureDomain: string;
-- version: string;
-- isLeader: boolean;
-- }
-- --------------------------------------------------
local function list_replicasets()
local replicasets = get_replicasets()
local instances = get_instances()
local memory_info = get_memory_info()
local replicaset_table = fun.iter(instances)
:reduce(function(acc, instance)
local is_leader = is_leader(instance, replicasets)
local instance_memory_info = memory_info[instance.replicaset_uuid]
if acc[instance.replicaset_uuid] == nil then
acc[instance.replicaset_uuid] = {
id = instance.replicaset_id,
uuid = instance.replicaset_uuid,
instanceCount = 1,
instances = { cast_instance(instance, is_leader) },
version = VERSION_PLACEHOLDER,
grade = is_leader and instance.current_grade[1],
-- frontend expects number
is_leader
and instance_memory_info.quota_used / instance_memory_info.quota_size * 100,
memory = is_leader and {
used = instance_memory_info.quota_used,
usable = instance_memory_info.quota_size,
},
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
}
else
local replicaset = acc[instance.replicaset_uuid]
table.insert(replicaset.instances, cast_instance(instance))
replicaset.instanceCount = replicaset.instanceCount + 1
if is_leader then
replicaset.grade = instance.current_grade[1]
replicaset.capacity = instance_memory_info.quota_used / instance_memory_info.quota_size * 100
end
end
return acc
end, {})
local replicasets = fun.iter(replicaset_table)
:map(function(_, replicaset) return replicaset end)
:totable()
return {
status = 200,
body = json.encode(replicasets),
headers = {
['content-type'] = 'application/json' }
}
end
-- the expected response:
-- --------------------------------------------------
-- export interface ClusterInfoType {
-- capacityUsage: string;
-- memory: {
-- used: string;
-- usable: string;
-- };
-- replicasetsCount: number;
-- instancesCurrentGradeOnline: number;
-- instancesCurrentGradeOffline: number;
-- currentInstaceVersion: string;
-- }
-- --------------------------------------------------
local function cluster_info()
local replicasets = get_replicasets()
local replicasetsCount = table_len(replicasets)
local instances = get_instances()
local instancesOnlineIter, instancesOfflineIter = fun.iter(instances)
:partition(function(instance) return instance.current_grade[1] == INSTANCE_GRADE.ONLINE end)
local memory_info = get_memory_info()
-- '#' operator does not work for map-like tables
local memory_info_count = fun.iter(memory_info):length()
local avg_capacity = fun.iter(memory_info)
:map(function(_, slab_info)
-- i believe that calculating quota_used_ratio one more time is
-- easier than parsing it from '54.23%' string
return slab_info.quota_used / slab_info.quota_size
end)
:sum() / memory_info_count * 100
local total_used = fun.iter(memory_info)
:map(function(_, slab_info) return slab_info.quota_used end)
:sum()
local total_size = fun.iter(memory_info)
:map(function(_, slab_info) return slab_info.quota_size end)
:sum()
local resp = {
capacityUsage = avg_capacity,
memory = {
used = total_used,
usable = total_size,
},
replicasetsCount = replicasetsCount,
instancesCurrentGradeOnline = instancesOnlineIter:length(),
instancesCurrentGradeOffline = instancesOfflineIter:length(),
currentInstaceVersion = pico.PICODATA_VERSION,
}
return {
status = 200,
body = json.encode(resp),
headers = {
['content-type'] = 'application/json' }
}
end
local host, port = ...;
local httpd = require('http.server').new(host, port);
httpd:route({ method = 'GET', path = 'api/v1/replicaset' }, list_replicasets)
httpd:route({ method = 'GET', path = 'api/v1/cluster' }, cluster_info)
httpd:start();
_G.pico.httpd = httpd