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

Passing arguments for queues as expected #26966

Conversation

bartoszkubicki
Copy link

@bartoszkubicki bartoszkubicki commented Feb 21, 2020

Description (*)

According to docs it should be possible to declare queue with arguments (like dead-letter-exchange or message-ttl). Unfortunately during process of setting up rabbite schema, arguments are lost and queues are declared without any arguments.

It is because here arguements are always empty array. PR fixes this and also adds return type and some minor formatting fixes. If there are any tests for these class I will fix them as soon as bug will be reproduced, confirmed and my solution will be accepted.
If PR will be accepted, please port to 2.3 as well.

Manual testing scenarios (*)

  1. Create some schema for rabbit using magento xml queue_topology.xml, like in dev docs.
  2. Check if queue is declared in rabbit with arguments (for testing use real arguments like message ttl)

Questions or comments

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds are green)

Resolved issues:

  1. resolves [Issue] Passing arguments for queues as expected #30216: Passing arguments for queues as expected

@m2-assistant
Copy link

m2-assistant bot commented Feb 21, 2020

Hi @bartoszkubicki. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

For more details, please, review the Magento Contributor Guide documentation.

@bartoszkubicki bartoszkubicki force-pushed the message-queue-arguments-passing-from-config branch from 97a7874 to e91a3f1 Compare March 22, 2020 20:02
@magento-engcom-team magento-engcom-team added this to Pending Review in Pull Requests Dashboard Mar 24, 2020
@bartoszkubicki
Copy link
Author

Static tests seems to fail because of some copy-paste in EE. I have encountered this failure in multiple PR opened.

@sidolov sidolov added Priority: P3 May be fixed according to the position in the backlog. Severity: S3 Affects non-critical data or functionality and does not force users to employ a workaround. labels Aug 17, 2020
@ihor-sviziev ihor-sviziev self-assigned this Sep 28, 2020
@ihor-sviziev
Copy link
Contributor

@magento run all tests

@ihor-sviziev ihor-sviziev moved this from Pending Review to Review in Progress in Pull Requests Dashboard Sep 28, 2020
@ihor-sviziev
Copy link
Contributor

Hi @bartoszkubicki,
We're really sorry for long waiting for processing this PR. I'm taking this PR and will try to push it forward.

From first looks changes looks good, but need to see the test results and analyze a bit deeper.

(array) $binding['arguments'],
(string) $connection
);
$this->mappedData += $queueItems;
Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately it might be not an equal to the array_merge. Could you fix it as described here https://github.com/magento/magento-coding-standard/blob/develop/Magento2/Sniffs/Performance/ForeachArrayMergeSniff.md?

@ihor-sviziev
Copy link
Contributor

@magento create issue

@ihor-sviziev ihor-sviziev added the Auto-Tests: Covered All changes in Pull Request is covered by auto-tests label Sep 28, 2020
Copy link
Contributor

@ihor-sviziev ihor-sviziev left a comment

Choose a reason for hiding this comment

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

Hi @bartoszkubicki,
Thank you for your contribution!
Your changes looks good, but small adjustments are needed here. Could you review my comments and update your PR accordingly?

