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

     1  package compactor
     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 compactors 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  	// Wait ring stability.
    26  	WaitStabilityMinDuration time.Duration `yaml:"wait_stability_min_duration"`
    27  	WaitStabilityMaxDuration time.Duration `yaml:"wait_stability_max_duration"`
    28  
    29  	// Instance details
    30  	InstanceID             string   `yaml:"instance_id" doc:"hidden"`
    31  	InstanceInterfaceNames []string `yaml:"instance_interface_names"`
    32  	InstancePort           int      `yaml:"instance_port" doc:"hidden"`
    33  	InstanceAddr           string   `yaml:"instance_addr" doc:"hidden"`
    34  
    35  	// Injected internally
    36  	ListenPort int `yaml:"-"`
    37  
    38  	WaitActiveInstanceTimeout time.Duration `yaml:"wait_active_instance_timeout"`
    39  
    40  	ObservePeriod time.Duration `yaml:"-"`
    41  }
    42  
    43  // RegisterFlags adds the flags required to config this to the given FlagSet
    44  func (cfg *RingConfig) RegisterFlags(f *flag.FlagSet) {
    45  	hostname, err := os.Hostname()
    46  	if err != nil {
    47  		level.Error(util_log.Logger).Log("msg", "failed to get hostname", "err", err)
    48  		os.Exit(1)
    49  	}
    50  
    51  	// Ring flags
    52  	cfg.KVStore.RegisterFlagsWithPrefix("compactor.ring.", "collectors/", f)
    53  	f.DurationVar(&cfg.HeartbeatPeriod, "compactor.ring.heartbeat-period", 5*time.Second, "Period at which to heartbeat to the ring. 0 = disabled.")
    54  	f.DurationVar(&cfg.HeartbeatTimeout, "compactor.ring.heartbeat-timeout", time.Minute, "The heartbeat timeout after which compactors are considered unhealthy within the ring. 0 = never (timeout disabled).")
    55  
    56  	// Wait stability flags.
    57  	f.DurationVar(&cfg.WaitStabilityMinDuration, "compactor.ring.wait-stability-min-duration", time.Minute, "Minimum time to wait for ring stability at startup. 0 to disable.")
    58  	f.DurationVar(&cfg.WaitStabilityMaxDuration, "compactor.ring.wait-stability-max-duration", 5*time.Minute, "Maximum time to wait for ring stability at startup. If the compactor ring keeps changing after this period of time, the compactor will start anyway.")
    59  
    60  	// Instance flags
    61  	cfg.InstanceInterfaceNames = []string{"eth0", "en0"}
    62  	f.Var((*flagext.StringSlice)(&cfg.InstanceInterfaceNames), "compactor.ring.instance-interface-names", "Name of network interface to read address from.")
    63  	f.StringVar(&cfg.InstanceAddr, "compactor.ring.instance-addr", "", "IP address to advertise in the ring.")
    64  	f.IntVar(&cfg.InstancePort, "compactor.ring.instance-port", 0, "Port to advertise in the ring (defaults to server.grpc-listen-port).")
    65  	f.StringVar(&cfg.InstanceID, "compactor.ring.instance-id", hostname, "Instance ID to register in the ring.")
    66  
    67  	// Timeout durations
    68  	f.DurationVar(&cfg.WaitActiveInstanceTimeout, "compactor.ring.wait-active-instance-timeout", 10*time.Minute, "Timeout for waiting on compactor to become ACTIVE in the ring.")
    69  }
    70  
    71  // ToLifecyclerConfig returns a LifecyclerConfig based on the compactor
    72  // ring config.
    73  func (cfg *RingConfig) ToLifecyclerConfig() ring.LifecyclerConfig {
    74  	// We have to make sure that the ring.LifecyclerConfig and ring.Config
    75  	// defaults are preserved
    76  	lc := ring.LifecyclerConfig{}
    77  	rc := ring.Config{}
    78  
    79  	flagext.DefaultValues(&lc)
    80  	flagext.DefaultValues(&rc)
    81  
    82  	// Configure ring
    83  	rc.KVStore = cfg.KVStore
    84  	rc.HeartbeatTimeout = cfg.HeartbeatTimeout
    85  	rc.ReplicationFactor = 1
    86  
    87  	// Configure lifecycler
    88  	lc.RingConfig = rc
    89  	lc.RingConfig.SubringCacheDisabled = true
    90  	lc.ListenPort = cfg.ListenPort
    91  	lc.Addr = cfg.InstanceAddr
    92  	lc.Port = cfg.InstancePort
    93  	lc.ID = cfg.InstanceID
    94  	lc.InfNames = cfg.InstanceInterfaceNames
    95  	lc.UnregisterOnShutdown = true
    96  	lc.HeartbeatPeriod = cfg.HeartbeatPeriod
    97  	lc.ObservePeriod = cfg.ObservePeriod
    98  	lc.JoinAfter = 0
    99  	lc.MinReadyDuration = 0
   100  	lc.FinalSleep = 0
   101  
   102  	// We use a safe default instead of exposing to config option to the user
   103  	// in order to simplify the config.
   104  	lc.NumTokens = 512
   105  
   106  	return lc
   107  }