Skip to content
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 8 commits into from
Aug 6, 2020
Merged

Conversation

watermelo
Copy link
Member

@watermelo watermelo commented Jul 19, 2020

What this PR does:
#593

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:


@watermelo watermelo changed the title Ftr: dynamic tag router [WIP]Ftr: dynamic tag router Jul 19, 2020
@watermelo watermelo marked this pull request as draft July 19, 2020 14:48
@watermelo watermelo changed the base branch from master to develop July 26, 2020 17:12
@zouyx zouyx added this to the 1.5.1 milestone Jul 28, 2020
@zouyx zouyx linked an issue Jul 28, 2020 that may be closed by this pull request
@watermelo watermelo marked this pull request as ready for review July 29, 2020 17:30
@watermelo watermelo changed the title [WIP]Ftr: dynamic tag router Ftr: dynamic tag router Jul 29, 2020
return r, nil
}

func (t *RouterRule) init() {
t.addressToTagNames = make(map[string][]string)
t.tagNameToAddresses = make(map[string][]string)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t.addressToTagNames = make(map[string][]string, 8)
t.tagNameToAddresses = make(map[string][]string, 8)

func (t *RouterRule) getAddresses() []string {
// TODO get all tag addresses
return nil
var result []string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var result = make([]string, 0, 8 * len(t.Tags))

}

func (t *RouterRule) getTagNames() []string {
// TODO get all tag names
return nil
var result []string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var result = make([]string, 0, len(t.Tags))

// TODO address parse
if address == (host + port) {
// TODO ip match
if address == host+":"+port {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete "host+":"+port", using net.JoinHostPort(host, port) instead.

cluster/router/tag/router_rule.go Show resolved Hide resolved
cluster/router/tag/tag.go Show resolved Hide resolved
cluster/router/tag/tag_router.go Outdated Show resolved Hide resolved
cluster/router/tag/tag_router.go Outdated Show resolved Hide resolved
cluster/router/tag/tag_router.go Outdated Show resolved Hide resolved
logger.Errorf("Get rule fail, config rule{%s}, error{%v}", rule, err)
return
}
if rule != "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

len>0

cluster/router/tag/tag_router.go Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

Codecov Report

Merging #665 into develop will decrease coverage by 0.34%.
The diff coverage is 54.91%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #665      +/-   ##
===========================================
- Coverage    63.72%   63.37%   -0.35%     
===========================================
  Files          237      239       +2     
  Lines        12295    12598     +303     
===========================================
+ Hits          7835     7984     +149     
- Misses        3706     3829     +123     
- Partials       754      785      +31     
Impacted Files Coverage Δ
cluster/router/condition/listenable_router.go 54.83% <ø> (ø)
cluster/router/tag/file.go 76.92% <ø> (ø)
cluster/router/tag/tag.go 0.00% <0.00%> (ø)
cluster/router/tag/tag_router.go 54.62% <52.19%> (-15.97%) ⬇️
cluster/router/tag/router_rule.go 84.21% <87.09%> (+12.78%) ⬆️
filter/filter_impl/metrics_filter.go 86.36% <0.00%> (-13.64%) ⬇️
registry/kubernetes/listener.go 65.95% <0.00%> (-6.39%) ⬇️
filter/filter_impl/hystrix_filter.go 68.64% <0.00%> (-3.39%) ⬇️
protocol/dubbo/client.go 67.87% <0.00%> (-1.22%) ⬇️
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 272ddd5...a31a4e2. Read the comment docs.

Copy link
Member

@zouyx zouyx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the comment has resolved you can click resolved button in the comment.

var (
result []protocol.Invoker
addresses []string
)
Copy link
Member

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 😂

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree

}

pattern = hostAndPort[0]
// TODO 常量化
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

has some easy todo

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not use Chinese comment in our codes.

@@ -19,32 +19,59 @@ package tag

import (
"context"
"fmt"
"github.com/stretchr/testify/suite"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pkg import problem

@@ -19,32 +19,59 @@ package tag

import (
"context"
"fmt"
"github.com/stretchr/testify/suite"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pkg import problem

var (
result []protocol.Invoker
addresses []string
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree

for _, invoker := range invokers {
for _, filter := range filters {
if !filter(invoker) {
continue OUTER
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

break OUTER?

}

pattern = hostAndPort[0]
// TODO 常量化
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not use Chinese comment in our codes.


ipAddress := strings.Split(host, splitCharacter)
for i := 0; i < len(mask); i++ {
if "*" == mask[i] || mask[i] == ipAddress[i] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

u should add comment for every if/else branch to make it easy to understand.


func getPatternHostAndPort(pattern string, isIpv4 bool) []string {
result := make([]string, 2)
if strings.HasPrefix(pattern, "[") && strings.Contains(pattern, "]:") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add some comments for every if/else branch, pls.

return r, nil
}

func (t *RouterRule) init() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why create a new func named init? If you create the func ,pls add the comment for it.

}

func (t *RouterRule) getAddresses() []string {
var result = make([]string, 0, 8*len(t.Tags))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls add func comment for RouterRule

Comment on lines +60 to +61
t.addressToTagNames = make(map[string][]string, 8)
t.tagNameToAddresses = make(map[string][]string, 8)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
t.addressToTagNames = make(map[string][]string, 8)
t.tagNameToAddresses = make(map[string][]string, 8)
l:=len(t.Tags)
t.addressToTagNames = make(map[string][]string, l*2)
t.tagNameToAddresses = make(map[string][]string, l)

}

func (t *RouterRule) getAddresses() []string {
var result = make([]string, 0, 8*len(t.Tags))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var result = make([]string, 0, 8*len(t.Tags))
var result = make([]string, 0, 2*len(t.Tags))

i think * 2 enough

Comment on lines +87 to +92
for _, t := range t.Tags {
if tag == t.Name {
return true
}
}
return false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for _, t := range t.Tags {
if tag == t.Name {
return true
}
}
return false
return len(tagNameToAddresses[tag])>0

Why dont you use tagNameToAddresses map[string][]string?

Comment on lines +72 to +73
routerRule := *c.tagRouterRule
return routerRule
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
routerRule := *c.tagRouterRule
return routerRule
return *c.tagRouterRule

enough? and i think no need to extract a new function for single scene

Comment on lines +138 to +141
if invoker.GetUrl().GetParam(constant.Tagkey, "") == "" {
return true
}
return false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if invoker.GetUrl().GetParam(constant.Tagkey, "") == "" {
return true
}
return false
return invoker.GetUrl().GetParam(constant.Tagkey, "") == ""

Comment on lines +117 to +120
if invoker.GetUrl().GetParam(constant.Tagkey, "") == tag {
return true
}
return false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if invoker.GetUrl().GetParam(constant.Tagkey, "") == tag {
return true
}
return false
return invoker.GetUrl().GetParam(constant.Tagkey, "") == tag

Comment on lines +132 to +135
if len(addresses) == 0 || !checkAddressMatch(tagRouterRuleCopy.getAddresses(), url.Ip, url.Port) {
return true
}
return false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if len(addresses) == 0 || !checkAddressMatch(tagRouterRuleCopy.getAddresses(), url.Ip, url.Port) {
return true
}
return false
return len(addresses) == 0 || !checkAddressMatch(tagRouterRuleCopy.getAddresses(), url.Ip, url.Port)

if remoting.EventTypeDel == event.ConfigType {
c.tagRouterRule = nil
return
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete else

Comment on lines +300 to +303
if subnet != nil && subnet.Contains(net.ParseIP(host)) {
return true
}
return false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if subnet != nil && subnet.Contains(net.ParseIP(host)) {
return true
}
return false
return subnet != nil && subnet.Contains(net.ParseIP(host))

)

import (
"github.com/dubbogo/go-zookeeper/zk"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

split package

@AlexStocks AlexStocks merged commit 5e377f4 into apache:develop Aug 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Align with Dubbo: Route based on tag
6 participants