Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ServiceBus] fix receive_messages receive and delete bug #31833

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sdk/servicebus/azure-servicebus/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

### Bugs Fixed

- Fixed a bug where receiving messages in `RECEIVE_AND_DELETE` mode while messages were being sent simultaneously resulted in messages not being returned ([#31711](https://github.com/Azure/azure-sdk-for-python/issues/31711)).

### Other Changes

## 7.11.1 (2023-07-12)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,8 @@ def enhanced_message_received(
if receiver._receive_context.is_set():
receiver._handler._received_messages.put((frame, message))
else:
if receiver._receive_mode == ServiceBusReceiveMode.RECEIVE_AND_DELETE:
Copy link
Member

@l0lawrence l0lawrence Aug 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just to double check have we tested iterator with receive/delete turned on to make sure we receive all the messages correctly

Copy link
Member Author

@swathipil swathipil Aug 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I ran the receiveanddelete queue tests and they all passed! was there any test in particular that you were thinking?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this need another check on frame & message if prefetch is set to 0 ( in the other PR), because then it wont have any preloaded messages or frames that would be added to received messages ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when prefetch is 0, it should be using the default _message_received callback, not this one. So I don't think it should have issues?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could move this up into the original if-statement?
For example:
if receiver._receive_context.is_set() or receiver._receive_mode == 'RECEIVE_AND_DELETE':

I would follow this up with a descriptive inline comment or method docstring about what circumstances we want to keep messages around, vs releasing them. This entire enhanced_message_received method seems to be missing a lot of documentation about the reason for its existence. Without that context, the design is hard to explain.
If I recall correctly, messages received in PEEK_LOCK need to be released to prevent them accumulating deliver-attempt counts when they expire without settlement - is that right?
If so, we should add a comment explaining how RECEIVE_AND_DELETE fits into that pattern.

receiver._handler._received_messages.put((frame, message))
receiver._handler.settle_messages(frame[1], 'released')

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,8 @@ def enhanced_message_received(
if receiver._receive_context.is_set():
receiver._handler._received_messages.put(message)
else:
if receiver._receive_mode == ServiceBusReceiveMode.RECEIVE_AND_DELETE:
receiver._handler._received_messages.put(message)
message.release()

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ async def enhanced_message_received_async(
if receiver._receive_context.is_set():
receiver._handler._received_messages.put((frame, message))
else:
if receiver._receive_mode == ServiceBusReceiveMode.RECEIVE_AND_DELETE:
receiver._handler._received_messages.put((frame, message))
await receiver._handler.settle_messages_async(frame[1], 'released')

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging
import sys
import os
import asyncio
import pytest
import time
from datetime import datetime, timedelta
Expand Down Expand Up @@ -231,3 +232,44 @@ async def test_subscription_message_expiry(self, uamqp_transport, *, servicebus_
assert len(messages) == 1
assert messages[0].delivery_count > 0
await receiver.complete_message(messages[0])

@pytest.mark.asyncio
@pytest.mark.liveTest
@pytest.mark.live_test_only
@CachedServiceBusResourceGroupPreparer(name_prefix='servicebustest')
@CachedServiceBusNamespacePreparer(name_prefix='servicebustest')
@ServiceBusTopicPreparer(name_prefix='servicebustest')
@ServiceBusSubscriptionPreparer(name_prefix='servicebustest', lock_duration='PT5S')
@pytest.mark.parametrize("uamqp_transport", uamqp_transport_params, ids=uamqp_transport_ids)
@ArgPasserAsync()
async def test_subscription_receive_and_delete_with_send_and_wait(self, uamqp_transport, *, servicebus_namespace=None, servicebus_namespace_key_name=None, servicebus_namespace_primary_key=None, servicebus_topic=None, servicebus_subscription=None, **kwargs):
fully_qualified_namespace = f"{servicebus_namespace.name}{SERVICEBUS_ENDPOINT_SUFFIX}"
async with ServiceBusClient(
fully_qualified_namespace=fully_qualified_namespace,
credential=ServiceBusSharedKeyCredential(
policy=servicebus_namespace_key_name,
key=servicebus_namespace_primary_key
),
logging_enable=False,
uamqp_transport=uamqp_transport
) as sb_client:

sender = sb_client.get_topic_sender(topic_name=servicebus_topic.name)
receiver = sb_client.get_subscription_receiver(
topic_name=servicebus_topic.name,
subscription_name=servicebus_subscription.name,
receive_mode=ServiceBusReceiveMode.RECEIVE_AND_DELETE,
)
async with sender, receiver:
# queue should be empty
received_msgs = await receiver.receive_messages(max_message_count=10, max_wait_time=10)
assert len(received_msgs) == 0

messages = [ServiceBusMessage("Message") for _ in range(10)]
await sender.send_messages(messages)
# wait for all messages to be received and added to the receiver internal buffer
await asyncio.sleep(10)

# internal buffer should have messages now
received_msgs = await receiver.receive_messages(max_message_count=10, max_wait_time=10)
assert len(received_msgs) == 10
40 changes: 40 additions & 0 deletions sdk/servicebus/azure-servicebus/tests/test_subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,43 @@ def test_subscription_message_expiry(self, uamqp_transport, *, servicebus_namesp
assert len(messages) == 1
assert messages[0].delivery_count > 0
receiver.complete_message(messages[0])

@pytest.mark.liveTest
@pytest.mark.live_test_only
@CachedServiceBusResourceGroupPreparer(name_prefix='servicebustest')
@CachedServiceBusNamespacePreparer(name_prefix='servicebustest')
@ServiceBusTopicPreparer(name_prefix='servicebustest')
@ServiceBusSubscriptionPreparer(name_prefix='servicebustest', lock_duration='PT5S')
@pytest.mark.parametrize("uamqp_transport", uamqp_transport_params, ids=uamqp_transport_ids)
@ArgPasser()
def test_subscription_receive_and_delete_with_send_and_wait(self, uamqp_transport, *, servicebus_namespace=None, servicebus_namespace_key_name=None, servicebus_namespace_primary_key=None, servicebus_topic=None, servicebus_subscription=None, **kwargs):
fully_qualified_namespace = f"{servicebus_namespace.name}{SERVICEBUS_ENDPOINT_SUFFIX}"
with ServiceBusClient(
fully_qualified_namespace=fully_qualified_namespace,
credential=ServiceBusSharedKeyCredential(
policy=servicebus_namespace_key_name,
key=servicebus_namespace_primary_key
),
logging_enable=False,
uamqp_transport=uamqp_transport
) as sb_client:

sender = sb_client.get_topic_sender(topic_name=servicebus_topic.name)
receiver = sb_client.get_subscription_receiver(
topic_name=servicebus_topic.name,
subscription_name=servicebus_subscription.name,
receive_mode=ServiceBusReceiveMode.RECEIVE_AND_DELETE,
)
with sender, receiver:
# queue should be empty
received_msgs = receiver.receive_messages(max_message_count=10, max_wait_time=10)
assert len(received_msgs) == 0

messages = [ServiceBusMessage("Message") for _ in range(10)]
sender.send_messages(messages)
# wait for all messages to be received and added to the receiver internal buffer in the background
time.sleep(10)

# internal buffer should have messages now
received_msgs = receiver.receive_messages(max_message_count=10, max_wait_time=10)
assert len(received_msgs) == 10
Loading