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.

staticWeight.go 1.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package strategy
  2. import (
  3. "errors"
  4. "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/scheduler/entity"
  5. )
  6. type StaticWeightStrategy struct {
  7. // TODO: add fields
  8. //每个
  9. num int32
  10. weights []entity.WeightP
  11. }
  12. func (ps *StaticWeightStrategy) Schedule() ([]*AssignedCluster, error) {
  13. // TODO: implement the scheduling logic return nil, nil
  14. if ps.num < 1 {
  15. return nil, errors.New("numbers must be greater than 0")
  16. }
  17. if ps.weights == nil {
  18. return nil, errors.New("weight must be set")
  19. }
  20. var weightSum int32
  21. weightSum = 0
  22. for _, w := range ps.weights {
  23. weightSum += w.Weight
  24. }
  25. weightRatio := make([]float64, len(ps.weights))
  26. for i, w := range ps.weights {
  27. weightRatio[i] = float64(w.Weight) / float64(weightSum)
  28. }
  29. var rest = ps.num
  30. var results []*AssignedCluster
  31. for i := 0; i < len(ps.weights); i++ {
  32. var n = int(float64(ps.num) * weightRatio[i])
  33. rest -= int32(n)
  34. cluster := &AssignedCluster{ParticipantId: ps.weights[i].Participant_id, Name: ps.weights[i].Name, Replicas: int32(n)}
  35. results = append(results, cluster)
  36. }
  37. if rest != 0 {
  38. if rest < 0 { // 如果差值小于0,需要增加某些元素的值
  39. for i := len(ps.weights) - 1; rest < 0 && i >= 0; i-- {
  40. if results[i].Replicas < ps.weights[i].Weight {
  41. results[i].Replicas++
  42. rest++
  43. }
  44. }
  45. } else {
  46. for i := len(ps.weights) - 1; rest > 0 && i >= 0; i-- {
  47. if results[i].Replicas < ps.weights[i].Weight {
  48. results[i].Replicas--
  49. rest--
  50. }
  51. }
  52. }
  53. }
  54. return results, nil
  55. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.