forked from dgraph-io/dgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.go
190 lines (168 loc) · 4.95 KB
/
batch.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
/*
* 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 live
import (
"context"
"errors"
"fmt"
"math"
"sync"
"sync/atomic"
"time"
"github.com/dgraph-io/badger"
"github.com/dgraph-io/dgraph/client"
"github.com/dgraph-io/dgraph/protos/api"
"github.com/dgraph-io/dgraph/protos/intern"
"github.com/dgraph-io/dgraph/x"
"github.com/dgraph-io/dgraph/xidmap"
)
var (
ErrMaxTries = errors.New("Max retries exceeded for request while doing batch mutations.")
)
// batchMutationOptions sets the clients batch mode to Pending number of buffers each of Size.
// Running counters of number of rdfs processed, total time and mutations per second are printed
// if PrintCounters is set true. See Counter.
type batchMutationOptions struct {
Size int
Pending int
PrintCounters bool
MaxRetries uint32
// User could pass a context so that we can stop retrying requests once context is done
Ctx context.Context
}
var defaultOptions = batchMutationOptions{
Size: 100,
Pending: 100,
PrintCounters: false,
MaxRetries: math.MaxUint32,
}
type uidProvider struct {
zero intern.ZeroClient
ctx context.Context
}
// loader is the data structure held by the user program for all interactions with the Dgraph
// server. After making grpc connection a new Dgraph is created by function NewDgraphClient.
type loader struct {
opts batchMutationOptions
dc *client.Dgraph
alloc *xidmap.XidMap
ticker *time.Ticker
kv *badger.DB
wg sync.WaitGroup
// Miscellaneous information to print counters.
// Num of RDF's sent
rdfs uint64
// Num of txns sent
txns uint64
// Num of aborts
aborts uint64
// To get time elapsel.
start time.Time
reqs chan api.Mutation
}
func (p *uidProvider) ReserveUidRange() (start, end uint64, err error) {
factor := time.Second
for {
assignedIds, err := p.zero.AssignUids(context.Background(), &intern.Num{Val: 1000})
if err == nil {
return assignedIds.StartId, assignedIds.EndId, nil
}
x.Printf("Error while getting lease %v\n", err)
select {
case <-time.After(factor):
case <-p.ctx.Done():
return 0, 0, p.ctx.Err()
}
if factor < 256*time.Second {
factor = factor * 2
}
}
}
// Counter keeps a track of various parameters about a batch mutation. Running totals are printed
// if BatchMutationOptions PrintCounters is set to true.
type Counter struct {
// Number of RDF's processed by server.
Rdfs uint64
// Number of mutations processed by the server.
TxnsDone uint64
// Number of Aborts
Aborts uint64
// Time elapsed since the batch started.
Elapsed time.Duration
}
func (l *loader) infinitelyRetry(req api.Mutation) {
defer l.wg.Done()
for {
txn := l.dc.NewTxn()
req.CommitNow = true
req.IgnoreIndexConflict = opt.ignoreIndexConflict
_, err := txn.Mutate(l.opts.Ctx, &req)
if err == nil {
atomic.AddUint64(&l.txns, 1)
return
}
atomic.AddUint64(&l.aborts, 1)
time.Sleep(10 * time.Millisecond)
}
}
func (l *loader) request(req api.Mutation) {
txn := l.dc.NewTxn()
req.CommitNow = true
req.IgnoreIndexConflict = opt.ignoreIndexConflict
_, err := txn.Mutate(l.opts.Ctx, &req)
if err == nil {
atomic.AddUint64(&l.txns, 1)
return
}
atomic.AddUint64(&l.aborts, 1)
l.wg.Add(1)
go l.infinitelyRetry(req)
}
// makeRequests can receive requests from batchNquads or directly from BatchSetWithMark.
// It doesn't need to batch the requests anymore. Batching is already done for it by the
// caller functions.
func (l *loader) makeRequests() {
defer l.wg.Done()
for req := range l.reqs {
l.request(req)
}
}
func (l *loader) printCounters() {
l.ticker = time.NewTicker(2 * time.Second)
start := time.Now()
for range l.ticker.C {
counter := l.Counter()
rate := float64(counter.Rdfs) / counter.Elapsed.Seconds()
elapsed := ((time.Since(start) / time.Second) * time.Second).String()
fmt.Printf("Total Txns done: %8d RDFs per second: %7.0f Time Elapsed: %v, Aborts: %d\n",
counter.TxnsDone, rate, elapsed, counter.Aborts)
}
}
// Counter returns the current state of the BatchMutation.
func (l *loader) Counter() Counter {
return Counter{
Rdfs: atomic.LoadUint64(&l.txns) * uint64(l.opts.Size),
TxnsDone: atomic.LoadUint64(&l.txns),
Elapsed: time.Since(l.start),
Aborts: atomic.LoadUint64(&l.aborts),
}
}
func (l *loader) stopTickers() {
if l.ticker != nil {
l.ticker.Stop()
}
}