github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/storage/stores/shipper/indexgateway/config.go (about) 1 package indexgateway 2 3 import ( 4 "flag" 5 "fmt" 6 7 loki_util "github.com/grafana/loki/pkg/util" 8 ) 9 10 // Mode represents in which mode an Index Gateway instance is running. 11 // 12 // Right now, two modes are supported: simple mode (default) and ring mode. 13 type Mode string 14 15 // Set implements a flag interface, and is necessary to use the IndexGatewayClientMode as a flag. 16 func (i Mode) Set(v string) error { 17 switch v { 18 case string(SimpleMode): 19 // nolint:ineffassign 20 i = SimpleMode 21 case string(RingMode): 22 // nolint:ineffassign 23 i = RingMode 24 default: 25 return fmt.Errorf("mode %s not supported. list of supported modes: simple (default), ring", v) 26 } 27 return nil 28 } 29 30 // String implements a flag interface, and is necessary to use the IndexGatewayClientMode as a flag. 31 func (i Mode) String() string { 32 switch i { 33 case RingMode: 34 return string(RingMode) 35 default: 36 return string(SimpleMode) 37 } 38 } 39 40 const ( 41 // SimpleMode is a mode where an Index Gateway instance solely handle all the work. 42 SimpleMode Mode = "simple" 43 44 // RingMode is a mode where different Index Gateway instances are assigned to handle different tenants. 45 // 46 // It is more horizontally scalable than the simple mode, but requires running a key-value store ring. 47 RingMode Mode = "ring" 48 ) 49 50 // RingCfg is a wrapper for our Index Gateway ring configuration plus the replication factor. 51 type RingCfg struct { 52 // InternalRingCfg configures the Index Gateway ring. 53 loki_util.RingConfig `yaml:",inline"` 54 55 // ReplicationFactor defines how many Index Gateway instances are assigned to each tenant. 56 // 57 // Whenever the store queries the ring key-value store for the Index Gateway instance responsible for tenant X, 58 // multiple Index Gateway instances are expected to be returned as Index Gateway might be busy/locked for specific 59 // reasons (this is assured by the spikey behavior of Index Gateway latencies). 60 ReplicationFactor int `yaml:"replication_factor"` 61 } 62 63 // RegisterFlagsWithPrefix register all Index Gateway flags related to its ring but with a proper store prefix to avoid conflicts. 64 func (cfg *RingCfg) RegisterFlags(prefix, storePrefix string, f *flag.FlagSet) { 65 cfg.RegisterFlagsWithPrefix(prefix, storePrefix, f) 66 f.IntVar(&cfg.ReplicationFactor, "replication-factor", 3, "how many index gateway instances are assigned to each tenant") 67 } 68 69 // Config configures an Index Gateway server. 70 type Config struct { 71 // Mode configures in which mode the client will be running when querying and communicating with an Index Gateway instance. 72 Mode Mode `yaml:"mode"` 73 74 // Ring configures the ring key-value store used to save and retrieve the different Index Gateway instances. 75 // 76 // In case it isn't explicitly set, it follows the same behavior of the other rings (ex: using the common configuration 77 // section and the ingester configuration by default). 78 Ring RingCfg `yaml:"ring,omitempty"` 79 } 80 81 // RegisterFlags register all IndexGatewayClientConfig flags and all the flags of its subconfigs but with a prefix (ex: shipper). 82 func (cfg *Config) RegisterFlags(f *flag.FlagSet) { 83 cfg.Ring.RegisterFlags("index-gateway.", "collectors/", f) 84 f.StringVar((*string)(&cfg.Mode), "index-gateway.mode", SimpleMode.String(), "mode in which the index gateway client will be running") 85 }