-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
loader.go
284 lines (247 loc) · 6.81 KB
/
loader.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
/*
* Copyright 2017-2018 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package bulk
import (
"bytes"
"compress/gzip"
"context"
"fmt"
"hash/adler32"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"sync"
"time"
"github.com/dgraph-io/badger/v2"
"github.com/dgraph-io/badger/v2/y"
"github.com/dgraph-io/dgraph/chunker"
"github.com/dgraph-io/dgraph/protos/pb"
"github.com/dgraph-io/dgraph/schema"
"github.com/dgraph-io/dgraph/x"
"github.com/dgraph-io/dgraph/xidmap"
"google.golang.org/grpc"
)
type options struct {
DataFiles string
DataFormat string
SchemaFile string
OutDir string
ReplaceOutDir bool
TmpDir string
NumGoroutines int
MapBufSize uint64
SkipMapPhase bool
CleanupTmp bool
NumReducers int
Version bool
StoreXids bool
ZeroAddr string
HttpAddr string
IgnoreErrors bool
CustomTokenizers string
NewUids bool
ClientDir string
MapShards int
ReduceShards int
shardOutputDirs []string
// ........... Badger options ..........
// BadgerKeyFile is the file containing the key used for encryption. Enterprise only feature.
BadgerKeyFile string
// BadgerCompressionlevel is the compression level to use while writing to badger.
BadgerCompressionLevel int
}
type state struct {
opt *options
prog *progress
xids *xidmap.XidMap
schema *schemaStore
shards *shardMap
readerChunkCh chan *bytes.Buffer
mapFileId uint32 // Used atomically to name the output files of the mappers.
dbs []*badger.DB
writeTs uint64 // All badger writes use this timestamp
}
type loader struct {
*state
mappers []*mapper
zero *grpc.ClientConn
}
func newLoader(opt *options) *loader {
if opt == nil {
log.Fatalf("Cannot create loader with nil options.")
}
fmt.Printf("Connecting to zero at %s\n", opt.ZeroAddr)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
zero, err := grpc.DialContext(ctx, opt.ZeroAddr,
grpc.WithBlock(),
grpc.WithInsecure())
x.Checkf(err, "Unable to connect to zero, Is it running at %s?", opt.ZeroAddr)
st := &state{
opt: opt,
prog: newProgress(),
shards: newShardMap(opt.MapShards),
// Lots of gz readers, so not much channel buffer needed.
readerChunkCh: make(chan *bytes.Buffer, opt.NumGoroutines),
writeTs: getWriteTimestamp(zero),
}
st.schema = newSchemaStore(readSchema(opt.SchemaFile), opt, st)
ld := &loader{
state: st,
mappers: make([]*mapper, opt.NumGoroutines),
zero: zero,
}
for i := 0; i < opt.NumGoroutines; i++ {
ld.mappers[i] = newMapper(st)
}
go ld.prog.report()
return ld
}
func getWriteTimestamp(zero *grpc.ClientConn) uint64 {
client := pb.NewZeroClient(zero)
for {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
ts, err := client.Timestamps(ctx, &pb.Num{Val: 1})
cancel()
if err == nil {
return ts.GetStartId()
}
fmt.Printf("Error communicating with dgraph zero, retrying: %v", err)
time.Sleep(time.Second)
}
}
func readSchema(filename string) *schema.ParsedSchema {
f, err := os.Open(filename)
x.Check(err)
defer f.Close()
var r io.Reader = f
if filepath.Ext(filename) == ".gz" {
r, err = gzip.NewReader(f)
x.Check(err)
}
buf, err := ioutil.ReadAll(r)
x.Check(err)
result, err := schema.Parse(string(buf))
x.Check(err)
return result
}
func (ld *loader) mapStage(opt *options) {
ld.prog.setPhase(mapPhase)
if len(opt.ClientDir) > 0 {
var db *badger.DB
x.Check(os.MkdirAll(opt.ClientDir, 0700))
var err error
db, err = badger.Open(badger.DefaultOptions(opt.ClientDir))
x.Checkf(err, "Error while creating badger KV posting store")
ld.xids = xidmap.New(ld.zero, db)
} else {
ld.xids = xidmap.New(ld.zero, nil)
}
files := x.FindDataFiles(ld.opt.DataFiles, []string{".rdf", ".rdf.gz", ".json", ".json.gz"})
if len(files) == 0 {
fmt.Printf("No data files found in %s.\n", ld.opt.DataFiles)
os.Exit(1)
}
// Because mappers must handle chunks that may be from different input files, they must all
// assume the same data format, either RDF or JSON. Use the one specified by the user or by
// the first load file.
loadType := chunker.DataFormat(files[0], ld.opt.DataFormat)
if loadType == chunker.UnknownFormat {
// Dont't try to detect JSON input in bulk loader.
fmt.Printf("Need --format=rdf or --format=json to load %s", files[0])
os.Exit(1)
}
var mapperWg sync.WaitGroup
mapperWg.Add(len(ld.mappers))
for _, m := range ld.mappers {
go func(m *mapper) {
m.run(loadType)
mapperWg.Done()
}(m)
}
// This is the main map loop.
thr := y.NewThrottle(ld.opt.NumGoroutines)
for i, file := range files {
x.Check(thr.Do())
fmt.Printf("Processing file (%d out of %d): %s\n", i+1, len(files), file)
go func(file string) {
defer thr.Done(nil)
r, cleanup := chunker.FileReader(file)
defer cleanup()
chunk := chunker.NewChunker(loadType, 1000)
for {
chunkBuf, err := chunk.Chunk(r)
if chunkBuf != nil && chunkBuf.Len() > 0 {
ld.readerChunkCh <- chunkBuf
}
if err == io.EOF {
break
} else if err != nil {
x.Check(err)
}
}
}(file)
}
x.Check(thr.Finish())
close(ld.readerChunkCh)
mapperWg.Wait()
// Allow memory to GC before the reduce phase.
for i := range ld.mappers {
ld.mappers[i] = nil
}
x.Check(ld.xids.Flush())
ld.xids = nil
}
func (ld *loader) reduceStage() {
ld.prog.setPhase(reducePhase)
r := reducer{
state: ld.state,
streamIds: make(map[string]uint32),
}
x.Check(r.run())
}
func (ld *loader) writeSchema() {
numDBs := uint32(len(ld.dbs))
preds := make([][]string, numDBs)
// Get all predicates that have data in some DB.
m := make(map[string]struct{})
for i, db := range ld.dbs {
preds[i] = ld.schema.getPredicates(db)
for _, p := range preds[i] {
m[p] = struct{}{}
}
}
// Find any predicates that don't have data in any DB
// and distribute them among all the DBs.
for p := range ld.schema.schemaMap {
if _, ok := m[p]; !ok {
i := adler32.Checksum([]byte(p)) % numDBs
preds[i] = append(preds[i], p)
}
}
// Write out each DB's final predicate list.
for i, db := range ld.dbs {
ld.schema.write(db, preds[i])
}
}
func (ld *loader) cleanup() {
for _, db := range ld.dbs {
x.Check(db.Close())
}
ld.prog.endSummary()
}