Describe the bug
Using the Cosmos SDK v4, I attempt to create a client using CosmosClientBuilder.
Both CosmosClientBuilder.buildAsyncClient() and CosmosClientBuilder.buildClient() fail with an assertion error.
Exception or Stack Trace
Assertion fails due to globalEndpointManager.getLatestDatabaseAccount() returning null during initialization:
java.lang.AssertionError
at com.azure.cosmos.implementation.RxDocumentClientImpl.initializeGatewayConfigurationReader(RxDocumentClientImpl.java:264)
at com.azure.cosmos.implementation.RxDocumentClientImpl.init(RxDocumentClientImpl.java:282)
at com.azure.cosmos.implementation.AsyncDocumentClient$Builder.build(AsyncDocumentClient.java:204)
at com.azure.cosmos.CosmosAsyncClient.<init>(CosmosAsyncClient.java:98)
at com.azure.cosmos.CosmosClientBuilder.buildAsyncClient(CosmosClientBuilder.java:649)
...
To Reproduce
Run the following code snippet. The code is executed during a test, while invoking it from @Test and @BeforeClass does not make a difference.
Code Snippet
new CosmosClientBuilder()
.endpoint(ENDPOINT)
.key(KEY)
.buildAsyncClient();
Expected behavior
Client is created successfully and can be used to create database and container objects.
Setup (please complete the following information):
com.azure:azure-cosmos:4.3.0Changing to Java openjdk version "1.8.0_262" makes no difference.
Library versions 4.2.0, 4.1.0 and 4.0.1 also fails with the assertion error.
GlobalEndpointManager::getDatabaseAccountAsync is called , but response is async and not processed in time. This results in latestDatabaseAccount field not set in GlobalEndpointManager. When RxDocumentClientImpl reads this value the value remains at null.
I have found the root issue: key is not correct. Meanwhile, client failed with AssertionError in case the key is not valid. Then the latestDatabaseAccount does not get populated. This causes creating a client to fail. Therefore it is not a race condition but simply not setting the field in case of an error. The key I had for Cosmos DB did not work initially.
Thanks for filing this GitHub issue @JanneKataja-TomTom, someone from the Cosmos team will follow up shortly
@JanneKataja-TomTom - closing this issue since the key used was incorrect, hopefully it works fine with the right key :)
@kushagraThapar - how are your customers going to know their access key is incorrect based on an AssertionError? This is a low-effort issue to fix for you and a potential time sink for your new customers trying out Cosmos DB.
@JanneKataja-TomTom - apologies for closing the issue, I thought we had better understanding of this issue here.
I did a test locally and provided incorrect key and got the below error -
@Test(groups = { "unit" })
public void testClientCreation() {
CosmosAsyncClient cosmosClient = new CosmosClientBuilder()
.endpoint("correct-endpoint")
.key("incorrect-key")
.buildAsyncClient();
cosmosClient.createDatabaseIfNotExists("test-database");
}
2020-10-01 10:16:37,847 [main] ERROR com.azure.cosmos.implementation.RxDocumentClientImpl - Client initialization failed. Check if the endpoint is reachable and if your auth token is valid
2020-10-01 10:16:37,847 [main] ERROR com.azure.cosmos.implementation.RxDocumentClientImpl - unexpected failure in initializing client.
java.lang.RuntimeException: Client initialization failed. Check if the endpoint is reachable and if your auth token is valid
at com.azure.cosmos.implementation.RxDocumentClientImpl.initializeGatewayConfigurationReader(RxDocumentClientImpl.java:351) ~[classes/:?]
at com.azure.cosmos.implementation.RxDocumentClientImpl.init(RxDocumentClientImpl.java:372) [classes/:?]
at com.azure.cosmos.implementation.AsyncDocumentClient$Builder.build(AsyncDocumentClient.java:218) [classes/:?]
at com.azure.cosmos.CosmosAsyncClient.<init>(CosmosAsyncClient.java:102) [classes/:?]
at com.azure.cosmos.CosmosClientBuilder.buildAsyncClient(CosmosClientBuilder.java:699) [classes/:?]
at com.azure.cosmos.implementation.SqlParameterTest.testClientCreation(SqlParameterTest.java:41) [test-classes/:?]
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?]
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?]
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124) [testng-6.14.3.jar:?]
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583) [testng-6.14.3.jar:?]
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719) [testng-6.14.3.jar:?]
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989) [testng-6.14.3.jar:?]
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) [testng-6.14.3.jar:?]
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) [testng-6.14.3.jar:?]
at org.testng.TestRunner.privateRun(TestRunner.java:648) [testng-6.14.3.jar:?]
at org.testng.TestRunner.run(TestRunner.java:505) [testng-6.14.3.jar:?]
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455) [testng-6.14.3.jar:?]
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450) [testng-6.14.3.jar:?]
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415) [testng-6.14.3.jar:?]
at org.testng.SuiteRunner.run(SuiteRunner.java:364) [testng-6.14.3.jar:?]
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) [testng-6.14.3.jar:?]
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84) [testng-6.14.3.jar:?]
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208) [testng-6.14.3.jar:?]
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137) [testng-6.14.3.jar:?]
at org.testng.TestNG.runSuites(TestNG.java:1049) [testng-6.14.3.jar:?]
at org.testng.TestNG.run(TestNG.java:1017) [testng-6.14.3.jar:?]
at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66) [testng-rt.jar:?]
at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109) [testng-rt.jar:?]
2020-10-01 10:16:37,849 [main] INFO com.azure.cosmos.implementation.RxDocumentClientImpl - Attempting to close client 0
2020-10-01 10:16:37,849 [main] INFO com.azure.cosmos.implementation.RxDocumentClientImpl - Shutting down ...
2020-10-01 10:16:37,850 [main] INFO com.azure.cosmos.implementation.RxDocumentClientImpl - Closing Global Endpoint Manager ...
2020-10-01 10:16:37,852 [main] INFO com.azure.cosmos.implementation.RxDocumentClientImpl - Closing StoreClientFactory ...
2020-10-01 10:16:37,853 [main] INFO com.azure.cosmos.implementation.RxDocumentClientImpl - Shutting down reactorHttpClient ...
2020-10-01 10:16:37,858 [main] INFO com.azure.cosmos.implementation.RxDocumentClientImpl - Shutting down CpuMonitor ...
2020-10-01 10:16:37,858 [main] INFO com.azure.cosmos.implementation.RxDocumentClientImpl - Shutting down completed.
I don't know why you are getting some AssertionErrors, can you please check your code and make sure you don't have any assertions.
It appears this bug was fixed as a part of #14742
Also, in case the domain name is incorrect, the exception is not thrown but instead the following error is logged in a loop:
2020-10-01 22:42:17.320 WARN 75928 --- [ctor-http-nio-5] c.a.c.i.WebExceptionRetryPolicy : Received retriable web exception, will retry
com.azure.cosmos.CosmosException: null
Will take a look at the null cosmos domain name, thanks for the feedback.