Skip to content

Commit

Permalink
[imon] Refactor with orchestrationResource
Browse files Browse the repository at this point in the history
Replaced individual timers and scheduling maps with a unified orchestrationResource
struct for better modularity and maintainability.
  • Loading branch information
cgalibern committed Jan 10, 2025
1 parent 10ae0db commit 5b9b08c
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 167 deletions.
28 changes: 14 additions & 14 deletions daemon/imon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,21 +136,11 @@ type (
// Its Value is created/refreshed during func initResourceMonitor.
initialMonitorAction instance.MonitorAction

// resourceRestartTimer represents a timer used to schedule the restart
// of non-standby resources.
resourceRestartTimer *time.Timer
// standbyResourceOrchestrate is the orchestrationResource for standby resources
standbyResourceOrchestrate orchestrationResource

// resourceStandbyRestartTimer is a timer used to schedule the restart
// of standby resources.
resourceStandbyRestartTimer *time.Timer

// resourceWithRestartScheduled maintains a mapping of non-standby
// resource identifiers to their restart scheduled as boolean values.
resourceWithRestartScheduled map[string]bool

// resourceStandbyWithRestartScheduled maintains a mapping of standby
// resource identifiers to their restart scheduled as boolean values.
resourceStandbyWithRestartScheduled map[string]bool
// standbyResourceOrchestrate is the orchestrationResource for regular resources
regularResourceOrchestrate orchestrationResource
}

// cmdOrchestrate can be used from post action go routines
Expand Down Expand Up @@ -245,6 +235,9 @@ func start(parent context.Context, qs pubsub.QueueSizer, p naming.Path, nodes []
}

t.log = t.newLogger(uuid.Nil)
t.regularResourceOrchestrate.log = t.newResourceLogger("regular resource")
t.standbyResourceOrchestrate.log = t.newResourceLogger( "standby resource")

t.startSubscriptions(qs)

go func() {
Expand All @@ -254,13 +247,20 @@ func start(parent context.Context, qs pubsub.QueueSizer, p naming.Path, nodes []
return nil
}

func (t *Manager) newResourceLogger(s string) *plog.Logger {
return naming.LogWithPath(plog.NewDefaultLogger(), t.path).
Attr("pkg", "daemon/imon").
WithPrefix(fmt.Sprintf("daemon: imon: %s: %s: ", t.path.String(), s))
}

func (t *Manager) newLogger(i uuid.UUID) *plog.Logger {
return naming.LogWithPath(plog.NewDefaultLogger(), t.path).
Attr("pkg", "daemon/imon").
Attr("orchestration_id", i.String()).
WithPrefix(fmt.Sprintf("daemon: imon: %s: ", t.path.String()))
}


func (t *Manager) startSubscriptions(qs pubsub.QueueSizer) {
sub := t.pubsubBus.Sub("daemon.imon "+t.id, qs)
sub.AddFilter(&msgbus.NodeConfigUpdated{}, t.labelLocalhost)
Expand Down
20 changes: 10 additions & 10 deletions daemon/imon/main_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -1049,24 +1049,24 @@ func (t *Manager) doLastAction(action func() error, newState, successState, erro
func (t *Manager) initResourceMonitor() {
// Stop any pending restart timers before init. We may be called after
// instance config refreshed with some previous resource restart scheduled.
t.resetResourceMonitorTimers()
t.cancelResourceOrchestrateSchedules()

logDropped := func(l map[string]bool, comment string) {
if l != nil {
logDropped := func(or *orchestrationResource) {
if or != nil && or.scheduled != nil {
dropped := make([]string, 0)
for rid := range l {
for rid := range or.scheduled {
dropped = append(dropped, rid)
}
if len(dropped) > 0 {
t.log.Infof("instance config has been updated: drop previously scheduled restart %s %v", comment, dropped)
or.log.Infof("instance config has been updated: drop previously scheduled restarts %v", dropped)
}
}
}
logDropped(t.resourceWithRestartScheduled, "resources")
logDropped(t.resourceStandbyWithRestartScheduled, "standby resources")
logDropped(&t.regularResourceOrchestrate)
logDropped(&t.standbyResourceOrchestrate)

t.resourceWithRestartScheduled = make(map[string]bool)
t.resourceStandbyWithRestartScheduled = make(map[string]bool)
t.regularResourceOrchestrate.scheduled = make(map[string]bool)
t.standbyResourceOrchestrate.scheduled = make(map[string]bool)

if monitorAction, ok := t.getValidMonitorAction(0); !ok {
t.initialMonitorAction = instance.MonitorActionNone
Expand All @@ -1084,7 +1084,7 @@ func (t *Manager) initResourceMonitor() {
},
}
if rcfg.IsMonitored && hasMonitorActionNone {
t.log.Infof("unusable monitor action: resource %s is monitored, but monitor action is none", rid)
t.orchestrationResource(rcfg.IsStandby).log.Infof("rid %s is monitored, but monitor action is none", rid)
}
}
t.state.Resources = m
Expand Down
Loading

0 comments on commit 5b9b08c

Please sign in to comment.