Introduce a DynamoDB-backed remote config store

This commit is contained in:
Jon Chambers
2021-11-29 12:44:37 -05:00
committed by Jon Chambers
parent 4eb7dde1c8
commit 23bc11f3b6
10 changed files with 275 additions and 82 deletions

View File

@@ -50,17 +50,20 @@ public class DynamoDbTables {
private final TableWithExpiration redeemedReceipts;
private final Table subscriptions;
private final Table profiles;
private final Table remoteConfig;
@JsonCreator
public DynamoDbTables(
@JsonProperty("issuedReceipts") final IssuedReceiptsTableConfiguration issuedReceipts,
@JsonProperty("redeemedReceipts") final TableWithExpiration redeemedReceipts,
@JsonProperty("subscriptions") final Table subscriptions,
@JsonProperty("profiles") final Table profiles) {
@JsonProperty("profiles") final Table profiles,
@JsonProperty("remoteConfig") final Table remoteConfig) {
this.issuedReceipts = issuedReceipts;
this.redeemedReceipts = redeemedReceipts;
this.subscriptions = subscriptions;
this.profiles = profiles;
this.remoteConfig = remoteConfig;
}
@Valid
@@ -86,4 +89,10 @@ public class DynamoDbTables {
public Table getProfiles() {
return profiles;
}
@Valid
@NotNull
public Table getRemoteConfig() {
return remoteConfig;
}
}

View File

@@ -0,0 +1,17 @@
/*
* Copyright 2013-2021 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.whispersystems.textsecuregcm.storage;
import java.util.List;
public interface RemoteConfigStore {
void set(RemoteConfig remoteConfig);
List<RemoteConfig> getAll();
void delete(String name);
}

View File

@@ -15,7 +15,7 @@ import java.util.UUID;
import org.whispersystems.textsecuregcm.storage.mappers.RemoteConfigRowMapper;
import org.whispersystems.textsecuregcm.util.Constants;
public class RemoteConfigs {
public class RemoteConfigs implements RemoteConfigStore {
public static final String ID = "id";
public static final String NAME = "name";
@@ -38,6 +38,7 @@ public class RemoteConfigs {
this.database.getDatabase().registerArrayType(UUID.class, "uuid");
}
@Override
public void set(RemoteConfig remoteConfig) {
database.use(jdbi -> jdbi.useHandle(handle -> {
try (Timer.Context ignored = setTimer.time()) {
@@ -53,6 +54,7 @@ public class RemoteConfigs {
}));
}
@Override
public List<RemoteConfig> getAll() {
return database.with(jdbi -> jdbi.withHandle(handle -> {
try (Timer.Context ignored = getAllTimer.time()) {
@@ -63,6 +65,7 @@ public class RemoteConfigs {
}));
}
@Override
public void delete(String name) {
database.use(jdbi -> jdbi.useHandle(handle -> {
try (Timer.Context ignored = deleteTimer.time()) {

View File

@@ -0,0 +1,117 @@
/*
* Copyright 2013-2021 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.whispersystems.textsecuregcm.storage;
import org.whispersystems.textsecuregcm.util.AttributeValues;
import org.whispersystems.textsecuregcm.util.UUIDUtil;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest;
import software.amazon.awssdk.services.dynamodb.model.PutItemRequest;
import software.amazon.awssdk.services.dynamodb.model.ScanRequest;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
public class RemoteConfigsDynamoDb implements RemoteConfigStore {
private final DynamoDbClient dynamoDbClient;
private final String tableName;
// Config name; string
static final String KEY_NAME = "N";
// Rollout percentage; integer
private static final String ATTR_PERCENTAGE = "P";
// Enrolled UUIDs (ACIs); list of byte arrays
private static final String ATTR_UUIDS = "U";
// Default value; string
private static final String ATTR_DEFAULT_VALUE = "D";
// Value when enrolled; string
private static final String ATTR_VALUE = "V";
// Hash key; string
private static final String ATTR_HASH_KEY = "H";
public RemoteConfigsDynamoDb(final DynamoDbClient dynamoDbClient, final String tableName) {
this.dynamoDbClient = dynamoDbClient;
this.tableName = tableName;
}
@Override
public void set(final RemoteConfig remoteConfig) {
final Map<String, AttributeValue> item = new HashMap<>(Map.of(
KEY_NAME, AttributeValues.fromString(remoteConfig.getName()),
ATTR_PERCENTAGE, AttributeValues.fromInt(remoteConfig.getPercentage())));
if (remoteConfig.getUuids() != null && !remoteConfig.getUuids().isEmpty()) {
final List<SdkBytes> uuidByteSets = remoteConfig.getUuids().stream()
.map(UUIDUtil::toByteBuffer)
.map(SdkBytes::fromByteBuffer)
.collect(Collectors.toList());
item.put(ATTR_UUIDS, AttributeValue.builder().bs(uuidByteSets).build());
}
if (remoteConfig.getDefaultValue() != null) {
item.put(ATTR_DEFAULT_VALUE, AttributeValues.fromString(remoteConfig.getDefaultValue()));
}
if (remoteConfig.getValue() != null) {
item.put(ATTR_VALUE, AttributeValues.fromString(remoteConfig.getValue()));
}
if (remoteConfig.getHashKey() != null) {
item.put(ATTR_HASH_KEY, AttributeValues.fromString(remoteConfig.getHashKey()));
}
dynamoDbClient.putItem(PutItemRequest.builder()
.tableName(tableName)
.item(item)
.build());
}
@Override
public List<RemoteConfig> getAll() {
return dynamoDbClient.scanPaginator(ScanRequest.builder()
.tableName(tableName)
.consistentRead(true)
.build())
.items()
.stream()
.map(item -> {
final String name = AttributeValues.getString(item, KEY_NAME, null);
final int percentage = AttributeValues.getInt(item, ATTR_PERCENTAGE, 0);
final String defaultValue = AttributeValues.getString(item, ATTR_DEFAULT_VALUE, null);
final String value = AttributeValues.getString(item, ATTR_VALUE, null);
final String hashKey = AttributeValues.getString(item, ATTR_HASH_KEY, null);
final Set<UUID> uuids;
if (item.containsKey(ATTR_UUIDS)) {
uuids = item.get(ATTR_UUIDS).bs().stream()
.map(sdkBytes -> UUIDUtil.fromByteBuffer(sdkBytes.asByteBuffer()))
.collect(Collectors.toSet());
} else {
uuids = Collections.emptySet();
}
return new RemoteConfig(name, percentage, uuids, defaultValue, value, hashKey);
})
.collect(Collectors.toList());
}
@Override
public void delete(final String name) {
dynamoDbClient.deleteItem(DeleteItemRequest.builder()
.tableName(tableName)
.key(Map.of(KEY_NAME, AttributeValues.fromString(name)))
.build());
}
}

View File

@@ -12,11 +12,11 @@ import java.util.function.Supplier;
public class RemoteConfigsManager {
private final RemoteConfigs remoteConfigs;
private final RemoteConfigStore remoteConfigs;
private final Supplier<List<RemoteConfig>> remoteConfigSupplier;
public RemoteConfigsManager(RemoteConfigs remoteConfigs) {
public RemoteConfigsManager(RemoteConfigStore remoteConfigs) {
this.remoteConfigs = remoteConfigs;
remoteConfigSupplier =