|
| 1 | +package tsdb |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/cortexproject/cortex/pkg/util/tls" |
| 8 | + "github.com/pkg/errors" |
| 9 | + "github.com/thanos-io/thanos/pkg/cacheutil" |
| 10 | +) |
| 11 | + |
| 12 | +type RedisClientConfig struct { |
| 13 | + // Addr specifies the addresses of redis server. |
| 14 | + Addr string `yaml:"addr"` |
| 15 | + |
| 16 | + // Use the specified Username to authenticate the current connection |
| 17 | + // with one of the connections defined in the ACL list when connecting |
| 18 | + // to a Redis 6.0 instance, or greater, that is using the Redis ACL system. |
| 19 | + Username string `yaml:"username"` |
| 20 | + // Optional password. Must match the password specified in the |
| 21 | + // requirepass server configuration option (if connecting to a Redis 5.0 instance, or lower), |
| 22 | + // or the User Password when connecting to a Redis 6.0 instance, or greater, |
| 23 | + // that is using the Redis ACL system. |
| 24 | + Password string `yaml:"password"` |
| 25 | + |
| 26 | + // DB Database to be selected after connecting to the server. |
| 27 | + DB int `yaml:"db"` |
| 28 | + |
| 29 | + // DialTimeout specifies the client dial timeout. |
| 30 | + DialTimeout time.Duration `yaml:"dial_timeout"` |
| 31 | + |
| 32 | + // ReadTimeout specifies the client read timeout. |
| 33 | + ReadTimeout time.Duration `yaml:"read_timeout"` |
| 34 | + |
| 35 | + // WriteTimeout specifies the client write timeout. |
| 36 | + WriteTimeout time.Duration `yaml:"write_timeout"` |
| 37 | + |
| 38 | + // Maximum number of socket connections. |
| 39 | + PoolSize int `yaml:"pool_size"` |
| 40 | + |
| 41 | + // MinIdleConns specifies the minimum number of idle connections which is useful when establishing |
| 42 | + // new connection is slow. |
| 43 | + MinIdleConns int `yaml:"min_idle_conns"` |
| 44 | + |
| 45 | + // Amount of time after which client closes idle connections. |
| 46 | + // Should be less than server's timeout. |
| 47 | + // -1 disables idle timeout check. |
| 48 | + IdleTimeout time.Duration `yaml:"idle_timeout"` |
| 49 | + |
| 50 | + // Connection age at which client retires (closes) the connection. |
| 51 | + // Default 0 is to not close aged connections. |
| 52 | + MaxConnAge time.Duration `yaml:"max_conn_age"` |
| 53 | + |
| 54 | + // MaxGetMultiConcurrency specifies the maximum number of concurrent GetMulti() operations. |
| 55 | + // If set to 0, concurrency is unlimited. |
| 56 | + MaxGetMultiConcurrency int `yaml:"max_get_multi_concurrency"` |
| 57 | + |
| 58 | + // GetMultiBatchSize specifies the maximum size per batch for mget. |
| 59 | + GetMultiBatchSize int `yaml:"get_multi_batch_size"` |
| 60 | + |
| 61 | + // MaxSetMultiConcurrency specifies the maximum number of concurrent SetMulti() operations. |
| 62 | + // If set to 0, concurrency is unlimited. |
| 63 | + MaxSetMultiConcurrency int `yaml:"max_set_multi_concurrency"` |
| 64 | + |
| 65 | + // SetMultiBatchSize specifies the maximum size per batch for pipeline set. |
| 66 | + SetMultiBatchSize int `yaml:"set_multi_batch_size"` |
| 67 | + |
| 68 | + // If not zero then client-side caching is enabled. |
| 69 | + // Client-side caching is when data is stored in memory |
| 70 | + // instead of fetching data each time. |
| 71 | + // See https://redis.io/docs/manual/client-side-caching/ for info. |
| 72 | + CacheSize int `yaml:"cache_size"` |
| 73 | + |
| 74 | + TLSEnabled bool `yaml:"tls_enabled"` |
| 75 | + TLS tls.ClientConfig `yaml:",inline"` |
| 76 | +} |
| 77 | + |
| 78 | +func (cfg *RedisClientConfig) RegisterFlagsWithPrefix(f *flag.FlagSet, prefix string) { |
| 79 | + f.StringVar(&cfg.Addr, prefix+"addr", "", "Comma separated list of redis addresses. Supported prefixes are: dns+ (looked up as an A/AAAA query), dnssrv+ (looked up as a SRV query, dnssrvnoa+ (looked up as a SRV query, with no A/AAAA lookup made after that).") |
| 80 | + f.StringVar(&cfg.Username, prefix+"username", "", "The socket read/write timeout.") |
| 81 | + f.StringVar(&cfg.Password, prefix+"password", "", "The socket read/write timeout.") |
| 82 | + f.IntVar(&cfg.DB, prefix+"db", 0, "Database to be selected after connecting to the server.") |
| 83 | + f.DurationVar(&cfg.DialTimeout, prefix+"dial-timeout", time.Second*5, "Client dial timeout.") |
| 84 | + f.DurationVar(&cfg.ReadTimeout, prefix+"read-timeout", time.Second*3, "Client read timeout.") |
| 85 | + f.DurationVar(&cfg.WriteTimeout, prefix+"write-timeout", time.Second*3, "Client write timeout.") |
| 86 | + f.DurationVar(&cfg.IdleTimeout, prefix+"idle-timeout", time.Minute*5, "The socket read/write timeout.") |
| 87 | + f.DurationVar(&cfg.MaxConnAge, prefix+"max-conn-age", 0, "The socket read/write timeout.") |
| 88 | + f.IntVar(&cfg.PoolSize, prefix+"pool-size", 100, "Maximum number of socket connections.") |
| 89 | + f.IntVar(&cfg.MinIdleConns, prefix+"min-idle-conns", 10, "Specifies the minimum number of idle connections, which is useful when it is slow to establish new connections.") |
| 90 | + f.IntVar(&cfg.MaxGetMultiConcurrency, prefix+"max-get-multi-concurrency", 100, "The maximum number of concurrent GetMulti() operations. If set to 0, concurrency is unlimited.") |
| 91 | + f.IntVar(&cfg.GetMultiBatchSize, prefix+"get-multi-batch-size", 100, "The maximum size per batch for mget.") |
| 92 | + f.IntVar(&cfg.MaxSetMultiConcurrency, prefix+"max-set-multi-concurrency", 100, "The maximum number of concurrent SetMulti() operations. If set to 0, concurrency is unlimited.") |
| 93 | + f.IntVar(&cfg.SetMultiBatchSize, prefix+"set-multi-batch-size", 100, "The maximum size per batch for pipeline set.") |
| 94 | + f.IntVar(&cfg.CacheSize, prefix+"cache-size", 0, "If not zero then client-side caching is enabled. Client-side caching is when data is stored in memory instead of fetching data each time. See https://redis.io/docs/manual/client-side-caching/ for more info.") |
| 95 | + f.BoolVar(&cfg.TLSEnabled, prefix+"tls-enabled", false, "Whether to enable tls for redis connection.") |
| 96 | + cfg.TLS.RegisterFlagsWithPrefix(prefix, f) |
| 97 | +} |
| 98 | + |
| 99 | +// Validate the config. |
| 100 | +func (cfg *RedisClientConfig) Validate() error { |
| 101 | + if cfg.Addr == "" { |
| 102 | + return errNoIndexCacheAddresses |
| 103 | + } |
| 104 | + |
| 105 | + if cfg.TLSEnabled { |
| 106 | + if (cfg.TLS.CertPath != "") != (cfg.TLS.KeyPath != "") { |
| 107 | + return errors.New("both client key and certificate must be provided") |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +func (cfg *RedisClientConfig) ToRedisClientConfig() cacheutil.RedisClientConfig { |
| 115 | + return cacheutil.RedisClientConfig{ |
| 116 | + Addr: cfg.Addr, |
| 117 | + Username: cfg.Username, |
| 118 | + Password: cfg.Password, |
| 119 | + DB: cfg.DB, |
| 120 | + DialTimeout: cfg.DialTimeout, |
| 121 | + ReadTimeout: cfg.ReadTimeout, |
| 122 | + WriteTimeout: cfg.WriteTimeout, |
| 123 | + PoolSize: cfg.PoolSize, |
| 124 | + MinIdleConns: cfg.MinIdleConns, |
| 125 | + IdleTimeout: cfg.IdleTimeout, |
| 126 | + MaxConnAge: cfg.MaxConnAge, |
| 127 | + MaxGetMultiConcurrency: cfg.MaxGetMultiConcurrency, |
| 128 | + GetMultiBatchSize: cfg.GetMultiBatchSize, |
| 129 | + MaxSetMultiConcurrency: cfg.MaxSetMultiConcurrency, |
| 130 | + SetMultiBatchSize: cfg.SetMultiBatchSize, |
| 131 | + TLSEnabled: cfg.TLSEnabled, |
| 132 | + TLSConfig: cacheutil.TLSConfig{ |
| 133 | + CAFile: cfg.TLS.CAPath, |
| 134 | + KeyFile: cfg.TLS.KeyPath, |
| 135 | + CertFile: cfg.TLS.CertPath, |
| 136 | + ServerName: cfg.TLS.ServerName, |
| 137 | + InsecureSkipVerify: cfg.TLS.InsecureSkipVerify, |
| 138 | + }, |
| 139 | + } |
| 140 | +} |
0 commit comments