generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
engine.go
244 lines (228 loc) · 7.14 KB
/
engine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package buildengine
import (
"context"
"fmt"
"path/filepath"
"runtime"
"time"
"connectrpc.com/connect"
"github.com/jpillora/backoff"
"github.com/puzpuzpuz/xsync/v3"
"golang.org/x/exp/maps"
"golang.org/x/sync/errgroup"
ftlv1 "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1"
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect"
"github.com/TBD54566975/ftl/backend/schema"
"github.com/TBD54566975/ftl/internal/log"
"github.com/TBD54566975/ftl/internal/rpc"
)
// Engine for building a set of modules.
type Engine struct {
modules map[string]Module
controllerSchema *xsync.MapOf[string, *schema.Module]
cancel func()
}
// New constructs a new [Engine].
//
// Completely offline builds are possible if the full dependency graph is
// locally available. If the FTL controller is available, it will be used to
// pull in missing schemas.
//
// "dirs" are directories to scan for local modules.
func New(ctx context.Context, client ftlv1connect.ControllerServiceClient, dirs ...string) (*Engine, error) {
ctx = rpc.ContextWithClient(ctx, client)
e := &Engine{
modules: map[string]Module{},
controllerSchema: xsync.NewMapOf[string, *schema.Module](),
}
e.controllerSchema.Store("builtin", schema.Builtins())
ctx, cancel := context.WithCancel(ctx)
e.cancel = cancel
configs, err := DiscoverModules(ctx, dirs...)
if err != nil {
return nil, err
}
modules, err := UpdateAllDependencies(ctx, configs...)
if err != nil {
return nil, err
}
for _, module := range modules {
e.modules[module.Module] = module
}
if client == nil {
return e, nil
}
schemaSync := e.startSchemaSync(ctx, client)
go rpc.RetryStreamingServerStream(ctx, backoff.Backoff{Max: time.Second}, &ftlv1.PullSchemaRequest{}, client.PullSchema, schemaSync)
return e, nil
}
// Sync module schema changes from the FTL controller, as well as from manual
// updates, and merge them into a single schema map.
func (e *Engine) startSchemaSync(ctx context.Context, client ftlv1connect.ControllerServiceClient) func(ctx context.Context, msg *ftlv1.PullSchemaResponse) error {
logger := log.FromContext(ctx)
// Blocking schema sync from the controller.
psch, err := client.GetSchema(ctx, connect.NewRequest(&ftlv1.GetSchemaRequest{}))
if err == nil {
sch, err := schema.FromProto(psch.Msg.Schema)
if err == nil {
modules := map[string]*schema.Module{}
for _, module := range sch.Modules {
e.controllerSchema.Store(module.Name, module)
modules[module.Name] = module
}
} else {
logger.Debugf("Failed to parse schema from controller: %s", err)
}
} else {
logger.Debugf("Failed to get schema from controller: %s", err)
}
// Sync module schema changes from the controller into the schema event source.
return func(ctx context.Context, msg *ftlv1.PullSchemaResponse) error {
switch msg.ChangeType {
case ftlv1.DeploymentChangeType_DEPLOYMENT_ADDED, ftlv1.DeploymentChangeType_DEPLOYMENT_CHANGED:
sch, err := schema.ModuleFromProto(msg.Schema)
if err == nil {
return err
}
e.controllerSchema.Store(sch.Name, sch)
case ftlv1.DeploymentChangeType_DEPLOYMENT_REMOVED:
e.controllerSchema.Delete(msg.ModuleName)
}
return nil
}
}
// Close stops the Engine's schema sync.
func (e *Engine) Close() error {
e.cancel()
return nil
}
// Graph returns the dependency graph for the given modules.
//
// If no modules are provided, the entire graph is returned. An error is returned if
// any dependencies are missing.
func (e *Engine) Graph(modules ...string) (map[string][]string, error) {
out := map[string][]string{}
if len(modules) == 0 {
modules = maps.Keys(e.modules)
}
for _, name := range modules {
if err := e.buildGraph(name, out); err != nil {
return nil, err
}
}
return out, nil
}
func (e *Engine) buildGraph(name string, out map[string][]string) error {
var deps []string
if module, ok := e.modules[name]; ok {
deps = module.Dependencies
} else if sch, ok := e.controllerSchema.Load(name); ok {
deps = sch.Imports()
} else {
return fmt.Errorf("module %q not found", name)
}
out[name] = deps
for _, dep := range deps {
if err := e.buildGraph(dep, out); err != nil {
return err
}
}
return nil
}
// Import manually imports a schema for a module as if it were retrieved from
// the FTL controller.
func (e *Engine) Import(ctx context.Context, schema *schema.Module) {
e.controllerSchema.Store(schema.Name, schema)
}
// Build attempts to build the specified modules, or all local modules if none are provided.
func (e *Engine) Build(ctx context.Context, modules ...string) error {
mustBuild := map[string]bool{}
if len(modules) == 0 {
modules = maps.Keys(e.modules)
}
for _, name := range modules {
module, ok := e.modules[name]
if !ok {
return fmt.Errorf("module %q not found", module)
}
// Update dependencies before building.
var err error
module, err = UpdateDependencies(ctx, module.ModuleConfig)
if err != nil {
return err
}
e.modules[name] = module
mustBuild[name] = true
}
graph, err := e.Graph(modules...)
if err != nil {
return err
}
built := map[string]*schema.Module{
"builtin": schema.Builtins(),
}
topology := TopologicalSort(graph)
for _, group := range topology {
// 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())
for _, name := range group {
wg.Go(func() error {
if mustBuild[name] {
return e.build(ctx, name, built, schemas)
}
return e.mustSchema(ctx, name, built, schemas)
})
}
err := wg.Wait()
if err != nil {
return err
}
// Now this group is built, collect al the schemas.
close(schemas)
for sch := range schemas {
built[sch.Name] = sch
}
}
return nil
}
// Publish either the schema from the FTL controller, or from a local build.
func (e *Engine) mustSchema(ctx context.Context, name string, built map[string]*schema.Module, schemas chan<- *schema.Module) error {
if sch, ok := e.controllerSchema.Load(name); ok {
schemas <- sch
return nil
}
return e.build(ctx, name, built, schemas)
}
// Build a module and publish its schema.
//
// Assumes that all dependencies have been built and are available in "built".
func (e *Engine) build(ctx context.Context, name string, built map[string]*schema.Module, schemas chan<- *schema.Module) error {
combined := map[string]*schema.Module{}
gatherShemas(built, e.modules, e.modules[name], combined)
sch := &schema.Schema{Modules: maps.Values(combined)}
module := e.modules[name]
err := Build(ctx, sch, module)
if err != nil {
return err
}
moduleSchema, err := schema.ModuleFromProtoFile(filepath.Join(module.Dir, module.DeployDir, module.Schema))
if err != nil {
return fmt.Errorf("load schema %s: %w", name, err)
}
schemas <- moduleSchema
return nil
}
// Construct a combined schema for a module and its transitive dependencies.
func gatherShemas(
moduleSchemas map[string]*schema.Module,
modules map[string]Module,
module Module,
out map[string]*schema.Module,
) {
for _, dep := range modules[module.Module].Dependencies {
out[dep] = moduleSchemas[dep]
gatherShemas(moduleSchemas, modules, modules[dep], out)
}
}