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.

weightDistributing.go 1.1 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package weightDistributing
  2. import (
  3. "math"
  4. )
  5. type Weight struct {
  6. Id int64
  7. Weight int32
  8. Name string
  9. Replica int32
  10. }
  11. func DistributeReplicas(weights []*Weight, replicas int32) {
  12. var weightSum int32
  13. weightSum = 0
  14. for _, w := range weights {
  15. weightSum += w.Weight
  16. }
  17. weightRatio := make([]float64, len(weights))
  18. for i, w := range weights {
  19. weightRatio[i] = float64(w.Weight) / float64(weightSum)
  20. }
  21. var rest = replicas
  22. for i := 0; i < len(weights); i++ {
  23. var n = math.Round(float64(replicas) * weightRatio[i])
  24. rest -= int32(n)
  25. weights[i].Replica = int32(n)
  26. }
  27. for {
  28. if rest == 0 {
  29. break
  30. }
  31. maxIdx := 0
  32. minIdx := 0
  33. if rest > 0 {
  34. for i, ratio := range weightRatio {
  35. if ratio > weightRatio[maxIdx] {
  36. maxIdx = i
  37. }
  38. }
  39. } else {
  40. for i, ratio := range weightRatio {
  41. if ratio < weightRatio[minIdx] {
  42. minIdx = i
  43. }
  44. }
  45. }
  46. if rest > 0 {
  47. weights[maxIdx].Replica++
  48. weightRatio[maxIdx]--
  49. rest--
  50. } else {
  51. weights[minIdx].Replica--
  52. weightRatio[minIdx]++
  53. rest++
  54. }
  55. }
  56. }

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.