Skip to content

Commit

Permalink
Merge pull request #696 from apache/revert-665-featrue/tagRouter
Browse files Browse the repository at this point in the history
Ftr: dynamic tag router
  • Loading branch information
zouyx authored Aug 7, 2020
2 parents 0a98a20 + 603ffc1 commit 613cc6e
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 733 deletions.
3 changes: 0 additions & 3 deletions before_ut.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,5 @@ cp ${zkJar} cluster/router/chain/zookeeper-4unittest/contrib/fatjar
mkdir -p cluster/router/condition/zookeeper-4unittest/contrib/fatjar
cp ${zkJar} cluster/router/condition/zookeeper-4unittest/contrib/fatjar

mkdir -p cluster/router/tag/zookeeper-4unittest/contrib/fatjar
cp ${zkJar} cluster/router/tag/zookeeper-4unittest/contrib/fatjar

mkdir -p metadata/report/zookeeper/zookeeper-4unittest/contrib/fatjar
cp ${zkJar} metadata/report/zookeeper/zookeeper-4unittest/contrib/fatjar
2 changes: 1 addition & 1 deletion cluster/router/condition/app_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
)

import (
_ "github.com/apache/dubbo-go/config_center/zookeeper"
"github.com/stretchr/testify/assert"
)

Expand All @@ -33,7 +34,6 @@ import (
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/config_center"
_ "github.com/apache/dubbo-go/config_center/zookeeper"
"github.com/apache/dubbo-go/remoting"
"github.com/apache/dubbo-go/remoting/zookeeper"
)
Expand Down
2 changes: 1 addition & 1 deletion cluster/router/condition/listenable_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func newListenableRouter(url *common.URL, ruleKey string) (*AppRouter, error) {
return l, nil
}

// Process Process config change event, generate routers and set them to the listenableRouter instance
// Process Process config change event , generate routers and set them to the listenableRouter instance
func (l *listenableRouter) Process(event *config_center.ConfigChangeEvent) {
logger.Infof("Notification of condition rule, change type is:[%s] , raw rule is:[%v]", event.ConfigType, event.Value)
if remoting.EventTypeDel == event.ConfigType {
Expand Down
2 changes: 1 addition & 1 deletion cluster/router/tag/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type FileTagRouter struct {
force bool
}

// NewFileTagRouter Create file tag router instance with content (from config file)
// NewFileTagRouter Create file tag router instance with content ( from config file)
func NewFileTagRouter(content []byte) (*FileTagRouter, error) {
fileRouter := &FileTagRouter{}
rule, err := getRule(string(content))
Expand Down
71 changes: 0 additions & 71 deletions cluster/router/tag/router_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,9 @@ import (
"github.com/apache/dubbo-go/common/yaml"
)

/**
* %YAML1.2
* ---
* force: true
* runtime: false
* enabled: true
* priority: 1
* key: demo-provider
* tags:
* - name: tag1
* addresses: [ip1, ip2]
* - name: tag2
* addresses: [ip3, ip4]
* ...
*/
// RouterRule RouterRule config read from config file or config center
type RouterRule struct {
router.BaseRouterRule `yaml:",inline""`
Tags []Tag
addressToTagNames map[string][]string
tagNameToAddresses map[string][]string
}

func getRule(rawRule string) (*RouterRule, error) {
Expand All @@ -52,58 +34,5 @@ func getRule(rawRule string) (*RouterRule, error) {
return r, err
}
r.RawRule = rawRule
r.init()
return r, nil
}

func (t *RouterRule) init() {
t.addressToTagNames = make(map[string][]string, 8)
t.tagNameToAddresses = make(map[string][]string, 8)
for _, tag := range t.Tags {
for _, address := range tag.Addresses {
t.addressToTagNames[address] = append(t.addressToTagNames[address], tag.Name)
}
t.tagNameToAddresses[tag.Name] = tag.Addresses
}
}

func (t *RouterRule) getAddresses() []string {
var result = make([]string, 0, 8*len(t.Tags))
for _, tag := range t.Tags {
result = append(result, tag.Addresses...)
}
return result
}

func (t *RouterRule) getTagNames() []string {
var result = make([]string, 0, len(t.Tags))
for _, tag := range t.Tags {
result = append(result, tag.Name)
}
return result
}

func (t *RouterRule) hasTag(tag string) bool {
for _, t := range t.Tags {
if tag == t.Name {
return true
}
}
return false
}

func (t *RouterRule) getAddressToTagNames() map[string][]string {
return t.addressToTagNames
}

func (t *RouterRule) getTagNameToAddresses() map[string][]string {
return t.tagNameToAddresses
}

func (t *RouterRule) getTags() []Tag {
return t.Tags
}

func (t *RouterRule) setTags(tags []Tag) {
t.Tags = tags
}
55 changes: 9 additions & 46 deletions cluster/router/tag/router_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,56 +22,19 @@ import (
)

import (
"github.com/stretchr/testify/suite"
"github.com/stretchr/testify/assert"
)

type RuleTestSuite struct {
suite.Suite
rule *RouterRule
}

func (suite *RuleTestSuite) SetupTest() {
var err error
func TestGetRule(t *testing.T) {
yml := `
scope: application
runtime: true
force: true
runtime: false
enabled: true
priority: 1
key: demo-provider
tags:
- name: tag1
addresses: [ip1, ip2]
- name: tag2
addresses: [ip3, ip4]
`
suite.rule, err = getRule(yml)
suite.Nil(err)
}

func (suite *RuleTestSuite) TestGetRule() {
var err error
suite.Equal(true, suite.rule.Force)
suite.Equal(false, suite.rule.Runtime)
suite.Equal("application", suite.rule.Scope)
suite.Equal(1, suite.rule.Priority)
suite.Equal("demo-provider", suite.rule.Key)
suite.Nil(err)
}

func (suite *RuleTestSuite) TestGetTagNames() {
suite.Equal([]string{"tag1", "tag2"}, suite.rule.getTagNames())
}

func (suite *RuleTestSuite) TestGetAddresses() {
suite.Equal([]string{"ip1", "ip2", "ip3", "ip4"}, suite.rule.getAddresses())
}

func (suite *RuleTestSuite) TestHasTag() {
suite.Equal(true, suite.rule.hasTag("tag1"))
suite.Equal(false, suite.rule.hasTag("tag404"))
}

func TestRuleTestSuite(t *testing.T) {
suite.Run(t, new(RuleTestSuite))
rule, e := getRule(yml)
assert.Nil(t, e)
assert.NotNil(t, rule)
assert.Equal(t, true, rule.Force)
assert.Equal(t, true, rule.Runtime)
assert.Equal(t, "application", rule.Scope)
}
39 changes: 0 additions & 39 deletions cluster/router/tag/tag.go

This file was deleted.

Loading

0 comments on commit 613cc6e

Please sign in to comment.