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

Fixed idempotency issue by first checking if table does not exist #1035

Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
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 dlp-cli
Submodule dlp-cli updated 48 files
+7 −0 .goreleaser.yaml
+6 −0 CODEOWNERS
+20 −7 README.md
+3 −4 cmd/backend/add/add.go
+74 −0 cmd/backend/add_param/add-param.go
+5 −0 cmd/backend/backend.go
+48 −0 cmd/backend/build_training_env/build-training-env-file.go
+63 −0 cmd/backend/get_secret/get-secret.go
+1 −3 cmd/backend/id_token/id_token.go
+5 −6 cmd/backend/install/install.go
+54 −0 cmd/backend/pull_config/pull-config.go
+3 −4 cmd/backend/remove/remove.go
+52 −0 cmd/backend/remove_param/remove-param.go
+1 −2 cmd/backend/start/start.go
+1 −2 cmd/backend/uid/uid.go
+1 −2 cmd/backend/uninstall/uninstall.go
+82 −0 cmd/backend/update_param/update-param.go
+1 −2 cmd/frontend/add/add.go
+48 −0 cmd/frontend/build_frontend_env/build-frontend-env-file.go
+5 −0 cmd/frontend/frontend.go
+1 −2 cmd/frontend/install/install.go
+1 −2 cmd/frontend/remove/remove.go
+1 −2 cmd/frontend/start/start.go
+11 −0 cmd/root.go
+38 −0 cmd/serverless/build_serverless_env/build-serverless-env-file.go
+1 −2 cmd/serverless/core/add/add.go
+5 −0 cmd/serverless/core/core.go
+1 −2 cmd/serverless/core/remove/remove.go
+1 −2 cmd/serverless/functions/add/add.go
+5 −0 cmd/serverless/functions/functions.go
+1 −2 cmd/serverless/functions/remove/remove.go
+1 −2 cmd/serverless/install/install.go
+4 −0 cmd/serverless/serverless.go
+1 −2 cmd/serverless/start/start.go
+2 −0 go.mod
+6 −1 go.sum
+8 −0 main.go
+34 −0 manifests/d/DSGT-DLP/dlp-cli/0.1.3/DSGT-DLP.dlp-cli.installer.yaml
+12 −0 manifests/d/DSGT-DLP/dlp-cli/0.1.3/DSGT-DLP.dlp-cli.locale.en-US.yaml
+7 −0 manifests/d/DSGT-DLP/dlp-cli/0.1.3/DSGT-DLP.dlp-cli.yaml
+34 −0 manifests/d/DSGT-DLP/dlp-cli/0.2.0/DSGT-DLP.dlp-cli.installer.yaml
+12 −0 manifests/d/DSGT-DLP/dlp-cli/0.2.0/DSGT-DLP.dlp-cli.locale.en-US.yaml
+7 −0 manifests/d/DSGT-DLP/dlp-cli/0.2.0/DSGT-DLP.dlp-cli.yaml
+31 −0 pkg/exec_bash_cmd_any_os.go
+7 −5 pkg/exec_bash_cmd_unix_os.go
+2 −26 pkg/exec_bash_cmd_windows_os.go
+8 −0 utils/constants.go
+34 −0 utils/envfile.go
22 changes: 22 additions & 0 deletions serverless/packages/functions/src/dbutils/get_all_tablenames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

export async function get_all_tablenames() : Promise<string[]> {
const AWS = require("aws-sdk");
AWS.config.update({region:'us-east-1'});
let dynamodb = new AWS.DynamoDB();

let params =
{
ExclusiveStartTableName: null
};
let tables: string[] = [];
while (true) {
let response = await dynamodb.listTables(params).promise();
tables = tables.concat(response.TableNames);

if (response.LastEvaluatedTableName === undefined) {
break;
}
params.ExclusiveStartTableName = response.LastEvaluatedTableName;
}
return tables;
}
24 changes: 24 additions & 0 deletions serverless/packages/functions/src/dbutils/table_exists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { get_all_tablenames } from "./get_all_tablenames";
import { DynamoDBClient, DescribeTableCommand } from '@aws-sdk/client-dynamodb';

