You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

upstream.go 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. Copyright 2021 The KubeEdge Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package federatedlearning
  14. import (
  15. "context"
  16. "encoding/json"
  17. "fmt"
  18. sednav1 "github.com/kubeedge/sedna/pkg/apis/sedna/v1alpha1"
  19. "github.com/kubeedge/sedna/pkg/globalmanager/runtime"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. )
  22. func (c *Controller) updateModelMetrics(jobName, namespace string, metrics []sednav1.Metric) error {
  23. var err error
  24. job, err := c.client.FederatedLearningJobs(namespace).Get(context.TODO(), jobName, metav1.GetOptions{})
  25. if err != nil {
  26. // federated crd not found
  27. return err
  28. }
  29. modelName := job.Spec.AggregationWorker.Model.Name
  30. client := c.client.Models(namespace)
  31. return runtime.RetryUpdateStatus(modelName, namespace, func() error {
  32. model, err := client.Get(context.TODO(), modelName, metav1.GetOptions{})
  33. if err != nil {
  34. return err
  35. }
  36. now := metav1.Now()
  37. model.Status.UpdateTime = &now
  38. model.Status.Metrics = metrics
  39. _, err = client.UpdateStatus(context.TODO(), model, metav1.UpdateOptions{})
  40. return err
  41. })
  42. }
  43. func (c *Controller) appendStatusCondition(name, namespace string, cond sednav1.FLJobCondition) error {
  44. client := c.client.FederatedLearningJobs(namespace)
  45. return runtime.RetryUpdateStatus(name, namespace, (func() error {
  46. job, err := client.Get(context.TODO(), name, metav1.GetOptions{})
  47. if err != nil {
  48. return err
  49. }
  50. job.Status.Conditions = append(job.Status.Conditions, cond)
  51. _, err = client.UpdateStatus(context.TODO(), job, metav1.UpdateOptions{})
  52. return err
  53. }))
  54. }
  55. // updateFromEdge updates the federated job's status
  56. func (c *Controller) updateFromEdge(name, namespace, operation string, content []byte) (err error) {
  57. // JobInfo defines the job information
  58. type JobInfo struct {
  59. // Current training round
  60. CurrentRound int `json:"currentRound"`
  61. UpdateTime string `json:"updateTime"`
  62. }
  63. // Output defines job output information
  64. type Output struct {
  65. Models []runtime.Model `json:"models"`
  66. JobInfo *JobInfo `json:"ownerInfo"`
  67. }
  68. var status struct {
  69. Phase string `json:"phase"`
  70. Status string `json:"status"`
  71. Output *Output `json:"output"`
  72. }
  73. err = json.Unmarshal(content, &status)
  74. if err != nil {
  75. return
  76. }
  77. output := status.Output
  78. if output != nil {
  79. // Update the model's metrics
  80. if len(output.Models) > 0 {
  81. // only one model
  82. model := output.Models[0]
  83. metrics := runtime.ConvertMapToMetrics(model.Metrics)
  84. if len(metrics) > 0 {
  85. c.updateModelMetrics(name, namespace, metrics)
  86. }
  87. }
  88. jobInfo := output.JobInfo
  89. // update job info if having any info
  90. if jobInfo != nil && jobInfo.CurrentRound > 0 {
  91. // Find a good place to save the progress info
  92. // TODO: more meaningful reason/message
  93. reason := "DoTraining"
  94. message := fmt.Sprintf("Round %v reaches at %s", jobInfo.CurrentRound, jobInfo.UpdateTime)
  95. cond := NewJobCondition(sednav1.FLJobCondTraining, reason, message)
  96. c.appendStatusCondition(name, namespace, cond)
  97. }
  98. }
  99. return nil
  100. }
  101. func (c *Controller) SetUpstreamHandler(addFunc runtime.UpstreamHandlerAddFunc) error {
  102. return addFunc(KindName, c.updateFromEdge)
  103. }