Skip to content

Commit

Permalink
Avoid unnecessary update when tfjob is complete (#1051)
Browse files Browse the repository at this point in the history
* no need to update the tfjob if the status hasn't changed since last time

* no need to update the tfjob if the status hasn't changed since last time

* no need to update the tfjob if the status hasn't changed since last time

* using apiequality.Semantic

* Avoid unnecessary update when tfjob is complete

* Avoid unnecessary update when tfjob is complete
  • Loading branch information
cheyang authored and k8s-ci-robot committed Aug 6, 2019
1 parent 1ad93aa commit be4ad79
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
11 changes: 8 additions & 3 deletions pkg/controller.v1/tensorflow/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package tensorflow

import (
"fmt"
"reflect"
"strings"
"time"

Expand Down Expand Up @@ -45,6 +44,7 @@ import (
"github.com/kubeflow/tf-operator/pkg/util/k8sutil"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/runtime/schema"
)

Expand Down Expand Up @@ -432,7 +432,12 @@ func (tc *TFController) reconcileTFJobs(tfjob *tfv1.TFJob) error {
tfjob.Status.ReplicaStatuses[rtype].Active = 0
}
}
return tc.updateStatusHandler(tfjob)
// no need to update the tfjob if the status hasn't changed since last time even the tfjob is not running.

if !apiequality.Semantic.DeepEqual(*oldStatus, tfjob.Status) {
return tc.updateStatusHandler(tfjob)
}
return nil
}

if tc.Config.EnableGangScheduling {
Expand Down Expand Up @@ -463,7 +468,7 @@ func (tc *TFController) reconcileTFJobs(tfjob *tfv1.TFJob) error {
}

// no need to update the tfjob if the status hasn't changed since last time.
if !reflect.DeepEqual(*oldStatus, tfjob.Status) {
if !apiequality.Semantic.DeepEqual(*oldStatus, tfjob.Status) {
return tc.updateStatusHandler(tfjob)
}
return nil
Expand Down
5 changes: 5 additions & 0 deletions pkg/controller.v1/tensorflow/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ func (tc *TFController) updateStatusSingle(tfjob *tfv1.TFJob, rtype tfv1.TFRepli

// updateTFJobStatus updates the status of the given TFJob.
func (tc *TFController) updateTFJobStatus(tfjob *tfv1.TFJob) error {
startTime := time.Now()
defer func() {
tflogger.LoggerForJob(tfjob).Infof("Finished updating TFJobs Status %q (%v)",
tfjob.Name, time.Since(startTime))
}()
_, err := tc.tfJobClientSet.KubeflowV1().TFJobs(tfjob.Namespace).UpdateStatus(tfjob)
return err
}
Expand Down
9 changes: 6 additions & 3 deletions pkg/controller.v1beta2/tensorflow/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package tensorflow

import (
"fmt"
"reflect"
"strings"
"time"

Expand All @@ -43,6 +42,7 @@ import (
"github.com/kubeflow/tf-operator/pkg/common/jobcontroller"
tflogger "github.com/kubeflow/tf-operator/pkg/logger"
"github.com/kubeflow/tf-operator/pkg/util/k8sutil"
apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/runtime/schema"
)

Expand Down Expand Up @@ -423,7 +423,10 @@ func (tc *TFController) reconcileTFJobs(tfjob *tfv1beta2.TFJob) error {
tfjob.Status.ReplicaStatuses[rtype].Active = 0
}
}
return tc.updateStatusHandler(tfjob)
// no need to update the tfjob if the status hasn't changed since last time even the tfjob is not running.
if !apiequality.Semantic.DeepEqual(*oldStatus, tfjob.Status) {
return tc.updateStatusHandler(tfjob)
}
}

if tc.Config.EnableGangScheduling {
Expand Down Expand Up @@ -454,7 +457,7 @@ func (tc *TFController) reconcileTFJobs(tfjob *tfv1beta2.TFJob) error {
}

// no need to update the tfjob if the status hasn't changed since last time.
if !reflect.DeepEqual(*oldStatus, tfjob.Status) {
if !apiequality.Semantic.DeepEqual(*oldStatus, tfjob.Status) {
return tc.updateStatusHandler(tfjob)
}
return nil
Expand Down
5 changes: 5 additions & 0 deletions pkg/controller.v1beta2/tensorflow/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ func (tc *TFController) updateStatusSingle(tfjob *tfv1beta2.TFJob, rtype tfv1bet

// updateTFJobStatus updates the status of the given TFJob.
func (tc *TFController) updateTFJobStatus(tfjob *tfv1beta2.TFJob) error {
startTime := time.Now()
defer func() {
tflogger.LoggerForJob(tfjob).Infof("Finished updating TFJobs Status %q (%v)",
tfjob.Name, time.Since(startTime))
}()
_, err := tc.tfJobClientSet.KubeflowV1beta2().TFJobs(tfjob.Namespace).UpdateStatus(tfjob)
return err
}
Expand Down

0 comments on commit be4ad79

Please sign in to comment.