Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(dynamodb): show table creation times to the millisecond (#6325)

TATSUNO “Taz” Yasuhiro80e5df933b5f
diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java
index 9b90614993..43ae8b7d9b 100644
--- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java
@@ -4903,7 +4903,8 @@ public class DynamoDbService {
TableDefinition table = requireBackupSource(storageKey, tableArn);
Instant startTime = clock.instant().truncatedTo(ChronoUnit.MILLIS);
- // Seconds, as CreationDateTime is shown, so an EarliestRestorableDateTime in the creation second passes.
+ // AWS cannot enable point-in-time recovery until seconds after the creation, so its
+ // EarliestRestorableDateTime is never in the creation second.
if (exportTime != null && (exportTime.isBefore(table.getCreationDateTime().truncatedTo(ChronoUnit.SECONDS))
|| startTime.isBefore(exportTime))) {
throw new AwsException("InvalidExportTimeException", "Export Time is invalid", 400);
diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java
index 08b7abcaba..f5416037b8 100644
--- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java
@@ -486,7 +486,7 @@ public class NativeDynamoDbJsonHandler {
description.put("GlobalTableStatus", "ACTIVE");
description.put("GlobalTableArn", table.getTableArn());
if (table.getCreationDateTime() != null) {
- description.put("CreationDateTime", table.getCreationDateTime().getEpochSecond());
+ description.put("CreationDateTime", epochSeconds(table.getCreationDateTime()));
}
description.set("ReplicationGroup", replicaNodes(table, objectMapper.createArrayNode(), true));
return description;
@@ -3133,7 +3133,7 @@ public class NativeDynamoDbJsonHandler {
node.put("TableId", table.getTableId());
node.put("TableStatus", table.getTableStatus());
node.put("TableArn", table.getTableArn());
- node.put("CreationDateTime", table.getCreationDateTime().getEpochSecond());
+ node.put("CreationDateTime", epochSeconds(table.getCreationDateTime()));
node.put("ItemCount", table.getItemCount());
node.put("TableSizeBytes", table.getTableSizeBytes());
node.put("DeletionProtectionEnabled", table.isDeletionProtectionEnabled());
@@ -3142,7 +3142,7 @@ public class NativeDynamoDbJsonHandler {
ObjectNode billing = objectMapper.createObjectNode();
billing.put("BillingMode", "PAY_PER_REQUEST");
billing.put("LastUpdateToPayPerRequestDateTime",
- table.getCreationDateTime().getEpochSecond());
+ epochSeconds(table.getCreationDateTime()));
node.set("BillingModeSummary", billing);
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbGlobalTableIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbGlobalTableIntegrationTest.java
index 75dc4ef98f..e35baabb31 100644
--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbGlobalTableIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbGlobalTableIntegrationTest.java
@@ -6,6 +6,10 @@ import io.restassured.parsing.Parser;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
+import java.math.BigDecimal;
+
+import static io.github.hectorvent.floci.testing.DynamoDbRequests.assertEpochMillisBetween;
+import static io.github.hectorvent.floci.testing.DynamoDbRequests.decimalAt;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
@@ -71,6 +75,19 @@ class DynamoDbGlobalTableIntegrationTest {
.body("GlobalTables.GlobalTableName", hasItem(name));
}
+ @Test
+ void creationDateTimeHasMilliseconds() throws Exception {
+ String name = "gt-created-" + Long.toString(System.nanoTime(), 36);
+ long before = System.currentTimeMillis();
+ createTable(name, true, "NEW_AND_OLD_IMAGES");
+
+ BigDecimal created = decimalAt(call("CreateGlobalTable", "{\"GlobalTableName\":\"" + name + "\","
+ + "\"ReplicationGroup\":[{\"RegionName\":\"eu-west-1\"}]}")
+ .then().statusCode(200), "/GlobalTableDescription/CreationDateTime");
+ long after = System.currentTimeMillis();
+ assertEpochMillisBetween(before, created, after);
+ }
+
@Test
void aSecondCreateOnTheSameTableIsRefused() {
String name = "gt-dup-" + Long.toString(System.nanoTime(), 36);
diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.java
index eebbf47ad3..591fdcbb8d 100644
--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.java
@@ -5,18 +5,22 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.Response;
+import io.restassured.response.ValidatableResponse;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
+import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.zip.CRC32;
+import static io.github.hectorvent.floci.testing.DynamoDbRequests.assertEpochMillisBetween;
+import static io.github.hectorvent.floci.testing.DynamoDbRequests.decimalAt;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -5879,6 +5883,45 @@ given()
deleteTable(tableName);
}
+ @Test
+ void tableTimesHaveMilliseconds() throws Exception {
+ String tableName = "CreationMillisTable";
+ long before = System.currentTimeMillis();
+ BigDecimal created = decimalAt(given()
+ .header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "%s",
+ "KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
+ "AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
+ "BillingMode": "PAY_PER_REQUEST"
+ }
+ """.formatted(tableName))
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200), "/TableDescription/CreationDateTime");
+ long after = System.currentTimeMillis();
+ assertEpochMillisBetween(before, created, after);
+
+ ValidatableResponse described = given()
+ .header("X-Amz-Target", "DynamoDB_20120810.DescribeTable")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {"TableName": "%s"}
+ """.formatted(tableName))
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+ assertEquals(0, created.compareTo(decimalAt(described, "/Table/CreationDateTime")));
+ assertEquals(0, created.compareTo(
+ decimalAt(described, "/Table/BillingModeSummary/LastUpdateToPayPerRequestDateTime")));
+
+ deleteTable(tableName);
+ }
+
@Test
void twoGsiDeletionsInSameRequestFailWithLimitExceeded() {
// Catches: Floci deletes both indexes when one UpdateTable request carries two GSI deletes
diff --git a/src/test/java/io/github/hectorvent/floci/testing/DynamoDbRequests.java b/src/test/java/io/github/hectorvent/floci/testing/DynamoDbRequests.java
index 308c3b0ef3..c822fd6097 100644
--- a/src/test/java/io/github/hectorvent/floci/testing/DynamoDbRequests.java
+++ b/src/test/java/io/github/hectorvent/floci/testing/DynamoDbRequests.java
@@ -7,6 +7,7 @@ import io.restassured.response.ValidatableResponse;
import java.math.BigDecimal;
import static io.restassured.RestAssured.given;
+import static org.junit.jupiter.api.Assertions.assertTrue;
public final class DynamoDbRequests {
@@ -28,4 +29,9 @@ public final class DynamoDbRequests {
public static BigDecimal decimalAt(ValidatableResponse response, String pointer) throws Exception {
return EXACT.readTree(response.extract().asString()).at(pointer).decimalValue();
}
+
+ public static void assertEpochMillisBetween(long before, BigDecimal epochSeconds, long after) {
+ long millis = epochSeconds.movePointRight(3).longValueExact();
+ assertTrue(before <= millis && millis <= after, epochSeconds + " is not between " + before + " and " + after);
+ }
}