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

[YUNIKORN-2876] Initialize queue metrics for app after queue is set #968

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 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
11 changes: 6 additions & 5 deletions pkg/scheduler/objects/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ var (
terminatedTimeout = 3 * 24 * time.Hour
defaultPlaceholderTimeout = 15 * time.Minute
)

var initAppLogOnce sync.Once
var rateLimitedAppLog *log.RateLimitedLogger

Expand Down Expand Up @@ -191,13 +190,15 @@ func NewApplication(siApp *si.AddApplicationRequest, ugi security.UserGroup, eve
app.rmID = rmID
app.appEvents = schedEvt.NewApplicationEvents(events.GetEventSystem())
app.appEvents.SendNewApplicationEvent(app.ApplicationID)
app.setNewMetrics()
return app
}

func (sa *Application) setNewMetrics() {
metrics.GetSchedulerMetrics().IncTotalApplicationsNew()
metrics.GetQueueMetrics(sa.GetQueuePath()).IncQueueApplicationsNew()
func (sa *Application) SetNewMetrics() {
// ensure the queue is set before calling this function
if sa.GetQueuePath() != "" {
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't add a conditional, just execute setNewMetrics() inside SetQueue(). Callers should not need to be updated at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @craigcondit , good suggestion, addressed in latest PR.

metrics.GetQueueMetrics(sa.GetQueuePath()).IncQueueApplicationsNew()
metrics.GetSchedulerMetrics().IncTotalApplicationsNew()
}
}

func (sa *Application) String() string {
Expand Down
12 changes: 12 additions & 0 deletions pkg/scheduler/objects/application_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,11 @@ func TestAppStateTransitionEvents(t *testing.T) {
func TestAppStateTransitionMetrics(t *testing.T) { //nolint:funlen
queue := createQueue(t, "metrics")
metrics.GetSchedulerMetrics().Reset()
metrics.GetQueueMetrics("root.metrics").Reset()
// app-00001: New -> Resuming -> Accepted --> Running -> Completing-> Completed
app := newApplication("app-00001", "default", "root.metrics")
app.SetQueue(queue)
app.SetNewMetrics()
assertState(t, app, nil, New.String())
assertTotalAppsNewMetrics(t, 1)
assertTotalAppsRunningMetrics(t, 0)
Expand Down Expand Up @@ -397,6 +399,7 @@ func TestAppStateTransitionMetrics(t *testing.T) { //nolint:funlen
// app-00002: New -> Accepted -> Completing -> Running -> Failing-> Failed
app = newApplication("app-00002", "default", "root.metrics")
app.SetQueue(queue)
app.SetNewMetrics()
assertState(t, app, nil, New.String())
// New -> Accepted
err = app.HandleApplicationEvent(RunApplication)
Expand Down Expand Up @@ -431,6 +434,7 @@ func TestAppStateTransitionMetrics(t *testing.T) { //nolint:funlen
// app-00003: New -> Accepted -> Running -> Failing -> Failed
app = newApplication("app-00003", "default", "root.metrics")
app.SetQueue(queue)
app.SetNewMetrics()
assertState(t, app, nil, New.String())
// New -> Accepted
err = app.HandleApplicationEvent(RunApplication)
Expand Down Expand Up @@ -459,6 +463,7 @@ func TestAppStateTransitionMetrics(t *testing.T) { //nolint:funlen
// app-00004: New -> Rejected
app = newApplication("app-00004", "default", "root.metrics")
app.SetQueue(queue)
app.SetNewMetrics()
assertState(t, app, nil, New.String())
// New -> Rejected
err = app.HandleApplicationEvent(RejectApplication)
Expand All @@ -475,6 +480,13 @@ func TestAppStateTransitionMetrics(t *testing.T) { //nolint:funlen
assertQueueApplicationsRejectedMetrics(t, app, 1)
assertQueueApplicationsFailedMetrics(t, app, 2)
assertQueueApplicationsCompletedMetrics(t, app, 1)

// app-00005: the queuePath is empty, it will happen for dynamic queue when it before the queue is created
app = newApplication("app-00005", "default", "")
app.SetNewMetrics()
assertState(t, app, nil, New.String())
assertQueueApplicationsNewMetrics(t, app, 0)
assertTotalAppsNewMetrics(t, 4)
}

func assertState(t testing.TB, app *Application, err error, expected string) {
Expand Down
3 changes: 3 additions & 0 deletions pkg/scheduler/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,9 @@ func (pc *PartitionContext) AddApplication(app *objects.Application) error {
queue.AddApplication(app)
pc.applications[appID] = app

// here we can make sure the queue is not empty
app.SetNewMetrics()

return nil
}

Expand Down
42 changes: 42 additions & 0 deletions pkg/scheduler/partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/apache/yunikorn-core/pkg/common/resources"
"github.com/apache/yunikorn-core/pkg/common/security"
"github.com/apache/yunikorn-core/pkg/events"
"github.com/apache/yunikorn-core/pkg/metrics"
"github.com/apache/yunikorn-core/pkg/mock"
"github.com/apache/yunikorn-core/pkg/plugins"
"github.com/apache/yunikorn-core/pkg/rmproxy/rmevent"
Expand Down Expand Up @@ -246,6 +247,9 @@ func TestRemoveNodeWithAllocations(t *testing.T) {
partition, err := newBasePartition()
assert.NilError(t, err, "partition create failed")

defer metrics.GetSchedulerMetrics().Reset()
defer metrics.GetQueueMetrics(defQueue).Reset()

// add a new app
app := newApplication(appID1, "default", defQueue)
err = partition.AddApplication(app)
Expand Down Expand Up @@ -293,6 +297,9 @@ func TestRemoveNodeWithPlaceholders(t *testing.T) {
partition, err := newBasePartition()
assert.NilError(t, err, "partition create failed")

defer metrics.GetSchedulerMetrics().Reset()
defer metrics.GetQueueMetrics(defQueue).Reset()

// add a new app
app := newApplication(appID1, "default", defQueue)
err = partition.AddApplication(app)
Expand Down Expand Up @@ -395,6 +402,9 @@ func TestPlaceholderDataWithPlaceholderPreemption(t *testing.T) {
partition, err := newBasePartition()
assert.NilError(t, err, "partition create failed")

defer metrics.GetSchedulerMetrics().Reset()
defer metrics.GetQueueMetrics(defQueue).Reset()

// add a new app1
app1, _ := newApplicationWithHandler(appID1, "default", defQueue)
err = partition.AddApplication(app1)
Expand Down Expand Up @@ -522,6 +532,9 @@ func TestPlaceholderDataWithNodeRemoval(t *testing.T) {
partition, err := newBasePartition()
assert.NilError(t, err, "partition create failed")

defer metrics.GetSchedulerMetrics().Reset()
defer metrics.GetQueueMetrics(defQueue).Reset()

// add a new app1
app1, _ := newApplicationWithHandler(appID1, "default", defQueue)
err = partition.AddApplication(app1)
Expand Down Expand Up @@ -605,6 +618,9 @@ func TestPlaceholderDataWithRemoval(t *testing.T) {
partition, err := newBasePartition()
assert.NilError(t, err, "partition create failed")

defer metrics.GetSchedulerMetrics().Reset()
defer metrics.GetQueueMetrics(defQueue).Reset()

// add a new app1
app1, _ := newApplicationWithHandler(appID1, "default", defQueue)
err = partition.AddApplication(app1)
Expand Down Expand Up @@ -698,6 +714,8 @@ func TestRemoveNodeWithReplacement(t *testing.T) {
partition, err := newBasePartition()
assert.NilError(t, err, "partition create failed")

defer metrics.GetSchedulerMetrics().Reset()
defer metrics.GetQueueMetrics(defQueue).Reset()
// add a new app
app := newApplication(appID1, "default", defQueue)
err = partition.AddApplication(app)
Expand Down Expand Up @@ -770,6 +788,9 @@ func TestRemoveNodeWithReal(t *testing.T) {
partition, err := newBasePartition()
assert.NilError(t, err, "partition create failed")

defer metrics.GetSchedulerMetrics().Reset()
defer metrics.GetQueueMetrics(defQueue).Reset()

// add a new app
app := newApplication(appID1, "default", defQueue)
err = partition.AddApplication(app)
Expand Down Expand Up @@ -831,13 +852,22 @@ func TestRemoveNodeWithReal(t *testing.T) {
}

func TestAddApp(t *testing.T) {
defer metrics.GetSchedulerMetrics().Reset()
defer metrics.GetQueueMetrics(defQueue).Reset()
partition, err := newBasePartition()
assert.NilError(t, err, "partition create failed")

// add a new app
app := newApplication(appID1, "default", defQueue)
err = partition.AddApplication(app)
assert.NilError(t, err, "add application to partition should not have failed")
queueApplicationsNew, err := metrics.GetQueueMetrics(defQueue).GetQueueApplicationsNew()
assert.NilError(t, err, "get queue metrics failed")
assert.Equal(t, queueApplicationsNew, 1)
scheduleApplicationsNew, err := metrics.GetSchedulerMetrics().GetTotalApplicationsNew()
assert.NilError(t, err, "get scheduler metrics failed")
assert.Equal(t, scheduleApplicationsNew, 1)

// add the same app
err = partition.AddApplication(app)
if err == nil {
Expand All @@ -853,6 +883,12 @@ func TestAddApp(t *testing.T) {
if err == nil || partition.getApplication(appID2) != nil {
t.Errorf("add application on stopped partition should have failed but did not")
}
queueApplicationsNew, err = metrics.GetQueueMetrics(defQueue).GetQueueApplicationsNew()
assert.NilError(t, err, "get queue metrics failed")
assert.Equal(t, queueApplicationsNew, 1)
scheduleApplicationsNew, err = metrics.GetSchedulerMetrics().GetTotalApplicationsNew()
assert.NilError(t, err, "get scheduler metrics failed")
assert.Equal(t, scheduleApplicationsNew, 1)

// mark partition for deletion, no new application can be added
partition.stateMachine.SetState(objects.Active.String())
Expand All @@ -863,6 +899,12 @@ func TestAddApp(t *testing.T) {
if err == nil || partition.getApplication(appID3) != nil {
t.Errorf("add application on draining partition should have failed but did not")
}
queueApplicationsNew, err = metrics.GetQueueMetrics(defQueue).GetQueueApplicationsNew()
assert.NilError(t, err, "get queue metrics failed")
assert.Equal(t, queueApplicationsNew, 1)
scheduleApplicationsNew, err = metrics.GetSchedulerMetrics().GetTotalApplicationsNew()
assert.NilError(t, err, "get scheduler metrics failed")
assert.Equal(t, scheduleApplicationsNew, 1)
}

func TestAddAppForced(t *testing.T) {
Expand Down
Loading