Add ttl for braintree writes to onetime donation table

This commit is contained in:
Katherine 2023-12-15 13:37:35 -05:00 committed by GitHub
parent 372e3f83d2
commit a37acd1f42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 5 deletions

View File

@ -104,6 +104,7 @@ dynamoDbTables:
expiration: P30D # Duration of time until rows expire expiration: P30D # Duration of time until rows expire
onetimeDonations: onetimeDonations:
tableName: Example_OnetimeDonations tableName: Example_OnetimeDonations
expiration: P90D
phoneNumberIdentifiers: phoneNumberIdentifiers:
tableName: Example_PhoneNumberIdentifiers tableName: Example_PhoneNumberIdentifiers
profiles: profiles:

View File

@ -549,7 +549,7 @@ public class WhisperServerService extends Application<WhisperServerConfiguration
dynamoDbAsyncClient, dynamoDbAsyncClient,
config.getDynamoDbTables().getIssuedReceipts().getGenerator()); config.getDynamoDbTables().getIssuedReceipts().getGenerator());
OneTimeDonationsManager oneTimeDonationsManager = new OneTimeDonationsManager( OneTimeDonationsManager oneTimeDonationsManager = new OneTimeDonationsManager(
config.getDynamoDbTables().getOnetimeDonations().getTableName(), dynamoDbAsyncClient); config.getDynamoDbTables().getOnetimeDonations().getTableName(), config.getDynamoDbTables().getOnetimeDonations().getExpiration(), dynamoDbAsyncClient);
RedeemedReceiptsManager redeemedReceiptsManager = new RedeemedReceiptsManager(clock, RedeemedReceiptsManager redeemedReceiptsManager = new RedeemedReceiptsManager(clock,
config.getDynamoDbTables().getRedeemedReceipts().getTableName(), config.getDynamoDbTables().getRedeemedReceipts().getTableName(),
dynamoDbAsyncClient, dynamoDbAsyncClient,

View File

@ -59,7 +59,7 @@ public class DynamoDbTables {
private final Table kemKeys; private final Table kemKeys;
private final Table kemLastResortKeys; private final Table kemLastResortKeys;
private final TableWithExpiration messages; private final TableWithExpiration messages;
private final Table onetimeDonations; private final TableWithExpiration onetimeDonations;
private final Table phoneNumberIdentifiers; private final Table phoneNumberIdentifiers;
private final Table profiles; private final Table profiles;
private final Table pushChallenge; private final Table pushChallenge;
@ -83,7 +83,7 @@ public class DynamoDbTables {
@JsonProperty("pqKeys") final Table kemKeys, @JsonProperty("pqKeys") final Table kemKeys,
@JsonProperty("pqLastResortKeys") final Table kemLastResortKeys, @JsonProperty("pqLastResortKeys") final Table kemLastResortKeys,
@JsonProperty("messages") final TableWithExpiration messages, @JsonProperty("messages") final TableWithExpiration messages,
@JsonProperty("onetimeDonations") final Table onetimeDonations, @JsonProperty("onetimeDonations") final TableWithExpiration onetimeDonations,
@JsonProperty("phoneNumberIdentifiers") final Table phoneNumberIdentifiers, @JsonProperty("phoneNumberIdentifiers") final Table phoneNumberIdentifiers,
@JsonProperty("profiles") final Table profiles, @JsonProperty("profiles") final Table profiles,
@JsonProperty("pushChallenge") final Table pushChallenge, @JsonProperty("pushChallenge") final Table pushChallenge,
@ -192,7 +192,7 @@ public class DynamoDbTables {
@NotNull @NotNull
@Valid @Valid
public Table getOnetimeDonations() { public TableWithExpiration getOnetimeDonations() {
return onetimeDonations; return onetimeDonations;
} }

View File

@ -8,6 +8,7 @@ package org.whispersystems.textsecuregcm.storage;
import static com.codahale.metrics.MetricRegistry.name; import static com.codahale.metrics.MetricRegistry.name;
import io.micrometer.core.instrument.Metrics; import io.micrometer.core.instrument.Metrics;
import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -21,14 +22,19 @@ import software.amazon.awssdk.services.dynamodb.model.PutItemRequest;
public class OneTimeDonationsManager { public class OneTimeDonationsManager {
public static final String KEY_PAYMENT_ID = "P"; // S public static final String KEY_PAYMENT_ID = "P"; // S
public static final String ATTR_PAID_AT = "A"; // N public static final String ATTR_PAID_AT = "A"; // N
public static final String ATTR_TTL = "E"; // N
private static final String ONETIME_DONATION_NOT_FOUND_COUNTER_NAME = name(OneTimeDonationsManager.class, "onetimeDonationNotFound"); private static final String ONETIME_DONATION_NOT_FOUND_COUNTER_NAME = name(OneTimeDonationsManager.class, "onetimeDonationNotFound");
private final String table; private final String table;
private final Duration ttl;
private final DynamoDbAsyncClient dynamoDbAsyncClient; private final DynamoDbAsyncClient dynamoDbAsyncClient;
public OneTimeDonationsManager( public OneTimeDonationsManager(
@Nonnull String table, @Nonnull String table,
@Nonnull Duration ttl,
@Nonnull DynamoDbAsyncClient dynamoDbAsyncClient) { @Nonnull DynamoDbAsyncClient dynamoDbAsyncClient) {
this.table = Objects.requireNonNull(table); this.table = Objects.requireNonNull(table);
this.ttl = Objects.requireNonNull(ttl);
this.dynamoDbAsyncClient = Objects.requireNonNull(dynamoDbAsyncClient); this.dynamoDbAsyncClient = Objects.requireNonNull(dynamoDbAsyncClient);
} }
@ -55,7 +61,8 @@ public class OneTimeDonationsManager {
.tableName(table) .tableName(table)
.item(Map.of( .item(Map.of(
KEY_PAYMENT_ID, AttributeValues.fromString(paymentId), KEY_PAYMENT_ID, AttributeValues.fromString(paymentId),
ATTR_PAID_AT, AttributeValues.fromLong(paidAt.getEpochSecond()))) ATTR_PAID_AT, AttributeValues.fromLong(paidAt.getEpochSecond()),
ATTR_TTL, AttributeValues.fromLong(paidAt.plus(ttl).getEpochSecond())))
.build()) .build())
.thenApply(unused -> paymentId); .thenApply(unused -> paymentId);
} }

View File

@ -7,6 +7,7 @@ package org.whispersystems.textsecuregcm.storage;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -21,6 +22,7 @@ public class OnetimeDonationsManagerTest {
void beforeEach() { void beforeEach() {
oneTimeDonationsManager = new OneTimeDonationsManager( oneTimeDonationsManager = new OneTimeDonationsManager(
DynamoDbExtensionSchema.Tables.ONETIME_DONATIONS.tableName(), DynamoDbExtensionSchema.Tables.ONETIME_DONATIONS.tableName(),
Duration.ofDays(90),
DYNAMO_DB_EXTENSION.getDynamoDbAsyncClient()); DYNAMO_DB_EXTENSION.getDynamoDbAsyncClient());
} }