github.com/m3db/m3@v1.5.0/src/cluster/placementhandler/remove.go (about)

     1  // Copyright (c) 2021 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package placementhandler
    22  
    23  import (
    24  	"net/http"
    25  	"path"
    26  	"time"
    27  
    28  	"github.com/gogo/protobuf/jsonpb"
    29  	"go.uber.org/zap"
    30  
    31  	"github.com/m3db/m3/src/cluster/placement"
    32  	"github.com/m3db/m3/src/cluster/placementhandler/handleroptions"
    33  	"github.com/m3db/m3/src/query/api/v1/route"
    34  	"github.com/m3db/m3/src/query/generated/proto/admin"
    35  	"github.com/m3db/m3/src/query/util/logging"
    36  	xerrors "github.com/m3db/m3/src/x/errors"
    37  	xhttp "github.com/m3db/m3/src/x/net/http"
    38  )
    39  
    40  const (
    41  	// RemoveHTTPMethod is the HTTP method used with this resource.
    42  	RemoveHTTPMethod = http.MethodPost
    43  
    44  	removePathName = "remove"
    45  )
    46  
    47  var (
    48  	// M3DBRemoveURL is the url for the placement Remove handler (with the POST method)
    49  	// for the M3DB service.
    50  	M3DBRemoveURL = path.Join(route.Prefix, M3DBServicePlacementPathName, removePathName)
    51  
    52  	// M3AggRemoveURL is the url for the placement Remove handler (with the POST method)
    53  	// for the M3Agg service.
    54  	M3AggRemoveURL = path.Join(route.Prefix, M3AggServicePlacementPathName, removePathName)
    55  
    56  	// M3CoordinatorRemoveURL is the url for the placement Remove handler (with the POST method)
    57  	// for the M3Coordinator service.
    58  	M3CoordinatorRemoveURL = path.Join(route.Prefix, M3CoordinatorServicePlacementPathName, removePathName)
    59  )
    60  
    61  // RemoveHandler is the handler for placement removes.
    62  type RemoveHandler Handler
    63  
    64  // NewRemoveHandler returns a new instance of RemoveHandler.
    65  func NewRemoveHandler(opts HandlerOptions) *RemoveHandler {
    66  	return &RemoveHandler{HandlerOptions: opts, nowFn: time.Now}
    67  }
    68  
    69  // ServeHTTP serves HTTP requests.
    70  //nolint: dupl
    71  func (h *RemoveHandler) ServeHTTP(
    72  	svc handleroptions.ServiceNameAndDefaults,
    73  	w http.ResponseWriter,
    74  	r *http.Request,
    75  ) {
    76  	ctx := r.Context()
    77  	logger := logging.WithContext(ctx, h.instrumentOptions)
    78  
    79  	req, rErr := h.parseRequest(r)
    80  	if rErr != nil {
    81  		xhttp.WriteError(w, rErr)
    82  		return
    83  	}
    84  
    85  	placement, err := h.Remove(svc, r, req)
    86  	if err != nil {
    87  		logger.Error("unable to Remove placement", zap.Error(err))
    88  		xhttp.WriteError(w, err)
    89  		return
    90  	}
    91  
    92  	placementProto, err := placement.Proto()
    93  	if err != nil {
    94  		logger.Error("unable to get placement protobuf", zap.Error(err))
    95  		xhttp.WriteError(w, err)
    96  		return
    97  	}
    98  
    99  	resp := &admin.PlacementGetResponse{
   100  		Placement: placementProto,
   101  		Version:   int32(placement.Version()),
   102  	}
   103  
   104  	xhttp.WriteProtoMsgJSONResponse(w, resp, logger)
   105  }
   106  
   107  func (h *RemoveHandler) parseRequest(r *http.Request) (*admin.PlacementRemoveRequest, error) {
   108  	defer r.Body.Close()
   109  
   110  	removeReq := new(admin.PlacementRemoveRequest)
   111  	if err := jsonpb.Unmarshal(r.Body, removeReq); err != nil {
   112  		return nil, xerrors.NewInvalidParamsError(err)
   113  	}
   114  
   115  	return removeReq, nil
   116  }
   117  
   118  // Remove removes an instance.
   119  func (h *RemoveHandler) Remove(
   120  	svc handleroptions.ServiceNameAndDefaults,
   121  	httpReq *http.Request,
   122  	req *admin.PlacementRemoveRequest,
   123  ) (placement.Placement, error) {
   124  	serviceOpts := handleroptions.NewServiceOptions(svc, httpReq.Header,
   125  		h.m3AggServiceOptions)
   126  	var validateFn placement.ValidateFn
   127  	if !req.Force {
   128  		validateFn = validateAllAvailable
   129  	}
   130  
   131  	pcfg, err := Handler(*h).PlacementConfigCopy()
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  	service, _, err := ServiceWithAlgo(
   136  		h.clusterClient,
   137  		serviceOpts,
   138  		pcfg.ApplyOverride(req.OptionOverride),
   139  		h.nowFn(),
   140  		validateFn,
   141  	)
   142  	if err != nil {
   143  		return nil, err
   144  	}
   145  	newPlacement, err := service.RemoveInstances(req.InstanceIds)
   146  	if err != nil {
   147  		return nil, err
   148  	}
   149  
   150  	return newPlacement, nil
   151  }