storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/healthcheck-handler.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2018 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package cmd
    18  
    19  import (
    20  	"context"
    21  	"net/http"
    22  	"strconv"
    23  
    24  	xhttp "storj.io/minio/cmd/http"
    25  )
    26  
    27  const unavailable = "offline"
    28  
    29  // ClusterCheckHandler returns if the server is ready for requests.
    30  func ClusterCheckHandler(w http.ResponseWriter, r *http.Request) {
    31  	ctx := NewContext(r, w, "ClusterCheckHandler")
    32  
    33  	if shouldProxy() {
    34  		w.Header().Set(xhttp.MinIOServerStatus, unavailable)
    35  		writeResponse(w, http.StatusServiceUnavailable, nil, mimeNone)
    36  		return
    37  	}
    38  
    39  	objLayer := newObjectLayerFn()
    40  
    41  	ctx, cancel := context.WithTimeout(ctx, globalAPIConfig.getClusterDeadline())
    42  	defer cancel()
    43  
    44  	opts := HealthOptions{Maintenance: r.URL.Query().Get("maintenance") == "true"}
    45  	result := objLayer.Health(ctx, opts)
    46  	if result.WriteQuorum > 0 {
    47  		w.Header().Set(xhttp.MinIOWriteQuorum, strconv.Itoa(result.WriteQuorum))
    48  	}
    49  	if !result.Healthy {
    50  		// return how many drives are being healed if any
    51  		if result.HealingDrives > 0 {
    52  			w.Header().Set(xhttp.MinIOHealingDrives, strconv.Itoa(result.HealingDrives))
    53  		}
    54  		// As a maintenance call we are purposefully asked to be taken
    55  		// down, this is for orchestrators to know if we can safely
    56  		// take this server down, return appropriate error.
    57  		if opts.Maintenance {
    58  			writeResponse(w, http.StatusPreconditionFailed, nil, mimeNone)
    59  		} else {
    60  			writeResponse(w, http.StatusServiceUnavailable, nil, mimeNone)
    61  		}
    62  		return
    63  	}
    64  	writeResponse(w, http.StatusOK, nil, mimeNone)
    65  }
    66  
    67  // ClusterReadCheckHandler returns if the server is ready for requests.
    68  func ClusterReadCheckHandler(w http.ResponseWriter, r *http.Request) {
    69  	ctx := NewContext(r, w, "ClusterReadCheckHandler")
    70  
    71  	if shouldProxy() {
    72  		w.Header().Set(xhttp.MinIOServerStatus, unavailable)
    73  		writeResponse(w, http.StatusServiceUnavailable, nil, mimeNone)
    74  		return
    75  	}
    76  
    77  	objLayer := newObjectLayerFn()
    78  
    79  	ctx, cancel := context.WithTimeout(ctx, globalAPIConfig.getClusterDeadline())
    80  	defer cancel()
    81  
    82  	result := objLayer.ReadHealth(ctx)
    83  	if !result {
    84  		writeResponse(w, http.StatusServiceUnavailable, nil, mimeNone)
    85  		return
    86  	}
    87  	writeResponse(w, http.StatusOK, nil, mimeNone)
    88  }
    89  
    90  // ReadinessCheckHandler Checks if the process is up. Always returns success.
    91  func ReadinessCheckHandler(w http.ResponseWriter, r *http.Request) {
    92  	if shouldProxy() {
    93  		// Service not initialized yet
    94  		w.Header().Set(xhttp.MinIOServerStatus, unavailable)
    95  	}
    96  
    97  	writeResponse(w, http.StatusOK, nil, mimeNone)
    98  }
    99  
   100  // LivenessCheckHandler - Checks if the process is up. Always returns success.
   101  func LivenessCheckHandler(w http.ResponseWriter, r *http.Request) {
   102  	if shouldProxy() {
   103  		// Service not initialized yet
   104  		w.Header().Set(xhttp.MinIOServerStatus, unavailable)
   105  	}
   106  	writeResponse(w, http.StatusOK, nil, mimeNone)
   107  }