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 105 commits
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
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ install: true

script:
- go fmt ./... && [[ -z `git status -s` ]]
- mkdir -p remoting/zookeeper/zookeeper-4unittest/contrib/fatjar config_center/zookeeper/zookeeper-4unittest/contrib/fatjar registry/zookeeper/zookeeper-4unittest/contrib/fatjar
- wget -P "remoting/zookeeper/zookeeper-4unittest/contrib/fatjar" https://github.com/dubbogo/resources/raw/master/zookeeper-4unitest/contrib/fatjar/zookeeper-3.4.9-fatjar.jar
- cp remoting/zookeeper/zookeeper-4unittest/contrib/fatjar/zookeeper-3.4.9-fatjar.jar config_center/zookeeper/zookeeper-4unittest/contrib/fatjar/
- cp remoting/zookeeper/zookeeper-4unittest/contrib/fatjar/zookeeper-3.4.9-fatjar.jar registry/zookeeper/zookeeper-4unittest/contrib/fatjar/
- chmod u+x before_ut.sh && ./before_ut.sh
- go mod vendor && go test ./... -coverprofile=coverage.txt -covermode=atomic

after_success:
Expand Down
5 changes: 3 additions & 2 deletions before_ut.bat
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
:: limitations under the License.

set zkJar=zookeeper-3.4.9-fatjar.jar
md remoting\zookeeper\zookeeper-4unittest\contrib\fatjar config_center\zookeeper\zookeeper-4unittest\contrib\fatjar registry\zookeeper\zookeeper-4unittest\contrib\fatjar
md remoting\zookeeper\zookeeper-4unittest\contrib\fatjar config_center\zookeeper\zookeeper-4unittest\contrib\fatjar registry\zookeeper\zookeeper-4unittest\contrib\fatjar cluster\router\chain\zookeeper-4unittest\contrib\fatjar
curl -L https://github.com/dubbogo/resources/raw/master/zookeeper-4unitest/contrib/fatjar/%zkJar% -o remoting/zookeeper/zookeeper-4unittest/contrib/fatjar/%zkJar%
xcopy /f "remoting/zookeeper/zookeeper-4unittest/contrib/fatjar/%zkJar%" "config_center/zookeeper/zookeeper-4unittest/contrib/fatjar/"
xcopy /f "remoting/zookeeper/zookeeper-4unittest/contrib/fatjar/%zkJar%" "registry/zookeeper/zookeeper-4unittest/contrib/fatjar/"
xcopy /f "remoting/zookeeper/zookeeper-4unittest/contrib/fatjar/%zkJar%" "registry/zookeeper/zookeeper-4unittest/contrib/fatjar/"
xcopy /f "remoting/zookeeper/zookeeper-4unittest/contrib/fatjar/%zkJar%" "cluster/router/chain/zookeeper-4unittest/contrib/fatjar/"
5 changes: 3 additions & 2 deletions before_ut.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
# limitations under the License.


mkdir -p remoting/zookeeper/zookeeper-4unittest/contrib/fatjar config_center/zookeeper/zookeeper-4unittest/contrib/fatjar registry/zookeeper/zookeeper-4unittest/contrib/fatjar
mkdir -p remoting/zookeeper/zookeeper-4unittest/contrib/fatjar config_center/zookeeper/zookeeper-4unittest/contrib/fatjar registry/zookeeper/zookeeper-4unittest/contrib/fatjar cluster/router/chain/zookeeper-4unittest/contrib/fatjar
wget -P "remoting/zookeeper/zookeeper-4unittest/contrib/fatjar" https://github.com/dubbogo/resources/raw/master/zookeeper-4unitest/contrib/fatjar/zookeeper-3.4.9-fatjar.jar
cp remoting/zookeeper/zookeeper-4unittest/contrib/fatjar/zookeeper-3.4.9-fatjar.jar config_center/zookeeper/zookeeper-4unittest/contrib/fatjar/
cp remoting/zookeeper/zookeeper-4unittest/contrib/fatjar/zookeeper-3.4.9-fatjar.jar registry/zookeeper/zookeeper-4unittest/contrib/fatjar/
cp remoting/zookeeper/zookeeper-4unittest/contrib/fatjar/zookeeper-3.4.9-fatjar.jar registry/zookeeper/zookeeper-4unittest/contrib/fatjar/
cp remoting/zookeeper/zookeeper-4unittest/contrib/fatjar/zookeeper-3.4.9-fatjar.jar cluster/router/chain/zookeeper-4unittest/contrib/fatjar
84 changes: 75 additions & 9 deletions cluster/directory/base_directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,94 @@ package directory
import (
"sync"
)

