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: condition router #294

Merged
merged 119 commits into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
119 commits
Select commit Hold shift + click to select a range
7b07c34
use router in dubbo-go
aliiohs Aug 2, 2019
dabfded
add GetBackupUrls method
Aug 6, 2019
26ca61e
use router in dubbo-go
aliiohs Aug 11, 2019
3ef29b7
use router in dubbo-go
aliiohs Aug 17, 2019
1faef98
use router in dubbo-go
aliiohs Aug 18, 2019
8b45629
change Version file
aliiohs Sep 1, 2019
50e42d0
add init router from yaml file
aliiohs Sep 6, 2019
dea76be
fix cycle import bug
aliiohs Sep 6, 2019
84c39a3
fix
aliiohs Sep 6, 2019
313171b
fix
aliiohs Sep 6, 2019
10c873c
fix
aliiohs Sep 7, 2019
bf409d3
fix
aliiohs Sep 7, 2019
0be9cf0
fix
aliiohs Sep 7, 2019
16d0b99
debug log
aliiohs Sep 9, 2019
dcf2993
debug log
aliiohs Sep 10, 2019
20ac52c
fix bug
aliiohs Sep 10, 2019
44ead07
fix bug
aliiohs Sep 10, 2019
92b1787
debug log
aliiohs Sep 10, 2019
7034520
change router factory param
aliiohs Sep 10, 2019
738321e
init ConsumerUrl when build BaseDirectory
aliiohs Sep 10, 2019
3cafe33
init router only once
aliiohs Sep 10, 2019
a876f85
Merge branch 'aliiohs-UseRouter' into feature/addRouter
zouyx Dec 30, 2019
c84c1e1
add router
zouyx Dec 30, 2019
52d3554
fix problems
zouyx Dec 30, 2019
eca2285
fix problems
zouyx Dec 30, 2019
58bb83a
fix problems
zouyx Dec 30, 2019
1c9c03f
fix problems
zouyx Dec 30, 2019
cbb4759
fix problems
zouyx Dec 31, 2019
e155e58
fix review problems
zouyx Dec 31, 2019
5909a32
fix review problems
zouyx Dec 31, 2019
085961b
fix review problems
zouyx Dec 31, 2019
ee8ff8d
fix review problems
zouyx Jan 6, 2020
18cac91
refactor router code
zouyx Jan 6, 2020
b016f01
refactor router code
zouyx Jan 7, 2020
6436e24
add test case
zouyx Jan 8, 2020
163e9a7
refactor router code
zouyx Jan 9, 2020
5ee0f0d
Merge branch 'develop' into feature/addRouter
Jan 9, 2020
2956fc0
remove router chain
zouyx Jan 9, 2020
9b2700a
rename package
zouyx Jan 9, 2020
af32a05
rename package
zouyx Jan 9, 2020
6d5d849
add to routers method
zouyx Jan 9, 2020
a4b2d66
add event update
zouyx Jan 10, 2020
5a69d1c
Merge branch 'develop' into feature/addRouter
zouyx Jan 11, 2020
caa0df0
add router
zouyx Jan 11, 2020
9b892e6
split package
zouyx Jan 11, 2020
6ccf9f4
fix test case
zouyx Jan 11, 2020
d0035fd
add error log
zouyx Jan 12, 2020
a2d433b
Merge branch 'develop' into feature/addRouter
zouyx Jan 12, 2020
5573269
fix review problem
zouyx Jan 12, 2020
2b8699f
fix review comment
zouyx Jan 18, 2020
dd95421
fix review comment
zouyx Jan 18, 2020
68ac99e
fix review comment
zouyx Jan 18, 2020
c959be6
Merge branch 'develop' into feature/addRouter
zouyx Jan 20, 2020
bc732b5
add router event listener
zouyx Jan 20, 2020
3e71f69
add router chain
zouyx Jan 21, 2020
ce49840
add router chain
zouyx Jan 21, 2020
3a2b75e
add router chain
zouyx Jan 21, 2020
96c0902
Merge branch 'develop' into feature/addRouter
zouyx Jan 21, 2020
4502c56
add test case
zouyx Jan 21, 2020
625d8b0
fix test
zouyx Jan 22, 2020
552d642
Merge branch 'develop' into feature/addRouter
zouyx Jan 22, 2020
7068404
use before_ut.sh in travis.yml
zouyx Jan 22, 2020
bb91f0f
update test case
zouyx Jan 22, 2020
bd45b58
Update router
zouyx Jan 22, 2020
2b3ac70
Merge branch 'develop' into feature/addRouter
zouyx Jan 24, 2020
1971704
Merge branch 'develop' into feature/addRouter
zouyx Jan 24, 2020
5b149da
format code
zouyx Jan 24, 2020
948c6c5
format package
zouyx Jan 24, 2020
eff1a5a
format package
zouyx Jan 24, 2020
cd777ee
add build router chain
zouyx Jan 24, 2020
4e61cdb
Merge branch 'develop' into feature/addRouter
zouyx Jan 26, 2020
2be8f85
fix problems
zouyx Jan 26, 2020
45256e9
fix problems
zouyx Jan 26, 2020
9694d44
fix problems
zouyx Jan 26, 2020
cc51f5f
fix file config init invliad
zouyx Jan 26, 2020
207c5a3
delete useless block
zouyx Jan 26, 2020
4d73001
fix test case
zouyx Jan 26, 2020
1c9a716
fix review comment
zouyx Jan 27, 2020
587b9a0
fix review comment
zouyx Jan 28, 2020
dd802c0
fix test case
zouyx Jan 28, 2020
1979db2
fix test case
zouyx Jan 28, 2020
242e954
fix review problems
zouyx Jan 28, 2020
1ce2cd2
fix review comment
zouyx Jan 31, 2020
e9e57e5
fix test case
zouyx Jan 31, 2020
6054f9f
add comment
zouyx Feb 1, 2020
cb4ae79
fix review comment
zouyx Feb 4, 2020
0bbcf48
add comment
zouyx Feb 4, 2020
c08374e
Merge branch 'develop' into feature/addRouter
zouyx Feb 8, 2020
92679f9
Merge remote-tracking branch 'origin/feature/addRouter' into feature/…
zouyx Feb 8, 2020
27cfa65
Merge branch 'develop' into feature/addRouter
zouyx Feb 8, 2020
c4ef90e
delete useless file
zouyx Feb 8, 2020
70f4880
Merge branch 'develop' into feature/addRouter
zouyx Feb 8, 2020
009f1ca
fix test case
zouyx Feb 8, 2020
326e1fb
Merge branch 'develop' into feature/addRouter
zouyx Feb 8, 2020
4f2731a
fix test case and add some comment
zouyx Feb 8, 2020
7b13315
fix review comment
zouyx Feb 9, 2020
7fab9e0
fix review comment
zouyx Feb 10, 2020
53277c5
Merge branch 'develop' into feature/addRouter
zouyx Feb 13, 2020
a5126e1
fix review comment
zouyx Feb 13, 2020
fa4b5c0
delete useless file
zouyx Feb 13, 2020
2f33bf0
fix review comment
zouyx Feb 13, 2020
dc7e5d0
fix review comment
zouyx Feb 13, 2020
a82ee38
fix review comment
zouyx Feb 15, 2020
77c9c9e
fix test case
zouyx Feb 15, 2020
f419cd1
add router selection
zouyx Feb 16, 2020
d6c62c8
add comment and testcase
zouyx Feb 17, 2020
09d7777
change init router to config.Load()
zouyx Feb 22, 2020
305aa06
fix router init bug
zouyx Feb 22, 2020
02498b2
add UT for app_router
zouyx Feb 24, 2020
f326095
Update before_ut.bat and before_ut.sh
zouyx Feb 25, 2020
35223c2
add test case
zouyx Feb 25, 2020
21cf354
add test case
zouyx Feb 25, 2020
e34203a
add test case
zouyx Feb 26, 2020
124a108
add test case
zouyx Feb 26, 2020
843d312
Merge branch 'develop' into feature/addRouter
zouyx Feb 26, 2020
fc4ed3d
add test case
zouyx Feb 26, 2020
8df93da
add UT for app_router
zouyx Feb 26, 2020
86e35ce
add test case
zouyx Feb 26, 2020
0c7af31
split package
zouyx Feb 27, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions cluster/router/condition/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,27 @@ func init() {
type ConditionRouterFactory struct{}

func newConditionRouterFactory() router.RouterFactory {
return ConditionRouterFactory{}
return &ConditionRouterFactory{}
}

// NewRouter Create ConditionRouterFactory by URL
func (c ConditionRouterFactory) NewRouter(url *common.URL) (router.Router, error) {
func (c *ConditionRouterFactory) NewRouter(url *common.URL) (router.Router, error) {
return NewConditionRouter(url)
}

// NewRouter Create FileRouterFactory by Content
func (c *ConditionRouterFactory) NewFileRouter(content []byte) (router.Router, error) {
return NewFileConditionRouter(content)
}

// AppRouterFactory Application router factory
type AppRouterFactory struct{}

func newAppRouterFactory() router.RouterFactory {
return AppRouterFactory{}
return &AppRouterFactory{}
}

// NewRouter Create AppRouterFactory by URL
func (c AppRouterFactory) NewRouter(url *common.URL) (router.Router, error) {
func (c *AppRouterFactory) NewRouter(url *common.URL) (router.Router, error) {
return NewAppRouter(url)
}
101 changes: 101 additions & 0 deletions cluster/router/condition/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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 condition

import (
"encoding/base64"
"net/url"
"strconv"
"strings"
)

import (
perrors "github.com/pkg/errors"
)

import (
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/constant"
)

type FileConditionRouter struct {
zouyx marked this conversation as resolved.
Show resolved Hide resolved
listenableRouter
}

func NewFileConditionRouter(content []byte) (*FileConditionRouter, error) {
zouyx marked this conversation as resolved.
Show resolved Hide resolved
fileRouter := &FileConditionRouter{}
rule, err := Parse(string(content))
if err != nil {
return nil, perrors.Errorf("yaml.Unmarshal() failed , error:%v", perrors.WithStack(err))
}

if !rule.Valid {
return nil, perrors.Errorf("rule content is not verify for condition router , error:%v", perrors.WithStack(err))
}

fileRouter.generateConditions(rule)

return fileRouter, nil
}

func (f *FileConditionRouter) URL() common.URL {
zouyx marked this conversation as resolved.
Show resolved Hide resolved
routerRule := f.routerRule
rule := parseCondition(routerRule.Conditions)
return *common.NewURLWithOptions(
common.WithProtocol(constant.ROUTE_PROTOCOL),
common.WithIp(constant.ANYHOST_VALUE),
common.WithParams(url.Values{}),
common.WithParamsValue(constant.RouterForce, strconv.FormatBool(routerRule.Force)),
common.WithParamsValue(constant.RouterPriority, strconv.Itoa(routerRule.Priority)),
common.WithParamsValue(constant.RULE_KEY, base64.URLEncoding.EncodeToString([]byte(rule))),
common.WithParamsValue(constant.ROUTER_KEY, "condition"),
common.WithParamsValue(constant.CATEGORY_KEY, constant.ROUTERS_CATEGORY))
}

func parseCondition(conditions []string) string {
var (
when string
then string
)
for _, condition := range conditions {
condition = strings.Trim(condition, " ")
if strings.Contains(condition, "=>") {
array := strings.SplitN(condition, "=>", 2)
consumer := strings.Trim(array[0], " ")
provider := strings.Trim(array[1], " ")
if len(consumer) != 0 {
if len(when) != 0 {
when = strings.Join([]string{when, consumer}, " & ")
} else {
when = consumer
}
}
if len(provider) != 0 {
if len(then) != 0 {
then = strings.Join([]string{then, provider}, " & ")
} else {
then = provider
}
}

}

}

return strings.Join([]string{when, then}, " => ")
}
47 changes: 47 additions & 0 deletions cluster/router/condition/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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 condition

import (
"testing"
)

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

func TestLoadYmlConfig(t *testing.T) {
router, e := NewFileConditionRouter([]byte(`priority: 1
force: true
conditions :
- "a => b"
- "c => d"`))
assert.Nil(t, e)
assert.NotNil(t, router)
assert.Equal(t, router.routerRule.Priority, 1)
assert.Equal(t, router.routerRule.Force, true)
assert.Equal(t, len(router.routerRule.Conditions), 2)
}

func TestParseCondition(t *testing.T) {
s := make([]string, 2)
s = append(s, "a => b")
s = append(s, "c => d")
condition := parseCondition(s)
assert.Equal(t, "a & c => b & d", condition)
}
5 changes: 5 additions & 0 deletions cluster/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ type RouterFactory interface {
NewRouter(*common.URL) (Router, error)
}

zouyx marked this conversation as resolved.
Show resolved Hide resolved
type FIleRouterFactory interface {
// NewFileRouters Create file router with config file
NewFileRouter([]byte) (Router, error)
}

// Router
type Router interface {
// Route Determine the target invokers list.
Expand Down
25 changes: 24 additions & 1 deletion common/extension/router_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@

package extension

import (
"sync"
)

import (
"github.com/apache/dubbo-go/cluster/router"
)

var (
routers = make(map[string]func() router.RouterFactory)
routers = make(map[string]func() router.RouterFactory)
fileRouterFactoryOnce sync.Once
fileRouterFactories = make(map[string]router.FIleRouterFactory)
)

// SetRouterFactory Set create router factory function by name
Expand All @@ -42,3 +48,20 @@ func GetRouterFactory(name string) router.RouterFactory {
func GetRouterFactories() map[string]func() router.RouterFactory {
return routers
}

// GetFileRouterFactories Get all create file router factory instance
func GetFileRouterFactories() map[string]router.FIleRouterFactory {
l := len(routers)
if l == 0 {
return nil
}
fileRouterFactoryOnce.Do(func() {
for k := range routers {
factory := GetRouterFactory(k)
if fr, ok := factory.(router.FIleRouterFactory); ok {
fileRouterFactories[k] = fr
}
}
})
return fileRouterFactories
}
18 changes: 5 additions & 13 deletions config/base_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (

import (
perrors "github.com/pkg/errors"
"gopkg.in/yaml.v2"
)

import (
Expand Down Expand Up @@ -364,22 +363,15 @@ func initializeStruct(t reflect.Type, v reflect.Value) {

}

func loadYmlConfig(confRouterFile string, i interface{}) error {
// loadYmlConfig Load yml config byte from file
func loadYmlConfig(confRouterFile string) ([]byte, error) {
if len(confRouterFile) == 0 {
return perrors.Errorf("application configure(provider) file name is nil")
return nil, perrors.Errorf("application configure(provider) file name is nil")
}

if path.Ext(confRouterFile) != ".yml" {
return perrors.Errorf("application configure file name{%v} suffix must be .yml", confRouterFile)
return nil, perrors.Errorf("application configure file name{%v} suffix must be .yml", confRouterFile)
}

confFileStream, err := ioutil.ReadFile(confRouterFile)
if err != nil {
return perrors.Errorf("ioutil.ReadFile(file:%s) = error:%v", confRouterFile, perrors.WithStack(err))
}
err = yaml.Unmarshal(confFileStream, i)
if err != nil {
return perrors.Errorf("yaml.Unmarshal() = error:%v", perrors.WithStack(err))
}
return nil
return ioutil.ReadFile(confRouterFile)
}
91 changes: 14 additions & 77 deletions config/condition_router_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,93 +18,30 @@
package config

import (
"encoding/base64"
"net/url"
"strconv"
"strings"
"sync"
perrors "github.com/pkg/errors"
)
zouyx marked this conversation as resolved.
Show resolved Hide resolved

import (
"github.com/apache/dubbo-go/cluster/directory"
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/common/logger"
)

var (
mutex sync.Mutex
)

// ConditionRouterConfig Condition router config map to router config file
type ConditionRouterConfig struct {
Priority int `yaml:"priority"`
Force bool `yaml:"force" default:"false"`
Conditions []string `yaml:"conditions"`
}

// Prefix Return router config prefix
func (*ConditionRouterConfig) Prefix() string {
return constant.RouterConfigPrefix
}

//RouterInit Load config file to init router config
func RouterInit(confRouterFile string) error {
routerConfig = &ConditionRouterConfig{}
e := loadYmlConfig(confRouterFile, routerConfig)

if e != nil {
return e
fileRouterFactories := extension.GetFileRouterFactories()
bytes, err := loadYmlConfig(confRouterFile)
if err != nil {
return perrors.Errorf("ioutil.ReadFile(file:%s) = error:%v", confRouterFile, perrors.WithStack(err))
}

logger.Debugf("router config{%#v}\n", routerConfig)
directory.AddRouterURLSet(initRouterUrl(routerConfig))
return nil
}

func initRouterUrl(routerConfig *ConditionRouterConfig) *common.URL {
rule := parseCondition(routerConfig.Conditions)

return common.NewURLWithOptions(
common.WithProtocol(constant.ROUTE_PROTOCOL),
common.WithIp(constant.ANYHOST_VALUE),
common.WithParams(url.Values{}),
common.WithParamsValue("force", strconv.FormatBool(routerConfig.Force)),
common.WithParamsValue("priority", strconv.Itoa(routerConfig.Priority)),
common.WithParamsValue(constant.RULE_KEY, base64.URLEncoding.EncodeToString([]byte(rule))),
common.WithParamsValue("router", "condition"),
common.WithParamsValue(constant.CATEGORY_KEY, constant.ROUTERS_CATEGORY))
}

func parseCondition(conditions []string) string {
var (
when string
then string
)
for _, condition := range conditions {
condition = strings.Trim(condition, " ")
if strings.Contains(condition, "=>") {
array := strings.SplitN(condition, "=>", 2)
consumer := strings.Trim(array[0], " ")
provider := strings.Trim(array[1], " ")
if len(consumer) != 0 {
if len(when) != 0 {
when = strings.Join([]string{when, consumer}, " & ")
} else {
when = consumer
}
}
if len(provider) != 0 {
if len(then) != 0 {
then = strings.Join([]string{then, provider}, " & ")
} else {
then = provider
}
}

for k, factory := range fileRouterFactories {
r, e := factory.NewFileRouter(bytes)
if e == nil {
url := r.URL()
directory.AddRouterURLSet(&url)
break
}

logger.Warnf("router config type %s create fail \n", k)
}

return strings.Join([]string{when, then}, " => ")
return nil
}
Loading