Newer
Older
/*
* Copyright 2015 Manish R Jain <manishrjain@gmail.com>
*
* 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 loader
import (
"bufio"
"io"
Manish R Jain
committed
"math/rand"
"runtime"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/Sirupsen/logrus"
"github.com/dgraph-io/dgraph/posting"
"github.com/dgraph-io/dgraph/rdf"
"github.com/dgraph-io/dgraph/x"
"github.com/dgryski/go-farm"
)
var glog = x.Log("loader")
type counters struct {
read uint64
parsed uint64
processed uint64
ignored uint64
}
type state struct {
input chan string
cnq chan rdf.NQuad
ctr *counters
instanceIdx uint64
numInstances uint64
Manish R Jain
committed
func (s *state) printCounters(ticker *time.Ticker) {
Manish R Jain
committed
var prev uint64
for _ = range ticker.C {
Manish R Jain
committed
processed := atomic.LoadUint64(&s.ctr.processed)
if prev == processed {
continue
}
prev = processed
Manish R Jain
committed
parsed := atomic.LoadUint64(&s.ctr.parsed)
ignored := atomic.LoadUint64(&s.ctr.ignored)
pending := parsed - ignored - processed
glog.WithFields(logrus.Fields{
Manish R Jain
committed
"read": atomic.LoadUint64(&s.ctr.read),
"processed": processed,
"parsed": parsed,
"ignored": ignored,
"pending": pending,
"len_cnq": len(s.cnq),
}).Info("Counters")
}
}
func (s *state) readLines(r io.Reader) {
Manish R Jain
committed
var buf []string
scanner := bufio.NewScanner(r)
Manish R Jain
committed
// Randomize lines to avoid contention on same subject.
for i := 0; i < 1000; i++ {
if scanner.Scan() {
buf = append(buf, scanner.Text())
} else {
break
}
}
ln := len(buf)
for scanner.Scan() {
Manish R Jain
committed
k := rand.Intn(ln)
s.input <- buf[k]
buf[k] = scanner.Text()
atomic.AddUint64(&s.ctr.read, 1)
}
if err := scanner.Err(); err != nil {
glog.WithError(err).Fatal("While reading file.")
}
Manish R Jain
committed
for i := 0; i < len(buf); i++ {
s.input <- buf[i]
}
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
close(s.input)
}
func (s *state) parseStream(done chan error) {
for line := range s.input {
line = strings.Trim(line, " \t")
if len(line) == 0 {
glog.Info("Empty line.")
continue
}
glog.Debugf("Got line: %q", line)
nq, err := rdf.Parse(line)
if err != nil {
glog.WithError(err).Errorf("While parsing: %q", line)
done <- err
return
}
s.cnq <- nq
atomic.AddUint64(&s.ctr.parsed, 1)
}
done <- nil
}
func (s *state) handleNQuads(wg *sync.WaitGroup) {
for nq := range s.cnq {
Manish R Jain
committed
for err != nil {
// Just put in a retry loop to tackle temporary errors.
if err == posting.E_TMP_ERROR {
time.Sleep(time.Microsecond)
} else {
glog.WithError(err).WithField("nq", nq).
Error("While converting to edge")
return
}
}
key := posting.Key(edge.Entity, edge.Attribute)
Manish R Jain
committed
plist := posting.GetOrCreate(key)
plist.AddMutation(edge, posting.Set)
atomic.AddUint64(&s.ctr.processed, 1)
}
wg.Done()
}
func (s *state) getUidForString(str string) {
_, err := rdf.GetUid(str, s.instanceIdx, s.numInstances)
for err != nil {
// Just put in a retry loop to tackle temporary errors.
if err == posting.E_TMP_ERROR {
time.Sleep(time.Microsecond)
glog.WithError(err).WithField("nq.Subject", str).
Error("Temporary error")
} else {
glog.WithError(err).WithField("nq.Subject", str).
Error("While getting UID")
return
}
_, err = rdf.GetUid(str, s.instanceIdx, s.numInstances)
}
}
func (s *state) handleNQuadsWhileAssign(wg *sync.WaitGroup) {
for nq := range s.cnq {
if farm.Fingerprint64([]byte(nq.Subject))%s.numInstances != s.instanceIdx {
atomic.AddUint64(&s.ctr.ignored, 1)
if len(nq.ObjectId) == 0 || farm.Fingerprint64([]byte(nq.ObjectId))%s.numInstances != s.instanceIdx {
// This instance shouldnt or cant assign UID to this string
atomic.AddUint64(&s.ctr.ignored, 1)
} else {
// Blocking function.
func HandleRdfReader(reader io.Reader, instanceIdx uint64, numInstances uint64) (uint64, error) {
s := new(state)
s.ctr = new(counters)
ticker := time.NewTicker(time.Second)
Manish R Jain
committed
go s.printCounters(ticker)
// Producer: Start buffering input to channel.
s.input = make(chan string, 10000)
go s.readLines(reader)
s.cnq = make(chan rdf.NQuad, 10000)
Manish R Jain
committed
numr := runtime.GOMAXPROCS(-1)
done := make(chan error, numr)
for i := 0; i < numr; i++ {
go s.parseStream(done) // Input --> NQuads
}
Manish R Jain
committed
wg := new(sync.WaitGroup)
Manish R Jain
committed
for i := 0; i < 3000; i++ {
Manish R Jain
committed
wg.Add(1)
go s.handleNQuads(wg) // NQuads --> Posting list [slow].
}
// Block until all parseStream goroutines are finished.
for i := 0; i < numr; i++ {
if err := <-done; err != nil {
glog.WithError(err).Fatal("While reading input.")
}
}
Manish R Jain
committed
close(s.cnq)
// Okay, we've stopped input to cnq, and closed it.
// Now wait for handleNQuads to finish.
wg.Wait()
ticker.Stop()
return atomic.LoadUint64(&s.ctr.processed), nil
}
func HandleRdfReaderWhileAssign(reader io.Reader, instanceIdx uint64, numInstances uint64) (uint64, error) {
s := new(state)
s.ctr = new(counters)
ticker := time.NewTicker(time.Second)
go s.printCounters(ticker)
// Producer: Start buffering input to channel.
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
s.input = make(chan string, 10000)
go s.readLines(reader)
s.cnq = make(chan rdf.NQuad, 10000)
numr := runtime.GOMAXPROCS(-1)
done := make(chan error, numr)
for i := 0; i < numr; i++ {
go s.parseStream(done) // Input --> NQuads
}
wg := new(sync.WaitGroup)
for i := 0; i < 3000; i++ {
wg.Add(1)
go s.handleNQuadsWhileAssign(wg) //Different compared to HandleRdfReader
}
// Block until all parseStream goroutines are finished.
for i := 0; i < numr; i++ {
if err := <-done; err != nil {
glog.WithError(err).Fatal("While reading input.")
}
}
close(s.cnq)
// Okay, we've stopped input to cnq, and closed it.
// Now wait for handleNQuads to finish.
wg.Wait()
ticker.Stop()
return atomic.LoadUint64(&s.ctr.processed), nil
}