Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[query] Query handlers refactoring #2872

Merged
merged 24 commits into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
69e69d5
Initial commit for proposed query handlers refactoring:
soundvibe Nov 11, 2020
1fe3aa1
Merge branch 'master' into linasn/query-handlers-refactoring
soundvibe Nov 11, 2020
5ebcdeb
trying to make linter happy
soundvibe Nov 11, 2020
67814a7
linter fixes
soundvibe Nov 11, 2020
eaddb85
revert old behaviour
soundvibe Nov 11, 2020
3b04c30
Make sure route methods are taken into account when adding and search…
soundvibe Nov 11, 2020
ea121f2
fixed code formatting
soundvibe Nov 11, 2020
dd38ead
[dbnode] Refactor wide query path (#2826)
arnikola Nov 11, 2020
a3bd18a
[dbnode] Introduce Aggregator type (#2840)
linasm Nov 11, 2020
3b5c0ff
[coordinator] Set default namespace tag to avoid colliding with commo…
robskillington Nov 12, 2020
96a5efb
Improve some slow tests (#2881)
vdarulis Nov 12, 2020
9b9c6da
Changes after code review.
soundvibe Nov 12, 2020
78db238
[query] Remove dead code in prom package (#2871)
vpranckaitis Nov 12, 2020
9af0b72
Register separate route for each method.
soundvibe Nov 12, 2020
5814642
linter fixes
soundvibe Nov 12, 2020
a7d6696
removed code duplication in hasndler_test
soundvibe Nov 12, 2020
03f5e35
Merge branch 'master' into linasn/query-handlers-refactoring
soundvibe Nov 12, 2020
3a3a885
Fail if route was already registered.
soundvibe Nov 12, 2020
2d71d9a
formatted code
soundvibe Nov 12, 2020
6b82ce4
Merge branch 'master' into linasn/query-handlers-refactoring
soundvibe Nov 16, 2020
c46d9fd
Update src/query/api/v1/httpd/handler_test.go
soundvibe Nov 16, 2020
ff3a894
Update src/query/api/v1/httpd/handler_test.go
soundvibe Nov 16, 2020
3bc8e4c
More handler tests.
soundvibe Nov 16, 2020
b956426
Merge branch 'master' into linasn/query-handlers-refactoring
soundvibe Nov 16, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions src/query/api/v1/handler/database/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ import (
dbconfig "github.com/m3db/m3/src/cmd/services/m3dbnode/config"
"github.com/m3db/m3/src/cmd/services/m3query/config"
"github.com/m3db/m3/src/query/api/v1/handler/prometheus/handleroptions"
"github.com/m3db/m3/src/query/util/logging"
"github.com/m3db/m3/src/x/instrument"

"github.com/gorilla/mux"
)

// Handler represents a generic handler for namespace endpoints.
Expand All @@ -41,33 +38,27 @@ type Handler struct {
instrumentOpts instrument.Options
}

type addRouteFn func(path string, handler http.Handler, methods ...string)

// RegisterRoutes registers the namespace routes
func RegisterRoutes(
r *mux.Router,
addRoute addRouteFn,
client clusterclient.Client,
cfg config.Configuration,
embeddedDbCfg *dbconfig.DBConfiguration,
defaults []handleroptions.ServiceOptionsDefault,
instrumentOpts instrument.Options,
) error {
wrapped := func(n http.Handler) http.Handler {
return logging.WithResponseTimeAndPanicErrorLogging(n, instrumentOpts)
}

createHandler, err := NewCreateHandler(client, cfg, embeddedDbCfg,
defaults, instrumentOpts)
if err != nil {
return err
}

r.HandleFunc(CreateURL,
wrapped(createHandler).ServeHTTP).
Methods(CreateHTTPMethod)

// Register the same handler under two different endpoints. This just makes explaining things in
// our documentation easier so we can separate out concepts, but share the underlying code.
r.HandleFunc(CreateURL, wrapped(createHandler).ServeHTTP).Methods(CreateHTTPMethod)
r.HandleFunc(CreateNamespaceURL, wrapped(createHandler).ServeHTTP).Methods(CreateNamespaceHTTPMethod)
addRoute(CreateURL, createHandler, CreateHTTPMethod)
addRoute(CreateNamespaceURL, createHandler, CreateNamespaceHTTPMethod)

return nil
}
84 changes: 43 additions & 41 deletions src/query/api/v1/handler/namespace/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,8 @@ import (
"github.com/m3db/m3/src/dbnode/namespace"
"github.com/m3db/m3/src/query/api/v1/handler/prometheus/handleroptions"
"github.com/m3db/m3/src/query/storage/m3"
"github.com/m3db/m3/src/query/util/logging"
"github.com/m3db/m3/src/x/instrument"
xhttp "github.com/m3db/m3/src/x/net/http"

"github.com/gorilla/mux"
)

const (
Expand Down Expand Up @@ -75,6 +72,8 @@ type Handler struct {
instrumentOpts instrument.Options
}

type addRouteFn func(path string, handler http.Handler, methods ...string)
soundvibe marked this conversation as resolved.
Show resolved Hide resolved

// Metadata returns the current metadata in the given store and its version
func Metadata(store kv.Store) ([]namespace.Metadata, int, error) {
value, err := store.Get(M3DBNodeNamespacesKey)
Expand Down Expand Up @@ -104,64 +103,67 @@ func Metadata(store kv.Store) ([]namespace.Metadata, int, error) {

// RegisterRoutes registers the namespace routes.
func RegisterRoutes(
r *mux.Router,
addRouteFn addRouteFn,
client clusterclient.Client,
clusters m3.Clusters,
defaults []handleroptions.ServiceOptionsDefault,
instrumentOpts instrument.Options,
) {
wrapped := func(n http.Handler) http.Handler {
return logging.WithResponseTimeAndPanicErrorLogging(n, instrumentOpts)
}
applyMiddleware := func(
f func(svc handleroptions.ServiceNameAndDefaults,
var (
applyMiddleware = func(
f func(svc handleroptions.ServiceNameAndDefaults,
w http.ResponseWriter, r *http.Request),
defaults []handleroptions.ServiceOptionsDefault,
) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
svc := handleroptions.ServiceNameAndDefaults{
ServiceName: handleroptions.M3DBServiceName,
Defaults: defaults,
}
f(svc, w, r)
})
}
defaults []handleroptions.ServiceOptionsDefault,
) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
svc := handleroptions.ServiceNameAndDefaults{
ServiceName: handleroptions.M3DBServiceName,
Defaults: defaults,
}
f(svc, w, r)
})
}

addRoute = func(
path string,
f func(
svc handleroptions.ServiceNameAndDefaults,
w http.ResponseWriter,
r *http.Request,
),
soundvibe marked this conversation as resolved.
Show resolved Hide resolved
methods ...string,
) {
addRouteFn(path, applyMiddleware(f, defaults), methods...)
}
)
soundvibe marked this conversation as resolved.
Show resolved Hide resolved

// Get M3DB namespaces.
getHandler := wrapped(
applyMiddleware(NewGetHandler(client, instrumentOpts).ServeHTTP, defaults))
r.HandleFunc(M3DBGetURL, getHandler.ServeHTTP).Methods(GetHTTPMethod)
getHandler := NewGetHandler(client, instrumentOpts).ServeHTTP
addRoute(M3DBGetURL, getHandler, GetHTTPMethod)

// Add M3DB namespaces.
addHandler := wrapped(
applyMiddleware(NewAddHandler(client, instrumentOpts).ServeHTTP, defaults))
r.HandleFunc(M3DBAddURL, addHandler.ServeHTTP).Methods(AddHTTPMethod)
addHandler := NewAddHandler(client, instrumentOpts).ServeHTTP
addRoute(M3DBAddURL, addHandler, AddHTTPMethod)

// Update M3DB namespaces.
updateHandler := wrapped(
applyMiddleware(NewUpdateHandler(client, instrumentOpts).ServeHTTP, defaults))
r.HandleFunc(M3DBUpdateURL, updateHandler.ServeHTTP).Methods(UpdateHTTPMethod)
updateHandler := NewUpdateHandler(client, instrumentOpts).ServeHTTP
addRoute(M3DBUpdateURL, updateHandler, UpdateHTTPMethod)

// Delete M3DB namespaces.
deleteHandler := wrapped(
applyMiddleware(NewDeleteHandler(client, instrumentOpts).ServeHTTP, defaults))
r.HandleFunc(M3DBDeleteURL, deleteHandler.ServeHTTP).Methods(DeleteHTTPMethod)
deleteHandler := NewDeleteHandler(client, instrumentOpts).ServeHTTP
addRoute(M3DBDeleteURL, deleteHandler, DeleteHTTPMethod)

// Deploy M3DB schemas.
schemaHandler := wrapped(
applyMiddleware(NewSchemaHandler(client, instrumentOpts).ServeHTTP, defaults))
r.HandleFunc(M3DBSchemaURL, schemaHandler.ServeHTTP).Methods(SchemaDeployHTTPMethod)
schemaHandler := NewSchemaHandler(client, instrumentOpts).ServeHTTP
addRoute(M3DBSchemaURL, schemaHandler, SchemaDeployHTTPMethod)

// Reset M3DB schemas.
schemaResetHandler := wrapped(
applyMiddleware(NewSchemaResetHandler(client, instrumentOpts).ServeHTTP, defaults))
r.HandleFunc(M3DBSchemaURL, schemaResetHandler.ServeHTTP).Methods(DeleteHTTPMethod)
schemaResetHandler := NewSchemaResetHandler(client, instrumentOpts).ServeHTTP
addRoute(M3DBSchemaURL, schemaResetHandler, DeleteHTTPMethod)

// Mark M3DB namespace as ready.
readyHandler := wrapped(
applyMiddleware(NewReadyHandler(client, clusters, instrumentOpts).ServeHTTP, defaults))
r.HandleFunc(M3DBReadyURL, readyHandler.ServeHTTP).Methods(ReadyHTTPMethod)

readyHandler := NewReadyHandler(client, clusters, instrumentOpts).ServeHTTP
addRoute(M3DBReadyURL, readyHandler, ReadyHTTPMethod)
}

func validateNamespaceAggregationOptions(mds []namespace.Metadata) error {
Expand Down
51 changes: 25 additions & 26 deletions src/query/api/v1/handler/placement/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ import (
xerrors "github.com/m3db/m3/src/x/errors"
"github.com/m3db/m3/src/x/instrument"
xhttp "github.com/m3db/m3/src/x/net/http"

"github.com/gorilla/mux"
)

const (
Expand Down Expand Up @@ -222,7 +220,7 @@ func ConvertInstancesProto(instancesProto []*placementpb.Instance) ([]placement.

// RegisterRoutes registers the placement routes
func RegisterRoutes(
r *mux.Router,
addRoute func(path string, handler http.Handler, methods ...string),
defaults []handleroptions.ServiceOptionsDefault,
opts HandlerOptions,
) {
Expand All @@ -231,63 +229,64 @@ func RegisterRoutes(
initHandler = NewInitHandler(opts)
initFn = applyMiddleware(initHandler.ServeHTTP, defaults, opts.instrumentOptions)
)
r.HandleFunc(M3DBInitURL, initFn).Methods(InitHTTPMethod)
r.HandleFunc(M3AggInitURL, initFn).Methods(InitHTTPMethod)
r.HandleFunc(M3CoordinatorInitURL, initFn).Methods(InitHTTPMethod)

addRoute(M3DBInitURL, initFn, InitHTTPMethod)
addRoute(M3AggInitURL, initFn, InitHTTPMethod)
addRoute(M3CoordinatorInitURL, initFn, InitHTTPMethod)

// Get
var (
getHandler = NewGetHandler(opts)
getFn = applyMiddleware(getHandler.ServeHTTP, defaults, opts.instrumentOptions)
)
r.HandleFunc(M3DBGetURL, getFn).Methods(GetHTTPMethod)
r.HandleFunc(M3AggGetURL, getFn).Methods(GetHTTPMethod)
r.HandleFunc(M3CoordinatorGetURL, getFn).Methods(GetHTTPMethod)
addRoute(M3DBGetURL, getFn, GetHTTPMethod)
addRoute(M3AggGetURL, getFn, GetHTTPMethod)
addRoute(M3CoordinatorGetURL, getFn, GetHTTPMethod)

// Delete all
var (
deleteAllHandler = NewDeleteAllHandler(opts)
deleteAllFn = applyMiddleware(deleteAllHandler.ServeHTTP, defaults, opts.instrumentOptions)
)
r.HandleFunc(M3DBDeleteAllURL, deleteAllFn).Methods(DeleteAllHTTPMethod)
r.HandleFunc(M3AggDeleteAllURL, deleteAllFn).Methods(DeleteAllHTTPMethod)
r.HandleFunc(M3CoordinatorDeleteAllURL, deleteAllFn).Methods(DeleteAllHTTPMethod)
addRoute(M3DBDeleteAllURL, deleteAllFn, DeleteAllHTTPMethod)
addRoute(M3AggDeleteAllURL, deleteAllFn, DeleteAllHTTPMethod)
addRoute(M3CoordinatorDeleteAllURL, deleteAllFn, DeleteAllHTTPMethod)

// Add
var (
addHandler = NewAddHandler(opts)
addFn = applyMiddleware(addHandler.ServeHTTP, defaults, opts.instrumentOptions)
)
r.HandleFunc(M3DBAddURL, addFn).Methods(AddHTTPMethod)
r.HandleFunc(M3AggAddURL, addFn).Methods(AddHTTPMethod)
r.HandleFunc(M3CoordinatorAddURL, addFn).Methods(AddHTTPMethod)
addRoute(M3DBAddURL, addFn, AddHTTPMethod)
addRoute(M3AggAddURL, addFn, AddHTTPMethod)
addRoute(M3CoordinatorAddURL, addFn, AddHTTPMethod)

// Delete
var (
deleteHandler = NewDeleteHandler(opts)
deleteFn = applyMiddleware(deleteHandler.ServeHTTP, defaults, opts.instrumentOptions)
)
r.HandleFunc(M3DBDeleteURL, deleteFn).Methods(DeleteHTTPMethod)
r.HandleFunc(M3AggDeleteURL, deleteFn).Methods(DeleteHTTPMethod)
r.HandleFunc(M3CoordinatorDeleteURL, deleteFn).Methods(DeleteHTTPMethod)
addRoute(M3DBDeleteURL, deleteFn, DeleteHTTPMethod)
addRoute(M3AggDeleteURL, deleteFn, DeleteHTTPMethod)
addRoute(M3CoordinatorDeleteURL, deleteFn, DeleteHTTPMethod)

// Replace
var (
replaceHandler = NewReplaceHandler(opts)
replaceFn = applyMiddleware(replaceHandler.ServeHTTP, defaults, opts.instrumentOptions)
)
r.HandleFunc(M3DBReplaceURL, replaceFn).Methods(ReplaceHTTPMethod)
r.HandleFunc(M3AggReplaceURL, replaceFn).Methods(ReplaceHTTPMethod)
r.HandleFunc(M3CoordinatorReplaceURL, replaceFn).Methods(ReplaceHTTPMethod)
addRoute(M3DBReplaceURL, replaceFn, ReplaceHTTPMethod)
addRoute(M3AggReplaceURL, replaceFn, ReplaceHTTPMethod)
addRoute(M3CoordinatorReplaceURL, replaceFn, ReplaceHTTPMethod)

// Set
var (
setHandler = NewSetHandler(opts)
setFn = applyMiddleware(setHandler.ServeHTTP, defaults, opts.instrumentOptions)
)
r.HandleFunc(M3DBSetURL, setFn).Methods(SetHTTPMethod)
r.HandleFunc(M3AggSetURL, setFn).Methods(SetHTTPMethod)
r.HandleFunc(M3CoordinatorSetURL, setFn).Methods(SetHTTPMethod)
addRoute(M3DBSetURL, setFn, SetHTTPMethod)
addRoute(M3AggSetURL, setFn, SetHTTPMethod)
addRoute(M3CoordinatorSetURL, setFn, SetHTTPMethod)
}

func newPlacementCutoverNanosFn(
Expand Down Expand Up @@ -386,11 +385,11 @@ func applyMiddleware(
f func(svc handleroptions.ServiceNameAndDefaults, w http.ResponseWriter, r *http.Request),
defaults []handleroptions.ServiceOptionsDefault,
instrumentOpts instrument.Options,
) func(w http.ResponseWriter, r *http.Request) {
) http.Handler {
return logging.WithResponseTimeAndPanicErrorLoggingFunc(
parseServiceMiddleware(f, defaults),
instrumentOpts,
).ServeHTTP
)
}

func parseServiceMiddleware(
Expand Down
14 changes: 14 additions & 0 deletions src/query/api/v1/handler/prometheus/native/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ const (
// handler, this matches the default URL for the query endpoint
// found on a Prometheus server.
PromReadInstantURL = handler.RoutePrefixV1 + "/query"

// PrometheusReadURL is the url for native prom read handler.
soundvibe marked this conversation as resolved.
Show resolved Hide resolved
PrometheusReadURL = "/prometheus" + PromReadURL

// PrometheusReadInstantURL is the url for native instantaneous prom read
// handler
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: . at end

PrometheusReadInstantURL = "/prometheus" + PromReadInstantURL

// M3QueryReadURL is the url for native m3 query read handler.
M3QueryReadURL = "/m3query" + PromReadURL

// M3QueryReadInstantURL is the url for native instantaneous m3 query read
// handler
soundvibe marked this conversation as resolved.
Show resolved Hide resolved
M3QueryReadInstantURL = "/m3query" + PromReadInstantURL
)

var (
Expand Down
30 changes: 8 additions & 22 deletions src/query/api/v1/handler/topic/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@ import (
"github.com/m3db/m3/src/cmd/services/m3query/config"
"github.com/m3db/m3/src/msg/topic"
"github.com/m3db/m3/src/query/api/v1/handler/prometheus/handleroptions"
"github.com/m3db/m3/src/query/util/logging"
xerrors "github.com/m3db/m3/src/x/errors"
"github.com/m3db/m3/src/x/instrument"

"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
"github.com/gorilla/mux"
)

const (
Expand All @@ -58,6 +56,8 @@ type Handler struct {
instrumentOpts instrument.Options
}

type addRouteFn func(path string, handler http.Handler, methods ...string)

// Service gets a topic service from m3cluster client
func Service(clusterClient clusterclient.Client, opts handleroptions.ServiceOptions) (topic.Service, error) {
kvOverride := kv.NewOverrideOptions().
Expand All @@ -71,30 +71,16 @@ func Service(clusterClient clusterclient.Client, opts handleroptions.ServiceOpti

// RegisterRoutes registers the topic routes
func RegisterRoutes(
r *mux.Router,
addRoute addRouteFn,
client clusterclient.Client,
cfg config.Configuration,
instrumentOpts instrument.Options,
) {
wrapped := func(n http.Handler) http.Handler {
return logging.WithResponseTimeAndPanicErrorLogging(n, instrumentOpts)
}

r.HandleFunc(InitURL,
wrapped(newInitHandler(client, cfg, instrumentOpts)).ServeHTTP).
Methods(InitHTTPMethod)
r.HandleFunc(GetURL,
wrapped(newGetHandler(client, cfg, instrumentOpts)).ServeHTTP).
Methods(GetHTTPMethod)
r.HandleFunc(AddURL,
wrapped(newAddHandler(client, cfg, instrumentOpts)).ServeHTTP).
Methods(AddHTTPMethod)
r.HandleFunc(UpdateURL,
wrapped(newUpdateHandler(client, cfg, instrumentOpts)).ServeHTTP).
Methods(UpdateHTTPMethod)
r.HandleFunc(DeleteURL,
wrapped(newDeleteHandler(client, cfg, instrumentOpts)).ServeHTTP).
Methods(DeleteHTTPMethod)
addRoute(InitURL, newInitHandler(client, cfg, instrumentOpts), InitHTTPMethod)
addRoute(GetURL, newGetHandler(client, cfg, instrumentOpts), GetHTTPMethod)
addRoute(AddURL, newAddHandler(client, cfg, instrumentOpts), AddHTTPMethod)
addRoute(UpdateURL, newUpdateHandler(client, cfg, instrumentOpts), UpdateHTTPMethod)
addRoute(DeleteURL, newDeleteHandler(client, cfg, instrumentOpts), DeleteHTTPMethod)
}

func topicName(headers http.Header) string {
Expand Down
Loading