github.com/m3db/m3@v1.5.0/src/cluster/placementhandler/init.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/m3db/m3/src/cluster/kv" 29 "github.com/m3db/m3/src/cluster/placement" 30 "github.com/m3db/m3/src/cluster/placementhandler/handleroptions" 31 "github.com/m3db/m3/src/query/api/v1/route" 32 "github.com/m3db/m3/src/query/generated/proto/admin" 33 "github.com/m3db/m3/src/query/util/logging" 34 xerrors "github.com/m3db/m3/src/x/errors" 35 xhttp "github.com/m3db/m3/src/x/net/http" 36 37 "github.com/gogo/protobuf/jsonpb" 38 "go.uber.org/zap" 39 ) 40 41 const ( 42 initPathName = "init" 43 ) 44 45 var ( 46 // M3DBInitURL is the url for the placement init handler, (with the POST method). 47 M3DBInitURL = path.Join(route.Prefix, M3DBServicePlacementPathName, initPathName) 48 49 // M3AggInitURL is the url for the m3agg placement init handler (with the POST method). 50 M3AggInitURL = path.Join(route.Prefix, M3AggServicePlacementPathName, initPathName) 51 52 // M3CoordinatorInitURL is the url for the m3agg placement init handler (with the POST method). 53 M3CoordinatorInitURL = path.Join(route.Prefix, M3CoordinatorServicePlacementPathName, initPathName) 54 55 // InitHTTPMethod is the HTTP method used with this resource. 56 InitHTTPMethod = http.MethodPost 57 ) 58 59 // InitHandler is the handler for placement inits. 60 type InitHandler Handler 61 62 // NewInitHandler returns a new instance of InitHandler. 63 func NewInitHandler(opts HandlerOptions) *InitHandler { 64 return &InitHandler{HandlerOptions: opts, nowFn: time.Now} 65 } 66 67 func (h *InitHandler) 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.Init(svc, r, req) 82 if err != nil { 83 if err == kv.ErrAlreadyExists { 84 logger.Error("placement already exists", zap.Error(err)) 85 xhttp.WriteError(w, xhttp.NewError(err, http.StatusConflict)) 86 return 87 } 88 logger.Error("unable to initialize placement", zap.Error(err)) 89 xhttp.WriteError(w, err) 90 return 91 } 92 93 placementProto, err := placement.Proto() 94 if err != nil { 95 logger.Error("unable to get placement protobuf", zap.Error(err)) 96 xhttp.WriteError(w, err) 97 return 98 } 99 100 resp := &admin.PlacementGetResponse{ 101 Placement: placementProto, 102 } 103 104 xhttp.WriteProtoMsgJSONResponse(w, resp, logger) 105 } 106 107 func (h *InitHandler) parseRequest(r *http.Request) (*admin.PlacementInitRequest, error) { 108 defer r.Body.Close() 109 110 initReq := new(admin.PlacementInitRequest) 111 if err := jsonpb.Unmarshal(r.Body, initReq); err != nil { 112 return nil, xerrors.NewInvalidParamsError(err) 113 } 114 115 return initReq, nil 116 } 117 118 // Init initializes a placement. 119 func (h *InitHandler) Init( 120 svc handleroptions.ServiceNameAndDefaults, 121 httpReq *http.Request, 122 req *admin.PlacementInitRequest, 123 ) (placement.Placement, error) { 124 instances, err := ConvertInstancesProto(req.Instances) 125 if err != nil { 126 return nil, err 127 } 128 129 serviceOpts := handleroptions.NewServiceOptions(svc, httpReq.Header, 130 h.m3AggServiceOptions) 131 132 pcfg, err := Handler(*h).PlacementConfigCopy() 133 if err != nil { 134 return nil, err 135 } 136 service, err := Service(h.clusterClient, serviceOpts, 137 pcfg.ApplyOverride(req.OptionOverride), h.nowFn(), nil) 138 if err != nil { 139 return nil, err 140 } 141 142 replicationFactor := int(req.ReplicationFactor) 143 switch svc.ServiceName { 144 case handleroptions.M3CoordinatorServiceName: 145 // M3Coordinator placements are stateless 146 replicationFactor = 1 147 } 148 149 placement, err := service.BuildInitialPlacement(instances, 150 int(req.NumShards), replicationFactor) 151 if err != nil { 152 return nil, err 153 } 154 155 return placement, nil 156 }