Skip to content

Commit

Permalink
maintenance: improve api and address reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
tsahee committed Jan 24, 2025
1 parent 95b803a commit b3cd355
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
11 changes: 6 additions & 5 deletions arbnode/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package arbnode

import (
"context"
"errors"
"fmt"
"time"

Expand Down Expand Up @@ -67,12 +68,12 @@ type MaintenanceAPI struct {
runner *MaintenanceRunner
}

func (a *MaintenanceAPI) GetLastMaintenanceTime(ctx context.Context) (string, error) {
lastRun, err := a.runner.GetLastMaintenanceTime()
if err != nil {
return "", err
func (a *MaintenanceAPI) SecondsSinceLastMaintenance(ctx context.Context) (int64, error) {
running, since := a.runner.TimeSinceLastMaintenance()
if running {
return 0, errors.New("maintenance currently running")
}
return lastRun.GoString(), err
return int64(since.Seconds()), nil
}

func (a *MaintenanceAPI) Trigger(ctx context.Context) error {
Expand Down
28 changes: 19 additions & 9 deletions arbnode/maintenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func NewMaintenanceRunner(config MaintenanceConfigFetcher, seqCoordinator *SeqCo
dbs: dbs,
}

// node restart is considered "maintenance"
res.lastMaintenance.Store(time.Now().UnixMilli())
if seqCoordinator != nil {
c := func() *redislock.SimpleCfg { return &cfg.Lock }
Expand Down Expand Up @@ -141,12 +142,22 @@ func wentPastTimeOfDay(before time.Time, after time.Time, timeOfDay int) bool {
return prevMinutes < dbCompactionMinutes && newMinutes >= dbCompactionMinutes
}

func (mr *MaintenanceRunner) GetLastMaintenanceTime() (time.Time, error) {
// bool if running currently, if false - time of last time it was running
func (mr *MaintenanceRunner) getPrevMaintenance() (bool, time.Time) {
milli := mr.lastMaintenance.Load()
if milli == 0 {
return time.Time{}, errors.New("maintenance running")
return true, time.Time{}
}
return time.UnixMilli(milli), nil
return false, time.UnixMilli(milli)
}

// bool if running currently, if false - time of last time it was running
func (mr *MaintenanceRunner) TimeSinceLastMaintenance() (bool, time.Duration) {
running, maintTime := mr.getPrevMaintenance()
if running {
return true, 0
}
return false, time.Since(maintTime)
}

func (mr *MaintenanceRunner) setMaintenanceDone() {
Expand All @@ -173,16 +184,16 @@ func (mr *MaintenanceRunner) maybeRunScheduledMaintenance(ctx context.Context) t

now := time.Now().UTC()

lastMaintenance, err := mr.GetLastMaintenanceTime()
if err != nil {
inMaintenance, lastMaintenance := mr.getPrevMaintenance()
if inMaintenance {
return time.Minute
}

if !wentPastTimeOfDay(lastMaintenance, now, config.minutesAfterMidnight) {
return time.Minute
}

err = mr.attemptMaintenance(ctx)
err := mr.attemptMaintenance(ctx)
if err != nil {
log.Warn("scheduled maintenance error", "err", err)
}
Expand All @@ -195,9 +206,8 @@ func (mr *MaintenanceRunner) Trigger() error {
return errors.New("maintenance not configured to be triggerable")
}
// error if already running
_, err := mr.GetLastMaintenanceTime()
if err != nil {
return err
if running, _ := mr.getPrevMaintenance(); running {
return nil
}
// maintenance takes a long time, run on a separate thread
mr.LaunchThread(func(ctx context.Context) {
Expand Down

0 comments on commit b3cd355

Please sign in to comment.