Skip to content

Commit

Permalink
spotless apply
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammad Awawdi <[email protected]>
  • Loading branch information
Muhammad-awawdi-amazon committed Feb 5, 2025
1 parent 465bea2 commit b78cf51
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ public abstract class BaseClientConfiguration {
private final Integer inflightRequestsLimit;

/**
* Availability Zone of the client. If ReadFrom strategy is AZAffinity or AZAffinityReplicasAndPrimary, this setting ensures that
* readonly commands are directed to nodes within the specified AZ if exits.
* Availability Zone of the client. If ReadFrom strategy is AZAffinity or
* AZAffinityReplicasAndPrimary, this setting ensures that readonly commands are directed to nodes
* within the specified AZ if exits.
*/
private final String clientAZ;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public enum ReadFrom {
*/
AZ_AFFINITY,
/**
* Spread the read requests among nodes within the client's Availability Zone (AZ) in a round robin manner,
* prioritizing local replicas, then the local primary, and falling back to any replica or the primary if needed.
* Spread the read requests among nodes within the client's Availability Zone (AZ) in a round
* robin manner, prioritizing local replicas, then the local primary, and falling back to any
* replica or the primary if needed.
*/
AZ_AFFINITY_REPLICAS_AND_PRIMARY,
}
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public void test_convert_config_with_azaffinity_to_protobuf() {
public void test_convert_config_with_azaffinity_replicas_and_primary_to_protobuf() {
testConvertConfigWithAzAffinity(ReadFrom.AZ_AFFINITY_REPLICAS_AND_PRIMARY);
}

private void testConvertConfigWithAzAffinity(ReadFrom readFrom) throws Exception {
// setup
String az = "us-east-1a";
Expand Down Expand Up @@ -330,7 +330,8 @@ public void test_az_affinity_without_client_az_throws_ConfigurationError() {
@SneakyThrows
@Test
public void test_az_affinity_replicas_and_primary_without_client_az_throws_ConfigurationError() {
testAzAffinityWithoutClientAzThrowsConfigurationError(ReadFrom.AZ_AFFINITY_REPLICAS_AND_PRIMARY);
testAzAffinityWithoutClientAzThrowsConfigurationError(
ReadFrom.AZ_AFFINITY_REPLICAS_AND_PRIMARY);
}

private void testAzAffinityWithoutClientAzThrowsConfigurationError(ReadFrom readFrom) {
Expand Down
80 changes: 46 additions & 34 deletions java/integTest/src/test/java/glide/ConnectionTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -329,73 +329,85 @@ public void test_connection_timeout(boolean clusterMode) {
@Test
public void test_az_affinity_replicas_and_primary_routes_to_primary() {
assumeTrue(SERVER_VERSION.isGreaterThanOrEqualTo("8.0.0"), "Skip for versions below 8");

String az = "us-east-1a";
String otherAz = "us-east-1b";
int nGetCalls = 4;
String getCmdstat = String.format("cmdstat_get:calls=%d", nGetCalls);

// Create client for setting the configs
GlideClusterClient configSetClient =
GlideClusterClient.createClient(azClusterClientConfig().requestTimeout(2000).build()).get();

// Reset stats and set all nodes to other_az
assertEquals(configSetClient.configResetStat().get(), OK);
configSetClient.configSet(Map.of("availability-zone", otherAz), ALL_NODES).get();

// Set primary for slot 12182 to az
configSetClient.configSet(
Map.of("availability-zone", az),
new RequestRoutingConfiguration.SlotIdRoute(12182, PRIMARY)
).get();

configSetClient
.configSet(
Map.of("availability-zone", az),
new RequestRoutingConfiguration.SlotIdRoute(12182, PRIMARY))
.get();

// Verify primary AZ
ClusterValue<Map<String, String>> primaryAzResult = configSetClient.configGet(
new String[]{"availability-zone"},
new RequestRoutingConfiguration.SlotIdRoute(12182, PRIMARY)
).get();
assertEquals(az, primaryAzResult.getSingleValue().get("availability-zone"),
ClusterValue<Map<String, String>> primaryAzResult =
configSetClient
.configGet(
new String[] {"availability-zone"},
new RequestRoutingConfiguration.SlotIdRoute(12182, PRIMARY))
.get();
assertEquals(
az,
primaryAzResult.getSingleValue().get("availability-zone"),
"Primary for slot 12182 is not in the expected AZ " + az);

configSetClient.close();

// Create test client with AZ_AFFINITY_REPLICAS_AND_PRIMARY configuration
GlideClusterClient azTestClient = GlideClusterClient.createClient(
azClusterClientConfig()
.readFrom(ReadFrom.AZ_AFFINITY_REPLICAS_AND_PRIMARY)
.clientAZ(az)
.requestTimeout(2000)
.build()
).get();

GlideClusterClient azTestClient =
GlideClusterClient.createClient(
azClusterClientConfig()
.readFrom(ReadFrom.AZ_AFFINITY_REPLICAS_AND_PRIMARY)
.clientAZ(az)
.requestTimeout(2000)
.build())
.get();

// Execute GET commands
for (int i = 0; i < nGetCalls; i++) {
azTestClient.get("foo").get();
}

ClusterValue<String> infoResult =
azTestClient.info(new InfoOptions.Section[] {InfoOptions.Section.ALL}, ALL_NODES).get();
Map<String, String> infoData = infoResult.getMultiValue();

// Check that only the primary in the specified AZ handled all GET calls
long matchingEntries =
infoData.values().stream()
.filter(value -> value.contains(getCmdstat) && value.contains(az) && value.contains("role:master"))
.filter(
value ->
value.contains(getCmdstat)
&& value.contains(az)
&& value.contains("role:master"))
.count();
assertEquals(1, matchingEntries, "Exactly one primary in AZ should handle all calls");

// Verify total GET calls
long totalGetCalls =
infoData.values().stream()
.filter(value -> value.contains("cmdstat_get:calls="))
.mapToInt(value -> {
int startIndex = value.indexOf("cmdstat_get:calls=") + "cmdstat_get:calls=".length();
int endIndex = value.indexOf(",", startIndex);
return Integer.parseInt(value.substring(startIndex, endIndex));
})
.mapToInt(
value -> {
int startIndex =
value.indexOf("cmdstat_get:calls=") + "cmdstat_get:calls=".length();
int endIndex = value.indexOf(",", startIndex);
return Integer.parseInt(value.substring(startIndex, endIndex));
})
.sum();
assertEquals(nGetCalls, totalGetCalls, "Total GET calls mismatch");

azTestClient.close();
}
}

0 comments on commit b78cf51

Please sign in to comment.