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

OSPP: Traffic Distribution #501

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Changes from all commits
Commits
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
45 changes: 26 additions & 19 deletions pixiu/pkg/filter/traffic/traffic.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
package traffic

import (
"fmt"
"math/rand"
"strconv"
"strings"
"time"
)
Expand Down Expand Up @@ -72,7 +70,7 @@ type (
Name string `yaml:"name" json:"name" mapstructure:"name"`
Router string `yaml:"router" json:"router" mapstructure:"router"`
CanaryByHeader string `yaml:"canary-by-header" json:"canary-by-header" mapstructure:"canary-by-header"`
CanaryWeight string `yaml:"canary-weight" json:"canary-weight" mapstructure:"canary-weight"`
CanaryWeight int `yaml:"canary-weight" json:"canary-weight" mapstructure:"canary-weight"`
}
)

Expand Down Expand Up @@ -105,33 +103,45 @@ func (factory *FilterFactory) PrepareFilterChain(ctx *http.HttpContext, chain fi
}

func (f *Filter) Decode(ctx *http.HttpContext) filter.FilterStatus {
cluster := ""
if f.Rules != nil {
for _, wp := range f.Rules {
if f.traffic(wp, ctx) {
ctx.Route.Cluster = wp.Cluster.Name
if f.trafficHeader(wp, ctx) {
cluster = wp.Cluster.Name
logger.Debugf("[dubbo-go-pixiu] execute traffic split to cluster %s", wp.Cluster.Name)
break
}
}
if cluster == "" {
for _, wp := range f.Rules {
if f.trafficWeight(wp, ctx) {
ctx.Route.Cluster = wp.Cluster.Name
cluster = wp.Cluster.Name
logger.Debugf("[dubbo-go-pixiu] execute traffic split to cluster %s", wp.Cluster.Name)
break
}
}
}
if cluster != "" {
ctx.Route.Cluster = cluster
}
} else {
logger.Warnf("[dubbo-go-pixiu] execute traffic split fail because of empty rules.")
}
return filter.Continue
}

func (f *Filter) traffic(c *ClusterWrapper, ctx *http.HttpContext) bool {
func (f *Filter) trafficHeader(c *ClusterWrapper, ctx *http.HttpContext) bool {
return spiltHeader(ctx.Request, c.header)
}

func (f *Filter) trafficWeight(c *ClusterWrapper, ctx *http.HttpContext) bool {
if f.weight == unInitialize {
rand.Seed(time.Now().UnixNano())
f.weight = rand.Intn(100) + 1
}

res := false
if c.header != "" {
res = spiltHeader(ctx.Request, c.header)
} else if !res && c.weightFloor != -1 && c.weightCeil != -1 {
res = spiltWeight(f.weight, c.weightFloor, c.weightCeil)
}
return res
return spiltWeight(f.weight, c.weightFloor, c.weightCeil)
}

func (factory *FilterFactory) rulesMatch(f *Filter, path string) []*ClusterWrapper {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

想了解一下这个pathcluster.Router 是如何匹配上的,有没有具体的例子可以描述一下 ,比如 path="/xxx/xx", cluster.Router = "/xxx"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expand All @@ -156,15 +166,12 @@ func (factory *FilterFactory) rulesMatch(f *Filter, path string) []*ClusterWrapp
wp.header = cluster.CanaryByHeader
}
}
if cluster.CanaryWeight != "" {
val, err := strconv.Atoi(cluster.CanaryWeight)
if err != nil || val <= 0 {
logger.Errorf(fmt.Sprintf("Wrong canary-weight value: %v", cluster.CanaryWeight))
}
if cluster.CanaryWeight > 0 && cluster.CanaryWeight <= 100 {
wp.weightFloor = up
up += val
up += cluster.CanaryWeight
if up > 100 {
logger.Errorf("[dubbo-go-pixiu] clusters' weight sum more than 100 in %v service!", cluster.Router)
maxingg marked this conversation as resolved.
Show resolved Hide resolved
continue
}
wp.weightCeil = up
}
Expand Down