Skip to content

Commit

Permalink
Bind memory_limit_in_bytes variable (#753)
Browse files Browse the repository at this point in the history
  • Loading branch information
waahm7 authored Jan 26, 2024
1 parent 73d4171 commit 63c9a00
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion crt/aws-c-s3
6 changes: 4 additions & 2 deletions src/main/java/software/amazon/awssdk/crt/s3/S3Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ public S3Client(S3ClientOptions options) throws CrtRuntimeException {
monitoringThroughputThresholdInBytesPerSecond,
monitoringFailureIntervalInSeconds,
options.getEnableS3Express(),
options.getS3ExpressCredentialsProviderFactory()));
options.getS3ExpressCredentialsProviderFactory(),
options.getMemoryLimitInBytes()));

addReferenceTo(options.getClientBootstrap());
if(didCreateSigningConfig) {
Expand Down Expand Up @@ -225,7 +226,8 @@ private static native long s3ClientNew(S3Client thisObj, byte[] region, long cli
long monitoringThroughputThresholdInBytesPerSecond,
int monitoringFailureIntervalInSeconds,
boolean enableS3Express,
S3ExpressCredentialsProviderFactory s3expressCredentialsProviderFactory) throws CrtRuntimeException;
S3ExpressCredentialsProviderFactory s3expressCredentialsProviderFactory,
long memoryLimitInBytes) throws CrtRuntimeException;

private static native void s3ClientDestroy(long client);

Expand Down
24 changes: 24 additions & 0 deletions src/main/java/software/amazon/awssdk/crt/s3/S3ClientOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class S3ClientOptions {
private long initialReadWindowSize;
private int maxConnections;
private boolean enableS3Express;
private long memoryLimitInBytes;
private S3ExpressCredentialsProviderFactory s3expressCredentialsProviderFactory;
/**
* For multi-part upload, content-md5 will be calculated if the
Expand Down Expand Up @@ -346,4 +347,27 @@ public S3ClientOptions withS3ExpressCredentialsProviderFactory(S3ExpressCredenti
public S3ExpressCredentialsProviderFactory getS3ExpressCredentialsProviderFactory() {
return s3expressCredentialsProviderFactory;
}

/**
* The amount of memory the CRT client is allowed to use.
* The client makes a best-effort attempt at memory limiting but might exceed this limit in some cases.
* If not provided, the client calculates this optimally from other settings, such as targetThroughput.
* On a 64-bit system, the default is between 2Gib-8Gib.
* It must be at least 1GiB and will be capped to SIZE_MAX of the system.
* @param memoryLimitBytes Memory limit in bytes.
* @return this
*/
public S3ClientOptions withMemoryLimitInBytes(long memoryLimitBytes) {
this.memoryLimitInBytes = memoryLimitBytes;
return this;
}

/**
* Retrieves the memory limit set for the CRT client in bytes.
* If not set, this will return 0.
* @return long memory limit in bytes
*/
public long getMemoryLimitInBytes() {
return memoryLimitInBytes;
}
}
5 changes: 4 additions & 1 deletion src/native/s3_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ JNIEXPORT jlong JNICALL Java_software_amazon_awssdk_crt_s3_S3Client_s3ClientNew(
jlong jni_monitoring_throughput_threshold_in_bytes_per_second,
jint jni_monitoring_failure_interval_in_seconds,
jboolean enable_s3express,
jobject java_s3express_provider_factory) {
jobject java_s3express_provider_factory,
jlong jni_memory_limit_bytes_jlong) {
(void)jni_class;
aws_cache_jni_ids(env);

Expand All @@ -359,6 +360,7 @@ JNIEXPORT jlong JNICALL Java_software_amazon_awssdk_crt_s3_S3Client_s3ClientNew(

uint64_t part_size = (uint64_t)part_size_jlong;
uint64_t multipart_upload_threshold = (uint64_t)multipart_upload_threshold_jlong;
uint64_t memory_limit_in_bytes = (uint64_t)jni_memory_limit_bytes_jlong;

size_t initial_read_window;
if (aws_size_t_from_java(env, &initial_read_window, initial_read_window_jlong, "Initial read window")) {
Expand Down Expand Up @@ -456,6 +458,7 @@ JNIEXPORT jlong JNICALL Java_software_amazon_awssdk_crt_s3_S3Client_s3ClientNew(
.s3express_provider_override_factory =
java_s3express_provider_factory ? s_s3express_provider_jni_factory : NULL,
.factory_user_data = callback_data,
.memory_limit_in_bytes = memory_limit_in_bytes,
};

struct aws_http_connection_monitoring_options monitoring_options;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.junit.Test;
import software.amazon.awssdk.crt.CrtRuntimeException;
import software.amazon.awssdk.crt.Log;
import software.amazon.awssdk.crt.Log.LogLevel;
import software.amazon.awssdk.crt.auth.credentials.Credentials;
import software.amazon.awssdk.crt.auth.credentials.CredentialsProvider;
import software.amazon.awssdk.crt.auth.credentials.DefaultChainCredentialsProvider;
Expand Down Expand Up @@ -119,7 +118,8 @@ public void testS3ClientCreateDestroy() {
skipIfNetworkUnavailable();

S3ClientOptions clientOptions = new S3ClientOptions().withRegion(REGION)
.withComputeContentMd5(true);
.withComputeContentMd5(true)
.withMemoryLimitInBytes(5L * 1024 * 1024 * 1024);
try (S3Client client = createS3Client(clientOptions)) {

}
Expand Down

0 comments on commit 63c9a00

Please sign in to comment.