/
mrtest.go
165 lines (150 loc) · 4.71 KB
/
mrtest.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
// Package mrtest is a test package tightly tied to the mr package, separated
// out to avoid import cycles when other tests want to use it.
package mrtest // import "entrogo.com/entroq/contrib/mrtest"
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"math/rand"
"sort"
"strings"
"entrogo.com/entroq"
. "entrogo.com/entroq/contrib/mr"
"github.com/google/uuid"
)
// MRCheck is a check function that runs a mapreduce using the specified number of mappers and reducers.
//
// Creates a bunch of documents, filled with numeric strings (words are all
// numbers, makes things easy). We start with a known histogram, then we
// assemble documents by randomly drawing without replacement until the
// distribution is empty. That way we know our outputs from the beginning,
// and we get a random starting point.
//
// When using with quick.Check, the first two arguments should usually be
// fixed, but the remainder can be "checked". Thus, it often makes sense to use
// it in a closure, thus:
//
// config := &quick.Config{
// MaxCount: 5,
// Values: func(values []reflect.Value, rand *rand.Rand) {
// values[0] = reflect.ValueOf(rand.Intn(2000) + 1000)
// values[1] = reflect.ValueOf(rand.Intn(100) + 1)
// values[2] = reflect.ValueOf(rand.Intn(20) + 1)
// },
// }
// check := func(nm, nr int) bool {
// return MRCheck(ctx, client, nm, nr)
// }
// if err := quick.Check(check, config); err != nil {
// t.Fatal(err)
// }
func MRCheck(ctx context.Context, eq *entroq.EntroQ, numDocs, numMappers, numReducers int) bool {
const (
uniqueWords = 10
wordsPerDoc = 1000
)
log.Printf("Checking MR with docs=%d, mappers=%d, reducers=%d", numDocs, numMappers, numReducers)
// Flattened slice of words (keep track in a histogram, too).
// Random histogram of "words" (integers).
var occurrences []string
histogram := make(map[string]int)
for i := 0; i < wordsPerDoc*numDocs; i++ {
val := fmt.Sprint(rand.Intn(uniqueWords))
histogram[val]++
occurrences = append(occurrences, val)
}
// Shuffle occurrences, then make "documents" out of them.
rand.Shuffle(len(occurrences), func(i, j int) {
occurrences[i], occurrences[j] = occurrences[j], occurrences[i]
})
var docs []*KV
for di := 0; di < numDocs; di++ {
docs = append(docs, NewKV(nil, []byte(strings.Join(occurrences[di*wordsPerDoc:(di+1)*wordsPerDoc], " "))))
}
// Create expected output from processing these documents. Should be sorted
// (lexicographical) list of words with their frequencies.
var expected []*KV
for word, count := range histogram {
expected = append(expected, NewKV([]byte(word), []byte(fmt.Sprint(count))))
}
sort.Slice(expected, func(i, j int) bool {
return bytes.Compare(expected[i].Key, expected[j].Key) < 0
})
queuePrefix := "/mrtest/" + uuid.New().String()
mr := NewMapReduce(eq, queuePrefix,
WithNumMappers(numMappers),
WithNumReducers(numReducers),
WithMap(WordCountMapper),
WithReduce(SumReducer),
WithEarlyReduce(SumReducer),
AddInput(docs...))
outQ, err := mr.Run(ctx)
if err != nil {
log.Print(err)
return false
}
tasks, err := eq.Tasks(ctx, outQ)
if err != nil {
log.Print(err)
return false
}
good := true
if len(tasks) == 0 || len(tasks) > numReducers {
log.Printf("Expected between 1 and %d output tasks, got %d", numReducers, len(tasks))
good = false
for i, t := range tasks {
var kvs []*KV
if err := json.Unmarshal(t.Value, &kvs); err != nil {
log.Printf("Failed to unmarshal task %v on queue %q: %v", t.IDVersion(), t.Queue, err)
return false
}
log.Printf("%d: %q %v", i, t.Queue, t.IDVersion())
kvmap := make(map[string]string)
for _, kv := range kvs {
kvmap[string(kv.Key)] = string(kv.Value)
}
out, err := json.Marshal(kvmap)
if err != nil {
log.Printf("Failed to marshal kvmap: %v", err)
}
log.Print(string(out))
}
}
// Check that each task's values are already in key-sorted order.
var allKVs []*KV
for i, task := range tasks {
var got []*KV
if err := json.Unmarshal(task.Value, &got); err != nil {
log.Print(err)
return false
}
if !sort.SliceIsSorted(got, func(i, j int) bool {
return bytes.Compare(got[i].Key, got[j].Key) < 0
}) {
log.Printf("reduce task %d contains unsorted keys: %v", i, got)
return false
}
allKVs = append(allKVs, got...)
}
sort.Slice(allKVs, func(i, j int) bool {
return bytes.Compare(allKVs[i].Key, allKVs[j].Key) < 0
})
for i, kv := range allKVs {
if want, got := expected[i].String(), kv.String(); want != got {
log.Printf("Expected %s, got %s", want, got)
good = false
}
}
if !good {
queues, err := eq.Queues(ctx, entroq.MatchPrefix(queuePrefix))
for q, n := range queues {
log.Printf("queue %q = %d", q, n)
}
if err != nil {
log.Printf("queues error: %v", err)
}
}
return good
}