diff --git a/dax/controller/balancer/balancer.go b/dax/controller/balancer/balancer.go index 6e7686f23..560e6bc29 100644 --- a/dax/controller/balancer/balancer.go +++ b/dax/controller/balancer/balancer.go @@ -672,6 +672,10 @@ func (b *Balancer) WorkersForTable(tx dax.Transaction, roleType dax.RoleType, qt return workers, nil } +func (b *Balancer) GetDatabaseNumberOfWorkers(tx dax.Transaction, roleType dax.RoleType, qdbid dax.QualifiedDatabaseID) (int, error) { + return b.current.WorkerCount(tx, roleType, qdbid) +} + func (b *Balancer) removeJob(tx dax.Transaction, roleType dax.RoleType, qdbid dax.QualifiedDatabaseID, job dax.Job) (InternalDiffs, error) { if addr, ok, err := b.workerForJob(tx, roleType, qdbid, job); err != nil { return nil, errors.Wrapf(err, "getting worker for job: %s", job) diff --git a/dax/controller/controller.go b/dax/controller/controller.go index e5afd553a..9ab90d73a 100644 --- a/dax/controller/controller.go +++ b/dax/controller/controller.go @@ -784,6 +784,19 @@ func (c *Controller) Databases(ctx context.Context, orgID dax.OrganizationID, id return c.Schemar.Databases(tx, orgID, ids...) } +// get the list of workers being used by a database +func (c *Controller) GetDatabaseNumberOfWorkers(ctx context.Context, qdbid dax.QualifiedDatabaseID) (int, error) { + tx, err := c.Transactor.BeginTx(ctx, false) + if err != nil { + return -1, errors.Wrap(err, "beginning tx") + } + defer tx.Rollback() + + // get the number of workers for the database from the balancer + workers, err := c.Balancer.GetDatabaseNumberOfWorkers(tx, qdbid) + +} + // CreateTable adds a table to the schemar, and then sends directives to all // affected nodes based on the change. func (c *Controller) CreateTable(ctx context.Context, qtbl *dax.QualifiedTable) error { diff --git a/dax/controller/http/handler.go b/dax/controller/http/handler.go index 903ac380e..e4c8489c2 100644 --- a/dax/controller/http/handler.go +++ b/dax/controller/http/handler.go @@ -25,6 +25,7 @@ func Handler(c *controller.Controller) http.Handler { router.HandleFunc("/database-by-name", server.postDatabaseByName).Methods("POST").Name("PostDatabaseByName") router.HandleFunc("/databases", server.postDatabases).Methods("POST").Name("PostDatabases") router.HandleFunc("/database/options", server.patchDatabaseOptions).Methods("PATCH").Name("PatchDatabaseOptions") + router.HandleFunc("/database-number-of-workers", server.getDatabaseNumberOfWorkers).Methods("GET").Name("DatabaseWorkers") router.HandleFunc("/create-table", server.postCreateTable).Methods("POST").Name("PostCreateTable") router.HandleFunc("/drop-table", server.postDropTable).Methods("POST").Name("PostDropTable") @@ -214,6 +215,45 @@ func (s *server) patchDatabaseOptions(w http.ResponseWriter, r *http.Request) { } } +// get the number of workers for a database +// receive request + +// decode the id + +// send the id into the controller, get the number of workers back + +// send that number back to the client via request + +// create a response struct +func (s *server) getDatabaseNumberOfWorkers(w http.ResponseWriter, r *http.Request) { + // get the context + ctx := r.Context() + + // create a variable to hold the id + var id dax.QualifiedDatabaseID + + // for now, fill the id with a hard-coded value + id = dax.QualifiedDatabaseID{ + OrganizationID: dax.OrganizationID("org1"), + DatabaseID: dax.DatabaseID("db1"), + } + + // send the id into the controller, get the number of workers back + numWorkers, err := s.controller.GetDatabaseNumberOfWorkers(ctx, id) + + // handle error + if err != nil { + http.Error(w, errors.MarshalJSON(err), http.StatusBadRequest) + return + } + + // send the number back to the client + if err := json.NewEncoder(w).Encode(numWorkers); err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } +} + // DatabaseOptionRequest represents a change to a database option. The thinking // is to only support changing one database option at a time to keep the // implementation sane. At time of writing, only WorkersMin is supported.