Skip to content

DynamoDB: fix GlobalSecondaryIndexes parity in Create and DescribeTable #12119

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

Merged
merged 3 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
38 changes: 38 additions & 0 deletions localstack-core/localstack/services/dynamodb/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,33 @@ def create_table(
)
table_description["TableClassSummary"] = {"TableClass": table_class}

if "GlobalSecondaryIndexes" in table_description:
gsis = copy.deepcopy(table_description["GlobalSecondaryIndexes"])
# update the different values, as DynamoDB-local v2 has a regression around GSI and does not return anything
# anymore
for gsi in gsis:
index_name = gsi.get("IndexName", "")
gsi.update(
{
"IndexArn": f"{table_arn}/index/{index_name}",
"IndexSizeBytes": 0,
"IndexStatus": "ACTIVE",
"ItemCount": 0,
}
)
gsi_provisioned_throughput = gsi.setdefault("ProvisionedThroughput", {})
gsi_provisioned_throughput["NumberOfDecreasesToday"] = 0

if billing_mode == BillingMode.PAY_PER_REQUEST:
gsi_provisioned_throughput["ReadCapacityUnits"] = 0
gsi_provisioned_throughput["WriteCapacityUnits"] = 0

table_description["GlobalSecondaryIndexes"] = gsis

if "ProvisionedThroughput" in table_description:
if "NumberOfDecreasesToday" not in table_description["ProvisionedThroughput"]:
table_description["ProvisionedThroughput"]["NumberOfDecreasesToday"] = 0

