github.com/m3db/m3@v1.5.0/src/cluster/placementhandler/get.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 "errors" 25 "fmt" 26 "net/http" 27 "path" 28 "strconv" 29 "time" 30 31 "github.com/m3db/m3/src/cluster/kv" 32 "github.com/m3db/m3/src/cluster/placement" 33 "github.com/m3db/m3/src/cluster/placementhandler/handleroptions" 34 "github.com/m3db/m3/src/query/api/v1/route" 35 "github.com/m3db/m3/src/query/generated/proto/admin" 36 "github.com/m3db/m3/src/query/util/logging" 37 xerrors "github.com/m3db/m3/src/x/errors" 38 xhttp "github.com/m3db/m3/src/x/net/http" 39 40 "go.uber.org/zap" 41 ) 42 43 const ( 44 // GetHTTPMethod is the HTTP method used with this resource. 45 GetHTTPMethod = http.MethodGet 46 ) 47 48 var ( 49 // M3DBGetURL is the url for the placement get handler (with the GET method) 50 // for the M3DB service. 51 M3DBGetURL = path.Join(route.Prefix, M3DBServicePlacementPathName) 52 53 // M3AggGetURL is the url for the placement get handler (with the GET method) 54 // for the M3Agg service. 55 M3AggGetURL = path.Join(route.Prefix, M3AggServicePlacementPathName) 56 57 // M3CoordinatorGetURL is the url for the placement get handler (with the GET method) 58 // for the M3Coordinator service. 59 M3CoordinatorGetURL = path.Join(route.Prefix, M3CoordinatorServicePlacementPathName) 60 61 errPlacementDoesNotExist = xhttp.NewError(errors.New("placement does not exist"), http.StatusNotFound) 62 ) 63 64 // GetHandler is the handler for placement gets. 65 type GetHandler Handler 66 67 // NewGetHandler returns a new instance of GetHandler. 68 func NewGetHandler(opts HandlerOptions) *GetHandler { 69 return &GetHandler{HandlerOptions: opts, nowFn: time.Now} 70 } 71 72 func (h *GetHandler) ServeHTTP( 73 service handleroptions.ServiceNameAndDefaults, 74 w http.ResponseWriter, 75 r *http.Request, 76 ) { 77 var ( 78 ctx = r.Context() 79 logger = logging.WithContext(ctx, h.instrumentOptions) 80 ) 81 82 placement, err := h.Get(service, r) 83 if err != nil { 84 xhttp.WriteError(w, err) 85 return 86 } 87 if placement == nil { 88 xhttp.WriteError(w, errPlacementDoesNotExist) 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 // Get gets a placement. 108 func (h *GetHandler) Get( 109 svc handleroptions.ServiceNameAndDefaults, 110 httpReq *http.Request, 111 ) (placement placement.Placement, err error) { 112 var headers http.Header 113 if httpReq != nil { 114 headers = httpReq.Header 115 } 116 117 opts := handleroptions.NewServiceOptions(svc, headers, h.m3AggServiceOptions) 118 service, err := Service(h.clusterClient, opts, 119 Handler(*h).PlacementConfig(), h.nowFn(), nil) 120 if err != nil { 121 return nil, err 122 } 123 124 if httpReq != nil && httpReq.FormValue("version") != "" { 125 version, err := strconv.Atoi(httpReq.FormValue("version")) 126 if err != nil { 127 return nil, xerrors.NewInvalidParamsError(fmt.Errorf("could not parse version: %v", err)) 128 } 129 130 placement, err = service.PlacementForVersion(version) 131 if err == kv.ErrNotFound { 132 // TODO(rartoul): This should probably be handled at the service 133 // level but that would be a large refactor. 134 return nil, nil 135 } 136 if err != nil { 137 return nil, err 138 } 139 140 return placement, nil 141 } 142 143 placement, err = service.Placement() 144 if err == kv.ErrNotFound { 145 // TODO(rartoul): This should probably be handled at the service 146 // level but that would be a large refactor. 147 return nil, nil 148 } 149 if err != nil { 150 return nil, err 151 } 152 153 return placement, nil 154 }