Skip to content

LambdaRuntimeClient works with Invocation #19

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 1 commit into from
Mar 8, 2020
Merged

Conversation

fabianfett
Copy link
Member

@fabianfett fabianfett commented Mar 7, 2020

Motivation:

  • We want to store different entities that are needed when executing a handler within the LambdaContext (Logger, EventLoop, ByteBufferAllocator, …)
  • Currently the LambdaRuntimeClient creates the LambdaContext. Having the LambdaContext with the Logger, EventLoop and ByteBufferAllocator be created from the LambdaRuntimeClient feels too much for me.
  • Conceptionally the Lambda control plane api call is “get next Invocation” (API naming)

Changes:

  • LambdaRuntimeClient responds with an Invocation and does not use the LambdaContext at all anymore.
  • LambdaRunner creates the LambdaContext with the Invocation and Logger (easy to add EventLoop and ByteBufferAllocator – not for this PR).
  • LambdaContext has been renamed to Lambda.Context
  • Lambda.Context has become a class, since it is conceptually not a value type and might be passed around a lot
  • Lambda.Context properties traceId, invokedFunctionArn, deadline are not optional anymore since they will be always set when executing a lambda
  • Creating an Invocation can fail with LambdaRuntimeClientError.invocationMissingHeader(String), if non optional headers are not present
  • the test MockLambdaServer and the performance test MockServer always return headers for deadline, traceId and function arn (static for now – could be changed with Behaviour flag – new PR?). This leads to decreased performance in performance tests, but is probably closer to real life behaviour.

Open ends:

  • we will need to build some kind of Deadline into the context (See also Replacing Date #9 - probably for a different PR)
  • we have a stupid mapping between ByteBuffer and [UInt8] in the LambdaRunner for now (marked with two TODOs). I don’t want to change this in this PR since it will lead to huge merge conflicts down the road with the potentiall API changes we have in mind.

@fabianfett fabianfett requested a review from tomerd March 7, 2020 15:07
@swift-server-bot
Copy link

Can one of the admins verify this patch?

1 similar comment
@swift-server-bot
Copy link

Can one of the admins verify this patch?

@fabianfett fabianfett changed the title WIP: LambdaRuntimeClient works with Invocation [DNM] LambdaRuntimeClient works with Invocation Mar 7, 2020
Copy link
Contributor

@tomerd tomerd left a comment

Choose a reason for hiding this comment

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

+1 i like this change!

@tomerd
Copy link
Contributor

tomerd commented Mar 7, 2020

@swift-server-bot add to whitelist

@tomerd
Copy link
Contributor

tomerd commented Mar 7, 2020

@swift-server-bot test this please

@fabianfett fabianfett changed the title [DNM] LambdaRuntimeClient works with Invocation LambdaRuntimeClient works with Invocation Mar 7, 2020
@@ -117,6 +118,20 @@ private extension LambdaHandler {
}
}

