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

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