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

config.WithPath方法中ioutil.ReadFile的实参改为conf.path #2209

Merged
merged 12 commits into from
Feb 12, 2023
19 changes: 18 additions & 1 deletion cluster/router/polaris/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ var (
)

func newPolarisRouter() (*polarisRouter, error) {
if err := remotingpolaris.Check(); errors.Is(err, remotingpolaris.ErrorNoOpenPolarisAbility) {
return &polarisRouter{
openRoute: false,
}, nil
}

routerAPI, err := remotingpolaris.GetRouterAPI()
if err != nil {
return nil, err
Expand All @@ -63,12 +69,15 @@ func newPolarisRouter() (*polarisRouter, error) {
}

return &polarisRouter{
openRoute: true,
routerAPI: routerAPI,
consumerAPI: consumerAPI,
}, nil
}

type polarisRouter struct {
openRoute bool

routerAPI polaris.RouterAPI
consumerAPI polaris.ConsumerAPI

Expand All @@ -82,8 +91,13 @@ type polarisRouter struct {
func (p *polarisRouter) Route(invokers []protocol.Invoker, url *common.URL,
invoaction protocol.Invocation) []protocol.Invoker {

if !p.openRoute {
logger.Debug("[Router][Polaris] not open polaris route ability")
return invokers
}

if len(invokers) == 0 {
logger.Warnf("[tag router] invokers from previous router is empty")
logger.Warn("[Router][Polaris] invokers from previous router is empty")
return invokers
}

Expand Down Expand Up @@ -280,6 +294,9 @@ func (p *polarisRouter) Priority() int64 {

// Notify the router the invoker list
func (p *polarisRouter) Notify(invokers []protocol.Invoker) {
if !p.openRoute {
return
}
if len(invokers) == 0 {
return
}
Expand Down
49 changes: 20 additions & 29 deletions common/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ import (

gxset "github.com/dubbogo/gost/container/set"

"github.com/google/uuid"
"github.com/jinzhu/copier"

perrors "github.com/pkg/errors"

"github.com/satori/go.uuid"
)

import (
Expand Down Expand Up @@ -203,7 +201,7 @@ func WithToken(token string) Option {
if len(token) > 0 {
value := token
if strings.ToLower(token) == "true" || strings.ToLower(token) == "default" {
u, _ := uuid.NewV4()
u, _ := uuid.NewUUID()
value = u.String()
}
url.SetParam(constant.TokenKey, value)
Expand Down Expand Up @@ -684,7 +682,8 @@ func (c *URL) ToMap() map[string]string {
// will be added into result.
// for example, if serviceURL contains params (a1->v1, b1->v2) and referenceURL contains params(a2->v3, b1 -> v4)
// the params of result will be (a1->v1, b1->v2, a2->v3).
// You should notice that the value of b1 is v2, not v4.
// You should notice that the value of b1 is v2, not v4
// except constant.LoadbalanceKey, constant.ClusterKey, constant.RetriesKey, constant.TimeoutKey.
// due to URL is not thread-safe, so this method is not thread-safe
func MergeURL(serviceURL *URL, referenceURL *URL) *URL {
// After Clone, it is a new URL that there is no thread safe issue.
Expand All @@ -693,16 +692,15 @@ func MergeURL(serviceURL *URL, referenceURL *URL) *URL {
// iterator the referenceURL if serviceURL not have the key ,merge in
// referenceURL usually will not changed. so change RangeParams to GetParams to avoid the string value copy.// Group get group
for key, value := range referenceURL.GetParams() {
if v := mergedURL.GetParam(key, ""); len(v) == 0 {
if len(value) > 0 {
params[key] = value
if v := mergedURL.GetParam(key, ""); len(v) == 0 && len(value) > 0 {
if params == nil {
params = url.Values{}
}
params[key] = make([]string, len(value))
copy(params[key], value)
}
}

// loadBalance,cluster,retries strategy config
methodConfigMergeFcn := mergeNormalParam(params, referenceURL, []string{constant.LoadbalanceKey, constant.ClusterKey, constant.RetriesKey, constant.TimeoutKey})

// remote timestamp
if v := serviceURL.GetParam(constant.TimestampKey, ""); len(v) > 0 {
params[constant.RemoteTimestampKey] = []string{v}
Expand All @@ -711,8 +709,17 @@ func MergeURL(serviceURL *URL, referenceURL *URL) *URL {

// finally execute methodConfigMergeFcn
for _, method := range referenceURL.Methods {
for _, fcn := range methodConfigMergeFcn {
fcn("methods." + method)
for _, paramKey := range []string{constant.LoadbalanceKey, constant.ClusterKey, constant.RetriesKey, constant.TimeoutKey} {
if v := referenceURL.GetParam(paramKey, ""); len(v) > 0 {
params[paramKey] = []string{v}
}

methodsKey := "methods." + method + "." + paramKey
//if len(mergedURL.GetParam(methodsKey, "")) == 0 {
if v := referenceURL.GetParam(methodsKey, ""); len(v) > 0 {
params[methodsKey] = []string{v}
}
//}
}
}
// In this way, we will raise some performance.
Expand All @@ -732,7 +739,6 @@ func (c *URL) Clone() *URL {
newURL.SetParam(key, value)
return true
})

return newURL
}

Expand Down Expand Up @@ -818,21 +824,6 @@ func IsEquals(left *URL, right *URL, excludes ...string) bool {
return true
}

func mergeNormalParam(params url.Values, referenceURL *URL, paramKeys []string) []func(method string) {
methodConfigMergeFcn := make([]func(method string), 0, len(paramKeys))
for _, paramKey := range paramKeys {
if v := referenceURL.GetParam(paramKey, ""); len(v) > 0 {
params[paramKey] = []string{v}
}
methodConfigMergeFcn = append(methodConfigMergeFcn, func(method string) {
if v := referenceURL.GetParam(method+"."+paramKey, ""); len(v) > 0 {
params[method+"."+paramKey] = []string{v}
}
})
}
return methodConfigMergeFcn
}

// URLSlice will be used to sort URL instance
// Instances will be order by URL.String()
type URLSlice []*URL
Expand Down
2 changes: 1 addition & 1 deletion common/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func TestMergeUrl(t *testing.T) {
assert.Equal(t, "1", mergedUrl.GetParam("test2", ""))
assert.Equal(t, "1", mergedUrl.GetParam("test3", ""))
assert.Equal(t, "1", mergedUrl.GetParam(constant.RetriesKey, ""))
assert.Equal(t, "2", mergedUrl.GetParam(constant.MethodKeys+".testMethod."+constant.RetriesKey, ""))
assert.Equal(t, "1", mergedUrl.GetParam(constant.MethodKeys+".testMethod."+constant.RetriesKey, ""))
}

func TestURLSetParams(t *testing.T) {
Expand Down
10 changes: 5 additions & 5 deletions config/config_loader_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (fn loaderConfigFunc) apply(vc *loaderConf) {
}

// WithGenre set load config file suffix
//Deprecated: replaced by WithSuffix
// Deprecated: replaced by WithSuffix
func WithGenre(suffix string) LoaderConfOption {
return loaderConfigFunc(func(conf *loaderConf) {
g := strings.ToLower(suffix)
Expand All @@ -108,7 +108,7 @@ func WithSuffix(suffix file.Suffix) LoaderConfOption {
func WithPath(path string) LoaderConfOption {
return loaderConfigFunc(func(conf *loaderConf) {
conf.path = absolutePath(path)
if bytes, err := ioutil.ReadFile(path); err != nil {
if bytes, err := ioutil.ReadFile(conf.path); err != nil {
panic(err)
} else {
conf.bytes = bytes
Expand Down Expand Up @@ -157,7 +157,7 @@ func absolutePath(inPath string) string {
return ""
}

//userHomeDir get gopath
// userHomeDir get gopath
func userHomeDir() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
Expand All @@ -179,7 +179,7 @@ func checkFileSuffix(suffix string) error {
return errors.Errorf("no support file suffix: %s", suffix)
}

//resolverFilePath resolver file path
// resolverFilePath resolver file path
// eg: give a ./conf/dubbogo.yaml return dubbogo and yaml
func resolverFilePath(path string) (name, suffix string) {
paths := strings.Split(path, "/")
Expand All @@ -190,7 +190,7 @@ func resolverFilePath(path string) (name, suffix string) {
return fileName[0], fileName[1]
}

//MergeConfig merge config file
// MergeConfig merge config file
func (conf *loaderConf) MergeConfig(koan *koanf.Koanf) *koanf.Koanf {
var (
activeKoan *koanf.Koanf
Expand Down
8 changes: 8 additions & 0 deletions config/logger_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,11 @@ func (lcb *LoggerConfigBuilder) SetZapConfig(zapConfig ZapConfig) *LoggerConfigB
func (lcb *LoggerConfigBuilder) Build() *LoggerConfig {
return lcb.loggerConfig
}

// DynamicUpdateProperties dynamically update properties.
func (lc *LoggerConfig) DynamicUpdateProperties(newLoggerConfig *LoggerConfig) {
if newLoggerConfig != nil && lc.ZapConfig.Level != newLoggerConfig.ZapConfig.Level {
lc.ZapConfig.Level = newLoggerConfig.ZapConfig.Level
logger.Infof("LoggerConfig's ZapConfig Level was dynamically updated, new value:%v", lc.ZapConfig.Level)
}
}
13 changes: 13 additions & 0 deletions config/metric_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package config

import (
"github.com/creasty/defaults"
"github.com/dubbogo/gost/log/logger"

"github.com/pkg/errors"
)
Expand Down Expand Up @@ -81,3 +82,15 @@ func NewMetricConfigBuilder() *MetricConfigBuilder {
func (mcb *MetricConfigBuilder) Build() *MetricConfig {
return mcb.metricConfig
}

// DynamicUpdateProperties dynamically update properties.
func (mc *MetricConfig) DynamicUpdateProperties(newMetricConfig *MetricConfig) {
if newMetricConfig != nil {
if newMetricConfig.Enable != mc.Enable {
mc.Enable = newMetricConfig.Enable
logger.Infof("MetricConfig's Enable was dynamically updated, new value:%v", mc.Enable)

extension.GetMetricReporter("prometheus", mc.ToReporterConfig())
}
}
}
6 changes: 6 additions & 0 deletions config/root_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,4 +395,10 @@ func (rc *RootConfig) Process(event *config_center.ConfigChangeEvent) {
}
// dynamically update consumer
rc.Consumer.DynamicUpdateProperties(updateRootConfig.Consumer)

// dynamically update logger
rc.Logger.DynamicUpdateProperties(updateRootConfig.Logger)

// dynamically update metric
rc.Metric.DynamicUpdateProperties(updateRootConfig.Metric)
}
6 changes: 6 additions & 0 deletions filter/polaris/limit/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package limit

import (
"errors"
"fmt"
"time"
)
Expand Down Expand Up @@ -45,6 +46,11 @@ type polarisTpsLimiter struct {
}

func (pl *polarisTpsLimiter) IsAllowable(url *common.URL, invocation protocol.Invocation) bool {
if err := remotingpolaris.Check(); errors.Is(err, remotingpolaris.ErrorNoOpenPolarisAbility) {
logger.Debug("[TpsLimiter][Polaris] not open polaris ratelimit ability")
return true
}

var err error

pl.limitAPI, err = remotingpolaris.GetLimiterAPI()
Expand Down
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.15

require (
contrib.go.opencensus.io/exporter/prometheus v0.4.1
github.com/RoaringBitmap/roaring v1.2.0
github.com/RoaringBitmap/roaring v1.2.3
github.com/Workiva/go-datastructures v1.0.52
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5
github.com/alibaba/sentinel-golang v1.0.4
Expand All @@ -27,26 +27,26 @@ require (
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.2
github.com/google/go-cmp v0.5.9
github.com/google/uuid v1.3.0
github.com/gopherjs/gopherjs v0.0.0-20190910122728-9d188e94fb99 // indirect
github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645
github.com/hashicorp/vault/sdk v0.6.0
github.com/hashicorp/vault/sdk v0.7.0
github.com/jinzhu/copier v0.3.5
github.com/knadh/koanf v1.4.4
github.com/knadh/koanf v1.5.0
github.com/magiconair/properties v1.8.7
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/mapstructure v1.5.0
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
github.com/nacos-group/nacos-sdk-go v1.1.3
github.com/nacos-group/nacos-sdk-go v1.1.4
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852
github.com/opentracing/opentracing-go v1.2.0
github.com/pkg/errors v0.9.1
github.com/polarismesh/polaris-go v1.3.0
github.com/prometheus/client_golang v1.12.2
github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b
github.com/stretchr/testify v1.8.1
go.etcd.io/etcd/api/v3 v3.5.6
go.etcd.io/etcd/client/v3 v3.5.6
go.etcd.io/etcd/api/v3 v3.5.7
go.etcd.io/etcd/client/v3 v3.5.7
go.opentelemetry.io/otel v1.11.0
go.opentelemetry.io/otel/trace v1.11.0
go.uber.org/atomic v1.9.0
Expand Down
Loading