cluster_shuffle.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2018 The etcd Authors
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package tester
  15. import (
  16. "math/rand"
  17. "time"
  18. "go.uber.org/zap"
  19. )
  20. func (clus *Cluster) shuffleCases() {
  21. rand.Seed(time.Now().UnixNano())
  22. offset := rand.Intn(1000)
  23. n := len(clus.cases)
  24. cp := coprime(n)
  25. css := make([]Case, n)
  26. for i := 0; i < n; i++ {
  27. css[i] = clus.cases[(cp*i+offset)%n]
  28. }
  29. clus.cases = css
  30. clus.lg.Info("shuffled test failure cases", zap.Int("total", n))
  31. }
  32. /*
  33. x and y of GCD 1 are coprime to each other
  34. x1 = ( coprime of n * idx1 + offset ) % n
  35. x2 = ( coprime of n * idx2 + offset ) % n
  36. (x2 - x1) = coprime of n * (idx2 - idx1) % n
  37. = (idx2 - idx1) = 1
  38. Consecutive x's are guaranteed to be distinct
  39. */
  40. func coprime(n int) int {
  41. coprime := 1
  42. for i := n / 2; i < n; i++ {
  43. if gcd(i, n) == 1 {
  44. coprime = i
  45. break
  46. }
  47. }
  48. return coprime
  49. }
  50. func gcd(x, y int) int {
  51. if y == 0 {
  52. return x
  53. }
  54. return gcd(y, x%y)
  55. }