Skip to content

Commit

Permalink
make condition take data source status
Browse files Browse the repository at this point in the history
  • Loading branch information
cwaldren-ld committed Dec 11, 2024
1 parent b7a24bb commit 9f387a9
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions internal/datasystem/fdv2_datasystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ type FDv2 struct {
mu sync.Mutex
status interfaces.DataSourceStatus

fallbackCond func() bool
recoveryCond func() bool
fallbackCond func(status interfaces.DataSourceStatus) bool
recoveryCond func(status interfaces.DataSourceStatus) bool
}

// NewFDv2 creates a new instance of the FDv2 data system. The first argument indicates if the system is enabled or
Expand Down Expand Up @@ -112,19 +112,14 @@ func NewFDv2(disabled bool, cfgBuilder subsystems.ComponentConfigurer[subsystems
fdv2.primarySync = cfg.Synchronizers.Primary
fdv2.secondarySync = cfg.Synchronizers.Secondary
fdv2.disabled = disabled
fdv2.fallbackCond = func() bool {
status := fdv2.getStatus()
fdv2.loggers.Debugf("Status: %s", status.String())
fdv2.fallbackCond = func(status interfaces.DataSourceStatus) bool {
interruptedAtRuntime := status.State == interfaces.DataSourceStateInterrupted && time.Since(status.StateSince) > 1*time.Minute
cannotInitialize := status.State == interfaces.DataSourceStateInitializing && time.Since(status.StateSince) > 10*time.Second
healthyForTooLong := status.State == interfaces.DataSourceStateValid && time.Since(status.StateSince) > 30*time.Second

return interruptedAtRuntime || cannotInitialize || healthyForTooLong
}
fdv2.recoveryCond = func() bool {
status := fdv2.getStatus()
fdv2.loggers.Debugf("Status: %s", status.String())

fdv2.recoveryCond = func(status interfaces.DataSourceStatus) bool {
interruptedAtRuntime := status.State == interfaces.DataSourceStateInterrupted && time.Since(status.StateSince) > 1*time.Minute
healthyForTooLong := status.State == interfaces.DataSourceStateValid && time.Since(status.StateSince) > 5*time.Minute
cannotInitialize := status.State == interfaces.DataSourceStateInitializing && time.Since(status.StateSince) > 10*time.Second
Expand Down Expand Up @@ -306,15 +301,17 @@ func (f *FDv2) runSynchronizers(ctx context.Context, closeWhenReady chan struct{
})
}

func (f *FDv2) evaluateCond(ctx context.Context, cond func() bool) error {
func (f *FDv2) evaluateCond(ctx context.Context, cond func(status interfaces.DataSourceStatus) bool) error {
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
if cond() {
status := f.getStatus()
f.loggers.Debugf("Data source status used to evaluate condition: %s", status.String())
if cond(status) {
return nil
}
f.loggers.Debugf("Condition check succeeded, continue with current synchronizer")
Expand Down

0 comments on commit 9f387a9

Please sign in to comment.