Home > Software engineering >  Force Azure CosmosDB Java SDK Read Latest Value
Force Azure CosmosDB Java SDK Read Latest Value

Time:11-24

I am setting the TTL on my Cosmos Container to 1 to force the deletion of all items, I then query SELECT VALUE COUNT(1) from c to check that all items are deleted before setting TTL back to its previous value.

My issue is, I can see via the portal that the items are deleted but my query via the SDK returns the "old" wrong value for an inordinate time. Is there a way to force it to read the "real" value from the back end or establish a fresh connection etc?

        //I create my client like so, setting Consistencylevel.STRONG will throw an error as 
        //it is higher level than the DB

        CosmosClient cosmosClient= new CosmosClientBuilder().endpoint(DATABASE_HOST)
        .key(DATABASE_KEY)
        .consistencyLevel(ConsistencyLevel.SESSION)
        .contentResponseOnWriteEnabled(true)
        .buildClient();
         //get the database
        CosmosDatabase dataBase = cosmosClient.getDatabase(databaseName);
        return dataBase;
        //get the container
        CosmosContainer container = theDatabase.getContainer(containerProps.getId());#
        //update the TTL
        containerProps.setDefaultTimeToLiveInSeconds(1);
        container.replace(containerProps);
        Thread.sleep(1000);
         //now confirm that the container contents are deleted
         //i tried refreshing my client/db/container objects to see if it would help
         CosmosClient refreshedCosmosClient = createSyncCosmosClient();
         CosmosDatabase refreshedDatabase =  refreshedCosmosClient.getDatabase(DATABASE_NAME);
         CosmosContainer refreshedContainer = refreshedDatabase.getContainer(container.getId());

    //query the number of ITEMS in the container
    CosmosPagedIterable<JsonNode> countOfDocs = 
    refreshedContainer.queryItems(CHECK_CONTAINER_EMPTY_QUERY, new CosmosQueryRequestOptions(),
    JsonNode.class);
    context.getLogger().info("wooooooooooooooooaaaa"  countOfDocs.toString());
    //THIS VALUE IS NOT UP TO DATE. IT IS THE OLD VALUE
    JsonNode count = countOfDocs.iterator().next();
    int numberOfDocuments = count.asInt();

CodePudding user response:

When you set TTL to 1 second, yes, it will eventually delete all the documents but this does not happen instantaneously. Depending on the volume of data, this can take some time, what happens is that the documents that are in the process of deletion by TTL cannot be seen by read operations (hence the COUNT shows 0)

If you disable TTL and there are documents still in the process of TTL deletion, then those are now back to being accessible (because the process by which they were being deleted is disabled).

Reference: https://learn.microsoft.com/azure/cosmos-db/nosql/time-to-live

Deletion of expired items is a background task that consumes left-over Request Units, that is Request Units that haven't been consumed by user requests. Even after the TTL has expired, if the container is overloaded with requests and if there aren't enough RU's available, the data deletion is delayed. Data is deleted once there are enough RUs available to perform the delete operation. Though the data deletion is delayed, data is not returned by any queries (by any API) after the TTL has expired.

CodePudding user response:

I can set the ConsistencyLevel to BOUNDED_STALENESS @5 seconds, this seems to improve things. I cannot use ConsistencyLevel.STRONG

  • Related