github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/executor/config.go (about)

     1  // Copyright 2022 PingCAP, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package executor
    15  
    16  import (
    17  	"bytes"
    18  	"encoding/json"
    19  	"fmt"
    20  	"strings"
    21  	"time"
    22  
    23  	"github.com/BurntSushi/toml"
    24  	"github.com/pingcap/log"
    25  	"github.com/pingcap/tiflow/pkg/errors"
    26  	"github.com/pingcap/tiflow/pkg/label"
    27  	"github.com/pingcap/tiflow/pkg/logutil"
    28  	"github.com/pingcap/tiflow/pkg/security"
    29  )
    30  
    31  var (
    32  	defaultJoinAddr          = "127.0.0.1:10240"
    33  	defaultKeepAliveTTL      = "20s"
    34  	defaultKeepAliveInterval = "500ms"
    35  	defaultRPCTimeout        = "3s"
    36  	defaultMetricInterval    = 15 * time.Second
    37  	defaultExecutorAddr      = "127.0.0.1:10340"
    38  )
    39  
    40  // Config is the configuration.
    41  type Config struct {
    42  	Name string `toml:"name" json:"name"`
    43  
    44  	LogConf logutil.Config `toml:"log" json:"log"`
    45  
    46  	Join          string `toml:"join" json:"join" `
    47  	Addr          string `toml:"addr" json:"addr"`
    48  	AdvertiseAddr string `toml:"advertise-addr" json:"advertise-addr"`
    49  
    50  	Labels map[string]string `toml:"labels" json:"labels"`
    51  	// EnableGCTuning enables a GC tuning mechanism that adjusts the GC frequency
    52  	// according to the used memory with reference to the total memory. It can be
    53  	// enabled when the executor can consume almost all the memory of the
    54  	// container/machine.
    55  	EnableGCTuning bool `toml:"enable-gc-tuning" json:"enable-gc-tuning"`
    56  
    57  	// TODO: in the future executors should share a same ttl from server-master
    58  	KeepAliveTTLStr      string `toml:"keepalive-ttl" json:"keepalive-ttl"`
    59  	KeepAliveIntervalStr string `toml:"keepalive-interval" json:"keepalive-interval"`
    60  	RPCTimeoutStr        string `toml:"rpc-timeout" json:"rpc-timeout"`
    61  
    62  	KeepAliveTTL      time.Duration `toml:"-" json:"-"`
    63  	KeepAliveInterval time.Duration `toml:"-" json:"-"`
    64  	RPCTimeout        time.Duration `toml:"-" json:"-"`
    65  
    66  	Security *security.Credential `toml:"security" json:"security"`
    67  }
    68  
    69  // String implements fmt.Stringer
    70  func (c *Config) String() string {
    71  	cfg, err := json.Marshal(c)
    72  	if err != nil {
    73  		log.Error("fail to marshal config to json", logutil.ShortError(err))
    74  	}
    75  	return string(cfg)
    76  }
    77  
    78  // Toml returns TOML format representation of config.
    79  func (c *Config) Toml() (string, error) {
    80  	var b bytes.Buffer
    81  
    82  	err := toml.NewEncoder(&b).Encode(c)
    83  	if err != nil {
    84  		log.Error("fail to marshal config to toml", logutil.ShortError(err))
    85  	}
    86  
    87  	return b.String(), nil
    88  }
    89  
    90  // configFromFile loads config from file and merges items into Config.
    91  func (c *Config) configFromFile(path string) error {
    92  	metaData, err := toml.DecodeFile(path, c)
    93  	if err != nil {
    94  		return errors.WrapError(errors.ErrExecutorDecodeConfigFile, err)
    95  	}
    96  	undecoded := metaData.Undecoded()
    97  	if len(undecoded) > 0 && err == nil {
    98  		var undecodedItems []string
    99  		for _, item := range undecoded {
   100  			undecodedItems = append(undecodedItems, item.String())
   101  		}
   102  		return errors.ErrExecutorConfigUnknownItem.GenWithStackByArgs(strings.Join(undecodedItems, ","))
   103  	}
   104  	return nil
   105  }
   106  
   107  // Adjust adjusts the executor configuration
   108  func (c *Config) Adjust() (err error) {
   109  	if c.AdvertiseAddr == "" {
   110  		c.AdvertiseAddr = c.Addr
   111  	}
   112  
   113  	if c.Name == "" {
   114  		c.Name = fmt.Sprintf("executor-%s", c.AdvertiseAddr)
   115  	}
   116  
   117  	c.KeepAliveInterval, err = time.ParseDuration(c.KeepAliveIntervalStr)
   118  	if err != nil {
   119  		return
   120  	}
   121  
   122  	c.KeepAliveTTL, err = time.ParseDuration(c.KeepAliveTTLStr)
   123  	if err != nil {
   124  		return
   125  	}
   126  
   127  	c.RPCTimeout, err = time.ParseDuration(c.RPCTimeoutStr)
   128  	if err != nil {
   129  		return
   130  	}
   131  
   132  	if _, err := label.NewSetFromMap(c.Labels); err != nil {
   133  		return err
   134  	}
   135  
   136  	return nil
   137  }
   138  
   139  // GetDefaultExecutorConfig returns a default executor config
   140  func GetDefaultExecutorConfig() *Config {
   141  	return &Config{
   142  		LogConf: logutil.Config{
   143  			Level: "info",
   144  			File:  "",
   145  		},
   146  		Name:                 "",
   147  		Join:                 defaultJoinAddr,
   148  		Addr:                 defaultExecutorAddr,
   149  		AdvertiseAddr:        "",
   150  		EnableGCTuning:       true, // currently 1 container 1 executor
   151  		KeepAliveTTLStr:      defaultKeepAliveTTL,
   152  		KeepAliveIntervalStr: defaultKeepAliveInterval,
   153  		RPCTimeoutStr:        defaultRPCTimeout,
   154  	}
   155  }