Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Prometheus metrics for LocalQueue #3673

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmd/kueue/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ func main() {

metrics.Register()

if features.Enabled(features.LocalQueueMetrics) {
metrics.RegisterLQMetrics()
}

kubeConfig := ctrl.GetConfigOrDie()
if kubeConfig.UserAgent == "" {
kubeConfig.UserAgent = useragent.Default()
Expand Down
5 changes: 5 additions & 0 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,11 @@ func (c *Cache) DeleteClusterQueue(cq *kueue.ClusterQueue) {
if !ok {
return
}
if features.Enabled(features.LocalQueueMetrics) {
for _, q := range c.hm.ClusterQueues[cq.Name].localQueues {
metrics.ClearLocalQueueCacheMetrics(metrics.LQRefFromLocalQueueKey(q.key))
}
}
c.hm.DeleteClusterQueue(cq.Name)
metrics.ClearCacheMetrics(cq.Name)
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/cache/clusterqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ func (c *clusterQueue) updateQueueStatus() {
if status != c.Status {
c.Status = status
metrics.ReportClusterQueueStatus(c.Name, c.Status)
if features.Enabled(features.LocalQueueMetrics) {
for _, lq := range c.localQueues {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This iteration might be adding unnecessary performance cost. What is the scenario that it needs calling here? Maybe we could move the call per LQ, when we update the specific LQ. PTAL.

Copy link
Contributor Author

@KPostOffice KPostOffice Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lq status is equal to the cq status. So when the cq status updates, all the cq's associated lqs should have their statuses updated as well

metrics.ReportLocalQueueStatus(metrics.LQRefFromLocalQueueKey(lq.key), c.Status)
}
}
}
}

Expand Down Expand Up @@ -500,6 +505,12 @@ func (c *clusterQueue) reportActiveWorkloads() {
metrics.ReservingActiveWorkloads.WithLabelValues(c.Name).Set(float64(len(c.Workloads)))
}

func (q *queue) reportActiveWorkloads() {
qKeySlice := strings.Split(q.key, "/")
metrics.LocalQueueAdmittedActiveWorkloads.WithLabelValues(qKeySlice[1], qKeySlice[0]).Set(float64(q.admittedWorkloads))
metrics.LocalQueueReservingActiveWorkloads.WithLabelValues(qKeySlice[1], qKeySlice[0]).Set(float64(q.reservingWorkloads))
}

// updateWorkloadUsage updates the usage of the ClusterQueue for the workload
// and the number of admitted workloads for local queues.
func (c *clusterQueue) updateWorkloadUsage(wi *workload.Info, m int64) {
Expand Down Expand Up @@ -537,6 +548,9 @@ func (c *clusterQueue) updateWorkloadUsage(wi *workload.Info, m int64) {
updateFlavorUsage(frUsage, lq.admittedUsage, m)
lq.admittedWorkloads += int(m)
}
if features.Enabled(features.LocalQueueMetrics) {
lq.reportActiveWorkloads()
}
}
}

Expand Down Expand Up @@ -581,11 +595,18 @@ func (c *clusterQueue) addLocalQueue(q *kueue.LocalQueue) error {
}
}
c.localQueues[qKey] = qImpl
if features.Enabled(features.LocalQueueMetrics) {
qImpl.reportActiveWorkloads()
metrics.ReportLocalQueueStatus(metrics.LQRefFromLocalQueueKey(qKey), c.Status)
}
return nil
}

