Skip to content

Commit a58305c

Browse files
committed
fix some errors
1 parent 2982e3e commit a58305c

File tree

2 files changed

+45
-40
lines changed

2 files changed

+45
-40
lines changed

source/s3_util.c

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const uint64_t g_default_part_size_fallback = MB_TO_BYTES(8);
7878
const uint64_t g_default_max_part_size = 5368709120ULL;
7979

8080
/* TODO: Use a reasonable alignment with the update of the buffer pool */
81-
const uint64_t g_s3_optimal_range_size_alignment = MB_TO_BYTES(32);
81+
const uint64_t g_s3_optimal_range_size_alignment = 1;
8282
/**
8383
* The most parts in memory will be:
8484
* - All downloaded parts to deliver them in the right order (download)
@@ -786,7 +786,14 @@ int aws_s3_check_headers_for_checksum(
786786
return AWS_OP_SUCCESS;
787787
}
788788

789-
int aws_s3_range_size_alignment(uint64_t *range_size) {}
789+
static void s_s3_range_size_alignment(uint64_t *range_size) {
790+
/* TODO: Round up the part size should be part of the memory pool impl. We should let memory pool to decide
791+
* what's the best round up for the part size. */
792+
if (*range_size > g_s3_optimal_range_size_alignment && *range_size % g_s3_optimal_range_size_alignment != 0) {
793+
/* Only apply alignment for sizes above minimum to avoid excessive rounding */
794+
*range_size = ((*range_size / g_s3_optimal_range_size_alignment) + 1) * g_s3_optimal_range_size_alignment;
795+
}
796+
}
790797

