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

View File

@ -16,7 +16,10 @@
package main package main
import "path" import (
"path"
"time"
)
// getLoadBalancedQuorumDisks - fetches load balanced sufficiently // getLoadBalancedQuorumDisks - fetches load balanced sufficiently
// randomized quorum disk slice. // randomized quorum disk slice.
@ -29,7 +32,7 @@ func (xl xlObjects) getLoadBalancedQuorumDisks() (disks []StorageAPI) {
// randomized) disk slice. // randomized) disk slice.
func (xl xlObjects) getLoadBalancedDisks() (disks []StorageAPI) { func (xl xlObjects) getLoadBalancedDisks() (disks []StorageAPI) {
// Based on the random shuffling return back randomized disks. // Based on the random shuffling return back randomized disks.
for _, i := range randInts(len(xl.storageDisks)) { for _, i := range hashOrder(time.Now().UTC().String(), len(xl.storageDisks)) {
disks = append(disks, xl.storageDisks[i-1]) disks = append(disks, xl.storageDisks[i-1])
} }
return disks return disks

View File

@ -119,7 +119,7 @@ func newXLMetaV1(dataBlocks, parityBlocks int) (xlMeta xlMetaV1) {
DataBlocks: dataBlocks, DataBlocks: dataBlocks,
ParityBlocks: parityBlocks, ParityBlocks: parityBlocks,
BlockSize: blockSizeV1, BlockSize: blockSizeV1,
Distribution: randInts(dataBlocks + parityBlocks), Distribution: hashOrder(time.Now().UTC().String(), dataBlocks+parityBlocks),
} }
return xlMeta return xlMeta
} }

View File

@ -18,9 +18,9 @@ package main
import ( import (
"encoding/json" "encoding/json"
"math/rand" "errors"
"hash/crc32"
"path" "path"
"time"
) )
// Validates if we have quorum based on the errors with errDiskNotFound. // Validates if we have quorum based on the errors with errDiskNotFound.
@ -48,19 +48,19 @@ func diskCount(disks []StorageAPI) int {
return diskCount return diskCount
} }
// randInts - uses Knuth Fisher-Yates shuffle algorithm for generating uniform shuffling. // hashOrder - returns consistent hashed integers of count slice, based on the input token.
func randInts(count int) []int { func hashOrder(token string, count int) []int {
rand.Seed(time.Now().UTC().UnixNano()) // Seed with current time. if count < 0 {
ints := make([]int, count) panic(errors.New("hashOrder count cannot be negative"))
for i := 0; i < count; i++ {
ints[i] = i + 1
} }
for i := 0; i < count; i++ { nums := make([]int, count)
// Choose index uniformly in [i, count-1] tokenCrc := crc32.Checksum([]byte(token), crc32.IEEETable)
r := i + rand.Intn(count-i)
ints[r], ints[i] = ints[i], ints[r] 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. // readXLMeta reads `xl.json` and returns back XL metadata structure.

View File

@ -19,6 +19,7 @@ package main
import ( import (
"os" "os"
"path/filepath" "path/filepath"
"reflect"
"testing" "testing"
) )
@ -136,3 +137,30 @@ func TestNewXL(t *testing.T) {
t.Fatalf("Unable to initialize erasure, %s", err) t.Fatalf("Unable to initialize erasure, %s", err)
} }
} }
// TestHashOrder - test order of ints in array
func TestHashOrder(t *testing.T) {
testCases := []struct {
objectName string
hashedOrder []int
}{
// cases which should pass the test.
// passing in valid object name.
{"object", []int{15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}},
{"The Shining Script <v1>.pdf", []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}},
{"Cost Benefit Analysis (2009-2010).pptx", []int{15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}},
{"117Gn8rfHL2ACARPAhaFd0AGzic9pUbIA/5OCn5A", []int{3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2}},
{"SHØRT", []int{11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}},
{"There are far too many object names, and far too few bucket names!", []int{15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}},
{"a/b/c/", []int{3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2}},
{"/a/b/c", []int{7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6}},
{string([]byte{0xff, 0xfe, 0xfd}), []int{15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}},
}
for i, testCase := range testCases {
hashedOrder := hashOrder(testCase.objectName, 16)
if !reflect.DeepEqual(testCase.hashedOrder, hashedOrder) {
t.Errorf("Test case %d: Expected \"%#v\" but failed \"%#v\"", i+1, testCase.hashedOrder, hashedOrder)
}
}
}