import (
"github.com/dubbogo/gost/container/set"
"go.uber.org/atomic"
)

import (
"github.com/apache/dubbo-go/cluster/router"
"github.com/apache/dubbo-go/cluster/router/chain"
"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"
)

// BaseDirectory ...
var routerURLSet = gxset.NewSet()

// BaseDirectory Abstract implementation of Directory: Invoker list returned from this Directory's list method have been filtered by Routers
type BaseDirectory struct {
zouyx marked this conversation as resolved.
Show resolved Hide resolved
url *common.URL
destroyed *atomic.Bool
mutex sync.Mutex
// this mutex for change the properties in BaseDirectory, like routerChain , destroyed etc
mutex sync.Mutex
zouyx marked this conversation as resolved.
Show resolved Hide resolved
routerChain router.Chain
}

// RouterChain Return router chain in directory
func (dir *BaseDirectory) RouterChain() router.Chain {
return dir.routerChain
}

// NewBaseDirectory ...
// SetRouterChain Set router chain in directory
func (dir *BaseDirectory) SetRouterChain(routerChain router.Chain) {
dir.mutex.Lock()
defer dir.mutex.Unlock()
dir.routerChain = routerChain
}

// NewBaseDirectory Create BaseDirectory with URL
func NewBaseDirectory(url *common.URL) BaseDirectory {
return BaseDirectory{
url: url,
destroyed: atomic.NewBool(false),
url: url,
destroyed: atomic.NewBool(false),
routerChain: &chain.RouterChain{},
}
}

// GetUrl ...
// GetUrl Get URL
func (dir *BaseDirectory) GetUrl() common.URL {
return *dir.url
}

// GetDirectoryUrl ...
// GetDirectoryUrl Get URL instance
func (dir *BaseDirectory) GetDirectoryUrl() *common.URL {
return dir.url
}

// Destroy ...
// SetRouters Convert url to routers and add them into dir.routerChain
func (dir *BaseDirectory) SetRouters(urls []*common.URL) {
if len(urls) == 0 {
return
}

routers := make([]router.Router, len(urls), len(urls))

for _, url := range urls {
routerKey := url.GetParam(constant.ROUTER_KEY, "")

if len(routerKey) > 0 {
factory := extension.GetRouterFactory(url.Protocol)
r, err := factory.NewRouter(url)
if err != nil {
logger.Errorf("Create router fail. router key: %s, error: %v", routerKey, url.Service(), err)
return
}
routers = append(routers, r)
}
}

logger.Infof("Init file condition router success, size: %v", len(routers))
dir.mutex.Lock()
rc := dir.routerChain
dir.mutex.Unlock()

rc.AddRouters(routers)
}

