Skip to content

Commit

Permalink
feat: allow build parallelism to be controlled with a flag (#1067)
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas authored Mar 12, 2024
1 parent 7223f5a commit 8f4e3b4
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 16 deletions.
4 changes: 2 additions & 2 deletions backend/schema/protobuf_dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func declListToSchema(s []*schemapb.Decl) []Decl {

func typeToSchema(s *schemapb.Type) Type {
switch s := s.Value.(type) {
// case *schemapb.Type_VerbRef:
// return verbRefToSchema(s.VerbRef)
case *schemapb.Type_VerbRef:
return VerbRefFromProto(s.VerbRef)
case *schemapb.Type_DataRef:
return DataRefFromProto(s.DataRef)
case *schemapb.Type_EnumRef:
Expand Down
17 changes: 15 additions & 2 deletions buildengine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ type Engine struct {
controllerSchema *xsync.MapOf[string, *schema.Module]
schemaChanges *pubsub.Topic[schemaChange]
cancel func()
parallelism int
}

type Option func(o *Engine)

func Parallelism(n int) Option {
return func(o *Engine) {
o.parallelism = n
}
}

// New constructs a new [Engine].
Expand All @@ -45,14 +54,18 @@ type Engine struct {
// pull in missing schemas.
//
// "dirs" are directories to scan for local modules.
func New(ctx context.Context, client ftlv1connect.ControllerServiceClient, dirs ...string) (*Engine, error) {
func New(ctx context.Context, client ftlv1connect.ControllerServiceClient, dirs []string, options ...Option) (*Engine, error) {
ctx = rpc.ContextWithClient(ctx, client)
e := &Engine{
client: client,
dirs: dirs,
modules: map[string]Module{},
controllerSchema: xsync.NewMapOf[string, *schema.Module](),
schemaChanges: pubsub.New[schemaChange](),
parallelism: runtime.NumCPU(),
}
for _, option := range options {
option(e)
}
e.controllerSchema.Store("builtin", schema.Builtins())
ctx, cancel := context.WithCancel(ctx)
Expand Down Expand Up @@ -368,7 +381,7 @@ func (e *Engine) buildWithCallback(ctx context.Context, callback buildCallback,
// Collect schemas to be inserted into "built" map for subsequent groups.
schemas := make(chan *schema.Module, len(group))
wg, ctx := errgroup.WithContext(ctx)
wg.SetLimit(runtime.NumCPU())
wg.SetLimit(e.parallelism)
for _, name := range group {
wg.Go(func() error {
if mustBuild[name] {
Expand Down
2 changes: 1 addition & 1 deletion buildengine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func TestEngine(t *testing.T) {
ctx := log.ContextWithNewDefaultLogger(context.Background())
engine, err := buildengine.New(ctx, nil, "testdata/modules/alpha", "testdata/modules/another")
engine, err := buildengine.New(ctx, nil, []string{"testdata/modules/alpha", "testdata/modules/another"})
assert.NoError(t, err)

defer engine.Close()
Expand Down
5 changes: 3 additions & 2 deletions cmd/ftl/cmd_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (
)

type buildCmd struct {
Dirs []string `arg:"" help:"Base directories containing modules." type:"existingdir" required:""`
Parallelism int `short:"j" help:"Number of modules to build in parallel." default:"${numcpu}"`
Dirs []string `arg:"" help:"Base directories containing modules." type:"existingdir" required:""`
}

func (b *buildCmd) Run(ctx context.Context) error {
client := rpc.ClientFromContext[ftlv1connect.ControllerServiceClient](ctx)
engine, err := buildengine.New(ctx, client, b.Dirs...)
engine, err := buildengine.New(ctx, client, b.Dirs, buildengine.Parallelism(b.Parallelism))
if err != nil {
return err
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/ftl/cmd_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
)

type deployCmd struct {
Replicas int32 `short:"n" help:"Number of replicas to deploy." default:"1"`
Dirs []string `arg:"" help:"Base directories containing modules." type:"existingdir" required:""`
NoWait bool `help:"Do not wait for deployment to complete." default:"false"`
Parallelism int `short:"j" help:"Number of modules to build in parallel." default:"${numcpu}"`
Replicas int32 `short:"n" help:"Number of replicas to deploy." default:"1"`
Dirs []string `arg:"" help:"Base directories containing modules." type:"existingdir" required:""`
NoWait bool `help:"Do not wait for deployment to complete." default:"false"`
}

func (d *deployCmd) Run(ctx context.Context) error {
client := rpc.ClientFromContext[ftlv1connect.ControllerServiceClient](ctx)
engine, err := buildengine.New(ctx, client, d.Dirs...)
engine, err := buildengine.New(ctx, client, d.Dirs, buildengine.Parallelism(d.Parallelism))
if err != nil {
return err
}
Expand Down
11 changes: 6 additions & 5 deletions cmd/ftl/cmd_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (
)

type devCmd struct {
Dirs []string `arg:"" help:"Base directories containing modules." type:"existingdir" required:""`
Watch time.Duration `help:"Watch template directory at this frequency and regenerate on change." default:"500ms"`
NoServe bool `help:"Do not start the FTL server." default:"false"`
ServeCmd serveCmd `embed:""`
Parallelism int `short:"j" help:"Number of modules to build in parallel." default:"${numcpu}"`
Dirs []string `arg:"" help:"Base directories containing modules." type:"existingdir" required:""`
Watch time.Duration `help:"Watch template directory at this frequency and regenerate on change." default:"500ms"`
NoServe bool `help:"Do not start the FTL server." default:"false"`
ServeCmd serveCmd `embed:""`
}

func (d *devCmd) Run(ctx context.Context) error {
Expand All @@ -39,7 +40,7 @@ func (d *devCmd) Run(ctx context.Context) error {
}

g.Go(func() error {
engine, err := buildengine.New(ctx, client, d.Dirs...)
engine, err := buildengine.New(ctx, client, d.Dirs, buildengine.Parallelism(d.Parallelism))
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/ftl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/signal"
"runtime"
"strconv"
"strings"
"syscall"

Expand Down Expand Up @@ -63,6 +64,7 @@ func main() {
"version": ftl.Version,
"os": runtime.GOOS,
"arch": runtime.GOARCH,
"numcpu": strconv.Itoa(runtime.NumCPU()),
},
)

Expand Down

0 comments on commit 8f4e3b4

Please sign in to comment.