github.com/matrixorigin/matrixone@v1.2.0/pkg/proxy/config.go (about)

     1  // Copyright 2021 - 2023 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package proxy
    16  
    17  import (
    18  	"context"
    19  	"time"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    22  	"github.com/matrixorigin/matrixone/pkg/common/runtime"
    23  	"github.com/matrixorigin/matrixone/pkg/logservice"
    24  	logservicepb "github.com/matrixorigin/matrixone/pkg/pb/logservice"
    25  	"github.com/matrixorigin/matrixone/pkg/util"
    26  	"github.com/matrixorigin/matrixone/pkg/util/errutil"
    27  	"github.com/matrixorigin/matrixone/pkg/util/toml"
    28  )
    29  
    30  var (
    31  	// The default value of proxy service.
    32  	defaultListenAddress = "127.0.0.1:6009"
    33  	// The default value of refresh interval to HAKeeper.
    34  	defaultRefreshInterval = 5 * time.Second
    35  	// The default value of rebalance interval.
    36  	defaultRebalanceInterval = 10 * time.Second
    37  	// The default value of rebalnce tolerance.
    38  	defaultRebalanceTolerance = 0.3
    39  	// The default value of rebalance policy.
    40  	defaultRebalancePolicy = "active"
    41  	// The default value of heartbeat interval.
    42  	defaultHeartbeatInterval = time.Second * 3
    43  	// The default value of heartbeat timeout.
    44  	defaultHeartbeatTimeout = time.Second * 3
    45  	// The default value of connect timeout.
    46  	defaultConnectTimeout = time.Second * 3
    47  	// The default value of handshake auth timeout.
    48  	defaultAuthTimeout = time.Second * 10
    49  	// The default value of TSL connect timeout.
    50  	defaultTLSConnectTimeout = time.Second * 10
    51  )
    52  
    53  type RebalancePolicy int
    54  
    55  const (
    56  	RebalancePolicyActive RebalancePolicy = iota
    57  	RebalancePolicyPassive
    58  )
    59  
    60  var RebalancePolicyMapping = map[string]RebalancePolicy{
    61  	"active":  RebalancePolicyActive,
    62  	"passive": RebalancePolicyPassive,
    63  }
    64  
    65  // Config is the configuration of proxy server.
    66  type Config struct {
    67  	UUID          string `toml:"uuid"`
    68  	ListenAddress string `toml:"listen-address" user_setting:"basic"`
    69  	// RebalanceInterval is the interval between two rebalance operations.
    70  	RebalanceInterval toml.Duration `toml:"rebalance-interval" user_setting:"advanced"`
    71  	// RebalanceDisabled indicates that the rebalancer is disabled.
    72  	RebalanceDisabled bool `toml:"rebalance-disabled" user_setting:"advanced"`
    73  	// RebalanceTolerance indicates the rebalancer's tolerance.
    74  	// Connections above the avg*(1+tolerance) will be migrated to
    75  	// other CN servers. This value should be less than 1.
    76  	RebalanceTolerance float64 `toml:"rebalance-tolerance" user_setting:"advanced"`
    77  	// RebalancePolicy indicates that the rebalance policy, which could be active or
    78  	// passive currently. Active means that the connection transfer will be more proactive
    79  	// to make sure the sessions are balanced in all CN servers. Default value is "active".
    80  	RebalancePolicy string `toml:"rebalance-proactive" user_setting:"advanced"`
    81  	// ConnectTimeout is the timeout duration when proxy connects to backend
    82  	// CN servers. If proxy connects to cn timeout, it will return a retryable
    83  	// error and try to connect to other cn servers.
    84  	ConnectTimeout toml.Duration `toml:"connect-timeout" user_setting:"advanced"`
    85  	// AuthTimeout is the timeout duration when proxy handshakes with backend
    86  	// CN servers. If proxy handshakes with cn timeout, it will return a retryable
    87  	// error and try to connect to other cn servers.
    88  	AuthTimeout toml.Duration `toml:"auth-timeout" user_setting:"advanced"`
    89  	// TLSConnectTimeout is the timeout duration when TLS connect to server.
    90  	TLSConnectTimeout toml.Duration `toml:"tls-connect-timeout" user_setting:"advanced"`
    91  
    92  	// Default is false. With true. Server will support tls.
    93  	// This value should be ths same with all CN servers, and the name
    94  	// of this parameter is enableTls.
    95  	TLSEnabled bool `toml:"tls-enabled" user_setting:"advanced"`
    96  	// TSLCAFile is the file path of file that contains list of trusted
    97  	// SSL CAs for client.
    98  	TLSCAFile string `toml:"tls-ca-file" user_setting:"advanced"`
    99  	// TLSCertFile is the file path of file that contains X509 certificate
   100  	// in PEM format for client.
   101  	TLSCertFile string `toml:"tls-cert-file" user_setting:"advanced"`
   102  	// TLSKeyFile is the file path of file that contains X509 key in PEM
   103  	// format for client.
   104  	TLSKeyFile string `toml:"tls-key-file" user_setting:"advanced"`
   105  	// InternalCIDRs is the config which indicates that the CIDR list of
   106  	// internal network. The addresses outside the range are external
   107  	// addresses.
   108  	InternalCIDRs []string `toml:"internal-cidrs"`
   109  
   110  	// HAKeeper is the configuration of HAKeeper.
   111  	HAKeeper struct {
   112  		// ClientConfig is HAKeeper client configuration.
   113  		ClientConfig logservice.HAKeeperClientConfig
   114  		// HeartbeatInterval heartbeat interval to send message to HAKeeper. Default is 1s.
   115  		HeartbeatInterval toml.Duration `toml:"hakeeper-heartbeat-interval"`
   116  		// HeartbeatTimeout heartbeat request timeout. Default is 3s.
   117  		HeartbeatTimeout toml.Duration `toml:"hakeeper-heartbeat-timeout"`
   118  	}
   119  	// Cluster is the configuration of MO Cluster.
   120  	Cluster struct {
   121  		// RefreshInterval refresh cluster info from hakeeper interval
   122  		RefreshInterval toml.Duration `toml:"refresh-interval"`
   123  	}
   124  	// Plugin specifies an optional proxy plugin backend
   125  	//
   126  	// NB: the connection between proxy and plugin is assumed to be stable, external orchestrators
   127  	// are responsible for ensuring the stability of rpc tunnels, for example, by deploying proxy and
   128  	// plugin in a same machine and communicate through local loopback address
   129  	Plugin *PluginConfig `toml:"plugin"`
   130  }
   131  
   132  type PluginConfig struct {
   133  	// Backend is the plugin backend URL
   134  	Backend string `toml:"backend"`
   135  	// Timeout is the rpc timeout when communicate with the plugin backend
   136  	Timeout time.Duration `toml:"timeout"`
   137  }
   138  
   139  // Option is used to set up configuration.
   140  type Option func(*Server)
   141  
   142  // WithRuntime sets the runtime of proxy server.
   143  func WithRuntime(runtime runtime.Runtime) Option {
   144  	return func(s *Server) {
   145  		s.runtime = runtime
   146  	}
   147  }
   148  
   149  // WithTLSEnabled enable the TLS.
   150  func WithTLSEnabled() Option {
   151  	return func(s *Server) {
   152  		s.config.TLSEnabled = true
   153  	}
   154  }
   155  
   156  // WithTLSCAFile sets the CA file.
   157  func WithTLSCAFile(f string) Option {
   158  	return func(s *Server) {
   159  		s.config.TLSCAFile = f
   160  	}
   161  }
   162  
   163  // WithTLSCertFile sets the cert file.
   164  func WithTLSCertFile(f string) Option {
   165  	return func(s *Server) {
   166  		s.config.TLSCertFile = f
   167  	}
   168  }
   169  
   170  // WithTLSKeyFile sets the key file.
   171  func WithTLSKeyFile(f string) Option {
   172  	return func(s *Server) {
   173  		s.config.TLSKeyFile = f
   174  	}
   175  }
   176  
   177  // WithConfigData saves the data from the config file
   178  func WithConfigData(data map[string]*logservicepb.ConfigItem) Option {
   179  	return func(s *Server) {
   180  		s.configData = util.NewConfigData(data)
   181  	}
   182  }
   183  
   184  // FillDefault fill the default config values of proxy server.
   185  func (c *Config) FillDefault() {
   186  	if c.ListenAddress == "" {
   187  		c.ListenAddress = defaultListenAddress
   188  	}
   189  	if c.ConnectTimeout.Duration == 0 {
   190  		c.ConnectTimeout.Duration = defaultConnectTimeout
   191  	}
   192  	if c.AuthTimeout.Duration == 0 {
   193  		c.AuthTimeout.Duration = defaultAuthTimeout
   194  	}
   195  	if c.TLSConnectTimeout.Duration == 0 {
   196  		c.TLSConnectTimeout.Duration = defaultTLSConnectTimeout
   197  	}
   198  	if c.RebalanceInterval.Duration == 0 {
   199  		c.RebalanceInterval.Duration = defaultRebalanceInterval
   200  	}
   201  	if c.Cluster.RefreshInterval.Duration == 0 {
   202  		c.Cluster.RefreshInterval.Duration = defaultRefreshInterval
   203  	}
   204  	if c.RebalanceTolerance == 0 {
   205  		c.RebalanceTolerance = defaultRebalanceTolerance
   206  	}
   207  	if c.RebalancePolicy == "" {
   208  		c.RebalancePolicy = defaultRebalancePolicy
   209  	}
   210  	if c.Plugin != nil {
   211  		if c.Plugin.Timeout == 0 {
   212  			c.Plugin.Timeout = time.Second
   213  		}
   214  	}
   215  	if c.HAKeeper.HeartbeatInterval.Duration == 0 {
   216  		c.HAKeeper.HeartbeatInterval.Duration = defaultHeartbeatInterval
   217  	}
   218  	if c.HAKeeper.HeartbeatTimeout.Duration == 0 {
   219  		c.HAKeeper.HeartbeatTimeout.Duration = defaultHeartbeatTimeout
   220  	}
   221  }
   222  
   223  // Validate validates the configuration of proxy server.
   224  func (c *Config) Validate() error {
   225  	noReport := errutil.ContextWithNoReport(context.Background(), true)
   226  	if c.Plugin != nil {
   227  		if c.Plugin.Backend == "" {
   228  			return moerr.NewInternalError(noReport, "proxy plugin backend must be set")
   229  		}
   230  		if c.Plugin.Timeout == 0 {
   231  			return moerr.NewInternalError(noReport, "proxy plugin backend timeout must be set")
   232  		}
   233  	}
   234  	if _, ok := RebalancePolicyMapping[c.RebalancePolicy]; !ok {
   235  		c.RebalancePolicy = defaultRebalancePolicy
   236  	}
   237  	return nil
   238  }
   239  
   240  func dumpProxyConfig(cfg Config) (map[string]*logservicepb.ConfigItem, error) {
   241  	defCfg := Config{}
   242  	return util.DumpConfig(cfg, defCfg)
   243  }