├── .github ├── ISSUE_TEMPLATE.md └── PULL_REQUEST_TEMPLATE.md ├── .gitignore ├── .travis.yml ├── .travis.yml.patch ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── LICENSE-MPL-RabbitMQ ├── Makefile ├── README.md ├── RUNNING_TESTS.md ├── erlang.mk ├── include └── rabbit_peer_discovery_etcd.hrl ├── priv └── schema │ └── rabbitmq_peer_discovery_etcd.schema ├── rabbitmq-components.mk ├── src ├── rabbit_peer_discovery_etcd.erl ├── rabbitmq_peer_discovery_etcd.erl ├── rabbitmq_peer_discovery_etcd_app.erl ├── rabbitmq_peer_discovery_etcd_sup.erl └── rabbitmq_peer_discovery_etcd_v3_client.erl └── test ├── config_schema_SUITE.erl ├── config_schema_SUITE_data └── rabbitmq_peer_discovery_etcd.snippets ├── system_SUITE.erl ├── system_SUITE_data └── init-etcd.sh └── unit_SUITE.erl /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for using RabbitMQ. 2 | 3 | **STOP NOW AND READ THIS** BEFORE OPENING A NEW ISSUE ON GITHUB 4 | 5 | Unless you are CERTAIN you have found a reproducible problem in RabbitMQ or 6 | have a **specific, actionable** suggestion for our team, you must first ask 7 | your question or discuss your suspected issue on the mailing list: 8 | 9 | https://groups.google.com/forum/#!forum/rabbitmq-users 10 | 11 | Team RabbitMQ does not use GitHub issues for discussions, investigations, root 12 | cause analysis and so on. 13 | 14 | Please take the time to read the CONTRIBUTING.md document for instructions on 15 | how to effectively ask a question or report a suspected issue: 16 | 17 | https://github.com/rabbitmq/rabbitmq-server/blob/master/CONTRIBUTING.md#github-issues 18 | 19 | Following these rules **will save time** for both you and RabbitMQ's maintainers. 20 | 21 | Thank you. 22 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ## Proposed Changes 2 | 3 | Please describe the big picture of your changes here to communicate to the 4 | RabbitMQ team why we should accept this pull request. If it fixes a bug or 5 | resolves a feature request, be sure to link to that issue. 6 | 7 | A pull request that doesn't explain **why** the change was made has a much 8 | lower chance of being accepted. 9 | 10 | If English isn't your first language, don't worry about it and try to 11 | communicate the problem you are trying to solve to the best of your abilities. 12 | As long as we can understand the intent, it's all good. 13 | 14 | ## Types of Changes 15 | 16 | What types of changes does your code introduce to this project? 17 | _Put an `x` in the boxes that apply_ 18 | 19 | - [ ] Bug fix (non-breaking change which fixes issue #NNNN) 20 | - [ ] New feature (non-breaking change which adds functionality) 21 | - [ ] Breaking change (fix or feature that would cause an observable behavior change in existing systems) 22 | - [ ] Documentation improvements (corrections, new content, etc) 23 | - [ ] Cosmetic change (whitespace, formatting, etc) 24 | 25 | ## Checklist 26 | 27 | _Put an `x` in the boxes that apply. You can also fill these out after creating 28 | the PR. If you're unsure about any of them, don't hesitate to ask on the 29 | mailing list. We're here to help! This is simply a reminder of what we are 30 | going to look for before merging your code._ 31 | 32 | - [ ] I have read the `CONTRIBUTING.md` document 33 | - [ ] I have signed the CA (see https://cla.pivotal.io/sign/rabbitmq) 34 | - [ ] All tests pass locally with my changes 35 | - [ ] I have added tests that prove my fix is effective or that my feature works 36 | - [ ] I have added necessary documentation (if appropriate) 37 | - [ ] Any dependent changes have been merged and published in related repositories 38 | 39 | ## Further Comments 40 | 41 | If this is a relatively large or complex change, kick off the discussion by 42 | explaining why you chose the solution you did and what alternatives you 43 | considered, etc. 44 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | .sw? 3 | .*.sw? 4 | *.beam 5 | *.coverdata 6 | tags 7 | /.erlang.mk/ 8 | /cover/ 9 | /deps/ 10 | /doc/ 11 | /ebin/ 12 | /escript/ 13 | /escript.lock 14 | /git-revisions.txt 15 | /logs/ 16 | /plugins/ 17 | /plugins.lock 18 | /rebar.config 19 | /rebar.lock 20 | /sbin/ 21 | /sbin.lock 22 | /test/config_schema_SUITE_data/schema/ 23 | /test/ct.cover.spec 24 | /xrefr 25 | 26 | /rabbitmq_peer_discovery_etcd.d 27 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # vim:sw=2:et: 2 | 3 | os: linux 4 | dist: bionic 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 | - daemonize 17 | cache: 18 | apt: true 19 | env: 20 | global: 21 | - secure: AZevrggrLdOzDo8TeLHTLFyCp7En4ksZRy6gBqxb31/KmeDXooLsSfuvr0FhVmLRzCWFIdcZQk2j9ZHqvCz6OFfw4ZtOUoZmjwMP9FpiLcBQprWeiQhBXrhdSPe3ZY/oRFsMXaVIyc9+Z0dR32zgoYHJcn3KR6vjmzPaigzygStTc/Om6Xoal4AQghtOAJJ27OsayBQJyldknL6tVkFzB9oCOYBpu5HoLPSf+jzG/bF1BIHCre2mU7FASFAyNAQ+BlnyBBv5CY8hKTPTNL9FApErg0gUmmjZPRepZOJjAzFVBK2qOIAcSieTvAYzP2GkedZppyYuJLJnSuala+tSYKaQ/o9XZlIdFkoAX7ta3qk55JHCIzhSf9IE5SOfIS9DTaIWiQKSO6bvONMnqpy+ttQuCVeaFqROzxav6LcD0tJnHAP0to1pYKtJ0imxDxjFrGg3yrV2KkEAAAfc1Suo+B9vNzmarj580rspC5yZXmiIPE3tB42XvfIaeuJ2Ag3KCR3UyLIctl/HFBz1xTEYCX5mcWN5OQ/7YSmmL8xGc88hDg3BO8TJiHdxpi+lRxWztwzG6PTZrcn4G2aaEBjBgLP5Vq65MX2TEG5PFsgraecaLYszuZSNtHFEAZ+ITGjhuOfgn4IpjrasxFwD8vlwxxaoHNHgrdYLsiSn73WoIA0= 22 | - secure: Qqz6IymWGWwcne62cer87hsGD84ioz0PhG4hLjBObP+xfqVVHXMOENKUPFDEtb6+e/DHh3QyHXd88EyIRAgaiCJyzIed85rxsyUcnlc0qyeW+4G1shzG4V9H/d+/0hUUc7PFgquwcQ7YUWH3y7GZt+GBIthYc1nV8uiWOEEAu04OXCFxfuVZ9TPMrdKjlgYkjwCwfZ76tHxOh8gXWHmPFog5mWOwbwS/rQwCCpyCc0J6PK3EuMtL98b5GDFjSY4wQxvLCYt1Cv6kwCTZNoNr7ke4g+LmqksCZ9fCmdM99B6T0+iAGA+0OlKZRLUIv1ANoktWHiB/XYseyqfC+rfcb0uaS0+b8Yg52Ojej7qMvlNowrT6gmxblTC02CzBkdiJlM4QqThiHbUkdSR3tDSd2W/2IgABo8EQ8p74otrnUsAcAJIGtHAOoiUmzXTozATp0Jpp7z4vmAWkh5wqLFhR0CdzGv3jFZrgxVlL596MjmpNS14NhrQpU1YU319rnnq0qflK5Fv+U3KQBIOuFWXABlYEr6zxd7zNmHOXpV5PGPMnilY9j7uLP9BLrR30GdKeucN0r1xUoYXqjCx0msfZwW1bhC0WXiC3raBRWuRelG4wAAWSrckBqBwHQ8rsY0FWxvbw4jrf6yJf4zyQxmIdCUe+fyEfAmZUrYgdTVwd9nk= 23 | 24 | # $base_rmq_ref is used by rabbitmq-components.mk to select the 25 | # appropriate branch for dependencies. 26 | - base_rmq_ref=master 27 | 28 | elixir: 29 | - '1.9' 30 | otp_release: 31 | - '21.3' 32 | - '22.2' 33 | 34 | install: 35 | # This project being an Erlang one (we just set language to Elixir 36 | # to ensure it is installed), we don't want Travis to run mix(1) 37 | # automatically as it will break. 38 | skip 39 | 40 | script: 41 | # $current_rmq_ref is also used by rabbitmq-components.mk to select 42 | # the appropriate branch for dependencies. 43 | - make check-rabbitmq-components.mk 44 | current_rmq_ref="${TRAVIS_PULL_REQUEST_BRANCH:-${TRAVIS_BRANCH}}" 45 | - make xref 46 | current_rmq_ref="${TRAVIS_PULL_REQUEST_BRANCH:-${TRAVIS_BRANCH}}" 47 | - make tests 48 | current_rmq_ref="${TRAVIS_PULL_REQUEST_BRANCH:-${TRAVIS_BRANCH}}" 49 | 50 | after_failure: 51 | - | 52 | cd "$TRAVIS_BUILD_DIR" 53 | if test -d logs && test "$AWS_ACCESS_KEY_ID" && test "$AWS_SECRET_ACCESS_KEY"; then 54 | archive_name="$(basename "$TRAVIS_REPO_SLUG")-$TRAVIS_JOB_NUMBER" 55 | 56 | tar -c --transform "s/^logs/${archive_name}/" -f - logs | \ 57 | xz > "${archive_name}.tar.xz" 58 | 59 | aws s3 cp "${archive_name}.tar.xz" s3://server-release-pipeline/travis-ci-logs/ \ 60 | --region eu-west-1 \ 61 | --acl public-read 62 | fi 63 | -------------------------------------------------------------------------------- /.travis.yml.patch: -------------------------------------------------------------------------------- 1 | --- .travis.yml 2 | +++ .travis.yml 3 | @@ -1,7 +1,7 @@ 4 | # vim:sw=2:et: 5 | 6 | os: linux 7 | -dist: xenial 8 | +dist: bionic 9 | language: elixir 10 | notifications: 11 | email: 12 | @@ -13,6 +13,7 @@ addons: 13 | apt: 14 | packages: 15 | - awscli 16 | + - daemonize 17 | cache: 18 | apt: true 19 | env: 20 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, and in the interest of fostering an open 4 | and welcoming community, we pledge to respect all people who contribute through reporting 5 | issues, posting feature requests, updating documentation, submitting pull requests or 6 | patches, and other activities. 7 | 8 | We are committed to making participation in this project a harassment-free experience for 9 | everyone, regardless of level of experience, gender, gender identity and expression, 10 | sexual orientation, disability, personal appearance, body size, race, ethnicity, age, 11 | religion, or nationality. 12 | 13 | Examples of unacceptable behavior by participants include: 14 | 15 | * The use of sexualized language or imagery 16 | * Personal attacks 17 | * Trolling or insulting/derogatory comments 18 | * Public or private harassment 19 | * Publishing other's private information, such as physical or electronic addresses, 20 | without explicit permission 21 | * Other unethical or unprofessional conduct 22 | 23 | Project maintainers have the right and responsibility to remove, edit, or reject comments, 24 | commits, code, wiki edits, issues, and other contributions that are not aligned to this 25 | Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors 26 | that they deem inappropriate, threatening, offensive, or harmful. 27 | 28 | By adopting this Code of Conduct, project maintainers commit themselves to fairly and 29 | consistently applying these principles to every aspect of managing this project. Project 30 | maintainers who do not follow or enforce the Code of Conduct may be permanently removed 31 | from the project team. 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an 34 | individual is representing the project or its community. 35 | 36 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by 37 | contacting a project maintainer at [info@rabbitmq.com](mailto:info@rabbitmq.com). All complaints will 38 | be reviewed and investigated and will result in a response that is deemed necessary and 39 | appropriate to the circumstances. Maintainers are obligated to maintain confidentiality 40 | with regard to the reporter of an incident. 41 | 42 | This Code of Conduct is adapted from the 43 | [Contributor Covenant](https://contributor-covenant.org), version 1.3.0, available at 44 | [contributor-covenant.org/version/1/3/0/](https://contributor-covenant.org/version/1/3/0/) 45 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Thank you for using RabbitMQ and for taking the time to contribute to the project. 2 | This document has two main parts: 3 | 4 | * when and how to file GitHub issues for RabbitMQ projects 5 | * how to submit pull requests 6 | 7 | They intend to save you and RabbitMQ maintainers some time, so please 8 | take a moment to read through them. 9 | 10 | ## Overview 11 | 12 | ### GitHub issues 13 | 14 | The RabbitMQ team uses GitHub issues for _specific actionable items_ that 15 | engineers can work on. This assumes the following: 16 | 17 | * GitHub issues are not used for questions, investigations, root cause 18 | analysis, discussions of potential issues, etc (as defined by this team) 19 | * Enough information is provided by the reporter for maintainers to work with 20 | 21 | The team receives many questions through various venues every single 22 | day. Frequently, these questions do not include the necessary details 23 | the team needs to begin useful work. GitHub issues can very quickly 24 | turn into a something impossible to navigate and make sense 25 | of. Because of this, questions, investigations, root cause analysis, 26 | and discussions of potential features are all considered to be 27 | [mailing list][rmq-users] material. If you are unsure where to begin, 28 | the [RabbitMQ users mailing list][rmq-users] is the right place. 29 | 30 | Getting all the details necessary to reproduce an issue, make a 31 | conclusion or even form a hypothesis about what's happening can take a 32 | fair amount of time. Please help others help you by providing a way to 33 | reproduce the behavior you're observing, or at least sharing as much 34 | relevant information as possible on the [RabbitMQ users mailing 35 | list][rmq-users]. 36 | 37 | Please provide versions of the software used: 38 | 39 | * RabbitMQ server 40 | * Erlang 41 | * Operating system version (and distribution, if applicable) 42 | * All client libraries used 43 | * RabbitMQ plugins (if applicable) 44 | 45 | The following information greatly helps in investigating and reproducing issues: 46 | 47 | * RabbitMQ server logs 48 | * A code example or terminal transcript that can be used to reproduce 49 | * Full exception stack traces (a single line message is not enough!) 50 | * `rabbitmqctl report` and `rabbitmqctl environment` output 51 | * Other relevant details about the environment and workload, e.g. a traffic capture 52 | * Feel free to edit out hostnames and other potentially sensitive information. 53 | 54 | To make collecting much of this and other environment information, use 55 | the [`rabbitmq-collect-env`][rmq-collect-env] script. It will produce an archive with 56 | server logs, operating system logs, output of certain diagnostics commands and so on. 57 | Please note that **no effort is made to scrub any information that may be sensitive**. 58 | 59 | ### Pull Requests 60 | 61 | RabbitMQ projects use pull requests to discuss, collaborate on and accept code contributions. 62 | Pull requests is the primary place of discussing code changes. 63 | 64 | Here's the recommended workflow: 65 | 66 | * [Fork the repository][github-fork] or repositories you plan on contributing to. If multiple 67 | repositories are involved in addressing the same issue, please use the same branch name 68 | in each repository 69 | * Create a branch with a descriptive name in the relevant repositories 70 | * Make your changes, run tests (usually with `make tests`), commit with a 71 | [descriptive message][git-commit-msgs], push to your fork 72 | * Submit pull requests with an explanation what has been changed and **why** 73 | * Submit a filled out and signed [Contributor Agreement][ca-agreement] if needed (see below) 74 | * Be patient. We will get to your pull request eventually 75 | 76 | If what you are going to work on is a substantial change, please first 77 | ask the core team for their opinion on the [RabbitMQ users mailing list][rmq-users]. 78 | 79 | ## Running Tests 80 | 81 | See [RUNNING_TESTS.md](./RUNNING_TESTS.md) to learn how the test suites provision an `etcd` 82 | node and what the prerequisites are. 83 | 84 | To run a particular suite: 85 | 86 | make ct-$suite_name 87 | 88 | for example, to run the `system` suite: 89 | 90 | make ct-system 91 | 92 | Finally, 93 | 94 | make tests 95 | 96 | will run all suites. 97 | 98 | ## Code of Conduct 99 | 100 | See [CODE_OF_CONDUCT.md](./CODE_OF_CONDUCT.md). 101 | 102 | ## Contributor Agreement 103 | 104 | If you want to contribute a non-trivial change, please submit a signed 105 | copy of our [Contributor Agreement][ca-agreement] around the time you 106 | submit your pull request. This will make it much easier (in some 107 | cases, possible) for the RabbitMQ team at Pivotal to merge your 108 | contribution. 109 | 110 | ## Where to Ask Questions 111 | 112 | If something isn't clear, feel free to ask on our [mailing list][rmq-users]. 113 | 114 | [rmq-collect-env]: https://github.com/rabbitmq/support-tools/blob/master/scripts/rabbitmq-collect-env 115 | [git-commit-msgs]: https://chris.beams.io/posts/git-commit/ 116 | [rmq-users]: https://groups.google.com/forum/#!forum/rabbitmq-users 117 | [ca-agreement]: https://cla.pivotal.io/sign/rabbitmq 118 | [github-fork]: https://help.github.com/articles/fork-a-repo/ 119 | -------------------------------------------------------------------------------- /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_peer_discovery_etcd 2 | PROJECT_DESCRIPTION = etcd-based RabbitMQ peer discovery backend 3 | PROJECT_MOD = rabbitmq_peer_discovery_etcd_app 4 | 5 | DEPS = rabbit_common rabbitmq_peer_discovery_common rabbit eetcd gun 6 | TEST_DEPS = rabbitmq_ct_helpers rabbitmq_ct_client_helpers ct_helper meck 7 | dep_ct_helper = git https://github.com/extend/ct_helper.git master 8 | dep_gun = hex 1.3.3 9 | dep_eetcd = hex 0.3.3 10 | 11 | DEP_EARLY_PLUGINS = rabbit_common/mk/rabbitmq-early-plugin.mk 12 | DEP_PLUGINS = rabbit_common/mk/rabbitmq-plugin.mk 13 | 14 | # FIXME: Use erlang.mk patched for RabbitMQ, while waiting for PRs to be 15 | # reviewed and merged. 16 | 17 | ERLANG_MK_REPO = https://github.com/rabbitmq/erlang.mk.git 18 | ERLANG_MK_COMMIT = rabbitmq-tmp 19 | 20 | include rabbitmq-components.mk 21 | include erlang.mk 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RabbitMQ Peer Discovery Etcd 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_peer_discovery_etcd](https://github.com/rabbitmq/rabbitmq-server/tree/master/deps/rabbitmq_peer_discovery_etcd). 6 | All issues have been transferred. 7 | 8 | ## Introduction 9 | 10 | This is an etcd-based implementation of RabbitMQ [peer discovery interface](http://www.rabbitmq.com/blog/2018/02/12/peer-discovery-subsystem-in-rabbitmq-3-7/) 11 | (new in 3.7.0, previously available in the [rabbitmq-autocluster plugin](https://github.com/rabbitmq/rabbitmq-autocluster) 12 | by Gavin Roy). 13 | 14 | This plugin only performs peer discovery and most basic node health monitoring 15 | using [etcd](https://coreos.com/etcd/) as a data source. 16 | Please get familiar with [RabbitMQ clustering fundamentals](https://rabbitmq.com/clustering.html) before attempting 17 | to use it. 18 | 19 | While it may seem at times that this is a RabbitMQ cluster management solution, 20 | it is not. Cluster provisioning and most of Day 2 operations such as [proper monitoring](https://rabbitmq.com/monitoring.html) 21 | are not in scope for this plugin. 22 | 23 | 24 | ## Supported RabbitMQ Versions 25 | 26 | This plugin requires RabbitMQ 3.7.0 or later. 27 | 28 | 29 | ## Supported etcd Versions 30 | 31 | The plugin supports etcd 3.4 or later and uses the current stable v3 gRPC API. 32 | 33 | ## Installation 34 | 35 | This plugin ships with [supported RabbitMQ versions](https://www.rabbitmq.com/versions.html). 36 | There is no need to install it separately. 37 | 38 | As with any [plugin](https://rabbitmq.com/plugins.html), it must be enabled before it 39 | can be used. For peer discovery plugins it means they must be [enabled](https://rabbitmq.com//plugins.html#basics) or [preconfigured](https://rabbitmq.com//plugins.html#enabled-plugins-file) 40 | before first node boot: 41 | 42 | ``` 43 | rabbitmq-plugins --offline enable rabbitmq_peer_discovery_etcd 44 | ``` 45 | 46 | 47 | ## Documentation 48 | 49 | See [RabbitMQ Cluster Formation guide](https://www.rabbitmq.com/cluster-formation.html). 50 | 51 | 52 | ## Contributing 53 | 54 | See [CONTRIBUTING.md](./CONTRIBUTING.md) and our [development process overview](http://www.rabbitmq.com/github.html). 55 | 56 | 57 | ## License 58 | 59 | [Licensed under the MPL](LICENSE-MPL-RabbitMQ), same as RabbitMQ server. 60 | 61 | 62 | ## Copyright 63 | 64 | (c) 2007-2020 VMware, Inc. or its affiliates. 65 | -------------------------------------------------------------------------------- /RUNNING_TESTS.md: -------------------------------------------------------------------------------- 1 | ## Running Common Test Suites 2 | 3 | ```shell 4 | gmake tests 5 | ``` 6 | 7 | When running tests via `gmake tests`, there is no need to run the 8 | `init-etcd.sh` script as the test suite will do it for you. 9 | 10 | 11 | ## etcd Node Management 12 | 13 | The test suite of this plugin ships with a [script](./test/system_SUITE_data/init-etcd.sh) 14 | that starts an `etcd` node in the background. 15 | 16 | This script can also be used to start a node for experimenting with this 17 | plugin. To run it: 18 | 19 | ```shell 20 | ./test/system_SUITE_data/init-etcd.sh [etcd data dir] [etcd client port] 21 | ``` 22 | 23 | where `[etcd data dir]` is the desired `etcd` node data directory path. 24 | 25 | The script depends on the [`daemonize` 26 | tool](https://software.clapper.org/daemonize/) for running the process in the 27 | background and pid file creation. 28 | 29 | To stop a node started this way use the pid file created by `daemonize`: 30 | 31 | ```shell 32 | pkill -INT $(cat [etcd data dir]/etcd.pid) 33 | ``` 34 | -------------------------------------------------------------------------------- /include/rabbit_peer_discovery_etcd.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 | -define(BACKEND_CONFIG_KEY, peer_discovery_etcd). 9 | 10 | -record(statem_data, { 11 | endpoints, 12 | tls_options, 13 | connection_name, 14 | connection_pid, 15 | connection_monitor, 16 | key_prefix, 17 | cluster_name, 18 | node_key_lease_id, 19 | node_key_ttl_in_seconds, 20 | %% the pid of the process returned by eetcd_lease:keep_alive/2 21 | %% which refreshes this node's key lease 22 | node_lease_keepalive_pid, 23 | lock_ttl_in_seconds, 24 | username, 25 | obfuscated_password 26 | }). 27 | -------------------------------------------------------------------------------- /priv/schema/rabbitmq_peer_discovery_etcd.schema: -------------------------------------------------------------------------------- 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 | %% Endpoints 9 | 10 | {mapping, "cluster_formation.etcd.endpoints", "rabbit.cluster_formation.peer_discovery_etcd.endpoints", [ 11 | {datatype, {enum, [none]}} 12 | ]}. 13 | 14 | {mapping, "cluster_formation.etcd.endpoints.$index", "rabbit.cluster_formation.peer_discovery_etcd.endpoints", [ 15 | {datatype, [string, ip]} 16 | ]}. 17 | 18 | {translation, "rabbit.cluster_formation.peer_discovery_etcd.endpoints", 19 | fun(Conf) -> 20 | case cuttlefish:conf_get("cluster_formation.etcd.endpoints", Conf, undefined) of 21 | none -> []; 22 | _ -> 23 | Endpoints = cuttlefish_variable:filter_by_prefix("cluster_formation.etcd.endpoints", Conf), 24 | [V || {_, V} <- Endpoints] 25 | end 26 | end}. 27 | 28 | %% Legacy: etcd host 29 | 30 | {mapping, "cluster_formation.etcd.host", "rabbit.cluster_formation.peer_discovery_etcd.etcd_host", [ 31 | {datatype, string} 32 | ]}. 33 | 34 | {translation, "rabbit.cluster_formation.peer_discovery_etcd.etcd_host", 35 | fun(Conf) -> 36 | case cuttlefish:conf_get("cluster_formation.etcd.host", Conf, undefined) of 37 | undefined -> cuttlefish:unset(); 38 | Value -> Value 39 | end 40 | end}. 41 | 42 | %% Legacy: etcd port 43 | 44 | {mapping, "cluster_formation.etcd.port", "rabbit.cluster_formation.peer_discovery_etcd.etcd_port", [ 45 | {datatype, integer}, 46 | {validators, ["non_negative_integer"]} 47 | ]}. 48 | 49 | {translation, "rabbit.cluster_formation.peer_discovery_etcd.etcd_port", 50 | fun(Conf) -> 51 | case cuttlefish:conf_get("cluster_formation.etcd.port", Conf, undefined) of 52 | undefined -> cuttlefish:unset(); 53 | Value -> Value 54 | end 55 | end}. 56 | 57 | %% Legacy: etcd scheme. The key remains for backwards compatibility, it will not be used. 58 | 59 | {mapping, "cluster_formation.etcd.scheme", "rabbit.cluster_formation.peer_discovery_etcd.etcd_scheme", [ 60 | {datatype, string} 61 | ]}. 62 | 63 | {translation, "rabbit.cluster_formation.peer_discovery_etcd.etcd_scheme", 64 | fun(Conf) -> 65 | case cuttlefish:conf_get("cluster_formation.etcd.scheme", Conf, undefined) of 66 | undefined -> cuttlefish:unset(); 67 | Value -> Value 68 | end 69 | end}. 70 | 71 | %% key prefix appended after /rabbitmq/{discovery,locks} (a mandatory prefix we enforce as of #22) 72 | 73 | {mapping, "cluster_formation.etcd.key_prefix", "rabbit.cluster_formation.peer_discovery_etcd.etcd_prefix", [ 74 | {datatype, string} 75 | ]}. 76 | 77 | {translation, "rabbit.cluster_formation.peer_discovery_etcd.etcd_prefix", 78 | fun(Conf) -> 79 | case cuttlefish:conf_get("cluster_formation.etcd.key_prefix", Conf, undefined) of 80 | undefined -> cuttlefish:unset(); 81 | Value -> Value 82 | end 83 | end}. 84 | 85 | %% cluster name 86 | 87 | {mapping, "cluster_formation.etcd.cluster_name", "rabbit.cluster_formation.peer_discovery_etcd.cluster_name", [ 88 | {datatype, string} 89 | ]}. 90 | 91 | {translation, "rabbit.cluster_formation.peer_discovery_etcd.cluster_name", 92 | fun(Conf) -> 93 | case cuttlefish:conf_get("cluster_formation.etcd.cluster_name", Conf, undefined) of 94 | undefined -> cuttlefish:unset(); 95 | Value -> Value 96 | end 97 | end}. 98 | 99 | %% node key ttl 100 | 101 | {mapping, "cluster_formation.etcd.node_ttl", "rabbit.cluster_formation.peer_discovery_etcd.etcd_node_ttl", [ 102 | {datatype, integer}, 103 | {validators, ["non_negative_integer"]} 104 | ]}. 105 | 106 | {translation, "rabbit.cluster_formation.peer_discovery_etcd.etcd_node_ttl", 107 | fun(Conf) -> 108 | case cuttlefish:conf_get("cluster_formation.etcd.node_ttl", Conf, undefined) of 109 | undefined -> cuttlefish:unset(); 110 | Value -> Value 111 | end 112 | end}. 113 | 114 | %% lock acquisition timeout 115 | 116 | {mapping, "cluster_formation.etcd.lock_wait_time", "rabbit.cluster_formation.peer_discovery_etcd.lock_wait_time", [ 117 | {datatype, integer}, 118 | {validators, ["non_negative_integer"]} 119 | ]}. 120 | 121 | {mapping, "cluster_formation.etcd.lock_timeout", "rabbit.cluster_formation.peer_discovery_etcd.lock_wait_time", [ 122 | {datatype, integer}, 123 | {validators, ["non_negative_integer"]} 124 | ]}. 125 | 126 | %% an alias for lock acquisition timeout to be consistent with the etcd backend 127 | 128 | {translation, "rabbit.cluster_formation.peer_discovery_etcd.lock_wait_time", 129 | fun(Conf) -> 130 | case cuttlefish:conf_get("cluster_formation.etcd.lock_timeout", Conf, undefined) of 131 | undefined -> 132 | case cuttlefish:conf_get("cluster_formation.etcd.lock_wait_time", Conf, undefined) of 133 | undefined -> cuttlefish:unset(); 134 | Value -> Value 135 | end; 136 | Value -> Value 137 | end 138 | end}. 139 | 140 | %% authentication 141 | 142 | {mapping, "cluster_formation.etcd.username", "rabbit.cluster_formation.peer_discovery_etcd.etcd_username", [ 143 | {datatype, string} 144 | ]}. 145 | 146 | {translation, "rabbit.cluster_formation.peer_discovery_etcd.etcd_username", 147 | fun(Conf) -> 148 | case cuttlefish:conf_get("cluster_formation.etcd.username", Conf, undefined) of 149 | undefined -> cuttlefish:unset(); 150 | Value -> Value 151 | end 152 | end}. 153 | 154 | {mapping, "cluster_formation.etcd.password", "rabbit.cluster_formation.peer_discovery_etcd.etcd_password", [ 155 | {datatype, string} 156 | ]}. 157 | 158 | {translation, "rabbit.cluster_formation.peer_discovery_etcd.etcd_password", 159 | fun(Conf) -> 160 | case cuttlefish:conf_get("cluster_formation.etcd.password", Conf, undefined) of 161 | undefined -> cuttlefish:unset(); 162 | Value -> Value 163 | end 164 | end}. 165 | 166 | %% 167 | %% TLS client options 168 | %% 169 | 170 | {mapping, "cluster_formation.etcd.ssl_options", "rabbit.cluster_formation.peer_discovery_etcd.ssl_options", [ 171 | {datatype, {enum, [none]}} 172 | ]}. 173 | 174 | {translation, "rabbit.cluster_formation.peer_discovery_etcd.ssl_options", 175 | fun(Conf) -> 176 | case cuttlefish:conf_get("cluster_formation.etcd.ssl_options", Conf, undefined) of 177 | none -> []; 178 | _ -> cuttlefish:invalid("Invalid cluster_formation.etcd.ssl_options") 179 | end 180 | end}. 181 | 182 | {mapping, "cluster_formation.etcd.ssl_options.verify", "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.verify", [ 183 | {datatype, {enum, [verify_peer, verify_none]}}]}. 184 | 185 | {mapping, "cluster_formation.etcd.ssl_options.fail_if_no_peer_cert", "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.fail_if_no_peer_cert", [ 186 | {datatype, {enum, [true, false]}}]}. 187 | 188 | {mapping, "cluster_formation.etcd.ssl_options.cacertfile", "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.cacertfile", 189 | [{datatype, string}, {validators, ["file_accessible"]}]}. 190 | 191 | {mapping, "cluster_formation.etcd.ssl_options.certfile", "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.certfile", 192 | [{datatype, string}, {validators, ["file_accessible"]}]}. 193 | 194 | {mapping, "cluster_formation.etcd.ssl_options.cacerts.$name", "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.cacerts", 195 | [{datatype, string}]}. 196 | 197 | {translation, "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.cacerts", 198 | fun(Conf) -> 199 | Settings = cuttlefish_variable:filter_by_prefix("cluster_formation.etcd.ssl_options.cacerts", Conf), 200 | [ list_to_binary(V) || {_, V} <- Settings ] 201 | end}. 202 | 203 | {mapping, "cluster_formation.etcd.ssl_options.cert", "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.cert", 204 | [{datatype, string}]}. 205 | 206 | {translation, "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.cert", 207 | fun(Conf) -> 208 | list_to_binary(cuttlefish:conf_get("cluster_formation.etcd.ssl_options.cert", Conf)) 209 | end}. 210 | 211 | {mapping, "cluster_formation.etcd.ssl_options.crl_check", "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.crl_check", 212 | [{datatype, [{enum, [true, false, peer, best_effort]}]}]}. 213 | 214 | {mapping, "cluster_formation.etcd.ssl_options.depth", "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.depth", 215 | [{datatype, integer}, {validators, ["byte"]}]}. 216 | 217 | {mapping, "cluster_formation.etcd.ssl_options.dh", "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.dh", 218 | [{datatype, string}]}. 219 | 220 | {translation, "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.dh", 221 | fun(Conf) -> 222 | list_to_binary(cuttlefish:conf_get("cluster_formation.etcd.ssl_options.dh", Conf)) 223 | end}. 224 | 225 | {mapping, "cluster_formation.etcd.ssl_options.dhfile", "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.dhfile", 226 | [{datatype, string}, {validators, ["file_accessible"]}]}. 227 | 228 | {mapping, "cluster_formation.etcd.ssl_options.key.RSAPrivateKey", "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.key", 229 | [{datatype, string}]}. 230 | 231 | {mapping, "cluster_formation.etcd.ssl_options.key.DSAPrivateKey", "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.key", 232 | [{datatype, string}]}. 233 | 234 | {mapping, "cluster_formation.etcd.ssl_options.key.PrivateKeyInfo", "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.key", 235 | [{datatype, string}]}. 236 | 237 | {translation, "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.key", 238 | fun(Conf) -> 239 | case cuttlefish_variable:filter_by_prefix("cluster_formation.etcd.ssl_options.key", Conf) of 240 | [{[_,_,Key], Val}|_] -> {list_to_atom(Key), list_to_binary(Val)}; 241 | _ -> undefined 242 | end 243 | end}. 244 | 245 | {mapping, "cluster_formation.etcd.ssl_options.keyfile", "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.keyfile", 246 | [{datatype, string}, {validators, ["file_accessible"]}]}. 247 | 248 | {mapping, "cluster_formation.etcd.ssl_options.log_alert", "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.log_alert", 249 | [{datatype, {enum, [true, false]}}]}. 250 | 251 | {mapping, "cluster_formation.etcd.ssl_options.password", "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.password", 252 | [{datatype, string}]}. 253 | 254 | {mapping, "cluster_formation.etcd.ssl_options.psk_identity", "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.psk_identity", 255 | [{datatype, string}]}. 256 | 257 | {mapping, "cluster_formation.etcd.ssl_options.reuse_sessions", "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.reuse_sessions", 258 | [{datatype, {enum, [true, false]}}]}. 259 | 260 | {mapping, "cluster_formation.etcd.ssl_options.secure_renegotiate", "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.secure_renegotiate", 261 | [{datatype, {enum, [true, false]}}]}. 262 | 263 | {mapping, "cluster_formation.etcd.ssl_options.versions.$version", "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.versions", 264 | [{datatype, atom}]}. 265 | 266 | {translation, "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.versions", 267 | fun(Conf) -> 268 | Settings = cuttlefish_variable:filter_by_prefix("cluster_formation.etcd.ssl_options.versions", Conf), 269 | [V || {_, V} <- Settings] 270 | end}. 271 | 272 | {mapping, "cluster_formation.etcd.ssl_options.ciphers.$cipher", "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.ciphers", 273 | [{datatype, string}]}. 274 | 275 | {translation, "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.ciphers", 276 | fun(Conf) -> 277 | Settings = cuttlefish_variable:filter_by_prefix("cluster_formation.etcd.ssl_options.ciphers", Conf), 278 | lists:reverse([V || {_, V} <- Settings]) 279 | end}. 280 | -------------------------------------------------------------------------------- /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/rabbit_peer_discovery_etcd.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 | %% The Initial Developer of the Original Code is AWeber Communications. 6 | %% Copyright (c) 2015-2016 AWeber Communications 7 | %% Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. 8 | %% 9 | 10 | -module(rabbit_peer_discovery_etcd). 11 | -behaviour(rabbit_peer_discovery_backend). 12 | 13 | -include_lib("rabbit_common/include/rabbit.hrl"). 14 | -include_lib("rabbitmq_peer_discovery_common/include/rabbit_peer_discovery.hrl"). 15 | -include("rabbit_peer_discovery_etcd.hrl"). 16 | 17 | -export([init/0, list_nodes/0, supports_registration/0, register/0, unregister/0, 18 | post_registration/0, lock/1, unlock/1]). 19 | 20 | -define(ETCD_CLIENT, rabbitmq_peer_discovery_etcd_v3_client). 21 | 22 | %% 23 | %% API 24 | %% 25 | 26 | init() -> 27 | %% We cannot start this plugin yet since it depends on the rabbit app, 28 | %% which is in the process of being started by the time this function is called 29 | application:load(rabbitmq_peer_discovery_common), 30 | application:load(rabbitmq_peer_discovery_etcd), 31 | 32 | %% Here we start the client very early on, before plugins have initialized. 33 | %% We need to do it conditionally, however. 34 | NoOp = fun() -> ok end, 35 | Run = fun(_) -> 36 | rabbit_log:debug("Peer discovery etcd: initialising..."), 37 | application:ensure_all_started(eetcd), 38 | Formation = application:get_env(rabbit, cluster_formation, []), 39 | Opts = maps:from_list(proplists:get_value(peer_discovery_etcd, Formation, [])), 40 | {ok, Pid} = rabbitmq_peer_discovery_etcd_v3_client:start_link(Opts), 41 | %% unlink so that this supervisor's lifecycle does not affect RabbitMQ core 42 | unlink(Pid), 43 | rabbit_log:debug("etcd peer discovery: v3 client pid: ~p", [whereis(rabbitmq_peer_discovery_etcd_v3_client)]) 44 | end, 45 | rabbit_peer_discovery_util:maybe_backend_configured(?BACKEND_CONFIG_KEY, NoOp, NoOp, Run), 46 | 47 | ok. 48 | 49 | 50 | -spec list_nodes() -> {ok, {Nodes :: list(), NodeType :: rabbit_types:node_type()}} | {error, Reason :: string()}. 51 | 52 | list_nodes() -> 53 | Fun0 = fun() -> {ok, {[], disc}} end, 54 | Fun1 = fun() -> 55 | rabbit_log:warning("Peer discovery backend is set to ~s " 56 | "but final config does not contain " 57 | "rabbit.cluster_formation.peer_discovery_etcd. " 58 | "Cannot discover any nodes because etcd cluster details are not configured!", 59 | [?MODULE]), 60 | {ok, {[], disc}} 61 | end, 62 | Fun2 = fun(_Proplist) -> 63 | %% error logging will be done by the client 64 | Nodes = rabbitmq_peer_discovery_etcd_v3_client:list_nodes(), 65 | {ok, {Nodes, disc}} 66 | end, 67 | rabbit_peer_discovery_util:maybe_backend_configured(?BACKEND_CONFIG_KEY, Fun0, Fun1, Fun2). 68 | 69 | 70 | -spec supports_registration() -> boolean(). 71 | 72 | supports_registration() -> 73 | true. 74 | 75 | 76 | -spec register() -> ok | {error, string()}. 77 | 78 | register() -> 79 | Result = ?ETCD_CLIENT:register(), 80 | rabbit_log:info("Registered node with etcd"), 81 | Result. 82 | 83 | 84 | -spec unregister() -> ok | {error, string()}. 85 | unregister() -> 86 | %% This backend unregisters on plugin (etcd v3 client) deactivation 87 | %% because by the time unregistration happens, the plugin and thus the client 88 | %% it provides are already gone. MK. 89 | ok. 90 | 91 | -spec post_registration() -> ok | {error, Reason :: string()}. 92 | 93 | post_registration() -> 94 | ok. 95 | 96 | -spec lock(Node :: atom()) -> {ok, Data :: term()} | {error, Reason :: string()}. 97 | 98 | lock(Node) when is_atom(Node) -> 99 | case rabbitmq_peer_discovery_etcd_v3_client:lock(Node) of 100 | {ok, GeneratedKey} -> {ok, GeneratedKey}; 101 | {error, _} = Error -> Error 102 | end. 103 | 104 | 105 | -spec unlock(Data :: term()) -> ok. 106 | 107 | unlock(GeneratedKey) -> 108 | rabbitmq_peer_discovery_etcd_v3_client:unlock(GeneratedKey). 109 | -------------------------------------------------------------------------------- /src/rabbitmq_peer_discovery_etcd.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 | %% The Initial Developer of the Original Code is AWeber Communications. 6 | %% Copyright (c) 2015-2016 AWeber Communications 7 | %% Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. 8 | %% 9 | 10 | -module(rabbitmq_peer_discovery_etcd). 11 | -behaviour(rabbit_peer_discovery_backend). 12 | 13 | -export([init/0, list_nodes/0, supports_registration/0, register/0, unregister/0, 14 | post_registration/0, lock/1, unlock/1]). 15 | 16 | -define(DELEGATE, rabbit_peer_discovery_etcd). 17 | 18 | %% 19 | %% API 20 | %% 21 | 22 | init() -> 23 | ?DELEGATE:init(). 24 | 25 | -spec list_nodes() -> {ok, {Nodes :: list(), NodeType :: rabbit_types:node_type()}} | 26 | {error, Reason :: string()}. 27 | list_nodes() -> 28 | ?DELEGATE:list_nodes(). 29 | 30 | -spec supports_registration() -> boolean(). 31 | supports_registration() -> 32 | ?DELEGATE:supports_registration(). 33 | 34 | 35 | -spec register() -> ok. 36 | register() -> 37 | ?DELEGATE:register(). 38 | 39 | -spec unregister() -> ok. 40 | unregister() -> 41 | ?DELEGATE:unregister(). 42 | 43 | -spec post_registration() -> ok | {error, Reason :: string()}. 44 | post_registration() -> 45 | ?DELEGATE:post_registration(). 46 | 47 | -spec lock(Node :: atom()) -> not_supported. 48 | lock(Node) -> 49 | ?DELEGATE:lock(Node). 50 | 51 | -spec unlock(Data :: term()) -> ok. 52 | unlock(Data) -> 53 | ?DELEGATE:unlock(Data). 54 | -------------------------------------------------------------------------------- /src/rabbitmq_peer_discovery_etcd_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(rabbitmq_peer_discovery_etcd_app). 9 | 10 | %% 11 | %% API 12 | %% 13 | 14 | -behaviour(application). 15 | -export([start/2, stop/1, prep_stop/1]). 16 | 17 | start(_Type, _StartArgs) -> 18 | %% The tree had been started earlier, see rabbit_peer_discovery_etcd:init/0. MK. 19 | rabbitmq_peer_discovery_etcd_sup:start_link(). 20 | 21 | prep_stop(_State) -> 22 | try 23 | rabbitmq_peer_discovery_etcd_v3_client:unregister() 24 | catch 25 | _:_ -> ok 26 | end. 27 | 28 | stop(_State) -> 29 | ok. 30 | -------------------------------------------------------------------------------- /src/rabbitmq_peer_discovery_etcd_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(rabbitmq_peer_discovery_etcd_sup). 9 | 10 | -behaviour(supervisor). 11 | 12 | -export([init/1, start_link/0]). 13 | 14 | -include_lib("rabbit_common/include/rabbit.hrl"). 15 | -include("rabbit_peer_discovery_etcd.hrl"). 16 | 17 | %% 18 | %% API 19 | %% 20 | 21 | init([]) -> 22 | Flags = #{strategy => one_for_one, intensity => 10, period => 1}, 23 | Fun0 = fun() -> {ok, {Flags, []}} end, 24 | Fun1 = fun() -> {ok, {Flags, []}} end, 25 | Fun2 = fun(_) -> 26 | %% we stop the previously started client and "re-attach" it. MK. 27 | rabbitmq_peer_discovery_etcd_v3_client:stop(), 28 | Formation = application:get_env(rabbit, cluster_formation, []), 29 | Opts = maps:from_list(proplists:get_value(peer_discovery_etcd, Formation, [])), 30 | EtcdClientFSM = #{ 31 | id => rabbitmq_peer_discovery_etcd_v3_client, 32 | start => {rabbitmq_peer_discovery_etcd_v3_client, start_link, [Opts]}, 33 | restart => permanent, 34 | shutdown => ?SUPERVISOR_WAIT, 35 | type => worker, 36 | modules => [rabbitmq_peer_discovery_etcd_v3_client] 37 | }, 38 | Specs = [ 39 | EtcdClientFSM 40 | ], 41 | {ok, {Flags, Specs}} 42 | end, 43 | rabbit_peer_discovery_util:maybe_backend_configured(?BACKEND_CONFIG_KEY, Fun0, Fun1, Fun2). 44 | 45 | start_link() -> 46 | case supervisor:start_link({local, ?MODULE}, ?MODULE, []) of 47 | {ok, Pid} -> {ok, Pid}; 48 | {error, {already_started, Pid}} -> {ok, Pid}; 49 | {error, _} = Err -> Err 50 | end. 51 | -------------------------------------------------------------------------------- /src/rabbitmq_peer_discovery_etcd_v3_client.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(rabbitmq_peer_discovery_etcd_v3_client). 9 | 10 | %% API 11 | -export([]). 12 | 13 | 14 | -behaviour(gen_statem). 15 | 16 | -export([start_link/1, start/1, stop/0]). 17 | -export([init/1, callback_mode/0, terminate/3]). 18 | -export([register/1, register/0, unregister/1, unregister/0, list_nodes/0, list_nodes/1]). 19 | -export([lock/0, lock/1, lock/2, unlock/0, unlock/1, unlock/2]). 20 | -export([recover/3, connected/3, disconnected/3]). 21 | 22 | %% for tests 23 | -export([extract_node/1, filter_node/1, registration_value/1, node_key_base/1, node_key/1, lock_key_base/1]). 24 | 25 | -import(rabbit_data_coercion, [to_binary/1, to_list/1]). 26 | 27 | -compile(nowarn_unused_function). 28 | 29 | -include("rabbit_peer_discovery_etcd.hrl"). 30 | 31 | %% 32 | %% API 33 | %% 34 | 35 | -define(ETCD_CONN_NAME, ?MODULE). 36 | %% 60s by default matches the default heartbeat timeout. 37 | %% We add 1s for state machine bookkeeping and 38 | -define(DEFAULT_NODE_KEY_LEASE_TTL, 61). 39 | %% don't allow node lease key TTL to be lower than this 40 | %% as overly low values can cause annoying timeouts in etcd client operations 41 | -define(MINIMUM_NODE_KEY_LEASE_TTL, 15). 42 | %% default randomized delay range is 5s to 60s, so this value 43 | %% produces a comparable delay 44 | -define(DEFAULT_LOCK_WAIT_TTL, 70). 45 | %% don't allow lock lease TTL to be lower than this 46 | %% as overly low values can cause annoying timeouts in etcd client operations 47 | -define(MINIMUM_LOCK_WAIT_TTL, 30). 48 | 49 | -define(CALL_TIMEOUT, 15000). 50 | 51 | start(Conf) -> 52 | gen_statem:start({local, ?MODULE}, ?MODULE, Conf, []). 53 | 54 | start_link(Conf) -> 55 | gen_statem:start_link({local, ?MODULE}, ?MODULE, Conf, []). 56 | 57 | stop() -> 58 | gen_statem:stop(?MODULE). 59 | 60 | init(Args) -> 61 | ok = application:ensure_started(eetcd), 62 | Settings = normalize_settings(Args), 63 | Endpoints = maps:get(endpoints, Settings), 64 | Username = maps:get(etcd_username, Settings, undefined), 65 | Password = maps:get(etcd_password, Settings, undefined), 66 | TLSOpts = maps:get(ssl_options, Settings, []), 67 | Actions = [{next_event, internal, start}], 68 | {ok, recover, #statem_data{ 69 | endpoints = Endpoints, 70 | tls_options = TLSOpts, 71 | username = Username, 72 | obfuscated_password = obfuscate(Password), 73 | key_prefix = maps:get(etcd_prefix, Settings, <<"rabbitmq">>), 74 | node_key_ttl_in_seconds = erlang:max( 75 | ?MINIMUM_NODE_KEY_LEASE_TTL, 76 | maps:get(etcd_node_ttl, Settings, ?DEFAULT_NODE_KEY_LEASE_TTL) 77 | ), 78 | cluster_name = maps:get(cluster_name, Settings, <<"default">>), 79 | lock_ttl_in_seconds = erlang:max( 80 | ?MINIMUM_LOCK_WAIT_TTL, 81 | maps:get(lock_wait_time, Settings, ?DEFAULT_LOCK_WAIT_TTL) 82 | ) 83 | }, Actions}. 84 | 85 | callback_mode() -> [state_functions, state_enter]. 86 | 87 | terminate(Reason, State, Data) -> 88 | rabbit_log:debug("etcd v3 API client will terminate in state ~p, reason: ~p", 89 | [State, Reason]), 90 | disconnect(?ETCD_CONN_NAME, Data), 91 | rabbit_log:debug("etcd v3 API client has disconnected"), 92 | rabbit_log:debug("etcd v3 API client: total number of connections to etcd is ~p", [length(eetcd_conn_sup:info())]), 93 | ok. 94 | 95 | register() -> 96 | register(?MODULE). 97 | 98 | register(ServerRef) -> 99 | gen_statem:call(ServerRef, register, ?CALL_TIMEOUT). 100 | 101 | unregister() -> 102 | ?MODULE:unregister(?MODULE). 103 | 104 | unregister(ServerRef) -> 105 | gen_statem:call(ServerRef, unregister, ?CALL_TIMEOUT). 106 | 107 | list_nodes() -> 108 | list_nodes(?MODULE). 109 | 110 | list_nodes(ServerRef) -> 111 | gen_statem:call(ServerRef, list_keys, ?CALL_TIMEOUT). 112 | 113 | lock() -> 114 | lock(?MODULE, node()). 115 | 116 | lock(Node) -> 117 | lock(?MODULE, Node). 118 | 119 | lock(ServerRef, Node) -> 120 | gen_statem:call(ServerRef, {lock, Node}, ?CALL_TIMEOUT). 121 | 122 | unlock() -> 123 | unlock(?MODULE, node()). 124 | 125 | unlock(LockKey) -> 126 | unlock(?MODULE, LockKey). 127 | 128 | unlock(ServerRef, LockKey) -> 129 | gen_statem:call(ServerRef, {unlock, LockKey}, ?CALL_TIMEOUT). 130 | 131 | %% 132 | %% States 133 | %% 134 | 135 | recover(enter, _PrevState, #statem_data{endpoints = Endpoints}) -> 136 | rabbit_log:debug("etcd v3 API client has entered recovery state, endpoints: ~s", 137 | [string:join(Endpoints, ",")]), 138 | keep_state_and_data; 139 | recover(internal, start, Data = #statem_data{endpoints = Endpoints, connection_monitor = Ref}) -> 140 | rabbit_log:debug("etcd v3 API client will attempt to connect, endpoints: ~s", 141 | [string:join(Endpoints, ",")]), 142 | maybe_demonitor(Ref), 143 | {Transport, TransportOpts} = pick_transport(Data), 144 | case Transport of 145 | tcp -> rabbit_log:info("etcd v3 API client is configured to connect over plain TCP, without using TLS"); 146 | tls -> rabbit_log:info("etcd v3 API client is configured to use TLS") 147 | end, 148 | ConnName = ?ETCD_CONN_NAME, 149 | case connect(ConnName, Endpoints, Transport, TransportOpts, Data) of 150 | {ok, Pid} -> 151 | rabbit_log:debug("etcd v3 API client connection: ~p", [Pid]), 152 | rabbit_log:debug("etcd v3 API client: total number of connections to etcd is ~p", [length(eetcd_conn_sup:info())]), 153 | {next_state, connected, Data#statem_data{ 154 | connection_name = ConnName, 155 | connection_pid = Pid, 156 | connection_monitor = monitor(process, Pid) 157 | }}; 158 | {error, Errors} -> 159 | [rabbit_log:error("etcd peer discovery: failed to connect to endpoint ~p: ~p", [Endpoint, Err]) || {Endpoint, Err} <- Errors], 160 | ensure_disconnected(?ETCD_CONN_NAME, Data), 161 | Actions = [{state_timeout, reconnection_interval(), recover}], 162 | {keep_state, reset_statem_data(Data), Actions} 163 | end; 164 | recover(state_timeout, _PrevState, Data) -> 165 | rabbit_log:debug("etcd peer discovery: connection entered a reconnection delay state"), 166 | ensure_disconnected(?ETCD_CONN_NAME, Data), 167 | {next_state, recover, reset_statem_data(Data)}; 168 | recover({call, From}, Req, _Data) -> 169 | rabbit_log:error("etcd v3 API: client received a call ~p while not connected, will do nothing", [Req]), 170 | gen_statem:reply(From, {error, not_connected}), 171 | keep_state_and_data. 172 | 173 | 174 | connected(enter, _PrevState, Data) -> 175 | rabbit_log:info("etcd peer discovery: successfully connected to etcd"), 176 | 177 | {keep_state, acquire_node_key_lease_grant(Data)}; 178 | connected(info, {'DOWN', ConnRef, process, ConnPid, Reason}, Data = #statem_data{ 179 | connection_pid = ConnPid, 180 | connection_monitor = ConnRef 181 | }) -> 182 | rabbit_log:debug("etcd peer discovery: connection to etcd ~p is down: ~p", [ConnPid, Reason]), 183 | maybe_demonitor(ConnRef), 184 | {next_state, recover, reset_statem_data(Data)}; 185 | connected({call, From}, {lock, _Node}, Data = #statem_data{connection_name = Conn, lock_ttl_in_seconds = TTL}) -> 186 | case eetcd_lease:grant(eetcd_kv:new(Conn), TTL) of 187 | {ok, #{'ID' := LeaseID}} -> 188 | Key = lock_key_base(Data), 189 | rabbit_log:debug("etcd peer discovery: granted a lease ~p for registration lock ~s with TTL = ~p", [LeaseID, Key, TTL]), 190 | case eetcd_lock:lock(lock_context(Conn, Data), Key, LeaseID) of 191 | {ok, #{key := GeneratedKey}} -> 192 | rabbit_log:debug("etcd peer discovery: successfully acquired a lock, lock owner key: ~s", [GeneratedKey]), 193 | reply_and_retain_state(From, {ok, GeneratedKey}); 194 | {error, _} = Error -> 195 | rabbit_log:debug("etcd peer discovery: failed to acquire a lock using key ~s: ~p", [Key, Error]), 196 | reply_and_retain_state(From, Error) 197 | end; 198 | {error, _} = Error -> 199 | rabbit_log:debug("etcd peer discovery: failed to get a lease for registration lock: ~p", [Error]), 200 | reply_and_retain_state(From, Error) 201 | end; 202 | connected({call, From}, {unlock, GeneratedKey}, Data = #statem_data{connection_name = Conn}) -> 203 | Ctx = unlock_context(Conn, Data), 204 | case eetcd_lock:unlock(Ctx, GeneratedKey) of 205 | {ok, _} -> 206 | rabbit_log:debug("etcd peer discovery: successfully released lock, lock owner key: ~s", [GeneratedKey]), 207 | reply_and_retain_state(From, ok); 208 | {error, _} = Error -> 209 | rabbit_log:debug("etcd peer discovery: failed to release registration lock, lock owner key: ~s, error ~p", 210 | [GeneratedKey, Error]), 211 | reply_and_retain_state(From, Error) 212 | end; 213 | connected({call, From}, register, Data = #statem_data{connection_name = Conn}) -> 214 | Ctx = registration_context(Conn, Data), 215 | Key = node_key(Data), 216 | eetcd_kv:put(Ctx, Key, registration_value(Data)), 217 | rabbit_log:debug("etcd peer discovery: put key ~p, done with registration", [Key]), 218 | gen_statem:reply(From, ok), 219 | keep_state_and_data; 220 | connected({call, From}, unregister, Data = #statem_data{connection_name = Conn}) -> 221 | unregister(Conn, Data), 222 | gen_statem:reply(From, ok), 223 | {keep_state, Data#statem_data{ 224 | node_key_lease_id = undefined 225 | }}; 226 | connected({call, From}, list_keys, Data = #statem_data{connection_name = Conn}) -> 227 | Prefix = node_key_base(Data), 228 | C1 = eetcd_kv:new(Conn), 229 | C2 = eetcd_kv:with_prefix(eetcd_kv:with_key(C1, Prefix)), 230 | rabbit_log:debug("etcd peer discovery: will use prefix ~s to query for node keys", [Prefix]), 231 | {ok, #{kvs := Result}} = eetcd_kv:get(C2), 232 | rabbit_log:debug("etcd peer discovery returned keys: ~p", [Result]), 233 | Values = [maps:get(value, M) || M <- Result], 234 | case Values of 235 | Xs when is_list(Xs) -> 236 | rabbit_log:debug("etcd peer discovery: listing node keys returned ~b results", [length(Xs)]), 237 | ParsedNodes = lists:map(fun extract_node/1, Xs), 238 | {Successes, Failures} = lists:partition(fun filter_node/1, ParsedNodes), 239 | JoinedString = lists:join(",", [rabbit_data_coercion:to_list(Node) || Node <- lists:usort(Successes)]), 240 | rabbit_log:error("etcd peer discovery: successfully extracted nodes: ~s", [JoinedString]), 241 | lists:foreach(fun(Val) -> 242 | rabbit_log:error("etcd peer discovery: failed to extract node name from etcd value ~p", [Val]) 243 | end, Failures), 244 | gen_statem:reply(From, lists:usort(Successes)), 245 | keep_state_and_data; 246 | Other -> 247 | rabbit_log:debug("etcd peer discovery: listing node keys returned ~p", [Other]), 248 | gen_statem:reply(From, []), 249 | keep_state_and_data 250 | end. 251 | 252 | disconnected(enter, _PrevState, _Data) -> 253 | rabbit_log:info("etcd peer discovery: successfully disconnected from etcd"), 254 | keep_state_and_data. 255 | 256 | 257 | %% 258 | %% Implementation 259 | %% 260 | 261 | acquire_node_key_lease_grant(Data = #statem_data{connection_name = Name, node_key_ttl_in_seconds = TTL}) -> 262 | %% acquire a lease for TTL 263 | {ok, #{'ID' := LeaseID}} = eetcd_lease:grant(Name, TTL), 264 | {ok, KeepalivePid} = eetcd_lease:keep_alive(Name, LeaseID), 265 | rabbit_log:debug("etcd peer discovery: acquired a lease ~p for node key ~s with TTL = ~p", [LeaseID, node_key(Data), TTL]), 266 | Data#statem_data{ 267 | node_key_lease_id = LeaseID, 268 | node_lease_keepalive_pid = KeepalivePid 269 | }. 270 | 271 | registration_context(ConnName, #statem_data{node_key_lease_id = LeaseID}) -> 272 | Ctx1 = eetcd_kv:new(ConnName), 273 | eetcd_kv:with_lease(Ctx1, LeaseID). 274 | 275 | unregistration_context(ConnName, _Data) -> 276 | eetcd_kv:new(ConnName). 277 | 278 | lock_context(ConnName, #statem_data{lock_ttl_in_seconds = LeaseTTL}) -> 279 | %% LeaseTT is in seconds, eetcd_lock:with_timeout/2 expects milliseconds 280 | eetcd_lock:with_timeout(eetcd_lock:new(ConnName), LeaseTTL * 1000). 281 | 282 | unlock_context(ConnName, #statem_data{lock_ttl_in_seconds = Timeout}) -> 283 | %% caps the timeout here using the lock TTL value, it makes more 284 | %% sense than picking an arbitrary number. MK. 285 | eetcd_lock:with_timeout(eetcd_lock:new(ConnName), Timeout * 1000). 286 | 287 | node_key_base(#statem_data{cluster_name = ClusterName, key_prefix = Prefix}) -> 288 | to_binary(rabbit_misc:format("/rabbitmq/discovery/~s/clusters/~s/nodes", [Prefix, ClusterName])). 289 | 290 | node_key(Data) -> 291 | to_binary(rabbit_misc:format("~s/~s", [node_key_base(Data), node()])). 292 | 293 | lock_key_base(#statem_data{key_prefix = Prefix, cluster_name = ClusterName}) -> 294 | Key = rabbit_misc:format("/rabbitmq/locks/~s/clusters/~s/registration", 295 | [Prefix, ClusterName]), 296 | to_binary(Key). 297 | 298 | %% This value is not used and merely 299 | %% provides additional context to the operator. 300 | registration_value(#statem_data{node_key_lease_id = LeaseID, node_key_ttl_in_seconds = TTL}) -> 301 | to_binary(rabbit_json:encode(#{ 302 | <<"node">> => to_binary(node()), 303 | <<"lease_id">> => LeaseID, 304 | <<"ttl">> => TTL 305 | })). 306 | 307 | -spec extract_node(binary()) -> atom() | {error, any()}. 308 | 309 | extract_node(Payload) -> 310 | case rabbit_json:decode(Payload) of 311 | {error, Error} -> {error, Error}; 312 | Map -> 313 | case maps:get(<<"node">>, Map, undefined) of 314 | undefined -> undefined; 315 | Node -> rabbit_data_coercion:to_atom(Node) 316 | end 317 | end. 318 | 319 | filter_node(undefined) -> false; 320 | filter_node({error, _}) -> false; 321 | filter_node(_Other) -> true. 322 | 323 | 324 | error_is_already_started({_Endpoint, already_started}) -> 325 | true; 326 | error_is_already_started({_Endpoint, _}) -> 327 | false. 328 | 329 | connect(Name, Endpoints, Transport, TransportOpts, Data) -> 330 | case eetcd_conn:lookup(Name) of 331 | {ok, Pid} when is_pid(Pid) -> 332 | {ok, Pid}; 333 | {error, eetcd_conn_unavailable} -> 334 | do_connect(Name, Endpoints, Transport, TransportOpts, Data) 335 | end. 336 | 337 | do_connect(Name, Endpoints, Transport, TransportOpts, Data = #statem_data{username = Username}) -> 338 | case Username of 339 | undefined -> rabbit_log:info("etcd peer discovery: will connect to etcd without authentication (no credentials configured)"); 340 | _ -> rabbit_log:info("etcd peer discovery: will connect to etcd as user '~s'", [Username]) 341 | end, 342 | case eetcd:open(Name, Endpoints, connection_options(Data), Transport, TransportOpts) of 343 | {ok, Pid} -> {ok, Pid}; 344 | {error, Errors0} -> 345 | Errors = case is_list(Errors0) of 346 | true -> Errors0; 347 | false -> [Errors0] 348 | end, 349 | rabbit_log:debug("etcd peer discovery: connection errors: ~p", 350 | [Errors]), 351 | rabbit_log:debug("etcd peer discovery: are all connection errors benign?: ~p", 352 | [lists:all(fun error_is_already_started/1, Errors)]), 353 | %% If all errors are already_started we can ignore them. 354 | %% eetcd registers connections under a name 355 | case lists:all(fun error_is_already_started/1, Errors) of 356 | true -> 357 | eetcd_conn:lookup(Name); 358 | false -> 359 | {error, Errors} 360 | end 361 | end. 362 | 363 | connection_options(#statem_data{username = Username, obfuscated_password = Password}) -> 364 | SharedOpts = [{mode, random}], 365 | case {Username, Password} of 366 | {undefined, _} -> SharedOpts; 367 | {_, undefined} -> SharedOpts; 368 | {UVal, PVal} -> 369 | [{name, UVal}, {password, to_list(deobfuscate(PVal))}] ++ SharedOpts 370 | end. 371 | 372 | 373 | obfuscate(undefined) -> undefined; 374 | obfuscate(Password) -> 375 | credentials_obfuscation:encrypt(to_binary(Password)). 376 | 377 | deobfuscate(undefined) -> undefined; 378 | deobfuscate(Password) -> 379 | credentials_obfuscation:decrypt(to_binary(Password)). 380 | 381 | disconnect(ConnName, #statem_data{connection_monitor = Ref}) -> 382 | maybe_demonitor(Ref), 383 | do_disconnect(ConnName). 384 | 385 | unregister(Conn, Data = #statem_data{node_key_lease_id = LeaseID, node_lease_keepalive_pid = KAPid}) -> 386 | Ctx = unregistration_context(Conn, Data), 387 | Key = node_key(Data), 388 | eetcd_kv:delete(Ctx, Key), 389 | rabbit_log:debug("etcd peer discovery: deleted key ~s, done with unregistration", [Key]), 390 | eetcd_lease:revoke(Ctx, LeaseID), 391 | exit(KAPid, normal), 392 | rabbit_log:debug("etcd peer discovery: revoked a lease ~p for node key ~s", [LeaseID, Key]), 393 | ok. 394 | 395 | reply_and_retain_state(From, Value) -> 396 | gen_statem:reply(From, Value), 397 | keep_state_and_data. 398 | 399 | maybe_demonitor(undefined) -> 400 | true; 401 | maybe_demonitor(Ref) when is_reference(Ref) -> 402 | erlang:demonitor(Ref). 403 | 404 | reset_statem_data(Data0 = #statem_data{endpoints = Es, connection_monitor = Ref}) when Es =/= undefined -> 405 | maybe_demonitor(Ref), 406 | Data0#statem_data{ 407 | connection_pid = undefined, 408 | connection_monitor = undefined 409 | }. 410 | 411 | ensure_disconnected(Name, #statem_data{connection_monitor = Ref}) -> 412 | maybe_demonitor(Ref), 413 | do_disconnect(Name). 414 | 415 | do_disconnect(Name) -> 416 | try 417 | eetcd:close(Name) 418 | catch _:_ -> 419 | ok 420 | end. 421 | 422 | reconnection_interval() -> 423 | 3000. 424 | 425 | normalize_settings(Map) when is_map(Map) -> 426 | Endpoints = maps:get(endpoints, Map, []), 427 | LegacyEndpoints = case maps:get(etcd_host, Map, undefined) of 428 | undefined -> []; 429 | Hostname -> 430 | Port = maps:get(etcd_port, Map, 2379), 431 | [rabbit_misc:format("~s:~p", [Hostname, Port])] 432 | end, 433 | 434 | AllEndpoints = Endpoints ++ LegacyEndpoints, 435 | maps:merge(maps:without([etcd_prefix, etcd_node_ttl, lock_wait_time], Map), 436 | #{endpoints => AllEndpoints}). 437 | 438 | pick_transport(#statem_data{tls_options = []}) -> 439 | {tcp, []}; 440 | pick_transport(#statem_data{tls_options = Opts}) -> 441 | {tls, Opts}. 442 | -------------------------------------------------------------------------------- /test/config_schema_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 | -module(config_schema_SUITE). 8 | 9 | -compile(export_all). 10 | 11 | all() -> 12 | [ 13 | run_snippets 14 | ]. 15 | 16 | %% ------------------------------------------------------------------- 17 | %% Testsuite setup/teardown. 18 | %% ------------------------------------------------------------------- 19 | 20 | init_per_suite(Config) -> 21 | rabbit_ct_helpers:log_environment(), 22 | Config1 = rabbit_ct_helpers:run_setup_steps(Config), 23 | rabbit_ct_config_schema:init_schemas(rabbitmq_peer_discovery_etcd, Config1). 24 | 25 | 26 | end_per_suite(Config) -> 27 | rabbit_ct_helpers:run_teardown_steps(Config). 28 | 29 | init_per_testcase(Testcase, Config) -> 30 | rabbit_ct_helpers:testcase_started(Config, Testcase), 31 | Config1 = rabbit_ct_helpers:set_config(Config, [ 32 | {rmq_nodename_suffix, Testcase} 33 | ]), 34 | rabbit_ct_helpers:run_steps(Config1, 35 | rabbit_ct_broker_helpers:setup_steps() ++ 36 | rabbit_ct_client_helpers:setup_steps()). 37 | 38 | end_per_testcase(Testcase, Config) -> 39 | Config1 = rabbit_ct_helpers:run_steps(Config, 40 | rabbit_ct_client_helpers:teardown_steps() ++ 41 | rabbit_ct_broker_helpers:teardown_steps()), 42 | rabbit_ct_helpers:testcase_finished(Config1, Testcase). 43 | 44 | %% ------------------------------------------------------------------- 45 | %% Testcases. 46 | %% ------------------------------------------------------------------- 47 | 48 | run_snippets(Config) -> 49 | ok = rabbit_ct_broker_helpers:rpc(Config, 0, 50 | ?MODULE, run_snippets1, [Config]). 51 | 52 | run_snippets1(Config) -> 53 | rabbit_ct_config_schema:run_snippets(Config). 54 | 55 | -------------------------------------------------------------------------------- /test/config_schema_SUITE_data/rabbitmq_peer_discovery_etcd.snippets: -------------------------------------------------------------------------------- 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 | [ 9 | %% discovery mechanism 10 | {etcd_discovery_mechanism_as_module, 11 | "cluster_formation.peer_discovery_backend = rabbit_peer_discovery_etcd 12 | cluster_formation.etcd.host = etcd.eng.megacorp.local", [ 13 | {rabbit, [ 14 | {cluster_formation, [ 15 | {peer_discovery_backend, rabbit_peer_discovery_etcd}, 16 | {peer_discovery_etcd, [ 17 | {etcd_host, "etcd.eng.megacorp.local"} 18 | ]} 19 | ]} 20 | ]} 21 | ], [rabbitmq_peer_discovery_etcd] 22 | }, 23 | 24 | {etcd_discovery_mechanism_shortcut, 25 | "cluster_formation.peer_discovery_backend = etcd 26 | cluster_formation.etcd.host = etcd.eng.megacorp.local", [ 27 | {rabbit, [ 28 | {cluster_formation, [ 29 | {peer_discovery_backend, rabbit_peer_discovery_etcd}, 30 | {peer_discovery_etcd, [ 31 | {etcd_host, "etcd.eng.megacorp.local"} 32 | ]} 33 | ]} 34 | ]} 35 | ], [rabbitmq_peer_discovery_etcd] 36 | }, 37 | 38 | %% etcd hostname 39 | {etcd_host, "cluster_formation.etcd.host = etcd.eng.megacorp.local", [ 40 | {rabbit, [ 41 | {cluster_formation, [ 42 | {peer_discovery_etcd, [ 43 | {etcd_host, "etcd.eng.megacorp.local"} 44 | ]} 45 | ]} 46 | ]} 47 | ], [rabbitmq_peer_discovery_etcd] 48 | }, 49 | %% etcd port 50 | {etcd_port, "cluster_formation.etcd.port = 7979", [ 51 | {rabbit, [ 52 | {cluster_formation, [ 53 | {peer_discovery_etcd, [ 54 | {etcd_port, 7979} 55 | ]} 56 | ]} 57 | ]} 58 | ], [rabbitmq_peer_discovery_etcd] 59 | }, 60 | %% credentials 61 | {etcd_username, "cluster_formation.etcd.username = rabbitmq", [ 62 | {rabbit, [ 63 | {cluster_formation, [ 64 | {peer_discovery_etcd, [ 65 | {etcd_username, "rabbitmq"} 66 | ]} 67 | ]} 68 | ]} 69 | ], [rabbitmq_peer_discovery_etcd] 70 | }, 71 | {etcd_password, "cluster_formation.etcd.password = rabbitmq", [ 72 | {rabbit, [ 73 | {cluster_formation, [ 74 | {peer_discovery_etcd, [ 75 | {etcd_password, "rabbitmq"} 76 | ]} 77 | ]} 78 | ]} 79 | ], [rabbitmq_peer_discovery_etcd] 80 | }, 81 | %% key prefix 82 | {etcd_key_prefix, "cluster_formation.etcd.key_prefix = staging", [ 83 | {rabbit, [ 84 | {cluster_formation, [ 85 | {peer_discovery_etcd, [ 86 | {etcd_prefix, "staging"} 87 | ]} 88 | ]} 89 | ]} 90 | ], [rabbitmq_peer_discovery_etcd] 91 | }, 92 | %% cluster name 93 | {etcd_cluster_name, "cluster_formation.etcd.cluster_name = staging", [ 94 | {rabbit, [ 95 | {cluster_formation, [ 96 | {peer_discovery_etcd, [ 97 | {cluster_name, "staging"} 98 | ]} 99 | ]} 100 | ]} 101 | ], [rabbitmq_peer_discovery_etcd] 102 | }, 103 | %% node key TTL 104 | {etcd_node_ttl, "cluster_formation.etcd.node_ttl = 60", [ 105 | {rabbit, [ 106 | {cluster_formation, [ 107 | {peer_discovery_etcd, [ 108 | {etcd_node_ttl, 60} 109 | ]} 110 | ]} 111 | ]} 112 | ], [rabbitmq_peer_discovery_etcd] 113 | }, 114 | %% lock acquisition timeout 115 | {etcd_lock_wait_time, "cluster_formation.etcd.lock_timeout = 400", [ 116 | {rabbit, [ 117 | {cluster_formation, [ 118 | {peer_discovery_etcd, [ 119 | {lock_wait_time, 400} 120 | ]} 121 | ]} 122 | ]} 123 | ], [rabbitmq_peer_discovery_etcd] 124 | }, 125 | 126 | %% alias for consistency with etcd 127 | {etcd_lock_wait_time, "cluster_formation.etcd.lock_wait_time = 400", [ 128 | {rabbit, [ 129 | {cluster_formation, [ 130 | {peer_discovery_etcd, [ 131 | {lock_wait_time, 400} 132 | ]} 133 | ]} 134 | ]} 135 | ], [rabbitmq_peer_discovery_etcd] 136 | } 137 | ]. 138 | -------------------------------------------------------------------------------- /test/system_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 | %% The Initial Developer of the Original Code is AWeber Communications. 6 | %% Copyright (c) 2015-2016 AWeber Communications 7 | %% Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. 8 | %% 9 | 10 | -module(system_SUITE). 11 | 12 | -compile(export_all). 13 | 14 | -include_lib("common_test/include/ct.hrl"). 15 | -include_lib("eunit/include/eunit.hrl"). 16 | 17 | -include("rabbit_peer_discovery_etcd.hrl"). 18 | 19 | -import(rabbit_data_coercion, [to_binary/1, to_integer/1]). 20 | 21 | 22 | all() -> 23 | [ 24 | {group, v3_client} 25 | ]. 26 | 27 | groups() -> 28 | [ 29 | {v3_client, [], [ 30 | etcd_connection_sanity_check_test, 31 | init_opens_a_connection_test, 32 | registration_with_locking_test 33 | ]} 34 | ]. 35 | 36 | init_per_suite(Config) -> 37 | rabbit_ct_helpers:log_environment(), 38 | rabbit_ct_helpers:run_setup_steps(Config, [fun init_etcd/1]). 39 | 40 | end_per_suite(Config) -> 41 | rabbit_ct_helpers:run_teardown_steps(Config, [fun stop_etcd/1]). 42 | 43 | init_etcd(Config) -> 44 | DataDir = ?config(data_dir, Config), 45 | PrivDir = ?config(priv_dir, Config), 46 | TcpPort = 25389, 47 | EtcdDir = filename:join([PrivDir, "etcd"]), 48 | InitEtcd = filename:join([DataDir, "init-etcd.sh"]), 49 | Cmd = [InitEtcd, EtcdDir, {"~b", [TcpPort]}], 50 | case rabbit_ct_helpers:exec(Cmd) of 51 | {ok, Stdout} -> 52 | case re:run(Stdout, "^ETCD_PID=([0-9]+)$", [{capture, all_but_first, list}, multiline]) of 53 | {match, [EtcdPid]} -> 54 | ct:pal(?LOW_IMPORTANCE, "etcd PID: ~s~netcd is listening on: ~b", [EtcdPid, TcpPort]), 55 | rabbit_ct_helpers:set_config(Config, [{etcd_pid, EtcdPid}, 56 | {etcd_endpoints, [rabbit_misc:format("localhost:~p", [TcpPort])]}, 57 | {etcd_port, TcpPort}]); 58 | nomatch -> 59 | ct:pal(?HI_IMPORTANCE, "init-etcd.sh output did not match what's expected: ~p", [Stdout]) 60 | end; 61 | {error, Code, Reason} -> 62 | ct:pal(?HI_IMPORTANCE, "init-etcd.sh exited with code ~p: ~p", [Code, Reason]), 63 | _ = rabbit_ct_helpers:exec(["pkill", "-INT", "etcd"]), 64 | {skip, "Failed to initialize etcd"} 65 | end. 66 | 67 | stop_etcd(Config) -> 68 | EtcdPid = ?config(etcd_pid, Config), 69 | Cmd = ["kill", "-INT", EtcdPid], 70 | _ = rabbit_ct_helpers:exec(Cmd), 71 | Config. 72 | 73 | 74 | %% 75 | %% Test cases 76 | %% 77 | 78 | etcd_connection_sanity_check_test(Config) -> 79 | application:ensure_all_started(eetcd), 80 | Endpoints = ?config(etcd_endpoints, Config), 81 | ?assertMatch({ok, _Pid}, eetcd:open(test, Endpoints)), 82 | 83 | Condition1 = fun() -> 84 | 1 =:= length(eetcd_conn_sup:info()) 85 | end, 86 | try 87 | rabbit_ct_helpers:await_condition(Condition1, 60000) 88 | after 89 | eetcd:close(test) 90 | end, 91 | Condition2 = fun() -> 92 | 0 =:= length(eetcd_conn_sup:info()) 93 | end, 94 | rabbit_ct_helpers:await_condition(Condition2, 60000). 95 | 96 | init_opens_a_connection_test(Config) -> 97 | Endpoints = ?config(etcd_endpoints, Config), 98 | {ok, Pid} = start_client(Endpoints), 99 | Condition = fun() -> 100 | 1 =:= length(eetcd_conn_sup:info()) 101 | end, 102 | try 103 | rabbit_ct_helpers:await_condition(Condition, 90000) 104 | after 105 | gen_statem:stop(Pid) 106 | end, 107 | ?assertEqual(0, length(eetcd_conn_sup:info())). 108 | 109 | 110 | registration_with_locking_test(Config) -> 111 | Endpoints = ?config(etcd_endpoints, Config), 112 | {ok, Pid} = start_client(Endpoints), 113 | Condition1 = fun() -> 114 | 1 =:= length(eetcd_conn_sup:info()) 115 | end, 116 | rabbit_ct_helpers:await_condition(Condition1, 90000), 117 | 118 | {ok, LockOwnerKey} = rabbitmq_peer_discovery_etcd_v3_client:lock(Pid, node()), 119 | rabbitmq_peer_discovery_etcd_v3_client:register(Pid), 120 | ?assertEqual(ok, rabbitmq_peer_discovery_etcd_v3_client:unlock(Pid, LockOwnerKey)), 121 | 122 | Condition2 = fun() -> 123 | [node()] =:= rabbitmq_peer_discovery_etcd_v3_client:list_nodes(Pid) 124 | end, 125 | try 126 | rabbit_ct_helpers:await_condition(Condition2, 45000) 127 | after 128 | gen_statem:stop(Pid) 129 | end. 130 | 131 | %% 132 | %% Helpers 133 | %% 134 | 135 | start_client(Endpoints) -> 136 | case rabbitmq_peer_discovery_etcd_v3_client:start(#{endpoints => Endpoints}) of 137 | {ok, Pid} -> 138 | {ok, Pid}; 139 | {error, {already_started, Pid}} -> 140 | {ok, Pid} 141 | end. 142 | -------------------------------------------------------------------------------- /test/system_SUITE_data/init-etcd.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # vim:sw=4:et: 3 | 4 | # This Source Code Form is subject to the terms of the Mozilla Public 5 | # License, v. 2.0. If a copy of the MPL was not distributed with this 6 | # file, You can obtain one at https://mozilla.org/MPL/2.0/. 7 | # 8 | # Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. 9 | # 10 | 11 | set -ex 12 | 13 | case "$(uname -s)" in 14 | Linux) 15 | package_os="linux" 16 | tmpdir=/tmp 17 | archive="tar.gz" 18 | ;; 19 | Darwin) 20 | package_os="darwin" 21 | tmpdir=$TMPDIR 22 | archive="zip" 23 | ;; 24 | *) 25 | exit 1 26 | ;; 27 | esac 28 | 29 | etcd_data_dir=${1:-"$tmpdir/etcd/data"} 30 | pidfile="$etcd_data_dir/etcd.pid" 31 | tcp_port=${2:-2379} 32 | 33 | ETCD_VER=v3.4.6 34 | 35 | GITHUB_URL=https://github.com/etcd-io/etcd/releases/download 36 | DOWNLOAD_URL=${GITHUB_URL} 37 | 38 | rm -rf "${tmpdir}/etcd-${ETCD_VER}" 39 | 40 | if ! [ -f "${tmpdir}/etcd-${ETCD_VER}-$package_os-amd64.$archive" ]; then 41 | curl -L "${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-$package_os-amd64.$archive" -o "${tmpdir}/etcd-${ETCD_VER}-$package_os-amd64.$archive" 42 | fi 43 | 44 | case "$(uname -s)" in 45 | Linux) 46 | mkdir -p "${tmpdir}/etcd-${ETCD_VER}-$package_os-amd64/" 47 | tar xzvf "/tmp/etcd-${ETCD_VER}-$package_os-amd64.$archive" -C "${tmpdir}/etcd-${ETCD_VER}-$package_os-amd64/" --strip-components=1 48 | ;; 49 | Darwin) 50 | unzip -q -o -d "$tmpdir" "${tmpdir}/etcd-${ETCD_VER}-$package_os-amd64.$archive" 51 | ;; 52 | *) 53 | exit 1 54 | ;; 55 | esac 56 | 57 | mv "${tmpdir}/etcd-${ETCD_VER}-$package_os-amd64/" "${tmpdir}/etcd-${ETCD_VER}/" 58 | 59 | rm -rf "$etcd_data_dir" 60 | mkdir -p "$etcd_data_dir" 61 | 62 | # daemonize(1) is installed under this path on Debian 63 | PATH=$PATH:/usr/sbin 64 | 65 | daemonize -p "$pidfile" -l "${etcd_data_dir}/daemonize_lock" -- "$tmpdir/etcd-${ETCD_VER}/etcd" \ 66 | --data-dir "$etcd_data_dir" --name peer-discovery-0 --initial-advertise-peer-urls http://127.0.0.1:2380 \ 67 | --listen-peer-urls http://127.0.0.1:2380 \ 68 | --listen-client-urls "http://127.0.0.1:${tcp_port}" \ 69 | --advertise-client-urls "http://127.0.0.1:${tcp_port}" \ 70 | --initial-cluster-token rabbitmq-peer-discovery-etcd \ 71 | --initial-cluster peer-discovery-0=http://127.0.0.1:2380 \ 72 | --initial-cluster-state new 73 | 74 | 75 | for seconds in {1..30}; do 76 | "$tmpdir/etcd-${ETCD_VER}/etcdctl" put rabbitmq-ct rabbitmq-ct --dial-timeout=1s && break 77 | sleep 1 78 | done 79 | 80 | echo ETCD_PID=$(cat "$pidfile") 81 | 82 | -------------------------------------------------------------------------------- /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 | %% The Initial Developer of the Original Code is AWeber Communications. 6 | %% Copyright (c) 2015-2016 AWeber Communications 7 | %% Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. 8 | %% 9 | 10 | -module(unit_SUITE). 11 | 12 | -compile(export_all). 13 | 14 | -include_lib("common_test/include/ct.hrl"). 15 | -include_lib("eunit/include/eunit.hrl"). 16 | 17 | -include("rabbit_peer_discovery_etcd.hrl"). 18 | 19 | -import(rabbit_data_coercion, [to_binary/1]). 20 | 21 | 22 | all() -> 23 | [ 24 | {group, unit} 25 | ]. 26 | 27 | groups() -> 28 | [ 29 | {unit, [], [ 30 | registration_value_test, 31 | extract_nodes_case1_test, 32 | filter_nodes_test, 33 | node_key_base_test, 34 | node_key_test, 35 | lock_key_base_test 36 | ]} 37 | ]. 38 | 39 | 40 | %% 41 | %% Test cases 42 | %% 43 | 44 | registration_value_test(_Config) -> 45 | LeaseID = 8488283859587364900, 46 | TTL = 61, 47 | Input = #statem_data{ 48 | node_key_lease_id = LeaseID, 49 | node_key_ttl_in_seconds = TTL 50 | }, 51 | Expected = registration_value_of(LeaseID, TTL), 52 | ?assertEqual(Expected, rabbitmq_peer_discovery_etcd_v3_client:registration_value(Input)). 53 | 54 | 55 | extract_nodes_case1_test(_Config) -> 56 | Input = registration_value_of(8488283859587364900, 61), 57 | Expected = node(), 58 | 59 | ?assertEqual(Expected, rabbitmq_peer_discovery_etcd_v3_client:extract_node(Input)), 60 | 61 | ?assertEqual(undefined, rabbitmq_peer_discovery_etcd_v3_client:extract_node(<<"{}">>)). 62 | 63 | filter_nodes_test(_Config) -> 64 | Input = [node(), undefined, undefined, {error, reason1}, {error, {another, reason}}], 65 | Expected = [node()], 66 | 67 | ?assertEqual(Expected, lists:filter(fun rabbitmq_peer_discovery_etcd_v3_client:filter_node/1, Input)). 68 | 69 | node_key_base_test(_Config) -> 70 | Expected = <<"/rabbitmq/discovery/prefffix/clusters/cluster-a/nodes">>, 71 | Input = #statem_data{ 72 | cluster_name = "cluster-a", 73 | key_prefix = "prefffix" 74 | }, 75 | ?assertEqual(Expected, rabbitmq_peer_discovery_etcd_v3_client:node_key_base(Input)). 76 | 77 | node_key_test(_Config) -> 78 | Expected = to_binary(rabbit_misc:format("/rabbitmq/discovery/prefffix/clusters/cluster-a/nodes/~s", [node()])), 79 | Input = #statem_data{ 80 | cluster_name = "cluster-a", 81 | key_prefix = "prefffix" 82 | }, 83 | ?assertEqual(Expected, rabbitmq_peer_discovery_etcd_v3_client:node_key(Input)). 84 | 85 | lock_key_base_test(_Config) -> 86 | Expected = <<"/rabbitmq/locks/prefffix/clusters/cluster-b/registration">>, 87 | Input = #statem_data{ 88 | cluster_name = "cluster-b", 89 | key_prefix = "prefffix" 90 | }, 91 | ?assertEqual(Expected, rabbitmq_peer_discovery_etcd_v3_client:lock_key_base(Input)). 92 | 93 | %% 94 | %% Helpers 95 | %% 96 | 97 | registration_value_of(LeaseID, TTL) -> 98 | to_binary(rabbit_json:encode(#{ 99 | <<"node">> => to_binary(node()), 100 | <<"lease_id">> => LeaseID, 101 | <<"ttl">> => TTL 102 | })). 103 | --------------------------------------------------------------------------------