forked from dgraph-io/dgraph
/
schema.go
210 lines (191 loc) · 5.99 KB
/
schema.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
/*
* 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 (
"golang.org/x/net/context"
"golang.org/x/net/trace"
"github.com/dgraph-io/dgraph/conn"
"github.com/dgraph-io/dgraph/protos/api"
"github.com/dgraph-io/dgraph/protos/intern"
"github.com/dgraph-io/dgraph/schema"
"github.com/dgraph-io/dgraph/types"
"github.com/dgraph-io/dgraph/x"
)
var (
emptySchemaResult intern.SchemaResult
)
type resultErr struct {
result *intern.SchemaResult
err error
}
// getSchema iterates over all predicates and populates the asked fields, if list of
// predicates is not specified, then all the predicates belonging to the group
// are returned
func getSchema(ctx context.Context, s *intern.SchemaRequest) (*intern.SchemaResult, error) {
var result intern.SchemaResult
var predicates []string
var fields []string
if len(s.Predicates) > 0 {
predicates = s.Predicates
} else {
predicates = schema.State().Predicates()
}
if len(s.Fields) > 0 {
fields = s.Fields
} else {
fields = []string{"type", "index", "tokenizer", "reverse", "count", "list"}
}
for _, attr := range predicates {
// This can happen after a predicate is moved. We don't delete predicate from schema state
// immediately. So lets ignore this predicate.
if !groups().ServesTablet(attr) {
continue
}
if schemaNode := populateSchema(attr, fields); schemaNode != nil {
result.Schema = append(result.Schema, schemaNode)
}
}
return &result, nil
}
// populateSchema returns the information of asked fields for given attribute
func populateSchema(attr string, fields []string) *api.SchemaNode {
var schemaNode api.SchemaNode
var typ types.TypeID
var err error
if typ, err = schema.State().TypeOf(attr); err != nil {
// schema is not defined
return nil
}
schemaNode.Predicate = attr
for _, field := range fields {
switch field {
case "type":
schemaNode.Type = typ.Name()
case "index":
schemaNode.Index = schema.State().IsIndexed(attr)
case "tokenizer":
if schema.State().IsIndexed(attr) {
schemaNode.Tokenizer = schema.State().TokenizerNames(attr)
}
case "reverse":
schemaNode.Reverse = schema.State().IsReversed(attr)
case "count":
schemaNode.Count = schema.State().HasCount(attr)
case "list":
schemaNode.List = schema.State().IsList(attr)
default:
//pass
}
}
return &schemaNode
}
// addToSchemaMap groups the predicates by group id, if list of predicates is
// empty then it adds all known groups
func addToSchemaMap(schemaMap map[uint32]*intern.SchemaRequest, schema *intern.SchemaRequest) {
for _, attr := range schema.Predicates {
gid := groups().BelongsTo(attr)
s := schemaMap[gid]
if s == nil {
s = &intern.SchemaRequest{GroupId: gid}
s.Fields = schema.Fields
schemaMap[gid] = s
}
s.Predicates = append(s.Predicates, attr)
}
if len(schema.Predicates) > 0 {
return
}
// TODO: Janardhan - node shouldn't serve any request until membership
// information is synced, should we fail health check till then ?
gids := groups().KnownGroups()
for _, gid := range gids {
if gid == 0 {
continue
}
s := schemaMap[gid]
if s == nil {
s = &intern.SchemaRequest{GroupId: gid}
s.Fields = schema.Fields
schemaMap[gid] = s
}
}
}
// If the current node serves the group serve the schema or forward
// to relevant node
// TODO: Janardhan - if read fails try other servers serving same group
func getSchemaOverNetwork(ctx context.Context, gid uint32, s *intern.SchemaRequest, ch chan resultErr) {
if groups().ServesGroup(gid) {
schema, e := getSchema(ctx, s)
ch <- resultErr{result: schema, err: e}
return
}
pl := groups().Leader(gid)
if pl == nil {
ch <- resultErr{err: conn.ErrNoConnection}
return
}
conn := pl.Get()
c := intern.NewWorkerClient(conn)
schema, e := c.Schema(ctx, s)
ch <- resultErr{result: schema, err: e}
}
// GetSchemaOverNetwork checks which group should be serving the schema
// according to fingerprint of the predicate and sends it to that instance.
func GetSchemaOverNetwork(ctx context.Context, schema *intern.SchemaRequest) ([]*api.SchemaNode, error) {
if err := x.HealthCheck(); err != nil {
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("Request rejected %v", err)
}
return nil, err
}
// Map of groupd id => Predicates for that group.
schemaMap := make(map[uint32]*intern.SchemaRequest)
addToSchemaMap(schemaMap, schema)
results := make(chan resultErr, len(schemaMap))
var schemaNodes []*api.SchemaNode
for gid, s := range schemaMap {
if gid == 0 {
return schemaNodes, errUnservedTablet
}
go getSchemaOverNetwork(ctx, gid, s, results)
}
// 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(schemaMap); i++ {
select {
case r := <-results:
if r.err != nil {
return nil, r.err
}
schemaNodes = append(schemaNodes, r.result.Schema...)
case <-ctx.Done():
return nil, ctx.Err()
}
}
close(results)
return schemaNodes, nil
}
// Schema is used to get schema information over the network on other instances.
func (w *grpcWorker) Schema(ctx context.Context, s *intern.SchemaRequest) (*intern.SchemaResult, error) {
if ctx.Err() != nil {
return &emptySchemaResult, ctx.Err()
}
if !groups().ServesGroup(s.GroupId) {
return &emptySchemaResult, x.Errorf("This server doesn't serve group id: %v", s.GroupId)
}
return getSchema(ctx, s)
}