Skip to content

Commit

Permalink
Initial commit for proposed query handlers refactoring:
Browse files Browse the repository at this point in the history
 * updated CustomHandler interface
 * unified how query routes are added. no need to pass router around and wrapping is done in one place.
 * routes are registered with names so they could be easily found later when custom handlers are registered.
  • Loading branch information
soundvibe committed Nov 11, 2020
1 parent 4129dbb commit 69e69d5
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 182 deletions.
17 changes: 3 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 @@ -43,31 +40,23 @@ type Handler struct {

// RegisterRoutes registers the namespace routes
func RegisterRoutes(
r *mux.Router,
addRoute func(path string, handler http.Handler, methods ...string),
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
}
44 changes: 15 additions & 29 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 @@ -104,15 +101,12 @@ func Metadata(store kv.Store) ([]namespace.Metadata, int, error) {

// RegisterRoutes registers the namespace routes.
func RegisterRoutes(
r *mux.Router,
addRoute func(path string, handler http.Handler, methods ...string),
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,
w http.ResponseWriter, r *http.Request),
Expand All @@ -128,40 +122,32 @@ func RegisterRoutes(
}

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

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

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

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

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

// Reset M3DB schemas.
schemaResetHandler := wrapped(
applyMiddleware(NewSchemaResetHandler(client, instrumentOpts).ServeHTTP, defaults))
r.HandleFunc(M3DBSchemaURL, schemaResetHandler.ServeHTTP).Methods(DeleteHTTPMethod)
schemaResetHandler := applyMiddleware(NewSchemaResetHandler(client, instrumentOpts).ServeHTTP, defaults)
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 := applyMiddleware(NewReadyHandler(client, clusters, instrumentOpts).ServeHTTP, defaults)
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
28 changes: 6 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 Down Expand Up @@ -71,30 +69,16 @@ func Service(clusterClient clusterclient.Client, opts handleroptions.ServiceOpti

// RegisterRoutes registers the topic routes
func RegisterRoutes(
r *mux.Router,
addRoute func(path string, handler http.Handler, methods ...string),
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

0 comments on commit 69e69d5

Please sign in to comment.