Skip to content

Commit e6cce81

Browse files
committed
Make consecutive random pickings for zones
1 parent c61be7e commit e6cce81

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

pkg/gce-cloud-provider/compute/fake-gce.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func FakeCreateCloudProvider(project, zone string) (*FakeCloudProvider, error) {
4949
}
5050

5151
func (cloud *FakeCloudProvider) ListZones(ctx context.Context, region string) ([]string, error) {
52-
return []string{cloud.zone, "fake-second-zone"}, nil
52+
return []string{cloud.zone, "country-region-fakesecondzone"}, nil
5353
}
5454

5555
// Disk Methods

pkg/gce-pd-csi-driver/controller.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package gceGCEDriver
1717
import (
1818
"fmt"
1919
"math/rand"
20+
"sort"
2021
"strings"
2122

2223
csi "github.com/container-storage-interface/spec/lib/go/csi/v0"
@@ -532,7 +533,7 @@ func pickZonesFromTopology(top *csi.TopologyRequirement, numZones int) ([]string
532533
}
533534
// Add the remaining number of zones into the set
534535
// TODO: Maybe we should pick a random set to get
535-
nSlice, err := pickRandNFromSlice(remainingZones.List(), remainingNumZones)
536+
nSlice, err := pickRandAndConsecutive(remainingZones.List(), remainingNumZones)
536537
if err != nil {
537538
return nil, err
538539
}
@@ -684,14 +685,16 @@ func createSingleZoneDisk(ctx context.Context, cloudProvider gce.GCECompute, nam
684685
return disk, nil
685686
}
686687

687-
func pickRandNFromSlice(slice []string, n int) ([]string, error) {
688+
func pickRandAndConsecutive(slice []string, n int) ([]string, error) {
688689
if n > len(slice) {
689690
return nil, fmt.Errorf("n: %v is greater than length of provided slice: %v", n, slice)
690691
}
691-
permed := rand.Perm(len(slice))
692+
sort.Strings(slice)
693+
start := rand.Intn(len(slice))
692694
ret := []string{}
693695
for i := 0; i < n; i++ {
694-
ret = append(ret, slice[permed[i]])
696+
idx := (start + i) % len(slice)
697+
ret = append(ret, slice[idx])
695698
}
696699
return ret, nil
697700
}

pkg/gce-pd-csi-driver/controller_test.go

+25-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ package gceGCEDriver
1616

1717
import (
1818
"fmt"
19+
"math/rand"
1920
"reflect"
21+
"sort"
2022
"testing"
23+
"time"
2124

2225
"golang.org/x/net/context"
2326
compute "google.golang.org/api/compute/v1"
@@ -333,7 +336,7 @@ func TestCreateVolumeArguments(t *testing.T) {
333336
Segments: map[string]string{common.TopologyKeyZone: metadataservice.FakeZone},
334337
},
335338
{
336-
Segments: map[string]string{common.TopologyKeyZone: "fake-second-zone"},
339+
Segments: map[string]string{common.TopologyKeyZone: "country-region-fakesecondzone"},
337340
},
338341
},
339342
},
@@ -945,7 +948,8 @@ func TestPickZonesFromTopology(t *testing.T) {
945948
}
946949
}
947950

948-
func TestPickRandNFromSlice(t *testing.T) {
951+
func TestPickRandAndConsecutive(t *testing.T) {
952+
rand.Seed(time.Now().UnixNano())
949953
testCases := []struct {
950954
name string
951955
slice []string
@@ -977,8 +981,9 @@ func TestPickRandNFromSlice(t *testing.T) {
977981
for _, tc := range testCases {
978982
t.Logf("test case: %s", tc.name)
979983
tot := sets.String{}
984+
sort.Strings(tc.slice)
980985
for i := 0; i < 25; i++ {
981-
theslice, err := pickRandNFromSlice(tc.slice, tc.n)
986+
theslice, err := pickRandAndConsecutive(tc.slice, tc.n)
982987
if err != nil && !tc.expErr {
983988
t.Errorf("Did not expect error but got: %v", err)
984989
}
@@ -991,6 +996,23 @@ func TestPickRandNFromSlice(t *testing.T) {
991996
if len(theslice) != tc.n {
992997
t.Errorf("expected the resulting slice to be length %v, but got %v instead", tc.n, theslice)
993998
}
999+
// Find where it is in the slice
1000+
var idx = -1
1001+
for j, elem := range tc.slice {
1002+
if elem == theslice[0] {
1003+
idx = j
1004+
break
1005+
}
1006+
}
1007+
if idx == -1 {
1008+
t.Errorf("could not find %v in the original slice %v", theslice[0], tc.slice)
1009+
}
1010+
for j := 0; j < tc.n; j++ {
1011+
if theslice[j] != tc.slice[(idx+j)%len(tc.slice)] {
1012+
t.Errorf("did not pick sorted consecutive values from the slice")
1013+
}
1014+
}
1015+
9941016
tot.Insert(theslice...)
9951017
}
9961018
if !tot.Equal(sets.NewString(tc.slice...)) {

0 commit comments

Comments
 (0)