forked from dgraph-io/dgraph
/
shuffle.go
181 lines (161 loc) · 4.43 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* Copyright (C) 2017 Dgraph Labs, Inc. and Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package bulk
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/intern"
"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, db *badger.ManagedDB) {
mapFiles := filenamesInTree(shardDirs[i])
shuffleInputChs := make([]chan *intern.MapEntry, len(mapFiles))
for i, mapFile := range mapFiles {
shuffleInputChs[i] = make(chan *intern.MapEntry, 1000)
go readMapOutput(mapFile, shuffleInputChs[i])
}
ci := &countIndexer{state: s.state, db: db}
s.shufflePostings(shuffleInputChs, ci)
ci.wait()
thr.Done()
}(i, s.createBadger(i))
}
thr.Wait()
close(s.output)
}
func (s *shuffler) createBadger(i int) *badger.ManagedDB {
opt := badger.DefaultOptions
opt.SyncWrites = false
opt.TableLoadingMode = bo.MemoryMap
opt.Dir = s.opt.shardOutputDirs[i]
opt.ValueDir = opt.Dir
db, err := badger.OpenManaged(opt)
x.Check(err)
s.dbs = append(s.dbs, db)
return db
}
func readMapOutput(filename string, mapEntryCh chan<- *intern.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(intern.MapEntry)
x.Check(proto.Unmarshal(unmarshalBuf[:sz], me))
mapEntryCh <- me
}
close(mapEntryCh)
}
func (s *shuffler) shufflePostings(mapEntryChs []chan *intern.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([]*intern.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, db: ci.db}
NumQueuedReduceJobs.Add(1)
batch = make([]*intern.MapEntry, 0, batchAlloc)
}
prevKey = me.Key
batch = append(batch, me)
plistLen++
}
if len(batch) > 0 {
s.output <- shuffleOutput{mapEntries: batch, db: ci.db}
NumQueuedReduceJobs.Add(1)
}
if plistLen > 0 {
ci.addUid(prevKey, plistLen)
}
}
type heapNode struct {
mapEntry *intern.MapEntry
ch <-chan *intern.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
}