├── .github ├── ISSUE_TEMPLATE.md └── PULL_REQUEST_TEMPLATE.md ├── .gitignore ├── .travis.yml ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── LICENSE-MPL-RabbitMQ ├── Makefile ├── README-hacking ├── README.md ├── erlang.mk ├── etc ├── rabbit-test.sh └── setup-rabbit-test.sh ├── include └── rabbit_federation.hrl ├── rabbitmq-components.mk ├── src ├── Elixir.RabbitMQ.CLI.Ctl.Commands.FederationStatusCommand.erl ├── Elixir.RabbitMQ.CLI.Ctl.Commands.RestartFederationLinkCommand.erl ├── rabbit_federation_app.erl ├── rabbit_federation_db.erl ├── rabbit_federation_event.erl ├── rabbit_federation_exchange.erl ├── rabbit_federation_exchange_link.erl ├── rabbit_federation_exchange_link_sup_sup.erl ├── rabbit_federation_link_sup.erl ├── rabbit_federation_link_util.erl ├── rabbit_federation_parameters.erl ├── rabbit_federation_queue.erl ├── rabbit_federation_queue_link.erl ├── rabbit_federation_queue_link_sup_sup.erl ├── rabbit_federation_status.erl ├── rabbit_federation_sup.erl ├── rabbit_federation_upstream.erl ├── rabbit_federation_upstream_exchange.erl └── rabbit_federation_util.erl └── test ├── exchange_SUITE.erl ├── federation_status_command_SUITE.erl ├── queue_SUITE.erl ├── rabbit_federation_status_SUITE.erl ├── rabbit_federation_test_util.erl ├── restart_federation_link_command_SUITE.erl ├── unit_SUITE.erl └── unit_inbroker_SUITE.erl /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for using RabbitMQ and for taking the time to report an 2 | issue. 3 | 4 | ## Does This Belong to GitHub or RabbitMQ Mailing List? 5 | 6 | *Important:* please first read the `CONTRIBUTING.md` document in the 7 | root of this repository. It will help you determine whether your 8 | feedback should be directed to the RabbitMQ mailing list [1] instead. 9 | 10 | ## Please Help Maintainers and Contributors Help You 11 | 12 | In order for the RabbitMQ team to investigate your issue, please provide 13 | **as much as possible** of the following details: 14 | 15 | * RabbitMQ version 16 | * Erlang version 17 | * RabbitMQ server and client application log files 18 | * A runnable code sample, terminal transcript or detailed set of 19 | instructions that can be used to reproduce the issue 20 | * RabbitMQ plugin information via `rabbitmq-plugins list` 21 | * Client library version (for all libraries used) 22 | * Operating system, version, and patch level 23 | 24 | Running the `rabbitmq-collect-env` [2] script can provide most of the 25 | information needed. Please make the archive available via a third-party 26 | service and note that **the script does not attempt to scrub any 27 | sensitive data**. 28 | 29 | If your issue involves RabbitMQ management UI or HTTP API, please also provide 30 | the following: 31 | 32 | * Browser and its version 33 | * What management UI page was used (if applicable) 34 | * How the HTTP API requests performed can be reproduced with `curl` 35 | * Operating system on which you are running your browser, and its version 36 | * Errors reported in the JavaScript console (if any) 37 | 38 | This information **greatly speeds up issue investigation** (or makes it 39 | possible to investigate it at all). Please help project maintainers and 40 | contributors to help you by providing it! 41 | 42 | 1. https://groups.google.com/forum/#!forum/rabbitmq-users 43 | 2. https://github.com/rabbitmq/support-tools/blob/master/scripts/rabbitmq-collect-env 44 | -------------------------------------------------------------------------------- /.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 | - [ ] Bugfix (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 existing functionality to not work as expected) 22 | - [ ] Documentation (correction or otherwise) 23 | - [ ] Cosmetics (whitespace, appearance) 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 | -------------------------------------------------------------------------------- /.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_federation.d 19 | -------------------------------------------------------------------------------- /.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: NPk7yFzpxl8ZhheQvf1MG3dWTQq+8o1Fp+XvrBWr3PiQAn8TWHmlmxAt74LlTqRpu8CXINCOpVUYNWF9Gl4gZ0HerV1Gobj0hrF8/PVO6Qz7bqRhcnXj1ax1q4CMjzqyBtt09Pd06Vmx/hgEOQys8+kimpaftIWfBREKyjlYSss= 21 | - secure: PjgBRIvkFXBaty94tOf4oomMSg9B9qdVLKmlb1o8DzE4KkCt2Cb3u00tqHYkvSzzgizSYsdff21TLrdQxw04aRtSVUIk2hqMt4tHbE9+sZL1Y8Cd75ZpjiSOYqnJtG0RbNxhTplCoyDOjaJWc9pjWgafHwHrZdo1wEgfzWDPWic= 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 | ## Code of Conduct 80 | 81 | See [CODE_OF_CONDUCT.md](./CODE_OF_CONDUCT.md). 82 | 83 | ## Contributor Agreement 84 | 85 | If you want to contribute a non-trivial change, please submit a signed 86 | copy of our [Contributor Agreement][ca-agreement] around the time you 87 | submit your pull request. This will make it much easier (in some 88 | cases, possible) for the RabbitMQ team at Pivotal to merge your 89 | contribution. 90 | 91 | ## Where to Ask Questions 92 | 93 | If something isn't clear, feel free to ask on our [mailing list][rmq-users]. 94 | 95 | [rmq-collect-env]: https://github.com/rabbitmq/support-tools/blob/master/scripts/rabbitmq-collect-env 96 | [git-commit-msgs]: https://chris.beams.io/posts/git-commit/ 97 | [rmq-users]: https://groups.google.com/forum/#!forum/rabbitmq-users 98 | [ca-agreement]: https://cla.pivotal.io/sign/rabbitmq 99 | [github-fork]: https://help.github.com/articles/fork-a-repo/ 100 | -------------------------------------------------------------------------------- /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_federation 2 | PROJECT_DESCRIPTION = RabbitMQ Federation 3 | PROJECT_MOD = rabbit_federation_app 4 | 5 | define PROJECT_ENV 6 | [ 7 | {pgroup_name_cluster_id, false}, 8 | {internal_exchange_check_interval, 90000} 9 | ] 10 | endef 11 | 12 | define PROJECT_APP_EXTRA_KEYS 13 | {broker_version_requirements, []} 14 | endef 15 | 16 | DEPS = rabbit_common rabbit amqp_client 17 | TEST_DEPS = rabbitmq_ct_helpers rabbitmq_ct_client_helpers 18 | 19 | DEP_EARLY_PLUGINS = rabbit_common/mk/rabbitmq-early-plugin.mk 20 | DEP_PLUGINS = rabbit_common/mk/rabbitmq-plugin.mk 21 | 22 | # FIXME: Use erlang.mk patched for RabbitMQ, while waiting for PRs to be 23 | # reviewed and merged. 24 | 25 | ERLANG_MK_REPO = https://github.com/rabbitmq/erlang.mk.git 26 | ERLANG_MK_COMMIT = rabbitmq-tmp 27 | 28 | include rabbitmq-components.mk 29 | include erlang.mk 30 | -------------------------------------------------------------------------------- /README-hacking: -------------------------------------------------------------------------------- 1 | This file is intended to tell you How It All Works, concentrating on 2 | the things you might not expect. 3 | 4 | The theory 5 | ========== 6 | 7 | The 'x-federation' exchange is defined in 8 | rabbit_federation_exchange. This starts up a bunch of link processes 9 | (one for each upstream) which: 10 | 11 | * Connect to the upstream broker 12 | * Create a queue and bind it to the upstream exchange 13 | * Keep bindings in sync with the downstream exchange 14 | * Consume messages from the upstream queue and republish them to the 15 | downstream exchange (matching confirms with acks) 16 | 17 | Each link process monitors the connections / channels it opens, and 18 | dies if they do. We use a supervisor2 to ensure that we get some 19 | backoff when restarting. 20 | 21 | We use process groups to identify all link processes for a certain 22 | exchange, as well as all link processes together. 23 | 24 | However, there are a bunch of wrinkles: 25 | 26 | 27 | Wrinkle: The exchange will be recovered when the Erlang client is not available 28 | =============================================================================== 29 | 30 | Exchange recovery happens within the rabbit application - therefore at 31 | the time that the exchange is recovered, we can't make any connections 32 | since the amqp_client application has not yet started. Each link 33 | therefore initially has a state 'not_started'. When it is created it 34 | checks to see if the rabbitmq_federation application is running. If 35 | so, it starts fully. If not, it goes into the 'not_started' 36 | state. When rabbitmq_federation starts, it sends a 'go' message to all 37 | links, prodding them to bring up the link. 38 | 39 | 40 | Wrinkle: On reconnect we want to assert bindings atomically 41 | =========================================================== 42 | 43 | If the link goes down for whatever reason, then by the time it comes 44 | up again the bindings downstream may no longer be in sync with those 45 | upstream. Therefore on link establishment we want to ensure that a 46 | certain set of bindings exists. (Of course bringing up a link for the 47 | first time is a simple case of this.) And we want to do this with AMQP 48 | methods. But if we were to tear down all bindings and recreate them, 49 | we would have a time period when messages would not be forwarded for 50 | bindings that *do* still exist before and after. 51 | 52 | We use exchange to exchange bindings to work around this: 53 | 54 | We bind the upstream exchange (X) to the upstream queue (Q) via an 55 | internal fanout exchange (IXA) like so: (routing keys R1 and R2): 56 | 57 | X----R1,R2--->IXA---->Q 58 | 59 | This has the same effect as binding the queue to the exchange directly. 60 | 61 | Now imagine the link has gone down, and is about to be 62 | reestablished. In the meanwhile, routing has changed downstream so 63 | that we now want routing keys R1 and R3. On link reconnection we can 64 | create and bind another internal fanout exchange IXB: 65 | 66 | X----R1,R2--->IXA---->Q 67 | | ^ 68 | | | 69 | \----R1,R3--->IXB-----/ 70 | 71 | and then delete the original exchange IXA: 72 | 73 | X Q 74 | | ^ 75 | | | 76 | \----R1,R3--->IXB-----/ 77 | 78 | This means that messages matching R1 are always routed during the 79 | switchover. Messages for R3 will start being routed as soon as we bind 80 | the second exchange, and messages for R2 will be stopped in a timely 81 | way. Of course this could lag the downstream situation somewhat, in 82 | which case some R2 messages will get thrown away downstream since they 83 | are unroutable. However this lag is inevitable when the link goes 84 | down. 85 | 86 | This means that the downstream only needs to keep track of whether the 87 | upstream is currently going via internal exchange A or B. This is 88 | held in the exchange scratch space in Mnesia. 89 | 90 | 91 | Wrinkle: We need to amalgamate bindings 92 | ======================================= 93 | 94 | Since we only bind to one exchange upstream, but the downstream 95 | exchange can be bound to many queues, we can have duplicated bindings 96 | downstream (same source, routing key and args but different 97 | destination) that cannot be duplicated upstream (since the destination 98 | is the same). The link therefore maintains a mapping of (Key, Args) to 99 | set(Dest). Duplicated bindings do not get repeated upstream, and are 100 | only unbound upstream when the last one goes away downstream. 101 | 102 | Furthermore, this works as an optimisation since this will tend to 103 | reduce upstream binding count and churn. 104 | 105 | 106 | Wrinkle: We may receive binding events out of order 107 | =================================================== 108 | 109 | The rabbit_federation_exchange callbacks are invoked by channel 110 | processes within rabbit. Therefore they can be executed concurrently, 111 | and can arrive at the link processes in an order that does not 112 | correspond to the wall clock. 113 | 114 | We need to keep the state of the link in sync with Mnesia. Therefore 115 | not only do we need to impose an ordering on these events, we need to 116 | impose Mnesia's ordering on them. We therefore added a function to the 117 | callback interface, serialise_events. When this returns true, the 118 | callback mechanism inside rabbit increments a per-exchange counter 119 | within an Mnesia transaction, and returns the value as part of the 120 | add_binding and remove_binding callbacks. The link process then queues 121 | up these events, and replays them in order. The link process's state 122 | thus always follows Mnesia (it may be delayed, but the effects happen 123 | in the same order). 124 | 125 | 126 | Other issues 127 | ============ 128 | 129 | Since links are implemented in terms of AMQP, link failure may cause 130 | messages to be redelivered. If you're unlucky this could lead to 131 | duplication. 132 | 133 | Message duplication can also happen with some topologies. In some 134 | cases it may not be possible to set max_hops such that messages arrive 135 | once at every node. 136 | 137 | While we correctly order bind / unbind events, we don't do the same 138 | thing for exchange creation / deletion. (This is harder - if you 139 | delete and recreate an exchange with the same name, is it the same 140 | exchange? What about if its type changes?) This would only be an issue 141 | if exchanges churn rapidly; however we could get into a state where 142 | Mnesia sees CDCD but we see CDDC and leave a process running when we 143 | shouldn't. 144 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## RabbitMQ Federation 2 | 3 | ## This was migrated to https://github.com/rabbitmq/rabbitmq-server 4 | 5 | This repository has been moved to the main unified RabbitMQ "monorepo", including all open issues. You can find the source under [/deps/rabbitmq_federation](https://github.com/rabbitmq/rabbitmq-server/tree/master/deps/rabbitmq_federation). 6 | All issues have been transferred. 7 | 8 | ## Overview 9 | 10 | [![Build Status](https://travis-ci.org/rabbitmq/rabbitmq-federation.svg?branch=master)](https://travis-ci.org/rabbitmq/rabbitmq-federation) 11 | 12 | RabbitMQ federation offers a group of features for loosely 13 | coupled and WAN-friendly distributed RabbitMQ setups. Note that 14 | this is not an alternative to queue mirroring. 15 | 16 | 17 | ## Supported RabbitMQ Versions 18 | 19 | This plugin ships with RabbitMQ, there is no need to 20 | install it separately. 21 | 22 | 23 | ## Documentation 24 | 25 | See [RabbitMQ federation plugin](https://www.rabbitmq.com/federation.html) on rabbitmq.com. 26 | 27 | 28 | ## License and Copyright 29 | 30 | Released under [the same license as RabbitMQ](https://www.rabbitmq.com/mpl.html). 31 | 32 | 2007-2015 (c) 2007-2020 VMware, Inc. or its affiliates. 33 | -------------------------------------------------------------------------------- /etc/rabbit-test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | CTL=$1 3 | 4 | # Test direct connections 5 | $CTL set_parameter federation-upstream localhost '{"uri": "amqp://"}' 6 | # We will test the guest:guest gets stripped out in user_id_test 7 | $CTL set_parameter federation-upstream local5673 '{"uri": "amqp://guest:guest@localhost:5673"}' 8 | 9 | $CTL set_parameter federation-upstream-set upstream '[{"upstream": "localhost", "exchange": "upstream", "queue": "upstream"}]' 10 | $CTL set_parameter federation-upstream-set upstream2 '[{"upstream": "localhost", "exchange": "upstream2", "queue": "upstream2"}]' 11 | $CTL set_parameter federation-upstream-set localhost '[{"upstream": "localhost"}]' 12 | $CTL set_parameter federation-upstream-set upstream12 '[{"upstream": "localhost", "exchange": "upstream", "queue": "upstream"}, 13 | {"upstream": "localhost", "exchange": "upstream2", "queue": "upstream2"}]' 14 | $CTL set_parameter federation-upstream-set one '[{"upstream": "localhost", "exchange": "one", "queue": "one"}]' 15 | $CTL set_parameter federation-upstream-set two '[{"upstream": "localhost", "exchange": "two", "queue": "two"}]' 16 | $CTL set_parameter federation-upstream-set upstream5673 '[{"upstream": "local5673", "exchange": "upstream"}]' 17 | 18 | $CTL set_policy fed "^fed\." '{"federation-upstream-set": "upstream"}' 19 | $CTL set_policy fed12 "^fed12\." '{"federation-upstream-set": "upstream12"}' 20 | $CTL set_policy one "^two$" '{"federation-upstream-set": "one"}' 21 | $CTL set_policy two "^one$" '{"federation-upstream-set": "two"}' 22 | $CTL set_policy hare "^hare\." '{"federation-upstream-set": "upstream5673"}' 23 | $CTL set_policy all "^all\." '{"federation-upstream-set": "all"}' 24 | $CTL set_policy new "^new\." '{"federation-upstream-set": "new-set"}' 25 | -------------------------------------------------------------------------------- /etc/setup-rabbit-test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | sh -e `dirname $0`/rabbit-test.sh "$DEPS_DIR/rabbit/scripts/rabbitmqctl -n $RABBITMQ_NODENAME" 3 | -------------------------------------------------------------------------------- /include/rabbit_federation.hrl: -------------------------------------------------------------------------------- 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 | -record(upstream, {uris, 9 | exchange_name, 10 | queue_name, 11 | consumer_tag, 12 | prefetch_count, 13 | max_hops, 14 | reconnect_delay, 15 | expires, 16 | message_ttl, 17 | trust_user_id, 18 | ack_mode, 19 | ha_policy, 20 | name, 21 | bind_nowait, 22 | resource_cleanup_mode}). 23 | 24 | -record(upstream_params, 25 | {uri, 26 | params, 27 | x_or_q, 28 | %% The next two can be derived from the above three, but we don't 29 | %% want to do that every time we forward a message. 30 | safe_uri, 31 | table}). 32 | 33 | %% Name of the message header used to collect the hop (forwarding) path 34 | %% metadata as the message is forwarded by exchange federation. 35 | -define(ROUTING_HEADER, <<"x-received-from">>). 36 | -define(BINDING_HEADER, <<"x-bound-from">>). 37 | -define(MAX_HOPS_ARG, <<"x-max-hops">>). 38 | %% Identifies a cluster, used by exchange federation cycle detection 39 | -define(DOWNSTREAM_NAME_ARG, <<"x-downstream-name">>). 40 | %% Identifies a virtual host, used by exchange federation cycle detection 41 | -define(DOWNSTREAM_VHOST_ARG, <<"x-downstream-vhost">>). 42 | -define(DEF_PREFETCH, 1000). 43 | 44 | -define(FEDERATION_GUIDE_URL, <<"https://rabbitmq.com/federation.html">>). 45 | -------------------------------------------------------------------------------- /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 rabbitmq-server-release; 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 | PROJECT_VERSION := $(RABBITMQ_VERSION) 15 | 16 | ifeq ($(PROJECT_VERSION),) 17 | PROJECT_VERSION := $(shell \ 18 | if test -f git-revisions.txt; then \ 19 | head -n1 git-revisions.txt | \ 20 | awk '{print $$$(words $(PROJECT_DESCRIPTION) version);}'; \ 21 | else \ 22 | (git describe --dirty --abbrev=7 --tags --always --first-parent \ 23 | 2>/dev/null || echo rabbitmq_v0_0_0) | \ 24 | sed -e 's/^rabbitmq_v//' -e 's/^v//' -e 's/_/./g' -e 's/-/+/' \ 25 | -e 's/-/./g'; \ 26 | fi) 27 | endif 28 | 29 | # -------------------------------------------------------------------- 30 | # RabbitMQ components. 31 | # -------------------------------------------------------------------- 32 | 33 | # For RabbitMQ repositories, we want to checkout branches which match 34 | # the parent project. For instance, if the parent project is on a 35 | # release tag, dependencies must be on the same release tag. If the 36 | # parent project is on a topic branch, dependencies must be on the same 37 | # topic branch or fallback to `stable` or `master` whichever was the 38 | # base of the topic branch. 39 | 40 | dep_amqp_client = git_rmq rabbitmq-erlang-client $(current_rmq_ref) $(base_rmq_ref) master 41 | dep_amqp10_client = git_rmq rabbitmq-amqp1.0-client $(current_rmq_ref) $(base_rmq_ref) master 42 | dep_amqp10_common = git_rmq rabbitmq-amqp1.0-common $(current_rmq_ref) $(base_rmq_ref) master 43 | dep_rabbit = git_rmq rabbitmq-server $(current_rmq_ref) $(base_rmq_ref) master 44 | dep_rabbit_common = git_rmq rabbitmq-common $(current_rmq_ref) $(base_rmq_ref) master 45 | dep_rabbitmq_amqp1_0 = git_rmq rabbitmq-amqp1.0 $(current_rmq_ref) $(base_rmq_ref) master 46 | dep_rabbitmq_auth_backend_amqp = git_rmq rabbitmq-auth-backend-amqp $(current_rmq_ref) $(base_rmq_ref) master 47 | dep_rabbitmq_auth_backend_cache = git_rmq rabbitmq-auth-backend-cache $(current_rmq_ref) $(base_rmq_ref) master 48 | dep_rabbitmq_auth_backend_http = git_rmq rabbitmq-auth-backend-http $(current_rmq_ref) $(base_rmq_ref) master 49 | dep_rabbitmq_auth_backend_ldap = git_rmq rabbitmq-auth-backend-ldap $(current_rmq_ref) $(base_rmq_ref) master 50 | dep_rabbitmq_auth_backend_oauth2 = git_rmq rabbitmq-auth-backend-oauth2 $(current_rmq_ref) $(base_rmq_ref) master 51 | dep_rabbitmq_auth_mechanism_ssl = git_rmq rabbitmq-auth-mechanism-ssl $(current_rmq_ref) $(base_rmq_ref) master 52 | dep_rabbitmq_aws = git_rmq rabbitmq-aws $(current_rmq_ref) $(base_rmq_ref) master 53 | dep_rabbitmq_boot_steps_visualiser = git_rmq rabbitmq-boot-steps-visualiser $(current_rmq_ref) $(base_rmq_ref) master 54 | dep_rabbitmq_cli = git_rmq rabbitmq-cli $(current_rmq_ref) $(base_rmq_ref) master 55 | dep_rabbitmq_codegen = git_rmq rabbitmq-codegen $(current_rmq_ref) $(base_rmq_ref) master 56 | dep_rabbitmq_consistent_hash_exchange = git_rmq rabbitmq-consistent-hash-exchange $(current_rmq_ref) $(base_rmq_ref) master 57 | dep_rabbitmq_ct_client_helpers = git_rmq rabbitmq-ct-client-helpers $(current_rmq_ref) $(base_rmq_ref) master 58 | dep_rabbitmq_ct_helpers = git_rmq rabbitmq-ct-helpers $(current_rmq_ref) $(base_rmq_ref) master 59 | dep_rabbitmq_delayed_message_exchange = git_rmq rabbitmq-delayed-message-exchange $(current_rmq_ref) $(base_rmq_ref) master 60 | dep_rabbitmq_dotnet_client = git_rmq rabbitmq-dotnet-client $(current_rmq_ref) $(base_rmq_ref) master 61 | dep_rabbitmq_event_exchange = git_rmq rabbitmq-event-exchange $(current_rmq_ref) $(base_rmq_ref) master 62 | dep_rabbitmq_federation = git_rmq rabbitmq-federation $(current_rmq_ref) $(base_rmq_ref) master 63 | dep_rabbitmq_federation_management = git_rmq rabbitmq-federation-management $(current_rmq_ref) $(base_rmq_ref) master 64 | dep_rabbitmq_java_client = git_rmq rabbitmq-java-client $(current_rmq_ref) $(base_rmq_ref) master 65 | dep_rabbitmq_jms_client = git_rmq rabbitmq-jms-client $(current_rmq_ref) $(base_rmq_ref) master 66 | dep_rabbitmq_jms_cts = git_rmq rabbitmq-jms-cts $(current_rmq_ref) $(base_rmq_ref) master 67 | dep_rabbitmq_jms_topic_exchange = git_rmq rabbitmq-jms-topic-exchange $(current_rmq_ref) $(base_rmq_ref) master 68 | dep_rabbitmq_lvc_exchange = git_rmq rabbitmq-lvc-exchange $(current_rmq_ref) $(base_rmq_ref) master 69 | dep_rabbitmq_management = git_rmq rabbitmq-management $(current_rmq_ref) $(base_rmq_ref) master 70 | dep_rabbitmq_management_agent = git_rmq rabbitmq-management-agent $(current_rmq_ref) $(base_rmq_ref) master 71 | dep_rabbitmq_management_exchange = git_rmq rabbitmq-management-exchange $(current_rmq_ref) $(base_rmq_ref) master 72 | dep_rabbitmq_management_themes = git_rmq rabbitmq-management-themes $(current_rmq_ref) $(base_rmq_ref) master 73 | dep_rabbitmq_message_timestamp = git_rmq rabbitmq-message-timestamp $(current_rmq_ref) $(base_rmq_ref) master 74 | dep_rabbitmq_metronome = git_rmq rabbitmq-metronome $(current_rmq_ref) $(base_rmq_ref) master 75 | dep_rabbitmq_mqtt = git_rmq rabbitmq-mqtt $(current_rmq_ref) $(base_rmq_ref) master 76 | dep_rabbitmq_objc_client = git_rmq rabbitmq-objc-client $(current_rmq_ref) $(base_rmq_ref) master 77 | dep_rabbitmq_peer_discovery_aws = git_rmq rabbitmq-peer-discovery-aws $(current_rmq_ref) $(base_rmq_ref) master 78 | dep_rabbitmq_peer_discovery_common = git_rmq rabbitmq-peer-discovery-common $(current_rmq_ref) $(base_rmq_ref) master 79 | dep_rabbitmq_peer_discovery_consul = git_rmq rabbitmq-peer-discovery-consul $(current_rmq_ref) $(base_rmq_ref) master 80 | dep_rabbitmq_peer_discovery_etcd = git_rmq rabbitmq-peer-discovery-etcd $(current_rmq_ref) $(base_rmq_ref) master 81 | dep_rabbitmq_peer_discovery_k8s = git_rmq rabbitmq-peer-discovery-k8s $(current_rmq_ref) $(base_rmq_ref) master 82 | dep_rabbitmq_prometheus = git_rmq rabbitmq-prometheus $(current_rmq_ref) $(base_rmq_ref) master 83 | dep_rabbitmq_random_exchange = git_rmq rabbitmq-random-exchange $(current_rmq_ref) $(base_rmq_ref) master 84 | dep_rabbitmq_recent_history_exchange = git_rmq rabbitmq-recent-history-exchange $(current_rmq_ref) $(base_rmq_ref) master 85 | dep_rabbitmq_routing_node_stamp = git_rmq rabbitmq-routing-node-stamp $(current_rmq_ref) $(base_rmq_ref) master 86 | dep_rabbitmq_rtopic_exchange = git_rmq rabbitmq-rtopic-exchange $(current_rmq_ref) $(base_rmq_ref) master 87 | dep_rabbitmq_server_release = git_rmq rabbitmq-server-release $(current_rmq_ref) $(base_rmq_ref) master 88 | dep_rabbitmq_sharding = git_rmq rabbitmq-sharding $(current_rmq_ref) $(base_rmq_ref) master 89 | dep_rabbitmq_shovel = git_rmq rabbitmq-shovel $(current_rmq_ref) $(base_rmq_ref) master 90 | dep_rabbitmq_shovel_management = git_rmq rabbitmq-shovel-management $(current_rmq_ref) $(base_rmq_ref) master 91 | dep_rabbitmq_stomp = git_rmq rabbitmq-stomp $(current_rmq_ref) $(base_rmq_ref) master 92 | dep_rabbitmq_stream = git_rmq rabbitmq-stream $(current_rmq_ref) $(base_rmq_ref) master 93 | dep_rabbitmq_toke = git_rmq rabbitmq-toke $(current_rmq_ref) $(base_rmq_ref) master 94 | dep_rabbitmq_top = git_rmq rabbitmq-top $(current_rmq_ref) $(base_rmq_ref) master 95 | dep_rabbitmq_tracing = git_rmq rabbitmq-tracing $(current_rmq_ref) $(base_rmq_ref) master 96 | dep_rabbitmq_trust_store = git_rmq rabbitmq-trust-store $(current_rmq_ref) $(base_rmq_ref) master 97 | dep_rabbitmq_test = git_rmq rabbitmq-test $(current_rmq_ref) $(base_rmq_ref) master 98 | dep_rabbitmq_web_dispatch = git_rmq rabbitmq-web-dispatch $(current_rmq_ref) $(base_rmq_ref) master 99 | dep_rabbitmq_web_stomp = git_rmq rabbitmq-web-stomp $(current_rmq_ref) $(base_rmq_ref) master 100 | dep_rabbitmq_web_stomp_examples = git_rmq rabbitmq-web-stomp-examples $(current_rmq_ref) $(base_rmq_ref) master 101 | dep_rabbitmq_web_mqtt = git_rmq rabbitmq-web-mqtt $(current_rmq_ref) $(base_rmq_ref) master 102 | dep_rabbitmq_web_mqtt_examples = git_rmq rabbitmq-web-mqtt-examples $(current_rmq_ref) $(base_rmq_ref) master 103 | dep_rabbitmq_website = git_rmq rabbitmq-website $(current_rmq_ref) $(base_rmq_ref) live master 104 | dep_toke = git_rmq toke $(current_rmq_ref) $(base_rmq_ref) master 105 | 106 | dep_rabbitmq_public_umbrella = git_rmq rabbitmq-public-umbrella $(current_rmq_ref) $(base_rmq_ref) master 107 | 108 | # Third-party dependencies version pinning. 109 | # 110 | # We do that in this file, which is copied in all projects, to ensure 111 | # all projects use the same versions. It avoids conflicts and makes it 112 | # possible to work with rabbitmq-public-umbrella. 113 | 114 | dep_accept = hex 0.3.5 115 | dep_cowboy = hex 2.8.0 116 | dep_cowlib = hex 2.9.1 117 | dep_jsx = hex 2.11.0 118 | dep_lager = hex 3.8.0 119 | dep_prometheus = git https://github.com/deadtrickster/prometheus.erl.git master 120 | dep_ra = git https://github.com/rabbitmq/ra.git master 121 | dep_ranch = hex 1.7.1 122 | dep_recon = hex 2.5.1 123 | dep_observer_cli = hex 1.5.4 124 | dep_stdout_formatter = hex 0.2.4 125 | dep_sysmon_handler = hex 1.3.0 126 | 127 | RABBITMQ_COMPONENTS = amqp_client \ 128 | amqp10_common \ 129 | amqp10_client \ 130 | rabbit \ 131 | rabbit_common \ 132 | rabbitmq_amqp1_0 \ 133 | rabbitmq_auth_backend_amqp \ 134 | rabbitmq_auth_backend_cache \ 135 | rabbitmq_auth_backend_http \ 136 | rabbitmq_auth_backend_ldap \ 137 | rabbitmq_auth_backend_oauth2 \ 138 | rabbitmq_auth_mechanism_ssl \ 139 | rabbitmq_aws \ 140 | rabbitmq_boot_steps_visualiser \ 141 | rabbitmq_cli \ 142 | rabbitmq_codegen \ 143 | rabbitmq_consistent_hash_exchange \ 144 | rabbitmq_ct_client_helpers \ 145 | rabbitmq_ct_helpers \ 146 | rabbitmq_delayed_message_exchange \ 147 | rabbitmq_dotnet_client \ 148 | rabbitmq_event_exchange \ 149 | rabbitmq_federation \ 150 | rabbitmq_federation_management \ 151 | rabbitmq_java_client \ 152 | rabbitmq_jms_client \ 153 | rabbitmq_jms_cts \ 154 | rabbitmq_jms_topic_exchange \ 155 | rabbitmq_lvc_exchange \ 156 | rabbitmq_management \ 157 | rabbitmq_management_agent \ 158 | rabbitmq_management_exchange \ 159 | rabbitmq_management_themes \ 160 | rabbitmq_message_timestamp \ 161 | rabbitmq_metronome \ 162 | rabbitmq_mqtt \ 163 | rabbitmq_objc_client \ 164 | rabbitmq_peer_discovery_aws \ 165 | rabbitmq_peer_discovery_common \ 166 | rabbitmq_peer_discovery_consul \ 167 | rabbitmq_peer_discovery_etcd \ 168 | rabbitmq_peer_discovery_k8s \ 169 | rabbitmq_prometheus \ 170 | rabbitmq_random_exchange \ 171 | rabbitmq_recent_history_exchange \ 172 | rabbitmq_routing_node_stamp \ 173 | rabbitmq_rtopic_exchange \ 174 | rabbitmq_server_release \ 175 | rabbitmq_sharding \ 176 | rabbitmq_shovel \ 177 | rabbitmq_shovel_management \ 178 | rabbitmq_stomp \ 179 | rabbitmq_stream \ 180 | rabbitmq_toke \ 181 | rabbitmq_top \ 182 | rabbitmq_tracing \ 183 | rabbitmq_trust_store \ 184 | rabbitmq_web_dispatch \ 185 | rabbitmq_web_mqtt \ 186 | rabbitmq_web_mqtt_examples \ 187 | rabbitmq_web_stomp \ 188 | rabbitmq_web_stomp_examples \ 189 | rabbitmq_website 190 | 191 | # Erlang.mk does not rebuild dependencies by default, once they were 192 | # compiled once, except for those listed in the `$(FORCE_REBUILD)` 193 | # variable. 194 | # 195 | # We want all RabbitMQ components to always be rebuilt: this eases 196 | # the work on several components at the same time. 197 | 198 | FORCE_REBUILD = $(RABBITMQ_COMPONENTS) 199 | 200 | # Several components have a custom erlang.mk/build.config, mainly 201 | # to disable eunit. Therefore, we can't use the top-level project's 202 | # erlang.mk copy. 203 | NO_AUTOPATCH += $(RABBITMQ_COMPONENTS) 204 | 205 | ifeq ($(origin current_rmq_ref),undefined) 206 | ifneq ($(wildcard .git),) 207 | current_rmq_ref := $(shell (\ 208 | ref=$$(LANG=C git branch --list | awk '/^\* \(.*detached / {ref=$$0; sub(/.*detached [^ ]+ /, "", ref); sub(/\)$$/, "", ref); print ref; exit;} /^\* / {ref=$$0; sub(/^\* /, "", ref); print ref; exit}');\ 209 | if test "$$(git rev-parse --short HEAD)" != "$$ref"; then echo "$$ref"; fi)) 210 | else 211 | current_rmq_ref := master 212 | endif 213 | endif 214 | export current_rmq_ref 215 | 216 | ifeq ($(origin base_rmq_ref),undefined) 217 | ifneq ($(wildcard .git),) 218 | possible_base_rmq_ref := master 219 | ifeq ($(possible_base_rmq_ref),$(current_rmq_ref)) 220 | base_rmq_ref := $(current_rmq_ref) 221 | else 222 | base_rmq_ref := $(shell \ 223 | (git rev-parse --verify -q master >/dev/null && \ 224 | git rev-parse --verify -q $(possible_base_rmq_ref) >/dev/null && \ 225 | git merge-base --is-ancestor $$(git merge-base master HEAD) $(possible_base_rmq_ref) && \ 226 | echo $(possible_base_rmq_ref)) || \ 227 | echo master) 228 | endif 229 | else 230 | base_rmq_ref := master 231 | endif 232 | endif 233 | export base_rmq_ref 234 | 235 | # Repository URL selection. 236 | # 237 | # First, we infer other components' location from the current project 238 | # repository URL, if it's a Git repository: 239 | # - We take the "origin" remote URL as the base 240 | # - The current project name and repository name is replaced by the 241 | # target's properties: 242 | # eg. rabbitmq-common is replaced by rabbitmq-codegen 243 | # eg. rabbit_common is replaced by rabbitmq_codegen 244 | # 245 | # If cloning from this computed location fails, we fallback to RabbitMQ 246 | # upstream which is GitHub. 247 | 248 | # Macro to transform eg. "rabbit_common" to "rabbitmq-common". 249 | rmq_cmp_repo_name = $(word 2,$(dep_$(1))) 250 | 251 | # Upstream URL for the current project. 252 | RABBITMQ_COMPONENT_REPO_NAME := $(call rmq_cmp_repo_name,$(PROJECT)) 253 | RABBITMQ_UPSTREAM_FETCH_URL ?= https://github.com/rabbitmq/$(RABBITMQ_COMPONENT_REPO_NAME).git 254 | RABBITMQ_UPSTREAM_PUSH_URL ?= git@github.com:rabbitmq/$(RABBITMQ_COMPONENT_REPO_NAME).git 255 | 256 | # Current URL for the current project. If this is not a Git clone, 257 | # default to the upstream Git repository. 258 | ifneq ($(wildcard .git),) 259 | git_origin_fetch_url := $(shell git config remote.origin.url) 260 | git_origin_push_url := $(shell git config remote.origin.pushurl || git config remote.origin.url) 261 | RABBITMQ_CURRENT_FETCH_URL ?= $(git_origin_fetch_url) 262 | RABBITMQ_CURRENT_PUSH_URL ?= $(git_origin_push_url) 263 | else 264 | RABBITMQ_CURRENT_FETCH_URL ?= $(RABBITMQ_UPSTREAM_FETCH_URL) 265 | RABBITMQ_CURRENT_PUSH_URL ?= $(RABBITMQ_UPSTREAM_PUSH_URL) 266 | endif 267 | 268 | # Macro to replace the following pattern: 269 | # 1. /foo.git -> /bar.git 270 | # 2. /foo -> /bar 271 | # 3. /foo/ -> /bar/ 272 | subst_repo_name = $(patsubst %/$(1)/%,%/$(2)/%,$(patsubst %/$(1),%/$(2),$(patsubst %/$(1).git,%/$(2).git,$(3)))) 273 | 274 | # Macro to replace both the project's name (eg. "rabbit_common") and 275 | # repository name (eg. "rabbitmq-common") by the target's equivalent. 276 | # 277 | # This macro is kept on one line because we don't want whitespaces in 278 | # the returned value, as it's used in $(dep_fetch_git_rmq) in a shell 279 | # single-quoted string. 280 | dep_rmq_repo = $(if $(dep_$(2)),$(call subst_repo_name,$(PROJECT),$(2),$(call subst_repo_name,$(RABBITMQ_COMPONENT_REPO_NAME),$(call rmq_cmp_repo_name,$(2)),$(1))),$(pkg_$(1)_repo)) 281 | 282 | dep_rmq_commits = $(if $(dep_$(1)), \ 283 | $(wordlist 3,$(words $(dep_$(1))),$(dep_$(1))), \ 284 | $(pkg_$(1)_commit)) 285 | 286 | define dep_fetch_git_rmq 287 | fetch_url1='$(call dep_rmq_repo,$(RABBITMQ_CURRENT_FETCH_URL),$(1))'; \ 288 | fetch_url2='$(call dep_rmq_repo,$(RABBITMQ_UPSTREAM_FETCH_URL),$(1))'; \ 289 | if test "$$$$fetch_url1" != '$(RABBITMQ_CURRENT_FETCH_URL)' && \ 290 | git clone -q -n -- "$$$$fetch_url1" $(DEPS_DIR)/$(call dep_name,$(1)); then \ 291 | fetch_url="$$$$fetch_url1"; \ 292 | push_url='$(call dep_rmq_repo,$(RABBITMQ_CURRENT_PUSH_URL),$(1))'; \ 293 | elif git clone -q -n -- "$$$$fetch_url2" $(DEPS_DIR)/$(call dep_name,$(1)); then \ 294 | fetch_url="$$$$fetch_url2"; \ 295 | push_url='$(call dep_rmq_repo,$(RABBITMQ_UPSTREAM_PUSH_URL),$(1))'; \ 296 | fi; \ 297 | cd $(DEPS_DIR)/$(call dep_name,$(1)) && ( \ 298 | $(foreach ref,$(call dep_rmq_commits,$(1)), \ 299 | git checkout -q $(ref) >/dev/null 2>&1 || \ 300 | ) \ 301 | (echo "error: no valid pathspec among: $(call dep_rmq_commits,$(1))" \ 302 | 1>&2 && false) ) && \ 303 | (test "$$$$fetch_url" = "$$$$push_url" || \ 304 | git remote set-url --push origin "$$$$push_url") 305 | endef 306 | 307 | # -------------------------------------------------------------------- 308 | # Component distribution. 309 | # -------------------------------------------------------------------- 310 | 311 | list-dist-deps:: 312 | @: 313 | 314 | prepare-dist:: 315 | @: 316 | 317 | # -------------------------------------------------------------------- 318 | # Umbrella-specific settings. 319 | # -------------------------------------------------------------------- 320 | 321 | # If the top-level project is a RabbitMQ component, we override 322 | # $(DEPS_DIR) for this project to point to the top-level's one. 323 | # 324 | # We also verify that the guessed DEPS_DIR is actually named `deps`, 325 | # to rule out any situation where it is a coincidence that we found a 326 | # `rabbitmq-components.mk` up upper directories. 327 | 328 | possible_deps_dir_1 = $(abspath ..) 329 | possible_deps_dir_2 = $(abspath ../../..) 330 | 331 | ifeq ($(notdir $(possible_deps_dir_1)),deps) 332 | ifneq ($(wildcard $(possible_deps_dir_1)/../rabbitmq-components.mk),) 333 | deps_dir_overriden = 1 334 | DEPS_DIR ?= $(possible_deps_dir_1) 335 | DISABLE_DISTCLEAN = 1 336 | endif 337 | endif 338 | 339 | ifeq ($(deps_dir_overriden),) 340 | ifeq ($(notdir $(possible_deps_dir_2)),deps) 341 | ifneq ($(wildcard $(possible_deps_dir_2)/../rabbitmq-components.mk),) 342 | deps_dir_overriden = 1 343 | DEPS_DIR ?= $(possible_deps_dir_2) 344 | DISABLE_DISTCLEAN = 1 345 | endif 346 | endif 347 | endif 348 | 349 | ifneq ($(wildcard UMBRELLA.md),) 350 | DISABLE_DISTCLEAN = 1 351 | endif 352 | 353 | # We disable `make distclean` so $(DEPS_DIR) is not accidentally removed. 354 | 355 | ifeq ($(DISABLE_DISTCLEAN),1) 356 | ifneq ($(filter distclean distclean-deps,$(MAKECMDGOALS)),) 357 | SKIP_DEPS = 1 358 | endif 359 | endif 360 | -------------------------------------------------------------------------------- /src/Elixir.RabbitMQ.CLI.Ctl.Commands.FederationStatusCommand.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('Elixir.RabbitMQ.CLI.Ctl.Commands.FederationStatusCommand'). 9 | 10 | -include("rabbit_federation.hrl"). 11 | 12 | -behaviour('Elixir.RabbitMQ.CLI.CommandBehaviour'). 13 | 14 | -export([ 15 | usage/0, 16 | usage_additional/0, 17 | usage_doc_guides/0, 18 | flags/0, 19 | validate/2, 20 | merge_defaults/2, 21 | banner/2, 22 | run/2, 23 | switches/0, 24 | aliases/0, 25 | output/2, 26 | scopes/0, 27 | formatter/0, 28 | help_section/0, 29 | description/0 30 | ]). 31 | 32 | 33 | %%---------------------------------------------------------------------------- 34 | %% Callbacks 35 | %%---------------------------------------------------------------------------- 36 | usage() -> 37 | <<"federation_status [--only-down]">>. 38 | 39 | usage_additional() -> 40 | [ 41 | {<<"--only-down">>, <<"only display links that failed or are not currently connected">>} 42 | ]. 43 | 44 | usage_doc_guides() -> 45 | [?FEDERATION_GUIDE_URL]. 46 | 47 | help_section() -> 48 | {plugin, federation}. 49 | 50 | description() -> 51 | <<"Displays federation link status">>. 52 | 53 | flags() -> 54 | []. 55 | 56 | validate(_,_) -> 57 | ok. 58 | 59 | formatter() -> 60 | 'Elixir.RabbitMQ.CLI.Formatters.Erlang'. 61 | 62 | merge_defaults(A, Opts) -> 63 | {A, maps:merge(#{only_down => false}, Opts)}. 64 | 65 | banner(_, #{node := Node, only_down := true}) -> 66 | erlang:iolist_to_binary([<<"Listing federation links which are down on node ">>, 67 | atom_to_binary(Node, utf8), <<"...">>]); 68 | banner(_, #{node := Node, only_down := false}) -> 69 | erlang:iolist_to_binary([<<"Listing federation links on node ">>, 70 | atom_to_binary(Node, utf8), <<"...">>]). 71 | 72 | run(_Args, #{node := Node, only_down := OnlyDown}) -> 73 | case rabbit_misc:rpc_call(Node, rabbit_federation_status, status, []) of 74 | {badrpc, _} = Error -> 75 | Error; 76 | Status -> 77 | {stream, filter(Status, OnlyDown)} 78 | end. 79 | 80 | switches() -> 81 | [{only_down, boolean}]. 82 | 83 | aliases() -> 84 | []. 85 | 86 | output({stream, FederationStatus}, _) -> 87 | Formatted = [begin 88 | Timestamp = proplists:get_value(timestamp, St), 89 | Map0 = maps:remove(timestamp, maps:from_list(St)), 90 | Map1 = maps:merge(#{queue => <<>>, 91 | exchange => <<>>, 92 | upstream_queue => <<>>, 93 | upstream_exchange => <<>>, 94 | local_connection => <<>>, 95 | error => <<>>}, Map0), 96 | Map1#{last_changed => fmt_ts(Timestamp)} 97 | end || St <- FederationStatus], 98 | {stream, Formatted}; 99 | output(E, _Opts) -> 100 | 'Elixir.RabbitMQ.CLI.DefaultOutput':output(E). 101 | 102 | scopes() -> 103 | ['ctl', 'diagnostics']. 104 | 105 | %%---------------------------------------------------------------------------- 106 | %% Formatting 107 | %%---------------------------------------------------------------------------- 108 | fmt_ts({{YY, MM, DD}, {Hour, Min, Sec}}) -> 109 | erlang:list_to_binary( 110 | io_lib:format("~4..0w-~2..0w-~2..0w ~2..0w:~2..0w:~2..0w", 111 | [YY, MM, DD, Hour, Min, Sec])). 112 | 113 | filter(Status, _OnlyDown = false) -> 114 | Status; 115 | filter(Status, _OnlyDown = true) -> 116 | [St || St <- Status, 117 | not lists:member(proplists:get_value(status, St), [running, starting])]. 118 | -------------------------------------------------------------------------------- /src/Elixir.RabbitMQ.CLI.Ctl.Commands.RestartFederationLinkCommand.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('Elixir.RabbitMQ.CLI.Ctl.Commands.RestartFederationLinkCommand'). 9 | 10 | -include("rabbit_federation.hrl"). 11 | 12 | -behaviour('Elixir.RabbitMQ.CLI.CommandBehaviour'). 13 | 14 | -export([ 15 | usage/0, 16 | usage_additional/0, 17 | usage_doc_guides/0, 18 | flags/0, 19 | validate/2, 20 | merge_defaults/2, 21 | banner/2, 22 | run/2, 23 | aliases/0, 24 | output/2, 25 | help_section/0, 26 | description/0 27 | ]). 28 | 29 | 30 | %%---------------------------------------------------------------------------- 31 | %% Callbacks 32 | %%---------------------------------------------------------------------------- 33 | usage() -> 34 | <<"restart_federation_link ">>. 35 | 36 | usage_additional() -> 37 | [ 38 | {<<"">>, <<"ID of the link to restart">>} 39 | ]. 40 | 41 | usage_doc_guides() -> 42 | [?FEDERATION_GUIDE_URL]. 43 | 44 | help_section() -> 45 | {plugin, federation}. 46 | 47 | description() -> 48 | <<"Restarts a running federation link">>. 49 | 50 | flags() -> 51 | []. 52 | 53 | validate([], _Opts) -> 54 | {validation_failure, not_enough_args}; 55 | validate([_, _ | _], _Opts) -> 56 | {validation_failure, too_many_args}; 57 | validate([_], _) -> 58 | ok. 59 | 60 | merge_defaults(A, O) -> 61 | {A, O}. 62 | 63 | banner([Link], #{node := Node}) -> 64 | erlang:iolist_to_binary([<<"Restarting federation link ">>, Link, << " on node ">>, 65 | atom_to_binary(Node, utf8)]). 66 | 67 | run([Id], #{node := Node}) -> 68 | case rabbit_misc:rpc_call(Node, rabbit_federation_status, lookup, [Id]) of 69 | {badrpc, _} = Error -> 70 | Error; 71 | not_found -> 72 | {error, <<"Link with the given ID was not found">>}; 73 | Obj -> 74 | Upstream = proplists:get_value(upstream, Obj), 75 | Supervisor = proplists:get_value(supervisor, Obj), 76 | rabbit_misc:rpc_call(Node, rabbit_federation_link_sup, restart, 77 | [Supervisor, Upstream]) 78 | end. 79 | 80 | aliases() -> 81 | []. 82 | 83 | output(Output, _Opts) -> 84 | 'Elixir.RabbitMQ.CLI.DefaultOutput':output(Output). 85 | -------------------------------------------------------------------------------- /src/rabbit_federation_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_federation_app). 9 | 10 | -behaviour(application). 11 | -export([start/2, stop/1]). 12 | 13 | %% Dummy supervisor - see Ulf Wiger's comment at 14 | %% http://erlang.2086793.n4.nabble.com/initializing-library-applications-without-processes-td2094473.html 15 | 16 | %% All of our actual server processes are supervised by 17 | %% rabbit_federation_sup, which is started by a rabbit_boot_step 18 | %% (since it needs to start up before queue / exchange recovery, so it 19 | %% can't be part of our application). 20 | %% 21 | %% However, we still need an application behaviour since we need to 22 | %% know when our application has started since then the Erlang client 23 | %% will have started and we can therefore start our links going. Since 24 | %% the application behaviour needs a tree of processes to supervise, 25 | %% this is it... 26 | -behaviour(supervisor). 27 | -export([init/1]). 28 | 29 | start(_Type, _StartArgs) -> 30 | rabbit_federation_exchange_link:go(), 31 | rabbit_federation_queue_link:go(), 32 | supervisor:start_link({local, ?MODULE}, ?MODULE, []). 33 | 34 | stop(_State) -> 35 | ok. 36 | %%---------------------------------------------------------------------------- 37 | 38 | init([]) -> {ok, {{one_for_one, 3, 10}, []}}. 39 | -------------------------------------------------------------------------------- /src/rabbit_federation_db.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_federation_db). 9 | 10 | -include("rabbit_federation.hrl"). 11 | -include_lib("amqp_client/include/amqp_client.hrl"). 12 | 13 | -define(DICT, orddict). 14 | 15 | -export([get_active_suffix/3, set_active_suffix/3, prune_scratch/2]). 16 | 17 | %%---------------------------------------------------------------------------- 18 | 19 | get_active_suffix(XName, Upstream, Default) -> 20 | case rabbit_exchange:lookup_scratch(XName, federation) of 21 | {ok, Dict} -> 22 | case ?DICT:find(key(Upstream), Dict) of 23 | {ok, Suffix} -> Suffix; 24 | error -> Default 25 | end; 26 | {error, not_found} -> 27 | Default 28 | end. 29 | 30 | set_active_suffix(XName, Upstream, Suffix) -> 31 | ok = rabbit_exchange:update_scratch( 32 | XName, federation, 33 | fun(D) -> ?DICT:store(key(Upstream), Suffix, ensure(D)) end). 34 | 35 | prune_scratch(XName, Upstreams) -> 36 | ok = rabbit_exchange:update_scratch( 37 | XName, federation, 38 | fun(D) -> Keys = [key(U) || U <- Upstreams], 39 | ?DICT:filter( 40 | fun(K, _V) -> lists:member(K, Keys) end, ensure(D)) 41 | end). 42 | 43 | key(#upstream{name = UpstreamName, exchange_name = XNameBin}) -> 44 | {UpstreamName, XNameBin}. 45 | 46 | ensure(undefined) -> ?DICT:new(); 47 | ensure(D) -> D. 48 | -------------------------------------------------------------------------------- /src/rabbit_federation_event.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_federation_event). 9 | -behaviour(gen_event). 10 | 11 | -include_lib("rabbit_common/include/rabbit.hrl"). 12 | 13 | -export([add_handler/0, remove_handler/0]). 14 | 15 | -export([init/1, handle_call/2, handle_event/2, handle_info/2, 16 | terminate/2, code_change/3]). 17 | 18 | -import(rabbit_misc, [pget/2]). 19 | 20 | %%---------------------------------------------------------------------------- 21 | 22 | add_handler() -> 23 | gen_event:add_handler(rabbit_event, ?MODULE, []). 24 | 25 | remove_handler() -> 26 | gen_event:delete_handler(rabbit_event, ?MODULE, []). 27 | 28 | init([]) -> 29 | {ok, []}. 30 | 31 | handle_call(_Request, State) -> 32 | {ok, not_understood, State}. 33 | 34 | handle_event(#event{type = parameter_set, 35 | props = Props0}, State) -> 36 | Props = rabbit_data_coercion:to_list(Props0), 37 | case {pget(component, Props), pget(name, Props)} of 38 | {global, cluster_name} -> 39 | rabbit_federation_parameters:adjust(everything); 40 | _ -> 41 | ok 42 | end, 43 | {ok, State}; 44 | handle_event(_Event, State) -> 45 | {ok, State}. 46 | 47 | handle_info(_Info, State) -> 48 | {ok, State}. 49 | 50 | terminate(_Arg, _State) -> 51 | ok. 52 | 53 | code_change(_OldVsn, State, _Extra) -> 54 | {ok, State}. 55 | -------------------------------------------------------------------------------- /src/rabbit_federation_exchange.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 | %% TODO rename this 9 | -module(rabbit_federation_exchange). 10 | 11 | -rabbit_boot_step({?MODULE, 12 | [{description, "federation exchange decorator"}, 13 | {mfa, {rabbit_registry, register, 14 | [exchange_decorator, <<"federation">>, ?MODULE]}}, 15 | {requires, rabbit_registry}, 16 | {cleanup, {rabbit_registry, unregister, 17 | [exchange_decorator, <<"federation">>]}}, 18 | {enables, recovery}]}). 19 | 20 | -include_lib("amqp_client/include/amqp_client.hrl"). 21 | 22 | -behaviour(rabbit_exchange_decorator). 23 | 24 | -export([description/0, serialise_events/1]). 25 | -export([create/2, delete/3, policy_changed/2, 26 | add_binding/3, remove_bindings/3, route/2, active_for/1]). 27 | 28 | %%---------------------------------------------------------------------------- 29 | 30 | description() -> 31 | [{description, <<"Federation exchange decorator">>}]. 32 | 33 | serialise_events(X) -> federate(X). 34 | 35 | create(transaction, _X) -> 36 | ok; 37 | create(none, X) -> 38 | maybe_start(X). 39 | 40 | delete(transaction, _X, _Bs) -> 41 | ok; 42 | delete(none, X, _Bs) -> 43 | maybe_stop(X). 44 | 45 | policy_changed(OldX, NewX) -> 46 | maybe_stop(OldX), 47 | maybe_start(NewX). 48 | 49 | add_binding(transaction, _X, _B) -> 50 | ok; 51 | add_binding(Serial, X = #exchange{name = XName}, B) -> 52 | case federate(X) of 53 | true -> rabbit_federation_exchange_link:add_binding(Serial, XName, B), 54 | ok; 55 | false -> ok 56 | end. 57 | 58 | remove_bindings(transaction, _X, _Bs) -> 59 | ok; 60 | remove_bindings(Serial, X = #exchange{name = XName}, Bs) -> 61 | case federate(X) of 62 | true -> rabbit_federation_exchange_link:remove_bindings(Serial, XName, Bs), 63 | ok; 64 | false -> ok 65 | end. 66 | 67 | route(_, _) -> []. 68 | 69 | active_for(X) -> 70 | case federate(X) of 71 | true -> noroute; 72 | false -> none 73 | end. 74 | 75 | %%---------------------------------------------------------------------------- 76 | 77 | %% Don't federate default exchange, we can't bind to it 78 | federate(#exchange{name = #resource{name = <<"">>}}) -> 79 | false; 80 | 81 | %% Don't federate any of our intermediate exchanges. Note that we use 82 | %% internal=true since older brokers may not declare 83 | %% x-federation-upstream on us. Also other internal exchanges should 84 | %% probably not be federated. 85 | federate(#exchange{internal = true}) -> 86 | false; 87 | 88 | federate(X) -> 89 | rabbit_federation_upstream:federate(X). 90 | 91 | maybe_start(X = #exchange{name = XName})-> 92 | case federate(X) of 93 | true -> ok = rabbit_federation_db:prune_scratch( 94 | XName, rabbit_federation_upstream:for(X)), 95 | ok = rabbit_federation_exchange_link_sup_sup:start_child(X), 96 | ok; 97 | false -> ok 98 | end. 99 | 100 | maybe_stop(X = #exchange{name = XName}) -> 101 | case federate(X) of 102 | true -> ok = rabbit_federation_exchange_link_sup_sup:stop_child(X), 103 | rabbit_federation_status:remove_exchange_or_queue(XName); 104 | false -> ok 105 | end. 106 | -------------------------------------------------------------------------------- /src/rabbit_federation_exchange_link_sup_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_federation_exchange_link_sup_sup). 9 | 10 | -behaviour(mirrored_supervisor). 11 | 12 | -include_lib("rabbit_common/include/rabbit.hrl"). 13 | -define(SUPERVISOR, ?MODULE). 14 | 15 | %% Supervises the upstream links for all exchanges (but not queues). We need 16 | %% different handling here since exchanges want a mirrored sup. 17 | 18 | -export([start_link/0, start_child/1, adjust/1, stop_child/1]). 19 | -export([init/1]). 20 | 21 | %%---------------------------------------------------------------------------- 22 | 23 | start_link() -> 24 | mirrored_supervisor:start_link({local, ?SUPERVISOR}, ?SUPERVISOR, 25 | fun rabbit_misc:execute_mnesia_transaction/1, 26 | ?MODULE, []). 27 | 28 | %% Note that the next supervisor down, rabbit_federation_link_sup, is common 29 | %% between exchanges and queues. 30 | start_child(X) -> 31 | case mirrored_supervisor:start_child( 32 | ?SUPERVISOR, 33 | {id(X), {rabbit_federation_link_sup, start_link, [X]}, 34 | transient, ?SUPERVISOR_WAIT, supervisor, 35 | [rabbit_federation_link_sup]}) of 36 | {ok, _Pid} -> ok; 37 | {error, {already_started, _Pid}} -> 38 | #exchange{name = ExchangeName} = X, 39 | rabbit_log_federation:debug("Federation link for exchange ~p was already started", 40 | [rabbit_misc:rs(ExchangeName)]), 41 | ok; 42 | %% A link returned {stop, gone}, the link_sup shut down, that's OK. 43 | {error, {shutdown, _}} -> ok 44 | end. 45 | 46 | adjust({clear_upstream, VHost, UpstreamName}) -> 47 | [rabbit_federation_link_sup:adjust(Pid, X, {clear_upstream, UpstreamName}) || 48 | {#exchange{name = Name} = X, Pid, _, _} <- mirrored_supervisor:which_children(?SUPERVISOR), 49 | Name#resource.virtual_host == VHost], 50 | ok; 51 | adjust(Reason) -> 52 | [rabbit_federation_link_sup:adjust(Pid, X, Reason) || 53 | {X, Pid, _, _} <- mirrored_supervisor:which_children(?SUPERVISOR)], 54 | ok. 55 | 56 | stop_child(X) -> 57 | case mirrored_supervisor:terminate_child(?SUPERVISOR, id(X)) of 58 | ok -> ok; 59 | {error, Err} -> 60 | #exchange{name = ExchangeName} = X, 61 | rabbit_log_federation:warning( 62 | "Attempt to stop a federation link for exchange ~p failed: ~p", 63 | [rabbit_misc:rs(ExchangeName), Err]), 64 | ok 65 | end, 66 | ok = mirrored_supervisor:delete_child(?SUPERVISOR, id(X)). 67 | 68 | %%---------------------------------------------------------------------------- 69 | 70 | init([]) -> 71 | {ok, {{one_for_one, 1200, 60}, []}}. 72 | 73 | %% See comment in rabbit_federation_queue_link_sup_sup:id/1 74 | id(X = #exchange{policy = Policy}) -> X1 = rabbit_exchange:immutable(X), 75 | X1#exchange{policy = Policy}. 76 | -------------------------------------------------------------------------------- /src/rabbit_federation_link_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_federation_link_sup). 9 | 10 | -behaviour(supervisor2). 11 | 12 | -include_lib("rabbit_common/include/rabbit.hrl"). 13 | -include_lib("rabbit/include/amqqueue.hrl"). 14 | -include("rabbit_federation.hrl"). 15 | 16 | %% Supervises the upstream links for an exchange or queue. 17 | 18 | -export([start_link/1, adjust/3, restart/2]). 19 | -export([init/1]). 20 | 21 | start_link(XorQ) -> 22 | supervisor2:start_link(?MODULE, XorQ). 23 | 24 | adjust(Sup, XorQ, everything) -> 25 | [stop(Sup, Upstream, XorQ) || 26 | {Upstream, _, _, _} <- supervisor2:which_children(Sup)], 27 | [{ok, _Pid} = supervisor2:start_child(Sup, Spec) || Spec <- specs(XorQ)]; 28 | 29 | adjust(Sup, XorQ, {upstream, UpstreamName}) -> 30 | OldUpstreams0 = children(Sup, UpstreamName), 31 | NewUpstreams0 = rabbit_federation_upstream:for(XorQ, UpstreamName), 32 | %% If any haven't changed, don't restart them. The broker will 33 | %% avoid telling us about connections that have not changed 34 | %% syntactically, but even if one has, this XorQ may not have that 35 | %% connection in an upstream, so we still need to check here. 36 | {OldUpstreams, NewUpstreams} = 37 | lists:foldl( 38 | fun (OldU, {OldUs, NewUs}) -> 39 | case lists:member(OldU, NewUs) of 40 | true -> {OldUs -- [OldU], NewUs -- [OldU]}; 41 | false -> {OldUs, NewUs} 42 | end 43 | end, {OldUpstreams0, NewUpstreams0}, OldUpstreams0), 44 | [stop(Sup, OldUpstream, XorQ) || OldUpstream <- OldUpstreams], 45 | [start(Sup, NewUpstream, XorQ) || NewUpstream <- NewUpstreams]; 46 | 47 | adjust(Sup, XorQ, {clear_upstream, UpstreamName}) -> 48 | ok = rabbit_federation_db:prune_scratch( 49 | name(XorQ), rabbit_federation_upstream:for(XorQ)), 50 | [stop(Sup, Upstream, XorQ) || Upstream <- children(Sup, UpstreamName)]; 51 | 52 | adjust(Sup, X = #exchange{name = XName}, {upstream_set, _Set}) -> 53 | adjust(Sup, X, everything), 54 | case rabbit_federation_upstream:federate(X) of 55 | false -> ok; 56 | true -> ok = rabbit_federation_db:prune_scratch( 57 | XName, rabbit_federation_upstream:for(X)) 58 | end; 59 | adjust(Sup, Q, {upstream_set, _}) when ?is_amqqueue(Q) -> 60 | adjust(Sup, Q, everything); 61 | adjust(Sup, XorQ, {clear_upstream_set, _}) -> 62 | adjust(Sup, XorQ, everything). 63 | 64 | restart(Sup, Upstream) -> 65 | ok = supervisor2:terminate_child(Sup, Upstream), 66 | {ok, _Pid} = supervisor2:restart_child(Sup, Upstream), 67 | ok. 68 | 69 | start(Sup, Upstream, XorQ) -> 70 | {ok, _Pid} = supervisor2:start_child(Sup, spec(rabbit_federation_util:obfuscate_upstream(Upstream), XorQ)), 71 | ok. 72 | 73 | stop(Sup, Upstream, XorQ) -> 74 | ok = supervisor2:terminate_child(Sup, Upstream), 75 | ok = supervisor2:delete_child(Sup, Upstream), 76 | %% While the link will report its own removal, that only works if 77 | %% the link was actually up. If the link was broken and failing to 78 | %% come up, the possibility exists that there *is* no link 79 | %% process, but we still have a report in the status table. So 80 | %% remove it here too. 81 | rabbit_federation_status:remove(Upstream, name(XorQ)). 82 | 83 | children(Sup, UpstreamName) -> 84 | rabbit_federation_util:find_upstreams( 85 | UpstreamName, [U || {U, _, _, _} <- supervisor2:which_children(Sup)]). 86 | 87 | %%---------------------------------------------------------------------------- 88 | 89 | init(XorQ) -> 90 | %% 1, ?MAX_WAIT so that we always give up after one fast retry and get 91 | %% into the reconnect delay. 92 | {ok, {{one_for_one, 1, ?MAX_WAIT}, specs(XorQ)}}. 93 | 94 | specs(XorQ) -> 95 | [spec(rabbit_federation_util:obfuscate_upstream(Upstream), XorQ) 96 | || Upstream <- rabbit_federation_upstream:for(XorQ)]. 97 | 98 | spec(U = #upstream{reconnect_delay = Delay}, #exchange{name = XName}) -> 99 | {U, {rabbit_federation_exchange_link, start_link, [{U, XName}]}, 100 | {permanent, Delay}, ?WORKER_WAIT, worker, 101 | [rabbit_federation_exchange_link]}; 102 | 103 | spec(Upstream = #upstream{reconnect_delay = Delay}, Q) when ?is_amqqueue(Q) -> 104 | {Upstream, {rabbit_federation_queue_link, start_link, [{Upstream, Q}]}, 105 | {permanent, Delay}, ?WORKER_WAIT, worker, 106 | [rabbit_federation_queue_link]}. 107 | 108 | name(#exchange{name = XName}) -> XName; 109 | name(Q) when ?is_amqqueue(Q) -> amqqueue:get_name(Q). 110 | -------------------------------------------------------------------------------- /src/rabbit_federation_link_util.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_federation_link_util). 9 | 10 | -include_lib("rabbit/include/amqqueue.hrl"). 11 | -include_lib("amqp_client/include/amqp_client.hrl"). 12 | -include("rabbit_federation.hrl"). 13 | 14 | %% real 15 | -export([start_conn_ch/5, disposable_channel_call/2, disposable_channel_call/3, 16 | disposable_connection_call/3, ensure_connection_closed/1, 17 | log_terminate/4, unacked_new/0, ack/3, nack/3, forward/9, 18 | handle_downstream_down/3, handle_upstream_down/3, 19 | get_connection_name/2, log_debug/3, log_info/3, log_warning/3, 20 | log_error/3]). 21 | 22 | %% temp 23 | -export([connection_error/6]). 24 | 25 | -import(rabbit_misc, [pget/2]). 26 | 27 | -define(MAX_CONNECTION_CLOSE_TIMEOUT, 10000). 28 | 29 | %%---------------------------------------------------------------------------- 30 | 31 | start_conn_ch(Fun, OUpstream, OUParams, 32 | XorQName = #resource{virtual_host = DownVHost}, State) -> 33 | 34 | Upstream = rabbit_federation_util:deobfuscate_upstream(OUpstream), 35 | UParams = rabbit_federation_util:deobfuscate_upstream_params(OUParams), 36 | 37 | ConnName = get_connection_name(Upstream, UParams), 38 | case open_monitor(#amqp_params_direct{virtual_host = DownVHost}, ConnName) of 39 | {ok, DConn, DCh} -> 40 | case Upstream#upstream.ack_mode of 41 | 'on-confirm' -> 42 | #'confirm.select_ok'{} = 43 | amqp_channel:call(DCh, #'confirm.select'{}), 44 | amqp_channel:register_confirm_handler(DCh, self()); 45 | _ -> 46 | ok 47 | end, 48 | case open_monitor(UParams#upstream_params.params, ConnName) of 49 | {ok, Conn, Ch} -> 50 | %% Don't trap exits until we have established 51 | %% connections so that if we try to delete 52 | %% federation upstreams while waiting for a 53 | %% connection to be established then we don't 54 | %% block 55 | process_flag(trap_exit, true), 56 | try 57 | R = Fun(Conn, Ch, DConn, DCh), 58 | log_info( 59 | XorQName, "connected to ~s~n", 60 | [rabbit_federation_upstream:params_to_string( 61 | UParams)]), 62 | Name = pget(name, amqp_connection:info(DConn, [name])), 63 | rabbit_federation_status:report( 64 | OUpstream, OUParams, XorQName, {running, Name}), 65 | R 66 | catch exit:E -> 67 | %% terminate/2 will not get this, as we 68 | %% have not put them in our state yet 69 | ensure_connection_closed(DConn), 70 | ensure_connection_closed(Conn), 71 | connection_error(remote_start, E, 72 | OUpstream, OUParams, XorQName, State) 73 | end; 74 | E -> 75 | ensure_connection_closed(DConn), 76 | connection_error(remote_start, E, 77 | OUpstream, OUParams, XorQName, State) 78 | end; 79 | E -> 80 | connection_error(local_start, E, 81 | OUpstream, OUParams, XorQName, State) 82 | end. 83 | 84 | get_connection_name(#upstream{name = UpstreamName}, 85 | #upstream_params{x_or_q = Resource}) when is_record(Resource, exchange)-> 86 | Policy = Resource#exchange.policy, 87 | PolicyName = proplists:get_value(name, Policy), 88 | connection_name(UpstreamName, PolicyName); 89 | 90 | get_connection_name(#upstream{name = UpstreamName}, 91 | #upstream_params{x_or_q = Resource}) when ?is_amqqueue(Resource) -> 92 | Policy = amqqueue:get_policy(Resource), 93 | PolicyName = proplists:get_value(name, Policy), 94 | connection_name(UpstreamName, PolicyName); 95 | 96 | get_connection_name(_, _) -> 97 | connection_name(undefined, undefined). 98 | 99 | connection_name(Upstream, Policy) when is_binary(Upstream), is_binary(Policy) -> 100 | <<<<"Federation link (upstream: ">>/binary, Upstream/binary, <<", policy: ">>/binary, Policy/binary, <<")">>/binary>>; 101 | connection_name(_, _) -> 102 | <<"Federation link">>. 103 | 104 | open_monitor(Params, Name) -> 105 | case open(Params, Name) of 106 | {ok, Conn, Ch} -> erlang:monitor(process, Ch), 107 | {ok, Conn, Ch}; 108 | E -> E 109 | end. 110 | 111 | open(Params, Name) -> 112 | try 113 | amqp_connection:start(Params, Name) 114 | of 115 | {ok, Conn} -> 116 | try 117 | amqp_connection:open_channel(Conn) 118 | of 119 | {ok, Ch} -> {ok, Conn, Ch}; 120 | E -> ensure_connection_closed(Conn), 121 | E 122 | catch 123 | _:E -> 124 | ensure_connection_closed(Conn), 125 | E 126 | end; 127 | E -> E 128 | catch 129 | _:E -> E 130 | end. 131 | 132 | ensure_channel_closed(Ch) -> catch amqp_channel:close(Ch). 133 | 134 | ensure_connection_closed(Conn) -> 135 | catch amqp_connection:close(Conn, ?MAX_CONNECTION_CLOSE_TIMEOUT). 136 | 137 | connection_error(remote_start, {{shutdown, {server_initiated_close, Code, Message}}, _} = E, 138 | Upstream, UParams, XorQName, State) -> 139 | rabbit_federation_status:report( 140 | Upstream, UParams, XorQName, clean_reason(E)), 141 | log_warning(XorQName, 142 | "did not connect to ~s. Server has closed the connection due to an error, code: ~p, " 143 | "message: ~s", 144 | [rabbit_federation_upstream:params_to_string(UParams), 145 | Code, Message]), 146 | {stop, {shutdown, restart}, State}; 147 | 148 | connection_error(remote_start, E, Upstream, UParams, XorQName, State) -> 149 | rabbit_federation_status:report( 150 | Upstream, UParams, XorQName, clean_reason(E)), 151 | log_warning(XorQName, "did not connect to ~s. Reason: ~p", 152 | [rabbit_federation_upstream:params_to_string(UParams), 153 | E]), 154 | {stop, {shutdown, restart}, State}; 155 | 156 | connection_error(remote, E, Upstream, UParams, XorQName, State) -> 157 | rabbit_federation_status:report( 158 | Upstream, UParams, XorQName, clean_reason(E)), 159 | log_info(XorQName, "disconnected from ~s~n~p", 160 | [rabbit_federation_upstream:params_to_string(UParams), E]), 161 | {stop, {shutdown, restart}, State}; 162 | 163 | connection_error(command_channel, E, Upstream, UParams, XorQName, State) -> 164 | rabbit_federation_status:report( 165 | Upstream, UParams, XorQName, clean_reason(E)), 166 | log_info(XorQName, "failed to open a command channel for upstream ~s~n~p", 167 | [rabbit_federation_upstream:params_to_string(UParams), E]), 168 | {stop, {shutdown, restart}, State}; 169 | 170 | connection_error(local, basic_cancel, Upstream, UParams, XorQName, State) -> 171 | rabbit_federation_status:report( 172 | Upstream, UParams, XorQName, {error, basic_cancel}), 173 | log_info(XorQName, "received a 'basic.cancel'", []), 174 | {stop, {shutdown, restart}, State}; 175 | 176 | connection_error(local_start, E, Upstream, UParams, XorQName, State) -> 177 | rabbit_federation_status:report( 178 | Upstream, UParams, XorQName, clean_reason(E)), 179 | log_warning(XorQName, "did not connect locally~n~p", [E]), 180 | {stop, {shutdown, restart}, State}. 181 | 182 | %% If we terminate due to a gen_server call exploding (almost 183 | %% certainly due to an amqp_channel:call() exploding) then we do not 184 | %% want to report the gen_server call in our status. 185 | clean_reason({E = {shutdown, _}, _}) -> E; 186 | clean_reason(E) -> E. 187 | 188 | %% local / disconnected never gets invoked, see handle_info({'DOWN', ... 189 | 190 | %%---------------------------------------------------------------------------- 191 | 192 | unacked_new() -> gb_trees:empty(). 193 | 194 | ack(#'basic.ack'{delivery_tag = Seq, 195 | multiple = Multiple}, Ch, Unack) -> 196 | amqp_channel:cast(Ch, #'basic.ack'{delivery_tag = gb_trees:get(Seq, Unack), 197 | multiple = Multiple}), 198 | remove_delivery_tags(Seq, Multiple, Unack). 199 | 200 | 201 | %% Note: at time of writing the broker will never send requeue=false. And it's 202 | %% hard to imagine why it would. But we may as well handle it. 203 | nack(#'basic.nack'{delivery_tag = Seq, 204 | multiple = Multiple, 205 | requeue = Requeue}, Ch, Unack) -> 206 | amqp_channel:cast(Ch, #'basic.nack'{delivery_tag = gb_trees:get(Seq, Unack), 207 | multiple = Multiple, 208 | requeue = Requeue}), 209 | remove_delivery_tags(Seq, Multiple, Unack). 210 | 211 | remove_delivery_tags(Seq, false, Unacked) -> 212 | gb_trees:delete(Seq, Unacked); 213 | remove_delivery_tags(Seq, true, Unacked) -> 214 | case gb_trees:is_empty(Unacked) of 215 | true -> Unacked; 216 | false -> {Smallest, _Val, Unacked1} = gb_trees:take_smallest(Unacked), 217 | case Smallest > Seq of 218 | true -> Unacked; 219 | false -> remove_delivery_tags(Seq, true, Unacked1) 220 | end 221 | end. 222 | 223 | forward(#upstream{ack_mode = AckMode, 224 | trust_user_id = Trust}, 225 | #'basic.deliver'{delivery_tag = DT}, 226 | Ch, DCh, PublishMethod, HeadersFun, ForwardFun, Msg, Unacked) -> 227 | Headers = extract_headers(Msg), 228 | case ForwardFun(Headers) of 229 | true -> Msg1 = maybe_clear_user_id( 230 | Trust, update_headers(HeadersFun(Headers), Msg)), 231 | Seq = case AckMode of 232 | 'on-confirm' -> amqp_channel:next_publish_seqno(DCh); 233 | _ -> ignore 234 | end, 235 | amqp_channel:cast(DCh, PublishMethod, Msg1), 236 | case AckMode of 237 | 'on-confirm' -> 238 | gb_trees:insert(Seq, DT, Unacked); 239 | 'on-publish' -> 240 | amqp_channel:cast(Ch, #'basic.ack'{delivery_tag = DT}), 241 | Unacked; 242 | 'no-ack' -> 243 | Unacked 244 | end; 245 | false -> amqp_channel:cast(Ch, #'basic.ack'{delivery_tag = DT}), 246 | %% Drop it, but acknowledge it! 247 | Unacked 248 | end. 249 | 250 | maybe_clear_user_id(false, Msg = #amqp_msg{props = Props}) -> 251 | Msg#amqp_msg{props = Props#'P_basic'{user_id = undefined}}; 252 | maybe_clear_user_id(true, Msg) -> 253 | Msg. 254 | 255 | extract_headers(#amqp_msg{props = #'P_basic'{headers = Headers}}) -> 256 | Headers. 257 | 258 | update_headers(Headers, Msg = #amqp_msg{props = Props}) -> 259 | Msg#amqp_msg{props = Props#'P_basic'{headers = Headers}}. 260 | 261 | %%---------------------------------------------------------------------------- 262 | 263 | %% If the downstream channel shuts down cleanly, we can just ignore it 264 | %% - we're the same node, we're presumably about to go down too. 265 | handle_downstream_down(shutdown, _Args, State) -> 266 | {noreply, State}; 267 | 268 | handle_downstream_down(Reason, _Args, State) -> 269 | {stop, {downstream_channel_down, Reason}, State}. 270 | 271 | %% If the upstream channel goes down for an intelligible reason, just 272 | %% log it and die quietly. 273 | handle_upstream_down({shutdown, Reason}, {Upstream, UParams, XName}, State) -> 274 | rabbit_federation_link_util:connection_error( 275 | remote, {upstream_channel_down, Reason}, Upstream, UParams, XName, State); 276 | 277 | handle_upstream_down(Reason, _Args, State) -> 278 | {stop, {upstream_channel_down, Reason}, State}. 279 | 280 | %%---------------------------------------------------------------------------- 281 | 282 | log_terminate(gone, _Upstream, _UParams, _XorQName) -> 283 | %% the link cannot start, this has been logged already 284 | ok; 285 | log_terminate({shutdown, restart}, _Upstream, _UParams, _XorQName) -> 286 | %% We've already logged this before munging the reason 287 | ok; 288 | log_terminate(shutdown, Upstream, UParams, XorQName) -> 289 | %% The supervisor is shutting us down; we are probably restarting 290 | %% the link because configuration has changed. So try to shut down 291 | %% nicely so that we do not cause unacked messages to be 292 | %% redelivered. 293 | log_info(XorQName, "disconnecting from ~s~n", 294 | [rabbit_federation_upstream:params_to_string(UParams)]), 295 | rabbit_federation_status:remove(Upstream, XorQName); 296 | 297 | log_terminate(Reason, Upstream, UParams, XorQName) -> 298 | %% Unexpected death. sasl will log it, but we should update 299 | %% rabbit_federation_status. 300 | rabbit_federation_status:report( 301 | Upstream, UParams, XorQName, clean_reason(Reason)). 302 | 303 | log_debug(XorQName, Fmt, Args) -> log(debug, XorQName, Fmt, Args). 304 | log_info(XorQName, Fmt, Args) -> log(info, XorQName, Fmt, Args). 305 | log_warning(XorQName, Fmt, Args) -> log(warning, XorQName, Fmt, Args). 306 | log_error(XorQName, Fmt, Args) -> log(error, XorQName, Fmt, Args). 307 | 308 | log(Level, XorQName, Fmt0, Args0) -> 309 | Fmt = "Federation ~s " ++ Fmt0, 310 | Args = [rabbit_misc:rs(XorQName) | Args0], 311 | case Level of 312 | debug -> rabbit_log_federation:debug(Fmt, Args); 313 | info -> rabbit_log_federation:info(Fmt, Args); 314 | warning -> rabbit_log_federation:warning(Fmt, Args); 315 | error -> rabbit_log_federation:error(Fmt, Args) 316 | end. 317 | 318 | %%---------------------------------------------------------------------------- 319 | 320 | disposable_channel_call(Conn, Method) -> 321 | disposable_channel_call(Conn, Method, fun(_, _) -> ok end). 322 | 323 | disposable_channel_call(Conn, Method, ErrFun) -> 324 | try 325 | {ok, Ch} = amqp_connection:open_channel(Conn), 326 | try 327 | amqp_channel:call(Ch, Method) 328 | catch exit:{{shutdown, {server_initiated_close, Code, Message}}, _} -> 329 | ErrFun(Code, Message) 330 | after 331 | ensure_channel_closed(Ch) 332 | end 333 | catch 334 | Exception:Reason -> 335 | rabbit_log_federation:error("Federation link could not create a disposable (one-off) channel due to an error ~p: ~p~n", [Exception, Reason]) 336 | end. 337 | 338 | disposable_connection_call(Params, Method, ErrFun) -> 339 | try 340 | rabbit_log_federation:debug("Disposable connection parameters: ~p", [Params]), 341 | case open(Params, <<"Disposable exchange federation link connection">>) of 342 | {ok, Conn, Ch} -> 343 | try 344 | amqp_channel:call(Ch, Method) 345 | catch exit:{{shutdown, {connection_closing, {server_initiated_close, Code, Message}}}, _} -> 346 | ErrFun(Code, Message); 347 | exit:{{shutdown, {server_initiated_close, Code, Message}}, _} -> 348 | ErrFun(Code, Message) 349 | after 350 | ensure_connection_closed(Conn) 351 | end; 352 | {error, {auth_failure, Message}} -> 353 | rabbit_log_federation:error("Federation link could not open a disposable (one-off) connection " 354 | "due to an authentication failure: ~s~n", [Message]); 355 | Error -> 356 | rabbit_log_federation:error("Federation link could not open a disposable (one-off) connection, " 357 | "reason: ~p~n", [Error]), 358 | Error 359 | end 360 | catch 361 | Exception:Reason -> 362 | rabbit_log_federation:error("Federation link could not create a disposable (one-off) connection " 363 | "due to an error ~p: ~p~n", [Exception, Reason]) 364 | end. 365 | -------------------------------------------------------------------------------- /src/rabbit_federation_parameters.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_federation_parameters). 9 | -behaviour(rabbit_runtime_parameter). 10 | -behaviour(rabbit_policy_validator). 11 | 12 | -include_lib("rabbit_common/include/rabbit.hrl"). 13 | 14 | -export([validate/5, notify/5, notify_clear/4]). 15 | -export([register/0, unregister/0, validate_policy/1, adjust/1]). 16 | 17 | -define(RUNTIME_PARAMETERS, 18 | [{runtime_parameter, <<"federation">>}, 19 | {runtime_parameter, <<"federation-upstream">>}, 20 | {runtime_parameter, <<"federation-upstream-set">>}, 21 | {policy_validator, <<"federation-upstream">>}, 22 | {policy_validator, <<"federation-upstream-pattern">>}, 23 | {policy_validator, <<"federation-upstream-set">>}]). 24 | 25 | -rabbit_boot_step({?MODULE, 26 | [{description, "federation parameters"}, 27 | {mfa, {rabbit_federation_parameters, register, []}}, 28 | {requires, rabbit_registry}, 29 | {cleanup, {rabbit_federation_parameters, unregister, []}}, 30 | {enables, recovery}]}). 31 | 32 | register() -> 33 | [rabbit_registry:register(Class, Name, ?MODULE) || 34 | {Class, Name} <- ?RUNTIME_PARAMETERS], 35 | ok. 36 | 37 | unregister() -> 38 | [rabbit_registry:unregister(Class, Name) || 39 | {Class, Name} <- ?RUNTIME_PARAMETERS], 40 | ok. 41 | 42 | validate(_VHost, <<"federation-upstream-set">>, Name, Term0, _User) -> 43 | Term = [rabbit_data_coercion:to_proplist(Upstream) || Upstream <- Term0], 44 | [rabbit_parameter_validation:proplist( 45 | Name, 46 | [{<<"upstream">>, fun rabbit_parameter_validation:binary/2, mandatory} | 47 | shared_validation()], Upstream) 48 | || Upstream <- Term]; 49 | 50 | validate(_VHost, <<"federation-upstream">>, Name, Term0, _User) -> 51 | Term = rabbit_data_coercion:to_proplist(Term0), 52 | rabbit_parameter_validation:proplist( 53 | Name, [{<<"uri">>, fun validate_uri/2, mandatory} | 54 | shared_validation()], Term); 55 | 56 | validate(_VHost, _Component, Name, _Term, _User) -> 57 | {error, "name not recognised: ~p", [Name]}. 58 | 59 | notify(_VHost, <<"federation-upstream-set">>, Name, _Term, _Username) -> 60 | adjust({upstream_set, Name}); 61 | 62 | notify(_VHost, <<"federation-upstream">>, Name, _Term, _Username) -> 63 | adjust({upstream, Name}). 64 | 65 | notify_clear(_VHost, <<"federation-upstream-set">>, Name, _Username) -> 66 | adjust({clear_upstream_set, Name}); 67 | 68 | notify_clear(VHost, <<"federation-upstream">>, Name, _Username) -> 69 | rabbit_federation_exchange_link_sup_sup:adjust({clear_upstream, VHost, Name}), 70 | rabbit_federation_queue_link_sup_sup:adjust({clear_upstream, VHost, Name}). 71 | 72 | adjust(Thing) -> 73 | rabbit_federation_exchange_link_sup_sup:adjust(Thing), 74 | rabbit_federation_queue_link_sup_sup:adjust(Thing). 75 | 76 | %%---------------------------------------------------------------------------- 77 | 78 | shared_validation() -> 79 | [{<<"exchange">>, fun rabbit_parameter_validation:binary/2, optional}, 80 | {<<"queue">>, fun rabbit_parameter_validation:binary/2, optional}, 81 | {<<"consumer-tag">>, fun rabbit_parameter_validation:binary/2, optional}, 82 | {<<"prefetch-count">>, fun rabbit_parameter_validation:number/2, optional}, 83 | {<<"reconnect-delay">>,fun rabbit_parameter_validation:number/2, optional}, 84 | {<<"max-hops">>, fun rabbit_parameter_validation:number/2, optional}, 85 | {<<"expires">>, fun rabbit_parameter_validation:number/2, optional}, 86 | {<<"message-ttl">>, fun rabbit_parameter_validation:number/2, optional}, 87 | {<<"trust-user-id">>, fun rabbit_parameter_validation:boolean/2, optional}, 88 | {<<"ack-mode">>, rabbit_parameter_validation:enum( 89 | ['no-ack', 'on-publish', 'on-confirm']), optional}, 90 | {<<"resource-cleanup-mode">>, rabbit_parameter_validation:enum(['default', 'never']), optional}, 91 | {<<"ha-policy">>, fun rabbit_parameter_validation:binary/2, optional}, 92 | {<<"bind-nowait">>, fun rabbit_parameter_validation:boolean/2, optional}]. 93 | 94 | validate_uri(Name, Term) when is_binary(Term) -> 95 | case rabbit_parameter_validation:binary(Name, Term) of 96 | ok -> case amqp_uri:parse(binary_to_list(Term)) of 97 | {ok, _} -> ok; 98 | {error, E} -> {error, "\"~s\" not a valid URI: ~p", [Term, E]} 99 | end; 100 | E -> E 101 | end; 102 | validate_uri(Name, Term) -> 103 | case rabbit_parameter_validation:list(Name, Term) of 104 | ok -> case [V || U <- Term, 105 | V <- [validate_uri(Name, U)], 106 | element(1, V) =:= error] of 107 | [] -> ok; 108 | [E | _] -> E 109 | end; 110 | E -> E 111 | end. 112 | 113 | %%---------------------------------------------------------------------------- 114 | 115 | validate_policy([{<<"federation-upstream-set">>, Value}]) 116 | when is_binary(Value) -> 117 | ok; 118 | validate_policy([{<<"federation-upstream-set">>, Value}]) -> 119 | {error, "~p is not a valid federation upstream set name", [Value]}; 120 | 121 | validate_policy([{<<"federation-upstream-pattern">>, Value}]) 122 | when is_binary(Value) -> 123 | case re:compile(Value) of 124 | {ok, _} -> ok; 125 | {error, Reason} -> {error, "could not compile pattern ~s to a regular expression. " 126 | "Error: ~p", [Value, Reason]} 127 | end; 128 | validate_policy([{<<"federation-upstream-pattern">>, Value}]) -> 129 | {error, "~p is not a valid federation upstream pattern name", [Value]}; 130 | 131 | validate_policy([{<<"federation-upstream">>, Value}]) 132 | when is_binary(Value) -> 133 | ok; 134 | validate_policy([{<<"federation-upstream">>, Value}]) -> 135 | {error, "~p is not a valid federation upstream name", [Value]}; 136 | 137 | validate_policy(L) when length(L) >= 2 -> 138 | {error, "cannot specify federation-upstream, federation-upstream-set " 139 | "or federation-upstream-pattern together", []}. 140 | -------------------------------------------------------------------------------- /src/rabbit_federation_queue.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_federation_queue). 9 | 10 | -rabbit_boot_step({?MODULE, 11 | [{description, "federation queue decorator"}, 12 | {mfa, {rabbit_queue_decorator, register, 13 | [<<"federation">>, ?MODULE]}}, 14 | {requires, rabbit_registry}, 15 | {cleanup, {rabbit_queue_decorator, unregister, 16 | [<<"federation">>]}}, 17 | {enables, recovery}]}). 18 | 19 | -include_lib("rabbit/include/amqqueue.hrl"). 20 | -include_lib("amqp_client/include/amqp_client.hrl"). 21 | -include("rabbit_federation.hrl"). 22 | 23 | -behaviour(rabbit_queue_decorator). 24 | 25 | -export([startup/1, shutdown/1, policy_changed/2, active_for/1, 26 | consumer_state_changed/3]). 27 | -export([policy_changed_local/2]). 28 | 29 | %%---------------------------------------------------------------------------- 30 | 31 | startup(Q) -> 32 | case active_for(Q) of 33 | true -> rabbit_federation_queue_link_sup_sup:start_child(Q); 34 | false -> ok 35 | end, 36 | ok. 37 | 38 | shutdown(Q) when ?is_amqqueue(Q) -> 39 | QName = amqqueue:get_name(Q), 40 | case active_for(Q) of 41 | true -> rabbit_federation_queue_link_sup_sup:stop_child(Q), 42 | rabbit_federation_status:remove_exchange_or_queue(QName); 43 | false -> ok 44 | end, 45 | ok. 46 | 47 | policy_changed(Q1, Q2) when ?is_amqqueue(Q1) -> 48 | QName = amqqueue:get_name(Q1), 49 | case rabbit_amqqueue:lookup(QName) of 50 | {ok, Q0} when ?is_amqqueue(Q0) -> 51 | QPid = amqqueue:get_pid(Q0), 52 | rpc:call(node(QPid), rabbit_federation_queue, 53 | policy_changed_local, [Q1, Q2]); 54 | {error, not_found} -> 55 | ok 56 | end. 57 | 58 | policy_changed_local(Q1, Q2) -> 59 | shutdown(Q1), 60 | startup(Q2). 61 | 62 | active_for(Q) -> 63 | Args = amqqueue:get_arguments(Q), 64 | case rabbit_misc:table_lookup(Args, <<"x-internal-purpose">>) of 65 | {longstr, _} -> false; %% [0] 66 | _ -> rabbit_federation_upstream:federate(Q) 67 | end. 68 | %% [0] Currently the only "internal purpose" is federation, but I 69 | %% suspect if we introduce another one it will also be for something 70 | %% that doesn't want to be federated. 71 | 72 | %% We need to reconsider whether we need to run or pause every time 73 | %% the consumer state changes in the queue. But why can the state 74 | %% change? 75 | %% 76 | %% consumer blocked | We may have no more active consumers, and thus need to 77 | %% | pause 78 | %% | 79 | %% consumer unblocked | We don't care 80 | %% | 81 | %% queue empty | The queue has become empty therefore we need to run to 82 | %% | get more messages 83 | %% | 84 | %% basic consume | We don't care 85 | %% | 86 | %% basic cancel | We may have no more active consumers, and thus need to 87 | %% | pause 88 | %% | 89 | %% refresh | We asked for it (we have started a new link after 90 | %% | failover and need something to prod us into action 91 | %% | (or not)). 92 | %% 93 | %% In the cases where we don't care it's not prohibitively expensive 94 | %% for us to be here anyway, so never mind. 95 | %% 96 | %% Note that there is no "queue became non-empty" state change - that's 97 | %% because of the queue invariant. If the queue transitions from empty to 98 | %% non-empty then it must have no active consumers - in which case it stays 99 | %% the same from our POV. 100 | 101 | consumer_state_changed(Q, MaxActivePriority, IsEmpty) -> 102 | QName = amqqueue:get_name(Q), 103 | case IsEmpty andalso active_unfederated(MaxActivePriority) of 104 | true -> rabbit_federation_queue_link:run(QName); 105 | false -> rabbit_federation_queue_link:pause(QName) 106 | end, 107 | ok. 108 | 109 | active_unfederated(empty) -> false; 110 | active_unfederated(P) when P >= 0 -> true; 111 | active_unfederated(_P) -> false. 112 | -------------------------------------------------------------------------------- /src/rabbit_federation_queue_link.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_federation_queue_link). 9 | 10 | %% pg2 is deprecated in OTP 23. 11 | -compile(nowarn_deprecated_function). 12 | 13 | -include_lib("rabbit/include/amqqueue.hrl"). 14 | -include_lib("amqp_client/include/amqp_client.hrl"). 15 | -include("rabbit_federation.hrl"). 16 | 17 | -behaviour(gen_server2). 18 | 19 | -export([start_link/1, go/0, run/1, pause/1]). 20 | 21 | -export([init/1, handle_call/3, handle_cast/2, handle_info/2, 22 | terminate/2, code_change/3]). 23 | 24 | -import(rabbit_misc, [pget/2]). 25 | -import(rabbit_federation_util, [name/1, pgname/1]). 26 | 27 | -record(not_started, {queue, run, upstream, upstream_params}). 28 | -record(state, {queue, run, conn, ch, dconn, dch, upstream, upstream_params, 29 | unacked}). 30 | 31 | start_link(Args) -> 32 | gen_server2:start_link(?MODULE, Args, [{timeout, infinity}]). 33 | 34 | run(QName) -> cast(QName, run). 35 | pause(QName) -> cast(QName, pause). 36 | go() -> cast(go). 37 | 38 | %%---------------------------------------------------------------------------- 39 | %%call(QName, Msg) -> [gen_server2:call(Pid, Msg, infinity) || Pid <- q(QName)]. 40 | cast(Msg) -> [gen_server2:cast(Pid, Msg) || Pid <- all()]. 41 | cast(QName, Msg) -> [gen_server2:cast(Pid, Msg) || Pid <- q(QName)]. 42 | 43 | join(Name) -> 44 | pg2:create(pgname(Name)), 45 | ok = pg2:join(pgname(Name), self()). 46 | 47 | all() -> 48 | pg2:create(pgname(rabbit_federation_queues)), 49 | pg2:get_members(pgname(rabbit_federation_queues)). 50 | 51 | q(QName) -> 52 | pg2:create(pgname({rabbit_federation_queue, QName})), 53 | pg2:get_members(pgname({rabbit_federation_queue, QName})). 54 | 55 | federation_up() -> 56 | proplists:is_defined(rabbitmq_federation, 57 | application:which_applications(infinity)). 58 | 59 | %%---------------------------------------------------------------------------- 60 | 61 | init({Upstream, Queue}) when ?is_amqqueue(Queue) -> 62 | QName = amqqueue:get_name(Queue), 63 | case rabbit_amqqueue:lookup(QName) of 64 | {ok, Q} -> 65 | DeobfuscatedUpstream = rabbit_federation_util:deobfuscate_upstream(Upstream), 66 | DeobfuscatedUParams = rabbit_federation_upstream:to_params(DeobfuscatedUpstream, Queue), 67 | UParams = rabbit_federation_util:obfuscate_upstream_params(DeobfuscatedUParams), 68 | rabbit_federation_status:report(Upstream, UParams, QName, starting), 69 | join(rabbit_federation_queues), 70 | join({rabbit_federation_queue, QName}), 71 | gen_server2:cast(self(), maybe_go), 72 | rabbit_amqqueue:notify_decorators(Q), 73 | {ok, #not_started{queue = Queue, 74 | run = false, 75 | upstream = Upstream, 76 | upstream_params = UParams}}; 77 | {error, not_found} -> 78 | rabbit_federation_link_util:log_warning(QName, "not found, stopping link~n", []), 79 | {stop, gone} 80 | end. 81 | 82 | handle_call(Msg, _From, State) -> 83 | {stop, {unexpected_call, Msg}, State}. 84 | 85 | handle_cast(maybe_go, State) -> 86 | case federation_up() of 87 | true -> go(State); 88 | false -> {noreply, State} 89 | end; 90 | 91 | handle_cast(go, State = #not_started{}) -> 92 | go(State); 93 | 94 | handle_cast(go, State) -> 95 | {noreply, State}; 96 | 97 | handle_cast(run, State = #state{upstream = Upstream, 98 | upstream_params = UParams, 99 | ch = Ch, 100 | run = false}) -> 101 | consume(Ch, Upstream, UParams#upstream_params.x_or_q), 102 | {noreply, State#state{run = true}}; 103 | 104 | handle_cast(run, State = #not_started{}) -> 105 | {noreply, State#not_started{run = true}}; 106 | 107 | handle_cast(run, State) -> 108 | %% Already started 109 | {noreply, State}; 110 | 111 | handle_cast(pause, State = #state{run = false}) -> 112 | %% Already paused 113 | {noreply, State}; 114 | 115 | handle_cast(pause, State = #not_started{}) -> 116 | {noreply, State#not_started{run = false}}; 117 | 118 | handle_cast(pause, State = #state{ch = Ch, upstream = Upstream}) -> 119 | cancel(Ch, Upstream), 120 | {noreply, State#state{run = false}}; 121 | 122 | handle_cast(Msg, State) -> 123 | {stop, {unexpected_cast, Msg}, State}. 124 | 125 | handle_info(#'basic.consume_ok'{}, State) -> 126 | {noreply, State}; 127 | 128 | handle_info(#'basic.ack'{} = Ack, State = #state{ch = Ch, 129 | unacked = Unacked}) -> 130 | Unacked1 = rabbit_federation_link_util:ack(Ack, Ch, Unacked), 131 | {noreply, State#state{unacked = Unacked1}}; 132 | 133 | handle_info(#'basic.nack'{} = Nack, State = #state{ch = Ch, 134 | unacked = Unacked}) -> 135 | Unacked1 = rabbit_federation_link_util:nack(Nack, Ch, Unacked), 136 | {noreply, State#state{unacked = Unacked1}}; 137 | 138 | handle_info({#'basic.deliver'{redelivered = Redelivered, 139 | exchange = X, 140 | routing_key = K} = DeliverMethod, Msg}, 141 | State = #state{queue = Q, 142 | upstream = Upstream, 143 | upstream_params = UParams, 144 | ch = Ch, 145 | dch = DCh, 146 | unacked = Unacked}) when ?is_amqqueue(Q) -> 147 | QName = amqqueue:get_name(Q), 148 | PublishMethod = #'basic.publish'{exchange = <<"">>, 149 | routing_key = QName#resource.name}, 150 | HeadersFun = fun (H) -> update_headers(UParams, Redelivered, X, K, H) end, 151 | ForwardFun = fun (_H) -> true end, 152 | Unacked1 = rabbit_federation_link_util:forward( 153 | Upstream, DeliverMethod, Ch, DCh, PublishMethod, 154 | HeadersFun, ForwardFun, Msg, Unacked), 155 | %% TODO actually we could reject when 'stopped' 156 | {noreply, State#state{unacked = Unacked1}}; 157 | 158 | handle_info(#'basic.cancel'{}, 159 | State = #state{queue = Q, 160 | upstream = Upstream, 161 | upstream_params = UParams}) when ?is_amqqueue(Q) -> 162 | QName = amqqueue:get_name(Q), 163 | rabbit_federation_link_util:connection_error( 164 | local, basic_cancel, Upstream, UParams, QName, State); 165 | 166 | handle_info({'DOWN', _Ref, process, Pid, Reason}, 167 | State = #state{dch = DCh, 168 | ch = Ch, 169 | upstream = Upstream, 170 | upstream_params = UParams, 171 | queue = Q}) when ?is_amqqueue(Q) -> 172 | QName = amqqueue:get_name(Q), 173 | handle_down(Pid, Reason, Ch, DCh, {Upstream, UParams, QName}, State); 174 | 175 | handle_info(Msg, State) -> 176 | {stop, {unexpected_info, Msg}, State}. 177 | 178 | terminate(Reason, #not_started{upstream = Upstream, 179 | upstream_params = UParams, 180 | queue = Q}) when ?is_amqqueue(Q) -> 181 | QName = amqqueue:get_name(Q), 182 | rabbit_federation_link_util:log_terminate(Reason, Upstream, UParams, QName), 183 | ok; 184 | 185 | terminate(Reason, #state{dconn = DConn, 186 | conn = Conn, 187 | upstream = Upstream, 188 | upstream_params = UParams, 189 | queue = Q}) when ?is_amqqueue(Q) -> 190 | QName = amqqueue:get_name(Q), 191 | rabbit_federation_link_util:ensure_connection_closed(DConn), 192 | rabbit_federation_link_util:ensure_connection_closed(Conn), 193 | rabbit_federation_link_util:log_terminate(Reason, Upstream, UParams, QName), 194 | ok. 195 | 196 | code_change(_OldVsn, State, _Extra) -> 197 | {ok, State}. 198 | 199 | %%---------------------------------------------------------------------------- 200 | 201 | go(S0 = #not_started{run = Run, 202 | upstream = Upstream = #upstream{ 203 | prefetch_count = Prefetch}, 204 | upstream_params = UParams, 205 | queue = Queue}) when ?is_amqqueue(Queue) -> 206 | QName = amqqueue:get_name(Queue), 207 | #upstream_params{x_or_q = UQueue} = UParams, 208 | Durable = amqqueue:is_durable(UQueue), 209 | AutoDelete = amqqueue:is_auto_delete(UQueue), 210 | Args = amqqueue:get_arguments(UQueue), 211 | Unacked = rabbit_federation_link_util:unacked_new(), 212 | rabbit_federation_link_util:start_conn_ch( 213 | fun (Conn, Ch, DConn, DCh) -> 214 | check_upstream_suitable(Conn), 215 | amqp_channel:call(Ch, #'queue.declare'{queue = name(UQueue), 216 | durable = Durable, 217 | auto_delete = AutoDelete, 218 | arguments = Args}), 219 | case Upstream#upstream.ack_mode of 220 | 'no-ack' -> ok; 221 | _ -> amqp_channel:call( 222 | Ch, #'basic.qos'{prefetch_count = Prefetch}) 223 | end, 224 | amqp_selective_consumer:register_default_consumer(Ch, self()), 225 | case Run of 226 | true -> consume(Ch, Upstream, UQueue); 227 | false -> ok 228 | end, 229 | {noreply, #state{queue = Queue, 230 | run = Run, 231 | conn = Conn, 232 | ch = Ch, 233 | dconn = DConn, 234 | dch = DCh, 235 | upstream = Upstream, 236 | upstream_params = UParams, 237 | unacked = Unacked}} 238 | end, Upstream, UParams, QName, S0). 239 | 240 | check_upstream_suitable(Conn) -> 241 | Props = pget(server_properties, 242 | amqp_connection:info(Conn, [server_properties])), 243 | {table, Caps} = rabbit_misc:table_lookup(Props, <<"capabilities">>), 244 | case rabbit_misc:table_lookup(Caps, <<"consumer_priorities">>) of 245 | {bool, true} -> ok; 246 | _ -> exit({error, upstream_lacks_consumer_priorities}) 247 | end. 248 | 249 | update_headers(UParams, Redelivered, X, K, undefined) -> 250 | update_headers(UParams, Redelivered, X, K, []); 251 | 252 | update_headers(#upstream_params{table = Table}, Redelivered, X, K, Headers) -> 253 | {Headers1, Count} = 254 | case rabbit_misc:table_lookup(Headers, ?ROUTING_HEADER) of 255 | undefined -> 256 | %% We only want to record the original exchange and 257 | %% routing key the first time a message gets 258 | %% forwarded; after that it's known that they were 259 | %% <<>> and QueueName respectively. 260 | {init_x_original_source_headers(Headers, X, K), 0}; 261 | {array, Been} -> 262 | update_visit_count(Table, Been, Headers); 263 | %% this means the header comes from the client 264 | %% which re-published the message, most likely unintentionally. 265 | %% We can't assume much about the value, so we simply ignore it. 266 | _Other -> 267 | {init_x_original_source_headers(Headers, X, K), 0} 268 | end, 269 | rabbit_basic:prepend_table_header( 270 | ?ROUTING_HEADER, Table ++ [{<<"redelivered">>, bool, Redelivered}, 271 | {<<"visit-count">>, long, Count + 1}], 272 | swap_cc_header(Headers1)). 273 | 274 | init_x_original_source_headers(Headers, X, K) -> 275 | rabbit_misc:set_table_value( 276 | rabbit_misc:set_table_value( 277 | Headers, <<"x-original-exchange">>, longstr, X), 278 | <<"x-original-routing-key">>, longstr, K). 279 | 280 | update_visit_count(Table, Been, Headers) -> 281 | {Found, Been1} = lists:partition( 282 | fun(I) -> visit_match(I, Table) end, 283 | Been), 284 | C = case Found of 285 | [] -> 0; 286 | [{table, T}] -> case rabbit_misc:table_lookup( 287 | T, <<"visit-count">>) of 288 | {_, I} when is_number(I) -> I; 289 | _ -> 0 290 | end 291 | end, 292 | {rabbit_misc:set_table_value( 293 | Headers, ?ROUTING_HEADER, array, Been1), C}. 294 | 295 | swap_cc_header(Table) -> 296 | [{case K of 297 | <<"CC">> -> <<"x-original-cc">>; 298 | _ -> K 299 | end, T, V} || {K, T, V} <- Table]. 300 | 301 | visit_match({table, T}, Info) -> 302 | lists:all(fun (K) -> 303 | rabbit_misc:table_lookup(T, K) =:= 304 | rabbit_misc:table_lookup(Info, K) 305 | end, [<<"uri">>, <<"virtual_host">>, <<"queue">>]); 306 | visit_match(_ ,_) -> 307 | false. 308 | 309 | consumer_tag(#upstream{consumer_tag = ConsumerTag}) -> 310 | ConsumerTag. 311 | 312 | consume(Ch, Upstream, UQueue) -> 313 | ConsumerTag = consumer_tag(Upstream), 314 | NoAck = Upstream#upstream.ack_mode =:= 'no-ack', 315 | amqp_channel:cast( 316 | Ch, #'basic.consume'{queue = name(UQueue), 317 | no_ack = NoAck, 318 | nowait = true, 319 | consumer_tag = ConsumerTag, 320 | arguments = [{<<"x-priority">>, long, -1}]}). 321 | 322 | cancel(Ch, Upstream) -> 323 | ConsumerTag = consumer_tag(Upstream), 324 | amqp_channel:cast(Ch, #'basic.cancel'{nowait = true, 325 | consumer_tag = ConsumerTag}). 326 | 327 | handle_down(DCh, Reason, _Ch, DCh, Args, State) -> 328 | rabbit_federation_link_util:handle_downstream_down(Reason, Args, State); 329 | handle_down(Ch, Reason, Ch, _DCh, Args, State) -> 330 | rabbit_federation_link_util:handle_upstream_down(Reason, Args, State). 331 | -------------------------------------------------------------------------------- /src/rabbit_federation_queue_link_sup_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_federation_queue_link_sup_sup). 9 | 10 | -behaviour(mirrored_supervisor). 11 | 12 | -include_lib("rabbit_common/include/rabbit.hrl"). 13 | -include_lib("rabbit/include/amqqueue.hrl"). 14 | -define(SUPERVISOR, ?MODULE). 15 | 16 | %% Supervises the upstream links for all queues (but not exchanges). We need 17 | %% different handling here since queues do not want a mirrored sup. 18 | 19 | -export([start_link/0, start_child/1, adjust/1, stop_child/1]). 20 | -export([init/1]). 21 | 22 | %%---------------------------------------------------------------------------- 23 | 24 | start_link() -> 25 | mirrored_supervisor:start_link({local, ?SUPERVISOR}, ?SUPERVISOR, 26 | fun rabbit_misc:execute_mnesia_transaction/1, 27 | ?MODULE, []). 28 | 29 | %% Note that the next supervisor down, rabbit_federation_link_sup, is common 30 | %% between exchanges and queues. 31 | start_child(Q) -> 32 | case mirrored_supervisor:start_child( 33 | ?SUPERVISOR, 34 | {id(Q), {rabbit_federation_link_sup, start_link, [Q]}, 35 | transient, ?SUPERVISOR_WAIT, supervisor, 36 | [rabbit_federation_link_sup]}) of 37 | {ok, _Pid} -> ok; 38 | {error, {already_started, _Pid}} -> 39 | QueueName = amqqueue:get_name(Q), 40 | rabbit_log_federation:warning("Federation link for queue ~p was already started", 41 | [rabbit_misc:rs(QueueName)]), 42 | ok; 43 | %% A link returned {stop, gone}, the link_sup shut down, that's OK. 44 | {error, {shutdown, _}} -> ok 45 | end. 46 | 47 | 48 | adjust({clear_upstream, VHost, UpstreamName}) -> 49 | [rabbit_federation_link_sup:adjust(Pid, Q, {clear_upstream, UpstreamName}) || 50 | {Q, Pid, _, _} <- mirrored_supervisor:which_children(?SUPERVISOR), 51 | ?amqqueue_vhost_equals(Q, VHost)], 52 | ok; 53 | adjust(Reason) -> 54 | [rabbit_federation_link_sup:adjust(Pid, Q, Reason) || 55 | {Q, Pid, _, _} <- mirrored_supervisor:which_children(?SUPERVISOR)], 56 | ok. 57 | 58 | stop_child(Q) -> 59 | case mirrored_supervisor:terminate_child(?SUPERVISOR, id(Q)) of 60 | ok -> ok; 61 | {error, Err} -> 62 | QueueName = amqqueue:get_name(Q), 63 | rabbit_log_federation:warning( 64 | "Attempt to stop a federation link for queue ~p failed: ~p", 65 | [rabbit_misc:rs(QueueName), Err]), 66 | ok 67 | end, 68 | ok = mirrored_supervisor:delete_child(?SUPERVISOR, id(Q)). 69 | 70 | %%---------------------------------------------------------------------------- 71 | 72 | init([]) -> 73 | {ok, {{one_for_one, 1200, 60}, []}}. 74 | 75 | %% Clean out all mutable aspects of the queue except policy. We need 76 | %% to keep the entire queue around rather than just take its name 77 | %% since we will want to know its policy to determine how to federate 78 | %% it, and its immutable properties in case we want to redeclare it 79 | %% upstream. We don't just take its name and look it up again since 80 | %% that would introduce race conditions when policies change 81 | %% frequently. Note that since we take down all the links and start 82 | %% again when policies change, the policy will always be correct, so 83 | %% we don't clear it out here and can trust it. 84 | id(Q) when ?is_amqqueue(Q) -> 85 | Policy = amqqueue:get_policy(Q), 86 | Q1 = rabbit_amqqueue:immutable(Q), 87 | amqqueue:set_policy(Q1, Policy). 88 | -------------------------------------------------------------------------------- /src/rabbit_federation_status.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_federation_status). 9 | -behaviour(gen_server). 10 | 11 | -include_lib("amqp_client/include/amqp_client.hrl"). 12 | -include("rabbit_federation.hrl"). 13 | 14 | -export([start_link/0]). 15 | 16 | -export([report/4, remove_exchange_or_queue/1, remove/2, status/0, lookup/1]). 17 | 18 | -export([init/1, handle_call/3, handle_cast/2, handle_info/2, 19 | terminate/2, code_change/3]). 20 | 21 | -import(rabbit_federation_util, [name/1]). 22 | 23 | -define(SERVER, ?MODULE). 24 | -define(ETS_NAME, ?MODULE). 25 | 26 | -record(state, {}). 27 | -record(entry, {key, uri, status, timestamp, id, supervisor, upstream}). 28 | 29 | start_link() -> 30 | gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). 31 | 32 | report(Upstream, UParams, XorQName, Status) -> 33 | [Supervisor | _] = get('$ancestors'), 34 | gen_server:cast(?SERVER, {report, Supervisor, Upstream, UParams, XorQName, 35 | Status, calendar:local_time()}). 36 | 37 | remove_exchange_or_queue(XorQName) -> 38 | gen_server:call(?SERVER, {remove_exchange_or_queue, XorQName}, infinity). 39 | 40 | remove(Upstream, XorQName) -> 41 | gen_server:call(?SERVER, {remove, Upstream, XorQName}, infinity). 42 | 43 | status() -> 44 | gen_server:call(?SERVER, status, infinity). 45 | 46 | lookup(Id) -> 47 | gen_server:call(?SERVER, {lookup, Id}, infinity). 48 | 49 | init([]) -> 50 | ?ETS_NAME = ets:new(?ETS_NAME, 51 | [named_table, {keypos, #entry.key}, private]), 52 | {ok, #state{}}. 53 | 54 | handle_call({remove_exchange_or_queue, XorQName}, _From, State) -> 55 | [link_gone(Entry) 56 | || Entry <- ets:match_object(?ETS_NAME, match_entry(xorqkey(XorQName)))], 57 | {reply, ok, State}; 58 | 59 | handle_call({remove, Upstream, XorQName}, _From, State) -> 60 | case ets:match_object(?ETS_NAME, match_entry(key(XorQName, Upstream))) of 61 | [Entry] -> link_gone(Entry); 62 | [] -> ok 63 | end, 64 | {reply, ok, State}; 65 | 66 | handle_call({lookup, Id}, _From, State) -> 67 | Link = case ets:match_object(?ETS_NAME, match_id(Id)) of 68 | [Entry] -> 69 | [{key, Entry#entry.key}, 70 | {uri, Entry#entry.uri}, 71 | {status, Entry#entry.status}, 72 | {timestamp, Entry#entry.timestamp}, 73 | {id, Entry#entry.id}, 74 | {supervisor, Entry#entry.supervisor}, 75 | {upstream, Entry#entry.upstream}]; 76 | [] -> not_found 77 | end, 78 | {reply, Link, State}; 79 | 80 | handle_call(status, _From, State) -> 81 | Entries = ets:tab2list(?ETS_NAME), 82 | {reply, [format(Entry) || Entry <- Entries], State}. 83 | 84 | handle_cast({report, Supervisor, Upstream, #upstream_params{safe_uri = URI}, 85 | XorQName, Status, Timestamp}, State) -> 86 | Key = key(XorQName, Upstream), 87 | Entry = #entry{key = Key, 88 | status = Status, 89 | uri = URI, 90 | timestamp = Timestamp, 91 | supervisor = Supervisor, 92 | upstream = Upstream, 93 | id = unique_id(Key)}, 94 | true = ets:insert(?ETS_NAME, Entry), 95 | rabbit_event:notify(federation_link_status, format(Entry)), 96 | {noreply, State}. 97 | 98 | handle_info(_Info, State) -> 99 | {noreply, State}. 100 | 101 | terminate(_Reason, _State) -> 102 | ok. 103 | 104 | code_change(_OldVsn, State, _Extra) -> 105 | {ok, State}. 106 | 107 | format(#entry{status = Status, 108 | uri = URI, 109 | timestamp = Timestamp} = Entry) -> 110 | identity(Entry) ++ split_status(Status) ++ [{uri, URI}, 111 | {timestamp, Timestamp}]. 112 | 113 | identity(#entry{key = {#resource{virtual_host = VHost, 114 | kind = Type, 115 | name = XorQNameBin}, 116 | UpstreamName, UXorQNameBin}, 117 | id = Id, 118 | upstream = #upstream{consumer_tag = ConsumerTag}}) -> 119 | case Type of 120 | exchange -> [{exchange, XorQNameBin}, 121 | {upstream_exchange, UXorQNameBin}]; 122 | queue -> [{queue, XorQNameBin}, 123 | {upstream_queue, UXorQNameBin}, 124 | {consumer_tag, ConsumerTag}] 125 | end ++ [{type, Type}, 126 | {vhost, VHost}, 127 | {upstream, UpstreamName}, 128 | {id, Id}]. 129 | 130 | unique_id(Key = {#resource{}, UpName, ResName}) when is_binary(UpName), is_binary(ResName) -> 131 | PHash = erlang:phash2(Key, 1 bsl 32), 132 | << << case N >= 10 of 133 | true -> N - 10 + $a; 134 | false -> N + $0 end >> 135 | || <> <= <> >>. 136 | 137 | split_status({running, ConnName}) -> [{status, running}, 138 | {local_connection, ConnName}]; 139 | split_status({Status, Error}) -> [{status, Status}, 140 | {error, Error}]; 141 | split_status(Status) when is_atom(Status) -> [{status, Status}]. 142 | 143 | link_gone(Entry) -> 144 | rabbit_event:notify(federation_link_removed, identity(Entry)), 145 | true = ets:delete_object(?ETS_NAME, Entry). 146 | 147 | %% We don't want to key off the entire upstream, bits of it may change 148 | key(XName = #resource{kind = exchange}, #upstream{name = UpstreamName, 149 | exchange_name = UXNameBin}) -> 150 | {XName, UpstreamName, UXNameBin}; 151 | 152 | key(QName = #resource{kind = queue}, #upstream{name = UpstreamName, 153 | queue_name = UQNameBin}) -> 154 | {QName, UpstreamName, UQNameBin}. 155 | 156 | xorqkey(XorQName) -> 157 | {XorQName, '_', '_'}. 158 | 159 | match_entry(Key) -> 160 | #entry{key = Key, 161 | uri = '_', 162 | status = '_', 163 | timestamp = '_', 164 | id = '_', 165 | supervisor = '_', 166 | upstream = '_'}. 167 | 168 | match_id(Id) -> 169 | #entry{key = '_', 170 | uri = '_', 171 | status = '_', 172 | timestamp = '_', 173 | id = Id, 174 | supervisor = '_', 175 | upstream = '_'}. 176 | -------------------------------------------------------------------------------- /src/rabbit_federation_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_federation_sup). 9 | 10 | -behaviour(supervisor). 11 | 12 | %% Supervises everything. There is just one of these. 13 | 14 | -include_lib("rabbit_common/include/rabbit.hrl"). 15 | -define(SUPERVISOR, rabbit_federation_sup). 16 | 17 | -export([start_link/0, stop/0]). 18 | 19 | -export([init/1]). 20 | 21 | %% This supervisor needs to be part of the rabbit application since 22 | %% a) it needs to be in place when exchange recovery takes place 23 | %% b) it needs to go up and down with rabbit 24 | 25 | -rabbit_boot_step({rabbit_federation_supervisor, 26 | [{description, "federation"}, 27 | {mfa, {rabbit_sup, start_child, [?MODULE]}}, 28 | {requires, kernel_ready}, 29 | {cleanup, {?MODULE, stop, []}}, 30 | {enables, rabbit_federation_exchange}, 31 | {enables, rabbit_federation_queue}]}). 32 | 33 | %%---------------------------------------------------------------------------- 34 | 35 | start_link() -> 36 | R = supervisor:start_link({local, ?SUPERVISOR}, ?MODULE, []), 37 | rabbit_federation_event:add_handler(), 38 | R. 39 | 40 | stop() -> 41 | rabbit_federation_event:remove_handler(), 42 | ok = supervisor:terminate_child(rabbit_sup, ?MODULE), 43 | ok = supervisor:delete_child(rabbit_sup, ?MODULE). 44 | 45 | %%---------------------------------------------------------------------------- 46 | 47 | init([]) -> 48 | Status = {status, {rabbit_federation_status, start_link, []}, 49 | transient, ?WORKER_WAIT, worker, 50 | [rabbit_federation_status]}, 51 | XLinkSupSup = {x_links, 52 | {rabbit_federation_exchange_link_sup_sup, start_link, []}, 53 | transient, ?SUPERVISOR_WAIT, supervisor, 54 | [rabbit_federation_exchange_link_sup_sup]}, 55 | QLinkSupSup = {q_links, 56 | {rabbit_federation_queue_link_sup_sup, start_link, []}, 57 | transient, ?SUPERVISOR_WAIT, supervisor, 58 | [rabbit_federation_queue_link_sup_sup]}, 59 | %% with default reconnect-delay of 5 second, this supports up to 60 | %% 100 links constantly failing and being restarted a minute 61 | %% (or 200 links if reconnect-delay is 10 seconds, 600 with 30 seconds, 62 | %% etc: N * (60/reconnect-delay) <= 1200) 63 | {ok, {{one_for_one, 1200, 60}, [Status, XLinkSupSup, QLinkSupSup]}}. 64 | -------------------------------------------------------------------------------- /src/rabbit_federation_upstream.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_federation_upstream). 9 | 10 | -include("rabbit_federation.hrl"). 11 | -include_lib("rabbit/include/amqqueue.hrl"). 12 | -include_lib("amqp_client/include/amqp_client.hrl"). 13 | 14 | -export([federate/1, for/1, for/2, params_to_string/1, to_params/2]). 15 | %% For testing 16 | -export([from_set/2, from_pattern/2, remove_credentials/1]). 17 | 18 | -import(rabbit_misc, [pget/2, pget/3]). 19 | -import(rabbit_federation_util, [name/1, vhost/1, r/1]). 20 | -import(rabbit_data_coercion, [to_atom/1]). 21 | 22 | %%---------------------------------------------------------------------------- 23 | 24 | federate(XorQ) -> 25 | rabbit_policy:get(<<"federation-upstream">>, XorQ) =/= undefined orelse 26 | rabbit_policy:get(<<"federation-upstream-set">>, XorQ) =/= undefined orelse 27 | rabbit_policy:get(<<"federation-upstream-pattern">>, XorQ) =/= undefined. 28 | 29 | for(XorQ) -> 30 | case federate(XorQ) of 31 | false -> []; 32 | true -> from_set_contents(upstreams(XorQ), XorQ) 33 | end. 34 | 35 | for(XorQ, UpstreamName) -> 36 | case federate(XorQ) of 37 | false -> []; 38 | true -> rabbit_federation_util:find_upstreams( 39 | UpstreamName, from_set_contents(upstreams(XorQ), XorQ)) 40 | end. 41 | 42 | upstreams(XorQ) -> 43 | UName = rabbit_policy:get(<<"federation-upstream">>, XorQ), 44 | USetName = rabbit_policy:get(<<"federation-upstream-set">>, XorQ), 45 | UPatternValue = rabbit_policy:get(<<"federation-upstream-pattern">>, XorQ), 46 | %% Cannot define 2 at a time, see rabbit_federation_parameters:validate_policy/1 47 | case {UName, USetName, UPatternValue} of 48 | {undefined, undefined, undefined} -> []; 49 | {undefined, undefined, _} -> find_contents(UPatternValue, vhost(XorQ)); 50 | {undefined, _, undefined} -> set_contents(USetName, vhost(XorQ)); 51 | {_, undefined, undefined} -> [[{<<"upstream">>, UName}]] 52 | end. 53 | 54 | params_table(SafeURI, XorQ) -> 55 | Key = case XorQ of 56 | #exchange{} -> <<"exchange">>; 57 | Q when ?is_amqqueue(Q) -> <<"queue">> 58 | end, 59 | [{<<"uri">>, longstr, SafeURI}, 60 | {Key, longstr, name(XorQ)}]. 61 | 62 | params_to_string(#upstream_params{safe_uri = SafeURI, 63 | x_or_q = XorQ}) -> 64 | print("~s on ~s", [rabbit_misc:rs(r(XorQ)), SafeURI]). 65 | 66 | remove_credentials(URI) -> 67 | list_to_binary(amqp_uri:remove_credentials(binary_to_list(URI))). 68 | 69 | to_params(Upstream = #upstream{uris = URIs}, XorQ) -> 70 | URI = lists:nth(rand:uniform(length(URIs)), URIs), 71 | {ok, Params} = amqp_uri:parse(binary_to_list(URI), vhost(XorQ)), 72 | XorQ1 = with_name(Upstream, vhost(Params), XorQ), 73 | SafeURI = remove_credentials(URI), 74 | #upstream_params{params = Params, 75 | uri = URI, 76 | x_or_q = XorQ1, 77 | safe_uri = SafeURI, 78 | table = params_table(SafeURI, XorQ)}. 79 | 80 | print(Fmt, Args) -> iolist_to_binary(io_lib:format(Fmt, Args)). 81 | 82 | from_set(SetName, XorQ) -> 83 | from_set_contents(set_contents(SetName, vhost(XorQ)), XorQ). 84 | 85 | from_pattern(SetName, XorQ) -> 86 | from_set_contents(find_contents(SetName, vhost(XorQ)), XorQ). 87 | 88 | set_contents(<<"all">>, VHost) -> 89 | Upstreams0 = rabbit_runtime_parameters:list( 90 | VHost, <<"federation-upstream">>), 91 | Upstreams = [rabbit_data_coercion:to_list(U) || U <- Upstreams0], 92 | [[{<<"upstream">>, pget(name, U)}] || U <- Upstreams]; 93 | 94 | set_contents(SetName, VHost) -> 95 | case rabbit_runtime_parameters:value( 96 | VHost, <<"federation-upstream-set">>, SetName) of 97 | not_found -> []; 98 | Set -> Set 99 | end. 100 | 101 | find_contents(RegExp, VHost) -> 102 | Upstreams0 = rabbit_runtime_parameters:list( 103 | VHost, <<"federation-upstream">>), 104 | Upstreams = [rabbit_data_coercion:to_list(U) || U <- Upstreams0, 105 | re:run(pget(name, U), RegExp) =/= nomatch], 106 | [[{<<"upstream">>, pget(name, U)}] || U <- Upstreams]. 107 | 108 | from_set_contents(Set, XorQ) -> 109 | Results = [from_set_element(P, XorQ) || P <- Set], 110 | [R || R <- Results, R =/= not_found]. 111 | 112 | from_set_element(UpstreamSetElem0, XorQ) -> 113 | UpstreamSetElem = rabbit_data_coercion:to_proplist(UpstreamSetElem0), 114 | Name = bget(upstream, UpstreamSetElem, []), 115 | case rabbit_runtime_parameters:value( 116 | vhost(XorQ), <<"federation-upstream">>, Name) of 117 | not_found -> not_found; 118 | Upstream -> from_upstream_or_set( 119 | UpstreamSetElem, Name, Upstream, XorQ) 120 | end. 121 | 122 | from_upstream_or_set(US, Name, U, XorQ) -> 123 | URIParam = bget(uri, US, U), 124 | URIs = case URIParam of 125 | B when is_binary(B) -> [B]; 126 | L when is_list(L) -> L 127 | end, 128 | #upstream{uris = URIs, 129 | exchange_name = bget(exchange, US, U, name(XorQ)), 130 | queue_name = bget(queue, US, U, name(XorQ)), 131 | consumer_tag = bget('consumer-tag', US, U, <<"federation-link-", Name/binary>>), 132 | prefetch_count = bget('prefetch-count', US, U, ?DEF_PREFETCH), 133 | reconnect_delay = bget('reconnect-delay', US, U, 5), 134 | max_hops = bget('max-hops', US, U, 1), 135 | expires = bget(expires, US, U, none), 136 | message_ttl = bget('message-ttl', US, U, none), 137 | trust_user_id = bget('trust-user-id', US, U, false), 138 | ack_mode = to_atom(bget('ack-mode', US, U, <<"on-confirm">>)), 139 | ha_policy = bget('ha-policy', US, U, none), 140 | name = Name, 141 | bind_nowait = bget('bind-nowait', US, U, false), 142 | resource_cleanup_mode = to_atom(bget('resource-cleanup-mode', US, U, <<"default">>))}. 143 | 144 | %%---------------------------------------------------------------------------- 145 | 146 | bget(K, L1, L2) -> bget(K, L1, L2, undefined). 147 | 148 | bget(K0, L1, L2, D) -> 149 | K = a2b(K0), 150 | %% coerce maps to proplists 151 | PL1 = rabbit_data_coercion:to_list(L1), 152 | PL2 = rabbit_data_coercion:to_list(L2), 153 | case pget(K, PL1, undefined) of 154 | undefined -> pget(K, PL2, D); 155 | Result -> Result 156 | end. 157 | 158 | a2b(A) -> list_to_binary(atom_to_list(A)). 159 | 160 | with_name(#upstream{exchange_name = XNameBin}, VHostBin, X = #exchange{}) -> 161 | X#exchange{name = rabbit_misc:r(VHostBin, exchange, XNameBin)}; 162 | 163 | with_name(#upstream{queue_name = QNameBin}, VHostBin, Q) when ?is_amqqueue(Q) -> 164 | amqqueue:set_name(Q, rabbit_misc:r(VHostBin, queue, QNameBin)). 165 | -------------------------------------------------------------------------------- /src/rabbit_federation_upstream_exchange.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_federation_upstream_exchange). 9 | 10 | -rabbit_boot_step({?MODULE, 11 | [{description, "federation upstream exchange type"}, 12 | {mfa, {rabbit_registry, register, 13 | [exchange, <<"x-federation-upstream">>, ?MODULE]}}, 14 | {requires, rabbit_registry}, 15 | {cleanup, {rabbit_registry, unregister, 16 | [exchange, <<"x-federation-upstream">>]}}, 17 | {enables, recovery}]}). 18 | 19 | -include_lib("rabbit_common/include/rabbit.hrl"). 20 | -include("rabbit_federation.hrl"). 21 | 22 | -behaviour(rabbit_exchange_type). 23 | 24 | -export([description/0, serialise_events/0, route/2]). 25 | -export([validate/1, validate_binding/2, 26 | create/2, delete/3, policy_changed/2, 27 | add_binding/3, remove_bindings/3, assert_args_equivalence/2]). 28 | -export([info/1, info/2]). 29 | 30 | %%---------------------------------------------------------------------------- 31 | 32 | info(_X) -> []. 33 | info(_X, _) -> []. 34 | 35 | description() -> 36 | [{description, <<"Federation upstream helper exchange">>}, 37 | {internal_purpose, federation}]. 38 | 39 | serialise_events() -> false. 40 | 41 | route(X = #exchange{arguments = Args}, 42 | D = #delivery{message = #basic_message{content = Content}}) -> 43 | %% This arg was introduced in the same release as this exchange type; 44 | %% it must be set 45 | {long, MaxHops} = rabbit_misc:table_lookup(Args, ?MAX_HOPS_ARG), 46 | %% Will be missing for pre-3.3.0 versions 47 | DName = case rabbit_misc:table_lookup(Args, ?DOWNSTREAM_NAME_ARG) of 48 | {longstr, Val0} -> Val0; 49 | _ -> unknown 50 | end, 51 | %% Will be missing for pre-3.8.9 versions 52 | DVhost = case rabbit_misc:table_lookup(Args, ?DOWNSTREAM_VHOST_ARG) of 53 | {longstr, Val1} -> Val1; 54 | _ -> unknown 55 | end, 56 | Headers = rabbit_basic:extract_headers(Content), 57 | case rabbit_federation_util:should_forward(Headers, MaxHops, DName, DVhost) of 58 | true -> rabbit_exchange_type_fanout:route(X, D); 59 | false -> [] 60 | end. 61 | 62 | validate(#exchange{arguments = Args}) -> 63 | rabbit_federation_util:validate_arg(?MAX_HOPS_ARG, long, Args). 64 | 65 | validate_binding(_X, _B) -> ok. 66 | create(_Tx, _X) -> ok. 67 | delete(_Tx, _X, _Bs) -> ok. 68 | policy_changed(_X1, _X2) -> ok. 69 | add_binding(_Tx, _X, _B) -> ok. 70 | remove_bindings(_Tx, _X, _Bs) -> ok. 71 | 72 | assert_args_equivalence(X = #exchange{name = Name, 73 | arguments = Args}, ReqArgs) -> 74 | rabbit_misc:assert_args_equivalence(Args, ReqArgs, Name, [?MAX_HOPS_ARG]), 75 | rabbit_exchange:assert_args_equivalence(X, Args). 76 | -------------------------------------------------------------------------------- /src/rabbit_federation_util.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_federation_util). 9 | 10 | -include_lib("rabbit/include/amqqueue.hrl"). 11 | -include_lib("amqp_client/include/amqp_client.hrl"). 12 | -include("rabbit_federation.hrl"). 13 | 14 | -export([should_forward/4, find_upstreams/2, already_seen/3]). 15 | -export([validate_arg/3, fail/2, name/1, vhost/1, r/1, pgname/1]). 16 | -export([obfuscate_upstream/1, deobfuscate_upstream/1, obfuscate_upstream_params/1, deobfuscate_upstream_params/1]). 17 | 18 | -import(rabbit_misc, [pget_or_die/2, pget/3]). 19 | 20 | %%---------------------------------------------------------------------------- 21 | 22 | should_forward(undefined, _MaxHops, _DName, _DVhost) -> 23 | true; 24 | should_forward(Headers, MaxHops, DName, DVhost) -> 25 | case rabbit_misc:table_lookup(Headers, ?ROUTING_HEADER) of 26 | {array, A} -> length(A) < MaxHops andalso not already_seen(DName, DVhost, A); 27 | _ -> true 28 | end. 29 | 30 | %% Used to detect message and binding forwarding cycles. 31 | already_seen(UpstreamID, UpstreamVhost, Array) -> 32 | lists:any(fun ({table, T}) -> 33 | {longstr, UpstreamID} =:= rabbit_misc:table_lookup(T, <<"cluster-name">>) andalso 34 | {longstr, UpstreamVhost} =:= rabbit_misc:table_lookup(T, <<"vhost">>); 35 | (_) -> 36 | false 37 | end, Array). 38 | 39 | find_upstreams(Name, Upstreams) -> 40 | [U || U = #upstream{name = Name2} <- Upstreams, 41 | Name =:= Name2]. 42 | 43 | validate_arg(Name, Type, Args) -> 44 | case rabbit_misc:table_lookup(Args, Name) of 45 | {Type, _} -> ok; 46 | undefined -> fail("Argument ~s missing", [Name]); 47 | _ -> fail("Argument ~s must be of type ~s", [Name, Type]) 48 | end. 49 | 50 | -spec fail(io:format(), [term()]) -> no_return(). 51 | 52 | fail(Fmt, Args) -> rabbit_misc:protocol_error(precondition_failed, Fmt, Args). 53 | 54 | name( #resource{name = XorQName}) -> XorQName; 55 | name(#exchange{name = #resource{name = XName}}) -> XName; 56 | name(Q) when ?is_amqqueue(Q) -> #resource{name = QName} = amqqueue:get_name(Q), QName. 57 | 58 | vhost( #resource{virtual_host = VHost}) -> VHost; 59 | vhost(#exchange{name = #resource{virtual_host = VHost}}) -> VHost; 60 | vhost(Q) when ?is_amqqueue(Q) -> #resource{virtual_host = VHost} = amqqueue:get_name(Q), VHost; 61 | vhost(#amqp_params_direct{virtual_host = VHost}) -> VHost; 62 | vhost(#amqp_params_network{virtual_host = VHost}) -> VHost. 63 | 64 | r(#exchange{name = XName}) -> XName; 65 | r(Q) when ?is_amqqueue(Q) -> amqqueue:get_name(Q). 66 | 67 | pgname(Name) -> 68 | case application:get_env(rabbitmq_federation, pgroup_name_cluster_id) of 69 | {ok, false} -> Name; 70 | {ok, true} -> {rabbit_nodes:cluster_name(), Name}; 71 | %% default value is 'false', so do the same thing 72 | {ok, undefined} -> Name; 73 | _ -> Name 74 | end. 75 | 76 | obfuscate_upstream(#upstream{uris = Uris} = Upstream) -> 77 | Upstream#upstream{uris = [credentials_obfuscation:encrypt(Uri) || Uri <- Uris]}. 78 | 79 | obfuscate_upstream_params(#upstream_params{uri = Uri, params = #amqp_params_network{password = Password} = Params} = UParams) -> 80 | UParams#upstream_params{ 81 | uri = credentials_obfuscation:encrypt(Uri), 82 | params = Params#amqp_params_network{password = credentials_obfuscation:encrypt(rabbit_data_coercion:to_binary(Password))} 83 | }; 84 | obfuscate_upstream_params(#upstream_params{uri = Uri, params = #amqp_params_direct{password = Password} = Params} = UParams) -> 85 | UParams#upstream_params{ 86 | uri = credentials_obfuscation:encrypt(Uri), 87 | params = Params#amqp_params_direct{password = credentials_obfuscation:encrypt(rabbit_data_coercion:to_binary(Password))} 88 | }. 89 | 90 | deobfuscate_upstream(#upstream{uris = EncryptedUris} = Upstream) -> 91 | Upstream#upstream{uris = [credentials_obfuscation:decrypt(EncryptedUri) || EncryptedUri <- EncryptedUris]}. 92 | 93 | deobfuscate_upstream_params(#upstream_params{uri = EncryptedUri, params = #amqp_params_network{password = EncryptedPassword} = Params} = UParams) -> 94 | UParams#upstream_params{ 95 | uri = credentials_obfuscation:decrypt(EncryptedUri), 96 | params = Params#amqp_params_network{password = credentials_obfuscation:decrypt(EncryptedPassword)} 97 | }; 98 | deobfuscate_upstream_params(#upstream_params{uri = EncryptedUri, params = #amqp_params_direct{password = EncryptedPassword} = Params} = UParams) -> 99 | UParams#upstream_params{ 100 | uri = credentials_obfuscation:decrypt(EncryptedUri), 101 | params = Params#amqp_params_direct{password = credentials_obfuscation:decrypt(EncryptedPassword)} 102 | }. 103 | -------------------------------------------------------------------------------- /test/federation_status_command_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(federation_status_command_SUITE). 9 | 10 | -include_lib("common_test/include/ct.hrl"). 11 | -include_lib("amqp_client/include/amqp_client.hrl"). 12 | 13 | -compile(export_all). 14 | 15 | -define(CMD, 'Elixir.RabbitMQ.CLI.Ctl.Commands.FederationStatusCommand'). 16 | 17 | all() -> 18 | [ 19 | {group, not_federated}, 20 | {group, federated}, 21 | {group, federated_down} 22 | ]. 23 | 24 | groups() -> 25 | [ 26 | {not_federated, [], [ 27 | run_not_federated, 28 | output_not_federated 29 | ]}, 30 | {federated, [], [ 31 | run_federated, 32 | output_federated 33 | ]}, 34 | {federated_down, [], [ 35 | run_down_federated 36 | ]} 37 | ]. 38 | 39 | %% ------------------------------------------------------------------- 40 | %% Testsuite setup/teardown. 41 | %% ------------------------------------------------------------------- 42 | 43 | init_per_suite(Config) -> 44 | rabbit_ct_helpers:log_environment(), 45 | Config1 = rabbit_ct_helpers:set_config(Config, [ 46 | {rmq_nodename_suffix, ?MODULE} 47 | ]), 48 | Config2 = rabbit_ct_helpers:run_setup_steps(Config1, 49 | rabbit_ct_broker_helpers:setup_steps() ++ 50 | rabbit_ct_client_helpers:setup_steps()), 51 | Config2. 52 | 53 | end_per_suite(Config) -> 54 | rabbit_ct_helpers:run_teardown_steps(Config, 55 | rabbit_ct_client_helpers:teardown_steps() ++ 56 | rabbit_ct_broker_helpers:teardown_steps()). 57 | 58 | init_per_group(federated, Config) -> 59 | rabbit_federation_test_util:setup_federation(Config), 60 | Config; 61 | init_per_group(federated_down, Config) -> 62 | rabbit_federation_test_util:setup_down_federation(Config), 63 | Config; 64 | init_per_group(_, Config) -> 65 | Config. 66 | 67 | end_per_group(_, Config) -> 68 | Config. 69 | 70 | init_per_testcase(Testcase, Config) -> 71 | rabbit_ct_helpers:testcase_started(Config, Testcase). 72 | 73 | end_per_testcase(Testcase, Config) -> 74 | rabbit_ct_helpers:testcase_finished(Config, Testcase). 75 | 76 | %% ------------------------------------------------------------------- 77 | %% Testcases. 78 | %% ------------------------------------------------------------------- 79 | run_not_federated(Config) -> 80 | [A] = rabbit_ct_broker_helpers:get_node_configs(Config, nodename), 81 | Opts = #{node => A}, 82 | {stream, []} = ?CMD:run([], Opts#{only_down => false}). 83 | 84 | output_not_federated(Config) -> 85 | [A] = rabbit_ct_broker_helpers:get_node_configs(Config, nodename), 86 | Opts = #{node => A}, 87 | {stream, []} = ?CMD:output({stream, []}, Opts). 88 | 89 | run_federated(Config) -> 90 | [A] = rabbit_ct_broker_helpers:get_node_configs(Config, nodename), 91 | Opts = #{node => A}, 92 | %% All 93 | rabbit_federation_test_util:with_ch( 94 | Config, 95 | fun(_) -> 96 | timer:sleep(3000), 97 | {stream, [Props]} = ?CMD:run([], Opts#{only_down => false}), 98 | <<"upstream">> = proplists:get_value(upstream_queue, Props), 99 | <<"fed.downstream">> = proplists:get_value(queue, Props), 100 | <<"fed.tag">> = proplists:get_value(consumer_tag, Props), 101 | running = proplists:get_value(status, Props) 102 | end, 103 | [rabbit_federation_test_util:q(<<"upstream">>), 104 | rabbit_federation_test_util:q(<<"fed.downstream">>)]), 105 | %% Down 106 | rabbit_federation_test_util:with_ch( 107 | Config, 108 | fun(_) -> 109 | {stream, []} = ?CMD:run([], Opts#{only_down => true}) 110 | end, 111 | [rabbit_federation_test_util:q(<<"upstream">>), 112 | rabbit_federation_test_util:q(<<"fed.downstream">>)]). 113 | 114 | run_down_federated(Config) -> 115 | [A] = rabbit_ct_broker_helpers:get_node_configs(Config, nodename), 116 | Opts = #{node => A}, 117 | %% All 118 | rabbit_federation_test_util:with_ch( 119 | Config, 120 | fun(_) -> 121 | timer:sleep(3000), 122 | {stream, ManyProps} = ?CMD:run([], Opts#{only_down => false}), 123 | Links = [{proplists:get_value(upstream, Props), 124 | proplists:get_value(status, Props)} 125 | || Props <- ManyProps], 126 | [{<<"broken-bunny">>, error}, {<<"localhost">>, running}] 127 | = lists:sort(Links) 128 | end, 129 | [rabbit_federation_test_util:q(<<"upstream">>), 130 | rabbit_federation_test_util:q(<<"fed.downstream">>)]), 131 | %% Down 132 | rabbit_federation_test_util:with_ch( 133 | Config, 134 | fun(_) -> 135 | timer:sleep(3000), 136 | {stream, [Props]} = ?CMD:run([], Opts#{only_down => true}), 137 | <<"broken-bunny">> = proplists:get_value(upstream, Props), 138 | error = proplists:get_value(status, Props) 139 | end, 140 | [rabbit_federation_test_util:q(<<"upstream">>), 141 | rabbit_federation_test_util:q(<<"fed.downstream">>)]). 142 | 143 | output_federated(Config) -> 144 | [A] = rabbit_ct_broker_helpers:get_node_configs(Config, nodename), 145 | Opts = #{node => A}, 146 | Input = {stream,[[{queue, <<"fed.downstream">>}, 147 | {consumer_tag, <<"fed.tag">>}, 148 | {upstream_queue, <<"upstream">>}, 149 | {type, queue}, 150 | {vhost, <<"/">>}, 151 | {upstream, <<"localhost">>}, 152 | {status, running}, 153 | {local_connection, <<"">>}, 154 | {uri, <<"amqp://localhost:21000">>}, 155 | {timestamp, {{2016,11,21},{8,51,19}}}]]}, 156 | {stream, [#{queue := <<"fed.downstream">>, 157 | upstream_queue := <<"upstream">>, 158 | type := queue, 159 | vhost := <<"/">>, 160 | upstream := <<"localhost">>, 161 | status := running, 162 | local_connection := <<"">>, 163 | uri := <<"amqp://localhost:21000">>, 164 | last_changed := <<"2016-11-21 08:51:19">>, 165 | exchange := <<>>, 166 | upstream_exchange := <<>>, 167 | error := <<>>}]} 168 | = ?CMD:output(Input, Opts). 169 | -------------------------------------------------------------------------------- /test/queue_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(queue_SUITE). 9 | 10 | -include_lib("common_test/include/ct.hrl"). 11 | -include_lib("amqp_client/include/amqp_client.hrl"). 12 | 13 | -compile(export_all). 14 | 15 | -import(rabbit_federation_test_util, 16 | [wait_for_federation/2, expect/3, expect/4, 17 | set_upstream/4, set_upstream/5, clear_upstream/3, set_policy/5, clear_policy/3, 18 | set_policy_pattern/5, set_policy_upstream/5, q/1, with_ch/3, 19 | declare_queue/2, delete_queue/2, 20 | federation_links_in_vhost/3]). 21 | 22 | -define(INITIAL_WAIT, 6000). 23 | -define(EXPECT_FEDERATION_TIMEOUT, 30000). 24 | 25 | all() -> 26 | [ 27 | {group, without_disambiguate}, 28 | {group, with_disambiguate} 29 | ]. 30 | 31 | groups() -> 32 | [ 33 | {without_disambiguate, [], [ 34 | {cluster_size_1, [], [ 35 | simple, 36 | multiple_upstreams, 37 | multiple_upstreams_pattern, 38 | multiple_downstreams, 39 | bidirectional, 40 | dynamic_reconfiguration, 41 | federate_unfederate, 42 | dynamic_plugin_stop_start 43 | ]} 44 | ]}, 45 | {with_disambiguate, [], [ 46 | {cluster_size_2, [], [ 47 | restart_upstream 48 | ]} 49 | ]} 50 | ]. 51 | 52 | %% ------------------------------------------------------------------- 53 | %% Testsuite setup/teardown. 54 | %% ------------------------------------------------------------------- 55 | 56 | init_per_suite(Config) -> 57 | rabbit_ct_helpers:log_environment(), 58 | rabbit_ct_helpers:run_setup_steps(Config). 59 | 60 | end_per_suite(Config) -> 61 | rabbit_ct_helpers:run_teardown_steps(Config). 62 | 63 | init_per_group(without_disambiguate, Config) -> 64 | rabbit_ct_helpers:set_config(Config, 65 | {disambiguate_step, []}); 66 | init_per_group(with_disambiguate, Config) -> 67 | rabbit_ct_helpers:set_config(Config, 68 | {disambiguate_step, [fun rabbit_federation_test_util:disambiguate/1]}); 69 | init_per_group(cluster_size_1 = Group, Config) -> 70 | Config1 = rabbit_ct_helpers:set_config(Config, [ 71 | {rmq_nodes_count, 1} 72 | ]), 73 | init_per_group1(Group, Config1); 74 | init_per_group(cluster_size_2 = Group, Config) -> 75 | Config1 = rabbit_ct_helpers:set_config(Config, [ 76 | {rmq_nodes_count, 2} 77 | ]), 78 | init_per_group1(Group, Config1). 79 | 80 | init_per_group1(Group, Config) -> 81 | SetupFederation = case Group of 82 | cluster_size_1 -> [fun rabbit_federation_test_util:setup_federation/1]; 83 | cluster_size_2 -> [] 84 | end, 85 | Disambiguate = ?config(disambiguate_step, Config), 86 | Suffix = rabbit_ct_helpers:testcase_absname(Config, "", "-"), 87 | Config1 = rabbit_ct_helpers:set_config(Config, [ 88 | {rmq_nodename_suffix, Suffix}, 89 | {rmq_nodes_clustered, false} 90 | ]), 91 | rabbit_ct_helpers:run_steps(Config1, 92 | rabbit_ct_broker_helpers:setup_steps() ++ 93 | rabbit_ct_client_helpers:setup_steps() ++ 94 | SetupFederation ++ Disambiguate). 95 | 96 | end_per_group(without_disambiguate, Config) -> 97 | Config; 98 | end_per_group(with_disambiguate, Config) -> 99 | Config; 100 | end_per_group(_, Config) -> 101 | rabbit_ct_helpers:run_steps(Config, 102 | rabbit_ct_client_helpers:teardown_steps() ++ 103 | rabbit_ct_broker_helpers:teardown_steps()). 104 | 105 | init_per_testcase(Testcase, Config) -> 106 | rabbit_ct_helpers:testcase_started(Config, Testcase). 107 | 108 | end_per_testcase(Testcase, Config) -> 109 | rabbit_ct_helpers:testcase_finished(Config, Testcase). 110 | 111 | %% ------------------------------------------------------------------- 112 | %% Testcases. 113 | %% ------------------------------------------------------------------- 114 | 115 | simple(Config) -> 116 | with_ch(Config, 117 | fun (Ch) -> 118 | expect_federation(Ch, <<"upstream">>, <<"fed.downstream">>) 119 | end, upstream_downstream()). 120 | 121 | multiple_upstreams(Config) -> 122 | with_ch(Config, 123 | fun (Ch) -> 124 | expect_federation(Ch, <<"upstream">>, <<"fed12.downstream">>), 125 | expect_federation(Ch, <<"upstream2">>, <<"fed12.downstream">>) 126 | end, [q(<<"upstream">>), 127 | q(<<"upstream2">>), 128 | q(<<"fed12.downstream">>)]). 129 | 130 | multiple_upstreams_pattern(Config) -> 131 | set_upstream(Config, 0, <<"local453x">>, 132 | rabbit_ct_broker_helpers:node_uri(Config, 0), [ 133 | {<<"exchange">>, <<"upstream">>}, 134 | {<<"queue">>, <<"upstream">>}]), 135 | 136 | set_upstream(Config, 0, <<"zzzzzZZzz">>, 137 | rabbit_ct_broker_helpers:node_uri(Config, 0), [ 138 | {<<"exchange">>, <<"upstream-zzz">>}, 139 | {<<"queue">>, <<"upstream-zzz">>}]), 140 | 141 | set_upstream(Config, 0, <<"local3214x">>, 142 | rabbit_ct_broker_helpers:node_uri(Config, 0), [ 143 | {<<"exchange">>, <<"upstream2">>}, 144 | {<<"queue">>, <<"upstream2">>}]), 145 | 146 | set_policy_pattern(Config, 0, <<"pattern">>, <<"^pattern\.">>, <<"local\\d+x">>), 147 | 148 | with_ch(Config, 149 | fun (Ch) -> 150 | expect_federation(Ch, <<"upstream">>, <<"pattern.downstream">>, ?EXPECT_FEDERATION_TIMEOUT), 151 | expect_federation(Ch, <<"upstream2">>, <<"pattern.downstream">>, ?EXPECT_FEDERATION_TIMEOUT) 152 | end, [q(<<"upstream">>), 153 | q(<<"upstream2">>), 154 | q(<<"pattern.downstream">>)]), 155 | 156 | clear_upstream(Config, 0, <<"local453x">>), 157 | clear_upstream(Config, 0, <<"local3214x">>), 158 | clear_policy(Config, 0, <<"pattern">>). 159 | 160 | multiple_downstreams(Config) -> 161 | with_ch(Config, 162 | fun (Ch) -> 163 | timer:sleep(?INITIAL_WAIT), 164 | expect_federation(Ch, <<"upstream">>, <<"fed.downstream">>, ?EXPECT_FEDERATION_TIMEOUT), 165 | expect_federation(Ch, <<"upstream">>, <<"fed.downstream2">>, ?EXPECT_FEDERATION_TIMEOUT) 166 | end, upstream_downstream() ++ [q(<<"fed.downstream2">>)]). 167 | 168 | bidirectional(Config) -> 169 | with_ch(Config, 170 | fun (Ch) -> 171 | timer:sleep(?INITIAL_WAIT), 172 | publish_expect(Ch, <<>>, <<"one">>, <<"one">>, <<"first one">>, ?EXPECT_FEDERATION_TIMEOUT), 173 | publish_expect(Ch, <<>>, <<"two">>, <<"two">>, <<"first two">>, ?EXPECT_FEDERATION_TIMEOUT), 174 | Seq = lists:seq(1, 100), 175 | [publish(Ch, <<>>, <<"one">>, <<"bulk">>) || _ <- Seq], 176 | [publish(Ch, <<>>, <<"two">>, <<"bulk">>) || _ <- Seq], 177 | expect(Ch, <<"one">>, repeat(150, <<"bulk">>)), 178 | expect(Ch, <<"two">>, repeat(50, <<"bulk">>)), 179 | expect_empty(Ch, <<"one">>), 180 | expect_empty(Ch, <<"two">>) 181 | end, [q(<<"one">>), 182 | q(<<"two">>)]). 183 | 184 | dynamic_reconfiguration(Config) -> 185 | with_ch(Config, 186 | fun (Ch) -> 187 | timer:sleep(?INITIAL_WAIT), 188 | expect_federation(Ch, <<"upstream">>, <<"fed.downstream">>, ?EXPECT_FEDERATION_TIMEOUT), 189 | 190 | %% Test that clearing connections works 191 | clear_upstream(Config, 0, <<"localhost">>), 192 | expect_no_federation(Ch, <<"upstream">>, <<"fed.downstream">>), 193 | 194 | %% Test that reading them and changing them works 195 | set_upstream(Config, 0, 196 | <<"localhost">>, rabbit_ct_broker_helpers:node_uri(Config, 0)), 197 | %% Do it twice so we at least hit the no-restart optimisation 198 | URI = rabbit_ct_broker_helpers:node_uri(Config, 0, [use_ipaddr]), 199 | set_upstream(Config, 0, <<"localhost">>, URI), 200 | set_upstream(Config, 0, <<"localhost">>, URI), 201 | expect_federation(Ch, <<"upstream">>, <<"fed.downstream">>) 202 | end, upstream_downstream()). 203 | 204 | federate_unfederate(Config) -> 205 | with_ch(Config, 206 | fun (Ch) -> 207 | timer:sleep(?INITIAL_WAIT), 208 | expect_federation(Ch, <<"upstream">>, <<"fed.downstream">>, ?EXPECT_FEDERATION_TIMEOUT), 209 | expect_federation(Ch, <<"upstream">>, <<"fed.downstream2">>, ?EXPECT_FEDERATION_TIMEOUT), 210 | 211 | %% clear the policy 212 | rabbit_ct_broker_helpers:clear_policy(Config, 0, <<"fed">>), 213 | 214 | expect_no_federation(Ch, <<"upstream">>, <<"fed.downstream">>), 215 | expect_no_federation(Ch, <<"upstream">>, <<"fed.downstream2">>), 216 | 217 | rabbit_ct_broker_helpers:set_policy(Config, 0, 218 | <<"fed">>, <<"^fed\.">>, <<"all">>, [ 219 | {<<"federation-upstream-set">>, <<"upstream">>}]) 220 | end, upstream_downstream() ++ [q(<<"fed.downstream2">>)]). 221 | 222 | dynamic_plugin_stop_start(Config) -> 223 | DownQ2 = <<"fed.downstream2">>, 224 | with_ch(Config, 225 | fun (Ch) -> 226 | timer:sleep(?INITIAL_WAIT), 227 | UpQ = <<"upstream">>, 228 | DownQ1 = <<"fed.downstream">>, 229 | expect_federation(Ch, UpQ, DownQ1, ?EXPECT_FEDERATION_TIMEOUT), 230 | expect_federation(Ch, UpQ, DownQ2, ?EXPECT_FEDERATION_TIMEOUT), 231 | 232 | %% Disable the plugin, the link disappears 233 | ok = rabbit_ct_broker_helpers:disable_plugin(Config, 0, "rabbitmq_federation"), 234 | 235 | expect_no_federation(Ch, UpQ, DownQ1), 236 | expect_no_federation(Ch, UpQ, DownQ2), 237 | 238 | declare_queue(Ch, q(DownQ1)), 239 | declare_queue(Ch, q(DownQ2)), 240 | ok = rabbit_ct_broker_helpers:enable_plugin(Config, 0, "rabbitmq_federation"), 241 | 242 | %% Declare a queue then re-enable the plugin, the links appear 243 | wait_for_federation( 244 | 90, 245 | fun() -> 246 | Status = rabbit_ct_broker_helpers:rpc(Config, 0, 247 | rabbit_federation_status, status, []), 248 | L = [ 249 | Entry || Entry <- Status, 250 | proplists:get_value(queue, Entry) =:= DownQ1 orelse 251 | proplists:get_value(queue, Entry) =:= DownQ2, 252 | proplists:get_value(upstream_queue, Entry) =:= UpQ, 253 | proplists:get_value(status, Entry) =:= running 254 | ], 255 | length(L) =:= 2 256 | end), 257 | expect_federation(Ch, UpQ, DownQ1, 120000) 258 | end, upstream_downstream() ++ [q(DownQ2)]). 259 | 260 | restart_upstream(Config) -> 261 | [Rabbit, Hare] = rabbit_ct_broker_helpers:get_node_configs(Config, 262 | nodename), 263 | set_policy_upstream(Config, Rabbit, <<"^test$">>, 264 | rabbit_ct_broker_helpers:node_uri(Config, Hare), []), 265 | 266 | Downstream = rabbit_ct_client_helpers:open_channel(Config, Rabbit), 267 | Upstream = rabbit_ct_client_helpers:open_channel(Config, Hare), 268 | 269 | declare_queue(Upstream, q(<<"test">>)), 270 | declare_queue(Downstream, q(<<"test">>)), 271 | Seq = lists:seq(1, 100), 272 | [publish(Upstream, <<>>, <<"test">>, <<"bulk">>) || _ <- Seq], 273 | expect(Upstream, <<"test">>, repeat(25, <<"bulk">>)), 274 | expect(Downstream, <<"test">>, repeat(25, <<"bulk">>)), 275 | 276 | rabbit_ct_client_helpers:close_channels_and_connection(Config, Hare), 277 | ok = rabbit_ct_broker_helpers:restart_node(Config, Hare), 278 | Upstream2 = rabbit_ct_client_helpers:open_channel(Config, Hare), 279 | 280 | expect(Upstream2, <<"test">>, repeat(25, <<"bulk">>)), 281 | expect(Downstream, <<"test">>, repeat(25, <<"bulk">>)), 282 | expect_empty(Upstream2, <<"test">>), 283 | expect_empty(Downstream, <<"test">>), 284 | 285 | ok. 286 | 287 | %upstream_has_no_federation(Config) -> 288 | % %% TODO 289 | % ok. 290 | 291 | %%---------------------------------------------------------------------------- 292 | repeat(Count, Item) -> [Item || _ <- lists:seq(1, Count)]. 293 | 294 | %%---------------------------------------------------------------------------- 295 | 296 | publish(Ch, X, Key, Payload) when is_binary(Payload) -> 297 | publish(Ch, X, Key, #amqp_msg{payload = Payload}); 298 | 299 | publish(Ch, X, Key, Msg = #amqp_msg{}) -> 300 | amqp_channel:call(Ch, #'basic.publish'{exchange = X, 301 | routing_key = Key}, Msg). 302 | 303 | publish_expect(Ch, X, Key, Q, Payload) -> 304 | publish(Ch, X, Key, Payload), 305 | expect(Ch, Q, [Payload]). 306 | 307 | publish_expect(Ch, X, Key, Q, Payload, Timeout) -> 308 | publish(Ch, X, Key, Payload), 309 | expect(Ch, Q, [Payload], Timeout). 310 | 311 | %% Doubled due to our strange basic.get behaviour. 312 | expect_empty(Ch, Q) -> 313 | rabbit_federation_test_util:expect_empty(Ch, Q), 314 | rabbit_federation_test_util:expect_empty(Ch, Q). 315 | 316 | expect_federation(Ch, UpstreamQ, DownstreamQ) -> 317 | publish_expect(Ch, <<>>, UpstreamQ, DownstreamQ, <<"HELLO">>). 318 | 319 | expect_federation(Ch, UpstreamQ, DownstreamQ, Timeout) -> 320 | publish_expect(Ch, <<>>, UpstreamQ, DownstreamQ, <<"HELLO">>, Timeout). 321 | 322 | expect_no_federation(Ch, UpstreamQ, DownstreamQ) -> 323 | publish(Ch, <<>>, UpstreamQ, <<"HELLO">>), 324 | expect_empty(Ch, DownstreamQ), 325 | expect(Ch, UpstreamQ, [<<"HELLO">>]). 326 | 327 | upstream_downstream() -> 328 | [q(<<"upstream">>), q(<<"fed.downstream">>)]. 329 | -------------------------------------------------------------------------------- /test/rabbit_federation_status_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(rabbit_federation_status_SUITE). 9 | 10 | -include_lib("common_test/include/ct.hrl"). 11 | -include_lib("amqp_client/include/amqp_client.hrl"). 12 | 13 | -include("rabbit_federation.hrl"). 14 | 15 | -compile(export_all). 16 | 17 | -import(rabbit_federation_test_util, 18 | [expect/3, expect_empty/2, 19 | set_upstream/4, clear_upstream/3, set_upstream_set/4, 20 | set_policy/5, clear_policy/3, 21 | set_policy_upstream/5, set_policy_upstreams/4, 22 | no_plugins/1, with_ch/3]). 23 | 24 | all() -> 25 | [ 26 | {group, non_parallel_tests} 27 | ]. 28 | 29 | groups() -> 30 | [ 31 | {non_parallel_tests, [], [ 32 | exchange_status, 33 | queue_status, 34 | lookup_exchange_status, 35 | lookup_queue_status, 36 | lookup_bad_status 37 | ]} 38 | ]. 39 | 40 | suite() -> 41 | [{timetrap, {minutes, 5}}]. 42 | 43 | %% ------------------------------------------------------------------- 44 | %% Testsuite setup/teardown. 45 | %% ------------------------------------------------------------------- 46 | init_per_suite(Config) -> 47 | rabbit_ct_helpers:log_environment(), 48 | Config1 = rabbit_ct_helpers:set_config(Config, [ 49 | {rmq_nodename_suffix, ?MODULE} 50 | ]), 51 | rabbit_ct_helpers:run_setup_steps(Config1, 52 | rabbit_ct_broker_helpers:setup_steps() ++ 53 | rabbit_ct_client_helpers:setup_steps() ++ 54 | [fun rabbit_federation_test_util:setup_federation/1]). 55 | end_per_suite(Config) -> 56 | rabbit_ct_helpers:run_teardown_steps(Config, 57 | rabbit_ct_client_helpers:teardown_steps() ++ 58 | rabbit_ct_broker_helpers:teardown_steps()). 59 | 60 | init_per_group(_, Config) -> 61 | Config. 62 | 63 | end_per_group(_, Config) -> 64 | Config. 65 | 66 | init_per_testcase(Testcase, Config) -> 67 | rabbit_ct_helpers:testcase_started(Config, Testcase). 68 | 69 | end_per_testcase(Testcase, Config) -> 70 | rabbit_ct_helpers:testcase_finished(Config, Testcase). 71 | 72 | %% ------------------------------------------------------------------- 73 | %% Testcases. 74 | %% ------------------------------------------------------------------- 75 | exchange_status(Config) -> 76 | exchange_SUITE:with_ch( 77 | Config, 78 | fun (_Ch) -> 79 | [Link] = rabbit_ct_broker_helpers:rpc(Config, 0, 80 | rabbit_federation_status, status, []), 81 | true = is_binary(proplists:get_value(id, Link)) 82 | end, exchange_SUITE:upstream_downstream()). 83 | 84 | queue_status(Config) -> 85 | with_ch( 86 | Config, 87 | fun (_Ch) -> 88 | timer:sleep(3000), 89 | [Link] = rabbit_ct_broker_helpers:rpc(Config, 0, 90 | rabbit_federation_status, status, []), 91 | true = is_binary(proplists:get_value(id, Link)) 92 | end, queue_SUITE:upstream_downstream()). 93 | 94 | lookup_exchange_status(Config) -> 95 | exchange_SUITE:with_ch( 96 | Config, 97 | fun (_Ch) -> 98 | [Link] = rabbit_ct_broker_helpers:rpc(Config, 0, 99 | rabbit_federation_status, status, []), 100 | Id = proplists:get_value(id, Link), 101 | Props = rabbit_ct_broker_helpers:rpc(Config, 0, 102 | rabbit_federation_status, lookup, [Id]), 103 | lists:all(fun(K) -> lists:keymember(K, 1, Props) end, 104 | [key, uri, status, timestamp, id, supervisor, upstream]) 105 | end, exchange_SUITE:upstream_downstream()). 106 | 107 | lookup_queue_status(Config) -> 108 | with_ch( 109 | Config, 110 | fun (_Ch) -> 111 | timer:sleep(3000), 112 | [Link] = rabbit_ct_broker_helpers:rpc(Config, 0, 113 | rabbit_federation_status, status, []), 114 | Id = proplists:get_value(id, Link), 115 | Props = rabbit_ct_broker_helpers:rpc(Config, 0, 116 | rabbit_federation_status, lookup, [Id]), 117 | lists:all(fun(K) -> lists:keymember(K, 1, Props) end, 118 | [key, uri, status, timestamp, id, supervisor, upstream]) 119 | end, queue_SUITE:upstream_downstream()). 120 | 121 | lookup_bad_status(Config) -> 122 | with_ch( 123 | Config, 124 | fun (_Ch) -> 125 | timer:sleep(3000), 126 | not_found = rabbit_ct_broker_helpers:rpc( 127 | Config, 0, 128 | rabbit_federation_status, lookup, [<<"justmadeitup">>]) 129 | end, queue_SUITE:upstream_downstream()). 130 | -------------------------------------------------------------------------------- /test/rabbit_federation_test_util.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_federation_test_util). 9 | 10 | -include("rabbit_federation.hrl"). 11 | -include_lib("eunit/include/eunit.hrl"). 12 | -include_lib("amqp_client/include/amqp_client.hrl"). 13 | 14 | -compile(export_all). 15 | 16 | -import(rabbit_misc, [pget/2]). 17 | 18 | setup_federation(Config) -> 19 | rabbit_ct_broker_helpers:set_parameter(Config, 0, 20 | <<"federation-upstream">>, <<"localhost">>, [ 21 | {<<"uri">>, rabbit_ct_broker_helpers:node_uri(Config, 0)}, 22 | {<<"consumer-tag">>, <<"fed.tag">>}]), 23 | 24 | rabbit_ct_broker_helpers:set_parameter(Config, 0, 25 | <<"federation-upstream">>, <<"local5673">>, [ 26 | {<<"uri">>, <<"amqp://localhost:1">>}]), 27 | 28 | rabbit_ct_broker_helpers:set_parameter(Config, 0, 29 | <<"federation-upstream-set">>, <<"upstream">>, [ 30 | [ 31 | {<<"upstream">>, <<"localhost">>}, 32 | {<<"exchange">>, <<"upstream">>}, 33 | {<<"queue">>, <<"upstream">>} 34 | ] 35 | ]), 36 | 37 | rabbit_ct_broker_helpers:set_parameter(Config, 0, 38 | <<"federation-upstream-set">>, <<"upstream2">>, [ 39 | [ 40 | {<<"upstream">>, <<"localhost">>}, 41 | {<<"exchange">>, <<"upstream2">>}, 42 | {<<"queue">>, <<"upstream2">>} 43 | ] 44 | ]), 45 | 46 | rabbit_ct_broker_helpers:set_parameter(Config, 0, 47 | <<"federation-upstream-set">>, <<"localhost">>, [ 48 | [{<<"upstream">>, <<"localhost">>}] 49 | ]), 50 | 51 | rabbit_ct_broker_helpers:set_parameter(Config, 0, 52 | <<"federation-upstream-set">>, <<"upstream12">>, [ 53 | [ 54 | {<<"upstream">>, <<"localhost">>}, 55 | {<<"exchange">>, <<"upstream">>}, 56 | {<<"queue">>, <<"upstream">>} 57 | ], [ 58 | {<<"upstream">>, <<"localhost">>}, 59 | {<<"exchange">>, <<"upstream2">>}, 60 | {<<"queue">>, <<"upstream2">>} 61 | ] 62 | ]), 63 | 64 | rabbit_ct_broker_helpers:set_parameter(Config, 0, 65 | <<"federation-upstream-set">>, <<"one">>, [ 66 | [ 67 | {<<"upstream">>, <<"localhost">>}, 68 | {<<"exchange">>, <<"one">>}, 69 | {<<"queue">>, <<"one">>} 70 | ] 71 | ]), 72 | 73 | rabbit_ct_broker_helpers:set_parameter(Config, 0, 74 | <<"federation-upstream-set">>, <<"two">>, [ 75 | [ 76 | {<<"upstream">>, <<"localhost">>}, 77 | {<<"exchange">>, <<"two">>}, 78 | {<<"queue">>, <<"two">>} 79 | ] 80 | ]), 81 | 82 | rabbit_ct_broker_helpers:set_parameter(Config, 0, 83 | <<"federation-upstream-set">>, <<"upstream5673">>, [ 84 | [ 85 | {<<"upstream">>, <<"local5673">>}, 86 | {<<"exchange">>, <<"upstream">>} 87 | ] 88 | ]), 89 | 90 | rabbit_ct_broker_helpers:set_policy(Config, 0, 91 | <<"fed">>, <<"^fed\.">>, <<"all">>, [ 92 | {<<"federation-upstream-set">>, <<"upstream">>}]), 93 | 94 | rabbit_ct_broker_helpers:set_policy(Config, 0, 95 | <<"fed12">>, <<"^fed12\.">>, <<"all">>, [ 96 | {<<"federation-upstream-set">>, <<"upstream12">>}]), 97 | 98 | rabbit_ct_broker_helpers:set_policy(Config, 0, 99 | <<"one">>, <<"^two$">>, <<"all">>, [ 100 | {<<"federation-upstream-set">>, <<"one">>}]), 101 | 102 | rabbit_ct_broker_helpers:set_policy(Config, 0, 103 | <<"two">>, <<"^one$">>, <<"all">>, [ 104 | {<<"federation-upstream-set">>, <<"two">>}]), 105 | 106 | rabbit_ct_broker_helpers:set_policy(Config, 0, 107 | <<"hare">>, <<"^hare\.">>, <<"all">>, [ 108 | {<<"federation-upstream-set">>, <<"upstream5673">>}]), 109 | 110 | rabbit_ct_broker_helpers:set_policy(Config, 0, 111 | <<"all">>, <<"^all\.">>, <<"all">>, [ 112 | {<<"federation-upstream-set">>, <<"all">>}]), 113 | 114 | rabbit_ct_broker_helpers:set_policy(Config, 0, 115 | <<"new">>, <<"^new\.">>, <<"all">>, [ 116 | {<<"federation-upstream-set">>, <<"new-set">>}]), 117 | Config. 118 | 119 | setup_down_federation(Config) -> 120 | rabbit_ct_broker_helpers:set_parameter( 121 | Config, 0, <<"federation-upstream">>, <<"broken-bunny">>, 122 | [{<<"uri">>, <<"amqp://broken-bunny">>}, 123 | {<<"reconnect-delay">>, 600000}]), 124 | rabbit_ct_broker_helpers:set_parameter( 125 | Config, 0, <<"federation-upstream">>, <<"localhost">>, 126 | [{<<"uri">>, rabbit_ct_broker_helpers:node_uri(Config, 0)}]), 127 | rabbit_ct_broker_helpers:set_parameter( 128 | Config, 0, 129 | <<"federation-upstream-set">>, <<"upstream">>, 130 | [[{<<"upstream">>, <<"localhost">>}, 131 | {<<"exchange">>, <<"upstream">>}, 132 | {<<"queue">>, <<"upstream">>}], 133 | [{<<"upstream">>, <<"broken-bunny">>}, 134 | {<<"exchange">>, <<"upstream">>}, 135 | {<<"queue">>, <<"upstream">>}]]), 136 | rabbit_ct_broker_helpers:set_policy( 137 | Config, 0, 138 | <<"fed">>, <<"^fed\.">>, <<"all">>, [{<<"federation-upstream-set">>, <<"upstream">>}]), 139 | rabbit_ct_broker_helpers:set_policy( 140 | Config, 0, 141 | <<"fed">>, <<"^fed\.">>, <<"all">>, [{<<"federation-upstream-set">>, <<"upstream">>}]), 142 | Config. 143 | 144 | wait_for_federation(Retries, Fun) -> 145 | case Fun() of 146 | true -> 147 | ok; 148 | false when Retries > 0 -> 149 | timer:sleep(1000), 150 | wait_for_federation(Retries - 1, Fun); 151 | false -> 152 | throw({timeout_while_waiting_for_federation, Fun}) 153 | end. 154 | 155 | expect(Ch, Q, Fun) when is_function(Fun) -> 156 | amqp_channel:subscribe(Ch, #'basic.consume'{queue = Q, 157 | no_ack = true}, self()), 158 | CTag = receive 159 | #'basic.consume_ok'{consumer_tag = CT} -> CT 160 | end, 161 | Fun(), 162 | amqp_channel:call(Ch, #'basic.cancel'{consumer_tag = CTag}); 163 | 164 | expect(Ch, Q, Payloads) -> 165 | expect(Ch, Q, fun() -> expect(Payloads) end). 166 | 167 | expect(Ch, Q, Payloads, Timeout) -> 168 | expect(Ch, Q, fun() -> expect(Payloads, Timeout) end). 169 | 170 | expect([]) -> 171 | ok; 172 | expect(Payloads) -> 173 | expect(Payloads, 60000). 174 | 175 | expect([], _Timeout) -> 176 | ok; 177 | expect(Payloads, Timeout) -> 178 | receive 179 | {#'basic.deliver'{}, #amqp_msg{payload = Payload}} -> 180 | case lists:member(Payload, Payloads) of 181 | true -> 182 | ct:pal("Consumed a message: ~p", [Payload]), 183 | expect(Payloads -- [Payload], Timeout); 184 | false -> ?assert(false, rabbit_misc:format("received an unexpected payload ~p", [Payload])) 185 | end 186 | after Timeout -> 187 | ?assert(false, rabbit_misc:format("Did not receive expected payloads ~p in time", [Payloads])) 188 | end. 189 | 190 | expect_empty(Ch, Q) -> 191 | ?assertMatch(#'basic.get_empty'{}, 192 | amqp_channel:call(Ch, #'basic.get'{ queue = Q })). 193 | 194 | set_upstream(Config, Node, Name, URI) -> 195 | set_upstream(Config, Node, Name, URI, []). 196 | 197 | set_upstream(Config, Node, Name, URI, Extra) -> 198 | rabbit_ct_broker_helpers:set_parameter(Config, Node, 199 | <<"federation-upstream">>, Name, [{<<"uri">>, URI} | Extra]). 200 | 201 | set_upstream_in_vhost(Config, Node, VirtualHost, Name, URI) -> 202 | set_upstream_in_vhost(Config, Node, VirtualHost, Name, URI, []). 203 | 204 | set_upstream_in_vhost(Config, Node, VirtualHost, Name, URI, Extra) -> 205 | rabbit_ct_broker_helpers:set_parameter(Config, Node, VirtualHost, 206 | <<"federation-upstream">>, Name, [{<<"uri">>, URI} | Extra]). 207 | 208 | clear_upstream(Config, Node, Name) -> 209 | rabbit_ct_broker_helpers:clear_parameter(Config, Node, 210 | <<"federation-upstream">>, Name). 211 | 212 | set_upstream_set(Config, Node, Name, Set) -> 213 | rabbit_ct_broker_helpers:set_parameter(Config, Node, 214 | <<"federation-upstream-set">>, Name, 215 | [[{<<"upstream">>, UStream} | Extra] || {UStream, Extra} <- Set]). 216 | 217 | clear_upstream_set(Config, Node, Name) -> 218 | rabbit_ct_broker_helpers:clear_parameter(Config, Node, 219 | <<"federation-upstream-set">>, Name). 220 | 221 | set_policy(Config, Node, Name, Pattern, UpstreamSet) -> 222 | rabbit_ct_broker_helpers:set_policy(Config, Node, 223 | Name, Pattern, <<"all">>, 224 | [{<<"federation-upstream-set">>, UpstreamSet}]). 225 | 226 | set_policy_pattern(Config, Node, Name, Pattern, Regex) -> 227 | rabbit_ct_broker_helpers:set_policy(Config, Node, 228 | Name, Pattern, <<"all">>, 229 | [{<<"federation-upstream-pattern">>, Regex}]). 230 | 231 | clear_policy(Config, Node, Name) -> 232 | rabbit_ct_broker_helpers:clear_policy(Config, Node, Name). 233 | 234 | set_policy_upstream(Config, Node, Pattern, URI, Extra) -> 235 | set_policy_upstreams(Config, Node, Pattern, [{URI, Extra}]). 236 | 237 | set_policy_upstreams(Config, Node, Pattern, URIExtras) -> 238 | put(upstream_num, 1), 239 | [set_upstream(Config, Node, gen_upstream_name(), URI, Extra) 240 | || {URI, Extra} <- URIExtras], 241 | set_policy(Config, Node, Pattern, Pattern, <<"all">>). 242 | 243 | gen_upstream_name() -> 244 | list_to_binary("upstream-" ++ integer_to_list(next_upstream_num())). 245 | 246 | next_upstream_num() -> 247 | R = get(upstream_num) + 1, 248 | put(upstream_num, R), 249 | R. 250 | 251 | %% Make sure that even though multiple nodes are in a single 252 | %% distributed system, we still keep all our process groups separate. 253 | disambiguate(Config) -> 254 | rabbit_ct_broker_helpers:rpc_all(Config, 255 | application, set_env, 256 | [rabbitmq_federation, pgroup_name_cluster_id, true]), 257 | Config. 258 | 259 | no_plugins(Cfg) -> 260 | [{K, case K of 261 | plugins -> none; 262 | _ -> V 263 | end} || {K, V} <- Cfg]. 264 | 265 | %%---------------------------------------------------------------------------- 266 | 267 | all_federation_links(Config, Node) -> 268 | rabbit_ct_broker_helpers:rpc(Config, Node, rabbit_federation_status, status, []). 269 | 270 | federation_links_in_vhost(Config, Node, VirtualHost) -> 271 | Links = rabbit_ct_broker_helpers:rpc(Config, Node, rabbit_federation_status, status, []), 272 | lists:filter( 273 | fun(Link) -> 274 | VirtualHost =:= proplists:get_value(vhost, Link) 275 | end, Links). 276 | 277 | status_fields(Prop, Statuses) -> 278 | lists:usort( 279 | lists:map( 280 | fun(Link) -> proplists:get_value(Prop, Link) end, 281 | Statuses)). 282 | 283 | assert_status(Config, Node, XorQs, Names) -> 284 | rabbit_ct_broker_helpers:rpc(Config, Node, 285 | ?MODULE, assert_status1, [XorQs, Names]). 286 | 287 | assert_status1(XorQs, Names) -> 288 | [begin 289 | ct:pal("links(XorQ) for ~p: ~p", [XorQ, links(XorQ)]) 290 | end || XorQ <- XorQs], 291 | Links = lists:append([links(XorQ) || XorQ <- XorQs]), 292 | Remaining = lists:foldl(fun (Link, Status) -> 293 | assert_link_status(Link, Status, Names) 294 | end, rabbit_federation_status:status(), Links), 295 | ?assertEqual([], Remaining), 296 | ok. 297 | 298 | assert_link_status({DXorQNameBin, UpstreamName, UXorQNameBin}, Status, 299 | {TypeName, UpstreamTypeName}) -> 300 | {This, Rest} = lists:partition( 301 | fun(St) -> 302 | pget(upstream, St) =:= UpstreamName andalso 303 | pget(TypeName, St) =:= DXorQNameBin andalso 304 | pget(UpstreamTypeName, St) =:= UXorQNameBin 305 | end, Status), 306 | ?assertMatch([_], This), 307 | Rest. 308 | 309 | links(#'exchange.declare'{exchange = Name}) -> 310 | case rabbit_exchange:lookup(xr(Name)) of 311 | {ok, X} -> 312 | case rabbit_policy:get(<<"federation-upstream-set">>, X) of 313 | undefined -> 314 | case rabbit_policy:get(<<"federation-upstream-pattern">>, X) of 315 | undefined -> []; 316 | Regex -> 317 | [{Name, U#upstream.name, U#upstream.exchange_name} || 318 | U <- rabbit_federation_upstream:from_pattern(Regex, X)] 319 | end; 320 | Set -> 321 | [{Name, U#upstream.name, U#upstream.exchange_name} || 322 | U <- rabbit_federation_upstream:from_set(Set, X)] 323 | end; 324 | {error, not_found} -> 325 | [] 326 | end. 327 | 328 | xr(Name) -> rabbit_misc:r(<<"/">>, exchange, Name). 329 | 330 | with_ch(Config, Fun, Qs) -> 331 | Ch = rabbit_ct_client_helpers:open_channel(Config, 0), 332 | declare_all(Ch, Qs), 333 | %% Clean up queues even after test failure. 334 | try 335 | Fun(Ch) 336 | after 337 | delete_all(Ch, Qs), 338 | rabbit_ct_client_helpers:close_channel(Ch) 339 | end, 340 | ok. 341 | 342 | declare_all(Ch, Qs) -> [declare_queue(Ch, Q) || Q <- Qs]. 343 | delete_all(Ch, Qs) -> 344 | [delete_queue(Ch, Q) || #'queue.declare'{queue = Q} <- Qs]. 345 | 346 | declare_queue(Ch, Q) -> 347 | amqp_channel:call(Ch, Q). 348 | 349 | delete_queue(Ch, Q) -> 350 | amqp_channel:call(Ch, #'queue.delete'{queue = Q}). 351 | 352 | q(Name) -> 353 | #'queue.declare'{queue = Name, 354 | durable = true}. 355 | -------------------------------------------------------------------------------- /test/restart_federation_link_command_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(restart_federation_link_command_SUITE). 9 | 10 | -include_lib("common_test/include/ct.hrl"). 11 | -include_lib("amqp_client/include/amqp_client.hrl"). 12 | 13 | -compile(export_all). 14 | 15 | -define(CMD, 'Elixir.RabbitMQ.CLI.Ctl.Commands.RestartFederationLinkCommand'). 16 | 17 | all() -> 18 | [ 19 | {group, federated_down} 20 | ]. 21 | 22 | groups() -> 23 | [ 24 | {federated_down, [], [ 25 | run, 26 | run_not_found, 27 | output 28 | ]} 29 | ]. 30 | 31 | %% ------------------------------------------------------------------- 32 | %% Testsuite setup/teardown. 33 | %% ------------------------------------------------------------------- 34 | 35 | init_per_suite(Config) -> 36 | rabbit_ct_helpers:log_environment(), 37 | Config1 = rabbit_ct_helpers:set_config(Config, [ 38 | {rmq_nodename_suffix, ?MODULE} 39 | ]), 40 | Config2 = rabbit_ct_helpers:run_setup_steps(Config1, 41 | rabbit_ct_broker_helpers:setup_steps() ++ 42 | rabbit_ct_client_helpers:setup_steps()), 43 | Config2. 44 | 45 | end_per_suite(Config) -> 46 | rabbit_ct_helpers:run_teardown_steps(Config, 47 | rabbit_ct_client_helpers:teardown_steps() ++ 48 | rabbit_ct_broker_helpers:teardown_steps()). 49 | 50 | init_per_group(federated_down, Config) -> 51 | rabbit_federation_test_util:setup_down_federation(Config), 52 | Config; 53 | init_per_group(_, Config) -> 54 | Config. 55 | 56 | end_per_group(_, Config) -> 57 | Config. 58 | 59 | init_per_testcase(Testcase, Config) -> 60 | rabbit_ct_helpers:testcase_started(Config, Testcase). 61 | 62 | end_per_testcase(Testcase, Config) -> 63 | rabbit_ct_helpers:testcase_finished(Config, Testcase). 64 | 65 | %% ------------------------------------------------------------------- 66 | %% Testcases. 67 | %% ------------------------------------------------------------------- 68 | run_not_federated(Config) -> 69 | [A] = rabbit_ct_broker_helpers:get_node_configs(Config, nodename), 70 | Opts = #{node => A}, 71 | {stream, []} = ?CMD:run([], Opts#{'only-down' => false}). 72 | 73 | output_not_federated(Config) -> 74 | [A] = rabbit_ct_broker_helpers:get_node_configs(Config, nodename), 75 | Opts = #{node => A}, 76 | {stream, []} = ?CMD:output({stream, []}, Opts). 77 | 78 | run(Config) -> 79 | [A] = rabbit_ct_broker_helpers:get_node_configs(Config, nodename), 80 | Opts = #{node => A}, 81 | rabbit_federation_test_util:with_ch( 82 | Config, 83 | fun(_) -> 84 | timer:sleep(3000), 85 | [Link | _] = rabbit_ct_broker_helpers:rpc(Config, 0, 86 | rabbit_federation_status, status, []), 87 | Id = proplists:get_value(id, Link), 88 | ok = ?CMD:run([Id], Opts) 89 | end, 90 | [rabbit_federation_test_util:q(<<"upstream">>), 91 | rabbit_federation_test_util:q(<<"fed.downstream">>)]). 92 | 93 | run_not_found(Config) -> 94 | [A] = rabbit_ct_broker_helpers:get_node_configs(Config, nodename), 95 | Opts = #{node => A}, 96 | {error, _ErrorMsg} = ?CMD:run([<<"MakingItUp">>], Opts). 97 | 98 | output(Config) -> 99 | [A] = rabbit_ct_broker_helpers:get_node_configs(Config, nodename), 100 | Opts = #{node => A}, 101 | ok = ?CMD:output(ok, Opts). 102 | -------------------------------------------------------------------------------- /test/unit_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) 2019-2020 VMware, Inc. or its affiliates. All rights reserved. 6 | %% 7 | 8 | -module(unit_SUITE). 9 | -include_lib("common_test/include/ct.hrl"). 10 | -include_lib("eunit/include/eunit.hrl"). 11 | -include_lib("amqp_client/include/amqp_client.hrl"). 12 | -include("rabbit_federation.hrl"). 13 | 14 | -compile(export_all). 15 | 16 | all() -> [ 17 | obfuscate_upstream, 18 | obfuscate_upstream_params_network, 19 | obfuscate_upstream_params_network_with_char_list_password_value, 20 | obfuscate_upstream_params_direct 21 | ]. 22 | 23 | init_per_suite(Config) -> 24 | application:ensure_all_started(credentials_obfuscation), 25 | Config. 26 | 27 | end_per_suite(Config) -> 28 | Config. 29 | 30 | obfuscate_upstream(_Config) -> 31 | Upstream = #upstream{uris = [<<"amqp://guest:password@localhost">>]}, 32 | ObfuscatedUpstream = rabbit_federation_util:obfuscate_upstream(Upstream), 33 | ?assertEqual(Upstream, rabbit_federation_util:deobfuscate_upstream(ObfuscatedUpstream)), 34 | ok. 35 | 36 | obfuscate_upstream_params_network(_Config) -> 37 | UpstreamParams = #upstream_params{ 38 | uri = <<"amqp://guest:password@localhost">>, 39 | params = #amqp_params_network{password = <<"password">>} 40 | }, 41 | ObfuscatedUpstreamParams = rabbit_federation_util:obfuscate_upstream_params(UpstreamParams), 42 | ?assertEqual(UpstreamParams, rabbit_federation_util:deobfuscate_upstream_params(ObfuscatedUpstreamParams)), 43 | ok. 44 | 45 | obfuscate_upstream_params_network_with_char_list_password_value(_Config) -> 46 | Input = #upstream_params{ 47 | uri = <<"amqp://guest:password@localhost">>, 48 | params = #amqp_params_network{password = "password"} 49 | }, 50 | Output = #upstream_params{ 51 | uri = <<"amqp://guest:password@localhost">>, 52 | params = #amqp_params_network{password = <<"password">>} 53 | }, 54 | ObfuscatedUpstreamParams = rabbit_federation_util:obfuscate_upstream_params(Input), 55 | ?assertEqual(Output, rabbit_federation_util:deobfuscate_upstream_params(ObfuscatedUpstreamParams)), 56 | ok. 57 | 58 | obfuscate_upstream_params_direct(_Config) -> 59 | UpstreamParams = #upstream_params{ 60 | uri = <<"amqp://guest:password@localhost">>, 61 | params = #amqp_params_direct{password = <<"password">>} 62 | }, 63 | ObfuscatedUpstreamParams = rabbit_federation_util:obfuscate_upstream_params(UpstreamParams), 64 | ?assertEqual(UpstreamParams, rabbit_federation_util:deobfuscate_upstream_params(ObfuscatedUpstreamParams)), 65 | ok. 66 | -------------------------------------------------------------------------------- /test/unit_inbroker_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(unit_inbroker_SUITE). 9 | 10 | -include_lib("common_test/include/ct.hrl"). 11 | -include_lib("rabbit_common/include/rabbit.hrl"). 12 | -include_lib("eunit/include/eunit.hrl"). 13 | 14 | -include("rabbit_federation.hrl"). 15 | 16 | -compile(export_all). 17 | 18 | -define(US_NAME, <<"upstream">>). 19 | -define(DS_NAME, <<"fed.downstream">>). 20 | 21 | all() -> 22 | [ 23 | {group, non_parallel_tests} 24 | ]. 25 | 26 | groups() -> 27 | [ 28 | {non_parallel_tests, [], [ 29 | serialisation, 30 | scratch_space, 31 | remove_credentials, 32 | get_connection_name, 33 | upstream_validation, 34 | upstream_set_validation 35 | ]} 36 | ]. 37 | 38 | %% ------------------------------------------------------------------- 39 | %% Testsuite setup/teardown. 40 | %% ------------------------------------------------------------------- 41 | 42 | init_per_suite(Config) -> 43 | rabbit_ct_helpers:log_environment(), 44 | Config1 = rabbit_ct_helpers:set_config(Config, [ 45 | {rmq_nodename_suffix, ?MODULE} 46 | ]), 47 | rabbit_ct_helpers:run_setup_steps(Config1, 48 | rabbit_ct_broker_helpers:setup_steps() ++ 49 | rabbit_ct_client_helpers:setup_steps()). 50 | 51 | end_per_suite(Config) -> 52 | rabbit_ct_helpers:run_teardown_steps(Config, 53 | rabbit_ct_client_helpers:teardown_steps() ++ 54 | rabbit_ct_broker_helpers:teardown_steps()). 55 | 56 | init_per_group(_, Config) -> 57 | Config. 58 | 59 | end_per_group(_, Config) -> 60 | Config. 61 | 62 | init_per_testcase(Testcase, Config) -> 63 | rabbit_ct_helpers:testcase_started(Config, Testcase). 64 | 65 | end_per_testcase(Testcase, Config) -> 66 | rabbit_ct_helpers:testcase_finished(Config, Testcase). 67 | 68 | %% ------------------------------------------------------------------- 69 | %% Testcases. 70 | %% ------------------------------------------------------------------- 71 | 72 | %% Test that we apply binding changes in the correct order even when 73 | %% they arrive out of order. 74 | serialisation(Config) -> 75 | ok = rabbit_ct_broker_helpers:rpc(Config, 0, 76 | ?MODULE, serialisation1, []). 77 | 78 | serialisation1() -> 79 | with_exchanges( 80 | fun(X) -> 81 | [B1, B2, B3] = [b(K) || K <- [<<"1">>, <<"2">>, <<"3">>]], 82 | remove_bindings(4, X, [B1, B3]), 83 | add_binding(5, X, B1), 84 | add_binding(1, X, B1), 85 | add_binding(2, X, B2), 86 | add_binding(3, X, B3), 87 | %% List of lists because one for each link 88 | Keys = rabbit_federation_exchange_link:list_routing_keys( 89 | X#exchange.name), 90 | [[<<"1">>, <<"2">>]] =:= Keys 91 | end). 92 | 93 | scratch_space(Config) -> 94 | ok = rabbit_ct_broker_helpers:rpc(Config, 0, 95 | ?MODULE, scratch_space1, []). 96 | 97 | scratch_space1() -> 98 | A = <<"A">>, 99 | B = <<"B">>, 100 | DB = rabbit_federation_db, 101 | with_exchanges( 102 | fun(#exchange{name = N}) -> 103 | DB:set_active_suffix(N, upstream(x), A), 104 | DB:set_active_suffix(N, upstream(y), A), 105 | DB:prune_scratch(N, [upstream(y), upstream(z)]), 106 | DB:set_active_suffix(N, upstream(y), B), 107 | DB:set_active_suffix(N, upstream(z), A), 108 | none = DB:get_active_suffix(N, upstream(x), none), 109 | B = DB:get_active_suffix(N, upstream(y), none), 110 | A = DB:get_active_suffix(N, upstream(z), none) 111 | end). 112 | 113 | remove_credentials(Config) -> 114 | Test0 = fun (In, Exp) -> 115 | Act = rabbit_ct_broker_helpers:rpc(Config, 0, 116 | rabbit_federation_upstream, remove_credentials, [In]), 117 | Exp = Act 118 | end, 119 | Cat = fun (Bs) -> 120 | list_to_binary(lists:append([binary_to_list(B) || B <- Bs])) 121 | end, 122 | Test = fun (Scheme, Rest) -> 123 | Exp = Cat([Scheme, Rest]), 124 | Test0(Exp, Exp), 125 | Test0(Cat([Scheme, <<"user@">>, Rest]), Exp), 126 | Test0(Cat([Scheme, <<"user:pass@">>, Rest]), Exp) 127 | end, 128 | Test(<<"amqp://">>, <<"">>), 129 | Test(<<"amqp://">>, <<"localhost">>), 130 | Test(<<"amqp://">>, <<"localhost/">>), 131 | Test(<<"amqp://">>, <<"localhost/foo">>), 132 | Test(<<"amqp://">>, <<"localhost:5672">>), 133 | Test(<<"amqp://">>, <<"localhost:5672/foo">>), 134 | Test(<<"amqps://">>, <<"localhost:5672/%2f">>), 135 | ok. 136 | 137 | get_connection_name(Config) -> 138 | Amqqueue = rabbit_ct_broker_helpers:rpc( 139 | Config, 0, 140 | amqqueue, new, [rabbit_misc:r(<<"/">>, queue, <<"queue">>), 141 | self(), 142 | false, 143 | false, 144 | none, 145 | [], 146 | undefined, 147 | #{}, 148 | classic]), 149 | AmqqueueWithPolicy = amqqueue:set_policy(Amqqueue, [{name, <<"my.federation.policy">>}]), 150 | AmqqueueWithEmptyPolicy = amqqueue:set_policy(Amqqueue, []), 151 | 152 | 153 | <<"Federation link (upstream: my.upstream, policy: my.federation.policy)">> = rabbit_federation_link_util:get_connection_name( 154 | #upstream{name = <<"my.upstream">>}, 155 | #upstream_params{x_or_q = AmqqueueWithPolicy} 156 | ), 157 | <<"Federation link (upstream: my.upstream, policy: my.federation.policy)">> = rabbit_federation_link_util:get_connection_name( 158 | #upstream{name = <<"my.upstream">>}, 159 | #upstream_params{x_or_q = #exchange{policy = [{name, <<"my.federation.policy">>}]}} 160 | ), 161 | <<"Federation link">> = rabbit_federation_link_util:get_connection_name( 162 | #upstream{}, 163 | #upstream_params{x_or_q = AmqqueueWithEmptyPolicy} 164 | ), 165 | <<"Federation link">> = rabbit_federation_link_util:get_connection_name( 166 | #upstream{}, 167 | #upstream_params{x_or_q = #exchange{policy = []}} 168 | ), 169 | <<"Federation link">> = rabbit_federation_link_util:get_connection_name( 170 | whatever, 171 | whatever 172 | ), 173 | ok. 174 | 175 | upstream_set_validation(_Config) -> 176 | ?assertEqual(rabbit_federation_parameters:validate(<<"/">>, <<"federation-upstream-set">>, 177 | <<"a-name">>, 178 | [[{<<"upstream">>, <<"devtest1">>}], 179 | [{<<"upstream">>, <<"devtest2">>}]], 180 | <<"acting-user">>), 181 | [[ok], [ok]]), 182 | ?assertEqual(rabbit_federation_parameters:validate(<<"/">>, <<"federation-upstream-set">>, 183 | <<"a-name">>, 184 | [#{<<"upstream">> => <<"devtest3">>}, 185 | #{<<"upstream">> => <<"devtest4">>}], 186 | <<"acting-user">>), 187 | [[ok], [ok]]), 188 | ok. 189 | 190 | upstream_validation(_Config) -> 191 | ?assertEqual(rabbit_federation_parameters:validate(<<"/">>, <<"federation-upstream">>, 192 | <<"a-name">>, 193 | [{<<"uri">>, <<"amqp://127.0.0.1/%2f">>}], 194 | <<"acting-user">>), 195 | [ok]), 196 | ?assertEqual(rabbit_federation_parameters:validate(<<"/">>, <<"federation-upstream">>, 197 | <<"a-name">>, 198 | #{<<"uri">> => <<"amqp://127.0.0.1/%2f">>}, 199 | <<"acting-user">>), 200 | [ok]), 201 | ok. 202 | 203 | with_exchanges(Fun) -> 204 | rabbit_exchange:declare(r(?US_NAME), fanout, false, false, false, [], 205 | <<"acting-user">>), 206 | X = rabbit_exchange:declare(r(?DS_NAME), fanout, false, false, false, [], 207 | <<"acting-user">>), 208 | Fun(X), 209 | %% Delete downstream first or it will recreate the upstream 210 | rabbit_exchange:delete(r(?DS_NAME), false, <<"acting-user">>), 211 | rabbit_exchange:delete(r(?US_NAME), false, <<"acting-user">>), 212 | ok. 213 | 214 | add_binding(Ser, X, B) -> 215 | rabbit_federation_exchange:add_binding(transaction, X, B), 216 | rabbit_federation_exchange:add_binding(Ser, X, B). 217 | 218 | remove_bindings(Ser, X, Bs) -> 219 | rabbit_federation_exchange:remove_bindings(transaction, X, Bs), 220 | rabbit_federation_exchange:remove_bindings(Ser, X, Bs). 221 | 222 | r(Name) -> rabbit_misc:r(<<"/">>, exchange, Name). 223 | 224 | b(Key) -> 225 | #binding{source = ?DS_NAME, destination = <<"whatever">>, 226 | key = Key, args = []}. 227 | 228 | upstream(UpstreamName) -> 229 | #upstream{name = atom_to_list(UpstreamName), 230 | exchange_name = <<"upstream">>}. 231 | --------------------------------------------------------------------------------