Skip to content

Commit

Permalink
Merge pull request #10 from SumoLogic/aws-sdk-v2
Browse files Browse the repository at this point in the history
Switching to AWS SDK v2, Java 8
  • Loading branch information
mccartney authored Nov 20, 2024
2 parents 015ac90 + 35a22db commit e25eb51
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 408 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@
*/
package com.netflix.config.sources;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughputExceededException;
import com.amazonaws.services.dynamodbv2.model.ScanRequest;
import com.amazonaws.services.dynamodbv2.model.ScanResult;
import com.netflix.config.DynamicLongProperty;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.config.DynamicStringProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughputExceededException;
import software.amazon.awssdk.services.dynamodb.model.ScanRequest;
import software.amazon.awssdk.services.dynamodb.model.ScanResponse;

import java.util.Map;

Expand Down Expand Up @@ -74,44 +70,13 @@ public abstract class AbstractDynamoDbConfigurationSource <T> {
protected DynamicLongProperty maxRetryCount = DynamicPropertyFactory.getInstance()
.getLongProperty(maxRetryCountPropertyName, defaultMaxRetryCount);

protected AmazonDynamoDB dbClient;
protected DynamoDbClient dbClient;

public AbstractDynamoDbConfigurationSource() {
this(new AmazonDynamoDBClient());
setEndpoint();
}

public AbstractDynamoDbConfigurationSource(ClientConfiguration clientConfiguration) {
this(new AmazonDynamoDBClient(clientConfiguration));
setEndpoint();
}

public AbstractDynamoDbConfigurationSource(AWSCredentials credentials) {
this(new AmazonDynamoDBClient(credentials));
setEndpoint();
}

public AbstractDynamoDbConfigurationSource(AWSCredentials credentials, ClientConfiguration clientConfiguration) {
this(new AmazonDynamoDBClient(credentials, clientConfiguration));
setEndpoint();
}

public AbstractDynamoDbConfigurationSource(AWSCredentialsProvider credentialsProvider) {
this(new AmazonDynamoDBClient(credentialsProvider));
setEndpoint();
}

public AbstractDynamoDbConfigurationSource(AWSCredentialsProvider credentialsProvider, ClientConfiguration clientConfiguration) {
this(new AmazonDynamoDBClient(credentialsProvider, clientConfiguration));
setEndpoint();
}

public AbstractDynamoDbConfigurationSource(AmazonDynamoDB dbClient) {
public AbstractDynamoDbConfigurationSource(DynamoDbClient dbClient) {
this.dbClient = dbClient;
}


protected ScanResult dbScanWithThroughputBackOff(ScanRequest scanRequest) {
protected ScanResponse dbScanWithThroughputBackOff(ScanRequest scanRequest) {
Long currentBackOffMs = minBackOffMs.get();
Long retryCount = 0L;
while (true) {
Expand Down Expand Up @@ -143,10 +108,4 @@ public void validateDb(){
loadPropertiesFromTable(table);
log.info("Successfully polled Dynamo for a new configuration based on table:" + table);
}

private void setEndpoint() {
String endpoint = endpointName.get();
dbClient.setEndpoint(endpoint);
log.info("Set Dynamo endpoint:" + endpoint);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,14 @@
*/
package com.netflix.config.sources;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.ScanRequest;
import com.amazonaws.services.dynamodbv2.model.ScanResult;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.config.DynamicStringProperty;
import com.netflix.config.PollResult;
import com.netflix.config.PolledConfigurationSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.ScanRequest;
import software.amazon.awssdk.services.dynamodb.model.ScanResponse;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -42,31 +36,7 @@
public class DynamoDbConfigurationSource extends AbstractDynamoDbConfigurationSource<Object> implements PolledConfigurationSource {
private static final Logger log = LoggerFactory.getLogger(DynamoDbConfigurationSource.class);

public DynamoDbConfigurationSource() {
super();
}

public DynamoDbConfigurationSource(ClientConfiguration clientConfiguration) {
super(clientConfiguration);
}

public DynamoDbConfigurationSource(AWSCredentials credentials) {
super(credentials);
}

public DynamoDbConfigurationSource(AWSCredentials credentials, ClientConfiguration clientConfiguration) {
super(credentials, clientConfiguration);
}

public DynamoDbConfigurationSource(AWSCredentialsProvider credentialsProvider) {
super(credentialsProvider);
}

public DynamoDbConfigurationSource(AWSCredentialsProvider credentialsProvider, ClientConfiguration clientConfiguration) {
super(credentialsProvider, clientConfiguration);
}

public DynamoDbConfigurationSource(AmazonDynamoDB dbClient) {
public DynamoDbConfigurationSource(DynamoDbClient dbClient) {
super(dbClient);
}

Expand All @@ -75,15 +45,16 @@ protected synchronized Map<String, Object> loadPropertiesFromTable(String table)
Map<String, Object> propertyMap = new HashMap<String, Object>();
Map<String, AttributeValue> lastKeysEvaluated = null;
do {
ScanRequest scanRequest = new ScanRequest()
.withTableName(table)
.withExclusiveStartKey(lastKeysEvaluated);
ScanResult result = dbScanWithThroughputBackOff(scanRequest);
for (Map<String, AttributeValue> item : result.getItems()) {
propertyMap.put(item.get(keyAttributeName.get()).getS(), item.get(valueAttributeName.get()).getS());
ScanRequest scanRequest = ScanRequest.builder()
.tableName(table)
.exclusiveStartKey(lastKeysEvaluated)
.build();
ScanResponse result = dbScanWithThroughputBackOff(scanRequest);
for (Map<String, AttributeValue> item : result.items()) {
propertyMap.put(item.get(keyAttributeName.get()).s(), item.get(valueAttributeName.get()).s());
}
lastKeysEvaluated = result.getLastEvaluatedKey();
} while (lastKeysEvaluated != null);
lastKeysEvaluated = result.lastEvaluatedKey();
} while (!lastKeysEvaluated.isEmpty());
return propertyMap;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.netflix.config.sources;

import com.netflix.config.*;
import org.apache.commons.configuration.AbstractConfiguration;

import java.util.HashMap;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@
*/
package com.netflix.config.sources;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.ScanRequest;
import com.amazonaws.services.dynamodbv2.model.ScanResult;
import com.netflix.config.*;
import com.netflix.config.DeploymentContext;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.config.DynamicStringProperty;
import com.netflix.config.PropertyWithDeploymentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.ScanRequest;
import software.amazon.awssdk.services.dynamodb.model.ScanResponse;

import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -70,134 +69,11 @@ public class DynamoDbDeploymentContextTableCache extends AbstractDynamoDbConfigu
private ScheduledExecutorService executor;
private volatile Map<String, PropertyWithDeploymentContext> cachedTable = new HashMap<String, PropertyWithDeploymentContext>();


public DynamoDbDeploymentContextTableCache() {
this(defaultInitialDelayMillis, defaultDelayMillis);
}

/**
* @param initialDelayMillis
* @param delayMillis
*/
public DynamoDbDeploymentContextTableCache(int initialDelayMillis, int delayMillis) {
super();
this.initialDelayMillis = initialDelayMillis;
this.delayMillis = delayMillis;
start();
}

/**
* @param clientConfiguration
*/
public DynamoDbDeploymentContextTableCache(ClientConfiguration clientConfiguration) {
this(clientConfiguration, defaultInitialDelayMillis, defaultDelayMillis);
}

/**
* @param clientConfiguration
* @param initialDelayMillis
* @param delayMillis
*/
public DynamoDbDeploymentContextTableCache(ClientConfiguration clientConfiguration, int initialDelayMillis, int delayMillis) {
super(clientConfiguration);
this.initialDelayMillis = initialDelayMillis;
this.delayMillis = delayMillis;
start();
}

/**
* @param credentials
*/
public DynamoDbDeploymentContextTableCache(AWSCredentials credentials) {
this(credentials, defaultInitialDelayMillis, defaultDelayMillis);
}

/**
* @param credentials
* @param initialDelayMillis
* @param delayMillis
*/
public DynamoDbDeploymentContextTableCache(AWSCredentials credentials, int initialDelayMillis, int delayMillis) {
super(credentials);
this.initialDelayMillis = initialDelayMillis;
this.delayMillis = delayMillis;
start();
}

/**
* @param credentials
* @param clientConfiguration
*/
public DynamoDbDeploymentContextTableCache(AWSCredentials credentials, ClientConfiguration clientConfiguration) {
this(credentials, clientConfiguration, defaultInitialDelayMillis, defaultDelayMillis);
}

/**
* @param credentials
* @param clientConfiguration
* @param initialDelayMillis
* @param delayMillis
*/
public DynamoDbDeploymentContextTableCache(AWSCredentials credentials, ClientConfiguration clientConfiguration, int initialDelayMillis, int delayMillis) {
super(credentials, clientConfiguration);
this.initialDelayMillis = initialDelayMillis;
this.delayMillis = delayMillis;
start();
}

/**
* @param credentialsProvider
*/
public DynamoDbDeploymentContextTableCache(AWSCredentialsProvider credentialsProvider) {
this(credentialsProvider, defaultInitialDelayMillis, defaultDelayMillis);
}

/**
* @param credentialsProvider
* @param initialDelayMillis
* @param delayMillis
*/
public DynamoDbDeploymentContextTableCache(AWSCredentialsProvider credentialsProvider, int initialDelayMillis, int delayMillis) {
super(credentialsProvider);
this.initialDelayMillis = initialDelayMillis;
this.delayMillis = delayMillis;
start();
}

/**
* @param credentialsProvider
* @param clientConfiguration
*/
public DynamoDbDeploymentContextTableCache(AWSCredentialsProvider credentialsProvider, ClientConfiguration clientConfiguration) {
this(credentialsProvider, clientConfiguration, defaultInitialDelayMillis, defaultDelayMillis);
}

/**
* @param credentialsProvider
* @param clientConfiguration
* @param initialDelayMillis
* @param delayMillis
*/
public DynamoDbDeploymentContextTableCache(AWSCredentialsProvider credentialsProvider, ClientConfiguration clientConfiguration, int initialDelayMillis, int delayMillis) {
super(credentialsProvider, clientConfiguration);
this.initialDelayMillis = initialDelayMillis;
this.delayMillis = delayMillis;
start();
}

/**
* @param dbClient
*/
public DynamoDbDeploymentContextTableCache(AmazonDynamoDB dbClient) {
public DynamoDbDeploymentContextTableCache(DynamoDbClient dbClient) {
this(dbClient, defaultInitialDelayMillis, defaultDelayMillis);
}

/**
* @param dbClient
* @param initialDelayMillis
* @param delayMillis
*/
public DynamoDbDeploymentContextTableCache(AmazonDynamoDB dbClient, int initialDelayMillis, int delayMillis) {
public DynamoDbDeploymentContextTableCache(DynamoDbClient dbClient, int initialDelayMillis, int delayMillis) {
super(dbClient);
this.initialDelayMillis = initialDelayMillis;
this.delayMillis = delayMillis;
Expand Down Expand Up @@ -258,27 +134,28 @@ protected Map<String, PropertyWithDeploymentContext> loadPropertiesFromTable(Str
Map<String, PropertyWithDeploymentContext> propertyMap = new HashMap<String, PropertyWithDeploymentContext>();
Map<String, AttributeValue> lastKeysEvaluated = null;
do {
ScanRequest scanRequest = new ScanRequest()
.withTableName(table)
.withExclusiveStartKey(lastKeysEvaluated);
ScanResult result = dbScanWithThroughputBackOff(scanRequest);
for (Map<String, AttributeValue> item : result.getItems()) {
String keyVal = item.get(keyAttributeName.get()).getS();
ScanRequest scanRequest = ScanRequest.builder()
.tableName(table)
.exclusiveStartKey(lastKeysEvaluated)
.build();
ScanResponse result = dbScanWithThroughputBackOff(scanRequest);
for (Map<String, AttributeValue> item : result.items()) {
String keyVal = item.get(keyAttributeName.get()).s();

//Need to deal with the fact that these attributes might not exist
DeploymentContext.ContextKey contextKey = item.containsKey(contextKeyAttributeName.get()) ? DeploymentContext.ContextKey.valueOf(item.get(contextKeyAttributeName.get()).getS()) : null;
String contextVal = item.containsKey(contextValueAttributeName.get()) ? item.get(contextValueAttributeName.get()).getS() : null;
DeploymentContext.ContextKey contextKey = item.containsKey(contextKeyAttributeName.get()) ? DeploymentContext.ContextKey.valueOf(item.get(contextKeyAttributeName.get()).s()) : null;
String contextVal = item.containsKey(contextValueAttributeName.get()) ? item.get(contextValueAttributeName.get()).s() : null;
String key = keyVal + ";" + contextKey + ";" + contextVal;
propertyMap.put(key,
new PropertyWithDeploymentContext(
contextKey,
contextVal,
keyVal,
item.get(valueAttributeName.get()).getS()
item.get(valueAttributeName.get()).s()
));
}
lastKeysEvaluated = result.getLastEvaluatedKey();
} while (lastKeysEvaluated != null);
lastKeysEvaluated = result.lastEvaluatedKey();
} while (!lastKeysEvaluated.isEmpty());
return propertyMap;
}

Expand Down
Loading

0 comments on commit e25eb51

Please sign in to comment.