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

     1  // Copyright (c) 2018 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  	// AddHTTPMethod is the HTTP method used with this resource.
    42  	AddHTTPMethod = http.MethodPost
    43  )
    44  
    45  var (
    46  	// M3DBAddURL is the url for the placement add handler (with the POST method)
    47  	// for the M3DB service.
    48  	M3DBAddURL = path.Join(route.Prefix, M3DBServicePlacementPathName)
    49  
    50  	// M3AggAddURL is the url for the placement add handler (with the POST method)
    51  	// for the M3Agg service.
    52  	M3AggAddURL = path.Join(route.Prefix, M3AggServicePlacementPathName)
    53  
    54  	// M3CoordinatorAddURL is the url for the placement add handler (with the POST method)
    55  	// for the M3Coordinator service.
    56  	M3CoordinatorAddURL = path.Join(route.Prefix, M3CoordinatorServicePlacementPathName)
    57  )
    58  
    59  // AddHandler is the handler for placement adds.
    60  type AddHandler Handler
    61  
    62  // NewAddHandler returns a new instance of AddHandler.
    63  func NewAddHandler(opts HandlerOptions) *AddHandler {
    64  	return &AddHandler{HandlerOptions: opts, nowFn: time.Now}
    65  }
    66  
    67  func (h *AddHandler) ServeHTTP(
    68  	svc handleroptions.ServiceNameAndDefaults,
    69  	w http.ResponseWriter,
    70  	r *http.Request,
    71  ) {
    72  	ctx := r.Context()
    73  	logger := logging.WithContext(ctx, h.instrumentOptions)
    74  
    75  	req, rErr := h.parseRequest(r)
    76  	if rErr != nil {
    77  		xhttp.WriteError(w, rErr)
    78  		return
    79  	}
    80  
    81  	placement, err := h.Add(svc, r, req)
    82  	if err != nil {
    83  		logger.Error("unable to add placement", zap.Error(err))
    84  		xhttp.WriteError(w, err)
    85  		return
    86  	}
    87  
    88  	placementProto, err := placement.Proto()
    89  	if err != nil {
    90  		logger.Error("unable to get placement protobuf", zap.Error(err))
    91  		xhttp.WriteError(w, err)
    92  		return
    93  	}
    94  
    95  	resp := &admin.PlacementGetResponse{
    96  		Placement: placementProto,
    97  		Version:   int32(placement.Version()),
    98  	}
    99  
   100  	xhttp.WriteProtoMsgJSONResponse(w, resp, logger)
   101  }
   102  
   103  func (h *AddHandler) parseRequest(r *http.Request) (*admin.PlacementAddRequest, error) {
   104  	defer r.Body.Close()
   105  
   106  	addReq := new(admin.PlacementAddRequest)
   107  	if err := jsonpb.Unmarshal(r.Body, addReq); err != nil {
   108  		return nil, xerrors.NewInvalidParamsError(err)
   109  	}
   110  
   111  	return addReq, nil
   112  }
   113  
   114  // Add adds a placement.
   115  func (h *AddHandler) Add(
   116  	svc handleroptions.ServiceNameAndDefaults,
   117  	httpReq *http.Request,
   118  	req *admin.PlacementAddRequest,
   119  ) (placement.Placement, error) {
   120  	instances, err := ConvertInstancesProto(req.Instances)
   121  	if err != nil {
   122  		return nil, err
   123  	}
   124  
   125  	serviceOpts := handleroptions.NewServiceOptions(svc, httpReq.Header,
   126  		h.m3AggServiceOptions)
   127  	var validateFn placement.ValidateFn
   128  	if !req.Force {
   129  		validateFn = validateAllAvailable
   130  	}
   131  
   132  	pcfg, err := Handler(*h).PlacementConfigCopy()
   133  	if err != nil {
   134  		return nil, err
   135  	}
   136  	service, _, err := ServiceWithAlgo(
   137  		h.clusterClient,
   138  		serviceOpts,
   139  		pcfg.ApplyOverride(req.OptionOverride),
   140  		h.nowFn(),
   141  		validateFn,
   142  	)
   143  	if err != nil {
   144  		return nil, err
   145  	}
   146  	newPlacement, _, err := service.AddInstances(instances)
   147  	if err != nil {
   148  		return nil, err
   149  	}
   150  
   151  	return newPlacement, nil
   152  }