MCPcopy Create free account
hub / github.com/cortexproject/cortex / ModeHandler

Method ModeHandler

pkg/ingester/ingester.go:3664–3716  ·  view source on GitHub ↗

ModeHandler Change mode of ingester.

(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

3662
3663// ModeHandler Change mode of ingester.
3664func (i *Ingester) ModeHandler(w http.ResponseWriter, r *http.Request) {
3665 err := r.ParseForm()
3666 if err != nil {
3667 respMsg := "failed to parse HTTP request in mode handler"
3668 level.Warn(logutil.WithContext(r.Context(), i.logger)).Log("msg", respMsg, "err", err)
3669 w.WriteHeader(http.StatusBadRequest)
3670 // We ignore errors here, because we cannot do anything about them.
3671 _, _ = w.Write([]byte(respMsg))
3672 return
3673 }
3674
3675 currentState := i.lifecycler.GetState()
3676 reqMode := strings.ToUpper(r.Form.Get("mode"))
3677 switch reqMode {
3678 case "READONLY":
3679 if currentState != ring.READONLY {
3680 err = i.lifecycler.ChangeState(r.Context(), ring.READONLY)
3681 if err != nil {
3682 respMsg := fmt.Sprintf("failed to change state: %s", err)
3683 level.Warn(logutil.WithContext(r.Context(), i.logger)).Log("msg", respMsg)
3684 w.WriteHeader(http.StatusBadRequest)
3685 // We ignore errors here, because we cannot do anything about them.
3686 _, _ = w.Write([]byte(respMsg))
3687 return
3688 }
3689 }
3690 case "ACTIVE":
3691 if currentState != ring.ACTIVE {
3692 err = i.lifecycler.ChangeState(r.Context(), ring.ACTIVE)
3693 if err != nil {
3694 respMsg := fmt.Sprintf("failed to change state: %s", err)
3695 level.Warn(logutil.WithContext(r.Context(), i.logger)).Log("msg", respMsg)
3696 w.WriteHeader(http.StatusBadRequest)
3697 // We ignore errors here, because we cannot do anything about them.
3698 _, _ = w.Write([]byte(respMsg))
3699 return
3700 }
3701 }
3702 default:
3703 respMsg := fmt.Sprintf("invalid mode input: %s", html.EscapeString(reqMode))
3704 level.Warn(logutil.WithContext(r.Context(), i.logger)).Log("msg", respMsg)
3705 w.WriteHeader(http.StatusBadRequest)
3706 // We ignore errors here, because we cannot do anything about them.
3707 _, _ = w.Write([]byte(respMsg))
3708 return
3709 }
3710
3711 respMsg := fmt.Sprintf("Ingester mode %s", i.lifecycler.GetState())
3712 level.Info(logutil.WithContext(r.Context(), i.logger)).Log("msg", respMsg)
3713 w.WriteHeader(http.StatusOK)
3714 // We ignore errors here, because we cannot do anything about them.
3715 _, _ = w.Write([]byte(respMsg))
3716}
3717
3718func (i *Ingester) getInstanceLimits() *InstanceLimits {
3719 // Don't apply any limits while starting. We especially don't want to apply series in memory limit while replaying WAL.

Callers

nothing calls this directly

Implementers 1

Ingesterpkg/ingester/ingester.go

Calls 6

GetMethod · 0.65
LogMethod · 0.45
ContextMethod · 0.45
WriteMethod · 0.45
GetStateMethod · 0.45
ChangeStateMethod · 0.45

Tested by

no test coverage detected