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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
/*
*
* Copyright 2023 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package orca
import (
"sync"
v3orcapb "github.com/cncf/xds/go/xds/data/orca/v3"
)
// ServerMetrics is the data returned from a server to a client to describe the
// current state of the server and/or the cost of a request when used per-call.
type ServerMetrics struct {
CPUUtilization float64 // CPU utilization: [0, inf); unset=-1
MemUtilization float64 // Memory utilization: [0, 1.0]; unset=-1
AppUtilization float64 // Application utilization: [0, inf); unset=-1
QPS float64 // queries per second: [0, inf); unset=-1
EPS float64 // errors per second: [0, inf); unset=-1
// The following maps must never be nil.
Utilization map[string]float64 // Custom fields: [0, 1.0]
RequestCost map[string]float64 // Custom fields: [0, inf); not sent OOB
NamedMetrics map[string]float64 // Custom fields: [0, inf); not sent OOB
}
// toLoadReportProto dumps sm as an OrcaLoadReport proto.
func (sm *ServerMetrics) toLoadReportProto() *v3orcapb.OrcaLoadReport {
ret := &v3orcapb.OrcaLoadReport{
Utilization: sm.Utilization,
RequestCost: sm.RequestCost,
NamedMetrics: sm.NamedMetrics,
}
if sm.CPUUtilization != -1 {
ret.CpuUtilization = sm.CPUUtilization
}
if sm.MemUtilization != -1 {
ret.MemUtilization = sm.MemUtilization
}
if sm.AppUtilization != -1 {
ret.ApplicationUtilization = sm.AppUtilization
}
if sm.QPS != -1 {
ret.RpsFractional = sm.QPS
}
if sm.EPS != -1 {
ret.Eps = sm.EPS
}
return ret
}
// merge merges o into sm, overwriting any values present in both.
func (sm *ServerMetrics) merge(o *ServerMetrics) {
mergeMap(sm.Utilization, o.Utilization)
mergeMap(sm.RequestCost, o.RequestCost)
mergeMap(sm.NamedMetrics, o.NamedMetrics)
if o.CPUUtilization != -1 {
sm.CPUUtilization = o.CPUUtilization
}
if o.MemUtilization != -1 {
sm.MemUtilization = o.MemUtilization
}
if o.AppUtilization != -1 {
sm.AppUtilization = o.AppUtilization
}
if o.QPS != -1 {
sm.QPS = o.QPS
}
if o.EPS != -1 {
sm.EPS = o.EPS
}
}
func mergeMap(a, b map[string]float64) {
for k, v := range b {
a[k] = v
}
}
// ServerMetricsRecorder allows for recording and providing out of band server
// metrics.
type ServerMetricsRecorder interface {
ServerMetricsProvider
// SetCPUUtilization sets the CPU utilization server metric. Must be
// greater than zero.
SetCPUUtilization(float64)
// DeleteCPUUtilization deletes the CPU utilization server metric to
// prevent it from being sent.
DeleteCPUUtilization()
// SetMemoryUtilization sets the memory utilization server metric. Must be
// in the range [0, 1].
SetMemoryUtilization(float64)
// DeleteMemoryUtilization deletes the memory utiliztion server metric to
// prevent it from being sent.
DeleteMemoryUtilization()
// SetApplicationUtilization sets the application utilization server
// metric. Must be greater than zero.
SetApplicationUtilization(float64)
// DeleteApplicationUtilization deletes the application utilization server
// metric to prevent it from being sent.
DeleteApplicationUtilization()
// SetQPS sets the Queries Per Second server metric. Must be greater than
// zero.
SetQPS(float64)
// DeleteQPS deletes the Queries Per Second server metric to prevent it
// from being sent.
DeleteQPS()
// SetEPS sets the Errors Per Second server metric. Must be greater than
// zero.
SetEPS(float64)
// DeleteEPS deletes the Errors Per Second server metric to prevent it from
// being sent.
DeleteEPS()
// SetNamedUtilization sets the named utilization server metric for the
// name provided. val must be in the range [0, 1].
SetNamedUtilization(name string, val float64)
// DeleteNamedUtilization deletes the named utilization server metric for
// the name provided to prevent it from being sent.
DeleteNamedUtilization(name string)
}
type serverMetricsRecorder struct {
mu sync.Mutex // protects state
state *ServerMetrics // the current metrics
}
// NewServerMetricsRecorder returns an in-memory store for ServerMetrics and
// allows for safe setting and retrieving of ServerMetrics. Also implements
// ServerMetricsProvider for use with NewService.
func NewServerMetricsRecorder() ServerMetricsRecorder {
return newServerMetricsRecorder()
}
func newServerMetricsRecorder() *serverMetricsRecorder {
return &serverMetricsRecorder{
state: &ServerMetrics{
CPUUtilization: -1,
MemUtilization: -1,
AppUtilization: -1,
QPS: -1,
EPS: -1,
Utilization: make(map[string]float64),
RequestCost: make(map[string]float64),
NamedMetrics: make(map[string]float64),
},
}
}
// ServerMetrics returns a copy of the current ServerMetrics.
func (s *serverMetricsRecorder) ServerMetrics() *ServerMetrics {
s.mu.Lock()
defer s.mu.Unlock()
return &ServerMetrics{
CPUUtilization: s.state.CPUUtilization,
MemUtilization: s.state.MemUtilization,
AppUtilization: s.state.AppUtilization,
QPS: s.state.QPS,
EPS: s.state.EPS,
Utilization: copyMap(s.state.Utilization),
RequestCost: copyMap(s.state.RequestCost),
NamedMetrics: copyMap(s.state.NamedMetrics),
}
}
func copyMap(m map[string]float64) map[string]float64 {
ret := make(map[string]float64, len(m))
for k, v := range m {
ret[k] = v
}
return ret
}
// SetCPUUtilization records a measurement for the CPU utilization metric.
func (s *serverMetricsRecorder) SetCPUUtilization(val float64) {
if val < 0 {
if logger.V(2) {
logger.Infof("Ignoring CPU Utilization value out of range: %v", val)
}
return
}
s.mu.Lock()
defer s.mu.Unlock()
s.state.CPUUtilization = val
}
// DeleteCPUUtilization deletes the relevant server metric to prevent it from
// being sent.
func (s *serverMetricsRecorder) DeleteCPUUtilization() {
s.mu.Lock()
defer s.mu.Unlock()
s.state.CPUUtilization = -1
}
// SetMemoryUtilization records a measurement for the memory utilization metric.
func (s *serverMetricsRecorder) SetMemoryUtilization(val float64) {
if val < 0 || val > 1 {
if logger.V(2) {
logger.Infof("Ignoring Memory Utilization value out of range: %v", val)
}
return
}
s.mu.Lock()
defer s.mu.Unlock()
s.state.MemUtilization = val
}
// DeleteMemoryUtilization deletes the relevant server metric to prevent it
// from being sent.
func (s *serverMetricsRecorder) DeleteMemoryUtilization() {
s.mu.Lock()
defer s.mu.Unlock()
s.state.MemUtilization = -1
}
// SetApplicationUtilization records a measurement for a generic utilization
// metric.
func (s *serverMetricsRecorder) SetApplicationUtilization(val float64) {
if val < 0 {
if logger.V(2) {
logger.Infof("Ignoring Application Utilization value out of range: %v", val)
}
return
}
s.mu.Lock()
defer s.mu.Unlock()
s.state.AppUtilization = val
}
// DeleteApplicationUtilization deletes the relevant server metric to prevent
// it from being sent.
func (s *serverMetricsRecorder) DeleteApplicationUtilization() {
s.mu.Lock()
defer s.mu.Unlock()
s.state.AppUtilization = -1
}
// SetQPS records a measurement for the QPS metric.
func (s *serverMetricsRecorder) SetQPS(val float64) {
if val < 0 {
if logger.V(2) {
logger.Infof("Ignoring QPS value out of range: %v", val)
}
return
}
s.mu.Lock()
defer s.mu.Unlock()
s.state.QPS = val
}
// DeleteQPS deletes the relevant server metric to prevent it from being sent.
func (s *serverMetricsRecorder) DeleteQPS() {
s.mu.Lock()
defer s.mu.Unlock()
s.state.QPS = -1
}
// SetEPS records a measurement for the EPS metric.
func (s *serverMetricsRecorder) SetEPS(val float64) {
if val < 0 {
if logger.V(2) {
logger.Infof("Ignoring EPS value out of range: %v", val)
}
return
}
s.mu.Lock()
defer s.mu.Unlock()
s.state.EPS = val
}
// DeleteEPS deletes the relevant server metric to prevent it from being sent.
func (s *serverMetricsRecorder) DeleteEPS() {
s.mu.Lock()
defer s.mu.Unlock()
s.state.EPS = -1
}
// SetNamedUtilization records a measurement for a utilization metric uniquely
// identifiable by name.
func (s *serverMetricsRecorder) SetNamedUtilization(name string, val float64) {
if val < 0 || val > 1 {
if logger.V(2) {
logger.Infof("Ignoring Named Utilization value out of range: %v", val)
}
return
}
s.mu.Lock()
defer s.mu.Unlock()
s.state.Utilization[name] = val
}
// DeleteNamedUtilization deletes any previously recorded measurement for a
// utilization metric uniquely identifiable by name.
func (s *serverMetricsRecorder) DeleteNamedUtilization(name string) {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.state.Utilization, name)
}
// SetRequestCost records a measurement for a utilization metric uniquely
// identifiable by name.
func (s *serverMetricsRecorder) SetRequestCost(name string, val float64) {
s.mu.Lock()
defer s.mu.Unlock()
s.state.RequestCost[name] = val
}
// DeleteRequestCost deletes any previously recorded measurement for a
// utilization metric uniquely identifiable by name.
func (s *serverMetricsRecorder) DeleteRequestCost(name string) {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.state.RequestCost, name)
}
// SetNamedMetric records a measurement for a utilization metric uniquely
// identifiable by name.
func (s *serverMetricsRecorder) SetNamedMetric(name string, val float64) {
s.mu.Lock()
defer s.mu.Unlock()
s.state.NamedMetrics[name] = val
}
// DeleteNamedMetric deletes any previously recorded measurement for a
// utilization metric uniquely identifiable by name.
func (s *serverMetricsRecorder) DeleteNamedMetric(name string) {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.state.NamedMetrics, name)
}
|