forked from bilibili/discovery
/
registry.go
391 lines (361 loc) · 10.1 KB
/
registry.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
package registry
import (
"fmt"
"math/rand"
"sync"
"time"
"github.com/bingochen87/discovery/conf"
"github.com/bingochen87/discovery/model"
"github.com/go-kratos/kratos/pkg/ecode"
log "github.com/go-kratos/kratos/pkg/log"
)
const (
_evictThreshold = int64(90 * time.Second)
_evictCeiling = int64(3600 * time.Second)
)
// Registry handles replication of all operations to peer Discovery nodes to keep them all in sync.
type Registry struct {
appm map[string]*model.Apps // appid-env -> apps
aLock sync.RWMutex
conns map[string]*hosts // region.zone.env.appid-> host
cLock sync.RWMutex
scheduler *scheduler
gd *Guard
}
type hosts struct {
hclock sync.RWMutex
hosts map[string]*conn // host name to conn
}
// conn the poll chan contains consumer.
type conn struct {
ch chan map[string]*model.InstanceInfo // TODO(felix): increase
arg *model.ArgPolls
latestTime int64
count int
}
// newConn new consumer chan.
func newConn(ch chan map[string]*model.InstanceInfo, latestTime int64, arg *model.ArgPolls) *conn {
return &conn{ch: ch, latestTime: latestTime, arg: arg, count: 1}
}
// NewRegistry new register.
func NewRegistry(conf *conf.Config) (r *Registry) {
r = &Registry{
appm: make(map[string]*model.Apps),
conns: make(map[string]*hosts),
gd: new(Guard),
}
r.scheduler = newScheduler(r)
r.scheduler.Load()
go r.scheduler.Reload()
go r.proc()
return
}
func (r *Registry) newapps(appid, env string) (a *model.Apps, ok bool) {
key := appsKey(appid, env)
r.aLock.Lock()
if a, ok = r.appm[key]; !ok {
a = model.NewApps()
r.appm[key] = a
}
r.aLock.Unlock()
return
}
func (r *Registry) apps(appid, env, zone string) (as []*model.App, a *model.Apps, ok bool) {
key := appsKey(appid, env)
r.aLock.RLock()
a, ok = r.appm[key]
r.aLock.RUnlock()
if ok {
as = a.App(zone)
}
return
}
func appsKey(appid, env string) string {
return fmt.Sprintf("%s-%s", appid, env)
}
func (r *Registry) newApp(ins *model.Instance) (a *model.App) {
as, _ := r.newapps(ins.AppID, ins.Env)
a, _ = as.NewApp(ins.Zone, ins.AppID, ins.LatestTimestamp)
return
}
// Register a new instance.
func (r *Registry) Register(ins *model.Instance, latestTime int64) (err error) {
a := r.newApp(ins)
i, ok := a.NewInstance(ins, latestTime)
if ok {
r.gd.incrExp()
}
// NOTE: make sure free poll before update appid latest timestamp.
r.broadcast(i.Env, i.AppID)
return
}
// Renew marks the given instance of the given app name as renewed, and also marks whether it originated from replication.
func (r *Registry) Renew(arg *model.ArgRenew) (i *model.Instance, ok bool) {
a, _, _ := r.apps(arg.AppID, arg.Env, arg.Zone)
if len(a) == 0 {
return
}
if i, ok = a[0].Renew(arg.Hostname); !ok {
return
}
r.gd.incrFac()
return
}
// Cancel cancels the registration of an instance.
func (r *Registry) Cancel(arg *model.ArgCancel) (i *model.Instance, ok bool) {
if i, ok = r.cancel(arg.Zone, arg.Env, arg.AppID, arg.Hostname, arg.LatestTimestamp); !ok {
return
}
r.gd.decrExp()
return
}
func (r *Registry) cancel(zone, env, appid, hostname string, latestTime int64) (i *model.Instance, ok bool) {
var l int
a, as, _ := r.apps(appid, env, zone)
if len(a) == 0 {
return
}
if i, l, ok = a[0].Cancel(hostname, latestTime); !ok {
return
}
as.UpdateLatest(latestTime)
if l == 0 {
if a[0].Len() == 0 {
as.Del(zone)
}
}
if len(as.App("")) == 0 {
r.aLock.Lock()
delete(r.appm, appsKey(appid, env))
r.aLock.Unlock()
}
r.broadcast(env, appid) // NOTE: make sure free poll before update appid latest timestamp.
return
}
// FetchAll fetch all instances of all the families.
func (r *Registry) FetchAll() (im map[string][]*model.Instance) {
ass := r.allapp()
im = make(map[string][]*model.Instance)
for _, as := range ass {
for _, a := range as.App("") {
im[a.AppID] = append(im[a.AppID], a.Instances()...)
}
}
return
}
// Fetch fetch all instances by appid.
func (r *Registry) Fetch(zone, env, appid string, latestTime int64, status uint32) (info *model.InstanceInfo, err error) {
key := appsKey(appid, env)
r.aLock.RLock()
a, ok := r.appm[key]
r.aLock.RUnlock()
if !ok {
err = ecode.NothingFound
return
}
info, err = a.InstanceInfo(zone, latestTime, status)
if err != nil {
return
}
sch := r.scheduler.Get(appid, env)
if sch != nil {
info.Scheduler = new(model.Scheduler)
info.Scheduler.Clients = sch.Clients
}
return
}
// Polls hangs request and then write instances when that has changes, or return NotModified.
func (r *Registry) Polls(arg *model.ArgPolls) (ch chan map[string]*model.InstanceInfo, new bool, miss []string, err error) {
var (
ins = make(map[string]*model.InstanceInfo, len(arg.AppID))
)
if len(arg.AppID) != len(arg.LatestTimestamp) {
arg.LatestTimestamp = make([]int64, len(arg.AppID))
}
for i := range arg.AppID {
in, err := r.Fetch(arg.Zone, arg.Env, arg.AppID[i], arg.LatestTimestamp[i], model.InstanceStatusUP)
if err == ecode.NothingFound {
miss = append(miss, arg.AppID[i])
log.Error("Polls zone(%s) env(%s) appid(%s) error(%v)", arg.Zone, arg.Env, arg.AppID[i], err)
continue
}
if err == nil {
ins[arg.AppID[i]] = in
new = true
}
}
if new {
ch = make(chan map[string]*model.InstanceInfo, 1)
ch <- ins
return
}
for i := range arg.AppID {
k := pollKey(arg.Env, arg.AppID[i])
r.cLock.Lock()
if _, ok := r.conns[k]; !ok {
r.conns[k] = &hosts{hosts: make(map[string]*conn, 1)}
}
hosts := r.conns[k]
r.cLock.Unlock()
hosts.hclock.Lock()
connection, ok := hosts.hosts[arg.Hostname]
if !ok {
if ch == nil {
ch = make(chan map[string]*model.InstanceInfo, 5) // NOTE: there maybe have more than one connection on the same hostname!!!
}
connection = newConn(ch, arg.LatestTimestamp[i], arg)
log.Info("Polls from(%s) new connection(%d)", arg.Hostname, connection.count)
} else {
connection.count++ // NOTE: there maybe have more than one connection on the same hostname!!!
if ch == nil {
ch = connection.ch
}
log.Info("Polls from(%s) reuse connection(%d)", arg.Hostname, connection.count)
}
hosts.hosts[arg.Hostname] = connection
hosts.hclock.Unlock()
}
return
}
// broadcast on poll by chan.
// NOTE: make sure free poll before update appid latest timestamp.
func (r *Registry) broadcast(env, appid string) {
key := pollKey(env, appid)
r.cLock.Lock()
conns, ok := r.conns[key]
if !ok {
r.cLock.Unlock()
return
}
delete(r.conns, key)
r.cLock.Unlock()
conns.hclock.RLock()
for _, conn := range conns.hosts {
ii, err := r.Fetch(conn.arg.Zone, env, appid, 0, model.InstanceStatusUP) // TODO(felix): latesttime!=0 increase
if err != nil {
// may be not found ,just continue until next poll return err.
log.Error("get appid:%s env:%s zone:%s err:%v", appid, env, conn.arg.Zone, err)
continue
}
for i := 0; i < conn.count; i++ {
select {
case conn.ch <- map[string]*model.InstanceInfo{appid: ii}: // NOTE: if chan is full, means no poller.
log.Info("broadcast to(%s) success(%d)", conn.arg.Hostname, i+1)
case <-time.After(time.Millisecond * 500):
log.Info("broadcast to(%s) failed(%d) maybe chan full", conn.arg.Hostname, i+1)
}
}
}
conns.hclock.RUnlock()
}
func pollKey(env, appid string) string {
return fmt.Sprintf("%s.%s", env, appid)
}
// Set Set the metadata of instance by hostnames.
func (r *Registry) Set(arg *model.ArgSet) (ok bool) {
a, _, _ := r.apps(arg.AppID, arg.Env, arg.Zone)
if len(a) == 0 {
return
}
if ok = a[0].Set(arg); !ok {
return
}
r.broadcast(arg.Env, arg.AppID)
return
}
func (r *Registry) allapp() (ass []*model.Apps) {
r.aLock.RLock()
ass = make([]*model.Apps, 0, len(r.appm))
for _, as := range r.appm {
ass = append(ass, as)
}
r.aLock.RUnlock()
return
}
// reset expect renews, count the renew of all app, one app has two expect remews in minute.
func (r *Registry) resetExp() {
cnt := int64(0)
for _, p := range r.allapp() {
for _, a := range p.App("") {
cnt += int64(a.Len())
}
}
r.gd.setExp(cnt)
}
func (r *Registry) proc() {
tk := time.Tick(1 * time.Minute)
tk2 := time.Tick(15 * time.Minute)
for {
select {
case <-tk:
r.gd.updateFac()
r.evict()
case <-tk2:
r.resetExp()
}
}
}
func (r *Registry) evict() {
protect := r.gd.ok()
// We collect first all expired items, to evict them in random order. For large eviction sets,
// if we do not that, we might wipe out whole apps before self preservation kicks in. By randomizing it,
// the impact should be evenly distributed across all applications.
var eis []*model.Instance
var registrySize int
// all projects
ass := r.allapp()
for _, as := range ass {
for _, a := range as.App("") {
registrySize += a.Len()
is := a.Instances()
for _, i := range is {
delta := time.Now().UnixNano() - i.RenewTimestamp
if (!protect && delta > _evictThreshold) || delta > _evictCeiling {
eis = append(eis, i)
}
}
}
}
// To compensate for GC pauses or drifting local time, we need to use current registry size as a base for
// triggering self-preservation. Without that we would wipe out full registry.
eCnt := len(eis)
registrySizeThreshold := int(float64(registrySize) * _percentThreshold)
evictionLimit := registrySize - registrySizeThreshold
if eCnt > evictionLimit {
eCnt = evictionLimit
}
if eCnt == 0 {
return
}
for i := 0; i < eCnt; i++ {
// Pick a random item (Knuth shuffle algorithm)
next := i + rand.Intn(len(eis)-i)
eis[i], eis[next] = eis[next], eis[i]
ei := eis[i]
r.cancel(ei.Zone, ei.Env, ei.AppID, ei.Hostname, time.Now().UnixNano())
}
}
// DelConns delete conn of host in appid
func (r *Registry) DelConns(arg *model.ArgPolls) {
for i := range arg.AppID {
r.cLock.Lock()
k := pollKey(arg.Env, arg.AppID[i])
conns, ok := r.conns[k]
r.cLock.Unlock()
if !ok {
log.Warn("DelConn key(%s) not found", k)
continue
}
conns.hclock.Lock()
if connection, ok := conns.hosts[arg.Hostname]; ok {
if connection.count > 1 {
log.Info("DelConns from(%s) count decr(%d)", arg.Hostname, connection.count)
connection.count--
} else {
log.Info("DelConns from(%s) delete(%d)", arg.Hostname, connection.count)
delete(conns.hosts, arg.Hostname)
}
}
conns.hclock.Unlock()
}
}