azure-sdk-for-python icon indicating copy to clipboard operation
azure-sdk-for-python copied to clipboard

Cosmos: RecordDiagnostics.request_charge is always 0

Open shabbyrobe opened this issue 6 years ago • 12 comments

  • Package Name: azure-cosmos
  • Package Version: 4.0.0
  • Operating System: macOS Catalina
  • Python Version: 3.6

Describe the bug

RecordDiagnostics.request_charge is always 0 after calling container.query_items.

To Reproduce

Setup:

python -m venv venv
source venv/bin/activate
pip install azure-cosmos==4.0.0

Query code:

from azure.cosmos.cosmos_client import CosmosClient
from azure.cosmos.diagnostics import RecordDiagnostics

db_name = '...'
container_name = '...'

cosmos = CosmosClient(...)
database = cosmos.get_database_client(db_name)
container = database.get_container_client(container_name)
diag = RecordDiagnostics()
items = list(container.query_items(
    'SELECT VALUE COUNT(1) FROM c,
    enable_cross_partition_query=True,
    populate_query_metrics=True,
    response_hook=diag,
))
print(diag.request_charge)
# 0.0

Expected behavior

I expect to see a non-zero number.

shabbyrobe avatar May 27 '20 02:05 shabbyrobe

Thanks @shabbyrobe - taking a look now.

annatisch avatar May 28 '20 22:05 annatisch

Hi @shabbyrobe, I tried adding your query code to one of our tests:

container = create_multi_partition_collection_with_custom_pk_if_not_exist(self.client)
diag = RecordDiagnostics()
items = list(container.query_items(
    'SELECT VALUE COUNT(1) FROM c',
    enable_cross_partition_query=True,
    populate_query_metrics=True,
    response_hook=diag,
))
assert len(items) > 0
assert diag.headers
assert diag.request_charge > 0

And this test is passing - ('x-ms-request-charge': '2.25')

@southpolesteve - could you please confirm the expected service behaviour of this header? Maybe I need to tweak my test to repro this?

annatisch avatar May 28 '20 22:05 annatisch

Thanks heaps for looking into it, @annatisch. I tried my script again in case it was just an intermittent gremlin, but I still get the same result. Could it possibly be that I'm doing something silly and using a bad combination of versions for the relevant libs? I can try to post a "minimum viable requirements.txt" file if it would help?

shabbyrobe avatar May 29 '20 06:05 shabbyrobe

Thanks @shabbyrobe - I think a lib version issue seems unlikely - as I believe in that scenario the code snippet above wouldn't work at all and we would see more fundamental errors... Admittedly I did run my repro in Python 3.7, so I will try it again in 3.6 just to rule that out.

However I'm not an expert in the Cosmos service behaviour. @southpolesteve could this difference be related to how our Cosmos accounts/resources are configured?

annatisch avatar May 29 '20 13:05 annatisch

@annatisch I'm not seeing anything out of the ordinary between the two examples.

COUNT queries are a bit tricky as they need to run till completion. The requests are aggregated on the client and you might have to call an iterator multiple times. Still getting a grasp on Python, is the list() call in the code do that? It may be that since your example has only a few docs, the query is done on the first call and the RUs are populated. In the OP's example, if the iterator needs more calls, the query pipeline may not have populated the RU value yet.

southpolesteve avatar Jun 03 '20 04:06 southpolesteve

Thanks @southpolesteve - you're correct in that the cast to list() will iterate the query to completion, so the code in both of our samples should indicate a completed query.

annatisch avatar Jun 03 '20 22:06 annatisch

Relabeling this as a bug. The cross partition query code has some bugs around edge cases and properly accounting RUs. I have hit these in JavaScript as well.

southpolesteve avatar Jul 02 '20 04:07 southpolesteve

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @anfeldma-ms, @zfoster, @southpolesteve.

ghost avatar Jul 30 '20 15:07 ghost

@srinathnarayanan I took a look at this a while back but was unable to find the bug. Can you take a look? The CP query code is a bit dense for me.

southpolesteve avatar Sep 08 '20 00:09 southpolesteve

Since we have been unable to reproduce this issue, I am going to close it. If anyone is able to come up with a stand alone repro, please let me know.

southpolesteve avatar Oct 07 '20 19:10 southpolesteve

I tried the reproduction steps I provided again, with 4.0.0 and now with 4.1.0, and the problem persists. I don't believe this should be closed. If I can provide more debugging information to assist with reproducing on your own machines, please let me know.

shabbyrobe avatar Oct 07 '20 22:10 shabbyrobe

This is rather late (now v4.2.0), but in case anyone else looks at this for reference - in the OPs post, the request charge is not populated because you don't do anything with the 'items' you defined using the iterator returned by cosmosdb. As soon as you call print(list) or len(list), as in the test done by @annatisch, the request charge would be populated. Given only a single result is expected, using next(iter(<iterator>)) also works for getting the container item count:

# get container item count
m = RecordDiagnostics()
q = container.query_items(query='SELECT VALUE COUNT(1) from c', 
                          enable_cross_partition_query=True, 
                          populate_query_metrics = True, 
                          response_hook=m)
print('{} documents in container "{}" ({} RU)'.format(next(iter(q)),cName,m.request_charge))

@southpolesteve regarding your comment on delay of query metrics population, I'm finding that the setting of max_item_count has a delayed effect on the RU reported:

# get Top 1000 items
q = container.query_items(query='SELECT TOP 1000 * from c', 
                          enable_cross_partition_query=True, 
                          populate_query_metrics = True, 
                          response_hook=m, 
                          max_item_count=10)


print(len(list(q)))                   # -> always reports 1000
print(m.request_charge)               # -> correlates with previous execution's max_item_count
print(m.headers['x-ms-item-count'])   # -> reflects previous execution's max_item_count

print(m.request_charge) and print(m.headers['x-ms-item-count'] reflect the values of the previous execution of the cell above, for example if max_item_count was changed from 10 to 100, the reported charge and item count will still reflect 10.

Aside from that delay, I'm unclear whether the reported values reflect the true cost of the len(list(q) statement if that is able to answer with an array length that is greater than the max item count.

lindacmsheard avatar Feb 07 '21 11:02 lindacmsheard