Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing int. Use SyncPool for pathInfo #1210

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/dgraphloader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ func main() {
hostList := strings.Split(*dgraph, ",")
x.AssertTrue(len(hostList) > 0)
for _, host := range hostList {
host = strings.Trim(host, " \t")
conn, err := setupConnection(host)
x.Checkf(err, "While trying to dial gRPC")
conns = append(conns, conn)
Expand Down Expand Up @@ -263,6 +264,7 @@ func main() {
time.Sleep(1 * time.Second)
var wg sync.WaitGroup
for _, file := range filesList {
file = strings.Trim(file, " \t")
wg.Add(1)
go func(file string) {
defer wg.Done()
Expand Down
6 changes: 3 additions & 3 deletions query/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func (mr *InternalMutation) AddEdge(edge *protos.DirectedEdge, op protos.Directe
func ApplyMutations(ctx context.Context, m *protos.Mutations) error {
if worker.Config.ExpandEdge {
err := addInternalEdge(ctx, m)
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("Added Internal edges")
}
if err != nil {
return x.Wrapf(err, "While adding internal edges")
}
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("Added Internal edges")
}
}
if err := worker.MutateOverNetwork(ctx, m); err != nil {
if tr, ok := trace.FromContext(ctx); ok {
Expand Down
19 changes: 18 additions & 1 deletion query/shortest.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"container/heap"
"context"
"math"
"sync"

"golang.org/x/net/trace"

Expand Down Expand Up @@ -49,6 +50,12 @@ type Item struct {
path route // used in k shortest path.
}

var pathPool = sync.Pool{
New: func() interface{} {
return []pathInfo{}
},
}

var ErrStop = x.Errorf("STOP")
var ErrTooBig = x.Errorf("Query exceeded memory limit. Please modify the query")
var ErrFacet = x.Errorf("Skip the edge")
Expand Down Expand Up @@ -355,7 +362,15 @@ func KShortestPath(ctx context.Context, sg *SubGraph) ([]*SubGraph, error) {
neighbours := adjacencyMap[item.uid]
for toUid, info := range neighbours {
cost := info.cost
curPath := make([]pathInfo, len(item.path.route)+1)
curPath := pathPool.Get().([]pathInfo)
if cap(curPath) < len(item.path.route)+1 {
// We can't use it due to insufficient capacity. Put it back.
pathPool.Put(curPath)
curPath = make([]pathInfo, len(item.path.route)+1)
} else {
// Use the curPath from pathPool. Set length appropriately.
curPath = curPath[:len(item.path.route)+1]
}
n := copy(curPath, item.path.route)
curPath[n] = pathInfo{
uid: toUid,
Expand All @@ -373,6 +388,8 @@ func KShortestPath(ctx context.Context, sg *SubGraph) ([]*SubGraph, error) {
}
heap.Push(&pq, node)
}
// Return the popped nodes path to pool.
pathPool.Put(item.path.route)
}

next <- false
Expand Down
10 changes: 4 additions & 6 deletions worker/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,25 +378,23 @@ func MutateOverNetwork(ctx context.Context, m *protos.Mutations) error {
mutationMap := make(map[uint32]*protos.Mutations)
addToMutationMap(mutationMap, m)

errors := make(chan error, len(mutationMap))
errorCh := make(chan error, len(mutationMap))
for gid, mu := range mutationMap {
go proposeOrSend(ctx, gid, mu, errors)
go proposeOrSend(ctx, gid, mu, errorCh)
}

// Wait for all the goroutines to reply back.
// We return if an error was returned or the parent called ctx.Done()
var e error
for i := 0; i < len(mutationMap); i++ {
err := <-errors
if err != nil {
if err := <-errorCh; err != nil {
e = err
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("Error while running all mutations: %+v", err)
}
}
}
close(errors)

close(errorCh)
return e
}

Expand Down
4 changes: 2 additions & 2 deletions worker/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ func parseSrcFn(q *protos.Query) (*functionContext, error) {
if err = ensureArgsCount(q.SrcFunc, 2); err != nil {
return nil, err
}
if fc.threshold, err = strconv.ParseInt(q.SrcFunc[3], 10, 64); err != nil {
if fc.threshold, err = strconv.ParseInt(q.SrcFunc[3], 0, 64); err != nil {
return nil, x.Wrapf(err, "Compare %v(%v) require digits, but got invalid num",
q.SrcFunc[0], q.SrcFunc[2])
}
Expand Down Expand Up @@ -817,7 +817,7 @@ func parseSrcFn(q *protos.Query) (*functionContext, error) {
if err = ensureArgsCount(q.SrcFunc, 1); err != nil {
return nil, err
}
if fc.uidPresent, err = strconv.ParseUint(q.SrcFunc[2], 10, 64); err != nil {
if fc.uidPresent, err = strconv.ParseUint(q.SrcFunc[2], 0, 64); err != nil {
return nil, err
}
checkRoot(q, fc)
Expand Down