@@ -17,17 +21,17 @@
class DataMapperTest extends TestCase
{
/**
* @var MockObject
* @var Data|MockObject
*/
private $configData;
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add "Mock" suffix to all property names that contains mock object?

$queueItems = $this->createQueueItems($binding['destination'], $binding['topic'], $connection);
$this->mappedData = array_merge($this->mappedData, $queueItems);
$queueItems = $this->createQueueItems(
(string) $binding['destination'],
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. Could you remove the space between type casting and variable in all places in order to have similar code formatting as in other places in Magento, like this?
Suggested change
(string) $binding['destination'],
(string)$binding['destination'],
  1. Do we really need this casting? Do we have any case where we'll have another type?

@ghost ghost moved this from Review in Progress to Changes Requested in Pull Requests Dashboard Sep 28, 2020
@bartoszkubicki
Copy link
Author

@engcom-Foxtrot I will check it. I don't remeber why this branch is set to be merged to 2.4-develop. It was problem for sure in magento 2.3

@bartoszkubicki
Copy link
Author

bartoszkubicki commented Oct 10, 2020

@engcom-Foxtrot Ok, I have checked it against 2.3 and 2.4. I can still reproduce an issue. The thing is, I was expecting to set these arguments on queue level, now I see it may work with this codebase on binding level.

async all
We can see that aguments are set on binding level, but not as a 'feature' of whole queue

Here we have different effect:
queue w arguments
Is is achieved by this script:

<?php

declare(strict_types=1);

require_once __DIR__ . '/../vendor/autoload.php';

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Wire\AMQPTable;

$connection = new AMQPStreamConnection('rabbitmq', 5672, 'user', 'pass', 'vhost');
$channel = $connection->channel();

$taskExchange = 'task';
$taskQueue = 'task';
$deadLetterExchange = 'retry';
$deadLetterQueue = 'retry_task';

// Normal queue
$channel->exchange_declare($taskExchange, 'direct', false, true);
$channel->queue_declare($taskQueue, false, true, false, false, false, new AMQPTable([
    'x-dead-letter-exchange' => $deadLetterExchange
]));
$channel->queue_bind($taskQueue, $taskExchange);

// Retry queue with TTL
$channel->exchange_declare($deadLetterExchange, 'direct', false, true);
$channel->queue_declare($deadLetterQueue, false, true, false, false, false, new AMQPTable([
    'x-dead-letter-exchange' => $taskExchange,
    'x-message-ttl' => 10000
]));
$channel->queue_bind($deadLetterQueue, $deadLetterExchange);
$channel->basic_qos(null, 1, null);

Here difference is also visible:
queues

I think our misunderstanding,in my opinion, is effect of highly inuintuitive queue declaration (#26969). They are normally declared explicitly (like in script above) and with use of Magento MQ Framework, they are "side effect" of declaring bindings. In other words queue don't have to exist when we create binding. If you can explain why all elements are not declared separately (exchange, queue) and later bound by binding I will be grateful.

With fix arguments are applied both to queue and binding, which is also not perfect solution, but it is still somehow fixes the issue.
with fix
In order to test it, this PR have to be applied (#26967). It is like this because the same piece of configuration is used or queues and bindings.

From my tests, for now I assume that arguments on bindings level are not equal to bindings on queue level.
Official documentation says (in case of forming dead-letter exchange, which also requires arguments): "To set the dead letter exchange for a queue, specify the optional x-dead-letter-exchange argument when declaring the queue. " - https://www.rabbitmq.com/dlx.html#using-optional-queue-arguments. There is no word about using bindings' arguments for this. I include test zip, where there are simple topologies for dead-letter-exchange feature with different approaches. With queue arguments works and message cycle there and back between queues (max 3 times, because of x-death argument check, but it doesn't really matter, as we test dead letter fallback). With bindings arguments message is dropped after first consumer nack.
Arguments added to binding (as a side effect of fix) don't seem to spoil anything.
test-topology.zip

I think it is still a bug, because if we would like to add some arguments on queue level - it is impossible.

@engcom-Foxtrot
Copy link
Contributor

engcom-Foxtrot commented Oct 13, 2020

@bartekszymanski thank you for clarification, but it seems code still works incorrectly.
I'm using config from this PR with some customization:

    <exchange name="test.dead_letter" type="topic" connection="amqp">
        <arguments>
            <argument name="x-dead-letter-exchange" xsi:type="string">test.retry</argument>
        </arguments>
        <binding id="test" topic="#" destinationType="queue"
                 destination="test_dead_letter">
            <arguments>
                <argument name="x-message-ttl" xsi:type="string">36000</argument>
            </arguments>
        </binding>
    </exchange>

As I understand, you assume we should get 'x-dead-letter-exchange' argument set for both topic and queue.
On 2.4-develop branch, I'm getting 'x-dead-letter-exchange' argument set only for the topic.
Screenshot from 2020-10-13 12-23-20

On bartoszkubicki:message-queue-arguments-passing-from-config branch, the topic is not created at all:
Screenshot from 2020-10-13 12-11-28

@bartoszkubicki
Copy link
Author

@engcom-Foxtrot I can't locate from which place you have taken this config, but it is not about setting arguments for exchanges (it seems to be working), but for queues - so it is not relevant (as official rabbit mq docs says, in order to create working DLX fallback you have to add dlx to queue, not to another exchange, because message is sent to DLX after its expired, rejected and so on).

From my tests on 2.4 you can set arguments for exchanges and bindings only. Arguments for queues are missing. I would like to have possibility to set arguments separately for queues and bindings. Basing on magento queues xmls construction, with this PR, I achieved setting queues arguments (with bindings arguments set, as a side effect). Normally (using php amq lib) you can set arguments for each component separately - exchange, queue, binding.

@engcom-Foxtrot
Copy link
Contributor

@bartoszkubicki could you please provide some xml what would obviously confirm your PR is fixing the issue you described?

@bartoszkubicki
Copy link
Author

@engcom-Foxtrot Me and my agency has created sample module that needs this fix - you can install it and test it or take configuration files. Without fix DLX exchanges are not configured properly.

@ihor-sviziev
Copy link
Contributor

@engcom-Foxtrot could you review #26966 (comment) ?

@engcom-Foxtrot
Copy link
Contributor

@bartoszkubicki thank you for clarification.

@engcom-Foxtrot
Copy link
Contributor

engcom-Foxtrot commented Oct 27, 2020

QA passed.

Steps:

  1. Add
    <exchange name="test.dead_letter" type="topic" connection="amqp">
        <arguments>
            <argument name="x-dead-letter-exchange" xsi:type="string">test.retry</argument>
        </arguments>
        <binding id="test" topic="#" destinationType="queue"
                 destination="test_dead_letter">
            <arguments>
                <argument name="x-message-ttl" xsi:type="string">36000</argument>
            </arguments>
        </binding>
    </exchange>

to any queue_topology.xml file.
2. Run bin/magento setup:upgrade
3. Check if 'test_dead_letter' queue has 'x-message-ttl' feature set (you can use rabbitmq_management plugin for RabbitMQ).

@ghost ghost moved this from Changes Requested to Ready for Testing in Pull Requests Dashboard Oct 27, 2020
@engcom-Foxtrot engcom-Foxtrot moved this from Ready for Testing to Extended Testing (optional) in Pull Requests Dashboard Oct 27, 2020
@engcom-Foxtrot engcom-Foxtrot added Progress: ready for testing QA: Ready to add to Regression Scope Should be analyzed and added to Regression Testing Scope(if applicable) and removed Progress: extended testing labels Oct 27, 2020
@engcom-Bravo engcom-Bravo added QA: Added to Regression Scope Scenario was analysed and added to Regression Testing Scope and removed QA: Ready to add to Regression Scope Should be analyzed and added to Regression Testing Scope(if applicable) labels Oct 28, 2020
@engcom-Foxtrot engcom-Foxtrot moved this from Extended Testing (optional) to Merge in Progress in Pull Requests Dashboard Oct 28, 2020
@magento-engcom-team magento-engcom-team merged commit 26a1fce into magento:2.4-develop Nov 9, 2020
@m2-assistant
Copy link

m2-assistant bot commented Nov 9, 2020

Hi @bartoszkubicki, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

@sidolov sidolov moved this from Merge in Progress to Recently Merged in Pull Requests Dashboard Nov 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Auto-Tests: Covered All changes in Pull Request is covered by auto-tests Award: bug fix Award: test coverage Component: MessageQueue Priority: P3 May be fixed according to the position in the backlog. Progress: accept QA: Added to Regression Scope Scenario was analysed and added to Regression Testing Scope Release Line: 2.4 Severity: S3 Affects non-critical data or functionality and does not force users to employ a workaround. Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it
Projects
Pull Requests Dashboard
  
Recently Merged
Development

Successfully merging this pull request may close these issues.

[Issue] Passing arguments for queues as expected
7 participants