-
Notifications
You must be signed in to change notification settings - Fork 934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ftr: dynamic tag router #665
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2e01ed5
Ftr: add dynamic tag router
watermelo 71095a3
Add: add unit tests for tag router
watermelo 4ef21cb
Add: add ip address match function
watermelo 891ec6c
Add: add listener for tag router
watermelo 0d56ab0
Merge branch 'develop' into featrue/tagRouter
watermelo 8580ad0
Mod: update tag router unit test
watermelo 70c01a5
Add: add unit test for dynamic tag
watermelo a31a4e2
Add: add zk jar for tag test
watermelo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Ftr: add dynamic tag router
- Loading branch information
commit 2e01ed5bee64bac3a09d859278768be3dfa386cc
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* 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 | ||
|
||
type tag struct { | ||
name string | ||
addresses []string | ||
} | ||
|
||
func (t *tag) getName() string { | ||
return t.name | ||
} | ||
|
||
func (t *tag) setName(name string) { | ||
t.name = name | ||
} | ||
|
||
func (t *tag) getAddresses() []string { | ||
return t.addresses | ||
} | ||
|
||
func (t *tag) setAddresses(addresses []string) { | ||
t.addresses = addresses | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
package tag | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
) | ||
|
||
|
@@ -28,13 +29,17 @@ import ( | |
import ( | ||
"github.com/apache/dubbo-go/common" | ||
"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" | ||
) | ||
|
||
type tagRouter struct { | ||
url *common.URL | ||
enabled bool | ||
priority int64 | ||
url *common.URL | ||
tagRouterRule *RouterRule | ||
enabled bool | ||
priority int64 | ||
} | ||
|
||
func NewTagRouter(url *common.URL) (*tagRouter, error) { | ||
|
@@ -59,7 +64,81 @@ func (c *tagRouter) Route(invokers []protocol.Invoker, url *common.URL, invocati | |
if len(invokers) == 0 { | ||
return invokers | ||
} | ||
return filterUsingStaticTag(invokers, url, invocation) | ||
// since the rule can be changed by config center, we should copy one to use. | ||
tagRouterRuleCopy := c.tagRouterRule | ||
if tagRouterRuleCopy == nil || !tagRouterRuleCopy.Valid || !tagRouterRuleCopy.Enabled { | ||
return filterUsingStaticTag(invokers, url, invocation) | ||
} | ||
tag, ok := invocation.Attachments()[constant.Tagkey] | ||
if !ok { | ||
tag = url.GetParam(constant.Tagkey, "") | ||
} | ||
var ( | ||
result []protocol.Invoker | ||
addresses []string | ||
) | ||
if tag != "" { | ||
zouyx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
addresses, _ = tagRouterRuleCopy.getTagNameToAddresses()[tag] | ||
// filter by dynamic tag group first | ||
if len(addresses) > 0 { | ||
// TODO filter invokers | ||
result = nil | ||
if len(result) > 0 || tagRouterRuleCopy.Force { | ||
return result | ||
} | ||
} else { | ||
// 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 | ||
// TODO filter invokers | ||
return result | ||
} | ||
// 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 len(result) > 0 || isForceUseTag(url, invocation) { | ||
return result | ||
} else { | ||
// FAILOVER: return all Providers without any tags. | ||
// TODO filter invokers | ||
return result | ||
} | ||
} else { | ||
// return all addresses in dynamic tag group. | ||
addresses = tagRouterRuleCopy.getAddresses() | ||
if len(addresses) > 0 { | ||
// TODO filter invokers | ||
// 1. all addresses are in dynamic tag group, return empty list. | ||
if len(result) == 0 { | ||
return result | ||
} | ||
// 2. if there are some addresses that are not in any dynamic tag group, continue to filter using the | ||
// static tag group. | ||
} | ||
// TODO filter invokers | ||
return result | ||
} | ||
} | ||
|
||
func (c *tagRouter) Process(event *config_center.ConfigChangeEvent) { | ||
logger.Infof("Notification of dynamic tag rule, change type is:[%s] , raw rule is:[%v]", event.ConfigType, event.Value) | ||
if remoting.EventTypeDel == event.ConfigType { | ||
c.tagRouterRule = nil | ||
return | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. delete else |
||
content, ok := event.Value.(string) | ||
if !ok { | ||
msg := fmt.Sprintf("Convert event content fail,raw content:[%s] ", event.Value) | ||
logger.Error(msg) | ||
zouyx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return | ||
} | ||
|
||
routerRule, err := getRule(content) | ||
if err != nil { | ||
logger.Errorf("Parse dynamic tag router rule fail,error:[%s] ", err) | ||
return | ||
} | ||
c.tagRouterRule = routerRule | ||
return | ||
} | ||
} | ||
|
||
func (c *tagRouter) URL() common.URL { | ||
|
@@ -92,3 +171,21 @@ func isForceUseTag(url *common.URL, invocation protocol.Invocation) bool { | |
} | ||
return false | ||
} | ||
|
||
func addressMatches(url *common.URL, addresses []string) bool { | ||
return len(addresses) > 0 && checkAddressMatch(addresses, url.Ip, url.Port) | ||
} | ||
|
||
func addressNotMatches(url *common.URL, addresses []string) bool { | ||
return len(addresses) == 0 || !checkAddressMatch(addresses, url.Ip, url.Port) | ||
} | ||
|
||
func checkAddressMatch(addresses []string, host, port string) bool { | ||
for _, address := range addresses { | ||
// TODO address parse | ||
if address == (host + port) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The definition can be moved to the top.
And can you break this pile of code into several parts,it is hard for me 😂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree