-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.go
618 lines (500 loc) · 15.8 KB
/
func.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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*
Copyright 2022-2024 The nagare media authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sleep
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"net/http"
"os"
"os/exec"
"regexp"
"runtime"
"strconv"
"strings"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/log"
backoff "github.com/cenkalti/backoff/v4"
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
enginev1 "github.com/nagare-media/engine/api/v1alpha1"
"github.com/nagare-media/engine/internal/functions"
enginehttp "github.com/nagare-media/engine/internal/pkg/http"
"github.com/nagare-media/engine/internal/pkg/mime"
"github.com/nagare-media/engine/internal/pkg/uuid"
"github.com/nagare-media/engine/pkg/events"
"github.com/nagare-media/engine/pkg/nbmp"
nbmputils "github.com/nagare-media/engine/pkg/nbmp/utils"
nbmpv2 "github.com/nagare-media/models.go/iso/nbmp/v2"
)
// Function description
const (
Name = "mmsys-test-scene-detection"
TestParameterKey = "mmsys-test-scene-detection.engine.nagare.media/test"
MaxNumberOfSimulatedCrashesParameterKey = "mmsys-test-scene-detection.engine.nagare.media/max-number-of-simulated-crashes"
SimulatedCrashWaitDurationParameterKey = "mmsys-test-scene-detection.engine.nagare.media/simulated-crash-wait-duration"
DefaultMaxNumberOfSimulatedCrashes = 1
DefaultSimulatedCrashWaitDuration = 20 * time.Second
)
const (
// * baseline: scene detection
// * test-no-recovery: scene detection with n disruptions
// * test-recovery: scene detection with n disruptions and recovery
Baseline = "baseline"
TestNoRecovery = "test-no-recovery"
TestRecovery = "test-recovery"
)
var (
ffmpegScdetRegex = regexp.MustCompile(`.*scdet.*lavfi\.scd\.score:\D+(?P<score>\d+\.\d+),.*lavfi\.scd\.time:\D+(?P<time>\d+\.\d+)`)
scoreIndex = ffmpegScdetRegex.SubexpIndex("score")
timeIndex = ffmpegScdetRegex.SubexpIndex("time")
)
const (
SceneDetectedEventType = "media.nagare.engine.v1alpha1.functions.mmsys-test-scene-detection.scene-detected"
)
type SceneDetectionEventData struct {
Time float64
}
// function mmsys-test-scene-detection implements various video encoding test for the evaluation portion of an MMSys paper.
type function struct {
// meta
task *nbmpv2.Task
workflowID string
taskID string
instanceID string
instanceNumber int
// config
test string
maxNumberOfSimulatedCrashes int
simulatedCrashWaitDuration time.Duration
// input
inVideoURL string
// output
awsAccessID string
awsSecretAccessKey string
s3Insecure bool
s3Endpoint string
s3Bucket string
s3ObjectKey string
s3 *minio.Client
reportClient events.Client
}
var _ nbmp.Function = &function{}
// Exec mmsys-test-scene-detection function.
func (f *function) Exec(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
l := log.FromContext(ctx, "test", f.test).WithName(Name)
ctx = log.IntoContext(ctx, l)
l.Info(fmt.Sprintf("executing %s", f.test))
switch f.test {
case Baseline:
return f.baseline(ctx)
case TestNoRecovery:
return f.testNoRecovery(ctx)
case TestRecovery:
return f.testRecovery(ctx)
default:
return fmt.Errorf("unknown test '%s'", f.test)
}
}
func (f *function) baseline(ctx context.Context) error {
l := log.FromContext(ctx)
scenes, err := f.detectScenes(ctx, 0, f.inVideoURL)
if err != nil {
l.Error(err, "scene detection failed")
return err
}
err = f.outputScenes(ctx, scenes, f.s3ObjectKey)
if err != nil {
l.Error(err, "writing scenes to S3 failed")
return err
}
return nil
}
func (f *function) testNoRecovery(ctx context.Context) error {
l := log.FromContext(ctx)
// count number of previous task instances for simulated crash
_ = f.syncSceneDetectionEvents(ctx)
l.Info(fmt.Sprintf("previous task instances: %d", f.instanceNumber))
f.setupSimulatedCrash(ctx)
return f.baseline(ctx)
}
func (f *function) testRecovery(ctx context.Context) error {
l := log.FromContext(ctx)
// sync with previous instances of this task
l.Info("recover from previous task instances")
scenes := f.syncSceneDetectionEvents(ctx)
if f.instanceNumber > 0 {
l.Info(fmt.Sprintf("synced with %d previous task instance(s)", f.instanceNumber))
} else {
l.Info("no previous task instances found")
}
// simulate crash
f.setupSimulatedCrash(ctx)
// detect scenes
seekSecond := 0.0
if n := len(scenes); n > 0 {
seekSecond = scenes[n-1]
}
newScenes, err := f.detectScenes(ctx, seekSecond, f.inVideoURL)
if err != nil {
l.Error(err, "scene detection failed")
return err
}
// merge with previously detected scenes
scenes = append(scenes, newScenes...)
// output scenes
err = f.outputScenes(ctx, scenes, f.s3ObjectKey)
if err != nil {
l.Error(err, "writing scenes to S3 failed")
return err
}
return nil
}
func (f *function) detectScenes(ctx context.Context, seekSeconds float64, inUrl string) ([]float64, error) {
l := log.FromContext(ctx)
l.Info(fmt.Sprintf("detect scenes starting at %.3f in %s", seekSeconds, inUrl))
if seekSeconds < 0 {
return nil, fmt.Errorf("seek cannot be negative: %.3fs", seekSeconds)
}
var (
err error
scenes []float64
)
// open /dev/null
devNull, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0755)
if err != nil {
return nil, err
}
// FFmpeg process
ffmpegArgs := []string{"-hide_banner", "-nostats"}
if seekSeconds > 0 {
ffmpegArgs = append(ffmpegArgs, "-ss", fmt.Sprintf("%.3f", seekSeconds))
}
ffmpegArgs = append(ffmpegArgs,
"-copyts",
"-reconnect", "1",
"-reconnect_on_network_error", "1",
"-reconnect_on_http_error", "1",
"-reconnect_delay_max", "5",
"-i", inUrl,
"-filter:v", "scdet=threshold=14.0",
"-an",
"-f", "null",
"-",
)
ffmpegCmd := exec.CommandContext(ctx, "ffmpeg", ffmpegArgs...)
ffmpegCmd.Stdout = devNull // ignore stdout
ffmpegPipe, err := ffmpegCmd.StderrPipe()
if err != nil {
return nil, err
}
ffmpegCmd.Cancel = func() error {
if runtime.GOOS == "windows" {
return ffmpegCmd.Process.Signal(os.Kill)
}
go func() {
time.Sleep(30 * time.Second)
_ = ffmpegCmd.Process.Signal(os.Kill)
}()
return ffmpegCmd.Process.Signal(os.Interrupt)
}
// start FFmpeg
l.V(1).Info(fmt.Sprintf("running command %s", ffmpegCmd))
if err = ffmpegCmd.Start(); err != nil {
l.Error(err, "FFmpeg failed")
return nil, err
}
// scan output for detected scenes
s := bufio.NewScanner(ffmpegPipe)
for s.Scan() {
matches := ffmpegScdetRegex.FindStringSubmatch(s.Text())
if matches == nil {
continue
}
// found a scene
time, err := strconv.ParseFloat(matches[timeIndex], 64)
if err != nil {
l.Error(err, "parsing scene detection failed; fix implementation")
return nil, err
}
scenes = append(scenes, time)
f.observeSceneDetectionEvent(ctx, SceneDetectedEventType, SceneDetectionEventData{
Time: time,
})
l.Info("detected a scene", "time", time, "score", matches[scoreIndex])
}
// call Wait on cmds to clean up resources
if err = ffmpegCmd.Wait(); err != nil {
l.Error(err, "FFmpeg failed")
return nil, err
}
return scenes, nil
}
func (f *function) outputScenes(ctx context.Context, scenes []float64, outObjKey string) error {
l := log.FromContext(ctx)
outObjURL := strings.Join([]string{f.s3.EndpointURL().String(), f.s3Bucket, outObjKey}, "/")
l.Info(fmt.Sprintf("output to %s", outObjURL))
file := &bytes.Buffer{}
for _, time := range scenes {
fmt.Fprintf(file, "%.2f\n", time)
}
_, err := f.s3.PutObject(ctx, f.s3Bucket, outObjKey, file, int64(file.Len()), minio.PutObjectOptions{ContentType: "text/plain"})
if err != nil {
l.Error(err, "S3 upload failed")
return err
}
return nil
}
func (f *function) syncSceneDetectionEvents(ctx context.Context) []float64 {
l := log.FromContext(ctx)
var (
scenes = make([]float64, 0)
sceneDetectionEventsCh = make(chan cloudevents.Event)
synceDone = make(chan struct{})
)
go f.execEventAPIServer(ctx, sceneDetectionEventsCh, synceDone)
for {
select {
case <-synceDone:
close(sceneDetectionEventsCh)
return scenes
case me := <-sceneDetectionEventsCh:
if me.Type() != SceneDetectedEventType {
continue
}
sded := &SceneDetectionEventData{}
if err := me.DataAs(sded); err != nil {
l.Error(err, "failed to decode scene detection event data")
continue
}
scenes = append(scenes, sded.Time)
}
}
}
func (f *function) execEventAPIServer(ctx context.Context, sceneDetectionEventsCh chan<- cloudevents.Event, synceDone chan struct{}) {
l := log.FromContext(ctx)
srv := enginehttp.NewServer(&enginev1.WebserverConfiguration{
BindAddress: ptr.To[string](":8080"),
ReadTimeout: &metav1.Duration{Duration: 1 * time.Minute},
WriteTimeout: &metav1.Duration{Duration: 1 * time.Minute},
IdleTimeout: &metav1.Duration{Duration: 1 * time.Minute},
Network: ptr.To[string]("tcp"),
PublicBaseURL: ptr.To[string]("http://127.0.0.1:8080"),
})
eventCh := make(chan cloudevents.Event)
events.API(eventCh).MountTo(srv.App)
l.Info("start event API server")
var (
srvErr error
srvDone = make(chan struct{})
)
go func() { srvErr = srv.Start(ctx); close(srvDone) }()
synced := false
subj := fmt.Sprintf("/engine.nagare.media/workflow(%s)/task(%s)/instance(%s)", f.workflowID, f.taskID, f.instanceID)
for {
select {
case <-ctx.Done():
l.Info("terminate event API server")
return
case <-srvDone:
if srvErr != nil {
panic(fmt.Sprintf("event API server failed: %s", srvErr))
}
case e := <-eventCh:
// ignore events after synced is done
if synced {
continue
}
switch e.Type() {
case SceneDetectedEventType:
sceneDetectionEventsCh <- e
case events.TaskCreated:
if e.Subject() != subj {
f.instanceNumber++
continue
}
// we are synced up to this instance
synced = true
close(synceDone)
}
}
}
}
func (f *function) setupSimulatedCrash(ctx context.Context) {
l := log.FromContext(ctx)
if f.instanceNumber >= f.maxNumberOfSimulatedCrashes {
l.Info("reached max number of simulated crashes: disable simulated crash")
return
}
// simulate hard crash
l.Info("enable simulated crash")
go func() {
time.Sleep(f.simulatedCrashWaitDuration)
l.Info("simulate crash now")
os.Exit(1)
}()
}
func (f *function) observeSceneDetectionEvent(ctx context.Context, t string, me SceneDetectionEventData) {
l := log.FromContext(ctx)
e := cloudevents.NewEvent()
e.SetID(uuid.UUIDv4())
e.SetType(t)
e.SetSource("/engine.nagare.media/functions/mmsys-test-scene-detection")
e.SetSubject(fmt.Sprintf("/engine.nagare.media/workflow(%s)/task(%s)/instance(%s)", f.workflowID, f.taskID, f.instanceID))
e.SetTime(time.Now())
if err := e.SetData(mime.ApplicationJSON, me); err != nil {
l.Error(err, "failed to encode data for event report")
return
}
if err := e.Validate(); err != nil {
panic(fmt.Sprintf("Event creation results in invalid CloudEvent; fix implementation! error: %s", err))
}
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
if err := f.reportClient.Send(ctx, e); err != nil {
l.Error(err, "failed to report event")
return
}
}
// BuildTask from mmsys-test-scene-detection function.
func BuildTask(ctx context.Context, t *nbmpv2.Task) (nbmp.Function, error) {
var (
ok bool
err error
f = &function{}
)
// meta
f.task = t
f.workflowID, ok = nbmputils.GetStringParameterValue(t.Configuration.Parameters, nbmp.EngineWorkflowIDParameterKey)
if !ok {
return nil, fmt.Errorf("missing %s config", nbmp.EngineWorkflowIDParameterKey)
}
f.taskID, ok = nbmputils.GetStringParameterValue(t.Configuration.Parameters, nbmp.EngineTaskIDParameterKey)
if !ok {
return nil, fmt.Errorf("missing %s config", nbmp.EngineTaskIDParameterKey)
}
f.instanceID = t.General.ID
// config
f.test, ok = nbmputils.GetStringParameterValue(t.Configuration.Parameters, TestParameterKey)
if !ok {
return nil, fmt.Errorf("missing %s config", TestParameterKey)
}
f.maxNumberOfSimulatedCrashes = DefaultMaxNumberOfSimulatedCrashes
maxNumberOfSimulatedCrashesStr, ok := nbmputils.GetStringParameterValue(t.Configuration.Parameters, MaxNumberOfSimulatedCrashesParameterKey)
if ok {
f.maxNumberOfSimulatedCrashes, err = strconv.Atoi(maxNumberOfSimulatedCrashesStr)
if err != nil {
return nil, fmt.Errorf("%s is not an integer", MaxNumberOfSimulatedCrashesParameterKey)
}
}
f.simulatedCrashWaitDuration = DefaultSimulatedCrashWaitDuration
simulatedCrashWaitDurationStr, ok := nbmputils.GetStringParameterValue(t.Configuration.Parameters, SimulatedCrashWaitDurationParameterKey)
if ok {
f.simulatedCrashWaitDuration, err = time.ParseDuration(simulatedCrashWaitDurationStr)
if err != nil {
return nil, fmt.Errorf("%s is not an integer", SimulatedCrashWaitDurationParameterKey)
}
}
// input
if len(t.Input.MediaParameters) == 0 {
return nil, errors.New("no media input")
}
// TODO: use port bindings to determine input
in := t.Input.MediaParameters[0]
f.inVideoURL = string(in.CachingServerURL)
if in.CachingServerURL == "" {
return nil, errors.New("missing caching-server-url for media input")
}
// output
if len(t.Output.MediaParameters) == 0 {
return nil, errors.New("no media output")
}
// TODO: use port bindings to determine output
out := t.Output.MediaParameters[0]
if out.CachingServerURL == "" {
return nil, errors.New("missing caching-server-url for media output")
}
url, err := out.CachingServerURL.URL()
if err != nil {
return nil, err
}
if url.Scheme != "s3" {
return nil, errors.New("output is not in an S3 bucket")
}
seg := strings.SplitN(url.Path[1:], "/", 2)
if len(seg) != 2 {
return nil, errors.New("output is missing object name")
}
f.s3Endpoint = url.Host
f.s3Bucket = seg[0]
f.s3ObjectKey = seg[1]
q, ok := url.Query()["AWS_ACCESS_KEY_ID"]
if !ok {
return nil, errors.New("output is missing AWS_ACCESS_KEY_ID")
}
f.awsAccessID = q[0]
q, ok = url.Query()["AWS_SECRET_ACCESS_KEY"]
if !ok {
return nil, errors.New("output is missing AWS_SECRET_ACCESS_KEY")
}
f.awsSecretAccessKey = q[0]
f.s3Insecure = true
q, ok = url.Query()["INSECURE"]
if ok {
f.s3Insecure, _ = strconv.ParseBool(q[0])
}
// initialize NBMP reporting
// TODO: DRY
if t.Reporting == nil {
return nil, errors.New("reporting description missing")
}
if t.Reporting.ReportType != nbmp.ReportTypeEngineCloudEvents {
return nil, fmt.Errorf("unsupported report type '%s'", t.Reporting.ReportType)
}
if t.Reporting.DeliveryMethod != nbmpv2.HTTP_POSTDeliveryMethod {
return nil, fmt.Errorf("unsupported delivery method '%s'", t.Reporting.ReportType)
}
c := &events.HTTPClient{
URL: string(t.Reporting.URL),
Client: http.DefaultClient,
}
expBackOff := &backoff.ExponentialBackOff{
InitialInterval: 500 * time.Millisecond,
RandomizationFactor: 0.25,
Multiplier: 1.5,
MaxInterval: 2 * time.Second,
MaxElapsedTime: 0, // = indefinitely (we use contexts for that)
Stop: backoff.Stop,
Clock: backoff.SystemClock,
}
f.reportClient = events.ClientWithBackoff(c, expBackOff)
// initialize S3
f.s3, err = minio.New(f.s3Endpoint, &minio.Options{
Creds: credentials.NewStaticV4(f.awsAccessID, f.awsSecretAccessKey, ""),
Secure: !f.s3Insecure,
})
if err != nil {
return nil, err
}
return f, nil
}
func init() {
functions.TaskBuilders.Register(Name, BuildTask)
}