Skip to content

feat(lambda): store lambda scripts within the dgraph, send it to JS s… #7955

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

Merged
merged 6 commits into from
Jul 27, 2021

Conversation

NamanJain8
Copy link
Contributor

@NamanJain8 NamanJain8 commented Jul 19, 2021

Related to dgraph-io/dgraph-lambda#22

This PR changes the way lambda is handled by Dgraph. Earlier, the user had to start the server with a predefined lambda script. This does not scale well in a multi-tenant environment. As we will need N servers (or endpoints) to handle the load. Also, each time the script is changed, the lambda server needs to be restarted.

With this change, instead of loading the script from the predefined file, we will be sending the script in the request body to the lambda-server.

The details are mentioned in the discuss post. To summarize:

Storage

We will store the lambda script within dgraph.graphql.schema predicate. The value will be JSON marshal of

type GQL struct {
   Script:     String
   Schema: String
}

This change has been done in a backward-compatible manner.

New resolvers

This adds 2 new admin resolvers:

  • UpdateLambdaScript → accepts base64 encoded lambda script
  • GetLambdaScript → Returns base64 encoded script
mutation ($script: String!) {
  updateLambdaScript(input:{set:{script:$script}}) {
     lambdaScript{
      script
    }
  }
}
query {
  getLambdaScript {
    script
  }
}

Export/Import:

The GraphQL schema is exported in a separate file in the following JSON format:
[ExportedGQL] where ExportedGQL type is {Namespace: Int, Schema: String}. We will extend it to {Namespace: Int, Schema: String, Script: String} and hence the bulk loader can handle it properly.


This change is Reviewable

@github-actions github-actions bot added area/bulk-loader Issues related to bulk loading. area/graphql Issues related to GraphQL support on Dgraph. labels Jul 19, 2021
Copy link
Contributor

@manishrjain manishrjain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm: Had a rough look, looks good. Get a deeper review by @aman-bansal / @ahsanbarkati

Reviewed 12 of 12 files at r1.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @NamanJain8)


protos/pb.proto, line 743 at r1 (raw file):

message UpdateGraphQLSchemaRequest {
  enum Op {
    SCHEMA = 0;

Do you need the Op?

Copy link
Contributor Author

@NamanJain8 NamanJain8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @manishrjain)


protos/pb.proto, line 743 at r1 (raw file):

Previously, manishrjain (Manish R Jain) wrote…

Do you need the Op?

Op is needed because update the schema to an empty string, then we won't be able to decide the type of update on the receiver.

@NamanJain8 NamanJain8 marked this pull request as ready for review July 22, 2021 10:55
Copy link
Contributor

@ahsanbarkati ahsanbarkati left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm: Looks good to me. Few nits.

Reviewable status: 8 of 20 files reviewed, 5 unresolved discussions (waiting on @manishrjain and @NamanJain8)


edgraph/server.go, line 500 at r3 (raw file):

		// just reinsert the GraphQL schema, no need to alter dgraph schema as this was drop_data
		if _, e := UpdateGQLSchema(ctx, graphQLSchema, ""); e != nil {
			err = errors.Errorf("While updating gql schema, got err: %+v.", e)

If e is not nil then why not return here itself? Why do we need to accumulate the errors?


worker/lambda_script.go, line 31 at r3 (raw file):

type LambdaScriptStore struct {
	mux    sync.RWMutex

Can we avoid the variable mux, it will make it look cleaner.

type LambdaScriptStore struct {
  sync.RQMutex
  ...
}

Then directly do ls.Lock() instead of ls.mux.Lock()

Copy link
Contributor Author

@NamanJain8 NamanJain8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 7 of 20 files reviewed, 5 unresolved discussions (waiting on @ahsanbarkati and @manishrjain)


edgraph/server.go, line 500 at r3 (raw file):

Previously, ahsanbarkati (Ahsan Barkati) wrote…

If e is not nil then why not return here itself? Why do we need to accumulate the errors?

Done.


worker/lambda_script.go, line 31 at r3 (raw file):

Previously, ahsanbarkati (Ahsan Barkati) wrote…

Can we avoid the variable mux, it will make it look cleaner.

type LambdaScriptStore struct {
  sync.RQMutex
  ...
}

Then directly do ls.Lock() instead of ls.mux.Lock()

Done. Thanks.

@NamanJain8 NamanJain8 merged commit b1266d0 into master Jul 27, 2021
@NamanJain8 NamanJain8 deleted the naman/lambda-new branch July 27, 2021 13:48
NamanJain8 added a commit that referenced this pull request Jul 27, 2021
#7955)

Related to dgraph-io/dgraph-lambda#22

This PR changes the way lambda is handled by Dgraph. Earlier, the user had to start the server with a predefined lambda script. This does not scale well in a multi-tenant environment. As we will need N servers (or endpoints) to handle the load. Also, each time the script is changed, the lambda server needs to be restarted.

With this change, instead of loading the script from the predefined file, we will be sending the script in the request body to the lambda-server.

The details are mentioned in the discuss post. To summarize:

Storage
We will store the lambda script within dgraph.graphql.schema predicate. The value will be JSON marshal of

type GQL struct {
   Script:     String
   Schema: String
}
This change has been done in a backward-compatible manner.

New resolvers
This adds 2 new admin resolvers:

UpdateLambdaScript → accepts base64 encoded lambda script
GetLambdaScript → Returns base64 encoded script
Export/Import:
The GraphQL schema is exported in a separate file in the following JSON format:
[ExportedGQL] where ExportedGQL type is {Namespace: Int, Schema: String}. We will extend it to {Namespace: Int, Schema: String, Script: String} and hence the bulk loader can handle it properly.

(cherry picked from commit b1266d0)
joshua-goldstein added a commit that referenced this pull request Nov 23, 2022
… (#8442)

Cherry pick from #8014 

In #7955 the team made a change to the way lambda scripts were loaded
into Dgraph. This PR had a small conflict with that PR. Any change
related to #7955 should be made in another PR.

Co-authored-by: Naman Jain <naman@dgraph.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/bulk-loader Issues related to bulk loading. area/graphql Issues related to GraphQL support on Dgraph.
Development

Successfully merging this pull request may close these issues.

3 participants