/
instance.go
170 lines (148 loc) · 4.18 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
/**
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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 model
import (
"strconv"
"sync"
)
type ServiceInstances struct {
lock sync.RWMutex
instances map[string]*Instance
healthyInstances map[string]*Instance
unhealthyInstances map[string]*Instance
protectInstances map[string]*Instance
protectThreshold float32
}
func NewServiceInstances(protectThreshold float32) *ServiceInstances {
return &ServiceInstances{
instances: make(map[string]*Instance, 128),
healthyInstances: make(map[string]*Instance, 128),
unhealthyInstances: make(map[string]*Instance, 128),
protectInstances: make(map[string]*Instance, 128),
}
}
func (si *ServiceInstances) TotalCount() int {
si.lock.RLock()
defer si.lock.RUnlock()
return len(si.instances)
}
func (si *ServiceInstances) UpdateProtectThreshold(protectThreshold float32) {
si.lock.Lock()
defer si.lock.Unlock()
si.protectThreshold = protectThreshold
}
func (si *ServiceInstances) UpsertInstance(ins *Instance) {
si.lock.Lock()
defer si.lock.Unlock()
si.instances[ins.ID()] = ins
if ins.Healthy() {
si.healthyInstances[ins.ID()] = ins
} else {
si.unhealthyInstances[ins.ID()] = ins
}
}
func (si *ServiceInstances) RemoveInstance(ins *Instance) {
si.lock.Lock()
defer si.lock.Unlock()
delete(si.instances, ins.ID())
delete(si.healthyInstances, ins.ID())
delete(si.unhealthyInstances, ins.ID())
delete(si.protectInstances, ins.ID())
}
func (si *ServiceInstances) Range(iterator func(id string, ins *Instance)) {
si.lock.RLock()
defer si.lock.RUnlock()
for k, v := range si.instances {
iterator(k, v)
}
}
func (si *ServiceInstances) GetInstances(onlyHealthy bool) []*Instance {
si.lock.RLock()
defer si.lock.RUnlock()
ret := make([]*Instance, 0, len(si.healthyInstances)+len(si.protectInstances))
if !onlyHealthy {
for k, v := range si.instances {
protectIns, ok := si.protectInstances[k]
if ok {
ret = append(ret, protectIns)
} else {
ret = append(ret, v)
}
}
} else {
for _, v := range si.healthyInstances {
ret = append(ret, v)
}
for _, v := range si.protectInstances {
ret = append(ret, v)
}
}
return ret
}
func (si *ServiceInstances) ReachHealthyProtect() bool {
si.lock.RLock()
defer si.lock.RUnlock()
return len(si.protectInstances) > 0
}
func (si *ServiceInstances) RunHealthyProtect() {
si.lock.Lock()
defer si.lock.Unlock()
lastBeat := int64(-1)
curProportion := float32(len(si.healthyInstances)) / float32(len(si.instances))
if curProportion > si.protectThreshold {
// 不会触发, 并且清空当前保护状态的实例
si.protectInstances = make(map[string]*Instance, 128)
return
}
instanceLastBeatTimes := map[string]int64{}
instances := si.unhealthyInstances
for i := range instances {
ins := instances[i]
metadata := ins.Metadata()
if len(metadata) == 0 {
continue
}
val, ok := metadata[MetadataInstanceLastHeartbeatTime]
if !ok {
continue
}
beatTime, _ := strconv.ParseInt(val, 10, 64)
if beatTime >= lastBeat {
lastBeat = beatTime
}
instanceLastBeatTimes[ins.ID()] = beatTime
}
if lastBeat == -1 {
return
}
for i := range instances {
ins := instances[i]
beatTime, ok := instanceLastBeatTimes[ins.ID()]
if !ok {
continue
}
needProtect := needZeroProtect(lastBeat, beatTime, int64(ins.HealthCheck().GetHeartbeat().GetTtl().GetValue()))
if !needProtect {
continue
}
si.protectInstances[ins.ID()] = ins
}
}
// needZeroProtect .
func needZeroProtect(lastBeat, beatTime, ttl int64) bool {
return lastBeat-3*ttl > beatTime
}