Skip to content

Commit

Permalink
fix:修复路由匹配失败默认转为返回全部实例 (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
chuntaojun authored Aug 26, 2024
1 parent d27bee2 commit e9177ea
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 22 deletions.
10 changes: 10 additions & 0 deletions pkg/plugin/servicerouter/servicerouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ import (
"github.com/polarismesh/polaris-go/pkg/plugin/common"
)

type FailOverType int32

const (
_ FailOverType = iota
FailOverAll
FailOverNone
)

// RouteInfo 路由信息
type RouteInfo struct {
// 源服务信息
Expand Down Expand Up @@ -57,6 +65,8 @@ type RouteInfo struct {
Canary string
// 进行匹配的规则类型,如规则路由有入规则和出规则之分
MatchRuleType RuleType
// 规则路由失败降级类型
FailOverType *FailOverType
}

// Init 初始化map
Expand Down
62 changes: 40 additions & 22 deletions plugin/servicerouter/rulebase/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package rulebase

import (
"encoding/json"
"fmt"
"sync"

"github.com/golang/protobuf/jsonpb"
Expand All @@ -28,7 +27,6 @@ import (

"github.com/polarismesh/polaris-go/pkg/algorithm/rand"
"github.com/polarismesh/polaris-go/pkg/config"
"github.com/polarismesh/polaris-go/pkg/log"
"github.com/polarismesh/polaris-go/pkg/model"
"github.com/polarismesh/polaris-go/pkg/plugin"
"github.com/polarismesh/polaris-go/pkg/plugin/common"
Expand All @@ -44,6 +42,7 @@ type RuleBasedInstancesFilter struct {
recoverAll bool
prioritySubsetPool *sync.Pool
systemCfg config.SystemConfig
routerConf *RuleRouterConfig
}

// Type 插件类型
Expand All @@ -66,6 +65,10 @@ func (g *RuleBasedInstancesFilter) Init(ctx *plugin.InitContext) error {
g.valueCtx = ctx.ValueCtx
g.prioritySubsetPool = &sync.Pool{}
g.systemCfg = ctx.Config.GetGlobal().GetSystem()
routerConf := ctx.Config.GetConsumer().GetServiceRouter().GetPluginConfig(g.Name())
if routerConf != nil {
g.routerConf = routerConf.(*RuleRouterConfig)
}
return nil
}

Expand Down Expand Up @@ -144,27 +147,20 @@ finally:
case dstRuleSuccess:
targetCluster = dstFilteredInstances
default:
checkRule := routeInfo.DestService.GetNamespace() + ":" + routeInfo.DestService.GetService()
if ruleStatus == sourceRuleFail {
checkRule = routeInfo.SourceService.GetNamespace() + ":" + routeInfo.SourceService.GetService()
failoverType := routeInfo.FailOverType
if failoverType == nil {
failoverType = &g.routerConf.failoverType
}
if *failoverType == servicerouter.FailOverNone {
emptyCluster := model.NewServiceClusters(model.NewDefaultServiceInstancesWithRegistryValue(model.ServiceInfo{
Service: withinCluster.GetClusters().GetServiceInstances().GetService(),
Namespace: withinCluster.GetClusters().GetServiceInstances().GetNamespace(),
Metadata: withinCluster.GetClusters().GetServiceInstances().GetMetadata(),
}, withinCluster.GetClusters().GetServiceInstances(), []model.Instance{}))
targetCluster = model.NewCluster(emptyCluster, withinCluster)
} else {
targetCluster = model.NewCluster(clusters, withinCluster)
}
// 如果规则匹配失败, 返回错误
notMatchedSrcText := getSourcesText(summary.notMatchedSources)
matchedSrcText := getSourcesText(summary.matchedSource)
invalidRegexSourceText := getSourcesText(summary.invalidRegexSources)
notMatchedDstText := getNotMatchedDestinationText(summary.notMatchedDestinations)
invalidRegexDstText := getNotMatchedDestinationText(summary.invalidRegexDestinations)
weightZeroDstText := getNotMatchedDestinationText(summary.weightZeroDestinations)
regexCompileErrText := getErrorRegexText(summary.errorRegexes)
errorText := fmt.Sprintf("route rule not match, rule status: %s, sourceService %s, used variables %v,"+
" dstService %s, notMatchedSource is %s, invalidRegexSource is %s, matchedSource is %s,"+
" notMatchedDestination is %s, invalidRegexDestination is %s, zeroWeightDestination is %s,"+
" regexCompileErrors is %s, please check your route rule of service %s",
ruleStatus.String(), model.ToStringService(routeInfo.SourceService, true), routeInfo.EnvironmentVariables,
model.ToStringService(routeInfo.DestService, false), notMatchedSrcText, invalidRegexSourceText,
matchedSrcText, notMatchedDstText, invalidRegexDstText, weightZeroDstText, regexCompileErrText, checkRule)
log.GetBaseLogger().Errorf(errorText)
return nil, model.NewSDKError(model.ErrCodeRouteRuleNotMatch, nil, errorText)
}
result := servicerouter.PoolGetRouteResult(g.valueCtx)
result.OutputCluster = targetCluster
Expand Down Expand Up @@ -237,3 +233,25 @@ func checkRouteRule(routeRule model.ServiceRule) error {
func init() {
plugin.RegisterPlugin(&RuleBasedInstancesFilter{})
}

func init() {
plugin.RegisterConfigurablePlugin(&RuleBasedInstancesFilter{}, &RuleRouterConfig{})
}

type RuleRouterConfig struct {
failoverType servicerouter.FailOverType
FailoverType string `yaml:"failoverType"`
}

// Verify 校验配置是否OK
func (rc *RuleRouterConfig) Verify() error {
return nil
}

// SetDefault 对关键值设置默认值
func (rc *RuleRouterConfig) SetDefault() {
rc.failoverType = servicerouter.FailOverAll
if rc.FailoverType == "none" {
rc.failoverType = servicerouter.FailOverNone
}
}

0 comments on commit e9177ea

Please sign in to comment.