forked from dgraph-io/dgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutation.go
143 lines (124 loc) · 3.9 KB
/
mutation.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
/*
* Copyright 2016 DGraph Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package worker
import (
"golang.org/x/net/context"
"github.com/dgraph-io/dgraph/group"
"github.com/dgraph-io/dgraph/posting"
"github.com/dgraph-io/dgraph/task"
"github.com/dgraph-io/dgraph/x"
)
const (
set = "set"
del = "delete"
)
// runMutations goes through all the edges and applies them. It returns the
// mutations which were not applied in left.
func runMutations(ctx context.Context, edges []*task.DirectedEdge) error {
for _, edge := range edges {
if !groups().ServesGroup(group.BelongsTo(edge.Attr)) {
return x.Errorf("Predicate fingerprint doesn't match this instance")
}
var group uint32
if rv, ok := ctx.Value("raft").(x.RaftValue); ok {
group = rv.Group
}
key := x.DataKey(edge.Attr, edge.Entity)
plist, decr := posting.GetOrCreate(key, group)
defer decr()
if err := plist.AddMutationWithIndex(ctx, edge); err != nil {
x.Printf("Error while adding mutation: %v %v", edge, err)
return err // abort applying the rest of them.
}
}
return nil
}
// runMutate is used to run the mutations on an instance.
func proposeOrSend(ctx context.Context, gid uint32, m *task.Mutations, che chan error) {
if groups().ServesGroup(gid) {
node := groups().Node(gid)
che <- node.ProposeAndWait(ctx, &task.Proposal{Mutations: m})
return
}
_, addr := groups().Leader(gid)
pl := pools().get(addr)
conn, err := pl.Get()
if err != nil {
x.TraceError(ctx, err)
che <- err
return
}
defer pl.Put(conn)
c := NewWorkerClient(conn)
_, err = c.Mutate(ctx, m)
che <- err
}
// addToMutationArray adds the edges to the appropriate index in the mutationArray,
// taking into account the op(operation) and the attribute.
func addToMutationMap(mutationMap map[uint32]*task.Mutations, edges []*task.DirectedEdge) {
for _, edge := range edges {
gid := group.BelongsTo(edge.Attr)
mu := mutationMap[gid]
if mu == nil {
mu = &task.Mutations{GroupId: gid}
mutationMap[gid] = mu
}
mu.Edges = append(mu.Edges, edge)
}
}
// MutateOverNetwork checks which group should be running the mutations
// according to fingerprint of the predicate and sends it to that instance.
func MutateOverNetwork(ctx context.Context, m *task.Mutations) error {
mutationMap := make(map[uint32]*task.Mutations)
addToMutationMap(mutationMap, m.Edges)
errors := make(chan error, len(mutationMap))
for gid, mu := range mutationMap {
proposeOrSend(ctx, gid, mu, errors)
}
// Wait for all the goroutines to reply back.
// We return if an error was returned or the parent called ctx.Done()
for i := 0; i < len(mutationMap); i++ {
select {
case err := <-errors:
if err != nil {
x.TraceError(ctx, x.Wrapf(err, "Error while running all mutations"))
return err
}
case <-ctx.Done():
return ctx.Err()
}
}
close(errors)
return nil
}
// Mutate is used to apply mutations over the network on other instances.
func (w *grpcWorker) Mutate(ctx context.Context, m *task.Mutations) (*Payload, error) {
if ctx.Err() != nil {
return &Payload{}, ctx.Err()
}
if !groups().ServesGroup(m.GroupId) {
return &Payload{}, x.Errorf("This server doesn't serve group id: %v", m.GroupId)
}
c := make(chan error, 1)
node := groups().Node(m.GroupId)
go func() { c <- node.ProposeAndWait(ctx, &task.Proposal{Mutations: m}) }()
select {
case <-ctx.Done():
return &Payload{}, ctx.Err()
case err := <-c:
return &Payload{}, err
}
}