From efbf6dda9d67b2de668256bb4ed4b0e1fbd67fe5 Mon Sep 17 00:00:00 2001 From: nehrao1 <166562460+nehrao1@users.noreply.github.com> Date: Sat, 18 Jan 2025 14:38:12 -0800 Subject: [PATCH] Thinclient Environment Variables (#43188) * progress * add separate endpoint * progress * fix env var * progress * pr comments * cleanup * changelog * cleanup * pr comments * changelog * fix test * pr comment - fix tests --- .../cosmos/implementation/ConfigsTests.java | 90 ++++++++++++++----- sdk/cosmos/azure-cosmos/CHANGELOG.md | 1 + .../azure/cosmos/implementation/Configs.java | 37 ++++++++ 3 files changed, 108 insertions(+), 20 deletions(-) diff --git a/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/ConfigsTests.java b/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/ConfigsTests.java index f071799b08ccb..26c34a1c423c7 100644 --- a/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/ConfigsTests.java +++ b/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/ConfigsTests.java @@ -10,6 +10,7 @@ import io.netty.handler.ssl.SslContext; import org.testng.annotations.Test; +import java.net.URI; import java.util.EnumSet; import static org.assertj.core.api.Assertions.assertThat; @@ -77,10 +78,13 @@ public void httpConnectionWithoutTLSAllowed() { Configs config = new Configs(); assertThat(config.isHttpConnectionWithoutTLSAllowed()).isFalse(); - System.setProperty("COSMOS.HTTP_CONNECTION_WITHOUT_TLS_ALLOWED", "true"); - assertThat(config.isHttpConnectionWithoutTLSAllowed()).isTrue(); - System.clearProperty("COSMOS.HTTP_CONNECTION_WITHOUT_TLS_ALLOWED"); + System.setProperty("COSMOS.HTTP_CONNECTION_WITHOUT_TLS_ALLOWED", "true"); + try { + assertThat(config.isHttpConnectionWithoutTLSAllowed()).isTrue(); + } finally { + System.clearProperty("COSMOS.HTTP_CONNECTION_WITHOUT_TLS_ALLOWED"); + } } @Test(groups = { "emulator" }) @@ -88,10 +92,13 @@ public void emulatorCertValidationDisabled() { Configs config = new Configs(); assertThat(config.isEmulatorServerCertValidationDisabled()).isFalse(); - System.setProperty("COSMOS.EMULATOR_SERVER_CERTIFICATE_VALIDATION_DISABLED", "true"); - assertThat(config.isEmulatorServerCertValidationDisabled()).isTrue(); - System.clearProperty("COSMOS.EMULATOR_SERVER_CERTIFICATE_VALIDATION_DISABLED"); + System.setProperty("COSMOS.EMULATOR_SERVER_CERTIFICATE_VALIDATION_DISABLED", "true"); + try { + assertThat(config.isEmulatorServerCertValidationDisabled()).isTrue(); + } finally { + System.clearProperty("COSMOS.EMULATOR_SERVER_CERTIFICATE_VALIDATION_DISABLED"); + } } @Test(groups = { "emulator" }) @@ -99,49 +106,92 @@ public void emulatorHost() { Configs config = new Configs(); assertThat(config.getEmulatorHost()).isEmpty(); - System.setProperty("COSMOS.EMULATOR_HOST", "randomHost"); - assertThat(config.getEmulatorHost()).isEqualTo("randomHost"); - System.clearProperty("COSMOS.EMULATOR_HOST"); + System.setProperty("COSMOS.EMULATOR_HOST", "randomHost"); + try { + assertThat(config.getEmulatorHost()).isEqualTo("randomHost"); + } finally { + System.clearProperty("COSMOS.EMULATOR_HOST"); + } } @Test(groups = { "emulator" }) public void http2Enabled() { assertThat(Configs.isHttp2Enabled()).isFalse(); - System.setProperty("COSMOS.HTTP2_ENABLED", "true"); - assertThat(Configs.isHttp2Enabled()).isTrue(); - System.clearProperty("COSMOS.HTTP2_ENABLED"); + System.setProperty("COSMOS.HTTP2_ENABLED", "true"); + try { + assertThat(Configs.isHttp2Enabled()).isTrue(); + } finally { + System.clearProperty("COSMOS.HTTP2_ENABLED"); + } } @Test(groups = { "unit" }) public void http2MaxConnectionPoolSize() { assertThat(Configs.getHttp2MaxConnectionPoolSize()).isEqualTo(1000); - System.setProperty("COSMOS.HTTP2_MAX_CONNECTION_POOL_SIZE", "10"); - assertThat(Configs.getHttp2MaxConnectionPoolSize()).isEqualTo(10); - System.clearProperty("COSMOS.HTTP2_MAX_CONNECTION_POOL_SIZE"); + System.setProperty("COSMOS.HTTP2_MAX_CONNECTION_POOL_SIZE", "10"); + try { + assertThat(Configs.getHttp2MaxConnectionPoolSize()).isEqualTo(10); + } finally { + System.clearProperty("COSMOS.HTTP2_MAX_CONNECTION_POOL_SIZE"); + } } @Test(groups = { "unit" }) public void http2MinConnectionPoolSize() { assertThat(Configs.getHttp2MinConnectionPoolSize()).isEqualTo(1); - System.setProperty("COSMOS.HTTP2_MIN_CONNECTION_POOL_SIZE", "10"); - assertThat(Configs.getHttp2MinConnectionPoolSize()).isEqualTo(10); - System.clearProperty("COSMOS.HTTP2_MIN_CONNECTION_POOL_SIZE"); + System.setProperty("COSMOS.HTTP2_MIN_CONNECTION_POOL_SIZE", "10"); + try { + assertThat(Configs.getHttp2MinConnectionPoolSize()).isEqualTo(10); + } finally { + System.clearProperty("COSMOS.HTTP2_MIN_CONNECTION_POOL_SIZE"); + } } @Test(groups = { "unit" }) public void http2MaxConcurrentStreams() { assertThat(Configs.getHttp2MaxConcurrentStreams()).isEqualTo(30); + System.clearProperty("COSMOS.HTTP2_MAX_CONCURRENT_STREAMS"); System.setProperty("COSMOS.HTTP2_MAX_CONCURRENT_STREAMS", "10"); - assertThat(Configs.getHttp2MaxConcurrentStreams()).isEqualTo(10); + try { + assertThat(Configs.getHttp2MaxConcurrentStreams()).isEqualTo(10); + } finally { + System.clearProperty("COSMOS.HTTP2_MAX_CONCURRENT_STREAMS"); + } + } - System.clearProperty("COSMOS.HTTP2_MAX_CONCURRENT_STREAMS"); + @Test(groups = { "unit" }) + public void thinClientEnabledTest() { + Configs config = new Configs(); + assertThat(config.getThinclientEnabled()).isFalse(); + + System.clearProperty("COSMOS.THINCLIENT_ENABLED"); + System.setProperty("COSMOS.THINCLIENT_ENABLED", "true"); + try { + assertThat(config.getThinclientEnabled()).isTrue(); + } finally { + System.clearProperty("COSMOS.THINCLIENT_ENABLED"); + } + } + + @Test(groups = { "unit" }) + public void thinClientEndpointTest() { + Configs config = new Configs(); + assertThat(config.getThinclientEndpoint()).isEqualTo(URI.create("")); + + System.clearProperty("COSMOS.THINCLIENT_ENDPOINT"); + System.setProperty("COSMOS.THINCLIENT_ENDPOINT", "testThinClientEndpoint"); + try { + assertThat(config.getThinclientEndpoint()).isEqualTo(URI.create("testThinClientEndpoint")); + } finally { + System.clearProperty("COSMOS.THINCLIENT_ENDPOINT"); + } } } diff --git a/sdk/cosmos/azure-cosmos/CHANGELOG.md b/sdk/cosmos/azure-cosmos/CHANGELOG.md index 71a32eb345023..530f017b21333 100644 --- a/sdk/cosmos/azure-cosmos/CHANGELOG.md +++ b/sdk/cosmos/azure-cosmos/CHANGELOG.md @@ -9,6 +9,7 @@ #### Bugs Fixed #### Other Changes +* Added temporary internal-only option to enable thin client mode with system property COSMOS.THINCLIENT_ENABLED, setting the thin client endpoint with system property COSMOS.THINCLIENT_ENDPOINT, and default thin client endpoint with system property COSMOS.DEFAULT_THINCLIENT_ENDPOINT while the thin-client transport is still under development. This transport mode is not yet supported or ready to be used by external customers. Please don't use these configs in any production scenario yet. - [PR 43188](https://github.com/Azure/azure-sdk-for-java/pull/43188) ### 4.66.0 (2025-01-14) diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/Configs.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/Configs.java index 7f07616919e06..7544eb4539dfa 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/Configs.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/Configs.java @@ -14,8 +14,11 @@ import org.slf4j.LoggerFactory; import javax.net.ssl.SSLException; +import java.net.URI; +import java.net.URISyntaxException; import java.time.Duration; import java.util.Locale; +import java.util.Objects; import static com.azure.cosmos.implementation.guava25.base.MoreObjects.firstNonNull; import static com.azure.cosmos.implementation.guava25.base.Strings.emptyToNull; @@ -44,6 +47,12 @@ public class Configs { private static final String UNAVAILABLE_LOCATIONS_EXPIRATION_TIME_IN_SECONDS = "COSMOS.UNAVAILABLE_LOCATIONS_EXPIRATION_TIME_IN_SECONDS"; private static final String GLOBAL_ENDPOINT_MANAGER_INITIALIZATION_TIME_IN_SECONDS = "COSMOS.GLOBAL_ENDPOINT_MANAGER_MAX_INIT_TIME_IN_SECONDS"; + private static final String DEFAULT_THINCLIENT_ENDPOINT = ""; + private static final String THINCLIENT_ENDPOINT = "COSMOS.THINCLIENT_ENDPOINT"; + private static final String THINCLIENT_ENDPOINT_VARIABLE = "COSMOS_THINCLIENT_ENDPOINT"; + private static final boolean DEFAULT_THINCLIENT_ENABLED = false; + private static final String THINCLIENT_ENABLED = "COSMOS.THINCLIENT_ENABLED"; + private static final String THINCLIENT_ENABLED_VARIABLE = "COSMOS_THINCLIENT_ENABLED"; private static final String MAX_HTTP_BODY_LENGTH_IN_BYTES = "COSMOS.MAX_HTTP_BODY_LENGTH_IN_BYTES"; private static final String MAX_HTTP_INITIAL_LINE_LENGTH_IN_BYTES = "COSMOS.MAX_HTTP_INITIAL_LINE_LENGTH_IN_BYTES"; @@ -405,6 +414,34 @@ public int getGlobalEndpointManagerMaxInitializationTimeInSeconds() { return getJVMConfigAsInt(GLOBAL_ENDPOINT_MANAGER_INITIALIZATION_TIME_IN_SECONDS, DEFAULT_GLOBAL_ENDPOINT_MANAGER_INITIALIZATION_TIME_IN_SECONDS); } + public URI getThinclientEndpoint() { + String valueFromSystemProperty = System.getProperty(THINCLIENT_ENDPOINT); + if (valueFromSystemProperty != null && !valueFromSystemProperty.isEmpty()) { + return URI.create(valueFromSystemProperty); + } + + String valueFromEnvVariable = System.getenv(THINCLIENT_ENDPOINT_VARIABLE); + if (valueFromEnvVariable != null && !valueFromEnvVariable.isEmpty()) { + return URI.create(valueFromEnvVariable); + } + + return URI.create(DEFAULT_THINCLIENT_ENDPOINT); + } + + public static boolean getThinclientEnabled() { + String valueFromSystemProperty = System.getProperty(THINCLIENT_ENABLED); + if (valueFromSystemProperty != null && !valueFromSystemProperty.isEmpty()) { + return Boolean.parseBoolean(valueFromSystemProperty); + } + + String valueFromEnvVariable = System.getenv(THINCLIENT_ENABLED_VARIABLE); + if (valueFromEnvVariable != null && !valueFromEnvVariable.isEmpty()) { + return Boolean.parseBoolean(valueFromEnvVariable); + } + + return DEFAULT_THINCLIENT_ENABLED; + } + public int getUnavailableLocationsExpirationTimeInSeconds() { return getJVMConfigAsInt(UNAVAILABLE_LOCATIONS_EXPIRATION_TIME_IN_SECONDS, DEFAULT_UNAVAILABLE_LOCATIONS_EXPIRATION_TIME_IN_SECONDS); }