-
Notifications
You must be signed in to change notification settings - Fork 905
/
tag_router.go
387 lines (337 loc) · 11.5 KB
/
tag_router.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
/*
* 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 tag
import (
"strconv"
"strings"
"sync"
)
import (
"github.com/RoaringBitmap/roaring"
gxnet "github.com/dubbogo/gost/net"
perrors "github.com/pkg/errors"
)
import (
"github.com/apache/dubbo-go/cluster/router"
"github.com/apache/dubbo-go/cluster/router/utils"
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/config"
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/logger"
"github.com/apache/dubbo-go/config_center"
"github.com/apache/dubbo-go/protocol"
"github.com/apache/dubbo-go/remoting"
)
const (
name = "tag-router"
staticPrefix = "static-"
dynamicPrefix = "dynamic-"
)
// addrMetadata keeps snapshot data for app name, and some infos extracted from dynamic tag rule in order to make
// Route() method lock-free.
type addrMetadata struct {
// application name
application string
// is rule a runtime rule
// ruleRuntime bool
// is rule a force rule
ruleForce bool
// is rule a valid rule
ruleValid bool
// is rule an enabled rule
ruleEnabled bool
}
// Source indicates where the metadata comes from.
func (m *addrMetadata) Source() string {
return name
}
// tagRouter defines url, enable and the priority
type tagRouter struct {
url *common.URL
tagRouterRule *RouterRule
enabled bool
priority int64
application string
ruleChanged bool
mutex sync.RWMutex
notify chan struct{}
}
// NewTagRouter returns a tagRouter instance if url is not nil
func NewTagRouter(url *common.URL, notify chan struct{}) (*tagRouter, error) {
if url == nil {
return nil, perrors.Errorf("Illegal route URL!")
}
r := &tagRouter{
url: url,
enabled: url.GetParamBool(constant.RouterEnabled, true),
priority: url.GetParamInt(constant.RouterPriority, 0),
notify: notify,
}
if content, err := url.GetParamAndDecoded(constant.RULE_KEY); err != nil {
return nil, err
} else if content != "" {
if rule, err := getRule(content); err != nil {
return nil, err
} else {
r.tagRouterRule = rule
r.ruleChanged = true
}
}
return r, nil
}
// nolint
func (c *tagRouter) isEnabled() bool {
return c.enabled
}
// Route gets a list of invoker
func (c *tagRouter) Route(invokers *roaring.Bitmap, cache router.Cache, url *common.URL, invocation protocol.Invocation) *roaring.Bitmap {
if !c.isEnabled() || invokers.IsEmpty() {
return invokers
}
if shouldUseDynamicTag(cache.FindAddrMeta(c)) {
return c.routeWithDynamicTag(invokers, cache, url, invocation)
}
return c.routeWithStaticTag(invokers, cache, url, invocation)
}
// routeWithStaticTag routes with static tag rule
func (c *tagRouter) routeWithStaticTag(invokers *roaring.Bitmap, cache router.Cache, url *common.URL, invocation protocol.Invocation) *roaring.Bitmap {
tag := findTag(invocation, url)
if tag == "" {
return invokers
}
ret, _ := c.filterWithTag(invokers, cache, staticPrefix+tag)
if ret.IsEmpty() && !isForceUseTag(url, invocation) {
return invokers
}
return ret
}
// routeWithDynamicTag routes with dynamic tag rule
func (c *tagRouter) routeWithDynamicTag(invokers *roaring.Bitmap, cache router.Cache, url *common.URL, invocation protocol.Invocation) *roaring.Bitmap {
tag := findTag(invocation, url)
if tag == "" {
return c.filterNotInDynamicTag(invokers, cache)
}
ret, ok := c.filterWithTag(invokers, cache, dynamicPrefix+tag)
if ok && (!ret.IsEmpty() || isTagRuleForce(cache.FindAddrMeta(c))) {
return ret
}
// dynamic tag group doesn't have any item about the requested app OR it's null after filtered by
// dynamic tag group but force=false. check static tag
if ret.IsEmpty() {
ret, _ = c.filterWithTag(invokers, cache, staticPrefix+tag)
// If there's no tagged providers that can match the current tagged request. force.tag is set by default
// to false, which means it will invoke any providers without a tag unless it's explicitly disallowed.
if !ret.IsEmpty() || isForceUseTag(url, invocation) {
return ret
}
return c.filterNotInDynamicTag(invokers, cache)
}
return ret
}
// filterWithTag filters incoming invokers with the given tag
func (c *tagRouter) filterWithTag(invokers *roaring.Bitmap, cache router.Cache, tag string) (*roaring.Bitmap, bool) {
if target, ok := cache.FindAddrPool(c)[tag]; ok {
return utils.JoinIfNotEqual(target, invokers), true
}
return utils.EmptyAddr, false
}
// filterNotInDynamicTag filters incoming invokers not applied to dynamic tag rule
func (c *tagRouter) filterNotInDynamicTag(invokers *roaring.Bitmap, cache router.Cache) *roaring.Bitmap {
// FAILOVER: return all Providers without any tags.
invokers = invokers.Clone()
for k, v := range cache.FindAddrPool(c) {
if strings.HasPrefix(k, dynamicPrefix) {
invokers.AndNot(v)
}
}
return invokers
}
// Process parses dynamic tag rule
func (c *tagRouter) Process(event *config_center.ConfigChangeEvent) {
logger.Infof("Notification of tag rule, change type is:[%s] , raw rule is:[%v]", event.ConfigType, event.Value)
if remoting.EventTypeDel == event.ConfigType {
c.tagRouterRule = nil
return
}
content, ok := event.Value.(string)
if !ok {
logger.Errorf("Convert event content fail,raw content:[%s] ", event.Value)
return
}
routerRule, err := getRule(content)
if err != nil {
logger.Errorf("Parse dynamic tag router rule fail,error:[%s] ", err)
return
}
c.mutex.Lock()
defer c.mutex.Unlock()
c.tagRouterRule = routerRule
c.ruleChanged = true
c.notify <- struct{}{}
}
// URL gets the url of tagRouter
func (c *tagRouter) URL() *common.URL {
return c.url
}
// Priority gets the priority of tagRouter
func (c *tagRouter) Priority() int64 {
return c.priority
}
// Pool divided invokers into different address pool by tag.
func (c *tagRouter) Pool(invokers []protocol.Invoker) (router.AddrPool, router.AddrMetadata) {
c.fetchRuleIfNecessary(invokers)
rb := make(router.AddrPool, 8)
poolWithStaticTag(invokers, rb)
c.mutex.Lock()
defer c.mutex.Unlock()
poolWithDynamicTag(invokers, c.tagRouterRule, rb)
c.ruleChanged = false
// create metadata in order to avoid lock in route()
meta := addrMetadata{application: c.application}
if c.tagRouterRule != nil {
meta.ruleForce = c.tagRouterRule.Force
meta.ruleEnabled = c.tagRouterRule.Enabled
meta.ruleValid = c.tagRouterRule.Valid
}
return rb, &meta
}
// fetchRuleIfNecessary fetches, parses rule and register listener for the further change
func (c *tagRouter) fetchRuleIfNecessary(invokers []protocol.Invoker) {
if len(invokers) == 0 {
return
}
url := invokers[0].GetURL()
providerApplication := url.GetParam(constant.RemoteApplicationKey, "")
if len(providerApplication) == 0 {
logger.Error("TagRouter must getConfig from or subscribe to a specific application, but the application " +
"in this TagRouter is not specified.")
return
}
dynamicConfiguration := config.GetEnvInstance().GetDynamicConfiguration()
if dynamicConfiguration == nil {
logger.Error("Get dynamicConfiguration fail, dynamicConfiguration is nil, init config center plugin please")
return
}
if providerApplication != c.application {
dynamicConfiguration.RemoveListener(c.application+constant.TagRouterRuleSuffix, c)
} else {
// if app name from URL is as same as the current app name, then it is safe to jump out
return
}
c.application = providerApplication
routerKey := providerApplication + constant.TagRouterRuleSuffix
dynamicConfiguration.AddListener(routerKey, c)
// get rule
rule, err := dynamicConfiguration.GetRule(routerKey, config_center.WithGroup(config_center.DEFAULT_GROUP))
if len(rule) == 0 || err != nil {
logger.Errorf("Get rule fail, config rule{%s}, error{%v}", rule, err)
return
}
if len(rule) > 0 {
c.Process(&config_center.ConfigChangeEvent{
Key: routerKey,
Value: rule,
ConfigType: remoting.EventTypeUpdate})
}
}
// ShouldPool returns false, to make sure address cache for tag router happens once and only once.
func (c *tagRouter) ShouldPool() bool {
c.mutex.RLock()
defer c.mutex.RUnlock()
return c.ruleChanged
}
// Name returns pool's name
func (c *tagRouter) Name() string {
return name
}
// poolWithDynamicTag pools addresses with the tags defined in dynamic tag rule, all keys have prefix "dynamic-"
func poolWithDynamicTag(invokers []protocol.Invoker, rule *RouterRule, pool router.AddrPool) {
if rule == nil {
return
}
tagNameToAddresses := rule.getTagNameToAddresses()
for tag, addrs := range tagNameToAddresses {
pool[dynamicPrefix+tag] = addrsToBitmap(addrs, invokers)
}
}
// poolWithStaticTag pools addresses with tags found from incoming URLs, all keys have prefix "static-"
func poolWithStaticTag(invokers []protocol.Invoker, pool router.AddrPool) {
for i, invoker := range invokers {
url := invoker.GetURL()
tag := url.GetParam(constant.Tagkey, "")
if len(tag) > 0 {
if _, ok := pool[staticPrefix+tag]; !ok {
pool[staticPrefix+tag] = roaring.NewBitmap()
}
pool[staticPrefix+tag].AddInt(i)
}
}
}
// shouldUseDynamicTag uses the snapshot data from the parsed rule to decide if dynamic tag rule should be used or not
func shouldUseDynamicTag(meta router.AddrMetadata) bool {
return meta.(*addrMetadata).ruleValid && meta.(*addrMetadata).ruleEnabled
}
// isTagRuleForce uses the snapshot data from the parsed rule to decide if dynamic tag rule is forced or not
func isTagRuleForce(meta router.AddrMetadata) bool {
return meta.(*addrMetadata).ruleForce
}
// isForceUseTag returns whether force use tag
func isForceUseTag(url *common.URL, invocation protocol.Invocation) bool {
if b, e := strconv.ParseBool(invocation.AttachmentsByKey(constant.ForceUseTag, url.GetParam(constant.ForceUseTag, "false"))); e == nil {
return b
}
return false
}
// addrsToBitmap finds indexes for the given IP addresses in the target URL list, if any '0.0.0.0' IP address is met,
// then returns back all indexes of the URLs list.
func addrsToBitmap(addrs []string, invokers []protocol.Invoker) *roaring.Bitmap {
ret := roaring.NewBitmap()
for _, addr := range addrs {
if isAnyHost(addr) {
ret.AddRange(0, uint64(len(invokers)))
return ret
}
index := findIndexWithIp(addr, invokers)
if index != -1 {
ret.AddInt(index)
}
}
return ret
}
// findIndexWithIp finds index for one particular IP
func findIndexWithIp(addr string, invokers []protocol.Invoker) int {
for i, invoker := range invokers {
if gxnet.MatchIP(addr, invoker.GetURL().Ip, invoker.GetURL().Port) {
return i
}
}
return -1
}
// isAnyHost checks if an IP is '0.0.0.0'
func isAnyHost(addr string) bool {
return strings.HasPrefix(addr, constant.ANYHOST_VALUE)
}
// findTag finds tag, first from invocation's attachment, then from URL
func findTag(invocation protocol.Invocation, consumerUrl *common.URL) string {
tag, ok := invocation.Attachments()[constant.Tagkey]
if !ok {
return consumerUrl.GetParam(constant.Tagkey, "")
} else if v, t := tag.(string); t {
return v
}
return ""
}