code.vegaprotocol.io/vega@v0.79.0/datanode/config/config.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  //lint:file-ignore SA5008 duplicated struct tags are ok for config
    17  
    18  package config
    19  
    20  import (
    21  	"errors"
    22  	"fmt"
    23  	"os"
    24  
    25  	"code.vegaprotocol.io/vega/datanode/admin"
    26  	"code.vegaprotocol.io/vega/datanode/api"
    27  	"code.vegaprotocol.io/vega/datanode/broker"
    28  	"code.vegaprotocol.io/vega/datanode/candlesv2"
    29  	"code.vegaprotocol.io/vega/datanode/config/encoding"
    30  	"code.vegaprotocol.io/vega/datanode/gateway"
    31  	"code.vegaprotocol.io/vega/datanode/metrics"
    32  	"code.vegaprotocol.io/vega/datanode/networkhistory"
    33  	"code.vegaprotocol.io/vega/datanode/service"
    34  	"code.vegaprotocol.io/vega/datanode/sqlstore"
    35  	vgfs "code.vegaprotocol.io/vega/libs/fs"
    36  	"code.vegaprotocol.io/vega/libs/pprof"
    37  	"code.vegaprotocol.io/vega/logging"
    38  	"code.vegaprotocol.io/vega/paths"
    39  )
    40  
    41  // Config ties together all other application configuration types.
    42  type Config struct {
    43  	Admin     admin.Config     `group:"Admin"     namespace:"admin"`
    44  	API       api.Config       `group:"API"       namespace:"api"`
    45  	CandlesV2 candlesv2.Config `group:"CandlesV2" namespace:"candlesv2"`
    46  	Logging   logging.Config   `group:"Logging"   namespace:"logging"`
    47  	SQLStore  sqlstore.Config  `group:"Sqlstore"  namespace:"sqlstore"`
    48  	Gateway   gateway.Config   `group:"Gateway"   namespace:"gateway"`
    49  	Metrics   metrics.Config   `group:"Metrics"   namespace:"metrics"`
    50  	Broker    broker.Config    `group:"Broker"    namespace:"broker"`
    51  	Service   service.Config   `group:"Service"   namespace:"service"`
    52  
    53  	Pprof          pprof.Config  `group:"Pprof" namespace:"pprof"`
    54  	GatewayEnabled encoding.Bool `choice:"true" choice:"false"    description:" " long:"gateway-enabled"`
    55  
    56  	NetworkHistory                   networkhistory.Config `group:"NetworkHistory" namespace:"networkhistory"`
    57  	AutoInitialiseFromNetworkHistory encoding.Bool         `choice:"true"          choice:"false"             description:"if true the node will attempt to load the latest history segment(s) from network history if the node is empty" long:"auto-initialise"`
    58  
    59  	ChainID          string `long:"chainID"`
    60  	MaxMemoryPercent uint8  `description:"The maximum amount of memory reserved for the data node (default: 33%)" long:"max-memory-percent"`
    61  }
    62  
    63  // NewDefaultConfig returns a set of default configs for all vega packages, as specified at the per package
    64  // config level, if there is an error initialising any of the configs then this is returned.
    65  func NewDefaultConfig() Config {
    66  	return Config{
    67  		MaxMemoryPercent:                 33,
    68  		Admin:                            admin.NewDefaultConfig(),
    69  		API:                              api.NewDefaultConfig(),
    70  		CandlesV2:                        candlesv2.NewDefaultConfig(),
    71  		SQLStore:                         sqlstore.NewDefaultConfig(),
    72  		Pprof:                            pprof.NewDefaultConfig(),
    73  		Logging:                          logging.NewDefaultConfig(),
    74  		Gateway:                          gateway.NewDefaultConfig(),
    75  		Metrics:                          metrics.NewDefaultConfig(),
    76  		Broker:                           broker.NewDefaultConfig(),
    77  		Service:                          service.NewDefaultConfig(),
    78  		GatewayEnabled:                   true,
    79  		NetworkHistory:                   networkhistory.NewDefaultConfig(),
    80  		AutoInitialiseFromNetworkHistory: false,
    81  	}
    82  }
    83  
    84  func (c Config) GetMaxMemoryFactor() (float64, error) {
    85  	if c.MaxMemoryPercent <= 0 || c.MaxMemoryPercent >= 100 {
    86  		return 0, errors.New("MaxMemoryPercent is out of range, expect > 0 and < 100")
    87  	}
    88  
    89  	return float64(c.MaxMemoryPercent) / 100., nil
    90  }
    91  
    92  type Loader struct {
    93  	configFilePath string
    94  }
    95  
    96  func InitialiseLoader(vegaPaths paths.Paths) (*Loader, error) {
    97  	configFilePath, err := vegaPaths.CreateConfigPathFor(paths.DataNodeDefaultConfigFile)
    98  	if err != nil {
    99  		return nil, fmt.Errorf("couldn't get path for %s: %w", paths.NodeDefaultConfigFile, err)
   100  	}
   101  
   102  	return &Loader{
   103  		configFilePath: configFilePath,
   104  	}, nil
   105  }
   106  
   107  func (l *Loader) ConfigFilePath() string {
   108  	return l.configFilePath
   109  }
   110  
   111  func (l *Loader) ConfigExists() (bool, error) {
   112  	exists, err := vgfs.FileExists(l.configFilePath)
   113  	if err != nil {
   114  		return false, err
   115  	}
   116  	return exists, nil
   117  }
   118  
   119  func (l *Loader) Save(cfg *Config) error {
   120  	if err := paths.WriteStructuredFile(l.configFilePath, cfg); err != nil {
   121  		return fmt.Errorf("couldn't write configuration file at %s: %w", l.configFilePath, err)
   122  	}
   123  	return nil
   124  }
   125  
   126  func (l *Loader) Get() (*Config, error) {
   127  	cfg := NewDefaultConfig()
   128  	if err := paths.ReadStructuredFile(l.configFilePath, &cfg); err != nil {
   129  		return nil, fmt.Errorf("couldn't read configuration file at %s: %w", l.configFilePath, err)
   130  	}
   131  	return &cfg, nil
   132  }
   133  
   134  func (l *Loader) Remove() {
   135  	_ = os.RemoveAll(l.configFilePath)
   136  }