Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(GraphQL): Generate GraphQL query response by optimized JSON encoding (GraphQL-730) #7371

Merged
merged 20 commits into from
Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
5c70eb4
perf(GraphQL): JSON Part-1: JSON is generated by Dgraph for simple qu…
abhimanyusinghgaur Jan 4, 2021
6b761e6
perf(GraphQL): JSON Part-2: JSON is generated by Dgraph for simple mu…
abhimanyusinghgaur Jan 4, 2021
55ae164
perf(GraphQL): JSON Part-3: Geo queries (GRAPHQL-879) (#7238)
abhimanyusinghgaur Jan 5, 2021
3748289
perf(GraphQL): JSON Part-4: Aggregate queries at root and child level…
abhimanyusinghgaur Jan 5, 2021
5c4f358
Merge branch 'master' of github.com:dgraph-io/dgraph into abhimanyu/g…
abhimanyusinghgaur Jan 5, 2021
1ea6d72
review comments from merged PRs
abhimanyusinghgaur Jan 5, 2021
8f6eada
Merge branch 'master' of github.com:dgraph-io/dgraph into abhimanyu/g…
abhimanyusinghgaur Jan 6, 2021
01a617a
Merge branch 'master' of github.com:dgraph-io/dgraph into abhimanyu/g…
abhimanyusinghgaur Jan 8, 2021
c95c464
perf(GraphQL): JSON Part-5: Change query rewriting for scalar/object …
abhimanyusinghgaur Jan 13, 2021
be73dc3
Merge branch 'master' of github.com:dgraph-io/dgraph into abhimanyu/g…
abhimanyusinghgaur Jan 14, 2021
848b07f
perf(GraphQL): JSON Part-6: Admin Server and custom HTTP query/mutati…
abhimanyusinghgaur Jan 18, 2021
1ebb53b
Merge branch 'master' of github.com:dgraph-io/dgraph into abhimanyu/g…
abhimanyusinghgaur Jan 18, 2021
38a377b
perf(GraphQL): JSON Part-7: JSON is generated by Dgraph for custom HT…
abhimanyusinghgaur Jan 27, 2021
45a2270
perf(GraphQL): JSON part-8: Cleanup and comments (GRAPHQL-947) (#7376)
abhimanyusinghgaur Jan 28, 2021
87251b4
Merge branch 'master' of github.com:dgraph-io/dgraph into abhimanyu/g…
abhimanyusinghgaur Jan 28, 2021
e6e33ef
fix merge
abhimanyusinghgaur Jan 28, 2021
3ef44e6
review comments
abhimanyusinghgaur Jan 29, 2021
2632dbd
fix tests
abhimanyusinghgaur Jan 29, 2021
be098f1
Merge branch 'master' of github.com:dgraph-io/dgraph into abhimanyu/g…
abhimanyusinghgaur Jan 29, 2021
51f7abb
fix more tests
abhimanyusinghgaur Jan 29, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dgraph/cmd/alpha/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func processToFastJSON(q string) string {
log.Fatal(err)
}

buf, err := query.ToJson(&l, qr.Subgraphs)
buf, err := query.ToJson(context.Background(), &l, qr.Subgraphs, nil)
if err != nil {
log.Fatal(err)
}
Expand Down
64 changes: 38 additions & 26 deletions edgraph/access_ee.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,15 @@ func authorizeUser(ctx context.Context, userid string, password string) (
"$userid": userid,
"$password": password,
}
queryRequest := api.Request{
Query: queryUser,
Vars: queryVars,
req := &Request{
req: &api.Request{
Query: queryUser,
Vars: queryVars,
},
doAuth: NoAuthorize,
}

queryResp, err := (&Server{}).doQuery(ctx, &queryRequest, NoAuthorize)
queryResp, err := (&Server{}).doQuery(ctx, req)
if err != nil {
glog.Errorf("Error while query user with id %s: %v", userid, err)
return nil, err
Expand Down Expand Up @@ -326,13 +329,16 @@ func RefreshAcls(closer *z.Closer) {
maxRefreshTs = refreshTs

glog.V(3).Infof("Refreshing ACLs")
queryRequest := api.Request{
Query: queryAcls,
ReadOnly: true,
StartTs: refreshTs,
req := &Request{
req: &api.Request{
Query: queryAcls,
ReadOnly: true,
StartTs: refreshTs,
},
doAuth: NoAuthorize,
}

queryResp, err := (&Server{}).doQuery(closer.Ctx(), &queryRequest, NoAuthorize)
queryResp, err := (&Server{}).doQuery(closer.Ctx(), req)
if err != nil {
return errors.Errorf("unable to retrieve acls: %v", err)
}
Expand Down Expand Up @@ -406,18 +412,21 @@ func ResetAcl(closer *z.Closer) {
}
`, x.GuardiansId)
groupNQuads := acl.CreateGroupNQuads(x.GuardiansId)
req := &api.Request{
CommitNow: true,
Query: query,
Mutations: []*api.Mutation{
{
Set: groupNQuads,
Cond: "@if(eq(len(guid), 0))",
req := &Request{
req: &api.Request{
CommitNow: true,
Query: query,
Mutations: []*api.Mutation{
{
Set: groupNQuads,
Cond: "@if(eq(len(guid), 0))",
},
},
},
doAuth: NoAuthorize,
}

resp, err := (&Server{}).doQuery(ctx, req, NoAuthorize)
resp, err := (&Server{}).doQuery(ctx, req)

// Structs to parse guardians group uid from query response
type groupNode struct {
Expand Down Expand Up @@ -474,19 +483,22 @@ func ResetAcl(closer *z.Closer) {
Predicate: "dgraph.user.group",
ObjectId: "uid(guid)",
})
req := &api.Request{
CommitNow: true,
Query: query,
Mutations: []*api.Mutation{
{
Set: userNQuads,
// Assuming that if groot exists, it is in guardian group
Cond: "@if(eq(len(grootid), 0) and gt(len(guid), 0))",
req := &Request{
req: &api.Request{
CommitNow: true,
Query: query,
Mutations: []*api.Mutation{
{
Set: userNQuads,
// Assuming that if groot exists, it is in guardian group
Cond: "@if(eq(len(grootid), 0) and gt(len(guid), 0))",
},
},
},
doAuth: NoAuthorize,
}

resp, err := (&Server{}).doQuery(ctx, req, NoAuthorize)
resp, err := (&Server{}).doQuery(ctx, req)
if err != nil {
return errors.Wrapf(err, "while upserting user with id %s", x.GrootId)
}
Expand Down
171 changes: 95 additions & 76 deletions edgraph/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/dgraph-io/dgraph/gql"
"sort"
"time"

"github.com/dgraph-io/dgraph/gql"

"github.com/dgraph-io/dgo/v200/protos/api"
"github.com/dgraph-io/dgraph/graphql/schema"
"github.com/dgraph-io/ristretto/z"
Expand All @@ -42,36 +43,39 @@ func ResetCors(closer *z.Closer) {
closer.Done()
}()

req := &api.Request{
Query: `query{
req := &Request{
req: &api.Request{
Query: `query{
cors as var(func: has(dgraph.cors))
}`,
Mutations: []*api.Mutation{
{
Set: []*api.NQuad{
{
Subject: "_:a",
Predicate: "dgraph.cors",
ObjectValue: &api.Value{Val: &api.Value_StrVal{StrVal: "*"}},
},
{
Subject: "_:a",
Predicate: "dgraph.type",
ObjectValue: &api.Value{Val: &api.Value_StrVal{
StrVal: "dgraph.type.cors"}},
Mutations: []*api.Mutation{
{
Set: []*api.NQuad{
{
Subject: "_:a",
Predicate: "dgraph.cors",
ObjectValue: &api.Value{Val: &api.Value_StrVal{StrVal: "*"}},
},
{
Subject: "_:a",
Predicate: "dgraph.type",
ObjectValue: &api.Value{Val: &api.Value_StrVal{
StrVal: "dgraph.type.cors"}},
},
},
Cond: `@if(eq(len(cors), 0))`,
},
Cond: `@if(eq(len(cors), 0))`,
},
CommitNow: true,
},
CommitNow: true,
doAuth: NoAuthorize,
}

for closer.Ctx().Err() == nil {
ctx, cancel := context.WithTimeout(closer.Ctx(), time.Minute)
defer cancel()
ctx = context.WithValue(ctx, IsGraphql, true)
if _, err := (&Server{}).doQuery(ctx, req, NoAuthorize); err != nil {
if _, err := (&Server{}).doQuery(ctx, req); err != nil {
glog.Infof("Unable to upsert cors. Error: %v", err)
time.Sleep(100 * time.Millisecond)
}
Expand All @@ -93,35 +97,41 @@ func AddCorsOrigins(ctx context.Context, origins []string) error {
if err != nil {
return err
}
req := &api.Request{
Query: `query{
req := &Request{
req: &api.Request{
Query: `query{
cors as var(func: has(dgraph.cors))
}`,
Mutations: []*api.Mutation{
{
SetNquads: generateNquadsForCors(uid, origins),
Cond: `@if(gt(len(cors), 0))`,
DelNquads: []byte(`<` + uid + `>` + ` <dgraph.cors> * .`),
Mutations: []*api.Mutation{
{
SetNquads: generateNquadsForCors(uid, origins),
Cond: `@if(gt(len(cors), 0))`,
DelNquads: []byte(`<` + uid + `>` + ` <dgraph.cors> * .`),
},
},
CommitNow: true,
},
CommitNow: true,
doAuth: NoAuthorize,
}
_, err = (&Server{}).doQuery(context.WithValue(ctx, IsGraphql, true), req, NoAuthorize)
_, err = (&Server{}).doQuery(context.WithValue(ctx, IsGraphql, true), req)
return err
}

// GetCorsOrigins retrieve all the cors origin from the database.
func GetCorsOrigins(ctx context.Context) (string, []string, error) {
req := &api.Request{
Query: `query{
req := &Request{
req: &api.Request{
Query: `query{
me(func: has(dgraph.cors)){
uid
dgraph.cors
}
}`,
ReadOnly: true,
ReadOnly: true,
},
doAuth: NoAuthorize,
}
res, err := (&Server{}).doQuery(context.WithValue(ctx, IsGraphql, true), req, NoAuthorize)
res, err := (&Server{}).doQuery(context.WithValue(ctx, IsGraphql, true), req)
if err != nil {
return "", nil, err
}
Expand Down Expand Up @@ -160,29 +170,32 @@ func GetCorsOrigins(ctx context.Context) (string, []string, error) {

// UpdateSchemaHistory updates graphql schema history.
func UpdateSchemaHistory(ctx context.Context, schema string) error {
req := &api.Request{
Mutations: []*api.Mutation{
{
Set: []*api.NQuad{
{
Subject: "_:a",
Predicate: "dgraph.graphql.schema_history",
ObjectValue: &api.Value{Val: &api.Value_StrVal{StrVal: schema}},
},
{
Subject: "_:a",
Predicate: "dgraph.type",
ObjectValue: &api.Value{Val: &api.Value_StrVal{
StrVal: "dgraph.graphql.history"}},
req := &Request{
req: &api.Request{
Mutations: []*api.Mutation{
{
Set: []*api.NQuad{
{
Subject: "_:a",
Predicate: "dgraph.graphql.schema_history",
ObjectValue: &api.Value{Val: &api.Value_StrVal{StrVal: schema}},
},
{
Subject: "_:a",
Predicate: "dgraph.type",
ObjectValue: &api.Value{Val: &api.Value_StrVal{
StrVal: "dgraph.graphql.history"}},
},
},
SetNquads: []byte(fmt.Sprintf(`_:a <dgraph.graphql.schema_created_at> "%s" .`,
time.Now().Format(time.RFC3339))),
},
SetNquads: []byte(fmt.Sprintf(`_:a <dgraph.graphql.schema_created_at> "%s" .`,
time.Now().Format(time.RFC3339))),
},
CommitNow: true,
},
CommitNow: true,
doAuth: NoAuthorize,
}
_, err := (&Server{}).doQuery(context.WithValue(ctx, IsGraphql, true), req, NoAuthorize)
_, err := (&Server{}).doQuery(context.WithValue(ctx, IsGraphql, true), req)
return err
}

Expand Down Expand Up @@ -213,13 +226,16 @@ func ProcessPersistedQuery(ctx context.Context, gqlReq *schema.Request) error {
variables := map[string]string{
"$sha": sha256Hash,
}
req := &api.Request{
Query: queryForSHA,
Vars: variables,
ReadOnly: true,
req := &Request{
req: &api.Request{
Query: queryForSHA,
Vars: variables,
ReadOnly: true,
},
doAuth: NoAuthorize,
}

storedQuery, err := (&Server{}).doQuery(ctx, req, NoAuthorize)
storedQuery, err := (&Server{}).doQuery(ctx, req)

if err != nil {
glog.Errorf("Error while querying sha %s", sha256Hash)
Expand Down Expand Up @@ -249,33 +265,36 @@ func ProcessPersistedQuery(ctx context.Context, gqlReq *schema.Request) error {
return errors.New("provided sha does not match query")
}

req := &api.Request{
Mutations: []*api.Mutation{
{
Set: []*api.NQuad{
{
Subject: "_:a",
Predicate: "dgraph.graphql.p_query",
ObjectValue: &api.Value{Val: &api.Value_StrVal{StrVal: query}},
},
{
Subject: "_:a",
Predicate: "dgraph.graphql.p_sha256hash",
ObjectValue: &api.Value{Val: &api.Value_StrVal{StrVal: sha256Hash}},
},
{
Subject: "_:a",
Predicate: "dgraph.type",
ObjectValue: &api.Value{Val: &api.Value_StrVal{
StrVal: "dgraph.graphql.persisted_query"}},
req = &Request{
req: &api.Request{
Mutations: []*api.Mutation{
{
Set: []*api.NQuad{
{
Subject: "_:a",
Predicate: "dgraph.graphql.p_query",
ObjectValue: &api.Value{Val: &api.Value_StrVal{StrVal: query}},
},
{
Subject: "_:a",
Predicate: "dgraph.graphql.p_sha256hash",
ObjectValue: &api.Value{Val: &api.Value_StrVal{StrVal: sha256Hash}},
},
{
Subject: "_:a",
Predicate: "dgraph.type",
ObjectValue: &api.Value{Val: &api.Value_StrVal{
StrVal: "dgraph.graphql.persisted_query"}},
},
},
},
},
CommitNow: true,
},
CommitNow: true,
doAuth: NoAuthorize,
}

_, err := (&Server{}).doQuery(context.WithValue(ctx, IsGraphql, true), req, NoAuthorize)
_, err := (&Server{}).doQuery(context.WithValue(ctx, IsGraphql, true), req)
return err

}
Expand Down
Loading