Skip to content

Commit

Permalink
Add function parameters for crash simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
mtneug committed Nov 21, 2023
1 parent 0e5b617 commit c93e110
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ task:
config:
mmsys-test-encode.engine.nagare.media/test: baseline-simple
mmsys-test-encode.engine.nagare.media/chunk-seconds: "60"
mmsys-test-encode.engine.nagare.media/max-number-of-simulated-crashes: "2"
mmsys-test-encode.engine.nagare.media/simulated-crash-wait-duration: 360s

system:
nats:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ task:
config:
mmsys-test-encode.engine.nagare.media/test: baseline-split-merge
mmsys-test-encode.engine.nagare.media/chunk-seconds: "60"
mmsys-test-encode.engine.nagare.media/max-number-of-simulated-crashes: "2"
mmsys-test-encode.engine.nagare.media/simulated-crash-wait-duration: 360s

system:
nats:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ task:
config:
mmsys-test-encode.engine.nagare.media/test: test-no-recovery-simple
mmsys-test-encode.engine.nagare.media/chunk-seconds: "60"
mmsys-test-encode.engine.nagare.media/max-number-of-simulated-crashes: "2"
mmsys-test-encode.engine.nagare.media/simulated-crash-wait-duration: 360s

system:
nats:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ task:
config:
mmsys-test-encode.engine.nagare.media/test: test-no-recovery-split-merge
mmsys-test-encode.engine.nagare.media/chunk-seconds: "60"
mmsys-test-encode.engine.nagare.media/max-number-of-simulated-crashes: "2"
mmsys-test-encode.engine.nagare.media/simulated-crash-wait-duration: 360s

system:
nats:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ task:
config:
mmsys-test-encode.engine.nagare.media/test: test-recovery-split-merge
mmsys-test-encode.engine.nagare.media/chunk-seconds: "60"
mmsys-test-encode.engine.nagare.media/max-number-of-simulated-crashes: "2"
mmsys-test-encode.engine.nagare.media/simulated-crash-wait-duration: 360s

system:
nats:
Expand Down
44 changes: 32 additions & 12 deletions internal/functions/mmsys-test-encode/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,13 @@ import (
const (
Name = "mmsys-test-encode"

TestParameterKey = "mmsys-test-encode.engine.nagare.media/test"
ChunkSecondsParameterKey = "mmsys-test-encode.engine.nagare.media/chunk-seconds"
TestParameterKey = "mmsys-test-encode.engine.nagare.media/test"
ChunkSecondsParameterKey = "mmsys-test-encode.engine.nagare.media/chunk-seconds"
MaxNumberOfSimulatedCrashesParameterKey = "mmsys-test-encode.engine.nagare.media/max-number-of-simulated-crashes"
SimulatedCrashWaitDurationParameterKey = "mmsys-test-encode.engine.nagare.media/simulated-crash-wait-duration"

DefaultMaxNumberOfSimulatedCrashes = 2
DefaultSimulatedCrashWaitDuration = 120 * time.Second
)

const (
Expand Down Expand Up @@ -84,11 +89,6 @@ const (
TestRecoverySplitMergeDistributed = "test-recovery-split-merge-distributed"
)

const (
MaxNumberOfSimulatedCrashes = 2
SimulatedCrashWaitDuration = 120 * time.Second
)

const (
MediaEncodedEventType = "media.nagare.engine.v1alpha1.functions.mmsys-test-encode.media-encoded"
MediaPackagedEventType = "media.nagare.engine.v1alpha1.functions.mmsys-test-encode.media-packaged"
Expand All @@ -109,8 +109,10 @@ type function struct {
instanceNumber int

// config
test string
chunkSeconds int
test string
chunkSeconds int
maxNumberOfSimulatedCrashes int
simulatedCrashWaitDuration time.Duration

// input
inVideoURL string
Expand Down Expand Up @@ -629,15 +631,15 @@ func (f *function) execEventAPIServer(ctx context.Context, mediaEventsCh chan<-
func (f *function) setupSimulatedCrash(ctx context.Context) {
l := log.FromContext(ctx)

if f.instanceNumber >= MaxNumberOfSimulatedCrashes {
if f.instanceNumber >= f.maxNumberOfSimulatedCrashes {
l.Info("reached max number of simulated crashes: disable simulated crash")
return
}

// simulate hard crash after 2m
l.Info("enable simulated crash")
go func() {
time.Sleep(SimulatedCrashWaitDuration)
time.Sleep(f.simulatedCrashWaitDuration)
l.Info("simulate crash now")
os.Exit(1)
}()
Expand Down Expand Up @@ -708,7 +710,25 @@ func BuildTask(ctx context.Context, t *nbmpv2.Task) (nbmp.Function, error) {
}
f.chunkSeconds, err = strconv.Atoi(chunkSecondsStr)
if err != nil {
return nil, errors.New("chunk-seconds is not an integer")
return nil, fmt.Errorf("%s is not an integer", ChunkSecondsParameterKey)
}

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
Expand Down

0 comments on commit c93e110

Please sign in to comment.