github.com/m3db/m3@v1.5.0/src/query/api/v1/handler/namespace/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 namespace 22 23 import ( 24 "bytes" 25 "fmt" 26 "net/http" 27 "path" 28 "strconv" 29 30 clusterclient "github.com/m3db/m3/src/cluster/client" 31 "github.com/m3db/m3/src/cluster/kv" 32 "github.com/m3db/m3/src/cluster/placementhandler/handleroptions" 33 nsproto "github.com/m3db/m3/src/dbnode/generated/proto/namespace" 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 "github.com/m3db/m3/src/x/instrument" 38 xhttp "github.com/m3db/m3/src/x/net/http" 39 40 "github.com/gogo/protobuf/jsonpb" 41 "github.com/golang/protobuf/proto" 42 "go.uber.org/zap" 43 ) 44 45 var ( 46 // M3DBGetURL is the url for the namespace get handler (with the GET method). 47 M3DBGetURL = path.Join(route.Prefix, M3DBServiceNamespacePathName) 48 49 // GetHTTPMethod is the HTTP method used with this resource. 50 GetHTTPMethod = http.MethodGet 51 ) 52 53 const ( 54 debugParam = "debug" 55 ) 56 57 // GetHandler is the handler for namespace gets. 58 type GetHandler Handler 59 60 // NewGetHandler returns a new instance of GetHandler. 61 func NewGetHandler( 62 client clusterclient.Client, 63 instrumentOpts instrument.Options, 64 ) *GetHandler { 65 return &GetHandler{ 66 client: client, 67 instrumentOpts: instrumentOpts, 68 } 69 } 70 71 func (h *GetHandler) ServeHTTP( 72 svc handleroptions.ServiceNameAndDefaults, 73 w http.ResponseWriter, 74 r *http.Request, 75 ) { 76 ctx := r.Context() 77 logger := logging.WithContext(ctx, h.instrumentOpts) 78 opts := handleroptions.NewServiceOptions(svc, r.Header, nil) 79 nsRegistry, err := h.Get(opts) 80 81 if err != nil { 82 logger.Error("unable to get namespace", zap.Error(err)) 83 xhttp.WriteError(w, err) 84 return 85 } 86 87 resp := &admin.NamespaceGetResponse{ 88 Registry: &nsRegistry, 89 } 90 91 if debug, err := strconv.ParseBool(r.URL.Query().Get(debugParam)); err == nil && debug { 92 nanosToDurationMap, err := nanosToDuration(resp) 93 if err != nil { 94 logger.Error("error converting nano fields to duration", zap.Error(err)) 95 xhttp.WriteError(w, err) 96 return 97 } 98 99 xhttp.WriteJSONResponse(w, nanosToDurationMap, logger) 100 return 101 } 102 103 xhttp.WriteProtoMsgJSONResponse(w, resp, logger) 104 } 105 106 // Get gets the namespaces. 107 func (h *GetHandler) Get(opts handleroptions.ServiceOptions) (nsproto.Registry, error) { 108 var emptyReg = nsproto.Registry{} 109 110 store, err := h.client.Store(opts.KVOverrideOptions()) 111 if err != nil { 112 return emptyReg, err 113 } 114 115 value, err := store.Get(M3DBNodeNamespacesKey) 116 117 if err == kv.ErrNotFound { 118 // Having no namespace should not be treated as an error 119 return emptyReg, nil 120 } else if err != nil { 121 return emptyReg, err 122 } 123 124 var protoRegistry nsproto.Registry 125 126 if err := value.Unmarshal(&protoRegistry); err != nil { 127 return emptyReg, fmt.Errorf("failed to parse namespace version %v: %v", value.Version(), err) 128 } 129 130 return protoRegistry, nil 131 } 132 133 func nanosToDuration(resp proto.Message) (map[string]interface{}, error) { 134 marshaler := jsonpb.Marshaler{EmitDefaults: true} 135 buf := new(bytes.Buffer) 136 marshaler.Marshal(buf, resp) 137 138 toDuration, err := xhttp.NanosToDurationBytes(buf) 139 if err != nil { 140 return nil, err 141 } 142 143 return toDuration, nil 144 }