Skip to content

Commit

Permalink
[worker] jobFeedDaemonStatus should also populate FeedObjectConfigFor…
Browse files Browse the repository at this point in the history
…ClusterIDH

This will allow earlier detection of object without config when jobFeedDaemonPing is
never called.
  • Loading branch information
cgalibern committed Aug 19, 2024
1 parent 7c433fc commit dcc3ab9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 36 deletions.
39 changes: 39 additions & 0 deletions worker/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import (
"database/sql"
"fmt"
"log/slog"
"strings"
"time"

"github.com/go-redis/redis/v8"
"github.com/prometheus/client_golang/prometheus"

"github.com/opensvc/oc3/cachekeys"
)

type (
Expand Down Expand Up @@ -177,3 +180,39 @@ func (j *BaseJob) dropPending() error {
func (d *BaseJob) pushFromTableChanges() error {
return pushFromTableChanges(d.ctx, d.oDb, d.ev)
}

// populateFeedObjectConfigForClusterIDH HSET FeedObjectConfigForClusterIDH <clusterID> with the names of objects
// without config or HDEL FeedObjectConfigForClusterIDH <clusterID> if there are no missing configs.
func (d *BaseJob) populateFeedObjectConfigForClusterIDH(clusterID string, byObjectID map[string]*DBObject) error {
needConfig := make(map[string]struct{})
for _, obj := range byObjectID {
if obj.nullConfig {
objName := obj.svcname
// TODO: import om3 naming ?
if strings.Contains(objName, "/svc/") ||
strings.Contains(objName, "/vol/") ||
strings.HasPrefix(objName, "svc/") ||
strings.HasPrefix(objName, "vol/") ||
!strings.Contains(objName, "/") {
needConfig[objName] = struct{}{}
}
}
}

keyName := cachekeys.FeedObjectConfigForClusterIDH

if len(needConfig) > 0 {
l := make([]string, 0, len(needConfig))
for k := range needConfig {
l = append(l, k)
}
if err := d.redis.HSet(d.ctx, keyName, clusterID, strings.Join(l, " ")).Err(); err != nil {
return fmt.Errorf("populateFeedObjectConfigForClusterIDH: HSet %s %s: %w", keyName, clusterID, err)
}
} else {
if err := d.redis.HDel(d.ctx, keyName, clusterID).Err(); err != nil {
return fmt.Errorf("populateFeedObjectConfigForClusterIDH: HDEL %s %s: %w", keyName, clusterID, err)
}
}
return nil
}
40 changes: 4 additions & 36 deletions worker/job_feed_daemon_ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package worker
import (
"fmt"
"log/slog"
"strings"

"github.com/opensvc/oc3/cachekeys"
)
Expand Down Expand Up @@ -44,7 +43,7 @@ func (d *jobFeedDaemonPing) Operations() []operation {
{desc: "daemonPing/dbFetchObjects", do: d.dbFetchObjects},
{desc: "daemonPing/dbPingInstances", do: d.dbPingInstances},
{desc: "daemonPing/dbPingObjects", do: d.dbPingObjects},
{desc: "daemonPing/dbObjectsWithoutConfig", do: d.dbObjectsWithoutConfig},
{desc: "daemonPing/cacheObjectsWithoutConfig", do: d.cacheObjectsWithoutConfig},
{desc: "daemonPing/pushFromTableChanges", do: d.pushFromTableChanges},
}
}
Expand Down Expand Up @@ -111,38 +110,7 @@ func (d *jobFeedDaemonPing) dbPingObjects() (err error) {
return nil
}

// dbObjectsWithoutConfig populate FeedObjectConfigForClusterIDH with
// name of objects without config
func (d *jobFeedDaemonPing) dbObjectsWithoutConfig() error {
needConfig := make(map[string]struct{})
for _, obj := range d.byObjectID {
if obj.nullConfig {
objName := obj.svcname
// TODO: import om3 naming ?
if strings.Contains(objName, "/svc/") ||
strings.Contains(objName, "/vol/") ||
strings.HasPrefix(objName, "svc/") ||
strings.HasPrefix(objName, "vol/") ||
!strings.Contains(objName, "/") {
needConfig[objName] = struct{}{}
}
}
}

keyName := cachekeys.FeedObjectConfigForClusterIDH

if len(needConfig) > 0 {
l := make([]string, 0, len(needConfig))
for k := range needConfig {
l = append(l, k)
}
if err := d.redis.HSet(d.ctx, keyName, d.clusterID, strings.Join(l, " ")).Err(); err != nil {
return fmt.Errorf("detectObjectWithoutConfig: HSet %s %s: %w", keyName, d.clusterID, err)
}
} else {
if err := d.redis.HDel(d.ctx, keyName, d.clusterID).Err(); err != nil {
return fmt.Errorf("detectObjectWithoutConfig: HDEL %s %s: %w", keyName, d.clusterID, err)
}
}
return nil
// cacheObjectsWithoutConfig populate FeedObjectConfigForClusterIDH with names of objects without config
func (d *jobFeedDaemonPing) cacheObjectsWithoutConfig() error {
return d.populateFeedObjectConfigForClusterIDH(d.clusterID, d.byObjectID)
}
6 changes: 6 additions & 0 deletions worker/job_feed_daemon_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func (d *jobFeedDaemonStatus) Operations() []operation {
{desc: "daemonStatus/dbUpdateInstances", do: d.dbUpdateInstances},
{desc: "daemonStatus/dbPurgeInstances", do: d.dbPurgeInstances},
{desc: "daemonStatus/dbPurgeServices", do: d.dbPurgeServices},
{desc: "daemonStatus/cacheObjectsWithoutConfig", do: d.cacheObjectsWithoutConfig},
{desc: "daemonStatus/pushFromTableChanges", do: d.pushFromTableChanges},
}
}
Expand Down Expand Up @@ -614,6 +615,11 @@ func (d *jobFeedDaemonStatus) dbPurgeServices() error {
return nil
}

// cacheObjectsWithoutConfig populate FeedObjectConfigForClusterIDH with names of objects without config
func (d *jobFeedDaemonStatus) cacheObjectsWithoutConfig() error {
return d.populateFeedObjectConfigForClusterIDH(d.clusterID, d.byObjectID)
}

func logDuration(s string, begin time.Time) {
slog.Debug(fmt.Sprintf("STAT: %s elapse: %s", s, time.Now().Sub(begin)))
}
Expand Down

0 comments on commit dcc3ab9

Please sign in to comment.