791798
int aws_s3_calculate_client_optimal_range_size(
792799
uint64_t memory_limit_in_bytes,
@@ -821,12 +828,7 @@ int aws_s3_calculate_client_optimal_range_size(
821828
if (optimal_size < g_default_part_size_fallback) {
822829
optimal_size = g_default_part_size_fallback;
823830
} else {
824-
/* Only apply alignment for sizes above minimum to avoid excessive rounding */
825-
/* TODO: Round up the part size should be part of the memory pool impl. We should let memory pool to decide
826-
* what's the best round up for the part size. */
827-
if (optimal_size > g_s3_optimal_range_size_alignment && optimal_size % g_s3_optimal_range_size_alignment != 0) {
828-
optimal_size = ((optimal_size / g_s3_optimal_range_size_alignment) + 1) * g_s3_optimal_range_size_alignment;
829-
}
831+
s_s3_range_size_alignment(&optimal_size);
830832
}
831833

832834
/* Apply maximum constraint */
@@ -875,14 +877,7 @@ int aws_s3_calculate_request_optimal_range_size(
875877
if (optimal_size < g_default_part_size_fallback) {
876878
optimal_size = g_default_part_size_fallback;
877879
} else {
878-
/* Only apply alignment for sizes above minimum to avoid excessive rounding */
879-
/* TODO: Round up the part size should be part of the memory pool impl. We should let memory pool to decide
880-
* what's the best round up for the part size. */
881-
if (optimal_size > g_s3_optimal_range_size_alignment &&
882-
optimal_size % g_s3_optimal_range_size_alignment != 0) {
883-
optimal_size =
884-
((optimal_size / g_s3_optimal_range_size_alignment) + 1) * g_s3_optimal_range_size_alignment;
885-
}
880+
s_s3_range_size_alignment(&optimal_size);
886881
}
887882
}
888883

tests/s3_util_tests.c

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,15 @@ static int s_test_s3_aws_xml_get_body_at_path(struct aws_allocator *allocator, v
782782
return AWS_OP_SUCCESS;
783783
}
784784

785+
static void s_s3_range_size_alignment(uint64_t *range_size) {
786+
/* TODO: Round up the part size should be part of the memory pool impl. We should let memory pool to decide
787+
* what's the best round up for the part size. */
788+
if (*range_size > g_s3_optimal_range_size_alignment && *range_size % g_s3_optimal_range_size_alignment != 0) {
789+
/* Only apply alignment for sizes above minimum to avoid excessive rounding */
790+
*range_size = ((*range_size / g_s3_optimal_range_size_alignment) + 1) * g_s3_optimal_range_size_alignment;
791+
}
792+
}
793+
785794
AWS_TEST_CASE(test_s3_calculate_client_optimal_range_size, s_test_s3_calculate_client_optimal_range_size)
786795
static int s_test_s3_calculate_client_optimal_range_size(struct aws_allocator *allocator, void *ctx) {
787796
(void)ctx;
@@ -799,9 +808,9 @@ static int s_test_s3_calculate_client_optimal_range_size(struct aws_allocator *a
799808

800809
/* Expected: 1GB / 10 / 3 = 34.13MB -> rounded up to alignment boundary */
801810
uint64_t memory_constrained = memory_limit / max_connections / 3;
802-
uint64_t aligned_size =
803-
((memory_constrained / g_s3_optimal_range_size_alignment) + 1) * g_s3_optimal_range_size_alignment;
804-
uint64_t expected = (aligned_size < g_default_part_size_fallback) ? g_default_part_size_fallback : aligned_size;
811+
s_s3_range_size_alignment(&memory_constrained);
812+
uint64_t expected =
813+
(memory_constrained < g_default_part_size_fallback) ? g_default_part_size_fallback : memory_constrained;
805814
ASSERT_UINT_EQUALS(expected, client_optimal_range_size);
806815
}
807816

@@ -841,9 +850,9 @@ static int s_test_s3_calculate_client_optimal_range_size(struct aws_allocator *a
841850

842851
/* Expected: 300MB / 10 / 3 = 10MB -> rounded up to alignment boundary */
843852
uint64_t memory_constrained = memory_limit / max_connections / 3;
844-
uint64_t aligned_size =
845-
((memory_constrained / g_s3_optimal_range_size_alignment) + 1) * g_s3_optimal_range_size_alignment;
846-
uint64_t expected = (aligned_size < g_default_part_size_fallback) ? g_default_part_size_fallback : aligned_size;
853+
s_s3_range_size_alignment(&memory_constrained);
854+
uint64_t expected =
855+
(memory_constrained < g_default_part_size_fallback) ? g_default_part_size_fallback : memory_constrained;
847856
ASSERT_UINT_EQUALS(expected, client_optimal_range_size);
848857
}
849858

@@ -880,72 +889,73 @@ static int s_test_s3_calculate_request_optimal_range_size(struct aws_allocator *
880889

881890
/* Test 1: Estimated part size is larger than client size - use client size */
882891
{
883-
uint64_t client_optimal_range_size = g_s3_optimal_range_size_alignment * 5; /* 160 MB (5 * 32MB) */
884-
uint64_t estimated_part_size = MB_TO_BYTES(200); /* 200 MB */
892+
uint64_t client_optimal_range_size = MB_TO_BYTES(160); /* 160 MB (5 * 32MB) */
893+
uint64_t estimated_part_size = MB_TO_BYTES(200); /* 200 MB */
885894

886895
ASSERT_SUCCESS(aws_s3_calculate_request_optimal_range_size(
887896
client_optimal_range_size, estimated_part_size, &request_optimal_range_size));
888897

889898
/* Expected: min(160MB, 200MB) = 160MB */
890-
uint64_t expected = g_s3_optimal_range_size_alignment * 5; /* 160 MB (5 * 32MB) */
899+
uint64_t expected = client_optimal_range_size; /* 160 MB (5 * 32MB) */
900+
s_s3_range_size_alignment(&expected);
891901
ASSERT_UINT_EQUALS(expected, request_optimal_range_size);
892902
}
893903

894904
/* Test 2: Estimated part size is smaller than client size - use estimated size */
895905
{
896-
uint64_t client_optimal_range_size = g_s3_optimal_range_size_alignment * 8; /* 64 MB (8 * 8MB) */
897-
uint64_t estimated_part_size = MB_TO_BYTES(50); /* 50 MB */
906+
uint64_t client_optimal_range_size = MB_TO_BYTES(64); /* 64 MB */
907+
uint64_t estimated_part_size = MB_TO_BYTES(50); /* 50 MB */
898908

899909
ASSERT_SUCCESS(aws_s3_calculate_request_optimal_range_size(
900910
client_optimal_range_size, estimated_part_size, &request_optimal_range_size));
901911

902912
/* Expected: min(64MB, 50MB) = 50MB -> rounded up to alignment boundary */
903913
uint64_t smaller_size = estimated_part_size; /* 50MB is smaller than 64MB */
904-
uint64_t aligned_size =
905-
((smaller_size / g_s3_optimal_range_size_alignment) + 1) * g_s3_optimal_range_size_alignment;
906-
uint64_t expected = (aligned_size < g_default_part_size_fallback) ? g_default_part_size_fallback : aligned_size;
914+
s_s3_range_size_alignment(&smaller_size);
915+
uint64_t expected = (smaller_size < g_default_part_size_fallback) ? g_default_part_size_fallback : smaller_size;
907916
ASSERT_UINT_EQUALS(expected, request_optimal_range_size);
908917
}
909918

910919
/* Test 3: Estimated part size is very small - apply minimum constraint */
911920
{
912-
uint64_t client_optimal_range_size = g_s3_optimal_range_size_alignment * 5; /* 160 MB (5 * 32MB) */
913-
uint64_t estimated_part_size = MB_TO_BYTES(1); /* 1 MB */
921+
uint64_t client_optimal_range_size = MB_TO_BYTES(160); /* 160 MB */
922+
uint64_t estimated_part_size = MB_TO_BYTES(1); /* 1 MB */
914923

915924
ASSERT_SUCCESS(aws_s3_calculate_request_optimal_range_size(
916925
client_optimal_range_size, estimated_part_size, &request_optimal_range_size));
917926

918927
/* Expected: min(160MB, 1MB) = minimum size (minimum constraint) */
919928
uint64_t expected = g_default_part_size_fallback; /* minimum size */
929+
s_s3_range_size_alignment(&expected);
920930
ASSERT_UINT_EQUALS(expected, request_optimal_range_size);
921931
}
922932

923933
/* Test 4: Zero estimated part size (unknown) - use client size */
924934
{
925-
uint64_t client_optimal_range_size = g_s3_optimal_range_size_alignment * 5; /* 160 MB (5 * 32MB) */
926-
uint64_t estimated_part_size = 0; /* 0 - unknown */
935+
uint64_t client_optimal_range_size = MB_TO_BYTES(160); /* 160 MB */
936+
uint64_t estimated_part_size = 0; /* 0 - unknown */
927937

928938
ASSERT_SUCCESS(aws_s3_calculate_request_optimal_range_size(
929939
client_optimal_range_size, estimated_part_size, &request_optimal_range_size));
930940

931941
/* Expected: use client_optimal_range_size when estimated is unknown */
932-
uint64_t expected = g_s3_optimal_range_size_alignment * 5; /* 160 MB (5 * 32MB) */
942+
uint64_t expected = client_optimal_range_size; /* 160 MB (5 * 32MB) */
943+
s_s3_range_size_alignment(&expected);
933944
ASSERT_UINT_EQUALS(expected, request_optimal_range_size);
934945
}
935946

936947
/* Test 5: Alignment to buffer pool boundaries */
937948
{
938-
uint64_t client_optimal_range_size = g_s3_optimal_range_size_alignment * 5; /* 40 MB (5 * 8MB) */
939-
uint64_t estimated_part_size = MB_TO_BYTES(9); /* 9 MB */
949+
uint64_t client_optimal_range_size = MB_TO_BYTES(40); /* 40 MB (5 * 8MB) */
950+
uint64_t estimated_part_size = MB_TO_BYTES(9); /* 9 MB */
940951

941952
ASSERT_SUCCESS(aws_s3_calculate_request_optimal_range_size(
942953
client_optimal_range_size, estimated_part_size, &request_optimal_range_size));
943954

944955
/* Expected: min(40MB, 9MB) = 9MB -> rounded up to alignment boundary */
945956
uint64_t smaller_size = estimated_part_size; /* 9MB is smaller than 40MB */
946-
uint64_t aligned_size =
947-
((smaller_size / g_s3_optimal_range_size_alignment) + 1) * g_s3_optimal_range_size_alignment;
948-
uint64_t expected = (aligned_size < g_default_part_size_fallback) ? g_default_part_size_fallback : aligned_size;
957+
s_s3_range_size_alignment(&smaller_size);
958+
uint64_t expected = (smaller_size < g_default_part_size_fallback) ? g_default_part_size_fallback : smaller_size;
949959
ASSERT_UINT_EQUALS(expected, request_optimal_range_size);
950960
}
951961

0 commit comments

Comments
 (0)