Skip to content

Commit

Permalink
remove struct DubboRouterRule which is useless
Browse files Browse the repository at this point in the history
  • Loading branch information
dongjianhui03 committed Oct 14, 2021
1 parent 4b1746f commit 8cafa46
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 86 deletions.
61 changes: 0 additions & 61 deletions cluster/router/v3router/dubbo_rule.go

This file was deleted.

2 changes: 1 addition & 1 deletion cluster/router/v3router/k8s_api/listener_handler_impl.go
Expand Up @@ -36,7 +36,7 @@ import (

const (
VirtualServiceEventKey = "virtualServiceEventKey"
DestinationRuleEventKey = "destinationRuleEventKe3y"
DestinationRuleEventKey = "destinationRuleEventKey"

VirtualServiceResource = "virtualservices"
DestRuleResource = "destinationrules"
Expand Down
20 changes: 8 additions & 12 deletions cluster/router/v3router/router_chain.go
Expand Up @@ -82,7 +82,7 @@ func (r *RouterChain) Process(event *config_center.ConfigChangeEvent) {
if event.ConfigType == remoting.EventTypeAdd || event.ConfigType == remoting.EventTypeUpdate {
switch event.Key {
case k8s_api.VirtualServiceEventKey:
logger.Debug("virtul service event")
logger.Debug("virtual service event")
newVSValue, ok := event.Value.(*config.VirtualServiceConfig)
if !ok {
logger.Error("event.Value assertion error")
Expand All @@ -103,7 +103,7 @@ func (r *RouterChain) Process(event *config_center.ConfigChangeEvent) {
newVirtualServiceConfig.YamlAPIVersion = newVirtualServiceConfig.APIVersion
newVirtualServiceConfig.YamlKind = newVirtualServiceConfig.Kind
newVirtualServiceConfig.MetaData.Name = newVirtualServiceConfig.ObjectMeta.Name
logger.Debugf("get event after asseration = %+v\n", newVirtualServiceConfig)
logger.Debugf("get event after assertion = %+v\n", newVirtualServiceConfig)
data, err := yaml.Marshal(newVirtualServiceConfig)
if err != nil {
logger.Error("Process change of virtual service: event.Value marshal error:", err)
Expand Down Expand Up @@ -220,20 +220,16 @@ func parseFromConfigToRouters(virtualServiceConfig, destinationRuleConfig []byte
routers := make([]*UniformRouter, 0)

for _, v := range virtualServiceConfigList {
tempSerivceNeedsDescMap := make(map[string]map[string]string)
tempServiceNeedsDescMap := make(map[string]map[string]string)
for _, host := range v.Spec.Hosts {
// name -> labels
targetDestMap := destRuleConfigsMap[host]

// copy to new Map
mapCombine(tempSerivceNeedsDescMap, targetDestMap)
mapCombine(tempServiceNeedsDescMap, targetDestMap)
}
// change single config to one rule
newRule, err := newDubboRouterRule(v.Spec.Dubbo, tempSerivceNeedsDescMap)
if err != nil {
logger.Error("Parse config to uniform rule err = ", err)
return nil, err
}
rtr, err := NewUniformRouter(newRule)
rtr, err := NewUniformRouter(v.Spec.Dubbo, tempServiceNeedsDescMap)
if err != nil {
logger.Error("new uniform router err = ", err)
return nil, err
Expand All @@ -244,8 +240,8 @@ func parseFromConfigToRouters(virtualServiceConfig, destinationRuleConfig []byte
return routers, nil
}

func mapCombine(dist map[string]map[string]string, from map[string]map[string]string) {
for k, v := range from {
func mapCombine(dist map[string]map[string]string, source map[string]map[string]string) {
for k, v := range source {
dist[k] = v
}
}
6 changes: 3 additions & 3 deletions cluster/router/v3router/router_chain_test.go
Expand Up @@ -70,9 +70,9 @@ func TestParseConfigFromFile(t *testing.T) {
routers, err := parseFromConfigToRouters(vsBytes, drBytes)
fmt.Println(routers, err)
assert.Equal(t, len(routers), 1)
assert.NotNil(t, routers[0].dubboRouter)
assert.Equal(t, len(routers[0].dubboRouter.uniformRules), 2)
for i, v := range routers[0].dubboRouter.uniformRules {
assert.NotNil(t, routers[0].uniformRules)
assert.Equal(t, len(routers[0].uniformRules), 2)
for i, v := range routers[0].uniformRules {
if i == 0 {
assert.Equal(t, len(v.services), 2)
assert.Equal(t, "com.taobao.hsf.demoService:1.0.0", v.services[0].Exact)
Expand Down
30 changes: 24 additions & 6 deletions cluster/router/v3router/uniform_route.go
Expand Up @@ -19,6 +19,7 @@ package v3router

import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/config"
"dubbo.apache.org/dubbo-go/v3/config_center"
"dubbo.apache.org/dubbo-go/v3/protocol"
)
Expand All @@ -29,20 +30,37 @@ const (

// UniformRouter have
type UniformRouter struct {
dubboRouter *DubboRouterRule
uniformRules []*UniformRule
}

// NewUniformRouter construct an NewConnCheckRouter via url
func NewUniformRouter(dubboRouter *DubboRouterRule) (*UniformRouter, error) {
r := &UniformRouter{
dubboRouter: dubboRouter,
func NewUniformRouter(dubboRoutes []*config.DubboRoute, destinationMap map[string]map[string]string) (*UniformRouter, error) {
uniformRules := make([]*UniformRule, 0)
for _, v := range dubboRoutes {
uniformRule, err := newUniformRule(v, destinationMap)
if err != nil {
return nil, err
}
uniformRules = append(uniformRules, uniformRule)
}
return r, nil

return &UniformRouter{
uniformRules: uniformRules,
}, nil
}

// Route gets a list of routed invoker
func (r *UniformRouter) Route(invokers []protocol.Invoker, url *common.URL, invocation protocol.Invocation) []protocol.Invoker {
return r.dubboRouter.route(invokers, url, invocation)
resultInvokers := make([]protocol.Invoker, 0)
for _, v := range r.uniformRules {
if resultInvokers = v.route(invokers, url, invocation); len(resultInvokers) == 0 {
continue
}
// once there is a uniformRule successfully get target invoker lists, return it
return resultInvokers
}
// return s empty invoker list
return resultInvokers
}

// Process there is no process needs for uniform Router, as it upper struct RouterChain has done it
Expand Down
4 changes: 2 additions & 2 deletions cluster/router/v3router/uniform_rule.go
Expand Up @@ -44,7 +44,7 @@ type VirtualServiceRule struct {
uniformRule *UniformRule
}

// match read from vsr's Match config
// match read from VirtualServiceRule's Match config
// it judges if this invocation matches the router rule request defined in config one by one
func (vsr *VirtualServiceRule) match(url *common.URL, invocation protocol.Invocation) bool {
for _, v := range vsr.routerItem.Match {
Expand Down Expand Up @@ -198,7 +198,7 @@ func (vsr *VirtualServiceRule) getRuleTargetInvokers(invokers []protocol.Invoker
return weightInvokerPairResult.getTargetInvokers(), nil
}

// UniformRule
// UniformRule uniform rule
type UniformRule struct {
services []*config.StringMatch
virtualServiceRules []VirtualServiceRule
Expand Down
2 changes: 2 additions & 0 deletions common/constant/key.go
Expand Up @@ -249,6 +249,8 @@ const (
TagRouterRuleSuffix = ".tag-router"
// ConditionRouterRuleSuffix Specify condition router suffix
ConditionRouterRuleSuffix = ".condition-router"
// MeshRouteSuffix Specify mesh router suffix
MeshRouteSuffix = ".MESHAPPRULE"
// ForceUseTag is the tag in attachment
ForceUseTag = "dubbo.force.tag"
Tagkey = "dubbo.tag"
Expand Down
3 changes: 2 additions & 1 deletion config_center/zookeeper/listener.go
Expand Up @@ -80,7 +80,8 @@ func (l *CacheListener) pathToKey(path string) string {
key := strings.Replace(strings.Replace(path, l.rootPath+"/", "", -1), "/", ".", -1)
if strings.HasSuffix(key, constant.CONFIGURATORS_SUFFIX) ||
strings.HasSuffix(key, constant.TagRouterRuleSuffix) ||
strings.HasSuffix(key, constant.ConditionRouterRuleSuffix) {
strings.HasSuffix(key, constant.ConditionRouterRuleSuffix) ||
strings.HasSuffix(key, constant.MeshRouteSuffix) {
// governance config, so we remove the "dubbo." prefix
return key[strings.Index(key, ".")+1:]
}
Expand Down

0 comments on commit 8cafa46

Please sign in to comment.