github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/distributor/distributor_ring.go (about)

     1  package distributor
     2  
     3  import (
     4  	"flag"
     5  	"os"
     6  	"time"
     7  
     8  	"github.com/go-kit/log/level"
     9  	"github.com/grafana/dskit/flagext"
    10  	"github.com/grafana/dskit/kv"
    11  	"github.com/grafana/dskit/ring"
    12  
    13  	util_log "github.com/cortexproject/cortex/pkg/util/log"
    14  )
    15  
    16  // RingConfig masks the ring lifecycler config which contains
    17  // many options not really required by the distributors ring. This config
    18  // is used to strip down the config to the minimum, and avoid confusion
    19  // to the user.
    20  type RingConfig struct {
    21  	KVStore          kv.Config     `yaml:"kvstore"`
    22  	HeartbeatPeriod  time.Duration `yaml:"heartbeat_period"`
    23  	HeartbeatTimeout time.Duration `yaml:"heartbeat_timeout"`
    24  
    25  	// Instance details
    26  	InstanceID             string   `yaml:"instance_id" doc:"hidden"`
    27  	InstanceInterfaceNames []string `yaml:"instance_interface_names"`
    28  	InstancePort           int      `yaml:"instance_port" doc:"hidden"`
    29  	InstanceAddr           string   `yaml:"instance_addr" doc:"hidden"`
    30  
    31  	// Injected internally
    32  	ListenPort int `yaml:"-"`
    33  }
    34  
    35  // RegisterFlags adds the flags required to config this to the given FlagSet
    36  func (cfg *RingConfig) RegisterFlags(f *flag.FlagSet) {
    37  	hostname, err := os.Hostname()
    38  	if err != nil {
    39  		level.Error(util_log.Logger).Log("msg", "failed to get hostname", "err", err)
    40  		os.Exit(1)
    41  	}
    42  
    43  	// Ring flags
    44  	cfg.KVStore.RegisterFlagsWithPrefix("distributor.ring.", "collectors/", f)
    45  	f.DurationVar(&cfg.HeartbeatPeriod, "distributor.ring.heartbeat-period", 5*time.Second, "Period at which to heartbeat to the ring. 0 = disabled.")
    46  	f.DurationVar(&cfg.HeartbeatTimeout, "distributor.ring.heartbeat-timeout", time.Minute, "The heartbeat timeout after which distributors are considered unhealthy within the ring. 0 = never (timeout disabled).")
    47  
    48  	// Instance flags
    49  	cfg.InstanceInterfaceNames = []string{"eth0", "en0"}
    50  	f.Var((*flagext.StringSlice)(&cfg.InstanceInterfaceNames), "distributor.ring.instance-interface-names", "Name of network interface to read address from.")
    51  	f.StringVar(&cfg.InstanceAddr, "distributor.ring.instance-addr", "", "IP address to advertise in the ring.")
    52  	f.IntVar(&cfg.InstancePort, "distributor.ring.instance-port", 0, "Port to advertise in the ring (defaults to server.grpc-listen-port).")
    53  	f.StringVar(&cfg.InstanceID, "distributor.ring.instance-id", hostname, "Instance ID to register in the ring.")
    54  }
    55  
    56  // ToLifecyclerConfig returns a LifecyclerConfig based on the distributor
    57  // ring config.
    58  func (cfg *RingConfig) ToLifecyclerConfig() ring.LifecyclerConfig {
    59  	// We have to make sure that the ring.LifecyclerConfig and ring.Config
    60  	// defaults are preserved
    61  	lc := ring.LifecyclerConfig{}
    62  	rc := ring.Config{}
    63  
    64  	flagext.DefaultValues(&lc)
    65  	flagext.DefaultValues(&rc)
    66  
    67  	// Configure ring
    68  	rc.KVStore = cfg.KVStore
    69  	rc.HeartbeatTimeout = cfg.HeartbeatTimeout
    70  	rc.ReplicationFactor = 1
    71  
    72  	// Configure lifecycler
    73  	lc.RingConfig = rc
    74  	lc.ListenPort = cfg.ListenPort
    75  	lc.Addr = cfg.InstanceAddr
    76  	lc.Port = cfg.InstancePort
    77  	lc.ID = cfg.InstanceID
    78  	lc.InfNames = cfg.InstanceInterfaceNames
    79  	lc.UnregisterOnShutdown = true
    80  	lc.HeartbeatPeriod = cfg.HeartbeatPeriod
    81  	lc.ObservePeriod = 0
    82  	lc.NumTokens = 1
    83  	lc.JoinAfter = 0
    84  	lc.MinReadyDuration = 0
    85  	lc.FinalSleep = 0
    86  
    87  	return lc
    88  }
    89  
    90  func (cfg *RingConfig) ToRingConfig() ring.Config {
    91  	rc := ring.Config{}
    92  	flagext.DefaultValues(&rc)
    93  
    94  	rc.KVStore = cfg.KVStore
    95  	rc.HeartbeatTimeout = cfg.HeartbeatTimeout
    96  	rc.ReplicationFactor = 1
    97  
    98  	return rc
    99  }