mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(dynamodb): export the table as of ExportTime (#6132)
* fix(dynamodb): export the table as of ExportTime * refactor(dynamodb): share the epoch seconds conversions * fix(dynamodb): treat a table that is still being created as not found on export * docs(dynamodb): note the writes made before point-in-time recovery is enabled * fix(dynamodb): round timestamp inputs to milliseconds as AWS does * fix(dynamodb): check the ListBackups time range limits * fix(dynamodb): check the ListBackups ExclusiveStartBackupArn length * refactor(dynamodb): reuse requireBackupSource for the export table lookup
This commit is contained in:
1 parent
f7576eda1a
commit
da66550c70
12 files changed
+396
-37
No files matched your search
@@ -80,6 +80,9 @@ import java.util.stream.Collectors;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
import static io.github.hectorvent.floci.services.dynamodb.DynamoDbTimestamps.epochSeconds;
|
||||
import static io.github.hectorvent.floci.services.dynamodb.DynamoDbTimestamps.instantOf;
|
||||
|
||||
@ApplicationScoped
|
||||
public class DynamoDbService {
|
||||
|
||||
@@ -4718,7 +4721,8 @@ public class DynamoDbService {
|
||||
|
||||
// --- Export Operations ---
|
||||
|
||||
public ExportDescription exportTable(Map<String, Object> request, String region) {
|
||||
/** As on AWS, an ExportTime after LatestRestorableDateTime is accepted up to now. */
|
||||
public ExportDescription exportTable(Map<String, Object> request, Instant exportTime, String region) {
|
||||
String tableArn = (String) request.get("TableArn");
|
||||
String s3Bucket = (String) request.get("S3Bucket");
|
||||
String s3Prefix = request.containsKey("S3Prefix") ? (String) request.get("S3Prefix") : null;
|
||||
@@ -4742,10 +4746,24 @@ public class DynamoDbService {
|
||||
String tableRegion = ref.region() != null ? ref.region() : region;
|
||||
String storageKey = regionKey(tableRegion, tableName);
|
||||
|
||||
TableDefinition table = tableStore.get(storageKey)
|
||||
.orElseThrow(() -> resourceNotFoundException(tableName));
|
||||
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.
|
||||
if (exportTime != null && (exportTime.isBefore(table.getCreationDateTime().truncatedTo(ChronoUnit.SECONDS))
|
||||
|| startTime.isBefore(exportTime))) {
|
||||
throw new AwsException("InvalidExportTimeException", "Export Time is invalid", 400);
|
||||
}
|
||||
if (!table.isPointInTimeRecoveryEnabled()) {
|
||||
throw new AwsException("PointInTimeRecoveryUnavailableException",
|
||||
"Point in time recovery is not enabled for table '" + table.getTableName() + "'", 400);
|
||||
}
|
||||
if (exportTime != null
|
||||
&& exportTime.isBefore(pointInTimeRecovery.window(withPointInTimeStart(storageKey, table)).earliest())) {
|
||||
throw new AwsException("InvalidExportTimeException",
|
||||
"Point in Time Recovery is not available for specified export time", 400);
|
||||
}
|
||||
Instant time = exportTime != null ? exportTime : startTime;
|
||||
|
||||
long now = Instant.now().getEpochSecond();
|
||||
var exportId = newJobId();
|
||||
String exportArn = AwsArnUtils.Arn.of("dynamodb", tableRegion, regionResolver.getAccountId(), "table/" + table.getTableName() + "/export/" + exportId).toString();
|
||||
|
||||
@@ -4758,8 +4776,8 @@ public class DynamoDbService {
|
||||
desc.setS3Prefix(s3Prefix);
|
||||
desc.setExportFormat(exportFormat);
|
||||
desc.setExportType("FULL_EXPORT");
|
||||
desc.setExportTime(now);
|
||||
desc.setStartTime(now);
|
||||
desc.setExportTime(epochSeconds(time));
|
||||
desc.setStartTime(epochSeconds(startTime));
|
||||
desc.setClientToken(clientToken);
|
||||
desc.setS3SseAlgorithm(s3SseAlgorithm);
|
||||
desc.setS3BucketOwner(s3BucketOwner);
|
||||
@@ -4769,9 +4787,8 @@ public class DynamoDbService {
|
||||
}
|
||||
|
||||
ConcurrentSkipListMap<String, JsonNode> tableItems = itemsByTable.get(scopedItemsKey(storageKey));
|
||||
List<JsonNode> snapshot = tableItems != null
|
||||
? List.copyOf(tableItems.values())
|
||||
: List.of();
|
||||
List<JsonNode> snapshot = pointInTimeRecovery.itemsAt(regionResolver.getAccountId(), storageKey,
|
||||
tableItems != null ? tableItems : Map.of(), time);
|
||||
|
||||
// The worker gets its own copy so the object serialized into this response never
|
||||
// changes underneath the handler, and the request account so its writes land there.
|
||||
@@ -4804,7 +4821,7 @@ public class DynamoDbService {
|
||||
desc.setExportStatus("FAILED");
|
||||
desc.setFailureCode("S3NoSuchBucket");
|
||||
desc.setFailureMessage("The specified bucket does not exist: " + s3Bucket);
|
||||
desc.setEndTime(Instant.now().getEpochSecond());
|
||||
desc.setEndTime(epochSeconds(clock.instant()));
|
||||
if (exportStore != null) {
|
||||
exportStore.put(exportArn, desc);
|
||||
}
|
||||
@@ -4830,9 +4847,8 @@ public class DynamoDbService {
|
||||
manifestSummaryContent.getBytes(StandardCharsets.UTF_8),
|
||||
"application/json", Map.of());
|
||||
|
||||
long endTime = Instant.now().getEpochSecond();
|
||||
desc.setExportStatus("COMPLETED");
|
||||
desc.setEndTime(endTime);
|
||||
desc.setEndTime(epochSeconds(clock.instant()));
|
||||
desc.setItemCount(itemCount);
|
||||
desc.setBilledSizeBytes(billedSize);
|
||||
desc.setExportManifest(manifestSummaryKey);
|
||||
@@ -4847,7 +4863,7 @@ public class DynamoDbService {
|
||||
desc.setExportStatus("FAILED");
|
||||
desc.setFailureCode("UNKNOWN");
|
||||
desc.setFailureMessage(e.getMessage());
|
||||
desc.setEndTime(Instant.now().getEpochSecond());
|
||||
desc.setEndTime(epochSeconds(clock.instant()));
|
||||
if (exportStore != null) {
|
||||
exportStore.put(exportArn, desc);
|
||||
}
|
||||
@@ -4885,11 +4901,11 @@ public class DynamoDbService {
|
||||
ObjectNode root = objectMapper.createObjectNode();
|
||||
root.put("version", "2020-06-30");
|
||||
root.put("exportArn", desc.getExportArn());
|
||||
root.put("startTime", Instant.ofEpochSecond(desc.getStartTime()).toString());
|
||||
root.put("startTime", instantOf(desc.getStartTime()).toString());
|
||||
root.put("endTime", Instant.now().toString());
|
||||
root.put("tableArn", desc.getTableArn());
|
||||
root.put("tableId", desc.getTableId());
|
||||
root.put("exportTime", Instant.ofEpochSecond(desc.getExportTime()).toString());
|
||||
root.put("exportTime", instantOf(desc.getExportTime()).toString());
|
||||
root.put("s3Bucket", desc.getS3Bucket());
|
||||
root.putNull("s3Prefix");
|
||||
if (desc.getS3Prefix() != null) {
|
||||
@@ -5379,7 +5395,7 @@ public class DynamoDbService {
|
||||
desc.setExportStatus("FAILED");
|
||||
desc.setFailureCode("InterruptedByRestart");
|
||||
desc.setFailureMessage("The emulator restarted before the export finished");
|
||||
desc.setEndTime(now);
|
||||
desc.setEndTime(BigDecimal.valueOf(now));
|
||||
exports.putForAccount(entry.accountId(), entry.key(), desc);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package io.github.hectorvent.floci.services.dynamodb;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.Instant;
|
||||
|
||||
final class DynamoDbTimestamps {
|
||||
|
||||
private DynamoDbTimestamps() {}
|
||||
|
||||
static BigDecimal epochSeconds(Instant instant) {
|
||||
return BigDecimal.valueOf(instant.toEpochMilli(), 3);
|
||||
}
|
||||
|
||||
static Instant instantOf(BigDecimal epochSeconds) {
|
||||
return Instant.ofEpochMilli(epochSeconds.movePointRight(3).longValue());
|
||||
}
|
||||
}
|
||||
+31
-8
@@ -33,7 +33,6 @@ import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
@@ -48,6 +47,8 @@ import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static io.github.hectorvent.floci.services.dynamodb.DynamoDbTimestamps.epochSeconds;
|
||||
|
||||
/**
|
||||
* DynamoDB JSON protocol handler of the native engine, reached through the native backend
|
||||
* behind the DynamoDB operation seam rather than directly from the controllers.
|
||||
@@ -60,6 +61,9 @@ public class NativeDynamoDbJsonHandler {
|
||||
// The order AWS prints the enum constraint in.
|
||||
private static final List<String> BACKUP_TYPE_FILTERS = List.of("ALL", "AWS_BACKUP", "USER", "SYSTEM");
|
||||
private static final int MAX_LIST_BACKUPS_LIMIT = 100;
|
||||
private static final int MIN_BACKUP_ARN_LENGTH = 37;
|
||||
private static final int MAX_BACKUP_ARN_LENGTH = 1024;
|
||||
private static final Instant MAX_TIME_RANGE_BOUND = Instant.ofEpochMilli(99_999_999_999_999L);
|
||||
private static final int MAX_RECOVERY_PERIOD_DAYS = 35;
|
||||
|
||||
private final DynamoDbService dynamoDbService;
|
||||
@@ -1961,6 +1965,15 @@ public class NativeDynamoDbJsonHandler {
|
||||
violations.add("Value '" + limit + "' at 'limit' failed to satisfy constraint: "
|
||||
+ "Member must have value less than or equal to " + MAX_LIST_BACKUPS_LIMIT);
|
||||
}
|
||||
String exclusiveStartBackupArn = textOrNull(request, "ExclusiveStartBackupArn");
|
||||
if (exclusiveStartBackupArn != null && exclusiveStartBackupArn.length() < MIN_BACKUP_ARN_LENGTH) {
|
||||
violations.add("Value '" + exclusiveStartBackupArn + "' at 'exclusiveStartBackupArn' failed to satisfy "
|
||||
+ "constraint: Member must have length greater than or equal to " + MIN_BACKUP_ARN_LENGTH);
|
||||
}
|
||||
if (exclusiveStartBackupArn != null && MAX_BACKUP_ARN_LENGTH < exclusiveStartBackupArn.length()) {
|
||||
violations.add("Value '" + exclusiveStartBackupArn + "' at 'exclusiveStartBackupArn' failed to satisfy "
|
||||
+ "constraint: Member must have length less than or equal to " + MAX_BACKUP_ARN_LENGTH);
|
||||
}
|
||||
String backupType = request.hasNonNull("BackupType") ? request.get("BackupType").asText() : "USER";
|
||||
if (!BACKUP_TYPE_FILTERS.contains(backupType)) {
|
||||
violations.add("Value '" + backupType + "' at 'backupType' failed to satisfy constraint: "
|
||||
@@ -1973,9 +1986,11 @@ public class NativeDynamoDbJsonHandler {
|
||||
throw new AwsException("ValidationException", "One or more parameter values were invalid: "
|
||||
+ "Time range lower bound must be less than or equal to upper bound", 400);
|
||||
}
|
||||
checkTimeRangeBound("TimeRangeLowerBound", lowerBound);
|
||||
checkTimeRangeBound("TimeRangeUpperBound", upperBound);
|
||||
|
||||
DynamoDbService.ListBackupsResult result = dynamoDbService.listBackups(tableName, limit, lowerBound,
|
||||
upperBound, textOrNull(request, "ExclusiveStartBackupArn"), backupType, region);
|
||||
upperBound, exclusiveStartBackupArn, backupType, region);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode summaries = response.putArray("BackupSummaries");
|
||||
@@ -2211,13 +2226,21 @@ public class NativeDynamoDbJsonHandler {
|
||||
return node.hasNonNull(member) ? node.get(member).asText() : null;
|
||||
}
|
||||
|
||||
private static Instant timestampOrNull(JsonNode request, String member) {
|
||||
JsonNode node = request.path(member);
|
||||
return node.isNumber() ? Instant.ofEpochMilli(node.decimalValue().movePointRight(3).longValue()) : null;
|
||||
private static void checkTimeRangeBound(String member, Instant bound) {
|
||||
if (bound != null && bound.isBefore(Instant.EPOCH)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Invalid Request: The supplied " + member + " must not be before " + Instant.EPOCH, 400);
|
||||
}
|
||||
if (bound != null && MAX_TIME_RANGE_BOUND.isBefore(bound)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Invalid Request: The supplied " + member + " must not be after " + MAX_TIME_RANGE_BOUND, 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static BigDecimal epochSeconds(Instant instant) {
|
||||
return BigDecimal.valueOf(instant.toEpochMilli(), 3);
|
||||
private static Instant timestampOrNull(JsonNode request, String member) {
|
||||
JsonNode node = request.path(member);
|
||||
// AWS rounds the seconds as a double, so a value beyond the long range of milliseconds saturates.
|
||||
return node.isNumber() ? Instant.ofEpochMilli(Math.round(node.doubleValue() * 1000)) : null;
|
||||
}
|
||||
|
||||
private ObjectNode backupDetailsNode(TableBackup backup, String status) {
|
||||
@@ -3384,7 +3407,7 @@ public class NativeDynamoDbJsonHandler {
|
||||
? e.getValue().asText() : e.getValue()));
|
||||
|
||||
io.github.hectorvent.floci.services.dynamodb.model.ExportDescription desc =
|
||||
dynamoDbService.exportTable(params, region);
|
||||
dynamoDbService.exportTable(params, timestampOrNull(request, "ExportTime"), region);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.set("ExportDescription", objectMapper.valueToTree(desc));
|
||||
|
||||
+11
-9
@@ -5,6 +5,8 @@ import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
@@ -35,13 +37,13 @@ public class ExportDescription {
|
||||
private String exportType;
|
||||
|
||||
@JsonProperty("ExportTime")
|
||||
private Long exportTime;
|
||||
private BigDecimal exportTime;
|
||||
|
||||
@JsonProperty("StartTime")
|
||||
private Long startTime;
|
||||
private BigDecimal startTime;
|
||||
|
||||
@JsonProperty("EndTime")
|
||||
private Long endTime;
|
||||
private BigDecimal endTime;
|
||||
|
||||
@JsonProperty("ItemCount")
|
||||
private Long itemCount;
|
||||
@@ -91,14 +93,14 @@ public class ExportDescription {
|
||||
public String getExportType() { return exportType; }
|
||||
public void setExportType(String exportType) { this.exportType = exportType; }
|
||||
|
||||
public Long getExportTime() { return exportTime; }
|
||||
public void setExportTime(Long exportTime) { this.exportTime = exportTime; }
|
||||
public BigDecimal getExportTime() { return exportTime; }
|
||||
public void setExportTime(BigDecimal exportTime) { this.exportTime = exportTime; }
|
||||
|
||||
public Long getStartTime() { return startTime; }
|
||||
public void setStartTime(Long startTime) { this.startTime = startTime; }
|
||||
public BigDecimal getStartTime() { return startTime; }
|
||||
public void setStartTime(BigDecimal startTime) { this.startTime = startTime; }
|
||||
|
||||
public Long getEndTime() { return endTime; }
|
||||
public void setEndTime(Long endTime) { this.endTime = endTime; }
|
||||
public BigDecimal getEndTime() { return endTime; }
|
||||
public void setEndTime(BigDecimal endTime) { this.endTime = endTime; }
|
||||
|
||||
public Long getItemCount() { return itemCount; }
|
||||
public void setItemCount(Long itemCount) { this.itemCount = itemCount; }
|
||||
|
||||
Reference in new issue
Block a user