code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/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  package sqlstore
    17  
    18  import (
    19  	"fmt"
    20  	"time"
    21  
    22  	"code.vegaprotocol.io/vega/datanode/config/encoding"
    23  	"code.vegaprotocol.io/vega/logging"
    24  
    25  	"github.com/jackc/pgx/v4/pgxpool"
    26  )
    27  
    28  type RetentionPeriod string
    29  
    30  const (
    31  	RetentionPeriodStandard RetentionPeriod = "standard"
    32  	RetentionPeriodArchive  RetentionPeriod = "forever"
    33  	RetentionPeriodLite     RetentionPeriod = "1 day"
    34  )
    35  
    36  type Config struct {
    37  	ConnectionConfig                                   ConnectionConfig      `group:"ConnectionConfig"                                                                          namespace:"ConnectionConfig"`
    38  	WipeOnStartup                                      encoding.Bool         `description:"deprecated, use data-node unsafe_reset_all command instead"                          long:"wipe-on-startup"`
    39  	Level                                              encoding.LogLevel     `long:"log-level"`
    40  	UseEmbedded                                        encoding.Bool         `description:"Use an embedded version of Postgresql for the SQL data store"                        long:"use-embedded"`
    41  	FanOutBufferSize                                   int                   `description:"buffer size used by the fan out event source"                                        long:"fan-out-buffer-size"`
    42  	RetentionPolicies                                  []RetentionPolicy     `group:"RetentionPolicies"                                                                         namespace:"RetentionPolicies"`
    43  	ConnectionRetryConfig                              ConnectionRetryConfig `group:"ConnectionRetryConfig"                                                                     namespace:"ConnectionRetryConfig"`
    44  	LogRotationConfig                                  LogRotationConfig     `group:"LogRotationConfig"                                                                         namespace:"LogRotationConfig"`
    45  	DisableMinRetentionPolicyCheckForUseInSysTestsOnly encoding.Bool         `description:"Disables the minimum retention policy interval check - only for use in system tests" long:"disable-min-retention-policy-use-in-sys-test-only"`
    46  	RetentionPeriod                                    RetentionPeriod       `description:"Set the retention level for the database. standard, archive, or lite"                long:"retention-period"`
    47  	VerboseMigration                                   encoding.Bool         `description:"Enable verbose logging of SQL migrations"                                            long:"verbose-migration"`
    48  	ChunkIntervals                                     []ChunkInterval       `group:"ChunkIntervals"                                                                            namespace:"ChunkIntervals"`
    49  }
    50  
    51  type ConnectionConfig struct {
    52  	Host                  string            `long:"host"`
    53  	Port                  int               `long:"port"`
    54  	Username              string            `long:"username"`
    55  	Password              string            `long:"password"`
    56  	Database              string            `long:"database"`
    57  	SocketDir             string            `description:"location of postgres UNIX socket directory (used if host is empty string)" long:"socket-dir"`
    58  	MaxConnLifetime       encoding.Duration `long:"max-conn-lifetime"`
    59  	MaxConnLifetimeJitter encoding.Duration `long:"max-conn-lifetime-jitter"`
    60  	MaxConnPoolSize       int               `long:"max-conn-pool-size"`
    61  	MinConnPoolSize       int32             `long:"min-conn-pool-size"`
    62  	RuntimeParams         map[string]string `long:"runtime-params"`
    63  }
    64  
    65  type HypertableOverride interface {
    66  	RetentionPolicy | ChunkInterval
    67  	EntityName() string
    68  }
    69  
    70  type RetentionPolicy struct {
    71  	HypertableOrCaggName string `description:"the name of the hyper table of continuous aggregate (cagg) to which this policy applies"                          string:"hypertable-or-cagg-name"`
    72  	DataRetentionPeriod  string `description:"the period to retain data, e.g '3 days', '3 months', '1 year' etc. To retain data indefinitely specify 'forever'" string:"interval"`
    73  }
    74  
    75  func (p RetentionPolicy) EntityName() string {
    76  	return p.HypertableOrCaggName
    77  }
    78  
    79  type ChunkInterval struct {
    80  	HypertableOrCaggName string `description:"the name of the hyper table of continuous aggregate (cagg) to which this policy applies" string:"hypertable-or-cagg-name"`
    81  	ChunkInterval        string `description:"the interval at which to create new chunks, e.g '1 day', '1 month', '1 year' etc."       string:"chunk-interval"`
    82  }
    83  
    84  func (p ChunkInterval) EntityName() string {
    85  	return p.HypertableOrCaggName
    86  }
    87  
    88  type ConnectionRetryConfig struct {
    89  	MaxRetries      uint64        `description:"the maximum number of times to retry connecting to the database" long:"max-retries"`
    90  	InitialInterval time.Duration `description:"the initial interval to wait before retrying"                    long:"initial-interval"`
    91  	MaxInterval     time.Duration `description:"the maximum interval to wait before retrying"                    long:"max-interval"`
    92  	MaxElapsedTime  time.Duration `description:"the maximum elapsed time to wait before giving up"               long:"max-elapsed-time"`
    93  }
    94  
    95  type LogRotationConfig struct {
    96  	MaxSize int `description:"the maximum size of the log file in bytes"       long:"max-size"`
    97  	MaxAge  int `description:"the maximum number of days to keep the log file" long:"max-age"`
    98  }
    99  
   100  func (conf ConnectionConfig) GetConnectionString() string {
   101  	return conf.getConnectionStringForDatabase(conf.Database)
   102  }
   103  
   104  func (conf ConnectionConfig) getConnectionStringForDatabase(database string) string {
   105  	if conf.Host == "" {
   106  		//nolint:nosprintfhostport
   107  		return fmt.Sprintf("postgresql://%s:%s@/%s?host=%s&port=%d",
   108  			conf.Username,
   109  			conf.Password,
   110  			database,
   111  			conf.SocketDir,
   112  			conf.Port)
   113  	}
   114  	//nolint:nosprintfhostport
   115  	return fmt.Sprintf("postgresql://%s:%s@%s:%d/%s",
   116  		conf.Username,
   117  		conf.Password,
   118  		conf.Host,
   119  		conf.Port,
   120  		database)
   121  }
   122  
   123  func (conf ConnectionConfig) GetConnectionStringForPostgresDatabase() string {
   124  	return conf.getConnectionStringForDatabase("postgres")
   125  }
   126  
   127  func (conf ConnectionConfig) GetPoolConfig() (*pgxpool.Config, error) {
   128  	cfg, err := pgxpool.ParseConfig(conf.GetConnectionString())
   129  	if err != nil {
   130  		return nil, err
   131  	}
   132  	cfg.MaxConnLifetime = conf.MaxConnLifetime.Duration
   133  	cfg.MaxConnLifetimeJitter = conf.MaxConnLifetimeJitter.Duration
   134  
   135  	cfg.ConnConfig.RuntimeParams["application_name"] = "Vega Data Node"
   136  	for paramKey, paramValue := range conf.RuntimeParams {
   137  		cfg.ConnConfig.RuntimeParams[paramKey] = paramValue
   138  	}
   139  	return cfg, nil
   140  }
   141  
   142  func NewDefaultConfig() Config {
   143  	return Config{
   144  		ConnectionConfig: ConnectionConfig{
   145  			Host:                  "localhost",
   146  			Port:                  5432,
   147  			Username:              "vega",
   148  			Password:              "vega",
   149  			Database:              "vega",
   150  			SocketDir:             "/tmp",
   151  			MaxConnLifetime:       encoding.Duration{Duration: time.Minute * 30},
   152  			MaxConnLifetimeJitter: encoding.Duration{Duration: time.Minute * 5},
   153  			RuntimeParams:         map[string]string{},
   154  		},
   155  		Level:            encoding.LogLevel{Level: logging.InfoLevel},
   156  		UseEmbedded:      false,
   157  		FanOutBufferSize: 1000,
   158  		DisableMinRetentionPolicyCheckForUseInSysTestsOnly: false,
   159  		ConnectionRetryConfig: ConnectionRetryConfig{
   160  			MaxRetries:      10,
   161  			InitialInterval: time.Second,
   162  			MaxInterval:     time.Second * 10,
   163  			MaxElapsedTime:  time.Minute,
   164  		},
   165  		LogRotationConfig: LogRotationConfig{
   166  			MaxSize: 100,
   167  			MaxAge:  2,
   168  		},
   169  		RetentionPeriod:  RetentionPeriodStandard,
   170  		VerboseMigration: false,
   171  	}
   172  }