Documentation
¶
Overview ¶
Package target provides the sqi-server a load run drives: a managed one the harness starts, configures and stops itself, or an attached one somebody else runs, on which the harness creates and later deletes its own farm.
Index ¶
- Variables
- func BuildBinaries(ctx context.Context, outDir string) (server, worker, noop string, err error)
- func NewClient(t Target, opts ...client.Option) (*client.Client, error)
- func PoolPrefix(runID string) string
- func RunPoolName(runID, name string) string
- type AttachConfig
- type Cleaned
- type CleanupConfig
- type Faults
- type Incarnation
- type Lane
- type ManagedConfig
- type Target
Constants ¶
This section is empty.
Variables ¶
var ErrWorkerRunning = errors.New("target: real worker is still running")
ErrWorkerRunning is wrapped by the error of a RestartWorker call on a real worker whose process is still running.
Functions ¶
func BuildBinaries ¶
BuildBinaries builds sqi-server, sqi-worker and the no-op task command into outDir with one go build, and returns their paths. A build of the same git revision with a clean working tree (untracked files do not count) is reused; a dirty tree always rebuilds. It needs the sqi source: the module containing the working directory, or the one this package was compiled from.
Concurrent runs may share outDir: a lock file serializes their builds, and a rebuild goes to a scratch directory first and then moves each binary into place, moving the old one aside rather than overwriting it, since Windows refuses to overwrite an executable another run is still running.
func NewClient ¶
NewClient returns a REST client for t, carrying its API key and trusting its CA. It retries throttled calls (429 and 503, but never a submission) for up to client.DefaultRetryBudget each; opts are applied after, so client.WithRetry(0) turns that off.
func PoolPrefix ¶
PoolPrefix is the prefix every usage pool of run runID carries: load_<runID>_, with the run ID made an identifier.
func RunPoolName ¶
RunPoolName returns the usage pool an attached run creates for the scenario's pool name: load_<runID>_<name>, with every character outside letters, digits and '_' turned into '_' (a capability name is made of identifiers), at most 76 characters. A name that does not fit beside the run ID is shortened and suffixed with a hash of itself; the load_<runID>_ prefix is always kept, which is how cleanup finds a run's pools. Pools are global to the server, so a run claiming them by their scenario names would take slots from a production pool of the same name.
Types ¶
type AttachConfig ¶
type AttachConfig struct {
HTTPURL, NATSURL string
// APIKey is sent on every call; "" when the server's auth gate is off.
APIKey string
// JoinToken is a reusable join token each simulated worker enrolls with.
// It is required unless NoAuth says the broker needs no credential.
JoinToken string
// DataDir is the server's data directory when the harness can read it,
// for disk measurements; "" otherwise.
DataDir string
TLS simworker.TLSFiles
// PostgresDSN is the server's PostgreSQL database when the harness can
// reach it, for measuring its size; "" otherwise.
PostgresDSN string
// NoAuth says the server's broker authentication is off.
NoAuth bool
// Keep leaves the run's farm on the server after Close.
Keep bool
// SharedPools says other runs use the scenario's usage pools at the same
// time, as the shards of one run do: Close then leaves every pool in
// place, created by this run or not, and logs their names. Pools are
// global, so a shard deleting one would pull it from under the others,
// and the server refuses while any claim names it.
Scenario *scenario.Scenario
// RunID names the run's farm, load-<RunID>.
RunID string
// PoolRunID names the run's usage pools, load_<PoolRunID>_<pool> (see
// [RunPoolName]); "" means RunID. The shards of one run pass the run's
// own ID here, so they share their pools while each has a farm.
PoolRunID string
// Logger receives the target's own progress; nil discards it.
Logger *slog.Logger
}
AttachConfig configures Attach.
type Cleaned ¶
type Cleaned struct {
// Farms are the names of the farms removed, with their jobs, workers and
// queues.
Farms []string
// Pools are the names of the usage pools removed.
Pools []string
}
Cleaned is what Cleanup removed.
func Cleanup ¶
func Cleanup(ctx context.Context, cfg CleanupConfig) (Cleaned, error)
Cleanup removes what an attached run left on a server, for a run whose teardown failed or that was kept: the run's farm, load-<RunID>, or the shards' farms, load-<RunID>-s<i>, each with its jobs, workers and queues, then the run's usage pools, the ones named load_<RunID>_ (see RunPoolName). It touches nothing else: a pool of another load run whose farm is still on the server is left alone even when its name starts the same way, and a pool a job still claims is refused by the server.
Like a run's own teardown it waits, up to two minutes, for the farm's workers to go offline, and it retries throttled calls. It keeps going after an error, so it removes as much as it can, and returns the errors joined.
type CleanupConfig ¶
type CleanupConfig struct {
HTTPURL string
// APIKey is sent on every call; "" when the server's auth gate is off.
APIKey string
TLS simworker.TLSFiles
// RunID is the run whose leftovers to remove: the --run-id it ran with,
// shared by the shards of a sharded run.
RunID string
// Logger receives one line per thing removed; nil discards it.
Logger *slog.Logger
}
CleanupConfig configures Cleanup.
type Faults ¶
type Faults interface {
// KillServer kills sqi-server alone (SIGKILL, or TerminateProcess on
// Windows) and returns once it is reaped.
KillServer(ctx context.Context) error
// TermServer stops sqi-server gracefully, the way Close does.
TermServer(ctx context.Context) error
// StallServer stops sqi-server with SIGSTOP (POSIX only) and, on Linux,
// returns once every thread of it has stopped.
StallServer() error
// ResumeServer continues a stalled sqi-server with SIGCONT.
ResumeServer() error
// RestartServer starts sqi-server again with the same server.yaml, so on
// the same ports, and waits for /readyz. The server must not be running.
RestartServer(ctx context.Context) error
// RealWorkers returns the indexes (0-based, in start order) of the
// group's real workers.
RealWorkers(group string) []int
// KillWorker kills real worker i's process alone, leaving the tasks it
// started running, and returns once it is reaped.
KillWorker(ctx context.Context, i int) error
// TermWorker stops real worker i gracefully.
TermWorker(ctx context.Context, i int) error
// StallWorker stops real worker i's process with SIGSTOP (POSIX only),
// then the process group of every task its current incarnation is
// running, as the markers show them, so a sleeping machine's renders
// stop too.
StallWorker(i int) error
// ResumeWorker continues what StallWorker stopped, in reverse order of
// the stops: the tasks' groups first, then the worker.
ResumeWorker(i int) error
// RestartWorker starts real worker i again as a new process, a new
// incarnation, on the same data directory, so with the same worker ID.
// The worker must not be running: if it is, the error wraps
// [ErrWorkerRunning].
RestartWorker(ctx context.Context, i int) error
// RealLive is the number of real worker processes running now.
RealLive() int
// Incarnations lists every real worker process the run started, in start
// order.
Incarnations() []Incarnation
// Lane is the fault proxy between the group's workers and the broker,
// nil when the scenario has no network fault.
Lane(group string) Lane
// MarkerDir is where real tasks write their markers, "" when no job class
// has real_task.
MarkerDir() string
// StopForChecks stops the real workers, then the server, gracefully,
// keeping the data directory and the database for the checks.
StopForChecks(ctx context.Context) error
// StoreOptions says how to open the run's store once the server is
// stopped.
StoreOptions() backend.Options
// StallDatabase stalls the store: on SQLite an outside connection takes
// the write lock, on PostgreSQL the proxy in front of the server's
// connections forwards nothing.
StallDatabase(ctx context.Context) error
// DropDatabase cuts the server's PostgreSQL connections and refuses new
// ones. PostgreSQL only: on SQLite it is an error.
DropDatabase(ctx context.Context) error
// HealDatabase releases the lock or heals the proxy. It does nothing when
// no database fault is applied.
HealDatabase(ctx context.Context) error
// FillVolume fills the volume "server" or "workers" with ballast, leaving
// headroom bytes free and, with inodes, no inodes. It is an error when the
// target has no such volume. Filling a filled volume is a no-op.
FillVolume(volume string, headroom int64, inodes bool) error
// FreeVolume removes the volume's ballast. It does nothing when the volume
// is not filled and is an error when the target has no such volume.
FreeVolume(volume string) error
// LimitServerFDs lowers the running sqi-server's soft RLIMIT_NOFILE so that it
// can open headroom more files, leaving the hard limit, and returns the
// open-file count it saw. Limiting a limited server is a no-op that returns
// the first count. Linux only; elsewhere, with a server running, it returns
// errFDLimitUnsupported.
LimitServerFDs(headroom int) (open int, err error)
// RestoreServerFDs puts back the soft limit LimitServerFDs replaced. It does
// nothing when none is applied or the server is gone.
RestoreServerFDs() error
// ServerReady probes the server's /readyz: "ready", or the probe's error
// text.
ServerReady(ctx context.Context) (status string)
// RealWorkerID is real worker i's worker ID, read from its data directory's
// worker.id. It is an error until the worker has started once.
RealWorkerID(i int) (string, error)
// RevokeWorker removes real worker i's join-token file, then revokes its
// credential through the REST API, and returns its worker ID. Without the
// file a restarted worker cannot enroll again. The run must have broker
// authentication.
RevokeWorker(ctx context.Context, i int) (workerID string, err error)
// ServerLog is the path of the server's log, which every incarnation
// appends to.
ServerLog() string
}
Faults is what a fault controller drives. Only the managed target has it.
type Incarnation ¶
type Incarnation struct {
// ID is "<worker index>.<restart count>", e.g. "0.0", "0.1". Each real
// worker process gets it as SQILOAD_INCARNATION.
ID string
Worker int
Started time.Time
// Ended is set when the harness kills or stops the process, or starts the
// worker's next incarnation; until then it is zero, also for a process
// that exited on its own.
Ended time.Time
}
Incarnation is one real worker process.
type Lane ¶
type Lane interface {
Cut()
Blackhole()
Shape(delay, jitter time.Duration, bytesPerSec int)
Heal()
}
Lane is a worker group's connection to the broker, which a fault can cut, blackhole or shape. *netfault.Proxy implements it.
type ManagedConfig ¶
type ManagedConfig struct {
// RunDir holds everything of the run: server.yaml, server.log, data/,
// tls/ and workers/.
RunDir string
// RunID names the farm (load-<RunID>) and a created PostgreSQL database;
// "" means RunDir's base name.
RunID string
// ServerBin, WorkerBin and NoopBin are the binaries the target runs.
// ServerBin is required; WorkerBin and NoopBin are needed only when the
// scenario has real workers. ResolveBinaries builds those not given.
ServerBin, WorkerBin, NoopBin string
// Scenario is the run's scenario. Each worker group's Real count is
// started as real sqi-worker processes, of which only the group's name,
// Queues, Tags and Heartbeat apply.
Scenario *scenario.Scenario
// PostgresDSN is the store when Scenario.Server.Store is postgres. A DSN
// naming the maintenance database "postgres" is administrative: the
// harness creates sqi_load_<RunID> through it and drops it on Close. Any
// other DSN names an empty database the harness uses as it is.
PostgresDSN string
// Logger receives the target's own progress; nil discards it.
Logger *slog.Logger
// Seed is the run seed, mixed into each broker lane's jitter; 0 seeds a
// lane with its group's index alone.
Seed uint64
// ServerVolume, when set, is a directory on a size-limited file system
// that holds the server's data: its data directory is
// ServerVolume/<RunID>. A disk_full fault fills that volume.
ServerVolume string
// WorkerVolume, when set, is the same for the real workers' session
// directories: each is WorkerVolume/<RunID>/<n>-sessions, so a task's
// working directory is on it.
WorkerVolume string
// contains filtered or unexported fields
}
ManagedConfig configures StartManaged.
func (*ManagedConfig) ResolveBinaries ¶
ResolveBinaries fills in the binaries c does not name, building sqi-server, sqi-worker and the no-op task command into dir when one is missing: sqi-server always, and the other two when workers says the run starts real workers.
type Target ¶
type Target interface {
// HTTPURL is the base URL of the REST and WebSocket API.
HTTPURL() string
// NATSURL is the broker URL workers dial.
NATSURL() string
// APIKey is the bearer key every REST and WebSocket call sends, "" when
// the server's auth gate is off.
APIKey() string
// FarmID is the farm the run created.
FarmID() string
// QueueIDs maps each scenario queue name to its id.
QueueIDs() map[string]string
// DataDir is the server's data directory, "" when the harness cannot
// reach it.
DataDir() string
// Credentials supplies each simulated worker's broker credential.
Credentials() simworker.Credentials
// TLS is the broker and API TLS material, the zero value without TLS.
TLS() simworker.TLSFiles
// NoopCommand is the command a job's tasks run: the no-op real workers
// execute. Simulated workers never execute it.
NoopCommand() scenario.Command
// PoolName is the name the server knows the scenario's usage pool name
// by: on an attached server the run's own namespaced pool (see
// [RunPoolName]), on a managed one name itself. "" stays "".
PoolName(name string) string
// PostgresDSN is the DSN of the server's PostgreSQL database, for
// measuring its size: a managed target's own (the one it created, when
// given an administrative DSN), an attached target's as configured, and
// "" for SQLite or an unknown store.
PostgresDSN() string
// StoreLabel names the server's store for the run's record.
StoreLabel() string
// Close releases what the target created: a managed target stops every
// process it started, an attached one deletes its farm unless kept. It is
// safe to call more than once.
Close(ctx context.Context) error
}
Target is the server a run drives and the farm it owns there.
func Attach ¶
func Attach(ctx context.Context, cfg AttachConfig) (Target, error)
Attach creates the run's farm, its queues and its usage pools on a running server. The pools are the run's own, named load_<PoolRunID>_<pool>, so the run never claims slots from a pool somebody else uses; one that already exists fails the setup unless SharedPools says another shard of the run made it. If setup fails part way, Attach deletes what it created.
func StartManaged ¶
func StartManaged(ctx context.Context, cfg ManagedConfig) (Target, error)
StartManaged starts an sqi-server configured for the scenario, waits for it to be ready, creates the run's farm, queues and usage pools, and starts the real workers. On any error it stops whatever it had started before returning.