From 8cafa462e2c06b2f636ae5eb24a973cb2b206608 Mon Sep 17 00:00:00 2001 From: dongjianhui03 Date: Fri, 15 Oct 2021 00:15:56 +0800 Subject: [PATCH] remove struct DubboRouterRule which is useless --- cluster/router/v3router/dubbo_rule.go | 61 ------------------- .../v3router/k8s_api/listener_handler_impl.go | 2 +- cluster/router/v3router/router_chain.go | 20 +++--- cluster/router/v3router/router_chain_test.go | 6 +- cluster/router/v3router/uniform_route.go | 30 +++++++-- cluster/router/v3router/uniform_rule.go | 4 +- common/constant/key.go | 2 + config_center/zookeeper/listener.go | 3 +- 8 files changed, 42 insertions(+), 86 deletions(-) delete mode 100644 cluster/router/v3router/dubbo_rule.go diff --git a/cluster/router/v3router/dubbo_rule.go b/cluster/router/v3router/dubbo_rule.go deleted file mode 100644 index c809354b19..0000000000 --- a/cluster/router/v3router/dubbo_rule.go +++ /dev/null @@ -1,61 +0,0 @@ -/* - * 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 v3router - -import ( - "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/config" - "dubbo.apache.org/dubbo-go/v3/protocol" -) - -// nolint -type DubboRouterRule struct { - uniformRules []*UniformRule -} - -func newDubboRouterRule(dubboRoutes []*config.DubboRoute, - destinationMap map[string]map[string]string) (*DubboRouterRule, 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 &DubboRouterRule{ - uniformRules: uniformRules, - }, nil -} - -func (drr *DubboRouterRule) route(invokers []protocol.Invoker, url *common.URL, - invocation protocol.Invocation) []protocol.Invoker { - - resultInvokers := make([]protocol.Invoker, 0) - for _, v := range drr.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 -} diff --git a/cluster/router/v3router/k8s_api/listener_handler_impl.go b/cluster/router/v3router/k8s_api/listener_handler_impl.go index 38f5748bb9..70dc8be2e4 100644 --- a/cluster/router/v3router/k8s_api/listener_handler_impl.go +++ b/cluster/router/v3router/k8s_api/listener_handler_impl.go @@ -36,7 +36,7 @@ import ( const ( VirtualServiceEventKey = "virtualServiceEventKey" - DestinationRuleEventKey = "destinationRuleEventKe3y" + DestinationRuleEventKey = "destinationRuleEventKey" VirtualServiceResource = "virtualservices" DestRuleResource = "destinationrules" diff --git a/cluster/router/v3router/router_chain.go b/cluster/router/v3router/router_chain.go index ce8269493d..3af479eb69 100644 --- a/cluster/router/v3router/router_chain.go +++ b/cluster/router/v3router/router_chain.go @@ -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") @@ -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) @@ -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 @@ -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 } } diff --git a/cluster/router/v3router/router_chain_test.go b/cluster/router/v3router/router_chain_test.go index ac4176931e..8832b607f4 100644 --- a/cluster/router/v3router/router_chain_test.go +++ b/cluster/router/v3router/router_chain_test.go @@ -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) diff --git a/cluster/router/v3router/uniform_route.go b/cluster/router/v3router/uniform_route.go index 9ea12dd421..f2e4273a9f 100644 --- a/cluster/router/v3router/uniform_route.go +++ b/cluster/router/v3router/uniform_route.go @@ -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" ) @@ -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 diff --git a/cluster/router/v3router/uniform_rule.go b/cluster/router/v3router/uniform_rule.go index 3b552b89f0..c1c9da25b7 100644 --- a/cluster/router/v3router/uniform_rule.go +++ b/cluster/router/v3router/uniform_rule.go @@ -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 { @@ -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 diff --git a/common/constant/key.go b/common/constant/key.go index 849d1def0d..b6f835fcdc 100644 --- a/common/constant/key.go +++ b/common/constant/key.go @@ -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" diff --git a/config_center/zookeeper/listener.go b/config_center/zookeeper/listener.go index 9be9233a73..9049e6dbcd 100644 --- a/config_center/zookeeper/listener.go +++ b/config_center/zookeeper/listener.go @@ -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:] }