forked from go-chassis/go-chassis
/
loadbalance_handler.go
executable file
·307 lines (272 loc) · 9.43 KB
/
loadbalance_handler.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
package handler
import (
"errors"
"strings"
"time"
"github.com/ServiceComb/go-chassis/client/rest"
"github.com/ServiceComb/go-chassis/core/archaius"
"github.com/ServiceComb/go-chassis/core/common"
"github.com/ServiceComb/go-chassis/core/invocation"
"github.com/ServiceComb/go-chassis/core/lager"
"github.com/ServiceComb/go-chassis/core/loadbalance"
"github.com/ServiceComb/go-chassis/third_party/forked/go-micro/selector"
"github.com/ServiceComb/go-chassis/third_party/forked/valyala/fasthttp"
"github.com/cenkalti/backoff"
)
const (
lbPrefix = "cse.loadbalance"
propertyStrategyName = "strategy.name"
propertySessionStickinessRuleTimeout = "SessionStickinessRule.sessionTimeoutInSeconds"
propertySessionStickinessRuleFailedTimes = "SessionStickinessRule.successiveFailedTimes"
propertyRetryEnabled = "retryEnabled"
propertyRetryOnNext = "retryOnNext"
propertyRetryOnSame = "retryOnSame"
propertyBackoffKind = "backoff.kind"
propertyBackoffMinMs = "backoff.minMs"
propertyBackoffMaxMs = "backoff.maxMs"
backoffJittered = "jittered"
backoffConstant = "constant"
backoffZero = "zero"
)
// LBHandler loadbalancer handler struct
type LBHandler struct{}
// StrategyName strategy name
func StrategyName(i *invocation.Invocation) string {
global := archaius.GetString(genKey(lbPrefix, propertyStrategyName), loadbalance.StrategyRoundRobin)
ms := archaius.GetString(genMsKey(lbPrefix, i.SourceMicroService, i.MicroServiceName, propertyStrategyName), global)
return ms
}
// StrategySessionTimeout strategy session timeout
func StrategySessionTimeout(i *invocation.Invocation) int {
global := archaius.GetInt(genKey(lbPrefix, propertySessionStickinessRuleTimeout), 30)
ms := archaius.GetInt(genMsKey(lbPrefix, i.SourceMicroService, i.MicroServiceName, propertySessionStickinessRuleTimeout), global)
return ms
}
// StrategySuccessiveFailedTimes strategy successive failed times
func StrategySuccessiveFailedTimes(i *invocation.Invocation) int {
global := archaius.GetInt(genKey(lbPrefix, propertySessionStickinessRuleFailedTimes), 5)
ms := archaius.GetInt(genMsKey(lbPrefix, i.SourceMicroService, i.MicroServiceName, propertySessionStickinessRuleFailedTimes), global)
return ms
}
// retryEnabled retry enabled
func (lb *LBHandler) retryEnabled(i *invocation.Invocation) bool {
global := archaius.GetBool(genKey(lbPrefix, propertyRetryEnabled), false)
ms := archaius.GetBool(genMsKey(lbPrefix, i.SourceMicroService, i.MicroServiceName, propertyRetryEnabled), global)
return ms
}
func (lb *LBHandler) retryOnNext(i *invocation.Invocation) int {
global := archaius.GetInt(genKey(lbPrefix, propertyRetryOnNext), 0)
ms := archaius.GetInt(genMsKey(lbPrefix, i.SourceMicroService, i.MicroServiceName, propertyRetryOnNext), global)
return ms
}
func (lb *LBHandler) retryOnSame(i *invocation.Invocation) int {
global := archaius.GetInt(genKey(lbPrefix, propertyRetryOnSame), 0)
ms := archaius.GetInt(genMsKey(lbPrefix, i.SourceMicroService, i.MicroServiceName, propertyRetryOnSame), global)
return ms
}
func (lb *LBHandler) backoffKind(i *invocation.Invocation) string {
global := archaius.GetString(genKey(lbPrefix, propertyBackoffKind), backoffZero)
ms := archaius.GetString(genMsKey(lbPrefix, i.SourceMicroService, i.MicroServiceName, propertyBackoffKind), global)
return ms
}
func (lb *LBHandler) backoffMinMs(i *invocation.Invocation) int {
global := archaius.GetInt(genKey(lbPrefix, propertyBackoffMinMs), 0)
ms := archaius.GetInt(genMsKey(lbPrefix, i.SourceMicroService, i.MicroServiceName, propertyBackoffMinMs), global)
return ms
}
func (lb *LBHandler) backoffMaxMs(i *invocation.Invocation) int {
global := archaius.GetInt(genKey(lbPrefix, propertyBackoffMaxMs), 0)
ms := archaius.GetInt(genMsKey(lbPrefix, i.SourceMicroService, i.MicroServiceName, propertyBackoffMaxMs), global)
return ms
}
func (lb *LBHandler) getBackOff(i *invocation.Invocation) backoff.BackOff {
backoffKind := lb.backoffKind(i)
backMin := lb.backoffMinMs(i)
backMax := lb.backoffMaxMs(i)
switch backoffKind {
case backoffJittered:
return &backoff.ExponentialBackOff{
InitialInterval: time.Duration(backMin) * time.Millisecond,
RandomizationFactor: backoff.DefaultRandomizationFactor,
Multiplier: backoff.DefaultMultiplier,
MaxInterval: time.Duration(backMax) * time.Millisecond,
MaxElapsedTime: 0,
Clock: backoff.SystemClock,
}
case backoffConstant:
return backoff.NewConstantBackOff(time.Duration(backMin) * time.Millisecond)
case backoffZero:
return &backoff.ZeroBackOff{}
default:
lager.Logger.Errorf(nil, "Not support backoff kind: %s, reset to: zero.", backoffKind)
return &backoff.ZeroBackOff{}
}
}
func (lb *LBHandler) getEndpoint(i *invocation.Invocation, cb invocation.ResponseCallBack) (string, error) {
var metadata interface{}
strategy := i.Strategy
var strategyFun selector.Strategy
var err error
if strategy == "" {
strategyName := StrategyName(i)
i.Strategy = strategyName
strategyFun, err = loadbalance.GetStrategyPlugin(strategyName)
if err != nil {
lager.Logger.Errorf(err, selector.LBError{
Message: "Get strategy [" + strategyName + "] failed."}.Error())
}
} else {
strategyFun, err = loadbalance.GetStrategyPlugin(strategy)
if err != nil {
lager.Logger.Errorf(err, selector.LBError{
Message: "Get strategy [" + strategy + "] failed."}.Error())
}
}
//append filters in config
filters := archaius.GetServerListFilters()
for _, fName := range filters {
f := loadbalance.Filters[fName]
if f != nil {
i.Filters = append(i.Filters, f)
continue
}
}
if i.Strategy == loadbalance.StrategySessionStickiness {
metadata = getSessionID(i)
}
if i.Strategy == loadbalance.StrategyLatency {
metadata = i.MicroServiceName + "/" + i.Protocol
}
next, err := loadbalance.DefaultSelector.Select(
i.MicroServiceName, i.Version,
selector.WithStrategy(strategyFun),
selector.WithFilter(i.Filters),
selector.WithAppID(i.AppID),
selector.WithConsumerID(i.SourceServiceID),
selector.WithMetadata(metadata))
if err != nil {
writeErr(err, cb)
return "", err
}
ins, err := next()
if err != nil {
lbErr := selector.LBError{Message: err.Error()}
writeErr(lbErr, cb)
return "", lbErr
}
var ep string
if i.Protocol == "" {
i.Protocol = archaius.GetString("cse.references."+i.MicroServiceName+".transport", ins.DefaultProtocol)
}
if i.Protocol == "" {
for k := range ins.EndpointsMap {
i.Protocol = k
break
}
}
ep, ok := ins.EndpointsMap[i.Protocol]
if !ok {
errStr := "No available instance support [" + i.Protocol + "] protocol, msName: " + i.MicroServiceName
lbErr := selector.LBError{Message: errStr}
lager.Logger.Errorf(nil, lbErr.Error())
writeErr(lbErr, cb)
return "", lbErr
}
return ep, nil
}
// Handle to handle the load balancing
func (lb *LBHandler) Handle(chain *Chain, i *invocation.Invocation, cb invocation.ResponseCallBack) {
if !lb.retryEnabled(i) {
lb.handleWithNoRetry(chain, i, cb)
} else {
lb.handleWithRetry(chain, i, cb)
}
}
func (lb *LBHandler) handleWithNoRetry(chain *Chain, i *invocation.Invocation, cb invocation.ResponseCallBack) {
ep, err := lb.getEndpoint(i, cb)
if err != nil {
writeErr(err, cb)
return
}
i.Endpoint = ep
chain.Next(i, func(r *invocation.InvocationResponse) error {
return cb(r)
})
}
func (lb *LBHandler) handleWithRetry(chain *Chain, i *invocation.Invocation, cb invocation.ResponseCallBack) {
retryOnSame := lb.retryOnSame(i)
retryOnNext := lb.retryOnNext(i)
handlerIndex := chain.HandlerIndex
for j := 0; j < retryOnNext+1; j++ {
// exchange and retry on the next server
ep, err := lb.getEndpoint(i, cb)
if err != nil {
writeErr(err, cb)
return
}
// retry on the same server
lbBackoff := lb.getBackOff(i)
callTimes := 0
operation := func() error {
if callTimes == retryOnSame+1 {
return backoff.Permanent(errors.New("Retry time expires"))
}
callTimes++
i.Endpoint = ep
var respErr error
var callbackErr error
chain.HandlerIndex = handlerIndex
chain.Next(i, func(r *invocation.InvocationResponse) error {
respErr = r.Err
callbackErr = cb(r)
return callbackErr
})
if respErr != nil {
return respErr
}
if callbackErr != nil {
return callbackErr
}
return nil
}
if err = backoff.Retry(operation, lbBackoff); err == nil {
return
}
}
}
// Name returns loadbalance string
func (lb *LBHandler) Name() string {
return "loadbalance"
}
func newLBHandler() Handler {
return &LBHandler{}
}
func getSessionID(i *invocation.Invocation) interface{} {
var metadata interface{}
switch i.Args.(type) {
case *rest.Request:
req := i.Args.(*rest.Request)
value := req.GetHeader("Cookie")
cookieKey := strings.Split(value, "=")
if value != "" && (cookieKey[0] == common.SessionID || cookieKey[0] == common.LBSessionID) {
metadata = cookieKey[1]
}
case *fasthttp.Request:
req := i.Args.(*fasthttp.Request)
value := string(req.Header.Peek("Cookie"))
cookieKey := strings.Split(value, "=")
if value != "" && (cookieKey[0] == common.SessionID || cookieKey[0] == common.LBSessionID) {
metadata = cookieKey[1]
}
}
return metadata
}
func genKey(s ...string) string {
return strings.Join(s, ".")
}
func genMsKey(prefix, src, dest, property string) string {
if src == "" {
return genKey(prefix, dest, property)
}
return genKey(prefix, src, dest, property)
}