forked from dgraph-io/dgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shuffle.go
164 lines (145 loc) · 3.69 KB
/
shuffle.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
package main
import (
"bufio"
"bytes"
"container/heap"
"encoding/binary"
"io"
"log"
"os"
"github.com/dgraph-io/badger"
bo "github.com/dgraph-io/badger/options"
"github.com/dgraph-io/dgraph/protos"
"github.com/dgraph-io/dgraph/x"
"github.com/gogo/protobuf/proto"
)
type shuffler struct {
*state
output chan<- shuffleOutput
}
func (s *shuffler) run() {
shardDirs := shardDirs(s.opt.TmpDir)
x.AssertTrue(len(shardDirs) == s.opt.ReduceShards)
x.AssertTrue(len(s.opt.shardOutputDirs) == s.opt.ReduceShards)
thr := x.NewThrottle(s.opt.NumShufflers)
for i := 0; i < s.opt.ReduceShards; i++ {
thr.Start()
go func(i int, kv *badger.KV) {
mapFiles := filenamesInTree(shardDirs[i])
shuffleInputChs := make([]chan *protos.MapEntry, len(mapFiles))
for i, mapFile := range mapFiles {
shuffleInputChs[i] = make(chan *protos.MapEntry, 1000)
go readMapOutput(mapFile, shuffleInputChs[i])
}
ci := &countIndexer{state: s.state, kv: kv}
s.shufflePostings(shuffleInputChs, ci)
ci.wait()
thr.Done()
}(i, s.createBadger(i))
}
thr.Wait()
close(s.output)
}
func (s *shuffler) createBadger(i int) *badger.KV {
opt := badger.DefaultOptions
opt.SyncWrites = false
opt.TableLoadingMode = bo.MemoryMap
opt.Dir = s.opt.shardOutputDirs[i]
opt.ValueDir = opt.Dir
kv, err := badger.NewKV(&opt)
x.Check(err)
s.kvs = append(s.kvs, kv)
return kv
}
func readMapOutput(filename string, mapEntryCh chan<- *protos.MapEntry) {
fd, err := os.Open(filename)
x.Check(err)
defer fd.Close()
r := bufio.NewReaderSize(fd, 16<<10)
unmarshalBuf := make([]byte, 1<<10)
for {
buf, err := r.Peek(binary.MaxVarintLen64)
if err == io.EOF {
break
}
x.Check(err)
sz, n := binary.Uvarint(buf)
if n <= 0 {
log.Fatal("Could not read uvarint: %d", n)
}
x.Check2(r.Discard(n))
for cap(unmarshalBuf) < int(sz) {
unmarshalBuf = make([]byte, sz)
}
x.Check2(io.ReadFull(r, unmarshalBuf[:sz]))
me := new(protos.MapEntry)
x.Check(proto.Unmarshal(unmarshalBuf[:sz], me))
mapEntryCh <- me
}
close(mapEntryCh)
}
func (s *shuffler) shufflePostings(mapEntryChs []chan *protos.MapEntry, ci *countIndexer) {
var ph postingHeap
for _, ch := range mapEntryChs {
heap.Push(&ph, heapNode{mapEntry: <-ch, ch: ch})
}
const batchSize = 1000
const batchAlloc = batchSize * 11 / 10
batch := make([]*protos.MapEntry, 0, batchAlloc)
var prevKey []byte
var plistLen int
for len(ph.nodes) > 0 {
me := ph.nodes[0].mapEntry
var ok bool
ph.nodes[0].mapEntry, ok = <-ph.nodes[0].ch
if ok {
heap.Fix(&ph, 0)
} else {
heap.Pop(&ph)
}
keyChanged := bytes.Compare(prevKey, me.Key) != 0
if keyChanged && plistLen > 0 {
ci.addUid(prevKey, plistLen)
plistLen = 0
}
if len(batch) >= batchSize && bytes.Compare(prevKey, me.Key) != 0 {
s.output <- shuffleOutput{mapEntries: batch, kv: ci.kv}
NumQueuedReduceJobs.Add(1)
batch = make([]*protos.MapEntry, 0, batchAlloc)
}
prevKey = me.Key
batch = append(batch, me)
plistLen++
}
if len(batch) > 0 {
s.output <- shuffleOutput{mapEntries: batch, kv: ci.kv}
NumQueuedReduceJobs.Add(1)
}
if plistLen > 0 {
ci.addUid(prevKey, plistLen)
}
}
type heapNode struct {
mapEntry *protos.MapEntry
ch <-chan *protos.MapEntry
}
type postingHeap struct {
nodes []heapNode
}
func (h *postingHeap) Len() int {
return len(h.nodes)
}
func (h *postingHeap) Less(i, j int) bool {
return less(h.nodes[i].mapEntry, h.nodes[j].mapEntry)
}
func (h *postingHeap) Swap(i, j int) {
h.nodes[i], h.nodes[j] = h.nodes[j], h.nodes[i]
}
func (h *postingHeap) Push(x interface{}) {
h.nodes = append(h.nodes, x.(heapNode))
}
func (h *postingHeap) Pop() interface{} {
elem := h.nodes[len(h.nodes)-1]
h.nodes = h.nodes[:len(h.nodes)-1]
return elem
}