-
Notifications
You must be signed in to change notification settings - Fork 8
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
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} |
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; | ||
} | ||
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", { | ||
|
@@ -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", | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: { | ||
|
@@ -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 ?? "" | ||
}); | ||
} |
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.
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