featurebase/dax/controller/service/controller.go
Matthew Jaffee 8fe73146c8
Sqldb rip boltdb (#2341)
* serverless sqldb use same env for test config as normal

* rip boltdb implementation of controller backend out

it was replaced by postgres and no longer works properly.

This involved migrating a number of tests which only worked with
boltdb, which exposed several ways in which the postgres
implementation had slightly different behavior from the bolt
one:
1. ordering of results in some cases, and
2. (more importantly) erroring when a record to delete was not
found. The bolt implementation silently ignored it when things to
delete weren't found, so we make some changes to match that behavior.

Also stopped propagating CreatedAt and UpdatedAt from DB tables into
dax types. These were breaking existing tests. Perhaps it would be
better to actually use them, but for now they will only exist at the
DB level.

This change set also moves the insertion of the directive_versions
record out of migrations and into the startup/connection code. Having
this in the migrations was a bit ugly because you couldn't just
truncate all the tables and have everything work from
scratch. Inserting it during startup is fairly innocuous, and will
just continue on if it already exists.

* update directive_version test

I changed the initial value to 0 so that the first version that gets
sent out is 1
2023-03-22 08:54:13 -05:00

88 lines
2.2 KiB
Go

package service
import (
"net/http"
"os"
"github.com/featurebasedb/featurebase/v3/dax"
"github.com/featurebasedb/featurebase/v3/dax/controller"
controllerhttp "github.com/featurebasedb/featurebase/v3/dax/controller/http"
"github.com/featurebasedb/featurebase/v3/dax/controller/sqldb"
"github.com/featurebasedb/featurebase/v3/errors"
"github.com/featurebasedb/featurebase/v3/logger"
fbnet "github.com/featurebasedb/featurebase/v3/net"
)
// Ensure type implements interface.
var _ dax.Service = (*controllerService)(nil)
type controllerService struct {
uri *fbnet.URI
controller *controller.Controller
logger logger.Logger
}
func New(uri *fbnet.URI, cfg controller.Config) *controllerService {
// Set up logger.
var logr logger.Logger = logger.StderrLogger
if cfg.Logger != nil {
logr = cfg.Logger.WithPrefix("Controller: ")
}
controller := controller.New(cfg)
controllerSvc := &controllerService{
uri: uri,
controller: controller,
logger: logr,
}
// Storage methods.
switch cfg.StorageMethod {
case "sqldb":
controller.Schemar = sqldb.NewSchemar(logr)
controller.Balancer = sqldb.NewBalancer(logr)
controller.DirectiveVersion = sqldb.NewDirectiveVersion(logr)
transactor, err := sqldb.NewTransactor(cfg.SQLDB, logr)
if err != nil {
logr.Printf("setting up new transactor: %v", err)
os.Exit(1)
}
controller.Transactor = transactor
default:
logr.Printf("storagemethod %s not supported, only 'sqldb' is currently accepted.", cfg.StorageMethod)
os.Exit(1)
}
if cfg.Director != nil {
controller.Director = cfg.Director
}
return controllerSvc
}
func (m *controllerService) Start() error {
// Start controller service.
if err := m.controller.Start(); err != nil {
return errors.Wrap(err, "starting controller")
}
return nil
}
func (m *controllerService) Stop() error {
err := m.controller.Stop()
if err != nil {
m.logger.Warnf("error stopping controller: %v", err)
}
return err
}
func (m *controllerService) Address() dax.Address {
return dax.Address(m.uri.HostPort() + "/" + dax.ServicePrefixController)
}
func (m *controllerService) HTTPHandler() http.Handler {
return controllerhttp.Handler(m.controller)
}