code.vegaprotocol.io/vega@v0.79.0/cmd/data-node/commands/start.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  package commands
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  	"runtime/debug"
    22  
    23  	"code.vegaprotocol.io/vega/cmd/data-node/commands/start"
    24  	"code.vegaprotocol.io/vega/datanode/config"
    25  	"code.vegaprotocol.io/vega/libs/memory"
    26  	"code.vegaprotocol.io/vega/logging"
    27  	"code.vegaprotocol.io/vega/paths"
    28  	"code.vegaprotocol.io/vega/version"
    29  
    30  	"github.com/jessevdk/go-flags"
    31  )
    32  
    33  type StartCmd struct {
    34  	config.VegaHomeFlag
    35  
    36  	config.Config
    37  }
    38  
    39  var startCmd StartCmd
    40  
    41  const namedLogger = "datanode"
    42  
    43  func (cmd *StartCmd) Execute(args []string) error {
    44  	ctx, cfunc := context.WithCancel(context.Background())
    45  	defer cfunc()
    46  	log := logging.NewLoggerFromConfig(
    47  		logging.NewDefaultConfig()).Named(namedLogger)
    48  	defer log.AtExit()
    49  
    50  	// we define this option to parse the cli args each time the config is
    51  	// loaded. So that we can respect the cli flag precedence.
    52  	parseFlagOpt := func(cfg *config.Config) error {
    53  		_, err := flags.NewParser(cfg, flags.Default|flags.IgnoreUnknown).Parse()
    54  		return err
    55  	}
    56  
    57  	vegaPaths := paths.New(cmd.VegaHome)
    58  
    59  	configWatcher, err := config.NewWatcher(context.Background(), log, vegaPaths, config.Use(parseFlagOpt))
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	// setup max memory usage
    65  	memFactor, err := configWatcher.Get().GetMaxMemoryFactor()
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	// only set max memory if user didn't require 100%
    71  	if memFactor != 1 {
    72  		totalMem, err := memory.TotalMemory()
    73  		if err != nil {
    74  			return fmt.Errorf("failed to get total memory: %w", err)
    75  		}
    76  		debug.SetMemoryLimit(int64(float64(totalMem) * memFactor))
    77  	}
    78  
    79  	return (&start.NodeCommand{
    80  		Log:         log,
    81  		Version:     version.Get(),
    82  		VersionHash: version.GetCommitHash(),
    83  	}).Run(
    84  		ctx,
    85  		configWatcher,
    86  		vegaPaths,
    87  		args,
    88  	)
    89  }
    90  
    91  func Node(ctx context.Context, parser *flags.Parser) error {
    92  	startCmd = StartCmd{
    93  		Config: config.NewDefaultConfig(),
    94  	}
    95  	cmd, err := parser.AddCommand("node", "deprecated, see data-node start instead", "deprecated, see data-node start instead", &startCmd)
    96  	if err != nil {
    97  		return err
    98  	}
    99  
   100  	// Print nested groups under parent's name using `::` as the separator.
   101  	for _, parent := range cmd.Groups() {
   102  		for _, grp := range parent.Groups() {
   103  			grp.ShortDescription = parent.ShortDescription + "::" + grp.ShortDescription
   104  		}
   105  	}
   106  	return nil
   107  }
   108  
   109  func Start(_ context.Context, parser *flags.Parser) error {
   110  	startCmd = StartCmd{
   111  		Config: config.NewDefaultConfig(),
   112  	}
   113  	cmd, err := parser.AddCommand("start", "Start a vega data node", "Start a vega data node as defined by the config files", &startCmd)
   114  	if err != nil {
   115  		return err
   116  	}
   117  
   118  	// Print nested groups under parent's name using `::` as the separator.
   119  	for _, parent := range cmd.Groups() {
   120  		for _, grp := range parent.Groups() {
   121  			grp.ShortDescription = parent.ShortDescription + "::" + grp.ShortDescription
   122  		}
   123  	}
   124  	return nil
   125  }