Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: clean up string interpolation #1257

Merged
merged 1 commit into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions backend/controller/dal/dal.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func WithReservation(ctx context.Context, reservation Reservation, fn func() err
func New(ctx context.Context, pool *pgxpool.Pool) (*DAL, error) {
conn, err := pool.Acquire(ctx)
if err != nil {
return nil, fmt.Errorf("%s: %w", "failed to acquire PG PubSub connection", err)
return nil, fmt.Errorf("failed to acquire PG PubSub connection: %w", err)
}
dal := &DAL{
db: sql.NewDB(pool),
Expand Down Expand Up @@ -269,23 +269,23 @@ func (d *DAL) GetStatus(
) (Status, error) {
controllers, err := d.GetControllers(ctx, allControllers)
if err != nil {
return Status{}, fmt.Errorf("%s: %w", "could not get control planes", translatePGError(err))
return Status{}, fmt.Errorf("could not get control planes: %w", translatePGError(err))
}
runners, err := d.db.GetActiveRunners(ctx, allRunners)
if err != nil {
return Status{}, fmt.Errorf("%s: %w", "could not get active runners", translatePGError(err))
return Status{}, fmt.Errorf("could not get active runners: %w", translatePGError(err))
}
deployments, err := d.db.GetActiveDeployments(ctx)
if err != nil {
return Status{}, fmt.Errorf("%s: %w", "could not get active deployments", translatePGError(err))
return Status{}, fmt.Errorf("could not get active deployments: %w", translatePGError(err))
}
ingressRoutes, err := d.db.GetAllIngressRoutes(ctx, allIngressRoutes)
if err != nil {
return Status{}, fmt.Errorf("%s: %w", "could not get ingress routes", translatePGError(err))
return Status{}, fmt.Errorf("could not get ingress routes: %w", translatePGError(err))
}
routes, err := d.db.GetRoutingTable(ctx, nil)
if err != nil {
return Status{}, fmt.Errorf("%s: %w", "could not get routing table", translatePGError(err))
return Status{}, fmt.Errorf("could not get routing table: %w", translatePGError(err))
}
statusDeployments, err := slices.MapErr(deployments, func(in sql.GetActiveDeploymentsRow) (Deployment, error) {
labels := model.Labels{}
Expand Down Expand Up @@ -417,7 +417,7 @@ func (d *DAL) CreateDeployment(ctx context.Context, language string, moduleSchem
// Start the transaction
tx, err := d.db.Begin(ctx)
if err != nil {
return model.DeploymentKey{}, fmt.Errorf("%s: %w", "could not start transaction", err)
return model.DeploymentKey{}, fmt.Errorf("could not start transaction: %w", err)
}

defer tx.CommitOrRollback(ctx, &err)
Expand All @@ -436,27 +436,27 @@ func (d *DAL) CreateDeployment(ctx context.Context, language string, moduleSchem

schemaBytes, err := proto.Marshal(moduleSchema.ToProto())
if err != nil {
return model.DeploymentKey{}, fmt.Errorf("%s: %w", "failed to marshal schema", err)
return model.DeploymentKey{}, fmt.Errorf("failed to marshal schema: %w", err)
}

// TODO(aat): "schema" containing language?
_, err = tx.UpsertModule(ctx, language, moduleSchema.Name)
if err != nil {
return model.DeploymentKey{}, fmt.Errorf("%s: %w", "failed to upsert module", translatePGError(err))
return model.DeploymentKey{}, fmt.Errorf("failed to upsert module: %w", translatePGError(err))
}

deploymentKey := model.NewDeploymentKey(moduleSchema.Name)

// Create the deployment
err = tx.CreateDeployment(ctx, moduleSchema.Name, schemaBytes, deploymentKey)
if err != nil {
return model.DeploymentKey{}, fmt.Errorf("%s: %w", "failed to create deployment", translatePGError(err))
return model.DeploymentKey{}, fmt.Errorf("failed to create deployment: %w", translatePGError(err))
}

uploadedDigests := slices.Map(artefacts, func(in DeploymentArtefact) []byte { return in.Digest[:] })
artefactDigests, err := tx.GetArtefactDigests(ctx, uploadedDigests)
if err != nil {
return model.DeploymentKey{}, fmt.Errorf("%s: %w", "failed to get artefact digests", err)
return model.DeploymentKey{}, fmt.Errorf("failed to get artefact digests: %w", err)
}
if len(artefactDigests) != len(artefacts) {
missingDigests := strings.Join(slices.Map(artefacts, func(in DeploymentArtefact) string { return in.Digest.String() }), ", ")
Expand All @@ -473,7 +473,7 @@ func (d *DAL) CreateDeployment(ctx context.Context, language string, moduleSchem
Path: artefact.Path,
})
if err != nil {
return model.DeploymentKey{}, fmt.Errorf("%s: %w", "failed to associate artefact with deployment", translatePGError(err))
return model.DeploymentKey{}, fmt.Errorf("failed to associate artefact with deployment: %w", translatePGError(err))
}
}

Expand All @@ -486,7 +486,7 @@ func (d *DAL) CreateDeployment(ctx context.Context, language string, moduleSchem
Verb: ingressRoute.Verb,
})
if err != nil {
return model.DeploymentKey{}, fmt.Errorf("%s: %w", "failed to create ingress route", translatePGError(err))
return model.DeploymentKey{}, fmt.Errorf("failed to create ingress route: %w", translatePGError(err))
}
}

Expand Down Expand Up @@ -525,7 +525,7 @@ func (d *DAL) GetDeployment(ctx context.Context, key model.DeploymentKey) (*mode
func (d *DAL) UpsertRunner(ctx context.Context, runner Runner) error {
attrBytes, err := json.Marshal(runner.Labels)
if err != nil {
return fmt.Errorf("%s: %w", "failed to JSON encode runner labels", err)
return fmt.Errorf("failed to JSON encode runner labels: %w", err)
}
deploymentID, err := d.db.UpsertRunner(ctx, sql.UpsertRunnerParams{
Key: runner.Key,
Expand Down Expand Up @@ -573,7 +573,7 @@ func (d *DAL) DeregisterRunner(ctx context.Context, key model.RunnerKey) error {
func (d *DAL) ReserveRunnerForDeployment(ctx context.Context, deployment model.DeploymentKey, reservationTimeout time.Duration, labels model.Labels) (Reservation, error) {
jsonLabels, err := json.Marshal(labels)
if err != nil {
return nil, fmt.Errorf("%s: %w", "failed to JSON encode labels", err)
return nil, fmt.Errorf("failed to JSON encode labels: %w", err)
}
ctx, cancel := context.WithTimeout(ctx, reservationTimeout)
tx, err := d.db.Begin(ctx)
Expand Down Expand Up @@ -687,7 +687,7 @@ func (d *DAL) ReplaceDeployment(ctx context.Context, newDeploymentKey model.Depl
return translatePGError(err)
}
if count == 1 {
return fmt.Errorf("%s: %w", "deployment already exists", ErrConflict)
return fmt.Errorf("deployment already exists: %w", ErrConflict)
}
replacedDeploymentKey = optional.Some(oldDeployment.Key)
} else if !isNotFound(err) {
Expand Down Expand Up @@ -780,7 +780,7 @@ func (d *DAL) GetDeploymentsWithMinReplicas(ctx context.Context) ([]Deployment,
func (d *DAL) GetActiveDeploymentSchemas(ctx context.Context) ([]*schema.Module, error) {
rows, err := d.db.GetActiveDeploymentSchemas(ctx)
if err != nil {
return nil, fmt.Errorf("%s: %w", "could not get active deployments", translatePGError(err))
return nil, fmt.Errorf("could not get active deployments: %w", translatePGError(err))
}
return slices.MapErr(rows, func(in sql.GetActiveDeploymentSchemasRow) (*schema.Module, error) { return in.Schema, nil })
}
Expand Down Expand Up @@ -844,7 +844,7 @@ func (d *DAL) GetProcessList(ctx context.Context) ([]Process, error) {
func (d *DAL) GetIdleRunners(ctx context.Context, limit int, labels model.Labels) ([]Runner, error) {
jsonb, err := json.Marshal(labels)
if err != nil {
return nil, fmt.Errorf("%s: %w", "could not marshal labels", err)
return nil, fmt.Errorf("could not marshal labels: %w", err)
}
runners, err := d.db.GetIdleRunners(ctx, jsonb, int64(limit))
if isNotFound(err) {
Expand All @@ -856,7 +856,7 @@ func (d *DAL) GetIdleRunners(ctx context.Context, limit int, labels model.Labels
rowLabels := model.Labels{}
err := json.Unmarshal(row.Labels, &rowLabels)
if err != nil {
return Runner{}, fmt.Errorf("%s: %w", "could not unmarshal labels", err)
return Runner{}, fmt.Errorf("could not unmarshal labels: %w", err)
}

return Runner{
Expand All @@ -878,7 +878,7 @@ func (d *DAL) GetRoutingTable(ctx context.Context, modules []string) (map[string
return nil, translatePGError(err)
}
if len(routes) == 0 {
return nil, fmt.Errorf("%s: %w", "no routes found", ErrNotFound)
return nil, fmt.Errorf("no routes found: %w", ErrNotFound)
}
out := make(map[string][]Route, len(routes))
for _, route := range routes {
Expand Down Expand Up @@ -1133,7 +1133,7 @@ func (*DAL) checkForExistingDeployments(ctx context.Context, tx *sql.Tx, moduleS
int64(len(artefacts)),
)
if err != nil {
return model.DeploymentKey{}, fmt.Errorf("%s: %w", "couldn't check for existing deployment", err)
return model.DeploymentKey{}, fmt.Errorf("couldn't check for existing deployment: %w", err)
}
if len(existing) > 0 {
return existing[0].DeploymentKey, nil
Expand Down
6 changes: 3 additions & 3 deletions backend/controller/dal/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,20 +142,20 @@ func decodeNotification[K any, T NotificationPayload, KP interface {
var deletedKey K
var deletedKeyP KP = &deletedKey
if err := deletedKeyP.UnmarshalText([]byte(notification.Old)); err != nil {
return Notification[T, K, KP]{}, fmt.Errorf("%s: %w", "failed to unmarshal notification key", err)
return Notification[T, K, KP]{}, fmt.Errorf("failed to unmarshal notification key: %w", err)
}
deleted = optional.Some(deletedKey)
} else {
var newKey K
var newKeyP KP = &newKey
if err := newKeyP.UnmarshalText([]byte(notification.New)); err != nil {
return Notification[T, K, KP]{}, fmt.Errorf("%s: %w", "failed to unmarshal notification key", err)
return Notification[T, K, KP]{}, fmt.Errorf("failed to unmarshal notification key: %w", err)
}
var msg T
var err error
msg, deleted, err = translate(newKey)
if err != nil {
return Notification[T, K, KP]{}, fmt.Errorf("%s: %w", "failed to translate database notification", err)
return Notification[T, K, KP]{}, fmt.Errorf("failed to translate database notification: %w", err)
}

if !deleted.Ok() {
Expand Down
6 changes: 3 additions & 3 deletions backend/controller/sql/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ var migrationSchema embed.FS
func Migrate(ctx context.Context, dsn string) error {
u, err := url.Parse(dsn)
if err != nil {
return fmt.Errorf("%s: %w", "invalid DSN", err)
return fmt.Errorf("invalid DSN: %w", err)
}
conn, err := sql.Open("pgx", dsn)
if err != nil {
return fmt.Errorf("%s: %w", "failed to connect to database", err)
return fmt.Errorf("failed to connect to database: %w", err)
}
defer conn.Close()

Expand All @@ -35,7 +35,7 @@ func Migrate(ctx context.Context, dsn string) error {
db.MigrationsDir = []string{"schema"}
err = db.CreateAndMigrate()
if err != nil {
return fmt.Errorf("%s: %w", "failed to create and migrate database", err)
return fmt.Errorf("failed to create and migrate database: %w", err)
}
return nil
}
2 changes: 1 addition & 1 deletion backend/protos/xyz/block/ftl/v1/mixins.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (r *RegisterRunnerRequest) DeploymentAsOptional() (optional.Option[model.De
}
key, err := model.ParseDeploymentKey(*r.Deployment)
if err != nil {
return optional.None[model.DeploymentKey](), fmt.Errorf("%s: %w", "invalid deployment key", err)
return optional.None[model.DeploymentKey](), fmt.Errorf("invalid deployment key: %w", err)
}
return optional.Some(key), nil
}
28 changes: 14 additions & 14 deletions backend/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func Start(ctx context.Context, config Config) error {
}
hostname, err := os.Hostname()
if err != nil {
return fmt.Errorf("%s: %w", "failed to get hostname", err)
return fmt.Errorf("failed to get hostname: %w", err)
}
pid := os.Getpid()

Expand All @@ -67,7 +67,7 @@ func Start(ctx context.Context, config Config) error {
logger.Debugf("Deployment directory: %s", config.DeploymentDir)
err = os.MkdirAll(config.DeploymentDir, 0700)
if err != nil {
return fmt.Errorf("%s: %w", "failed to create deployment directory", err)
return fmt.Errorf("failed to create deployment directory: %w", err)
}
logger.Debugf("Using FTL endpoint: %s", config.ControllerEndpoint)
logger.Debugf("Listening on %s", config.Bind)
Expand All @@ -86,7 +86,7 @@ func Start(ctx context.Context, config Config) error {
"languages": slices.Map(config.Language, func(t string) any { return t }),
})
if err != nil {
return fmt.Errorf("%s: %w", "failed to marshal labels", err)
return fmt.Errorf("failed to marshal labels: %w", err)
}

svc := &Service{
Expand Down Expand Up @@ -154,12 +154,12 @@ func (s *Service) Ping(ctx context.Context, req *connect.Request[ftlv1.PingReque

func (s *Service) Deploy(ctx context.Context, req *connect.Request[ftlv1.DeployRequest]) (response *connect.Response[ftlv1.DeployResponse], err error) {
if err, ok := s.registrationFailure.Load().Get(); ok {
return nil, connect.NewError(connect.CodeUnavailable, fmt.Errorf("%s: %w", "failed to register runner", err))
return nil, connect.NewError(connect.CodeUnavailable, fmt.Errorf("failed to register runner: %w", err))
}

key, err := model.ParseDeploymentKey(req.Msg.DeploymentKey)
if err != nil {
return nil, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("%s: %w", "invalid deployment key", err))
return nil, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("invalid deployment key: %w", err))
}

deploymentLogger := s.getDeploymentLogger(ctx, key)
Expand Down Expand Up @@ -191,23 +191,23 @@ func (s *Service) Deploy(ctx context.Context, req *connect.Request[ftlv1.DeployR
}
module, err := schema.ModuleFromProto(gdResp.Msg.Schema)
if err != nil {
return nil, fmt.Errorf("%s: %w", "invalid module", err)
return nil, fmt.Errorf("invalid module: %w", err)
}
deploymentDir := filepath.Join(s.config.DeploymentDir, module.Name, key.String())
if s.config.TemplateDir != "" {
err = copy.Copy(s.config.TemplateDir, deploymentDir)
if err != nil {
return nil, fmt.Errorf("%s: %w", "failed to copy template directory", err)
return nil, fmt.Errorf("failed to copy template directory: %w", err)
}
} else {
err = os.MkdirAll(deploymentDir, 0700)
if err != nil {
return nil, fmt.Errorf("%s: %w", "failed to create deployment directory", err)
return nil, fmt.Errorf("failed to create deployment directory: %w", err)
}
}
err = download.Artefacts(ctx, s.controllerClient, key, deploymentDir)
if err != nil {
return nil, fmt.Errorf("%s: %w", "failed to download artefacts", err)
return nil, fmt.Errorf("failed to download artefacts: %w", err)
}

verbCtx := log.ContextWithLogger(ctx, deploymentLogger.Attrs(map[string]string{"module": module.Name}))
Expand All @@ -225,7 +225,7 @@ func (s *Service) Deploy(ctx context.Context, req *connect.Request[ftlv1.DeployR
),
)
if err != nil {
return nil, fmt.Errorf("%s: %w", "failed to spawn plugin", err)
return nil, fmt.Errorf("failed to spawn plugin: %w", err)
}

dep := s.makeDeployment(cmdCtx, key, deployment)
Expand All @@ -244,7 +244,7 @@ func (s *Service) Terminate(ctx context.Context, c *connect.Request[ftlv1.Termin
}
deploymentKey, err := model.ParseDeploymentKey(c.Msg.DeploymentKey)
if err != nil {
return nil, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("%s: %w", "invalid deployment key", err))
return nil, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("invalid deployment key: %w", err))
}
if !depl.key.Equal(deploymentKey) {
return nil, connect.NewError(connect.CodeInvalidArgument, errors.New("deployment key mismatch"))
Expand All @@ -253,7 +253,7 @@ func (s *Service) Terminate(ctx context.Context, c *connect.Request[ftlv1.Termin
// Soft kill.
err = depl.plugin.Cmd.Kill(syscall.SIGTERM)
if err != nil {
return nil, fmt.Errorf("%s: %w", "failed to kill plugin", err)
return nil, fmt.Errorf("failed to kill plugin: %w", err)
}
// Hard kill after 10 seconds.
select {
Expand All @@ -262,7 +262,7 @@ func (s *Service) Terminate(ctx context.Context, c *connect.Request[ftlv1.Termin
err := depl.plugin.Cmd.Kill(syscall.SIGKILL)
if err != nil {
// Should we os.Exit(1) here?
return nil, fmt.Errorf("%s: %w", "failed to kill plugin", err)
return nil, fmt.Errorf("failed to kill plugin: %w", err)
}
}
s.deployment.Store(optional.None[*deployment]())
Expand Down Expand Up @@ -320,7 +320,7 @@ func (s *Service) registrationLoop(ctx context.Context, send func(request *ftlv1
})
if err != nil {
s.registrationFailure.Store(optional.Some(err))
return fmt.Errorf("%s: %w", "failed to register with Controller", err)
return fmt.Errorf("failed to register with Controller: %w", err)
}
s.registrationFailure.Store(optional.None[error]())

Expand Down
2 changes: 1 addition & 1 deletion cmd/ftl/cmd_schema_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (s *schemaGenerateCmd) hotReload(ctx context.Context, client ftlv1connect.C
case ftlv1.DeploymentChangeType_DEPLOYMENT_ADDED, ftlv1.DeploymentChangeType_DEPLOYMENT_CHANGED:
module, err := schema.ModuleFromProto(msg.Schema)
if err != nil {
return fmt.Errorf("%s: %w", "invalid module schema", err)
return fmt.Errorf("invalid module schema: %w", err)
}
modules[module.Name] = module

Expand Down
2 changes: 1 addition & 1 deletion cmd/ftl/cmd_schema_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (g *getSchemaCmd) Run(ctx context.Context, client ftlv1connect.ControllerSe
module, err := schema.ModuleFromProto(msg.Schema)
if len(g.Modules) == 0 || remainingNames[msg.Schema.Name] {
if err != nil {
return fmt.Errorf("%s: %w", "invalid module schema", err)
return fmt.Errorf("invalid module schema: %w", err)
}
fmt.Println(module)
delete(remainingNames, msg.Schema.Name)
Expand Down
2 changes: 1 addition & 1 deletion common/plugin/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func Start[Impl any, Iface any, Config any](
func allocatePort() (*net.TCPAddr, error) {
l, err := net.ListenTCP("tcp", &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1)})
if err != nil {
return nil, fmt.Errorf("%s: %w", "failed to allocate port", err)
return nil, fmt.Errorf("failed to allocate port: %w", err)
}
_ = l.Close()
return l.Addr().(*net.TCPAddr), nil //nolint:forcetypeassert
Expand Down
6 changes: 3 additions & 3 deletions common/plugin/spawn.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,14 @@ func Spawn[Client PingableClient](

select {
case <-dialCtx.Done():
return nil, nil, fmt.Errorf("%s: %w", "plugin timed out while starting", dialCtx.Err())
return nil, nil, fmt.Errorf("plugin timed out while starting: %w", dialCtx.Err())

case <-cmdCtx.Done():
return nil, nil, fmt.Errorf("%s: %w", "plugin process died", cmdCtx.Err())
return nil, nil, fmt.Errorf("plugin process died: %w", cmdCtx.Err())

case err := <-pingErr:
if err != nil {
return nil, nil, fmt.Errorf("%s: %w", "plugin failed to respond to ping", err)
return nil, nil, fmt.Errorf("plugin failed to respond to ping: %w", err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion go-runtime/compile/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func parseDirectives(fset *token.FileSet, docs *ast.CommentGroup) ([]directive,
} else {
err = fmt.Errorf("%s: %w", pos, err)
}
return nil, fmt.Errorf("%s: %w", "invalid directive", err)
return nil, fmt.Errorf("invalid directive: %w", err)
}
directives = append(directives, directive.Directive)
}
Expand Down
Loading