Skip to content

Use rampMeter for Executor #5503

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

Merged
merged 5 commits into from
Jun 2, 2020
Merged
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
13 changes: 8 additions & 5 deletions worker/draft.go
Original file line number Diff line number Diff line change
Expand Up @@ -852,17 +852,20 @@ func (n *node) proposeSnapshot(discardN int) error {
return n.Raft().Propose(n.ctx, data)
}

const maxPendingSize int64 = 64 << 20 // in bytes.
const (
maxPendingSize int64 = 64 << 20 // in bytes.
nodeApplyChan = "raft node applyCh"
)

func (n *node) rampMeter() {
func rampMeter(address *int64, maxSize int64, component string) {
start := time.Now()
defer func() {
if dur := time.Since(start); dur > time.Second {
glog.Infof("Blocked pushing to applyCh for %v", dur.Round(time.Millisecond))
glog.Infof("Blocked pushing to %s for %v", component, dur.Round(time.Millisecond))
}
}()
for {
if atomic.LoadInt64(&n.pendingSize) <= maxPendingSize {
if atomic.LoadInt64(address) <= maxSize {
return
}
time.Sleep(3 * time.Millisecond)
Expand Down Expand Up @@ -1171,7 +1174,7 @@ func (n *node) Run() {
// Apply the meter this before adding size to pending size so some crazy big
// proposal can be pushed to applyCh. If this do this after adding its size to
// pending size, we could block forever in rampMeter.
n.rampMeter()
rampMeter(&n.pendingSize, maxPendingSize, nodeApplyChan)
var pendingSize int64
for _, p := range proposals {
pendingSize += int64(p.Size())
Expand Down
18 changes: 17 additions & 1 deletion worker/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package worker
import (
"context"
"sync"
"sync/atomic"

"github.com/dgraph-io/badger/v2/y"
"github.com/dgraph-io/dgraph/posting"
Expand All @@ -35,6 +36,8 @@ type subMutation struct {
}

type executor struct {
pendingSize int64

sync.RWMutex
predChan map[string]chan *subMutation
closer *y.Closer
Expand All @@ -54,8 +57,10 @@ func (e *executor) processMutationCh(ch chan *subMutation) {

writer := posting.NewTxnWriter(pstore)
for payload := range ch {
var esize int64
ptxn := posting.NewTxn(payload.startTs)
for _, edge := range payload.edges {
esize += int64(edge.Size())
for {
err := runMutation(payload.ctx, edge, ptxn)
if err == nil {
Expand All @@ -74,6 +79,8 @@ func (e *executor) processMutationCh(ch chan *subMutation) {
if err := writer.Wait(); err != nil {
glog.Errorf("Error while waiting for writes: %v", err)
}

atomic.AddInt64(&e.pendingSize, -esize)
}
}

Expand All @@ -99,9 +106,16 @@ func (e *executor) getChannelUnderLock(pred string) (ch chan *subMutation) {
return ch
}

const (
maxPendingEdgesSize int64 = 64 << 20
executorAddEdges = "executor.addEdges"
)

func (e *executor) addEdges(ctx context.Context, startTs uint64, edges []*pb.DirectedEdge) {
payloadMap := make(map[string]*subMutation)
rampMeter(&e.pendingSize, maxPendingEdgesSize, executorAddEdges)

payloadMap := make(map[string]*subMutation)
var esize int64
for _, edge := range edges {
payload, ok := payloadMap[edge.Attr]
if !ok {
Expand All @@ -112,6 +126,7 @@ func (e *executor) addEdges(ctx context.Context, startTs uint64, edges []*pb.Dir
payload = payloadMap[edge.Attr]
}
payload.edges = append(payload.edges, edge)
esize += int64(edge.Size())
}

// Lock() in case the channel gets closed from underneath us.
Expand All @@ -127,4 +142,5 @@ func (e *executor) addEdges(ctx context.Context, startTs uint64, edges []*pb.Dir
}
}

atomic.AddInt64(&e.pendingSize, esize)
}