-
Notifications
You must be signed in to change notification settings - Fork 117
/
app.go
516 lines (457 loc) · 13.9 KB
/
app.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
package local
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"path"
"path/filepath"
"time"
"github.com/rilldata/rill/cli/pkg/browser"
"github.com/rilldata/rill/cli/pkg/config"
"github.com/rilldata/rill/cli/pkg/dotrill"
"github.com/rilldata/rill/cli/pkg/examples"
"github.com/rilldata/rill/cli/pkg/update"
"github.com/rilldata/rill/cli/pkg/variable"
"github.com/rilldata/rill/cli/pkg/web"
"github.com/rilldata/rill/runtime"
"github.com/rilldata/rill/runtime/compilers/rillv1beta"
"github.com/rilldata/rill/runtime/drivers"
"github.com/rilldata/rill/runtime/pkg/graceful"
"github.com/rilldata/rill/runtime/pkg/observability"
runtimeserver "github.com/rilldata/rill/runtime/server"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"golang.org/x/sync/errgroup"
)
type LogFormat string
// Default log formats for logger
const (
LogFormatConsole = "console"
LogFormatJSON = "json"
)
// Default instance config on local.
const (
DefaultInstanceID = "default"
DefaultOLAPDriver = "duckdb"
DefaultOLAPDSN = "stage.db"
)
// App encapsulates the logic associated with configuring and running the UI and the runtime in a local environment.
// Here, a local environment means a non-authenticated, single-instance and single-project setup on localhost.
// App encapsulates logic shared between different CLI commands, like start, init, build and source.
type App struct {
Context context.Context
Runtime *runtime.Runtime
Instance *drivers.Instance
Logger *zap.SugaredLogger
BaseLogger *zap.Logger
Version config.Version
Verbose bool
ProjectPath string
observabilityShutdown observability.ShutdownFunc
}
func NewApp(ctx context.Context, ver config.Version, verbose bool, olapDriver, olapDSN, projectPath string, logFormat LogFormat, variables []string) (*App, error) {
// Setup a friendly-looking colored/json logger
var logger *zap.Logger
var err error
switch logFormat {
case LogFormatJSON:
cfg := zap.NewProductionConfig()
cfg.DisableStacktrace = true
cfg.Level.SetLevel(zapcore.DebugLevel)
logger, err = cfg.Build()
case LogFormatConsole:
encCfg := zap.NewDevelopmentEncoderConfig()
encCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder
logger = zap.New(zapcore.NewCore(
zapcore.NewConsoleEncoder(encCfg),
zapcore.AddSync(os.Stdout),
zapcore.DebugLevel,
))
}
if err != nil {
return nil, err
}
// Set logging level
lvl := zap.InfoLevel
if verbose {
lvl = zap.DebugLevel
}
logger = logger.WithOptions(zap.IncreaseLevel(lvl))
// Init Prometheus telemetry
shutdown, err := observability.Start(ctx, logger, &observability.Options{
MetricsExporter: observability.PrometheusExporter,
TracesExporter: observability.NoopExporter,
ServiceName: "rill-local",
ServiceVersion: ver.String(),
})
if err != nil {
return nil, err
}
// Create a local runtime with an in-memory metastore
rtOpts := &runtime.Options{
ConnectionCacheSize: 100,
MetastoreDriver: "sqlite",
MetastoreDSN: "file:rill?mode=memory&cache=shared",
QueryCacheSize: 10000,
AllowHostAccess: true,
}
rt, err := runtime.New(rtOpts, logger)
if err != nil {
return nil, err
}
// Get full path to project
projectPath, err = filepath.Abs(projectPath)
if err != nil {
return nil, err
}
err = os.MkdirAll(projectPath, os.ModePerm) // Create project dir if it doesn't exist
if err != nil {
return nil, err
}
// If the OLAP is the default OLAP (DuckDB in stage.db), we make it relative to the project directory (not the working directory)
if olapDriver == DefaultOLAPDriver && olapDSN == DefaultOLAPDSN {
olapDSN = path.Join(projectPath, olapDSN)
}
parsedVariables, err := variable.Parse(variables)
if err != nil {
return nil, err
}
// Create instance with its repo set to the project directory
inst := &drivers.Instance{
ID: DefaultInstanceID,
OLAPDriver: olapDriver,
OLAPDSN: olapDSN,
RepoDriver: "file",
RepoDSN: projectPath,
EmbedCatalog: olapDriver == "duckdb",
Variables: parsedVariables,
}
err = rt.CreateInstance(ctx, inst)
if err != nil {
return nil, err
}
// Done
app := &App{
Context: ctx,
Runtime: rt,
Instance: inst,
Logger: logger.Sugar(),
BaseLogger: logger,
Version: ver,
Verbose: verbose,
ProjectPath: projectPath,
observabilityShutdown: shutdown,
}
return app, nil
}
func (a *App) Close() error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
err := a.observabilityShutdown(ctx)
if err != nil {
fmt.Printf("telemetry shutdown failed: %s\n", err.Error())
}
return a.Runtime.Close()
}
func (a *App) IsProjectInit() bool {
repo, err := a.Runtime.Repo(a.Context, a.Instance.ID)
if err != nil {
panic(err) // checks in New should ensure it never happens
}
c := rillv1beta.New(repo, a.Instance.ID)
return c.IsInit(a.Context)
}
func (a *App) InitProject(exampleName string) error {
repo, err := a.Runtime.Repo(a.Context, a.Instance.ID)
if err != nil {
panic(err) // checks in New should ensure it never happens
}
c := rillv1beta.New(repo, a.Instance.ID)
if c.IsInit(a.Context) {
return fmt.Errorf("a Rill project already exists")
}
// Check if project path is pwd for nicer log messages
pwd, _ := os.Getwd()
isPwd := a.ProjectPath == pwd
// If no example is provided, init an empty project
if exampleName == "" {
// Infer a default project name from its path
defaultName := filepath.Base(a.ProjectPath)
if defaultName == "" || defaultName == "." || defaultName == ".." {
defaultName = "untitled"
}
// Init empty project
err := c.InitEmpty(a.Context, defaultName, a.Version.Number)
if err != nil {
if isPwd {
return fmt.Errorf("failed to initialize project in the current directory (detailed error: %w)", err)
}
return fmt.Errorf("failed to initialize project in '%s' (detailed error: %w)", a.ProjectPath, err)
}
// Log success
if isPwd {
a.Logger.Infof("Initialized empty project in the current directory")
} else {
a.Logger.Infof("Initialized empty project at '%s'", a.ProjectPath)
}
return nil
}
// It's an example project. We currently only support examples through direct file unpacking.
// TODO: Support unpacking examples through rillv1beta, instead of unpacking files.
err = examples.Init(exampleName, a.ProjectPath)
if err != nil {
if errors.Is(err, examples.ErrExampleNotFound) {
return fmt.Errorf("example project '%s' not found", exampleName)
}
return fmt.Errorf("failed to initialize project (detailed error: %w)", err)
}
if isPwd {
a.Logger.Infof("Initialized example project '%s' in the current directory", exampleName)
} else {
a.Logger.Infof("Initialized example project '%s' in directory '%s'", exampleName, a.ProjectPath)
}
return nil
}
func (a *App) Reconcile(strict bool) error {
a.Logger.Infof("Hydrating project '%s'", a.ProjectPath)
res, err := a.Runtime.Reconcile(a.Context, a.Instance.ID, nil, nil, false, false)
if err != nil {
return err
}
if a.Context.Err() != nil {
a.Logger.Errorf("Hydration canceled")
}
for _, path := range res.AffectedPaths {
a.Logger.Infof("Reconciled: %s", path)
}
for _, merr := range res.Errors {
a.Logger.Errorf("%s: %s", merr.FilePath, merr.Message)
}
if len(res.Errors) == 0 {
a.Logger.Infof("Hydration completed!")
} else if strict {
a.Logger.Fatalf("Hydration failed")
} else {
a.Logger.Infof("Hydration failed")
}
return nil
}
func (a *App) ReconcileSource(sourcePath string) error {
a.Logger.Infof("Reconciling source and impacted models in project '%s'", a.ProjectPath)
paths := []string{sourcePath}
res, err := a.Runtime.Reconcile(a.Context, a.Instance.ID, paths, paths, false, false)
if err != nil {
return err
}
if a.Context.Err() != nil {
a.Logger.Errorf("Hydration canceled")
return nil
}
for _, path := range res.AffectedPaths {
a.Logger.Infof("Reconciled: %s", path)
}
for _, merr := range res.Errors {
a.Logger.Errorf("%s: %s", merr.FilePath, merr.Message)
}
if len(res.Errors) == 0 {
a.Logger.Infof("Hydration completed!")
} else {
a.Logger.Infof("Hydration failed")
}
return nil
}
func (a *App) Serve(httpPort, grpcPort int, enableUI, openBrowser, readonly bool, userID string) error {
// Get analytics info
installID, enabled, err := dotrill.AnalyticsInfo()
if err != nil {
a.Logger.Warnf("error finding install ID: %v", err)
}
// Build local info for frontend
inf := &localInfo{
InstanceID: a.Instance.ID,
GRPCPort: grpcPort,
InstallID: installID,
ProjectPath: a.ProjectPath,
UserID: userID,
Version: a.Version.Number,
BuildCommit: a.Version.Commit,
BuildTime: a.Version.Timestamp,
IsDev: a.Version.IsDev(),
AnalyticsEnabled: enabled,
Readonly: readonly,
}
// Create server logger.
// It only logs error messages when !verbose to prevent lots of req/res info messages.
lvl := zap.ErrorLevel
if a.Verbose {
lvl = zap.DebugLevel
}
serverLogger := a.BaseLogger.WithOptions(zap.IncreaseLevel(lvl))
// Prepare errgroup and context with graceful shutdown
gctx := graceful.WithCancelOnTerminate(a.Context)
group, ctx := errgroup.WithContext(gctx)
// Create a runtime server
opts := &runtimeserver.Options{
HTTPPort: httpPort,
GRPCPort: grpcPort,
AllowedOrigins: []string{"*"},
ServePrometheus: true,
}
runtimeServer, err := runtimeserver.NewServer(opts, a.Runtime, serverLogger)
if err != nil {
return err
}
// Start the gRPC server
group.Go(func() error {
return runtimeServer.ServeGRPC(ctx)
})
// Start the local HTTP server
group.Go(func() error {
return runtimeServer.ServeHTTP(ctx, func(mux *http.ServeMux) {
// Inject local-only endpoints on the server for the local UI and local backend endpoints
if enableUI {
mux.Handle("/", web.StaticHandler())
}
mux.Handle("/local/config", a.infoHandler(inf))
mux.Handle("/local/version", a.versionHandler())
mux.Handle("/local/track", a.trackingHandler(inf))
})
})
// Open the browser when health check succeeds
go a.pollServer(ctx, httpPort, enableUI && openBrowser)
// Run the server
err = group.Wait()
if err != nil {
return fmt.Errorf("server crashed: %w", err)
}
a.Logger.Info("Rill shutdown gracefully")
return nil
}
func (a *App) pollServer(ctx context.Context, httpPort int, openOnHealthy bool) {
// Basic health check
uri := fmt.Sprintf("http://localhost:%d", httpPort)
client := http.Client{Timeout: time.Second}
for {
// Check for cancellation
if ctx.Err() != nil {
return
}
// Check if server is up
resp, err := client.Get(uri + "/v1/ping")
if err == nil {
resp.Body.Close()
if resp.StatusCode < http.StatusInternalServerError {
break
}
}
// Wait a bit and retry
time.Sleep(250 * time.Millisecond)
}
// Health check succeeded
a.Logger.Infof("Serving Rill on: %s", uri)
if openOnHealthy {
err := browser.Open(uri)
if err != nil {
a.Logger.Debugf("could not open browser: %v", err)
}
}
}
type localInfo struct {
InstanceID string `json:"instance_id"`
GRPCPort int `json:"grpc_port"`
InstallID string `json:"install_id"`
UserID string `json:"user_id"`
ProjectPath string `json:"project_path"`
Version string `json:"version"`
BuildCommit string `json:"build_commit"`
BuildTime string `json:"build_time"`
IsDev bool `json:"is_dev"`
AnalyticsEnabled bool `json:"analytics_enabled"`
Readonly bool `json:"readonly"`
}
// infoHandler servers the local info struct.
func (a *App) infoHandler(info *localInfo) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
data, err := json.Marshal(info)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
w.Header().Add("Content-Type", "application/json")
_, err = w.Write(data)
if err != nil {
http.Error(w, fmt.Sprintf("failed to write response data: %s", err), http.StatusInternalServerError)
return
}
})
}
// versionHandler servers the version struct.
func (a *App) versionHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Get the latest version available
latestVersion, err := update.LatestVersion(r.Context())
if err != nil {
a.Logger.Warnf("error finding latest version: %v", err)
}
inf := &versionInfo{
CurrentVersion: a.Version.Number,
LatestVersion: latestVersion,
}
data, err := json.Marshal(inf)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
w.Header().Add("Content-Type", "application/json")
_, err = w.Write(data)
if err != nil {
http.Error(w, fmt.Sprintf("failed to write response data: %s", err), http.StatusInternalServerError)
return
}
})
}
type versionInfo struct {
CurrentVersion string `json:"current_version"`
LatestVersion string `json:"latest_version"`
}
// trackingHandler proxies events to intake.rilldata.io.
func (a *App) trackingHandler(info *localInfo) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !info.AnalyticsEnabled {
w.WriteHeader(http.StatusOK)
return
}
// Proxy request to rill intake
proxyReq, err := http.NewRequest(r.Method, "https://intake.rilldata.io/events/data-modeler-metrics", r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
// Copy the auth header
proxyReq.Header = http.Header{
"Authorization": r.Header["Authorization"],
}
// Send proxied request
resp, err := http.DefaultClient.Do(proxyReq)
if err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
defer resp.Body.Close()
// Done
w.WriteHeader(http.StatusOK)
})
}
func ParseLogFormat(format string) (LogFormat, bool) {
switch format {
case "json":
return LogFormatJSON, true
case "console":
return LogFormatConsole, true
default:
return "", false
}
}