/
struct_clientdata.go
485 lines (426 loc) · 15.2 KB
/
struct_clientdata.go
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
package main
import (
"math"
"time"
"github.com/chronos-tachyon/roxy/internal/picker"
"github.com/chronos-tachyon/roxy/lib/costhistory"
"github.com/chronos-tachyon/roxy/lib/roxyutil"
"github.com/chronos-tachyon/roxy/proto/roxy_v0"
)
type ClientData struct {
ShardData *ShardData
UniqueID string
Location Location
DeclaredCPS float64
MeasuredCPS float64
AssignedCPS float64
IsAlive bool
IsServing bool
FlushCh chan struct{}
GoAwayCh chan *roxy_v0.GoAway
ExpireTime time.Time
CostHistory costhistory.CostHistory
Assignments map[string]float64
EventQueue []*roxy_v0.Event
}
func (clientData *ClientData) NeededCPSLocked() float64 {
return math.Max(1.0, math.Max(clientData.DeclaredCPS, clientData.MeasuredCPS))
}
func (clientData *ClientData) SendGoAwayLocked(goAway *roxy_v0.GoAway) {
if clientData.IsAlive {
select {
case clientData.GoAwayCh <- goAway:
default:
}
}
}
func (clientData *ClientData) UpdateLocked(isAlive bool, isServing bool) {
shardData := clientData.ShardData
histData := clientData.CostHistory.Data()
now := histData.Now
isAliveChanged := (clientData.IsAlive != isAlive)
clientData.IsAlive = isAlive
if isAliveChanged && !isAlive {
close(clientData.GoAwayCh)
close(clientData.FlushCh)
clientData.ExpireTime = now.Add(ExpireInterval)
}
if isAliveChanged && isAlive {
clientData.ExpireTime = time.Time{}
clientData.FlushCh = make(chan struct{}, 1)
clientData.GoAwayCh = make(chan *roxy_v0.GoAway, 1)
}
if clientData.IsServing {
shardData.DeclaredDemandCPS -= clientData.DeclaredCPS
shardData.MeasuredDemandCPS -= clientData.MeasuredCPS
}
clientData.MeasuredCPS = histData.PerSecond
clientData.IsServing = isServing
if clientData.IsServing {
shardData.DeclaredDemandCPS += clientData.DeclaredCPS
shardData.MeasuredDemandCPS += clientData.MeasuredCPS
}
}
func (clientData *ClientData) DeleteLocked() {
shardData := clientData.ShardData
if clientData.IsServing {
shardData.DeclaredDemandCPS -= clientData.DeclaredCPS
shardData.MeasuredDemandCPS -= clientData.MeasuredCPS
clientData.IsServing = false
}
for serverID, assignedCPS := range clientData.Assignments {
serverData := shardData.Servers[serverID]
serverData.AssignedCPS -= assignedCPS
clientData.AssignedCPS -= assignedCPS
shardData.AssignedCPS -= assignedCPS
delete(serverData.Assignments, clientData.UniqueID)
delete(clientData.Assignments, serverID)
}
}
func (clientData *ClientData) InsertServerLocked(serverData *ServerData, newCPS float64) {
oldCPS, found := clientData.Assignments[serverData.UniqueID]
roxyutil.Assertf(!found, "existing association between client %q and server %q (%f CPS)", clientData.UniqueID, serverData.UniqueID, oldCPS)
shardData := clientData.ShardData
shardData.AssignedCPS += newCPS
serverData.AssignedCPS += newCPS
serverData.Assignments[clientData.UniqueID] = newCPS
clientData.AssignedCPS += newCPS
clientData.Assignments[serverData.UniqueID] = newCPS
if clientData.IsAlive {
clientData.EventQueue = append(clientData.EventQueue, &roxy_v0.Event{
EventType: roxy_v0.Event_INSERT_IP,
UniqueId: serverData.UniqueID,
Location: string(serverData.Location),
ServerName: serverData.ServerName,
Ip: []byte(serverData.Addr.IP),
Zone: serverData.Addr.Zone,
Port: uint32(serverData.Addr.Port),
AssignedCostPerSecond: newCPS,
})
sendFlush(clientData.FlushCh)
}
}
func (clientData *ClientData) ModifyServerLocked(serverData *ServerData, newCPS float64) {
oldCPS, found := clientData.Assignments[serverData.UniqueID]
roxyutil.Assertf(found, "no existing association between client %q and server %q", clientData.UniqueID, serverData.UniqueID)
deltaCPS := (newCPS - oldCPS)
shardData := clientData.ShardData
shardData.AssignedCPS += deltaCPS
serverData.AssignedCPS += deltaCPS
serverData.Assignments[clientData.UniqueID] = newCPS
clientData.AssignedCPS += deltaCPS
clientData.Assignments[serverData.UniqueID] = newCPS
if clientData.IsAlive {
clientData.EventQueue = append(clientData.EventQueue, &roxy_v0.Event{
EventType: roxy_v0.Event_UPDATE_WEIGHT,
UniqueId: serverData.UniqueID,
AssignedCostPerSecond: newCPS,
})
sendFlush(clientData.FlushCh)
}
}
func (clientData *ClientData) DeleteServerLocked(serverData *ServerData) {
oldCPS, found := clientData.Assignments[serverData.UniqueID]
roxyutil.Assertf(found, "no existing association between client %q and server %q", clientData.UniqueID, serverData.UniqueID)
shardData := clientData.ShardData
shardData.AssignedCPS -= oldCPS
serverData.AssignedCPS -= oldCPS
delete(serverData.Assignments, clientData.UniqueID)
clientData.AssignedCPS -= oldCPS
delete(clientData.Assignments, serverData.UniqueID)
if clientData.IsAlive {
clientData.EventQueue = append(clientData.EventQueue, &roxy_v0.Event{
EventType: roxy_v0.Event_DELETE_IP,
UniqueId: serverData.UniqueID,
})
sendFlush(clientData.FlushCh)
}
}
func (clientData *ClientData) OnConnectLocked() {
if !clientData.IsOKLocked() {
clientData.RebalanceLocked(true)
return
}
shardData := clientData.ShardData
for serverID, assignedCPS := range clientData.Assignments {
serverData := shardData.Servers[serverID]
clientData.EventQueue = append(clientData.EventQueue, &roxy_v0.Event{
EventType: roxy_v0.Event_INSERT_IP,
UniqueId: serverID,
Location: string(serverData.Location),
ServerName: serverData.ServerName,
Ip: []byte(serverData.Addr.IP),
Zone: serverData.Addr.Zone,
Port: uint32(serverData.Addr.Port),
AssignedCostPerSecond: assignedCPS,
})
}
}
func (clientData *ClientData) RebalanceLocked(isOnConnect bool) {
shardData := clientData.ShardData
minServers, maxServers := shardData.NumServersLocked()
neededCPS := clientData.NeededCPSLocked() - clientData.AssignedCPS
availableCPS := float64(0.0)
p1, serverIndexByID1 := clientData.makePickerForServerInsertionLocked()
for serverID := range clientData.Assignments {
serverData := shardData.Servers[serverID]
availableCPS += serverData.AvailableCPSLocked()
if index, found := serverIndexByID1[serverID]; found {
p1.Disable(index)
}
}
newServers := make([]*ServerData, 0, maxServers)
for {
numServers := uint(len(newServers))
if numServers >= minServers && (numServers >= maxServers || availableCPS >= neededCPS) {
break
}
index := p1.Pick(nil)
p1.Disable(index)
serverData := p1.Get(index).(*ServerData)
availableCPS += serverData.AvailableCPSLocked()
newServers = append(newServers, serverData)
}
p2, _ := clientData.makePickerForServerAssignmentLocked(newServers)
amounts := p2.Divvy(neededCPS)
amountsLen := uint(len(amounts))
newAssignments := make(map[string]float64, amountsLen)
for index := uint(0); index < amountsLen; index++ {
suggestedCPS := amounts[index]
serverData := p2.Get(index).(*ServerData)
oldCPS := clientData.Assignments[serverData.UniqueID]
availableCPS := serverData.AvailableCPSLocked()
newCPS := math.Min(availableCPS, oldCPS+suggestedCPS)
deltaCPS := (newCPS - oldCPS)
newAssignments[serverData.UniqueID] = newCPS
neededCPS -= deltaCPS
}
numRemaining := amountsLen
for numRemaining != 0 && neededCPS > 0.0 {
index := p2.Pick(nil)
p2.Disable(index)
serverData := p2.Get(index).(*ServerData)
oldCPS := clientData.Assignments[serverData.UniqueID]
newCPS := newAssignments[serverData.UniqueID]
deltaCPS := (newCPS - oldCPS)
availableCPS := serverData.AvailableCPSLocked() - deltaCPS
additionalCPS := math.Min(neededCPS, availableCPS)
newAssignments[serverData.UniqueID] += additionalCPS
neededCPS -= additionalCPS
numRemaining--
}
for serverID, newCPS := range newAssignments {
oldCPS, found := clientData.Assignments[serverID]
serverData := shardData.Servers[serverID]
if isOnConnect && found {
shardData.AssignedCPS -= oldCPS
serverData.AssignedCPS -= oldCPS
delete(serverData.Assignments, clientData.UniqueID)
clientData.AssignedCPS -= oldCPS
delete(clientData.Assignments, serverID)
found = false
}
if found {
clientData.ModifyServerLocked(serverData, newCPS)
} else {
clientData.InsertServerLocked(serverData, newCPS)
}
}
}
func (clientData *ClientData) ShedExcessServersLocked() {
neededCPS := clientData.NeededCPSLocked()
assignedCPS := clientData.AssignedCPS
excessCPS := math.Max(0.0, assignedCPS-neededCPS)
excessRatio := excessCPS / neededCPS
if excessRatio < 0.125 {
return
}
numServers := uint(len(clientData.Assignments))
shardData := clientData.ShardData
minServers, _ := shardData.NumServersLocked()
if numServers <= minServers {
return
}
p, _ := clientData.makePickerForServerAssignmentLocked(nil)
discardList := make([]*ServerData, 0, numServers-minServers)
for numServers > minServers {
worst, ok := p.Worst()
if !ok {
break
}
p.Disable(worst)
amounts := p.Divvy(neededCPS)
amountsLen := uint(len(amounts))
ok = true
for index := uint(0); index < amountsLen; index++ {
serverData := p.Get(index).(*ServerData)
suggestedCPS := amounts[index]
availableCPS := serverData.AvailableCPSLocked()
if suggestedCPS > availableCPS {
ok = false
break
}
}
if !ok {
p.Enable(worst)
break
}
worstData := p.Get(worst).(*ServerData)
discardList = append(discardList, worstData)
numServers--
}
if len(discardList) == 0 {
return
}
discardSet := make(map[string]struct{}, len(discardList))
for _, serverData := range discardList {
discardSet[serverData.UniqueID] = struct{}{}
clientData.DeleteServerLocked(serverData)
}
amounts := p.Divvy(neededCPS)
amountsLen := uint(len(amounts))
for index := uint(0); index < amountsLen; index++ {
serverData := p.Get(index).(*ServerData)
if _, found := discardSet[serverData.UniqueID]; found {
continue
}
newCPS := amounts[index]
clientData.ModifyServerLocked(serverData, newCPS)
}
}
func (clientData *ClientData) IsExpiredLocked() bool {
t := clientData.ExpireTime
if t.IsZero() {
return false
}
now := clientData.CostHistory.Data().Now
return now.Sub(t) >= 0
}
func (clientData *ClientData) PeriodicLocked() {
shardData := clientData.ShardData
clientData.CostHistory.Update()
histData := clientData.CostHistory.Data()
oldCPS := clientData.MeasuredCPS
newCPS := histData.PerSecond
clientData.MeasuredCPS = newCPS
if clientData.IsServing {
shardData.MeasuredDemandCPS += (newCPS - oldCPS)
}
}
func (clientData *ClientData) ToProtoLocked() *roxy_v0.ClientData {
key := clientData.ShardData.Key
return &roxy_v0.ClientData{
ServiceName: string(key.ServiceName),
ShardNumber: uint32(key.ShardNumber),
HasShardNumber: key.HasShardNumber,
UniqueId: clientData.UniqueID,
Location: string(clientData.Location),
DeclaredCostPerSecond: clientData.DeclaredCPS,
MeasuredCostPerSecond: clientData.MeasuredCPS,
IsAlive: clientData.IsAlive,
IsServing: clientData.IsServing,
History: costhistory.SamplesToProto(clientData.CostHistory.Snapshot()),
}
}
func (clientData *ClientData) IsOKLocked() bool {
shardData := clientData.ShardData
minServers, _ := shardData.NumServersLocked()
numServers := uint(len(clientData.Assignments))
assignedCPS := clientData.AssignedCPS
neededCPS := clientData.NeededCPSLocked()
return (numServers >= minServers && assignedCPS >= neededCPS)
}
func (clientData *ClientData) makePickerForServerInsertionLocked() (picker.Picker, map[string]uint) {
shardData := clientData.ShardData
serverMap := make(map[string]uint, len(shardData.Servers))
serverList := make([]interface{}, 0, len(shardData.Servers))
for _, serverData := range shardData.Servers {
if serverData.IsServing {
serverMap[serverData.UniqueID] = uint(len(serverList))
serverList = append(serverList, serverData)
}
}
p := picker.Make(serverList, clientData.makeServerScoreFuncLocked())
return p, serverMap
}
func (clientData *ClientData) makePickerForServerAssignmentLocked(newServers []*ServerData) (picker.Picker, map[string]uint) {
shardData := clientData.ShardData
serverMap := make(map[string]uint, len(clientData.Assignments))
serverList := make([]interface{}, 0, len(clientData.Assignments)+len(newServers))
for serverID := range clientData.Assignments {
serverData := shardData.Servers[serverID]
serverMap[serverID] = uint(len(serverList))
serverList = append(serverList, serverData)
}
for _, serverData := range newServers {
serverMap[serverData.UniqueID] = uint(len(serverList))
serverList = append(serverList, serverData)
}
p := picker.Make(serverList, clientData.makeServerScoreFuncLocked())
return p, serverMap
}
func (clientData *ClientData) makeServerScoreFuncLocked() func(interface{}) float64 {
costMap := clientData.ShardData.CostMap
return func(v interface{}) float64 {
serverData := v.(*ServerData)
utilization := serverData.UtilizationRatioLocked()
if utilization > 1.0 {
utilization = 1.0
}
latency, _ := costMap.Cost(clientData.Location, serverData.Location)
// utilization ~~ 0.000 .. +0.875 .. +1.000
// (8.0 * utilization) ~~ 0.000 .. +7.000 .. +8.000
// (8.0 - 8.0 * utilization) ~~ +8.000 .. +1.000 .. 0.000
// ln(8.0 - 8.0 * utilization) ~~ +2.079 .. 0.000 .. -Inf
// -ln(8.0 - 8.0 * utilization) ~~ -2.079 .. 0.000 .. +Inf
//
// (latency / 32.0) means +1.000 score for each +32.0ms of latency
//
// ~~ RATIONALE ~~
//
// We want utilization >= 1.0 to have a score of +Inf. There
// are two natural functions that I automatically reach for
// when I want a single vertical asymptote: 1/x and log.
//
// 1/x comes with a horizontal asymptote as well, which we
// don't need here; it probably wouldn't _break_ anything,
// since we have a lower bound on utilization, but it's not
// natural. That makes log the natural choice.
//
// However, log's vertical asymptote is not oriented the way we
// want: it approaches -Inf as its argument approaches zero
// from the positive side, whereas we want +Inf when the
// argument approaches one from the negative side. So we apply
// two negations: we negate the argument, which mirrors log(x)
// about the Y axis so that we approach -Inf as x approaches 0
// from the negative side; and we negate the result, which
// mirrors log(-x) about the X axis so that we approach +Inf as
// x approaches 0 from the negative side.
//
// The result is -log(-x).
//
// From there, it's simple to shift the asymptote line by
// adding 1.0 to the argument.
//
// This produces -log(1-x).
//
// At this point, we realize that it would be nice if 0
// utilization yielded a slightly negative score, with a zero
// score somewhere in the ballpark of 70-90% utilization. We
// pick 7/8 = 0.875 utilization because it can be represented
// exactly in IEEE floating point. Playing around with the
// numbers, and recalling that log(1) = 0, it's easy to
// discover that scaling log's argument by 8 results in
// utilization 7/8 returning a zero score.
//
// Thus, our final function is -log(8*(1-utilization)).
//
return (float64(latency) / 32.0) - math.Log(8.0-8.0*utilization)
}
}
func sendFlush(ch chan<- struct{}) {
select {
case ch <- struct{}{}:
default:
}
}