mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(dynamodb): emulate Contributor Insights on tables and global secondary indexes (#7326)
This commit is contained in:
1 parent
bbe23ed550
commit
bb5e692b87
8 files changed
+707
No files matched your search
@@ -26,6 +26,7 @@ import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.AttributeDefinition;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.ConditionalCheckFailedException;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.ContributorInsights;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.ExportDescription;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.ExportSummary;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.GlobalSecondaryIndex;
|
||||
@@ -2935,6 +2936,122 @@ public class DynamoDbService {
|
||||
LOG.infov("Updated TTL for table {0}: enabled={1}, attr={2}", canonicalTableName, enabled, ttlAttributeName);
|
||||
}
|
||||
|
||||
// --- Contributor Insights ---
|
||||
|
||||
private static final String DEFAULT_CONTRIBUTOR_INSIGHTS_MODE = "ACCESSED_AND_THROTTLED_KEYS";
|
||||
private static final int MAX_CONTRIBUTOR_INSIGHTS_PAGE_SIZE = 100;
|
||||
|
||||
/**
|
||||
* A table or global secondary index as the Contributor Insights operations see it. {@code arn}
|
||||
* orders and pages ListContributorInsights; {@code insights} is null until the first update.
|
||||
*/
|
||||
public record ContributorInsightsTarget(String tableName, String indexName, String arn, boolean hasSortKey,
|
||||
ContributorInsights insights) {
|
||||
public boolean enabled() {
|
||||
return insights != null && insights.isEnabled();
|
||||
}
|
||||
}
|
||||
|
||||
public record ContributorInsightsPage(List<ContributorInsightsTarget> summaries, String nextToken) {}
|
||||
|
||||
public ContributorInsightsTarget describeContributorInsights(String tableName, String indexName, String region) {
|
||||
String canonicalTableName = canonicalTableName(region, tableName);
|
||||
TableDefinition table = tableStore.get(regionKey(region, canonicalTableName))
|
||||
.orElseThrow(() -> resourceNotFoundException(canonicalTableName));
|
||||
return contributorInsightsTarget(table, indexName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enabling a disabled table or index creates its rules again. Enabling an enabled one keeps its
|
||||
* rules and switches the mode only when the request names one.
|
||||
*/
|
||||
public ContributorInsightsTarget updateContributorInsights(String tableName, String indexName, boolean enable,
|
||||
String mode, String region) {
|
||||
String canonicalTableName = canonicalTableName(region, tableName);
|
||||
String storageKey = regionKey(region, canonicalTableName);
|
||||
TableDefinition table = tableStore.get(storageKey)
|
||||
.orElseThrow(() -> resourceNotFoundException(canonicalTableName));
|
||||
ContributorInsightsTarget target = contributorInsightsTarget(table, indexName);
|
||||
ContributorInsights insights = target.insights();
|
||||
if (insights == null) {
|
||||
insights = new ContributorInsights();
|
||||
if (indexName == null) {
|
||||
table.setContributorInsights(insights);
|
||||
} else {
|
||||
table.findGsi(indexName).orElseThrow().setContributorInsights(insights);
|
||||
}
|
||||
}
|
||||
Instant now = clock.instant();
|
||||
if (enable && !insights.isEnabled()) {
|
||||
insights.setRulesCreatedAt(now);
|
||||
insights.setMode(mode != null ? mode : DEFAULT_CONTRIBUTOR_INSIGHTS_MODE);
|
||||
} else if (enable && mode != null) {
|
||||
insights.setMode(mode);
|
||||
}
|
||||
insights.setEnabled(enable);
|
||||
insights.setLastUpdateDateTime(now);
|
||||
tableStore.put(storageKey, table);
|
||||
LOG.infov("Updated Contributor Insights for table {0} index {1}: enabled={2}, mode={3}",
|
||||
canonicalTableName, indexName, enable, insights.getMode());
|
||||
return new ContributorInsightsTarget(target.tableName(), target.indexName(), target.arn(),
|
||||
target.hasSortKey(), insights);
|
||||
}
|
||||
|
||||
/** The tables and global secondary indexes with Contributor Insights enabled, in ARN order. */
|
||||
public ContributorInsightsPage listContributorInsights(String tableName, Integer maxResults, String nextToken,
|
||||
String region) {
|
||||
if (maxResults != null && MAX_CONTRIBUTOR_INSIGHTS_PAGE_SIZE < maxResults) {
|
||||
throw SmithyValidationErrors.validationException(SmithyValidationErrors.valueTooLarge(
|
||||
"maxResults", String.valueOf(maxResults), MAX_CONTRIBUTOR_INSIGHTS_PAGE_SIZE));
|
||||
}
|
||||
// A token is the ARN of the last table or index of the previous page.
|
||||
if (nextToken != null && !AwsArnUtils.isArnFor(nextToken, "dynamodb")) {
|
||||
throw new AwsException("ValidationException", "The provided next token is invalid", 400);
|
||||
}
|
||||
List<TableDefinition> tables;
|
||||
if (tableName != null) {
|
||||
String canonicalTableName = canonicalTableName(region, tableName);
|
||||
tables = List.of(tableStore.get(regionKey(region, canonicalTableName))
|
||||
.orElseThrow(() -> resourceNotFoundException(canonicalTableName)));
|
||||
} else {
|
||||
tables = tableStore.scan(k -> k.startsWith(region + "::"));
|
||||
}
|
||||
List<ContributorInsightsTarget> enabled = new ArrayList<>();
|
||||
for (TableDefinition table : tables) {
|
||||
enabled.add(contributorInsightsTarget(table, null));
|
||||
for (GlobalSecondaryIndex gsi : table.getGlobalSecondaryIndexes()) {
|
||||
enabled.add(contributorInsightsTarget(table, gsi.getIndexName()));
|
||||
}
|
||||
}
|
||||
enabled.removeIf(target -> !target.enabled());
|
||||
enabled.sort(Comparator.comparing(ContributorInsightsTarget::arn));
|
||||
|
||||
int start = 0;
|
||||
if (nextToken != null) {
|
||||
while (start < enabled.size() && enabled.get(start).arn().compareTo(nextToken) <= 0) {
|
||||
start++;
|
||||
}
|
||||
}
|
||||
int limit = maxResults != null && maxResults > 0 ? maxResults : MAX_CONTRIBUTOR_INSIGHTS_PAGE_SIZE;
|
||||
int end = Math.min(start + limit, enabled.size());
|
||||
List<ContributorInsightsTarget> page = List.copyOf(enabled.subList(start, end));
|
||||
return new ContributorInsightsPage(page, end < enabled.size() ? page.getLast().arn() : null);
|
||||
}
|
||||
|
||||
/** Contributor Insights covers a table and its global secondary indexes, never a local one. */
|
||||
private static ContributorInsightsTarget contributorInsightsTarget(TableDefinition table, String indexName) {
|
||||
if (indexName == null) {
|
||||
return new ContributorInsightsTarget(table.getTableName(), null, table.getTableArn(),
|
||||
table.getSortKeyName() != null, table.getContributorInsights());
|
||||
}
|
||||
GlobalSecondaryIndex gsi = table.findGsi(indexName)
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
"Requested resource not found: Index: " + indexName + " not found for table: "
|
||||
+ table.getTableName(), 400));
|
||||
return new ContributorInsightsTarget(table.getTableName(), indexName, gsi.getIndexArn(),
|
||||
gsi.getSortKeyName() != null, gsi.getContributorInsights());
|
||||
}
|
||||
|
||||
/** The restore window is null while point-in-time recovery is disabled. */
|
||||
public record ContinuousBackups(TableDefinition table, DynamoDbPointInTimeRecovery.RestoreWindow window) {}
|
||||
|
||||
|
||||
+137
@@ -19,6 +19,7 @@ import io.github.hectorvent.floci.services.dynamodb.NativeDynamoDbTableService.T
|
||||
import io.github.hectorvent.floci.services.dynamodb.NativeDynamoDbTableService.UpdateTableRequest;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.AttributeDefinition;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.ConditionalCheckFailedException;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.ContributorInsights;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.ExportDescription;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.ExportSummary;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.GlobalSecondaryIndex;
|
||||
@@ -69,6 +70,9 @@ public class NativeDynamoDbJsonHandler {
|
||||
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 static final List<String> CONTRIBUTOR_INSIGHTS_ACTIONS = List.of("ENABLE", "DISABLE");
|
||||
// The order AWS prints the enum constraint in.
|
||||
private static final List<String> CONTRIBUTOR_INSIGHTS_MODES = List.of("THROTTLED_KEYS", "ACCESSED_AND_THROTTLED_KEYS");
|
||||
// The warm throughput a new on-demand table or index starts with.
|
||||
private static final long ON_DEMAND_WARM_READ_UNITS = 12000;
|
||||
private static final long ON_DEMAND_WARM_WRITE_UNITS = 4000;
|
||||
@@ -115,6 +119,9 @@ public class NativeDynamoDbJsonHandler {
|
||||
case "UpdateTimeToLive" -> handleUpdateTimeToLive(request, region);
|
||||
case "DescribeContinuousBackups" -> handleDescribeContinuousBackups(request, region);
|
||||
case "UpdateContinuousBackups" -> handleUpdateContinuousBackups(request, region);
|
||||
case "UpdateContributorInsights" -> handleUpdateContributorInsights(request, region);
|
||||
case "DescribeContributorInsights" -> handleDescribeContributorInsights(request, region);
|
||||
case "ListContributorInsights" -> handleListContributorInsights(request, region);
|
||||
case "CreateBackup" -> handleCreateBackup(request, region);
|
||||
case "DescribeBackup" -> handleDescribeBackup(request, region);
|
||||
case "ListBackups" -> handleListBackups(request, region);
|
||||
@@ -2001,6 +2008,136 @@ public class NativeDynamoDbJsonHandler {
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
/** AWS answers ENABLING or DISABLING here; Floci settles at once, so a describe reads ENABLED or DISABLED. */
|
||||
private Response handleUpdateContributorInsights(JsonNode request, String region) {
|
||||
String tableName = requireTableNameParameter(request, "TableName");
|
||||
DynamoDbTableNames.resolveWithRegion(tableName, region);
|
||||
List<String> violations = new ArrayList<>();
|
||||
String indexName = contributorInsightsIndexName(request, violations);
|
||||
JsonNode action = request.path("ContributorInsightsAction");
|
||||
if (action.isMissingNode() || action.isNull()) {
|
||||
violations.add(SmithyValidationErrors.notNull("contributorInsightsAction"));
|
||||
} else if (!CONTRIBUTOR_INSIGHTS_ACTIONS.contains(action.asText())) {
|
||||
violations.add(SmithyValidationErrors.notInEnum(action.asText(), "contributorInsightsAction",
|
||||
CONTRIBUTOR_INSIGHTS_ACTIONS));
|
||||
}
|
||||
String mode = request.hasNonNull("ContributorInsightsMode")
|
||||
? request.get("ContributorInsightsMode").asText() : null;
|
||||
if (mode != null && !CONTRIBUTOR_INSIGHTS_MODES.contains(mode)) {
|
||||
violations.add(SmithyValidationErrors.notInEnum(mode, "contributorInsightsMode",
|
||||
CONTRIBUTOR_INSIGHTS_MODES));
|
||||
}
|
||||
throwValidationErrors(violations);
|
||||
boolean enable = "ENABLE".equals(action.asText());
|
||||
|
||||
DynamoDbService.ContributorInsightsTarget target =
|
||||
dynamoDbService.updateContributorInsights(tableName, indexName, enable, mode, region);
|
||||
|
||||
ObjectNode response = contributorInsightsNode(target);
|
||||
response.put("ContributorInsightsStatus", enable ? "ENABLING" : "DISABLING");
|
||||
if (enable) {
|
||||
response.put("ContributorInsightsMode", target.insights().getMode());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleDescribeContributorInsights(JsonNode request, String region) {
|
||||
String tableName = requireTableNameParameter(request, "TableName");
|
||||
DynamoDbTableNames.resolveWithRegion(tableName, region);
|
||||
List<String> violations = new ArrayList<>();
|
||||
String indexName = contributorInsightsIndexName(request, violations);
|
||||
throwValidationErrors(violations);
|
||||
|
||||
DynamoDbService.ContributorInsightsTarget target =
|
||||
dynamoDbService.describeContributorInsights(tableName, indexName, region);
|
||||
|
||||
ObjectNode response = contributorInsightsNode(target);
|
||||
ContributorInsights insights = target.insights();
|
||||
if (target.enabled()) {
|
||||
ArrayNode rules = response.putArray("ContributorInsightsRuleList");
|
||||
contributorInsightsRuleNames(target).forEach(rules::add);
|
||||
response.put("ContributorInsightsStatus", "ENABLED");
|
||||
response.put("ContributorInsightsMode", insights.getMode());
|
||||
} else {
|
||||
response.put("ContributorInsightsStatus", "DISABLED");
|
||||
}
|
||||
if (insights != null) {
|
||||
response.put("LastUpdateDateTime", epochSeconds(insights.getLastUpdateDateTime()));
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleListContributorInsights(JsonNode request, String region) {
|
||||
String tableName = request.hasNonNull("TableName") ? request.get("TableName").asText() : null;
|
||||
if (tableName != null) {
|
||||
DynamoDbTableNames.resolveWithRegion(tableName, region);
|
||||
}
|
||||
Integer maxResults = request.hasNonNull("MaxResults") ? request.get("MaxResults").asInt() : null;
|
||||
String nextToken = request.hasNonNull("NextToken") ? request.get("NextToken").asText() : null;
|
||||
|
||||
DynamoDbService.ContributorInsightsPage page =
|
||||
dynamoDbService.listContributorInsights(tableName, maxResults, nextToken, region);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode summaries = response.putArray("ContributorInsightsSummaries");
|
||||
for (DynamoDbService.ContributorInsightsTarget target : page.summaries()) {
|
||||
ObjectNode summary = contributorInsightsNode(target);
|
||||
summary.put("ContributorInsightsStatus", "ENABLED");
|
||||
summary.put("ContributorInsightsMode", target.insights().getMode());
|
||||
summaries.add(summary);
|
||||
}
|
||||
if (page.nextToken() != null) {
|
||||
response.put("NextToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private static String contributorInsightsIndexName(JsonNode request, List<String> violations) {
|
||||
if (!request.hasNonNull("IndexName")) {
|
||||
return null;
|
||||
}
|
||||
String indexName = request.get("IndexName").asText();
|
||||
violations.addAll(nameViolations(indexName, "indexName"));
|
||||
return indexName;
|
||||
}
|
||||
|
||||
private ObjectNode contributorInsightsNode(DynamoDbService.ContributorInsightsTarget target) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("TableName", target.tableName());
|
||||
if (target.indexName() != null) {
|
||||
node.put("IndexName", target.indexName());
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
* The rules AWS creates, named {@code DynamoDBContributorInsights-<kind>-<resource>-<creation millis>}
|
||||
* where the resource is the table name, or {@code <table>-<index>} for an index: the accessed-key
|
||||
* rules (PKC, SKC) only in ACCESSED_AND_THROTTLED_KEYS mode, the sort-key rules (SKC, SKT) only
|
||||
* when the table or index has a sort key.
|
||||
*/
|
||||
private static List<String> contributorInsightsRuleNames(DynamoDbService.ContributorInsightsTarget target) {
|
||||
List<String> kinds = new ArrayList<>();
|
||||
if ("ACCESSED_AND_THROTTLED_KEYS".equals(target.insights().getMode())) {
|
||||
kinds.add("PKC");
|
||||
if (target.hasSortKey()) {
|
||||
kinds.add("SKC");
|
||||
}
|
||||
}
|
||||
kinds.add("PKT");
|
||||
if (target.hasSortKey()) {
|
||||
kinds.add("SKT");
|
||||
}
|
||||
String resource = target.indexName() != null
|
||||
? target.tableName() + "-" + target.indexName() : target.tableName();
|
||||
String suffix = "-" + resource + "-" + target.insights().getRulesCreatedAt().toEpochMilli();
|
||||
List<String> names = new ArrayList<>();
|
||||
for (String kind : kinds) {
|
||||
names.add("DynamoDBContributorInsights-" + kind + suffix);
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
private Response handleCreateBackup(JsonNode request, String region) {
|
||||
String tableName = requireTableNameParameter(request, "TableName");
|
||||
DynamoDbTableNames.resolveWithRegion(tableName, region);
|
||||
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package io.github.hectorvent.floci.services.dynamodb.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
/**
|
||||
* The CloudWatch Contributor Insights setting of a table or a global secondary index, kept from the
|
||||
* first UpdateContributorInsights call on it. {@code rulesCreatedAt} stamps the rule names and is
|
||||
* reset each time the setting goes from disabled to enabled, when AWS creates the rules again.
|
||||
*/
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class ContributorInsights {
|
||||
|
||||
private boolean enabled;
|
||||
private String mode;
|
||||
private Instant lastUpdateDateTime;
|
||||
private Instant rulesCreatedAt;
|
||||
|
||||
public ContributorInsights() {
|
||||
}
|
||||
|
||||
public boolean isEnabled() { return enabled; }
|
||||
public void setEnabled(boolean enabled) { this.enabled = enabled; }
|
||||
|
||||
public String getMode() { return mode; }
|
||||
public void setMode(String mode) { this.mode = mode; }
|
||||
|
||||
public Instant getLastUpdateDateTime() { return lastUpdateDateTime; }
|
||||
public void setLastUpdateDateTime(Instant lastUpdateDateTime) { this.lastUpdateDateTime = lastUpdateDateTime; }
|
||||
|
||||
public Instant getRulesCreatedAt() { return rulesCreatedAt; }
|
||||
public void setRulesCreatedAt(Instant rulesCreatedAt) { this.rulesCreatedAt = rulesCreatedAt; }
|
||||
}
|
||||
+7
@@ -21,6 +21,8 @@ public class GlobalSecondaryIndex {
|
||||
private List<String> nonKeyAttributes;
|
||||
private Integer onDemandMaxReadRequestUnits;
|
||||
private Integer onDemandMaxWriteRequestUnits;
|
||||
// Null until the first UpdateContributorInsights call on this index.
|
||||
private ContributorInsights contributorInsights;
|
||||
|
||||
public GlobalSecondaryIndex() {
|
||||
this.provisionedThroughput = new ProvisionedThroughput(0, 0);
|
||||
@@ -72,6 +74,11 @@ public class GlobalSecondaryIndex {
|
||||
public Integer getOnDemandMaxWriteRequestUnits() { return onDemandMaxWriteRequestUnits; }
|
||||
public void setOnDemandMaxWriteRequestUnits(Integer v) { this.onDemandMaxWriteRequestUnits = v; }
|
||||
|
||||
public ContributorInsights getContributorInsights() { return contributorInsights; }
|
||||
public void setContributorInsights(ContributorInsights contributorInsights) {
|
||||
this.contributorInsights = contributorInsights;
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
public String getPartitionKeyName() {
|
||||
return keySchema.stream()
|
||||
|
||||
@@ -60,6 +60,8 @@ public class TableDefinition {
|
||||
// concurrency on subsequent Put/DeleteResourcePolicy calls. Null when no policy is attached.
|
||||
private String resourcePolicy;
|
||||
private String resourcePolicyRevisionId;
|
||||
// Null until the first UpdateContributorInsights call on the table itself.
|
||||
private ContributorInsights contributorInsights;
|
||||
|
||||
public TableDefinition() {
|
||||
this.keySchema = new ArrayList<>();
|
||||
@@ -246,6 +248,11 @@ public class TableDefinition {
|
||||
public String getResourcePolicyRevisionId() { return resourcePolicyRevisionId; }
|
||||
public void setResourcePolicyRevisionId(String resourcePolicyRevisionId) { this.resourcePolicyRevisionId = resourcePolicyRevisionId; }
|
||||
|
||||
public ContributorInsights getContributorInsights() { return contributorInsights; }
|
||||
public void setContributorInsights(ContributorInsights contributorInsights) {
|
||||
this.contributorInsights = contributorInsights;
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
public String getPartitionKeyName() {
|
||||
return keySchema.stream()
|
||||
|
||||
Reference in new issue
Block a user