Use new algorithm to get fixed random order of disks (#2147)

This commit is contained in:
frankw
2016-07-08 16:33:21 +02:00
committed by Harshavardhana
parent 5ff1203fc0
commit 63b3f1dcfd
4 changed files with 47 additions and 16 deletions
+13 -13
View File
@@ -18,9 +18,9 @@ package main
import (
"encoding/json"
"math/rand"
"errors"
"hash/crc32"
"path"
"time"
)
// Validates if we have quorum based on the errors with errDiskNotFound.
@@ -48,19 +48,19 @@ func diskCount(disks []StorageAPI) int {
return diskCount
}
// randInts - uses Knuth Fisher-Yates shuffle algorithm for generating uniform shuffling.
func randInts(count int) []int {
rand.Seed(time.Now().UTC().UnixNano()) // Seed with current time.
ints := make([]int, count)
for i := 0; i < count; i++ {
ints[i] = i + 1
// hashOrder - returns consistent hashed integers of count slice, based on the input token.
func hashOrder(token string, count int) []int {
if count < 0 {
panic(errors.New("hashOrder count cannot be negative"))
}
for i := 0; i < count; i++ {
// Choose index uniformly in [i, count-1]
r := i + rand.Intn(count-i)
ints[r], ints[i] = ints[i], ints[r]
nums := make([]int, count)
tokenCrc := crc32.Checksum([]byte(token), crc32.IEEETable)
start := int(uint32(tokenCrc)%uint32(count)) | 1
for i := 1; i <= count; i++ {
nums[i-1] = 1 + ((start + i) % count)
}
return ints
return nums
}
// readXLMeta reads `xl.json` and returns back XL metadata structure.