// Destroy Destroy
func (dir *BaseDirectory) Destroy(doDestroy func()) {
if dir.destroyed.CAS(false, true) {
dir.mutex.Lock()
Expand All @@ -61,7 +116,18 @@ func (dir *BaseDirectory) Destroy(doDestroy func()) {
}
}

// IsAvailable ...
// IsAvailable Once directory init finish, it will change to true
func (dir *BaseDirectory) IsAvailable() bool {
return !dir.destroyed.Load()
}

// GetRouterURLSet Return router URL
func GetRouterURLSet() *gxset.HashSet {
return routerURLSet
}

// AddRouterURLSet Add router URL
// Router URL will init in config/config_loader.go
func AddRouterURLSet(url *common.URL) {
routerURLSet.Add(url)
}
16 changes: 13 additions & 3 deletions cluster/directory/static_directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type staticDirectory struct {
invokers []protocol.Invoker
}

// NewStaticDirectory ...
// NewStaticDirectory Create a new staticDirectory with invokers
func NewStaticDirectory(invokers []protocol.Invoker) *staticDirectory {
var url common.URL

Expand All @@ -53,11 +53,21 @@ func (dir *staticDirectory) IsAvailable() bool {
return true
}

// List List invokers
func (dir *staticDirectory) List(invocation protocol.Invocation) []protocol.Invoker {
zouyx marked this conversation as resolved.
Show resolved Hide resolved
//TODO:Here should add router
return dir.invokers
l := len(dir.invokers)
invokers := make([]protocol.Invoker, l, l)
copy(invokers, dir.invokers)
routerChain := dir.RouterChain()

if routerChain == nil {
return invokers
}
dirUrl := dir.GetUrl()
return routerChain.Route(invokers, &dirUrl, invocation)
}

// Destroy Destroy
func (dir *staticDirectory) Destroy() {
dir.BaseDirectory.Destroy(func() {
for _, ivk := range dir.invokers {
Expand Down
4 changes: 3 additions & 1 deletion cluster/directory/static_directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ func Test_StaticDirList(t *testing.T) {
}

staticDir := NewStaticDirectory(invokers)
assert.Len(t, staticDir.List(&invocation.RPCInvocation{}), 10)
list := staticDir.List(&invocation.RPCInvocation{})

assert.Len(t, list, 10)
}

func Test_StaticDirDestroy(t *testing.T) {
Expand Down
120 changes: 120 additions & 0 deletions cluster/router/chain/chain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* 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 chain

import (
"math"
"sort"
"sync"
)

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

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

// RouterChain Router chain
type RouterChain struct {
// Full list of addresses from registry, classified by method name.
invokers []protocol.Invoker
// Containing all routers, reconstruct every time 'route://' urls change.
routers []router.Router
// Fixed router instances: ConfigConditionRouter, TagRouter, e.g., the rule for each instance may change but the
// instance will never delete or recreate.
builtinRouters []router.Router

mutex sync.RWMutex
}

// Route Loop routers in RouterChain and call Route method to determine the target invokers list.
func (c RouterChain) Route(invoker []protocol.Invoker, url *common.URL, invocation protocol.Invocation) []protocol.Invoker {
finalInvokers := invoker
l := len(c.routers)
rs := make([]router.Router, l, int(math.Ceil(float64(l)*1.2)))
c.mutex.RLock()
copy(rs, c.routers)
c.mutex.RUnlock()

for _, r := range rs {
finalInvokers = r.Route(finalInvokers, url, invocation)
}
return finalInvokers
}

// AddRouters Add routers to router chain
// New a array add builtinRouters which is not sorted in RouterChain and routers
// Sort the array
// Replace router array in RouterChain
func (c RouterChain) AddRouters(routers []router.Router) {
newRouters := make([]router.Router, 0, len(c.builtinRouters)+len(routers))
newRouters = append(newRouters, c.builtinRouters...)
newRouters = append(newRouters, routers...)
sortRouter(newRouters)
c.mutex.Lock()
defer c.mutex.Unlock()
c.routers = newRouters
}

// NewRouterChain Use url to init router chain
// Loop routerFactories and call NewRouter method
func NewRouterChain(url *common.URL) (*RouterChain, error) {
routerFactories := extension.GetRouterFactories()
if len(routerFactories) == 0 {
return nil, perrors.Errorf("Illegal route rule!")
}
routers := make([]router.Router, 0, len(routerFactories))
for key, routerFactory := range routerFactories {
r, err := routerFactory().NewRouter(url)
if r == nil || err != nil {
logger.Errorf("router chain build router fail! routerFactories key:%s error:%s", key, err.Error())
continue
}
routers = append(routers, r)
}

newRouters := make([]router.Router, len(routers))
copy(newRouters, routers)

sortRouter(newRouters)

chain := &RouterChain{
builtinRouters: routers,
routers: newRouters,
}

return chain, nil
}

// sortRouter Sort router instance by priority with stable algorithm
func sortRouter(routers []router.Router) {
sort.Stable(byPriority(routers))
}

// byPriority Sort by priority
type byPriority []router.Router

func (a byPriority) Len() int { return len(a) }
func (a byPriority) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byPriority) Less(i, j int) bool { return a[i].Priority() < a[j].Priority() }
Loading