forked from bilibili/discovery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstance.go
357 lines (326 loc) · 8.26 KB
/
instance.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
package model
import (
"encoding/json"
"sync"
"time"
"github.com/bilibili/kratos/pkg/ecode"
log "github.com/bilibili/kratos/pkg/log"
)
// InstanceStatus Status of instance
// type InstanceStatus uint32
const (
// InstanceStatusUP Ready to receive traffic
InstanceStatusUP = uint32(1)
// InstancestatusWating Intentionally shutdown for traffic
InstancestatusWating = uint32(1) << 1
)
func (i *Instance) filter(status uint32) bool {
return status&i.Status > 0
}
// Action Replicate type of node
type Action int
const (
// Register Replicate the add action to all nodes
Register Action = iota
// Renew Replicate the heartbeat action to all nodes
Renew
// Cancel Replicate the cancel action to all nodes
Cancel
// Weight Replicate the Weight action to all nodes
Weight
// Delete Replicate the Delete action to all nodes
Delete
// Status Replicate the Status action to all nodes
Status
)
// Instance holds information required for registration with
// <Discovery Server> and to be discovered by other components.
type Instance struct {
Region string `json:"region"`
Zone string `json:"zone"`
Env string `json:"env"`
AppID string `json:"appid"`
Hostname string `json:"hostname"`
Addrs []string `json:"addrs"`
Version string `json:"version"`
Metadata map[string]string `json:"metadata"`
// Status enum instance status
Status uint32 `json:"status"`
// timestamp
RegTimestamp int64 `json:"reg_timestamp"`
UpTimestamp int64 `json:"up_timestamp"` // NOTE: It is latest timestamp that status becomes UP.
RenewTimestamp int64 `json:"renew_timestamp"`
DirtyTimestamp int64 `json:"dirty_timestamp"`
LatestTimestamp int64 `json:"latest_timestamp"`
}
// NewInstance new a instance.
func NewInstance(arg *ArgRegister) (i *Instance) {
now := time.Now().UnixNano()
i = &Instance{
Region: arg.Region,
Zone: arg.Zone,
Env: arg.Env,
AppID: arg.AppID,
Hostname: arg.Hostname,
Addrs: arg.Addrs,
Version: arg.Version,
Status: arg.Status,
RegTimestamp: now,
UpTimestamp: now,
LatestTimestamp: now,
RenewTimestamp: now,
DirtyTimestamp: now,
}
if arg.Metadata != "" {
if err := json.Unmarshal([]byte(arg.Metadata), &i.Metadata); err != nil {
log.Error("json unmarshal metadata err %v", err)
}
}
return
}
// deep copy a new instance from old one
func copyInstance(oi *Instance) (ni *Instance) {
ni = new(Instance)
*ni = *oi
ni.Addrs = make([]string, len(oi.Addrs))
for i, add := range oi.Addrs {
ni.Addrs[i] = add
}
ni.Metadata = make(map[string]string)
for k, v := range oi.Metadata {
ni.Metadata[k] = v
}
return
}
// InstanceInfo the info get by consumer.
type InstanceInfo struct {
Instances map[string][]*Instance `json:"instances"`
Scheduler *Scheduler `json:"scheduler,omitempty"`
LatestTimestamp int64 `json:"latest_timestamp"`
}
// Apps app distinguished by zone
type Apps struct {
apps map[string]*App
lock sync.RWMutex
latestTimestamp int64
}
// NewApps return new Apps.
func NewApps() *Apps {
return &Apps{
apps: make(map[string]*App),
}
}
// NewApp news a app by appid. If ok=false, returns the app of already exist.
func (p *Apps) NewApp(zone, appid string, lts int64) (a *App, new bool) {
p.lock.Lock()
a, ok := p.apps[zone]
if !ok {
a = NewApp(zone, appid)
p.apps[zone] = a
}
if lts <= p.latestTimestamp {
// insure increase
lts = p.latestTimestamp + 1
}
p.latestTimestamp = lts
p.lock.Unlock()
new = !ok
return
}
// App get app by zone.
func (p *Apps) App(zone string) (as []*App) {
p.lock.RLock()
if zone != "" {
a, ok := p.apps[zone]
if !ok {
p.lock.RUnlock()
return
}
as = []*App{a}
} else {
for _, a := range p.apps {
as = append(as, a)
}
}
p.lock.RUnlock()
return
}
// Del del app by zone.
func (p *Apps) Del(zone string) {
p.lock.Lock()
delete(p.apps, zone)
p.lock.Unlock()
}
// InstanceInfo return slice of instances.if up is true,return all status instance else return up status instance
func (p *Apps) InstanceInfo(zone string, latestTime int64, status uint32) (ci *InstanceInfo, err error) {
p.lock.RLock()
defer p.lock.RUnlock()
if latestTime >= p.latestTimestamp {
err = ecode.NotModified
return
}
ci = &InstanceInfo{
LatestTimestamp: p.latestTimestamp,
Instances: make(map[string][]*Instance),
}
var ok bool
for z, app := range p.apps {
if zone == "" || z == zone {
ok = true
instances := make([]*Instance, 0)
for _, i := range app.Instances() {
// if up is false return all status instance
if i.filter(status) {
// if i.Status == InstanceStatusUP && i.LatestTimestamp > latestTime { // TODO(felix): increase
ni := copyInstance(i)
instances = append(instances, ni)
}
}
ci.Instances[z] = instances
}
}
if !ok {
err = ecode.NothingFound
} else if len(ci.Instances) == 0 {
err = ecode.NotModified
}
return
}
// UpdateLatest update LatestTimestamp.
func (p *Apps) UpdateLatest(latestTime int64) {
p.lock.Lock()
if latestTime <= p.latestTimestamp {
// insure increase
latestTime = p.latestTimestamp + 1
}
p.latestTimestamp = latestTime
p.lock.Unlock()
}
// App Instances distinguished by hostname
type App struct {
AppID string
Zone string
instances map[string]*Instance
latestTimestamp int64
lock sync.RWMutex
}
// NewApp new App.
func NewApp(zone, appid string) (a *App) {
a = &App{
AppID: appid,
Zone: zone,
instances: make(map[string]*Instance),
}
return
}
// Instances return slice of instances.
func (a *App) Instances() (is []*Instance) {
a.lock.RLock()
is = make([]*Instance, 0, len(a.instances))
for _, i := range a.instances {
ni := new(Instance)
*ni = *i
is = append(is, ni)
}
a.lock.RUnlock()
return
}
// NewInstance new a instance.
func (a *App) NewInstance(ni *Instance, latestTime int64) (i *Instance, ok bool) {
i = new(Instance)
a.lock.Lock()
oi, ok := a.instances[ni.Hostname]
if ok {
ni.UpTimestamp = oi.UpTimestamp
if ni.DirtyTimestamp < oi.DirtyTimestamp {
log.Warn("register exist(%v) dirty timestamp over than caller(%v)", oi, ni)
ni = oi
}
}
a.instances[ni.Hostname] = ni
a.updateLatest(latestTime)
*i = *ni
a.lock.Unlock()
ok = !ok
return
}
// Renew new a instance.
func (a *App) Renew(hostname string) (i *Instance, ok bool) {
i = new(Instance)
a.lock.Lock()
defer a.lock.Unlock()
oi, ok := a.instances[hostname]
if !ok {
return
}
oi.RenewTimestamp = time.Now().UnixNano()
i = copyInstance(oi)
return
}
func (a *App) updateLatest(latestTime int64) {
if latestTime <= a.latestTimestamp {
// insure increase
latestTime = a.latestTimestamp + 1
}
a.latestTimestamp = latestTime
}
// Cancel cancel a instance.
func (a *App) Cancel(hostname string, latestTime int64) (i *Instance, l int, ok bool) {
i = new(Instance)
a.lock.Lock()
defer a.lock.Unlock()
oi, ok := a.instances[hostname]
if !ok {
return
}
delete(a.instances, hostname)
l = len(a.instances)
oi.LatestTimestamp = latestTime
a.updateLatest(latestTime)
*i = *oi
return
}
// Len returns the length of instances.
func (a *App) Len() (l int) {
a.lock.RLock()
l = len(a.instances)
a.lock.RUnlock()
return
}
// Set set new status,metadata,color of instance .
func (a *App) Set(changes *ArgSet) (ok bool) {
a.lock.Lock()
defer a.lock.Unlock()
var (
dst *Instance
setTime = changes.SetTimestamp
)
for i, hostname := range changes.Hostname {
if dst, ok = a.instances[hostname]; !ok {
log.Error("SetWeight hostname(%s) not found", hostname)
return
}
if len(changes.Status) != 0 {
if uint32(changes.Status[i]) != InstanceStatusUP && uint32(changes.Status[i]) != InstancestatusWating {
log.Error("SetWeight change status(%d) is error", changes.Status[i])
ok = false
return
}
dst.Status = uint32(changes.Status[i])
if dst.Status == InstanceStatusUP {
dst.UpTimestamp = setTime
}
}
if len(changes.Metadata) != 0 {
if err := json.Unmarshal([]byte(changes.Metadata[i]), &dst.Metadata); err != nil {
log.Error("set change metadata err %s", changes.Metadata[i])
ok = false
return
}
}
dst.LatestTimestamp = setTime
dst.DirtyTimestamp = setTime
}
a.updateLatest(setTime)
return
}