private extension LambdaContext {
init(logger: Logger, eventLoop: EventLoop, invocation: Invocation) {

Copy link
Contributor

@tomerd tomerd Mar 7, 2020

Choose a reason for hiding this comment

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

nit: remove empty line

Copy link
Member Author

Choose a reason for hiding this comment

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

swiftformat . has been run.

@@ -151,6 +149,7 @@ internal struct JsonCodecError: Error, Equatable {
internal struct ErrorResponse: Codable {
var errorType: String
var errorMessage: String

Copy link
Contributor

@tomerd tomerd Mar 7, 2020

Choose a reason for hiding this comment

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

remove empty line (use swiftformat .)

Copy link
Member Author

Choose a reason for hiding this comment

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

swiftformat . has been run.

let cognitoIdentity : String?

init(headers: HTTPHeaders) throws {

Copy link
Contributor

Choose a reason for hiding this comment

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

empty line

Copy link
Member Author

Choose a reason for hiding this comment

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

swiftformat . has been run.

if requestId.isEmpty {
return nil

guard let unixTimeMilliseconds = headers[AmazonHeaders.deadline].first else {
Copy link
Contributor

Choose a reason for hiding this comment

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

is it mandatory in AWS Lambda API, and if so should the lambda library enforce it? probably for different PR if the answer is yes

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, the deadline is mandatory and we should enforce it. As said in #9 we should find a good solution how we store time here. The Context should have some kind of remaining() function that returns the time till the deadline. Also creating a timeout handler that fires a second before the actual deadline is thinkable. As you stated this is something for a different PR though.

}

Copy link
Contributor

Choose a reason for hiding this comment

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

empty lines

Copy link
Member Author

Choose a reason for hiding this comment

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

swiftformat . has been run.

self.deadlineDate = unixTimeMilliseconds
self.invokedFunctionArn = invokedFunctionArn
self.traceId = traceId
self.clientContext = headers["Lambda-Runtime-Client-Context"].first
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure but maybe headers.first("Lambda-Runtime-Client-Context") is optimized?

@tomerd
Copy link
Contributor

tomerd commented Mar 7, 2020

@swift-server-bot test this please

@tomerd
Copy link
Contributor

tomerd commented Mar 8, 2020

@swift-server-bot ok to test

@tomerd
Copy link
Contributor

tomerd commented Mar 8, 2020

ah this is not triggering CI because its targeting the api branch instead of master. should we take this into master instead?

@tomerd
Copy link
Contributor

tomerd commented Mar 8, 2020

Open ends:

we will need to build some kind of Deadline into the context (See also #9 - probably for a different PR)

Invocation is currently a struct and I'd assume copied four times with six internal Strings. Maybe we can make this faster by using an internal Storage class. (potentially 4 ref counts vs 24).

Invocation should be immutable, right? so reference could work imo

I don’t know if Context really is a value type? Especially if we now add Logger, EventLoop, ByteBufferAllocator to the Context. grpc-swift uses a class for its context:
https://github.com/grpc/grpc-swift/blob/nio/Sources/GRPC/ServerCallContexts/ServerCallContext.swift

same argument as invocation?

What do you think about namespacing LambdaContext within Lambda so that we only have Context. Or when used external Lambda.Context

+1 was planning to do that

@fabianfett fabianfett changed the base branch from api to master March 8, 2020 11:43
@fabianfett fabianfett force-pushed the ff-api-invocation branch 2 times, most recently from d59d850 to a7aa066 Compare March 8, 2020 11:55
@fabianfett
Copy link
Member Author

@tomerd This is now a PR against master. Ready for final review and merge.

@fabianfett fabianfett mentioned this pull request Mar 8, 2020
@tomerd
Copy link
Contributor

tomerd commented Mar 8, 2020

@fabianfett lets rebase

### Motivation:

- We want to store different entities that are needed when executing a handler within the LambdaContext (Logger, EventLoop, ByteBufferAllocator, …)
- Currently the LambdaRuntimeClient creates the LambdaContext. Having the LambdaContext with the Logger, EventLoop and ByteBufferAllocator be created from the LambdaRuntimeClient feels to me too much for me.
- Conceptionally the Lambda control plane api call is “get next Invocation” (API naming)

### Changes:

- LambdaRuntimeClient responds with an Invocation and does not use the LambdaContext at all anymore.
- LambdaRunner creates the LambdaContext with the Invocation, Logger and EventLoop.
- LambdaContext has been renamed to Lambda.Context
- Lambda.Context is a class now, since it is conceptionally not a value type and might be passed around a lot
- Lambda.Context properties `traceId`, `invokedFunctionArn`, `deadline` are not optional anymore since they will be always set when executing a lambda
- Creating an Invocation can fail with LambdaRuntimeClientError.invocationMissingHeader(String), if non optional headers are not present
- the test MockLambdaServer and the performance test MockServer always return headers for deadline, traceId and function arn (static for now – could be changed with Behaviour flag?!)

### Open ends:

- we will need to build some kind of Deadline into the context (See also #9 - probably for a different PR)
- we have a stupid mapping between ByteBuffer and [UInt8] in the LambdaRunner for now (marked with two TODOs). I don’t want to change this in this PR since it will lead to huge merge conflicts down the road with the potentiall API changes we have in mind.
@fabianfett
Copy link
Member Author

@tomerd done

@tomerd tomerd merged commit ddee38c into master Mar 8, 2020
@tomerd tomerd deleted the ff-api-invocation branch March 10, 2020 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants