-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add FaaS (AWS Lambda) test app #1183
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/bin/bash | ||
|
||
set -o xtrace # Write all commands first to stderr | ||
set -o errexit # Exit the script with error if any of the commands fail | ||
|
||
RELATIVE_DIR_PATH="$(dirname "${BASH_SOURCE[0]:-$0}")" | ||
. "${RELATIVE_DIR_PATH}/javaConfig.bash" | ||
|
||
# compiled outside of lambda workflow. Note "SkipBuild: True" in template.yaml | ||
./gradlew -version | ||
./gradlew --info driver-lambda:shadowJar | ||
|
||
. ${DRIVERS_TOOLS}/.evergreen/aws_lambda/run-deployed-lambda-aws-tests.sh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vbabanin marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, 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. | ||
*/ | ||
|
||
buildscript { | ||
repositories { | ||
maven { url "https://plugins.gradle.org/m2/" } | ||
} | ||
dependencies { | ||
classpath 'com.github.jengelman.gradle.plugins:shadow:6.1.0' | ||
} | ||
} | ||
|
||
plugins { | ||
id("application") | ||
} | ||
|
||
apply plugin: 'application' | ||
apply plugin: 'com.github.johnrengelman.shadow' | ||
apply plugin: 'java' | ||
|
||
compileJava { | ||
sourceCompatibility = JavaVersion.VERSION_11 | ||
targetCompatibility = JavaVersion.VERSION_11 | ||
} | ||
|
||
mainClassName = "com.mongodb.workload.WorkloadExecutor" | ||
|
||
sourceSets { | ||
main { | ||
java { | ||
srcDir 'src/main' | ||
} | ||
resources { | ||
srcDir 'src/resources' | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation project(':driver-sync') | ||
implementation project(':bson') | ||
|
||
implementation('com.amazonaws:aws-lambda-java-core:1.2.2') | ||
implementation('com.amazonaws:aws-lambda-java-events:3.11.1') | ||
} | ||
|
||
|
||
javadoc { | ||
enabled = false | ||
} | ||
|
||
jar { | ||
manifest { | ||
attributes "Main-Class": "com.mongodb.lambdatest.LambdaTestApp" | ||
} | ||
} | ||
|
||
shadowJar { | ||
archiveBaseName.set('lambdatest') | ||
archiveVersion.set('') | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# More information about the configuration file can be found here: | ||
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html | ||
version = 0.1 | ||
|
||
[default] | ||
[default.global.parameters] | ||
stack_name = "lambdatest" | ||
|
||
[default.build.parameters] | ||
cached = true | ||
parallel = false | ||
|
||
[default.validate.parameters] | ||
lint = true | ||
|
||
[default.deploy.parameters] | ||
capabilities = "CAPABILITY_IAM" | ||
confirm_changeset = false # headless | ||
resolve_s3 = true | ||
|
||
[default.package.parameters] | ||
resolve_s3 = true | ||
|
||
[default.sync.parameters] | ||
watch = true | ||
|
||
[default.local_start_api.parameters] | ||
warm_containers = "EAGER" | ||
|
||
[default.local_start_lambda.parameters] | ||
warm_containers = "EAGER" |
154 changes: 154 additions & 0 deletions
154
driver-lambda/src/main/com/mongodb/lambdatest/LambdaTestApp.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, 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 com.mongodb.lambdatest; | ||
|
||
import com.amazonaws.services.lambda.runtime.Context; | ||
import com.amazonaws.services.lambda.runtime.RequestHandler; | ||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent; | ||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent; | ||
import com.mongodb.ConnectionString; | ||
import com.mongodb.MongoClientSettings; | ||
import com.mongodb.client.MongoClient; | ||
import com.mongodb.client.MongoClients; | ||
import com.mongodb.client.MongoCollection; | ||
import com.mongodb.event.CommandFailedEvent; | ||
import com.mongodb.event.CommandListener; | ||
import com.mongodb.event.CommandSucceededEvent; | ||
import com.mongodb.event.ConnectionClosedEvent; | ||
import com.mongodb.event.ConnectionCreatedEvent; | ||
import com.mongodb.event.ConnectionPoolListener; | ||
import com.mongodb.event.ServerHeartbeatFailedEvent; | ||
import com.mongodb.event.ServerHeartbeatSucceededEvent; | ||
import com.mongodb.event.ServerMonitorListener; | ||
import com.mongodb.lang.NonNull; | ||
import org.bson.BsonDocument; | ||
import org.bson.BsonInt64; | ||
import org.bson.BsonString; | ||
import org.bson.BsonValue; | ||
import org.bson.Document; | ||
|
||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static java.util.concurrent.TimeUnit.MILLISECONDS; | ||
|
||
/** | ||
* Test App for AWS lambda functions | ||
*/ | ||
public class LambdaTestApp implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { | ||
private final MongoClient mongoClient; | ||
private long openConnections = 0; | ||
private long totalHeartbeatCount = 0; | ||
private long totalHeartbeatDurationMs = 0; | ||
private long totalCommandCount = 0; | ||
private long totalCommandDurationMs = 0; | ||
|
||
public LambdaTestApp() { | ||
String connectionString = System.getenv("MONGODB_URI"); | ||
|
||
MongoClientSettings settings = MongoClientSettings.builder() | ||
.applyConnectionString(new ConnectionString(connectionString)) | ||
.addCommandListener(new CommandListener() { | ||
@Override | ||
public void commandSucceeded(@NonNull final CommandSucceededEvent event) { | ||
totalCommandCount++; | ||
totalCommandDurationMs += event.getElapsedTime(MILLISECONDS); | ||
} | ||
@Override | ||
public void commandFailed(@NonNull final CommandFailedEvent event) { | ||
totalCommandCount++; | ||
totalCommandDurationMs += event.getElapsedTime(MILLISECONDS); | ||
} | ||
}) | ||
.applyToServerSettings(builder -> builder.addServerMonitorListener(new ServerMonitorListener() { | ||
@Override | ||
public void serverHeartbeatSucceeded(@NonNull final ServerHeartbeatSucceededEvent event) { | ||
totalHeartbeatCount++; | ||
totalHeartbeatDurationMs += event.getElapsedTime(MILLISECONDS); | ||
} | ||
@Override | ||
public void serverHeartbeatFailed(@NonNull final ServerHeartbeatFailedEvent event) { | ||
totalHeartbeatCount++; | ||
totalHeartbeatDurationMs += event.getElapsedTime(MILLISECONDS); | ||
} | ||
})) | ||
.applyToConnectionPoolSettings(builder -> builder.addConnectionPoolListener(new ConnectionPoolListener() { | ||
@Override | ||
public void connectionCreated(@NonNull final ConnectionCreatedEvent event) { | ||
openConnections++; | ||
} | ||
@Override | ||
public void connectionClosed(@NonNull final ConnectionClosedEvent event) { | ||
openConnections--; | ||
} | ||
})) | ||
.build(); | ||
mongoClient = MongoClients.create(settings); | ||
} | ||
|
||
public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) { | ||
try { | ||
MongoCollection<Document> collection = mongoClient | ||
.getDatabase("lambdaTest") | ||
.getCollection("test"); | ||
BsonValue id = collection.insertOne(new Document("n", 1)).getInsertedId(); | ||
collection.deleteOne(new Document("_id", id)); | ||
|
||
BsonDocument responseBody = getBsonDocument(); | ||
|
||
return templateResponse() | ||
vbabanin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.withStatusCode(200) | ||
.withBody(responseBody.toJson()); | ||
|
||
} catch (Throwable e) { | ||
StringWriter sw = new StringWriter(); | ||
e.printStackTrace(new PrintWriter(sw)); | ||
BsonDocument responseBody = new BsonDocument() | ||
.append("throwable", new BsonString(e.getMessage())) | ||
.append("stacktrace", new BsonString(sw.toString())); | ||
return templateResponse() | ||
.withBody(responseBody.toJson()) | ||
.withStatusCode(500); | ||
} | ||
} | ||
|
||
private BsonDocument getBsonDocument() { | ||
BsonDocument responseBody = new BsonDocument() | ||
.append("totalCommandDurationMs", new BsonInt64(totalCommandDurationMs)) | ||
.append("totalCommandCount", new BsonInt64(totalCommandCount)) | ||
.append("totalHeartbeatDurationMs", new BsonInt64(totalHeartbeatDurationMs)) | ||
.append("totalHeartbeatCount", new BsonInt64(totalHeartbeatCount)) | ||
.append("openConnections", new BsonInt64(openConnections)); | ||
|
||
totalCommandDurationMs = 0; | ||
totalCommandCount = 0; | ||
totalHeartbeatCount = 0; | ||
totalHeartbeatDurationMs = 0; | ||
|
||
return responseBody; | ||
} | ||
|
||
private APIGatewayProxyResponseEvent templateResponse() { | ||
Map<String, String> headers = new HashMap<>(); | ||
headers.put("Content-Type", "application/json"); | ||
headers.put("X-Custom-Header", "application/json"); | ||
return new APIGatewayProxyResponseEvent() | ||
.withHeaders(headers); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rozza Should this run when a commit is merged into master, and if so, where could I set that up? (I did not see anything definite in the project settings.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great question!
So as the evergreen project monitors the master branch, all build variants will run by default.
See limiting when a task will run to learn about opting out. One such example is the publish release task which opts out by setting
git_tag_only: true
.Our evg.yml dates back from early evergreen days - I find it hard to grok and I wonder if there are newer features that we don't use. (a thought for another day).