func (c *clusterQueue) deleteLocalQueue(q *kueue.LocalQueue) {
qKey := queueKey(q)
if features.Enabled(features.LocalQueueMetrics) {
metrics.ClearLocalQueueCacheMetrics(metrics.LQRefFromLocalQueueKey(qKey))
}
delete(c.localQueues, qKey)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func SetupControllers(mgr ctrl.Manager, qManager *queue.Manager, cc *cache.Cache
qManager,
cc,
WithQueueVisibilityUpdateInterval(queueVisibilityUpdateInterval(cfg)),
WithQueueVisibilityClusterQueuesMaxCount(queueVisibilityClusterQueuesMaxCount(cfg)),
WithReportResourceMetrics(cfg.Metrics.EnableClusterQueueResources),
WithQueueVisibilityClusterQueuesMaxCount(queueVisibilityClusterQueuesMaxCount(cfg)),
WithFairSharing(fairSharingEnabled),
WithWatchers(rfRec, acRec),
)
Expand Down
48 changes: 47 additions & 1 deletion pkg/controller/core/localqueue_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ import (
"sigs.k8s.io/kueue/pkg/cache"
"sigs.k8s.io/kueue/pkg/constants"
"sigs.k8s.io/kueue/pkg/controller/core/indexer"
"sigs.k8s.io/kueue/pkg/features"
"sigs.k8s.io/kueue/pkg/metrics"
"sigs.k8s.io/kueue/pkg/queue"
"sigs.k8s.io/kueue/pkg/util/resource"
)

const (
Expand All @@ -63,7 +66,11 @@ type LocalQueueReconciler struct {
wlUpdateCh chan event.GenericEvent
}

func NewLocalQueueReconciler(client client.Client, queues *queue.Manager, cache *cache.Cache) *LocalQueueReconciler {
func NewLocalQueueReconciler(
client client.Client,
queues *queue.Manager,
cache *cache.Cache,
) *LocalQueueReconciler {
return &LocalQueueReconciler{
log: ctrl.Log.WithName("localqueue-reconciler"),
queues: queues,
Expand Down Expand Up @@ -142,6 +149,10 @@ func (r *LocalQueueReconciler) Create(e event.CreateEvent) bool {
log.Error(err, "Failed to add localQueue to the cache")
}

if features.Enabled(features.LocalQueueMetrics) {
recordLocalQueueUsageMetrics(q)
}

return true
}

Expand All @@ -151,6 +162,11 @@ func (r *LocalQueueReconciler) Delete(e event.DeleteEvent) bool {
// No need to interact with the queue manager for other objects.
return true
}

if features.Enabled(features.LocalQueueMetrics) {
metrics.ClearLocalQueueResourceMetrics(localQueueReferenceFromLocalQueue(q))
}

r.log.V(2).Info("LocalQueue delete event", "localQueue", klog.KObj(q))
r.queues.DeleteLocalQueue(q)
r.cache.DeleteLocalQueue(q)
Expand Down Expand Up @@ -191,10 +207,40 @@ func (r *LocalQueueReconciler) Update(e event.UpdateEvent) bool {
}

r.queues.DeleteLocalQueue(oldLq)
if features.Enabled(features.LocalQueueMetrics) {
updateLocalQueueResourceMetrics(newLq)
}

return true
}

func localQueueReferenceFromLocalQueue(lq *kueue.LocalQueue) metrics.LocalQueueReference {
return metrics.LocalQueueReference{
Name: lq.Name,
Namespace: lq.Namespace,
}
}

func recordLocalQueueUsageMetrics(queue *kueue.LocalQueue) {
for _, flavor := range queue.Status.FlavorUsage {
for _, r := range flavor.Resources {
metrics.ReportLocalQueueResourceUsage(localQueueReferenceFromLocalQueue(queue), string(flavor.Name), string(r.Name), resource.QuantityToFloat(&r.Total))
}
}

for _, flavor := range queue.Status.FlavorsReservation {
for _, r := range flavor.Resources {
metrics.ReportLocalQueueResourceReservations(localQueueReferenceFromLocalQueue(queue), string(flavor.Name), string(r.Name), resource.QuantityToFloat(&r.Total))
}
}

}

func updateLocalQueueResourceMetrics(queue *kueue.LocalQueue) {
metrics.ClearLocalQueueResourceMetrics(localQueueReferenceFromLocalQueue(queue))
recordLocalQueueUsageMetrics(queue)
}

func (r *LocalQueueReconciler) Generic(e event.GenericEvent) bool {
r.log.V(3).Info("Got Workload event", "workload", klog.KObj(e.Object))
return true
Expand Down
8 changes: 8 additions & 0 deletions pkg/controller/core/workload_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1"
"sigs.k8s.io/kueue/pkg/cache"
"sigs.k8s.io/kueue/pkg/controller/core/indexer"
"sigs.k8s.io/kueue/pkg/features"
"sigs.k8s.io/kueue/pkg/metrics"
"sigs.k8s.io/kueue/pkg/queue"
utilac "sigs.k8s.io/kueue/pkg/util/admissioncheck"
Expand Down Expand Up @@ -258,6 +259,10 @@ func (r *WorkloadReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
r.recorder.Eventf(&wl, corev1.EventTypeNormal, "Admitted", "Admitted by ClusterQueue %v, wait time since reservation was %.0fs", wl.Status.Admission.ClusterQueue, quotaReservedWaitTime.Seconds())
metrics.AdmittedWorkload(kueue.ClusterQueueReference(cqName), queuedWaitTime)
metrics.AdmissionChecksWaitTime(kueue.ClusterQueueReference(cqName), quotaReservedWaitTime)
if features.Enabled(features.LocalQueueMetrics) {
metrics.LocalQueueAdmittedWorkload(metrics.LQRefFromWorkload(&wl), queuedWaitTime)
metrics.LocalQueueAdmissionChecksWaitTime(metrics.LQRefFromWorkload(&wl), quotaReservedWaitTime)
}
}
return ctrl.Result{}, nil
}
Expand Down Expand Up @@ -428,6 +433,9 @@ func (r *WorkloadReconciler) reconcileOnLocalQueueActiveState(ctx context.Contex
cqName := string(lq.Spec.ClusterQueue)
if slices.Contains(r.queues.GetClusterQueueNames(), cqName) {
metrics.ReportEvictedWorkloads(cqName, kueue.WorkloadEvictedByLocalQueueStopped)
if features.Enabled(features.LocalQueueMetrics) {
metrics.ReportLocalQueueEvictedWorkloads(metrics.LQRefFromWorkload(wl), kueue.WorkloadEvictedByLocalQueueStopped)
}
}
}
return true, client.IgnoreNotFound(err)
Expand Down
7 changes: 7 additions & 0 deletions pkg/features/kube_features.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ const (
//
// Workloads keeps allocated quota and preserves QuotaReserved=True when ProvisioningRequest fails
KeepQuotaForProvReqRetry featuregate.Feature = "KeepQuotaForProvReqRetry"

// owner: @kpostoffice
// alpha: v0.10
//
// Enabled gathering of LocalQueue metrics
LocalQueueMetrics featuregate.Feature = "LocalQueueMetrics"
)

func init() {
Expand Down Expand Up @@ -180,6 +186,7 @@ var defaultFeatureGates = map[featuregate.Feature]featuregate.FeatureSpec{
ExposeFlavorsInLocalQueue: {Default: true, PreRelease: featuregate.Beta},
AdmissionCheckValidationRules: {Default: false, PreRelease: featuregate.Deprecated},
KeepQuotaForProvReqRetry: {Default: false, PreRelease: featuregate.Deprecated},
LocalQueueMetrics: {Default: false, PreRelease: featuregate.Alpha},
}

func SetFeatureGateDuringTest(tb testing.TB, f featuregate.Feature, value bool) {
Expand Down
Loading