tags = table_definitions.pop("Tags", [])
if tags:
get_store(context.account_id, context.region).TABLE_TAGS[table_arn] = {
Expand Down Expand Up @@ -765,6 +792,17 @@ def describe_table(
"TableClass": table_definitions["TableClass"]
}

if "GlobalSecondaryIndexes" in table_description:
for gsi in table_description["GlobalSecondaryIndexes"]:
default_values = {
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 0,
"WriteCapacityUnits": 0,
}
# even if the billing mode is PAY_PER_REQUEST, AWS returns the Read and Write Capacity Units
# Terraform depends on this parity for update operations
gsi["ProvisionedThroughput"] = default_values | gsi.get("ProvisionedThroughput", {})

return DescribeTableOutput(
Table=select_from_typed_dict(TableDescription, table_description)
)
Expand Down
2 changes: 1 addition & 1 deletion localstack-core/localstack/services/dynamodb/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
SCHEMA_CACHE = TTLCache(maxsize=50, ttl=20)

_ddb_local_arn_pattern = re.compile(
r'("TableArn"|"LatestStreamArn"|"StreamArn"|"ShardIterator")\s*:\s*"arn:[a-z-]+:dynamodb:ddblocal:000000000000:([^"]+)"'
r'("TableArn"|"LatestStreamArn"|"StreamArn"|"ShardIterator"|"IndexArn")\s*:\s*"arn:[a-z-]+:dynamodb:ddblocal:000000000000:([^"]+)"'
)
_ddb_local_region_pattern = re.compile(r'"awsRegion"\s*:\s*"([^"]+)"')
_ddb_local_exception_arn_pattern = re.compile(r'arn:[a-z-]+:dynamodb:ddblocal:000000000000:([^"]+)')
Expand Down
40 changes: 40 additions & 0 deletions localstack-core/localstack/services/dynamodb/v2/provider.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import json
import logging
import os
Expand Down Expand Up @@ -526,6 +527,34 @@ def create_table(
)
table_description["TableClassSummary"] = {"TableClass": table_class}

if "GlobalSecondaryIndexes" in table_description:
gsis = copy.deepcopy(table_description["GlobalSecondaryIndexes"])
# update the different values, as DynamoDB-local v2 has a regression around GSI and does not return anything
# anymore
for gsi in gsis:
index_name = gsi.get("IndexName", "")
gsi.update(
{
"IndexArn": f"{table_arn}/index/{index_name}",
"IndexSizeBytes": 0,
"IndexStatus": "ACTIVE",
"ItemCount": 0,
}
)
gsi_provisioned_throughput = gsi.setdefault("ProvisionedThroughput", {})
gsi_provisioned_throughput["NumberOfDecreasesToday"] = 0

if billing_mode == BillingMode.PAY_PER_REQUEST:
gsi_provisioned_throughput["ReadCapacityUnits"] = 0
gsi_provisioned_throughput["WriteCapacityUnits"] = 0

# table_definitions["GlobalSecondaryIndexes"] = gsis
table_description["GlobalSecondaryIndexes"] = gsis

if "ProvisionedThroughput" in table_description:
if "NumberOfDecreasesToday" not in table_description["ProvisionedThroughput"]:
table_description["ProvisionedThroughput"]["NumberOfDecreasesToday"] = 0

tags = table_definitions.pop("Tags", [])
if tags:
get_store(context.account_id, context.region).TABLE_TAGS[table_arn] = {
Expand Down Expand Up @@ -603,6 +632,17 @@ def describe_table(
"TableClass": table_definitions["TableClass"]
}

if "GlobalSecondaryIndexes" in table_description:
for gsi in table_description["GlobalSecondaryIndexes"]:
default_values = {
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 0,
"WriteCapacityUnits": 0,
}
# even if the billing mode is PAY_PER_REQUEST, AWS returns the Read and Write Capacity Units
# Terraform depends on this parity for update operations
gsi["ProvisionedThroughput"] = default_values | gsi.get("ProvisionedThroughput", {})

return DescribeTableOutput(
Table=select_from_typed_dict(TableDescription, table_description)
)
Expand Down
63 changes: 63 additions & 0 deletions tests/aws/services/dynamodb/test_dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2338,3 +2338,66 @@ def _get_records_amount(record_amount: int):

retry(lambda: _get_records_amount(1), sleep=1, retries=3)
snapshot.match("get-records", {"Records": records})

@markers.aws.validated
@pytest.mark.parametrize("billing_mode", ["PAY_PER_REQUEST", "PROVISIONED"])
@markers.snapshot.skip_snapshot_verify(
paths=[
# LS returns those and not AWS, probably because no changes happened there yet
"$..ProvisionedThroughput.LastDecreaseDateTime",
"$..ProvisionedThroughput.LastIncreaseDateTime",
"$..TableDescription.BillingModeSummary.LastUpdateToPayPerRequestDateTime",
]
)
def test_gsi_with_billing_mode(
self, aws_client, dynamodb_create_table_with_parameters, snapshot, billing_mode
):
snapshot.add_transformers_list(
[
snapshot.transform.key_value("TableName"),
snapshot.transform.key_value(
"TableStatus", reference_replacement=False, value_replacement="<table-status>"
),
snapshot.transform.key_value(
"IndexStatus", reference_replacement=False, value_replacement="<index-status>"
),
]
)

table_name = f"test-table-{short_uid()}"
create_table_kwargs = {}
global_secondary_index = {
"IndexName": "TransactionRecordID",
"KeySchema": [
{"AttributeName": "TRID", "KeyType": "HASH"},
],
"Projection": {"ProjectionType": "ALL"},
}

if billing_mode == "PROVISIONED":
create_table_kwargs["ProvisionedThroughput"] = {
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5,
}
global_secondary_index["ProvisionedThroughput"] = {
"ReadCapacityUnits": 1,
"WriteCapacityUnits": 1,
}

create_table = dynamodb_create_table_with_parameters(
TableName=table_name,
KeySchema=[
{"AttributeName": "TID", "KeyType": "HASH"},
],
AttributeDefinitions=[
{"AttributeName": "TID", "AttributeType": "S"},
{"AttributeName": "TRID", "AttributeType": "S"},
],
GlobalSecondaryIndexes=[global_secondary_index],
BillingMode=billing_mode,
**create_table_kwargs,
)
snapshot.match("create-table-with-gsi", create_table)

describe_table = aws_client.dynamodb.describe_table(TableName=table_name)
snapshot.match("describe-table", describe_table)
Loading
Loading