forked from dgraph-io/dgraph
-
Notifications
You must be signed in to change notification settings - Fork 1
/
predicate_move.go
227 lines (206 loc) · 5.66 KB
/
predicate_move.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
221
222
223
224
225
226
227
/*
* 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 worker
import (
"fmt"
"io"
"strconv"
"golang.org/x/net/context"
"github.com/dgraph-io/badger"
"gopkg.in/adibiarsotp/dgraph.v82/posting"
"gopkg.in/adibiarsotp/dgraph.v82/protos"
"gopkg.in/adibiarsotp/dgraph.v82/x"
)
var (
errEmptyPredicate = x.Errorf("Predicate not specified")
errNotLeader = x.Errorf("Server is not leader of this group")
emptyPayload = protos.Payload{}
)
// size of kvs won't be too big, we would take care before proposing.
func populateKeyValues(ctx context.Context, kvs []*protos.KV) error {
x.Printf("Writing %d keys\n", len(kvs))
wb := make([]*badger.Entry, 0, 1000)
// Badger does batching internally so no need to batch it.
for _, kv := range kvs {
entry := &badger.Entry{
Key: kv.Key,
Value: kv.Val,
UserMeta: kv.UserMeta[0],
}
wb = append(wb, entry)
}
if err := pstore.BatchSet(wb); err != nil {
return err
}
for _, wbe := range wb {
if err := wbe.Error; err != nil {
return err
}
}
return nil
}
func movePredicateHelper(ctx context.Context, predicate string, gid uint32) error {
pl := groups().Leader(gid)
if pl == nil {
return x.Errorf("Unable to find a connection for groupd: %d\n", gid)
}
c := protos.NewWorkerClient(pl.Get())
stream, err := c.ReceivePredicate(ctx)
if err != nil {
return err
}
count := 0
sendItem := func(stream protos.Worker_ReceivePredicateClient, item *badger.KVItem) error {
kv := &protos.KV{}
key := item.Key()
kv.Key = make([]byte, len(key))
copy(kv.Key, key)
kv.UserMeta = []byte{item.UserMeta()}
err := item.Value(func(val []byte) error {
kv.Val = make([]byte, len(val))
copy(kv.Val, val)
return nil
})
if err != nil {
return err
}
return stream.Send(kv)
}
// sends all data except schema, schema key has different prefix
it := pstore.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
prefix := x.PredicatePrefix(predicate)
for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
item := it.Item()
count++
if err := sendItem(stream, item); err != nil {
return err
}
}
// send schema
var item badger.KVItem
if err := pstore.Get(x.SchemaKey(predicate), &item); err != nil {
return err
}
if err := sendItem(stream, &item); err != nil {
return err
}
count++
payload, err := stream.CloseAndRecv()
if err != nil {
return err
}
recvCount, err := strconv.Atoi(string(payload.Data))
if err != nil {
return err
}
if recvCount != count {
return x.Errorf("Sent count %d doesn't match with received %d", count, recvCount)
}
return nil
}
func batchAndProposeKeyValues(ctx context.Context, kvs chan *protos.KV) error {
n := groups().Node
proposal := &protos.Proposal{}
size := 0
firstKV := true
for kv := range kvs {
if size >= 32<<20 { // 32 MB
if err := n.ProposeAndWait(ctx, proposal); err != nil {
return err
}
proposal.Kv = proposal.Kv[:0]
size = 0
continue
}
if firstKV {
firstKV = false
pk := x.Parse(kv.Key)
if err := posting.DeletePredicate(ctx, pk.Attr); err != nil {
return err
}
}
proposal.Kv = append(proposal.Kv, kv)
size = size + len(kv.Key) + len(kv.Val)
}
return nil
}
// Returns count which can be used to verify whether we have moved all keys
// for a predicate or not.
func (w *grpcWorker) ReceivePredicate(stream protos.Worker_ReceivePredicateServer) error {
// Values can be pretty big so having less buffer is safer.
kvs := make(chan *protos.KV, 10)
che := make(chan error, 1)
// We can use count to check the number of posting lists returned in tests.
count := 0
ctx := stream.Context()
payload := &protos.Payload{}
go func() {
// Takes care of throttling and batching.
che <- batchAndProposeKeyValues(ctx, kvs)
}()
for {
kv, err := stream.Recv()
if err == io.EOF {
payload.Data = []byte(fmt.Sprintf("%d", count))
stream.SendAndClose(payload)
break
}
if err != nil {
return err
}
count++
select {
case kvs <- kv:
case <-ctx.Done():
return ctx.Err()
case err := <-che:
return err
}
}
close(kvs)
err := <-che
return err
}
func (w *grpcWorker) MovePredicate(ctx context.Context,
in *protos.MovePredicatePayload) (*protos.Payload, error) {
if groups().gid != in.SourceGroupId {
return &emptyPayload,
x.Errorf("Group id doesn't match, received request for %d, my gid: %d",
in.SourceGroupId, groups().gid)
}
if len(in.Predicate) == 0 {
return &emptyPayload, errEmptyPredicate
}
if !groups().ServesTablet(in.Predicate) {
return &emptyPayload, errUnservedTablet
}
n := groups().Node
if n.AmLeader() {
return &emptyPayload, errNotLeader
}
// Ensures that all future mtuations beyond this point are rejected
if err := n.ProposeAndWait(ctx, &protos.Proposal{State: in.State}); err != nil {
return &emptyPayload, err
}
// We iterate over badger, so need to flush and wait for sync watermark to catch up.
if err := syncAllMarks(ctx); err != nil {
return &emptyPayload, err
}
err := movePredicateHelper(ctx, in.Predicate, in.DestGroupId)
return &emptyPayload, err
}