/
uniform_rule.go
258 lines (236 loc) · 7.45 KB
/
uniform_rule.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 v3router
import (
"math/rand"
"time"
)
import (
perrors "github.com/pkg/errors"
)
import (
"dubbo.apache.org/dubbo-go/v3/cluster/router/v3router/judger"
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/logger"
"dubbo.apache.org/dubbo-go/v3/config"
"dubbo.apache.org/dubbo-go/v3/protocol"
)
// VirtualServiceRule is item of virtual service, it aims at judge if invocation context match it's condition, and
// if match, get result destination key, which should be defined in DestinationRule yaml file
type VirtualServiceRule struct {
// routerItem store match router list and destination list of this router
routerItem *config.DubboRouteDetail
// uniformRule is the upper struct ptr
uniformRule *UniformRule
}
// match read from VirtualServiceRule's Match config
// it judges if this invocation matches the router rule request defined in config one by one
func (r *VirtualServiceRule) match(url *common.URL, invocation protocol.Invocation) bool {
for _, v := range r.routerItem.Match {
// method match judge
if v.Method != nil {
methodMatchJudger := judger.NewMethodMatchJudger(v.Method)
if !methodMatchJudger.Judge(invocation) {
return false
}
}
// source label match judge
if !judger.JudgeUrlLabel(url, v.SourceLabels) {
return false
}
// attachment match judge
if v.Attachment != nil {
attachmentMatchJudger := judger.NewAttachmentMatchJudger(v.Attachment)
if attachmentMatchJudger.Judge(invocation) {
return false
}
}
// threshold match judge
// todo
// reserve match judge
// todo
}
return true
}
// tryGetSubsetFromRouterOfOneDestination is a recursion function
// try from destination 's header to final fallback destination, when success, it return result destination, else return error
func (r *VirtualServiceRule) tryGetSubsetFromRouterOfOneDestination(desc *config.DubboDestination, invokers []protocol.Invoker) ([]protocol.Invoker, int, error) {
subSet := desc.Destination.Subset
labels, ok := r.uniformRule.DestinationLabelListMap[subSet]
resultInvokers := make([]protocol.Invoker, 0)
if ok {
for _, v := range invokers {
if judger.JudgeUrlLabel(v.GetURL(), labels) {
resultInvokers = append(resultInvokers, v)
}
}
if len(resultInvokers) != 0 {
return resultInvokers, desc.Destination.Weight, nil
}
}
if desc.Destination.Fallback != nil {
return r.tryGetSubsetFromRouterOfOneDestination(desc.Destination.Fallback, invokers)
}
return nil, 0, perrors.New("No invoker matches and no fallback destination to choose!")
}
//weightInvokersPair stores weight and invoker list.
type weightInvokersPair struct {
weight int
invokerList []protocol.Invoker
}
type weightInvokerPairResults struct {
pairs []weightInvokersPair
}
func (w *weightInvokerPairResults) getTargetInvokers() []protocol.Invoker {
if len(w.pairs) == 0 {
return []protocol.Invoker{}
}
if len(w.pairs) == 1 {
return w.pairs[0].invokerList
}
rand.Seed(time.Now().UnixNano())
target := rand.Intn(100)
// noweight means all weigh is zero, random choose one invoker list
noWeight := true
// check if empty
for _, v := range w.pairs {
if v.weight != 0 {
noWeight = false // user defined weight
break
}
}
if noWeight {
// random choose one list
weitUnit := 100/len(w.pairs) + 1
return w.pairs[target/weitUnit].invokerList
} else {
total := 0
for _, v := range w.pairs {
total += v.weight
if total > target {
return v.invokerList
}
}
}
// invalid weight set: total is smaller than 100, choose first
return w.pairs[0].invokerList
}
func (r *VirtualServiceRule) getRuleTargetInvokers(invokers []protocol.Invoker) ([]protocol.Invoker, error) {
// weightInvokerPairResult is the collection routerDesc of all destination fields,
weightInvokerPairResult := weightInvokerPairResults{}
for _, v := range r.routerItem.Router {
// v is one destination 's header e.g.
/*
route:
- destination: # v is here
host: demo
subset: v1
fallback:
destination:
host: demo
subset: v2
fallback:
destination:
host: demo
subset: v3
- destination:
host: demo
subset: v4
fallback:
destination:
host: demo
subset: v5
fallback:
destination:
host: demo
subset: v6
*/
invokerListOfOneDest, weight, err := r.tryGetSubsetFromRouterOfOneDestination(v, invokers)
if err != nil {
return nil, err
}
// combination of all destination field e.g.
/*
- destination:
host: demo
subset: na61
- destination:
host: demo
subset: na610
*/
weightInvokerPairResult.pairs = append(weightInvokerPairResult.pairs, weightInvokersPair{
weight: weight,
invokerList: invokerListOfOneDest,
})
}
return weightInvokerPairResult.getTargetInvokers(), nil
}
// UniformRule uniform rule
type UniformRule struct {
services []*config.StringMatch
virtualServiceRules []VirtualServiceRule
DestinationLabelListMap map[string]map[string]string
}
// NewDefaultConnChecker constructs a new DefaultConnChecker based on the url
func newUniformRule(dubboRoute *config.DubboRoute, destinationMap map[string]map[string]string) *UniformRule {
uniformRule := &UniformRule{
services: dubboRoute.Services,
DestinationLabelListMap: destinationMap,
}
routeDetail := dubboRoute.RouterDetail
virtualServiceRules := make([]VirtualServiceRule, 0)
for _, v := range routeDetail {
virtualServiceRules = append(virtualServiceRules, VirtualServiceRule{
routerItem: v,
uniformRule: uniformRule,
})
}
uniformRule.virtualServiceRules = virtualServiceRules
return uniformRule
}
func (u *UniformRule) route(invokers []protocol.Invoker, url *common.URL, invocation protocol.Invocation) []protocol.Invoker {
// service rule + destination -> filter
resultInvokers := make([]protocol.Invoker, 0)
var err error
matchService := false
for _, v := range u.services {
// check if match service field
if judger.NewStringMatchJudger(v).Judge(url.ServiceKey()) {
matchService = true
break
}
}
if !matchService {
// if not match, jump this rule
return resultInvokers
}
// match service field, route Details level(service level) match
// then, check all sub rule, if match, get destination rule target invokers, else do fail back logic
for _, rule := range u.virtualServiceRules {
if rule.match(url, invocation) {
// match this rule, do get target logic
resultInvokers, err = rule.getRuleTargetInvokers(invokers)
if err != nil {
logger.Error("getRuleTargetInvokers from rule err = ", err)
return nil
}
return resultInvokers
}
}
logger.Errorf("no match rule for invokers %+v", invokers)
return resultInvokers
}