forked from dgraph-io/dgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
220 lines (196 loc) · 4.26 KB
/
main.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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/dgraph-io/dgraph/client"
"github.com/dgraph-io/dgraph/protos/api"
"github.com/dgraph-io/dgraph/x"
"github.com/dgraph-io/dgraph/y"
"google.golang.org/grpc"
)
var (
dgraAddr = flag.String("d", "localhost:9081", "dgraph address")
concurr = flag.Int("c", 5, "number of concurrent upserts per account")
)
var (
firsts = []string{"Paul", "Eric", "Jack", "John", "Martin"}
lasts = []string{"Brown", "Smith", "Robinson", "Waters", "Taylor"}
ages = []int{20, 25, 30, 35}
)
type account struct {
first string
last string
age int
}
var accounts []account
func init() {
for _, first := range firsts {
for _, last := range lasts {
for _, age := range ages {
accounts = append(accounts, account{
first: first,
last: last,
age: age,
})
}
}
}
}
func main() {
flag.Parse()
c := newClient()
setup(c)
doUpserts(c)
checkIntegrity(c)
}
func newClient() *client.Dgraph {
d, err := grpc.Dial(*dgraAddr, grpc.WithInsecure())
x.Check(err)
return client.NewDgraphClient(
api.NewDgraphClient(d),
)
}
func setup(c *client.Dgraph) {
ctx := context.Background()
x.Check(c.Alter(ctx, &api.Operation{
DropAll: true,
}))
x.Check(c.Alter(ctx, &api.Operation{
Schema: `
first: string @index(term) @upsert .
last: string @index(hash) @upsert .
age: int @index(int) @upsert .
when: int .
`,
}))
}
func doUpserts(c *client.Dgraph) {
var wg sync.WaitGroup
wg.Add(len(accounts) * *concurr)
for _, acct := range accounts {
for i := 0; i < *concurr; i++ {
go func(acct account) {
upsert(c, acct)
wg.Done()
}(acct)
}
}
wg.Wait()
}
var (
successCount uint64
retryCount uint64
lastStatus time.Time
)
func upsert(c *client.Dgraph, acc account) {
for {
if time.Since(lastStatus) > 100*time.Millisecond {
fmt.Printf("Success: %d Retries: %d\n",
atomic.LoadUint64(&successCount), atomic.LoadUint64(&retryCount))
lastStatus = time.Now()
}
err := tryUpsert(c, acc)
if err == nil {
atomic.AddUint64(&successCount, 1)
return
}
if err != y.ErrAborted {
x.Check(err)
}
atomic.AddUint64(&retryCount, 1)
}
}
func tryUpsert(c *client.Dgraph, acc account) error {
ctx := context.Background()
txn := c.NewTxn()
defer txn.Discard(ctx)
q := fmt.Sprintf(`
{
get(func: eq(first, %q)) @filter(eq(last, %q) AND eq(age, %d)) {
uid
}
}
`, acc.first, acc.last, acc.age)
resp, err := txn.Query(ctx, q)
x.Check(err)
decode := struct {
Get []struct {
Uid *string
}
}{}
x.Check(json.Unmarshal(resp.GetJson(), &decode))
x.AssertTrue(len(decode.Get) <= 1)
var uid string
if len(decode.Get) == 1 {
x.AssertTrue(decode.Get[0].Uid != nil)
uid = *decode.Get[0].Uid
} else {
nqs := fmt.Sprintf(`
_:acct <first> %q .
_:acct <last> %q .
_:acct <age> "%d"^^<xs:int> .
`,
acc.first, acc.last, acc.age,
)
mu := &api.Mutation{SetNquads: []byte(nqs)}
assigned, err := txn.Mutate(ctx, mu)
if err != nil {
return err
}
uid = assigned.GetUids()["acct"]
x.AssertTrue(uid != "")
}
nq := fmt.Sprintf(`
<%s> <when> "%d"^^<xs:int> .
`,
uid, time.Now().Nanosecond(),
)
mu := &api.Mutation{SetNquads: []byte(nq)}
if _, err = txn.Mutate(ctx, mu); err != nil {
return err
}
return txn.Commit(ctx)
}
func checkIntegrity(c *client.Dgraph) {
ctx := context.Background()
q := fmt.Sprintf(`
{
all(func: anyofterms(first, %q)) {
first
last
age
}
}
`, strings.Join(firsts, " "))
resp, err := c.NewTxn().Query(ctx, q)
x.Check(err)
decode := struct {
All []struct {
First *string
Last *string
Age *int
}
}{}
x.Check(json.Unmarshal(resp.GetJson(), &decode))
// Make sure there is exactly one of each account.
accountSet := make(map[string]struct{})
for _, record := range decode.All {
x.AssertTrue(record.First != nil)
x.AssertTrue(record.Last != nil)
x.AssertTrue(record.Age != nil)
str := fmt.Sprintf("%s_%s_%d", *record.First, *record.Last, *record.Age)
accountSet[str] = struct{}{}
}
x.AssertTrue(len(accountSet) == len(accounts))
for _, acct := range accounts {
str := fmt.Sprintf("%s_%s_%d", acct.first, acct.last, acct.age)
_, ok := accountSet[str]
x.AssertTrue(ok)
}
}