question

GaneshSharma-1710 avatar image
0 Votes"
GaneshSharma-1710 asked OuryBa-MSFT answered

Cosmos Client creation with resource token

Hi,

We got one requirement where we have to use Cosmos DB. We are using Azure Java SDK (azure-cosmos', version: '4.16.0).

We are generating CosmosClient object with Resource token which expires in an hour. So we have to re-instantiate CosmosClient with a new resource token.

This way we are unable to keep CosmosClient as Singleton (as recommended by Azure). We are ending up creating several CosmosClient objects on the expiry of the resource token causing memory leak.

Can you please suggest the best way of creating CosmosClient with Resource Token in Java (Sprinboot)?

We are referring to the below links for cosmos DB connection:

https://docs.microsoft.com/en-us/azure/cosmos-db/performance-tips-java-sdk-v4-sql?tabs=api-sync#sdk-usage

https://github.com/Azure-Samples/azure-cosmos-java-sql-api-samples/blob/main/src/main/java/com/azure/cosmos/examples/autoscaledatabasecrud/sync/AutoscaleDatabaseCRUDQuickstart.java


Thanks,
Ganesh

azure-cosmos-db
· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @GaneshSharma-1710 Thank you for posting you question on Microsoft Q&A. Could you please share any error message. In the meantime we are checking this internally with our Product Group and will get back shortly.

Regards,
Oury

0 Votes 0 ·

Thanks @OuryBa-MSFT for acknowledgment. As information shared above we getting out of memory error. We have analyzed the heap dump of our application and found several CosmosClient objects retaining big chunks of memory.

Below is the error excerpt:-
"exception"":""failed to establish connection to bn6prdddc05-docdb-2.documents.azure.com:14549 due to io.netty.handler.codec.EncoderException: java.lang.OutOfMemoryError: Cannot reserve 8388608 bytes of direct buffer memory (allocated: 2076650368, limit: 2080374784)

And plenty of below errors after 1 hr of token expiry. Looks likes azure cosmos library internally calls and use the first CosmosClient object created at the application start and after then keeps on trying using the same client to refresh the location.
com.azure.cosmos.implementation.GlobalEndpointManager [0;39m: startRefreshLocationTimerAsync() - Unable to refresh database account from any location. Exception: CosmosException{userAgent=azsdk-java-cosmos/4.5.0 Linux/4.15.0-135-generic JRE/13.0.2, error={"code":"Forbidden","message":"The authorization token is not valid at the current time. Please create another token and retry (token start time: Fri, 25 Jun 2021 11:15:49 GMT, token expiry time: Fri, 25 Jun 2021 12:15:49 GMT, current server time: Fri, 25 Jun 2021 12:26:01 GMT)})
So in nutshell,due to resource token, we are unable to keep CosmosClient as a singleton and getting bad performance from azure SDK

Regards,
Ganesh





0 Votes 0 ·

Hi @OuryBa-MSFT, did you get any chance to get a solution for the above query? Your help will be highly appriciated.

0 Votes 0 ·
Show more comments
ZhihaoGuo-3794 avatar image
0 Votes"
ZhihaoGuo-3794 answered ZhihaoGuo-3794 published

Hi @GaneshSharma-1710 , can you provide a mini demo in github repo, so that we can better understand your requirement.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

OuryBa-MSFT avatar image
0 Votes"
OuryBa-MSFT answered

Hi @GaneshSharma-1710

Thanks for letting us know that the issue has now been resolved. I am posting the resolution below. So that it will be useful for other community members facing the same.

Resolution: Deployed the fix by replacing the RTB token with RBAC implementation and it fixed the memory issue completely.

Regards,
Oury

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.