├── .bazelrc ├── .bazelversion ├── .github ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md └── workflows │ ├── nightly.yml │ ├── release.yml │ └── test.yml ├── .gitignore ├── .travis.yml ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── LICENSE-MPL-RabbitMQ ├── Makefile ├── README.md ├── erlang.mk ├── etc ├── rabbit-hare.config └── rabbit-test.config ├── rabbitmq-components.mk ├── src ├── rabbit_delayed_message.erl ├── rabbit_delayed_message_app.erl ├── rabbit_delayed_message_sup.erl ├── rabbit_delayed_message_utils.erl └── rabbit_exchange_type_delayed_message.erl └── test └── plugin_SUITE.erl /.bazelrc: -------------------------------------------------------------------------------- 1 | build --enable_bzlmod 2 | 3 | build --registry=https://bcr.bazel.build/ 4 | build --registry=https://raw.githubusercontent.com/rabbitmq/bazel-central-registry/erlang-packages/ 5 | 6 | build --incompatible_strict_action_env 7 | build --local_test_jobs=1 8 | 9 | build --flag_alias=erlang_home=@rules_erlang//:erlang_home 10 | build --flag_alias=erlang_version=@rules_erlang//:erlang_version 11 | 12 | build --extra_toolchains="@erlang_config//..." 13 | 14 | build:buildbuddy --bes_results_url=https://app.buildbuddy.io/invocation/ 15 | build:buildbuddy --bes_backend=grpcs://remote.buildbuddy.io 16 | build:buildbuddy --remote_cache=grpcs://remote.buildbuddy.io 17 | build:buildbuddy --remote_timeout=1200 18 | build:buildbuddy --grpc_keepalive_time=30s 19 | build:buildbuddy --build_metadata=REPO_URL=https://github.com/rabbitmq/rabbitmq-delayed-message-exchange.git 20 | build:buildbuddy --experimental_remote_cache_compression 21 | build:buildbuddy --experimental_remote_cache_async 22 | build:buildbuddy --noslim_profile 23 | build:buildbuddy --experimental_profile_include_target_label 24 | build:buildbuddy --experimental_profile_include_primary_output 25 | 26 | build:rbe --config=buildbuddy 27 | 28 | build:rbe --remote_executor=grpcs://remote.buildbuddy.io 29 | 30 | build:rbe --spawn_strategy=remote 31 | build:rbe --test_strategy="" 32 | build:rbe --jobs=50 33 | 34 | build:rbe --crosstool_top=@rbe//cc:toolchain 35 | build:rbe --extra_toolchains=@rbe//config:cc-toolchain 36 | 37 | build:rbe --host_platform=@rabbitmq-server//bazel/platforms:erlang_internal_platform 38 | 39 | build:rbe --host_cpu=k8 40 | build:rbe --cpu=k8 41 | 42 | build:rbe-25_3 --config=rbe 43 | build:rbe-25_3 --platforms=@rabbitmq-server//bazel/platforms:erlang_linux_25_3_platform 44 | 45 | build:rbe-26_2 --config=rbe 46 | build:rbe-26_2 --platforms=@rabbitmq-server//bazel/platforms:erlang_linux_26_2_platform 47 | 48 | # Try importing a user specific .bazelrc 49 | # You can create your own by copying and editing the template-user.bazelrc template: 50 | # cp template-user.bazelrc user.bazelrc 51 | try-import %workspace%/user.bazelrc 52 | -------------------------------------------------------------------------------- /.bazelversion: -------------------------------------------------------------------------------- 1 | 6.4.0 2 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for using RabbitMQ. 2 | 3 | **STOP NOW AND READ THIS** BEFORE OPENING A NEW ISSUE ON GITHUB 4 | 5 | Unless you are CERTAIN you have found a reproducible problem in RabbitMQ or 6 | have a **specific, actionable** suggestion for our team, you must first ask 7 | your question or discuss your suspected issue on the mailing list: 8 | 9 | https://groups.google.com/forum/#!forum/rabbitmq-users 10 | 11 | Team RabbitMQ does not use GitHub issues for discussions, investigations, root 12 | cause analysis and so on. 13 | 14 | Please take the time to read the CONTRIBUTING.md document for instructions on 15 | how to effectively ask a question or report a suspected issue: 16 | 17 | https://github.com/rabbitmq/rabbitmq-server/blob/master/CONTRIBUTING.md#github-issues 18 | 19 | Following these rules **will save time** for both you and RabbitMQ's maintainers. 20 | 21 | Thank you. 22 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ## Proposed Changes 2 | 3 | Please describe the big picture of your changes here to communicate to the 4 | RabbitMQ team why we should accept this pull request. If it fixes a bug or 5 | resolves a feature request, be sure to link to that issue. 6 | 7 | A pull request that doesn't explain **why** the change was made has a much 8 | lower chance of being accepted. 9 | 10 | If English isn't your first language, don't worry about it and try to 11 | communicate the problem you are trying to solve to the best of your abilities. 12 | As long as we can understand the intent, it's all good. 13 | 14 | ## Types of Changes 15 | 16 | What types of changes does your code introduce to this project? 17 | _Put an `x` in the boxes that apply_ 18 | 19 | - [ ] Bug fix (non-breaking change which fixes issue #NNNN) 20 | - [ ] New feature (non-breaking change which adds functionality) 21 | - [ ] Breaking change (fix or feature that would cause an observable behavior change in existing systems) 22 | - [ ] Documentation improvements (corrections, new content, etc) 23 | - [ ] Cosmetic change (whitespace, formatting, etc) 24 | 25 | ## Checklist 26 | 27 | _Put an `x` in the boxes that apply. You can also fill these out after creating 28 | the PR. If you're unsure about any of them, don't hesitate to ask on the 29 | mailing list. We're here to help! This is simply a reminder of what we are 30 | going to look for before merging your code._ 31 | 32 | - [ ] I have read the `CONTRIBUTING.md` document 33 | - [ ] I have signed the CA (see https://cla.pivotal.io/sign/rabbitmq) 34 | - [ ] All tests pass locally with my changes 35 | - [ ] I have added tests that prove my fix is effective or that my feature works 36 | - [ ] I have added necessary documentation (if appropriate) 37 | - [ ] Any dependent changes have been merged and published in related repositories 38 | 39 | ## Further Comments 40 | 41 | If this is a relatively large or complex change, kick off the discussion by 42 | explaining why you chose the solution you did and what alternatives you 43 | considered, etc. 44 | -------------------------------------------------------------------------------- /.github/workflows/nightly.yml: -------------------------------------------------------------------------------- 1 | name: Test (Nightly) 2 | on: 3 | schedule: 4 | - cron: '0 3 * * *' 5 | workflow_dispatch: 6 | jobs: 7 | test: 8 | strategy: 9 | fail-fast: false 10 | matrix: 11 | bazel_otp_name: 12 | - "26_2" 13 | branch: 14 | - main 15 | - v3.13.x 16 | - v3.12.x 17 | uses: rabbitmq/rabbitmq-lvc-exchange/.github/workflows/bazel-test-nightly.yml@main 18 | with: 19 | bazel_otp_name: ${{ matrix.bazel_otp_name }} 20 | branch: ${{ matrix.branch }} 21 | summary: 22 | needs: 23 | - test 24 | runs-on: ubuntu-latest 25 | steps: 26 | - name: SUMMARY 27 | run: | 28 | echo "SUCCESS" 29 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | on: 3 | push: 4 | tags: 5 | - v3.* 6 | jobs: 7 | release: 8 | uses: rabbitmq/rabbitmq-lvc-exchange/.github/workflows/plugin-release.yml@main 9 | with: 10 | bazel_otp_name: 25 11 | plugin_name: rabbitmq_delayed_message_exchange 12 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | on: 3 | push: 4 | branches: 5 | - main 6 | - v3.13.x 7 | - v3.12.x 8 | pull_request: 9 | jobs: 10 | test: 11 | strategy: 12 | fail-fast: false 13 | matrix: 14 | bazel_otp_name: 15 | - "26_2" 16 | uses: rabbitmq/rabbitmq-lvc-exchange/.github/workflows/bazel-test.yml@main 17 | with: 18 | bazel_otp_name: ${{ matrix.bazel_otp_name }} 19 | summary: 20 | needs: 21 | - test 22 | runs-on: ubuntu-latest 23 | steps: 24 | - name: SUMMARY 25 | run: | 26 | echo "SUCCESS" 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .sw? 2 | .*.sw? 3 | *.beam 4 | /.erlang.mk/ 5 | /cover/ 6 | /deps/ 7 | /doc/ 8 | /ebin/ 9 | /escript/ 10 | /escript.lock 11 | /logs/ 12 | /plugins/ 13 | /plugins.lock 14 | /sbin/ 15 | /sbin.lock 16 | /xrefr 17 | 18 | rabbitmq_delayed_message_exchange.d 19 | 20 | /user.bazelrc 21 | /bazel-* 22 | 23 | /.vscode/ 24 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # vim:sw=2:et: 2 | 3 | os: linux 4 | dist: xenial 5 | language: elixir 6 | notifications: 7 | email: 8 | recipients: 9 | - alerts@rabbitmq.com 10 | on_success: never 11 | on_failure: always 12 | addons: 13 | apt: 14 | packages: 15 | - awscli 16 | cache: 17 | apt: true 18 | env: 19 | global: 20 | - secure: nibXH/qEVpheE1+M5U1cY/rKuv820TxpV/97pstEUDb5Fk5p8s3WZn4kG1BP5jtFozWtsAUNIjGlbJTTaDn4YnIFjxDBoPPO8IbgFWRnukuUd/v6phCunDrBx4u5oDyaNN0VJv5/c2jpl92kYaVs5cEbKTOag67DOy2D7rPWl+E= 21 | - secure: OhWqYiQ/+X4pQxFDiuh5QRHUhhR9+btzXDc5jjh5LQw9t7KHDiR3k5SrN1/x5Z/sGp1cUKz7tQkz5dkjTRo4wJCEgg3iFaTSGi/5t2n2KlJVJ3sxEs+Q/dCc2VNCx50bjYs64q4BH/sKDEthDp1LXRG7/DcRVfz28t4HJe7/P9I= 22 | 23 | # $base_rmq_ref is used by rabbitmq-components.mk to select the 24 | # appropriate branch for dependencies. 25 | - base_rmq_ref=master 26 | 27 | elixir: 28 | - '1.9' 29 | otp_release: 30 | - '21.3' 31 | - '22.2' 32 | 33 | install: 34 | # This project being an Erlang one (we just set language to Elixir 35 | # to ensure it is installed), we don't want Travis to run mix(1) 36 | # automatically as it will break. 37 | skip 38 | 39 | script: 40 | # $current_rmq_ref is also used by rabbitmq-components.mk to select 41 | # the appropriate branch for dependencies. 42 | - make check-rabbitmq-components.mk 43 | current_rmq_ref="${TRAVIS_PULL_REQUEST_BRANCH:-${TRAVIS_BRANCH}}" 44 | - make xref 45 | current_rmq_ref="${TRAVIS_PULL_REQUEST_BRANCH:-${TRAVIS_BRANCH}}" 46 | - make tests 47 | current_rmq_ref="${TRAVIS_PULL_REQUEST_BRANCH:-${TRAVIS_BRANCH}}" 48 | 49 | after_failure: 50 | - | 51 | cd "$TRAVIS_BUILD_DIR" 52 | if test -d logs && test "$AWS_ACCESS_KEY_ID" && test "$AWS_SECRET_ACCESS_KEY"; then 53 | archive_name="$(basename "$TRAVIS_REPO_SLUG")-$TRAVIS_JOB_NUMBER" 54 | 55 | tar -c --transform "s/^logs/${archive_name}/" -f - logs | \ 56 | xz > "${archive_name}.tar.xz" 57 | 58 | aws s3 cp "${archive_name}.tar.xz" s3://server-release-pipeline/travis-ci-logs/ \ 59 | --region eu-west-1 \ 60 | --acl public-read 61 | fi 62 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, and in the interest of fostering an open 4 | and welcoming community, we pledge to respect all people who contribute through reporting 5 | issues, posting feature requests, updating documentation, submitting pull requests or 6 | patches, and other activities. 7 | 8 | We are committed to making participation in this project a harassment-free experience for 9 | everyone, regardless of level of experience, gender, gender identity and expression, 10 | sexual orientation, disability, personal appearance, body size, race, ethnicity, age, 11 | religion, or nationality. 12 | 13 | Examples of unacceptable behavior by participants include: 14 | 15 | * The use of sexualized language or imagery 16 | * Personal attacks 17 | * Trolling or insulting/derogatory comments 18 | * Public or private harassment 19 | * Publishing other's private information, such as physical or electronic addresses, 20 | without explicit permission 21 | * Other unethical or unprofessional conduct 22 | 23 | Project maintainers have the right and responsibility to remove, edit, or reject comments, 24 | commits, code, wiki edits, issues, and other contributions that are not aligned to this 25 | Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors 26 | that they deem inappropriate, threatening, offensive, or harmful. 27 | 28 | By adopting this Code of Conduct, project maintainers commit themselves to fairly and 29 | consistently applying these principles to every aspect of managing this project. Project 30 | maintainers who do not follow or enforce the Code of Conduct may be permanently removed 31 | from the project team. 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an 34 | individual is representing the project or its community. 35 | 36 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by 37 | contacting a project maintainer at [info@rabbitmq.com](mailto:info@rabbitmq.com). All complaints will 38 | be reviewed and investigated and will result in a response that is deemed necessary and 39 | appropriate to the circumstances. Maintainers are obligated to maintain confidentiality 40 | with regard to the reporter of an incident. 41 | 42 | This Code of Conduct is adapted from the 43 | [Contributor Covenant](https://contributor-covenant.org), version 1.3.0, available at 44 | [contributor-covenant.org/version/1/3/0/](https://contributor-covenant.org/version/1/3/0/) 45 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Thank you for using RabbitMQ and for taking the time to contribute to the project. 2 | This document has two main parts: 3 | 4 | * when and how to file GitHub issues for RabbitMQ projects 5 | * how to submit pull requests 6 | 7 | They intend to save you and RabbitMQ maintainers some time, so please 8 | take a moment to read through them. 9 | 10 | ## Overview 11 | 12 | ### GitHub issues 13 | 14 | The RabbitMQ team uses GitHub issues for _specific actionable items_ that 15 | engineers can work on. This assumes the following: 16 | 17 | * GitHub issues are not used for questions, investigations, root cause 18 | analysis, discussions of potential issues, etc (as defined by this team) 19 | * Enough information is provided by the reporter for maintainers to work with 20 | 21 | The team receives many questions through various venues every single 22 | day. Frequently, these questions do not include the necessary details 23 | the team needs to begin useful work. GitHub issues can very quickly 24 | turn into a something impossible to navigate and make sense 25 | of. Because of this, questions, investigations, root cause analysis, 26 | and discussions of potential features are all considered to be 27 | [mailing list][rmq-users] material. If you are unsure where to begin, 28 | the [RabbitMQ users mailing list][rmq-users] is the right place. 29 | 30 | Getting all the details necessary to reproduce an issue, make a 31 | conclusion or even form a hypothesis about what's happening can take a 32 | fair amount of time. Please help others help you by providing a way to 33 | reproduce the behavior you're observing, or at least sharing as much 34 | relevant information as possible on the [RabbitMQ users mailing 35 | list][rmq-users]. 36 | 37 | Please provide versions of the software used: 38 | 39 | * RabbitMQ server 40 | * Erlang 41 | * Operating system version (and distribution, if applicable) 42 | * All client libraries used 43 | * RabbitMQ plugins (if applicable) 44 | 45 | The following information greatly helps in investigating and reproducing issues: 46 | 47 | * RabbitMQ server logs 48 | * A code example or terminal transcript that can be used to reproduce 49 | * Full exception stack traces (a single line message is not enough!) 50 | * `rabbitmqctl report` and `rabbitmqctl environment` output 51 | * Other relevant details about the environment and workload, e.g. a traffic capture 52 | * Feel free to edit out hostnames and other potentially sensitive information. 53 | 54 | To make collecting much of this and other environment information, use 55 | the [`rabbitmq-collect-env`][rmq-collect-env] script. It will produce an archive with 56 | server logs, operating system logs, output of certain diagnostics commands and so on. 57 | Please note that **no effort is made to scrub any information that may be sensitive**. 58 | 59 | ### Pull Requests 60 | 61 | RabbitMQ projects use pull requests to discuss, collaborate on and accept code contributions. 62 | Pull requests is the primary place of discussing code changes. 63 | 64 | Here's the recommended workflow: 65 | 66 | * [Fork the repository][github-fork] or repositories you plan on contributing to. If multiple 67 | repositories are involved in addressing the same issue, please use the same branch name 68 | in each repository 69 | * Create a branch with a descriptive name in the relevant repositories 70 | * Make your changes, run tests (usually with `make tests`), commit with a 71 | [descriptive message][git-commit-msgs], push to your fork 72 | * Submit pull requests with an explanation what has been changed and **why** 73 | * Submit a filled out and signed [Contributor Agreement][ca-agreement] if needed (see below) 74 | * Be patient. We will get to your pull request eventually 75 | 76 | If what you are going to work on is a substantial change, please first 77 | ask the core team for their opinion on the [RabbitMQ users mailing list][rmq-users]. 78 | 79 | ## Running Tests 80 | 81 | To run a "fast suite" (a subset of tests): 82 | 83 | make ct-fast 84 | 85 | To run a "slow suite" (a subset of tests that take much longer to run): 86 | 87 | make ct-slow 88 | 89 | To run a particular suite: 90 | 91 | make ct-$suite_name 92 | 93 | for example, to run the `backing_queue` suite: 94 | 95 | make ct-backing_queue 96 | 97 | Finally, 98 | 99 | make tests 100 | 101 | will run all suites. 102 | 103 | ## Code of Conduct 104 | 105 | See [CODE_OF_CONDUCT.md](./CODE_OF_CONDUCT.md). 106 | 107 | ## Contributor Agreement 108 | 109 | If you want to contribute a non-trivial change, please submit a signed 110 | copy of our [Contributor Agreement][ca-agreement] around the time you 111 | submit your pull request. This will make it much easier (in some 112 | cases, possible) for the RabbitMQ team at Pivotal to merge your 113 | contribution. 114 | 115 | ## Where to Ask Questions 116 | 117 | If something isn't clear, feel free to ask on our [mailing list][rmq-users]. 118 | 119 | [rmq-collect-env]: https://github.com/rabbitmq/support-tools/blob/master/scripts/rabbitmq-collect-env 120 | [git-commit-msgs]: https://chris.beams.io/posts/git-commit/ 121 | [rmq-users]: https://groups.google.com/forum/#!forum/rabbitmq-users 122 | [ca-agreement]: https://cla.pivotal.io/sign/rabbitmq 123 | [github-fork]: https://help.github.com/articles/fork-a-repo/ 124 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This package is licensed under the MPL 2.0. For the MPL 2.0, please see LICENSE-MPL-RabbitMQ. 2 | 3 | If you have any questions regarding licensing, please contact us at 4 | info@rabbitmq.com. 5 | -------------------------------------------------------------------------------- /LICENSE-MPL-RabbitMQ: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | PROJECT = rabbitmq_delayed_message_exchange 2 | PROJECT_DESCRIPTION = RabbitMQ Delayed Message Exchange 3 | PROJECT_MOD = rabbit_delayed_message_app 4 | 5 | RABBITMQ_VERSION ?= v4.1.x 6 | current_rmq_ref = $(RABBITMQ_VERSION) 7 | 8 | define PROJECT_APP_EXTRA_KEYS 9 | {broker_version_requirements, ["4.1.0"]} 10 | endef 11 | 12 | dep_amqp_client = git_rmq-subfolder rabbitmq-erlang-client $(RABBITMQ_VERSION) 13 | dep_rabbit_common = git_rmq-subfolder rabbitmq-common $(RABBITMQ_VERSION) 14 | dep_rabbit = git_rmq-subfolder rabbitmq-server $(RABBITMQ_VERSION) 15 | dep_rabbitmq_ct_client_helpers = git_rmq-subfolder rabbitmq-ct-client-helpers $(RABBITMQ_VERSION) 16 | dep_rabbitmq_ct_helpers = git_rmq-subfolder rabbitmq-ct-helpers $(RABBITMQ_VERSION) 17 | 18 | DEPS = rabbit_common rabbit 19 | TEST_DEPS = ct_helper rabbitmq_ct_helpers rabbitmq_ct_client_helpers amqp_client 20 | dep_ct_helper = git https://github.com/extend/ct_helper.git master 21 | 22 | DEP_EARLY_PLUGINS = rabbit_common/mk/rabbitmq-early-plugin.mk 23 | DEP_PLUGINS = rabbit_common/mk/rabbitmq-plugin.mk 24 | 25 | # FIXME: Use erlang.mk patched for RabbitMQ, while waiting for PRs to be 26 | # reviewed and merged. 27 | 28 | ERLANG_MK_REPO = https://github.com/rabbitmq/erlang.mk.git 29 | ERLANG_MK_COMMIT = rabbitmq-tmp 30 | 31 | include rabbitmq-components.mk 32 | include erlang.mk 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RabbitMQ Delayed Message Plugin 2 | 3 | ## Consider the Limitations 4 | 5 | This plugin adds delayed-messaging (or scheduled-messaging) to 6 | RabbitMQ. Its current design **has plenty of limitation** (documented below), 7 | consider using an external scheduler and a data store that fits your needs 8 | first. 9 | 10 | This plugin badly needs a [new design](https://github.com/rabbitmq/rabbitmq-delayed-message-exchange/issues/229) 11 | and a reimplementation from the ground up. 12 | 13 | If you accept the limitations, please read on. 14 | 15 | ## The Basics 16 | 17 | With this plugin [enabled](https://www.rabbitmq.com/docs/plugins), a user can declare an exchange with the type `x-delayed-message` and 18 | then publish messages with the custom header `x-delay` expressing in 19 | milliseconds a delay time for the message. The message will be 20 | delivered to the respective queues after `x-delay` milliseconds. 21 | 22 | ## Intended Use Cases 23 | 24 | This plugin was designed for delaying message publishing for a number of seconds, minutes, or hours. 25 | A day or two at most. 26 | 27 | It is **not a longer term scheduling solution**. If you need to delay publishing by days, weeks, months, or years, 28 | consider using a data store suitable for long-term storage, and an external scheduling tool 29 | of some kind. 30 | 31 | 32 | ## Supported RabbitMQ Versions 33 | 34 | The most recent release of this plugin targets RabbitMQ 4.0.x. 35 | 36 | This plugin can be enabled on a RabbitMQ cluster that uses either Mnesia or Khepri as [metadata store](https://www.rabbitmq.com/docs/metadata-store), 37 | however, when this plugin is enabled **before** Khepri, it must be restarted (or the node must be) 38 | after Khepri is enabled. 39 | 40 | In other words, there are three possible scenarios w.r.t. the schema data store used: 41 | 42 | 1. If the cluster uses Mnesia for schema store, it works exactly as it did against RabbitMQ 3.13.x 43 | 2. If the cluster uses Khepri and the plugin is enabled after Khepri, it will start Mnesia, set up a node-local Mnesia replica and schema, and works as in scenario 1 44 | 3. **Important**: if the cluster uses Mnesia, then the plugin is enabled, and then Khepri is enabled, the plugin must be disabled and re-enabled, or the node must be restarted. 45 | Then it will start Mnesia and works as in scenario 2 46 | 47 | ## Supported Erlang/OTP Versions 48 | 49 | The latest version of this plugin [requires Erlang 26.2 or later versions](https://www.rabbitmq.com/which-erlang.html), same as RabbitMQ 4.0.x. 50 | 51 | ## Project Maturity 52 | 53 | The current design of this plugin is **mature and potential suitable for production use 54 | as long as the user is aware of its limitations and the intended use cases**. 55 | 56 | This plugin is not commercially supported by VMware at the moment but 57 | it doesn't mean that it will be abandoned or team RabbitMQ is not interested 58 | in improving it in the future. It is not, however, a high priority for our small team. 59 | 60 | So, give it a try with your workload and decide for yourself. 61 | 62 | 63 | ## Installation 64 | 65 | ### Download a Binary Build 66 | 67 | Binary builds are distributed [via GitHub releases](https://github.com/rabbitmq/rabbitmq-delayed-message-exchange/releases). 68 | 69 | As with all 3rd party plugins, the `.ez` file must be copied into a [node's plugins directory](https://rabbitmq.com/plugins.html#plugin-directories) 70 | with sufficient permissions for the effective user of the RabbitMQ process to load it from disk. 71 | 72 | To find out what the plugins directory is, use `rabbitmq-plugins directories` 73 | 74 | ``` bash 75 | rabbitmq-plugins directories -s 76 | ``` 77 | 78 | ### Enabling the Plugin 79 | 80 | Then run the following command: 81 | 82 | ``` bash 83 | rabbitmq-plugins enable rabbitmq_delayed_message_exchange 84 | ``` 85 | 86 | ## Usage ## 87 | 88 | To use the delayed-messaging feature, declare an exchange with the 89 | type `x-delayed-message`: 90 | 91 | 92 | ```java 93 | // ... elided code ... 94 | Map args = new HashMap(); 95 | args.put("x-delayed-type", "direct"); 96 | channel.exchangeDeclare("my-exchange", "x-delayed-message", true, false, args); 97 | // ... more code ... 98 | ``` 99 | 100 | Note that we pass an extra header called `x-delayed-type`, more on it 101 | under the _Routing_ section. 102 | 103 | Once we have the exchange declared we can publish messages providing a 104 | header telling the plugin for how long to delay our messages: 105 | 106 | ```java 107 | // ... elided code ... 108 | byte[] messageBodyBytes = "delayed payload".getBytes("UTF-8"); 109 | Map headers = new HashMap(); 110 | headers.put("x-delay", 5000); 111 | AMQP.BasicProperties.Builder props = new AMQP.BasicProperties.Builder().headers(headers); 112 | channel.basicPublish("my-exchange", "", props.build(), messageBodyBytes); 113 | 114 | byte[] messageBodyBytes2 = "more delayed payload".getBytes("UTF-8"); 115 | Map headers2 = new HashMap(); 116 | headers2.put("x-delay", 1000); 117 | AMQP.BasicProperties.Builder props2 = new AMQP.BasicProperties.Builder().headers(headers2); 118 | channel.basicPublish("my-exchange", "", props2.build(), messageBodyBytes2); 119 | // ... more code ... 120 | ``` 121 | 122 | In the above example we publish two messages, specifying the delay 123 | time with the `x-delay` header. For this example, the plugin will 124 | deliver to our queues first the message with the body `"more delayed 125 | payload"` and then the one with the body `"delayed payload"`. 126 | 127 | If the `x-delay` header is not present, then the plugin will proceed 128 | to route the message without delay. 129 | 130 | ## Routing ## 131 | 132 | This plugin allows for flexible routing via the `x-delayed-type` 133 | arguments that can be passed during `exchange.declare`. In the example 134 | above we used `"direct"` as exchange type. That means the plugin 135 | will have the same routing behavior shown by the direct exchange. 136 | 137 | If you want a different routing behavior, then you could provide a 138 | different exchange type, like `"topic"` for example. You can also 139 | specify exchange types provided by plugins. Note that this argument is 140 | **required** and **must** refer to an **existing exchange type**. 141 | 142 | ## Performance Impact ## 143 | 144 | Due to the `"x-delayed-type"` argument, one could use this exchange in 145 | place of other exchanges, since the `"x-delayed-message"` exchange 146 | will just act as proxy. Note that there might be some performance 147 | implications if you do this. 148 | 149 | For each message that crosses an `"x-delayed-message"` exchange, the 150 | plugin will try to determine if the message has to be expired by 151 | making sure the delay is within range, ie: `Delay > 0, Delay =< 152 | ?ERL_MAX_T` (In Erlang a timer can be set up to (2^32)-1 milliseconds 153 | in the future). 154 | 155 | If the previous condition holds, then the message will be persisted to 156 | Mnesia and some other logic will kick in to determine if this 157 | particular message delay needs to replace the current scheduled timer 158 | and so on. 159 | 160 | This means that while one _could_ use this exchange in place of a 161 | _direct_ or _fanout_ exchange (or any other exchange for that matter), 162 | _it will be slower_ than using the actual exchange. If you don't need 163 | to delay messages, then use the actual exchange. 164 | 165 | 166 | ## Limitations 167 | 168 | Delayed messages are stored in a Mnesia table (also see Limitations below) 169 | with a single disk replica on the current node. They will survive a node 170 | restart. While timer(s) that triggered scheduled delivery are not persisted, 171 | it will be re-initialised during plugin activation on node start. 172 | Obviously, only having one copy of a scheduled message in a cluster means 173 | that losing that node or disabling the plugin on it will lose the 174 | messages residing on that node. 175 | 176 | The plugin only performs one attempt at publishing each message but since publishing 177 | is local, in practice the only issue that may prevent delivery is the lack of queues 178 | (or bindings) to route to. 179 | 180 | Closely related to the above, the mandatory flag is not supported by this exchange: 181 | we cannot be sure that at the future publishing point in time 182 | 183 | * there is at least one queue we can route to 184 | * the original connection is still around to send a `basic.return` to 185 | 186 | Current design of this plugin doesn't really fit scenarios 187 | with a high number of delayed messages (e.g. 100s of thousands or millions). 188 | See [#72](https://github.com/rabbitmq/rabbitmq-delayed-message-exchange/issues/72) for details. 189 | 190 | ## Disabling the Plugin ## 191 | 192 | You can disable this plugin by calling `rabbitmq-plugins disable 193 | rabbitmq_delayed_message_exchange` but note that **ALL DELAYED MESSAGES THAT 194 | HAVEN'T BEEN DELIVERED WILL BE LOST**. 195 | 196 | ## Building the Plugin 197 | 198 | ```shell 199 | bazel build //:erlang_app 200 | bazel build :ez 201 | ``` 202 | 203 | The EZ file is created in the `bazel-bin` directory. 204 | 205 | ## Creating a Release 206 | 207 | 1. Update `broker_version_requirements` in `helpers.bzl` & `Makefile` (Optional) 208 | 1. Update the plugin version in `MODULE.bazel` 209 | 1. Push a tag (i.e. `v4.0.0`) with the matching version 210 | 1. Allow the Release workflow to run and create a draft release 211 | 1. Review and publish the release 212 | 213 | ## LICENSE 214 | 215 | See the LICENSE file. 216 | -------------------------------------------------------------------------------- /etc/rabbit-hare.config: -------------------------------------------------------------------------------- 1 | %% -*- erlang -*- 2 | %% Note - we still need this for rabbit_exchange_type_delayed_message_test_util:plugin_dir/0 to work... 3 | []. 4 | -------------------------------------------------------------------------------- /etc/rabbit-test.config: -------------------------------------------------------------------------------- 1 | %% -*- erlang -*- 2 | %% Note - we still need this for rabbit_exchange_type_delayed_message_test_util:plugin_dir/0 to work... 3 | []. 4 | -------------------------------------------------------------------------------- /rabbitmq-components.mk: -------------------------------------------------------------------------------- 1 | ifeq ($(.DEFAULT_GOAL),) 2 | # Define default goal to `all` because this file defines some targets 3 | # before the inclusion of erlang.mk leading to the wrong target becoming 4 | # the default. 5 | .DEFAULT_GOAL = all 6 | endif 7 | 8 | # PROJECT_VERSION defaults to: 9 | # 1. the version exported by environment; 10 | # 2. the version stored in `git-revisions.txt`, if it exists; 11 | # 3. a version based on git-describe(1), if it is a Git clone; 12 | # 4. 0.0.0 13 | # 14 | # Note that in the case where git-describe(1) is used 15 | # (e.g. during development), running "git gc" may help 16 | # improve the performance. 17 | 18 | PROJECT_VERSION := $(RABBITMQ_VERSION) 19 | 20 | ifeq ($(PROJECT_VERSION),) 21 | ifneq ($(wildcard git-revisions.txt),) 22 | PROJECT_VERSION = $(shell \ 23 | head -n1 git-revisions.txt | \ 24 | awk '{print $$$(words $(PROJECT_DESCRIPTION) version);}') 25 | else 26 | PROJECT_VERSION = $(shell \ 27 | (git describe --dirty --abbrev=7 --tags --always --first-parent \ 28 | 2>/dev/null || echo 0.0.0) | \ 29 | sed -e 's/^v//' -e 's/_/./g' -e 's/-/+/' -e 's/-/./g') 30 | endif 31 | endif 32 | 33 | # -------------------------------------------------------------------- 34 | # RabbitMQ components. 35 | # -------------------------------------------------------------------- 36 | 37 | # Third-party dependencies version pinning. 38 | # 39 | # We do that in this file, which is included by all projects, to ensure 40 | # all projects use the same versions. It avoids conflicts. 41 | 42 | dep_accept = hex 0.3.5 43 | dep_cowboy = hex 2.13.0 44 | dep_cowlib = hex 2.14.0 45 | dep_credentials_obfuscation = hex 3.5.0 46 | dep_cuttlefish = hex 3.4.0 47 | dep_gen_batch_server = hex 0.8.8 48 | dep_jose = hex 1.11.10 49 | dep_khepri = hex 0.16.0 50 | dep_khepri_mnesia_migration = hex 0.7.2 51 | dep_meck = hex 1.0.0 52 | dep_osiris = git https://github.com/rabbitmq/osiris v1.8.7 53 | dep_prometheus = hex 4.11.0 54 | dep_ra = hex 2.16.8 55 | dep_ranch = hex 2.2.0 56 | dep_recon = hex 2.5.6 57 | dep_redbug = hex 2.0.7 58 | dep_systemd = hex 0.6.1 59 | dep_thoas = hex 1.2.1 60 | dep_observer_cli = hex 1.8.2 61 | dep_seshat = git https://github.com/rabbitmq/seshat v0.6.1 62 | dep_stdout_formatter = hex 0.2.4 63 | dep_sysmon_handler = hex 1.3.0 64 | 65 | # RabbitMQ applications found in the monorepo. 66 | # 67 | # Note that rabbitmq_server_release is not a real application 68 | # but is the name used in the top-level Makefile. 69 | 70 | RABBITMQ_BUILTIN = \ 71 | amqp10_client \ 72 | amqp10_common \ 73 | amqp_client \ 74 | oauth2_client \ 75 | rabbit \ 76 | rabbit_common \ 77 | rabbitmq_amqp1_0 \ 78 | rabbitmq_amqp_client \ 79 | rabbitmq_auth_backend_cache \ 80 | rabbitmq_auth_backend_http \ 81 | rabbitmq_auth_backend_ldap \ 82 | rabbitmq_auth_backend_oauth2 \ 83 | rabbitmq_auth_mechanism_ssl \ 84 | rabbitmq_aws \ 85 | rabbitmq_cli \ 86 | rabbitmq_codegen \ 87 | rabbitmq_consistent_hash_exchange \ 88 | rabbitmq_ct_client_helpers \ 89 | rabbitmq_ct_helpers \ 90 | rabbitmq_event_exchange \ 91 | rabbitmq_federation \ 92 | rabbitmq_federation_management \ 93 | rabbitmq_federation_prometheus \ 94 | rabbitmq_jms_topic_exchange \ 95 | rabbitmq_management \ 96 | rabbitmq_management_agent \ 97 | rabbitmq_mqtt \ 98 | rabbitmq_peer_discovery_aws \ 99 | rabbitmq_peer_discovery_common \ 100 | rabbitmq_peer_discovery_consul \ 101 | rabbitmq_peer_discovery_etcd \ 102 | rabbitmq_peer_discovery_k8s \ 103 | rabbitmq_prelaunch \ 104 | rabbitmq_prometheus \ 105 | rabbitmq_random_exchange \ 106 | rabbitmq_recent_history_exchange \ 107 | rabbitmq_server_release \ 108 | rabbitmq_sharding \ 109 | rabbitmq_shovel \ 110 | rabbitmq_shovel_management \ 111 | rabbitmq_stomp \ 112 | rabbitmq_stream \ 113 | rabbitmq_stream_common \ 114 | rabbitmq_stream_management \ 115 | rabbitmq_top \ 116 | rabbitmq_tracing \ 117 | rabbitmq_trust_store \ 118 | rabbitmq_web_dispatch \ 119 | rabbitmq_web_mqtt \ 120 | rabbitmq_web_mqtt_examples \ 121 | rabbitmq_web_stomp \ 122 | rabbitmq_web_stomp_examples \ 123 | trust_store_http 124 | 125 | # Applications outside of the monorepo maintained by Team RabbitMQ. 126 | 127 | RABBITMQ_COMMUNITY = \ 128 | rabbitmq_auth_backend_amqp \ 129 | rabbitmq_boot_steps_visualiser \ 130 | rabbitmq_delayed_message_exchange \ 131 | rabbitmq_lvc_exchange \ 132 | rabbitmq_management_exchange \ 133 | rabbitmq_management_themes \ 134 | rabbitmq_message_timestamp \ 135 | rabbitmq_metronome \ 136 | rabbitmq_routing_node_stamp \ 137 | rabbitmq_rtopic_exchange 138 | 139 | community_dep = git git@github.com:rabbitmq/$1.git $(if $2,$2,main) 140 | dep_rabbitmq_auth_backend_amqp = $(call community_dep,rabbitmq-auth-backend-amqp) 141 | dep_rabbitmq_boot_steps_visualiser = $(call community_dep,rabbitmq-boot-steps-visualiser,master) 142 | dep_rabbitmq_delayed_message_exchange = $(call community_dep,rabbitmq-delayed-message-exchange) 143 | dep_rabbitmq_lvc_exchange = $(call community_dep,rabbitmq-lvc-exchange) 144 | dep_rabbitmq_management_exchange = $(call community_dep,rabbitmq-management-exchange) 145 | dep_rabbitmq_management_themes = $(call community_dep,rabbitmq-management-themes,master) 146 | dep_rabbitmq_message_timestamp = $(call community_dep,rabbitmq-message-timestamp) 147 | dep_rabbitmq_metronome = $(call community_dep,rabbitmq-metronome,master) 148 | dep_rabbitmq_routing_node_stamp = $(call community_dep,rabbitmq-routing-node-stamp) 149 | dep_rabbitmq_rtopic_exchange = $(call community_dep,rabbitmq-rtopic-exchange) 150 | 151 | # All RabbitMQ applications. 152 | 153 | RABBITMQ_COMPONENTS = $(RABBITMQ_BUILTIN) $(RABBITMQ_COMMUNITY) 154 | 155 | # Erlang.mk does not rebuild dependencies by default, once they were 156 | # compiled once, except for those listed in the `$(FORCE_REBUILD)` 157 | # variable. 158 | # 159 | # We want all RabbitMQ components to always be rebuilt: this eases 160 | # the work on several components at the same time. 161 | 162 | FORCE_REBUILD = $(RABBITMQ_COMPONENTS) 163 | 164 | # We disable autopatching for community plugins as they sit in 165 | # their own repository and we want to avoid polluting the git 166 | # status with changes that should not be committed. 167 | NO_AUTOPATCH += $(RABBITMQ_COMMUNITY) 168 | 169 | # -------------------------------------------------------------------- 170 | # Component distribution. 171 | # -------------------------------------------------------------------- 172 | 173 | list-dist-deps:: 174 | @: 175 | 176 | prepare-dist:: 177 | @: 178 | 179 | # -------------------------------------------------------------------- 180 | # RabbitMQ-specific settings. 181 | # -------------------------------------------------------------------- 182 | 183 | # If the top-level project is a RabbitMQ component, we override 184 | # $(DEPS_DIR) for this project to point to the top-level's one. 185 | # 186 | # We do the same for $(ERLANG_MK_TMP) as we want to keep the 187 | # beam cache regardless of where we build. We also want to 188 | # share Hex tarballs. 189 | 190 | ifneq ($(PROJECT),rabbitmq_server_release) 191 | DEPS_DIR ?= $(abspath ..) 192 | ERLANG_MK_TMP ?= $(abspath ../../.erlang.mk) 193 | DISABLE_DISTCLEAN = 1 194 | endif 195 | 196 | # We disable `make distclean` so $(DEPS_DIR) is not accidentally removed. 197 | 198 | ifeq ($(DISABLE_DISTCLEAN),1) 199 | ifneq ($(filter distclean distclean-deps,$(MAKECMDGOALS)),) 200 | SKIP_DEPS = 1 201 | endif 202 | endif 203 | -------------------------------------------------------------------------------- /src/rabbit_delayed_message.erl: -------------------------------------------------------------------------------- 1 | %% This Source Code Form is subject to the terms of the Mozilla Public 2 | %% License, v. 2.0. If a copy of the MPL was not distributed with this 3 | %% file, You can obtain one at https://mozilla.org/MPL/2.0/. 4 | %% 5 | %% Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. 6 | %% 7 | 8 | %% NOTE that this module uses os:timestamp/0 but in the future Erlang 9 | %% will have a new time API. 10 | %% See: 11 | %% https://www.erlang.org/documentation/doc-7.0-rc1/erts-7.0/doc/html/erlang.html#now-0 12 | %% and 13 | %% https://www.erlang.org/documentation/doc-7.0-rc1/erts-7.0/doc/html/time_correction.html 14 | 15 | -module(rabbit_delayed_message). 16 | -include_lib("rabbit_common/include/rabbit.hrl"). 17 | -rabbit_boot_step({?MODULE, 18 | [{description, "exchange delayed message mnesia setup"}, 19 | {mfa, {?MODULE, setup_mnesia, []}}, 20 | {cleanup, {?MODULE, disable_plugin, []}}, 21 | {requires, pre_flight}]}). 22 | 23 | -behaviour(gen_server). 24 | 25 | -export([start_link/0, delay_message/3, setup_mnesia/0, disable_plugin/0, go/0]). 26 | -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, 27 | code_change/3]). 28 | -export([messages_delayed/1]). 29 | 30 | %% For testing, debugging and manual use 31 | -export([refresh_config/0, 32 | table_name/0, 33 | index_table_name/0]). 34 | 35 | -import(rabbit_delayed_message_utils, [swap_delay_header/1]). 36 | 37 | -type t_reference() :: reference(). 38 | -type delay() :: non_neg_integer(). 39 | 40 | 41 | -spec delay_message(rabbit_types:exchange(), 42 | mc:state(), 43 | delay()) -> 44 | nodelay | {ok, t_reference()}. 45 | 46 | -spec internal_delay_message(t_reference(), 47 | rabbit_types:exchange(), 48 | mc:state(), 49 | delay()) -> 50 | nodelay | {ok, t_reference()}. 51 | 52 | -define(TABLE_NAME, append_to_atom(?MODULE, node())). 53 | -define(INDEX_TABLE_NAME, append_to_atom(?TABLE_NAME, "_index")). 54 | 55 | -record(state, {timer, 56 | stats_state}). 57 | 58 | -record(delay_key, 59 | { timestamp, %% timestamp delay 60 | exchange %% rabbit_types:exchange() 61 | }). 62 | 63 | -record(delay_entry, 64 | { delay_key, %% delay_key record 65 | delivery, %% the message delivery 66 | ref %% ref to make records distinct for 'bag' semantics. 67 | }). 68 | 69 | -record(delay_index, 70 | { delay_key, %% delay_key record 71 | const %% record must have two fields 72 | }). 73 | 74 | %%-------------------------------------------------------------------- 75 | 76 | start_link() -> 77 | gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). 78 | 79 | go() -> 80 | gen_server:cast(?MODULE, go). 81 | 82 | delay_message(Exchange, Message, Delay) -> 83 | gen_server:call(?MODULE, {delay_message, Exchange, Message, Delay}, 84 | infinity). 85 | 86 | setup_mnesia() -> 87 | case rabbit_khepri:is_enabled() of 88 | true -> 89 | ensure_mnesia_running(); 90 | false -> 91 | %% Mnesia should already be running 92 | ok 93 | end, 94 | _ = mnesia:create_table(?TABLE_NAME, [{record_name, delay_entry}, 95 | {attributes, 96 | record_info(fields, delay_entry)}, 97 | {type, bag}, 98 | {disc_copies, [node()]}]), 99 | _ = mnesia:create_table(?INDEX_TABLE_NAME, [{record_name, delay_index}, 100 | {attributes, 101 | record_info(fields, delay_index)}, 102 | {type, ordered_set}, 103 | {disc_copies, [node()]}]), 104 | rabbit_table:wait([?TABLE_NAME, ?INDEX_TABLE_NAME]). 105 | 106 | ensure_mnesia_running() -> 107 | case rabbit_mnesia:is_running() of 108 | false -> 109 | ensure_mnesia_disc_schema(), 110 | rabbit_mnesia:start_mnesia(_CheckConsistency = false); 111 | true -> 112 | ok 113 | end. 114 | 115 | ensure_mnesia_disc_schema() -> 116 | case mnesia:system_info(use_dir) of 117 | true -> 118 | %% There is a disc schema already 119 | ok; 120 | false -> 121 | rabbit_misc:ensure_ok(mnesia:create_schema([node()]), 122 | {?MODULE, cannot_create_mnesia_schema}) 123 | end. 124 | 125 | disable_plugin() -> 126 | _ = mnesia:delete_table(?INDEX_TABLE_NAME), 127 | _ = mnesia:delete_table(?TABLE_NAME), 128 | ok. 129 | 130 | messages_delayed(Exchange) -> 131 | ExchangeName = Exchange#exchange.name, 132 | MatchHead = #delay_entry{delay_key = make_key('_', #exchange{name = ExchangeName, _ = '_'}), 133 | delivery = '_', ref = '_'}, 134 | Delays = mnesia:dirty_select(?TABLE_NAME, [{MatchHead, [], [true]}]), 135 | length(Delays). 136 | 137 | refresh_config() -> 138 | gen_server:call(?MODULE, refresh_config). 139 | 140 | %%-------------------------------------------------------------------- 141 | 142 | init([]) -> 143 | _ = recover(), 144 | {ok, #state{timer = not_set}}. 145 | 146 | handle_call({delay_message, Exchange, Message, Delay}, 147 | _From, State = #state{timer = CurrTimer}) -> 148 | Reply = {ok, NewTimer} = internal_delay_message(CurrTimer, Exchange, Message, Delay), 149 | State2 = State#state{timer = NewTimer}, 150 | {reply, Reply, State2}; 151 | handle_call(refresh_config, _From, State) -> 152 | {reply, ok, refresh_config(State)}; 153 | handle_call(_Req, _From, State) -> 154 | {reply, unknown_request, State}. 155 | 156 | handle_cast(go, State) -> 157 | State2 = refresh_config(State), 158 | {noreply, State2#state{timer = maybe_delay_first()}}; 159 | handle_cast(_C, State) -> 160 | {noreply, State}. 161 | 162 | handle_info({timeout, _TimerRef, {deliver, Key}}, State) -> 163 | case mnesia:dirty_read(?TABLE_NAME, Key) of 164 | [] -> 165 | mnesia:dirty_delete(?INDEX_TABLE_NAME, Key); 166 | Deliveries -> 167 | _ = route(Key, Deliveries, State), 168 | mnesia:dirty_delete(?TABLE_NAME, Key), 169 | mnesia:dirty_delete(?INDEX_TABLE_NAME, Key) 170 | end, 171 | {noreply, State#state{timer = maybe_delay_first()}}; 172 | handle_info(_I, State) -> 173 | {noreply, State}. 174 | 175 | terminate(_, _) -> 176 | ok. 177 | 178 | code_change(_, State, _) -> {ok, State}. 179 | 180 | %%-------------------------------------------------------------------- 181 | 182 | maybe_delay_first() -> 183 | case mnesia:dirty_first(?INDEX_TABLE_NAME) of 184 | %% destructuring to prevent matching '$end_of_table' 185 | #delay_key{timestamp = FirstTS} = Key2 -> 186 | %% there are messages that will expire and need to be delivered 187 | Now = erlang:system_time(milli_seconds), 188 | start_timer(FirstTS - Now, Key2); 189 | _ -> 190 | %% nothing to do 191 | not_set 192 | end. 193 | 194 | route(#delay_key{exchange = Ex}, Deliveries, State) -> 195 | ExName = Ex#exchange.name, 196 | lists:map(fun (#delay_entry{delivery = Msg0}) -> 197 | Msg1 = case Msg0 of 198 | #delivery{message = BasicMessage} -> 199 | BasicMessage; 200 | _MC -> 201 | Msg0 202 | end, 203 | Msg2 = swap_delay_header(Msg1), 204 | Dests = rabbit_exchange:route(Ex, Msg2), 205 | Qs = rabbit_amqqueue:lookup_many(Dests), 206 | _ = rabbit_queue_type:deliver(Qs, Msg2, #{}, stateless), 207 | bump_routed_stats(ExName, Qs, State) 208 | end, Deliveries). 209 | 210 | internal_delay_message(CurrTimer, Exchange, Message, Delay) -> 211 | Now = erlang:system_time(milli_seconds), 212 | %% keys are timestamps in milliseconds,in the future 213 | DelayTS = Now + Delay, 214 | mnesia:dirty_write(?INDEX_TABLE_NAME, 215 | make_index(DelayTS, Exchange)), 216 | mnesia:dirty_write(?TABLE_NAME, 217 | make_delay(DelayTS, Exchange, Message)), 218 | case CurrTimer of 219 | not_set -> 220 | %% No timer in progress, so we start our own. 221 | {ok, maybe_delay_first()}; 222 | _ -> 223 | case erlang:read_timer(CurrTimer) of 224 | false -> 225 | %% Timer is already expired. Handler will be invoked soon. 226 | {ok, CurrTimer}; 227 | CurrMS when Delay < CurrMS -> 228 | %% Current timer lasts longer that new message delay 229 | _ = erlang:cancel_timer(CurrTimer), 230 | {ok, start_timer(Delay, make_key(DelayTS, Exchange))}; 231 | _ -> 232 | %% Timer is set to expire sooner than this 233 | %% message's scheduled delivery time. 234 | {ok, CurrTimer} 235 | end 236 | end. 237 | 238 | %% Key will be used upon message receipt to fetch 239 | %% the deliveries from the database 240 | start_timer(Delay, Key) -> 241 | erlang:start_timer(erlang:max(0, Delay), self(), {deliver, Key}). 242 | 243 | make_delay(DelayTS, Exchange, Delivery) -> 244 | #delay_entry{delay_key = make_key(DelayTS, Exchange), 245 | delivery = Delivery, 246 | ref = make_ref()}. 247 | 248 | make_index(DelayTS, Exchange) -> 249 | #delay_index{delay_key = make_key(DelayTS, Exchange), 250 | const = true}. 251 | 252 | make_key(DelayTS, Exchange) -> 253 | #delay_key{timestamp = DelayTS, 254 | exchange = Exchange}. 255 | 256 | append_to_atom(Atom, Append) when is_atom(Append) -> 257 | append_to_atom(Atom, atom_to_list(Append)); 258 | append_to_atom(Atom, Append) when is_list(Append) -> 259 | list_to_atom(atom_to_list(Atom) ++ Append). 260 | 261 | recover() -> 262 | %% topology recovery has already happened, we have to recover state for any durable 263 | %% consistent hash exchanges since plugin activation was moved later in boot process 264 | %% starting with RabbitMQ 3.8.4 265 | case list_exchanges() of 266 | {error, Reason} -> 267 | rabbit_log:error( 268 | "Delayed message exchange: " 269 | "failed to recover durable bindings of one of the exchanges, reason: ~p", 270 | [Reason]); 271 | Xs -> 272 | rabbit_log:debug("Delayed message exchange: " 273 | "have ~b durable exchanges to recover", 274 | [length(Xs)]), 275 | [recover_exchange_and_bindings(X) || X <- lists:usort(Xs)] 276 | end. 277 | 278 | list_exchanges() -> 279 | Pattern = #exchange{durable = true, type = 'x-delayed-message', _ = '_'}, 280 | rabbit_db_exchange:match(Pattern). 281 | 282 | recover_exchange_and_bindings(#exchange{name = XName} = X) -> 283 | Bindings = rabbit_binding:list_for_source(XName), 284 | _ = [rabbit_exchange_type_delayed_message:add_binding(none, X, B) 285 | || B <- lists:usort(Bindings)], 286 | rabbit_log:debug("Delayed message exchange: " 287 | "recovered bindings for ~s", 288 | [rabbit_misc:rs(XName)]). 289 | 290 | %% These metrics are normally bumped from a channel process via which 291 | %% the publish actually happened. In the special case of delayed 292 | %% message delivery, the singleton delayed_message gen_server does 293 | %% this. 294 | %% 295 | %% Difference from delivering from a channel: 296 | %% 297 | %% The channel process keeps track of the state and monitors each 298 | %% queue it routed to. When the channel is notified of a queue DOWN, 299 | %% it marks all core metrics for that channel + queue as deleted. 300 | %% Monitoring all queues would be overkill for the delayed message 301 | %% gen_server, so this delete marking does not happen in this 302 | %% case. Still `rabbit_core_metrics_gc' will periodically scan all the 303 | %% core metrics and eventually delete entries for non-existing queues 304 | %% so there won't be any metrics leak. `rabbit_core_metrics_gc' will 305 | %% also delete the entries when this process is not alive ie when the 306 | %% plugin is disabled. 307 | bump_routed_stats(ExName, Qs, State) -> 308 | rabbit_global_counters:messages_routed(amqp091, length(Qs)), 309 | case rabbit_event:stats_level(State, #state.stats_state) of 310 | fine -> 311 | [begin 312 | QName = amqqueue:get_name(Q), 313 | %% Channel PID is just an identifier in the metrics 314 | %% DB. However core metrics GC will delete entries 315 | %% with a not-alive PID, and by the time the delayed 316 | %% message gets delivered the original channel 317 | %% process might be long gone, hence we need a live 318 | %% PID in the key. 319 | FakeChannelId = self(), 320 | Key = {FakeChannelId, {QName, ExName}}, 321 | rabbit_core_metrics:channel_stats(queue_exchange_stats, publish, Key, 1) 322 | end 323 | || Q <- Qs], 324 | ok; 325 | _ -> 326 | ok 327 | end. 328 | 329 | refresh_config(State) -> 330 | rabbit_event:init_stats_timer(State, #state.stats_state). 331 | 332 | table_name() -> 333 | ?TABLE_NAME. 334 | 335 | index_table_name() -> 336 | ?INDEX_TABLE_NAME. 337 | -------------------------------------------------------------------------------- /src/rabbit_delayed_message_app.erl: -------------------------------------------------------------------------------- 1 | %% This Source Code Form is subject to the terms of the Mozilla Public 2 | %% License, v. 2.0. If a copy of the MPL was not distributed with this 3 | %% file, You can obtain one at https://mozilla.org/MPL/2.0/. 4 | %% 5 | %% Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. 6 | %% 7 | 8 | -module(rabbit_delayed_message_app). 9 | 10 | -behaviour(application). 11 | -export([start/2, stop/1]). 12 | 13 | %% Same idea as for federation. We need to make sure 14 | %% rabbit_delayed_message.erl is running to make it start publishing 15 | %% delayed messages that might have been persisted but not published 16 | %% yet. 17 | -behaviour(supervisor). 18 | -export([init/1]). 19 | 20 | start(_Type, _StartArgs) -> 21 | rabbit_delayed_message:go(), 22 | supervisor:start_link({local, ?MODULE}, ?MODULE, []). 23 | 24 | stop(_State) -> 25 | ok. 26 | %%---------------------------------------------------------------------------- 27 | 28 | init([]) -> {ok, {{one_for_one, 3, 10}, []}}. 29 | -------------------------------------------------------------------------------- /src/rabbit_delayed_message_sup.erl: -------------------------------------------------------------------------------- 1 | %% This Source Code Form is subject to the terms of the Mozilla Public 2 | %% License, v. 2.0. If a copy of the MPL was not distributed with this 3 | %% file, You can obtain one at https://mozilla.org/MPL/2.0/. 4 | %% 5 | %% Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. 6 | %% 7 | 8 | -module(rabbit_delayed_message_sup). 9 | 10 | -include_lib("rabbit_common/include/rabbit.hrl"). 11 | 12 | -behaviour(supervisor2). 13 | 14 | -define(SERVER, ?MODULE). 15 | 16 | -export([start_link/0]). 17 | 18 | -export([init/1, stop/0]). 19 | 20 | -rabbit_boot_step({rabbit_delayed_message_supervisor, 21 | [{description, "delayed message sup"}, 22 | {mfa, {rabbit_sup, start_child, [?MODULE]}}, 23 | {requires, pre_flight}, 24 | {cleanup, {?MODULE, stop, []}}]}). 25 | 26 | start_link() -> 27 | supervisor2:start_link({local, ?SERVER}, ?MODULE, []). 28 | 29 | init([]) -> 30 | {ok, {{one_for_one, 3, 10}, 31 | [{rabbit_delayed_message, {rabbit_delayed_message, start_link, []}, 32 | transient, ?WORKER_WAIT, worker, [rabbit_delayed_message]}]}}. 33 | 34 | stop() -> 35 | ok = supervisor:terminate_child(rabbit_sup, ?MODULE), 36 | ok = supervisor:delete_child(rabbit_sup, ?MODULE). 37 | -------------------------------------------------------------------------------- /src/rabbit_delayed_message_utils.erl: -------------------------------------------------------------------------------- 1 | %% This Source Code Form is subject to the terms of the Mozilla Public 2 | %% License, v. 2.0. If a copy of the MPL was not distributed with this 3 | %% file, You can obtain one at https://mozilla.org/MPL/2.0/. 4 | %% 5 | %% Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. 6 | %% 7 | 8 | -module(rabbit_delayed_message_utils). 9 | 10 | -include_lib("rabbit_common/include/rabbit.hrl"). 11 | -include_lib("rabbit_common/include/rabbit_framing.hrl"). 12 | 13 | -export([get_delay/1, swap_delay_header/1]). 14 | 15 | -define(INTEGER_ARG_TYPES, [long, ubyte, short, ushort, int, uint]). 16 | 17 | -define(STRING_ARG_TYPES, [utf8, binary]). 18 | 19 | -define(FLOAT_ARG_TYPES, [double, float]). 20 | 21 | -import(rabbit_misc, [table_lookup/2, set_table_value/4]). 22 | 23 | get_delay(Delivery) -> 24 | case mc:x_header(<<"x-delay">>, Delivery) of 25 | undefined -> 26 | {error, nodelay}; 27 | {Type, Delay} -> 28 | case check_int_arg(Type) of 29 | ok -> {ok, Delay}; 30 | _ -> 31 | case try_convert_to_int(Type, Delay) of 32 | {ok, Converted} -> {ok, Converted}; 33 | _ -> {error, nodelay} 34 | end 35 | end 36 | end. 37 | 38 | %% set the x-delay header to -Delay, so it won't be re-delayed and the 39 | %% header can still be passed down via e2e to other queues that might 40 | %% lay after the next exchange so these queues/consumers can tell the 41 | %% message comes via the delay plugin. 42 | swap_delay_header(Delivery) -> 43 | case get_delay(Delivery) of 44 | {ok, Delay} -> 45 | mc:set_annotation(<<"x-delay">>, -Delay, Delivery); 46 | _ -> 47 | Delivery 48 | end. 49 | 50 | try_convert_to_int(Type, Delay) -> 51 | case lists:member(Type, ?STRING_ARG_TYPES) of 52 | true -> {ok, rabbit_data_coercion:to_integer(Delay)}; 53 | false -> 54 | case lists:member(Type, ?FLOAT_ARG_TYPES) of 55 | true -> {ok, trunc(Delay)}; 56 | false -> {error, {unacceptable_type, Type}} 57 | end 58 | end. 59 | 60 | %% adapted from rabbit_amqqueue.erl 61 | check_int_arg(Type) -> 62 | case lists:member(Type, ?INTEGER_ARG_TYPES) of 63 | true -> ok; 64 | false -> {error, {unacceptable_type, Type}} 65 | end. 66 | -------------------------------------------------------------------------------- /src/rabbit_exchange_type_delayed_message.erl: -------------------------------------------------------------------------------- 1 | %% This Source Code Form is subject to the terms of the Mozilla Public 2 | %% License, v. 2.0. If a copy of the MPL was not distributed with this 3 | %% file, You can obtain one at https://mozilla.org/MPL/2.0/. 4 | %% 5 | %% Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. 6 | %% 7 | 8 | -module(rabbit_exchange_type_delayed_message). 9 | 10 | -rabbit_boot_step( 11 | {?MODULE, 12 | [{description, "exchange type x-delayed-message: registry"}, 13 | {mfa, {rabbit_registry, register, 14 | [exchange, <<"x-delayed-message">>, ?MODULE]}}, 15 | {cleanup, {rabbit_registry, unregister, 16 | [exchange, <<"x-delayed-message">>]}}, 17 | {requires, rabbit_registry}, 18 | {enables, recovery}]}). 19 | 20 | -include_lib("rabbit_common/include/rabbit.hrl"). 21 | -include_lib("rabbit_common/include/rabbit_framing.hrl"). 22 | 23 | -behaviour(rabbit_exchange_type). 24 | 25 | -import(rabbit_misc, [table_lookup/2]). 26 | -import(rabbit_delayed_message_utils, [get_delay/1]). 27 | 28 | -export([description/0, serialise_events/0, route/3]). 29 | -export([validate/1, validate_binding/2, 30 | create/2, delete/2, policy_changed/2, 31 | add_binding/3, remove_bindings/3, assert_args_equivalence/2]). 32 | -export([info/1, info/2]). 33 | 34 | -define(EXCHANGE(Ex), (exchange_module(Ex))). 35 | -define(ERL_MAX_T, 4294967295). %% Max timer delay, per Erlang docs. 36 | 37 | %%---------------------------------------------------------------------------- 38 | 39 | description() -> 40 | [{name, <<"x-delayed-message">>}, 41 | {description, <<"Delayed Message Exchange.">>}]. 42 | 43 | route(X = #exchange{name = Name}, 44 | Message, 45 | Opts) -> 46 | case delay_message(X, Message) of 47 | nodelay -> 48 | %% route the message using proxy module 49 | case ?EXCHANGE(X) of 50 | rabbit_exchange_type_direct -> 51 | RKs = mc:routing_keys(Message), 52 | %% Exchange type x-delayed-message routes via "direct exchange routing v1" 53 | %% even when feature flag direct_exchange_routing_v2 is enabled because 54 | %% table rabbit_index_route only stores bindings whose source exchange 55 | %% is of type direct exchange. 56 | rabbit_router:match_routing_key(Name, RKs); 57 | Mod -> 58 | Mod:route(X, Message, Opts) 59 | end; 60 | _ -> 61 | [] 62 | end. 63 | 64 | validate(#exchange{arguments = Args} = X) -> 65 | case table_lookup(Args, <<"x-delayed-type">>) of 66 | {_ArgType, <<"x-delayed-message">>} -> 67 | rabbit_misc:protocol_error(precondition_failed, 68 | "Invalid argument, " 69 | "'x-delayed-message' can't be used " 70 | "for 'x-delayed-type'", 71 | []); 72 | {_ArgType, Type} when is_binary(Type) -> 73 | rabbit_exchange:check_type(Type), 74 | ?EXCHANGE(X):validate(X); 75 | _ -> 76 | rabbit_misc:protocol_error(precondition_failed, 77 | "Invalid argument, " 78 | "'x-delayed-type' must be " 79 | "an existing exchange type", 80 | []) 81 | end. 82 | 83 | validate_binding(X, B) -> 84 | ?EXCHANGE(X):validate_binding(X, B). 85 | create(Serial, X) -> 86 | ?EXCHANGE(X):create(Serial, X). 87 | delete(Serial, X) -> 88 | ?EXCHANGE(X):delete(Serial, X). 89 | policy_changed(X1, X2) -> 90 | ?EXCHANGE(X1):policy_changed(X1, X2). 91 | add_binding(Serial, X, B) -> 92 | ?EXCHANGE(X):add_binding(Serial, X, B). 93 | remove_bindings(Serial, X, Bs) -> 94 | ?EXCHANGE(X):remove_bindings(Serial, X, Bs). 95 | assert_args_equivalence(X, Args) -> 96 | ?EXCHANGE(X):assert_args_equivalence(X, Args). 97 | serialise_events() -> false. 98 | 99 | info(Exchange) -> 100 | info(Exchange, [messages_delayed]). 101 | 102 | info(Exchange, Items) -> 103 | case lists:member(messages_delayed, Items) of 104 | false -> []; 105 | true -> 106 | [{messages_delayed, 107 | rabbit_delayed_message:messages_delayed(Exchange)}] 108 | end. 109 | 110 | 111 | %%---------------------------------------------------------------------------- 112 | 113 | delay_message(Exchange, Message) -> 114 | case get_delay(Message) of 115 | {ok, Delay} when Delay > 0, Delay =< ?ERL_MAX_T -> 116 | rabbit_delayed_message:delay_message(Exchange, Message, Delay); 117 | _ -> 118 | nodelay 119 | end. 120 | 121 | %% assumes the type is set in the args and that validate/1 did its job 122 | exchange_module(Ex) -> 123 | T = rabbit_registry:binary_to_type(exchange_type(Ex)), 124 | {ok, M} = rabbit_registry:lookup_module(exchange, T), 125 | M. 126 | 127 | exchange_type(#exchange{arguments = Args}) -> 128 | case table_lookup(Args, <<"x-delayed-type">>) of 129 | {_ArgType, Type} -> Type; 130 | _ -> error 131 | end. 132 | -------------------------------------------------------------------------------- /test/plugin_SUITE.erl: -------------------------------------------------------------------------------- 1 | %% This Source Code Form is subject to the terms of the Mozilla Public 2 | %% License, v. 2.0. If a copy of the MPL was not distributed with this 3 | %% file, You can obtain one at https://mozilla.org/MPL/2.0/. 4 | %% 5 | %% Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. 6 | %% 7 | 8 | -module(plugin_SUITE). 9 | 10 | -compile([export_all, nowarn_export_all]). 11 | 12 | -include_lib("eunit/include/eunit.hrl"). 13 | -include_lib("amqp_client/include/amqp_client.hrl"). 14 | 15 | all() -> 16 | [ 17 | {group, non_parallel_tests}, 18 | {group, fine_stats} 19 | ]. 20 | 21 | groups() -> 22 | [ 23 | {non_parallel_tests, [], [ 24 | wrong_exchange_argument_type, 25 | exchange_argument_type_not_self, 26 | routing_topic, 27 | routing_direct, 28 | routing_fanout, 29 | e2e_nodelay, 30 | e2e_delay, 31 | delay_order, 32 | delayed_messages_count, 33 | node_restart_before_delay_expires, 34 | node_restart_after_delay_expires, 35 | no_message_for_index, 36 | string_delay_header 37 | ]}, 38 | {fine_stats, [], [ 39 | e2e_nodelay, 40 | e2e_delay 41 | ]} 42 | ]. 43 | 44 | 45 | %% ------------------------------------------------------------------- 46 | %% Setup/teardown. 47 | %% ------------------------------------------------------------------- 48 | 49 | init_per_suite(Config) -> 50 | rabbit_ct_helpers:log_environment(), 51 | Config1 = rabbit_ct_helpers:set_config(Config, [ 52 | {rmq_nodename_suffix, ?MODULE} 53 | ]), 54 | rabbit_ct_helpers:run_setup_steps(Config1, 55 | rabbit_ct_broker_helpers:setup_steps() ++ 56 | rabbit_ct_client_helpers:setup_steps()). 57 | 58 | end_per_suite(Config) -> 59 | rabbit_ct_helpers:run_teardown_steps(Config, 60 | rabbit_ct_client_helpers:teardown_steps() ++ 61 | rabbit_ct_broker_helpers:teardown_steps()). 62 | 63 | init_per_group(fine_stats, Config) -> 64 | CollectStatsOrig = get_collect_stats(Config), 65 | set_collect_stats(Config, fine), 66 | refresh_config(Config), 67 | [{collect_statistics, fine}, {collect_statistics_orig, CollectStatsOrig}|Config]; 68 | init_per_group(_, Config) -> 69 | Config. 70 | 71 | end_per_group(fine_stats, Config) -> 72 | CollectStatsOrig = rabbit_ct_helpers:get_config(Config, collect_statistics_orig), 73 | set_collect_stats(Config, CollectStatsOrig), 74 | refresh_config(Config), 75 | Config; 76 | end_per_group(_, Config) -> 77 | Config. 78 | 79 | init_per_testcase(Testcase, Config) -> 80 | TestCaseName = rabbit_ct_helpers:config_to_testcase_name(Config, Testcase), 81 | BaseName = re:replace(TestCaseName, "/", "-", [global,{return,list}]), 82 | Config1 = rabbit_ct_helpers:set_config(Config, {test_resource_name, BaseName}), 83 | reset_publish_out_stats(Config), 84 | rabbit_ct_helpers:testcase_started(Config1, Testcase). 85 | 86 | end_per_testcase(Testcase, Config) -> 87 | rabbit_ct_helpers:testcase_finished(Config, Testcase). 88 | 89 | %% ------------------------------------------------------------------- 90 | %% Testcases 91 | %% ------------------------------------------------------------------- 92 | 93 | wrong_exchange_argument_type(Config) -> 94 | Chan = rabbit_ct_client_helpers:open_channel(Config), 95 | Ex = make_exchange_name(Config, "fail"), 96 | Type = <<"x-not-valid-type">>, 97 | process_flag(trap_exit, true), 98 | ?assertExit(_, amqp_channel:call(Chan, make_exchange(Ex, Type))), 99 | rabbit_ct_client_helpers:close_channel(Chan), 100 | ok. 101 | 102 | exchange_argument_type_not_self(Config) -> 103 | Chan = rabbit_ct_client_helpers:open_channel(Config), 104 | Ex = make_exchange_name(Config, "1"), 105 | Type = <<"x-delayed-message">>, 106 | process_flag(trap_exit, true), 107 | ?assertExit(_, amqp_channel:call(Chan, make_exchange(Ex, Type))), 108 | rabbit_ct_client_helpers:close_channel(Chan), 109 | ok. 110 | 111 | routing_topic(Config) -> 112 | BKs = [<<"a.b.c">>, <<"a.*.c">>, <<"a.#">>], 113 | RKs = [<<"a.b.c">>, <<"a.z.c">>, <<"a.j.k">>, <<"b.b.c">>], 114 | %% all except <<"b.b.c">> should be routed. 115 | Count = 3, 116 | routing_test0(Config, BKs, RKs, <<"topic">>, Count). 117 | 118 | routing_direct(Config) -> 119 | BKs = [<<"mykey">>], 120 | RKs = [<<"mykey">>, <<"noroute">>, <<"mykey">>], 121 | %% all except <<"noroute">> should be routed. 122 | Count = 2, 123 | routing_test0(Config, BKs, RKs, <<"direct">>, Count). 124 | 125 | routing_fanout(Config) -> 126 | BKs = [<<"mykey">>, <<>>, <<"otherkey">>], 127 | RKs = [<<"mykey">>, <<"noroute">>, <<"mykey">>], 128 | %% all except <<"noroute">> should be routed. 129 | Count = 3, 130 | routing_test0(Config, BKs, RKs, <<"fanout">>, Count). 131 | 132 | routing_test0(Config, BKs, RKs, ExType, Count) -> 133 | Chan = rabbit_ct_client_helpers:open_channel(Config), 134 | 135 | Ex = make_exchange_name(Config, "1"), 136 | Q = make_queue_name(Config, "1"), 137 | 138 | [setup_fabric(Chan, make_exchange(Ex, ExType), make_queue(Q), BRK) || 139 | BRK <- BKs], 140 | 141 | %% message delay will be 0, we are testing routing here 142 | Msgs = [0], 143 | 144 | amqp_channel:call(Chan, #'confirm.select'{}), 145 | 146 | [publish_messages(Chan, Ex, K, Msgs) || 147 | K <- RKs], 148 | 149 | % ensure that the messages have been delivered to the queues 150 | % before asking for the message count 151 | amqp_channel:wait_for_confirms_or_die(Chan), 152 | 153 | #'queue.declare_ok'{message_count = MCount} = 154 | amqp_channel:call(Chan, make_queue(Q)), 155 | 156 | ?assertEqual(Count, MCount), 157 | 158 | amqp_channel:call(Chan, #'exchange.delete' { exchange = Ex }), 159 | amqp_channel:call(Chan, #'queue.delete' { queue = Q }), 160 | rabbit_ct_client_helpers:close_channel(Chan), 161 | ok. 162 | 163 | e2e_nodelay(Config) -> 164 | %% message delay will be 0, 165 | %% we are testing e2e without delays 166 | e2e_test0(Config, [0]). 167 | 168 | e2e_delay(Config) -> 169 | e2e_test0(Config, [500, 100, 300, 200, 100, 400]). 170 | 171 | e2e_test0(Config, Msgs) -> 172 | Chan = rabbit_ct_client_helpers:open_channel(Config), 173 | 174 | Ex = make_exchange_name(Config, "1"), 175 | Ex2 = make_exchange_name(Config, "2"), 176 | Q = make_queue_name(Config, "1"), 177 | 178 | declare_exchange(Chan, make_exchange(Ex, <<"direct">>)), 179 | 180 | setup_fabric(Chan, make_exchange(Ex2, <<"direct">>), make_queue(Q)), 181 | 182 | #'exchange.bind_ok'{} = 183 | amqp_channel:call(Chan, #'exchange.bind' { 184 | source = Ex, 185 | destination = Ex2 186 | }), 187 | 188 | [] = get_publish_out_stat(Config), 189 | 190 | publish_messages(Chan, Ex, Msgs), 191 | 192 | {ok, Result} = consume(Chan, Q, Msgs), 193 | Sorted = lists:sort(Msgs), 194 | ?assertEqual(Sorted, Result), 195 | 196 | PublishOutCount = length(Msgs), 197 | case rabbit_ct_helpers:get_config(Config, collect_statistics, none) of 198 | fine -> 199 | ?assertMatch([{_, PublishOutCount, _}], get_publish_out_stat(Config)); 200 | _ -> 201 | ?assertMatch([], get_publish_out_stat(Config)) 202 | end, 203 | 204 | amqp_channel:call(Chan, #'exchange.delete' { exchange = Ex }), 205 | amqp_channel:call(Chan, #'exchange.delete' { exchange = Ex2 }), 206 | amqp_channel:call(Chan, #'queue.delete' { queue = Q }), 207 | rabbit_ct_client_helpers:close_channel(Chan), 208 | ok. 209 | 210 | delay_order(Config) -> 211 | Chan = rabbit_ct_client_helpers:open_channel(Config), 212 | 213 | Ex = make_exchange_name(Config, "1"), 214 | Q = make_queue_name(Config, "1"), 215 | 216 | setup_fabric(Chan, make_exchange(Ex, <<"direct">>), make_queue(Q)), 217 | 218 | Msgs = [500, 100, 300, 200, 100, 400], 219 | 220 | publish_messages(Chan, Ex, Msgs), 221 | 222 | {ok, Result} = consume(Chan, Q, Msgs), 223 | Sorted = lists:sort(Msgs), 224 | ?assertEqual(Sorted, Result), 225 | 226 | rabbit_ct_client_helpers:close_channel(Chan), 227 | ok. 228 | 229 | delayed_messages_count(Config) -> 230 | Chan = rabbit_ct_client_helpers:open_channel(Config), 231 | 232 | Ex = make_exchange_name(Config, "1"), 233 | Q = make_queue_name(Config, "1"), 234 | 235 | setup_fabric(Chan, make_exchange(Ex, <<"direct">>), make_queue(Q)), 236 | 237 | Msgs = [500, 200, 300, 200, 300, 400], 238 | 239 | publish_messages(Chan, Ex, Msgs), 240 | 241 | % Let messages schedule. 242 | timer:sleep(50), 243 | Exchanges = rabbit_ct_broker_helpers:rpc(Config, 0, 244 | rabbit_exchange, info_all, [<<"/">>]), 245 | 246 | FilterEx = 247 | fun(X) -> 248 | {resource, <<"/">>, exchange, Ex} == proplists:get_value(name, X) 249 | end, 250 | 251 | [Exchange] = lists:filter(FilterEx, Exchanges), 252 | {messages_delayed, 6} = proplists:lookup(messages_delayed, Exchange), 253 | 254 | %% Set a policy for the exchange 255 | PolicyName = make_policy_name(Config, "1"), 256 | rabbit_ct_broker_helpers:set_policy( 257 | Config, 0, PolicyName, <<"^", Ex/binary>>, <<"exchanges">>, [{<<"alternate-exchange">>, <<"altex">>}]), 258 | 259 | %% Same message count returned for modified exchange 260 | Exchanges2 = rabbit_ct_broker_helpers:rpc(Config, 0, 261 | rabbit_exchange, info_all, [<<"/">>]), 262 | 263 | [Exchange2] = lists:filter(FilterEx, Exchanges2), 264 | {messages_delayed, 6} = proplists:lookup(messages_delayed, Exchange2), 265 | 266 | consume(Chan, Q, Msgs), 267 | 268 | rabbit_ct_broker_helpers:clear_policy(Config, 0, PolicyName), 269 | rabbit_ct_client_helpers:close_channel(Chan), 270 | ok. 271 | 272 | node_restart_before_delay_expires(Config) -> 273 | Chan = rabbit_ct_client_helpers:open_channel(Config), 274 | 275 | Ex = make_exchange_name(Config, "1"), 276 | Q = make_queue_name(Config, "1"), 277 | 278 | setup_fabric(Chan, make_durable_exchange(Ex, <<"direct">>), 279 | make_durable_queue(Q)), 280 | 281 | %% Here, we suppose the node will be restarted before all messages 282 | %% are actually queued. 283 | Msgs = [5000, 10000, 3000, 2000, 15000, 1000, 4000], 284 | 285 | publish_messages(Chan, Ex, Msgs), 286 | 287 | rabbit_ct_broker_helpers:restart_node(Config, 0), 288 | 289 | Chan2 = rabbit_ct_client_helpers:open_channel(Config), 290 | 291 | {ok, Result} = consume(Chan2, Q, Msgs), 292 | Sorted = lists:sort(Msgs), 293 | ?assertEqual(Sorted, Result), 294 | 295 | amqp_channel:call(Chan2, #'exchange.delete' { exchange = Ex }), 296 | amqp_channel:call(Chan2, #'queue.delete' { queue = Q }), 297 | 298 | rabbit_ct_client_helpers:close_channel(Chan2), 299 | 300 | ok. 301 | 302 | node_restart_after_delay_expires(Config) -> 303 | Chan = rabbit_ct_client_helpers:open_channel(Config), 304 | 305 | Ex = make_exchange_name(Config, "1"), 306 | Q = make_queue_name(Config, "1"), 307 | 308 | setup_fabric(Chan, make_durable_exchange(Ex, <<"direct">>), 309 | make_durable_queue(Q)), 310 | 311 | Msgs = [5000, 1000, 3000, 2000, 1000, 4000], 312 | 313 | publish_messages(Chan, Ex, Msgs), 314 | 315 | timer:sleep(lists:max(Msgs) + 3000), 316 | rabbit_ct_broker_helpers:restart_node(Config, 0), 317 | 318 | Chan2 = rabbit_ct_client_helpers:open_channel(Config), 319 | 320 | {ok, Result} = consume(Chan2, Q, Msgs), 321 | Sorted = lists:sort(Msgs), 322 | ?assertEqual(Sorted, Result), 323 | 324 | amqp_channel:call(Chan2, #'exchange.delete' { exchange = Ex }), 325 | amqp_channel:call(Chan2, #'queue.delete' { queue = Q }), 326 | 327 | rabbit_ct_client_helpers:close_channel(Chan2), 328 | 329 | ok. 330 | 331 | no_message_for_index(Config) -> 332 | Chan = rabbit_ct_client_helpers:open_channel(Config), 333 | 334 | Ex = make_exchange_name(Config, "1"), 335 | Q = make_queue_name(Config, "1"), 336 | 337 | setup_fabric(Chan, make_durable_exchange(Ex, <<"direct">>), 338 | make_durable_queue(Q)), 339 | 340 | Msgs = [1000, 2000], 341 | 342 | publish_messages(Chan, Ex, Msgs), 343 | 344 | %% delete the first message, but keep it in the index table 345 | make_table_corrupted(Config), 346 | 347 | timer:sleep(2000), 348 | 349 | %% the index key with no messages should be ignored/deleted 350 | %% and the following messages are successfully delivered 351 | Msgs2 = tl(Msgs), 352 | {ok, Result} = consume(Chan, Q, Msgs2), 353 | ?assertEqual(Msgs2, Result), 354 | 355 | amqp_channel:call(Chan, #'exchange.delete' { exchange = Ex }), 356 | amqp_channel:call(Chan, #'queue.delete' { queue = Q }), 357 | 358 | rabbit_ct_client_helpers:close_channel(Chan), 359 | 360 | ok. 361 | 362 | string_delay_header(Config) -> 363 | Chan = rabbit_ct_client_helpers:open_channel(Config), 364 | 365 | Ex = <<"e3">>, 366 | Q = <<"q1">>, 367 | 368 | setup_fabric(Chan, make_exchange(Ex, <<"direct">>), make_queue(Q)), 369 | 370 | Msgs = [500, 100, 300, 200, 100, 400], 371 | 372 | publish_messages(Chan, Ex, <<>>, Msgs, longstr), 373 | 374 | {ok, Result} = consume(Chan, Q, Msgs), 375 | Sorted = lists:sort(Msgs), 376 | ?assertEqual(Sorted, Result), 377 | 378 | ok. 379 | 380 | setup_fabric(Chan, ExDeclare, QueueDeclare) -> 381 | setup_fabric(Chan, ExDeclare, QueueDeclare, <<>>). 382 | 383 | setup_fabric(Chan, 384 | ExDeclare = #'exchange.declare'{exchange = Ex}, 385 | QueueDeclare, 386 | RK) -> 387 | declare_exchange(Chan, ExDeclare), 388 | 389 | #'queue.declare_ok'{queue = Q} = 390 | amqp_channel:call(Chan, QueueDeclare), 391 | 392 | #'queue.bind_ok'{} = 393 | amqp_channel:call(Chan, #'queue.bind' { 394 | queue = Q, 395 | exchange = Ex, 396 | routing_key = RK 397 | }). 398 | 399 | declare_exchange(Chan, ExDeclare) -> 400 | #'exchange.declare_ok'{} = 401 | amqp_channel:call(Chan, ExDeclare). 402 | 403 | publish_messages(Chan, Ex, Msgs) -> 404 | publish_messages(Chan, Ex, <<>>, Msgs). 405 | 406 | publish_messages(Chan, Ex, RK, Msgs) -> 407 | publish_messages(Chan, Ex, RK, Msgs, signedint). 408 | 409 | publish_messages(Chan, Ex, RK, Msgs, HeaderType) -> 410 | [amqp_channel:call(Chan, 411 | #'basic.publish'{exchange = Ex, 412 | routing_key = RK}, 413 | make_msg(HeaderType, V)) || V <- Msgs]. 414 | 415 | consume(Chan, Q, Msgs) -> 416 | #'basic.consume_ok'{} = 417 | amqp_channel:subscribe(Chan, #'basic.consume'{queue = Q, 418 | no_ack = true}, self()), 419 | collect(length(Msgs), lists:max(Msgs) + 3000). 420 | 421 | 422 | collect(N, Timeout) -> 423 | collect(0, N, Timeout, []). 424 | 425 | collect(N, N, _Timeout, Acc) -> 426 | {ok, lists:reverse(Acc)}; 427 | collect(Curr, N, Timeout, Acc) -> 428 | receive {#'basic.deliver'{}, 429 | #amqp_msg{payload = Bin}} -> 430 | collect(Curr+1, N, Timeout, [binary_to_term(Bin) | Acc]) 431 | after Timeout -> 432 | {error, {timeout, Acc}} 433 | end. 434 | 435 | make_queue(Q) -> 436 | #'queue.declare' { 437 | queue = Q 438 | }. 439 | 440 | make_durable_queue(Q) -> 441 | QR = make_queue(Q), 442 | QR#'queue.declare'{ 443 | durable = true, 444 | auto_delete = false 445 | }. 446 | 447 | make_exchange(Ex, Type) -> 448 | #'exchange.declare'{ 449 | exchange = Ex, 450 | type = <<"x-delayed-message">>, 451 | arguments = [{<<"x-delayed-type">>, 452 | longstr, Type}] 453 | }. 454 | 455 | make_durable_exchange(Ex, Type) -> 456 | ER = make_exchange(Ex, Type), 457 | ER#'exchange.declare'{ 458 | durable = true, 459 | auto_delete = false 460 | }. 461 | 462 | make_msg(HeaderType, V) -> 463 | #amqp_msg{props = #'P_basic'{ 464 | delivery_mode = 2, 465 | headers = make_h(HeaderType, V)}, 466 | payload = term_to_binary(V)}. 467 | 468 | make_h(V) -> 469 | make_h(signedint, V). 470 | 471 | make_h(signedint, V) -> 472 | [{<<"x-delay">>, signedint, V}]; 473 | make_h(longstr, V) -> 474 | [{<<"x-delay">>, longstr, integer_to_binary(V)}]. 475 | 476 | tests(Module, Timeout) -> 477 | {foreach, fun() -> ok end, 478 | [{timeout, Timeout, fun () -> Module:F() end} || 479 | {F, _Arity} <- proplists:get_value(exports, Module:module_info()), 480 | string:right(atom_to_list(F), 5) =:= "_test"]}. 481 | 482 | make_exchange_name(Config, Suffix) -> 483 | B = rabbit_ct_helpers:get_config(Config, test_resource_name), 484 | erlang:list_to_binary("x-" ++ B ++ "-" ++ Suffix). 485 | 486 | make_queue_name(Config, Suffix) -> 487 | B = rabbit_ct_helpers:get_config(Config, test_resource_name), 488 | erlang:list_to_binary("q-" ++ B ++ "-" ++ Suffix). 489 | 490 | make_policy_name(Config, Suffix) -> 491 | B = rabbit_ct_helpers:get_config(Config, test_resource_name), 492 | erlang:list_to_binary("p-" ++ B ++ "-" ++ Suffix). 493 | 494 | get_publish_out_stat(Config) -> 495 | rabbit_ct_broker_helpers:rpc(Config, 0, ets, tab2list, [channel_queue_exchange_metrics]). 496 | 497 | reset_publish_out_stats(Config) -> 498 | rabbit_ct_broker_helpers:rpc(Config, 0, ets, delete_all_objects, [channel_queue_exchange_metrics]). 499 | 500 | get_collect_stats(Config) -> 501 | rabbit_ct_broker_helpers:rpc( 502 | Config, 0, application, get_env, [rabbit, collect_statistics, undefined]). 503 | 504 | set_collect_stats(Config, undefined) -> 505 | ok = rabbit_ct_broker_helpers:rpc( 506 | Config, 0, application, unset_env, [rabbit, collect_statistics]); 507 | set_collect_stats(Config, CollectStats) -> 508 | ok = rabbit_ct_broker_helpers:rpc( 509 | Config, 0, application, set_env, [rabbit, collect_statistics, CollectStats]). 510 | 511 | refresh_config(Config) -> 512 | ok = rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_delayed_message, refresh_config, []). 513 | 514 | make_table_corrupted(Config) -> 515 | Table = rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_delayed_message, table_name, []), 516 | IndexTable = rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_delayed_message, index_table_name, []), 517 | 518 | FirstKey = rabbit_ct_broker_helpers:rpc(Config, 0, mnesia, dirty_first, [IndexTable]), 519 | rabbit_ct_broker_helpers:rpc(Config, 0, mnesia, dirty_delete, [Table, FirstKey]). 520 | --------------------------------------------------------------------------------