export async function table_exists(table_name: string) : Promise<[boolean, string]> {
const tableNames: string[] = await get_all_tablenames();
const index: number = tableNames.findIndex(storedTableName => storedTableName.endsWith(table_name));
return [index !== -1, index !== -1 ? tableNames[index] : ""];
}

export async function table_attributes_exists(tableExistenceInfo: [boolean, string], table_attributes: object) : Promise<boolean>
{
if (!tableExistenceInfo[0])
{
/* If table doesn't exist, return false. */
return false;
}
const client = new DynamoDBClient({});
const describeTableCommand = new DescribeTableCommand({
"TableName": tableExistenceInfo[1]
});

const response = await client.send(describeTableCommand);
return true;
}
Copy link
Member

Choose a reason for hiding this comment

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

Minor: can you please run Prettier for all changed files? There are some aesthetic issues; for example, each file should end with a blank line.

We'll need to update our checks to also check /serverless in addition to /frontend

2 changes: 1 addition & 1 deletion serverless/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
config(_input) {
return {
name: "dlp-sst-app",
region: "us-west-2",
region: "us-east-1",
};
},
stacks(app) {
Expand Down
49 changes: 35 additions & 14 deletions serverless/stacks/AppStack.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Api, Bucket, StackContext, Table } from "sst/constructs";
import { Bucket as s3Bucket } from "aws-cdk-lib/aws-s3";
import { DescribeTableCommand } from '@aws-sdk/client-dynamodb';
import { table_exists, table_attributes_exists } from "../packages/functions/src/dbutils/table_exists";

export function AppStack({ stack }: StackContext) {
const bucket = new Bucket(stack, "dlp-upload-bucket", {
Expand All @@ -11,8 +13,11 @@ export function AppStack({ stack }: StackContext) {
),
},
});
const trainspaceRunTable = new Table(stack, "trainspace-run", {
fields: {

table_exists("trainspace-run").then(exists =>
{
const tableFieldBody =
{
runId: "string",
trainspaceId: "string",
userId: "string",
Expand All @@ -22,15 +27,34 @@ export function AppStack({ stack }: StackContext) {
onnxUri: "string",
confusionMatrixUri: "string",
aucRocUri: "string"
},
primaryIndex: {
partitionKey: "runId",
},
globalIndexes: {
"TrainspaceIndex": { partitionKey: "trainspaceId", sortKey: "timestamp"},
"UserIndex": {partitionKey: "userId"}
},
};

table_attributes_exists(exists, tableFieldBody).then(tableAttributesExists => {
if (!tableAttributesExists) {
new Table(stack, "trainspace-run", {
fields: {
runId: "string",
trainspaceId: "string",
userId: "string",
timestamp: "string",
resultCsvUri: "string",
modelPtUri: "string",
onnxUri: "string",
confusionMatrixUri: "string",
aucRocUri: "string"
},
primaryIndex: {
partitionKey: "runId",
},
globalIndexes: {
"TrainspaceIndex": { partitionKey: "trainspaceId", sortKey: "timestamp"},
"UserIndex": {partitionKey: "userId"}
},
Comment on lines +49 to +52
Copy link
Member

@karkir0003 karkir0003 Oct 25, 2023

Choose a reason for hiding this comment

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

What if I want to update a GSI or User index. Can we update the PR to be resilient for those cases? @alantao912

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think the current approach is correct. I'm looking into other solutions.

});
}
});
});

const api = new Api(stack, "Api", {
authorizers: {
FirebaseAuthorizer: {
Expand Down Expand Up @@ -69,9 +93,6 @@ export function AppStack({ stack }: StackContext) {
api.getFunction("GET /datasets/user/{type}")?.functionName ?? "",
GetUserDatasetColumnsFunctionName:
api.getFunction("GET /datasets/user/{type}/{filename}/columns")
?.functionName ?? "",
TrainspaceRunTableName: {
value: trainspaceRunTable.tableName
}
?.functionName ?? ""
});
}