forked from artefactual-labs/enduro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
508 lines (444 loc) · 16.3 KB
/
main.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
package main
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"net/http/pprof"
"os"
"os/signal"
"runtime"
"syscall"
"time"
"github.com/artefactual-sdps/temporal-activities/archive"
"github.com/go-logr/logr"
"github.com/oklog/run"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"go.artefactual.dev/tools/log"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/noop"
temporalsdk_activity "go.temporal.io/sdk/activity"
temporalsdk_client "go.temporal.io/sdk/client"
temporalsdk_worker "go.temporal.io/sdk/worker"
temporalsdk_workflow "go.temporal.io/sdk/workflow"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"github.com/artefactual-labs/enduro/internal/api"
"github.com/artefactual-labs/enduro/internal/batch"
"github.com/artefactual-labs/enduro/internal/collection"
"github.com/artefactual-labs/enduro/internal/db"
"github.com/artefactual-labs/enduro/internal/metadata"
nha_activities "github.com/artefactual-labs/enduro/internal/nha/activities"
"github.com/artefactual-labs/enduro/internal/pipeline"
"github.com/artefactual-labs/enduro/internal/temporal"
"github.com/artefactual-labs/enduro/internal/validation"
"github.com/artefactual-labs/enduro/internal/watcher"
"github.com/artefactual-labs/enduro/internal/workflow"
"github.com/artefactual-labs/enduro/internal/workflow/activities"
"github.com/artefactual-labs/enduro/internal/workflow/hooks"
)
const appName = "enduro"
var (
version = "(dev-version)"
gitCommit = "(dev-commit)"
buildTime = "(dev-buildtime)"
goVersion = runtime.Version()
)
func main() {
var (
v = viper.New()
p = pflag.NewFlagSet(appName, pflag.ExitOnError)
)
configureViper(v)
p.String("config", "", "Configuration file")
p.Bool("version", false, "Show version information")
_ = p.Parse(os.Args[1:])
if v, _ := p.GetBool("version"); v {
fmt.Printf(
"%s version %s (commit=%s) built on %s using %s\n",
appName, version, gitCommit, buildTime, goVersion)
os.Exit(0)
}
var config configuration
configFile, _ := p.GetString("config")
configFileFound, err := readConfig(v, &config, configFile)
if err != nil {
fmt.Printf("Failed to read configuration: %v\n", err)
os.Exit(1)
}
// Logging configuration.
logger := log.New(os.Stderr,
log.WithName(appName),
log.WithDebug(config.Debug),
log.WithLevel(config.Verbosity),
)
defer log.Sync(logger)
logger.Info("Starting...", "version", version, "pid", os.Getpid())
if configFileFound {
logger.Info("Configuration file loaded.", "path", v.ConfigFileUsed())
} else {
logger.Info("Configuration file not found.")
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Set up tracing.
tp, shutdown, err := initTracerProvider(ctx, logger, config.Telemetry)
if err != nil {
logger.Error(err, "Error creating tracer provider.")
os.Exit(1)
}
defer func() { _ = shutdown(ctx) }()
tracer := tp.Tracer("enduro")
database, err := db.Connect(config.Database.DSN)
if err != nil {
logger.Error(err, "Database configuration failed.")
os.Exit(1)
}
_, span := tracer.Start(ctx, "db-ping")
span.SetAttributes(attribute.String("db.driver", "mysql"))
if err := database.Ping(); err != nil {
span.SetStatus(codes.Error, "ping failed")
span.RecordError(err)
}
span.AddEvent("Connected!")
span.End()
temporalClient, err := temporalsdk_client.Dial(temporalsdk_client.Options{
Namespace: config.Temporal.Namespace,
HostPort: config.Temporal.Address,
Logger: temporal.Logger(logger.WithName("temporal-client")),
})
if err != nil {
logger.Error(err, "Error creating Temporal client.")
os.Exit(1)
}
// Set up the pipeline registry.
pipelineRegistry, err := pipeline.NewPipelineRegistry(logger.WithName("registry"), config.Pipeline)
if err != nil {
logger.Error(err, "Pipeline registry cannot be initialized.")
os.Exit(1)
}
// Set up the pipeline service.
var pipesvc pipeline.Service
{
pipesvc = pipeline.NewService(logger.WithName("pipeline"), pipelineRegistry)
}
// Set up the batch service.
var batchsvc batch.Service
{
batchsvc = batch.NewService(logger.WithName("batch"), temporalClient, config.Temporal.TaskQueue, config.Watcher.CompletedDirs())
}
// Set up the collection service.
var colsvc collection.Service
{
colsvc = collection.NewService(logger.WithName("collection"), database, temporalClient, config.Temporal.TaskQueue, pipelineRegistry)
}
// Set up the watcher service.
var wsvc watcher.Service
{
wsvc, err = watcher.New(ctx, &config.Watcher)
if err != nil {
logger.Error(err, "Error setting up watchers.")
os.Exit(1)
}
}
var g run.Group
// API server.
{
var srv *http.Server
g.Add(
func() error {
srv = api.HTTPServer(logger, tp, &config.API, pipesvc, batchsvc, colsvc)
return srv.ListenAndServe()
},
func(err error) {
ctx, cancel := context.WithTimeout(ctx, time.Second*5)
defer cancel()
_ = srv.Shutdown(ctx)
},
)
}
// Watchers, where each watcher is a group actor.
{
for _, w := range wsvc.Watchers() {
w := w
done := make(chan struct{})
g.Add(
func() error {
for {
select {
case <-done:
return nil
default:
event, err := w.Watch(ctx)
if err != nil {
if !errors.Is(err, watcher.ErrWatchTimeout) {
logger.Error(err, "Error monitoring watcher interface.", "watcher", w)
}
continue
}
ctx, span := tracer.Start(ctx, "Watcher")
span.SetAttributes(
attribute.String("watcher", event.WatcherName),
attribute.String("bucket", event.Bucket),
attribute.String("key", event.Key),
attribute.Bool("dir", event.IsDir),
)
logger.V(1).Info("Starting new workflow", "watcher", event.WatcherName, "bucket", event.Bucket, "key", event.Key, "dir", event.IsDir)
req := collection.ProcessingWorkflowRequest{
WatcherName: event.WatcherName,
PipelineNames: event.PipelineName,
RetentionPeriod: event.RetentionPeriod,
CompletedDir: event.CompletedDir,
StripTopLevelDir: event.StripTopLevelDir,
RejectDuplicates: event.RejectDuplicates,
ExcludeHiddenFiles: event.ExcludeHiddenFiles,
TransferType: event.TransferType,
Key: event.Key,
IsDir: event.IsDir,
ValidationConfig: config.Validation,
MetadataConfig: config.Metadata,
}
if err := collection.InitProcessingWorkflow(ctx, tracer, temporalClient, config.Temporal.TaskQueue, &req); err != nil {
logger.Error(err, "Error initializing processing workflow.")
}
span.End()
}
}
},
func(err error) {
close(done)
},
)
}
}
// Workflow and activity worker.
{
h := hooks.NewHooks(config.Hooks)
done := make(chan struct{})
w := temporalsdk_worker.New(temporalClient, config.Temporal.TaskQueue, temporalsdk_worker.Options{
EnableSessionWorker: true,
MaxConcurrentSessionExecutionSize: config.Worker.MaxConcurrentSessionExecutionSize,
MaxConcurrentWorkflowTaskExecutionSize: config.Worker.MaxConcurrentWorkflowsExecutionsSize,
MaxHeartbeatThrottleInterval: config.Worker.HeartbeatThrottleInterval,
DefaultHeartbeatThrottleInterval: config.Worker.HeartbeatThrottleInterval,
})
if err != nil {
logger.Error(err, "Error creating Temporal worker.")
os.Exit(1)
}
w.RegisterWorkflowWithOptions(workflow.NewProcessingWorkflow(h, colsvc, pipelineRegistry, logger, config.Workflow).Execute, temporalsdk_workflow.RegisterOptions{Name: collection.ProcessingWorkflowName})
w.RegisterActivityWithOptions(activities.NewAcquirePipelineActivity(pipelineRegistry).Execute, temporalsdk_activity.RegisterOptions{Name: activities.AcquirePipelineActivityName})
w.RegisterActivityWithOptions(activities.NewDownloadActivity(h, pipelineRegistry, wsvc).Execute, temporalsdk_activity.RegisterOptions{Name: activities.DownloadActivityName})
w.RegisterActivityWithOptions(archive.NewExtractActivity(config.ExtractActivity).Execute, temporalsdk_activity.RegisterOptions{Name: archive.ExtractActivityName})
w.RegisterActivityWithOptions(activities.NewBundleActivity().Execute, temporalsdk_activity.RegisterOptions{Name: activities.BundleActivityName})
w.RegisterActivityWithOptions(activities.NewValidateTransferActivity().Execute, temporalsdk_activity.RegisterOptions{Name: activities.ValidateTransferActivityName})
w.RegisterActivityWithOptions(activities.NewTransferActivity(pipelineRegistry).Execute, temporalsdk_activity.RegisterOptions{Name: activities.TransferActivityName})
w.RegisterActivityWithOptions(activities.NewPollTransferActivity(pipelineRegistry).Execute, temporalsdk_activity.RegisterOptions{Name: activities.PollTransferActivityName})
w.RegisterActivityWithOptions(activities.NewPollIngestActivity(pipelineRegistry).Execute, temporalsdk_activity.RegisterOptions{Name: activities.PollIngestActivityName})
w.RegisterActivityWithOptions(activities.NewCleanUpActivity().Execute, temporalsdk_activity.RegisterOptions{Name: activities.CleanUpActivityName})
w.RegisterActivityWithOptions(activities.NewHidePackageActivity(pipelineRegistry).Execute, temporalsdk_activity.RegisterOptions{Name: activities.HidePackageActivityName})
w.RegisterActivityWithOptions(activities.NewDeleteOriginalActivity(wsvc).Execute, temporalsdk_activity.RegisterOptions{Name: activities.DeleteOriginalActivityName})
w.RegisterActivityWithOptions(activities.NewDisposeOriginalActivity(wsvc).Execute, temporalsdk_activity.RegisterOptions{Name: activities.DisposeOriginalActivityName})
w.RegisterActivityWithOptions(activities.NewPopulateMetadataActivity(pipelineRegistry).Execute, temporalsdk_activity.RegisterOptions{Name: activities.PopulateMetadataActivityName})
w.RegisterActivityWithOptions(workflow.NewAsyncCompletionActivity(colsvc).Execute, temporalsdk_activity.RegisterOptions{Name: workflow.AsyncCompletionActivityName})
w.RegisterActivityWithOptions(nha_activities.NewUpdateHARIActivity(h).Execute, temporalsdk_activity.RegisterOptions{Name: nha_activities.UpdateHARIActivityName})
w.RegisterActivityWithOptions(nha_activities.NewUpdateProductionSystemActivity(h).Execute, temporalsdk_activity.RegisterOptions{Name: nha_activities.UpdateProductionSystemActivityName})
w.RegisterWorkflowWithOptions(collection.BulkWorkflow, temporalsdk_workflow.RegisterOptions{Name: collection.BulkWorkflowName})
w.RegisterActivityWithOptions(collection.NewBulkActivity(colsvc).Execute, temporalsdk_activity.RegisterOptions{Name: collection.BulkActivityName})
w.RegisterWorkflowWithOptions(batch.BatchWorkflow, temporalsdk_workflow.RegisterOptions{Name: batch.BatchWorkflowName})
w.RegisterActivityWithOptions(batch.NewBatchActivity(batchsvc).Execute, temporalsdk_activity.RegisterOptions{Name: batch.BatchActivityName})
g.Add(
func() error {
if err := w.Start(); err != nil {
return err
}
<-done
return nil
},
func(err error) {
w.Stop()
close(done)
},
)
}
// Observability server.
{
ln, err := net.Listen("tcp", config.DebugListen)
if err != nil {
logger.Error(err, "Error setting up the debug interface.")
os.Exit(1)
}
g.Add(func() error {
mux := http.NewServeMux()
// Health check.
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "OK")
})
// Prometheus metrics.
mux.Handle("/metrics", promhttp.Handler())
// Profiling data.
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
mux.Handle("/debug/pprof/block", pprof.Handler("block"))
mux.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine"))
mux.Handle("/debug/pprof/heap", pprof.Handler("heap"))
mux.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
srv := &http.Server{
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
Handler: mux,
}
return srv.Serve(ln)
}, func(error) {
ln.Close()
})
}
// Signal handler.
{
var (
cancelInterrupt = make(chan struct{})
ch = make(chan os.Signal, 2)
)
defer close(ch)
g.Add(
func() error {
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
select {
case <-ch:
case <-cancelInterrupt:
}
return nil
}, func(err error) {
logger.Info("Quitting...")
close(cancelInterrupt)
cancel()
signal.Stop(ch)
},
)
}
err = g.Run()
if err != nil {
logger.Error(err, "Application failure.")
os.Exit(1)
}
logger.Info("Bye!")
}
type configuration struct {
Verbosity int
Debug bool
DebugListen string
API api.Config
ExtractActivity archive.Config
Database db.Config
Temporal temporal.Config
Watcher watcher.Config
Pipeline []pipeline.Config
Validation validation.Config
Telemetry TelemetryConfig
Metadata metadata.Config
Worker WorkerConfig
Workflow workflow.Config
// This is a workaround for client-specific functionality.
// Simple mechanism to support an arbitrary number of hooks and parameters.
Hooks map[string]map[string]interface{}
}
type WorkerConfig struct {
HeartbeatThrottleInterval time.Duration
MaxConcurrentSessionExecutionSize int
MaxConcurrentWorkflowsExecutionsSize int
}
func (c configuration) Validate() error {
return nil
}
func configureViper(v *viper.Viper) {
v.AddConfigPath(".")
v.AddConfigPath("$HOME/.config/")
v.AddConfigPath("/etc")
v.SetConfigName(appName)
v.SetDefault("debugListen", "127.0.0.1:9001")
v.SetDefault("api.listen", "127.0.0.1:9000")
v.Set("api.appVersion", version)
temporal.SetDefaults(v)
}
func readConfig(v *viper.Viper, config *configuration, configFile string) (found bool, err error) {
if configFile != "" {
v.SetConfigFile(configFile)
}
err = v.ReadInConfig()
_, ok := err.(viper.ConfigFileNotFoundError)
if !ok {
found = true
}
if found && err != nil {
return found, fmt.Errorf("failed to read configuration file: %w", err)
}
err = v.Unmarshal(config)
if err != nil {
return found, fmt.Errorf("failed to unmarshal configuration: %w", err)
}
if err := config.Validate(); err != nil {
return found, fmt.Errorf("failed to validate the provided config: %w", err)
}
return found, nil
}
type TelemetryConfig struct {
Traces struct {
Enabled bool
Address string
SamplingRatio *float64
}
}
func initTracerProvider(ctx context.Context, logger logr.Logger, cfg TelemetryConfig) (trace.TracerProvider, func(context.Context) error, error) {
if !cfg.Traces.Enabled || cfg.Traces.Address == "" {
logger.V(1).Info("Tracing system is disabled.", "enabled", cfg.Traces.Enabled, "addr", cfg.Traces.Address)
shutdown := func(context.Context) error { return nil }
return noop.NewTracerProvider(), shutdown, nil
}
conn, err := grpc.DialContext(
ctx,
cfg.Traces.Address,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
)
if err != nil {
return nil, nil, fmt.Errorf("can't connect to telemetry data collector: %v", err)
}
exporter, err := otlptracegrpc.New(ctx, otlptracegrpc.WithGRPCConn(conn))
if err != nil {
return nil, nil, fmt.Errorf("can't create gRPC telemetry data exporter: %v", err)
}
resource, _ := resource.Merge(
resource.Default(),
resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceName(appName),
semconv.ServiceVersion(version),
),
)
var ratio float64 = 1
if cfg.Traces.SamplingRatio != nil {
ratio = *cfg.Traces.SamplingRatio
}
sampler := sdktrace.ParentBased(sdktrace.TraceIDRatioBased(ratio))
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sampler),
sdktrace.WithResource(resource),
sdktrace.WithBatcher(exporter),
)
shutdown := func(context.Context) error { return tp.Shutdown(ctx) }
logger.V(1).Info("Using OTel gRPC tracer provider.", "addr", cfg.Traces.Address, "ratio", ratio)
return tp, shutdown, nil
}