From 4ff167d74bb0eabe31bde375a29464774d5311f5 Mon Sep 17 00:00:00 2001 From: Mark4z <36187602+mark4z@users.noreply.github.com> Date: Wed, 20 Dec 2023 10:56:17 +0800 Subject: [PATCH] 1.0.0 (#602) * V1.0.0 Changes (#588) * [feature] cors support options request (#591) * V1.0.0 Changes * [feature] cors support options request * Move Pixiu cmd files in /cmd/pixiu to pkg/cmd (#596) * remove unused types JTypeMapper check & support default types. (#597) --- cmd/pixiu/gateway.go | 70 -------- cmd/pixiu/pixiu.go | 146 +--------------- go.mod | 1 + pixiu/pkg/client/dubbo/option.go | 21 +-- pixiu/pkg/client/dubbo/option_test.go | 7 +- {cmd/pixiu => pixiu/pkg/cmd}/deployer.go | 2 +- pixiu/pkg/cmd/gateway.go | 201 +++++++++++++++++++++++ {cmd/pixiu => pixiu/pkg/cmd}/sidecar.go | 12 +- pixiu/pkg/filter/failinject/config.go | 4 +- pixiu/pkg/filter/failinject/filter.go | 2 + 10 files changed, 231 insertions(+), 235 deletions(-) delete mode 100644 cmd/pixiu/gateway.go rename {cmd/pixiu => pixiu/pkg/cmd}/deployer.go (98%) create mode 100644 pixiu/pkg/cmd/gateway.go rename {cmd/pixiu => pixiu/pkg/cmd}/sidecar.go (93%) diff --git a/cmd/pixiu/gateway.go b/cmd/pixiu/gateway.go deleted file mode 100644 index a7053dc56..000000000 --- a/cmd/pixiu/gateway.go +++ /dev/null @@ -1,70 +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 main - -import ( - "os" -) - -import ( - "github.com/spf13/cobra" -) - -import ( - "github.com/apache/dubbo-go-pixiu/pixiu/pkg/common/constant" -) - -var ( - gatewayCmd = &cobra.Command{ - Use: "gateway", - Short: "Run dubbo go pixiu in gateway mode", - } - - startGatewayCmd = &cobra.Command{ - Use: "start", - Short: "Start gateway", - Version: Version, - PreRun: func(cmd *cobra.Command, args []string) { - initDefaultValue() - - err := deploy.initialize() - if err != nil { - panic(err) - } - }, - Run: func(cmd *cobra.Command, args []string) { - - err := deploy.start() - if err != nil { - panic(err) - } - }, - } -) - -// init Init startCmd -func init() { - startGatewayCmd.PersistentFlags().StringVarP(&configPath, constant.ConfigPathKey, "c", os.Getenv(constant.EnvDubbogoPixiuConfig), "Load configuration from `FILE`") - startGatewayCmd.PersistentFlags().StringVarP(&apiConfigPath, constant.ApiConfigPathKey, "a", os.Getenv(constant.EnvDubbogoPixiuApiConfig), "Load api configuration from `FILE`") - startGatewayCmd.PersistentFlags().StringVarP(&logConfigPath, constant.LogConfigPathKey, "g", os.Getenv(constant.EnvDubbogoPixiuLogConfig), "Load log configuration from `FILE`") - startGatewayCmd.PersistentFlags().StringVarP(&logLevel, constant.LogLevelKey, "l", os.Getenv(constant.EnvDubbogoPixiuLogLevel), "dubbogo pixiu log level, trace|debug|info|warning|error|critical") - startGatewayCmd.PersistentFlags().StringVarP(&limitCpus, constant.LimitCpusKey, "m", os.Getenv(constant.EnvDubbogoPixiuLimitCpus), "dubbogo pixiu schedule threads count") - startGatewayCmd.PersistentFlags().StringVarP(&logFormat, constant.LogFormatKey, "f", os.Getenv(constant.EnvDubbogoPixiuLogFormat), "dubbogo pixiu log format, currently useless") - - gatewayCmd.AddCommand(startGatewayCmd) -} diff --git a/cmd/pixiu/pixiu.go b/cmd/pixiu/pixiu.go index 94020052d..0bb58aede 100644 --- a/cmd/pixiu/pixiu.go +++ b/cmd/pixiu/pixiu.go @@ -18,9 +18,7 @@ package main import ( - "fmt" _ "net/http/pprof" - "runtime" "strconv" "time" ) @@ -30,45 +28,15 @@ import ( ) import ( - "github.com/apache/dubbo-go-pixiu/pixiu/pkg/common/constant" - pxruntime "github.com/apache/dubbo-go-pixiu/pixiu/pkg/common/runtime" - "github.com/apache/dubbo-go-pixiu/pixiu/pkg/config" - "github.com/apache/dubbo-go-pixiu/pixiu/pkg/logger" - "github.com/apache/dubbo-go-pixiu/pixiu/pkg/model" + "github.com/apache/dubbo-go-pixiu/pixiu/pkg/cmd" _ "github.com/apache/dubbo-go-pixiu/pixiu/pkg/pluginregistry" - "github.com/apache/dubbo-go-pixiu/pixiu/pkg/server" ) -var ( +const ( // Version pixiu version - Version = "0.6.0" - - flagToLogLevel = map[string]string{ - "trace": "TRACE", - "debug": "DEBUG", - "info": "INFO", - "warning": "WARN", - "error": "ERROR", - "critical": "FATAL", - } - - configPath string - apiConfigPath string - logConfigPath string - logLevel string - - // CURRENTLY USELESS - logFormat string - - limitCpus string - - // Currently default set to false, wait for up coming support - initFromRemote = false + Version = "1.0.0" ) -// deploy server deployment -var deploy = &DefaultDeployer{} - // main pixiu run method func main() { app := getRootCmd() @@ -88,112 +56,8 @@ func getRootCmd() *cobra.Command { Version: Version, } - rootCmd.AddCommand(gatewayCmd) - rootCmd.AddCommand(sideCarCmd) + rootCmd.AddCommand(cmd.GatewayCmd) + rootCmd.AddCommand(cmd.SideCarCmd) return rootCmd } - -type DefaultDeployer struct { - bootstrap *model.Bootstrap - configManger *config.ConfigManager -} - -func (d *DefaultDeployer) initialize() error { - - err := initLog() - if err != nil { - logger.Warnf("[startGatewayCmd] failed to init logger, %s", err.Error()) - } - - // load Bootstrap config - d.bootstrap = d.configManger.LoadBootConfig(configPath) - if err != nil { - panic(fmt.Errorf("[startGatewayCmd] failed to get api meta config, %s", err.Error())) - } - - err = initLimitCpus() - if err != nil { - logger.Errorf("[startCmd] failed to get limit cpu number, %s", err.Error()) - } - - return err -} - -func (d *DefaultDeployer) start() error { - server.Start(d.bootstrap) - return nil -} - -func (d *DefaultDeployer) stop() error { - //TODO implement me - panic("implement me") -} - -// initDefaultValue If not set both in args and env, set default values -func initDefaultValue() { - if configPath == "" { - configPath = constant.DefaultConfigPath - } - - if apiConfigPath == "" { - apiConfigPath = constant.DefaultApiConfigPath - } - - if logConfigPath == "" { - logConfigPath = constant.DefaultLogConfigPath - } - - if logLevel == "" { - logLevel = constant.DefaultLogLevel - } - - if limitCpus == "" { - limitCpus = constant.DefaultLimitCpus - } - - if logFormat == "" { - logFormat = constant.DefaultLogFormat - } -} - -// initLog -func initLog() error { - err := logger.InitLog(logConfigPath) - if err != nil { - // cause `logger.InitLog` already handle init failed, so just use logger to log - return err - } - - if level, ok := flagToLogLevel[logLevel]; ok { - logger.SetLoggerLevel(level) - } else { - logger.SetLoggerLevel(flagToLogLevel[constant.DefaultLogLevel]) - return fmt.Errorf("logLevel is invalid, set log level to default: %s", constant.DefaultLogLevel) - } - return nil -} - -// initApiConfig return value of the bool is for the judgment of whether is a api meta data error, a kind of silly (?) -func initApiConfig() (*model.Bootstrap, error) { - bootstrap := config.Load(configPath) - return bootstrap, nil -} - -func initLimitCpus() error { - limitCpuNumberFromEnv, err := strconv.ParseInt(limitCpus, 10, 64) - if err != nil { - return err - } - limitCpuNumber := int(limitCpuNumberFromEnv) - if limitCpuNumber <= 0 { - limitCpuNumber = pxruntime.GetCPUNum() - } - runtime.GOMAXPROCS(limitCpuNumber) - logger.Infof("GOMAXPROCS set to %v", limitCpuNumber) - return nil -} - -func init() { - deploy.configManger = config.NewConfigManger() -} diff --git a/go.mod b/go.mod index 211d15821..324297c50 100644 --- a/go.mod +++ b/go.mod @@ -328,3 +328,4 @@ require ( ) replace google.golang.org/protobuf v1.28.1 => google.golang.org/protobuf v1.28.0 + diff --git a/pixiu/pkg/client/dubbo/option.go b/pixiu/pkg/client/dubbo/option.go index 41980f73c..1d1aae2ec 100644 --- a/pixiu/pkg/client/dubbo/option.go +++ b/pixiu/pkg/client/dubbo/option.go @@ -186,24 +186,19 @@ type paramTypesOpt struct{} // Action for paramTypesOpt override the other param types mapping/config. // The val must be string(e.g. "int, object"), and will then assign to the target.(dubboTarget).Types func (opt *paramTypesOpt) Action(target, val interface{}) error { - v, ok := val.(string) - if !ok { - return errors.New("The val type must be string") - } - types := strings.Split(v, ",") dubboTarget, ok := target.(*dubboTarget) if !ok { return errors.New("Target is not dubboTarget in target parameter") } - for i := range types { - trimType := strings.TrimSpace(types[i]) - if len(trimType) == 0 { - continue - } - if _, ok = constant.JTypeMapper[trimType]; !ok { - return errors.Errorf("Types invalid %s", trimType) + // empty types for func like func() + types := make([]string, 0) + if v, ok := val.(string); ok { + if len(v) > 0 { + types = strings.Split(v, ",") + for i := range types { + types[i] = strings.TrimSpace(types[i]) + } } - types[i] = trimType } dubboTarget.Types = types return nil diff --git a/pixiu/pkg/client/dubbo/option_test.go b/pixiu/pkg/client/dubbo/option_test.go index e8762e599..179ef1b55 100644 --- a/pixiu/pkg/client/dubbo/option_test.go +++ b/pixiu/pkg/client/dubbo/option_test.go @@ -84,14 +84,15 @@ func TestParamTypesOptAction(t *testing.T) { assert.Equal(t, "string", target.Types[1]) err = opt.Action(target, "object,whatsoever") - assert.EqualError(t, err, "Types invalid whatsoever") + assert.Nil(t, err) err = opt.Action("target", []string{}) - assert.EqualError(t, err, "The val type must be string") + assert.EqualError(t, err, "Target is not dubboTarget in target parameter") err = opt.Action(target, "object,") assert.Nil(t, err) assert.Equal(t, "object", target.Types[0]) - err = opt.Action(target, "object") + + err = opt.Action(target, "object ") assert.Nil(t, err) assert.Equal(t, "object", target.Types[0]) } diff --git a/cmd/pixiu/deployer.go b/pixiu/pkg/cmd/deployer.go similarity index 98% rename from cmd/pixiu/deployer.go rename to pixiu/pkg/cmd/deployer.go index a21bcddc1..ac166c43a 100644 --- a/cmd/pixiu/deployer.go +++ b/pixiu/pkg/cmd/deployer.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package main +package cmd type Deployer interface { initialize() error diff --git a/pixiu/pkg/cmd/gateway.go b/pixiu/pkg/cmd/gateway.go new file mode 100644 index 000000000..111a1d969 --- /dev/null +++ b/pixiu/pkg/cmd/gateway.go @@ -0,0 +1,201 @@ +/* + * 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 cmd + +import ( + "fmt" + "os" + "runtime" + "strconv" +) + +import ( + "github.com/spf13/cobra" +) + +import ( + "github.com/apache/dubbo-go-pixiu/pixiu/pkg/common/constant" + pxruntime "github.com/apache/dubbo-go-pixiu/pixiu/pkg/common/runtime" + "github.com/apache/dubbo-go-pixiu/pixiu/pkg/config" + "github.com/apache/dubbo-go-pixiu/pixiu/pkg/logger" + "github.com/apache/dubbo-go-pixiu/pixiu/pkg/model" + "github.com/apache/dubbo-go-pixiu/pixiu/pkg/server" +) + +var ( + flagToLogLevel = map[string]string{ + "trace": "TRACE", + "debug": "DEBUG", + "info": "INFO", + "warning": "WARN", + "error": "ERROR", + "critical": "FATAL", + } + + configPath string + apiConfigPath string + logConfigPath string + logLevel string + + // CURRENTLY USELESS + logFormat string + + limitCpus string + + // Currently default set to false, wait for up coming support + initFromRemote = false +) + +var ( + GatewayCmd = &cobra.Command{ + Use: "gateway", + Short: "Run dubbo go pixiu in gateway mode", + } + + deploy = &DefaultDeployer{ + configManger: config.NewConfigManger(), + } + + startGatewayCmd = &cobra.Command{ + Use: "start", + Short: "Start gateway", + PreRun: func(cmd *cobra.Command, args []string) { + initDefaultValue() + + err := deploy.initialize() + if err != nil { + panic(err) + } + }, + Run: func(cmd *cobra.Command, args []string) { + + err := deploy.start() + if err != nil { + panic(err) + } + }, + } +) + +// init Init startCmd +func init() { + startGatewayCmd.PersistentFlags().StringVarP(&configPath, constant.ConfigPathKey, "c", os.Getenv(constant.EnvDubbogoPixiuConfig), "Load configuration from `FILE`") + startGatewayCmd.PersistentFlags().StringVarP(&apiConfigPath, constant.ApiConfigPathKey, "a", os.Getenv(constant.EnvDubbogoPixiuApiConfig), "Load api configuration from `FILE`") + startGatewayCmd.PersistentFlags().StringVarP(&logConfigPath, constant.LogConfigPathKey, "g", os.Getenv(constant.EnvDubbogoPixiuLogConfig), "Load log configuration from `FILE`") + startGatewayCmd.PersistentFlags().StringVarP(&logLevel, constant.LogLevelKey, "l", os.Getenv(constant.EnvDubbogoPixiuLogLevel), "dubbogo pixiu log level, trace|debug|info|warning|error|critical") + startGatewayCmd.PersistentFlags().StringVarP(&limitCpus, constant.LimitCpusKey, "m", os.Getenv(constant.EnvDubbogoPixiuLimitCpus), "dubbogo pixiu schedule threads count") + startGatewayCmd.PersistentFlags().StringVarP(&logFormat, constant.LogFormatKey, "f", os.Getenv(constant.EnvDubbogoPixiuLogFormat), "dubbogo pixiu log format, currently useless") + + GatewayCmd.AddCommand(startGatewayCmd) +} + +type DefaultDeployer struct { + bootstrap *model.Bootstrap + configManger *config.ConfigManager +} + +func (d *DefaultDeployer) initialize() error { + err := initLog() + if err != nil { + logger.Warnf("[startGatewayCmd] failed to init logger, %s", err.Error()) + } + + // load Bootstrap config + d.bootstrap = d.configManger.LoadBootConfig(configPath) + + err = initLimitCpus() + if err != nil { + logger.Errorf("[startCmd] failed to get limit cpu number, %s", err.Error()) + } + + return err +} + +func (d *DefaultDeployer) start() error { + server.Start(d.bootstrap) + return nil +} + +func (d *DefaultDeployer) stop() error { + //TODO implement me + panic("implement me") +} + +// initDefaultValue If not set both in args and env, set default values +func initDefaultValue() { + if configPath == "" { + configPath = constant.DefaultConfigPath + } + + if apiConfigPath == "" { + apiConfigPath = constant.DefaultApiConfigPath + } + + if logConfigPath == "" { + logConfigPath = constant.DefaultLogConfigPath + } + + if logLevel == "" { + logLevel = constant.DefaultLogLevel + } + + if limitCpus == "" { + limitCpus = constant.DefaultLimitCpus + } + + if logFormat == "" { + logFormat = constant.DefaultLogFormat + } +} + +// initLog +func initLog() error { + err := logger.InitLog(logConfigPath) + if err != nil { + // cause `logger.InitLog` already handle init failed, so just use logger to log + return err + } + + if level, ok := flagToLogLevel[logLevel]; ok { + logger.SetLoggerLevel(level) + } else { + logger.SetLoggerLevel(flagToLogLevel[constant.DefaultLogLevel]) + return fmt.Errorf("logLevel is invalid, set log level to default: %s", constant.DefaultLogLevel) + } + return nil +} + +// initApiConfig return value of the bool is for the judgment of whether is a api meta data error, a kind of silly (?) +func initApiConfig() (*model.Bootstrap, error) { + bootstrap := config.Load(configPath) + return bootstrap, nil +} + +func initLimitCpus() error { + limitCpuNumberFromEnv, err := strconv.ParseInt(limitCpus, 10, 64) + if err != nil { + return err + } + limitCpuNumber := int(limitCpuNumberFromEnv) + if limitCpuNumber <= 0 { + limitCpuNumber = pxruntime.GetCPUNum() + } + runtime.GOMAXPROCS(limitCpuNumber) + logger.Infof("GOMAXPROCS set to %v", limitCpuNumber) + return nil +} diff --git a/cmd/pixiu/sidecar.go b/pixiu/pkg/cmd/sidecar.go similarity index 93% rename from cmd/pixiu/sidecar.go rename to pixiu/pkg/cmd/sidecar.go index 68d739ecd..e0260c2c4 100644 --- a/cmd/pixiu/sidecar.go +++ b/pixiu/pkg/cmd/sidecar.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package main +package cmd import ( "fmt" @@ -25,8 +25,12 @@ import ( "github.com/spf13/cobra" ) +func init() { + SideCarCmd.AddCommand(startSideCarCmd) +} + var ( - sideCarCmd = &cobra.Command{ + SideCarCmd = &cobra.Command{ Use: "sidecar", Short: "Run dubbo go pixiu in sidecar mode (implement in the future)", } @@ -39,7 +43,3 @@ var ( }, } ) - -func init() { - sideCarCmd.AddCommand(startSideCarCmd) -} diff --git a/pixiu/pkg/filter/failinject/config.go b/pixiu/pkg/filter/failinject/config.go index d6952fa86..cff0fce19 100644 --- a/pixiu/pkg/filter/failinject/config.go +++ b/pixiu/pkg/filter/failinject/config.go @@ -17,7 +17,9 @@ package failinject -import "time" +import ( + "time" +) type URI string diff --git a/pixiu/pkg/filter/failinject/filter.go b/pixiu/pkg/filter/failinject/filter.go index 8a1dcc77e..aff5c9ccb 100644 --- a/pixiu/pkg/filter/failinject/filter.go +++ b/pixiu/pkg/filter/failinject/filter.go @@ -20,7 +20,9 @@ package failinject import ( "math/rand" "time" +) +import ( "github.com/apache/dubbo-go-pixiu/pixiu/pkg/common/constant" "github.com/apache/dubbo-go-pixiu/pixiu/pkg/common/extension/filter" contextHttp "github.com/apache/dubbo-go-pixiu/pixiu/pkg/context/http"