mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-08-28 10:54:59 +00:00
* Introduce ServiceManager and Refactor DAX Integration tests The ServiceManager provides an interface with which to manage featurebase (dax) services (mds, queryer, computer). It replaces the confusing interface implementations in /dax/server/server.go (which optionally used pointers to in-process objects to satisfy an interface) with (for now) http implementations. The thought is that even if we're running all services in-process, we should communicate between services over http in order to mirror what we would do in a production environment where the services are running on different nodes. This batch of commits does quit a lot, most of which is captured here: - Added `path` support to `dax.Address`. Address is now a string of the form [scheme]://[host]:[port]/[path]. - Added `Holder.directiveApplied` to determine (in tests) if the computer has completed applying the latest directive. This is somewhat temporary until we improve the mds-to-computer logic. - Removed the "service prefix" code which was prepending client URL paths with the prefix. Instead, the serviceType (mds, queryer, computer[n] is now part of `dax.Address`). - Removed, from the dax config, the top level `StorageMethod` and `StorageDSN` and now just have `MDS.Config.DataDir`. - Added `Computer.Config.N` to specify the number of computers to run in-process. - Moved the `pilosa.MDS` interface to `computer.Registrar`. This is an example of getting the interfaces defined in the right packages. - Added `SnapshotTable()` method to the mds client (to align with its API). - Changed `Balancer.AddJob()` to `Balancer.AddJobs()` to support, for example, adding 256 partitions in a single call. Refactored some of the naive Balancer to account for this. - Added a `Seed` to the top-level config. It's not really useful because of package `crypto/rand`. - Added an in-memory implementation of the DisCo interface and disabled etcd in a computer service. - Create sepearte data-dirs for each in-process computer. - Disabled grpc in dax. - Modified the sql3 test definition format to support multiple insert steps and separate query results (to align with those steps). * Changes necessary to get multiple computer instance running in-process For now the config looks like this: ``` [computer] run = true n = 4 ``` but we can probably just change that to be something like: ``` [computer] run = 4 ``` *Issues found running multiple "computers" in-process* - grpc was trying to bind on the same port - changed GRPCListener from `*net.TCPListener` to `net.Listener` - created a nopListener and set to that for now (i.e. disabled grpc) - etcd was starting more than once - changed dax to use in-memory implementations of the disco interfaces (i.e. stop using etcd) - IDAllocator (which uses boltdb) was trying to open the `idalloc.db` file more than once - realized we have to set separate data-dirs for each holder. that fixed it. * Port dax integration tests to ManagedCommand * Modify Balancer-related methods like AddJob to AddJobs There were (and still are) a lot of places where we were adding on job at a time, even when we had a long list of jobs to add. This resulted in every job add (for example adding 1 of 256 shards) taking ~40ms, or over 10s to create a keyed table. One reason was because each job add was making multiple boltdb transactions. * Port over more dax integration test stuff * Add DirectiveApplied to signify that snapshot/writes have loaded. We use this in tests to avoid using sleeps. This should be considered temporary; we're going to need a more robust solution for determining when a computer node is ready to serve complete data. * Finish porting dax integration tests * Improve godocs * Remove docker-based DAX integration tests. * go mod tidy * Move test/managed.go to avoid package conflicts * Modify IDK integration tests to work with ServiceManager changes This is really just computer -> computer0 And the MDS DataDir config change. * cleanup found during review * echo $CI_COMMIT_REF_SLUG in CI * remove docker image arg, use build instead
244 lines
6.6 KiB
Go
244 lines
6.6 KiB
Go
// Copyright 2021 Molecula Corp. All rights reserved.
|
|
package test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/molecula/featurebase/v3/dax"
|
|
computersvc "github.com/molecula/featurebase/v3/dax/computer/service"
|
|
"github.com/molecula/featurebase/v3/dax/mds"
|
|
mdssvc "github.com/molecula/featurebase/v3/dax/mds/service"
|
|
"github.com/molecula/featurebase/v3/dax/queryer"
|
|
queryersvc "github.com/molecula/featurebase/v3/dax/queryer/service"
|
|
"github.com/molecula/featurebase/v3/dax/server"
|
|
"github.com/molecula/featurebase/v3/errors"
|
|
fbtest "github.com/molecula/featurebase/v3/test"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// ManagedCommand represents a test wrapper for server.Command.
|
|
type ManagedCommand struct {
|
|
*server.Command
|
|
|
|
svcmgr *dax.ServiceManager
|
|
|
|
started bool
|
|
}
|
|
|
|
// Manage returns the ServiceManager for the ManagedCommand.
|
|
func (mc *ManagedCommand) Manage() *dax.ServiceManager {
|
|
return mc.svcmgr
|
|
}
|
|
|
|
// Address returns the advertise address at which the command can be reached.
|
|
func (mc *ManagedCommand) Address() dax.Address {
|
|
uri := mc.URI()
|
|
return dax.Address(uri.String())
|
|
}
|
|
|
|
// Start starts the embedded command.
|
|
func (mc *ManagedCommand) Start() error {
|
|
if mc.started {
|
|
return nil
|
|
}
|
|
|
|
if err := mc.Command.Start(); err != nil {
|
|
return errors.Wrap(err, "starting command")
|
|
}
|
|
mc.started = true
|
|
|
|
return nil
|
|
}
|
|
|
|
// Close closes the embedded command.
|
|
func (mc *ManagedCommand) Close() error {
|
|
return mc.Command.Close()
|
|
}
|
|
|
|
// NewMDS adds a new MDSService to the ManagedCommands ServiceManager.
|
|
func (mc *ManagedCommand) NewMDS(cfg mds.Config) dax.ServiceKey {
|
|
uri := mc.URI()
|
|
cfg.Logger = mc.svcmgr.Logger
|
|
mc.svcmgr.MDS = mdssvc.New(uri, mds.New(cfg))
|
|
|
|
return dax.ServicePrefixMDS
|
|
}
|
|
|
|
// NewQueryer adds a new QueryerService to the ManagedCommands ServiceManager.
|
|
func (mc *ManagedCommand) NewQueryer(cfg queryer.Config) dax.ServiceKey {
|
|
uri := mc.URI()
|
|
logger := mc.svcmgr.Logger
|
|
cfg.Logger = logger
|
|
mc.svcmgr.Queryer = queryersvc.New(uri, queryer.New(cfg), logger)
|
|
|
|
var mdsAddr dax.Address
|
|
if cfg.MDSAddress != "" {
|
|
mdsAddr = dax.Address(cfg.MDSAddress + "/" + dax.ServicePrefixMDS)
|
|
} else if mc.svcmgr.MDS != nil {
|
|
mdsAddr = mc.svcmgr.MDS.Address()
|
|
}
|
|
|
|
// Set MDS
|
|
if err := mc.svcmgr.Queryer.SetMDS(mdsAddr); err != nil {
|
|
logger.Panicf(errors.Wrap(err, "setting mds").Error())
|
|
}
|
|
|
|
return dax.ServicePrefixQueryer
|
|
}
|
|
|
|
// NewComputer adds a new ComputerService to the ManagedCommands ServiceManager.
|
|
func (mc *ManagedCommand) NewComputer() dax.ServiceKey {
|
|
cfg := computersvc.CommandConfig{
|
|
WriteLoggerRun: mc.Config.WriteLogger.Run,
|
|
WriteLoggerConfig: mc.Config.WriteLogger.Config,
|
|
SnapshotterRun: mc.Config.Snapshotter.Run,
|
|
SnapshotterConfig: mc.Config.Snapshotter.Config,
|
|
ComputerConfig: mc.Config.Computer.Config,
|
|
|
|
RootDataDir: mc.Config.Computer.Config.DataDir,
|
|
|
|
Stderr: os.Stderr,
|
|
Logger: mc.svcmgr.Logger,
|
|
}
|
|
|
|
cfg.ComputerConfig.MDSAddress = mc.svcmgr.MDS.Address().String()
|
|
|
|
// Add new computer service.
|
|
return mc.svcmgr.AddComputer(
|
|
computersvc.New(mc.Address(), cfg, cfg.Logger))
|
|
}
|
|
|
|
// Healthy returns true if the provided service's /health endpoint returns 200
|
|
// OK. This means that the service has been added to the ServiceManager and
|
|
// started, and that its http handler has been dynamically added.
|
|
func (mc *ManagedCommand) Healthy(key dax.ServiceKey) bool {
|
|
if key == "" {
|
|
return false
|
|
}
|
|
|
|
addr := mc.Address()
|
|
url := fmt.Sprintf("%s/%s/health", addr.WithScheme("http"), key)
|
|
log.Println("HEALTH URL:", url)
|
|
res, err := http.Get(url)
|
|
if err != nil {
|
|
return false
|
|
} else if res.StatusCode != http.StatusOK {
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
// WaitForApplied is a test helper function which retries a computer's
|
|
// /directive endpoint a specified number of times, along with a sleep time in
|
|
// between tries, until the computer returns applied=true.
|
|
func (mc *ManagedCommand) WaitForApplied(t *testing.T, key dax.ServiceKey, n int, sleep time.Duration) {
|
|
t.Helper()
|
|
|
|
addr := mc.Address()
|
|
url := fmt.Sprintf("%s/%s/directive", addr.WithScheme("http"), key)
|
|
log.Println("WAIT URL:", url)
|
|
|
|
for i := 0; i < n; i++ {
|
|
resp, err := http.Get(url)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
body := resp.Body
|
|
defer body.Close()
|
|
|
|
got := struct {
|
|
Applied bool `json:"applied"`
|
|
}{}
|
|
assert.NoError(t, json.NewDecoder(body).Decode(&got))
|
|
|
|
if got.Applied {
|
|
return
|
|
}
|
|
|
|
t.Logf("Wait (%d/%d): url: %s (sleep: %s)\n", i, n, url, sleep.String())
|
|
if i < n-1 {
|
|
time.Sleep(sleep)
|
|
}
|
|
}
|
|
|
|
// Getting to here means the directive endpoint never returned successfully,
|
|
// so we need to stop the test.
|
|
t.Fatal("WaitForApplied timed out")
|
|
}
|
|
|
|
// NewManagedCommand returns a new instance of Command.
|
|
func NewManagedCommand(tb fbtest.DirCleaner, opts ...server.CommandOption) *ManagedCommand {
|
|
path := tb.TempDir()
|
|
|
|
svcmgr := dax.NewServiceManager()
|
|
opts = append(opts, server.OptCommandServiceManager(svcmgr))
|
|
|
|
mc := &ManagedCommand{}
|
|
output := io.Discard
|
|
if testing.Verbose() {
|
|
output = os.Stderr
|
|
}
|
|
mc.Command = server.NewCommand(output, opts...)
|
|
mc.svcmgr = svcmgr
|
|
|
|
mc.Config.Bind = "http://localhost:0"
|
|
mc.Config.MDS.Config.DataDir = path + "/mds"
|
|
mc.Config.Computer.Config.DataDir = path
|
|
mc.Config.WriteLogger.Config.DataDir = path + "/wl"
|
|
mc.Config.Snapshotter.Config.DataDir = path + "/sn"
|
|
|
|
return mc
|
|
}
|
|
|
|
// DefaultConfig includes a single instance of each service type.
|
|
func DefaultConfig() *server.Config {
|
|
cfg := server.NewConfig()
|
|
cfg.Verbose = true
|
|
cfg.MDS.Run = true
|
|
cfg.MDS.Config.RegistrationBatchTimeout = 0
|
|
cfg.Queryer.Run = true
|
|
cfg.Computer.Run = true
|
|
cfg.Computer.N = 1
|
|
cfg.WriteLogger.Run = true
|
|
cfg.Snapshotter.Run = true
|
|
return cfg
|
|
}
|
|
|
|
// MustRunManagedCommand starts an in-process set of Services based on the
|
|
// provided configuration. If no configuration is provided, it will use the
|
|
// DefaultConfig which consists of one instance of each service type.
|
|
func MustRunManagedCommand(tb testing.TB, opts ...server.CommandOption) *ManagedCommand {
|
|
// If no opts are passed, use the default configuration which includes a
|
|
// single instance of each service type. This is really just meant to keep
|
|
// test code a bit cleaner when it's not necessary to have a custom service
|
|
// configuration.
|
|
var basic bool
|
|
if len(opts) == 0 {
|
|
opts = []server.CommandOption{
|
|
server.OptCommandConfig(DefaultConfig()),
|
|
}
|
|
basic = true
|
|
}
|
|
|
|
mc := NewManagedCommand(tb, opts...)
|
|
|
|
if err := mc.Start(); err != nil {
|
|
tb.Fatalf("starting managed command: %v", err)
|
|
}
|
|
|
|
if basic {
|
|
assert.True(tb, mc.Healthy(dax.ServicePrefixMDS))
|
|
assert.True(tb, mc.Healthy(dax.ServicePrefixQueryer))
|
|
assert.True(tb, mc.Healthy(dax.ServicePrefixComputer+"0"))
|
|
}
|
|
|
|
return mc
|
|
}
|