github.com/m3db/m3@v1.5.0/src/cmd/services/m3comparator/main/series_load_handler.go (about) 1 // Copyright (c) 2020 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 main 22 23 import ( 24 "net/http" 25 "sync" 26 27 "github.com/m3db/m3/src/cmd/services/m3comparator/main/parser" 28 "github.com/m3db/m3/src/dbnode/encoding" 29 xhttp "github.com/m3db/m3/src/x/net/http" 30 31 "go.uber.org/zap" 32 ) 33 34 type seriesLoadHandler interface { 35 getSeriesIterators(string) (encoding.SeriesIterators, error) 36 } 37 38 type httpSeriesLoadHandler struct { 39 sync.RWMutex 40 reader parser.SeriesReader 41 iterOpts parser.Options 42 } 43 44 var ( 45 _ http.Handler = (*httpSeriesLoadHandler)(nil) 46 _ seriesLoadHandler = (*httpSeriesLoadHandler)(nil) 47 ) 48 49 // newHTTPSeriesLoadHandler builds a handler that can load series 50 // to the comparator via an http endpoint. 51 func newHTTPSeriesLoadHandler(iterOpts parser.Options) *httpSeriesLoadHandler { 52 return &httpSeriesLoadHandler{ 53 iterOpts: iterOpts, 54 reader: parser.NewSeriesReader(iterOpts), 55 } 56 } 57 58 func (l *httpSeriesLoadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { 59 logger := l.iterOpts.InstrumentOptions.Logger() 60 err := l.serveHTTP(r) 61 if err != nil { 62 logger.Error("unable to fetch data", zap.Error(err)) 63 xhttp.WriteError(w, err) 64 return 65 } 66 67 w.WriteHeader(http.StatusOK) 68 } 69 70 func (l *httpSeriesLoadHandler) getSeriesIterators( 71 name string) (encoding.SeriesIterators, error) { 72 return l.reader.SeriesIterators(name) 73 } 74 75 func (l *httpSeriesLoadHandler) serveHTTP(r *http.Request) error { 76 if r.Method == http.MethodDelete { 77 l.reader.Clear() 78 return nil 79 } 80 81 body := r.Body 82 defer body.Close() 83 return l.reader.Load(body) 84 }