Skip to content

Commit

Permalink
remove appconfig in favor of S3ObjectMonitor
Browse files Browse the repository at this point in the history
  • Loading branch information
jkt-signal authored Oct 31, 2024
1 parent 63021e0 commit 0018e0b
Show file tree
Hide file tree
Showing 16 changed files with 132 additions and 432 deletions.
10 changes: 6 additions & 4 deletions service/config/sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,12 @@ callingZkConfig:
backupsZkConfig:
serverSecret: secret://backupsZkConfig.serverSecret

appConfig:
application: example
environment: example
configuration: example
dynamicConfig:
s3Region: a-region
s3Bucket: a-bucket
objectKey: dynamic-config.yaml
maxSize: 100000
refreshInterval: PT10S

remoteConfig:
globalConfig: # keys and values that are given to clients on GET /v1/config
Expand Down
8 changes: 0 additions & 8 deletions service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,6 @@
<groupId>software.amazon.awssdk</groupId>
<artifactId>dynamodb</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>appconfig</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>appconfigdata</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>dynamodb-lock-client</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.whispersystems.textsecuregcm.configuration.DefaultAwsCredentialsFactory;
import org.whispersystems.textsecuregcm.configuration.DirectoryV2Configuration;
import org.whispersystems.textsecuregcm.configuration.DogstatsdConfiguration;
import org.whispersystems.textsecuregcm.configuration.DynamicConfigurationManagerFactory;
import org.whispersystems.textsecuregcm.configuration.DynamoDbClientFactory;
import org.whispersystems.textsecuregcm.configuration.DynamoDbTables;
import org.whispersystems.textsecuregcm.configuration.ExternalRequestFilterConfiguration;
Expand Down Expand Up @@ -244,7 +243,7 @@ public class WhisperServerConfiguration extends Configuration {
@Valid
@NotNull
@JsonProperty
private DynamicConfigurationManagerFactory appConfig;
private S3ObjectMonitorFactory dynamicConfig;

@Valid
@NotNull
Expand Down Expand Up @@ -487,8 +486,8 @@ public RemoteConfigConfiguration getRemoteConfigConfiguration() {
return remoteConfig;
}

public DynamicConfigurationManagerFactory getAppConfig() {
return appConfig;
public S3ObjectMonitorFactory getDynamicConfig() {
return dynamicConfig;
}

public BadgesConfiguration getBadges() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,9 @@ public void run(WhisperServerConfiguration config, Environment environment) thro
ScheduledExecutorService dynamicConfigurationExecutor = environment.lifecycle()
.scheduledExecutorService(name(getClass(), "dynamicConfiguration-%d")).threads(1).build();

DynamicConfigurationManager<DynamicConfiguration> dynamicConfigurationManager = config.getAppConfig()
.build(DynamicConfiguration.class, dynamicConfigurationExecutor, awsCredentialsProvider);
DynamicConfigurationManager<DynamicConfiguration> dynamicConfigurationManager =
new DynamicConfigurationManager<>(
config.getDynamicConfig().build(awsCredentialsProvider, dynamicConfigurationExecutor), DynamicConfiguration.class);
dynamicConfigurationManager.start();

MetricsUtil.configureRegistries(config, environment, dynamicConfigurationManager);
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,31 @@

import static org.whispersystems.textsecuregcm.metrics.MetricsUtil.name;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.annotations.VisibleForTesting;
import io.micrometer.core.instrument.Metrics;
import java.time.Duration;

import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.whispersystems.textsecuregcm.s3.S3ObjectMonitor;
import org.whispersystems.textsecuregcm.util.SystemMapper;
import org.whispersystems.textsecuregcm.util.Util;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.services.appconfigdata.AppConfigDataClient;
import software.amazon.awssdk.services.appconfigdata.model.GetLatestConfigurationRequest;
import software.amazon.awssdk.services.appconfigdata.model.GetLatestConfigurationResponse;
import software.amazon.awssdk.services.appconfigdata.model.StartConfigurationSessionRequest;
import software.amazon.awssdk.services.appconfigdata.model.StartConfigurationSessionResponse;

public class DynamicConfigurationManager<T> {

private final String application;
private final String environment;
private final String configurationName;
private final AppConfigDataClient appConfigClient;
private final S3ObjectMonitor configMonitor;
private final Class<T> configurationClass;

// Set on initial config fetch
private final AtomicReference<T> configuration = new AtomicReference<>();
private final CountDownLatch initialized = new CountDownLatch(1);
private final ScheduledExecutorService scheduledExecutorService;
private String configurationToken = null;

private static final Validator VALIDATOR = Validation.buildDefaultValidatorFactory().getValidator();

Expand All @@ -54,28 +41,9 @@ public class DynamicConfigurationManager<T> {

private static final Logger logger = LoggerFactory.getLogger(DynamicConfigurationManager.class);

public DynamicConfigurationManager(String application, String environment, String configurationName,
AwsCredentialsProvider awsCredentialsProvider, Class<T> configurationClass,
ScheduledExecutorService scheduledExecutorService) {
this(AppConfigDataClient
.builder()
.credentialsProvider(awsCredentialsProvider)
.overrideConfiguration(ClientOverrideConfiguration.builder()
.apiCallTimeout(Duration.ofSeconds(10))
.apiCallAttemptTimeout(Duration.ofSeconds(10)).build())
.build(),
application, environment, configurationName, configurationClass, scheduledExecutorService);
}

@VisibleForTesting
DynamicConfigurationManager(AppConfigDataClient appConfigClient, String application, String environment,
String configurationName, Class<T> configurationClass, ScheduledExecutorService scheduledExecutorService) {
this.appConfigClient = appConfigClient;
this.application = application;
this.environment = environment;
this.configurationName = configurationName;
public DynamicConfigurationManager(final S3ObjectMonitor configMonitor, final Class<T> configurationClass) {
this.configMonitor = configMonitor;
this.configurationClass = configurationClass;
this.scheduledExecutorService = scheduledExecutorService;
}

public T getConfiguration() {
Expand All @@ -88,92 +56,63 @@ public T getConfiguration() {
return configuration.get();
}

public void start() {
public synchronized void start() {
if (initialized.getCount() == 0) {
return;
}
configuration.set(retrieveInitialDynamicConfiguration());
initialized.countDown();

scheduledExecutorService.scheduleWithFixedDelay(() -> {
this.configMonitor.start(this::receiveConfiguration);

// Starting an S3ObjectMonitor immediately does a blocking retrieve of the data, but it might
// fail to parse, in which case we wait for an update (which will happen on another thread) to
// give us a valid configuration before marking ourselves ready
while (configuration.get() == null) {
logger.warn("Failed to retrieve or parse initial dynamic configuration");
try {
retrieveDynamicConfiguration().ifPresent(configuration::set);
} catch (Exception e) {
logger.warn("Error retrieving dynamic configuration", e);
}
}, 0, 5, TimeUnit.SECONDS);
this.wait();
} catch (InterruptedException e) {}
}
initialized.countDown();
}

private Optional<T> retrieveDynamicConfiguration() throws JsonProcessingException {
if (configurationToken == null) {
logger.error("Invalid configuration token, will not be able to fetch configuration updates");
}
GetLatestConfigurationResponse latestConfiguration;
private synchronized void receiveConfiguration(InputStream configDataStream) {
final String configData;
try {
latestConfiguration = appConfigClient.getLatestConfiguration(GetLatestConfigurationRequest.builder()
.configurationToken(configurationToken)
.build());
// token to use in the next fetch
configurationToken = latestConfiguration.nextPollConfigurationToken();
logger.debug("next token: {}", configurationToken);
} catch (final RuntimeException e) {
configData = new String(configDataStream.readAllBytes());
} catch (IOException e) {
Metrics.counter(ERROR_COUNTER_NAME, ERROR_TYPE_TAG_NAME, "fetch").increment();
throw e;
return;
}

if (!latestConfiguration.configuration().asByteBuffer().hasRemaining()) {
// empty configuration means nothing has changed
return Optional.empty();
}
logger.info("Received new config of length {}, next configuration token: {}",
latestConfiguration.configuration().asByteBuffer().remaining(),
configurationToken);
logger.info("Received new dynamic configuration of length {}", configData.length());
parseConfiguration(configData, configurationClass).ifPresent(configuration::set);
this.notify();
}

@VisibleForTesting
public static <T> Optional<T> parseConfiguration(final String configurationYaml, final Class<T> configurationClass) {
final T configuration;
try {
return parseConfiguration(latestConfiguration.configuration().asUtf8String(), configurationClass);
} catch (final JsonProcessingException e) {
configuration = SystemMapper.yamlMapper().readValue(configurationYaml, configurationClass);
} catch (final IOException e) {
logger.warn("Failed to parse dynamic configuration", e);
Metrics.counter(ERROR_COUNTER_NAME,
ERROR_TYPE_TAG_NAME, "parse",
CONFIG_CLASS_TAG_NAME, configurationClass.getName()).increment();
throw e;
return Optional.empty();
}
}

@VisibleForTesting
public static <T> Optional<T> parseConfiguration(final String configurationYaml, final Class<T> configurationClass)
throws JsonProcessingException {
final T configuration = SystemMapper.yamlMapper().readValue(configurationYaml, configurationClass);
final Set<ConstraintViolation<T>> violations = VALIDATOR.validate(configuration);

final Optional<T> maybeDynamicConfiguration;

if (violations.isEmpty()) {
maybeDynamicConfiguration = Optional.of(configuration);
} else {
if (!violations.isEmpty()) {
logger.warn("Failed to validate configuration: {}", violations);
maybeDynamicConfiguration = Optional.empty();
Metrics.counter(ERROR_COUNTER_NAME,
ERROR_TYPE_TAG_NAME, "validate",
CONFIG_CLASS_TAG_NAME, configurationClass.getName()).increment();
return Optional.empty();
}

return maybeDynamicConfiguration;
return Optional.of(configuration);
}

private T retrieveInitialDynamicConfiguration() {
for (;;) {
try {
if (configurationToken == null) {
// first time around, start the configuration session
final StartConfigurationSessionResponse startResponse = appConfigClient
.startConfigurationSession(StartConfigurationSessionRequest.builder()
.applicationIdentifier(application)
.environmentIdentifier(environment)
.configurationProfileIdentifier(configurationName).build());
configurationToken = startResponse.initialConfigurationToken();
}
return retrieveDynamicConfiguration().orElseThrow(() -> new IllegalStateException("No initial configuration available"));
} catch (Exception e) {
logger.warn("Error retrieving initial dynamic configuration", e);
Util.sleep(1000);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package org.whispersystems.textsecuregcm.workers;

import com.fasterxml.jackson.core.JsonProcessingException;
import io.dropwizard.core.cli.Command;
import io.dropwizard.core.setup.Bootstrap;
import java.nio.file.Files;
Expand Down Expand Up @@ -37,12 +36,7 @@ public void configure(final Subparser subparser) {
}

private boolean isValid(final Class<?> configurationClass, final String yamlConfig) {
try {
return DynamicConfigurationManager.parseConfiguration(yamlConfig, configurationClass).isPresent();
} catch (JsonProcessingException e) {
System.err.println(e.getMessage());
return false;
}
return DynamicConfigurationManager.parseConfiguration(yamlConfig, configurationClass).isPresent();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ static CommandDependencies build(
ScheduledExecutorService dynamicConfigurationExecutor = environment.lifecycle()
.scheduledExecutorService(name(name, "dynamicConfiguration-%d")).threads(1).build();

DynamicConfigurationManager<DynamicConfiguration> dynamicConfigurationManager = configuration.getAppConfig().build(
DynamicConfiguration.class, dynamicConfigurationExecutor, awsCredentialsProvider);
DynamicConfigurationManager<DynamicConfiguration> dynamicConfigurationManager =
new DynamicConfigurationManager<>(
configuration.getDynamicConfig().build(awsCredentialsProvider, dynamicConfigurationExecutor), DynamicConfiguration.class);
dynamicConfigurationManager.start();

final ClientResources.Builder redisClientResourcesBuilder = ClientResources.builder();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
org.whispersystems.textsecuregcm.configuration.AwsCredentialsProviderFactory
org.whispersystems.textsecuregcm.configuration.DatadogConfiguration
org.whispersystems.textsecuregcm.configuration.DynamicConfigurationManagerFactory
org.whispersystems.textsecuregcm.configuration.DynamoDbClientFactory
org.whispersystems.textsecuregcm.configuration.FaultTolerantRedisClusterFactory
org.whispersystems.textsecuregcm.configuration.FaultTolerantRedisClientFactory
Expand Down
Loading

0 comments on commit 0018e0b

Please sign in to comment.