-
Notifications
You must be signed in to change notification settings - Fork 35
/
reshuffle_test.go
318 lines (290 loc) · 8.18 KB
/
reshuffle_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright 2019 GRAIL, Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package bigslice_test
import (
"context"
"fmt"
"math/rand"
"reflect"
"sort"
"strings"
"testing"
"github.com/grailbio/bigslice"
"github.com/grailbio/bigslice/exec"
"github.com/grailbio/bigslice/frame"
"github.com/grailbio/bigslice/sliceio"
"github.com/grailbio/bigslice/slicetest"
)
func reshuffleTest(t *testing.T, transform func(bigslice.Slice) bigslice.Slice) {
t.Helper()
const N = 100
col1 := make([]lengthHashKey, N)
col2 := make([]int, N)
want := map[lengthHashKey][]int{} // map of val1 -> []val2
rnd := rand.New(rand.NewSource(0))
for i := range col1 {
s := strings.Repeat("a", rnd.Intn(N/10)) // We want many strings of each length.
col1[i] = lengthHashKey(s)
col2[i] = rnd.Intn(100)
want[col1[i]] = append(want[col1[i]], col2[i])
}
for _, vals := range want {
sort.Ints(vals) // for equality test later
}
for m := 1; m < 10; m++ {
t.Run(fmt.Sprint(m), func(t *testing.T) {
slice := bigslice.Const(m, append([]lengthHashKey{}, col1...), append([]int{}, col2...))
slice = transform(slice)
// map of col1 length -> set of shards that had keys of that length.
lengthShards := map[int]map[int]struct{}{}
got := map[lengthHashKey][]int{} // map of val1 -> []val2
var (
val1 lengthHashKey
val2 int
)
slice = bigslice.Scan(slice, func(shard int, scanner *sliceio.Scanner) error {
for scanner.Scan(context.Background(), &val1, &val2) {
if _, ok := lengthShards[len(val1)]; !ok {
lengthShards[len(val1)] = map[int]struct{}{}
}
lengthShards[len(val1)][shard] = struct{}{}
got[val1] = append(got[val1], val2)
}
return scanner.Err()
})
sess := exec.Start(exec.Local)
defer sess.Shutdown()
_, err := sess.Run(context.Background(), bigslice.Func(func() bigslice.Slice { return slice }))
if err != nil {
t.Fatalf("run error %v", err)
}
for length, shards := range lengthShards {
if len(shards) != 1 {
t.Errorf("found keys of length %d in multiple shards: %v", length, shards)
}
}
for _, vals := range got {
sort.Ints(vals)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}
})
}
}
type lengthHashKey string
func init() {
frame.RegisterOps(func(slice []lengthHashKey) frame.Ops {
return frame.Ops{
Less: func(i, j int) bool { return slice[i] < slice[j] },
HashWithSeed: func(i int, _ uint32) uint32 { return uint32(len(slice[i])) },
}
})
}
func TestReshuffle(t *testing.T) {
reshuffleTest(t, bigslice.Reshuffle)
}
func TestRepartition(t *testing.T) {
reshuffleTest(t, func(slice bigslice.Slice) bigslice.Slice {
return bigslice.Repartition(slice, func(nshard int, key lengthHashKey, value int) int {
return len(key) % nshard
})
})
}
func TestRepartitionType(t *testing.T) {
slice := bigslice.Const(1, []int{}, []string{})
expectTypeError(t, "repartition: expected func(int, int, string) int, got func() int", func() {
bigslice.Repartition(slice, func() int { return 0 })
})
expectTypeError(t, "repartition: expected func(int, int, string) int, got func(int, int, string)", func() {
bigslice.Repartition(slice, func(_ int, _ int, _ string) {})
})
}
func ExampleRepartition() {
// Count rows per shard before and after using Repartition to get ideal
// partitioning by taking advantage of the knowledge that our keys are
// sequential integers.
// countRowsPerShard is a utility that counts the number of rows per shard
// and stores it in rowsPerShard.
var rowsPerShard []int
countRowsPerShard := func(numShards int, slice bigslice.Slice) bigslice.Slice {
rowsPerShard = make([]int, numShards)
return bigslice.WriterFunc(slice,
func(shard int, _ struct{}, _ error, xs []int) error {
rowsPerShard[shard] += len(xs)
return nil
},
)
}
const numShards = 2
slice := bigslice.Const(numShards, []int{1, 2, 3, 4, 5, 6})
slice0 := countRowsPerShard(numShards, slice)
fmt.Println("# default partitioning")
fmt.Println("## slice contents")
slicetest.Print(slice0)
fmt.Println("## row count per shard")
for shard, count := range rowsPerShard {
fmt.Printf("shard:%d count:%d\n", shard, count)
}
slice1 := bigslice.Repartition(slice, func(nshard, x int) int {
// Put everything in partition 0 for illustration.
return 0
})
slice1 = countRowsPerShard(numShards, slice1)
fmt.Println("# repartitioned")
// Note that the slice contents are unchanged.
fmt.Println("## slice contents")
slicetest.Print(slice1)
// Note that the partitioning has changed.
fmt.Println("## row count per shard")
for shard, count := range rowsPerShard {
fmt.Printf("shard:%d count:%d\n", shard, count)
}
// Output:
// # default partitioning
// ## slice contents
// 1
// 2
// 3
// 4
// 5
// 6
// ## row count per shard
// shard:0 count:3
// shard:1 count:3
// # repartitioned
// ## slice contents
// 1
// 2
// 3
// 4
// 5
// 6
// ## row count per shard
// shard:0 count:6
// shard:1 count:0
}
func ExampleReshard() {
// Count rows per shard before and after using Reshard to change the number
// of shards from 2 to 4.
// countRowsPerShard is a utility that counts the number of rows per shard
// and stores it in rowsPerShard.
var rowsPerShard []int
countRowsPerShard := func(numShards int, slice bigslice.Slice) bigslice.Slice {
rowsPerShard = make([]int, numShards)
return bigslice.WriterFunc(slice,
func(shard int, _ struct{}, _ error, xs []int) error {
rowsPerShard[shard] += len(xs)
return nil
},
)
}
const beforeNumShards = 2
slice := bigslice.Const(beforeNumShards, []int{1, 2, 3, 4, 5, 6})
before := countRowsPerShard(beforeNumShards, slice)
fmt.Println("# before")
fmt.Println("## slice contents")
slicetest.Print(before)
fmt.Println("## row count per shard")
for shard, count := range rowsPerShard {
fmt.Printf("shard:%d count:%d\n", shard, count)
}
// Reshard to 4 shards.
const afterNumShards = 4
after := bigslice.Reshard(slice, afterNumShards)
after = countRowsPerShard(afterNumShards, after)
fmt.Println("# after")
fmt.Println("## slice contents")
slicetest.Print(after)
fmt.Println("## row count per shard")
for shard, count := range rowsPerShard {
fmt.Printf("shard:%d count:%d\n", shard, count)
}
// Output:
// # before
// ## slice contents
// 1
// 2
// 3
// 4
// 5
// 6
// ## row count per shard
// shard:0 count:3
// shard:1 count:3
// # after
// ## slice contents
// 1
// 2
// 3
// 4
// 5
// 6
// ## row count per shard
// shard:0 count:2
// shard:1 count:1
// shard:2 count:1
// shard:3 count:2
}
func ExampleReshuffle() {
// Count rows per shard before and after a Reshuffle, showing same-keyed
// rows all go to the same shard.
// countRowsPerShard is a utility that counts the number of rows per shard
// and stores it in rowsPerShard.
var rowsPerShard []int
countRowsPerShard := func(numShards int, slice bigslice.Slice) bigslice.Slice {
rowsPerShard = make([]int, numShards)
return bigslice.WriterFunc(slice,
func(shard int, _ struct{}, _ error, xs []int) error {
rowsPerShard[shard] += len(xs)
return nil
},
)
}
const numShards = 2
slice := bigslice.Const(numShards, []int{1, 2, 3, 4, 5, 6})
slice = bigslice.Map(slice, func(_ int) int { return 0 })
before := countRowsPerShard(numShards, slice)
fmt.Println("# before")
fmt.Println("## slice contents")
slicetest.Print(before)
fmt.Println("## row count per shard")
for shard, count := range rowsPerShard {
fmt.Printf("shard:%d count:%d\n", shard, count)
}
after := bigslice.Reshuffle(slice)
after = countRowsPerShard(numShards, after)
fmt.Println("# after")
// We set all our keys to 0. After reshuffling, all rows will be in the same
// shard.
fmt.Println("## slice contents")
slicetest.Print(after)
fmt.Println("## row count per shard")
for shard, count := range rowsPerShard {
fmt.Printf("shard:%d count:%d\n", shard, count)
}
// Output:
// # before
// ## slice contents
// 0
// 0
// 0
// 0
// 0
// 0
// ## row count per shard
// shard:0 count:3
// shard:1 count:3
// # after
// ## slice contents
// 0
// 0
// 0
// 0
// 0
// 0
// ## row count per shard
// shard:0 count:6
// shard:1 count:0
}