diff --git a/.asf.yaml b/.asf.yaml new file mode 100644 index 0000000000..24bcbcda98 --- /dev/null +++ b/.asf.yaml @@ -0,0 +1,45 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +github: + description: EventMesh is a dynamic cloud-native eventing infrastruture used to decouple the application and backend middleware layer, which supports a wide range of use cases that encompass complex multi-cloud, widely distributed topologies using diverse technology stacks. + homepage: https://eventmesh.apache.org/ + labels: + - event-mesh + - event-gateway + - event-driven + - event-streaming + - event-sourcing + - event-governance + - event-routing + - cloud-native + - serverless + - serverless-workflow + - esb + - message-bus + - cqrs + enabled_merge_buttons: + squash: true + merge: false + rebase: false + protected_branches: + master: + required_status_checks: + strict: true + required_pull_request_reviews: + dismiss_stale_reviews: true + required_approving_review_count: 2 diff --git a/.coveralls.yml b/.coveralls.yml deleted file mode 100644 index cf27a37024..0000000000 --- a/.coveralls.yml +++ /dev/null @@ -1 +0,0 @@ -service_name: travis-pro diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000000..381e4d77e6 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1 @@ +#TBD \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000000..1d0cbfc626 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,84 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +name: "Continuous Integration" + +on: + push: + branches: + - develop + pull_request: + branches: + - develop + workflow_dispatch: + +jobs: + build: + name: Build + + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + language: + - 'java' + java: + - 8 + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Initialize CodeQL + uses: github/codeql-action/init@v1 + with: + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + # queries: ./path/to/local/query, your-org/your-repo/queries@main + languages: ${{ matrix.language }} + + - name: Set up JDK ${{ matrix.java }} + uses: actions/setup-java@v1 + with: + java-version: ${{ matrix.java }} + + - name: Build + run: ./gradlew clean build jacocoTestReport + + - name: Perform CodeQL analysis + uses: github/codeql-action/analyze@v1 + + - name: Upload coverage report to codecov.io + run: bash <(curl -s https://codecov.io/bash) || echo 'Failed to upload coverage report!' + + license-check: + name: License Check + + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Check license header + uses: apache/skywalking-eyes@9bd5feb86b5817aa6072b008f9866a2c3bbc8587 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/gitee-mirror.yml b/.github/workflows/gitee-mirror.yml deleted file mode 100644 index 6582dfd5ac..0000000000 --- a/.github/workflows/gitee-mirror.yml +++ /dev/null @@ -1,60 +0,0 @@ -# 使用 GitHub Action 来解决手动同步到 Gitee 的问题 -# 效果:github repo 代码更新之后,会自动同步至 gitee -# 使用到的 GitHub Action:https://github.com/Yikun/hub-mirror-action - -# This is a basic workflow to help you get started with Actions - -name: Gitee Mirror - -# Controls when the action will run. Triggers the workflow on push or pull request -# events but only for the master branch -on: - push: - branches: - - '*' - -jobs: - # This workflow contains a single job called "build" - build: - # The type of runner that the job will run on - runs-on: ubuntu-latest - - # Steps represent a sequence of tasks that will be executed as part of the job - steps: - - name: Mirror the Github organization repos to Gitee. - uses: Yikun/hub-mirror-action@master - with: - src: github/WeBankFinTech - dst: gitee/webank - # 这里请填写与gitee上公钥匹配的的 ssh private key,参见:https://gitee.com/profile/sshkeys - # 填写地址:https://github.com/WeBankFinTech/fes.js/settings/secrets - dst_key: ${{ secrets.GITEE_PRIVATE_KEY }} - # 这里请填写 gitee的令牌,参见:https://gitee.com/profile/personal_access_tokens - # 填写地址:https://github.com/WeBankFinTech/fes.js/settings/secrets - dst_token: ${{ secrets.GITEE_TOKEN }} - # 项目同步白名单,可以选择填写多个,以英文逗号分割 - static_list: "EventMesh" - # 是否强制同步 - force_update: true - # 账号类型:对 luban-h5 而言是 user,因为是个人项目;如果是企业项目,请填写 org,因为是组织下的项目 - account_type: org - clone_style: ssh - - build-and-deploy: - runs-on: ubuntu-latest - steps: - - name: Checkout 🛎️ - uses: actions/checkout@v2 # If you're using actions/checkout@v2 you must set persist-credentials to false in most cases for the deployment to work correctly. - with: - persist-credentials: false - - - name: Install and Build 🔧 # This example project is built using npm and outputs the result to the 'build' folder. Replace with the commands required to build your project, or remove this step entirely if your site is pre-built. - run: | - yarn install - yarn run docs:build - - name: Deploy 🚀 - uses: JamesIves/github-pages-deploy-action@3.7.1 - with: - GITHUB_TOKEN: ${{ secrets.EVENTMESH }} - BRANCH: gh-pages - FOLDER: docs/.vuepress/dist # The folder the action should deploy. diff --git a/.github/workflows/greetings.yml b/.github/workflows/greetings.yml new file mode 100644 index 0000000000..8883906d2e --- /dev/null +++ b/.github/workflows/greetings.yml @@ -0,0 +1,72 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +name: Greetings + +on: [pull_request, issues] + +jobs: + greeting: + name: Greeting + runs-on: ubuntu-latest + steps: + - uses: actions/first-interaction@v1 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + issue-message: | + Welcome to the Apache EventMesh (incubating) community!! + We are glad that you are contributing by opening this issue. :D + + Please make sure to include all the relevant context. + We will be here shortly. + + If you are interested in contributing to our project, please let us know! + You can check out our contributing guide on [contributing to EventMesh](https://github.com/apache/incubator-eventmesh/blob/develop/CONTRIBUTING.md). + + Want to get closer to the community? + + WeChat Group: + ![wechat_qr](https://github.com/apache/incubator-eventmesh/blob/develop/docs/images/mesh-helper.png?raw=true) + + Mailing Lists: + | Name | Description |Subscribe |Unsubscribe|Archive + | ---- | ---- |---- | ---- | ---- | + |Users |User support and questions mailing list| [Subscribe](mailto:users-subscribe@eventmesh.incubator.apache.org) |[Unsubscribe](mailto:users-unsubscribe@eventmesh.incubator.apache.org) |[Mail Archives](https://lists.apache.org/list.html?users@eventmesh.apache.org)| + |Development |Development related discussions| [Subscribe](mailto:dev-subscribe@eventmesh.incubator.apache.org) |[Unsubscribe](mailto:dev-unsubscribe@eventmesh.incubator.apache.org) |[Mail Archives](https://lists.apache.org/list.html?dev@eventmesh.apache.org)| + |Commits |All commits to repositories| [Subscribe](mailto:commits-subscribe@eventmesh.incubator.apache.org) |[Unsubscribe](mailto:commits-unsubscribe@eventmesh.incubator.apache.org) |[Mail Archives](https://lists.apache.org/list.html?commits@eventmesh.apache.org)| + + pr-message: | + Welcome to the Apache EventMesh (incubating) community!! + This is your first PR in our project. We're very excited to have you onboard contributing. Your contributions are greatly appreciated! + + Please make sure that the changes are covered by tests. + We will be here shortly. + Let us know if you need any help! + + Want to get closer to the community? + + WeChat Group: + ![wechat_qr](https://github.com/apache/incubator-eventmesh/blob/develop/docs/images/mesh-helper.png?raw=true) + + Mailing Lists: + | Name | Description |Subscribe |Unsubscribe|Archive + | ---- | ---- |---- | ---- | ---- | + |Users |User support and questions mailing list| [Subscribe](mailto:users-subscribe@eventmesh.incubator.apache.org) |[Unsubscribe](mailto:users-unsubscribe@eventmesh.incubator.apache.org) |[Mail Archives](https://lists.apache.org/list.html?users@eventmesh.apache.org)| + |Development |Development related discussions| [Subscribe](mailto:dev-subscribe@eventmesh.incubator.apache.org) |[Unsubscribe](mailto:dev-unsubscribe@eventmesh.incubator.apache.org) |[Mail Archives](https://lists.apache.org/list.html?dev@eventmesh.apache.org)| + |Commits |All commits to repositories| [Subscribe](mailto:commits-subscribe@eventmesh.incubator.apache.org) |[Unsubscribe](mailto:commits-unsubscribe@eventmesh.incubator.apache.org) |[Mail Archives](https://lists.apache.org/list.html?commits@eventmesh.apache.org)| diff --git a/.licenserc.yaml b/.licenserc.yaml new file mode 100644 index 0000000000..5046f8e188 --- /dev/null +++ b/.licenserc.yaml @@ -0,0 +1,41 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +header: + license: + spdx-id: Apache-2.0 + copyright-owner: Apache Software Foundation + + paths-ignore: + - '.github/PULL_REQUEST_TEMPLATE' + - '.gitmodules' + - '**/.gitkeep' + - '**/.gitignore' + - '**/*.md' + - '**/*.json' + - '**/*.ftl' + - '**/*.iml' + - '**/*.ini' + - '**/*.crt' + - '**/*.pem' + - 'LICENSE' + - 'NOTICE' + - 'DISCLAIMER-WIP' + + comment: on-failure diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 4c2ba8ba57..0000000000 --- a/.travis.yml +++ /dev/null @@ -1,15 +0,0 @@ -dist: trusty - -language: java -jdk: -- oraclejdk8 - -script: -- travis_retry gradle check - -after_success: -- gradle clean test jacocoAllTestReport coveralls - -env: - global: - - secure: "T1QAuaAzcB7K8YjAAVVb4P9+W0JAdOFbyBwRxliyyoSZUShlIqa0eE7ioXHXWBP/d5f3XtROse6lq2qILqcU9sFSncKE2vRJlwJ5p7R23WIsCXdV70A9AVE2gLJcIJiOTMwd/YYYzNDrGLp3CSJNcKo8t7t70V2j/11I9xPTOHnaZ8FHGC3d/7bjfR/+g/3d4EOCvV8Vm6ndEmmailmF8OJ/kcbuRbArKIehjUwNDyQZfwAc9+vvPZlHgnQvR1pJ/KiK6muEIi7RQohDq7lMTmcc2LZSYgy/+aqFrmBcQwXScABFmSwysQ4KMXfrCqqMsBdmvno/NoKVGofHHKdym/oauv/G3lxLx5sgM9A7ZSFBK08x08r7u/6TDsTFmQ9LzVFDNo/OLZhxs3dr9x2C9Pa2A7IP1i1oVbbYkwBJv4z6o3khWpQAAY/IWijlCZ9vkjFfqdIXbvlPqamEaFRAmK5I3MVqL2+eBF+2Or/zwv4rXjo+v5LUKTfmDl77QUshPv6J+hblbBR0cJ/ZTSh9rHgdqhEzPGSt3e0YrEefkKWfinBr8TjIsHgQgmfU8Kz7gf7tvioOuAgKj4WlgEQJs3dPp7J5zxFVNMM6teAMFy8c2MtAdzKrs13Ri1qZWtL6B7JWlH6yhHqhKghyJhRjUFNVZnBkA/z9gzDKZ+tz2m8=" \ No newline at end of file diff --git a/CNAME b/CNAME deleted file mode 100644 index 8feffb14d1..0000000000 --- a/CNAME +++ /dev/null @@ -1 +0,0 @@ -www.eventmesher.com diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index de1080bc63..68c870c4b2 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,7 +1,7 @@ # Contributing to EventMesh -Welcome to EventMesh! This document is a guideline about how to contribute to EventMesh. -If you find something incorrect or missing, please leave comments / suggestions. +Welcome to EventMesh! This document is a guideline about how to contribute to EventMesh. If you find something incorrect +or missing, please leave comments / suggestions. ## Before you get started @@ -11,11 +11,11 @@ You should have JDK installed in your operating system. ## Contributing -We are always very happy to have contributions, whether for typo fix, bug fix or big new features. -Please do not ever hesitate to ask a question or send a pull request. +We are always very happy to have contributions, whether for typo fix, bug fix or big new features. Please do not ever +hesitate to ask a question or send a pull request. -We strongly value documentation and integration with other projects. -We are very glad to accept improvements for these aspects. +We strongly value documentation and integration with other projects. We are very glad to accept improvements for these +aspects. ### GitHub workflow @@ -31,29 +31,29 @@ Here are the workflow for contributors: 6. Push your commits to your forked repository 7. Create a pull request -Please follow [the pull request template](./.github/PULL_REQUEST_TEMPLATE.md). -Please make sure the PR has a corresponding issue. [GitHub Issues](https://github.com/WeBankFinTech/EventMesh/issues) +Please follow [the pull request template](./.github/PULL_REQUEST_TEMPLATE.md). Please make sure the PR has a +corresponding issue. [GitHub Issues](https://github.com/apache/incubator-eventmesh/issues) -After creating a PR, one or more reviewers will be assigned to the pull request. -The reviewers will review the code. +After creating a PR, one or more reviewers will be assigned to the pull request. The reviewers will review the code. -Before merging a PR, squash any fix review feedback, typo, merged, and rebased sorts of commits. -The final commit message should be clear and concise. +Before merging a PR, squash any fix review feedback, typo, merged, and rebased sorts of commits. The final commit +message should be clear and concise. ### Open an issue / PR -We use [GitHub Issues](https://github.com/WeBankFinTech/EventMesh/issues) and [Pull Requests](https://github.com/WeBankFinTech/EventMesh/pulls) for trackers. +We use [GitHub Issues](https://github.com/apache/incubator-eventmesh/issues) +and [Pull Requests](https://github.com/apache/incubator-eventmesh/pulls) for trackers. -If you find a typo in a document, find a bug in code, or want new features, or want to give suggestions, -you can [open an issue on GitHub](https://github.com/WeBankFinTech/EventMesh/issues/new) to report it. -Please follow the guideline message in the issue template. +If you find a typo in a document, find a bug in code, or want new features, or want to give suggestions, you +can [open an issue on GitHub](https://github.com/apache/incubator-eventmesh/issues/new) to report it. Please follow the +guideline message in the issue template. -If you want to contribute, please follow the [contribution workflow](#github-workflow) and create a new pull request. -If your PR contains large changes, e.g. component refactor or new components, please write detailed documents -about its design and usage. +If you want to contribute, please follow the [contribution workflow](#github-workflow) and create a new pull request. If +your PR contains large changes, e.g. component refactor or new components, please write detailed documents about its +design and usage. -Note that a single pull request should not be too large. If heavy changes are required, it's better to separate the changes -to a few individual PRs. +Note that a single pull request should not be too large. If heavy changes are required, it's better to separate the +changes to a few individual PRs. ### Code review diff --git a/CONTRIBUTING.zh-CN.md b/CONTRIBUTING.zh-CN.md index 29e6367267..742ccbdc32 100644 --- a/CONTRIBUTING.zh-CN.md +++ b/CONTRIBUTING.zh-CN.md @@ -1,7 +1,6 @@ # 贡献给EventMesh -欢迎使用EventMesh! 本文档是有关如何为EventMesh做出贡献的指南。 -如果发现不正确或缺失的内容,请留下评论/建议。 +欢迎使用EventMesh! 本文档是有关如何为EventMesh做出贡献的指南。 如果发现不正确或缺失的内容,请留下评论/建议。 ## 开始之前 @@ -11,11 +10,9 @@ ## 贡献 -无论是对于拼写错误,BUG修复还是重要的新功能,我们总是很乐意为您做出贡献。 -请不要犹豫,提出问题或发送请求请求。 +无论是对于拼写错误,BUG修复还是重要的新功能,我们总是很乐意为您做出贡献。 请不要犹豫,提出问题或发送请求请求。 -我们非常重视文档以及与其他项目的集成。 -我们很高兴接受这些方面的改进。 +我们非常重视文档以及与其他项目的集成。 我们很高兴接受这些方面的改进。 ### GitHub工作流程 @@ -23,39 +20,33 @@ 这是贡献者的工作流程 : -1.Fork到您个人仓库 -2.克隆到本地存储库 -3.创建一个新分支并对其进行处理 -4.保持分支同步 -5.提交您的更改(确保您的提交消息简明扼要) -6.将您的提交推送到分叉的存储库 -7.创建PR合并请求 +1. Fork到您个人仓库 +2. 克隆到本地存储库 +3. 创建一个新分支并对其进行处理 +4. 保持分支同步 +5. 提交您的更改(确保您的提交消息简明扼要) +6. 将您的提交推送到分叉的存储库 +7. 创建PR合并请求 请遵循[Pull Requests模板](./.github/PULL_REQUEST_TEMPLATE.md). -请确保PR对应有相应的问题. [GitHub Issues](https://github.com/WeBankFinTech/EventMesh/issues) +请确保PR对应有相应的问题. [GitHub Issues](https://github.com/apache/incubator-eventmesh/issues) -创建PR后,将为拉取请求分配一个或多个审阅者。 -审阅者将审阅代码。 +创建PR后,将为拉取请求分配一个或多个审阅者。 审阅者将审阅代码。 -在合并PR之前,请压缩所有修订审阅反馈,拼写错误,合并的内容和基于基础的提交内容。 -最终的提交消息应该清晰简洁。 +在合并PR之前,请压缩所有修订审阅反馈,拼写错误,合并的内容和基于基础的提交内容。 最终的提交消息应该清晰简洁。 ### 打开问题/ PR -我们将使用Issues和Pull Requests作为跟踪器 -[GitHub Issues](https://github.com/WeBankFinTech/EventMesh/issues) -[Pull Requests](https://github.com/WeBankFinTech/EventMesh/pulls) +我们将使用Issues和Pull Requests作为跟踪器 +[GitHub Issues](https://github.com/apache/incubator-eventmesh/issues) +[Pull Requests](https://github.com/apache/incubator-eventmesh/pulls) -如果您在文档中发现拼写错误,在代码中发现错误,想要新功能或提出建议, -您可以提出问题[在GitHub上打开问题](https://github.com/WeBankFinTech/EventMesh/issues/new) +如果您在文档中发现拼写错误,在代码中发现错误,想要新功能或提出建议, 您可以提出问题[在GitHub上打开问题](https://github.com/apache/incubator-eventmesh/issues/new) 请按照问题模板中的准则消息进行操作。 -如果您想贡献,请遵循[贡献工作流程](#github-workflow)并创建一个新的拉取请求。 -如果您的PR包含较大的更改,例如组件重构或新组件,请写详细文档 -有关其设计和使用的信息。 +如果您想贡献,请遵循[贡献工作流程](#github-workflow)并创建一个新的拉取请求。 如果您的PR包含较大的更改,例如组件重构或新组件,请写详细文档 有关其设计和使用的信息。 -请注意,单个拉取请求不应太大。如果需要进行重大更改,最好将更改分开 -到一些个人PR。 +请注意,单个拉取请求不应太大。如果需要进行重大更改,最好将更改分开 到一些个人PR。 ### 代码审查 @@ -67,4 +58,4 @@ ## 社区 -### 联系我们 \ No newline at end of file +### 联系我们 diff --git a/DISCLAIMER-WIP b/DISCLAIMER-WIP new file mode 100644 index 0000000000..e72dd1b9d0 --- /dev/null +++ b/DISCLAIMER-WIP @@ -0,0 +1,19 @@ +Apache EventMesh is an effort undergoing incubation at The Apache Software Foundation (ASF), +sponsored by the Apache Incubator PMC. + +Incubation is required of all newly accepted projects until a further review indicates that the infrastructure, +communications, and decision-making process have stabilized in a manner consistent with other successful ASF projects. + +While incubation status is not necessarily a reflection of the completeness or stability of the code, +it does indicate that the project has yet to be fully endorsed by the ASF. + +Some of the incubating project’s releases may not be fully compliant with ASF policy. +For example, releases may have incomplete or un-reviewed licensing conditions. +What follows is a list of issues the project is currently aware of (this list is likely to be incomplete): + +1- Releases may have incomplete licensing conditions + + +If you are planning to incorporate this work into your product/project, +please be aware that you will need to conduct a thorough licensing review to determine the overall implications of including this work. +For the current status of this project through the Apache Incubator, visit: https://incubator.apache.org/projects/eventmesh.html diff --git a/NOTICE b/NOTICE new file mode 100644 index 0000000000..19d0bec195 --- /dev/null +++ b/NOTICE @@ -0,0 +1,5 @@ +Apache EventMesh (incubating) +Copyright 2021 The Apache Software Foundation + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). diff --git a/README.md b/README.md index 968c229091..43cfc861ab 100644 --- a/README.md +++ b/README.md @@ -1,73 +1,91 @@ -[![Build Status](https://www.travis-ci.org/WeBankFinTech/DeFiBus.svg?branch=master)](https://www.travis-ci.org/WeBankFinTech/EventMesh) -[![Coverage Status](https://coveralls.io/repos/github/WeBankFinTech/DeFiBus/badge.svg?branch=master)](https://coveralls.io/github/WeBankFinTech/EventMesh?branch=master) -[![GitHub release](https://img.shields.io/badge/release-download-orange.svg)](https://github.com/WeBankFinTech/EventMesh/releases) +# Apache EventMesh (incubating) +[![CI status](https://github.com/apache/incubator-eventmesh/actions/workflows/ci.yml/badge.svg)](https://github.com/apache/incubator-eventmesh/actions/workflows/ci.yml) +[![CodeCov](https://codecov.io/gh/apache/incubator-eventmesh/branch/develop/graph/badge.svg)](https://codecov.io/gh/apache/incubator-eventmesh) +[![Language grade: Java](https://img.shields.io/lgtm/grade/java/g/apache/incubator-eventmesh.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/apache/incubator-eventmesh/context:java) +[![Total alerts](https://img.shields.io/lgtm/alerts/g/apache/incubator-eventmesh.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/apache/incubator-eventmesh/alerts/) +[![GitHub release](https://img.shields.io/badge/release-download-orange.svg)](https://github.com/apache/incubator-eventmesh/releases) [![License](https://img.shields.io/badge/license-Apache%202-4EB1BA.svg)](https://www.apache.org/licenses/LICENSE-2.0.html) [点我查看中文版](README.zh-CN.md) -## What is Event Mesh? -This figure shows the positioning of the event mesh relative to other similar technologies (such as service mesh) in the application framework. +![logo](docs/images/logo2.png) +## What is EventMesh? +EventMesh(incubating) is a dynamic cloud-native eventing infrastruture used to decouple the application and backend middleware layer, which supports a wide range of use cases that encompass complex multi-cloud, widely distributed topologies using diverse technology stacks. + +![architecture1](docs/images/eventmesh-multi-runtime.png) + +**EventMesh Ecosystem:** + ![architecture1](docs/images/eventmesh-define.png) -Event Mesh is a dynamic plug-in cloud-native basic service layer used to decouple the application and middleware layer. It provides flexible, reliable and fast event distribution, and can be managed. -![architecture1](docs/images/eventmesher.png) +**EventMesh Architecture:** + +![architecture1](docs/images/eventmesh-runtime.png) + +**EventMesh Cloud Native:** + +![architecture2](docs/images/eventmesh-panels.png) + + +**Support connecting event store:** + +* [RocketMQ](https://github.com/apache/rocketmq):RocketMQ is a distributed messaging and streaming platform with low latency, high performance and reliability, trillion-level capacity and flexible scalability. -Cloud Native Event Mesh: -![architecture2](docs/images/bus.png) +**Components:** -The event mesh allows events from one application to be dynamically routed to any other application. -General functions of the event mesh: -* Event driven; -* Event governance; -* Dynamic routing; -* Cloud native +* **eventmesh-runtime** : an middleware to transmit events between event producers and consumers, support cloud native apps and microservices. +* **eventmesh-sdk-java** : currently supports HTTP and TCP protocols. +* **eventmesh-connector-api** : an api layer based on OpenMessaging api and SPI pluggin, which can be implemented by popular EventStores such as IMDG, Messaging Engine and OSS etc. +* **eventmesh-connector-rocketmq** : an implementation of eventmesh-connector-api, pub event to or sub event from RocketMQ as EventStore. -Dependent components: -* DeFiBus : a distributed messaging platform with low latency, high performance and reliability, flexible scalability. [DeFiBus](https://github.com/WeBankFinTech/DeFiBus) -* RocketMQ +**Protocol:** -Key components: -* eventmesh-emesher : an middleware to transmit events between event producers and consumers, support cloud native apps and microservices -* eventmesh-sdk-java : currently supports HTTP and TCP protocols, and will support gRPC in the future -* eventmesh-registry : automatically routes events between applications and services connected to seperate event meshers, manage eventmesh-emesher +The protocol of eventmesh is easier and more convenient, you can read more [here](docs/en/instructions/eventmesh-runtime-protocol.md) ## RoadMap | version | feature | | ---- | ---- | -| v1.0.0 |Support DeFiBus as eventstore, support pub/sub, http api, java-sdk| -| v1.1.0 |Support rocketmq as eventstore| +| v1.0.0 |Support java-sdk , tcp pub/sub, http pub| +| v1.1.0 |Support RocketMQ as eventstore| | v1.1.1 |Support https| -| v1.2.0 |Support transaction event| -| v1.3.0 |Support Plug-in architectur| -| |Support Event Sourcing| -| |Support Event orchestration| -| |Support Dashboard| -| |Support Event governance| -| |Support Nacos as an event router| -| |Support Promethus| -| |Support Skywalking| -| |Support Spiffe| -| |Support gRPC| -| |Support c/go/python/nodejs SDK| +| v1.2.0 |Support EventMesh store layer pluggable by OpenMessaging Pub/Sub API, http sub, docker| +| V1.3.0 |Support CloudEvents, Event Streaming| +| |Support Event function,triggers and bindings| +| |Support Event orchestration, Servelss workflow| +| |Support Event transaction| +| |Support Event schema| +| |Support Event governance, dashboard| +| |Support Event security| +| |Support multi language SDK(c\go\python\wasm)| +| |Support Promethus as metrics| +| |Support Skywalking as tracing| +| |Support streaming event store| +| |Support gRPC protocol| +| |Support MQTT protocol| ## Quick Start -1. Build and deploy event-store([DeFiBus](https://github.com/WeBankFinTech/DeFiBus)), - see instruction ['event-store quickstart'](docs/en/instructions/eventmesh-store-quickstart.md). -2. Build and deploy eventmesh-emesher, see instruction ['eventmesh-emesher quickstart'](docs/en/instructions/eventmesh-emesher-quickstart.md). -3. Run eventmesh-sdk-java demo, see instruction ['eventmesh-sdk-java quickstart'](docs/en/instructions/eventmesh-sdk-java-quickstart.md). +1. Build and deploy event-store(default RocketMQ), see [instruction](https://rocketmq.apache.org/docs/quick-start/). +2. Build and deploy eventmesh-runtime, see instruction ['eventmesh-runtime quickstart'](docs/en/instructions/eventmesh-runtime-quickstart.md). +3. Run eventmesh-sdk-java demo, see instruction ['eventmesh-sdk-java quickstart'](docs/en/instructions/eventmesh-sdk-java-quickstart.md). ## Contributing -Contributions are always welcomed! Please see [CONTRIBUTING](CONTRIBUTING.md) for detailed guidelines +Contributions are always welcomed! Please see [CONTRIBUTING](CONTRIBUTING.md) for detailed guidelines. -You can start with the issues labeled with good first issue. -[GitHub Issues](https://github.com/WeBankFinTech/EventMesh/issues) +You can start with the issues labeled with good first issue. +[GitHub Issues](https://github.com/apache/incubator-eventmesh/issues) ## License -[Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.html) Copyright (C) Apache Software Foundation +[Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.html) Copyright (C) Apache Software Foundation. -## Contacts +## Community WeChat group: ![wechat_qr](docs/images/mesh-helper.png) +Mailing Lists: +| Name | Description |Subscribe |Unsubscribe|Archive +| ---- | ---- |---- | ---- | ---- | +|Users |User support and questions mailing list| [Subscribe](mailto:users-subscribe@eventmesh.incubator.apache.org) |[Unsubscribe](mailto:users-unsubscribe@eventmesh.incubator.apache.org) |[Mail Archives](https://lists.apache.org/list.html?users@eventmesh.apache.org)| +|Development |Development related discussions| [Subscribe](mailto:dev-subscribe@eventmesh.incubator.apache.org) |[Unsubscribe](mailto:dev-unsubscribe@eventmesh.incubator.apache.org) |[Mail Archives](https://lists.apache.org/list.html?dev@eventmesh.apache.org)| +|Commits |All commits to repositories| [Subscribe](mailto:commits-subscribe@eventmesh.incubator.apache.org) |[Unsubscribe](mailto:commits-unsubscribe@eventmesh.incubator.apache.org) |[Mail Archives](https://lists.apache.org/list.html?commits@eventmesh.apache.org)| diff --git a/README.zh-CN.md b/README.zh-CN.md index 5ceb16a504..8dadfaa24a 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -1,69 +1,87 @@ -[![Build Status](https://www.travis-ci.org/WeBankFinTech/DeFiBus.svg?branch=master)](https://www.travis-ci.org/WeBankFinTech/EventMesh) -[![Coverage Status](https://coveralls.io/repos/github/WeBankFinTech/DeFiBus/badge.svg?branch=master)](https://coveralls.io/github/WeBankFinTech/EventMesh?branch=master) -[![GitHub release](https://img.shields.io/badge/release-download-orange.svg)](https://github.com/WeBankFinTech/EventMesh/releases) +# Apache EventMesh (incubating) +[![CI status](https://github.com/apache/incubator-eventmesh/actions/workflows/ci.yml/badge.svg)](https://github.com/apache/incubator-eventmesh/actions/workflows/ci.yml) +[![CodeCov](https://codecov.io/gh/apache/incubator-eventmesh/branch/develop/graph/badge.svg)](https://codecov.io/gh/apache/incubator-eventmesh) +[![Language grade: Java](https://img.shields.io/lgtm/grade/java/g/apache/incubator-eventmesh.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/apache/incubator-eventmesh/context:java) +[![Total alerts](https://img.shields.io/lgtm/alerts/g/apache/incubator-eventmesh.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/apache/incubator-eventmesh/alerts/) +[![GitHub release](https://img.shields.io/badge/release-download-orange.svg)](https://github.com/apache/incubator-eventmesh/releases) [![License](https://img.shields.io/badge/license-Apache%202-4EB1BA.svg)](https://www.apache.org/licenses/LICENSE-2.0.html) +![logo](docs/images/logo2.png) ## 什么是Event Mesh? -该图显示了Event Mesh相对于应用程序框架中其他类似技术(例如Service Mesh)的定位. +EventMesh是一个动态的云原生事件驱动架构基础设施,用于分离应用程序和后端中间件层,它支持广泛的用例,包括复杂的混合云、使用了不同技术栈的分布式架构。 + +![architecture1](docs/images/eventmesh-multi-runtime.png) + +**EventMesh生态:** ![architecture1](docs/images/eventmesh-define.png) -Event Mesh是一个动态的插件式云原生基础服务层,用于分离应用程序和中间件层。它提供了灵活,可靠和快速的事件分发,并且可以进行管理: -![architecture1](docs/images/eventmesher.png) +**EventMesh架构:** + +![architecture1](docs/images/eventmesh-runtime.png) + +**EventMesh云原生结构:** + +![architecture2](docs/images/eventmesh-panels.png) -云原生Event Mesh: -![architecture2](docs/images/bus.png) +**支持连接的事件存储:** -Event Mesh允许将来自一个应用程序的事件动态路由到任何其他应用程序. -Event Mesh的一般功能: -* 事件驱动; -* 事件治理; -* 动态路由; -* 云原生 +* [RocketMQ](https://github.com/apache/rocketmq):RocketMQ是一个分布式消息流平台,具有低延迟、高性能和可靠性、万亿级容量和灵活的可伸缩性。 -依赖部件: -* DeFiBus:具有低延迟,高性能和可靠性,和灵活可伸缩性的分布式消息传递平台 [DeFiBus](https://github.com/WeBankFinTech/DeFiBus) -* RocketMQ +**关键部件:** -关键部件: -* eventmesh-emesher:一种中间件,用于在事件产生者和使用者之间传输事件,支持云原生应用程序和微服务 -* eventmesh-sdk-java:当前支持HTTP和TCP协议,未来会支持gRPC等 -* eventmesh-registry:自动在连接到单独事件网格器的应用程序和服务之间路由事件, 管理emesher +* **eventmesh-runtime**:一种中间件,用于在事件产生者和使用者之间传输事件,支持云原生应用程序和微服务 +* **eventmesh-sdk-java**:当前支持HTTP和TCP协议,未来会支持gRPC等 +* **eventmesh-connector-api**:一个基于OpenMessaging api和SPI插件机制的接口层,可以有很多不同的事件存储的实现,比如IMDG,Messaging Engine和OSS等 +* **eventmesh-connector-rocketmq** : 一种基于eventmesh-connector-api的实现,该实现支持将RocketMQ作为事件存储,实现事件的发布与订阅 + +**通信协议:** + +eventmesh的通信协议更加简洁方便,详细内容,阅读更多[这里](docs/cn/instructions/eventmesh-runtime-protocol.zh-CN.md) ## RoadMap | version | feature | | ---- | ---- | -| v1.0.0 |Support DeFiBus as eventstore, support pub/sub, http api, java-sdk| -| v1.1.0 |Support rocketmq as eventstore| +| v1.0.0 |Support java-sdk , tcp pub/sub, http pub| +| v1.1.0 |Support RocketMQ as eventstore| | v1.1.1 |Support https| -| v1.2.0 |Support transaction event| -| v1.3.0 |Support Plug-in architectur| -| |Support Event Sourcing| -| |Support Event orchestration| -| |Support Dashboard| -| |Support Event governance| -| |Support Nacos as an event router| -| |Support Promethus| -| |Support Skywalking| -| |Support Spiffe| -| |Support gRPC| -| |Support c/go/python/nodejs SDK| +| v1.2.0 |Support EventMesh store layer pluggable by OpenMessaging Pub/Sub API, http sub, docker| +| V1.3.0 |Support CloudEvents, Event Streaming| +| |Support Event function,triggers and bindings| +| |Support Event orchestration, Servelss workflow| +| |Support Event transaction| +| |Support Event schema| +| |Support Event governance, dashboard| +| |Support Event security| +| |Support multi language SDK(c\go\python\wasm)| +| |Support Promethus as metrics| +| |Support Skywalking as tracing| +| |Support streaming event store| +| |Support gRPC protocol| +| |Support MQTT protocol| ## 快速开始 -1. 构建并部署event-store([DeFiBus](https://github.com/WeBankFinTech/DeFiBus)) - 请参见说明['event-store quickstart.zh-CN'](docs/cn/instructions/eventmesh-store-quickstart.zh-CN.md) -2. 构建并部署eventmesh-emesher,请参见说明['eventmesh-emesher quickstart.zh-CN'](docs/cn/instructions/eventmesh-emesher-quickstart.zh-CN.md) +1. 构建并部署event-store(RocketMQ), 请参见[说明](https://rocketmq.apache.org/docs/quick-start/) +2. 构建并部署eventmesh-runtime,请参见说明['eventmesh-runtime quickstart.zh-CN'](docs/cn/instructions/eventmesh-runtime-quickstart.zh-CN.md) 3. 运行eventmesh-sdk-java演示,请参见说明['eventmesh-sdk-java quickstart.zh-CN'](docs/cn/instructions/eventmesh-sdk-java-quickstart.zh-CN.md) ## 贡献 永远欢迎参与共建, 请参阅[贡献](CONTRIBUTING.zh-CN.md)了解详细指南 -您可以从问题开始. -[GitHub Issues](https://github.com/WeBankFinTech/EventMesh/issues) +您可以从问题开始. +[GitHub Issues](https://github.com/apache/incubator-eventmesh/issues) ## License [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.html) Copyright (C) Apache Software Foundation -## 联系人 +## 开发社区 微信群: ![wechat_qr](docs/images/mesh-helper.png) + +Mailing Lists: + +| 列表名称 | 描述 |订阅 |取消订阅|邮件列表存档 +| ---- | ---- |---- | ---- | ---- | +|Users |用户支持与用户问题| [点击订阅](mailto:users-subscribe@eventmesh.incubator.apache.org) |[点击取消订阅](mailto:users-unsubscribe@eventmesh.incubator.apache.org) |[邮件列表存档](https://lists.apache.org/list.html?users@eventmesh.apache.org)| +|Development |开发相关| [点击订阅](mailto:dev-subscribe@eventmesh.incubator.apache.org) |[点击取消订阅](mailto:dev-unsubscribe@eventmesh.incubator.apache.org) |[邮件列表存档](https://lists.apache.org/list.html?dev@eventmesh.apache.org)| +|Commits |所有与仓库相关的commits信息通知| [点击订阅](mailto:commits-subscribe@eventmesh.incubator.apache.org) |[点击取消订阅](mailto:commits-unsubscribe@eventmesh.incubator.apache.org) |[邮件列表存档](https://lists.apache.org/list.html?commits@eventmesh.apache.org)| diff --git a/_config.yml b/_config.yml deleted file mode 100644 index c4192631f2..0000000000 --- a/_config.yml +++ /dev/null @@ -1 +0,0 @@ -theme: jekyll-theme-cayman \ No newline at end of file diff --git a/build.gradle b/build.gradle index bddbceba1f..12a2b005e9 100644 --- a/build.gradle +++ b/build.gradle @@ -17,6 +17,8 @@ import java.util.concurrent.TimeUnit +//import com.github.spotbugs.snom.SpotBugsTask + buildscript { repositories { maven { @@ -26,46 +28,66 @@ buildscript { maven { url "https://plugins.gradle.org/m2/" } - } dependencies { - //classpath("net.sourceforge.pmd:pmd-java:5.4.1") - //classpath("com.puppycrawl.tools:checkstyle:6.16.1") - classpath("gradle.plugin.com.github.spotbugs.snom:spotbugs-gradle-plugin:4.0.7") - classpath('com.github.spotbugs:spotbugs:4.0.0') + classpath "gradle.plugin.com.github.spotbugs.snom:spotbugs-gradle-plugin:4.7.1" } } allprojects { apply plugin: 'java' + apply plugin: "eclipse" + apply plugin: "idea" + apply plugin: "project-reports" + apply plugin: "maven-publish" + apply plugin: "com.github.spotbugs" + apply plugin: "project-reports" + apply plugin: "jacoco" + apply plugin: "pmd" + apply plugin: "java-library" + apply plugin: 'signing' + +// print(project) +// if (project.findProperty("jdk") == "1.7") { +// sourceCompatibility = 1.7 +// targetCompatibility = 1.7 +// version = version + "-jdk7" +// } else { +// sourceCompatibility = 1.8 +// targetCompatibility = 1.8 +// version = version + "-jdk8" +// } + [compileJava, compileTestJava, javadoc]*.options*.encoding = 'UTF-8' + compileJava.options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" clean.doFirst { delete 'build' delete 'dist' + delete 'out' } - if (project.findProperty("snapshot") instanceof String) { - if (project.property("snapshot").toBoolean()) - version = version + "-SNAPSHOT" + if ((project.findProperty("snapshot") instanceof String) && (Boolean.valueOf(project.property("snapshot")))) { + version = version + "-SNAPSHOT" } } task tar(type: Tar) { extension = 'tar.gz' compression = Compression.GZIP - archiveName = project.name + '_' + project.version + '.' + extension - destinationDir = new File(projectDir, 'build') + archiveFileName = project.name + '_' + project.version + '.' + extension + destinationDirectory = new File(projectDir, 'build') into('/') { from 'dist' } } + task zip(type: Zip) { extension = 'zip' - archiveName = project.name + '.' + project.version + '.' + extension - destinationDir = new File(projectDir, 'build') + archiveFileName = project.name + '.' + project.version + '.' + extension + destinationDirectory = new File(projectDir, 'build') into('/') { from 'dist' } @@ -73,33 +95,95 @@ task zip(type: Zip) { subprojects { + List fastjson = [ + "com.alibaba:fastjson:1.2.71" + ] - apply plugin: "maven" - apply plugin: "eclipse" - apply plugin: "idea" - apply plugin: "project-reports" - apply plugin: "jacoco" -// apply plugin: "checkstyle" - apply plugin: "pmd" - apply plugin: 'com.github.spotbugs' + List jackson = [ + 'com.fasterxml.jackson.core:jackson-databind:2.11.0', + 'com.fasterxml.jackson.core:jackson-core:2.11.0', + 'com.fasterxml.jackson.core:jackson-annotations:2.11.0' + ] - [compileJava, compileTestJava, javadoc]*.options*.encoding = 'UTF-8' + List apache_commons = [ + "org.apache.commons:commons-lang3:3.6", + "org.apache.commons:commons-collections4:4.1" + ] - compileJava.options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" + List httpclient = [ + "org.apache.httpcomponents:httpclient:4.5.2" + ] + + List guava = [ + "com.google.guava:guava:29.0-jre" + ] + + List netty = [ + "io.netty:netty-all:4.1.49.Final" + ] + + List junit = [ + "junit:junit:4.12" + ] + + List log = [ + "org.slf4j:slf4j-api:1.7.30" + ] + + List log4j2 = [ + "org.apache.logging.log4j:log4j-api:2.13.3", + "org.apache.logging.log4j:log4j-core:2.13.3", + "org.apache.logging.log4j:log4j-slf4j-impl:2.13.3", + "org.apache.logging.log4j:log4j-web:2.13.3", + "com.lmax:disruptor:3.4.2" + ] + + List common = [ + "commons-io:commons-io:2.4", + "org.apache.commons:commons-text:1.9" + ] + + List assertj = [ + "org.assertj:assertj-core:2.6.0" + ] + + List mock = [ + "org.mockito:mockito-core:2.23.0", + "org.powermock:powermock-module-junit4:2.0.2", + "org.powermock:powermock-api-mockito2:2.0.2", + ] + + dependencies { + implementation apache_commons, guava, log, log4j2, fastjson, jackson, common, httpclient, netty + testImplementation apache_commons, guava, log, log4j2, fastjson, jackson, common, junit, assertj, mock, httpclient, netty + spotbugsPlugins 'com.h3xstream.findsecbugs:findsecbugs-plugin:1.11.0' + spotbugsPlugins 'com.mebigfatguy.fb-contrib:fb-contrib:7.4.7' + } + + sourceSets { + main { + java.srcDirs = ['src/main/java'] + } + + test { + java.srcDirs = ['src/test/java'] + } + } + + clean.doFirst { + delete 'build' + delete 'dist' + } jacoco { - toolVersion = "0.8.5" - reportsDir = file("$buildDir/reports/jacoco") + toolVersion = "0.8.6" } jacocoTestReport { -// sourceSets sourceSets.main -// executionData files("$buildDir/jacoco/jacocoTest.exec") - reports { - xml.enabled false + xml.enabled true csv.enabled false - html.destination file("${buildDir}/reports/jacoco") + html.enabled false } } @@ -116,100 +200,92 @@ subprojects { // xml.enabled false // html.enabled true // } +// } +// +// sourceSets { +// main { +// java { +// srcDir 'src/main/java' +// } +// +// resources { +// srcDir 'src/main/resources' +// } +// +// } +// +// test { +// java { +// srcDir 'src/test/java' +// } +// +// resources { +// srcDir 'src/test/resources' +// } +// +// } // } spotbugs { - toolVersion = '4.0.2' + //toolVersion = '4.2.3' ignoreFailures = true - effort = "default" - reportLevel = "default" showProgress = true + showStackTraces = true + //reportLevel = 'default' + effort = 'default' + //visitors = [ 'FindSqlInjection', 'SwitchFallthrough' ] + //omitVisitors = [ 'FindNonShortCircuit' ] + reportsDir = file("$buildDir/reports/spotbugs") + //includeFilter = file('spotbugs-include.xml') + //excludeFilter = file('spotbugs-exclude.xml') + //onlyAnalyze = ['org/apache/eventmesh/**'] + projectName = rootProject.name + release = version + extraArgs = ['-nested:false'] + //jvmArgs = [ '-Duser.language=zh' ] + maxHeapSize = '256m' + } -// tasks.withType(com.github.spotbugs.SpotBugsTask) { -// sourceDirs = [sourceSets.main] -// reports { -// xml.enabled = false -// html.enabled = true -// } -// } spotbugsMain { + reports { xml.enabled = false - html.enabled = true + html { + enabled = true + destination = file("$buildDir/reports/spotbugs/main/spotbugs.html") + stylesheet = 'fancy-hist.xsl' + } } } spotbugsTest { reports { xml.enabled = false - html.enabled = true + html { + enabled = true + destination = file("$buildDir/reports/spotbugs/test/spotbugs.html") + stylesheet = 'fancy-hist.xsl' + } } } - tasks.withType(Pmd) { - reports { - xml.enabled = false - html.enabled = true - } - } +// tasks.withType(Pmd) { +// reports { +// xml.enabled = false +// html.enabled = true +// } +// } pmd { consoleOutput = true toolVersion = "6.23.0" - rulePriority = 5 + rulesMinimumPriority = 5 ruleSets = ["category/java/errorprone.xml", "category/java/bestpractices.xml"] ignoreFailures = true } - - - List junit = [ - "junit:junit:4.12" - ] - - List apache_commons = [ - "org.apache.commons:commons-collections4:4.1", - "commons-beanutils:commons-beanutils:1.9.3", - "org.apache.commons:commons-lang3:3.6", - "commons-codec:commons-codec:1.10" - - ] - - List log = [ - "org.slf4j:slf4j-api:1.7.30" - ] - - List guava = [ - "com.google.guava:guava:29.0-jre" - ] - - List fastjson = [ - "com.alibaba:fastjson:1.2.71" - ] - - List common_io = [ - "commons-io:commons-io:2.4" - ] - - List assertj = [ - "org.assertj:assertj-core:2.6.0" - ] - - List mock = [ -// "org.mockito:mockito-core:1.10.19", -"org.mockito:mockito-core:2.23.0", -"org.powermock:powermock-module-junit4:2.0.2", -"org.powermock:powermock-api-mockito2:2.0.2", - ] - - dependencies { - compile apache_commons, guava, log, fastjson, common_io - testCompile apache_commons, guava, log, fastjson, common_io, junit, assertj, mock - runtime apache_commons, guava, log, fastjson, common_io - } - jar { manifest { attributes("Specification-Version": project.version, @@ -235,10 +311,15 @@ subprojects { copy { into('../dist/apps/') from project.jar.getArchivePath() + exclude 'eventmesh-common*.jar' + exclude 'eventmesh-connector-api*.jar' + exclude 'eventmesh-starter*.jar' + exclude 'eventmesh-test*.jar' + exclude 'eventmesh-sdk*.jar' } copy { into '../dist/lib' - from project.configurations.runtime + from project.configurations.runtimeClasspath exclude '**/*.properties*' exclude '**/*testng*.jar' exclude '**/*powermock*.jar' @@ -248,26 +329,27 @@ subprojects { exclude '**/*log4j2.xml*' exclude '**/spring-boot-devtools*.jar' exclude '**/mumble-sdk-test*.jar' - exclude '**/defibus*.jar' - exclude '*log4j*.jar' + exclude '*connector-rocketmq*.jar' + exclude 'eventmesh-runtime*.jar' +// exclude '*log4j*.jar' exclude 'commons-collections-3.2.2.jar' } copy { into '../dist/bin' - from '../script' + from '../eventmesh-runtime/bin' } copy { into '../dist/conf' - from '../conf/' + from '../eventmesh-runtime/conf' } } } javadoc { source = sourceSets.main.java - classpath = configurations.compile + //classpath = configurations.implementation destinationDir = reporting.file("javadoc") } @@ -287,15 +369,82 @@ subprojects { archives packageSources } + if (!Boolean.valueOf(signEnabled)) { + tasks.whenTaskAdded {task -> + if(task.name.contains("sign")) { + task.enabled = false + } + } + } + repositories { maven { url "https://maven.aliyun.com/repository/public" } mavenCentral() + mavenLocal() } + configurations.all { resolutionStrategy.cacheChangingModulesFor 0, TimeUnit.SECONDS resolutionStrategy.cacheDynamicVersionsFor 0, TimeUnit.SECONDS } + publishing { + publications { + mavenJava(MavenPublication) { + from components.java + artifact packageSources + artifact packageJavadoc + versionMapping { + usage('java-api') { + fromResolutionOf('runtimeClasspath') + } + usage('java-runtime') { + fromResolutionResult() + } + } + pom { + name = 'EventMesh' + description = 'Apache EventMesh' + url = 'https://github.com/apache/incubator-eventmesh' + licenses { + license { + name = 'The Apache License, Version 2.0' + url = 'http://www.apache.org/licenses/LICENSE-2.0.txt' + } + } + developers { + developer { + id = 'Apache EventMesh(incubating)' + name = 'Apache EventMesh(incubating) of ASF' + url = 'https://eventmesh.apache.org/' + } + } + scm { + connection = 'scm:git:git@github.com:apache/incubator-eventmesh.git' + developerConnection = 'scm:git:git@github.com:apache/incubator-eventmesh.git' + url = 'https://github.com/apache/incubator-eventmesh' + } + } + } + } + repositories { + maven { + def releasesRepoUrl = 'https://repository.apache.org/content/repositories/releases/' + def snapshotsRepoUrl = 'https://repository.apache.org/content/repositories/snapshots/' + url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl + credentials { + username apacheUserName + password apachePassWord + } + + } + } + } + + signing { + sign publishing.publications.mavenJava + } + } diff --git a/docker/centos7-jdk8/Dockerfile b/docker/centos7-jdk8/Dockerfile new file mode 100644 index 0000000000..7d968413d5 --- /dev/null +++ b/docker/centos7-jdk8/Dockerfile @@ -0,0 +1,31 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +FROM docker.io/centos:7 + +MAINTAINER mikexue + +RUN yum update -y && yum install net-tools -y && yum install lrzsz -y && yum install vim -y +ADD jdk-8u281-linux-x64.tar.gz /usr/local/src/ +RUN ln -s /usr/local/src/jdk1.8.0_281/ /usr/local/jdk + +ENV JAVA_HOME /usr/local/jdk +ENV JRE_HOME $JAVA_HOME/jre +ENV CLASSPATH .:$JAVA_HOME/lib/:$JRE_HOME/lib/ +ENV PATH $PATH:$JAVA_HOME/bin diff --git a/docker/eventmesh-rocketmq/Dockerfile b/docker/eventmesh-rocketmq/Dockerfile new file mode 100644 index 0000000000..5b61cc4f5d --- /dev/null +++ b/docker/eventmesh-rocketmq/Dockerfile @@ -0,0 +1,35 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +FROM centos7-jdk8:latest + +MAINTAINER mikexue mike_xwm@126.com + +WORKDIR /data +RUN mkdir /data/app +ADD EventMesh_1.2.0-SNAPSHOT.tar.gz /data/app/eventmesh +WORKDIR /data/app/eventmesh/bin + +EXPOSE 10000 +EXPOSE 10105 + +ENV DOCKER true + +CMD sh start.sh + diff --git a/docs/.vuepress/config.js b/docs/.vuepress/config.js index 76c3bc6a09..870c593b08 100644 --- a/docs/.vuepress/config.js +++ b/docs/.vuepress/config.js @@ -1,74 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + module.exports = { - title: 'EventMesh', - description: 'EventMesh', - head: [ - ['link', { rel: 'icon', href: '/logo.png' }] - ], - locales: { - '/': { - lang: 'English', - title: 'EventMesh', - description: '' - }, - '/cn/': { - lang: '中文', - title: 'EventMesh', - description: '' - }, - }, - themeConfig: { + title: 'EventMesh', + description: 'EventMesh', + head: [ + ['link', {rel: 'icon', href: '/logo.png'}] + ], locales: { - '/': { - selectText: 'Languages', - label: 'English', - ariaLabel: 'Languages', - nav: [ - { text: 'Community', link: '/en/community/' }, - { - text: 'Blog', - link: '/en/blog/' - }, - { - text: 'Documentation', link: '/en/documentation/' - }, - { text: 'Github', link: 'https://github.com/WeBankFinTech/EventMesh'}, - ], - }, - '/en/': { - selectText: 'Languages', - label: 'English', - ariaLabel: 'Languages', - nav: [ - { text: 'Community', link: '/en/community/' }, - { - text: 'Blog', - link: '/en/blog/' - }, - { - text: 'Documentation', link: '/en/documentation/' - }, - { text: 'Github', link: 'https://github.com/WeBankFinTech/EventMesh'}, - ], - }, - '/cn/': { - // 多语言下拉菜单的标题 - selectText: '选择语言', - // 该语言在下拉菜单中的标签 - label: '简体中文', - nav: [ - { text: '社区', link: '/cn/community/' }, - { - text: '博客', - link: '/cn/blog/' - }, - { - text: '文档', link: '/en/documentation/' - }, - { text: 'Github', link: 'https://github.com/WeBankFinTech/EventMesh'}, - ], - } + '/': { + lang: 'English', + title: 'EventMesh', + description: '' + }, + '/cn/': { + lang: '中文', + title: 'EventMesh', + description: '' + }, }, - - sidebar: true - } + themeConfig: { + locales: { + '/': { + selectText: 'Languages', + label: 'English', + ariaLabel: 'Languages', + nav: [ + {text: 'Community', link: '/en/community/'}, + { + text: 'Blog', + link: '/en/blog/' + }, + { + text: 'Documentation', link: '/en/documentation/' + }, + {text: 'Github', link: 'https://github.com/WeBankFinTech/EventMesh'}, + ], + }, + '/en/': { + selectText: 'Languages', + label: 'English', + ariaLabel: 'Languages', + nav: [ + {text: 'Community', link: '/en/community/'}, + { + text: 'Blog', + link: '/en/blog/' + }, + { + text: 'Documentation', link: '/en/documentation/' + }, + {text: 'Github', link: 'https://github.com/WeBankFinTech/EventMesh'}, + ], + }, + '/cn/': { + // 多语言下拉菜单的标题 + selectText: '选择语言', + // 该语言在下拉菜单中的标签 + label: '简体中文', + nav: [ + {text: '社区', link: '/cn/community/'}, + { + text: '博客', + link: '/cn/blog/' + }, + { + text: '文档', link: '/en/documentation/' + }, + {text: 'Github', link: 'https://github.com/WeBankFinTech/EventMesh'}, + ], + } + }, + + sidebar: true + } } diff --git a/docs/README.md b/docs/README.md index e063bbaa3c..5909122319 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,43 +1,56 @@ -[![Build Status](https://www.travis-ci.org/WeBankFinTech/DeFiBus.svg?branch=master)](https://www.travis-ci.org/WeBankFinTech/EventMesh) -[![Coverage Status](https://coveralls.io/repos/github/WeBankFinTech/DeFiBus/badge.svg?branch=master)](https://coveralls.io/github/WeBankFinTech/EventMesh?branch=master) -[![GitHub release](https://img.shields.io/badge/release-download-orange.svg)](https://github.com/WeBankFinTech/EventMesh/releases) +# Apache EventMesh (Incubating) +[![CI status](https://github.com/apache/incubator-eventmesh/actions/workflows/ci.yml/badge.svg)](https://github.com/apache/incubator-eventmesh/actions/workflows/ci.yml) +[![CodeCov](https://codecov.io/gh/apache/incubator-eventmesh/branch/develop/graph/badge.svg)](https://codecov.io/gh/apache/incubator-eventmesh) +[![Language grade: Java](https://img.shields.io/lgtm/grade/java/g/apache/incubator-eventmesh.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/apache/incubator-eventmesh/context:java) +[![Total alerts](https://img.shields.io/lgtm/alerts/g/apache/incubator-eventmesh.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/apache/incubator-eventmesh/alerts/) +[![GitHub release](https://img.shields.io/badge/release-download-orange.svg)](https://github.com/apache/incubator-eventmesh/releases) [![License](https://img.shields.io/badge/license-Apache%202-4EB1BA.svg)](https://www.apache.org/licenses/LICENSE-2.0.html) [点我查看中文版](cn/README.md) ## What is Event Mesh? -This figure shows the positioning of the event mesh relative to other similar technologies (such as service mesh) in the application framework. + +EventMesh is a dynamic cloud-native eventing infrastruture used to decouple the application and backend middleware +layer, which supports a wide range of use cases that encompass complex multi-cloud, widely distributed topologies using +diverse technology stacks. ![architecture1](images/eventmesh-define.png) -Event Mesh is a dynamic plug-in cloud-native basic service layer used to decouple the application and middleware layer. It provides flexible, reliable and fast event distribution, and can be managed. -![architecture1](/images/eventmesher.png) +**EventMesh Architecture:** + +![architecture1](images/eventmesh-runtime.png) + +**EventMesh Cloud Native Structure:** -Cloud Native Event Mesh: -![architecture2](/images/bus.png) +![architecture2](images/eventmesh-panels.png) + +The event mesh allows events from one application to be dynamically routed to any other application. General functions +of the event mesh: -The event mesh allows events from one application to be dynamically routed to any other application. -General functions of the event mesh: * Event driven; * Event governance; * Dynamic routing; * Cloud native Dependent components: -* DeFiBus : a distributed messaging platform with low latency, high performance and reliability, flexible scalability. [DeFiBus](https://github.com/WeBankFinTech/DeFiBus) -* RocketMQ + +* [RocketMQ](https://github.com/apache/rocketmq):RocketMQ is a distributed messaging and streaming platform with low + latency, high performance and reliability, trillion-level capacity and flexible scalability. Key components: -* eventmesh-emesher : an middleware to transmit events between event producers and consumers, support cloud native apps and microservices + +* eventmesh-runtime : an middleware to transmit events between event producers and consumers, support cloud native apps + and microservices * eventmesh-sdk-java : currently supports HTTP and TCP protocols, and will support gRPC in the future -* eventmesh-registry : automatically routes events between applications and services connected to seperate event meshers, manage eventmesh-emesher ## RoadMap + | version | feature | | ---- | ---- | -| v1.0.0 |Support DeFiBus as eventstore, support pub/sub, http api, java-sdk| +| v1.0.0 |Support pub/sub, http api, java-sdk| | v1.1.0 |Support rocketmq as eventstore| -| v1.2.0 |Support transaction event| -| v1.3.0 |Support Plug-in architectur| +| v1.2.0 |Support Plug-in architecture, support http sub,support cloud native deploy| +| V1.3.0 |Support CloudEvents protocol| +| |Support transaction event| | |Support Event Sourcing| | |Support Event orchestration| | |Support Dashboard| @@ -47,26 +60,31 @@ Key components: | |Support Skywalking| | |Support Spiffe| | |Support gRPC| -| |Support c/go/python/nodejs SDK| +| |Support c/go/python/NodeJs/wasm SDK| ## Quick Start -1. Build and deploy event-store([DeFiBus](https://github.com/WeBankFinTech/DeFiBus)), - see instruction ['event-store quickstart'](instructions/eventmesh-store-quickstart.md). -2. Build and deploy eventmesh-emesher, see instruction ['eventmesh-emesher quickstart'](instructions/eventmesh-emesher-quickstart.md). -3. Run eventmesh-sdk-java demo, see instruction ['eventmesh-sdk-java quickstart'](instructions/eventmesh-sdk-java-quickstart.md). + +1. Build and deploy event-store(RocketMQ), see [instruction](https://rocketmq.apache.org/docs/quick-start/). +2. Build and deploy eventmesh-runtime, see + instruction ['eventmesh-runtime quickstart'](en/instructions/eventmesh-runtime-quickstart.md). +3. Run eventmesh-sdk-java demo, see + instruction ['eventmesh-sdk-java quickstart'](en/instructions/eventmesh-sdk-java-quickstart.md). ## Contributing -Contributions are always welcomed! Please see [CONTRIBUTING](CONTRIBUTING.md) for detailed guidelines -You can start with the issues labeled with good first issue. +Contributions are always welcomed! Please see [CONTRIBUTING](../CONTRIBUTING.md) for detailed guidelines + +You can start with the issues labeled with good first issue. [GitHub Issues](https://github.com/WeBankFinTech/EventMesh/issues) ## License + [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.html) Copyright (C) Apache Software Foundation ## Contacts + WeChat group: -![wechat_qr](/images/mesh-helper.png) +![wechat_qr](images/mesh-helper.png) diff --git a/docs/_config.yml b/docs/_config.yml index c4192631f2..3ef20c109c 100644 --- a/docs/_config.yml +++ b/docs/_config.yml @@ -1 +1,20 @@ -theme: jekyll-theme-cayman \ No newline at end of file +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +theme: jekyll-theme-cayman diff --git a/docs/cn/README.md b/docs/cn/README.md index ff6141cb73..3aae2a3726 100644 --- a/docs/cn/README.md +++ b/docs/cn/README.md @@ -1,41 +1,50 @@ -[![Build Status](https://www.travis-ci.org/WeBankFinTech/DeFiBus.svg?branch=master)](https://www.travis-ci.org/WeBankFinTech/EventMesh) -[![Coverage Status](https://coveralls.io/repos/github/WeBankFinTech/DeFiBus/badge.svg?branch=master)](https://coveralls.io/github/WeBankFinTech/EventMesh?branch=master) -[![GitHub release](https://img.shields.io/badge/release-download-orange.svg)](https://github.com/WeBankFinTech/EventMesh/releases) +# Apache EventMesh (Incubating) +[![CI status](https://github.com/apache/incubator-eventmesh/actions/workflows/ci.yml/badge.svg)](https://github.com/apache/incubator-eventmesh/actions/workflows/ci.yml) +[![CodeCov](https://codecov.io/gh/apache/incubator-eventmesh/branch/develop/graph/badge.svg)](https://codecov.io/gh/apache/incubator-eventmesh) +[![Language grade: Java](https://img.shields.io/lgtm/grade/java/g/apache/incubator-eventmesh.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/apache/incubator-eventmesh/context:java) +[![Total alerts](https://img.shields.io/lgtm/alerts/g/apache/incubator-eventmesh.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/apache/incubator-eventmesh/alerts/) +[![GitHub release](https://img.shields.io/badge/release-download-orange.svg)](https://github.com/apache/incubator-eventmesh/releases) [![License](https://img.shields.io/badge/license-Apache%202-4EB1BA.svg)](https://www.apache.org/licenses/LICENSE-2.0.html) ## 什么是Event Mesh? -该图显示了Event Mesh相对于应用程序框架中其他类似技术(例如Service Mesh)的定位. -![architecture1](/images/eventmesh-define.png) -Event Mesh是一个动态的插件式云原生基础服务层,用于分离应用程序和中间件层。它提供了灵活,可靠和快速的事件分发,并且可以进行管理: -![architecture1](/images/eventmesher.png) +EventMesh是一个动态的云原生事件驱动架构基础设施,用于分离应用程序和后端中间件层,它支持广泛的用例,包括复杂的混合云、使用了不同技术栈的分布式架构。 -云原生Event Mesh: -![architecture2](/images/bus.png) +![architecture1](../images/eventmesh-define.png) + +**EventMesh架构:** + +![architecture1](../images/eventmesh-runtime.png) + +**EventMesh云原生结构:** + +![architecture2](../images/eventmesh-panels.png) + +Event Mesh允许将来自一个应用程序的事件动态路由到任何其他应用程序. Event Mesh的一般功能: -Event Mesh允许将来自一个应用程序的事件动态路由到任何其他应用程序. -Event Mesh的一般功能: * 事件驱动; * 事件治理; * 动态路由; * 云原生 依赖部件: -* DeFiBus:具有低延迟,高性能和可靠性,和灵活可伸缩性的分布式消息传递平台 [DeFiBus](https://github.com/WeBankFinTech/DeFiBus) -* RocketMQ + +* [RocketMQ](https://github.com/apache/rocketmq):RocketMQ是一个分布式消息流平台,具有低延迟、高性能和可靠性、万亿级容量和灵活的可伸缩性。 关键部件: -* eventmesh-emesher:一种中间件,用于在事件产生者和使用者之间传输事件,支持云原生应用程序和微服务 + +* eventmesh-runtime:一种中间件,用于在事件产生者和使用者之间传输事件,支持云原生应用程序和微服务 * eventmesh-sdk-java:当前支持HTTP和TCP协议,未来会支持gRPC等 -* eventmesh-registry:自动在连接到单独事件网格器的应用程序和服务之间路由事件, 管理emesher ## RoadMap + | version | feature | | ---- | ---- | -| v1.0.0 |Support DeFiBus as eventstore, support pub/sub, http api, java-sdk| +| v1.0.0 |Support pub/sub, http api, java-sdk| | v1.1.0 |Support rocketmq as eventstore| -| v1.2.0 |Support transaction event| -| v1.3.0 |Support Plug-in architectur| +| v1.2.0 |Support Plug-in architecture, support http sub| +| V1.3.0 |Support CloudEvents protocol| +| |Support transaction event| | |Support Event Sourcing| | |Support Event orchestration| | |Support Dashboard| @@ -45,24 +54,27 @@ Event Mesh的一般功能: | |Support Skywalking| | |Support Spiffe| | |Support gRPC| -| |Support c/go/python/nodejs SDK| +| |Support c/go/python/NodeJs/wasm SDK| ## 快速开始 -1. 构建并部署event-store([DeFiBus](https://github.com/WeBankFinTech/DeFiBus)) - 请参见说明['event-store quickstart'](instructions/eventmesh-store-quickstart.md) -2. 构建并部署eventmesh-emesher,请参见说明['eventmesh-emesher quickstart']( instructions/eventmesh-emesher-quickstart.md) -3. 运行eventmesh-sdk-java演示,请参见说明['eventmesh-sdk-java quickstart'](instructions/eventmesh-sdk-java-quickstart.md) + +1. 构建并部署event-store(RocketMQ), 请参见[说明](https://rocketmq.apache.org/docs/quick-start/) +2. 构建并部署eventmesh-runtime,请参见说明['eventmesh-runtime quickstart'](instructions/eventmesh-runtime-quickstart.zh-CN.md) +3. 运行eventmesh-sdk-java演示,请参见说明['eventmesh-sdk-java quickstart'](instructions/eventmesh-sdk-java-quickstart.zh-CN.md) ## 贡献 -永远欢迎参与共建, 请参阅[贡献](CONTRIBUTING.md)了解详细指南 -您可以从问题开始. -[GitHub Issues](https://github.com/WeBankFinTech/EventMesh/issues) +永远欢迎参与共建, 请参阅[贡献](../../CONTRIBUTING.zh-CN.md)了解详细指南 + +您可以从问题开始. +[GitHub Issues](https://github.com/apache/incubator-eventmesh/issues) ## License + [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.html) Copyright (C) Apache Software Foundation ## 联系人 + 微信群: -![wechat_qr](/images/mesh-helper.png) +![wechat_qr](../images/mesh-helper.png) diff --git a/docs/cn/features/0-introduction.md b/docs/cn/features/0-introduction.md deleted file mode 100644 index 195054feb7..0000000000 --- a/docs/cn/features/0-introduction.md +++ /dev/null @@ -1,49 +0,0 @@ -# DeFiBus -       -**DeFiBus=RPC+MQ,是基于开源消息中间件打造的安全可靠的分布式金融级消息总线。DeFibus不仅提供了RPC同步调用,还提供了MQ的异步事件通知、事件组播和广播等常用服务调用和消息模式,同时增加了应用多中心多活、服务就近、灰度发布等分布式场景下的高可用能力。在对于机器故障的容错能力方面的增强,也让消息总线的服务更加稳定可靠,为业务提供7x24的服务。** - -### 整体架构 -
- -![architecture1](../../../docs/images/features/a-distributing-architecture-in-financial.png) - -
- -![architecture2](../../../docs/images/features/architecture-p1.png) - -DeFiBus主要包括以下几个组件(模块): - -* **Broker**:通过轻量的Topic和Queue机制提供消息存储功能。Broker定期将Topic信息上报到NameServer中,同集群中的Broker实例上报的NameServer必须保持一致,避免路由信息不一致。 - -* **NameServer**:NameServer提供Topic的发现和路由,每一个NameServer接受Broker上报的Topic信息,并维护Topic的路由信息供客户端查询。 - -* **GSL**:全局服务定位(Global Service Location)服务提供服务级别的路由发现。服务可以部署在不同的区域(比如不同的数据中心、逻辑分区等),服务请求方在请求某一个具体服务时,无需关注服务部署的区域,GSL能够根据服务发现规则自动定位到具体的服务,将服务信息返回给客户端。 - -* **SGS**:服务治理系统(Service Government System)负责全局的服务管理,包括服务的申请、服务部署规划、服务下线等服务全生命周期的管理。在DeFiBus中,服务与Topic一一对应,Topic的名称由对应的服务按照一定的规则来命名。Topic的创建、更新和删除由SGS统一管理。SGS在服务的部署区域对应的Broker集群中创建Topic之后,将更新全局服务路由数据,供GSL定位服务使用。 - -* **Proxy**:服务代理(Proxy)提供HTTP接入方式,同时允许按照协议规范开发的C、GO、Python等其他语言客户端的接入。 - -### 服务和Topic的定义 -       -DeFiBus把服务和Topic做了一一对应,每个服务必须对应一个Topic。Topic根据服务的唯一ID和服务的部署区域来命名。每个服务需要有服务的唯一标识,可以用数字ID或者字符串来表示。每个部署区域使用3位长度的字符串(限数字和字母构成)表示。 -Topic按照如下格式来命名: -``` -[区域代码]-[服务唯一ID] -``` - -比如,余额查询服务的服务ID为20190001表示,部署在“A10”这个区域,那么该服务在A10区域的Topic就命名为“A10-20190001”。Topic的命名规则 - - -### 特性列表: -* [RPC调用:即“Request-Reply”模式,支持系统间的同步调用](docs/cn/features/1-request-response-call.md) -* 消息发布/订阅:消息的发布和订阅 -* [灰度发布:服务级别的灰度发布](docs/cn/features/2-dark-launch.md) -* [熔断机制:应用实例级别的熔断](docs/cn/features/3-circuit-break-mechanism.md) -* [服务就近:就近进行服务的请求和响应,减少跨区调用](docs/cn/features/4-invoke-service-nearby.md) -* [应用多活:应用多中心多活](docs/cn/features/5-multi-active.md) -* [动态扩缩队列 :自适应应用实例数量,动态调整队列个数](docs/cn/features/6-dynamic-adjust-queue.md) -* [容错机制:故障和错误隔离](docs/cn/features/8-fault-tolerant.md) -* 服务路由和定位:动态路由及定位(后续开源) -* 服务代理:HTTP及多语言的代理(后续开源) -* 服务治理:服务元数据的管理(后续开源) -* 平滑升级:平滑升级、平滑扩容(后续开源) \ No newline at end of file diff --git a/docs/cn/features/1-request-response-call.md b/docs/cn/features/1-request-response-call.md deleted file mode 100644 index 1feabaf457..0000000000 --- a/docs/cn/features/1-request-response-call.md +++ /dev/null @@ -1,34 +0,0 @@ -## 1. Request-Reply同步调用 - -       -Request-Reply同步调用指的是请求方发出一条消息之后,需要响应方在消费完这条消息后回复一个响应结果。 - -
- -![RR](../../images/features/RR-call-p1.png) - -
- -整个调用过程包含了两个消息的产生和消费过程。 -**1.请求方产生请求消息,服务响应方消费这条请求消息** -       -请求方根据服务提供方的协议将请求内容设置到消息体中,并将消息发送到Broker上。服务响应方订阅相应的Topic,从Broker上获取到请求消息,并消费。 - -**2.服务响应方产生响应消息,请求方接收这条响应消息** -       -服务响应方收到请求消息后,执行相应的处理,并将请求结果设置到响应消息的消息体中,将响应消息发送到Broker上。请求方接收响应消息的方式采用的是Broker推送的形式,而不是由Producer订阅的方式,从而使得响应消息能够精准回到发出请求消息的实例上。 - -       -DeFiBus在每条请求消息中增加REPLY_TO属性来唯一标识每一个请求方实例。在创建响应消息时将REPLY_TO属性透传到响应消息中。Broker收到响应消息后,根据REPLY_TO属性,查找出对应的请求方实例的连接,将响应消息推送给该请求方实例。 - - ---- -#### Links: -* [架构介绍](../../../README.md) -* [Request-Reply调用](docs/cn/features/1-request-response-call.md) -* [灰度发布](docs/cn/features/2-dark-launch.md) -* [熔断机制](docs/cn/features/3-circuit-break-mechanism.md) -* [服务就近](docs/cn/features/4-invoke-service-nearby.md) -* [应用多活](docs/cn/features/5-multi-active.md) -* [动态扩缩队列](docs/cn/features/6-dynamic-adjust-queue.md) -* [容错机制](docs/cn/features/8-fault-tolerant.md) \ No newline at end of file diff --git a/docs/cn/features/10-flow-control.md b/docs/cn/features/10-flow-control.md deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs/cn/features/2-dark-launch.md b/docs/cn/features/2-dark-launch.md deleted file mode 100644 index e6e7e20119..0000000000 --- a/docs/cn/features/2-dark-launch.md +++ /dev/null @@ -1,24 +0,0 @@ -## 2.灰度发布 - -       -同一个消费组中的消费者实例通常订阅的Topic是相同的。在有新业务上线时,我们希望仅仅在个别实例上进行灰度,验证通过之后再进行全量。DeFiBus提供了灰度发布的能力,同一个消费组中,允许不同消费者实例订阅不同的Topic,只有订阅了某个具体Topic的实例才能够收到这个Topic的消息,同消费组中没有订阅这个Topic的实例不会收到消息。 - -       -假设一个消费组有3个消费者实例,上线初期只涉及到Topic1和Topic2。当业务扩展,需要增加Topic3的订阅时,可以先灰度其中一个实例,验证Topic3在灰度实例上执行正常之后,逐步再替换其他实例。在这期间,实例1和实例2不会收到Topic3的消息。 - -
- -![avater](../../images/features/dark-launch-p1.png) - -
- ---- -#### Links: -* [架构介绍](../../../README.md) -* [Request-Reply调用](docs/cn/features/1-request-response-call.md) -* [灰度发布](docs/cn/features/2-dark-launch.md) -* [熔断机制](docs/cn/features/3-circuit-break-mechanism.md) -* [服务就近](docs/cn/features/4-invoke-service-nearby.md) -* [应用多活](docs/cn/features/5-multi-active.md) -* [动态扩缩队列](docs/cn/features/6-dynamic-adjust-queue.md) -* [容错机制](docs/cn/features/8-fault-tolerant.md) \ No newline at end of file diff --git a/docs/cn/features/3-circuit-break-mechanism.md b/docs/cn/features/3-circuit-break-mechanism.md deleted file mode 100644 index 06eec22ae3..0000000000 --- a/docs/cn/features/3-circuit-break-mechanism.md +++ /dev/null @@ -1,25 +0,0 @@ -## 3.熔断 -       -DeFiBus基于队列来做消费端的负载均衡,对同一个消费组,除广播模式外,每个队列只由一个消费者实例消费。当一个实例处理能力下降或者异常出现消息堆积时,为了避免堆积情况继续加剧,DeFiBus会触发队列的熔断,此时生产者实例在感知到队列熔断之后,会优先把消息发送到其他没有熔断的队列上,暂停往熔断队列上写入新消息。当堆积消除后,熔断被解除,生产者恢复往该队列发送消息。 - -       -DeFiBus对每个Topic定义了深度的属性,表示Topic的队列允许堆积的最大消息条数。消息堆积数表示队列中尚未下发给消费者实例的消息条数,可由队列中最新一条消息的offset与消费者实例已经获取到的消息的offset的差值计算。 -
- -![offset](../../../docs/images/features/circuit-break-p1.png) - -
- -       -当Consumer出现异常或者触发了流控,Consumer拉消息过程受阻,队列的DeliverOffset停止不前,新消息持续写入,MaxOffset不断变大,最终MaxOffset与DeliverOffset将超过Topic的最大深度限制,触发队列熔断。 - ---- -#### Links: -* [架构介绍](../../../README.md) -* [Request-Reply调用](docs/cn/features/1-request-response-call.md) -* [灰度发布](docs/cn/features/2-dark-launch.md) -* [熔断机制](docs/cn/features/3-circuit-break-mechanism.md) -* [服务就近](docs/cn/features/4-invoke-service-nearby.md) -* [应用多活](docs/cn/features/5-multi-active.md) -* [动态扩缩队列](docs/cn/features/6-dynamic-adjust-queue.md) -* [容错机制](docs/cn/features/8-fault-tolerant.md) diff --git a/docs/cn/features/4-invoke-service-nearby.md b/docs/cn/features/4-invoke-service-nearby.md deleted file mode 100644 index 209940cbfd..0000000000 --- a/docs/cn/features/4-invoke-service-nearby.md +++ /dev/null @@ -1,29 +0,0 @@ -## 4.服务就近 -       -为了保证高可用,服务的部署通常分布在多个机房、区域。我们希望服务之间能够就近调用,减少跨机房跨区域网络访问的时延问题。对此,DeFiBus在Broker和客户端上都增加了区域的属性来标识实例属于哪个区域。对于Producer,消息会优先发往同区域内的Broker集群上;对于Consumer,则优先监听同区域内的Queue;当一个区域内没有Consumer实例监听时,则由其他区域的Consumer实例跨区域监听。 - -### 就近发送 -       -在创建Producer时,通过设置```DeFiBusClientConfig.setClusterPrefix("your region")```来标识Producer实例所在的区域。Producer在每次发送消息会先选则一个Queue来作为发送的目标队列。当启用就近发送时,Producer优先选择与自己同区域内的Queue,当本区域内没有可用Queue时,则选择其他区域的Queue。 -
- -
- -### 就近监听 -       -就近监听指的是Consumer在做负载均衡分配Queue的时候,每个区域内的Queue只由该区域内的Consumer监听和消费,当且仅当一个区域内没有订阅该Topic的Consumer时,由其他区域订阅了该Topic的Consumer跨区域监听和消费这些Queue。虽然Consumer是在同区域内就近消费,但仍通过心跳维持跨区域的连接,以保证能够随时跨区域接管消费。 - -
- -
- ---- -#### Links: -* [架构介绍](../../../README.md) -* [Request-Reply调用](docs/cn/features/1-request-response-call.md) -* [灰度发布](docs/cn/features/2-dark-launch.md) -* [熔断机制](docs/cn/features/3-circuit-break-mechanism.md) -* [服务就近](docs/cn/features/4-invoke-service-nearby.md) -* [应用多活](docs/cn/features/5-multi-active.md) -* [动态扩缩队列](docs/cn/features/6-dynamic-adjust-queue.md) -* [容错机制](docs/cn/features/8-fault-tolerant.md) \ No newline at end of file diff --git a/docs/cn/features/5-multi-active.md b/docs/cn/features/5-multi-active.md deleted file mode 100644 index f24366630b..0000000000 --- a/docs/cn/features/5-multi-active.md +++ /dev/null @@ -1,18 +0,0 @@ -## 5.同城多中心多活 -       -同城多中心多活指的是应用的多活,在DeFiBus集群正常运行的情况下,应用部署在多个数据中心,一个数据中心的应用实例全部挂掉后,DeFiBus能够自动将应用流量切换到另一个数据中心的应用实例上,保证应用能够持续稳定地提供服务而不中断。同城多中心多活得益于DeFiBus的服务就近特性,结合应用部署的规划,使得正常情况下服务调用发生在同一个数据中心,当一个中心的应用出现故障时,能够有其他中心的实例接管服务。 - -
- -
- ---- -#### Links: -* [架构介绍](../../../README.md) -* [Request-Reply调用](docs/cn/features/1-request-response-call.md) -* [灰度发布](docs/cn/features/2-dark-launch.md) -* [熔断机制](docs/cn/features/3-circuit-break-mechanism.md) -* [服务就近](docs/cn/features/4-invoke-service-nearby.md) -* [应用多活](docs/cn/features/5-multi-active.md) -* [动态扩缩队列](docs/cn/features/6-dynamic-adjust-queue.md) -* [容错机制](docs/cn/features/8-fault-tolerant.md) \ No newline at end of file diff --git a/docs/cn/features/6-dynamic-adjust-queue.md b/docs/cn/features/6-dynamic-adjust-queue.md deleted file mode 100644 index 9c27b7deb1..0000000000 --- a/docs/cn/features/6-dynamic-adjust-queue.md +++ /dev/null @@ -1,35 +0,0 @@ -## 自动伸缩Queue -       -在同一个消费组内,每个队列只由一个实例消费。当队列数小于消费者实例数时,会有部分消费者实例分不到队列;反之,当队列数大于消费者实例数时,每个消费者需要消费多个队列。队列数不是消费者实例数的整数倍时,则会出现部分实例需要消费比同组内的其他实例更多的队列,出现负载不均衡问题。 - -       -DeFiBus提供了队列数量自动调整的特性。当有Consumer新注册或者去注册时,Broker触发队列的自动伸缩,根据当前在线的消费者实例个数,增加或者减少队列个数,使队列个数与消费者实例数保持一致。 - -       -当队列数需要增加时,首先调整Topic的ReadQueueNum,将可读的队列数扩增;10s之后,再调整Topic的WriteQueueNum,将可写的队列数扩增。这样使得新扩增的队列能够先被消费者感知并监听上,然后才让生产者感知到,往新队列上发送消息,是扩增操作更平滑。 - -
- -
- -       -当队列数需要减少时,首先调整Topic的WriteQueueNum,将可写的队列数缩减;5分钟(默认,可配置)后先检查即将被缩减的队列中是否有消息没有被消费完,如果有,则继续延迟缩减操作,使消费者能够继续消费完队列中的消息;如果没有,则调整ReadQueueNum,将可写的队列数缩减。 - -
- -
- -       -对于多个消费组订阅相同Topic并且是集群消费模式时,在计算扩缩的队列个数时,以最大的消费组的消费者实例数为准,保证拥有最多实例数的消费组内每个消费者实例都能够分到Queue进行消费。 - - ---- -#### Links: -* [架构介绍](../../../README.md) -* [Request-Reply调用](docs/cn/features/1-request-response-call.md) -* [灰度发布](docs/cn/features/2-dark-launch.md) -* [熔断机制](docs/cn/features/3-circuit-break-mechanism.md) -* [服务就近](docs/cn/features/4-invoke-service-nearby.md) -* [应用多活](docs/cn/features/5-multi-active.md) -* [动态扩缩队列](docs/cn/features/6-dynamic-adjust-queue.md) -* [容错机制](docs/cn/features/8-fault-tolerant.md) \ No newline at end of file diff --git a/docs/cn/features/7-isolation-mechanism.md b/docs/cn/features/7-isolation-mechanism.md deleted file mode 100644 index 298efc461d..0000000000 --- a/docs/cn/features/7-isolation-mechanism.md +++ /dev/null @@ -1,23 +0,0 @@ -## 隔离机制 -Producer在往Topic发送消息时,会按照MessageQueueSelector定义的选择策略,从Topic的所有MessageQueue中选择一个作为目标队列发送消息。 -当队列发生熔断,或者Broker故障导致队列发送消息异常时,如果没有对这些队列进行特殊处理,下次再轮到发这个队列的时候仍然可能失败。 - -DeFiBus提供异常队列的隔离机制,当往某个队列发送消息失败时,将队列标记为隔离状态,在隔离过期之前将不再往这个队列发送消息,避免再次失败,降低失败概率。 - -异常队列隔离机制分为两步: -**-发现并标记队列为隔离** -在发送回调中更新发送队列的健康状态,如果执行的是onSuccess分支,则标记队列为健康,去除队列的隔离标记;如果执行的是onException分支,则标记队列为隔离状态。 - -**-不选择隔离中的队列发送消息** -在MessageQueueSelector中实现隔离机制的过滤逻辑,每次进行队列的选择时,优先从没有标记为隔离的队列中选择。当所有队列都被标记为隔离时,则从所有队列中选择,保证每次都要选出一个队列。 - ---- -#### Links: -* [架构介绍](../../../README.md) -* [Request-Reply调用](docs/cn/features/1-request-response-call.md) -* [灰度发布](docs/cn/features/2-dark-launch.md) -* [熔断机制](docs/cn/features/3-circuit-break-mechanism.md) -* [服务就近](docs/cn/features/4-invoke-service-nearby.md) -* [应用多活](docs/cn/features/5-multi-active.md) -* [动态扩缩队列](docs/cn/features/6-dynamic-adjust-queue.md) -* [容错机制](docs/cn/features/8-fault-tolerant.md) \ No newline at end of file diff --git a/docs/cn/features/8-fault-tolerant.md b/docs/cn/features/8-fault-tolerant.md deleted file mode 100644 index f118c9d311..0000000000 --- a/docs/cn/features/8-fault-tolerant.md +++ /dev/null @@ -1,50 +0,0 @@ -## 8.容错机制 -       -在金融场景下,对可用性和稳定性的要求非常高,中间件对机器故障、网络故障、应用故障以及中间件本身的故障等常见故障场景需要有容错能力,降低故障带来的影响。 - -### 隔离机制 -##### 1. Producer端的隔离 -       -Producer在往Topic发送消息时,会按照MessageQueueSelector定义的选择策略,从Topic的所有MessageQueue中选择一个作为目标队列发送消息。 -当队列发生熔断,或者Broker故障导致队列发送消息异常时,如果没有对这些队列进行特殊处理,下次再轮到发这个队列的时候仍然可能失败。 - -       -DeFiBus提供异常队列的隔离机制,当往某个队列发送消息失败时,将队列标记为隔离状态,在隔离过期之前将不再往这个队列发送消息,避免再次失败,降低失败概率。 - -异常队列隔离机制分为两步: -**-发现并标记队列为隔离** -       -在发送回调中更新发送队列的健康状态,如果执行的是onSuccess分支,则标记队列为健康,去除队列的隔离标记;如果执行的是onException分支,则标记队列为隔离状态。 - -**-不选择隔离中的队列发送消息** -       -在MessageQueueSelector中实现隔离机制的过滤逻辑,每次进行队列的选择时,优先从没有标记为隔离的队列中选择。当所有队列都被标记为隔离时,则从所有队列中选择,保证每次都要选出一个队列。 - - -##### 2. Consumer端的隔离 - -       -Consumer由拉消息线程只负责把拉消息请求以异步发送的形式发送出去。在正常情况下,每次拉消息请求的执行都很快,不会有卡顿。一旦有Broker故障导致PullRequest的执行发生了卡顿,则该Consumer监听的所有Queue都会因为PullRequest执行的延迟而出现消息消费延迟。对于RR同步请求的场景,这种是不能够接受的。 - -       -创建连接采用的是同步建立连接的策略,线程执行创建新连接时必须等待连接创建完成或者连接超时。当有Broker故障连不上时,就算是异步发送,也会因为同步等待连接建立而阻塞。此时就会出现一个Broker的故障导致其他健康Broker的消息消费出现延迟。 - -       -DeFiBus在Consumer拉消息的过程中增加了对拉消息任务的隔离,此处的隔离指的是将疑似有问题的任务隔离到另外的线程中执行,保证拉消息线程能够正常处理其他正常的任务。当发现执行拉消息耗时超过设定的阈值时,将该拉消息任务对应的Broker列入“隔离名单”中,在隔离过期之前,隔离Broker的拉消息请求都转交给另外线程执行,避免阻塞拉消息主线程,从而避免故障的Broker影响健康Broker的消息消费时效。 - -### 连接空闲机制 - -       -当连接的读或者写空闲超过60秒时,将主动断开连接。 - - ---- -#### Links: -* [架构介绍](../../../README.md) -* [Request-Reply调用](docs/cn/features/1-request-response-call.md) -* [灰度发布](docs/cn/features/2-dark-launch.md) -* [熔断机制](docs/cn/features/3-circuit-break-mechanism.md) -* [服务就近](docs/cn/features/4-invoke-service-nearby.md) -* [应用多活](docs/cn/features/5-multi-active.md) -* [动态扩缩队列](docs/cn/features/6-dynamic-adjust-queue.md) -* [容错机制](docs/cn/features/8-fault-tolerant.md) \ No newline at end of file diff --git a/docs/cn/features/9-publish-type.md b/docs/cn/features/9-publish-type.md deleted file mode 100644 index 74c3446d77..0000000000 --- a/docs/cn/features/9-publish-type.md +++ /dev/null @@ -1,35 +0,0 @@ -## 2. 单播、多播、广播 - -       -DeFiBus支持单播、多播、广播消费模式。 - -### 单播 - -
- -![unicast](../../images/features/unicast.png) - -
- -单播模式下,topic只被一个消费组监听;接收消息时,消费组内有且仅有一个实例会收到消息。 - -### 多播 - -
- -![multicast](../../images/features/multicast.png) - -
- -多播模式下,topic被多个消费组监听;接收消息时,每个消费组内有且仅有一个实例会收到消息。 - - -### 广播 - -
- -![broadcast](../../images/features/broadcast.png) - -
- -广播模式下,监听此topic的每个消费组中的每个实例都需要收到消息。 \ No newline at end of file diff --git a/docs/cn/features/https.zh-CN.md b/docs/cn/features/https.zh-CN.md index 768a3aa1e1..fa1244f949 100644 --- a/docs/cn/features/https.zh-CN.md +++ b/docs/cn/features/https.zh-CN.md @@ -1,19 +1,18 @@ -1.在eventmesh-emesher 中配置 +1.在eventmesh-runtime 中配置 ``` -proxy.properties(添加如下配置) -proxy.server.useTls.enabled=true //默认值 false +eventMesh.properties(添加如下配置) +eventMesh.server.useTls.enabled=true //默认值 false config env varible -Dssl.server.protocol=TLSv1.1 //默认值 TLSv1.1 -Dssl.server.cer=sChat2.jks //把文件放到启动脚本start.sh 指定的conPath目录下 -Dssl.server.pass=sNetty - ``` - 2.在eventmesh-sdk-java 中配置 + ``` //创建producer LiteClientConfig liteClientConfig = new liteClientConfig(); diff --git a/docs/cn/instructions/eventmesh-emesher-quickstart.zh-CN.md b/docs/cn/instructions/eventmesh-emesher-quickstart.zh-CN.md deleted file mode 100644 index b75899b317..0000000000 --- a/docs/cn/instructions/eventmesh-emesher-quickstart.zh-CN.md +++ /dev/null @@ -1,32 +0,0 @@ -#Eventmesh-emesher快速入门说明 - -###依赖 -``` -建议使用64位操作系统,建议使用Linux / Unix; -64位JDK 1.8+; -Gradle至少为5.6, 推荐 5.6.* -``` - -###下载源码 -[https://github.com/WeBankFinTech/EventMesh](https://github.com/WeBankFinTech/EventMesh) -您将获得**EventMesh-master.zip** - -###构建源码 -```$ xslt -unzip EventMesh-master.zip -cd / *您的部署路径* /EventMesh-master/eventmesh-emesher -gradle clean tar -x test -``` -您将在目录/ *您的部署路径* /EventMesh-master/eventmesh-emesher/dist中获得**eventmesh-emesher_1.0.0.tar.gz** - -###部署 --部署eventmesn-emesher -```$ xslt -upload eventmesh-emesher_1.0.0.tar.gz -tar -zxvf eventmesh-emesher_1.0.0.tar.gz -cd bin -配置 proxy.properties -cd ../bin -sh start.sh -``` -如果看到"ProxyTCPServer[port=10000] started....",则说明设置成功。 \ No newline at end of file diff --git a/docs/cn/instructions/eventmesh-runtime-protocol.zh-CN.md b/docs/cn/instructions/eventmesh-runtime-protocol.zh-CN.md new file mode 100644 index 0000000000..a3d152dbdc --- /dev/null +++ b/docs/cn/instructions/eventmesh-runtime-protocol.zh-CN.md @@ -0,0 +1,260 @@ +## TCP协议文档 + +#### 1. 协议格式 + +![dataFlow](../../images/protocol/eventmesh-tcp-dataFlow.png) + +**消息组成详解:** + +``` +魔术字:9位,当前值为“EventMesh” + +通信协议版本号:4位,当前值为“0000” + +消息总长度值(length):4位,int类型 + +消息头长度值(headerLength):4位,int类型 + +消息头(header):长度 = headerLength + +消息体(body):长度 = length - headerLength - 4 - 4 +``` + +#### 2. 业务逻辑层 + ++ 消息组成 + +消息头(header)+ 消息体(body) + +```java +public class Package { + + private Header header; + private Object body; +} + + +public class Header { + + private Command cmd; + private int code; + private String msg; + private String seq; +} +``` + ++ 详解 + +消息头(header):类型为Header,Header中有Command字段,用于区分不同的消息类型 + +消息体(body):对于不同的消息类型,body的类型不同 + +| 消息命令字 | body类型 | +| ------------------------------------------------------------ | ------------ | +| HEARTBEAT_REQUEST, HEARTBEAT_RESPONSE, HELLO_RESPONSE, CLIENT_GOODBYE_REQUEST, CLIENT_GOODBYE_RESPONSE, SERVER_GOODBYE_REQUEST, SERVER_GOODBYE_RESPONSE, LISTEN_REQUEST, LISTEN_RESPONSE, UNSUBSCRIBE_REQUEST, SUBSCRIBE_RESPONSE, UNSUBSCRIBE_RESPONSE, ASYNC_MESSAGE_TO_SERVER_ACK, BROADCAST_MESSAGE_TO_SERVER_ACK | 无 | +| HELLO_REQUEST | UserAgent | +| SUBSCRIBE_REQUEST | Subscription | +| REQUEST_TO_SERVER, REQUEST_TO_CLIENT, RESPONSE_TO_SERVER, RESPONSE_TO_CLIENT, ASYNC_MESSAGE_TO_SERVER, ASYNC_MESSAGE_TO_CLIENT, BROADCAST_MESSAGE_TO_SERVER, BROADCAST_MESSAGE_TO_CLIENT, ASYNC_MESSAGE_TO_CLIENT_ACK, BROADCAST_MESSAGE_TO_CLIENT_ACK, RESPONSE_TO_CLIENT_ACK, REQUEST_TO_CLIENT_ACK | OpenMessage | +| REDIRECT_TO_CLIENT | RedirectInfo | + +#### 3. Client 与 Eventmesh-Runtime(Server)交互场景详解 + +```java +public enum Command { + + //心跳 + HEARTBEAT_REQUEST(0), //client发给server的心跳包 + HEARTBEAT_RESPONSE(1), //server回复client的心跳包 + + //握手 + HELLO_REQUEST(2), //client发给server的握手请求 + HELLO_RESPONSE(3), //server回复client的握手请求 + + //断连 + CLIENT_GOODBYE_REQUEST(4), //client主动断连时通知server + CLIENT_GOODBYE_RESPONSE(5), //server回复client的主动断连通知 + SERVER_GOODBYE_REQUEST(6), //server主动断连时通知client + SERVER_GOODBYE_RESPONSE(7), //client回复server的主动断连通知 + + //订阅管理 + SUBSCRIBE_REQUEST(8), //client发给server的订阅请求 + SUBSCRIBE_RESPONSE(9), //server回复client的订阅请求 + UNSUBSCRIBE_REQUEST(10), //client发给server的取消订阅请求 + UNSUBSCRIBE_RESPONSE(11), //server回复client的取消订阅请求 + + //监听 + LISTEN_REQUEST(12), //client发给server的启动监听请求 + LISTEN_RESPONSE(13), //server回复client的监听请求 + + //RR + REQUEST_TO_SERVER(14), //client将RR请求发送给server + REQUEST_TO_CLIENT(15), //server将RR请求推送给client + REQUEST_TO_CLIENT_ACK(16), //client收到RR请求后ACK给server + RESPONSE_TO_SERVER(17), //client将RR回包发送给server + RESPONSE_TO_CLIENT(18), //server将RR回包推送给client + RESPONSE_TO_CLIENT_ACK(19), //client收到回包后ACK给server + + //异步事件 + ASYNC_MESSAGE_TO_SERVER(20), //client将异步事件发送给server + ASYNC_MESSAGE_TO_SERVER_ACK(21), //server收到异步事件后ACK给client + ASYNC_MESSAGE_TO_CLIENT(22), //server将异步事件推送给client + ASYNC_MESSAGE_TO_CLIENT_ACK(23), //client收到异步事件后ACK给server + + //广播 + BROADCAST_MESSAGE_TO_SERVER(24), //client将广播消息发送给server + BROADCAST_MESSAGE_TO_SERVER_ACK(25), //server收到广播消息后ACK给client + BROADCAST_MESSAGE_TO_CLIENT(26), //server将广播消息推送给client + BROADCAST_MESSAGE_TO_CLIENT_ACK(27), //client收到广播消息后ACK给server + + //重定向指令 + REDIRECT_TO_CLIENT(30), //server将重定向指令推动给client +} +``` + +#### 4. Client发起交互 + +| 场景 | Client向Server发送消息命令字 | Server回复Client消息的命令字 | 说明 | +| -------------- | ---------------------------- | ------------------------------- | ---- | +| 握手 | HELLO_REQUEST | HELLO_RESPONSE | | +| 心跳 | HEARTBEAT_REQUEST | HEARTBEAT_RESPONSE | | +| 订阅 | SUBSCRIBE_REQUEST | SUBSCRIBE_RESPONSE | | +| 取消订阅 | UNSUBSCRIBE_REQUEST | UNSUBSCRIBE_RESPONSE | | +| 开始监听消息 | LISTEN_REQUEST | LISTEN_RESPONSE | | +| 发送RR请求 | REQUEST_TO_SERVER | RESPONSE_TO_CLIENT | | +| 发送RR回包 | RESPONSE_TO_SERVER | 无 | | +| 发送异步事件 | ASYNC_MESSAGE_TO_SERVER | ASYNC_MESSAGE_TO_SERVER_ACK | | +| 发送广播事件 | BROADCAST_MESSAGE_TO_SERVER | BROADCAST_MESSAGE_TO_SERVER_ACK | | +| 客户端主动断连 | CLIENT_GOODBYE_REQUEST | CLIENT_GOODBYE_RESPONSE | | + +#### 5. Server发起交互 + +| 场景 | Server向Client发送消息命令字 | Client回复Server消息命令字 | 说明 | +| ------------------ | ---------------------------- | ------------------------------- | ---- | +| 客户端接收RR请求 | REQUEST_TO_CLIENT | REQUEST_TO_CLIENT_ACK | | +| 客户端接收RR回包 | RESPONSE_TO_CLIENT | RESPONSE_TO_CLIENT_ACK | | +| 客户端接收异步事件 | ASYNC_MESSAGE_TO_CLIENT | ASYNC_MESSAGE_TO_CLIENT_ACK | | +| 客户端接收广播事件 | BROADCAST_MESSAGE_TO_CLIENT | BROADCAST_MESSAGE_TO_CLIENT_ACK | | +| 服务端主动断连 | SERVER_GOODBYE_REQUEST | 无 | | +| 服务端进行重定向 | REDIRECT_TO_CLIENT | 无 | | +| | | | | + +#### 6. 消息类型 + ++ 发送RR消息 + +![rr-msg](../../images/protocol/eventmesh-rr-msg.png) + ++ 发送异步单播消息 + +![async-msg](../../images/protocol/eventmesh-async-msg.png) + ++ 发送广播消息 + +![broadcast-msg](../../images/protocol/eventmesh-broadcast-msg.png) + +## HTTP协议文档 + +Java类`LiteMessage`的`content`字段表示一个特殊的协议,因此,如果您要使用eventmesh-sdk-java的http-client,则只需设计协议的`content`即可。`LiteMessage`组成如下: + +```java +public class LiteMessage { + + private String bizSeqNo; + + private String uniqueId; + + private String topic; + + private String content; + + private Map prop; + + private long createTime = System.currentTimeMillis(); +} +``` + +#### 1. 消息发送方式与组成 + +**消息发送方式**:POST方式 + +**消息组成**:请求头(RequestHeader) + 请求体(RequestBody) + ++ 心跳消息 + +**RequestHeader** + +| Key | 说明 | +| -------- | ---------------- | +| Env | client所属环境 | +| Region | client所属区域 | +| Idc | client所属IDC | +| Dcn | client所在DCN | +| Sys | client所属子系统 | +| Pid | client进程号 | +| Ip | client Ip | +| Username | client 用户名 | +| Passwd | client 密码 | +| Version | 协议版本 | +| Language | 语言描述 | +| Code | 请求码 | + +**RequestBody** + +| Key | 说明 | +| ----------------- | ------------------------------ | +| clientType | 客户端类型 | +| heartbeatEntities | 心跳实体,包含topic、url等信息 | + ++ 订阅消息: + +**RequestHeader** + +与心跳消息一致 + +**RequestBody** + +| Key | 说明 | +| ----- | ----------------- | +| topic | 客户端订阅的topic | +| url | topic对应的url | + ++ 取消订阅消息: + +**RequestHeader** + +与心跳消息一致 + +**RequestBody** + +与订阅消息一致 + ++ 发送异步事件: + +**RequestHeader** + +与心跳消息一致 + +**RequestBody** + +| Key | 说明 | +| -------- | ----------------------- | +| topic | 客户端请求的topic | +| content | 客户端发送的topic的内容 | +| ttl | 客户端请求超时时间 | +| bizSeqNo | 客户端请求业务流水号 | +| uniqueId | 客户端请求消息唯一标识 | + +#### 2. Client发起交互 + +| 场景 | Client向Server发送消息请求码 | Server回复Client消息的响应码 | 说明 | +| ------------ | ---------------------------- | --------------------------------------- | ---- | +| 心跳 | HEARTBEAT(203) | SUCCESS(0)/EVENTMESH_HEARTBEAT_ERROR(19) | | +| 订阅 | SUBSCRIBE(206) | SUCCESS(0)/EVENTMESH_SUBSCRIBE_ERROR(17) | | +| 取消订阅 | UNSUBSCRIBE(207) | SUCCESS(0)/EVENTMESH_UNSUBSCRIBE_ERROR(18) | | +| 发送异步事件 | MSG_SEND_ASYNC(104) | SUCCESS(0)/EVENTMESH_SEND_ASYNC_MSG_ERR(14) | | + +#### 3. Server发起交互 + +| 场景 | Server向Client发送消息请求码 | Client回复Server消息响应码 | 说明 | +| ------------------ | ---------------------------- | -------------------------- | ---------------------- | +| 客户端接收异步事件 | HTTP_PUSH_CLIENT_ASYNC(105) | retCode | retCode值为0时代表成功 | \ No newline at end of file diff --git a/docs/cn/instructions/eventmesh-runtime-quickstart.zh-CN.md b/docs/cn/instructions/eventmesh-runtime-quickstart.zh-CN.md new file mode 100644 index 0000000000..adc9703796 --- /dev/null +++ b/docs/cn/instructions/eventmesh-runtime-quickstart.zh-CN.md @@ -0,0 +1,268 @@ +

Eventmesh-runtime快速入门说明

+ +## 1 远程部署 + +### 1.1 依赖 + +``` +建议使用64位操作系统,建议使用Linux / Unix; +64位JDK 1.8+; +Gradle至少为7.0, 推荐 7.0.* +``` + +### 1.2 下载源码 + +[https://github.com/apache/incubator-eventmesh](https://github.com/apache/incubator-eventmesh) +您将获得**EventMesh-master.zip** + +### 1.3 构建源码 + +```$ xslt +unzip EventMesh-master.zip +cd / *您的部署路径* /EventMesh-master +gradle clean dist tar -x test +``` + +您将在目录/ *您的部署路径* /EventMesh-master/eventmesh-runtime/dist中获得**eventmesh-runtime_1.0.0.tar.gz** + +### 1.4 部署 + +- 部署eventmesh-runtime + +```$ xslt +upload eventmesh-runtime_1.0.0.tar.gz +tar -zxvf eventmesh-runtime_1.0.0.tar.gz +cd bin +配置 eventMesh.properties +cd ../bin +sh start.sh +``` + +如果看到"EventMeshTCPServer[port=10000] started....",则说明设置成功。 + +## 2 本地构建运行 + +### 2.1 依赖 + +同上述步骤 1.1 + +### 2.2 下载源码 + +同上述步骤 1.2 + +### 2.3 本地启动 + +**2.3.1 项目结构说明:** + +![project-structure](../../images/project-structure.png) + +- eventmesh-common : eventmesh公共类与方法模块 +- eventmesh-connector-api : eventmesh插件接口定义模块 +- eventmesh-connector-rocketmq : eventmesh rocketmq插件模块 +- eventmesh-runtime : eventmesh运行时模块 +- eventmesh-sdk-java : eventmesh java客户端sdk +- eventmesh-starter : eventmesh本地启动运行项目入口 + +> 注:插件模块遵循java spi机制,需要在对应模块中的/main/resources/META-INF/services 下配置相关接口与实现类的映射文件 + +**2.3.2 配置VM启动参数** + +```java +-Dlog4j.configurationFile=eventmesh-runtime/conf/log4j2.xml +-Deventmesh.log.home=eventmesh-runtime/logs +-Deventmesh.home=eventmesh-runtime +-DconfPath=eventmesh-runtime/conf +``` +> 注:如果操作系统为Windows, 可能需要将文件分隔符换成\ + +**2.3.3 配置build.gradle文件** + +通过修改dependencies,compile project 项来指定项目启动后加载的插件 + +修改`eventmesh-starter`模块下面的`build.gradle`文件 + +加载**RocketMQ**插件配置: + +```java +dependencies { + compile project(":eventmesh-runtime"), project(":eventmesh-connector-rocketmq") +} +``` + +**2.3.4 启动运行** + +``` +运行org.apache.eventmesh.starter.StartUp的主要方法 +``` + +## 3 Docker 运行 + +### 3.1 拉取镜像 + +执行 `docker pull eventmesh/eventmesh-rocketmq:v1.2.0` , 你将会获取到EventMesh的镜像,如下图所示: + +![image-20210309155255510](../../images/docker/docker-image.png) + +### 3.2 配置 + +> **预先准备** : 你可能需要从github上下载源代码,并参考这两个文件(eventMesh.properties 和 rocketmq-client.properties)的内容来做下面的操作 + +**3.2.1 需要配置的文件** + +在运行容器之前,你需要配置如下文件: + +**eventMesh.properties** + +| 配置项 | 默认值 | 备注 | +| ---------------------- | ------ | ----------------------- | +| eventMesh.server.http.port | 10105 | EventMesh http 服务端口 | +| eventMesh.server.tcp.port | 10000 | EventMesh tcp 服务端口 | + +**rocketmq-client.properties** + +| 配置项 | 默认值 | 备注 | +| --------------------------------- | ----------------------------- | --------------------- | +| eventMesh.server.rocketmq.namesrvAddr | 127.0.0.1:9876;127.0.0.1:9876 | RocketMQ namesrv 地址 | + +拉取了EventMesh镜像到你的宿主机后,你可以执行下面的命令来完成**eventMesh.properties**和**rocketmq-client.properties** 文件的配置 + +**3.2.2 创建文件** + +```shell +mkdir -p /data/eventmesh/rocketmq/conf +cd /data/eventmesh/rocketmq/conf +vi eventMesh.properties +vi rocketmq-client.properties +``` + +这两个文件内容可以参考 [eventMesh.properties](https://github.com/apache/incubator-eventmesh/blob/develop/eventmesh-runtime/conf/eventMesh.properties) +和 [rocketmq-client.properties](https://github.com/apache/incubator-eventmesh/blob/develop/eventmesh-runtime/conf/rocketmq-client.properties) + +### 3.3 运行 + +**3.3.1 运行** + +执行下面的命令来运行容器 + +```shell +docker run -d -p 10000:10000 -p 10105:10105 -v /data/eventmesh/rocketmq/conf/eventMesh.properties:/data/app/eventmesh/conf/eventMesh.properties -v /data/eventmesh/rocketmq/conf/rocketmq-client.properties:/data/app/eventmesh/conf/rocketmq-client.properties docker.io/eventmesh/eventmesh-rocketmq:v1.2.0 +``` + +> -p : 将容器内端口与宿主机端口绑定,容器的端口应与配置文件中的端口一致 +> +> -v : 将容器内的配置文件挂载到宿主机下,需注意配置文件的路径 + +**3.3.2 检查容器的运行状况** + +执行 `docker ps` 来检查容器的运行状况 + +![image-docker-ps](../../images/docker/docker-ps.png) + +执行 `docker logs [container id]` 可以得到如下结果 + +![image-docker-logs](../../images/docker/docker-logs.png) + +执行 `docker exec -it [container id] /bin/bash` 可以进入到容器中并查看详细信息 + +![image-docker-exec](../../images/docker/docker-exec.png) + +### 3.4 测试 + +**预先准备** :RocketMQ Namesrv & Broker + +你可以通过[这里](https://github.com/apache/rocketmq-docker)来构建rocketmq镜像或者从 docker hub上获取rocketmq镜像. + +```shell +#获取namesrv镜像 +docker pull rocketmqinc/rocketmq-namesrv:4.5.0-alpine +#获取broker镜像 +docker pull rocketmqinc/rocketmq-broker:4.5.0-alpine + +#运行namerv容器 +docker run -d -p 9876:9876 -v `pwd` /data/namesrv/logs:/root/logs -v `pwd`/data/namesrv/store:/root/store --name rmqnamesrv rocketmqinc/rocketmq-namesrv:4.5.0-alpine sh mqnamesrv + +#运行broker容器 +docker run -d -p 10911:10911 -p 10909:10909 -v `pwd`/data/broker/logs:/root/logs -v `pwd`/data/broker/store:/root/store --name rmqbroker --link rmqnamesrv:namesrv -e "NAMESRV_ADDR=namesrv:9876" rocketmqinc/rocketmq-broker:4.5.0-alpine sh mqbroker -c ../conf/broker.conf +``` + +这里 **rocketmq-broker ip** 是 **pod ip**, 如果你想修改这个ip, 可以通过挂载容器中 **broker.conf** 文件的方式并修改文件中的 **brokerIP1** 配置项为自定义值 + +**3.4.1 运行示例** + +Windows + +- Windows系统下运行示例可以参考[这里](https://github.com/apache/incubator-eventmesh/blob/develop/docs/cn/instructions/eventmesh-sdk-java-quickstart.zh-CN.md) + +Linux + +- **获取 eventmesh-test_1.2.0-SNAPSHOT.tar.gz** + + 你可以从我们的 **releases** 获取或者**通过源码的方式进行构建** + + **通过源码的方式进行构建**: + + ```shell + cd /* Your Deploy Path */EventMesh/eventmesh-test + gradle clean testdist testtar -x test` + ``` + + 可以在 `/eventmesh-test/build` 目录下获得 **eventmesh-test_1.2.0-SNAPSHOT.tar.gz** + +- **修改配置文件** + + ```shell + #上传 + upload eventmesh-test_1.2.0-SNAPSHOT.tar.gz + #解压 + tar -zxvf eventmesh-test_1.2.0-SNAPSHOT.tar.gz + #配置 + cd conf + config your application.properties + ``` + +- **运行** + + TCP Sub + + ```shell + cd bin + sh tcp_sub.sh + ``` + + TCP Pub + + ```shell + cd bin + sh tcp_pub.sh + ``` + + TCP Sub Broadcast + + ```shell + cd bin + sh tcp_sub_broadcast.sh + ``` + + TCP Pub Broadcast + + ```shell + cd bin + sh tcp_pub_broadcast.sh + ``` + + HTTP Sub + + ```shell + cd bin + sh http_sub.sh + ``` + + HTTP Pub + + ```shell + cd bin + sh http_pub.sh + ``` + + 之后 , 你可以在 `/logs` 目录下面看到不同模式的运行日志 + diff --git a/docs/cn/instructions/eventmesh-sdk-java-quickstart.zh-CN.md b/docs/cn/instructions/eventmesh-sdk-java-quickstart.zh-CN.md index 67d7994de0..aa5768ac15 100644 --- a/docs/cn/instructions/eventmesh-sdk-java-quickstart.zh-CN.md +++ b/docs/cn/instructions/eventmesh-sdk-java-quickstart.zh-CN.md @@ -1,118 +1,76 @@ -##如何运行eventmesh-sdk-java演示 +

如何运行eventmesh-sdk-java演示

-> Eventmesh-sdk-java作为客户端,与eventmesh-emesher通信,用于完成消息的发送和接收。 +> Eventmesh-sdk-java作为客户端,与eventmesh-runtime通信,用于完成消息的发送和接收。 > -> Eventmesh-sdk-java支持同步消息,异步消息和广播消息。同步消息表示生产者发送消息,需要消费者提供响应消息;异步消息表示生产者只发送消息,不关心回复消息。广播消息表示生产者发送一次消息,所有订阅广播主题的消费者都将收到消息 +> Eventmesh-sdk-java支持异步消息和广播消息。异步消息表示生产者只发送消息,不关心回复消息。广播消息表示生产者发送一次消息,所有订阅广播主题的消费者都将收到消息 > > Eventmesh-sdk-java支持HTTP和TCP协议。 +TCP 和 Http 示例都在**eventmesh-test**模块下 -### 1. TCP DEMO - -####同步消息 - -- 创建主题 - -``` -sh runadmin.sh updateTopic -c $ {ClusterName} -t $ {topic} -n $ {namesrvAddr} -``` - - - -*启动消费者,订阅上一步骤已经创建的Topic - -``` -运行com.webank.eventmesh.client.tcp.demo.SyncResponse的主要方法 -``` - - - -启动发送端,发送消息 - -``` -运行com.webank.eventmesh.client.tcp.demo.SyncRequest的主要方法 -``` - - +**注意**:下载了源代码后,需要将`/conf/application.properties` 和 `/conf/log4j2.xml` 复制到 `resources` 目录下 -####异步消息 +![image-test-structure](../../images/eventmesh-test-structure.png) -- 创建主题 - -``` -sh runadmin.sh updateTopic -c $ {ClusterName} -t $ {topic} -n $ {namesrvAddr} -``` +### 1. TCP DEMO +

异步消息

+- 创建主题TEST-TOPIC-TCP-ASYNC,可以通过rocketmq-console或者rocketmq tools 命令 - 启动消费者,订阅上一步骤已经创建的Topic ``` -运行com.webank.eventmesh.client.tcp.demo.AsyncSubscribe的主要方法 -``` - - - -启动发送端,发送消息 - -``` -运行com.webank.eventmesh.client.tcp.demo.AsyncPublish的主要方法 +运行org.apache.eventmesh.tcp.demo.AsyncSubscribe的主要方法 ``` - - -####广播消息 - -- 创建主题 +- 启动发送端,发送消息 ``` -sh runadmin.sh updateTopic -c $ {ClusterName} -t $ {topic} -n $ {namesrvAddr} +运行org.apache.eventmesh.tcp.demo.AsyncPublish的主要方法 ``` +

广播消息

+- 创建主题TEST-TOPIC-TCP-BROADCAST,可以通过rocketmq-console或者rocketmq tools 命令 - 启动消费端,订阅上一步骤已经创建的Topic ``` -运行com.webank.eventmesh.client.tcp.demo.AsyncSubscribeBroadcast的主要方法 +运行org.apache.eventmesh.tcp.demo.AsyncSubscribeBroadcast的主要方法 ``` - - -*启动发送端,发送广播消息 +- 启动发送端,发送广播消息 ``` -运行com.webank.eventmesh.client.tcp.demo.AsyncPublishBroadcast的主要方法 +运行org.apache.eventmesh.tcp.demo.AsyncPublishBroadcast的主要方法 ``` ### 2. HTTP演示 ->对于http,eventmesh-sdk-java仅实现msg的发送。而且它已经支持同步消息和异步消息。 +> 对于http,eventmesh-sdk-java对对于异步事件实现了发送与订阅 > ->在演示中,Java类`LiteMessage`的`content`字段表示一个特殊的协议,因此,如果您要使用eventmesh-sdk-java的http-client,则只需设计协议的内容并提供消费者的应用程序在同一时间。 +>在演示中,Java类`LiteMessage`的`content`字段表示一个特殊的协议,因此,如果您要使用eventmesh-sdk-java的http-client,则只需设计协议的内容并在同一时间提供消费者的应用程序。 +

异步事件

+> 生产者将事件发送给下游即可,无需等待响应 -####同步消息 +- 创建主题TEST-TOPIC-HTTP-ASYNC,可以通过rocketmq-console或者rocketmq tools 命令 ->发送消息,生产者需要等到收到用户的响应消息 +- 启动消费端,订阅Topic + + 异步事件消费端为spring boot demo,运行demo即可启动服务并完成Topic订阅 ``` -运行com.webank.eventmesh.client.http.demo.SyncRequestInstance的主要方法 +运行org.apache.eventmesh.http.demo.sub.SpringBootDemoApplication的主要方法 ``` - - ->发送消息,生产者在回调中处理响应消息 +- 启动发送端,发送消息 ``` -运行com.webank.eventmesh.client.http.demo.AsyncSyncRequestInstance的主要方法 +运行org.apache.eventmesh.http.demo.AsyncPublishInstance的主要方法 ``` -####异步消息 - -``` -运行com.webank.eventmesh.client.http.demo.AsyncPublishInstance的主要方法 -``` \ No newline at end of file diff --git a/docs/cn/instructions/eventmesh-store-quickstart.zh-CN.md b/docs/cn/instructions/eventmesh-store-quickstart.zh-CN.md index b7bbb9e961..a9ca24c273 100644 --- a/docs/cn/instructions/eventmesh-store-quickstart.zh-CN.md +++ b/docs/cn/instructions/eventmesh-store-quickstart.zh-CN.md @@ -1,6 +1,7 @@ -#Eventmesh-store快速入门说明 +# Eventmesh-store快速入门说明 + +### 依赖 -###依赖 ``` 建议使用64位操作系统,建议使用Linux / Unix; 64位JDK 1.8+; @@ -8,30 +9,38 @@ Gradle至少为5.6, 推荐 5.6.* 4g +可用磁盘用于eventmesh-store服务器 ``` -###下载源码 +### 下载源码 + 下载源代码[https://github.com/WeBankFinTech/DeFiBus](https://github.com/WeBankFinTech/DeFiBus) 您将获得**DefiBus-master.zip** -###构建源码 +### 构建源码 + eventmesh-store在下面的部分采用DeFiBus为例,因为默认情况下,eventmesh依赖于defibus作为存储层,其他工具如Rocketmq等也即将推出。 + ``` unzip DefiBus-master.zip cd / *您的部署路径* / DefiBus-master gradle clean dist tar -x test ``` + 您将在目录/*您的部署路径*/DefiBus-master/build中获得**DeFiBus_1.0.0.tar.gz** -###部署 +### 部署 + - 部署DeFiBusNamesrv + ``` 上传DeFiBus_1.0.0.tar.gz tar -zxvf DeFiBus_1.0.0.tar.gz cd bin sh runnamesrv.sh ``` + 如果在../logs/namesrv.log中看到"Thre Name Server boot success”,则说明已成功设置DeFiBus Namesrv。 -部署DeFiBusBroker + ``` 上传DeFiBus_1.0.0.tar.gz tar -zxvf DeFiBus_1.0.0.tar.gz @@ -40,5 +49,5 @@ cd conf cd ../bin sh runbroker.sh ``` -如果看到"The broker \[YOUR-BROKER-NAME, IP:PORT\] boot success."在../logs/broker.log中, -您可以成功设置eventmesh-store。 \ No newline at end of file + +如果看到"The broker \[YOUR-BROKER-NAME, IP:PORT\] boot success."在../logs/broker.log中, 您可以成功设置eventmesh-store。 \ No newline at end of file diff --git a/docs/cn/instructions/quickstart.zh-CN.md b/docs/cn/instructions/quickstart.zh-CN.md index 6ab50786ef..e14bf122bf 100644 --- a/docs/cn/instructions/quickstart.zh-CN.md +++ b/docs/cn/instructions/quickstart.zh-CN.md @@ -1,17 +1,25 @@ -#快速入门说明 +# 快速入门说明 + +### 依赖 -###依赖 ``` 建议使用64位操作系统,建议使用Linux / Unix; 64位JDK 1.8+; Gradle至少为5.6, 推荐 5.6.* ``` -###部署eventmesh-store -阅读更多[这里](eventmesh-store-quickstart.md) +### 部署eventmesh-store + +阅读更多[这里](eventmesh-store-quickstart.zh-CN.md) + +### 部署eventmesh-runtime + +阅读更多[这里](eventmesh-runtime-quickstart.zh-CN.md) + +### 运行eventmesh sdk演示 + +阅读更多[这里](eventmesh-sdk-java-quickstart.zh-CN.md) -###部署eventmesn-emesher -阅读更多[这里](eventmesh-emesher-quickstart.md) +### 通信协议 -###运行eventmesh sdk演示 -阅读更多[这里](eventmesh-sdk-java-quickstart.md) \ No newline at end of file +阅读更多[这里](eventmesh-runtime-protocol.zh-CN.md) \ No newline at end of file diff --git a/docs/en/README.md b/docs/en/README.md index 8a8f3383a4..1b8d3c1178 100644 --- a/docs/en/README.md +++ b/docs/en/README.md @@ -1,43 +1,58 @@ -[![Build Status](https://www.travis-ci.org/WeBankFinTech/DeFiBus.svg?branch=master)](https://www.travis-ci.org/WeBankFinTech/EventMesh) -[![Coverage Status](https://coveralls.io/repos/github/WeBankFinTech/DeFiBus/badge.svg?branch=master)](https://coveralls.io/github/WeBankFinTech/EventMesh?branch=master) -[![GitHub release](https://img.shields.io/badge/release-download-orange.svg)](https://github.com/WeBankFinTech/EventMesh/releases) +# Apache EventMesh (Incubating) +[![CI status](https://github.com/apache/incubator-eventmesh/actions/workflows/ci.yml/badge.svg)](https://github.com/apache/incubator-eventmesh/actions/workflows/ci.yml) +[![CodeCov](https://codecov.io/gh/apache/incubator-eventmesh/branch/develop/graph/badge.svg)](https://codecov.io/gh/apache/incubator-eventmesh) +[![Language grade: Java](https://img.shields.io/lgtm/grade/java/g/apache/incubator-eventmesh.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/apache/incubator-eventmesh/context:java) +[![Total alerts](https://img.shields.io/lgtm/alerts/g/apache/incubator-eventmesh.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/apache/incubator-eventmesh/alerts/) +[![GitHub release](https://img.shields.io/badge/release-download-orange.svg)](https://github.com/apache/incubator-eventmesh/releases) [![License](https://img.shields.io/badge/license-Apache%202-4EB1BA.svg)](https://www.apache.org/licenses/LICENSE-2.0.html) -[点我查看中文版](/cn/README.md) +[点我查看中文版](../cn/README.md) ## What is Event Mesh? -This figure shows the positioning of the event mesh relative to other similar technologies (such as service mesh) in the application framework. -![architecture1](images/eventmesh-define.png) -Event Mesh is a dynamic plug-in cloud-native basic service layer used to decouple the application and middleware layer. It provides flexible, reliable and fast event distribution, and can be managed. -![architecture1](/images/eventmesher.png) +This figure shows the positioning of the event mesh relative to other similar technologies (such as service mesh) in the +application framework. + +![architecture1](../images/eventmesh-define.png) + +Event Mesh is a dynamic plug-in cloud-native basic service layer used to decouple the application and middleware layer. +It provides flexible, reliable and fast event distribution, and can be managed. + +![architecture1](../images/eventmesh-runtime.png) Cloud Native Event Mesh: -![architecture2](/images/bus.png) -The event mesh allows events from one application to be dynamically routed to any other application. -General functions of the event mesh: +![architecture2](../images/eventmesh-panels.png) + +The event mesh allows events from one application to be dynamically routed to any other application. General functions +of the event mesh: + * Event driven; * Event governance; * Dynamic routing; * Cloud native Dependent components: -* DeFiBus : a distributed messaging platform with low latency, high performance and reliability, flexible scalability. [DeFiBus](https://github.com/WeBankFinTech/DeFiBus) + +* DeFiBus : a distributed messaging platform with low latency, high performance and reliability, flexible + scalability. [DeFiBus](https://github.com/WeBankFinTech/DeFiBus) * RocketMQ Key components: -* eventmesh-emesher : an middleware to transmit events between event producers and consumers, support cloud native apps and microservices + +* eventmesh-runtime : an middleware to transmit events between event producers and consumers, support cloud native apps + and microservices * eventmesh-sdk-java : currently supports HTTP and TCP protocols, and will support gRPC in the future -* eventmesh-registry : automatically routes events between applications and services connected to seperate event meshers, manage eventmesh-emesher ## RoadMap + | version | feature | | ---- | ---- | | v1.0.0 |Support DeFiBus as eventstore, support pub/sub, http api, java-sdk| | v1.1.0 |Support rocketmq as eventstore| -| v1.2.0 |Support transaction event| -| v1.3.0 |Support Plug-in architectur| +| v1.2.0 |Support Plug-in architecture, support http sub| +| v1.3.0 |Support cloud event protocal| +| |Support transaction event| | |Support Event Sourcing| | |Support Event orchestration| | |Support Dashboard| @@ -50,23 +65,29 @@ Key components: | |Support c/go/python/nodejs SDK| ## Quick Start -1. Build and deploy event-store([DeFiBus](https://github.com/WeBankFinTech/DeFiBus)), - see instruction ['event-store quickstart'](instructions/eventmesh-store-quickstart.md). -2. Build and deploy eventmesh-emesher, see instruction ['eventmesh-emesher quickstart'](instructions/eventmesh-emesher-quickstart.md). -3. Run eventmesh-sdk-java demo, see instruction ['eventmesh-sdk-java quickstart'](instructions/eventmesh-sdk-java-quickstart.md). + +1. Build and deploy event-store([DeFiBus](https://github.com/WeBankFinTech/DeFiBus)), see + instruction ['event-store quickstart'](instructions/eventmesh-store-quickstart.md). +2. Build and deploy eventmesh-runtime, see + instruction ['eventmesh-runtime quickstart'](instructions/eventmesh-runtime-quickstart.md). +3. Run eventmesh-sdk-java demo, see + instruction ['eventmesh-sdk-java quickstart'](instructions/eventmesh-sdk-java-quickstart.md). ## Contributing -Contributions are always welcomed! Please see [CONTRIBUTING](CONTRIBUTING.md) for detailed guidelines -You can start with the issues labeled with good first issue. -[GitHub Issues](https://github.com/WeBankFinTech/EventMesh/issues) +Contributions are always welcomed! Please see [CONTRIBUTING](../../CONTRIBUTING.md) for detailed guidelines + +You can start with the issues labeled with good first issue. +[GitHub Issues](https://github.com/apache/incubator-eventmesh/issues) ## License + [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.html) Copyright (C) Apache Software Foundation ## Contacts + WeChat group: -![wechat_qr](/images/mesh-helper.png) +![wechat_qr](../images/mesh-helper.png) diff --git a/docs/en/features/architecture.md b/docs/en/features/architecture.md deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs/en/features/eventmesh-cloudevents-sdk-binding.md b/docs/en/features/eventmesh-cloudevents-sdk-binding.md new file mode 100644 index 0000000000..3c1224f413 --- /dev/null +++ b/docs/en/features/eventmesh-cloudevents-sdk-binding.md @@ -0,0 +1,55 @@ +# Lightweight EventMesh SDK (CloudEvents) + +## Introduction + +[EventMesh(incubating)](https://github.com/apache/incubator-eventmesh) is a dynamic +cloud-native eventing infrastructure. + +[CloudEvents](https://github.com/cloudevents/spec) is a specification for describing +event data in common formats to provide interoperability across services, platforms and systems. + +As of May 2021, EventMesh contains the following +major components: `eventmesh-runtime`, `eventmesh-sdk-java` and `eventmesh-connector-rocketmq`. +For a customer to use EventMesh, `eventmesh-runtime` can be deployed as microservices to transmit +customer's events between event producers and consumers. Customer's applications can then interact +with `eventmesh-runtime` using `eventmesh-sdk-java` to publish/subscribe for events on given topics. + +CloudEvents support has been a highly desired feature by EventMesh users. There are many reasons +for users to prefer using a SDK with CloudEvents support: +- CloudEvents is a more widely accepted and supported way to describe events. `eventmesh-sdk-java` + currently uses the `LiteMessage` structure to describe events, which is less standardized. +- CloudEvents's Java SDK has a wider range of distribution methods. For example, EventMesh users + currently need to use the SDK tarball or build from source for every EventMesh release. With + CloudEvents support, it's easier for users to take a dependency on EventMesh's SDK using CloudEvents's + public distributions (e.g. through a Maven configuration). +- CloudEvents's SDK supports multiple languages. Although EventMesh currently only supports a Java SDK, + in future if more languages need to be supported, the extensions can be easier with experience on + binding Java SDK with CloudEvents. + +## Requirements + +### Functional Requirements + +| Requirement ID | Requirement Description | Comments | +| -------------- | ----------------------- | -------- | +| F-1 | EventMesh users should be able to depend on a public SDK to publish/subscribe events in CloudEvents format | Functionality | +| F-2 | EventMesh users should continue to have access to existing EventMesh client features (e.g. load balancing) with an SDK that supports CloudEvent | Feature Parity | +| F-3 | EventMesh developers should be able to sync `eventmesh-sdk-java` and an SDK with CloudEvents support without much effort/pain | Maintainability | + +### Performance Requirements + +| Requirement ID | Requirement Description | Comments | +| -------------- | ----------------------- | -------- | +| P-1 | Client side latency for SDK with CloudEvents support should be similar to current SDK | | + +## Design Details + +Binding with the CloudEvents Java SDK (similar to what Kafka already did, see Reference for more details) +should be an easy way to achieve the requirements. + +Design details TBD. + +## Appendix + +### References +- https://cloudevents.github.io/sdk-java/kafka diff --git a/docs/en/features/https.md b/docs/en/features/https.md index 570c6d4fb5..247853960f 100644 --- a/docs/en/features/https.md +++ b/docs/en/features/https.md @@ -1,8 +1,8 @@ -1.config in eventmesh-emesher +1.config in eventmesh-runtime ``` -proxy.properties(add config as follows) -proxy.server.useTls.enabled=true //default value is false +eventMesh.properties(add config as follows) +eventMesh.server.useTls.enabled=true //default value is false config env varible @@ -12,8 +12,8 @@ config env varible ``` +2.config in eventmesh-sdk-java -2.config in eventmesh-sdk-java ``` // create producer LiteClientConfig liteClientConfig = new liteClientConfig(); diff --git a/docs/en/features/request-response-call.md b/docs/en/features/request-response-call.md deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs/en/instructions/eventmesh-emesher-quickstart.md b/docs/en/instructions/eventmesh-emesher-quickstart.md deleted file mode 100644 index 713de8613a..0000000000 --- a/docs/en/instructions/eventmesh-emesher-quickstart.md +++ /dev/null @@ -1,33 +0,0 @@ -# Eventmesh-emesher Quick start Instruction - -### dependencies -``` -64bit OS, Linux/Unix is recommended; -64bit JDK 1.8+; -Gradle at least 5.6, eg 5.6.* -``` - -### download sources -download source code from [https://github.com/WeBankFinTech/EventMesh](https://github.com/WeBankFinTech/EventMesh) -You will get **EventMesh-master.zip** - -### build sources -```$xslt -unzip EventMesh-master.zip -cd /*YOUR DEPLOY PATH*/EventMesh-master/eventmesh-emesher -gradle clean tar -x test -``` -You will get **eventmesh-emesher_1.0.0.tar.gz** in directory /* YOUR DEPLOY PATH */EventMesh-master/eventmesh-emesher/dist - -### Deployment -- deploy eventmesn-emesher -```$xslt -upload eventmesh-emesher_1.0.0.tar.gz -tar -zxvf eventmesh-emesher_1.0.0.tar.gz -cd conf -config your proxy.properties -cd ../bin -sh start.sh -``` -If you see "ProxyTCPServer[port=10000] started....", you setup emesher successfully. - diff --git a/docs/en/instructions/eventmesh-runtime-protocol.md b/docs/en/instructions/eventmesh-runtime-protocol.md new file mode 100644 index 0000000000..a884a49193 --- /dev/null +++ b/docs/en/instructions/eventmesh-runtime-protocol.md @@ -0,0 +1,261 @@ +## TCP Protocol Document In Eventmesh-Runtime + +#### 1. Protocol Format + +![dataFlow](../../images/protocol/eventmesh-tcp-dataFlow_EN.png) + +**Protocol Specification** + +``` +Magic Code: 9 bit, defaultValue:EventMesh + +Protocol Version: 4 bit, defaultValue:0000 + +Message Size: 4 bit, the total length of message + +Header Size: 4 bit,the length of Message Header + +Message Header: the specific header content of message + +Message Body: the specific body content of message +``` + +#### 2. Message Object in business logic layer + +* Message Composition + +The class of `Package.java` is message object in business logic layer. It contains two parts: header and body. + +```java +public class Package { + + private Header header; + private Object body; +} + + +public class Header { + + private Command cmd; + private int code; + private String msg; + private String seq; +} +``` + +* Specificatiion + +Message Header(header): The field of Command in Header, used to distinguishing different message types. + +Message Body(body): The message body is defined as different objects according to the message type. + +| Command | type of Body | +| ------------------------------------------------------------ | ------------ | +| HEARTBEAT_REQUEST, HEARTBEAT_RESPONSE, HELLO_RESPONSE, CLIENT_GOODBYE_REQUEST, CLIENT_GOODBYE_RESPONSE, SERVER_GOODBYE_REQUEST, SERVER_GOODBYE_RESPONSE, LISTEN_REQUEST, LISTEN_RESPONSE, UNSUBSCRIBE_REQUEST, SUBSCRIBE_RESPONSE, UNSUBSCRIBE_RESPONSE, ASYNC_MESSAGE_TO_SERVER_ACK, BROADCAST_MESSAGE_TO_SERVER_ACK | -- | +| HELLO_REQUEST | UserAgent | +| SUBSCRIBE_REQUEST | Subscription | +| REQUEST_TO_SERVER, REQUEST_TO_CLIENT, RESPONSE_TO_SERVER, RESPONSE_TO_CLIENT, ASYNC_MESSAGE_TO_SERVER, ASYNC_MESSAGE_TO_CLIENT, BROADCAST_MESSAGE_TO_SERVER, BROADCAST_MESSAGE_TO_CLIENT, ASYNC_MESSAGE_TO_CLIENT_ACK, BROADCAST_MESSAGE_TO_CLIENT_ACK, RESPONSE_TO_CLIENT_ACK, REQUEST_TO_CLIENT_ACK | OpenMessage | +| REDIRECT_TO_CLIENT | RedirectInfo | + +#### 3. The Interactive Command between Client and Server(Eventmesh-Runtime) + +```java +public enum Command { + + //HeartBeat + HEARTBEAT_REQUEST(0), //client send heartbeat request to server + HEARTBEAT_RESPONSE(1), //server reply heartbeat response to client + + //Hello + HELLO_REQUEST(2), //client send connect request to server + HELLO_RESPONSE(3), //server reply connect response to client + + //Disconncet + CLIENT_GOODBYE_REQUEST(4), //client send disconnect request to server + CLIENT_GOODBYE_RESPONSE(5), //server reply disconnect response to client + SERVER_GOODBYE_REQUEST(6), //server send disconncet request to client + SERVER_GOODBYE_RESPONSE(7), //client reply disconnect response to server + + //Subscribe and UnSubscribe + SUBSCRIBE_REQUEST(8), //client send subscribe request to server + SUBSCRIBE_RESPONSE(9), //server reply subscribe response to client + UNSUBSCRIBE_REQUEST(10), //client send unsubscribe request to server + UNSUBSCRIBE_RESPONSE(11), //server reply unsubscribe response to client + + //Listen + LISTEN_REQUEST(12), //client send listen request to server + LISTEN_RESPONSE(13), //server reply listen response to client + + //send sync message + REQUEST_TO_SERVER(14), //client(Producer) send sync msg to server + REQUEST_TO_CLIENT(15), //server push sync msg to client(Consumer) + REQUEST_TO_CLIENT_ACK(16), //client(Consumer) send ack of sync msg to server + RESPONSE_TO_SERVER(17), //client(Consumer) send reply msg to server + RESPONSE_TO_CLIENT(18), //server push reply msg to client(Producer) + RESPONSE_TO_CLIENT_ACK(19), //client(Producer) send ack of reply msg to server + + //send async message + ASYNC_MESSAGE_TO_SERVER(20), //client send async msg to server + ASYNC_MESSAGE_TO_SERVER_ACK(21), //server reply ack of async msg to client + ASYNC_MESSAGE_TO_CLIENT(22), //server push async msg to client + ASYNC_MESSAGE_TO_CLIENT_ACK(23), //client reply ack of async msg to server + + //send broadcast message + BROADCAST_MESSAGE_TO_SERVER(24), //client send broadcast msg to server + BROADCAST_MESSAGE_TO_SERVER_ACK(25), //server reply ack of broadcast msg to client + BROADCAST_MESSAGE_TO_CLIENT(26), //server push broadcast msg to client + BROADCAST_MESSAGE_TO_CLIENT_ACK(27), //client reply ack of broadcast msg to server + + //redirect + REDIRECT_TO_CLIENT(30), //server send redirect instruction to client +} +``` + +#### 4. Client initiates interaction + +| Scene | Client Send | Server Reply | Remark | +| -------------- | ---------------------------- | ------------------------------- | ---- | +| Hello | HELLO_REQUEST | HELLO_RESPONSE | | +| Heartbeat | HEARTBEAT_REQUEST | HEARTBEAT_RESPONSE | | +| Subscribe | SUBSCRIBE_REQUEST | SUBSCRIBE_RESPONSE | | +| Unsubscribe | UNSUBSCRIBE_REQUEST | UNSUBSCRIBE_RESPONSE | | +| Listen | LISTEN_REQUEST | LISTEN_RESPONSE | | +| Send sync msg | REQUEST_TO_SERVER | RESPONSE_TO_CLIENT | | +| Send reply msg of sync msg | RESPONSE_TO_SERVER | -- | | +| Send async msg | ASYNC_MESSAGE_TO_SERVER | ASYNC_MESSAGE_TO_SERVER_ACK | | +| Send broadcast msg | BROADCAST_MESSAGE_TO_SERVER | BROADCAST_MESSAGE_TO_SERVER_ACK | | +| Client start disconnect | CLIENT_GOODBYE_REQUEST | CLIENT_GOODBYE_RESPONSE | | + +#### 5. Server initiates interaction + +| Scene | Server Send | Client Reply | Remark | +| ------------------ | ---------------------------- | ------------------------------- | ---- | +| Push sync msg to client | REQUEST_TO_CLIENT | REQUEST_TO_CLIENT_ACK | | +| Push reply msg of sync msg to client | RESPONSE_TO_CLIENT | RESPONSE_TO_CLIENT_ACK | | +| Push async msg to client | ASYNC_MESSAGE_TO_CLIENT | ASYNC_MESSAGE_TO_CLIENT_ACK | | +| Push broadcast msg to client | BROADCAST_MESSAGE_TO_CLIENT | BROADCAST_MESSAGE_TO_CLIENT_ACK | | +| Server start disconnect | SERVER_GOODBYE_REQUEST | -- | | +| Server send redirect | REDIRECT_TO_CLIENT | -- | | + +#### 6. Message classification + ++ Send sync msg + +![rr-msg](../../images/protocol/eventmesh-rr-msg.png) + ++ Send async msg + +![async-msg](../../images/protocol/eventmesh-async-msg.png) + ++ Send broadcast msg + +![broadcast-msg](../../images/protocol/eventmesh-broadcast-msg.png) + +## HTTP Protocol Document In Eventmesh-Runtime + +The class of `LiteMessage.java` is message definition in http protocal of EventMesh-Runtime.If you want to send msg by +using http protocol,you can use client in eventmesh-sdk-java, and you just need care the specific protocol in the field +of content. + +```java +public class LiteMessage { + + private String bizSeqNo; + + private String uniqueId; + + private String topic; + + private String content; + + private Map prop; + + private long createTime = System.currentTimeMillis(); +} +``` + +#### 1. Message Send and Message Composition + +**Request Method**: POST + +**Message Composition**: RequestHeader + RequestBody + ++ Heartbeat Msg + +**RequestHeader** + +| Key | Description | +| -------- | ---------------- | +| Env | Enviroment of Client | +| Region | Region of Client | +| Idc | IDC of Client | +| Dcn | DCN of Client | +| Sys | Subsystem ID of Client | +| Pid | Client Process ID | +| Ip | Client Ip | +| Username | Client username | +| Passwd | Client password | +| Version | Protocol version | +| Language | Develop language | +| Code | Request Code | + +**RequestBody** + +| Key | Description | +| ----------------- | ------------------------------ | +| clientType | Producer:clientType is ClientType.PUB,Consumer:clientType is ClientType.SUB | +| heartbeatEntities | Heartbeat content,contains topic,url... | + ++ Subscribe Msg + +**RequestHeader** + +same with RequestHeader of heartbeat msg + +**RequestBody** + +| Key | Description | +| ----- | ----------------- | +| topic | topic of client want to subscribe | +| url | url of client, server push msg to the url when receiving msg from other components | + ++ Unsubscribe Msg + +**RequestHeader** + +same with RequestHeader of Heartbeat Msg + +**RequestBody** + +same with RequestBody of Subscribe Msg + ++ Send async msg + +**RequestHeader** + +same with RequestHeader of Heartbeat Msg + +**RequestBody** + +| Key | Description | +| -------- | ----------------------- | +| topic | topic of msg | +| content | msg content | +| ttl | timeout time of msg | +| bizSeqNo | biz sequence number of msg | +| uniqueId | unique mark of msg | + +#### 2. Client initiates interaction + +| Scene | Client Send | Server Reply | Remark | +| ------------ | ---------------------------- | --------------------------------------- | ---- | +| Heartbeat | HEARTBEAT(203) | SUCCESS(0)/EVENTMESH_HEARTBEAT_ERROR(19) | | +| Subscribe | SUBSCRIBE(206) | SUCCESS(0)/EVENTMESH_SUBSCRIBE_ERROR(17) | | +| Unsubscribe | UNSUBSCRIBE(207) | SUCCESS(0)/EVENTMESH_UNSUBSCRIBE_ERROR(18) | | +| Send async msg | MSG_SEND_ASYNC(104) | SUCCESS(0)/EVENTMESH_SEND_ASYNC_MSG_ERR(14) | | + +#### 3. Server initiates interaction + +| Scene | Server Send | Client Reply | Remark | +| ------------------ | ---------------------------- | -------------------------- | ---------------------- | +| Push async msg to client | HTTP_PUSH_CLIENT_ASYNC(105) | retCode | retCode=0,send success | \ No newline at end of file diff --git a/docs/en/instructions/eventmesh-runtime-quickstart.md b/docs/en/instructions/eventmesh-runtime-quickstart.md new file mode 100644 index 0000000000..0536e5b6ee --- /dev/null +++ b/docs/en/instructions/eventmesh-runtime-quickstart.md @@ -0,0 +1,267 @@ +# Eventmesh-runtime Quick start Instruction + +## 1 Remote Deploy + +### 1.1 dependencies + +``` +64bit OS, Linux/Unix is recommended; +64bit JDK 1.8+; +Gradle at least 7.0, eg 7.0.* +``` + +### 1.2 download sources + +download source code from [https://github.com/apache/incubator-eventmesh](https://github.com/apache/incubator-eventmesh) +You will get **EventMesh-master.zip** + +### 1.3 build sources + +```$xslt +unzip EventMesh-master.zip +cd /*YOUR DEPLOY PATH*/EventMesh-master +gradle clean dist tar -x test +``` + +You will get **EventMesh_1.2.0.tar.gz** in directory /* YOUR DEPLOY PATH */EventMesh-master/build + +### 1.4 Deployment + +- deploy eventmesh-runtime + +```shell +upload Eventmesh_1.2.0.tar.gz +tar -zxvf Eventmesh_1.2.0.tar.gz +cd conf +config your eventMesh.properties +cd ../bin +sh start.sh +``` + +If you see "EventMeshTCPServer[port=10000] started....", you setup runtime successfully. + +## 2 Run Locally + +### 2.1 dependencies + +Same with 1.1 + +### 2.2 download sources + +Same with 1.2 + +### 2.3 Run + +**2.3.1 Project structure:** + +![project-structure.png](../../images/project-structure.png) + +- eventmesh-common : eventmesh common classes and method module +- eventmesh-connector-api : eventmesh connector api definition module +- eventmesh-connector-rocketmq : eventmesh rocketmq connector module +- eventmesh-runtime : eventmesh runtime module +- eventmesh-sdk-java : eventmesh java client sdk +- eventmesh-starter : eventmesh project local start entry + +> ps: The loading of connector plugin follows the Java SPI mechanism, it's necessary to configure the mapping file of +related interface and implementation class under /main/resources/meta-inf/services in the corresponding module + +**2.3.2 Configure VM Options** + +```java +-Dlog4j.configurationFile=eventmesh-runtime/conf/log4j2.xml +-Deventmesh.log.home=eventmesh-runtime/logs +-Deventmesh.home=eventmesh-runtime +-DconfPath=eventmesh-runtime/conf +``` +> ps: If you use Windows, you may need to replace the file separator to \ + +**2.3.3 Configure build.gradle file** + +Specify the connector that will be loaded after the project start with updating compile project item in dependencies + +update `build.gradle` file under the `eventmesh-starter` module + +load **rocketmq connector** configuration: + +```java +dependencies { + compile project(":eventmesh-runtime"), project(":eventmesh-connector-rocketmq") +} +``` + +**2.3.4 Run** + +running `org.apache.eventmesh.starter.StartUp` main method + +## 3 Run with Docker + +### 3.1 Pull + +execute `docker pull eventmesh/eventmesh-rocketmq:v1.2.0` , you will get EventMesh image like below + +![image-20210309155255510](../../images/docker/docker-image.png) + +### 3.2 Config + +> **prerequisite** : may be you need download the source code from git first and use the contents of these files(eventMesh.properties and rocketmq-client.properties) as a reference for the following actions. + +**3.2.1 Files to configure** + +Before run the container you should configure some files. + +**eventMesh.properties** + +| Configuration Key | Default Value | Remarks | +| ---------------------- | ------------- | -------------------------- | +| eventMesh.server.http.port | 10105 | EventMesh http server port | +| eventMesh.server.tcp.port | 10000 | EventMesh tcp server port | + +**rocketmq-client.properties** + +| Configuration Key | Default Value | Remarks | +| --------------------------------- | ----------------------------- | -------------------------------- | +| eventMesh.server.rocketmq.namesrvAddr | 127.0.0.1:9876;127.0.0.1:9876 | RocketMQ namesrv default address | + +After pull the EventMesh image to your host machine, you can execute command below to configure **eventMesh.properties** +and **rocketmq-client.properties** + +**3.2.2 Create Files** + +```shell +mkdir -p /data/eventmesh/rocketmq/conf +cd /data/eventmesh/rocketmq/conf +vi eventMesh.properties +vi rocketmq-client.properties +``` + +The contents of these files can reference +from [eventMesh.properties](https://github.com/WeBankFinTech/EventMesh/blob/develop/eventmesh-runtime/conf/eventMesh.properties) +and [rocketmq-client.properties](https://github.com/WeBankFinTech/EventMesh/blob/develop/eventmesh-runtime/conf/rocketmq-client.properties) + +### 3.3 Run + +**3.3.1 run** + +execute command below to run container + +``` +docker run -d -p 10000:10000 -p 10105:10105 -v /data/eventmesh/rocketmq/conf/eventMesh.properties:/data/app/eventmesh/conf/eventMesh.properties -v /data/eventmesh/rocketmq/conf/rocketmq-client.properties:/data/app/eventmesh/conf/rocketmq-client.properties docker.io/eventmesh/eventmesh-rocketmq:v1.2.0 +``` + +> -p : binding the container port with host machine port +> +> -v : mount the container configuration files with host machine files + +**3.3.2 check container** + +execute `docker ps` to check the container health + +![image-docker-ps](../../images/docker/docker-ps.png) + +execute `docker logs [container id]` you will get following result: + +![image-docker-logs](../../images/docker/docker-logs.png) + +execute `docker exec -it [container id] /bin/bash` you will go into the container and see the details: + +![image-docker-exec](../../images/docker/docker-exec.png) + +### 3.4 Test + +**Prerequisite** :RocketMQ Namesrv & Broker + +you can build the rocketmq image following [here](https://github.com/apache/rocketmq-docker) or get the rocketmq image +from docker hub. + +``` +docker pull rocketmqinc/rocketmq-namesrv:4.5.0-alpine +docker pull rocketmqinc/rocketmq-broker:4.5.0-alpine + +#namesrv +docker run -d -p 9876:9876 -v `pwd` /data/namesrv/logs:/root/logs -v `pwd`/data/namesrv/store:/root/store --name rmqnamesrv rocketmqinc/rocketmq-namesrv:4.5.0-alpine sh mqnamesrv + +#broker +docker run -d -p 10911:10911 -p 10909:10909 -v `pwd`/data/broker/logs:/root/logs -v `pwd`/data/broker/store:/root/store --name rmqbroker --link rmqnamesrv:namesrv -e "NAMESRV_ADDR=namesrv:9876" rocketmqinc/rocketmq-broker:4.5.0-alpine sh mqbroker -c ../conf/broker.conf +``` + +When we get this point, **rocketmq-broker ip** is the **pod ip**, if you want to change the ip, you can mount the ** +broker.conf** file in container and modify **brokerIP1** configuration in this file to your custom values. + +**3.4.1 Run Demo** + +Windows + +- For demos running under the Windows , you can + refer [here](https://github.com/WeBankFinTech/EventMesh/blob/develop/docs/en/instructions/eventmesh-sdk-java-quickstart.md) + +Linux + +- **Get eventmesh-test_1.2.0-SNAPSHOT.tar.gz** + + you can get this package from **our releases** or **build with source code**. + + **build with source code**: + + ```shell + cd /* Your Deploy Path */EventMesh/eventmesh-test + gradle clean testdist testtar -x test` + ``` + + you will get **eventmesh-test_1.2.0-SNAPSHOT.tar.gz** under the /eventmesh-test/build + +- **Modify configuration files** + + ```shell + upload eventmesh-test_1.2.0-SNAPSHOT.tar.gz + tar -zxvf eventmesh-test_1.2.0-SNAPSHOT.tar.gz + cd conf + config your application.properties + ``` + +- **Run demo** + + TCP Sub + + ```shell + cd bin + sh tcp_sub.sh + ``` + + TCP Pub + + ```shell + cd bin + sh tcp_pub.sh + ``` + + TCP Sub Broadcast + + ```shell + cd bin + sh tcp_sub_broadcast.sh + ``` + + TCP Pub Broadcast + + ```shell + cd bin + sh tcp_pub_broadcast.sh + ``` + + HTTP Sub + + ```shell + cd bin + sh http_sub.sh + ``` + + HTTP Pub + + ```shell + cd bin + sh http_pub.sh + ``` + + After this , you can see result of different mode in logs file under `/logs` directory + diff --git a/docs/en/instructions/eventmesh-sdk-java-quickstart.md b/docs/en/instructions/eventmesh-sdk-java-quickstart.md index 2a79694f6e..53eb673709 100644 --- a/docs/en/instructions/eventmesh-sdk-java-quickstart.md +++ b/docs/en/instructions/eventmesh-sdk-java-quickstart.md @@ -1,119 +1,76 @@ ## How to run eventmesh-sdk-java demo -> Eventmesh-sdk-java , as the client, communicated with eventmesh-emesher, used to complete the sending and receiving of message. +> Eventmesh-sdk-java , as the client, communicated with eventmesh-runtime, used to complete the sending and receiving of message. > -> Eventmesh-sdk-java supports sync msg, async msg and broadcast msg. Sync msg means the producer sends msg which need the consumer supplies the response msg, Async msg means the producer just sends msg and does not care reply msg.Broadcast msg means the producer send msg once and all the consumer subscribed the broadcast topic will receive the msg. +> Eventmesh-sdk-java supports async msg and broadcast msg. Async msg means the producer just sends msg and does not care reply msg.Broadcast msg means the producer send msg once and all the consumer subscribed the broadcast topic will receive the msg. > -> Eventmesh-sdk-java supports the protocol of HTTP and TCP. +> Eventmesh-sdk-java supports the protocol of HTTP and TCP. +TCP demos and Http demos are both under the **eventmesh-test** module. -### 1. TCP DEMO +**prerequisite**:after download the source code you should copy `/conf/application.properties` and `/conf/log4j2.xml` to +the `resources` directory -#### Sync msg +![image-test-structure](../../images/eventmesh-test-structure.png) -- create topic - -``` -sh runadmin.sh updateTopic -c ${ClusterName} -t ${topic} -n ${namesrvAddr} -``` - - - -* start consumer ,subscribe topic in previous step. - -``` -Run the main method of SyncResponse -``` - - - -* start producer, send message - -``` -Run the main method of SyncRequest -``` - - - -#### Async msg - -- create topic - -``` -sh runadmin.sh updateTopic -c ${ClusterName} -t ${topic} -n ${namesrvAddr} -``` - - - -- start consumer ,subscribe topic in previous step. - -``` -Run the main method of AsyncSubscribe -``` +### 1. TCP DEMO +#### Async msg +- create topic TEST-TOPIC-TCP-ASYNC on rocketmq-console -start producer, send message +- start consumer ,subscribe topic in previous step. ``` -Run the main method of AsyncPublish +Run the main method of org.apache.eventmesh.tcp.demo.AsyncSubscribe ``` - - -#### Broadcast msg - -- create topic +- start producer, send message ``` -sh runadmin.sh updateTopic -c ${ClusterName} -t ${topic} -n ${namesrvAddr} +Run the main method of org.apache.eventmesh.tcp.demo.AsyncPublish ``` +#### Broadcast msg +- create topic TEST-TOPIC-TCP-BROADCAST on rocketmq-console -- start consumer ,subscribe topic in previous step. +- start consumer ,subscribe topic in previous step. ``` -Run the main method of AsyncSubscribeBroadcast +Run the main method of org.apache.eventmesh.tcp.demo.AsyncSubscribeBroadcast ``` - - * start producer, send broadcast message ``` -Run the main method of AsyncPublishBroadcast +Run the main method of org.apache.eventmesh.tcp.demo.AsyncPublishBroadcast ``` ### 2. HTTP DEMO -> As to http, eventmesh-sdk-java just implements the sending of msg. And it already supports sync msg and async msg. +> As to http, eventmesh-sdk-java implements the pub and sub for async event . > > In the demo ,the field of `content` of the java class `LiteMessage` represents a special protocal, so if you want to use http-client of eventmesh-sdk-java, you just need to design the content of protocal and supply the consumer appliacation at the same time. +#### Async event +> producer send the event to consumer and don't need waiting response msg from consumer -#### Sync msg - -> send msg ,producer need waiting until receive the response msg of consumer - -``` -Run the main method of SyncRequestInstance -``` - +- create topic TEST-TOPIC-HTTP-ASYNC on rocketmq-console +- start consumer, subscribe topic -> send msg,producer handles the reponse msg in callback + Async consumer demo is a spring boot application demo, you can easily run this demo to start service and subscribe the + topic. ``` -Run the main method of com.webank.eventmesh.client.http.demo.AsyncSyncRequestInstance +Run the main method of org.apache.eventmesh.http.demo.sub.SpringBootDemoApplication ``` - - -#### Async msg +- start producer, produce msg ``` -Run the main method of AsyncPublishInstance +Run the main method of org.apache.eventmesh.http.demo.AsyncPublishInstance ``` diff --git a/docs/en/instructions/eventmesh-store-quickstart.md b/docs/en/instructions/eventmesh-store-quickstart.md index 8ba16d99aa..74158e85fc 100644 --- a/docs/en/instructions/eventmesh-store-quickstart.md +++ b/docs/en/instructions/eventmesh-store-quickstart.md @@ -1,6 +1,7 @@ # Eventmesh-store Quick start Instruction ### dependencies + ``` 64bit OS, Linux/Unix is recommended; 64bit JDK 1.8+; @@ -9,29 +10,38 @@ Gradle at least 5.6, eg 5.6.* ``` ### download sources + download source code from [https://github.com/WeBankFinTech/DeFiBus](https://github.com/WeBankFinTech/DeFiBus) You will get **DefiBus-master.zip** ### build sources -The eventmesh-store takes DeFiBus for example at the follwing parts, because eventmesh depends on defibus as store layer by default, other implements such as Rocketmq etc. is coming soon. + +The eventmesh-store takes DeFiBus for example at the following parts, because eventmesh depends on defibus as store layer +by default, other implements such as Rocketmq etc. is coming soon. + ``` unzip DefiBus-master.zip cd /*YOUR DEPLOY PATH*/DefiBus-master gradle clean dist tar -x test ``` + You will get **DeFiBus_1.0.0.tar.gz** in directory /* YOUR DEPLOY PATH */DefiBus-master/build ### Deployment -- deploy DeFiBusNamesrv + +- deploy DeFiBusNamesrv + ``` upload DeFiBus_1.0.0.tar.gz tar -zxvf DeFiBus_1.0.0.tar.gz cd bin sh runnamesrv.sh ``` + If you see "Thre Name Server boot success" in ../logs/namesrv.log, you setup DeFiBus Namesrv successfully. - deploy DeFiBusBroker + ``` upload DeFiBus_1.0.0.tar.gz tar -zxvf DeFiBus_1.0.0.tar.gz @@ -40,6 +50,7 @@ config your broker.properties cd ../bin sh runbroker.sh ``` -If you see "The broker \[YOUR-BROKER-NAME, IP:PORT\] boot success." in ../logs/broker.log, -you setup eventmesh-store successfully. + +If you see "The broker \[YOUR-BROKER-NAME, IP:PORT\] boot success." in ../logs/broker.log, you setup eventmesh-store +successfully. diff --git a/docs/en/instructions/quickstart.md b/docs/en/instructions/quickstart.md index 63e57c96fa..697dd7c5e4 100644 --- a/docs/en/instructions/quickstart.md +++ b/docs/en/instructions/quickstart.md @@ -1,6 +1,7 @@ # Quick start Instruction ### dependencies + ``` 64bit OS, Linux/Unix is recommended; 64bit JDK 1.8+; @@ -8,10 +9,13 @@ Gradle at least 5.6, eg 5.6.* ``` ### Deploy eventmesh-store + Read more [here](eventmesh-store-quickstart.md) -### Deploy eventmesn-emesher -Read more [here](eventmesh-emesher-quickstart.md) +### Deploy eventmesh-runtime + +Read more [here](eventmesh-runtime-quickstart.md) ### Run eventmesh sdk demo + Read more [here](eventmesh-sdk-java-quickstart.md) \ No newline at end of file diff --git a/docs/eventmesh.md b/docs/eventmesh.md index 70b6102706..432c09360d 100644 --- a/docs/eventmesh.md +++ b/docs/eventmesh.md @@ -1,33 +1,36 @@ ## 什么是Event Mesh? -该图显示了Event Mesh相对于应用程序框架中其他类似技术(例如Service Mesh)的定位. -![architecture1](docs/images/eventmesh-define.png) -Event Mesh是一个动态的插件式云原生基础服务层,用于分离应用程序和中间件层。它提供了灵活,可靠和快速的事件分发,并且可以进行管理: -![architecture1](docs/images/eventmesher.png) +EventMesh是一个动态的云原生事件驱动架构基础设施,用于分离应用程序和后端中间件层,它支持广泛的用例,包括复杂的混合云、使用了不同技术栈的分布式架构。 +![architecture1](images/eventmesh-define.png) -云原生Event Mesh: -![architecture2](docs/images/bus.png) +**EventMesh架构:** + +![architecture1](images/eventmesh-runtime.png) + +**EventMesh云原生结构:** + +![architecture2](images/eventmesh-panels.png) + +EventMesh允许将来自一个应用程序的事件动态路由到任何其他应用程序. EventMesh的一般功能: -Event Mesh允许将来自一个应用程序的事件动态路由到任何其他应用程序. -Event Mesh的一般功能: * 事件驱动; * 事件治理; * 动态路由; * 云原生; 依赖部件: -* 可选1:DeFiBus:具有低延迟,高性能和可靠性,和灵活可伸缩性的分布式消息传递平台 [DeFiBus](https://github.com/WeBankFinTech/DeFiBus) -* 可选2:RocketMQ + +* [RocketMQ](https://github.com/apache/rocketmq):RocketMQ是一个分布式消息流平台,具有低延迟、高性能和可靠性、万亿级容量和灵活的可伸缩性。 关键部件: -* event mesher:一种中间件,用于在事件产生者和使用者之间传输事件,支持云原生应用程序和微服务 -* sdk:当前支持HTTP和TCP协议,未来会支持gRPC等 -* registry:自动在连接到事件网格的应用程序和服务之间路由事件, 管理event mesher +* eventmesh-runtime:一种中间件,用于在事件产生者和使用者之间传输事件,支持云原生应用程序和微服务 +* eventmesh-sdk-java:当前支持HTTP和TCP协议,未来会支持gRPC等 ## 开源地址 + * https://github.com/WeBankFinTech/DeFiBus -* https://github.com/WeBankFinTech/EventMesh +* https://github.com/apache/incubator-eventmesh * https://gitee.com/WeBank/DeFiBus * https://gitee.com/WeBank/EventMesh diff --git a/docs/images/docker/docker-exec.png b/docs/images/docker/docker-exec.png new file mode 100644 index 0000000000..f605921156 Binary files /dev/null and b/docs/images/docker/docker-exec.png differ diff --git a/docs/images/docker/docker-image.png b/docs/images/docker/docker-image.png new file mode 100644 index 0000000000..5ca80021d4 Binary files /dev/null and b/docs/images/docker/docker-image.png differ diff --git a/docs/images/docker/docker-logs.png b/docs/images/docker/docker-logs.png new file mode 100644 index 0000000000..8cd3c4df5b Binary files /dev/null and b/docs/images/docker/docker-logs.png differ diff --git a/docs/images/docker/docker-ps.png b/docs/images/docker/docker-ps.png new file mode 100644 index 0000000000..9ae1c52b2c Binary files /dev/null and b/docs/images/docker/docker-ps.png differ diff --git a/docs/images/eventmesh-define.png b/docs/images/eventmesh-define.png index 0f28170127..971afccc21 100644 Binary files a/docs/images/eventmesh-define.png and b/docs/images/eventmesh-define.png differ diff --git a/docs/images/eventmesh-multi-runtime.png b/docs/images/eventmesh-multi-runtime.png new file mode 100644 index 0000000000..a9b90be778 Binary files /dev/null and b/docs/images/eventmesh-multi-runtime.png differ diff --git a/docs/images/eventmesh-panels.png b/docs/images/eventmesh-panels.png new file mode 100644 index 0000000000..898dbb42ba Binary files /dev/null and b/docs/images/eventmesh-panels.png differ diff --git a/docs/images/eventmesh-runtime.png b/docs/images/eventmesh-runtime.png new file mode 100644 index 0000000000..a87a8d6908 Binary files /dev/null and b/docs/images/eventmesh-runtime.png differ diff --git a/docs/images/eventmesh-test-structure.png b/docs/images/eventmesh-test-structure.png new file mode 100644 index 0000000000..b783f1edb0 Binary files /dev/null and b/docs/images/eventmesh-test-structure.png differ diff --git a/docs/images/logo1.png b/docs/images/logo1.png new file mode 100644 index 0000000000..00cba89e08 Binary files /dev/null and b/docs/images/logo1.png differ diff --git a/docs/images/logo2.png b/docs/images/logo2.png new file mode 100644 index 0000000000..248020806c Binary files /dev/null and b/docs/images/logo2.png differ diff --git a/docs/images/project-structure.png b/docs/images/project-structure.png new file mode 100644 index 0000000000..252a9536c6 Binary files /dev/null and b/docs/images/project-structure.png differ diff --git a/docs/images/protocol/eventmesh-async-msg.png b/docs/images/protocol/eventmesh-async-msg.png new file mode 100644 index 0000000000..4ad6ac4e03 Binary files /dev/null and b/docs/images/protocol/eventmesh-async-msg.png differ diff --git a/docs/images/protocol/eventmesh-broadcast-msg.png b/docs/images/protocol/eventmesh-broadcast-msg.png new file mode 100644 index 0000000000..7c3be10c30 Binary files /dev/null and b/docs/images/protocol/eventmesh-broadcast-msg.png differ diff --git a/docs/images/protocol/eventmesh-rr-msg.png b/docs/images/protocol/eventmesh-rr-msg.png new file mode 100644 index 0000000000..b1c462fa51 Binary files /dev/null and b/docs/images/protocol/eventmesh-rr-msg.png differ diff --git a/docs/images/protocol/eventmesh-tcp-dataFlow.png b/docs/images/protocol/eventmesh-tcp-dataFlow.png new file mode 100644 index 0000000000..d3c1249d53 Binary files /dev/null and b/docs/images/protocol/eventmesh-tcp-dataFlow.png differ diff --git a/docs/images/protocol/eventmesh-tcp-dataFlow_EN.png b/docs/images/protocol/eventmesh-tcp-dataFlow_EN.png new file mode 100644 index 0000000000..4918cc7f59 Binary files /dev/null and b/docs/images/protocol/eventmesh-tcp-dataFlow_EN.png differ diff --git a/eventmesh-common/build.gradle b/eventmesh-common/build.gradle index e24499d431..d973dcedae 100644 --- a/eventmesh-common/build.gradle +++ b/eventmesh-common/build.gradle @@ -13,84 +13,4 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - */ - -apply plugin: 'java' -apply plugin: "maven" -apply plugin: "eclipse" -apply plugin: "idea" - -if (project.findProperty("jdk") == "1.7") { - sourceCompatibility = 1.7 - targetCompatibility = 1.7 -} else { - sourceCompatibility = 1.8 - targetCompatibility = 1.8 - version = version + "-jdk8" -} - -if (project.findProperty("snapshot") instanceof String) { - if (project.property("snapshot").toBoolean()) version = version + "-SNAPSHOT" -} - -repositories { - mavenLocal() - maven { - url "https://maven.aliyun.com/repository/public" - } - mavenCentral() -} - -List fastjson = [ - "com.alibaba:fastjson:1.2.71" -] - -List jackson = [ - 'com.fasterxml.jackson.core:jackson-databind:2.11.0', - 'com.fasterxml.jackson.core:jackson-core:2.11.0', - 'com.fasterxml.jackson.core:jackson-annotations:2.11.0' -] - -List apache_commons = [ - "org.apache.commons:commons-lang3:3.6", - "org.apache.commons:commons-collections4:4.1" -] - -List httpclient = [ - "org.apache.httpcomponents:httpclient:4.5.2" -] - -List guava = [ - "com.google.guava:guava:29.0-jre" -] - -List netty = [ - "io.netty:netty-all:4.1.49.Final" -] - -List defibus = [ - "com.webank.defibus:defibus-client:$defibus_version", - "com.webank.defibus:defibus-common:$defibus_version" -] - -dependencies { - compile apache_commons, httpclient, guava, netty, fastjson, defibus, jackson - testCompile apache_commons, httpclient, guava, netty, fastjson, defibus, jackson -} - -uploadArchives { - repositories { - mavenDeployer { - snapshotRepository(url: 'Your target repo address') { - authentication(userName: 'Your user name', password: 'Your password') - } - repository(url: 'Your target repo address') { - authentication(userName: 'Your user name', password: 'Your password') - } - } - } -} - -tasks.withType(JavaCompile) { - options.encoding = "UTF-8" -} \ No newline at end of file + */ \ No newline at end of file diff --git a/eventmesh-common/gradle.properties b/eventmesh-common/gradle.properties index e9bfb7fc8b..72ea1ac35b 100644 --- a/eventmesh-common/gradle.properties +++ b/eventmesh-common/gradle.properties @@ -14,9 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. # - -group=com.webank.eventmesh -version=1.1.1 -defibus_version=1.0.1 -jdk=1.7 -snapshot=false +group=org.apache.eventmesh +version=1.2.0-release +jdk=1.8 diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/SubscribeResponseBody.java b/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/SubscribeResponseBody.java deleted file mode 100644 index b0249e22ec..0000000000 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/SubscribeResponseBody.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.eventmesh.common.protocol.http.body.client; - -public class SubscribeResponseBody { -} diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/common/ProxyRetCode.java b/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/common/ProxyRetCode.java deleted file mode 100644 index b9b2e908b3..0000000000 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/common/ProxyRetCode.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.eventmesh.common.protocol.http.common; - -public enum ProxyRetCode { - - SUCCESS(0, "success"), - OVERLOAD(1, "proxy overload, try later, "), - PROXY_REQUESTCODE_INVALID(2, "requestCode can't be null, or must be number, "), - PROXY_SEND_SYNC_MSG_ERR(3, "proxy send rr msg err, "), - PROXY_WAITING_RR_MSG_ERR(4, "proxy waiting rr msg err, "), - PROXY_PROTOCOL_HEADER_ERR(6, "proxy protocol[header] err, "), - PROXY_PROTOCOL_BODY_ERR(7, "proxy protocol[body] err, "), - PROXY_STOP(8, "proxy will stop or had stopped, "), - PROXY_REJECT_BY_PROCESSOR_ERROR(9, "proxy reject by processor error, "), - PROXY_BATCH_PRODUCER_STOPED_ERR(10, "proxy batch msg producer stopped, "), - PROXY_SEND_BATCHLOG_MSG_ERR(17, "proxy send batchlog msg err, "), - PROXY_BATCH_SPEED_OVER_LIMIT_ERR(11, "proxy batch msg speed over the limit, "), - PROXY_PACKAGE_MSG_ERR(12, "proxy package msg err, "), - PROXY_GROUP_PRODUCER_STOPED_ERR(13, "proxy group producer stopped, "), - PROXY_SEND_ASYNC_MSG_ERR(14, "proxy send async msg err, "), - PROXY_REPLY_MSG_ERR(15, "proxy reply msg err, "), - PROXY_RUNTIME_ERR(16, "proxy runtime err, "); - - private Integer retCode; - - private String errMsg; - - ProxyRetCode(Integer retCode, String errMsg) { - this.retCode = retCode; - this.errMsg = errMsg; - } - - public Integer getRetCode() { - return retCode; - } - - public void setRetCode(Integer retCode) { - this.retCode = retCode; - } - - public String getErrMsg() { - return errMsg; - } - - public void setErrMsg(String errMsg) { - this.errMsg = errMsg; - } -} diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/HeartbeatResponseHeader.java b/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/HeartbeatResponseHeader.java deleted file mode 100644 index 14e0d30493..0000000000 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/HeartbeatResponseHeader.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.eventmesh.common.protocol.http.header.client; - - -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import com.webank.eventmesh.common.protocol.http.header.Header; - -import java.util.HashMap; -import java.util.Map; - -public class HeartbeatResponseHeader extends Header { - - private int code; - - private String proxyCluster; - - private String proxyIp; - - private String proxyEnv; - - private String proxyRegion; - - private String proxyIdc; - - private String proxyDcn; - - public int getCode() { - return code; - } - - public void setCode(int code) { - this.code = code; - } - - public String getProxyCluster() { - return proxyCluster; - } - - public void setProxyCluster(String proxyCluster) { - this.proxyCluster = proxyCluster; - } - - public String getProxyIp() { - return proxyIp; - } - - public void setProxyIp(String proxyIp) { - this.proxyIp = proxyIp; - } - - public String getProxyEnv() { - return proxyEnv; - } - - public void setProxyEnv(String proxyEnv) { - this.proxyEnv = proxyEnv; - } - - public String getProxyRegion() { - return proxyRegion; - } - - public void setProxyRegion(String proxyRegion) { - this.proxyRegion = proxyRegion; - } - - public String getProxyIdc() { - return proxyIdc; - } - - public void setProxyIdc(String proxyIdc) { - this.proxyIdc = proxyIdc; - } - - public String getProxyDcn() { - return proxyDcn; - } - - public void setProxyDcn(String proxyDcn) { - this.proxyDcn = proxyDcn; - } - - public static HeartbeatResponseHeader buildHeader(Integer requestCode, String proxyCluster, - String proxyIp, String proxyEnv, String proxyRegion, - String proxyDcn, String proxyIDC) { - HeartbeatResponseHeader heartbeatResponseHeader = new HeartbeatResponseHeader(); - heartbeatResponseHeader.setCode(requestCode); - heartbeatResponseHeader.setProxyCluster(proxyCluster); - heartbeatResponseHeader.setProxyDcn(proxyDcn); - heartbeatResponseHeader.setProxyIp(proxyIp); - heartbeatResponseHeader.setProxyEnv(proxyEnv); - heartbeatResponseHeader.setProxyRegion(proxyRegion); - heartbeatResponseHeader.setProxyIdc(proxyIDC); - return heartbeatResponseHeader; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("heartbeatResponseHeader={") - .append("code=").append(code).append(",") - .append("proxyEnv=").append(proxyEnv).append(",") - .append("proxyRegion=").append(proxyRegion).append(",") - .append("proxyIdc=").append(proxyIdc).append(",") - .append("proxyDcn=").append(proxyDcn).append(",") - .append("proxyCluster=").append(proxyCluster).append(",") - .append("proxyIp=").append(proxyIp).append("}"); - return sb.toString(); - } - - @Override - public Map toMap() { - Map map = new HashMap(); - map.put(ProtocolKey.REQUEST_CODE, code); - map.put(ProtocolKey.ProxyInstanceKey.PROXYCLUSTER, proxyCluster); - map.put(ProtocolKey.ProxyInstanceKey.PROXYIP, proxyIp); - map.put(ProtocolKey.ProxyInstanceKey.PROXYENV, proxyEnv); - map.put(ProtocolKey.ProxyInstanceKey.PROXYREGION, proxyRegion); - map.put(ProtocolKey.ProxyInstanceKey.PROXYIDC, proxyIdc); - map.put(ProtocolKey.ProxyInstanceKey.PROXYDCN, proxyDcn); - return map; - } -} diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/RegResponseHeader.java b/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/RegResponseHeader.java deleted file mode 100644 index 09fdbfe5a1..0000000000 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/RegResponseHeader.java +++ /dev/null @@ -1,147 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.eventmesh.common.protocol.http.header.client; - - -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import com.webank.eventmesh.common.protocol.http.header.Header; - -import java.util.HashMap; -import java.util.Map; - -public class RegResponseHeader extends Header { - - //响应码, 与对应Request的code一致 - private int code; - - //处理该次Request请求的proxy的集群名 - private String proxyCluster; - - //处理该次Request请求的proxy的IP - private String proxyIp; - - //处理该次Request请求的proxy所在的环境编号 - private String proxyEnv; - - //处理该次Request请求的proxy所在区域 - private String proxyRegion; - - //处理该次Request请求的proxy所在IDC - private String proxyIdc; - - //处理该次Request请求的proxy所在DCN - private String proxyDcn; - - public int getCode() { - return code; - } - - public void setCode(int code) { - this.code = code; - } - - public String getProxyCluster() { - return proxyCluster; - } - - public void setProxyCluster(String proxyCluster) { - this.proxyCluster = proxyCluster; - } - - public String getProxyIp() { - return proxyIp; - } - - public void setProxyIp(String proxyIp) { - this.proxyIp = proxyIp; - } - - public String getProxyEnv() { - return proxyEnv; - } - - public void setProxyEnv(String proxyEnv) { - this.proxyEnv = proxyEnv; - } - - public String getProxyRegion() { - return proxyRegion; - } - - public void setProxyRegion(String proxyRegion) { - this.proxyRegion = proxyRegion; - } - - public String getProxyIdc() { - return proxyIdc; - } - - public void setProxyIdc(String proxyIdc) { - this.proxyIdc = proxyIdc; - } - - public String getProxyDcn() { - return proxyDcn; - } - - public void setProxyDcn(String proxyDcn) { - this.proxyDcn = proxyDcn; - } - - public static RegResponseHeader buildHeader(Integer requestCode, String proxyCluster, - String proxyIp, String proxyEnv, String proxyRegion, - String proxyDcn, String proxyIDC) { - RegResponseHeader regResponseHeader = new RegResponseHeader(); - regResponseHeader.setCode(requestCode); - regResponseHeader.setProxyCluster(proxyCluster); - regResponseHeader.setProxyDcn(proxyDcn); - regResponseHeader.setProxyIp(proxyIp); - regResponseHeader.setProxyEnv(proxyEnv); - regResponseHeader.setProxyRegion(proxyRegion); - regResponseHeader.setProxyIdc(proxyIDC); - return regResponseHeader; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("regResponseHeader={") - .append("code=").append(code).append(",") - .append("proxyEnv=").append(proxyEnv).append(",") - .append("proxyRegion=").append(proxyRegion).append(",") - .append("proxyIdc=").append(proxyIdc).append(",") - .append("proxyDcn=").append(proxyDcn).append(",") - .append("proxyCluster=").append(proxyCluster).append(",") - .append("proxyIp=").append(proxyIp).append("}"); - return sb.toString(); - } - - @Override - public Map toMap() { - Map map = new HashMap(); - map.put(ProtocolKey.REQUEST_CODE, code); - map.put(ProtocolKey.ProxyInstanceKey.PROXYCLUSTER, proxyCluster); - map.put(ProtocolKey.ProxyInstanceKey.PROXYIP, proxyIp); - map.put(ProtocolKey.ProxyInstanceKey.PROXYENV, proxyEnv); - map.put(ProtocolKey.ProxyInstanceKey.PROXYREGION, proxyRegion); - map.put(ProtocolKey.ProxyInstanceKey.PROXYIDC, proxyIdc); - map.put(ProtocolKey.ProxyInstanceKey.PROXYDCN, proxyDcn); - return map; - } - -} diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/UnRegResponseHeader.java b/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/UnRegResponseHeader.java deleted file mode 100644 index 0a61cbbb63..0000000000 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/UnRegResponseHeader.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.eventmesh.common.protocol.http.header.client; - - -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import com.webank.eventmesh.common.protocol.http.header.Header; - -import java.util.HashMap; -import java.util.Map; - -public class UnRegResponseHeader extends Header { - - private int code; - - private String proxyCluster; - - private String proxyIp; - - private String proxyEnv; - - private String proxyRegion; - - private String proxyIdc; - - private String proxyDcn; - - public int getCode() { - return code; - } - - public void setCode(int code) { - this.code = code; - } - - public String getProxyCluster() { - return proxyCluster; - } - - public void setProxyCluster(String proxyCluster) { - this.proxyCluster = proxyCluster; - } - - public String getProxyIp() { - return proxyIp; - } - - public void setProxyIp(String proxyIp) { - this.proxyIp = proxyIp; - } - - public String getProxyEnv() { - return proxyEnv; - } - - public void setProxyEnv(String proxyEnv) { - this.proxyEnv = proxyEnv; - } - - public String getProxyRegion() { - return proxyRegion; - } - - public void setProxyRegion(String proxyRegion) { - this.proxyRegion = proxyRegion; - } - - public String getProxyIdc() { - return proxyIdc; - } - - public void setProxyIdc(String proxyIdc) { - this.proxyIdc = proxyIdc; - } - - public String getProxyDcn() { - return proxyDcn; - } - - public void setProxyDcn(String proxyDcn) { - this.proxyDcn = proxyDcn; - } - - public static UnRegResponseHeader buildHeader(Integer requestCode, String proxyCluster, - String proxyIp, String proxyEnv, String proxyRegion, - String proxyDcn, String proxyIDC) { - UnRegResponseHeader regResponseHeader = new UnRegResponseHeader(); - regResponseHeader.setCode(requestCode); - regResponseHeader.setProxyCluster(proxyCluster); - regResponseHeader.setProxyDcn(proxyDcn); - regResponseHeader.setProxyIp(proxyIp); - regResponseHeader.setProxyEnv(proxyEnv); - regResponseHeader.setProxyRegion(proxyRegion); - regResponseHeader.setProxyIdc(proxyIDC); - return regResponseHeader; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("nnRegResponseHeader={") - .append("code=").append(code).append(",") - .append("proxyEnv=").append(proxyEnv).append(",") - .append("proxyRegion=").append(proxyRegion).append(",") - .append("proxyIdc=").append(proxyIdc).append(",") - .append("proxyDcn=").append(proxyDcn).append(",") - .append("proxyCluster=").append(proxyCluster).append(",") - .append("proxyIp=").append(proxyIp).append("}"); - return sb.toString(); - } - - @Override - public Map toMap() { - Map map = new HashMap(); - map.put(ProtocolKey.REQUEST_CODE, code); - map.put(ProtocolKey.ProxyInstanceKey.PROXYCLUSTER, proxyCluster); - map.put(ProtocolKey.ProxyInstanceKey.PROXYIP, proxyIp); - map.put(ProtocolKey.ProxyInstanceKey.PROXYENV, proxyEnv); - map.put(ProtocolKey.ProxyInstanceKey.PROXYIDC, proxyIdc); - map.put(ProtocolKey.ProxyInstanceKey.PROXYREGION, proxyRegion); - map.put(ProtocolKey.ProxyInstanceKey.PROXYDCN, proxyDcn); - return map; - } - -} diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/ReplyMessageResponseHeader.java b/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/ReplyMessageResponseHeader.java deleted file mode 100644 index 6a681079a1..0000000000 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/ReplyMessageResponseHeader.java +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.eventmesh.common.protocol.http.header.message; - - -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import com.webank.eventmesh.common.protocol.http.header.Header; - -import java.util.HashMap; -import java.util.Map; - -public class ReplyMessageResponseHeader extends Header { - - //响应码, 与对应Request的code一致 - private int code; - - //处理该次Request请求的proxy的集群名 - private String proxyCluster; - - //处理该次Request请求的proxy的IP - private String proxyIp; - - //处理该次Request请求的proxy所在的环境编号 - private String proxyEnv; - - //处理该次Request请求的proxy所在区域 - private String proxyRegion; - - //处理该次Request请求的proxy所在IDC - private String proxyIdc; - - //处理该次Request请求的proxy所在DCN - private String proxyDcn; - - public int getCode() { - return code; - } - - public void setCode(int code) { - this.code = code; - } - - public String getProxyCluster() { - return proxyCluster; - } - - public void setProxyCluster(String proxyCluster) { - this.proxyCluster = proxyCluster; - } - - public String getProxyIp() { - return proxyIp; - } - - public void setProxyIp(String proxyIp) { - this.proxyIp = proxyIp; - } - - public String getProxyEnv() { - return proxyEnv; - } - - public void setProxyEnv(String proxyEnv) { - this.proxyEnv = proxyEnv; - } - - public String getProxyRegion() { - return proxyRegion; - } - - public void setProxyRegion(String proxyRegion) { - this.proxyRegion = proxyRegion; - } - - public String getProxyIdc() { - return proxyIdc; - } - - public void setProxyIdc(String proxyIdc) { - this.proxyIdc = proxyIdc; - } - - public String getProxyDcn() { - return proxyDcn; - } - - public void setProxyDcn(String proxyDcn) { - this.proxyDcn = proxyDcn; - } - - public static ReplyMessageResponseHeader buildHeader(Integer requestCode, String proxyCluster, - String proxyIp, String proxyEnv, String proxyRegion, - String proxyDcn, String proxyIDC) { - ReplyMessageResponseHeader replyMessageResponseHeader = new ReplyMessageResponseHeader(); - replyMessageResponseHeader.setCode(requestCode); - replyMessageResponseHeader.setProxyCluster(proxyCluster); - replyMessageResponseHeader.setProxyDcn(proxyDcn); - replyMessageResponseHeader.setProxyIp(proxyIp); - replyMessageResponseHeader.setProxyEnv(proxyEnv); - replyMessageResponseHeader.setProxyRegion(proxyRegion); - replyMessageResponseHeader.setProxyIdc(proxyIDC); - return replyMessageResponseHeader; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("replyMessageResponseHeader={") - .append("code=").append(code).append(",") - .append("proxyEnv=").append(proxyEnv).append(",") - .append("proxyRegion=").append(proxyRegion).append(",") - .append("proxyIdc=").append(proxyIdc).append(",") - .append("proxyDcn=").append(proxyDcn).append(",") - .append("proxyCluster=").append(proxyCluster).append(",") - .append("proxyIp=").append(proxyIp).append("}"); - return sb.toString(); - } - - @Override - public Map toMap() { - Map map = new HashMap(); - map.put(ProtocolKey.REQUEST_CODE, code); - map.put(ProtocolKey.ProxyInstanceKey.PROXYCLUSTER, proxyCluster); - map.put(ProtocolKey.ProxyInstanceKey.PROXYIP, proxyIp); - map.put(ProtocolKey.ProxyInstanceKey.PROXYENV, proxyEnv); - map.put(ProtocolKey.ProxyInstanceKey.PROXYIDC, proxyIdc); - map.put(ProtocolKey.ProxyInstanceKey.PROXYREGION, proxyRegion); - map.put(ProtocolKey.ProxyInstanceKey.PROXYDCN, proxyDcn); - return map; - } -} diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/SendMessageBatchResponseHeader.java b/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/SendMessageBatchResponseHeader.java deleted file mode 100644 index 8fc000da7a..0000000000 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/SendMessageBatchResponseHeader.java +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.eventmesh.common.protocol.http.header.message; - - -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import com.webank.eventmesh.common.protocol.http.header.Header; - -import java.util.HashMap; -import java.util.Map; - -public class SendMessageBatchResponseHeader extends Header { - - //响应码, 与对应Request的code一致 - private int code; - - //处理该次Request请求的proxy的集群名 - private String proxyCluster; - - //处理该次Request请求的proxy的IP - private String proxyIp; - - //处理该次Request请求的proxy所在的环境编号 - private String proxyEnv; - - //处理该次Request请求的proxy所在区域 - private String proxyRegion; - - //处理该次Request请求的proxy所在IDC - private String proxyIdc; - - //处理该次Request请求的proxy所在DCN - private String proxyDcn; - - public int getCode() { - return code; - } - - public void setCode(int code) { - this.code = code; - } - - public String getProxyCluster() { - return proxyCluster; - } - - public void setProxyCluster(String proxyCluster) { - this.proxyCluster = proxyCluster; - } - - public String getProxyIp() { - return proxyIp; - } - - public void setProxyIp(String proxyIp) { - this.proxyIp = proxyIp; - } - - public String getProxyEnv() { - return proxyEnv; - } - - public void setProxyEnv(String proxyEnv) { - this.proxyEnv = proxyEnv; - } - - public String getProxyRegion() { - return proxyRegion; - } - - public void setProxyRegion(String proxyRegion) { - this.proxyRegion = proxyRegion; - } - - public String getProxyIdc() { - return proxyIdc; - } - - public void setProxyIdc(String proxyIdc) { - this.proxyIdc = proxyIdc; - } - - public String getProxyDcn() { - return proxyDcn; - } - - public void setProxyDcn(String proxyDcn) { - this.proxyDcn = proxyDcn; - } - - public static SendMessageBatchResponseHeader buildHeader(Integer requestCode, String proxyCluster, - String proxyIp, String proxyEnv, String proxyRegion, - String proxyDcn, String proxyIDC) { - SendMessageBatchResponseHeader sendMessageBatchResponseHeader = new SendMessageBatchResponseHeader(); - sendMessageBatchResponseHeader.setCode(requestCode); - sendMessageBatchResponseHeader.setProxyCluster(proxyCluster); - sendMessageBatchResponseHeader.setProxyDcn(proxyDcn); - sendMessageBatchResponseHeader.setProxyEnv(proxyEnv); - sendMessageBatchResponseHeader.setProxyRegion(proxyRegion); - sendMessageBatchResponseHeader.setProxyIdc(proxyIDC); - sendMessageBatchResponseHeader.setProxyIp(proxyIp); - return sendMessageBatchResponseHeader; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("sendMessageBatchResponseHeader={") - .append("code=").append(code).append(",") - .append("proxyEnv=").append(proxyEnv).append(",") - .append("proxyRegion=").append(proxyRegion).append(",") - .append("proxyIdc=").append(proxyIdc).append(",") - .append("proxyDcn=").append(proxyDcn).append(",") - .append("proxyCluster=").append(proxyCluster).append(",") - .append("proxyIp=").append(proxyIp).append("}"); - return sb.toString(); - } - - @Override - public Map toMap() { - Map map = new HashMap(); - map.put(ProtocolKey.REQUEST_CODE, code); - map.put(ProtocolKey.ProxyInstanceKey.PROXYCLUSTER, proxyCluster); - map.put(ProtocolKey.ProxyInstanceKey.PROXYIP, proxyIp); - map.put(ProtocolKey.ProxyInstanceKey.PROXYENV, proxyEnv); - map.put(ProtocolKey.ProxyInstanceKey.PROXYREGION, proxyRegion); - map.put(ProtocolKey.ProxyInstanceKey.PROXYIDC, proxyIdc); - map.put(ProtocolKey.ProxyInstanceKey.PROXYDCN, proxyDcn); - return map; - } -} diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/SendMessageBatchV2ResponseHeader.java b/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/SendMessageBatchV2ResponseHeader.java deleted file mode 100644 index 93a033476c..0000000000 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/SendMessageBatchV2ResponseHeader.java +++ /dev/null @@ -1,145 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.eventmesh.common.protocol.http.header.message; - -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import com.webank.eventmesh.common.protocol.http.header.Header; - -import java.util.HashMap; -import java.util.Map; - -public class SendMessageBatchV2ResponseHeader extends Header { - - //响应码, 与对应Request的code一致 - private int code; - - //处理该次Request请求的proxy的集群名 - private String proxyCluster; - - //处理该次Request请求的proxy的IP - private String proxyIp; - - //处理该次Request请求的proxy所在的环境编号 - private String proxyEnv; - - //处理该次Request请求的proxy所在区域 - private String proxyRegion; - - //处理该次Request请求的proxy所在IDC - private String proxyIdc; - - //处理该次Request请求的proxy所在DCN - private String proxyDcn; - - public int getCode() { - return code; - } - - public void setCode(int code) { - this.code = code; - } - - public String getProxyCluster() { - return proxyCluster; - } - - public void setProxyCluster(String proxyCluster) { - this.proxyCluster = proxyCluster; - } - - public String getProxyIp() { - return proxyIp; - } - - public void setProxyIp(String proxyIp) { - this.proxyIp = proxyIp; - } - - public String getProxyEnv() { - return proxyEnv; - } - - public void setProxyEnv(String proxyEnv) { - this.proxyEnv = proxyEnv; - } - - public String getProxyRegion() { - return proxyRegion; - } - - public void setProxyRegion(String proxyRegion) { - this.proxyRegion = proxyRegion; - } - - public String getProxyIdc() { - return proxyIdc; - } - - public void setProxyIdc(String proxyIdc) { - this.proxyIdc = proxyIdc; - } - - public String getProxyDcn() { - return proxyDcn; - } - - public void setProxyDcn(String proxyDcn) { - this.proxyDcn = proxyDcn; - } - - public static SendMessageBatchV2ResponseHeader buildHeader(Integer requestCode, String proxyCluster, - String proxyIp, String proxyEnv, String proxyRegion, - String proxyDcn, String proxyIDC) { - SendMessageBatchV2ResponseHeader header = new SendMessageBatchV2ResponseHeader(); - header.setCode(requestCode); - header.setProxyCluster(proxyCluster); - header.setProxyDcn(proxyDcn); - header.setProxyEnv(proxyEnv); - header.setProxyRegion(proxyRegion); - header.setProxyIdc(proxyIDC); - header.setProxyIp(proxyIp); - return header; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("sendMessageBatchV2ResponseHeader={") - .append("code=").append(code).append(",") - .append("proxyEnv=").append(proxyEnv).append(",") - .append("proxyRegion=").append(proxyRegion).append(",") - .append("proxyIdc=").append(proxyIdc).append(",") - .append("proxyDcn=").append(proxyDcn).append(",") - .append("proxyCluster=").append(proxyCluster).append(",") - .append("proxyIp=").append(proxyIp).append("}"); - return sb.toString(); - } - - @Override - public Map toMap() { - Map map = new HashMap(); - map.put(ProtocolKey.REQUEST_CODE, code); - map.put(ProtocolKey.ProxyInstanceKey.PROXYCLUSTER, proxyCluster); - map.put(ProtocolKey.ProxyInstanceKey.PROXYIP, proxyIp); - map.put(ProtocolKey.ProxyInstanceKey.PROXYENV, proxyEnv); - map.put(ProtocolKey.ProxyInstanceKey.PROXYREGION, proxyRegion); - map.put(ProtocolKey.ProxyInstanceKey.PROXYIDC, proxyIdc); - map.put(ProtocolKey.ProxyInstanceKey.PROXYDCN, proxyDcn); - return map; - } -} diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/SendMessageResponseHeader.java b/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/SendMessageResponseHeader.java deleted file mode 100644 index 9272371a67..0000000000 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/SendMessageResponseHeader.java +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.eventmesh.common.protocol.http.header.message; - - -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import com.webank.eventmesh.common.protocol.http.header.Header; - -import java.util.HashMap; -import java.util.Map; - -public class SendMessageResponseHeader extends Header { - - //响应码, 与对应Request的code一致 - private int code; - - //处理该次Request请求的proxy的集群名 - private String proxyCluster; - - //处理该次Request请求的proxy的IP - private String proxyIp; - - //处理该次Request请求的proxy所在的环境编号 - private String proxyEnv; - - //处理该次Request请求的proxy所在区域 - private String proxyRegion; - - //处理该次Request请求的proxy所在IDC - private String proxyIdc; - - //处理该次Request请求的proxy所在DCN - private String proxyDcn; - - public int getCode() { - return code; - } - - public void setCode(int code) { - this.code = code; - } - - public String getProxyCluster() { - return proxyCluster; - } - - public void setProxyCluster(String proxyCluster) { - this.proxyCluster = proxyCluster; - } - - public String getProxyIp() { - return proxyIp; - } - - public void setProxyIp(String proxyIp) { - this.proxyIp = proxyIp; - } - - public String getProxyEnv() { - return proxyEnv; - } - - public void setProxyEnv(String proxyEnv) { - this.proxyEnv = proxyEnv; - } - - public String getProxyRegion() { - return proxyRegion; - } - - public void setProxyRegion(String proxyRegion) { - this.proxyRegion = proxyRegion; - } - - public String getProxyIdc() { - return proxyIdc; - } - - public void setProxyIdc(String proxyIdc) { - this.proxyIdc = proxyIdc; - } - - public String getProxyDcn() { - return proxyDcn; - } - - public void setProxyDcn(String proxyDcn) { - this.proxyDcn = proxyDcn; - } - - public static SendMessageResponseHeader buildHeader(Integer requestCode, String proxyCluster, - String proxyIp, String proxyEnv, String proxyRegion, - String proxyDcn, String proxyIDC) { - SendMessageResponseHeader sendMessageResponseHeader = new SendMessageResponseHeader(); - sendMessageResponseHeader.setCode(requestCode); - sendMessageResponseHeader.setProxyCluster(proxyCluster); - sendMessageResponseHeader.setProxyDcn(proxyDcn); - sendMessageResponseHeader.setProxyIp(proxyIp); - sendMessageResponseHeader.setProxyEnv(proxyEnv); - sendMessageResponseHeader.setProxyRegion(proxyRegion); - sendMessageResponseHeader.setProxyIdc(proxyIDC); - return sendMessageResponseHeader; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("sendMessageResponseHeader={") - .append("code=").append(code).append(",") - .append("proxyEnv=").append(proxyEnv).append(",") - .append("proxyRegion=").append(proxyRegion).append(",") - .append("proxyIdc=").append(proxyIdc).append(",") - .append("proxyDcn=").append(proxyDcn).append(",") - .append("proxyCluster=").append(proxyCluster).append(",") - .append("proxyIp=").append(proxyIp).append("}"); - return sb.toString(); - } - - @Override - public Map toMap() { - Map map = new HashMap(); - map.put(ProtocolKey.REQUEST_CODE, code); - map.put(ProtocolKey.ProxyInstanceKey.PROXYCLUSTER, proxyCluster); - map.put(ProtocolKey.ProxyInstanceKey.PROXYIP, proxyIp); - map.put(ProtocolKey.ProxyInstanceKey.PROXYENV, proxyEnv); - map.put(ProtocolKey.ProxyInstanceKey.PROXYREGION, proxyRegion); - map.put(ProtocolKey.ProxyInstanceKey.PROXYIDC, proxyIdc); - map.put(ProtocolKey.ProxyInstanceKey.PROXYDCN, proxyDcn); - return map; - } -} diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/ProxyClientInfo.java b/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/ProxyClientInfo.java deleted file mode 100644 index 227b394390..0000000000 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/ProxyClientInfo.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.eventmesh.common.protocol.tcp; - -import org.apache.rocketmq.common.DataVersion; - -public class ProxyClientInfo { - private String clientId; - private String consumerGroup; - private String endpoint; - private String language; - private long version; - private DataVersion dataVersion; - private long lastUpdateTimestamp; - private int protocolNumber; - - public ProxyClientInfo(String clientId, String consumerGroup, String endpoint, String language, long version, DataVersion dataVersion, long lastUpdateTimestamp, int protocolNumber) { - this.clientId = clientId; - this.endpoint = endpoint; - this.language = language; - this.version = version; - this.consumerGroup = consumerGroup; - this.dataVersion = dataVersion; - this.lastUpdateTimestamp = lastUpdateTimestamp; - this.protocolNumber = protocolNumber; - } - - public void setClientId(String clientId) { - this.clientId = clientId; - } - - public String getClientId() { - return clientId; - } - - public void setDataVersion(DataVersion dataVersion) { - this.dataVersion = dataVersion; - } - - public void setEndpoint(String endpoint) { - this.endpoint = endpoint; - } - - public DataVersion getDataVersion() { - return dataVersion; - } - - public String getEndpoint() { - return endpoint; - } - - public void setLastUpdateTimestamp(long lastUpdateTimestamp) { - this.lastUpdateTimestamp = lastUpdateTimestamp; - } - - public long getLastUpdateTimestamp() { - return lastUpdateTimestamp; - } - - public void setConsumerGroup(String consumerGroup) { - this.consumerGroup = consumerGroup; - } - - public String getConsumerGroup() { - return consumerGroup; - } - - public void setVersion(long version) { - this.version = version; - } - - public void setLanguage(String language) { - this.language = language; - } - - public String getLanguage() { - return language; - } - - public long getVersion() { - return version; - } - - public void setProtocolNumber(int protocolNumber) { - this.protocolNumber = protocolNumber; - } - - public int getProtocolNumber() { - return protocolNumber; - } - - @Override - public String toString() { - return "ClientId [clientId=" + clientId + ", consumerGroup=" + consumerGroup + ", endpoint=" + endpoint + ", language=" + language + ", version=" + version + ", dataVersion=" + dataVersion + ", lastUpdateTimestamp=" + lastUpdateTimestamp + "]"; - } -} diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/Constants.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/Constants.java similarity index 69% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/Constants.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/Constants.java index ebfcd77785..c2457addc6 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/Constants.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/Constants.java @@ -15,16 +15,12 @@ * limitations under the License. */ -package com.webank.eventmesh.common; +package org.apache.eventmesh.common; public class Constants { public static final String DEFAULT_CHARSET = "UTF-8"; - public static final String TARGET_PROXY_REGION = "TARGET_PROXY_REGION"; - - public static final String CONSTANTS_DEFAULT_REGION_KEY = "default"; - public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; public static final String LANGUAGE_JAVA = "JAVA"; @@ -35,7 +31,7 @@ public class Constants { public static final int DEFAULT_HTTP_TIME_OUT = 3000; - public static final String PROXY_MESSAGE_CONST_TTL = "ttl"; + public static final String EVENTMESH_MESSAGE_CONST_TTL = "ttl"; public static final Integer DEFAULT_CLIENT_UNACK = 12; @@ -47,8 +43,6 @@ public class Constants { public static final String CONSTANTS_INSTANCE_DESC_IDC = "idc"; - public static final String CONSTANTS_INSTANCE_DESC_DCN = "dcn"; - public static final String CONSTANTS_INSTANCE_DESC_SYSID = "sysId"; public static final String CONSTANTS_INSTANCE_DESC_IP = "ip"; @@ -61,4 +55,24 @@ public class Constants { public static final String IDC_SEPERATER = "-"; + public static final String PROPERTY_MESSAGE_TIMEOUT = "TIMEOUT"; + + public static final String PROPERTY_MESSAGE_SEARCH_KEYS = "SEARCH_KEYS"; + + public static final String PROPERTY_MESSAGE_QUEUE_ID = "QUEUE_ID"; + + public static final String PROPERTY_MESSAGE_QUEUE_OFFSET = "QUEUE_OFFSET"; + + public static final String PROPERTY_MESSAGE_DESTINATION = "DESTINATION"; + + public static final String PROPERTY_MESSAGE_MESSAGE_ID = "MESSAGE_ID"; + + public static final String PROPERTY_MESSAGE_BORN_HOST = "BORN_HOST"; + + public static final String PROPERTY_MESSAGE_BORN_TIMESTAMP = "BORN_TIMESTAMP"; + + public static final String PROPERTY_MESSAGE_STORE_HOST = "STORE_HOST"; + + public static final String PROPERTY_MESSAGE_STORE_TIMESTAMP = "STORE_TIMESTAMP"; + } diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/ProxyException.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/EventMeshException.java similarity index 70% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/ProxyException.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/EventMeshException.java index 7c5bda8b25..bffa57e780 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/ProxyException.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/EventMeshException.java @@ -15,30 +15,30 @@ * limitations under the License. */ -package com.webank.eventmesh.common; +package org.apache.eventmesh.common; -public class ProxyException extends Exception { +public class EventMeshException extends Exception { - public ProxyException() { + public EventMeshException() { } - public ProxyException(String message) { + public EventMeshException(String message) { super(message); } - public ProxyException(String message, Throwable cause) { + public EventMeshException(String message, Throwable cause) { super(message, cause); } - public ProxyException(Throwable cause) { + public EventMeshException(Throwable cause) { super(cause); } - public ProxyException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + public EventMeshException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } - public ProxyException(Integer errCode, String errMsg) { + public EventMeshException(Integer errCode, String errMsg) { super((new StringBuilder()).append(errCode) .append("|") .append(errMsg).toString()); diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/IPUtil.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/IPUtil.java similarity index 97% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/IPUtil.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/IPUtil.java index e0b7826b64..5165deb8af 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/IPUtil.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/IPUtil.java @@ -15,9 +15,7 @@ * limitations under the License. */ -package com.webank.eventmesh.common; - -import io.netty.channel.Channel; +package org.apache.eventmesh.common; import java.net.Inet6Address; import java.net.InetAddress; @@ -30,12 +28,14 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import io.netty.channel.Channel; + public class IPUtil { public static String getLocalAddress() { // if the progress works under docker environment // return the host ip about this docker located from environment value - String dockerHostIp = System.getenv("webank_docker_host_ip"); + String dockerHostIp = System.getenv("docker_host_ip"); if (dockerHostIp != null && !"".equals(dockerHostIp)) return dockerHostIp; @@ -107,8 +107,7 @@ else if (preferList.indexOf(networkInterface.getName()) return null; } - public static boolean isValidIPV4Address(String ip) - { + public static boolean isValidIPV4Address(String ip) { // Regex for digit from 0 to 255. String zeroTo255 diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/LiteMessage.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/LiteMessage.java similarity index 98% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/LiteMessage.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/LiteMessage.java index e0d9666bf7..455dfc65d0 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/LiteMessage.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/LiteMessage.java @@ -15,13 +15,13 @@ * limitations under the License. */ -package com.webank.eventmesh.common; - -import org.apache.commons.lang3.time.DateFormatUtils; +package org.apache.eventmesh.common; import java.util.HashMap; import java.util.Map; +import org.apache.commons.lang3.time.DateFormatUtils; + public class LiteMessage { private String bizSeqNo; diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/ThreadPoolFactory.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/ThreadPoolFactory.java similarity index 91% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/ThreadPoolFactory.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/ThreadPoolFactory.java index 3edfec776a..5be65297b3 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/ThreadPoolFactory.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/ThreadPoolFactory.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.webank.eventmesh.common; +package org.apache.eventmesh.common; import java.util.concurrent.BlockingQueue; import java.util.concurrent.Executors; @@ -36,7 +36,7 @@ public static ThreadPoolExecutor createThreadPoolExecutor(int core, int max, fin return createThreadPoolExecutor(core, max, new LinkedBlockingQueue(1000), threadName, isDaemon); } - public static ThreadPoolExecutor createThreadPoolExecutor(int core, int max, BlockingQueue blockingQueue, final String threadName, final boolean isDaemon) { + public static ThreadPoolExecutor createThreadPoolExecutor(int core, int max, BlockingQueue blockingQueue, final String threadName, final boolean isDaemon) { return new ThreadPoolExecutor(core, max, 10 * 1000, TimeUnit.MILLISECONDS, blockingQueue, new ThreadFactory() { @@ -56,8 +56,8 @@ public static ThreadPoolExecutor createThreadPoolExecutor(int core, int max, Thr return createThreadPoolExecutor(core, max, new LinkedBlockingQueue(1000), threadFactory); } - public static ThreadPoolExecutor createThreadPoolExecutor(int core, int max, BlockingQueue blockingQueue, ThreadFactory threadFactory) { - return new ThreadPoolExecutor(core, max,10 * 1000, TimeUnit.MILLISECONDS, blockingQueue, threadFactory); + public static ThreadPoolExecutor createThreadPoolExecutor(int core, int max, BlockingQueue blockingQueue, ThreadFactory threadFactory) { + return new ThreadPoolExecutor(core, max, 10 * 1000, TimeUnit.MILLISECONDS, blockingQueue, threadFactory); } public static ScheduledExecutorService createSingleScheduledExecutor(final String threadName) { diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/ThreadUtil.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/ThreadUtil.java similarity index 91% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/ThreadUtil.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/ThreadUtil.java index 3a25b806b3..aecfb0e3d0 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/ThreadUtil.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/ThreadUtil.java @@ -15,13 +15,13 @@ * limitations under the License. */ -package com.webank.eventmesh.common; +package org.apache.eventmesh.common; import java.util.concurrent.ThreadLocalRandom; public class ThreadUtil { - private static long currentPID=-1; + private static long currentPID = -1; public static void randomSleep(int min, int max) throws Exception { // nextInt is normally exclusive of the top value, so add 1 to make it inclusive @@ -36,6 +36,7 @@ public static void randomSleep(int max) throws Exception { /** * get current process id only once. + * * @return */ public static long getPID() { @@ -45,7 +46,7 @@ public static long getPID() { String processName = java.lang.management.ManagementFactory.getRuntimeMXBean().getName(); if (processName != null && processName.length() > 0) { try { - currentPID= Long.parseLong(processName.split("@")[0]); + currentPID = Long.parseLong(processName.split("@")[0]); } catch (Exception e) { return 0; } diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/command/HttpCommand.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/command/HttpCommand.java similarity index 90% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/command/HttpCommand.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/command/HttpCommand.java index 4ac40a0b99..d36a1ae613 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/command/HttpCommand.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/command/HttpCommand.java @@ -15,23 +15,18 @@ * limitations under the License. */ -package com.webank.eventmesh.common.command; +package org.apache.eventmesh.common.command; -import com.webank.eventmesh.common.Constants; -import com.webank.eventmesh.common.protocol.http.body.BaseResponseBody; -import com.webank.eventmesh.common.protocol.http.body.Body; -import com.webank.eventmesh.common.protocol.http.header.BaseResponseHeader; -import com.webank.eventmesh.common.protocol.http.header.Header; import com.alibaba.fastjson.JSON; import io.netty.buffer.Unpooled; -import io.netty.handler.codec.http.DefaultFullHttpResponse; -import io.netty.handler.codec.http.HttpHeaderNames; -import io.netty.handler.codec.http.HttpHeaderValues; -import io.netty.handler.codec.http.HttpHeaders; -import io.netty.handler.codec.http.HttpResponseStatus; -import io.netty.handler.codec.http.HttpVersion; +import io.netty.handler.codec.http.*; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.http.body.BaseResponseBody; +import org.apache.eventmesh.common.protocol.http.body.Body; +import org.apache.eventmesh.common.protocol.http.header.BaseResponseHeader; +import org.apache.eventmesh.common.protocol.http.header.Header; import java.util.Map; import java.util.concurrent.atomic.AtomicLong; @@ -75,7 +70,7 @@ public HttpCommand(String httpMethod, String httpVersion, String requestCode) { public HttpCommand createHttpCommandResponse(Header header, Body body) { - if(StringUtils.isBlank(requestCode)) { + if (StringUtils.isBlank(requestCode)) { return null; } HttpCommand response = new HttpCommand(this.httpMethod, this.httpVersion, this.requestCode); @@ -89,7 +84,7 @@ public HttpCommand createHttpCommandResponse(Header header, } public HttpCommand createHttpCommandResponse(Integer retCode, String retMsg) { - if(StringUtils.isBlank(requestCode)) { + if (StringUtils.isBlank(requestCode)) { return null; } HttpCommand response = new HttpCommand(this.httpMethod, this.httpVersion, this.requestCode); diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/CommonConfiguration.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/CommonConfiguration.java new file mode 100644 index 0000000000..08a44cb8e1 --- /dev/null +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/CommonConfiguration.java @@ -0,0 +1,198 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.common.config; + +import java.net.Inet6Address; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.Enumeration; + +import com.google.common.base.Preconditions; + +import org.apache.commons.lang3.StringUtils; + +public class CommonConfiguration { + public String eventMeshEnv = "P"; + public String eventMeshIDC = "FT"; + public String eventMeshCluster = "LS"; + public String eventMeshName = ""; + public String sysID = "5477"; + + + public String namesrvAddr = ""; + public String clientUserName = "username"; + public String clientPass = "user@123"; + public Integer consumeThreadMin = 2; + public Integer consumeThreadMax = 2; + public Integer consumeQueueSize = 10000; + public Integer pullBatchSize = 32; + public Integer ackWindow = 1000; + public Integer pubWindow = 100; + public long consumeTimeout = 0L; + public Integer pollNameServerInteval = 10 * 1000; + public Integer heartbeatBrokerInterval = 30 * 1000; + public Integer rebalanceInterval = 20 * 1000; + public Integer eventMeshRegisterIntervalInMills = 10 * 1000; + public Integer eventMeshFetchRegistryAddrInterval = 10 * 1000; + public String eventMeshServerIp = null; + protected ConfigurationWraper configurationWraper; + + public CommonConfiguration(ConfigurationWraper configurationWraper) { + this.configurationWraper = configurationWraper; + } + + public void init() { + + if (configurationWraper != null) { + String eventMeshEnvStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_ENV); + Preconditions.checkState(StringUtils.isNotEmpty(eventMeshEnvStr), String.format("%s error", ConfKeys.KEYS_EVENTMESH_ENV)); + eventMeshEnv = StringUtils.deleteWhitespace(eventMeshEnvStr); + + String sysIdStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_SYSID); + Preconditions.checkState(StringUtils.isNotEmpty(sysIdStr) && StringUtils.isNumeric(sysIdStr), String.format("%s error", ConfKeys.KEYS_EVENTMESH_SYSID)); + sysID = StringUtils.deleteWhitespace(sysIdStr); + + String eventMeshClusterStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_SERVER_CLUSTER); + Preconditions.checkState(StringUtils.isNotEmpty(eventMeshClusterStr), String.format("%s error", ConfKeys.KEYS_EVENTMESH_SERVER_CLUSTER)); + eventMeshCluster = StringUtils.deleteWhitespace(eventMeshClusterStr); + + String eventMeshNameStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_SERVER_NAME); + Preconditions.checkState(StringUtils.isNotEmpty(eventMeshNameStr), String.format("%s error", ConfKeys.KEYS_EVENTMESH_SERVER_NAME)); + eventMeshName = StringUtils.deleteWhitespace(eventMeshNameStr); + + String eventMeshIDCStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_IDC); + Preconditions.checkState(StringUtils.isNotEmpty(eventMeshIDCStr), String.format("%s error", ConfKeys.KEYS_EVENTMESH_IDC)); + eventMeshIDC = StringUtils.deleteWhitespace(eventMeshIDCStr); + + eventMeshServerIp = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_SERVER_HOST_IP); + if (StringUtils.isBlank(eventMeshServerIp)) { + eventMeshServerIp = getLocalAddr(); + } + } + } + + static class ConfKeys { + public static String KEYS_EVENTMESH_ENV = "eventMesh.server.env"; + + public static String KEYS_EVENTMESH_IDC = "eventMesh.server.idc"; + + public static String KEYS_EVENTMESH_SYSID = "eventMesh.sysid"; + + public static String KEYS_EVENTMESH_SERVER_CLUSTER = "eventMesh.server.cluster"; + + public static String KEYS_EVENTMESH_SERVER_NAME = "eventMesh.server.name"; + + public static String KEYS_EVENTMESH_SERVER_HOST_IP = "eventMesh.server.hostIp"; + + public static String KEYS_EVENTMESH_SERVER_REGISTER_INTERVAL = "eventMesh.server.registry.registerIntervalInMills"; + + public static String KEYS_EVENTMESH_SERVER_FETCH_REGISTRY_ADDR_INTERVAL = "eventMesh.server.registry.fetchRegistryAddrIntervalInMills"; + } + + public static String getLocalAddr() { + //priority of networkInterface when generating client ip + String priority = System.getProperty("networkInterface.priority", "bond1 preferList = new ArrayList(); + for (String eth : priority.split("<")) { + preferList.add(eth); + } + NetworkInterface preferNetworkInterface = null; + + try { + Enumeration enumeration1 = NetworkInterface.getNetworkInterfaces(); + while (enumeration1.hasMoreElements()) { + final NetworkInterface networkInterface = enumeration1.nextElement(); + if (!preferList.contains(networkInterface.getName())) { + continue; + } else if (preferNetworkInterface == null) { + preferNetworkInterface = networkInterface; + } + //get the networkInterface that has higher priority + else if (preferList.indexOf(networkInterface.getName()) + > preferList.indexOf(preferNetworkInterface.getName())) { + preferNetworkInterface = networkInterface; + } + } + + // Traversal Network interface to get the first non-loopback and non-private address + ArrayList ipv4Result = new ArrayList(); + ArrayList ipv6Result = new ArrayList(); + + if (preferNetworkInterface != null) { + final Enumeration en = preferNetworkInterface.getInetAddresses(); + getIpResult(ipv4Result, ipv6Result, en); + } else { + Enumeration enumeration = NetworkInterface.getNetworkInterfaces(); + while (enumeration.hasMoreElements()) { + final NetworkInterface networkInterface = enumeration.nextElement(); + final Enumeration en = networkInterface.getInetAddresses(); + getIpResult(ipv4Result, ipv6Result, en); + } + } + + // prefer ipv4 + if (!ipv4Result.isEmpty()) { + for (String ip : ipv4Result) { + if (ip.startsWith("127.0") || ip.startsWith("192.168")) { + continue; + } + + return ip; + } + + return ipv4Result.get(ipv4Result.size() - 1); + } else if (!ipv6Result.isEmpty()) { + return ipv6Result.get(0); + } + //If failed to find,fall back to localhost + final InetAddress localHost = InetAddress.getLocalHost(); + return normalizeHostAddress(localHost); + } catch (SocketException e) { + e.printStackTrace(); + } catch (UnknownHostException e) { + e.printStackTrace(); + } + + return null; + } + + public static String normalizeHostAddress(final InetAddress localHost) { + if (localHost instanceof Inet6Address) { + return "[" + localHost.getHostAddress() + "]"; + } else { + return localHost.getHostAddress(); + } + } + + private static void getIpResult(ArrayList ipv4Result, ArrayList ipv6Result, + Enumeration en) { + while (en.hasMoreElements()) { + final InetAddress address = en.nextElement(); + if (!address.isLoopbackAddress()) { + if (address instanceof Inet6Address) { + ipv6Result.add(normalizeHostAddress(address)); + } else { + ipv4Result.add(normalizeHostAddress(address)); + } + } + } + } +} \ No newline at end of file diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/configuration/ConfigurationWraper.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigurationWraper.java similarity index 93% rename from eventmesh-emesher/src/main/java/com/webank/emesher/configuration/ConfigurationWraper.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigurationWraper.java index 89fa278f55..2da975b394 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/configuration/ConfigurationWraper.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigurationWraper.java @@ -15,12 +15,7 @@ * limitations under the License. */ -package com.webank.emesher.configuration; - -import com.webank.eventmesh.common.ThreadPoolFactory; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +package org.apache.eventmesh.common.config; import java.io.BufferedReader; import java.io.File; @@ -30,6 +25,11 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.common.ThreadPoolFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class ConfigurationWraper { public Logger logger = LoggerFactory.getLogger(this.getClass()); @@ -40,7 +40,7 @@ public class ConfigurationWraper { private boolean reload = true; - private ScheduledExecutorService configLoader = ThreadPoolFactory.createSingleScheduledExecutor("proxy-configloader-"); + private ScheduledExecutorService configLoader = ThreadPoolFactory.createSingleScheduledExecutor("eventMesh-configloader-"); public ConfigurationWraper(String file, boolean reload) { this.file = file; @@ -73,6 +73,4 @@ private void load() { public String getProp(String key) { return StringUtils.isEmpty(key) ? null : properties.getProperty(key, null); } - - } \ No newline at end of file diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/loadbalance/LoadBalanceSelector.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/loadbalance/LoadBalanceSelector.java new file mode 100644 index 0000000000..3bee08c6ec --- /dev/null +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/loadbalance/LoadBalanceSelector.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.common.loadbalance; + +/** + * LoadBalance Interface + * + *

see {@link RandomLoadBalanceSelector} + *

see {@link WeightRoundRobinLoadBalanceSelector} + * + * @param Target type + */ +public interface LoadBalanceSelector { + + /** + * Select one + * + * @return target + */ + T select(); + + /** + * load balance type see {@link LoadBalanceType} + * + * @return load balance type of the selector + */ + LoadBalanceType getType(); + +} diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/loadbalance/LoadBalanceType.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/loadbalance/LoadBalanceType.java new file mode 100644 index 0000000000..858a69b1c6 --- /dev/null +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/loadbalance/LoadBalanceType.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.eventmesh.common.loadbalance; + +public enum LoadBalanceType { + RANDOM(0, "random load balance strategy"), + WEIGHT_ROUND_ROBIN(1, "weight round robin load balance strategy"); + + private int code; + private String desc; + + LoadBalanceType(int code, String desc) { + this.code = code; + this.desc = desc; + } + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getDesc() { + return desc; + } + + public void setDesc(String desc) { + this.desc = desc; + } + +} diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/loadbalance/RandomLoadBalanceSelector.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/loadbalance/RandomLoadBalanceSelector.java new file mode 100644 index 0000000000..fc741a5a82 --- /dev/null +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/loadbalance/RandomLoadBalanceSelector.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.common.loadbalance; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.RandomUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +/** + * This selector use random strategy. + * Each selection will randomly give one from the given list + * + * @param + */ +public class RandomLoadBalanceSelector implements LoadBalanceSelector { + + private final Logger logger = LoggerFactory.getLogger(RandomLoadBalanceSelector.class); + + private final List clusterGroup; + + public RandomLoadBalanceSelector(List clusterGroup) { + this.clusterGroup = clusterGroup; + } + + @Override + public T select() { + if (CollectionUtils.isEmpty(clusterGroup)) { + logger.warn("No servers available"); + return null; + } + return clusterGroup.get(RandomUtils.nextInt(0, clusterGroup.size())); + } + + @Override + public LoadBalanceType getType() { + return LoadBalanceType.RANDOM; + } +} diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/loadbalance/Weight.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/loadbalance/Weight.java new file mode 100644 index 0000000000..e793955c5c --- /dev/null +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/loadbalance/Weight.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.common.loadbalance; + +import java.util.concurrent.atomic.AtomicInteger; + +public class Weight { + + private T target; + + private final int weight; + + private final AtomicInteger currentWeight; + + public Weight(T target, int weight) { + this.target = target; + this.weight = weight; + this.currentWeight = new AtomicInteger(0); + } + + public void decreaseTotal(int total) { + currentWeight.addAndGet(-1 * total); + } + + public void increaseCurrentWeight() { + currentWeight.addAndGet(weight); + } + + + public T getTarget() { + return target; + } + + public void setTarget(T target) { + this.target = target; + } + + public int getWeight() { + return weight; + } + + + public AtomicInteger getCurrentWeight() { + return currentWeight; + } + + @Override + public String toString() { + return "Wight{" + + "target=" + target + + ", weight=" + weight + + ", currentWeight=" + currentWeight + + '}'; + } +} diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/loadbalance/WeightRoundRobinLoadBalanceSelector.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/loadbalance/WeightRoundRobinLoadBalanceSelector.java new file mode 100644 index 0000000000..d6f2009bf3 --- /dev/null +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/loadbalance/WeightRoundRobinLoadBalanceSelector.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.common.loadbalance; + +import org.apache.commons.collections4.CollectionUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +/** + * This selector use the weighted round robin strategy to select from list. + * If the weight is greater, the probability of being selected is larger. + * + * @param + */ +public class WeightRoundRobinLoadBalanceSelector implements LoadBalanceSelector { + + private final Logger logger = LoggerFactory.getLogger(WeightRoundRobinLoadBalanceSelector.class); + + private final List> clusterGroup; + + private final int totalWeight; + + public WeightRoundRobinLoadBalanceSelector(List> clusterGroup) { + int totalWeight = 0; + for (Weight weight : clusterGroup) { + totalWeight += weight.getWeight(); + } + this.clusterGroup = clusterGroup; + this.totalWeight = totalWeight; + } + + + @Override + @SuppressWarnings("ConstantConditions") + public T select() { + if (CollectionUtils.isEmpty(clusterGroup)) { + logger.warn("No servers available"); + return null; + } + Weight targetWeight = null; + for (Weight weight : clusterGroup) { + weight.increaseCurrentWeight(); + if (targetWeight == null || targetWeight.getCurrentWeight().get() < weight.getCurrentWeight().get()) { + targetWeight = weight; + } + } + targetWeight.decreaseTotal(totalWeight); + return targetWeight.getTarget(); + } + + @Override + public LoadBalanceType getType() { + return LoadBalanceType.WEIGHT_ROUND_ROBIN; + } +} diff --git a/eventmesh-emesher/src/test/java/protocol/CmdHelloTest.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/SubcriptionType.java similarity index 67% rename from eventmesh-emesher/src/test/java/protocol/CmdHelloTest.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/SubcriptionType.java index c472c384bc..6b6cb16823 100644 --- a/eventmesh-emesher/src/test/java/protocol/CmdHelloTest.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/SubcriptionType.java @@ -15,22 +15,29 @@ * limitations under the License. */ -package protocol; +package org.apache.eventmesh.common.protocol; -import client.common.Server; -import org.junit.Test; +public enum SubcriptionType { + /** + * SYNC + */ + SYNC("SYNC"), + /** + * ASYNC + */ + ASYNC("ASYNC"); -public class CmdHelloTest { + private String type; - @Test - public void test_Cmd_Hello() throws Exception { - Server server = new Server(); - server.startAccessServer(); - - //do protocol send/receive + SubcriptionType(String type) { + this.type = type; + } - Thread.sleep(3000); - server.shutdownAccessServer(); + public String getType() { + return type; } + public void setType(String type) { + this.type = type; + } } diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/SubscriptionItem.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/SubscriptionItem.java new file mode 100644 index 0000000000..abcc22c20e --- /dev/null +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/SubscriptionItem.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.common.protocol; + +public class SubscriptionItem { + + private String topic; + + private SubscriptionMode mode; + + private SubcriptionType type; + + public SubscriptionItem() { + } + + public SubscriptionItem(String topic, SubscriptionMode mode, SubcriptionType type) { + this.topic = topic; + this.mode = mode; + this.type = type; + } + + public SubcriptionType getType() { + return type; + } + + public void setType(SubcriptionType type) { + this.type = type; + } + + public String getTopic() { + return topic; + } + + public void setTopic(String topic) { + this.topic = topic; + } + + public SubscriptionMode getMode() { + return mode; + } + + public void setMode(SubscriptionMode mode) { + this.mode = mode; + } + + @Override + public String toString() { + return "SubscriptionItem{" + + "topic=" + topic + + ", mode=" + mode + + ", type=" + type + + '}'; + } +} + + diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/SubscriptionMode.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/SubscriptionMode.java new file mode 100644 index 0000000000..ad4b751a46 --- /dev/null +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/SubscriptionMode.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.common.protocol; + +public enum SubscriptionMode { + + /** + * broadcast + */ + BROADCASTING("BROADCASTING"), + /** + * clustering + */ + CLUSTERING("CLUSTERING"); + + private String mode; + + SubscriptionMode(String mode) { + this.mode = mode; + } + + public String getMode() { + return mode; + } + + public void setMode(String mode) { + this.mode = mode; + } + +} diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/BaseRequestBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/BaseRequestBody.java similarity index 95% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/BaseRequestBody.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/BaseRequestBody.java index 23107ffe08..09a3f741a0 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/BaseRequestBody.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/BaseRequestBody.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.body; +package org.apache.eventmesh.common.protocol.http.body; import java.util.HashMap; import java.util.Map; diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/BaseResponseBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/BaseResponseBody.java similarity index 93% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/BaseResponseBody.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/BaseResponseBody.java index 4f17ffe57a..5cb5922f19 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/BaseResponseBody.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/BaseResponseBody.java @@ -15,14 +15,14 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.body; +package org.apache.eventmesh.common.protocol.http.body; -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; - import java.util.HashMap; import java.util.Map; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; + public class BaseResponseBody extends Body { private Integer retCode; diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/Body.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/Body.java similarity index 70% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/Body.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/Body.java index 6750e25f63..c6976433d8 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/Body.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/Body.java @@ -15,21 +15,23 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.body; +package org.apache.eventmesh.common.protocol.http.body; -import com.webank.eventmesh.common.protocol.http.body.client.HeartbeatRequestBody; -import com.webank.eventmesh.common.protocol.http.body.client.UnRegRequestBody; -import com.webank.eventmesh.common.protocol.http.common.RequestCode; -import com.webank.eventmesh.common.protocol.http.body.client.RegRequestBody; -import com.webank.eventmesh.common.protocol.http.body.message.PushMessageRequestBody; -import com.webank.eventmesh.common.protocol.http.body.message.ReplyMessageRequestBody; -import com.webank.eventmesh.common.protocol.http.body.message.SendMessageBatchRequestBody; -import com.webank.eventmesh.common.protocol.http.body.message.SendMessageBatchV2RequestBody; -import com.webank.eventmesh.common.protocol.http.body.message.SendMessageRequestBody; - import java.util.Map; +import org.apache.eventmesh.common.protocol.http.body.client.HeartbeatRequestBody; +import org.apache.eventmesh.common.protocol.http.body.client.RegRequestBody; +import org.apache.eventmesh.common.protocol.http.body.client.SubscribeRequestBody; +import org.apache.eventmesh.common.protocol.http.body.client.UnRegRequestBody; +import org.apache.eventmesh.common.protocol.http.body.client.UnSubscribeRequestBody; +import org.apache.eventmesh.common.protocol.http.body.message.PushMessageRequestBody; +import org.apache.eventmesh.common.protocol.http.body.message.ReplyMessageRequestBody; +import org.apache.eventmesh.common.protocol.http.body.message.SendMessageBatchRequestBody; +import org.apache.eventmesh.common.protocol.http.body.message.SendMessageBatchV2RequestBody; +import org.apache.eventmesh.common.protocol.http.body.message.SendMessageRequestBody; +import org.apache.eventmesh.common.protocol.http.common.RequestCode; + public abstract class Body { public abstract Map toMap(); @@ -37,7 +39,8 @@ public abstract class Body { public static Body buildBody(String requestCode, Map originalMap) throws Exception { if (String.valueOf(RequestCode.MSG_BATCH_SEND.getRequestCode()).equals(requestCode)) { return SendMessageBatchRequestBody.buildBody(originalMap); - } if (String.valueOf(RequestCode.MSG_BATCH_SEND_V2.getRequestCode()).equals(requestCode)) { + } + if (String.valueOf(RequestCode.MSG_BATCH_SEND_V2.getRequestCode()).equals(requestCode)) { return SendMessageBatchV2RequestBody.buildBody(originalMap); } else if (String.valueOf(RequestCode.MSG_SEND_ASYNC.getRequestCode()).equals(requestCode)) { return SendMessageRequestBody.buildBody(originalMap); @@ -51,6 +54,10 @@ public static Body buildBody(String requestCode, Map originalMap return RegRequestBody.buildBody(originalMap); } else if (String.valueOf(RequestCode.UNREGISTER.getRequestCode()).equals(requestCode)) { return UnRegRequestBody.buildBody(originalMap); + } else if (String.valueOf(RequestCode.SUBSCRIBE.getRequestCode()).equals(requestCode)) { + return SubscribeRequestBody.buildBody(originalMap); + } else if (String.valueOf(RequestCode.UNSUBSCRIBE.getRequestCode()).equals(requestCode)) { + return UnSubscribeRequestBody.buildBody(originalMap); } else if (String.valueOf(RequestCode.HEARTBEAT.getRequestCode()).equals(requestCode)) { return HeartbeatRequestBody.buildBody(originalMap); } else if (String.valueOf(RequestCode.REPLY_MESSAGE.getRequestCode()).equals(requestCode)) { diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/HeartbeatRequestBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/HeartbeatRequestBody.java similarity index 79% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/HeartbeatRequestBody.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/HeartbeatRequestBody.java index 115e91c288..751b63248f 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/HeartbeatRequestBody.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/HeartbeatRequestBody.java @@ -15,22 +15,25 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.body.client; - -import com.webank.eventmesh.common.protocol.http.body.Body; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONArray; -import org.apache.commons.collections4.MapUtils; +package org.apache.eventmesh.common.protocol.http.body.client; import java.util.HashMap; import java.util.List; import java.util.Map; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; + +import org.apache.commons.collections4.MapUtils; +import org.apache.eventmesh.common.protocol.http.body.Body; + public class HeartbeatRequestBody extends Body { public static final String CLIENTTYPE = "clientType"; public static final String HEARTBEATENTITIES = "heartbeatEntities"; + public static final String CONSUMERGROUP = "consumerGroup"; + private String consumerGroup; private String clientType; @@ -52,9 +55,18 @@ public void setHeartbeatEntities(List heartbeatEntities) { this.heartbeatEntities = heartbeatEntities; } + public String getConsumerGroup() { + return consumerGroup; + } + + public void setConsumerGroup(String consumerGroup) { + this.consumerGroup = consumerGroup; + } + public static HeartbeatRequestBody buildBody(Map bodyParam) { HeartbeatRequestBody body = new HeartbeatRequestBody(); body.setClientType(MapUtils.getString(bodyParam, CLIENTTYPE)); + body.setConsumerGroup(MapUtils.getString(bodyParam, CONSUMERGROUP)); body.setHeartbeatEntities(JSONArray.parseArray(MapUtils.getString(bodyParam, HEARTBEATENTITIES), HeartbeatEntity.class)); return body; } @@ -63,6 +75,7 @@ public static HeartbeatRequestBody buildBody(Map bodyParam) { public Map toMap() { Map map = new HashMap(); map.put(CLIENTTYPE, clientType); + map.put(CONSUMERGROUP, consumerGroup); map.put(HEARTBEATENTITIES, JSON.toJSONString(heartbeatEntities)); return map; } @@ -70,6 +83,7 @@ public Map toMap() { public static class HeartbeatEntity { public String topic; public String serviceId; + public String url; public String instanceId; @Override @@ -77,8 +91,9 @@ public String toString() { StringBuilder sb = new StringBuilder(); sb.append("heartbeatEntity={") .append("topic=").append(topic).append(",") - .append("heartbeatEntities=").append(serviceId).append(",") - .append("instanceId=").append(instanceId).append("}"); + .append("serviceId=").append(serviceId).append(",") + .append("instanceId=").append(instanceId).append(",") + .append("url=").append(url).append("}"); return sb.toString(); } } @@ -87,6 +102,7 @@ public String toString() { public String toString() { StringBuilder sb = new StringBuilder(); sb.append("heartbeatRequestBody={") + .append("consumerGroup=").append(consumerGroup).append(",") .append("clientType=").append(clientType).append("}"); return sb.toString(); } diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/HeartbeatResponseBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/HeartbeatResponseBody.java similarity index 91% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/HeartbeatResponseBody.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/HeartbeatResponseBody.java index fd49b5df8c..0f05f512ce 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/HeartbeatResponseBody.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/HeartbeatResponseBody.java @@ -15,16 +15,16 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.body.client; - -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import com.webank.eventmesh.common.Constants; -import com.webank.eventmesh.common.protocol.http.body.Body; -import org.apache.commons.lang3.time.DateFormatUtils; +package org.apache.eventmesh.common.protocol.http.body.client; import java.util.HashMap; import java.util.Map; +import org.apache.commons.lang3.time.DateFormatUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.http.body.Body; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; + public class HeartbeatResponseBody extends Body { //响应码 diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/RegRequestBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/RegRequestBody.java similarity index 86% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/RegRequestBody.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/RegRequestBody.java index 8739590bf6..78bb684cbd 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/RegRequestBody.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/RegRequestBody.java @@ -15,17 +15,19 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.body.client; - -import com.webank.eventmesh.common.protocol.http.body.Body; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONArray; -import org.apache.commons.collections4.MapUtils; +package org.apache.eventmesh.common.protocol.http.body.client; import java.util.HashMap; import java.util.List; import java.util.Map; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; + +import org.apache.commons.collections4.MapUtils; +import org.apache.eventmesh.common.protocol.SubscriptionItem; +import org.apache.eventmesh.common.protocol.http.body.Body; + public class RegRequestBody extends Body { public static final String CLIENTTYPE = "clientType"; @@ -38,13 +40,13 @@ public class RegRequestBody extends Body { private String endPoint; - private List topics; + private List topics; - public List getTopics() { + public List getTopics() { return topics; } - public void setTopics(List topics) { + public void setTopics(List topics) { this.topics = topics; } @@ -68,7 +70,7 @@ public static RegRequestBody buildBody(Map bodyParam) { RegRequestBody body = new RegRequestBody(); body.setClientType(MapUtils.getString(bodyParam, CLIENTTYPE)); body.setEndPoint(MapUtils.getString(bodyParam, ENDPOINT)); - body.setTopics(JSONArray.parseArray(MapUtils.getString(bodyParam, TOPICS), String.class)); + body.setTopics(JSONArray.parseArray(MapUtils.getString(bodyParam, TOPICS), SubscriptionItem.class)); return body; } diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/RegResponseBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/RegResponseBody.java similarity index 91% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/RegResponseBody.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/RegResponseBody.java index cffd492eb6..1fc1366f31 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/RegResponseBody.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/RegResponseBody.java @@ -15,16 +15,16 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.body.client; - -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import com.webank.eventmesh.common.Constants; -import com.webank.eventmesh.common.protocol.http.body.Body; -import org.apache.commons.lang3.time.DateFormatUtils; +package org.apache.eventmesh.common.protocol.http.body.client; import java.util.HashMap; import java.util.Map; +import org.apache.commons.lang3.time.DateFormatUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.http.body.Body; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; + public class RegResponseBody extends Body { private Integer retCode; private String retMsg; diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/SubscribeRequestBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/SubscribeRequestBody.java new file mode 100644 index 0000000000..72e2e91aab --- /dev/null +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/SubscribeRequestBody.java @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.common.protocol.http.body.client; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; + +import org.apache.commons.collections4.MapUtils; +import org.apache.eventmesh.common.protocol.SubscriptionItem; +import org.apache.eventmesh.common.protocol.http.body.Body; + +public class SubscribeRequestBody extends Body { + + public static final String TOPIC = "topic"; + + public static final String URL = "url"; + + public static final String CONSUMERGROUP = "consumerGroup"; + + private List topics; + + private String url; + + private String consumerGroup; + + public String getConsumerGroup() { + return consumerGroup; + } + + public void setConsumerGroup(String consumerGroup) { + this.consumerGroup = consumerGroup; + } + + public List getTopics() { + return topics; + } + + public void setTopics(List topics) { + this.topics = topics; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public static SubscribeRequestBody buildBody(Map bodyParam) { + SubscribeRequestBody body = new SubscribeRequestBody(); + body.setUrl(MapUtils.getString(bodyParam, URL)); + body.setTopics(JSONArray.parseArray(MapUtils.getString(bodyParam, TOPIC), SubscriptionItem.class)); + body.setConsumerGroup(MapUtils.getString(bodyParam, CONSUMERGROUP)); + return body; + } + + @Override + public Map toMap() { + Map map = new HashMap(); + map.put(URL, url); + map.put(TOPIC, JSON.toJSONString(topics)); + map.put(CONSUMERGROUP, consumerGroup); + return map; + } + + @Override + public String toString() { + return "subscribeBody{" + + "consumerGroup='" + consumerGroup + '\'' + + ", url='" + url + '\'' + + ", topics=" + topics + + '}'; + } +} diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/SubscribeResponseBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/SubscribeResponseBody.java new file mode 100644 index 0000000000..21b70826cd --- /dev/null +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/SubscribeResponseBody.java @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.common.protocol.http.body.client; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.lang3.time.DateFormatUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.http.body.Body; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; + +public class SubscribeResponseBody extends Body { + private Integer retCode; + private String retMsg; + private long resTime = System.currentTimeMillis(); + + public Integer getRetCode() { + return retCode; + } + + public void setRetCode(Integer retCode) { + this.retCode = retCode; + } + + public String getRetMsg() { + return retMsg; + } + + public void setRetMsg(String retMsg) { + this.retMsg = retMsg; + } + + public long getResTime() { + return resTime; + } + + public void setResTime(long resTime) { + this.resTime = resTime; + } + + public static SubscribeResponseBody buildBody(Integer retCode, String retMsg) throws Exception { + SubscribeResponseBody regResponseBody = new SubscribeResponseBody(); + regResponseBody.setRetMsg(retMsg); + regResponseBody.setResTime(System.currentTimeMillis()); + regResponseBody.setRetCode(retCode); + return regResponseBody; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("regResponseBody={") + .append("retCode=").append(retCode).append(",") + .append("retMsg=").append(retMsg).append(",") + .append("resTime=").append(DateFormatUtils.format(resTime, Constants.DATE_FORMAT)).append("}"); + return sb.toString(); + } + + @Override + public Map toMap() { + Map map = new HashMap<>(); + map.put(ProtocolKey.RETCODE, retCode); + map.put(ProtocolKey.RETMSG, retMsg); + map.put(ProtocolKey.RESTIME, resTime); + return map; + } +} diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/UnRegRequestBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/UnRegRequestBody.java similarity index 96% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/UnRegRequestBody.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/UnRegRequestBody.java index f0e0b7c111..fe445957f2 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/UnRegRequestBody.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/UnRegRequestBody.java @@ -15,17 +15,18 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.body.client; - -import com.webank.eventmesh.common.protocol.http.body.Body; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONArray; -import org.apache.commons.collections4.MapUtils; +package org.apache.eventmesh.common.protocol.http.body.client; import java.util.HashMap; import java.util.List; import java.util.Map; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; + +import org.apache.commons.collections4.MapUtils; +import org.apache.eventmesh.common.protocol.http.body.Body; + public class UnRegRequestBody extends Body { public static final String CLIENTTYPE = "clientType"; diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/UnRegResponseBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/UnRegResponseBody.java similarity index 85% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/UnRegResponseBody.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/UnRegResponseBody.java index 3b1a033b6e..325095187e 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/UnRegResponseBody.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/UnRegResponseBody.java @@ -15,16 +15,16 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.body.client; - -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import com.webank.eventmesh.common.Constants; -import com.webank.eventmesh.common.protocol.http.body.Body; -import org.apache.commons.lang3.time.DateFormatUtils; +package org.apache.eventmesh.common.protocol.http.body.client; import java.util.HashMap; import java.util.Map; +import org.apache.commons.lang3.time.DateFormatUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.http.body.Body; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; + public class UnRegResponseBody extends Body { private Integer retCode; private String retMsg; @@ -54,8 +54,8 @@ public void setResTime(long resTime) { this.resTime = resTime; } - public static RegResponseBody buildBody(Integer retCode, String retMsg) throws Exception { - RegResponseBody regResponseBody = new RegResponseBody(); + public static UnRegResponseBody buildBody(Integer retCode, String retMsg) throws Exception { + UnRegResponseBody regResponseBody = new UnRegResponseBody(); regResponseBody.setRetMsg(retMsg); regResponseBody.setResTime(System.currentTimeMillis()); regResponseBody.setRetCode(retCode); diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/UnSubscribeRequestBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/UnSubscribeRequestBody.java new file mode 100644 index 0000000000..756f1a92e4 --- /dev/null +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/UnSubscribeRequestBody.java @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.common.protocol.http.body.client; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; + +import org.apache.commons.collections4.MapUtils; +import org.apache.eventmesh.common.protocol.http.body.Body; + +public class UnSubscribeRequestBody extends Body { + + public static final String TOPIC = "topic"; + + public static final String URL = "url"; + + public static final String CONSUMERGROUP = "consumerGroup"; + + private List topics; + + private String url; + + private String consumerGroup; + + public List getTopics() { + return topics; + } + + public void setTopics(List topics) { + this.topics = topics; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getConsumerGroup() { + return consumerGroup; + } + + public void setConsumerGroup(String consumerGroup) { + this.consumerGroup = consumerGroup; + } + + public static UnSubscribeRequestBody buildBody(Map bodyParam) { + UnSubscribeRequestBody body = new UnSubscribeRequestBody(); + body.setUrl(MapUtils.getString(bodyParam, URL)); + body.setTopics(JSONArray.parseArray(MapUtils.getString(bodyParam, TOPIC), String.class)); + body.setConsumerGroup(MapUtils.getString(bodyParam, CONSUMERGROUP)); + return body; + } + + @Override + public Map toMap() { + Map map = new HashMap(); + map.put(URL, url); + map.put(TOPIC, JSON.toJSONString(topics)); + map.put(CONSUMERGROUP, consumerGroup); + return map; + } + + @Override + public String toString() { + return "unSubscribeRequestBody{" + + "consumerGroup='" + consumerGroup + '\'' + + ", url='" + url + '\'' + + ", topics=" + topics + + '}'; + } +} diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/UnSubscribeResponseBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/UnSubscribeResponseBody.java new file mode 100644 index 0000000000..d62b9d9de3 --- /dev/null +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/client/UnSubscribeResponseBody.java @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.common.protocol.http.body.client; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.lang3.time.DateFormatUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.http.body.Body; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; + +public class UnSubscribeResponseBody extends Body { + + private Integer retCode; + private String retMsg; + private long resTime = System.currentTimeMillis(); + + public Integer getRetCode() { + return retCode; + } + + public void setRetCode(Integer retCode) { + this.retCode = retCode; + } + + public String getRetMsg() { + return retMsg; + } + + public void setRetMsg(String retMsg) { + this.retMsg = retMsg; + } + + public long getResTime() { + return resTime; + } + + public void setResTime(long resTime) { + this.resTime = resTime; + } + + public static UnSubscribeResponseBody buildBody(Integer retCode, String retMsg) throws Exception { + UnSubscribeResponseBody regResponseBody = new UnSubscribeResponseBody(); + regResponseBody.setRetMsg(retMsg); + regResponseBody.setResTime(System.currentTimeMillis()); + regResponseBody.setRetCode(retCode); + return regResponseBody; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("regResponseBody={") + .append("retCode=").append(retCode).append(",") + .append("retMsg=").append(retMsg).append(",") + .append("resTime=").append(DateFormatUtils.format(resTime, Constants.DATE_FORMAT)).append("}"); + return sb.toString(); + } + + @Override + public Map toMap() { + Map map = new HashMap<>(); + map.put(ProtocolKey.RETCODE, retCode); + map.put(ProtocolKey.RETMSG, retMsg); + map.put(ProtocolKey.RESTIME, resTime); + return map; + } +} diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/PushMessageRequestBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/PushMessageRequestBody.java similarity index 93% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/PushMessageRequestBody.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/PushMessageRequestBody.java index dbd0ce45c0..accefa66c8 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/PushMessageRequestBody.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/PushMessageRequestBody.java @@ -15,16 +15,17 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.body.message; +package org.apache.eventmesh.common.protocol.http.body.message; + +import java.util.HashMap; +import java.util.Map; -import com.webank.eventmesh.common.protocol.http.body.Body; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; + import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; - -import java.util.HashMap; -import java.util.Map; +import org.apache.eventmesh.common.protocol.http.body.Body; public class PushMessageRequestBody extends Body { @@ -95,6 +96,7 @@ public void setExtFields(HashMap extFields) { this.extFields = extFields; } + @SuppressWarnings("unchecked") public static PushMessageRequestBody buildBody(final Map bodyParam) { PushMessageRequestBody pushMessageRequestBody = new PushMessageRequestBody(); pushMessageRequestBody.setContent(MapUtils.getString(bodyParam, CONTENT)); @@ -103,8 +105,9 @@ public static PushMessageRequestBody buildBody(final Map bodyPar pushMessageRequestBody.setUniqueId(MapUtils.getString(bodyParam, UNIQUEID)); pushMessageRequestBody.setRandomNo(MapUtils.getString(bodyParam, RANDOMNO)); String extFields = MapUtils.getString(bodyParam, EXTFIELDS); + if (StringUtils.isNotBlank(extFields)) { - pushMessageRequestBody.setExtFields(JSONObject.parseObject(extFields, HashMap.class)); + pushMessageRequestBody.setExtFields((HashMap) JSONObject.parseObject(extFields, HashMap.class)); } return pushMessageRequestBody; } diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/PushMessageResponseBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/PushMessageResponseBody.java similarity index 91% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/PushMessageResponseBody.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/PushMessageResponseBody.java index 8b1f28e5a2..a5ac0d1658 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/PushMessageResponseBody.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/PushMessageResponseBody.java @@ -15,16 +15,16 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.body.message; - -import com.webank.eventmesh.common.Constants; -import com.webank.eventmesh.common.protocol.http.body.Body; -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import org.apache.commons.lang3.time.DateFormatUtils; +package org.apache.eventmesh.common.protocol.http.body.message; import java.util.HashMap; import java.util.Map; +import org.apache.commons.lang3.time.DateFormatUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.http.body.Body; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; + public class PushMessageResponseBody extends Body { private Integer retCode; diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/ReplyMessageRequestBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/ReplyMessageRequestBody.java similarity index 83% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/ReplyMessageRequestBody.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/ReplyMessageRequestBody.java index ebf9a69ef8..e2de019df4 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/ReplyMessageRequestBody.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/ReplyMessageRequestBody.java @@ -15,25 +15,26 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.body.message; +package org.apache.eventmesh.common.protocol.http.body.message; + +import java.util.HashMap; +import java.util.Map; -import com.webank.eventmesh.common.protocol.http.body.Body; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; + import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; - -import java.util.HashMap; -import java.util.Map; +import org.apache.eventmesh.common.protocol.http.body.Body; public class ReplyMessageRequestBody extends Body { - public static final String ORIGTOPIC = "origTopic"; public static final String BIZSEQNO = "bizSeqNo"; public static final String UNIQUEID = "uniqueId"; public static final String CONTENT = "content"; public static final String EXTFIELDS = "extFields"; + public static final String PRODUCERGROUP = "producerGroup"; private String bizSeqNo; @@ -45,6 +46,8 @@ public class ReplyMessageRequestBody extends Body { private HashMap extFields; + private String producerGroup; + public String getOrigTopic() { return origTopic; } @@ -85,6 +88,15 @@ public void setExtFields(HashMap extFields) { this.extFields = extFields; } + public String getProducerGroup() { + return producerGroup; + } + + public void setProducerGroup(String producerGroup) { + this.producerGroup = producerGroup; + } + + @SuppressWarnings("unchecked") public static ReplyMessageRequestBody buildBody(Map bodyParam) { ReplyMessageRequestBody body = new ReplyMessageRequestBody(); body.setBizSeqNo(MapUtils.getString(bodyParam, BIZSEQNO)); @@ -93,8 +105,9 @@ public static ReplyMessageRequestBody buildBody(Map bodyParam) { body.setOrigTopic(MapUtils.getString(bodyParam, ORIGTOPIC)); String extFields = MapUtils.getString(bodyParam, EXTFIELDS); if (StringUtils.isNotBlank(extFields)) { - body.setExtFields(JSONObject.parseObject(extFields, HashMap.class)); + body.setExtFields((HashMap) JSONObject.parseObject(extFields, HashMap.class)); } + body.setProducerGroup(MapUtils.getString(bodyParam, PRODUCERGROUP)); return body; } @@ -106,6 +119,7 @@ public String toString() { .append("uniqueId=").append(uniqueId).append(",") .append("origTopic=").append(origTopic).append(",") .append("content=").append(content).append(",") + .append("producerGroup=").append(producerGroup).append(",") .append("extFields=").append(extFields).append("}"); return sb.toString(); } @@ -118,6 +132,7 @@ public Map toMap() { map.put(UNIQUEID, uniqueId); map.put(CONTENT, content); map.put(EXTFIELDS, JSON.toJSONString(extFields)); + map.put(PRODUCERGROUP, producerGroup); return map; } } diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/ReplyMessageResponseBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/ReplyMessageResponseBody.java similarity index 91% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/ReplyMessageResponseBody.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/ReplyMessageResponseBody.java index 99b6a42939..b5a2069481 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/ReplyMessageResponseBody.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/ReplyMessageResponseBody.java @@ -15,16 +15,16 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.body.message; - -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import com.webank.eventmesh.common.Constants; -import com.webank.eventmesh.common.protocol.http.body.Body; -import org.apache.commons.lang3.time.DateFormatUtils; +package org.apache.eventmesh.common.protocol.http.body.message; import java.util.HashMap; import java.util.Map; +import org.apache.commons.lang3.time.DateFormatUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.http.body.Body; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; + public class ReplyMessageResponseBody extends Body { //响应码 diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/SendMessageBatchRequestBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/SendMessageBatchRequestBody.java similarity index 85% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/SendMessageBatchRequestBody.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/SendMessageBatchRequestBody.java index 067d398c06..576e35224c 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/SendMessageBatchRequestBody.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/SendMessageBatchRequestBody.java @@ -15,23 +15,25 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.body.message; +package org.apache.eventmesh.common.protocol.http.body.message; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; -import com.webank.eventmesh.common.protocol.http.body.Body; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; + import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import org.apache.eventmesh.common.protocol.http.body.Body; public class SendMessageBatchRequestBody extends Body { public static final String BATCHID = "batchId"; public static final String CONTENTS = "contents"; public static final String SIZE = "size"; + public static final String PRODUCERGROUP = "producerGroup"; private String batchId; @@ -39,6 +41,8 @@ public class SendMessageBatchRequestBody extends Body { private String size; + private String producerGroup; + public SendMessageBatchRequestBody() { } @@ -66,12 +70,21 @@ public void setSize(String size) { this.size = size; } + public String getProducerGroup() { + return producerGroup; + } + + public void setProducerGroup(String producerGroup) { + this.producerGroup = producerGroup; + } + @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("sendMessageBatchRequestBody={") .append("batchId=").append(batchId).append(",") .append("size=").append(size).append(",") + .append("producerGroup=").append(producerGroup).append(",") .append("contents=").append(JSON.toJSONString(contents)).append("}"); return sb.toString(); } @@ -100,7 +113,7 @@ public static SendMessageBatchRequestBody buildBody(final Map bo String batchId = MapUtils.getString(bodyParam, BATCHID); String size = StringUtils.isBlank(MapUtils.getString(bodyParam, - SIZE)) ? "1" : MapUtils.getString(bodyParam, + SIZE)) ? "1" : MapUtils.getString(bodyParam, SIZE); String contents = MapUtils.getString(bodyParam, CONTENTS, null); @@ -110,6 +123,7 @@ public static SendMessageBatchRequestBody buildBody(final Map bo body.setContents(JSONArray.parseArray(contents, BatchMessageEntity.class)); } body.setSize(size); + body.setProducerGroup(MapUtils.getString(bodyParam, PRODUCERGROUP)); return body; } @@ -119,6 +133,7 @@ public Map toMap() { map.put(BATCHID, batchId); map.put(SIZE, size); map.put(CONTENTS, contents); + map.put(PRODUCERGROUP, producerGroup); return map; } diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/SendMessageBatchResponseBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/SendMessageBatchResponseBody.java similarity index 92% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/SendMessageBatchResponseBody.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/SendMessageBatchResponseBody.java index 45ffb0dd7b..cf5ad72906 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/SendMessageBatchResponseBody.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/SendMessageBatchResponseBody.java @@ -15,16 +15,16 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.body.message; - -import com.webank.eventmesh.common.Constants; -import com.webank.eventmesh.common.protocol.http.body.Body; -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import org.apache.commons.lang3.time.DateFormatUtils; +package org.apache.eventmesh.common.protocol.http.body.message; import java.util.HashMap; import java.util.Map; +import org.apache.commons.lang3.time.DateFormatUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.http.body.Body; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; + public class SendMessageBatchResponseBody extends Body { //响应码 diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/SendMessageBatchV2RequestBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/SendMessageBatchV2RequestBody.java similarity index 85% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/SendMessageBatchV2RequestBody.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/SendMessageBatchV2RequestBody.java index 3532da883c..8f97cf16c0 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/SendMessageBatchV2RequestBody.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/SendMessageBatchV2RequestBody.java @@ -15,14 +15,14 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.body.message; - -import com.webank.eventmesh.common.protocol.http.body.Body; -import org.apache.commons.collections4.MapUtils; +package org.apache.eventmesh.common.protocol.http.body.message; import java.util.HashMap; import java.util.Map; +import org.apache.commons.collections4.MapUtils; +import org.apache.eventmesh.common.protocol.http.body.Body; + public class SendMessageBatchV2RequestBody extends Body { public static final String BIZSEQNO = "bizSeqNo"; @@ -30,6 +30,7 @@ public class SendMessageBatchV2RequestBody extends Body { public static final String MSG = "msg"; public static final String TAG = "tag"; public static final String TTL = "ttl"; + public static final String PRODUCERGROUP = "producerGroup"; private String bizSeqNo; @@ -41,6 +42,8 @@ public class SendMessageBatchV2RequestBody extends Body { private String ttl; + private String producerGroup; + public String getBizSeqNo() { return bizSeqNo; } @@ -81,6 +84,14 @@ public void setTtl(String ttl) { this.ttl = ttl; } + public String getProducerGroup() { + return producerGroup; + } + + public void setProducerGroup(String producerGroup) { + this.producerGroup = producerGroup; + } + public static SendMessageBatchV2RequestBody buildBody(final Map bodyParam) { String bizSeqno = MapUtils.getString(bodyParam, BIZSEQNO); @@ -98,6 +109,7 @@ public static SendMessageBatchV2RequestBody buildBody(final Map body.setTag(tag); body.setTtl(ttl); body.setTopic(topic); + body.setProducerGroup(MapUtils.getString(bodyParam, PRODUCERGROUP)); return body; } @@ -109,6 +121,7 @@ public Map toMap() { map.put(MSG, msg); map.put(TAG, tag); map.put(TTL, ttl); + map.put(PRODUCERGROUP, producerGroup); return map; } @@ -120,6 +133,7 @@ public String toString() { .append("topic=").append(topic).append(",") .append("tag=").append(tag).append(",") .append("ttl=").append(ttl).append(",") + .append("producerGroup=").append(producerGroup).append(",") .append("msg=").append(msg).append("}"); return sb.toString(); } diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/SendMessageBatchV2ResponseBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/SendMessageBatchV2ResponseBody.java similarity index 92% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/SendMessageBatchV2ResponseBody.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/SendMessageBatchV2ResponseBody.java index bc5d9b1b5e..fd0b49a22c 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/SendMessageBatchV2ResponseBody.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/SendMessageBatchV2ResponseBody.java @@ -15,16 +15,16 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.body.message; - -import com.webank.eventmesh.common.Constants; -import com.webank.eventmesh.common.protocol.http.body.Body; -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import org.apache.commons.lang3.time.DateFormatUtils; +package org.apache.eventmesh.common.protocol.http.body.message; import java.util.HashMap; import java.util.Map; +import org.apache.commons.lang3.time.DateFormatUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.http.body.Body; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; + public class SendMessageBatchV2ResponseBody extends Body { //响应码 diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/SendMessageRequestBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/SendMessageRequestBody.java similarity index 85% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/SendMessageRequestBody.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/SendMessageRequestBody.java index d123b61694..5c302e857c 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/SendMessageRequestBody.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/SendMessageRequestBody.java @@ -15,15 +15,16 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.body.message; +package org.apache.eventmesh.common.protocol.http.body.message; + +import java.util.HashMap; +import java.util.Map; -import com.webank.eventmesh.common.protocol.http.body.Body; import com.alibaba.fastjson.JSONObject; + import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; - -import java.util.HashMap; -import java.util.Map; +import org.apache.eventmesh.common.protocol.http.body.Body; public class SendMessageRequestBody extends Body { @@ -34,7 +35,7 @@ public class SendMessageRequestBody extends Body { public static final String TTL = "ttl"; public static final String TAG = "tag"; public static final String EXTFIELDS = "extFields"; - + public static final String PRODUCERGROUP = "producerGroup"; private String topic; @@ -50,6 +51,8 @@ public class SendMessageRequestBody extends Body { private HashMap extFields; + private String producerGroup; + public String getTopic() { return topic; } @@ -106,6 +109,15 @@ public void setTag(String tag) { this.tag = tag; } + public String getProducerGroup() { + return producerGroup; + } + + public void setProducerGroup(String producerGroup) { + this.producerGroup = producerGroup; + } + + @SuppressWarnings("unchecked") public static SendMessageRequestBody buildBody(Map bodyParam) { SendMessageRequestBody body = new SendMessageRequestBody(); body.setTopic(MapUtils.getString(bodyParam, TOPIC)); @@ -116,8 +128,9 @@ public static SendMessageRequestBody buildBody(Map bodyParam) { body.setContent(MapUtils.getString(bodyParam, CONTENT)); String extFields = MapUtils.getString(bodyParam, EXTFIELDS); if (StringUtils.isNotBlank(extFields)) { - body.setExtFields(JSONObject.parseObject(extFields, HashMap.class)); + body.setExtFields((HashMap) JSONObject.parseObject(extFields, HashMap.class)); } + body.setProducerGroup(MapUtils.getString(bodyParam, PRODUCERGROUP)); return body; } @@ -131,6 +144,7 @@ public Map toMap() { map.put(TAG, tag); map.put(CONTENT, content); map.put(EXTFIELDS, extFields); + map.put(PRODUCERGROUP, producerGroup); return map; } @@ -144,6 +158,7 @@ public String toString() { .append("content=").append(content).append(",") .append("ttl=").append(ttl).append(",") .append("tag=").append(tag).append(",") + .append("producerGroup=").append(producerGroup).append(",") .append("extFields=").append(extFields).append("}"); return sb.toString(); } diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/SendMessageResponseBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/SendMessageResponseBody.java similarity index 92% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/SendMessageResponseBody.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/SendMessageResponseBody.java index 375878439c..b9175a2c74 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/message/SendMessageResponseBody.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/SendMessageResponseBody.java @@ -15,16 +15,16 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.body.message; - -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import com.webank.eventmesh.common.Constants; -import com.webank.eventmesh.common.protocol.http.body.Body; -import org.apache.commons.lang3.time.DateFormatUtils; +package org.apache.eventmesh.common.protocol.http.body.message; import java.util.HashMap; import java.util.Map; +import org.apache.commons.lang3.time.DateFormatUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.http.body.Body; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; + public class SendMessageResponseBody extends Body { private Integer retCode; diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/common/ClientRetCode.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/ClientRetCode.java similarity index 94% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/common/ClientRetCode.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/ClientRetCode.java index 2da7563072..30986efa9c 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/common/ClientRetCode.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/ClientRetCode.java @@ -15,12 +15,12 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.common; +package org.apache.eventmesh.common.protocol.http.common; public enum ClientRetCode { /** - * 这个RETRY的意思是 客户端发现投递的消息它没有监听时, 告诉PROXY 发往下一个, 重试几次以实现灰度 , 预留 + * 这个RETRY的意思是 客户端发现投递的消息它没有监听时, 告诉EventMesh 发往下一个, 重试几次以实现灰度 , 预留 */ OK(1, "ok, SDK返回"), diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/common/ClientType.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/ClientType.java similarity index 91% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/common/ClientType.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/ClientType.java index 4ddeb826ab..fdebba5f3b 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/common/ClientType.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/ClientType.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.common; +package org.apache.eventmesh.common.protocol.http.common; public enum ClientType { @@ -44,8 +44,8 @@ public static ClientType get(Integer type) { public static boolean contains(Integer clientType) { boolean flag = false; - for(ClientType ct:ClientType.values()) { - if(ct.type == clientType.intValue()) { + for (ClientType ct : ClientType.values()) { + if (ct.type == clientType.intValue()) { flag = true; break; } diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/EventMeshRetCode.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/EventMeshRetCode.java new file mode 100644 index 0000000000..3593e0e40c --- /dev/null +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/EventMeshRetCode.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.common.protocol.http.common; + +public enum EventMeshRetCode { + + SUCCESS(0, "success"), + OVERLOAD(1, "eventMesh overload, try later, "), + EVENTMESH_REQUESTCODE_INVALID(2, "requestCode can't be null, or must be number, "), + EVENTMESH_SEND_SYNC_MSG_ERR(3, "eventMesh send rr msg err, "), + EVENTMESH_WAITING_RR_MSG_ERR(4, "eventMesh waiting rr msg err, "), + EVENTMESH_PROTOCOL_HEADER_ERR(6, "eventMesh protocol[header] err, "), + EVENTMESH_PROTOCOL_BODY_ERR(7, "eventMesh protocol[body] err, "), + EVENTMESH_STOP(8, "eventMesh will stop or had stopped, "), + EVENTMESH_REJECT_BY_PROCESSOR_ERROR(9, "eventMesh reject by processor error, "), + EVENTMESH_BATCH_PRODUCER_STOPED_ERR(10, "eventMesh batch msg producer stopped, "), + EVENTMESH_SEND_BATCHLOG_MSG_ERR(17, "eventMesh send batchlog msg err, "), + EVENTMESH_BATCH_SPEED_OVER_LIMIT_ERR(11, "eventMesh batch msg speed over the limit, "), + EVENTMESH_PACKAGE_MSG_ERR(12, "eventMesh package msg err, "), + EVENTMESH_GROUP_PRODUCER_STOPED_ERR(13, "eventMesh group producer stopped, "), + EVENTMESH_SEND_ASYNC_MSG_ERR(14, "eventMesh send async msg err, "), + EVENTMESH_REPLY_MSG_ERR(15, "eventMesh reply msg err, "), + EVENTMESH_RUNTIME_ERR(16, "eventMesh runtime err, "), + EVENTMESH_SUBSCRIBE_ERR(17, "eventMesh subscribe err"), + EVENTMESH_UNSUBSCRIBE_ERR(18, "eventMesh unsubscribe err"), + EVENTMESH_HEARTBEAT_ERR(19, "eventMesh heartbeat err"); + + private Integer retCode; + + private String errMsg; + + EventMeshRetCode(Integer retCode, String errMsg) { + this.retCode = retCode; + this.errMsg = errMsg; + } + + public Integer getRetCode() { + return retCode; + } + + public void setRetCode(Integer retCode) { + this.retCode = retCode; + } + + public String getErrMsg() { + return errMsg; + } + + public void setErrMsg(String errMsg) { + this.errMsg = errMsg; + } +} diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/common/ProtocolKey.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/ProtocolKey.java similarity index 73% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/common/ProtocolKey.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/ProtocolKey.java index aa4bda3133..12e79ad30d 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/common/ProtocolKey.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/ProtocolKey.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.common; +package org.apache.eventmesh.common.protocol.http.common; public class ProtocolKey { @@ -26,9 +26,7 @@ public class ProtocolKey { public static class ClientInstanceKey { ////////////////////////////////////协议层请求方描述/////////// public static final String ENV = "Env"; - public static final String REGION = "Region"; public static final String IDC = "Idc"; - public static final String DCN = "Dcn"; public static final String SYS = "Sys"; public static final String PID = "Pid"; public static final String IP = "Ip"; @@ -37,18 +35,16 @@ public static class ClientInstanceKey { } - public static class ProxyInstanceKey { - ///////////////////////////////////////////////协议层PROXY描述 - public static final String PROXYCLUSTER = "ProxyCluster"; - public static final String PROXYIP = "ProxyIp"; - public static final String PROXYENV = "ProxyEnv"; - public static final String PROXYREGION = "ProxyRegion"; - public static final String PROXYIDC = "ProxyIdc"; - public static final String PROXYDCN = "ProxyDcn"; + public static class EventMeshInstanceKey { + ///////////////////////////////////////////////协议层EventMesh描述 + public static final String EVENTMESHCLUSTER = "EventMeshCluster"; + public static final String EVENTMESHIP = "EventMeshIp"; + public static final String EVENTMESHENV = "EventMeshEnv"; + public static final String EVENTMESHIDC = "EventMeshIdc"; } - //CLIENT <-> PROXY 的 返回 + //CLIENT <-> EventMesh 的 返回 public static final String RETCODE = "retCode"; public static final String RETMSG = "retMsg"; public static final String RESTIME = "resTime"; diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/common/ProtocolVersion.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/ProtocolVersion.java similarity index 91% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/common/ProtocolVersion.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/ProtocolVersion.java index 9c3e14e05a..acfd655246 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/common/ProtocolVersion.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/ProtocolVersion.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.common; +package org.apache.eventmesh.common.protocol.http.common; public enum ProtocolVersion { @@ -29,9 +29,9 @@ public enum ProtocolVersion { } public static ProtocolVersion get(String version) { - if(V1.version.equals(version)) { + if (V1.version.equals(version)) { return V1; - } else if(V2.version.equals(version)) { + } else if (V2.version.equals(version)) { return V2; } else { return null; diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/common/RequestCode.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/RequestCode.java similarity index 97% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/common/RequestCode.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/RequestCode.java index dd6c4341b0..f193884faa 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/common/RequestCode.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/RequestCode.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.common; +package org.apache.eventmesh.common.protocol.http.common; public enum RequestCode { diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/BaseRequestHeader.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/BaseRequestHeader.java similarity index 93% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/BaseRequestHeader.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/BaseRequestHeader.java index 962c2600c1..d58919e3c4 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/BaseRequestHeader.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/BaseRequestHeader.java @@ -15,14 +15,14 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.header; - -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import org.apache.commons.collections4.MapUtils; +package org.apache.eventmesh.common.protocol.http.header; import java.util.HashMap; import java.util.Map; +import org.apache.commons.collections4.MapUtils; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; + public class BaseRequestHeader extends Header { private String code; diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/BaseResponseHeader.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/BaseResponseHeader.java similarity index 91% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/BaseResponseHeader.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/BaseResponseHeader.java index 30cbbbdd7f..cd7bbaf59b 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/BaseResponseHeader.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/BaseResponseHeader.java @@ -15,14 +15,14 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.header; +package org.apache.eventmesh.common.protocol.http.header; -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; - import java.util.HashMap; import java.util.Map; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; + public class BaseResponseHeader extends Header { private String code; @@ -41,7 +41,7 @@ public static BaseResponseHeader buildHeader(String code) { return baseResponseHeader; } - @Override + @Override public Map toMap() { Map map = new HashMap<>(); map.put(ProtocolKey.REQUEST_CODE, code); diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/Header.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/Header.java similarity index 71% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/Header.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/Header.java index c4fd267728..f2e44108e5 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/Header.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/Header.java @@ -15,21 +15,23 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.header; +package org.apache.eventmesh.common.protocol.http.header; -import com.webank.eventmesh.common.protocol.http.common.RequestCode; -import com.webank.eventmesh.common.protocol.http.header.client.HeartbeatRequestHeader; -import com.webank.eventmesh.common.protocol.http.header.client.RegRequestHeader; -import com.webank.eventmesh.common.protocol.http.header.client.UnRegRequestHeader; -import com.webank.eventmesh.common.protocol.http.header.message.PushMessageRequestHeader; -import com.webank.eventmesh.common.protocol.http.header.message.SendMessageBatchV2RequestHeader; -import com.webank.eventmesh.common.protocol.http.header.message.SendMessageRequestHeader; -import com.webank.eventmesh.common.protocol.http.header.message.ReplyMessageRequestHeader; -import com.webank.eventmesh.common.protocol.http.header.message.SendMessageBatchRequestHeader; - import java.util.Map; +import org.apache.eventmesh.common.protocol.http.common.RequestCode; +import org.apache.eventmesh.common.protocol.http.header.client.HeartbeatRequestHeader; +import org.apache.eventmesh.common.protocol.http.header.client.RegRequestHeader; +import org.apache.eventmesh.common.protocol.http.header.client.SubscribeRequestHeader; +import org.apache.eventmesh.common.protocol.http.header.client.UnRegRequestHeader; +import org.apache.eventmesh.common.protocol.http.header.client.UnSubscribeRequestHeader; +import org.apache.eventmesh.common.protocol.http.header.message.PushMessageRequestHeader; +import org.apache.eventmesh.common.protocol.http.header.message.ReplyMessageRequestHeader; +import org.apache.eventmesh.common.protocol.http.header.message.SendMessageBatchRequestHeader; +import org.apache.eventmesh.common.protocol.http.header.message.SendMessageBatchV2RequestHeader; +import org.apache.eventmesh.common.protocol.http.header.message.SendMessageRequestHeader; + public abstract class Header { public abstract Map toMap(); @@ -37,7 +39,8 @@ public abstract class Header { public static Header buildHeader(String requestCode, Map originalMap) throws Exception { if (String.valueOf(RequestCode.MSG_BATCH_SEND.getRequestCode()).equals(requestCode)) { return SendMessageBatchRequestHeader.buildHeader(originalMap); - } if (String.valueOf(RequestCode.MSG_BATCH_SEND_V2.getRequestCode()).equals(requestCode)) { + } + if (String.valueOf(RequestCode.MSG_BATCH_SEND_V2.getRequestCode()).equals(requestCode)) { return SendMessageBatchV2RequestHeader.buildHeader(originalMap); } else if (String.valueOf(RequestCode.MSG_SEND_SYNC.getRequestCode()).equals(requestCode)) { return SendMessageRequestHeader.buildHeader(originalMap); @@ -51,6 +54,10 @@ public static Header buildHeader(String requestCode, Map origina return RegRequestHeader.buildHeader(originalMap); } else if (String.valueOf(RequestCode.UNREGISTER.getRequestCode()).equals(requestCode)) { return UnRegRequestHeader.buildHeader(originalMap); + } else if (String.valueOf(RequestCode.SUBSCRIBE.getRequestCode()).equals(requestCode)) { + return SubscribeRequestHeader.buildHeader(originalMap); + } else if (String.valueOf(RequestCode.UNSUBSCRIBE.getRequestCode()).equals(requestCode)) { + return UnSubscribeRequestHeader.buildHeader(originalMap); } else if (String.valueOf(RequestCode.HEARTBEAT.getRequestCode()).equals(requestCode)) { return HeartbeatRequestHeader.buildHeader(originalMap); } else if (String.valueOf(RequestCode.REPLY_MESSAGE.getRequestCode()).equals(requestCode)) { diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/HeartbeatRequestHeader.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/HeartbeatRequestHeader.java similarity index 83% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/HeartbeatRequestHeader.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/HeartbeatRequestHeader.java index 263411ab7a..5cd07242e2 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/HeartbeatRequestHeader.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/HeartbeatRequestHeader.java @@ -15,18 +15,18 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.header.client; - -import com.webank.eventmesh.common.Constants; -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import com.webank.eventmesh.common.protocol.http.common.ProtocolVersion; -import com.webank.eventmesh.common.protocol.http.header.Header; -import org.apache.commons.collections4.MapUtils; -import org.apache.commons.lang3.StringUtils; +package org.apache.eventmesh.common.protocol.http.header.client; import java.util.HashMap; import java.util.Map; +import org.apache.commons.collections4.MapUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; +import org.apache.eventmesh.common.protocol.http.common.ProtocolVersion; +import org.apache.eventmesh.common.protocol.http.header.Header; + public class HeartbeatRequestHeader extends Header { //请求码 @@ -41,15 +41,9 @@ public class HeartbeatRequestHeader extends Header { //请求方所在环境编号 private String env; - //请求方所在区域编码 - private String region; - //请求方所在IDC private String idc; - //请求方所在DCN - private String dcn; - //请求方的子系统 private String sys; @@ -59,6 +53,8 @@ public class HeartbeatRequestHeader extends Header { //请求方的IP private String ip; + private String producerGroup; + //请求方的USERNAME private String username = "username"; @@ -97,14 +93,6 @@ public void setEnv(String env) { this.env = env; } - public String getRegion() { - return region; - } - - public void setRegion(String region) { - this.region = region; - } - public String getIdc() { return idc; } @@ -113,14 +101,6 @@ public void setIdc(String idc) { this.idc = idc; } - public String getDcn() { - return dcn; - } - - public void setDcn(String dcn) { - this.dcn = dcn; - } - public String getSys() { return sys; } @@ -168,9 +148,7 @@ public Map toMap() { map.put(ProtocolKey.LANGUAGE, language); map.put(ProtocolKey.VERSION, version); map.put(ProtocolKey.ClientInstanceKey.ENV, env); - map.put(ProtocolKey.ClientInstanceKey.REGION, region); map.put(ProtocolKey.ClientInstanceKey.IDC, idc); - map.put(ProtocolKey.ClientInstanceKey.DCN, dcn); map.put(ProtocolKey.ClientInstanceKey.SYS, sys); map.put(ProtocolKey.ClientInstanceKey.PID, pid); map.put(ProtocolKey.ClientInstanceKey.IP, ip); @@ -187,9 +165,7 @@ public String toString() { .append("language=").append(language).append(",") .append("version=").append(version).append(",") .append("env=").append(env).append(",") - .append("region=").append(region).append(",") .append("idc=").append(idc).append(",") - .append("dcn=").append(dcn).append(",") .append("sys=").append(sys).append(",") .append("pid=").append(pid).append(",") .append("ip=").append(ip).append(",") @@ -206,9 +182,7 @@ public static HeartbeatRequestHeader buildHeader(Map headerParam ? Constants.LANGUAGE_JAVA : MapUtils.getString(headerParam, ProtocolKey.LANGUAGE); header.setLanguage(lan); header.setEnv(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.ENV)); - header.setRegion(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.REGION)); header.setIdc(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.IDC)); - header.setDcn(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.DCN)); header.setSys(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.SYS)); header.setPid(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.PID)); header.setIp(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.IP)); diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/HeartbeatResponseHeader.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/HeartbeatResponseHeader.java new file mode 100644 index 0000000000..e0d6e99a17 --- /dev/null +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/HeartbeatResponseHeader.java @@ -0,0 +1,113 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.common.protocol.http.header.client; + + +import java.util.HashMap; +import java.util.Map; + +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; +import org.apache.eventmesh.common.protocol.http.header.Header; + +public class HeartbeatResponseHeader extends Header { + + private int code; + + private String eventMeshCluster; + + private String eventMeshIp; + + private String eventMeshEnv; + + private String eventMeshIdc; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getEventMeshCluster() { + return eventMeshCluster; + } + + public void setEventMeshCluster(String eventMeshCluster) { + this.eventMeshCluster = eventMeshCluster; + } + + public String getEventMeshIp() { + return eventMeshIp; + } + + public void setEventMeshIp(String eventMeshIp) { + this.eventMeshIp = eventMeshIp; + } + + public String getEventMeshEnv() { + return eventMeshEnv; + } + + public void setEventMeshEnv(String eventMeshEnv) { + this.eventMeshEnv = eventMeshEnv; + } + + public String getEventMeshIdc() { + return eventMeshIdc; + } + + public void setEventMeshIdc(String eventMeshIdc) { + this.eventMeshIdc = eventMeshIdc; + } + + public static HeartbeatResponseHeader buildHeader(Integer requestCode, String eventMeshCluster, + String eventMeshIp, String eventMeshEnv, + String eventMeshIDC) { + HeartbeatResponseHeader heartbeatResponseHeader = new HeartbeatResponseHeader(); + heartbeatResponseHeader.setCode(requestCode); + heartbeatResponseHeader.setEventMeshCluster(eventMeshCluster); + heartbeatResponseHeader.setEventMeshIp(eventMeshIp); + heartbeatResponseHeader.setEventMeshEnv(eventMeshEnv); + heartbeatResponseHeader.setEventMeshIdc(eventMeshIDC); + return heartbeatResponseHeader; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("heartbeatResponseHeader={") + .append("code=").append(code).append(",") + .append("eventMeshEnv=").append(eventMeshEnv).append(",") + .append("eventMeshIdc=").append(eventMeshIdc).append(",") + .append("eventMeshCluster=").append(eventMeshCluster).append(",") + .append("eventMeshIp=").append(eventMeshIp).append("}"); + return sb.toString(); + } + + @Override + public Map toMap() { + Map map = new HashMap(); + map.put(ProtocolKey.REQUEST_CODE, code); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHCLUSTER, eventMeshCluster); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHIP, eventMeshIp); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHENV, eventMeshEnv); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHIDC, eventMeshIdc); + return map; + } +} diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/RegRequestHeader.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/RegRequestHeader.java similarity index 83% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/RegRequestHeader.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/RegRequestHeader.java index 1ed7bc3643..0ee7cab899 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/RegRequestHeader.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/RegRequestHeader.java @@ -15,18 +15,18 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.header.client; - -import com.webank.eventmesh.common.Constants; -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import com.webank.eventmesh.common.protocol.http.common.ProtocolVersion; -import com.webank.eventmesh.common.protocol.http.header.Header; -import org.apache.commons.collections4.MapUtils; -import org.apache.commons.lang3.StringUtils; +package org.apache.eventmesh.common.protocol.http.header.client; import java.util.HashMap; import java.util.Map; +import org.apache.commons.collections4.MapUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; +import org.apache.eventmesh.common.protocol.http.common.ProtocolVersion; +import org.apache.eventmesh.common.protocol.http.header.Header; + public class RegRequestHeader extends Header { private String code; @@ -37,12 +37,8 @@ public class RegRequestHeader extends Header { private String env; - private String region; - private String idc; - private String dcn; - private String sys; private String pid; @@ -61,9 +57,7 @@ public static RegRequestHeader buildHeader(Map headerParam) { ? Constants.LANGUAGE_JAVA : MapUtils.getString(headerParam, ProtocolKey.LANGUAGE); header.setLanguage(lan); header.setEnv(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.ENV)); - header.setRegion(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.REGION)); header.setIdc(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.IDC)); - header.setDcn(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.DCN)); header.setSys(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.SYS)); header.setPid(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.PID)); header.setIp(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.IP)); @@ -104,14 +98,6 @@ public void setEnv(String env) { this.env = env; } - public String getRegion() { - return region; - } - - public void setRegion(String region) { - this.region = region; - } - public String getIdc() { return idc; } @@ -120,14 +106,6 @@ public void setIdc(String idc) { this.idc = idc; } - public String getDcn() { - return dcn; - } - - public void setDcn(String dcn) { - this.dcn = dcn; - } - public String getSys() { return sys; } @@ -175,9 +153,7 @@ public Map toMap() { map.put(ProtocolKey.LANGUAGE, language); map.put(ProtocolKey.VERSION, version); map.put(ProtocolKey.ClientInstanceKey.ENV, env); - map.put(ProtocolKey.ClientInstanceKey.REGION, region); map.put(ProtocolKey.ClientInstanceKey.IDC, idc); - map.put(ProtocolKey.ClientInstanceKey.DCN, dcn); map.put(ProtocolKey.ClientInstanceKey.SYS, sys); map.put(ProtocolKey.ClientInstanceKey.PID, pid); map.put(ProtocolKey.ClientInstanceKey.IP, ip); @@ -194,9 +170,7 @@ public String toString() { .append("language=").append(language).append(",") .append("version=").append(version).append(",") .append("env=").append(env).append(",") - .append("region=").append(region).append(",") .append("idc=").append(idc).append(",") - .append("dcn=").append(dcn).append(",") .append("sys=").append(sys).append(",") .append("pid=").append(pid).append(",") .append("ip=").append(ip).append(",") diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/RegResponseHeader.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/RegResponseHeader.java new file mode 100644 index 0000000000..503a527ea1 --- /dev/null +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/RegResponseHeader.java @@ -0,0 +1,118 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.common.protocol.http.header.client; + + +import java.util.HashMap; +import java.util.Map; + +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; +import org.apache.eventmesh.common.protocol.http.header.Header; + +public class RegResponseHeader extends Header { + + //响应码, 与对应Request的code一致 + private int code; + + //处理该次Request请求的eventMesh的集群名 + private String eventMeshCluster; + + //处理该次Request请求的eventMesh的IP + private String eventMeshIp; + + //处理该次Request请求的eventMesh所在的环境编号 + private String eventMeshEnv; + + //处理该次Request请求的eventMesh所在IDC + private String eventMeshIdc; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getEventMeshCluster() { + return eventMeshCluster; + } + + public void setEventMeshCluster(String eventMeshCluster) { + this.eventMeshCluster = eventMeshCluster; + } + + public String getEventMeshIp() { + return eventMeshIp; + } + + public void setEventMeshIp(String eventMeshIp) { + this.eventMeshIp = eventMeshIp; + } + + public String getEventMeshEnv() { + return eventMeshEnv; + } + + public void setEventMeshEnv(String eventMeshEnv) { + this.eventMeshEnv = eventMeshEnv; + } + + public String getEventMeshIdc() { + return eventMeshIdc; + } + + public void setEventMeshIdc(String eventMeshIdc) { + this.eventMeshIdc = eventMeshIdc; + } + + public static RegResponseHeader buildHeader(Integer requestCode, String eventMeshCluster, + String eventMeshIp, String eventMeshEnv, String eventMeshIDC) { + RegResponseHeader regResponseHeader = new RegResponseHeader(); + regResponseHeader.setCode(requestCode); + regResponseHeader.setEventMeshCluster(eventMeshCluster); + regResponseHeader.setEventMeshIp(eventMeshIp); + regResponseHeader.setEventMeshEnv(eventMeshEnv); + regResponseHeader.setEventMeshIdc(eventMeshIDC); + return regResponseHeader; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("regResponseHeader={") + .append("code=").append(code).append(",") + .append("eventMeshEnv=").append(eventMeshEnv).append(",") + .append("eventMeshIdc=").append(eventMeshIdc).append(",") + .append("eventMeshCluster=").append(eventMeshCluster).append(",") + .append("eventMeshIp=").append(eventMeshIp).append("}"); + return sb.toString(); + } + + @Override + public Map toMap() { + Map map = new HashMap(); + map.put(ProtocolKey.REQUEST_CODE, code); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHCLUSTER, eventMeshCluster); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHIP, eventMeshIp); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHENV, eventMeshEnv); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHIDC, eventMeshIdc); + return map; + } + +} diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/SubscribeRequestHeader.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/SubscribeRequestHeader.java new file mode 100644 index 0000000000..6143e4b4bc --- /dev/null +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/SubscribeRequestHeader.java @@ -0,0 +1,181 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.common.protocol.http.header.client; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.collections4.MapUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; +import org.apache.eventmesh.common.protocol.http.common.ProtocolVersion; +import org.apache.eventmesh.common.protocol.http.header.Header; + +public class SubscribeRequestHeader extends Header { + + private String code; + + private String language; + + private ProtocolVersion version; + + private String env; + + private String idc; + + private String sys; + + private String pid; + + private String ip; + + private String username = "username"; + + private String passwd = "user@123"; + + public static SubscribeRequestHeader buildHeader(Map headerParam) { + SubscribeRequestHeader header = new SubscribeRequestHeader(); + header.setCode(MapUtils.getString(headerParam, ProtocolKey.REQUEST_CODE)); + header.setVersion(ProtocolVersion.get(MapUtils.getString(headerParam, ProtocolKey.VERSION))); + String lan = StringUtils.isBlank(MapUtils.getString(headerParam, ProtocolKey.LANGUAGE)) + ? Constants.LANGUAGE_JAVA : MapUtils.getString(headerParam, ProtocolKey.LANGUAGE); + header.setLanguage(lan); + header.setEnv(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.ENV)); + header.setIdc(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.IDC)); + header.setSys(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.SYS)); + header.setPid(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.PID)); + header.setIp(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.IP)); + header.setUsername(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.USERNAME)); + header.setPasswd(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.PASSWD)); + return header; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getLanguage() { + return language; + } + + public void setLanguage(String language) { + this.language = language; + } + + public ProtocolVersion getVersion() { + return version; + } + + public void setVersion(ProtocolVersion version) { + this.version = version; + } + + public String getEnv() { + return env; + } + + public void setEnv(String env) { + this.env = env; + } + + public String getIdc() { + return idc; + } + + public void setIdc(String idc) { + this.idc = idc; + } + + public String getSys() { + return sys; + } + + public void setSys(String sys) { + this.sys = sys; + } + + public String getPid() { + return pid; + } + + public void setPid(String pid) { + this.pid = pid; + } + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPasswd() { + return passwd; + } + + public void setPasswd(String passwd) { + this.passwd = passwd; + } + + @Override + public Map toMap() { + Map map = new HashMap(); + map.put(ProtocolKey.REQUEST_CODE, code); + map.put(ProtocolKey.LANGUAGE, language); + map.put(ProtocolKey.VERSION, version); + map.put(ProtocolKey.ClientInstanceKey.ENV, env); + map.put(ProtocolKey.ClientInstanceKey.IDC, idc); + map.put(ProtocolKey.ClientInstanceKey.SYS, sys); + map.put(ProtocolKey.ClientInstanceKey.PID, pid); + map.put(ProtocolKey.ClientInstanceKey.IP, ip); + map.put(ProtocolKey.ClientInstanceKey.USERNAME, username); + map.put(ProtocolKey.ClientInstanceKey.PASSWD, passwd); + return map; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("subscribeRequestHeader={") + .append("code=").append(code).append(",") + .append("language=").append(language).append(",") + .append("version=").append(version).append(",") + .append("env=").append(env).append(",") + .append("idc=").append(idc).append(",") + .append("sys=").append(sys).append(",") + .append("pid=").append(pid).append(",") + .append("ip=").append(ip).append(",") + .append("username=").append(username).append(",") + .append("passwd=").append(passwd).append("}"); + return sb.toString(); + } +} diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/SubscribeResponseHeader.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/SubscribeResponseHeader.java new file mode 100644 index 0000000000..f21fbceb89 --- /dev/null +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/SubscribeResponseHeader.java @@ -0,0 +1,113 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.common.protocol.http.header.client; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; +import org.apache.eventmesh.common.protocol.http.header.Header; + +public class SubscribeResponseHeader extends Header { + + private int code; + + private String eventMeshCluster; + + private String eventMeshIp; + + private String eventMeshEnv; + + private String eventMeshIdc; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getEventMeshCluster() { + return eventMeshCluster; + } + + public void setEventMeshCluster(String eventMeshCluster) { + this.eventMeshCluster = eventMeshCluster; + } + + public String getEventMeshIp() { + return eventMeshIp; + } + + public void setEventMeshIp(String eventMeshIp) { + this.eventMeshIp = eventMeshIp; + } + + public String getEventMeshEnv() { + return eventMeshEnv; + } + + public void setEventMeshEnv(String eventMeshEnv) { + this.eventMeshEnv = eventMeshEnv; + } + + public String getEventMeshIdc() { + return eventMeshIdc; + } + + public void setEventMeshIdc(String eventMeshIdc) { + this.eventMeshIdc = eventMeshIdc; + } + + public static SubscribeResponseHeader buildHeader(Integer requestCode, String eventMeshCluster, + String eventMeshIp, String eventMeshEnv, + String eventMeshIDC) { + SubscribeResponseHeader subscribeResponseHeader = new SubscribeResponseHeader(); + subscribeResponseHeader.setCode(requestCode); + subscribeResponseHeader.setEventMeshCluster(eventMeshCluster); + subscribeResponseHeader.setEventMeshIp(eventMeshIp); + subscribeResponseHeader.setEventMeshEnv(eventMeshEnv); + subscribeResponseHeader.setEventMeshIdc(eventMeshIDC); + return subscribeResponseHeader; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("subscribeResponseHeader={") + .append("code=").append(code).append(",") + .append("eventMeshEnv=").append(eventMeshEnv).append(",") + .append("eventMeshIdc=").append(eventMeshIdc).append(",") + .append("eventMeshCluster=").append(eventMeshCluster).append(",") + .append("eventMeshIp=").append(eventMeshIp).append("}"); + return sb.toString(); + } + + @Override + public Map toMap() { + Map map = new HashMap(); + map.put(ProtocolKey.REQUEST_CODE, code); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHCLUSTER, eventMeshCluster); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHIP, eventMeshIp); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHENV, eventMeshEnv); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHIDC, eventMeshIdc); + return map; + } + +} diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/UnRegRequestHeader.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/UnRegRequestHeader.java similarity index 83% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/UnRegRequestHeader.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/UnRegRequestHeader.java index ce6fb8e22c..09c3a6d378 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/UnRegRequestHeader.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/UnRegRequestHeader.java @@ -15,18 +15,18 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.header.client; - -import com.webank.eventmesh.common.Constants; -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import com.webank.eventmesh.common.protocol.http.common.ProtocolVersion; -import com.webank.eventmesh.common.protocol.http.header.Header; -import org.apache.commons.collections4.MapUtils; -import org.apache.commons.lang3.StringUtils; +package org.apache.eventmesh.common.protocol.http.header.client; import java.util.HashMap; import java.util.Map; +import org.apache.commons.collections4.MapUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; +import org.apache.eventmesh.common.protocol.http.common.ProtocolVersion; +import org.apache.eventmesh.common.protocol.http.header.Header; + public class UnRegRequestHeader extends Header { //请求码 @@ -41,15 +41,9 @@ public class UnRegRequestHeader extends Header { //请求方所在环境编号 private String env; - //请求方所在区域编码 - private String region; - //请求方所在IDC private String idc; - //请求方所在DCN - private String dcn; - //请求方的子系统 private String sys; @@ -73,9 +67,7 @@ public static UnRegRequestHeader buildHeader(Map headerParam) { ? Constants.LANGUAGE_JAVA : MapUtils.getString(headerParam, ProtocolKey.LANGUAGE); header.setLanguage(lan); header.setEnv(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.ENV)); - header.setRegion(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.REGION)); header.setIdc(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.IDC)); - header.setDcn(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.DCN)); header.setSys(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.SYS)); header.setPid(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.PID)); header.setIp(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.IP)); @@ -116,14 +108,6 @@ public void setEnv(String env) { this.env = env; } - public String getRegion() { - return region; - } - - public void setRegion(String region) { - this.region = region; - } - public String getIdc() { return idc; } @@ -132,14 +116,6 @@ public void setIdc(String idc) { this.idc = idc; } - public String getDcn() { - return dcn; - } - - public void setDcn(String dcn) { - this.dcn = dcn; - } - public String getSys() { return sys; } @@ -187,9 +163,7 @@ public Map toMap() { map.put(ProtocolKey.LANGUAGE, language); map.put(ProtocolKey.VERSION, version); map.put(ProtocolKey.ClientInstanceKey.ENV, env); - map.put(ProtocolKey.ClientInstanceKey.REGION, region); map.put(ProtocolKey.ClientInstanceKey.IDC, idc); - map.put(ProtocolKey.ClientInstanceKey.DCN, dcn); map.put(ProtocolKey.ClientInstanceKey.SYS, sys); map.put(ProtocolKey.ClientInstanceKey.PID, pid); map.put(ProtocolKey.ClientInstanceKey.IP, ip); @@ -206,9 +180,7 @@ public String toString() { .append("language=").append(language).append(",") .append("version=").append(version).append(",") .append("env=").append(env).append(",") - .append("region=").append(region).append(",") .append("idc=").append(idc).append(",") - .append("dcn=").append(dcn).append(",") .append("sys=").append(sys).append(",") .append("pid=").append(pid).append(",") .append("ip=").append(ip).append(",") diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/UnRegResponseHeader.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/UnRegResponseHeader.java new file mode 100644 index 0000000000..4338c0305f --- /dev/null +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/UnRegResponseHeader.java @@ -0,0 +1,113 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.common.protocol.http.header.client; + + +import java.util.HashMap; +import java.util.Map; + +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; +import org.apache.eventmesh.common.protocol.http.header.Header; + +public class UnRegResponseHeader extends Header { + + private int code; + + private String eventMeshCluster; + + private String eventMeshIp; + + private String eventMeshEnv; + + private String eventMeshIdc; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getEventMeshCluster() { + return eventMeshCluster; + } + + public void setEventMeshCluster(String eventMeshCluster) { + this.eventMeshCluster = eventMeshCluster; + } + + public String getEventMeshIp() { + return eventMeshIp; + } + + public void setEventMeshIp(String eventMeshIp) { + this.eventMeshIp = eventMeshIp; + } + + public String getEventMeshEnv() { + return eventMeshEnv; + } + + public void setEventMeshEnv(String eventMeshEnv) { + this.eventMeshEnv = eventMeshEnv; + } + + public String getEventMeshIdc() { + return eventMeshIdc; + } + + public void setEventMeshIdc(String eventMeshIdc) { + this.eventMeshIdc = eventMeshIdc; + } + + public static UnRegResponseHeader buildHeader(Integer requestCode, String eventMeshCluster, + String eventMeshIp, String eventMeshEnv, String eventMeshIDC) { + UnRegResponseHeader regResponseHeader = new UnRegResponseHeader(); + regResponseHeader.setCode(requestCode); + regResponseHeader.setEventMeshCluster(eventMeshCluster); + regResponseHeader.setEventMeshIp(eventMeshIp); + regResponseHeader.setEventMeshEnv(eventMeshEnv); + regResponseHeader.setEventMeshIdc(eventMeshIDC); + return regResponseHeader; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("nnRegResponseHeader={") + .append("code=").append(code).append(",") + .append("eventMeshEnv=").append(eventMeshEnv).append(",") + .append("eventMeshIdc=").append(eventMeshIdc).append(",") + .append("eventMeshCluster=").append(eventMeshCluster).append(",") + .append("eventMeshIp=").append(eventMeshIp).append("}"); + return sb.toString(); + } + + @Override + public Map toMap() { + Map map = new HashMap(); + map.put(ProtocolKey.REQUEST_CODE, code); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHCLUSTER, eventMeshCluster); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHIP, eventMeshIp); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHENV, eventMeshEnv); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHIDC, eventMeshIdc); + return map; + } + +} diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/UnSubscribeRequestHeader.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/UnSubscribeRequestHeader.java new file mode 100644 index 0000000000..f5b34564ac --- /dev/null +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/UnSubscribeRequestHeader.java @@ -0,0 +1,181 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.common.protocol.http.header.client; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.collections4.MapUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; +import org.apache.eventmesh.common.protocol.http.common.ProtocolVersion; +import org.apache.eventmesh.common.protocol.http.header.Header; + +public class UnSubscribeRequestHeader extends Header { + + private String code; + + private String language; + + private ProtocolVersion version; + + private String env; + + private String idc; + + private String sys; + + private String pid; + + private String ip; + + private String username = "username"; + + private String passwd = "user@123"; + + public static UnSubscribeRequestHeader buildHeader(Map headerParam) { + UnSubscribeRequestHeader header = new UnSubscribeRequestHeader(); + header.setCode(MapUtils.getString(headerParam, ProtocolKey.REQUEST_CODE)); + header.setVersion(ProtocolVersion.get(MapUtils.getString(headerParam, ProtocolKey.VERSION))); + String lan = StringUtils.isBlank(MapUtils.getString(headerParam, ProtocolKey.LANGUAGE)) + ? Constants.LANGUAGE_JAVA : MapUtils.getString(headerParam, ProtocolKey.LANGUAGE); + header.setLanguage(lan); + header.setEnv(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.ENV)); + header.setIdc(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.IDC)); + header.setSys(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.SYS)); + header.setPid(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.PID)); + header.setIp(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.IP)); + header.setUsername(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.USERNAME)); + header.setPasswd(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.PASSWD)); + return header; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getLanguage() { + return language; + } + + public void setLanguage(String language) { + this.language = language; + } + + public ProtocolVersion getVersion() { + return version; + } + + public void setVersion(ProtocolVersion version) { + this.version = version; + } + + public String getEnv() { + return env; + } + + public void setEnv(String env) { + this.env = env; + } + + public String getIdc() { + return idc; + } + + public void setIdc(String idc) { + this.idc = idc; + } + + public String getSys() { + return sys; + } + + public void setSys(String sys) { + this.sys = sys; + } + + public String getPid() { + return pid; + } + + public void setPid(String pid) { + this.pid = pid; + } + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPasswd() { + return passwd; + } + + public void setPasswd(String passwd) { + this.passwd = passwd; + } + + @Override + public Map toMap() { + Map map = new HashMap(); + map.put(ProtocolKey.REQUEST_CODE, code); + map.put(ProtocolKey.LANGUAGE, language); + map.put(ProtocolKey.VERSION, version); + map.put(ProtocolKey.ClientInstanceKey.ENV, env); + map.put(ProtocolKey.ClientInstanceKey.IDC, idc); + map.put(ProtocolKey.ClientInstanceKey.SYS, sys); + map.put(ProtocolKey.ClientInstanceKey.PID, pid); + map.put(ProtocolKey.ClientInstanceKey.IP, ip); + map.put(ProtocolKey.ClientInstanceKey.USERNAME, username); + map.put(ProtocolKey.ClientInstanceKey.PASSWD, passwd); + return map; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("subscribeRequestHeader={") + .append("code=").append(code).append(",") + .append("language=").append(language).append(",") + .append("version=").append(version).append(",") + .append("env=").append(env).append(",") + .append("idc=").append(idc).append(",") + .append("sys=").append(sys).append(",") + .append("pid=").append(pid).append(",") + .append("ip=").append(ip).append(",") + .append("username=").append(username).append(",") + .append("passwd=").append(passwd).append("}"); + return sb.toString(); + } +} diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/UnSubscribeResponseHeader.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/UnSubscribeResponseHeader.java new file mode 100644 index 0000000000..f2638234d2 --- /dev/null +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/client/UnSubscribeResponseHeader.java @@ -0,0 +1,111 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.common.protocol.http.header.client; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; +import org.apache.eventmesh.common.protocol.http.header.Header; + +public class UnSubscribeResponseHeader extends Header { + + private int code; + + private String eventMeshCluster; + + private String eventMeshIp; + + private String eventMeshEnv; + + private String eventMeshIdc; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getEventMeshCluster() { + return eventMeshCluster; + } + + public void setEventMeshCluster(String eventMeshCluster) { + this.eventMeshCluster = eventMeshCluster; + } + + public String getEventMeshIp() { + return eventMeshIp; + } + + public void setEventMeshIp(String eventMeshIp) { + this.eventMeshIp = eventMeshIp; + } + + public String getEventMeshEnv() { + return eventMeshEnv; + } + + public void setEventMeshEnv(String eventMeshEnv) { + this.eventMeshEnv = eventMeshEnv; + } + + public String getEventMeshIdc() { + return eventMeshIdc; + } + + public void setEventMeshIdc(String eventMeshIdc) { + this.eventMeshIdc = eventMeshIdc; + } + + public static UnSubscribeResponseHeader buildHeader(Integer requestCode, String eventMeshCluster, + String eventMeshIp, String eventMeshEnv, String eventMeshIDC) { + UnSubscribeResponseHeader unSubscribeResponseHeader = new UnSubscribeResponseHeader(); + unSubscribeResponseHeader.setCode(requestCode); + unSubscribeResponseHeader.setEventMeshCluster(eventMeshCluster); + unSubscribeResponseHeader.setEventMeshIp(eventMeshIp); + unSubscribeResponseHeader.setEventMeshEnv(eventMeshEnv); + unSubscribeResponseHeader.setEventMeshIdc(eventMeshIDC); + return unSubscribeResponseHeader; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("unSubscribeResponseHeader={") + .append("code=").append(code).append(",") + .append("eventMeshEnv=").append(eventMeshEnv).append(",") + .append("eventMeshIdc=").append(eventMeshIdc).append(",") + .append("eventMeshCluster=").append(eventMeshCluster).append(",") + .append("eventMeshIp=").append(eventMeshIp).append("}"); + return sb.toString(); + } + + @Override + public Map toMap() { + Map map = new HashMap(); + map.put(ProtocolKey.REQUEST_CODE, code); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHCLUSTER, eventMeshCluster); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHIP, eventMeshIp); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHENV, eventMeshEnv); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHIDC, eventMeshIdc); + return map; + } +} diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/PushMessageRequestHeader.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/PushMessageRequestHeader.java similarity index 50% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/PushMessageRequestHeader.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/PushMessageRequestHeader.java index 9a885b68db..18f6cfc659 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/PushMessageRequestHeader.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/PushMessageRequestHeader.java @@ -15,18 +15,18 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.header.message; +package org.apache.eventmesh.common.protocol.http.header.message; -import com.webank.eventmesh.common.Constants; -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import com.webank.eventmesh.common.protocol.http.common.ProtocolVersion; -import com.webank.eventmesh.common.protocol.http.header.Header; -import org.apache.commons.collections4.MapUtils; - import java.util.HashMap; import java.util.Map; +import org.apache.commons.collections4.MapUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; +import org.apache.eventmesh.common.protocol.http.common.ProtocolVersion; +import org.apache.eventmesh.common.protocol.http.header.Header; + public class PushMessageRequestHeader extends Header { //请求码 @@ -38,17 +38,13 @@ public class PushMessageRequestHeader extends Header { //请求方采用的协议版本, 默认1.0 private ProtocolVersion version; - private String proxyCluster; + private String eventMeshCluster; - private String proxyIp; + private String eventMeshIp; - private String proxyEnv; + private String eventMeshEnv; - private String proxyRegion; - - private String proxyIdc; - - private String proxyDcn; + private String eventMeshIdc; public int getCode() { return code; @@ -58,52 +54,36 @@ public void setCode(int code) { this.code = code; } - public String getProxyCluster() { - return proxyCluster; - } - - public void setProxyCluster(String proxyCluster) { - this.proxyCluster = proxyCluster; - } - - public String getProxyIp() { - return proxyIp; - } - - public void setProxyIp(String proxyIp) { - this.proxyIp = proxyIp; - } - - public String getProxyEnv() { - return proxyEnv; + public String getEventMeshCluster() { + return eventMeshCluster; } - public void setProxyEnv(String proxyEnv) { - this.proxyEnv = proxyEnv; + public void setEventMeshCluster(String eventMeshCluster) { + this.eventMeshCluster = eventMeshCluster; } - public String getProxyRegion() { - return proxyRegion; + public String getEventMeshIp() { + return eventMeshIp; } - public void setProxyRegion(String proxyRegion) { - this.proxyRegion = proxyRegion; + public void setEventMeshIp(String eventMeshIp) { + this.eventMeshIp = eventMeshIp; } - public String getProxyIdc() { - return proxyIdc; + public String getEventMeshEnv() { + return eventMeshEnv; } - public void setProxyIdc(String proxyIdc) { - this.proxyIdc = proxyIdc; + public void setEventMeshEnv(String eventMeshEnv) { + this.eventMeshEnv = eventMeshEnv; } - public String getProxyDcn() { - return proxyDcn; + public String getEventMeshIdc() { + return eventMeshIdc; } - public void setProxyDcn(String proxyDcn) { - this.proxyDcn = proxyDcn; + public void setEventMeshIdc(String eventMeshIdc) { + this.eventMeshIdc = eventMeshIdc; } public String getLanguage() { @@ -127,12 +107,10 @@ public static PushMessageRequestHeader buildHeader(final Map hea pushMessageRequestHeader.setCode(MapUtils.getIntValue(headerParam, ProtocolKey.REQUEST_CODE)); pushMessageRequestHeader.setLanguage(MapUtils.getString(headerParam, ProtocolKey.LANGUAGE, Constants.LANGUAGE_JAVA)); pushMessageRequestHeader.setVersion(ProtocolVersion.get(MapUtils.getString(headerParam, ProtocolKey.VERSION))); - pushMessageRequestHeader.setProxyCluster(MapUtils.getString(headerParam, ProtocolKey.ProxyInstanceKey.PROXYCLUSTER)); - pushMessageRequestHeader.setProxyIp(MapUtils.getString(headerParam, ProtocolKey.ProxyInstanceKey.PROXYIP)); - pushMessageRequestHeader.setProxyDcn(MapUtils.getString(headerParam, ProtocolKey.ProxyInstanceKey.PROXYDCN)); - pushMessageRequestHeader.setProxyEnv(MapUtils.getString(headerParam, ProtocolKey.ProxyInstanceKey.PROXYENV)); - pushMessageRequestHeader.setProxyRegion(MapUtils.getString(headerParam, ProtocolKey.ProxyInstanceKey.PROXYREGION)); - pushMessageRequestHeader.setProxyIdc(MapUtils.getString(headerParam, ProtocolKey.ProxyInstanceKey.PROXYIDC)); + pushMessageRequestHeader.setEventMeshCluster(MapUtils.getString(headerParam, ProtocolKey.EventMeshInstanceKey.EVENTMESHCLUSTER)); + pushMessageRequestHeader.setEventMeshIp(MapUtils.getString(headerParam, ProtocolKey.EventMeshInstanceKey.EVENTMESHIP)); + pushMessageRequestHeader.setEventMeshEnv(MapUtils.getString(headerParam, ProtocolKey.EventMeshInstanceKey.EVENTMESHENV)); + pushMessageRequestHeader.setEventMeshIdc(MapUtils.getString(headerParam, ProtocolKey.EventMeshInstanceKey.EVENTMESHIDC)); return pushMessageRequestHeader; } @@ -142,12 +120,10 @@ public Map toMap() { map.put(ProtocolKey.REQUEST_CODE, code); map.put(ProtocolKey.LANGUAGE, language); map.put(ProtocolKey.VERSION, version); - map.put(ProtocolKey.ProxyInstanceKey.PROXYCLUSTER, proxyCluster); - map.put(ProtocolKey.ProxyInstanceKey.PROXYIP, proxyIp); - map.put(ProtocolKey.ProxyInstanceKey.PROXYENV, proxyEnv); - map.put(ProtocolKey.ProxyInstanceKey.PROXYREGION, proxyRegion); - map.put(ProtocolKey.ProxyInstanceKey.PROXYIDC, proxyIdc); - map.put(ProtocolKey.ProxyInstanceKey.PROXYDCN, proxyDcn); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHCLUSTER, eventMeshCluster); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHIP, eventMeshIp); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHENV, eventMeshEnv); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHIDC, eventMeshIdc); return map; } @@ -158,12 +134,10 @@ public String toString() { .append("code=").append(code).append(",") .append("language=").append(language).append(",") .append("version=").append(version.getVersion()).append(",") - .append("proxyEnv=").append(proxyEnv).append(",") - .append("proxyRegion=").append(proxyRegion).append(",") - .append("proxyIdc=").append(proxyIdc).append(",") - .append("proxyDcn=").append(proxyDcn).append(",") - .append("proxyCluster=").append(proxyCluster).append(",") - .append("proxyIp=").append(proxyIp).append("}"); + .append("eventMeshEnv=").append(eventMeshEnv).append(",") + .append("eventMeshIdc=").append(eventMeshIdc).append(",") + .append("eventMeshCluster=").append(eventMeshCluster).append(",") + .append("eventMeshIp=").append(eventMeshIp).append("}"); return sb.toString(); } diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/PushMessageResponseHeader.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/PushMessageResponseHeader.java similarity index 81% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/PushMessageResponseHeader.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/PushMessageResponseHeader.java index fa0af32fa5..2f8fc63f70 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/PushMessageResponseHeader.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/PushMessageResponseHeader.java @@ -15,18 +15,17 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.header.message; +package org.apache.eventmesh.common.protocol.http.header.message; - -import com.webank.eventmesh.common.Constants; -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import com.webank.eventmesh.common.protocol.http.common.ProtocolVersion; -import com.webank.eventmesh.common.protocol.http.header.Header; - import java.util.HashMap; import java.util.Map; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; +import org.apache.eventmesh.common.protocol.http.common.ProtocolVersion; +import org.apache.eventmesh.common.protocol.http.header.Header; + public class PushMessageResponseHeader extends Header { //响应码 @@ -41,15 +40,9 @@ public class PushMessageResponseHeader extends Header { //请求方所在环境编号 private String env; - //请求方所在区域编码 - private String region; - //请求方所在IDC private String idc; - //请求方所在DCN - private String dcn; - //请求方的子系统 private String sys; @@ -113,14 +106,6 @@ public void setEnv(String env) { this.env = env; } - public String getRegion() { - return region; - } - - public void setRegion(String region) { - this.region = region; - } - public String getIdc() { return idc; } @@ -129,14 +114,6 @@ public void setIdc(String idc) { this.idc = idc; } - public String getDcn() { - return dcn; - } - - public void setDcn(String dcn) { - this.dcn = dcn; - } - public String getSys() { return sys; } @@ -161,16 +138,14 @@ public void setIp(String ip) { this.ip = ip; } - public static PushMessageResponseHeader buildHeader(int requestCode, String clientEnv, String clientRegion, String clientIDC, - String clientDCN, String clientSysId, String clientPid, String clientIP) { + public static PushMessageResponseHeader buildHeader(int requestCode, String clientEnv, String clientIDC, + String clientSysId, String clientPid, String clientIP) { PushMessageResponseHeader pushMessageResponseHeader = new PushMessageResponseHeader(); pushMessageResponseHeader.setCode(requestCode); pushMessageResponseHeader.setVersion(ProtocolVersion.V1); pushMessageResponseHeader.setLanguage(Constants.LANGUAGE_JAVA); pushMessageResponseHeader.setEnv(clientEnv); - pushMessageResponseHeader.setRegion(clientRegion); pushMessageResponseHeader.setIdc(clientIDC); - pushMessageResponseHeader.setDcn(clientDCN); pushMessageResponseHeader.setSys(clientSysId); pushMessageResponseHeader.setPid(clientPid); pushMessageResponseHeader.setIp(clientIP); @@ -185,9 +160,7 @@ public String toString() { .append("language=").append(language).append(",") .append("version=").append(version).append(",") .append("env=").append(env).append(",") - .append("region=").append(region).append(",") .append("idc=").append(idc).append(",") - .append("dcn=").append(dcn).append(",") .append("sys=").append(sys).append(",") .append("pid=").append(pid).append(",") .append("ip=").append(ip).append(",") @@ -203,9 +176,7 @@ public Map toMap() { map.put(ProtocolKey.LANGUAGE, language); map.put(ProtocolKey.VERSION, version); map.put(ProtocolKey.ClientInstanceKey.ENV, env); - map.put(ProtocolKey.ClientInstanceKey.REGION, region); map.put(ProtocolKey.ClientInstanceKey.IDC, idc); - map.put(ProtocolKey.ClientInstanceKey.DCN, dcn); map.put(ProtocolKey.ClientInstanceKey.SYS, sys); map.put(ProtocolKey.ClientInstanceKey.PID, pid); map.put(ProtocolKey.ClientInstanceKey.IP, ip); diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/ReplyMessageRequestHeader.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/ReplyMessageRequestHeader.java similarity index 83% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/ReplyMessageRequestHeader.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/ReplyMessageRequestHeader.java index 0a22a8b071..e7d347efa0 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/ReplyMessageRequestHeader.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/ReplyMessageRequestHeader.java @@ -15,18 +15,18 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.header.message; - -import com.webank.eventmesh.common.Constants; -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import com.webank.eventmesh.common.protocol.http.common.ProtocolVersion; -import com.webank.eventmesh.common.protocol.http.header.Header; -import org.apache.commons.collections4.MapUtils; -import org.apache.commons.lang3.StringUtils; +package org.apache.eventmesh.common.protocol.http.header.message; import java.util.HashMap; import java.util.Map; +import org.apache.commons.collections4.MapUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; +import org.apache.eventmesh.common.protocol.http.common.ProtocolVersion; +import org.apache.eventmesh.common.protocol.http.header.Header; + public class ReplyMessageRequestHeader extends Header { //请求码 @@ -41,15 +41,9 @@ public class ReplyMessageRequestHeader extends Header { //请求方所在环境编号 private String env; - //请求方所在区域编码 - private String region; - //请求方所在IDC private String idc; - //请求方所在DCN - private String dcn; - //请求方的子系统 private String sys; @@ -113,14 +107,6 @@ public void setEnv(String env) { this.env = env; } - public String getRegion() { - return region; - } - - public void setRegion(String region) { - this.region = region; - } - public String getIdc() { return idc; } @@ -129,14 +115,6 @@ public void setIdc(String idc) { this.idc = idc; } - public String getDcn() { - return dcn; - } - - public void setDcn(String dcn) { - this.dcn = dcn; - } - public String getSys() { return sys; } @@ -169,9 +147,7 @@ public static ReplyMessageRequestHeader buildHeader(Map headerPa ? Constants.LANGUAGE_JAVA : MapUtils.getString(headerParam, ProtocolKey.LANGUAGE); header.setLanguage(lan); header.setEnv(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.ENV)); - header.setRegion(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.REGION)); header.setIdc(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.IDC)); - header.setDcn(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.DCN)); header.setSys(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.SYS)); header.setPid(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.PID)); header.setIp(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.IP)); @@ -187,9 +163,7 @@ public Map toMap() { map.put(ProtocolKey.LANGUAGE, language); map.put(ProtocolKey.VERSION, version); map.put(ProtocolKey.ClientInstanceKey.ENV, env); - map.put(ProtocolKey.ClientInstanceKey.REGION, region); map.put(ProtocolKey.ClientInstanceKey.IDC, idc); - map.put(ProtocolKey.ClientInstanceKey.DCN, dcn); map.put(ProtocolKey.ClientInstanceKey.SYS, sys); map.put(ProtocolKey.ClientInstanceKey.PID, pid); map.put(ProtocolKey.ClientInstanceKey.IP, ip); @@ -206,9 +180,7 @@ public String toString() { .append("language=").append(language).append(",") .append("version=").append(version).append(",") .append("env=").append(env).append(",") - .append("region=").append(region).append(",") .append("idc=").append(idc).append(",") - .append("dcn=").append(dcn).append(",") .append("sys=").append(sys).append(",") .append("pid=").append(pid).append(",") .append("ip=").append(ip).append(",") diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/ReplyMessageResponseHeader.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/ReplyMessageResponseHeader.java new file mode 100644 index 0000000000..33fd373251 --- /dev/null +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/ReplyMessageResponseHeader.java @@ -0,0 +1,118 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.common.protocol.http.header.message; + + +import java.util.HashMap; +import java.util.Map; + +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; +import org.apache.eventmesh.common.protocol.http.header.Header; + +public class ReplyMessageResponseHeader extends Header { + + //响应码, 与对应Request的code一致 + private int code; + + //处理该次Request请求的eventMesh的集群名 + private String eventMeshCluster; + + //处理该次Request请求的eventMesh的IP + private String eventMeshIp; + + //处理该次Request请求的eventMesh所在的环境编号 + private String eventMeshEnv; + + //处理该次Request请求的eventMesh所在IDC + private String eventMeshIdc; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getEventMeshCluster() { + return eventMeshCluster; + } + + public void setEventMeshCluster(String eventMeshCluster) { + this.eventMeshCluster = eventMeshCluster; + } + + public String getEventMeshIp() { + return eventMeshIp; + } + + public void setEventMeshIp(String eventMeshIp) { + this.eventMeshIp = eventMeshIp; + } + + public String getEventMeshEnv() { + return eventMeshEnv; + } + + public void setEventMeshEnv(String eventMeshEnv) { + this.eventMeshEnv = eventMeshEnv; + } + + public String getEventMeshIdc() { + return eventMeshIdc; + } + + public void setEventMeshIdc(String eventMeshIdc) { + this.eventMeshIdc = eventMeshIdc; + } + + public static ReplyMessageResponseHeader buildHeader(Integer requestCode, String eventMeshCluster, + String eventMeshIp, String eventMeshEnv, + String eventMeshIDC) { + ReplyMessageResponseHeader replyMessageResponseHeader = new ReplyMessageResponseHeader(); + replyMessageResponseHeader.setCode(requestCode); + replyMessageResponseHeader.setEventMeshCluster(eventMeshCluster); + replyMessageResponseHeader.setEventMeshIp(eventMeshIp); + replyMessageResponseHeader.setEventMeshEnv(eventMeshEnv); + replyMessageResponseHeader.setEventMeshIdc(eventMeshIDC); + return replyMessageResponseHeader; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("replyMessageResponseHeader={") + .append("code=").append(code).append(",") + .append("eventMeshEnv=").append(eventMeshEnv).append(",") + .append("eventMeshIdc=").append(eventMeshIdc).append(",") + .append("eventMeshCluster=").append(eventMeshCluster).append(",") + .append("eventMeshIp=").append(eventMeshIp).append("}"); + return sb.toString(); + } + + @Override + public Map toMap() { + Map map = new HashMap(); + map.put(ProtocolKey.REQUEST_CODE, code); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHCLUSTER, eventMeshCluster); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHIP, eventMeshIp); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHENV, eventMeshEnv); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHIDC, eventMeshIdc); + return map; + } +} diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/SendMessageBatchRequestHeader.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/SendMessageBatchRequestHeader.java similarity index 83% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/SendMessageBatchRequestHeader.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/SendMessageBatchRequestHeader.java index 041e6ed4ce..f981ceb2bf 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/SendMessageBatchRequestHeader.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/SendMessageBatchRequestHeader.java @@ -15,19 +15,19 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.header.message; +package org.apache.eventmesh.common.protocol.http.header.message; -import com.webank.eventmesh.common.Constants; -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import com.webank.eventmesh.common.protocol.http.common.ProtocolVersion; -import com.webank.eventmesh.common.protocol.http.header.Header; -import org.apache.commons.collections4.MapUtils; -import org.apache.commons.lang3.StringUtils; - import java.util.HashMap; import java.util.Map; +import org.apache.commons.collections4.MapUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; +import org.apache.eventmesh.common.protocol.http.common.ProtocolVersion; +import org.apache.eventmesh.common.protocol.http.header.Header; + public class SendMessageBatchRequestHeader extends Header { //请求码 @@ -42,15 +42,9 @@ public class SendMessageBatchRequestHeader extends Header { //请求方所在环境编号 private String env; - //请求方所在区域编码 - private String region; - //请求方所在IDC private String idc; - //请求方所在DCN - private String dcn; - //请求方的子系统 private String sys; @@ -114,14 +108,6 @@ public void setEnv(String env) { this.env = env; } - public String getRegion() { - return region; - } - - public void setRegion(String region) { - this.region = region; - } - public String getIdc() { return idc; } @@ -130,14 +116,6 @@ public void setIdc(String idc) { this.idc = idc; } - public String getDcn() { - return dcn; - } - - public void setDcn(String dcn) { - this.dcn = dcn; - } - public String getSys() { return sys; } @@ -170,9 +148,7 @@ public static SendMessageBatchRequestHeader buildHeader(final Map toMap() { map.put(ProtocolKey.LANGUAGE, language); map.put(ProtocolKey.VERSION, version); map.put(ProtocolKey.ClientInstanceKey.ENV, env); - map.put(ProtocolKey.ClientInstanceKey.REGION, region); map.put(ProtocolKey.ClientInstanceKey.IDC, idc); - map.put(ProtocolKey.ClientInstanceKey.DCN, dcn); map.put(ProtocolKey.ClientInstanceKey.SYS, sys); map.put(ProtocolKey.ClientInstanceKey.PID, pid); map.put(ProtocolKey.ClientInstanceKey.IP, ip); @@ -207,9 +181,7 @@ public String toString() { .append("language=").append(language).append(",") .append("version=").append(version).append(",") .append("env=").append(env).append(",") - .append("region=").append(region).append(",") .append("idc=").append(idc).append(",") - .append("dcn=").append(dcn).append(",") .append("sys=").append(sys).append(",") .append("pid=").append(pid).append(",") .append("ip=").append(ip).append(",") diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/SendMessageBatchResponseHeader.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/SendMessageBatchResponseHeader.java new file mode 100644 index 0000000000..d06660e5b5 --- /dev/null +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/SendMessageBatchResponseHeader.java @@ -0,0 +1,118 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.common.protocol.http.header.message; + + +import java.util.HashMap; +import java.util.Map; + +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; +import org.apache.eventmesh.common.protocol.http.header.Header; + +public class SendMessageBatchResponseHeader extends Header { + + //响应码, 与对应Request的code一致 + private int code; + + //处理该次Request请求的eventMesh的集群名 + private String eventMeshCluster; + + //处理该次Request请求的eventMesh的IP + private String eventMeshIp; + + //处理该次Request请求的eventMesh所在的环境编号 + private String eventMeshEnv; + + //处理该次Request请求的eventMesh所在IDC + private String eventMeshIdc; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getEventMeshCluster() { + return eventMeshCluster; + } + + public void setEventMeshCluster(String eventMeshCluster) { + this.eventMeshCluster = eventMeshCluster; + } + + public String getEventMeshIp() { + return eventMeshIp; + } + + public void setEventMeshIp(String eventMeshIp) { + this.eventMeshIp = eventMeshIp; + } + + public String getEventMeshEnv() { + return eventMeshEnv; + } + + public void setEventMeshEnv(String eventMeshEnv) { + this.eventMeshEnv = eventMeshEnv; + } + + public String getEventMeshIdc() { + return eventMeshIdc; + } + + public void setEventMeshIdc(String eventMeshIdc) { + this.eventMeshIdc = eventMeshIdc; + } + + public static SendMessageBatchResponseHeader buildHeader(Integer requestCode, String eventMeshCluster, + String eventMeshIp, String eventMeshEnv, + String eventMeshIDC) { + SendMessageBatchResponseHeader sendMessageBatchResponseHeader = new SendMessageBatchResponseHeader(); + sendMessageBatchResponseHeader.setCode(requestCode); + sendMessageBatchResponseHeader.setEventMeshCluster(eventMeshCluster); + sendMessageBatchResponseHeader.setEventMeshEnv(eventMeshEnv); + sendMessageBatchResponseHeader.setEventMeshIdc(eventMeshIDC); + sendMessageBatchResponseHeader.setEventMeshIp(eventMeshIp); + return sendMessageBatchResponseHeader; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("sendMessageBatchResponseHeader={") + .append("code=").append(code).append(",") + .append("eventMeshEnv=").append(eventMeshEnv).append(",") + .append("eventMeshIdc=").append(eventMeshIdc).append(",") + .append("eventMeshCluster=").append(eventMeshCluster).append(",") + .append("eventMeshIp=").append(eventMeshIp).append("}"); + return sb.toString(); + } + + @Override + public Map toMap() { + Map map = new HashMap(); + map.put(ProtocolKey.REQUEST_CODE, code); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHCLUSTER, eventMeshCluster); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHIP, eventMeshIp); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHENV, eventMeshEnv); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHIDC, eventMeshIdc); + return map; + } +} diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/SendMessageBatchV2RequestHeader.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/SendMessageBatchV2RequestHeader.java similarity index 83% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/SendMessageBatchV2RequestHeader.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/SendMessageBatchV2RequestHeader.java index 7a2972612b..1e7468e45d 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/SendMessageBatchV2RequestHeader.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/SendMessageBatchV2RequestHeader.java @@ -15,18 +15,18 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.header.message; - -import com.webank.eventmesh.common.Constants; -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import com.webank.eventmesh.common.protocol.http.common.ProtocolVersion; -import com.webank.eventmesh.common.protocol.http.header.Header; -import org.apache.commons.collections4.MapUtils; -import org.apache.commons.lang3.StringUtils; +package org.apache.eventmesh.common.protocol.http.header.message; import java.util.HashMap; import java.util.Map; +import org.apache.commons.collections4.MapUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; +import org.apache.eventmesh.common.protocol.http.common.ProtocolVersion; +import org.apache.eventmesh.common.protocol.http.header.Header; + public class SendMessageBatchV2RequestHeader extends Header { //请求码 @@ -41,15 +41,9 @@ public class SendMessageBatchV2RequestHeader extends Header { //请求方所在环境编号 private String env; - //请求方所在区域编码 - private String region; - //请求方所在IDC private String idc; - //请求方所在DCN - private String dcn; - //请求方的子系统 private String sys; @@ -113,14 +107,6 @@ public void setEnv(String env) { this.env = env; } - public String getRegion() { - return region; - } - - public void setRegion(String region) { - this.region = region; - } - public String getIdc() { return idc; } @@ -129,14 +115,6 @@ public void setIdc(String idc) { this.idc = idc; } - public String getDcn() { - return dcn; - } - - public void setDcn(String dcn) { - this.dcn = dcn; - } - public String getSys() { return sys; } @@ -169,9 +147,7 @@ public static SendMessageBatchV2RequestHeader buildHeader(final Map toMap() { map.put(ProtocolKey.LANGUAGE, language); map.put(ProtocolKey.VERSION, version); map.put(ProtocolKey.ClientInstanceKey.ENV, env); - map.put(ProtocolKey.ClientInstanceKey.REGION, region); map.put(ProtocolKey.ClientInstanceKey.IDC, idc); - map.put(ProtocolKey.ClientInstanceKey.DCN, dcn); map.put(ProtocolKey.ClientInstanceKey.SYS, sys); map.put(ProtocolKey.ClientInstanceKey.PID, pid); map.put(ProtocolKey.ClientInstanceKey.IP, ip); @@ -206,9 +180,7 @@ public String toString() { .append("language=").append(language).append(",") .append("version=").append(version).append(",") .append("env=").append(env).append(",") - .append("region=").append(region).append(",") .append("idc=").append(idc).append(",") - .append("dcn=").append(dcn).append(",") .append("sys=").append(sys).append(",") .append("pid=").append(pid).append(",") .append("ip=").append(ip).append(",") diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/SendMessageBatchV2ResponseHeader.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/SendMessageBatchV2ResponseHeader.java new file mode 100644 index 0000000000..1544381b04 --- /dev/null +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/SendMessageBatchV2ResponseHeader.java @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.common.protocol.http.header.message; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; +import org.apache.eventmesh.common.protocol.http.header.Header; + +public class SendMessageBatchV2ResponseHeader extends Header { + + //响应码, 与对应Request的code一致 + private int code; + + //处理该次Request请求的eventMesh的集群名 + private String eventMeshCluster; + + //处理该次Request请求的eventMesh的IP + private String eventMeshIp; + + //处理该次Request请求的eventMesh所在的环境编号 + private String eventMeshEnv; + + //处理该次Request请求的eventMesh所在IDC + private String eventMeshIdc; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getEventMeshCluster() { + return eventMeshCluster; + } + + public void setEventMeshCluster(String eventMeshCluster) { + this.eventMeshCluster = eventMeshCluster; + } + + public String getEventMeshIp() { + return eventMeshIp; + } + + public void setEventMeshIp(String eventMeshIp) { + this.eventMeshIp = eventMeshIp; + } + + public String getEventMeshEnv() { + return eventMeshEnv; + } + + public void setEventMeshEnv(String eventMeshEnv) { + this.eventMeshEnv = eventMeshEnv; + } + + public String getEventMeshIdc() { + return eventMeshIdc; + } + + public void setEventMeshIdc(String eventMeshIdc) { + this.eventMeshIdc = eventMeshIdc; + } + + public static SendMessageBatchV2ResponseHeader buildHeader(Integer requestCode, String eventMeshCluster, + String eventMeshIp, String eventMeshEnv, + String eventMeshIDC) { + SendMessageBatchV2ResponseHeader header = new SendMessageBatchV2ResponseHeader(); + header.setCode(requestCode); + header.setEventMeshCluster(eventMeshCluster); + header.setEventMeshEnv(eventMeshEnv); + header.setEventMeshIdc(eventMeshIDC); + header.setEventMeshIp(eventMeshIp); + return header; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("sendMessageBatchV2ResponseHeader={") + .append("code=").append(code).append(",") + .append("eventMeshEnv=").append(eventMeshEnv).append(",") + .append("eventMeshIdc=").append(eventMeshIdc).append(",") + .append("eventMeshCluster=").append(eventMeshCluster).append(",") + .append("eventMeshIp=").append(eventMeshIp).append("}"); + return sb.toString(); + } + + @Override + public Map toMap() { + Map map = new HashMap(); + map.put(ProtocolKey.REQUEST_CODE, code); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHCLUSTER, eventMeshCluster); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHIP, eventMeshIp); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHENV, eventMeshEnv); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHIDC, eventMeshIdc); + return map; + } +} diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/SendMessageRequestHeader.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/SendMessageRequestHeader.java similarity index 83% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/SendMessageRequestHeader.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/SendMessageRequestHeader.java index 515f0dfc3d..8512529baa 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/message/SendMessageRequestHeader.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/SendMessageRequestHeader.java @@ -15,18 +15,18 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.header.message; - -import com.webank.eventmesh.common.Constants; -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import com.webank.eventmesh.common.protocol.http.common.ProtocolVersion; -import com.webank.eventmesh.common.protocol.http.header.Header; -import org.apache.commons.collections4.MapUtils; -import org.apache.commons.lang3.StringUtils; +package org.apache.eventmesh.common.protocol.http.header.message; import java.util.HashMap; import java.util.Map; +import org.apache.commons.collections4.MapUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; +import org.apache.eventmesh.common.protocol.http.common.ProtocolVersion; +import org.apache.eventmesh.common.protocol.http.header.Header; + public class SendMessageRequestHeader extends Header { //请求码 @@ -41,15 +41,9 @@ public class SendMessageRequestHeader extends Header { //请求方所在环境编号 private String env; - //请求方所在区域编码 - private String region; - //请求方所在IDC private String idc; - //请求方所在DCN - private String dcn; - //请求方的子系统 private String sys; @@ -113,14 +107,6 @@ public void setEnv(String env) { this.env = env; } - public String getRegion() { - return region; - } - - public void setRegion(String region) { - this.region = region; - } - public String getIdc() { return idc; } @@ -129,14 +115,6 @@ public void setIdc(String idc) { this.idc = idc; } - public String getDcn() { - return dcn; - } - - public void setDcn(String dcn) { - this.dcn = dcn; - } - public String getSys() { return sys; } @@ -169,9 +147,7 @@ public static SendMessageRequestHeader buildHeader(Map headerPar ? Constants.LANGUAGE_JAVA : MapUtils.getString(headerParam, ProtocolKey.LANGUAGE); header.setLanguage(lan); header.setEnv(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.ENV)); - header.setRegion(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.REGION)); header.setIdc(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.IDC)); - header.setDcn(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.DCN)); header.setSys(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.SYS)); header.setPid(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.PID)); header.setIp(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.IP)); @@ -187,9 +163,7 @@ public Map toMap() { map.put(ProtocolKey.LANGUAGE, language); map.put(ProtocolKey.VERSION, version); map.put(ProtocolKey.ClientInstanceKey.ENV, env); - map.put(ProtocolKey.ClientInstanceKey.REGION, region); map.put(ProtocolKey.ClientInstanceKey.IDC, idc); - map.put(ProtocolKey.ClientInstanceKey.DCN, dcn); map.put(ProtocolKey.ClientInstanceKey.SYS, sys); map.put(ProtocolKey.ClientInstanceKey.PID, pid); map.put(ProtocolKey.ClientInstanceKey.IP, ip); @@ -206,9 +180,7 @@ public String toString() { .append("language=").append(language).append(",") .append("version=").append(version).append(",") .append("env=").append(env).append(",") - .append("region=").append(region).append(",") .append("idc=").append(idc).append(",") - .append("dcn=").append(dcn).append(",") .append("sys=").append(sys).append(",") .append("pid=").append(pid).append(",") .append("ip=").append(ip).append(",") diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/SendMessageResponseHeader.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/SendMessageResponseHeader.java new file mode 100644 index 0000000000..caa5eed8ad --- /dev/null +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/header/message/SendMessageResponseHeader.java @@ -0,0 +1,118 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.common.protocol.http.header.message; + + +import java.util.HashMap; +import java.util.Map; + +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; +import org.apache.eventmesh.common.protocol.http.header.Header; + +public class SendMessageResponseHeader extends Header { + + //响应码, 与对应Request的code一致 + private int code; + + //处理该次Request请求的eventMesh的集群名 + private String eventMeshCluster; + + //处理该次Request请求的eventMesh的IP + private String eventMeshIp; + + //处理该次Request请求的eventMesh所在的环境编号 + private String eventMeshEnv; + + //处理该次Request请求的eventMesh所在IDC + private String eventMeshIdc; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getEventMeshCluster() { + return eventMeshCluster; + } + + public void setEventMeshCluster(String eventMeshCluster) { + this.eventMeshCluster = eventMeshCluster; + } + + public String getEventMeshIp() { + return eventMeshIp; + } + + public void setEventMeshIp(String eventMeshIp) { + this.eventMeshIp = eventMeshIp; + } + + public String getEventMeshEnv() { + return eventMeshEnv; + } + + public void setEventMeshEnv(String eventMeshEnv) { + this.eventMeshEnv = eventMeshEnv; + } + + public String getEventMeshIdc() { + return eventMeshIdc; + } + + public void setEventMeshIdc(String eventMeshIdc) { + this.eventMeshIdc = eventMeshIdc; + } + + public static SendMessageResponseHeader buildHeader(Integer requestCode, String eventMeshCluster, + String eventMeshIp, String eventMeshEnv, + String eventMeshIDC) { + SendMessageResponseHeader sendMessageResponseHeader = new SendMessageResponseHeader(); + sendMessageResponseHeader.setCode(requestCode); + sendMessageResponseHeader.setEventMeshCluster(eventMeshCluster); + sendMessageResponseHeader.setEventMeshIp(eventMeshIp); + sendMessageResponseHeader.setEventMeshEnv(eventMeshEnv); + sendMessageResponseHeader.setEventMeshIdc(eventMeshIDC); + return sendMessageResponseHeader; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("sendMessageResponseHeader={") + .append("code=").append(code).append(",") + .append("eventMeshEnv=").append(eventMeshEnv).append(",") + .append("eventMeshIdc=").append(eventMeshIdc).append(",") + .append("eventMeshCluster=").append(eventMeshCluster).append(",") + .append("eventMeshIp=").append(eventMeshIp).append("}"); + return sb.toString(); + } + + @Override + public Map toMap() { + Map map = new HashMap(); + map.put(ProtocolKey.REQUEST_CODE, code); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHCLUSTER, eventMeshCluster); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHIP, eventMeshIp); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHENV, eventMeshEnv); + map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHIDC, eventMeshIdc); + return map; + } +} diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/Command.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/Command.java similarity index 97% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/Command.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/Command.java index f713d7020f..54d75b4a61 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/Command.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/Command.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.tcp; +package org.apache.eventmesh.common.protocol.tcp; public enum Command { @@ -80,7 +80,7 @@ public enum Command { UNREGISTER_REQUEST(33), //client发送去注册请求给server UNREGISTER_RESPONSE(34), //server将去注册结果给client - //client询问proxy推荐连哪个proxy + //client询问EventMesh推荐连哪个EventMesh RECOMMEND_REQUEST(35), //client发送推荐请求给server RECOMMEND_RESPONSE(36); //server将推荐结果给client diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/EventMeshClientInfo.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/EventMeshClientInfo.java new file mode 100644 index 0000000000..93616b2b8e --- /dev/null +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/EventMeshClientInfo.java @@ -0,0 +1,111 @@ +///* +// * Licensed to the Apache Software Foundation (ASF) under one or more +// * contributor license agreements. See the NOTICE file distributed with +// * this work for additional information regarding copyright ownership. +// * The ASF licenses this file to You under the Apache License, Version 2.0 +// * (the "License"); you may not use this file except in compliance with +// * the License. You may obtain a copy of the License at +// * +// * http://www.apache.org/licenses/LICENSE-2.0 +// * +// * Unless required by applicable law or agreed to in writing, software +// * distributed under the License is distributed on an "AS IS" BASIS, +// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// * See the License for the specific language governing permissions and +// * limitations under the License. +// */ +// +//package org.apache.eventmesh.common.protocol.tcp; +// +//import org.apache.rocketmq.common.DataVersion; +// +//public class EventMeshClientInfo { +// private String clientId; +// private String consumerGroup; +// private String endpoint; +// private String language; +// private long version; +// private DataVersion dataVersion; +// private long lastUpdateTimestamp; +// private int protocolNumber; +// +// public EventMeshClientInfo(String clientId, String consumerGroup, String endpoint, String language, long version, DataVersion dataVersion, long lastUpdateTimestamp, int protocolNumber) { +// this.clientId = clientId; +// this.endpoint = endpoint; +// this.language = language; +// this.version = version; +// this.consumerGroup = consumerGroup; +// this.dataVersion = dataVersion; +// this.lastUpdateTimestamp = lastUpdateTimestamp; +// this.protocolNumber = protocolNumber; +// } +// +// public void setClientId(String clientId) { +// this.clientId = clientId; +// } +// +// public String getClientId() { +// return clientId; +// } +// +// public void setDataVersion(DataVersion dataVersion) { +// this.dataVersion = dataVersion; +// } +// +// public void setEndpoint(String endpoint) { +// this.endpoint = endpoint; +// } +// +// public DataVersion getDataVersion() { +// return dataVersion; +// } +// +// public String getEndpoint() { +// return endpoint; +// } +// +// public void setLastUpdateTimestamp(long lastUpdateTimestamp) { +// this.lastUpdateTimestamp = lastUpdateTimestamp; +// } +// +// public long getLastUpdateTimestamp() { +// return lastUpdateTimestamp; +// } +// +// public void setConsumerGroup(String consumerGroup) { +// this.consumerGroup = consumerGroup; +// } +// +// public String getConsumerGroup() { +// return consumerGroup; +// } +// +// public void setVersion(long version) { +// this.version = version; +// } +// +// public void setLanguage(String language) { +// this.language = language; +// } +// +// public String getLanguage() { +// return language; +// } +// +// public long getVersion() { +// return version; +// } +// +// public void setProtocolNumber(int protocolNumber) { +// this.protocolNumber = protocolNumber; +// } +// +// public int getProtocolNumber() { +// return protocolNumber; +// } +// +// @Override +// public String toString() { +// return "ClientId [clientId=" + clientId + ", consumerGroup=" + consumerGroup + ", endpoint=" + endpoint + ", language=" + language + ", version=" + version + ", dataVersion=" + dataVersion + ", lastUpdateTimestamp=" + lastUpdateTimestamp + "]"; +// } +//} diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/AccessMessage.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/EventMeshMessage.java similarity index 88% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/AccessMessage.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/EventMeshMessage.java index 64c4e57359..01d744eea1 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/AccessMessage.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/EventMeshMessage.java @@ -15,21 +15,21 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.tcp; +package org.apache.eventmesh.common.protocol.tcp; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -public class AccessMessage { +public class EventMeshMessage { private String topic; Map properties = new ConcurrentHashMap<>(); private String body; - public AccessMessage() { + public EventMeshMessage() { } - public AccessMessage(String topic, Map properties, String body) { + public EventMeshMessage(String topic, Map properties, String body) { this.topic = topic; this.properties = properties; this.body = body; @@ -61,7 +61,7 @@ public void setBody(String body) { @Override public String toString() { - return "AccessMessage{" + + return "EventMeshMessage{" + "topic='" + topic + '\'' + ", properties=" + properties + ", body='" + body + '\'' + diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/Header.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/Header.java similarity index 97% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/Header.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/Header.java index 70a51075e3..1df51427c8 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/Header.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/Header.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.tcp; +package org.apache.eventmesh.common.protocol.tcp; public class Header { diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/HeartBeatInfo.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/HeartBeatInfo.java similarity index 97% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/HeartBeatInfo.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/HeartBeatInfo.java index 0e421eb966..c12d6917d2 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/HeartBeatInfo.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/HeartBeatInfo.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.tcp; +package org.apache.eventmesh.common.protocol.tcp; import java.util.List; diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/OPStatus.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/OPStatus.java similarity index 96% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/OPStatus.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/OPStatus.java index 56089c4e39..4ecfd04dbc 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/OPStatus.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/OPStatus.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.tcp; +package org.apache.eventmesh.common.protocol.tcp; public enum OPStatus { diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/Package.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/Package.java similarity index 96% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/Package.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/Package.java index 051c3223e2..d2cde02193 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/Package.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/Package.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.tcp; +package org.apache.eventmesh.common.protocol.tcp; public class Package { diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/RedirectInfo.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/RedirectInfo.java similarity index 96% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/RedirectInfo.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/RedirectInfo.java index 40a19db587..81373f8485 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/RedirectInfo.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/RedirectInfo.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.tcp; +package org.apache.eventmesh.common.protocol.tcp; public class RedirectInfo { private String ip; diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/RegisterInfo.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/RegisterInfo.java similarity index 96% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/RegisterInfo.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/RegisterInfo.java index 0e5867506e..44303f2f58 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/RegisterInfo.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/RegisterInfo.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.tcp; +package org.apache.eventmesh.common.protocol.tcp; import java.util.List; diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/Subscription.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/Subscription.java similarity index 77% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/Subscription.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/Subscription.java index 2c965bcc36..3fb74b95b0 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/Subscription.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/Subscription.java @@ -15,27 +15,29 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.tcp; +package org.apache.eventmesh.common.protocol.tcp; + +import org.apache.eventmesh.common.protocol.SubscriptionItem; import java.util.LinkedList; import java.util.List; public class Subscription { - private List topicList = new LinkedList<>(); + private List topicList = new LinkedList<>(); public Subscription() { } - public Subscription(List topicList) { + public Subscription(List topicList) { this.topicList = topicList; } - public List getTopicList() { + public List getTopicList() { return topicList; } - public void setTopicList(List topicList) { + public void setTopicList(List topicList) { this.topicList = topicList; } @@ -45,6 +47,4 @@ public String toString() { "topicList=" + topicList + '}'; } - - } diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/UnRegisterInfo.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/UnRegisterInfo.java similarity index 97% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/UnRegisterInfo.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/UnRegisterInfo.java index 6c0626de7a..33961ad486 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/UnRegisterInfo.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/UnRegisterInfo.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.tcp; +package org.apache.eventmesh.common.protocol.tcp; import java.util.List; diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/UserAgent.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/UserAgent.java similarity index 92% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/UserAgent.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/UserAgent.java index 07200a70d3..14b5058e69 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/UserAgent.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/UserAgent.java @@ -15,13 +15,12 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.tcp; +package org.apache.eventmesh.common.protocol.tcp; public class UserAgent { private String env; private String subsystem; - private String dcn; private String path; private int pid; private String host; @@ -30,12 +29,30 @@ public class UserAgent { private String username; private String password; private String idc; + private String producerGroup; + private String consumerGroup; private String purpose; private int unack = 0; public UserAgent() { } + public String getProducerGroup() { + return producerGroup; + } + + public void setProducerGroup(String producerGroup) { + this.producerGroup = producerGroup; + } + + public String getConsumerGroup() { + return consumerGroup; + } + + public void setConsumerGroup(String consumerGroup) { + this.consumerGroup = consumerGroup; + } + public String getEnv() { return env; } @@ -60,14 +77,6 @@ public void setSubsystem(String subsystem) { this.subsystem = subsystem; } - public String getDcn() { - return dcn; - } - - public void setDcn(String dcn) { - this.dcn = dcn; - } - public String getPath() { return path; } @@ -145,7 +154,6 @@ public String toString() { return "UserAgent{" + "env='" + env + '\'' + "subsystem='" + subsystem + '\'' + - ", dcn='" + dcn + '\'' + ", path='" + path + '\'' + ", pid=" + pid + ", host='" + host + '\'' + @@ -168,7 +176,6 @@ public boolean equals(Object o) { if (port != userAgent.port) return false; if (unack != userAgent.unack) return false; if (subsystem != null ? !subsystem.equals(userAgent.subsystem) : userAgent.subsystem != null) return false; - if (dcn != null ? !dcn.equals(userAgent.dcn) : userAgent.dcn != null) return false; if (path != null ? !path.equals(userAgent.path) : userAgent.path != null) return false; if (host != null ? !host.equals(userAgent.host) : userAgent.host != null) return false; if (purpose != null ? !purpose.equals(userAgent.purpose) : userAgent.purpose != null) return false; @@ -182,7 +189,6 @@ public boolean equals(Object o) { @Override public int hashCode() { int result = subsystem != null ? subsystem.hashCode() : 0; - result = 31 * result + (dcn != null ? dcn.hashCode() : 0); result = 31 * result + (path != null ? path.hashCode() : 0); result = 31 * result + pid; result = 31 * result + (host != null ? host.hashCode() : 0); diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/codec/Codec.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java similarity index 89% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/codec/Codec.java rename to eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java index 4935522986..d520e04dbc 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/tcp/codec/Codec.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java @@ -15,38 +15,40 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.tcp.codec; - -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.eventmesh.common.protocol.tcp.AccessMessage; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Header; -import com.webank.eventmesh.common.protocol.tcp.RedirectInfo; -import com.webank.eventmesh.common.protocol.tcp.Subscription; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; +package org.apache.eventmesh.common.protocol.tcp.codec; + +import java.nio.charset.Charset; +import java.util.Arrays; +import java.util.List; +import java.util.TimeZone; + import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; + import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToByteEncoder; import io.netty.handler.codec.ReplayingDecoder; + +import org.apache.eventmesh.common.protocol.tcp.Command; +import org.apache.eventmesh.common.protocol.tcp.EventMeshMessage; +import org.apache.eventmesh.common.protocol.tcp.Header; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.common.protocol.tcp.RedirectInfo; +import org.apache.eventmesh.common.protocol.tcp.Subscription; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.nio.charset.Charset; -import java.util.Arrays; -import java.util.List; -import java.util.TimeZone; - public class Codec { private final static Logger logger = LoggerFactory.getLogger(Codec.class); private static final int FRAME_MAX_LENGTH = 1024 * 1024 * 4; private static Charset UTF8 = Charset.forName("UTF-8"); - private static final byte[] CONSTANT_MAGIC_FLAG = "WEMQ".getBytes(UTF8); + private static final byte[] CONSTANT_MAGIC_FLAG = "EventMesh".getBytes(UTF8); private static final byte[] VERSION = "0000".getBytes(UTF8); @@ -160,17 +162,17 @@ private static Object parseFromJson(Command cmd, String data) throws Exception { case RESPONSE_TO_SERVER: case ASYNC_MESSAGE_TO_SERVER: case BROADCAST_MESSAGE_TO_SERVER: - return jsonMapper.readValue(data, AccessMessage.class); + return jsonMapper.readValue(data, EventMeshMessage.class); case REQUEST_TO_CLIENT: case RESPONSE_TO_CLIENT: case ASYNC_MESSAGE_TO_CLIENT: case BROADCAST_MESSAGE_TO_CLIENT: - return jsonMapper.readValue(data, AccessMessage.class); + return jsonMapper.readValue(data, EventMeshMessage.class); case REQUEST_TO_CLIENT_ACK: case RESPONSE_TO_CLIENT_ACK: case ASYNC_MESSAGE_TO_CLIENT_ACK: case BROADCAST_MESSAGE_TO_CLIENT_ACK: - return jsonMapper.readValue(data, AccessMessage.class); + return jsonMapper.readValue(data, EventMeshMessage.class); case REDIRECT_TO_CLIENT: return jsonMapper.readValue(data, RedirectInfo.class); default: diff --git a/eventmesh-common/src/test/java/org/apache/eventmesh/common/loadbalance/RandomLoadBalanceSelectorTest.java b/eventmesh-common/src/test/java/org/apache/eventmesh/common/loadbalance/RandomLoadBalanceSelectorTest.java new file mode 100644 index 0000000000..4f7198f899 --- /dev/null +++ b/eventmesh-common/src/test/java/org/apache/eventmesh/common/loadbalance/RandomLoadBalanceSelectorTest.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.common.loadbalance; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class RandomLoadBalanceSelectorTest { + + private RandomLoadBalanceSelector randomLoadBalanceSelector; + + private Logger logger = LoggerFactory.getLogger(RandomLoadBalanceSelectorTest.class); + + @Before + public void befor() { + List address = new ArrayList<>(); + address.add("A"); + address.add("B"); + address.add("C"); + randomLoadBalanceSelector = new RandomLoadBalanceSelector<>(address); + } + + + @Test + public void testSelect() { + Map addressToNum = new HashMap<>(); + for (int i = 0; i < 100; i++) { + String select = randomLoadBalanceSelector.select(); + addressToNum.put(select, addressToNum.getOrDefault(select, 0) + 1); + } + addressToNum.forEach((key, value) -> logger.info("{} : {}", key, value)); + // just assert success if no exception + Assert.assertTrue(true); + } + + @Test + public void testGetType() { + Assert.assertEquals(LoadBalanceType.RANDOM, randomLoadBalanceSelector.getType()); + } +} \ No newline at end of file diff --git a/eventmesh-common/src/test/java/org/apache/eventmesh/common/loadbalance/WeightRoundRobinLoadBalanceSelectorTest.java b/eventmesh-common/src/test/java/org/apache/eventmesh/common/loadbalance/WeightRoundRobinLoadBalanceSelectorTest.java new file mode 100644 index 0000000000..d37aed367b --- /dev/null +++ b/eventmesh-common/src/test/java/org/apache/eventmesh/common/loadbalance/WeightRoundRobinLoadBalanceSelectorTest.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.common.loadbalance; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class WeightRoundRobinLoadBalanceSelectorTest { + + private Logger logger = LoggerFactory.getLogger(WeightRoundRobinLoadBalanceSelectorTest.class); + + private WeightRoundRobinLoadBalanceSelector weightRoundRobinLoadBalanceSelector; + + @Before + public void before() { + List> weightList = new ArrayList<>(); + weightList.add(new Weight<>("A", 10)); + weightList.add(new Weight<>("B", 20)); + weightList.add(new Weight<>("C", 30)); + weightRoundRobinLoadBalanceSelector = new WeightRoundRobinLoadBalanceSelector<>(weightList); + } + + @Test + public void testSelect() { + Map addressToNum = new HashMap<>(); + for (int i = 0; i < 100_000; i++) { + String select = weightRoundRobinLoadBalanceSelector.select(); + addressToNum.put(select, addressToNum.getOrDefault(select, 0) + 1); + } + addressToNum.forEach((key, value) -> { + logger.info("{}: {}", key, value); + }); + Assert.assertTrue(addressToNum.get("B") > addressToNum.get("A")); + Assert.assertTrue(addressToNum.get("C") > addressToNum.get("B")); + } + + @Test + public void testGetType() { + Assert.assertEquals(LoadBalanceType.WEIGHT_ROUND_ROBIN, weightRoundRobinLoadBalanceSelector.getType()); + } +} \ No newline at end of file diff --git a/eventmesh-connector-api/build.gradle b/eventmesh-connector-api/build.gradle new file mode 100644 index 0000000000..2d1205df38 --- /dev/null +++ b/eventmesh-connector-api/build.gradle @@ -0,0 +1,25 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +List open_message = [ + "io.openmessaging:openmessaging-api:2.2.1-pubsub" +] + +dependencies { + implementation open_message,project(":eventmesh-common") + testImplementation open_message,project(":eventmesh-common") +} diff --git a/eventmesh-connector-api/gradle.properties b/eventmesh-connector-api/gradle.properties new file mode 100644 index 0000000000..c06c465567 --- /dev/null +++ b/eventmesh-connector-api/gradle.properties @@ -0,0 +1,21 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +group=org.apache.eventmesh +version=1.2.0-release +jdk=1.8 +mavenUserName= +mavenPassword= \ No newline at end of file diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/SubscribeRequestBody.java b/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/AbstractContext.java similarity index 89% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/SubscribeRequestBody.java rename to eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/AbstractContext.java index 690995cf8d..559a082bfc 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/SubscribeRequestBody.java +++ b/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/AbstractContext.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.body.client; +package org.apache.eventmesh.api; -public class SubscribeRequestBody { +public interface AbstractContext { } diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/SubscribeRequestHeader.java b/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/EventMeshAction.java similarity index 87% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/SubscribeRequestHeader.java rename to eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/EventMeshAction.java index 418164b845..4fda6d05b6 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/SubscribeRequestHeader.java +++ b/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/EventMeshAction.java @@ -14,8 +14,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package org.apache.eventmesh.api; -package com.webank.eventmesh.common.protocol.http.header.client; +public enum EventMeshAction { + CommitMessage, -public class SubscribeRequestHeader { + ReconsumeLater, + + ManualAck } diff --git a/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/EventMeshAsyncConsumeContext.java b/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/EventMeshAsyncConsumeContext.java new file mode 100644 index 0000000000..c7e4e7fb73 --- /dev/null +++ b/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/EventMeshAsyncConsumeContext.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.eventmesh.api; + +import io.openmessaging.api.Action; +import io.openmessaging.api.AsyncConsumeContext; + +public abstract class EventMeshAsyncConsumeContext extends AsyncConsumeContext { + + private AbstractContext abstractContext; + + public AbstractContext getAbstractContext() { + return abstractContext; + } + + public void setAbstractContext(AbstractContext abstractContext) { + this.abstractContext = abstractContext; + } + + public abstract void commit(EventMeshAction action); + + @Override + public void commit(Action action) { + throw new UnsupportedOperationException("not support yet"); + } +} \ No newline at end of file diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/SubscribeResponseHeader.java b/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/RRCallback.java similarity index 81% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/SubscribeResponseHeader.java rename to eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/RRCallback.java index 11d73f01dd..788f9f3906 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/SubscribeResponseHeader.java +++ b/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/RRCallback.java @@ -15,7 +15,14 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.header.client; +package org.apache.eventmesh.api; + +import io.openmessaging.api.Message; + +public interface RRCallback { + + public void onSuccess(Message msg); + + public void onException(Throwable e); -public class SubscribeResponseHeader { } diff --git a/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/consumer/MeshMQPushConsumer.java b/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/consumer/MeshMQPushConsumer.java new file mode 100644 index 0000000000..5e60e0e0df --- /dev/null +++ b/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/consumer/MeshMQPushConsumer.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.api.consumer; + +import java.util.List; +import java.util.Properties; + +import io.openmessaging.api.AsyncMessageListener; +import io.openmessaging.api.Consumer; +import io.openmessaging.api.Message; + +import org.apache.eventmesh.api.AbstractContext; + +public interface MeshMQPushConsumer extends Consumer { + + void init(Properties keyValue) throws Exception; + + void updateOffset(List msgs, AbstractContext context); + +// void registerMessageListener(MessageListenerConcurrently messageListenerConcurrently); + + void subscribe(String topic, final AsyncMessageListener listener) throws Exception; + + @Override + void unsubscribe(String topic); +} diff --git a/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/producer/MeshMQProducer.java b/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/producer/MeshMQProducer.java new file mode 100644 index 0000000000..82ca583ce7 --- /dev/null +++ b/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/producer/MeshMQProducer.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.api.producer; + +import java.util.Properties; + +import io.openmessaging.api.Message; +import io.openmessaging.api.Producer; +import io.openmessaging.api.SendCallback; + +import org.apache.eventmesh.api.RRCallback; + +public interface MeshMQProducer extends Producer { + + void init(Properties properties) throws Exception; + + void send(Message message, SendCallback sendCallback) throws Exception; + + void request(Message message, SendCallback sendCallback, RRCallback rrCallback, long timeout) throws Exception; + + Message request(Message message, long timeout) throws Exception; + + boolean reply(final Message message, final SendCallback sendCallback) throws Exception; + + void checkTopicExist(String topic) throws Exception; + + void setExtFields(); + +} diff --git a/eventmesh-connector-rocketmq/build.gradle b/eventmesh-connector-rocketmq/build.gradle new file mode 100644 index 0000000000..6126b50609 --- /dev/null +++ b/eventmesh-connector-rocketmq/build.gradle @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +//apply plugin: 'java' +////apply plugin: "maven" +//apply plugin: "eclipse" +//apply plugin: "idea" +// +//sourceCompatibility = 1.8 + +configurations { + implementation.exclude group: 'ch.qos.logback', module: 'logback-classic' +} + +List rocketmq = [ + "org.apache.rocketmq:rocketmq-client:$rocketmq_version", + "org.apache.rocketmq:rocketmq-broker:$rocketmq_version", + "org.apache.rocketmq:rocketmq-common:$rocketmq_version", + "org.apache.rocketmq:rocketmq-store:$rocketmq_version", + "org.apache.rocketmq:rocketmq-namesrv:$rocketmq_version", + "org.apache.rocketmq:rocketmq-tools:$rocketmq_version", + "org.apache.rocketmq:rocketmq-remoting:$rocketmq_version", + "org.apache.rocketmq:rocketmq-logging:$rocketmq_version", + "org.apache.rocketmq:rocketmq-test:$rocketmq_version", + "org.apache.rocketmq:rocketmq-srvutil:$rocketmq_version", + "org.apache.rocketmq:rocketmq-filter:$rocketmq_version", + "org.apache.rocketmq:rocketmq-acl:$rocketmq_version", + "org.apache.rocketmq:rocketmq-srvutil:$rocketmq_version", + +] + +List metrics = [ + "io.dropwizard.metrics:metrics-core:4.1.0", + "io.dropwizard.metrics:metrics-healthchecks:4.1.0", + "io.dropwizard.metrics:metrics-annotation:4.1.0", + "io.dropwizard.metrics:metrics-json:4.1.0" +] + +List open_message = [ + "io.openmessaging:openmessaging-api:2.2.1-pubsub" +] + +dependencies { + implementation rocketmq, metrics,open_message,project(":eventmesh-connector-api"),project(":eventmesh-common") + testImplementation rocketmq, metrics,open_message,project(":eventmesh-connector-api"),project(":eventmesh-common") +} diff --git a/eventmesh-connector-rocketmq/gradle.properties b/eventmesh-connector-rocketmq/gradle.properties new file mode 100644 index 0000000000..383fbc021b --- /dev/null +++ b/eventmesh-connector-rocketmq/gradle.properties @@ -0,0 +1,20 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +group=org.apache.eventmesh +version=1.2.0-release +rocketmq_version=4.7.1 +mavenUserName= +mavenPassword= \ No newline at end of file diff --git a/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/MessagingAccessPointImpl.java b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/MessagingAccessPointImpl.java new file mode 100644 index 0000000000..f1b7ec79f1 --- /dev/null +++ b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/MessagingAccessPointImpl.java @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.connector.rocketmq; + +import java.util.Properties; + +import io.openmessaging.api.Consumer; +import io.openmessaging.api.MessagingAccessPoint; +import io.openmessaging.api.Producer; +import io.openmessaging.api.PullConsumer; +import io.openmessaging.api.batch.BatchConsumer; +import io.openmessaging.api.order.OrderConsumer; +import io.openmessaging.api.order.OrderProducer; +import io.openmessaging.api.transaction.LocalTransactionChecker; +import io.openmessaging.api.transaction.TransactionProducer; + +import org.apache.eventmesh.connector.rocketmq.consumer.PushConsumerImpl; +import org.apache.eventmesh.connector.rocketmq.producer.ProducerImpl; + +public class MessagingAccessPointImpl implements MessagingAccessPoint { + + private Properties accessPointProperties; + + public MessagingAccessPointImpl(final Properties accessPointProperties) { + this.accessPointProperties = accessPointProperties; + } + + @Override + public String version() { + return null; + } + + @Override + public Properties attributes() { + return accessPointProperties; + } + + @Override + public Producer createProducer(Properties properties) { + return new ProducerImpl(this.accessPointProperties); + } + + @Override + public OrderProducer createOrderProducer(Properties properties) { + return null; + } + + @Override + public TransactionProducer createTransactionProducer(Properties properties, LocalTransactionChecker checker) { + return null; + } + + @Override + public TransactionProducer createTransactionProducer(Properties properties) { + return null; + } + + @Override + public Consumer createConsumer(Properties properties) { + return new PushConsumerImpl(properties); + } + + @Override + public PullConsumer createPullConsumer(Properties properties) { + return null; + } + + @Override + public BatchConsumer createBatchConsumer(Properties properties) { + return null; + } + + @Override + public OrderConsumer createOrderedConsumer(Properties properties) { + return null; + } + +} diff --git a/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/common/Constants.java b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/common/Constants.java new file mode 100644 index 0000000000..fc55a68739 --- /dev/null +++ b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/common/Constants.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.connector.rocketmq.common; + +public class Constants { + + public static final String BROADCAST_PREFIX = "broadcast-"; + + public static final String PROPERTY_MESSAGE_TIMEOUT = "TIMEOUT"; + + public static final String PROPERTY_MESSAGE_KEYS = "KEYS"; + + public static final String PROPERTY_MESSAGE_QUEUE_ID = "QUEUE_ID"; + + public static final String PROPERTY_MESSAGE_QUEUE_OFFSET = "QUEUE_OFFSET"; + + public static final String PROPERTY_MESSAGE_DESTINATION = "DESTINATION"; + + public static final String PROPERTY_MESSAGE_MESSAGE_ID = "MESSAGE_ID"; + + public static final String PROPERTY_MESSAGE_BORN_HOST = "BORN_HOST"; + + public static final String PROPERTY_MESSAGE_BORN_TIMESTAMP = "BORN_TIMESTAMP"; + + public static final String PROPERTY_MESSAGE_STORE_HOST = "STORE_HOST"; +} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/constants/ProxyConstants.java b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/common/EventMeshConstants.java similarity index 61% rename from eventmesh-emesher/src/main/java/com/webank/emesher/constants/ProxyConstants.java rename to eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/common/EventMeshConstants.java index 67307a1425..69fb8c10dc 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/constants/ProxyConstants.java +++ b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/common/EventMeshConstants.java @@ -15,9 +15,9 @@ * limitations under the License. */ -package com.webank.emesher.constants; +package org.apache.eventmesh.connector.rocketmq.common; -public class ProxyConstants { +public class EventMeshConstants { public static final String EVENT_STORE_PROPERTIES = "eventstore"; @@ -29,29 +29,25 @@ public class ProxyConstants { public static final String BROADCAST_PREFIX = "broadcast-"; - public final static String CONSUMER_GROUP_NAME_PREFIX = "ConsumerGroup-"; - - public final static String PRODUCER_GROUP_NAME_PREFIX = "ProducerGroup-"; - public static final String DEFAULT_CHARSET = "UTF-8"; - public static final String PROXY_CONF_HOME = System.getProperty("confPath", System.getenv("confPath")); + public static final String EVENTMESH_CONF_HOME = System.getProperty("confPath", System.getenv("confPath")); - public static final String PROXY_CONF_FILE = "proxy.properties"; + public static final String EVENTMESH_CONF_FILE = "rocketmq-client.properties"; - public static final String REQ_C2PROXY_TIMESTAMP = "req_c2access_timestamp"; - public static final String REQ_PROXY2MQ_TIMESTAMP = "req_access2mq_timestamp"; - public static final String REQ_MQ2PROXY_TIMESTAMP = "req_mq2access_timestamp"; - public static final String REQ_PROXY2C_TIMESTAMP = "req_access2c_timestamp"; - public static final String RSP_C2PROXY_TIMESTAMP = "rsp_c2access_timestamp"; - public static final String RSP_PROXY2MQ_TIMESTAMP = "rsp_access2mq_timestamp"; - public static final String RSP_MQ2PROXY_TIMESTAMP = "rsp_mq2access_timestamp"; - public static final String RSP_PROXY2C_TIMESTAMP = "rsp_access2c_timestamp"; + public static final String REQ_C2EventMesh_TIMESTAMP = "req_c2eventMesh_timestamp"; + public static final String REQ_EventMesh2MQ_TIMESTAMP = "req_eventMesh2mq_timestamp"; + public static final String REQ_MQ2EventMesh_TIMESTAMP = "req_mq2eventMesh_timestamp"; + public static final String REQ_EventMesh2C_TIMESTAMP = "req_eventMesh2c_timestamp"; + public static final String RSP_C2EventMesh_TIMESTAMP = "rsp_c2eventMesh_timestamp"; + public static final String RSP_EventMesh2MQ_TIMESTAMP = "rsp_eventMesh2mq_timestamp"; + public static final String RSP_MQ2EventMesh_TIMESTAMP = "rsp_mq2eventMesh_timestamp"; + public static final String RSP_EventMesh2C_TIMESTAMP = "rsp_eventMesh2c_timestamp"; - public static final String REQ_SEND_PROXY_IP = "req_send_proxy_ip"; - public static final String REQ_RECEIVE_PROXY_IP = "req_receive_proxy_ip"; - public static final String RSP_SEND_PROXY_IP = "rsp_send_proxy_ip"; - public static final String RSP_RECEIVE_PROXY_IP = "rsp_receive_proxy_ip"; + public static final String REQ_SEND_EventMesh_IP = "req_send_EventMesh_ip"; + public static final String REQ_RECEIVE_EventMesh_IP = "req_receive_EventMesh_ip"; + public static final String RSP_SEND_EventMesh_IP = "rsp_send_EventMesh_ip"; + public static final String RSP_RECEIVE_EventMesh_IP = "rsp_receive_EventMesh_ip"; //default TTL 4 hours public static final Integer DEFAULT_MSG_TTL_MILLS = 14400000; @@ -85,22 +81,21 @@ public class ProxyConstants { public static final String TAG = "TAG"; - public static final String MANAGE_DCN = "dcn"; public static final String MANAGE_SUBSYSTEM = "subSystem"; public static final String MANAGE_IP = "ip"; public static final String MANAGE_PORT = "port"; - public static final String MANAGE_DEST_IP = "destProxyIp"; - public static final String MANAGE_DEST_PORT = "destProxyPort"; + public static final String MANAGE_DEST_IP = "destEventMeshIp"; + public static final String MANAGE_DEST_PORT = "destEventMeshPort"; public static final String MANAGE_PATH = "path"; public static final String MANAGE_GROUP = "group"; public static final String MANAGE_PURPOSE = "purpose"; public static final String MANAGE_TOPIC = "topic"; - public static final String PROXY_SEND_BACK_TIMES= "proxySendBackTimes"; + public static final String EventMesh_SEND_BACK_TIMES = "EventMeshSendBackTimes"; - public static final String PROXY_SEND_BACK_IP= "proxySendBackIp"; + public static final String EventMesh_SEND_BACK_IP = "EventMeshSendBackIp"; - public static final String PROXY_REGISTRY_ADDR_KEY= "proxyRegistryAddr"; + public static final String EventMesh_REGISTRY_ADDR_KEY = "EventMeshRegistryAddr"; public static int DEFAULT_TIME_OUT_MILLS = 5 * 1000; diff --git a/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/config/ClientConfig.java b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/config/ClientConfig.java new file mode 100644 index 0000000000..aad0b49a53 --- /dev/null +++ b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/config/ClientConfig.java @@ -0,0 +1,205 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.connector.rocketmq.config; + +import io.openmessaging.api.OMSBuiltinKeys; + +import org.apache.eventmesh.connector.rocketmq.domain.NonStandardKeys; + +public class ClientConfig implements OMSBuiltinKeys, NonStandardKeys { + private String driverImpl; + private String accessPoints; + private String namespace; + private String producerId; + private String consumerId; + private int operationTimeout = 5000; + private String region; + private String routingSource; + private String routingDestination; + private String routingExpression; + private String rmqConsumerGroup; + private String rmqProducerGroup = "__OMS_PRODUCER_DEFAULT_GROUP"; + private int rmqMaxRedeliveryTimes = 16; + private int rmqMessageConsumeTimeout = 15; //In minutes + private int rmqMaxConsumeThreadNums = 64; + private int rmqMinConsumeThreadNums = 20; + private String rmqMessageDestination; + private int rmqPullMessageBatchNums = 32; + private int rmqPullMessageCacheCapacity = 1000; + private String messageModel; + + public String getDriverImpl() { + return driverImpl; + } + + public void setDriverImpl(final String driverImpl) { + this.driverImpl = driverImpl; + } + + public String getAccessPoints() { + return accessPoints; + } + + public void setAccessPoints(final String accessPoints) { + this.accessPoints = accessPoints; + } + + public String getNamespace() { + return namespace; + } + + public void setNamespace(final String namespace) { + this.namespace = namespace; + } + + public String getProducerId() { + return producerId; + } + + public void setProducerId(final String producerId) { + this.producerId = producerId; + } + + public String getConsumerId() { + return consumerId; + } + + public void setConsumerId(final String consumerId) { + this.consumerId = consumerId; + } + + public int getOperationTimeout() { + return operationTimeout; + } + + public void setOperationTimeout(final int operationTimeout) { + this.operationTimeout = operationTimeout; + } + + public String getRoutingSource() { + return routingSource; + } + + public void setRoutingSource(final String routingSource) { + this.routingSource = routingSource; + } + + public String getRmqConsumerGroup() { + return rmqConsumerGroup; + } + + public void setRmqConsumerGroup(final String rmqConsumerGroup) { + this.rmqConsumerGroup = rmqConsumerGroup; + } + + public String getRmqProducerGroup() { + return rmqProducerGroup; + } + + public void setRmqProducerGroup(final String rmqProducerGroup) { + this.rmqProducerGroup = rmqProducerGroup; + } + + public int getRmqMaxRedeliveryTimes() { + return rmqMaxRedeliveryTimes; + } + + public void setRmqMaxRedeliveryTimes(final int rmqMaxRedeliveryTimes) { + this.rmqMaxRedeliveryTimes = rmqMaxRedeliveryTimes; + } + + public int getRmqMessageConsumeTimeout() { + return rmqMessageConsumeTimeout; + } + + public void setRmqMessageConsumeTimeout(final int rmqMessageConsumeTimeout) { + this.rmqMessageConsumeTimeout = rmqMessageConsumeTimeout; + } + + public int getRmqMaxConsumeThreadNums() { + return rmqMaxConsumeThreadNums; + } + + public void setRmqMaxConsumeThreadNums(final int rmqMaxConsumeThreadNums) { + this.rmqMaxConsumeThreadNums = rmqMaxConsumeThreadNums; + } + + public int getRmqMinConsumeThreadNums() { + return rmqMinConsumeThreadNums; + } + + public void setRmqMinConsumeThreadNums(final int rmqMinConsumeThreadNums) { + this.rmqMinConsumeThreadNums = rmqMinConsumeThreadNums; + } + + public String getRmqMessageDestination() { + return rmqMessageDestination; + } + + public void setRmqMessageDestination(final String rmqMessageDestination) { + this.rmqMessageDestination = rmqMessageDestination; + } + + public int getRmqPullMessageBatchNums() { + return rmqPullMessageBatchNums; + } + + public void setRmqPullMessageBatchNums(final int rmqPullMessageBatchNums) { + this.rmqPullMessageBatchNums = rmqPullMessageBatchNums; + } + + public int getRmqPullMessageCacheCapacity() { + return rmqPullMessageCacheCapacity; + } + + public void setRmqPullMessageCacheCapacity(final int rmqPullMessageCacheCapacity) { + this.rmqPullMessageCacheCapacity = rmqPullMessageCacheCapacity; + } + + public String getRegion() { + return region; + } + + public void setRegion(String region) { + this.region = region; + } + + public String getRoutingDestination() { + return routingDestination; + } + + public void setRoutingDestination(String routingDestination) { + this.routingDestination = routingDestination; + } + + public String getRoutingExpression() { + return routingExpression; + } + + public void setRoutingExpression(String routingExpression) { + this.routingExpression = routingExpression; + } + + public String getMessageModel() { + return messageModel; + } + + public void setMessageModel(String messageModel) { + this.messageModel = messageModel; + } +} diff --git a/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/config/ClientConfiguration.java b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/config/ClientConfiguration.java new file mode 100644 index 0000000000..11a1858a6e --- /dev/null +++ b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/config/ClientConfiguration.java @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.connector.rocketmq.config; + +import com.google.common.base.Preconditions; + +import org.apache.commons.lang3.StringUtils; + +public class ClientConfiguration { + + public String namesrvAddr = ""; + public String clientUserName = "username"; + public String clientPass = "password"; + public Integer consumeThreadMin = 2; + public Integer consumeThreadMax = 2; + public Integer consumeQueueSize = 10000; + public Integer pullBatchSize = 32; + public Integer ackWindow = 1000; + public Integer pubWindow = 100; + public long consumeTimeout = 0L; + public Integer pollNameServerInteval = 10 * 1000; + public Integer heartbeatBrokerInterval = 30 * 1000; + public Integer rebalanceInterval = 20 * 1000; + + protected ConfigurationWraper configurationWraper; + + public ClientConfiguration(ConfigurationWraper configurationWraper) { + this.configurationWraper = configurationWraper; + } + + public void init() { + + String clientUserNameStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_ROCKETMQ_USERNAME); + if (StringUtils.isNotBlank(clientUserNameStr)) { + clientUserName = StringUtils.trim(clientUserNameStr); + } + + String clientPassStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_ROCKETMQ_PASSWORD); + if (StringUtils.isNotBlank(clientPassStr)) { + clientPass = StringUtils.trim(clientPassStr); + } + + String namesrvAddrStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_ROCKETMQ_NAMESRV_ADDR); + Preconditions.checkState(StringUtils.isNotEmpty(namesrvAddrStr), String.format("%s error", ConfKeys.KEYS_EVENTMESH_ROCKETMQ_NAMESRV_ADDR)); + namesrvAddr = StringUtils.trim(namesrvAddrStr); + + String consumeThreadPoolMinStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_ROCKETMQ_CONSUME_THREADPOOL_MIN); + if (StringUtils.isNotEmpty(consumeThreadPoolMinStr)) { + Preconditions.checkState(StringUtils.isNumeric(consumeThreadPoolMinStr), String.format("%s error", ConfKeys.KEYS_EVENTMESH_ROCKETMQ_CONSUME_THREADPOOL_MIN)); + consumeThreadMin = Integer.valueOf(consumeThreadPoolMinStr); + } + + String consumeThreadPoolMaxStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_ROCKETMQ_CONSUME_THREADPOOL_MAX); + if (StringUtils.isNotEmpty(consumeThreadPoolMaxStr)) { + Preconditions.checkState(StringUtils.isNumeric(consumeThreadPoolMaxStr), String.format("%s error", ConfKeys.KEYS_EVENTMESH_ROCKETMQ_CONSUME_THREADPOOL_MAX)); + consumeThreadMax = Integer.valueOf(consumeThreadPoolMaxStr); + } + + String consumerThreadPoolQueueSizeStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_ROCKETMQ_CONSUME_THREADPOOL_QUEUESIZE); + if (StringUtils.isNotEmpty(consumerThreadPoolQueueSizeStr)) { + Preconditions.checkState(StringUtils.isNumeric(consumerThreadPoolQueueSizeStr), String.format("%s error", ConfKeys.KEYS_EVENTMESH_ROCKETMQ_CONSUME_THREADPOOL_QUEUESIZE)); + consumeQueueSize = Integer.valueOf(consumerThreadPoolQueueSizeStr); + } + + String clientAckWindowStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_ROCKETMQ_CLIENT_ACK_WINDOW); + if (StringUtils.isNotEmpty(clientAckWindowStr)) { + Preconditions.checkState(StringUtils.isNumeric(clientAckWindowStr), String.format("%s error", ConfKeys.KEYS_EVENTMESH_ROCKETMQ_CLIENT_ACK_WINDOW)); + ackWindow = Integer.valueOf(clientAckWindowStr); + } + + String clientPubWindowStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_ROCKETMQ_CLIENT_PUB_WINDOW); + if (StringUtils.isNotEmpty(clientPubWindowStr)) { + Preconditions.checkState(StringUtils.isNumeric(clientPubWindowStr), String.format("%s error", ConfKeys.KEYS_EVENTMESH_ROCKETMQ_CLIENT_PUB_WINDOW)); + pubWindow = Integer.valueOf(clientPubWindowStr); + } + + String consumeTimeoutStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_ROCKETMQ_CLIENT_CONSUME_TIMEOUT); + if (StringUtils.isNotBlank(consumeTimeoutStr)) { + Preconditions.checkState(StringUtils.isNumeric(consumeTimeoutStr), String.format("%s error", ConfKeys.KEYS_EVENTMESH_ROCKETMQ_CLIENT_CONSUME_TIMEOUT)); + consumeTimeout = Long.valueOf(consumeTimeoutStr); + } + + String clientPullBatchSizeStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_ROCKETMQ_CLIENT_PULL_BATCHSIZE); + if (StringUtils.isNotEmpty(clientPullBatchSizeStr)) { + Preconditions.checkState(StringUtils.isNumeric(clientPullBatchSizeStr), String.format("%s error", ConfKeys.KEYS_EVENTMESH_ROCKETMQ_CLIENT_PULL_BATCHSIZE)); + pullBatchSize = Integer.valueOf(clientPullBatchSizeStr); + } + + String clientPollNamesrvIntervalStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_ROCKETMQ_CLIENT_POLL_NAMESRV_INTERVAL); + if (StringUtils.isNotEmpty(clientPollNamesrvIntervalStr)) { + Preconditions.checkState(StringUtils.isNumeric(clientPollNamesrvIntervalStr), String.format("%s error", ConfKeys.KEYS_EVENTMESH_ROCKETMQ_CLIENT_POLL_NAMESRV_INTERVAL)); + pollNameServerInteval = Integer.valueOf(clientPollNamesrvIntervalStr); + } + + String clientHeartbeatBrokerIntervalStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_ROCKETMQ_CLIENT_HEARTBEAT_BROKER_INTERVEL); + if (StringUtils.isNotEmpty(clientHeartbeatBrokerIntervalStr)) { + Preconditions.checkState(StringUtils.isNumeric(clientHeartbeatBrokerIntervalStr), String.format("%s error", ConfKeys.KEYS_EVENTMESH_ROCKETMQ_CLIENT_HEARTBEAT_BROKER_INTERVEL)); + heartbeatBrokerInterval = Integer.valueOf(clientHeartbeatBrokerIntervalStr); + } + + String clientRebalanceIntervalIntervalStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_ROCKETMQ_CLIENT_REBALANCE_INTERVEL); + if (StringUtils.isNotEmpty(clientRebalanceIntervalIntervalStr)) { + Preconditions.checkState(StringUtils.isNumeric(clientRebalanceIntervalIntervalStr), String.format("%s error", ConfKeys.KEYS_EVENTMESH_ROCKETMQ_CLIENT_REBALANCE_INTERVEL)); + rebalanceInterval = Integer.valueOf(clientRebalanceIntervalIntervalStr); + } + } + + static class ConfKeys { + + public static String KEYS_EVENTMESH_ROCKETMQ_NAMESRV_ADDR = "eventMesh.server.rocketmq.namesrvAddr"; + + public static String KEYS_EVENTMESH_ROCKETMQ_USERNAME = "eventMesh.server.rocketmq.username"; + + public static String KEYS_EVENTMESH_ROCKETMQ_PASSWORD = "eventMesh.server.rocketmq.password"; + + public static String KEYS_EVENTMESH_ROCKETMQ_CONSUME_THREADPOOL_MIN = "eventMesh.server.rocketmq.client.consumeThreadMin"; + + public static String KEYS_EVENTMESH_ROCKETMQ_CONSUME_THREADPOOL_MAX = "eventMesh.server.rocketmq.client.consumeThreadMax"; + + public static String KEYS_EVENTMESH_ROCKETMQ_CONSUME_THREADPOOL_QUEUESIZE = "eventMesh.server.rocketmq.client.consumeThreadPoolQueueSize"; + + public static String KEYS_EVENTMESH_ROCKETMQ_CLIENT_ACK_WINDOW = "eventMesh.server.rocketmq.client.ackwindow"; + + public static String KEYS_EVENTMESH_ROCKETMQ_CLIENT_PUB_WINDOW = "eventMesh.server.rocketmq.client.pubwindow"; + + public static String KEYS_EVENTMESH_ROCKETMQ_CLIENT_CONSUME_TIMEOUT = "eventMesh.server.rocketmq.client.comsumeTimeoutInMin"; + + public static String KEYS_EVENTMESH_ROCKETMQ_CLIENT_PULL_BATCHSIZE = "eventMesh.server.rocketmq.client.pullBatchSize"; + + public static String KEYS_EVENTMESH_ROCKETMQ_CLIENT_POLL_NAMESRV_INTERVAL = "eventMesh.server.rocketmq.client.pollNameServerInterval"; + + public static String KEYS_EVENTMESH_ROCKETMQ_CLIENT_HEARTBEAT_BROKER_INTERVEL = "eventMesh.server.rocketmq.client.heartbeatBrokerInterval"; + + public static String KEYS_EVENTMESH_ROCKETMQ_CLIENT_REBALANCE_INTERVEL = "eventMesh.server.rocketmq.client.rebalanceInterval"; + + } +} \ No newline at end of file diff --git a/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/config/ConfigurationWraper.java b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/config/ConfigurationWraper.java new file mode 100644 index 0000000000..75003b137d --- /dev/null +++ b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/config/ConfigurationWraper.java @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.connector.rocketmq.config; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.Properties; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.common.ThreadPoolFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ConfigurationWraper { + + public Logger logger = LoggerFactory.getLogger(this.getClass()); + + private String file; + + private Properties properties = new Properties(); + + private boolean reload = true; + + private ScheduledExecutorService configLoader = ThreadPoolFactory.createSingleScheduledExecutor("eventMesh-configloader-"); + + public ConfigurationWraper(String file, boolean reload) { + this.file = file; + this.reload = reload; + init(); + } + + private void init() { + load(); + if (this.reload) { + configLoader.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { + load(); + } + }, 30 * 1000, 30 * 1000, TimeUnit.MILLISECONDS); + } + } + + private void load() { + try { + logger.info("loading config: {}", file); + properties.load(new BufferedReader(new FileReader( + new File(file)))); + } catch (IOException e) { + logger.error("loading properties [{}] error", file, e); + } + } + + public String getProp(String key) { + return StringUtils.isEmpty(key) ? null : properties.getProperty(key, null); + } +} \ No newline at end of file diff --git a/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/consumer/PushConsumerImpl.java b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/consumer/PushConsumerImpl.java new file mode 100644 index 0000000000..47565e3b03 --- /dev/null +++ b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/consumer/PushConsumerImpl.java @@ -0,0 +1,335 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.connector.rocketmq.consumer; + +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicBoolean; +import io.openmessaging.api.AsyncGenericMessageListener; +import io.openmessaging.api.AsyncMessageListener; +import io.openmessaging.api.Consumer; +import io.openmessaging.api.GenericMessageListener; +import io.openmessaging.api.Message; +import io.openmessaging.api.MessageListener; +import io.openmessaging.api.MessageSelector; +import io.openmessaging.api.exception.OMSRuntimeException; +import org.apache.eventmesh.api.EventMeshAction; +import org.apache.eventmesh.api.EventMeshAsyncConsumeContext; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.connector.rocketmq.common.EventMeshConstants; +import org.apache.eventmesh.connector.rocketmq.config.ClientConfig; +import org.apache.eventmesh.connector.rocketmq.domain.NonStandardKeys; +import org.apache.eventmesh.connector.rocketmq.patch.EventMeshConsumeConcurrentlyContext; +import org.apache.eventmesh.connector.rocketmq.patch.EventMeshConsumeConcurrentlyStatus; +import org.apache.eventmesh.connector.rocketmq.patch.EventMeshMessageListenerConcurrently; +import org.apache.eventmesh.connector.rocketmq.utils.BeanUtils; +import org.apache.eventmesh.connector.rocketmq.utils.OMSUtil; +import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer; +import org.apache.rocketmq.client.exception.MQClientException; +import org.apache.rocketmq.common.message.MessageExt; +import org.apache.rocketmq.common.protocol.heartbeat.MessageModel; +import org.apache.rocketmq.remoting.protocol.LanguageCode; + +public class PushConsumerImpl implements Consumer { + private final DefaultMQPushConsumer rocketmqPushConsumer; + private final Properties properties; + private AtomicBoolean started = new AtomicBoolean(false); + private final Map subscribeTable = new ConcurrentHashMap<>(); + private final ClientConfig clientConfig; + + public PushConsumerImpl(final Properties properties) { + this.rocketmqPushConsumer = new DefaultMQPushConsumer(); + this.properties = properties; + this.clientConfig = BeanUtils.populate(properties, ClientConfig.class); + +// if ("true".equalsIgnoreCase(System.getenv("OMS_RMQ_DIRECT_NAME_SRV"))) { +// +// +// } + String accessPoints = clientConfig.getAccessPoints(); + if (accessPoints == null || accessPoints.isEmpty()) { + throw new OMSRuntimeException(-1, "OMS AccessPoints is null or empty."); + } + this.rocketmqPushConsumer.setNamesrvAddr(accessPoints.replace(',', ';')); + String consumerGroup = clientConfig.getConsumerId(); + if (null == consumerGroup || consumerGroup.isEmpty()) { + throw new OMSRuntimeException(-1, "Consumer Group is necessary for RocketMQ, please set it."); + } + this.rocketmqPushConsumer.setConsumerGroup(consumerGroup); + this.rocketmqPushConsumer.setMaxReconsumeTimes(clientConfig.getRmqMaxRedeliveryTimes()); + this.rocketmqPushConsumer.setConsumeTimeout(clientConfig.getRmqMessageConsumeTimeout()); + this.rocketmqPushConsumer.setConsumeThreadMax(clientConfig.getRmqMaxConsumeThreadNums()); + this.rocketmqPushConsumer.setConsumeThreadMin(clientConfig.getRmqMinConsumeThreadNums()); + this.rocketmqPushConsumer.setMessageModel(MessageModel.valueOf(clientConfig.getMessageModel())); + + String consumerId = OMSUtil.buildInstanceName(); + //this.rocketmqPushConsumer.setInstanceName(consumerId); + this.rocketmqPushConsumer.setInstanceName(properties.getProperty("instanceName")); + properties.put("CONSUMER_ID", consumerId); + this.rocketmqPushConsumer.setLanguage(LanguageCode.OMS); + + if (clientConfig.getMessageModel().equalsIgnoreCase(MessageModel.BROADCASTING.name())) { + rocketmqPushConsumer.registerMessageListener(new EventMeshMessageListenerConcurrently() { + + @Override + public EventMeshConsumeConcurrentlyStatus handleMessage(MessageExt msg, EventMeshConsumeConcurrentlyContext context) { + if (msg == null) { + return EventMeshConsumeConcurrentlyStatus.CONSUME_SUCCESS; + } + + +// if (!EventMeshUtil.isValidRMBTopic(msg.getTopic())) { +// return EventMeshConsumeConcurrentlyStatus.CONSUME_SUCCESS; +// } + + msg.putUserProperty(Constants.PROPERTY_MESSAGE_BORN_TIMESTAMP, String.valueOf(msg.getBornTimestamp())); + msg.putUserProperty(Constants.PROPERTY_MESSAGE_STORE_TIMESTAMP, String.valueOf(msg.getStoreTimestamp())); + + Message omsMsg = OMSUtil.msgConvert(msg); + + AsyncMessageListener listener = PushConsumerImpl.this.subscribeTable.get(msg.getTopic()); + + if (listener == null) { + throw new OMSRuntimeException(-1, + String.format("The topic/queue %s isn't attached to this consumer", msg.getTopic())); + } + + final Properties contextProperties = new Properties(); + contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, EventMeshConsumeConcurrentlyStatus.RECONSUME_LATER.name()); + EventMeshAsyncConsumeContext omsContext = new EventMeshAsyncConsumeContext() { + @Override + public void commit(EventMeshAction action) { + switch (action){ + case CommitMessage: + contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, EventMeshConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); + break; + case ReconsumeLater: + contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, EventMeshConsumeConcurrentlyStatus.RECONSUME_LATER.name()); + break; + case ManualAck: + contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, EventMeshConsumeConcurrentlyStatus.CONSUME_FINISH.name()); + break; + default: + break; + } + } + }; + omsContext.setAbstractContext(context); + listener.consume(omsMsg, omsContext); + + return EventMeshConsumeConcurrentlyStatus.valueOf(contextProperties.getProperty(NonStandardKeys.MESSAGE_CONSUME_STATUS)); + } + }); + } else { + rocketmqPushConsumer.registerMessageListener(new EventMeshMessageListenerConcurrently() { + + @Override + public EventMeshConsumeConcurrentlyStatus handleMessage(MessageExt msg, EventMeshConsumeConcurrentlyContext context) { + if (msg == null) { + return EventMeshConsumeConcurrentlyStatus.CONSUME_SUCCESS; + } +// if (!EventMeshUtil.isValidRMBTopic(msg.getTopic())) { +// return EventMeshConsumeConcurrentlyStatus.CONSUME_SUCCESS; +// } + + msg.putUserProperty(Constants.PROPERTY_MESSAGE_BORN_TIMESTAMP, String.valueOf(msg.getBornTimestamp())); + msg.putUserProperty(EventMeshConstants.STORE_TIMESTAMP, String.valueOf(msg.getStoreTimestamp())); + + Message omsMsg = OMSUtil.msgConvert(msg); + + AsyncMessageListener listener = PushConsumerImpl.this.subscribeTable.get(msg.getTopic()); + + if (listener == null) { + throw new OMSRuntimeException(-1, + String.format("The topic/queue %s isn't attached to this consumer", msg.getTopic())); + } + + final Properties contextProperties = new Properties(); + + contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, EventMeshConsumeConcurrentlyStatus.RECONSUME_LATER.name()); + + EventMeshAsyncConsumeContext omsContext = new EventMeshAsyncConsumeContext() { + @Override + public void commit(EventMeshAction action) { + switch (action) { + case CommitMessage: + contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, EventMeshConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); + break; + case ReconsumeLater: + contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, EventMeshConsumeConcurrentlyStatus.RECONSUME_LATER.name()); + break; + case ManualAck: + contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, EventMeshConsumeConcurrentlyStatus.CONSUME_FINISH.name()); + break; + default: + break; + } + } + }; + omsContext.setAbstractContext(context); + listener.consume(omsMsg, omsContext); + + return EventMeshConsumeConcurrentlyStatus.valueOf(contextProperties.getProperty(NonStandardKeys.MESSAGE_CONSUME_STATUS)); + } + }); + } + } + + public Properties attributes() { + return properties; + } + + @Override + public void start() { + if (this.started.compareAndSet(false, true)) { + try { + this.rocketmqPushConsumer.start(); + } catch (Exception e) { + throw new OMSRuntimeException(e.getMessage()); + } + } + } + + @Override + public synchronized void shutdown() { + if (this.started.compareAndSet(true, false)) { + this.rocketmqPushConsumer.shutdown(); + } + } + + @Override + public boolean isStarted() { + return this.started.get(); + } + + @Override + public boolean isClosed() { + return !this.isStarted(); + } + + public DefaultMQPushConsumer getRocketmqPushConsumer() { + return rocketmqPushConsumer; + } + +// class MessageListenerImpl implements MessageListenerConcurrently { +// +// @Override +// public ConsumeConcurrentlyStatus consumeMessage(List rmqMsgList, +// ConsumeConcurrentlyContext contextRMQ) { +// MessageExt rmqMsg = rmqMsgList.get(0); +// BytesMessage omsMsg = OMSUtil.msgConvert(rmqMsg); +// +// MessageListener listener = PushConsumerImpl.this.subscribeTable.get(rmqMsg.getTopic()); +// +// if (listener == null) { +// throw new OMSRuntimeException("-1", +// String.format("The topic/queue %s isn't attached to this consumer", rmqMsg.getTopic())); +// } +// +// final KeyValue contextProperties = OMS.newKeyValue(); +// final CountDownLatch sync = new CountDownLatch(1); +// +// contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ConsumeConcurrentlyStatus.RECONSUME_LATER.name()); +// +// MessageListener.Context context = new MessageListener.Context() { +// @Override +// public KeyValue attributes() { +// return contextProperties; +// } +// +// @Override +// public void ack() { +// sync.countDown(); +// contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, +// ConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); +// } +// }; +// long begin = System.currentTimeMillis(); +// listener.onReceived(omsMsg, context); +// long costs = System.currentTimeMillis() - begin; +// long timeoutMills = clientConfig.getRmqMessageConsumeTimeout() * 60 * 1000; +// try { +// sync.await(Math.max(0, timeoutMills - costs), TimeUnit.MILLISECONDS); +// } catch (InterruptedException ignore) { +// } +// +// return ConsumeConcurrentlyStatus.valueOf(contextProperties.getString(NonStandardKeys.MESSAGE_CONSUME_STATUS)); +// } +// } + + @Override + public void subscribe(String topic, String subExpression, MessageListener listener) { + + } + + @Override + public void subscribe(String topic, MessageSelector selector, MessageListener listener) { + + } + + @Override + public void subscribe(String topic, String subExpression, GenericMessageListener listener) { + + } + + @Override + public void subscribe(String topic, MessageSelector selector, GenericMessageListener listener) { + + } + + @Override + public void subscribe(String topic, String subExpression, AsyncMessageListener listener) { + this.subscribeTable.put(topic, listener); + try { + this.rocketmqPushConsumer.subscribe(topic, subExpression); + } catch (MQClientException e) { + throw new OMSRuntimeException(-1, String.format("RocketMQ push consumer can't attach to %s.", topic)); + } + } + + @Override + public void subscribe(String topic, MessageSelector selector, AsyncMessageListener listener) { + + } + + @Override + public void subscribe(String topic, String subExpression, AsyncGenericMessageListener listener) { + + } + + @Override + public void subscribe(String topic, MessageSelector selector, AsyncGenericMessageListener listener) { + + } + + @Override + public void unsubscribe(String topic) { + this.subscribeTable.remove(topic); + try { + this.rocketmqPushConsumer.unsubscribe(topic); + } catch (Exception e) { + throw new OMSRuntimeException(-1, String.format("RocketMQ push consumer fails to unsubscribe topic: %s", topic)); + } + } + + @Override + public void updateCredential(Properties credentialProperties) { + + } +} diff --git a/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/consumer/RocketMQConsumerImpl.java b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/consumer/RocketMQConsumerImpl.java new file mode 100644 index 0000000000..853c394bef --- /dev/null +++ b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/consumer/RocketMQConsumerImpl.java @@ -0,0 +1,182 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.connector.rocketmq.consumer; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import io.openmessaging.api.AsyncGenericMessageListener; +import io.openmessaging.api.AsyncMessageListener; +import io.openmessaging.api.GenericMessageListener; +import io.openmessaging.api.Message; +import io.openmessaging.api.MessageListener; +import io.openmessaging.api.MessageSelector; +import io.openmessaging.api.MessagingAccessPoint; +import io.openmessaging.api.OMS; +import io.openmessaging.api.OMSBuiltinKeys; + +import org.apache.eventmesh.api.AbstractContext; +import org.apache.eventmesh.api.consumer.MeshMQPushConsumer; +import org.apache.eventmesh.connector.rocketmq.common.Constants; +import org.apache.eventmesh.connector.rocketmq.common.EventMeshConstants; +import org.apache.eventmesh.connector.rocketmq.config.ClientConfiguration; +import org.apache.eventmesh.connector.rocketmq.config.ConfigurationWraper; +import org.apache.eventmesh.connector.rocketmq.patch.EventMeshConsumeConcurrentlyContext; +import org.apache.eventmesh.connector.rocketmq.utils.OMSUtil; +import org.apache.rocketmq.client.impl.consumer.ConsumeMessageConcurrentlyService; +import org.apache.rocketmq.client.impl.consumer.ConsumeMessageService; +import org.apache.rocketmq.common.message.MessageExt; +import org.apache.rocketmq.common.protocol.heartbeat.MessageModel; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RocketMQConsumerImpl implements MeshMQPushConsumer { + + public Logger logger = LoggerFactory.getLogger(this.getClass()); + + public Logger messageLogger = LoggerFactory.getLogger("message"); + + public final String DEFAULT_ACCESS_DRIVER = "org.apache.eventmesh.connector.rocketmq.MessagingAccessPointImpl"; + + private PushConsumerImpl pushConsumer; + + @Override + public synchronized void init(Properties keyValue) throws Exception { + ConfigurationWraper configurationWraper = + new ConfigurationWraper(EventMeshConstants.EVENTMESH_CONF_HOME + + File.separator + + EventMeshConstants.EVENTMESH_CONF_FILE, false); + final ClientConfiguration clientConfiguration = new ClientConfiguration(configurationWraper); + clientConfiguration.init(); + boolean isBroadcast = Boolean.parseBoolean(keyValue.getProperty("isBroadcast")); + String consumerGroup = keyValue.getProperty("consumerGroup"); + String instanceName = keyValue.getProperty("instanceName"); + + + if (isBroadcast) { + consumerGroup = Constants.BROADCAST_PREFIX + consumerGroup; + } + + String omsNamesrv = clientConfiguration.namesrvAddr; +// KeyValue properties = OMS.newKeyValue().put(OMSBuiltinKeys.DRIVER_IMPL, DEFAULT_ACCESS_DRIVER); + Properties properties = new Properties(); + properties.put(OMSBuiltinKeys.DRIVER_IMPL, DEFAULT_ACCESS_DRIVER); + properties.put("ACCESS_POINTS", omsNamesrv); + properties.put("REGION", "namespace"); + properties.put("instanceName", instanceName); + properties.put("CONSUMER_ID", consumerGroup); + if (isBroadcast) { + properties.put("MESSAGE_MODEL", MessageModel.BROADCASTING.name()); + } else { + properties.put("MESSAGE_MODEL", MessageModel.CLUSTERING.name()); + } + MessagingAccessPoint messagingAccessPoint = OMS.builder().build(properties); + pushConsumer = (PushConsumerImpl) messagingAccessPoint.createConsumer(properties); + } + + @Override + public void subscribe(String topic, AsyncMessageListener listener) throws Exception { + pushConsumer.subscribe(topic, "*", listener); + } + + @Override + public boolean isStarted() { + return pushConsumer.isStarted(); + } + + @Override + public boolean isClosed() { + return pushConsumer.isClosed(); + } + + @Override + public synchronized void start() { + pushConsumer.start(); + } + + @Override + public void updateOffset(List msgs, AbstractContext context) { + ConsumeMessageService consumeMessageService = pushConsumer.getRocketmqPushConsumer().getDefaultMQPushConsumerImpl().getConsumeMessageService(); + List msgExtList = new ArrayList<>(msgs.size()); + for (Message msg : msgs) { + msgExtList.add(OMSUtil.msgConvertExt(msg)); + } + ((ConsumeMessageConcurrentlyService) consumeMessageService).updateOffset(msgExtList, (EventMeshConsumeConcurrentlyContext) context); + } + + @Override + public void unsubscribe(String topic) { + pushConsumer.unsubscribe(topic); + } + + @Override + public synchronized void shutdown() { + pushConsumer.shutdown(); + } + + public Properties attributes() { + return pushConsumer.attributes(); + } + + @Override + public void subscribe(String topic, String subExpression, MessageListener listener) { + throw new UnsupportedOperationException("not supported yet"); + } + + @Override + public void subscribe(String topic, MessageSelector selector, MessageListener listener) { + throw new UnsupportedOperationException("not supported yet"); + } + + @Override + public void subscribe(String topic, String subExpression, GenericMessageListener listener) { + throw new UnsupportedOperationException("not supported yet"); + } + + @Override + public void subscribe(String topic, MessageSelector selector, GenericMessageListener listener) { + throw new UnsupportedOperationException("not supported yet"); + } + + @Override + public void subscribe(String topic, String subExpression, AsyncMessageListener listener) { + throw new UnsupportedOperationException("not supported yet"); + } + + @Override + public void subscribe(String topic, MessageSelector selector, AsyncMessageListener listener) { + throw new UnsupportedOperationException("not supported yet"); + } + + @Override + public void subscribe(String topic, String subExpression, AsyncGenericMessageListener listener) { + throw new UnsupportedOperationException("not supported yet"); + } + + @Override + public void subscribe(String topic, MessageSelector selector, AsyncGenericMessageListener listener) { + throw new UnsupportedOperationException("not supported yet"); + } + + @Override + public void updateCredential(Properties credentialProperties) { + + } +} diff --git a/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/domain/ConsumeRequest.java b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/domain/ConsumeRequest.java new file mode 100644 index 0000000000..2bcb32887b --- /dev/null +++ b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/domain/ConsumeRequest.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.connector.rocketmq.domain; + +import org.apache.rocketmq.client.impl.consumer.ProcessQueue; +import org.apache.rocketmq.common.message.MessageExt; +import org.apache.rocketmq.common.message.MessageQueue; + +public class ConsumeRequest { + private final MessageExt messageExt; + private final MessageQueue messageQueue; + private final ProcessQueue processQueue; + private long startConsumeTimeMillis; + + public ConsumeRequest(final MessageExt messageExt, final MessageQueue messageQueue, + final ProcessQueue processQueue) { + this.messageExt = messageExt; + this.messageQueue = messageQueue; + this.processQueue = processQueue; + } + + public MessageExt getMessageExt() { + return messageExt; + } + + public MessageQueue getMessageQueue() { + return messageQueue; + } + + public ProcessQueue getProcessQueue() { + return processQueue; + } + + public long getStartConsumeTimeMillis() { + return startConsumeTimeMillis; + } + + public void setStartConsumeTimeMillis(final long startConsumeTimeMillis) { + this.startConsumeTimeMillis = startConsumeTimeMillis; + } +} diff --git a/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/domain/NonStandardKeys.java b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/domain/NonStandardKeys.java new file mode 100644 index 0000000000..5264d785e0 --- /dev/null +++ b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/domain/NonStandardKeys.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.connector.rocketmq.domain; + +public interface NonStandardKeys { + String CONSUMER_GROUP = "rmq.consumer.group"; + String PRODUCER_GROUP = "rmq.producer.group"; + String MAX_REDELIVERY_TIMES = "rmq.max.redelivery.times"; + String MESSAGE_CONSUME_TIMEOUT = "rmq.message.consume.timeout"; + String MAX_CONSUME_THREAD_NUMS = "rmq.max.consume.thread.nums"; + String MIN_CONSUME_THREAD_NUMS = "rmq.min.consume.thread.nums"; + String MESSAGE_CONSUME_STATUS = "rmq.message.consume.status"; + String MESSAGE_DESTINATION = "rmq.message.destination"; + String PULL_MESSAGE_BATCH_NUMS = "rmq.pull.message.batch.nums"; + String PULL_MESSAGE_CACHE_CAPACITY = "rmq.pull.message.cache.capacity"; +} diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/UnSubscribeResponseHeader.java b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/domain/RocketMQConstants.java similarity index 79% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/UnSubscribeResponseHeader.java rename to eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/domain/RocketMQConstants.java index 8eda4f4d5a..063f0f36fc 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/UnSubscribeResponseHeader.java +++ b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/domain/RocketMQConstants.java @@ -15,7 +15,13 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.header.client; +package org.apache.eventmesh.connector.rocketmq.domain; + +public interface RocketMQConstants { + + /** + * Key of scheduled message delivery time + */ + String START_DELIVER_TIME = "__STARTDELIVERTIME"; -public class UnSubscribeResponseHeader { } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/patch/ProxyConsumeConcurrentlyContext.java b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/patch/EventMeshConsumeConcurrentlyContext.java similarity index 81% rename from eventmesh-emesher/src/main/java/com/webank/emesher/patch/ProxyConsumeConcurrentlyContext.java rename to eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/patch/EventMeshConsumeConcurrentlyContext.java index 81343d86db..05442a6ab2 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/patch/ProxyConsumeConcurrentlyContext.java +++ b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/patch/EventMeshConsumeConcurrentlyContext.java @@ -15,17 +15,18 @@ * limitations under the License. */ -package com.webank.emesher.patch; +package org.apache.eventmesh.connector.rocketmq.patch; +import org.apache.eventmesh.api.AbstractContext; import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext; import org.apache.rocketmq.client.impl.consumer.ProcessQueue; import org.apache.rocketmq.common.message.MessageQueue; -public class ProxyConsumeConcurrentlyContext extends ConsumeConcurrentlyContext { +public class EventMeshConsumeConcurrentlyContext extends ConsumeConcurrentlyContext implements AbstractContext { private final ProcessQueue processQueue; private boolean manualAck = true; - public ProxyConsumeConcurrentlyContext(MessageQueue messageQueue, ProcessQueue processQueue) { + public EventMeshConsumeConcurrentlyContext(MessageQueue messageQueue, ProcessQueue processQueue) { super(messageQueue); this.processQueue = processQueue; } diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/UnSubscribeResponseBody.java b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/patch/EventMeshConsumeConcurrentlyStatus.java similarity index 70% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/UnSubscribeResponseBody.java rename to eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/patch/EventMeshConsumeConcurrentlyStatus.java index 1ecfb884aa..468b14f568 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/UnSubscribeResponseBody.java +++ b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/patch/EventMeshConsumeConcurrentlyStatus.java @@ -15,7 +15,19 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.body.client; +package org.apache.eventmesh.connector.rocketmq.patch; -public class UnSubscribeResponseBody { +public enum EventMeshConsumeConcurrentlyStatus { + /** + * Success consumption + */ + CONSUME_SUCCESS, + /** + * Failure consumption,later try to consume + */ + RECONSUME_LATER, + /** + * Success consumption but ack later manually + */ + CONSUME_FINISH; } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/patch/ProxyMessageListenerConcurrently.java b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/patch/EventMeshMessageListenerConcurrently.java similarity index 75% rename from eventmesh-emesher/src/main/java/com/webank/emesher/patch/ProxyMessageListenerConcurrently.java rename to eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/patch/EventMeshMessageListenerConcurrently.java index 10b80cc455..47e8ee11be 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/patch/ProxyMessageListenerConcurrently.java +++ b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/patch/EventMeshMessageListenerConcurrently.java @@ -15,7 +15,9 @@ * limitations under the License. */ -package com.webank.emesher.patch; +package org.apache.eventmesh.connector.rocketmq.patch; + +import java.util.List; import org.apache.commons.collections4.CollectionUtils; import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext; @@ -25,11 +27,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.List; - -public abstract class ProxyMessageListenerConcurrently implements MessageListenerConcurrently { +public abstract class EventMeshMessageListenerConcurrently implements MessageListenerConcurrently { - private static final Logger LOG = LoggerFactory.getLogger(ProxyMessageListenerConcurrently.class); + private static final Logger LOG = LoggerFactory.getLogger(EventMeshMessageListenerConcurrently.class); @Override public ConsumeConcurrentlyStatus consumeMessage(final List msgs, @@ -42,16 +42,16 @@ public ConsumeConcurrentlyStatus consumeMessage(final List msgs, MessageExt msg = msgs.get(0); try { - ProxyConsumeConcurrentlyContext proxyConsumeConcurrentlyContext = (ProxyConsumeConcurrentlyContext) context; - ProxyConsumeConcurrentlyStatus proxyConsumeStatus = handleMessage(msg, proxyConsumeConcurrentlyContext); + EventMeshConsumeConcurrentlyContext eventMeshConsumeConcurrentlyContext = (EventMeshConsumeConcurrentlyContext) context; + EventMeshConsumeConcurrentlyStatus eventMeshConsumeStatus = handleMessage(msg, eventMeshConsumeConcurrentlyContext); try { - switch (proxyConsumeStatus) { + switch (eventMeshConsumeStatus) { case CONSUME_SUCCESS: return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; case RECONSUME_LATER: return ConsumeConcurrentlyStatus.RECONSUME_LATER; case CONSUME_FINISH: - proxyConsumeConcurrentlyContext.setManualAck(true); + eventMeshConsumeConcurrentlyContext.setManualAck(true); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } } catch (Throwable e) { @@ -65,5 +65,5 @@ public ConsumeConcurrentlyStatus consumeMessage(final List msgs, return status; } - public abstract ProxyConsumeConcurrentlyStatus handleMessage(MessageExt msg, ProxyConsumeConcurrentlyContext context); + public abstract EventMeshConsumeConcurrentlyStatus handleMessage(MessageExt msg, EventMeshConsumeConcurrentlyContext context); } diff --git a/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/producer/AbstractOMSProducer.java b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/producer/AbstractOMSProducer.java new file mode 100644 index 0000000000..548077a4ec --- /dev/null +++ b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/producer/AbstractOMSProducer.java @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.connector.rocketmq.producer; + +import java.util.Properties; +import java.util.concurrent.atomic.AtomicBoolean; + +import io.openmessaging.api.exception.OMSMessageFormatException; +import io.openmessaging.api.exception.OMSRuntimeException; +import io.openmessaging.api.exception.OMSTimeOutException; + +import org.apache.eventmesh.connector.rocketmq.config.ClientConfig; +import org.apache.eventmesh.connector.rocketmq.utils.BeanUtils; +import org.apache.eventmesh.connector.rocketmq.utils.OMSUtil; +import org.apache.rocketmq.client.exception.MQBrokerException; +import org.apache.rocketmq.client.exception.MQClientException; +import org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl; +import org.apache.rocketmq.client.log.ClientLogger; +import org.apache.rocketmq.client.producer.DefaultMQProducer; +import org.apache.rocketmq.common.protocol.ResponseCode; +import org.apache.rocketmq.logging.InternalLogger; +import org.apache.rocketmq.remoting.exception.RemotingConnectException; +import org.apache.rocketmq.remoting.exception.RemotingTimeoutException; +import org.apache.rocketmq.remoting.protocol.LanguageCode; + +public abstract class AbstractOMSProducer { + + final static InternalLogger log = ClientLogger.getLog(); + final Properties properties; + final DefaultMQProducer rocketmqProducer; + protected final AtomicBoolean started = new AtomicBoolean(false); + // private boolean started = false; + private final ClientConfig clientConfig; + private final String PRODUCER_ID = "PRODUCER_ID"; + + AbstractOMSProducer(final Properties properties) { + this.properties = properties; + this.rocketmqProducer = new DefaultMQProducer(); + this.clientConfig = BeanUtils.populate(properties, ClientConfig.class); + + String accessPoints = clientConfig.getAccessPoints(); + if (accessPoints == null || accessPoints.isEmpty()) { + throw new OMSRuntimeException(-1, "OMS AccessPoints is null or empty."); + } + + this.rocketmqProducer.setNamesrvAddr(accessPoints.replace(',', ';')); + + this.rocketmqProducer.setProducerGroup(clientConfig.getRmqProducerGroup()); + + String producerId = OMSUtil.buildInstanceName(); + this.rocketmqProducer.setSendMsgTimeout(clientConfig.getOperationTimeout()); + this.rocketmqProducer.setInstanceName(producerId); + this.rocketmqProducer.setMaxMessageSize(1024 * 1024 * 4); + this.rocketmqProducer.setLanguage(LanguageCode.OMS); + properties.put(PRODUCER_ID, producerId); + } + + public synchronized void start() { + if (!started.get()) { + try { + this.rocketmqProducer.start(); + } catch (MQClientException e) { + throw new OMSRuntimeException("-1", e); + } + } + this.started.set(true); + } + + public synchronized void shutdown() { + if (this.started.get()) { + this.rocketmqProducer.shutdown(); + } + this.started.set(false); + } + + public boolean isStarted() { + return this.started.get(); + } + + public boolean isClosed() { + return !this.isStarted(); + } + + OMSRuntimeException checkProducerException(String topic, String msgId, Throwable e) { + if (e instanceof MQClientException) { + if (e.getCause() != null) { + if (e.getCause() instanceof RemotingTimeoutException) { + return new OMSTimeOutException(-1, String.format("Send message to broker timeout, %dms, Topic=%s, msgId=%s", + this.rocketmqProducer.getSendMsgTimeout(), topic, msgId), e); + } else if (e.getCause() instanceof MQBrokerException || e.getCause() instanceof RemotingConnectException) { + if (e.getCause() instanceof MQBrokerException) { + MQBrokerException brokerException = (MQBrokerException) e.getCause(); + return new OMSRuntimeException(-1, String.format("Received a broker exception, Topic=%s, msgId=%s, %s", + topic, msgId, brokerException.getErrorMessage()), e); + } + + if (e.getCause() instanceof RemotingConnectException) { + RemotingConnectException connectException = (RemotingConnectException) e.getCause(); + return new OMSRuntimeException(-1, + String.format("Network connection experiences failures. Topic=%s, msgId=%s, %s", + topic, msgId, connectException.getMessage()), + e); + } + } + } + // Exception thrown by local. + else { + MQClientException clientException = (MQClientException) e; + if (-1 == clientException.getResponseCode()) { + return new OMSRuntimeException(-1, String.format("Topic does not exist, Topic=%s, msgId=%s", + topic, msgId), e); + } else if (ResponseCode.MESSAGE_ILLEGAL == clientException.getResponseCode()) { + return new OMSMessageFormatException(-1, String.format("A illegal message for RocketMQ, Topic=%s, msgId=%s", + topic, msgId), e); + } + } + } + return new OMSRuntimeException(-1, "Send message to RocketMQ broker failed.", e); + } + + protected void checkProducerServiceState(DefaultMQProducerImpl producer) { + switch (producer.getServiceState()) { + case CREATE_JUST: + throw new OMSRuntimeException(String.format("You do not have start the producer, %s", producer.getServiceState())); + case SHUTDOWN_ALREADY: + throw new OMSRuntimeException(String.format("Your producer has been shut down, %s", producer.getServiceState())); + case START_FAILED: + throw new OMSRuntimeException(String.format("When you start your service throws an exception, %s", producer.getServiceState())); + case RUNNING: + default: + } + } + + public DefaultMQProducer getRocketmqProducer() { + return rocketmqProducer; + } +} diff --git a/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/producer/ProducerImpl.java b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/producer/ProducerImpl.java new file mode 100644 index 0000000000..f8d4302d02 --- /dev/null +++ b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/producer/ProducerImpl.java @@ -0,0 +1,139 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.connector.rocketmq.producer; + +import java.util.Properties; +import java.util.concurrent.ExecutorService; + +import io.openmessaging.api.Message; +import io.openmessaging.api.MessageBuilder; +import io.openmessaging.api.OnExceptionContext; +import io.openmessaging.api.Producer; +import io.openmessaging.api.SendCallback; +import io.openmessaging.api.SendResult; +import io.openmessaging.api.exception.OMSRuntimeException; + +import org.apache.eventmesh.connector.rocketmq.utils.OMSUtil; +import org.apache.rocketmq.common.message.MessageClientIDSetter; + + +public class ProducerImpl extends AbstractOMSProducer implements Producer { + + public static final int eventMeshServerAsyncAccumulationThreshold = 1000; + + public ProducerImpl(final Properties properties) { + super(properties); + } + + public Properties attributes() { + return properties; + } + + public void setExtFields() { + super.getRocketmqProducer().setRetryTimesWhenSendFailed(0); + super.getRocketmqProducer().setRetryTimesWhenSendAsyncFailed(0); + super.getRocketmqProducer().setPollNameServerInterval(60000); + + super.getRocketmqProducer().getDefaultMQProducerImpl().getmQClientFactory() + .getNettyClientConfig().setClientAsyncSemaphoreValue(eventMeshServerAsyncAccumulationThreshold); + super.getRocketmqProducer().setCompressMsgBodyOverHowmuch(10); + } + + @Override + public SendResult send(Message message) { + this.checkProducerServiceState(rocketmqProducer.getDefaultMQProducerImpl()); + org.apache.rocketmq.common.message.Message msgRMQ = OMSUtil.msgConvert(message); + + try { + org.apache.rocketmq.client.producer.SendResult sendResultRMQ = this.rocketmqProducer.send(msgRMQ); + message.setMsgID(sendResultRMQ.getMsgId()); + SendResult sendResult = new SendResult(); + sendResult.setTopic(sendResultRMQ.getMessageQueue().getTopic()); + sendResult.setMessageId(sendResultRMQ.getMsgId()); + return sendResult; + } catch (Exception e) { + log.error(String.format("Send message Exception, %s", message), e); + throw this.checkProducerException(message.getTopic(), message.getMsgID(), e); + } + } + + @Override + public void sendOneway(Message message) { + this.checkProducerServiceState(this.rocketmqProducer.getDefaultMQProducerImpl()); + org.apache.rocketmq.common.message.Message msgRMQ = OMSUtil.msgConvert(message); + + try { + this.rocketmqProducer.sendOneway(msgRMQ); + message.setMsgID(MessageClientIDSetter.getUniqID(msgRMQ)); + } catch (Exception e) { + log.error(String.format("Send message oneway Exception, %s", message), e); + throw this.checkProducerException(message.getTopic(), message.getMsgID(), e); + } + } + + @Override + public void sendAsync(Message message, SendCallback sendCallback) { + this.checkProducerServiceState(this.rocketmqProducer.getDefaultMQProducerImpl()); + org.apache.rocketmq.common.message.Message msgRMQ = OMSUtil.msgConvert(message); + + try { + this.rocketmqProducer.send(msgRMQ, this.sendCallbackConvert(message, sendCallback)); + message.setMsgID(MessageClientIDSetter.getUniqID(msgRMQ)); + } catch (Exception e) { + log.error(String.format("Send message async Exception, %s", message), e); + throw this.checkProducerException(message.getTopic(), message.getMsgID(), e); + } + } + + private org.apache.rocketmq.client.producer.SendCallback sendCallbackConvert(final Message message, final SendCallback sendCallback) { + org.apache.rocketmq.client.producer.SendCallback rmqSendCallback = new org.apache.rocketmq.client.producer.SendCallback() { + @Override + public void onSuccess(org.apache.rocketmq.client.producer.SendResult sendResult) { + sendCallback.onSuccess(OMSUtil.sendResultConvert(sendResult)); + } + + @Override + public void onException(Throwable e) { + String topic = message.getTopic(); + String msgId = message.getMsgID(); + OMSRuntimeException onsEx = ProducerImpl.this.checkProducerException(topic, msgId, e); + OnExceptionContext context = new OnExceptionContext(); + context.setTopic(topic); + context.setMessageId(msgId); + context.setException(onsEx); + sendCallback.onException(context); + } + }; + return rmqSendCallback; + } + + @Override + public void setCallbackExecutor(ExecutorService callbackExecutor) { +// this.rocketmqProducer.setCallbackExecutor(callbackExecutor); + } + + @Override + public void updateCredential(Properties credentialProperties) { + + } + + @Override + public MessageBuilder messageBuilder() { + return null; + } +} diff --git a/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/producer/RocketMQProducerImpl.java b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/producer/RocketMQProducerImpl.java new file mode 100644 index 0000000000..72e20938b9 --- /dev/null +++ b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/producer/RocketMQProducerImpl.java @@ -0,0 +1,155 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.connector.rocketmq.producer; + +import java.io.File; +import java.util.Properties; +import java.util.concurrent.ExecutorService; + +import io.openmessaging.api.Message; +import io.openmessaging.api.MessageBuilder; +import io.openmessaging.api.MessagingAccessPoint; +import io.openmessaging.api.OMS; +import io.openmessaging.api.OMSBuiltinKeys; +import io.openmessaging.api.SendCallback; +import io.openmessaging.api.SendResult; + +import org.apache.eventmesh.api.RRCallback; +import org.apache.eventmesh.api.producer.MeshMQProducer; +import org.apache.eventmesh.connector.rocketmq.common.EventMeshConstants; +import org.apache.eventmesh.connector.rocketmq.config.ClientConfiguration; +import org.apache.eventmesh.connector.rocketmq.config.ConfigurationWraper; +import org.apache.rocketmq.client.exception.MQBrokerException; +import org.apache.rocketmq.client.exception.MQClientException; +import org.apache.rocketmq.remoting.exception.RemotingException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RocketMQProducerImpl implements MeshMQProducer { + + public Logger logger = LoggerFactory.getLogger(this.getClass()); + + private ProducerImpl producer; + + public final String DEFAULT_ACCESS_DRIVER = "org.apache.eventmesh.connector.rocketmq.MessagingAccessPointImpl"; + + @Override + public synchronized void init(Properties keyValue) { + ConfigurationWraper configurationWraper = + new ConfigurationWraper(EventMeshConstants.EVENTMESH_CONF_HOME + + File.separator + + EventMeshConstants.EVENTMESH_CONF_FILE, false); + final ClientConfiguration clientConfiguration = new ClientConfiguration(configurationWraper); + clientConfiguration.init(); + String producerGroup = keyValue.getProperty("producerGroup"); + + String omsNamesrv = clientConfiguration.namesrvAddr; + Properties properties = new Properties(); + properties.put(OMSBuiltinKeys.DRIVER_IMPL, DEFAULT_ACCESS_DRIVER); + properties.put("ACCESS_POINTS", omsNamesrv); + properties.put("REGION", "namespace"); + properties.put("RMQ_PRODUCER_GROUP", producerGroup); + properties.put("OPERATION_TIMEOUT", 3000); + properties.put("PRODUCER_ID", producerGroup); + + MessagingAccessPoint messagingAccessPoint = OMS.builder().build(properties); + producer = (ProducerImpl) messagingAccessPoint.createProducer(properties); + + } + + @Override + public boolean isStarted() { + return producer.isStarted(); + } + + @Override + public boolean isClosed() { + return producer.isClosed(); + } + + @Override + public void start() { + producer.start(); + } + + @Override + public synchronized void shutdown() { + producer.shutdown(); + } + + @Override + public void send(Message message, SendCallback sendCallback) throws Exception { + producer.sendAsync(message, sendCallback); + } + + @Override + public void request(Message message, SendCallback sendCallback, RRCallback rrCallback, long timeout) + throws InterruptedException, RemotingException, MQClientException, MQBrokerException { + throw new UnsupportedOperationException("not support request-reply mode when eventstore=rocketmq"); + } + + @Override + public Message request(Message message, long timeout) throws InterruptedException, RemotingException, MQClientException, MQBrokerException { + throw new UnsupportedOperationException("not support request-reply mode when eventstore=rocketmq"); + } + + @Override + public boolean reply(final Message message, final SendCallback sendCallback) throws Exception { + throw new UnsupportedOperationException("not support request-reply mode when eventstore=rocketmq"); + } + + @Override + public void checkTopicExist(String topic) throws Exception { + this.producer.getRocketmqProducer().getDefaultMQProducerImpl().getmQClientFactory().getMQClientAPIImpl().getDefaultTopicRouteInfoFromNameServer(topic, EventMeshConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS); + } + + @Override + public void setExtFields() { + producer.setExtFields(); + } + + @Override + public SendResult send(Message message) { + return producer.send(message); + } + + @Override + public void sendOneway(Message message) { + producer.sendOneway(message); + } + + @Override + public void sendAsync(Message message, SendCallback sendCallback) { + producer.sendAsync(message, sendCallback); + } + + @Override + public void setCallbackExecutor(ExecutorService callbackExecutor) { + producer.setCallbackExecutor(callbackExecutor); + } + + @Override + public void updateCredential(Properties credentialProperties) { + producer.updateCredential(credentialProperties); + } + + @Override + public MessageBuilder messageBuilder() { + return null; + } +} diff --git a/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/promise/DefaultPromise.java b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/promise/DefaultPromise.java new file mode 100644 index 0000000000..cc30eb5ab0 --- /dev/null +++ b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/promise/DefaultPromise.java @@ -0,0 +1,227 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.connector.rocketmq.promise; + +import java.util.ArrayList; +import java.util.List; + +import io.openmessaging.api.Future; +import io.openmessaging.api.FutureListener; +import io.openmessaging.api.exception.OMSRuntimeException; + +import org.apache.rocketmq.logging.InternalLogger; +import org.apache.rocketmq.logging.InternalLoggerFactory; + +public class DefaultPromise implements Future { + private static final InternalLogger LOG = InternalLoggerFactory.getLogger(DefaultPromise.class); + private final Object lock = new Object(); + private volatile FutureState state = FutureState.DOING; + private V result = null; + private long timeout; + private long createTime; + private Throwable exception = null; + private List> promiseListenerList; + + public DefaultPromise() { + createTime = System.currentTimeMillis(); + promiseListenerList = new ArrayList<>(); + timeout = 5000; + } + + @Override + public boolean cancel(final boolean mayInterruptIfRunning) { + return false; + } + + @Override + public boolean isCancelled() { + return state.isCancelledState(); + } + + @Override + public boolean isDone() { + return state.isDoneState(); + } + + @Override + public V get() { + return result; + } + + @Override + public V get(final long timeout) { + synchronized (lock) { + if (!isDoing()) { + return getValueOrThrowable(); + } + + if (timeout <= 0) { + try { + lock.wait(); + } catch (Exception e) { + cancel(e); + } + return getValueOrThrowable(); + } else { + long waitTime = timeout - (System.currentTimeMillis() - createTime); + if (waitTime > 0) { + for (; ; ) { + try { + lock.wait(waitTime); + } catch (InterruptedException e) { + LOG.error("promise get value interrupted,excepiton:{}", e.getMessage()); + } + + if (!isDoing()) { + break; + } else { + waitTime = timeout - (System.currentTimeMillis() - createTime); + if (waitTime <= 0) { + break; + } + } + } + } + + if (isDoing()) { + timeoutSoCancel(); + } + } + return getValueOrThrowable(); + } + } + + public boolean set(final V value) { + if (value == null) { + return false; + } + this.result = value; + return done(); + } + + public boolean setFailure(final Throwable cause) { + if (cause == null) { + return false; + } + this.exception = cause; + return done(); + } + + @Override + public void addListener(final FutureListener listener) { + if (listener == null) { + throw new NullPointerException("FutureListener is null"); + } + + boolean notifyNow = false; + synchronized (lock) { + if (!isDoing()) { + notifyNow = true; + } else { + if (promiseListenerList == null) { + promiseListenerList = new ArrayList<>(); + } + promiseListenerList.add(listener); + } + } + + if (notifyNow) { + notifyListener(listener); + } + } + + @Override + public Throwable getThrowable() { + return exception; + } + + private void notifyListeners() { + if (promiseListenerList != null) { + for (FutureListener listener : promiseListenerList) { + notifyListener(listener); + } + } + } + + private boolean isSuccess() { + return isDone() && (exception == null); + } + + private void timeoutSoCancel() { + synchronized (lock) { + if (!isDoing()) { + return; + } + state = FutureState.CANCELLED; + exception = new RuntimeException("Get request result is timeout or interrupted"); + lock.notifyAll(); + } + notifyListeners(); + } + + private V getValueOrThrowable() { + if (exception != null) { + Throwable e = exception.getCause() != null ? exception.getCause() : exception; + throw new OMSRuntimeException("-1", e); + } + notifyListeners(); + return result; + } + + private boolean isDoing() { + return state.isDoingState(); + } + + private boolean done() { + synchronized (lock) { + if (!isDoing()) { + return false; + } + + state = FutureState.DONE; + lock.notifyAll(); + } + + notifyListeners(); + return true; + } + + private void notifyListener(final FutureListener listener) { + try { + listener.operationComplete(this); + } catch (Throwable t) { + LOG.error("notifyListener {} Error:{}", listener.getClass().getSimpleName(), t); + } + } + + private boolean cancel(Exception e) { + synchronized (lock) { + if (!isDoing()) { + return false; + } + + state = FutureState.CANCELLED; + exception = e; + lock.notifyAll(); + } + + notifyListeners(); + return true; + } +} + diff --git a/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/promise/FutureState.java b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/promise/FutureState.java new file mode 100644 index 0000000000..1f364a89fc --- /dev/null +++ b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/promise/FutureState.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.connector.rocketmq.promise; + +public enum FutureState { + /** + * the task is doing + **/ + DOING(0), + /** + * the task is done + **/ + DONE(1), + /** + * ths task is cancelled + **/ + CANCELLED(2); + + public final int value; + + private FutureState(int value) { + this.value = value; + } + + public boolean isCancelledState() { + return this == CANCELLED; + } + + public boolean isDoneState() { + return this == DONE; + } + + public boolean isDoingState() { + return this == DOING; + } +} diff --git a/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/utils/BeanUtils.java b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/utils/BeanUtils.java new file mode 100644 index 0000000000..0aae801dcf --- /dev/null +++ b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/utils/BeanUtils.java @@ -0,0 +1,187 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.connector.rocketmq.utils; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; +import java.util.Set; + +import org.apache.commons.lang3.StringUtils; +import org.apache.rocketmq.client.log.ClientLogger; +import org.apache.rocketmq.logging.InternalLogger; + +public final class BeanUtils { + final static InternalLogger log = ClientLogger.getLog(); + + /** + * Maps primitive {@code Class}es to their corresponding wrapper {@code Class}. + */ + private static Map, Class> primitiveWrapperMap = new HashMap, Class>(); + + static { + primitiveWrapperMap.put(Boolean.TYPE, Boolean.class); + primitiveWrapperMap.put(Byte.TYPE, Byte.class); + primitiveWrapperMap.put(Character.TYPE, Character.class); + primitiveWrapperMap.put(Short.TYPE, Short.class); + primitiveWrapperMap.put(Integer.TYPE, Integer.class); + primitiveWrapperMap.put(Long.TYPE, Long.class); + primitiveWrapperMap.put(Double.TYPE, Double.class); + primitiveWrapperMap.put(Float.TYPE, Float.class); + primitiveWrapperMap.put(Void.TYPE, Void.TYPE); + } + + private static Map, Class> wrapperMap = new HashMap, Class>(); + + static { + for (final Class primitiveClass : primitiveWrapperMap.keySet()) { + final Class wrapperClass = primitiveWrapperMap.get(primitiveClass); + if (!primitiveClass.equals(wrapperClass)) { + wrapperMap.put(wrapperClass, primitiveClass); + } + } + wrapperMap.put(String.class, String.class); + } + + /** + *

Populate the JavaBeans properties of the specified bean, based on + * the specified name/value pairs. This method uses Java reflection APIs + * to identify corresponding "property setter" method names, and deals + * with setter arguments of type String, boolean, + * int, long, float, and + * double.

+ * + *

The particular setter method to be called for each property is + * determined using the usual JavaBeans introspection mechanisms. Thus, + * you may identify custom setter methods using a BeanInfo class that is + * associated with the class of the bean itself. If no such BeanInfo + * class is available, the standard method name conversion ("set" plus + * the capitalized name of the property in question) is used.

+ * + *

NOTE: It is contrary to the JavaBeans Specification + * to have more than one setter method (with different argument + * signatures) for the same property.

+ * + * @param clazz JavaBean class whose properties are being populated + * @param properties Map keyed by property name, with the corresponding (String or String[]) value(s) to be set + * @param Class type + * @return Class instance + */ + public static T populate(final Properties properties, final Class clazz) { + T obj = null; + try { + obj = clazz.getDeclaredConstructor().newInstance(); + return populate(properties, obj); + } catch (Throwable e) { + log.warn("Error occurs !", e); + } + return obj; + } + +// public static T populate(final KeyValue properties, final Class clazz) { +// T obj = null; +// try { +// obj = clazz.newInstance(); +// return populate(properties, obj); +// } catch (Throwable e) { +// log.warn("Error occurs !", e); +// } +// return obj; +// } + + public static Class getMethodClass(Class clazz, String methodName) { + Method[] methods = clazz.getMethods(); + for (Method method : methods) { + if (method.getName().equalsIgnoreCase(methodName)) { + return method.getParameterTypes()[0]; + } + } + return null; + } + + public static void setProperties(Class clazz, Object obj, String methodName, + Object value) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + Class parameterClass = getMethodClass(clazz, methodName); + Method setterMethod = clazz.getMethod(methodName, parameterClass); + if (parameterClass == Boolean.TYPE) { + setterMethod.invoke(obj, Boolean.valueOf(value.toString())); + } else if (parameterClass == Integer.TYPE) { + setterMethod.invoke(obj, Integer.valueOf(value.toString())); + } else if (parameterClass == Double.TYPE) { + setterMethod.invoke(obj, Double.valueOf(value.toString())); + } else if (parameterClass == Float.TYPE) { + setterMethod.invoke(obj, Float.valueOf(value.toString())); + } else if (parameterClass == Long.TYPE) { + setterMethod.invoke(obj, Long.valueOf(value.toString())); + } else { + setterMethod.invoke(obj, value); + } + } + + public static T populate(final Properties properties, final T obj) { + Class clazz = obj.getClass(); + try { + + Set> entries = properties.entrySet(); + for (Map.Entry entry : entries) { + String entryKey = entry.getKey().toString(); + String[] keyGroup = entryKey.split("[\\._]"); + for (int i = 0; i < keyGroup.length; i++) { + keyGroup[i] = keyGroup[i].toLowerCase(); + keyGroup[i] = StringUtils.capitalize(keyGroup[i]); + } + String beanFieldNameWithCapitalization = StringUtils.join(keyGroup); + try { + setProperties(clazz, obj, "set" + beanFieldNameWithCapitalization, entry.getValue()); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) { + //ignored... + } + } + } catch (RuntimeException e) { + log.warn("Error occurs !", e); + } + return obj; + } + +// public static T populate(final Properties properties, final T obj) { +// Class clazz = obj.getClass(); +// try { +// +// final Set keySet = properties.keySet(); +// for (Object key : keySet) { +// String[] keyGroup = key.toString().split("[\\._]"); +// for (int i = 0; i < keyGroup.length; i++) { +// keyGroup[i] = keyGroup[i].toLowerCase(); +// keyGroup[i] = StringUtils.capitalize(keyGroup[i]); +// } +// String beanFieldNameWithCapitalization = StringUtils.join(keyGroup); +// try { +// setProperties(clazz, obj, "set" + beanFieldNameWithCapitalization, properties.getProperty(key.toString())); +// } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) { +// //ignored... +// } +// } +// } catch (RuntimeException e) { +// log.warn("Error occurs !", e); +// } +// return obj; +// } +} + diff --git a/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/utils/OMSUtil.java b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/utils/OMSUtil.java new file mode 100644 index 0000000000..ba35acf186 --- /dev/null +++ b/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/utils/OMSUtil.java @@ -0,0 +1,286 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.connector.rocketmq.utils; + +import java.lang.reflect.Field; +import java.util.Iterator; +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.Properties; +import java.util.Set; + +import io.openmessaging.api.Message; +import io.openmessaging.api.OMSBuiltinKeys; +import io.openmessaging.api.SendResult; +import io.openmessaging.api.exception.OMSRuntimeException; + +import org.apache.eventmesh.common.Constants; +import org.apache.rocketmq.common.UtilAll; +import org.apache.rocketmq.common.message.MessageAccessor; +import org.apache.rocketmq.common.message.MessageExt; + +public class OMSUtil { + + /** + * Builds a OMS client instance name. + * + * @return a unique instance name + */ + public static String buildInstanceName() { + return Integer.toString(UtilAll.getPid()) + "%OpenMessaging" + "%" + System.nanoTime(); + } + + public static org.apache.rocketmq.common.message.Message msgConvert(Message omsMessage) { + org.apache.rocketmq.common.message.Message rmqMessage = new org.apache.rocketmq.common.message.Message(); + if (omsMessage == null) { + throw new OMSRuntimeException("'message' is null"); + } else { + if (omsMessage.getTopic() != null) { + rmqMessage.setTopic(omsMessage.getTopic()); + } + if (omsMessage.getKey() != null) { + rmqMessage.setKeys(omsMessage.getKey()); + } + if (omsMessage.getTag() != null) { + rmqMessage.setTags(omsMessage.getTag()); + } + if (omsMessage.getStartDeliverTime() > 0L) { + rmqMessage.putUserProperty("TIMER_DELIVER_MS", String.valueOf(omsMessage.getStartDeliverTime())); + } + + if (omsMessage.getBody() != null) { + rmqMessage.setBody(omsMessage.getBody()); + } + + if (omsMessage.getShardingKey() != null && !omsMessage.getShardingKey().isEmpty()) { + rmqMessage.putUserProperty("__SHARDINGKEY", omsMessage.getShardingKey()); + } + } + Properties systemProperties = omsMessage.getSystemProperties(); + Properties userProperties = omsMessage.getUserProperties(); + + //All destinations in RocketMQ use Topic +// rmqMessage.setTopic(systemProperties.getProperty(BuiltinKeys.DESTINATION)); + +// if (sysHeaders.containsKey(BuiltinKeys.START_TIME)) { +// long deliverTime = sysHeaders.getLong(BuiltinKeys.START_TIME, 0); +// if (deliverTime > 0) { +// rmqMessage.putUserProperty(RocketMQConstants.START_DELIVER_TIME, String.valueOf(deliverTime)); +// } +// } + + for (String key : userProperties.stringPropertyNames()) { + MessageAccessor.putProperty(rmqMessage, key, userProperties.getProperty(key)); + } + + //System headers has a high priority + for (String key : systemProperties.stringPropertyNames()) { + MessageAccessor.putProperty(rmqMessage, key, systemProperties.getProperty(key)); + } + + return rmqMessage; + } + + public static Message msgConvert(MessageExt rmqMsg) { + Message message = new Message(); + if (rmqMsg.getTopic() != null) { + message.setTopic(rmqMsg.getTopic()); + } + + if (rmqMsg.getKeys() != null) { + message.setKey(rmqMsg.getKeys()); + } + + if (rmqMsg.getTags() != null) { + message.setTag(rmqMsg.getTags()); + } + + if (rmqMsg.getBody() != null) { + message.setBody(rmqMsg.getBody()); + } + + if (rmqMsg.getUserProperty("TIMER_DELIVER_MS") != null) { + long ms = Long.parseLong(rmqMsg.getUserProperty("TIMER_DELIVER_MS")); + rmqMsg.getProperties().remove("TIMER_DELIVER_MS"); + message.setStartDeliverTime(ms); + } + + Properties systemProperties = new Properties(); + Properties userProperties = new Properties(); + + + final Set> entries = rmqMsg.getProperties().entrySet(); + + for (final Map.Entry entry : entries) { + if (isOMSHeader(entry.getKey())) { + //sysHeader + systemProperties.put(entry.getKey(), entry.getValue()); + } else { + //userHeader + userProperties.put(entry.getKey(), entry.getValue()); + } + } + + systemProperties.put(Constants.PROPERTY_MESSAGE_MESSAGE_ID, rmqMsg.getMsgId()); + + systemProperties.put(Constants.PROPERTY_MESSAGE_DESTINATION, rmqMsg.getTopic()); + +// omsMsg.putSysHeaders(BuiltinKeys.SEARCH_KEYS, rmqMsg.getKeys()); + systemProperties.put(Constants.PROPERTY_MESSAGE_BORN_HOST, String.valueOf(rmqMsg.getBornHost())); + systemProperties.put(Constants.PROPERTY_MESSAGE_BORN_TIMESTAMP, rmqMsg.getBornTimestamp()); + systemProperties.put(Constants.PROPERTY_MESSAGE_STORE_HOST, String.valueOf(rmqMsg.getStoreHost())); + systemProperties.put("STORE_TIMESTAMP", rmqMsg.getStoreTimestamp()); + + //use in manual ack + userProperties.put(Constants.PROPERTY_MESSAGE_QUEUE_ID, rmqMsg.getQueueId()); + userProperties.put(Constants.PROPERTY_MESSAGE_QUEUE_OFFSET, rmqMsg.getQueueOffset()); + + message.setSystemProperties(systemProperties); + message.setUserProperties(userProperties); + + return message; + } + + public static org.apache.rocketmq.common.message.MessageExt msgConvertExt(Message omsMessage) { + + org.apache.rocketmq.common.message.MessageExt rmqMessageExt = new org.apache.rocketmq.common.message.MessageExt(); + try { + if (omsMessage.getKey() != null) { + rmqMessageExt.setKeys(omsMessage.getKey()); + } + if (omsMessage.getTag() != null) { + rmqMessageExt.setTags(omsMessage.getTag()); + } + if (omsMessage.getStartDeliverTime() > 0L) { + rmqMessageExt.putUserProperty("TIMER_DELIVER_MS", String.valueOf(omsMessage.getStartDeliverTime())); + } + + if (omsMessage.getBody() != null) { + rmqMessageExt.setBody(omsMessage.getBody()); + } + + if (omsMessage.getShardingKey() != null && !omsMessage.getShardingKey().isEmpty()) { + rmqMessageExt.putUserProperty("__SHARDINGKEY", omsMessage.getShardingKey()); + } + + Properties systemProperties = omsMessage.getSystemProperties(); + Properties userProperties = omsMessage.getUserProperties(); + + //All destinations in RocketMQ use Topic + rmqMessageExt.setTopic(omsMessage.getTopic()); + + int queueId = (int) userProperties.get(Constants.PROPERTY_MESSAGE_QUEUE_ID); + long queueOffset = (long) userProperties.get(Constants.PROPERTY_MESSAGE_QUEUE_OFFSET); + //use in manual ack + rmqMessageExt.setQueueId(queueId); + rmqMessageExt.setQueueOffset(queueOffset); + + for (String key : userProperties.stringPropertyNames()) { + MessageAccessor.putProperty(rmqMessageExt, key, userProperties.getProperty(key)); + } + + //System headers has a high priority + for (String key : systemProperties.stringPropertyNames()) { + MessageAccessor.putProperty(rmqMessageExt, key, systemProperties.getProperty(key)); + } + + } catch (Exception e) { + e.printStackTrace(); + } + return rmqMessageExt; + + } + + public static boolean isOMSHeader(String value) { + for (Field field : OMSBuiltinKeys.class.getDeclaredFields()) { + try { + if (field.get(OMSBuiltinKeys.class).equals(value)) { + return true; + } + } catch (IllegalAccessException e) { + return false; + } + } + return false; + } + + /** + * Convert a RocketMQ SEND_OK SendResult instance to a OMS SendResult. + */ + public static SendResult sendResultConvert(org.apache.rocketmq.client.producer.SendResult rmqResult) { + SendResult sendResult = new SendResult(); + sendResult.setTopic(rmqResult.getMessageQueue().getTopic()); + sendResult.setMessageId(rmqResult.getMsgId()); + return sendResult; + } + +// public static KeyValue buildKeyValue(KeyValue... keyValues) { +// KeyValue keyValue = OMS.newKeyValue(); +// for (KeyValue properties : keyValues) { +// for (String key : properties.keySet()) { +// keyValue.put(key, properties.getString(key)); +// } +// } +// return keyValue; +// } + + /** + * Returns an iterator that cycles indefinitely over the elements of {@code Iterable}. + */ + public static Iterator cycle(final Iterable iterable) { + return new Iterator() { + Iterator iterator = new Iterator() { + @Override + public synchronized boolean hasNext() { + return false; + } + + @Override + public synchronized T next() { + throw new NoSuchElementException(); + } + + @Override + public synchronized void remove() { + //Ignore + } + }; + + @Override + public synchronized boolean hasNext() { + return iterator.hasNext() || iterable.iterator().hasNext(); + } + + @Override + public synchronized T next() { + if (!iterator.hasNext()) { + iterator = iterable.iterator(); + if (!iterator.hasNext()) { + throw new NoSuchElementException(); + } + } + return iterator.next(); + } + + @Override + public synchronized void remove() { + iterator.remove(); + } + }; + } +} diff --git a/eventmesh-emesher/src/main/java/org/apache/rocketmq/client/impl/consumer/ConsumeMessageConcurrentlyService.java b/eventmesh-connector-rocketmq/src/main/java/org/apache/rocketmq/client/impl/consumer/ConsumeMessageConcurrentlyService.java similarity index 94% rename from eventmesh-emesher/src/main/java/org/apache/rocketmq/client/impl/consumer/ConsumeMessageConcurrentlyService.java rename to eventmesh-connector-rocketmq/src/main/java/org/apache/rocketmq/client/impl/consumer/ConsumeMessageConcurrentlyService.java index 902bf0dc34..95c2fc9655 100644 --- a/eventmesh-emesher/src/main/java/org/apache/rocketmq/client/impl/consumer/ConsumeMessageConcurrentlyService.java +++ b/eventmesh-connector-rocketmq/src/main/java/org/apache/rocketmq/client/impl/consumer/ConsumeMessageConcurrentlyService.java @@ -14,9 +14,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.apache.rocketmq.client.impl.consumer; -import com.webank.emesher.patch.ProxyConsumeConcurrentlyContext; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Executors; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +import org.apache.eventmesh.connector.rocketmq.patch.EventMeshConsumeConcurrentlyContext; import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer; import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext; import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus; @@ -36,9 +51,6 @@ import org.apache.rocketmq.logging.InternalLogger; import org.apache.rocketmq.remoting.common.RemotingHelper; -import java.util.*; -import java.util.concurrent.*; - public class ConsumeMessageConcurrentlyService implements ConsumeMessageService { private static final InternalLogger log = ClientLogger.getLog(); private final DefaultMQPushConsumerImpl defaultMQPushConsumerImpl; @@ -49,7 +61,7 @@ public class ConsumeMessageConcurrentlyService implements ConsumeMessageService private final String consumerGroup; static { - log.info("load custom ConsumeMessageConcurrentlyService class for proxy, because of updateOffset"); + log.info("load custom ConsumeMessageConcurrentlyService class for eventMesh, because of updateOffset"); } private final ScheduledExecutorService scheduledExecutorService; @@ -75,7 +87,7 @@ public ConsumeMessageConcurrentlyService(DefaultMQPushConsumerImpl defaultMQPush this.scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryImpl("ConsumeMessageScheduledThread_")); this.cleanExpireMsgExecutors = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryImpl("CleanExpireMsgScheduledThread_")); - log.info("new ConsumeMessageConcurrentlyService instance for proxy has been created "); + log.info("new ConsumeMessageConcurrentlyService instance for eventMesh has been created "); } public void start() { @@ -95,6 +107,11 @@ public void run() { } } + @Override + public void shutdown(long awaitTerminateMillis) { + + } + // @Override // public void shutdown(long awaitTerminateMillis) { // this.scheduledExecutorService.shutdown(); @@ -165,12 +182,12 @@ public ConsumeMessageDirectlyResult consumeMessageDirectly(MessageExt msg, Strin List msgs = new ArrayList(); msgs.add(msg); MessageQueue mq = new MessageQueue(); - mq.setBrokerName(brokerName); +// mq.setBrokerName(brokerName); mq.setTopic(msg.getTopic()); mq.setQueueId(msg.getQueueId()); ProcessQueue pq = new ProcessQueue(); - ProxyConsumeConcurrentlyContext context = new ProxyConsumeConcurrentlyContext(mq, pq); + EventMeshConsumeConcurrentlyContext context = new EventMeshConsumeConcurrentlyContext(mq, pq); this.resetRetryTopic(msgs); @@ -273,7 +290,7 @@ private void cleanExpireMsg() { public void processConsumeResult( final ConsumeConcurrentlyStatus status, - final ProxyConsumeConcurrentlyContext context, + final EventMeshConsumeConcurrentlyContext context, final ConsumeRequest consumeRequest ) { int ackIndex = context.getAckIndex(); @@ -332,12 +349,12 @@ public void processConsumeResult( } } - public void updateOffset(List msgs, ProxyConsumeConcurrentlyContext context) { + public void updateOffset(List msgs, ConsumeConcurrentlyContext context) { for (MessageExt m : msgs) { log.debug("update offset, msg: {} {} {}", m.getTopic(), m.getQueueId(), m.getQueueOffset()); } MessageQueue messageQueue = context.getMessageQueue(); - ProcessQueue processQueue = context.getProcessQueue(); + ProcessQueue processQueue = ((EventMeshConsumeConcurrentlyContext) context).getProcessQueue(); long offset = processQueue.removeMessage(msgs); if (offset >= 0) { log.debug("update offset={}", offset); @@ -436,7 +453,7 @@ public void run() { } MessageListenerConcurrently listener = ConsumeMessageConcurrentlyService.this.messageListener; - ProxyConsumeConcurrentlyContext context = new ProxyConsumeConcurrentlyContext(messageQueue, processQueue); + EventMeshConsumeConcurrentlyContext context = new EventMeshConsumeConcurrentlyContext(messageQueue, processQueue); ConsumeConcurrentlyStatus status = null; ConsumeMessageContext consumeMessageContext = null; @@ -459,6 +476,8 @@ public void run() { for (MessageExt msg : msgs) { MessageAccessor.setConsumeStartTimeStamp(msg, String.valueOf(System.currentTimeMillis())); } + + } status = listener.consumeMessage(Collections.unmodifiableList(msgs), context); } catch (Throwable e) { diff --git a/eventmesh-connector-rocketmq/src/main/resources/META-INF/services/org.apache.eventmesh.api.consumer.MeshMQPushConsumer b/eventmesh-connector-rocketmq/src/main/resources/META-INF/services/org.apache.eventmesh.api.consumer.MeshMQPushConsumer new file mode 100644 index 0000000000..c98880a841 --- /dev/null +++ b/eventmesh-connector-rocketmq/src/main/resources/META-INF/services/org.apache.eventmesh.api.consumer.MeshMQPushConsumer @@ -0,0 +1,16 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +org.apache.eventmesh.connector.rocketmq.consumer.RocketMQConsumerImpl \ No newline at end of file diff --git a/eventmesh-connector-rocketmq/src/main/resources/META-INF/services/org.apache.eventmesh.api.producer.MeshMQProducer b/eventmesh-connector-rocketmq/src/main/resources/META-INF/services/org.apache.eventmesh.api.producer.MeshMQProducer new file mode 100644 index 0000000000..28907ca176 --- /dev/null +++ b/eventmesh-connector-rocketmq/src/main/resources/META-INF/services/org.apache.eventmesh.api.producer.MeshMQProducer @@ -0,0 +1,16 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +org.apache.eventmesh.connector.rocketmq.producer.RocketMQProducerImpl \ No newline at end of file diff --git a/eventmesh-connector-rocketmq/src/test/java/org/apache/rocketmq/consumer/PushConsumerImplTest.java b/eventmesh-connector-rocketmq/src/test/java/org/apache/rocketmq/consumer/PushConsumerImplTest.java new file mode 100644 index 0000000000..114053520e --- /dev/null +++ b/eventmesh-connector-rocketmq/src/test/java/org/apache/rocketmq/consumer/PushConsumerImplTest.java @@ -0,0 +1,108 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.rocketmq.consumer; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.lang.reflect.Field; +import java.util.Collections; +import java.util.Properties; + +import io.openmessaging.api.Action; +import io.openmessaging.api.AsyncConsumeContext; +import io.openmessaging.api.AsyncMessageListener; +import io.openmessaging.api.Consumer; +import io.openmessaging.api.Message; +import io.openmessaging.api.MessagingAccessPoint; +import io.openmessaging.api.OMS; +import io.openmessaging.api.OMSBuiltinKeys; + +import org.apache.eventmesh.api.EventMeshAction; +import org.apache.eventmesh.api.EventMeshAsyncConsumeContext; +import org.apache.eventmesh.connector.rocketmq.consumer.PushConsumerImpl; +import org.apache.eventmesh.connector.rocketmq.domain.NonStandardKeys; +import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer; +import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently; +import org.apache.rocketmq.common.message.MessageExt; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class PushConsumerImplTest { + private Consumer consumer; + + @Mock + private DefaultMQPushConsumer rocketmqPushConsumer; + + @Before + public void before() throws Exception { + Properties consumerProp = new Properties(); + consumerProp.setProperty(OMSBuiltinKeys.DRIVER_IMPL, "org.apache.eventmesh.connector.rocketmq.MessagingAccessPointImpl"); + consumerProp.setProperty("access_points", "IP1:9876,IP2:9876"); + final MessagingAccessPoint messagingAccessPoint = OMS.builder().build(consumerProp);//.endpoint("oms:rocketmq://IP1:9876,IP2:9876/namespace").build(config); + + consumerProp.setProperty("message.model", "CLUSTERING"); + + //Properties consumerProp = new Properties(); + consumerProp.put("CONSUMER_ID", "TestGroup"); + consumer = messagingAccessPoint.createConsumer(consumerProp); + + + Field field = PushConsumerImpl.class.getDeclaredField("rocketmqPushConsumer"); + field.setAccessible(true); + DefaultMQPushConsumer innerConsumer = (DefaultMQPushConsumer) field.get(consumer); + field.set(consumer, rocketmqPushConsumer); //Replace + + Mockito.when(rocketmqPushConsumer.getMessageListener()).thenReturn(innerConsumer.getMessageListener()); + consumer.start(); + } + + @After + public void after() throws Exception { + Mockito.verify(rocketmqPushConsumer).getMessageListener(); + consumer.shutdown(); + } + + @Test + public void testConsumeMessage() { + final byte[] testBody = new byte[]{'a', 'b'}; + + MessageExt consumedMsg = new MessageExt(); + consumedMsg.setMsgId("NewMsgId"); + consumedMsg.setBody(testBody); + consumedMsg.putUserProperty(NonStandardKeys.MESSAGE_DESTINATION, "TOPIC"); + consumedMsg.setTopic("HELLO_QUEUE"); + consumer.subscribe("HELLO_QUEUE", "*", new AsyncMessageListener() { + @Override + public void consume(Message message, AsyncConsumeContext context) { + assertThat(message.getSystemProperties("MESSAGE_ID")).isEqualTo("NewMsgId"); + assertThat(message.getBody()).isEqualTo(testBody); + ((EventMeshAsyncConsumeContext)context).commit(EventMeshAction.CommitMessage); + } + }); + ((MessageListenerConcurrently) rocketmqPushConsumer + .getMessageListener()).consumeMessage(Collections.singletonList(consumedMsg), null); + + + } +} \ No newline at end of file diff --git a/eventmesh-connector-rocketmq/src/test/java/org/apache/rocketmq/producer/ProducerImplTest.java b/eventmesh-connector-rocketmq/src/test/java/org/apache/rocketmq/producer/ProducerImplTest.java new file mode 100644 index 0000000000..83d55710f0 --- /dev/null +++ b/eventmesh-connector-rocketmq/src/test/java/org/apache/rocketmq/producer/ProducerImplTest.java @@ -0,0 +1,153 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.rocketmq.producer; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Fail.failBecauseExceptionWasNotThrown; +import static org.mockito.ArgumentMatchers.any; + +import java.lang.reflect.Field; +import java.util.Properties; + +import io.openmessaging.api.MessagingAccessPoint; +import io.openmessaging.api.OMS; +import io.openmessaging.api.OMSBuiltinKeys; +import io.openmessaging.api.Producer; +import io.openmessaging.api.exception.OMSRuntimeException; + +import org.apache.eventmesh.connector.rocketmq.producer.AbstractOMSProducer; +import org.apache.rocketmq.client.exception.MQBrokerException; +import org.apache.rocketmq.client.exception.MQClientException; +import org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl; +import org.apache.rocketmq.client.producer.DefaultMQProducer; +import org.apache.rocketmq.client.producer.SendResult; +import org.apache.rocketmq.client.producer.SendStatus; +import org.apache.rocketmq.common.ServiceState; +import org.apache.rocketmq.common.message.Message; +import org.apache.rocketmq.common.message.MessageQueue; +import org.apache.rocketmq.remoting.exception.RemotingException; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class ProducerImplTest { + private Producer producer; + + @Mock + private DefaultMQProducer rocketmqProducer; + + @Before + public void before() throws NoSuchFieldException, IllegalAccessException { + Properties config = new Properties(); + config.setProperty(OMSBuiltinKeys.DRIVER_IMPL, "org.apache.eventmesh.connector.rocketmq.MessagingAccessPointImpl"); + config.setProperty("access_points", "IP1:9876,IP2:9876"); + final MessagingAccessPoint messagingAccessPoint = OMS.builder().build(config);//.endpoint("oms:rocketmq://IP1:9876,IP2:9876/namespace").build(config); + producer = messagingAccessPoint.createProducer(config); + + Field field = AbstractOMSProducer.class.getDeclaredField("rocketmqProducer"); + field.setAccessible(true); + field.set(producer, rocketmqProducer); + +// messagingAccessPoint.startup(); + producer.start(); + + } + + + @After + public void after() throws NoSuchFieldException, IllegalAccessException { + + producer.shutdown(); + + } + + @Test + public void testSend_OK() throws InterruptedException, RemotingException, MQClientException, MQBrokerException { + SendResult sendResult = new SendResult(); + sendResult.setMsgId("TestMsgID"); + sendResult.setSendStatus(SendStatus.SEND_OK); + MessageQueue messageQueue = new MessageQueue("HELLO_TOPIC", "testBroker", 0); + sendResult.setMessageQueue(messageQueue); + + Mockito.when(rocketmqProducer.send(any(Message.class))).thenReturn(sendResult); + + DefaultMQProducer defaultMQProducer = new DefaultMQProducer("testGroup"); + DefaultMQProducerImpl defaultMQProducerImpl = new DefaultMQProducerImpl(defaultMQProducer); + defaultMQProducerImpl.setServiceState(ServiceState.RUNNING); + Mockito.when(rocketmqProducer.getDefaultMQProducerImpl()).thenReturn(defaultMQProducerImpl); + + + io.openmessaging.api.Message message = new io.openmessaging.api.Message("HELLO_TOPIC", "", new byte[]{'a'}); + io.openmessaging.api.SendResult omsResult = + producer.send(message); + + assertThat(omsResult.getMessageId()).isEqualTo("TestMsgID"); + Mockito.verify(rocketmqProducer).getDefaultMQProducerImpl(); + Mockito.verify(rocketmqProducer).send(any(Message.class)); + + } + +// @Test +// public void testSend_Not_OK() throws InterruptedException, RemotingException, MQClientException, MQBrokerException { +// SendResult sendResult = new SendResult(); +// sendResult.setSendStatus(SendStatus.FLUSH_DISK_TIMEOUT); +// MessageQueue messageQueue = new MessageQueue("HELLO_TOPIC", "testBroker", 0); +// sendResult.setMessageQueue(messageQueue); +// +// when(rocketmqProducer.send(any(Message.class))).thenReturn(sendResult); +// +// DefaultMQProducer defaultMQProducer =new DefaultMQProducer("testGroup"); +// DefaultMQProducerImpl defaultMQProducerImpl = new DefaultMQProducerImpl(defaultMQProducer); +// defaultMQProducerImpl.setServiceState(ServiceState.RUNNING); +// when(rocketmqProducer.getDefaultMQProducerImpl()).thenReturn(defaultMQProducerImpl); +// +// try { +// io.openmessaging.api.Message message = new io.openmessaging.api.Message("HELLO_TOPIC", "", new byte[] {'a'}); +// producer.send(message); +// failBecauseExceptionWasNotThrown(OMSRuntimeException.class); +// } catch (Exception e) { +// assertThat(e).hasMessageContaining("Send message to RocketMQ broker failed."); +// } +// } + + @Test + public void testSend_WithException() throws InterruptedException, RemotingException, MQClientException, MQBrokerException { + DefaultMQProducer defaultMQProducer = new DefaultMQProducer("testGroup"); + DefaultMQProducerImpl defaultMQProducerImpl = new DefaultMQProducerImpl(defaultMQProducer); + defaultMQProducerImpl.setServiceState(ServiceState.RUNNING); + Mockito.when(rocketmqProducer.getDefaultMQProducerImpl()).thenReturn(defaultMQProducerImpl); + MQClientException exception = new MQClientException("Send message to RocketMQ broker failed.", new Exception()); + Mockito.when(rocketmqProducer.send(any(Message.class))).thenThrow(exception); + + try { + io.openmessaging.api.Message message = new io.openmessaging.api.Message("HELLO_TOPIC", "", new byte[]{'a'}); + producer.send(message); + failBecauseExceptionWasNotThrown(OMSRuntimeException.class); + } catch (Exception e) { + assertThat(e).hasMessageContaining("Send message to RocketMQ broker failed."); + } + + Mockito.verify(rocketmqProducer).send(any(Message.class)); + } + +} \ No newline at end of file diff --git a/eventmesh-connector-rocketmq/src/test/java/org/apache/rocketmq/promise/DefaultPromiseTest.java b/eventmesh-connector-rocketmq/src/test/java/org/apache/rocketmq/promise/DefaultPromiseTest.java new file mode 100644 index 0000000000..3d4c8c2ab3 --- /dev/null +++ b/eventmesh-connector-rocketmq/src/test/java/org/apache/rocketmq/promise/DefaultPromiseTest.java @@ -0,0 +1,121 @@ +///* +// * Licensed to the Apache Software Foundation (ASF) under one or more +// * contributor license agreements. See the NOTICE file distributed with +// * this work for additional information regarding copyright ownership. +// * The ASF licenses this file to You under the Apache License, Version 2.0 +// * (the "License"); you may not use this file except in compliance with +// * the License. You may obtain a copy of the License at +// * +// * http://www.apache.org/licenses/LICENSE-2.0 +// * +// * Unless required by applicable law or agreed to in writing, software +// * distributed under the License is distributed on an "AS IS" BASIS, +// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// * See the License for the specific language governing permissions and +// * limitations under the License. +// */ +//package rocketmq.promise; +// +//import org.apache.eventmesh.connector.rocketmq.promise.DefaultPromise; +//import io.openmessaging.Future; +//import io.openmessaging.FutureListener; +//import io.openmessaging.Promise; +//import io.openmessaging.exception.OMSRuntimeException; +//import org.junit.Before; +//import org.junit.Test; +// +//import static org.assertj.core.api.Assertions.assertThat; +//import static org.assertj.core.api.Fail.failBecauseExceptionWasNotThrown; +// +//public class DefaultPromiseTest { +// private Promise promise; +// +// @Before +// public void init() { +// promise = new DefaultPromise<>(); +// } +// +// @Test +// public void testIsCancelled() throws Exception { +// assertThat(promise.isCancelled()).isEqualTo(false); +// } +// +// @Test +// public void testIsDone() throws Exception { +// assertThat(promise.isDone()).isEqualTo(false); +// promise.set("Done"); +// assertThat(promise.isDone()).isEqualTo(true); +// } +// +// @Test +// public void testGet() throws Exception { +// promise.set("Done"); +// assertThat(promise.get()).isEqualTo("Done"); +// } +// +// @Test +// public void testGet_WithTimeout() throws Exception { +// try { +// promise.get(100); +// failBecauseExceptionWasNotThrown(OMSRuntimeException.class); +// } catch (OMSRuntimeException e) { +// assertThat(e).hasMessageContaining("Get request result is timeout or interrupted"); +// } +// } +// +// @Test +// public void testAddListener() throws Exception { +// promise.addListener(new FutureListener() { +// @Override +// public void operationComplete(Future future) { +// assertThat(promise.get()).isEqualTo("Done"); +// +// } +// }); +// promise.set("Done"); +// } +// +// @Test +// public void testAddListener_ListenerAfterSet() throws Exception { +// promise.set("Done"); +// promise.addListener(new FutureListener() { +// @Override +// public void operationComplete(Future future) { +// assertThat(future.get()).isEqualTo("Done"); +// } +// }); +// } +// +// @Test +// public void testAddListener_WithException_ListenerAfterSet() throws Exception { +// final Throwable exception = new OMSRuntimeException("-1", "Test Error"); +// promise.setFailure(exception); +// promise.addListener(new FutureListener() { +// @Override +// public void operationComplete(Future future) { +// assertThat(promise.getThrowable()).isEqualTo(exception); +// } +// }); +// } +// +// @Test +// public void testAddListener_WithException() throws Exception { +// final Throwable exception = new OMSRuntimeException("-1", "Test Error"); +// promise.addListener(new FutureListener() { +// @Override +// public void operationComplete(Future future) { +// assertThat(promise.getThrowable()).isEqualTo(exception); +// } +// }); +// promise.setFailure(exception); +// } +// +// @Test +// public void getThrowable() throws Exception { +// assertThat(promise.getThrowable()).isNull(); +// Throwable exception = new OMSRuntimeException("-1", "Test Error"); +// promise.setFailure(exception); +// assertThat(promise.getThrowable()).isEqualTo(exception); +// } +// +//} \ No newline at end of file diff --git a/eventmesh-connector-rocketmq/src/test/java/org/apache/rocketmq/utils/BeanUtilsTest.java b/eventmesh-connector-rocketmq/src/test/java/org/apache/rocketmq/utils/BeanUtilsTest.java new file mode 100644 index 0000000000..7f545f3c0b --- /dev/null +++ b/eventmesh-connector-rocketmq/src/test/java/org/apache/rocketmq/utils/BeanUtilsTest.java @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.rocketmq.utils; + +import java.util.Properties; + +import org.apache.eventmesh.connector.rocketmq.config.ClientConfig; +import org.apache.eventmesh.connector.rocketmq.domain.NonStandardKeys; +import org.apache.eventmesh.connector.rocketmq.utils.BeanUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class BeanUtilsTest { + private Properties properties = new Properties(); + + public static class CustomizedConfig extends ClientConfig { + final static String STRING_TEST = "string.test"; + String stringTest = "foobar"; + + final static String DOUBLE_TEST = "double.test"; + double doubleTest = 123.0; + + final static String LONG_TEST = "long.test"; + long longTest = 123L; + + String getStringTest() { + return stringTest; + } + + public void setStringTest(String stringTest) { + this.stringTest = stringTest; + } + + double getDoubleTest() { + return doubleTest; + } + + public void setDoubleTest(final double doubleTest) { + this.doubleTest = doubleTest; + } + + long getLongTest() { + return longTest; + } + + public void setLongTest(final long longTest) { + this.longTest = longTest; + } + + public CustomizedConfig() { + } + } + + @Before + public void before() { + properties.put(NonStandardKeys.MAX_REDELIVERY_TIMES, 120); + properties.put(CustomizedConfig.STRING_TEST, "kaka"); + properties.put(NonStandardKeys.CONSUMER_GROUP, "Default_Consumer_Group"); + properties.put(NonStandardKeys.MESSAGE_CONSUME_TIMEOUT, 101); + + properties.put(CustomizedConfig.LONG_TEST, 1234567890L); + properties.put(CustomizedConfig.DOUBLE_TEST, 10.234); + } + + @Test + public void testPopulate() { + CustomizedConfig config = BeanUtils.populate(properties, CustomizedConfig.class); + + //RemotingConfig config = BeanUtils.populate(properties, RemotingConfig.class); + Assert.assertEquals(config.getRmqMaxRedeliveryTimes(), 120); + Assert.assertEquals(config.getStringTest(), "kaka"); + Assert.assertEquals(config.getRmqConsumerGroup(), "Default_Consumer_Group"); + Assert.assertEquals(config.getRmqMessageConsumeTimeout(), 101); + Assert.assertEquals(config.getLongTest(), 1234567890L); + Assert.assertEquals(config.getDoubleTest(), 10.234, 0.000001); + } + + @Test + public void testPopulate_ExistObj() { + CustomizedConfig config = new CustomizedConfig(); + config.setConsumerId("NewConsumerId"); + + Assert.assertEquals(config.getConsumerId(), "NewConsumerId"); + + config = BeanUtils.populate(properties, config); + + //RemotingConfig config = BeanUtils.populate(properties, RemotingConfig.class); + Assert.assertEquals(config.getRmqMaxRedeliveryTimes(), 120); + Assert.assertEquals(config.getStringTest(), "kaka"); + Assert.assertEquals(config.getRmqConsumerGroup(), "Default_Consumer_Group"); + Assert.assertEquals(config.getRmqMessageConsumeTimeout(), 101); + Assert.assertEquals(config.getLongTest(), 1234567890L); + Assert.assertEquals(config.getDoubleTest(), 10.234, 0.000001); + } + +} \ No newline at end of file diff --git a/eventmesh-connector-rocketmq/src/test/resources/META-INF/services/org.apache.io.openmessaging.MessagingAccessPoint b/eventmesh-connector-rocketmq/src/test/resources/META-INF/services/org.apache.io.openmessaging.MessagingAccessPoint new file mode 100644 index 0000000000..e326c919c7 --- /dev/null +++ b/eventmesh-connector-rocketmq/src/test/resources/META-INF/services/org.apache.io.openmessaging.MessagingAccessPoint @@ -0,0 +1,20 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +org.apache.eventmesh.connector.rocketmq.MessagingAccessPointImpl \ No newline at end of file diff --git a/eventmesh-connector-rocketmq/src/test/resources/META-INF/services/org.apache.io.openmessaging.producer.Producer b/eventmesh-connector-rocketmq/src/test/resources/META-INF/services/org.apache.io.openmessaging.producer.Producer new file mode 100644 index 0000000000..bf0a8ddc29 --- /dev/null +++ b/eventmesh-connector-rocketmq/src/test/resources/META-INF/services/org.apache.io.openmessaging.producer.Producer @@ -0,0 +1,20 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +org.apache.eventmesh.connector.rocketmq.producer.ProducerImpl \ No newline at end of file diff --git a/eventmesh-emesher/bin/stop.sh b/eventmesh-emesher/bin/stop.sh deleted file mode 100644 index 8476636c13..0000000000 --- a/eventmesh-emesher/bin/stop.sh +++ /dev/null @@ -1,60 +0,0 @@ -#!/bin/sh - -#detect operating system. -OS=$(uname -o) - -PROXY_HOME=`cd "./.." && pwd` - -export PROXY_HOME - -function get_pid { - local ppid="" - if [ -f ${PROXY_HOME}/bin/pid.file ]; then - ppid=$(cat ${PROXY_HOME}/bin/pid.file) - else - if [[ $OS =~ Msys ]]; then - # 在Msys上存在可能无法kill识别出的进程的BUG - ppid=`jps -v | grep -i "com.webank.emesher.boot.ProxyStartup" | grep java | grep -v grep | awk -F ' ' {'print $1'}` - elif [[ $OS =~ Darwin ]]; then - # 已知问题:grep java 可能无法精确识别java进程 - ppid=$(/bin/ps -o user,pid,command | grep "java" | grep -i "com.webank.emesher.boot.ProxyStartup" | grep -Ev "^root" |awk -F ' ' {'print $2'}) - else - #在Linux服务器上要求尽可能精确识别进程 - ppid=$(ps -C java -o user,pid,command --cols 99999 | grep -w $PROXY_HOME | grep -i "com.webank.emesher.boot.ProxyStartup" | grep -Ev "^root" |awk -F ' ' {'print $2'}) - fi - fi - echo "$ppid"; -} - -pid=$(get_pid) -if [ -z "$pid" ];then - echo -e "No proxy running.." - exit 0; -fi - -kill ${pid} -echo "Send shutdown request to proxy(${pid}) OK" - -[[ $OS =~ Msys ]] && PS_PARAM=" -W " -stop_timeout=60 -for no in $(seq 1 $stop_timeout); do - if ps $PS_PARAM -p "$pid" 2>&1 > /dev/null; then - if [ $no -lt $stop_timeout ]; then - echo "[$no] shutdown server ..." - sleep 1 - continue - fi - - echo "shutdown server timeout, kill process: $pid" - kill -9 $pid; sleep 1; break; - echo "`date +'%Y-%m-%-d %H:%M:%S'` , pid : [$pid] , error message : abnormal shutdown which can not be closed within 60s" > ../logs/shutdown.error - else - echo "shutdown server ok!"; break; - fi -done - -if [ -f "pid.file" ]; then - rm pid.file -fi - - diff --git a/eventmesh-emesher/build.gradle b/eventmesh-emesher/build.gradle deleted file mode 100644 index a7fba3f2c8..0000000000 --- a/eventmesh-emesher/build.gradle +++ /dev/null @@ -1,160 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import java.util.concurrent.TimeUnit - -apply plugin: 'java' -apply plugin: "maven" -apply plugin: "eclipse" -apply plugin: "idea" - -sourceCompatibility = 1.8 - -repositories { - mavenLocal() - maven { - url "https://maven.aliyun.com/repository/public" - } - mavenCentral() -} - -List defibus = [ - "com.webank.defibus:defibus-client:$defibus_version", - "com.webank.defibus:defibus-common:$defibus_version" -] - -List junit = [ - "junit:junit:4.12" -] - -List log4j2 = [ - "org.apache.logging.log4j:log4j-api:2.13.3", - "org.apache.logging.log4j:log4j-core:2.13.3", - "org.apache.logging.log4j:log4j-slf4j-impl:2.13.3", - "org.apache.logging.log4j:log4j-web:2.13.3", - "com.lmax:disruptor:3.4.2" -] - -List sl4j = [ - "org.slf4j:slf4j-api:1.7.30" -] - -List metrics = [ - "io.dropwizard.metrics:metrics-core:4.1.0", - "io.dropwizard.metrics:metrics-healthchecks:4.1.0", - "io.dropwizard.metrics:metrics-annotation:4.1.0", - "io.dropwizard.metrics:metrics-json:4.1.0" -] - -dependencies { - compile project(":eventmesh-common") - testCompile project(":eventmesh-common") - compile defibus, metrics, log4j2, sl4j - testCompile defibus, metrics, log4j2, sl4j, junit -} - -clean.doFirst { - delete 'build' - delete 'dist' -} - -def versionString = project.version -if (project.findProperty("buildNo") instanceof String) { - versionString = project.version + '_' + project.property("buildNo") -} - -task zip(type: Zip, dependsOn: ['clean', 'jar']) { - extension = 'zip' - archiveName = project.name + '_' + versionString + '.' + extension - destinationDir = new File(projectDir, 'dist') - - into('apps') { - from project.jar.getArchivePath() - } - - into('bin') { - from 'bin' - } - - into('conf') { - from "conf" - } - - into('lib') { - from project.configurations.runtime - exclude '**/*.properties*' - exclude '**/*testng*.jar' - exclude '**/*powermock*.jar' - exclude '**/*mockito*.jar' - exclude '**/*junit*.jar' - exclude '**/*jacoco*.jar' - exclude '**/*log4j2.xml*' - exclude '**/spring-boot-devtools*.jar' - } - - into('scripts') { - from "scripts" - } -} - -task tar(type: Tar, dependsOn: ['clean', 'jar']) { - extension = 'tar.gz' - compression = Compression.GZIP - archiveName = project.name + '_' + versionString + '.' + extension - destinationDir = new File(projectDir, 'dist') - - into('apps') { - from project.jar.getArchivePath() - } - - into('bin') { - from 'bin' - } - - into('conf') { - from "conf" - } - - into('lib') { - from project.configurations.runtime - exclude '**/*.properties*' - exclude '**/*testng*.jar' - exclude '**/*powermock*.jar' - exclude '**/*mockito*.jar' - exclude '**/*junit*.jar' - exclude '**/*jacoco*.jar' - exclude '**/*log4j2.xml*' - exclude '**/spring-boot-devtools*.jar' - } - - into('scripts') { - from "scripts" - } -} - -uploadArchives { - repositories { - mavenDeployer { - snapshotRepository(url: 'Your target repo address') { - authentication(userName: 'Your user name', password: 'Your password') - } - repository(url: 'Your target repo address') { - authentication(userName: 'Your user name', password: 'Your password') - } - } - } -} \ No newline at end of file diff --git a/eventmesh-emesher/conf/proxy.properties b/eventmesh-emesher/conf/proxy.properties deleted file mode 100644 index f7e04e117b..0000000000 --- a/eventmesh-emesher/conf/proxy.properties +++ /dev/null @@ -1,79 +0,0 @@ -# -# Licensed to the Apache Software Foundation (ASF) under one or more -# contributor license agreements. See the NOTICE file distributed with -# this work for additional information regarding copyright ownership. -# The ASF licenses this file to You under the Apache License, Version 2.0 -# (the "License"); you may not use this file except in compliance with -# the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -###############################EVNETMESH-EMESHER ENV################################# -proxy.server.idc=DEFAULT -proxy.server.env=PRD -proxy.server.region=region1 -proxy.server.cluster=COMMON -proxy.server.name=EVNETMESH-EMESHER -proxy.server.dcn=010 -proxy.sysid=0000 -proxy.server.http.port=10105 - -#######################defibus################## -proxy.server.defibus.namesrvAddr=127.0.0.1:9876;127.0.0.1:9876 -proxy.server.defibus.client.consumeThreadMin=2 -proxy.server.defibus.client.consumeThreadMax=2 -proxy.server.defibus.client.consumeThreadPoolQueueSize=10000 -proxy.server.defibus.client.pullBatchSize=32 -proxy.server.defibus.client.ackwindow=1000 -proxy.server.defibus.client.pubwindow=512 -proxy.server.defibus.client.pollNameServerInterval=10000 -proxy.server.defibus.client.heartbeatBrokerInterval=30000 -proxy.server.defibus.client.rebalanceInterval=20000 - -########################## proxy tcp configuration ############################ -proxy.server.tcp.enabled=true -proxy.server.tcp.port=10000 -proxy.server.tcp.readerIdleSeconds=120 -proxy.server.tcp.writerIdleSeconds=120 -proxy.server.tcp.allIdleSeconds=120 -proxy.server.tcp.clientMaxNum=10000 - -# client isolation time if the message send failure -proxy.server.tcp.pushFailIsolateTimeInMills=30000 - -# rebalance internal -proxy.server.tcp.RebalanceIntervalInMills=30000 - -# session expire time about client -proxy.server.session.expiredInMills=60000 - -# flow control, include the global level and session level -proxy.server.tcp.msgReqnumPerSecond=15000 -proxy.server.session.upstreamBufferSize=20 -proxy.server.session.downstreamUnackSize=100 - -# thread number about global scheduler -proxy.server.global.scheduler=5 -proxy.server.tcp.taskHandleExecutorPoolSize=8 - -#retry -proxy.server.retry.pushRetryTimes=3 -proxy.server.retry.pushRetryDelayInMills=500 -proxy.server.retry.pushRetryQueueSize=10000 - -#admin -proxy.server.admin.http.port=10106 - -#registry -proxy.server.registry.registerIntervalInMills=10000 -proxy.server.registry.fetchRegistryAddrIntervalInMills=20000 - -#auto-ack -proxy.server.defibus.client.comsumeTimeoutInMin=5 \ No newline at end of file diff --git a/eventmesh-emesher/conf/server.env b/eventmesh-emesher/conf/server.env deleted file mode 100644 index 4f8c6872ff..0000000000 --- a/eventmesh-emesher/conf/server.env +++ /dev/null @@ -1 +0,0 @@ -APP_START_JVM_OPTION:::-server -Xms128M -Xmx256M -Xmn128m -XX:SurvivorRatio=4 -Duser.language=zh diff --git a/eventmesh-emesher/scripts/before_shutdown.sh b/eventmesh-emesher/scripts/before_shutdown.sh deleted file mode 100644 index 20f241071d..0000000000 --- a/eventmesh-emesher/scripts/before_shutdown.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash - -source session.sh | awk '{print $1}' | awk -F '=' '{print $2}' | awk -F '|' '{print $1,$2}' | grep 2019 > tmp.txt - -cat tmp.txt | while read line -do - cmd="./client_manage.sh -s $line" - $cmd - sleep 10s -done - -rm tmp.txt - -./client_manage.sh -a diff --git a/eventmesh-emesher/scripts/connections.sh b/eventmesh-emesher/scripts/connections.sh deleted file mode 100644 index fc3373682b..0000000000 --- a/eventmesh-emesher/scripts/connections.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -set -e -date; -echo -e "active access connections:\n$(netstat -lnap |grep EST | grep $(jps | grep ProxyStartup | cut -d" " -f1) | awk -F"[\t ]+" '{print $4}' |sort| uniq -c | sort -rnk1 | grep ":10000")" -echo -e "active mq connections:\n$(netstat -lnap |grep EST | grep $(jps | grep ProxyStartup | cut -d" " -f1) | awk -F"[\t ]+" '{print $5}' |sort | uniq -c | sort -rnk1 |grep -E "10911|9876")" diff --git a/eventmesh-emesher/scripts/histo.sh b/eventmesh-emesher/scripts/histo.sh deleted file mode 100644 index 3fe5dc4642..0000000000 --- a/eventmesh-emesher/scripts/histo.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash -set -e -script_path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -cd $script_path -echo "[`date`] WARNING this script will PAUSE jvm for seconds." -pid=$(jcmd | grep ProxyStartup | cut -d" " -f1) -jmap -histo:live $pid | tee $script_path/../logs/histo.$(date +%m%d%H%M).$pid.log diff --git a/eventmesh-emesher/scripts/histo_suspects.sh b/eventmesh-emesher/scripts/histo_suspects.sh deleted file mode 100644 index e4550de7c9..0000000000 --- a/eventmesh-emesher/scripts/histo_suspects.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash -set -e -script_path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -cd $script_path -echo "[`date`] WARNING this script will PAUSE jvm for seconds." -pid=$(jcmd | grep ProxyStartup | cut -d" " -f1) -./histo.sh | grep -E "RRResponseFurture|PushConsumer|PullMessage|RR|RunnableAdapter|instance|ProducerFactory|ClientInstance|Session" | tee $script_path/../logs/histo.suspects.$(date +%m%d%H%M).$pid.log diff --git a/eventmesh-emesher/scripts/jstack.sh b/eventmesh-emesher/scripts/jstack.sh deleted file mode 100644 index 75ee34a8da..0000000000 --- a/eventmesh-emesher/scripts/jstack.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash -set -e -script_path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -echo "[`date`] WARNING this script will PAUSE jvm for seconds." -pid=$(jcmd | grep ProxyStartup | cut -d" " -f1) -jstack $pid | tee $script_path/../logs/jstack.$(date +%m%d%H%M).$pid.log diff --git a/eventmesh-emesher/scripts/monitor_connections.sh b/eventmesh-emesher/scripts/monitor_connections.sh deleted file mode 100644 index 6766c1cb9e..0000000000 --- a/eventmesh-emesher/scripts/monitor_connections.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash -set -e -script_path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -cd $script_path -while true; do - ./connections.sh - sleep 60s; -done diff --git a/eventmesh-emesher/scripts/monitor_histo_suspects.sh b/eventmesh-emesher/scripts/monitor_histo_suspects.sh deleted file mode 100644 index f8d7000698..0000000000 --- a/eventmesh-emesher/scripts/monitor_histo_suspects.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash -set -e -script_path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -cd $script_path -while true; do - ./histo_suspects.sh - sleep 60s; -done diff --git a/eventmesh-emesher/scripts/monitor_threads.sh b/eventmesh-emesher/scripts/monitor_threads.sh deleted file mode 100644 index 35b5a34174..0000000000 --- a/eventmesh-emesher/scripts/monitor_threads.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash -set -e -script_path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -cd $script_path -while true; do - ./threads.sh - sleep 60s; -done diff --git a/eventmesh-emesher/scripts/session.sh b/eventmesh-emesher/scripts/session.sh deleted file mode 100644 index 73a6754dcf..0000000000 --- a/eventmesh-emesher/scripts/session.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash -if [ $# -eq 0 ] -then - curl -s 'http://127.0.0.1:10106/clientManage/showClient?' -elif [ $# -eq 1 ] -then - TOPIC=$1 - curl -s "http://127.0.0.1:10106/clientManage/showListenClientByTopic?topic=${TOPIC}" -else - CLIENT_DCN=$1 - CLIENT_SYSTEM=$2 - curl -s "http://127.0.0.1:10106/clientManage/showClientBySystemAndDcn?dcn=${CLIENT_DCN}&subSystem=${CLIENT_SYSTEM}" -fi diff --git a/eventmesh-emesher/scripts/threads.sh b/eventmesh-emesher/scripts/threads.sh deleted file mode 100644 index 718b654553..0000000000 --- a/eventmesh-emesher/scripts/threads.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash -jstack $(jcmd | grep ProxyStartup | cut -d" " -f1) | \ - grep -vE "(Concurrent GC|DestroyJavaVM|Gang worker|Compiler|Attach Listener|GC Thread|VM Thread|Finalizer)" | \ - grep -oE '^".*?"' | \ - sed -r 's/"(.+)(-|_)([0-9]+).*"/\1/g' | \ - sort | uniq -c | sort -nk1 diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/admin/controller/ClientManageController.java b/eventmesh-emesher/src/main/java/com/webank/emesher/admin/controller/ClientManageController.java deleted file mode 100644 index 9e0cd32bbd..0000000000 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/admin/controller/ClientManageController.java +++ /dev/null @@ -1,780 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.emesher.admin.controller; - -import com.webank.emesher.boot.ProxyTCPServer; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.protocol.tcp.client.ProxyTcp2Client; -import com.webank.emesher.core.protocol.tcp.client.group.ClientGroupWrapper; -import com.webank.emesher.core.protocol.tcp.client.group.ClientSessionGroupMapping; -import com.webank.emesher.core.protocol.tcp.client.group.dispatch.DownstreamDispatchStrategy; -import com.webank.emesher.core.protocol.tcp.client.session.Session; -import com.webank.emesher.core.protocol.tcp.client.session.push.DownStreamMsgContext; -import com.webank.emesher.util.ProxyUtil; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; -import com.alibaba.fastjson.JSON; -import com.sun.net.httpserver.HttpExchange; -import com.sun.net.httpserver.HttpHandler; -import com.sun.net.httpserver.HttpServer; -import org.apache.commons.lang3.StringUtils; -import org.apache.rocketmq.common.message.MessageExt; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.UnsupportedEncodingException; -import java.net.InetSocketAddress; -import java.net.URLDecoder; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.AtomicInteger; - -public class ClientManageController { - - private static final Logger logger = LoggerFactory.getLogger(ClientManageController.class); - - private ProxyTCPServer proxyTCPServer; - - public ClientManageController(ProxyTCPServer proxyTCPServer){ - this.proxyTCPServer = proxyTCPServer; - } - - public void start() throws IOException { - int port = proxyTCPServer.getAccessConfiguration().proxyServerAdminPort; - HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); - server.createContext("/clientManage/showClient", new ShowClientHandler()); - server.createContext("/clientManage/showClientBySystemAndDcn", new ShowClientBySystemAndDcnHandler()); - server.createContext("/clientManage/rejectAllClient", new RejectAllClientHandler()); - server.createContext("/clientManage/rejectClientByIpPort", new RejectClientByIpPortHandler()); - server.createContext("/clientManage/rejectClientBySubSystem", new RejectClientBySubSystemHandler()); - server.createContext("/clientManage/redirectClientBySubSystem", new RedirectClientBySubSystemHandler()); - server.createContext("/clientManage/redirectClientByPath", new RedirectClientByPathHandler()); - server.createContext("/clientManage/redirectClientByIpPort", new RedirectClientByIpPortHandler()); - server.createContext("/proxy/msg/push", new ProxyMsgDownStreamHandler()); - server.createContext("/clientManage/showListenClientByTopic", new ShowListenClientByTopicHandler()); - - server.start(); - logger.info("ClientManageController start success, port:{}", port); - } - - private Map parsePostParameters(HttpExchange exchange) - throws IOException { - Map parameters = new HashMap<>(); - if ("post".equalsIgnoreCase(exchange.getRequestMethod())) { - InputStreamReader isr = - new InputStreamReader(exchange.getRequestBody(),"utf-8"); - BufferedReader br = new BufferedReader(isr); - String query = br.readLine(); - parseQuery(query, parameters); - } - return parameters; - } - - @SuppressWarnings("unchecked") - private void parseQuery(String query, Map parameters) - throws UnsupportedEncodingException { - - if (query != null) { - String pairs[] = query.split("&"); - - for (String pair : pairs) { - String param[] = pair.split("="); - - String key = null; - String value = null; - if (param.length > 0) { - key = URLDecoder.decode(param[0],"UTF-8"); - } - - if (param.length > 1) { - value = URLDecoder.decode(param[1],"UTF-8"); - } - - if (parameters.containsKey(key)) { - Object obj = parameters.get(key); - if(obj instanceof List) { - List values = (List)obj; - values.add(value); - } else if(obj instanceof String) { - List values = new ArrayList(); - values.add((String)obj); - values.add(value); - parameters.put(key, values); - } - } else { - parameters.put(key, value); - } - } - } - } - - /** - * 打印本proxy上所有客户端信息 - * - * @return - */ - class ShowClientHandler implements HttpHandler{ - @Override - public void handle(HttpExchange httpExchange) throws IOException { - String result = ""; - OutputStream out = httpExchange.getResponseBody(); - try{ - String newLine = System.getProperty("line.separator"); - logger.info("showAllClient================="); - ClientSessionGroupMapping clientSessionGroupMapping = proxyTCPServer.getClientSessionGroupMapping(); - Map dcnSystemMap = clientSessionGroupMapping.statDCNSystemInfo(); - if (!dcnSystemMap.isEmpty()) { - List> list = new ArrayList<>(); - ValueComparator vc = new ValueComparator(); - for (Map.Entry entry : dcnSystemMap.entrySet()) { - list.add(entry); - } - Collections.sort(list, vc); - for (Map.Entry entry : list) { - result += String.format("System=%s | ClientNum=%d", entry.getKey(), entry.getValue().intValue()) + - newLine; - } - } - httpExchange.sendResponseHeaders(200, 0); - out.write(result.getBytes()); - }catch (Exception e){ - logger.error("ShowClientHandler fail...", e); - }finally { - if(out != null){ - try { - out.close(); - }catch (IOException e){ - logger.warn("out close failed...", e); - } - } - } - - } - } - - class ValueComparator implements Comparator> { - @Override - public int compare(Map.Entry x, Map.Entry y) { - return x.getValue().intValue() - y.getValue().intValue(); - } - } - - /** - * print clientInfo by subsys and dcn - * - * @return - */ - class ShowClientBySystemAndDcnHandler implements HttpHandler{ - @Override - public void handle(HttpExchange httpExchange) throws IOException { - String result = ""; - OutputStream out = httpExchange.getResponseBody(); - try{ - String queryString = httpExchange.getRequestURI().getQuery(); - Map queryStringInfo = formData2Dic(queryString); - String dcn = queryStringInfo.get(ProxyConstants.MANAGE_DCN); - String subSystem = queryStringInfo.get(ProxyConstants.MANAGE_SUBSYSTEM); - - String newLine = System.getProperty("line.separator"); - logger.info("showClientBySubsysAndDcn,subsys:{},dcn:{}=================",subSystem,dcn); - ClientSessionGroupMapping clientSessionGroupMapping = proxyTCPServer.getClientSessionGroupMapping(); - ConcurrentHashMap sessionMap = clientSessionGroupMapping.getSessionMap(); - if (!sessionMap.isEmpty()) { - for (Session session : sessionMap.values()) { - if (session.getClient().getDcn().equals(dcn) && session.getClient().getSubsystem().equals(subSystem)) { - UserAgent userAgent = session.getClient(); - result += String.format("pid=%s | ip=%s | port=%s | path=%s | purpose=%s", userAgent.getPid(), userAgent - .getHost(), userAgent.getPort(), userAgent.getPath(), userAgent.getPurpose()) + newLine; - } - } - } - httpExchange.sendResponseHeaders(200, 0); - out.write(result.getBytes()); - }catch (Exception e){ - logger.error("ShowClientBySystemAndDcnHandler fail...", e); - }finally { - if(out != null){ - try { - out.close(); - }catch (IOException e){ - logger.warn("out close failed...", e); - } - } - } - - } - } - - - /** - * query client subscription by topic - * - */ - class ShowListenClientByTopicHandler implements HttpHandler{ - @Override - public void handle(HttpExchange httpExchange) throws IOException { - String result = ""; - OutputStream out = httpExchange.getResponseBody(); - try{ - String queryString = httpExchange.getRequestURI().getQuery(); - Map queryStringInfo = formData2Dic(queryString); - String topic = queryStringInfo.get(ProxyConstants.MANAGE_TOPIC); - - String newLine = System.getProperty("line.separator"); - logger.info("showListeningClientByTopic,topic:{}=================",topic); - ClientSessionGroupMapping clientSessionGroupMapping = proxyTCPServer.getClientSessionGroupMapping(); - ConcurrentHashMap clientGroupMap = clientSessionGroupMapping.getClientGroupMap(); - if (!clientGroupMap.isEmpty()) { - for (ClientGroupWrapper cgw : clientGroupMap.values()) { - Set listenSessionSet = cgw.getTopic2sessionInGroupMapping().get(topic); - if (listenSessionSet != null && listenSessionSet.size() > 0) { - result += String.format("group:%s",cgw.getGroupName()) + newLine; - for(Session session : listenSessionSet) { - UserAgent userAgent = session.getClient(); - result += String.format("pid=%s | ip=%s | port=%s | path=%s | version=%s", userAgent.getPid(), userAgent - .getHost(), userAgent.getPort(), userAgent.getPath(), userAgent.getVersion()) + newLine; - } - } - } - } - httpExchange.sendResponseHeaders(200, 0); - out.write(result.getBytes()); - }catch (Exception e){ - logger.error("ShowListenClientByTopicHandler fail...", e); - }finally { - if(out != null){ - try { - out.close(); - }catch (IOException e){ - logger.warn("out close failed...", e); - } - } - } - - } - } - - - /** - * remove all clients accessed by proxy - * - * @return - */ - class RejectAllClientHandler implements HttpHandler{ - @Override - public void handle(HttpExchange httpExchange) throws IOException { - String result = ""; - OutputStream out = httpExchange.getResponseBody(); - try{ - ClientSessionGroupMapping clientSessionGroupMapping = proxyTCPServer.getClientSessionGroupMapping(); - ConcurrentHashMap sessionMap = clientSessionGroupMapping.getSessionMap(); - final List successRemoteAddrs = new ArrayList(); - try { - logger.info("rejectAllClient in admin===================="); - if (!sessionMap.isEmpty()) { - for (Map.Entry entry : sessionMap.entrySet()) { - InetSocketAddress addr = ProxyTcp2Client.serverGoodby2Client(entry.getValue(), clientSessionGroupMapping); - if (addr != null) { - successRemoteAddrs.add(addr); - } - } - } - } catch (Exception e) { - logger.error("clientManage|rejectAllClient|fail", e); - result = String.format("rejectAllClient fail! sessionMap size {%d}, had reject {%s}, errorMsg : %s", - sessionMap.size(), printClients(successRemoteAddrs), e.getMessage()); - httpExchange.sendResponseHeaders(200, 0); - out.write(result.getBytes()); - return; - } - result = String.format("rejectAllClient success! sessionMap size {%d}, had reject {%s}", sessionMap.size - (), printClients(successRemoteAddrs)); - httpExchange.sendResponseHeaders(200, 0); - out.write(result.getBytes()); - }catch (Exception e){ - logger.error("rejectAllClient fail...", e); - }finally { - if(out != null){ - try { - out.close(); - }catch (IOException e){ - logger.warn("out close failed...", e); - } - } - } - - } - } - - /** - * remove c client by ip and port - * - * @return - */ - class RejectClientByIpPortHandler implements HttpHandler{ - @Override - public void handle(HttpExchange httpExchange) throws IOException { - String result = ""; - OutputStream out = httpExchange.getResponseBody(); - try{ - String queryString = httpExchange.getRequestURI().getQuery(); - Map queryStringInfo = formData2Dic(queryString); - String ip = queryStringInfo.get(ProxyConstants.MANAGE_IP); - String port = queryStringInfo.get(ProxyConstants.MANAGE_PORT); - - if (StringUtils.isBlank(ip) || StringUtils.isBlank(port)) { - httpExchange.sendResponseHeaders(200, 0); - result = "params illegal!"; - out.write(result.getBytes()); - return; - } - logger.info("rejectClientByIpPort in admin,ip:{},port:{}====================",ip,port); - ClientSessionGroupMapping clientSessionGroupMapping = proxyTCPServer.getClientSessionGroupMapping(); - ConcurrentHashMap sessionMap = clientSessionGroupMapping.getSessionMap(); - final List successRemoteAddrs = new ArrayList(); - try { - if (!sessionMap.isEmpty()) { - for (Map.Entry entry : sessionMap.entrySet()) { - if (entry.getKey().getHostString().equals(ip) && String.valueOf(entry.getKey().getPort()).equals(port)) { - InetSocketAddress addr = ProxyTcp2Client.serverGoodby2Client(entry.getValue(), clientSessionGroupMapping); - if (addr != null) { - successRemoteAddrs.add(addr); - } - } - } - } - } catch (Exception e) { - logger.error("clientManage|rejectClientByIpPort|fail|ip={}|port={},errMsg={}", ip, port, e); - result = String.format("rejectClientByIpPort fail! {ip=%s port=%s}, had reject {%s}, errorMsg : %s", ip, - port, printClients(successRemoteAddrs), e.getMessage()); - httpExchange.sendResponseHeaders(200, 0); - out.write(result.getBytes()); - return; - } - - result = String.format("rejectClientByIpPort success! {ip=%s port=%s}, had reject {%s}", ip, port, printClients - (successRemoteAddrs)); - httpExchange.sendResponseHeaders(200, 0); - out.write(result.getBytes()); - }catch (Exception e){ - logger.error("rejectClientByIpPort fail...", e); - }finally { - if(out != null){ - try { - out.close(); - }catch (IOException e){ - logger.warn("out close failed...", e); - } - } - } - - } - } - - - /** - * remove c client by dcn and susysId - * - * @return - */ - class RejectClientBySubSystemHandler implements HttpHandler{ - @Override - public void handle(HttpExchange httpExchange) throws IOException { - String result = ""; - OutputStream out = httpExchange.getResponseBody(); - try{ - String queryString = httpExchange.getRequestURI().getQuery(); - Map queryStringInfo = formData2Dic(queryString); - String dcn = queryStringInfo.get(ProxyConstants.MANAGE_DCN); - String subSystem = queryStringInfo.get(ProxyConstants.MANAGE_SUBSYSTEM); - - if (StringUtils.isBlank(dcn) || StringUtils.isBlank(subSystem)) { - httpExchange.sendResponseHeaders(200, 0); - result = "params illegal!"; - out.write(result.getBytes()); - return; - } - - logger.info("rejectClientBySubSystem in admin,subsys:{},dcn:{}====================",subSystem,dcn); - ClientSessionGroupMapping clientSessionGroupMapping = proxyTCPServer.getClientSessionGroupMapping(); - ConcurrentHashMap sessionMap = clientSessionGroupMapping.getSessionMap(); - final List successRemoteAddrs = new ArrayList(); - try { - if (!sessionMap.isEmpty()) { - for (Session session : sessionMap.values()) { - if (session.getClient().getDcn().equals(dcn) && session.getClient().getSubsystem().equals(subSystem)) { - InetSocketAddress addr = ProxyTcp2Client.serverGoodby2Client(session, clientSessionGroupMapping); - if (addr != null) { - successRemoteAddrs.add(addr); - } - } - } - } - } catch (Exception e) { - logger.error("clientManage|rejectClientBySubSystem|fail|dcn={}|subSystemId={},errMsg={}", dcn, subSystem, e); - result = String.format("rejectClientBySubSystem fail! sessionMap size {%d}, had reject {%d} , {dcn=%s " + - "port=%s}, errorMsg : %s", sessionMap.size(), printClients(successRemoteAddrs), dcn, - subSystem, e.getMessage()); - httpExchange.sendResponseHeaders(200, 0); - out.write(result.getBytes()); - return; - } - result = String.format("rejectClientBySubSystem success! sessionMap size {%d}, had reject {%s} , {dcn=%s " + - "port=%s}", sessionMap.size(), printClients(successRemoteAddrs), dcn, subSystem); - httpExchange.sendResponseHeaders(200, 0); - out.write(result.getBytes()); - }catch (Exception e){ - logger.error("rejectClientBySubSystem fail...", e); - }finally { - if(out != null){ - try { - out.close(); - }catch (IOException e){ - logger.warn("out close failed...", e); - } - } - } - - } - } - - /** - * redirect subsystem for subsys and dcn - * - * @return - */ - class RedirectClientBySubSystemHandler implements HttpHandler{ - @Override - public void handle(HttpExchange httpExchange) throws IOException { - String result = ""; - OutputStream out = httpExchange.getResponseBody(); - try{ - String queryString = httpExchange.getRequestURI().getQuery(); - Map queryStringInfo = formData2Dic(queryString); - String dcn = queryStringInfo.get(ProxyConstants.MANAGE_DCN); - String subSystem = queryStringInfo.get(ProxyConstants.MANAGE_SUBSYSTEM); - String destProxyIp = queryStringInfo.get(ProxyConstants.MANAGE_DEST_IP); - String destProxyPort = queryStringInfo.get(ProxyConstants.MANAGE_DEST_PORT); - - if (StringUtils.isBlank(dcn) || !StringUtils.isNumeric(subSystem) - || StringUtils.isBlank(destProxyIp) || StringUtils.isBlank(destProxyPort) - || !StringUtils.isNumeric(destProxyPort)) { - httpExchange.sendResponseHeaders(200, 0); - result = "params illegal!"; - out.write(result.getBytes()); - return; - } - logger.info("redirectClientBySubSystem in admin,subsys:{},dcn:{},destIp:{},destPort:{}====================",subSystem,dcn,destProxyIp,destProxyPort); - ClientSessionGroupMapping clientSessionGroupMapping = proxyTCPServer.getClientSessionGroupMapping(); - ConcurrentHashMap sessionMap = clientSessionGroupMapping.getSessionMap(); - String redirectResult = ""; - try { - if (!sessionMap.isEmpty()) { - for (Session session : sessionMap.values()) { - if (session.getClient().getDcn().equals(dcn) && session.getClient().getSubsystem().equals(subSystem)) { - redirectResult += "|"; - redirectResult += ProxyTcp2Client.redirectClient2NewProxy(destProxyIp, Integer.parseInt(destProxyPort), - session, clientSessionGroupMapping); - } - } - } - } catch (Exception e) { - logger.error("clientManage|redirectClientBySubSystem|fail|dcn={}|subSystem={}|destProxyIp" + - "={}|destProxyPort={},errMsg={}", dcn, subSystem, destProxyIp, destProxyPort, e); - result = String.format("redirectClientBySubSystem fail! sessionMap size {%d}, {clientIp=%s clientPort=%s " + - "destProxyIp=%s destProxyPort=%s}, result {%s}, errorMsg : %s", - sessionMap.size(), dcn, subSystem, destProxyIp, destProxyPort, redirectResult, e - .getMessage()); - httpExchange.sendResponseHeaders(200, 0); - out.write(result.getBytes()); - return; - } - result = String.format("redirectClientBySubSystem success! sessionMap size {%d}, {dcn=%s subSystem=%s " + - "destProxyIp=%s destProxyPort=%s}, result {%s} ", - sessionMap.size(), dcn, subSystem, destProxyIp, destProxyPort, redirectResult); - httpExchange.sendResponseHeaders(200, 0); - out.write(result.getBytes()); - }catch (Exception e){ - logger.error("redirectClientBySubSystem fail...", e); - }finally { - if(out != null){ - try { - out.close(); - }catch (IOException e){ - logger.warn("out close failed...", e); - } - } - } - - } - } - - /** - * redirect subsystem for path - * - * @return - */ - class RedirectClientByPathHandler implements HttpHandler{ - @Override - public void handle(HttpExchange httpExchange) throws IOException { - String result = ""; - OutputStream out = httpExchange.getResponseBody(); - try{ - String queryString = httpExchange.getRequestURI().getQuery(); - Map queryStringInfo = formData2Dic(queryString); - String path = queryStringInfo.get(ProxyConstants.MANAGE_PATH); - String destProxyIp = queryStringInfo.get(ProxyConstants.MANAGE_DEST_IP); - String destProxyPort = queryStringInfo.get(ProxyConstants.MANAGE_DEST_PORT); - - if (StringUtils.isBlank(path) || StringUtils.isBlank(destProxyIp) || StringUtils.isBlank(destProxyPort) || - !StringUtils.isNumeric(destProxyPort)) { - httpExchange.sendResponseHeaders(200, 0); - result = "params illegal!"; - out.write(result.getBytes()); - return; - } - logger.info("redirectClientByPath in admin,path:{},destIp:{},destPort:{}====================",path,destProxyIp,destProxyPort); - ClientSessionGroupMapping clientSessionGroupMapping = proxyTCPServer.getClientSessionGroupMapping(); - ConcurrentHashMap sessionMap = clientSessionGroupMapping.getSessionMap(); - String redirectResult = ""; - try { - if (!sessionMap.isEmpty()) { - for (Session session : sessionMap.values()) { - if (session.getClient().getPath().contains(path)) { - redirectResult += "|"; - redirectResult += ProxyTcp2Client.redirectClient2NewProxy(destProxyIp, Integer.parseInt(destProxyPort), - session, clientSessionGroupMapping); - } - } - } - } catch (Exception e) { - logger.error("clientManage|redirectClientByPath|fail|path={}|destProxyIp" + - "={}|destProxyPort={},errMsg={}", path, destProxyIp, destProxyPort, e); - result = String.format("redirectClientByPath fail! sessionMap size {%d}, {path=%s " + - "destProxyIp=%s destProxyPort=%s}, result {%s}, errorMsg : %s", - sessionMap.size(), path, destProxyIp, destProxyPort, redirectResult, e - .getMessage()); - httpExchange.sendResponseHeaders(200, 0); - out.write(result.getBytes()); - return; - } - result = String.format("redirectClientByPath success! sessionMap size {%d}, {path=%s " + - "destProxyIp=%s destProxyPort=%s}, result {%s} ", - sessionMap.size(), path, destProxyIp, destProxyPort, redirectResult); - httpExchange.sendResponseHeaders(200, 0); - out.write(result.getBytes()); - }catch (Exception e){ - logger.error("redirectClientByPath fail...", e); - }finally { - if(out != null){ - try { - out.close(); - }catch (IOException e){ - logger.warn("out close failed...", e); - } - } - } - - } - } - - /** - * redirect subsystem for ip and port - * - * @return - */ - class RedirectClientByIpPortHandler implements HttpHandler{ - @Override - public void handle(HttpExchange httpExchange) throws IOException { - String result = ""; - OutputStream out = httpExchange.getResponseBody(); - try{ - String queryString = httpExchange.getRequestURI().getQuery(); - Map queryStringInfo = formData2Dic(queryString); - String ip = queryStringInfo.get(ProxyConstants.MANAGE_IP); - String port = queryStringInfo.get(ProxyConstants.MANAGE_PORT); - String destProxyIp = queryStringInfo.get(ProxyConstants.MANAGE_DEST_IP); - String destProxyPort = queryStringInfo.get(ProxyConstants.MANAGE_DEST_PORT); - - if (StringUtils.isBlank(ip) || !StringUtils.isNumeric(port) - || StringUtils.isBlank(destProxyIp) || StringUtils.isBlank(destProxyPort) - || !StringUtils.isNumeric(destProxyPort)) { - httpExchange.sendResponseHeaders(200, 0); - result = "params illegal!"; - out.write(result.getBytes()); - return; - } - logger.info("redirectClientByIpPort in admin,ip:{},port:{},destIp:{},destPort:{}====================",ip,port,destProxyIp,destProxyPort); - ClientSessionGroupMapping clientSessionGroupMapping = proxyTCPServer.getClientSessionGroupMapping(); - ConcurrentHashMap sessionMap = clientSessionGroupMapping.getSessionMap(); - String redirectResult = ""; - try { - if (!sessionMap.isEmpty()) { - for (Session session : sessionMap.values()) { - if (session.getClient().getHost().equals(ip) && String.valueOf(session.getClient().getPort()).equals(port)) { - redirectResult += "|"; - redirectResult += ProxyTcp2Client.redirectClient2NewProxy(destProxyIp, Integer.parseInt(destProxyPort), - session, clientSessionGroupMapping); - } - } - } - } catch (Exception e) { - logger.error("clientManage|redirectClientByIpPort|fail|ip={}|port={}|destProxyIp" + - "={}|destProxyPort={},errMsg={}", ip, port, destProxyIp, destProxyPort, e); - result = String.format("redirectClientByIpPort fail! sessionMap size {%d}, {clientIp=%s clientPort=%s " + - "destProxyIp=%s destProxyPort=%s}, result {%s}, errorMsg : %s", - sessionMap.size(), ip, port, destProxyIp, destProxyPort, redirectResult, e - .getMessage()); - httpExchange.sendResponseHeaders(200, 0); - out.write(result.getBytes()); - return; - } - result = String.format("redirectClientByIpPort success! sessionMap size {%d}, {ip=%s port=%s " + - "destProxyIp=%s destProxyPort=%s}, result {%s} ", - sessionMap.size(), ip, port, destProxyIp, destProxyPort, redirectResult); - httpExchange.sendResponseHeaders(200, 0); - out.write(result.getBytes()); - }catch (Exception e){ - logger.error("redirectClientByIpPort fail...", e); - }finally { - if(out != null){ - try { - out.close(); - }catch (IOException e){ - logger.warn("out close failed...", e); - } - } - } - - } - } - - private String printClients(List clients) { - if (clients.isEmpty()) { - return "no session had been closed"; - } - StringBuffer sb = new StringBuffer(); - for (InetSocketAddress addr : clients) { - sb.append(addr).append("|"); - } - return sb.toString(); - } - - private Map formData2Dic(String formData) { - Map result = new HashMap<>(); - if(formData== null || formData.trim().length() == 0) { - return result; - } - final String[] items = formData.split("&"); - Arrays.stream(items).forEach(item ->{ - final String[] keyAndVal = item.split("="); - if( keyAndVal.length == 2) { - try{ - final String key = URLDecoder.decode( keyAndVal[0],"utf8"); - final String val = URLDecoder.decode( keyAndVal[1],"utf8"); - result.put(key,val); - }catch (UnsupportedEncodingException e) { - logger.warn("formData2Dic:param decode failed...", e); - } - } - }); - return result; - } - - class ProxyMsgDownStreamHandler implements HttpHandler{ - @Override - public void handle(HttpExchange httpExchange) throws IOException { - String result = "false"; - OutputStream out = httpExchange.getResponseBody(); - try{ - Map queryStringInfo = parsePostParameters(httpExchange); - String msgStr = (String)queryStringInfo.get("msg"); - String groupName = (String)queryStringInfo.get("group"); - logger.info("recieve msg from other proxy, group:{}, msg:{}", groupName, msgStr); - if (StringUtils.isBlank(msgStr) || StringUtils.isBlank(groupName)) { - logger.warn("msg or groupName is null"); - httpExchange.sendResponseHeaders(200, 0); - out.write(result.getBytes()); - return; - } - MessageExt messageExt = JSON.parseObject(msgStr, MessageExt.class); - String topic = messageExt.getTopic(); - - if (!ProxyUtil.isValidRMBTopic(topic)) { - logger.warn("msg topic is illegal"); - httpExchange.sendResponseHeaders(200, 0); - out.write(result.getBytes()); - return; - } - - DownstreamDispatchStrategy downstreamDispatchStrategy = proxyTCPServer.getClientSessionGroupMapping().getClientGroupWrapper(groupName).getDownstreamDispatchStrategy(); - Set groupConsumerSessions = proxyTCPServer.getClientSessionGroupMapping().getClientGroupWrapper(groupName).getGroupConsumerSessions(); - Session session = downstreamDispatchStrategy.select(groupName, topic, groupConsumerSessions); - - if(session == null){ - logger.error("DownStream msg,retry other proxy found no session again"); - httpExchange.sendResponseHeaders(200, 0); - out.write(result.getBytes()); - return; - } - - DownStreamMsgContext downStreamMsgContext = - new DownStreamMsgContext(messageExt, session, proxyTCPServer.getClientSessionGroupMapping().getClientGroupWrapper(groupName).getPersistentMsgConsumer(), null, true); - proxyTCPServer.getClientSessionGroupMapping().getClientGroupWrapper(groupName).getDownstreamMap().putIfAbsent(downStreamMsgContext.seq, downStreamMsgContext); - - if (session.isCanDownStream()) { - session.downstreamMsg(downStreamMsgContext); - httpExchange.sendResponseHeaders(200, 0); - result = "true"; - out.write(result.getBytes()); - return; - } - - logger.warn("ProxyMsgDownStreamHandler|dispatch retry, seq[{}]", downStreamMsgContext.seq); - long delayTime = ProxyUtil.isService(downStreamMsgContext.msgExt.getTopic()) ? 0 : proxyTCPServer.getAccessConfiguration().proxyTcpMsgRetryDelayInMills; - downStreamMsgContext.delay(delayTime); - proxyTCPServer.getProxyTcpRetryer().pushRetry(downStreamMsgContext); - result = "true"; - httpExchange.sendResponseHeaders(200, 0); - out.write(result.getBytes()); - - }catch (Exception e){ - logger.error("ProxyMsgDownStreamHandler handle fail...", e); - }finally { - if(out != null){ - try { - out.close(); - }catch (IOException e){ - logger.warn("out close failed...", e); - } - } - } - - } - } -} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/boot/ProxyServer.java b/eventmesh-emesher/src/main/java/com/webank/emesher/boot/ProxyServer.java deleted file mode 100644 index e3bff43852..0000000000 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/boot/ProxyServer.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.emesher.boot; - -import com.webank.emesher.common.ServiceState; -import com.webank.emesher.configuration.AccessConfiguration; -import com.webank.emesher.configuration.ProxyConfiguration; -import com.webank.emesher.constants.ProxyConstants; -import org.apache.rocketmq.client.impl.consumer.ConsumeMessageConcurrentlyService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class ProxyServer { - - public Logger logger = LoggerFactory.getLogger(this.getClass()); - - public ProxyHTTPServer proxyHTTPServer; - - private ProxyTCPServer proxyTCPServer; - - private ProxyConfiguration proxyConfiguration; - - private AccessConfiguration accessConfiguration; - - private ServiceState serviceState; - - public ProxyServer(ProxyConfiguration proxyConfiguration, - AccessConfiguration accessConfiguration) { - this.proxyConfiguration = proxyConfiguration; - this.accessConfiguration = accessConfiguration; - } - - public void init() throws Exception { - proxyHTTPServer = new ProxyHTTPServer(this, proxyConfiguration); - proxyHTTPServer.init(); - proxyTCPServer = new ProxyTCPServer(this, accessConfiguration); - if (accessConfiguration.proxyTcpServerEnabled) { - proxyTCPServer.init(); - } - - String eventstore = System.getProperty(ProxyConstants.EVENT_STORE_PROPERTIES, System.getenv(ProxyConstants.EVENT_STORE_ENV)); - logger.info("eventstore : {}", eventstore); - logger.info("load custom {} class for proxy", ConsumeMessageConcurrentlyService.class.getCanonicalName()); - - serviceState = ServiceState.INITED; - logger.info("server state:{}",serviceState); - } - - public void start() throws Exception { - proxyHTTPServer.start(); - if (accessConfiguration.proxyTcpServerEnabled) { - proxyTCPServer.start(); - } - serviceState = ServiceState.RUNNING; - logger.info("server state:{}",serviceState); - } - - public void shutdown() throws Exception { - serviceState = ServiceState.STOPING; - logger.info("server state:{}",serviceState); - proxyHTTPServer.shutdown(); - if (accessConfiguration.proxyTcpServerEnabled) { - proxyTCPServer.shutdown(); - } - serviceState = ServiceState.STOPED; - logger.info("server state:{}",serviceState); - } - - public ProxyHTTPServer getProxyHTTPServer() { - return proxyHTTPServer; - } - - public ProxyTCPServer getProxyTCPServer() { - return proxyTCPServer; - } - - public ServiceState getServiceState() { return serviceState; } -} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/boot/ProxyTCPServer.java b/eventmesh-emesher/src/main/java/com/webank/emesher/boot/ProxyTCPServer.java deleted file mode 100644 index 21485fa024..0000000000 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/boot/ProxyTCPServer.java +++ /dev/null @@ -1,251 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.emesher.boot; - -import com.google.common.util.concurrent.RateLimiter; -import com.webank.emesher.admin.controller.ClientManageController; -import com.webank.emesher.configuration.AccessConfiguration; -import com.webank.emesher.core.protocol.tcp.client.ProxyTcpConnectionHandler; -import com.webank.emesher.core.protocol.tcp.client.ProxyTcpExceptionHandler; -import com.webank.emesher.core.protocol.tcp.client.ProxyTcpMessageDispatcher; -import com.webank.emesher.core.protocol.tcp.client.group.ClientSessionGroupMapping; -import com.webank.emesher.core.protocol.tcp.client.session.push.retry.ProxyTcpRetryer; -import com.webank.emesher.metrics.tcp.ProxyTcpMonitor; -import com.webank.emesher.util.ProxyThreadFactoryImpl; -import com.webank.eventmesh.common.ThreadPoolFactory; -import com.webank.eventmesh.common.protocol.tcp.codec.Codec; -import io.netty.bootstrap.ServerBootstrap; -import io.netty.buffer.PooledByteBufAllocator; -import io.netty.channel.*; -import io.netty.channel.socket.nio.NioServerSocketChannel; -import io.netty.handler.timeout.IdleStateHandler; -import io.netty.handler.traffic.ChannelTrafficShapingHandler; -import io.netty.handler.traffic.GlobalTrafficShapingHandler; - -import java.util.concurrent.ExecutorService; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledFuture; - -public class ProxyTCPServer extends AbstractRemotingServer { - - private ClientSessionGroupMapping clientSessionGroupMapping; - - private ProxyTcpRetryer proxyTcpRetryer; - - private ProxyTcpMonitor proxyTcpMonitor; - - private ClientManageController clientManageController; - - private ProxyServer proxyServer; - - private AccessConfiguration accessConfiguration; - - private GlobalTrafficShapingHandler globalTrafficShapingHandler; - - public static ScheduledExecutorService scheduler; - - public static ExecutorService taskHandleExecutorService; - - public ScheduledFuture tcpRegisterTask; - - public RateLimiter rateLimiter; - - public ProxyTCPServer(ProxyServer proxyServer, - AccessConfiguration accessConfiguration) { - super(); - this.proxyServer = proxyServer; - this.accessConfiguration = accessConfiguration; - } - - private void startServer() throws Exception { - Runnable r = () -> { - ServerBootstrap bootstrap = new ServerBootstrap(); - bootstrap.group(bossGroup, ioGroup) - .channel(NioServerSocketChannel.class) - .option(ChannelOption.SO_BACKLOG, 128) - .option(ChannelOption.TCP_NODELAY, true) - .option(ChannelOption.SO_REUSEADDR, true) - .option(ChannelOption.SO_KEEPALIVE, false) - .option(ChannelOption.SO_TIMEOUT, 600000) - .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000) - .option(ChannelOption.SO_LINGER, 0) - .childOption(ChannelOption.SO_SNDBUF, 65535 * 4) - .childOption(ChannelOption.SO_RCVBUF, 65535 * 4) - .option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(2048, 4096, 65536)) - .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) - .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) - .childHandler(new ChannelInitializer() { - @Override - public void initChannel(Channel ch) throws Exception { - ch.pipeline().addLast(new Codec.Encoder()) - .addLast(new Codec.Decoder()) - .addLast("global-traffic-shaping", globalTrafficShapingHandler) - .addLast("channel-traffic-shaping", newCTSHandler()) - .addLast(new ProxyTcpConnectionHandler(ProxyTCPServer.this)) - .addLast(workerGroup, new IdleStateHandler(accessConfiguration.proxyTcpIdleReadSeconds, - accessConfiguration.proxyTcpIdleWriteSeconds, - accessConfiguration.proxyTcpIdleAllSeconds), - new ProxyTcpMessageDispatcher(ProxyTCPServer.this), - new ProxyTcpExceptionHandler(ProxyTCPServer.this) - ); - } - }); - try { - int port = accessConfiguration.proxyTcpServerPort; - ChannelFuture f = bootstrap.bind(port).sync(); - logger.info("ProxyTCPServer[port={}] started.....", port); - f.channel().closeFuture().sync(); - } catch (Exception e) { - logger.error("ProxyTCPServer RemotingServer Start Err!", e); - try { - shutdown(); - } catch (Exception e1) { - logger.error("ProxyTCPServer RemotingServer shutdown Err!", e); - } - return; - } - }; - - Thread t = new Thread(r, "proxy-tcp-server"); - t.start(); - } - - public void init() throws Exception { - logger.info("==================ProxyTCPServer Initialing=================="); - initThreadPool(); - - rateLimiter = RateLimiter.create(accessConfiguration.proxyTcpMsgReqnumPerSecond); - - globalTrafficShapingHandler = newGTSHandler(); - - clientManageController = new ClientManageController(this); - - clientSessionGroupMapping = new ClientSessionGroupMapping(this); - clientSessionGroupMapping.init(); - - proxyTcpRetryer = new ProxyTcpRetryer(this); - proxyTcpRetryer.init(); - - proxyTcpMonitor = new ProxyTcpMonitor(this); - proxyTcpMonitor.init(); - - logger.info("--------------------------ProxyTCPServer Inited"); - } - - public void start() throws Exception { - startServer(); - - clientSessionGroupMapping.start(); - - proxyTcpRetryer.start(); - - proxyTcpMonitor.start(); - - clientManageController.start(); - - logger.info("--------------------------ProxyTCPServer Started"); - } - - public void shutdown() throws Exception { - if (bossGroup != null) { - bossGroup.shutdownGracefully(); - logger.info("shutdown bossGroup, no client is allowed to connect access server"); - } - - clientSessionGroupMapping.shutdown(); - try { - Thread.sleep(40 * 1000); - } catch (InterruptedException e) { - logger.error("interruptedException occurred while sleeping", e); - } - - globalTrafficShapingHandler.release(); - - if (ioGroup != null) { - ioGroup.shutdownGracefully(); - logger.info("shutdown ioGroup"); - } - if (workerGroup != null) { - workerGroup.shutdownGracefully(); - logger.info("shutdown workerGroup"); - } - - proxyTcpRetryer.shutdown(); - - proxyTcpMonitor.shutdown(); - - shutdownThreadPool(); - logger.info("--------------------------ProxyTCPServer Shutdown"); - } - - private void initThreadPool() throws Exception { - super.init("proxy-tcp"); - - scheduler = ThreadPoolFactory.createScheduledExecutor(accessConfiguration.proxyTcpGlobalScheduler, new ProxyThreadFactoryImpl("proxy-tcp-scheduler", true)); - - taskHandleExecutorService = ThreadPoolFactory.createThreadPoolExecutor(accessConfiguration.proxyTcpTaskHandleExecutorPoolSize, accessConfiguration.proxyTcpTaskHandleExecutorPoolSize, new LinkedBlockingQueue(10000), new ProxyThreadFactoryImpl("proxy-tcp-task-handle", true));; - } - - private void shutdownThreadPool(){ - scheduler.shutdown(); - taskHandleExecutorService.shutdown(); - } - - private GlobalTrafficShapingHandler newGTSHandler() { - GlobalTrafficShapingHandler handler = new GlobalTrafficShapingHandler(scheduler, 0, accessConfiguration.getGtc().getReadLimit()) { - @Override - protected long calculateSize(Object msg) { - return 1; - } - }; - handler.setMaxTimeWait(1000); - return handler; - } - - private ChannelTrafficShapingHandler newCTSHandler() { - ChannelTrafficShapingHandler handler = new ChannelTrafficShapingHandler(0, accessConfiguration.getCtc().getReadLimit()) { - @Override - protected long calculateSize(Object msg) { - return 1; - } - }; - handler.setMaxTimeWait(3000); - return handler; - } - - public ClientSessionGroupMapping getClientSessionGroupMapping() { - return clientSessionGroupMapping; - } - - public ProxyTcpRetryer getProxyTcpRetryer() { - return proxyTcpRetryer; - } - - public ProxyTcpMonitor getProxyTcpMonitor() { - return proxyTcpMonitor; - } - - public ProxyServer getProxyServer() { - return proxyServer; - } - - public AccessConfiguration getAccessConfiguration() { - return accessConfiguration; - } -} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/configuration/AccessConfiguration.java b/eventmesh-emesher/src/main/java/com/webank/emesher/configuration/AccessConfiguration.java deleted file mode 100644 index ae16163b44..0000000000 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/configuration/AccessConfiguration.java +++ /dev/null @@ -1,293 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.emesher.configuration; - -import com.google.common.base.Preconditions; -import org.apache.commons.lang3.StringUtils; - -public class AccessConfiguration extends CommonConfiguration { - public int proxyTcpServerPort = 10000; - - public int proxyTcpIdleAllSeconds = 60; - - public int proxyTcpIdleWriteSeconds = 60; - - public int proxyTcpIdleReadSeconds = 60; - - public Integer proxyTcpMsgReqnumPerSecond = 15000; - - /** - * TCP Server allows max client num - */ - public int proxyTcpClientMaxNum = 10000; - - //======================================= New add config ================================= - /** - * whether enable TCP Serer - */ - public boolean proxyTcpServerEnabled = Boolean.FALSE; - - public int proxyTcpGlobalScheduler = 5; - - public int proxyTcpTaskHandleExecutorPoolSize = Runtime.getRuntime().availableProcessors(); - - public int proxyTcpSessionExpiredInMills = 60000; - - public int proxyTcpSessionUpstreamBufferSize = 100; - - public int proxyTcpSessionDownstreamUnackSize = 12; - - public int proxyTcpMsgRetryTimes = 3; - - public int proxyTcpMsgRetryDelayInMills = 500; - - public int proxyTcpMsgRetryQueueSize = 10000; - - public Integer proxyTcpRebalanceIntervalInMills = 30 * 1000; - - public int proxyServerAdminPort = 10106; - - public boolean proxyTcpSendBackEnabled = Boolean.TRUE; - - public int proxyTcpSendBackMaxTimes = 3; - - public int proxyTcpPushFailIsolateTimeInMills = 30 * 1000; - - public int proxyTcpDownStreamMapSize = 500; - - private TrafficShapingConfig gtc = new TrafficShapingConfig(0, 10_000, 1_000, 2000); - private TrafficShapingConfig ctc = new TrafficShapingConfig(0, 2_000, 1_000, 10_000); - - public AccessConfiguration(ConfigurationWraper configurationWraper){ - super(configurationWraper); - } - - public void init(){ - super.init(); - String proxyTcpServerPortStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_SERVER_TCP_PORT); - Preconditions.checkState(StringUtils.isNotEmpty(proxyTcpServerPortStr) && StringUtils.isNumeric(proxyTcpServerPortStr), - String.format("%s error", ConfKeys.KEYS_PROXY_SERVER_TCP_PORT)); - proxyTcpServerPort = Integer.valueOf(StringUtils.deleteWhitespace(proxyTcpServerPortStr)); - - String proxyTcpIdleReadSecondsStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_SERVER_READER_IDLE_SECONDS); - Preconditions.checkState(StringUtils.isNotEmpty(proxyTcpIdleReadSecondsStr) && StringUtils.isNumeric(proxyTcpIdleReadSecondsStr), - String.format("%s error", ConfKeys.KEYS_PROXY_SERVER_READER_IDLE_SECONDS)); - proxyTcpIdleReadSeconds = Integer.valueOf(StringUtils.deleteWhitespace(proxyTcpIdleReadSecondsStr)); - - String proxyTcpIdleWriteSecondsStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_SERVER_WRITER_IDLE_SECONDS); - Preconditions.checkState(StringUtils.isNotEmpty(proxyTcpIdleWriteSecondsStr) && StringUtils.isNumeric(proxyTcpIdleWriteSecondsStr), - String.format("%s error", ConfKeys.KEYS_PROXY_SERVER_WRITER_IDLE_SECONDS)); - proxyTcpIdleWriteSeconds = Integer.valueOf(StringUtils.deleteWhitespace(proxyTcpIdleWriteSecondsStr)); - - String proxyTcpIdleAllSecondsStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_SERVER_ALL_IDLE_SECONDS); - Preconditions.checkState(StringUtils.isNotEmpty(proxyTcpIdleAllSecondsStr) && StringUtils.isNumeric(proxyTcpIdleAllSecondsStr), - String.format("%s error", ConfKeys.KEYS_PROXY_SERVER_ALL_IDLE_SECONDS)); - proxyTcpIdleAllSeconds = Integer.valueOf(StringUtils.deleteWhitespace(proxyTcpIdleAllSecondsStr)); - - String proxyTcpMsgReqnumPerSecondStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_SERVER_MSG_REQ_NUM_PER_SECONDS); - Preconditions.checkState(StringUtils.isNotEmpty(proxyTcpMsgReqnumPerSecondStr) && StringUtils.isNumeric(proxyTcpMsgReqnumPerSecondStr), - String.format("%s error", ConfKeys.KEYS_PROXY_SERVER_MSG_REQ_NUM_PER_SECONDS)); - proxyTcpMsgReqnumPerSecond = Integer.valueOf(StringUtils.deleteWhitespace(proxyTcpMsgReqnumPerSecondStr)); - - String proxyTcpClientMaxNumStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_SERVER_CLIENT_MAX_NUM); - Preconditions.checkState(StringUtils.isNotEmpty(proxyTcpClientMaxNumStr) && StringUtils.isNumeric(proxyTcpClientMaxNumStr), - String.format("%s error", ConfKeys.KEYS_PROXY_SERVER_CLIENT_MAX_NUM)); - proxyTcpClientMaxNum = Integer.valueOf(StringUtils.deleteWhitespace(proxyTcpClientMaxNumStr)); - - String proxyTcpServerEnabledStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_TCP_SERVER_ENABLED); - if (StringUtils.isNotEmpty(proxyTcpServerEnabledStr)) { - proxyTcpServerEnabled = Boolean.valueOf(StringUtils.deleteWhitespace(proxyTcpServerEnabledStr)); - } - - String proxyTcpGlobalSchedulerStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_SERVER_GLOBAL_SCHEDULER); - if(StringUtils.isNotEmpty(proxyTcpGlobalSchedulerStr)){ - Preconditions.checkState(StringUtils.isNumeric(proxyTcpGlobalSchedulerStr), - String.format("%s error", ConfKeys.KEYS_PROXY_SERVER_GLOBAL_SCHEDULER)); - proxyTcpGlobalScheduler = Integer.valueOf(StringUtils.deleteWhitespace(proxyTcpGlobalSchedulerStr)); - } - - String proxyTcpTaskHandleExecutorPoolSizeStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_SERVER_TCP_TASK_HANDLE_POOL_SIZE); - if(StringUtils.isNotEmpty(proxyTcpTaskHandleExecutorPoolSizeStr)){ - Preconditions.checkState(StringUtils.isNumeric(proxyTcpTaskHandleExecutorPoolSizeStr), - String.format("%s error", ConfKeys.KEYS_PROXY_SERVER_TCP_TASK_HANDLE_POOL_SIZE)); - proxyTcpTaskHandleExecutorPoolSize = Integer.valueOf(StringUtils.deleteWhitespace(proxyTcpTaskHandleExecutorPoolSizeStr)); - } - - String proxyTcpSessionExpiredInMillsStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_SERVER_SESSION_EXPIRED_TIME); - if(StringUtils.isNotEmpty(proxyTcpSessionExpiredInMillsStr)){ - Preconditions.checkState(StringUtils.isNumeric(proxyTcpSessionExpiredInMillsStr), String.format("%s error", ConfKeys.KEYS_PROXY_SERVER_SESSION_EXPIRED_TIME)); - proxyTcpSessionExpiredInMills = Integer.valueOf(StringUtils.deleteWhitespace(proxyTcpSessionExpiredInMillsStr)); - } - - String proxyTcpSessionUpstreamBufferSizeStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_SERVER_SESSION_UPSTREAM_BUFFER_SIZE); - if(StringUtils.isNotEmpty(proxyTcpSessionUpstreamBufferSizeStr)){ - Preconditions.checkState(StringUtils.isNumeric(proxyTcpSessionUpstreamBufferSizeStr), - String.format("%s error", ConfKeys.KEYS_PROXY_SERVER_SESSION_UPSTREAM_BUFFER_SIZE)); - proxyTcpSessionUpstreamBufferSize = Integer.valueOf(StringUtils.deleteWhitespace(proxyTcpSessionUpstreamBufferSizeStr)); - } - - String proxyTcpSessionDownstreamUnackSizeStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_SERVER_SESSION_DOWNSTREAM_UNACK_SIZE); - if(StringUtils.isNotEmpty(proxyTcpSessionDownstreamUnackSizeStr)){ - Preconditions.checkState(StringUtils.isNumeric(proxyTcpSessionDownstreamUnackSizeStr), String.format("%s error", ConfKeys.KEYS_PROXY_SERVER_SESSION_DOWNSTREAM_UNACK_SIZE)); - proxyTcpSessionDownstreamUnackSize = Integer.valueOf(StringUtils.deleteWhitespace(proxyTcpSessionDownstreamUnackSizeStr)); - } - - //========================================proxy retry config=============================================// - String proxyTcpMsgRetryTimesStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_SERVER_RETRY_PUSH_RETRY_TIMES); - if(StringUtils.isNotEmpty(proxyTcpMsgRetryTimesStr)) { - Preconditions.checkState(StringUtils.isNumeric(proxyTcpMsgRetryTimesStr), String.format("%s error", ConfKeys.KEYS_PROXY_SERVER_RETRY_PUSH_RETRY_TIMES)); - proxyTcpMsgRetryTimes = Integer.valueOf(StringUtils.deleteWhitespace(proxyTcpMsgRetryTimesStr)); - } - - String proxyTcpMsgRetryDelayInMillsStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_SERVER_RETRY_PUSH_RETRY_DELAY); - if(StringUtils.isNotEmpty(proxyTcpMsgRetryDelayInMillsStr)) { - Preconditions.checkState(StringUtils.isNumeric(proxyTcpMsgRetryDelayInMillsStr), String.format("%s error", ConfKeys.KEYS_PROXY_SERVER_RETRY_PUSH_RETRY_DELAY)); - proxyTcpMsgRetryDelayInMills = Integer.valueOf(StringUtils.deleteWhitespace(proxyTcpMsgRetryDelayInMillsStr)); - } - - String proxyTcpMsgRetryQueueSizeStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_SERVER_RETRY_PUSH_RETRY_QUEUE_SIZE); - if(StringUtils.isNotEmpty(proxyTcpMsgRetryQueueSizeStr)) { - Preconditions.checkState(StringUtils.isNumeric(proxyTcpMsgRetryQueueSizeStr), String.format("%s error", ConfKeys.KEYS_PROXY_SERVER_RETRY_PUSH_RETRY_QUEUE_SIZE)); - proxyTcpMsgRetryQueueSize = Integer.valueOf(StringUtils.deleteWhitespace(proxyTcpMsgRetryQueueSizeStr)); - } - - String proxyTcpRebalanceIntervalStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_SERVER_TCP_REBALANCE_INTERVAL); - if (StringUtils.isNotEmpty(proxyTcpRebalanceIntervalStr)) { - Preconditions.checkState(StringUtils.isNumeric(proxyTcpRebalanceIntervalStr), - String.format("%s error", ConfKeys.KEYS_PROXY_SERVER_TCP_REBALANCE_INTERVAL)); - proxyTcpRebalanceIntervalInMills = Integer.valueOf(StringUtils.deleteWhitespace(proxyTcpRebalanceIntervalStr)); - } - - String proxyServerAdminPortStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_SERVER_ADMIN_HTTP_PORT); - if(StringUtils.isNotEmpty(proxyServerAdminPortStr)){ - Preconditions.checkState(StringUtils.isNumeric(proxyServerAdminPortStr), - String.format("%s error", ConfKeys.KEYS_PROXY_SERVER_ADMIN_HTTP_PORT)); - proxyServerAdminPort = Integer.valueOf(StringUtils.deleteWhitespace(proxyServerAdminPortStr)); - } - - String proxyTcpSendBackEnabledStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_TCP_SEND_BACK_ENABLED); - if (StringUtils.isNotEmpty(proxyTcpSendBackEnabledStr)) { - proxyTcpSendBackEnabled = Boolean.valueOf(StringUtils.deleteWhitespace(proxyTcpSendBackEnabledStr)); - } - - String proxyTcpPushFailIsolateTimeInMillsStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_SERVER_PUSH_FAIL_ISOLATE_TIME); - if(StringUtils.isNotEmpty(proxyTcpPushFailIsolateTimeInMillsStr)) { - Preconditions.checkState(StringUtils.isNumeric(proxyTcpPushFailIsolateTimeInMillsStr), String.format("%s error", ConfKeys.KEYS_PROXY_SERVER_PUSH_FAIL_ISOLATE_TIME)); - proxyTcpPushFailIsolateTimeInMills = Integer.valueOf(StringUtils.deleteWhitespace(proxyTcpPushFailIsolateTimeInMillsStr)); - } - - String proxyTcpDownStreamMapSizeStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_TCP_DOWNSTREAM_MAP_SIZE); - if(StringUtils.isNotEmpty(proxyTcpDownStreamMapSizeStr)) { - Preconditions.checkState(StringUtils.isNumeric(proxyTcpDownStreamMapSizeStr), String.format("%s error", ConfKeys.KEYS_PROXY_TCP_DOWNSTREAM_MAP_SIZE)); - proxyTcpDownStreamMapSize = Integer.valueOf(StringUtils.deleteWhitespace(proxyTcpDownStreamMapSizeStr)); - } - } - - public TrafficShapingConfig getGtc() { - return gtc; - } - - public TrafficShapingConfig getCtc() { - return ctc; - } - - static class ConfKeys{ - - public static String KEYS_PROXY_SERVER_TCP_PORT = "proxy.server.tcp.port"; - public static String KEYS_PROXY_SERVER_READER_IDLE_SECONDS = "proxy.server.tcp.readerIdleSeconds"; - public static String KEYS_PROXY_SERVER_WRITER_IDLE_SECONDS = "proxy.server.tcp.writerIdleSeconds"; - public static String KEYS_PROXY_SERVER_ALL_IDLE_SECONDS = "proxy.server.tcp.allIdleSeconds"; - public static String KEYS_PROXY_SERVER_CLIENT_MAX_NUM = "proxy.server.tcp.clientMaxNum"; - public static String KEYS_PROXY_SERVER_MSG_REQ_NUM_PER_SECONDS = "proxy.server.tcp.msgReqnumPerSecond"; - public static String KEYS_PROXY_SERVER_TCP_REBALANCE_INTERVAL = "proxy.server.tcp.RebalanceIntervalInMills"; - public static String KEYS_PROXY_SERVER_GLOBAL_SCHEDULER = "proxy.server.global.scheduler"; - public static String KEYS_PROXY_SERVER_TCP_TASK_HANDLE_POOL_SIZE = "proxy.server.tcp.taskHandleExecutorPoolSize"; - public static String KEYS_PROXY_SERVER_SESSION_EXPIRED_TIME = "proxy.server.session.expiredInMills"; - public static String KEYS_PROXY_SERVER_SESSION_UPSTREAM_BUFFER_SIZE = "proxy.server.session.upstreamBufferSize"; - public static String KEYS_PROXY_SERVER_SESSION_DOWNSTREAM_UNACK_SIZE = "proxy.server.session.downstreamUnackSize"; - public static String KEYS_PROXY_SERVER_RETRY_PUSH_RETRY_TIMES = "proxy.server.retry.pushRetryTimes"; - public static String KEYS_PROXY_SERVER_RETRY_PUSH_RETRY_DELAY = "proxy.server.retry.pushRetryDelayInMills"; - public static String KEYS_PROXY_SERVER_RETRY_PUSH_RETRY_QUEUE_SIZE = "proxy.server.retry.pushRetryQueueSize"; - public static String KEYS_PROXY_SERVER_ADMIN_HTTP_PORT = "proxy.server.admin.http.port"; - public static String KEYS_PROXY_TCP_SERVER_ENABLED = "proxy.server.tcp.enabled"; - public static String KEYS_PROXY_TCP_SEND_BACK_ENABLED = "proxy.server.tcp.sendBack.enabled"; - public static String KEYS_PROXY_SERVER_PUSH_FAIL_ISOLATE_TIME = "proxy.server.tcp.pushFailIsolateTimeInMills"; - public static String KEYS_PROXY_TCP_DOWNSTREAM_MAP_SIZE = "proxy.server.tcp.downstreamMapSize"; - } - - public static class TrafficShapingConfig { - long writeLimit = 0; - long readLimit = 1000; - long checkInterval = 1000; - long maxTime = 5000; - - public TrafficShapingConfig(long writeLimit, long readLimit, long checkInterval, long maxTime) { - this.writeLimit = writeLimit; - this.readLimit = readLimit; - this.checkInterval = checkInterval; - this.maxTime = maxTime; - } - - public TrafficShapingConfig() { - - } - - public long getWriteLimit() { - return writeLimit; - } - - public void setWriteLimit(long writeLimit) { - this.writeLimit = writeLimit; - } - - public long getReadLimit() { - return readLimit; - } - - public void setReadLimit(long readLimit) { - this.readLimit = readLimit; - } - - public long getCheckInterval() { - return checkInterval; - } - - public void setCheckInterval(long checkInterval) { - this.checkInterval = checkInterval; - } - - public long getMaxTime() { - return maxTime; - } - - public void setMaxTime(long maxTime) { - this.maxTime = maxTime; - } - - @Override - public String toString() { - return "TrafficShapingConfig{" + - "writeLimit=" + writeLimit + - ", readLimit=" + readLimit + - ", checkInterval=" + checkInterval + - ", maxTime=" + maxTime + - '}'; - } - } - -} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/configuration/CommonConfiguration.java b/eventmesh-emesher/src/main/java/com/webank/emesher/configuration/CommonConfiguration.java deleted file mode 100644 index 9a6cdf7868..0000000000 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/configuration/CommonConfiguration.java +++ /dev/null @@ -1,212 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.emesher.configuration; - -import com.google.common.base.Preconditions; -import com.webank.emesher.util.ProxyUtil; -import org.apache.commons.lang3.StringUtils; - -public class CommonConfiguration { - public String proxyEnv = "P"; - public String proxyRegion = ""; - public String proxyIDC = "FT"; - public String proxyDCN = "1C0"; - public String proxyCluster = "LS"; - public String proxyName = ""; - public String sysID = "5477"; - - - public String namesrvAddr = ""; - public String clientUserName = "username"; - public String clientPass = "user@123"; - public Integer consumeThreadMin = 2; - public Integer consumeThreadMax = 2; - public Integer consumeQueueSize = 10000; - public Integer pullBatchSize = 32; - public Integer ackWindow = 1000; - public Integer pubWindow = 100; - public long consumeTimeout = 0L; - public Integer pollNameServerInteval = 10 * 1000; - public Integer heartbeatBrokerInterval = 30 * 1000; - public Integer rebalanceInterval = 20 * 1000; - public Integer proxyRegisterIntervalInMills = 10 * 1000; - public Integer proxyFetchRegistryAddrInterval = 10 * 1000; - public String proxyServerIp = null; - protected ConfigurationWraper configurationWraper; - - public CommonConfiguration(ConfigurationWraper configurationWraper) { - this.configurationWraper = configurationWraper; - } - - public void init() { - String proxyEnvStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_ENV); - Preconditions.checkState(StringUtils.isNotEmpty(proxyEnvStr), String.format("%s error", ConfKeys.KEYS_PROXY_ENV)); - proxyEnv = StringUtils.deleteWhitespace(proxyEnvStr); - - String proxyRegionStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_REGION); - Preconditions.checkState(StringUtils.isNotEmpty(proxyRegionStr), String.format("%s error", ConfKeys.KEYS_PROXY_REGION)); - proxyRegion = StringUtils.deleteWhitespace(proxyRegionStr); - - String sysIdStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_SYSID); - Preconditions.checkState(StringUtils.isNotEmpty(sysIdStr) && StringUtils.isNumeric(sysIdStr), String.format("%s error", ConfKeys.KEYS_PROXY_SYSID)); - sysID = StringUtils.deleteWhitespace(sysIdStr); - - String proxyClusterStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_SERVER_CLUSTER); - Preconditions.checkState(StringUtils.isNotEmpty(proxyClusterStr), String.format("%s error", ConfKeys.KEYS_PROXY_SERVER_CLUSTER)); - proxyCluster = StringUtils.deleteWhitespace(proxyClusterStr); - - String proxyNameStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_SERVER_NAME); - Preconditions.checkState(StringUtils.isNotEmpty(proxyNameStr), String.format("%s error", ConfKeys.KEYS_PROXY_SERVER_NAME)); - proxyName = StringUtils.deleteWhitespace(proxyNameStr); - - String proxyIDCStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_IDC); - Preconditions.checkState(StringUtils.isNotEmpty(proxyIDCStr), String.format("%s error", ConfKeys.KEYS_PROXY_IDC)); - proxyIDC = StringUtils.deleteWhitespace(proxyIDCStr); - - String proxyDCNStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_DCN); - Preconditions.checkState(StringUtils.isNotEmpty(proxyDCNStr), String.format("%s error", ConfKeys.KEYS_PROXY_DCN)); - proxyDCN = StringUtils.deleteWhitespace(proxyDCNStr); - - String clientUserNameStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_DEFIBUS_USERNAME); - if (StringUtils.isNotBlank(clientUserNameStr)) { - clientUserName = StringUtils.trim(clientUserNameStr); - } - - String clientPassStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_DEFIBUS_PASSWORD); - if (StringUtils.isNotBlank(clientPassStr)) { - clientPass = StringUtils.trim(clientPassStr); - } - - String namesrvAddrStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_DEFIBUS_NAMESRV_ADDR); - Preconditions.checkState(StringUtils.isNotEmpty(namesrvAddrStr), String.format("%s error", ConfKeys.KEYS_PROXY_DEFIBUS_NAMESRV_ADDR)); - namesrvAddr = StringUtils.trim(namesrvAddrStr); - - String consumeThreadPoolMinStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_DEFIBUS_CONSUME_THREADPOOL_MIN); - if(StringUtils.isNotEmpty(consumeThreadPoolMinStr)){ - Preconditions.checkState(StringUtils.isNumeric(consumeThreadPoolMinStr), String.format("%s error", ConfKeys.KEYS_PROXY_DEFIBUS_CONSUME_THREADPOOL_MIN)); - consumeThreadMin = Integer.valueOf(consumeThreadPoolMinStr); - } - - String consumeThreadPoolMaxStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_DEFIBUS_CONSUME_THREADPOOL_MAX); - if(StringUtils.isNotEmpty(consumeThreadPoolMaxStr)){ - Preconditions.checkState(StringUtils.isNumeric(consumeThreadPoolMaxStr), String.format("%s error", ConfKeys.KEYS_PROXY_DEFIBUS_CONSUME_THREADPOOL_MAX)); - consumeThreadMax = Integer.valueOf(consumeThreadPoolMaxStr); - } - - String consumerThreadPoolQueueSizeStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_DEFIBUS_CONSUME_THREADPOOL_QUEUESIZE); - if(StringUtils.isNotEmpty(consumerThreadPoolQueueSizeStr)){ - Preconditions.checkState(StringUtils.isNumeric(consumerThreadPoolQueueSizeStr), String.format("%s error", ConfKeys.KEYS_PROXY_DEFIBUS_CONSUME_THREADPOOL_QUEUESIZE)); - consumeQueueSize = Integer.valueOf(consumerThreadPoolQueueSizeStr); - } - - String clientAckWindowStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_DEFIBUS_CLIENT_ACK_WINDOW); - if(StringUtils.isNotEmpty(clientAckWindowStr)){ - Preconditions.checkState(StringUtils.isNumeric(clientAckWindowStr), String.format("%s error", ConfKeys.KEYS_PROXY_DEFIBUS_CLIENT_ACK_WINDOW)); - ackWindow = Integer.valueOf(clientAckWindowStr); - } - - String clientPubWindowStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_DEFIBUS_CLIENT_PUB_WINDOW); - if(StringUtils.isNotEmpty(clientPubWindowStr)){ - Preconditions.checkState(StringUtils.isNumeric(clientPubWindowStr), String.format("%s error", ConfKeys.KEYS_PROXY_DEFIBUS_CLIENT_PUB_WINDOW)); - pubWindow = Integer.valueOf(clientPubWindowStr); - } - - String consumeTimeoutStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_DEFIBUS_CLIENT_CONSUME_TIMEOUT); - if(StringUtils.isNotBlank(consumeTimeoutStr)) { - Preconditions.checkState(StringUtils.isNumeric(consumeTimeoutStr), String.format("%s error", ConfKeys.KEYS_PROXY_DEFIBUS_CLIENT_CONSUME_TIMEOUT)); - consumeTimeout = Long.valueOf(consumeTimeoutStr); - } - - String clientPullBatchSizeStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_DEFIBUS_CLIENT_PULL_BATCHSIZE); - if(StringUtils.isNotEmpty(clientPullBatchSizeStr)){ - Preconditions.checkState(StringUtils.isNumeric(clientPullBatchSizeStr), String.format("%s error", ConfKeys.KEYS_PROXY_DEFIBUS_CLIENT_PULL_BATCHSIZE)); - pullBatchSize = Integer.valueOf(clientPullBatchSizeStr); - } - - String clientPollNamesrvIntervalStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_DEFIBUS_CLIENT_POLL_NAMESRV_INTERVAL); - if(StringUtils.isNotEmpty(clientPollNamesrvIntervalStr)){ - Preconditions.checkState(StringUtils.isNumeric(clientPollNamesrvIntervalStr), String.format("%s error", ConfKeys.KEYS_PROXY_DEFIBUS_CLIENT_POLL_NAMESRV_INTERVAL)); - pollNameServerInteval = Integer.valueOf(clientPollNamesrvIntervalStr); - } - - String clientHeartbeatBrokerIntervalStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_DEFIBUS_CLIENT_HEARTBEAT_BROKER_INTERVEL); - if(StringUtils.isNotEmpty(clientHeartbeatBrokerIntervalStr)){ - Preconditions.checkState(StringUtils.isNumeric(clientHeartbeatBrokerIntervalStr), String.format("%s error", ConfKeys.KEYS_PROXY_DEFIBUS_CLIENT_HEARTBEAT_BROKER_INTERVEL)); - heartbeatBrokerInterval = Integer.valueOf(clientHeartbeatBrokerIntervalStr); - } - - String clientRebalanceIntervalIntervalStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_DEFIBUS_CLIENT_REBALANCE_INTERVEL); - if(StringUtils.isNotEmpty(clientRebalanceIntervalIntervalStr)){ - Preconditions.checkState(StringUtils.isNumeric(clientRebalanceIntervalIntervalStr), String.format("%s error", ConfKeys.KEYS_PROXY_DEFIBUS_CLIENT_REBALANCE_INTERVEL)); - rebalanceInterval = Integer.valueOf(clientRebalanceIntervalIntervalStr); - } - - proxyServerIp = configurationWraper.getProp(ConfKeys.KEYS_PROXY_SERVER_HOST_IP); - if(StringUtils.isBlank(proxyServerIp)) { - proxyServerIp = ProxyUtil.getLocalAddr(); - } - } - - static class ConfKeys { - public static String KEYS_PROXY_ENV = "proxy.server.env"; - - public static String KEYS_PROXY_REGION = "proxy.server.region"; - - public static String KEYS_PROXY_IDC = "proxy.server.idc"; - - public static String KEYS_PROXY_DCN = "proxy.server.dcn"; - - public static String KEYS_PROXY_SYSID = "proxy.sysid"; - - public static String KEYS_PROXY_SERVER_CLUSTER = "proxy.server.cluster"; - - public static String KEYS_PROXY_SERVER_NAME = "proxy.server.name"; - - public static String KEYS_PROXY_DEFIBUS_NAMESRV_ADDR = "proxy.server.defibus.namesrvAddr"; - - public static String KEYS_PROXY_DEFIBUS_USERNAME = "proxy.server.defibus.username"; - - public static String KEYS_PROXY_DEFIBUS_PASSWORD = "proxy.server.defibus.password"; - - public static String KEYS_PROXY_DEFIBUS_CONSUME_THREADPOOL_MIN = "proxy.server.defibus.client.consumeThreadMin"; - - public static String KEYS_PROXY_DEFIBUS_CONSUME_THREADPOOL_MAX = "proxy.server.defibus.client.consumeThreadMax"; - - public static String KEYS_PROXY_DEFIBUS_CONSUME_THREADPOOL_QUEUESIZE = "proxy.server.defibus.client.consumeThreadPoolQueueSize"; - - public static String KEYS_PROXY_DEFIBUS_CLIENT_ACK_WINDOW = "proxy.server.defibus.client.ackwindow"; - - public static String KEYS_PROXY_DEFIBUS_CLIENT_PUB_WINDOW = "proxy.server.defibus.client.pubwindow"; - - public static String KEYS_PROXY_DEFIBUS_CLIENT_CONSUME_TIMEOUT = "proxy.server.defibus.client.comsumeTimeoutInMin"; - - public static String KEYS_PROXY_DEFIBUS_CLIENT_PULL_BATCHSIZE = "proxy.server.defibus.client.pullBatchSize"; - - public static String KEYS_PROXY_DEFIBUS_CLIENT_POLL_NAMESRV_INTERVAL = "proxy.server.defibus.client.pollNameServerInterval"; - - public static String KEYS_PROXY_DEFIBUS_CLIENT_HEARTBEAT_BROKER_INTERVEL = "proxy.server.defibus.client.heartbeatBrokerInterval"; - - public static String KEYS_PROXY_DEFIBUS_CLIENT_REBALANCE_INTERVEL = "proxy.server.defibus.client.rebalanceInterval"; - - public static String KEYS_PROXY_SERVER_HOST_IP = "proxy.server.hostIp"; - - public static String KEYS_PROXY_SERVER_REGISTER_INTERVAL = "proxy.server.registry.registerIntervalInMills"; - - public static String KEYS_PROXY_SERVER_FETCH_REGISTRY_ADDR_INTERVAL = "proxy.server.registry.fetchRegistryAddrIntervalInMills"; - } -} \ No newline at end of file diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/configuration/ProxyConfiguration.java b/eventmesh-emesher/src/main/java/com/webank/emesher/configuration/ProxyConfiguration.java deleted file mode 100644 index 261bf1f1c3..0000000000 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/configuration/ProxyConfiguration.java +++ /dev/null @@ -1,224 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.emesher.configuration; - -import com.google.common.base.Preconditions; -import com.google.common.util.concurrent.RateLimiter; -import org.apache.commons.lang3.StringUtils; - -public class ProxyConfiguration extends CommonConfiguration { - - public int httpServerPort = 10105; - - public RateLimiter proxyServerBatchMsgNumLimiter = RateLimiter.create(20000); - - public boolean proxyServerBatchMsgBatchEnabled = Boolean.TRUE; - - public int proxyServerBatchMsgThreadNum = 10; - - public int proxyServerSendMsgThreadNum = 8; - - public int proxyServerPushMsgThreadNum = 8; - - public int proxyServerReplyMsgThreadNum = 8; - - public int proxyServerClientManageThreadNum = 4; - - public int proxyServerRegistryThreadNum = 10; - - public int proxyServerAdminThreadNum = 2; - - public int proxyServerRetryThreadNum = 2; - - public int proxyServerPullRegistryIntervel = 30000; - - public int proxyServerAsyncAccumulationThreshold = 1000; - - public int proxyServerRetryBlockQSize = 10000; - - public int proxyServerBatchBlockQSize = 1000; - - public int proxyServerSendMsgBlockQSize = 1000; - - public int proxyServerPushMsgBlockQSize = 1000; - - public int proxyServerClientManageBlockQSize = 1000; - - public int proxyServerBusyCheckIntervel = 1000; - - public boolean proxyServerConsumerEnabled = false; - - public boolean proxyServerUseTls = false; - - public ProxyConfiguration(ConfigurationWraper configurationWraper){ - super(configurationWraper); - } - - public void init(){ - super.init(); - - String httpServerPortStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_SERVER_HTTP_PORT); - Preconditions.checkState(StringUtils.isNotEmpty(httpServerPortStr) && StringUtils.isNumeric(httpServerPortStr), String.format("%s error", ConfKeys.KEYS_PROXY_SERVER_HTTP_PORT)); - httpServerPort = Integer.valueOf(StringUtils.deleteWhitespace(httpServerPortStr)); - - String proxyServerBatchMsgThreadNumStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_BATCHMSG_THREAD_NUM); - if (StringUtils.isNotEmpty(proxyServerBatchMsgThreadNumStr) && StringUtils.isNumeric(proxyServerBatchMsgThreadNumStr)) { - proxyServerBatchMsgThreadNum = Integer.valueOf(StringUtils.deleteWhitespace(proxyServerBatchMsgThreadNumStr)); - } - - String proxyServerBatchMsgNumLimiterStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_BATCHMSG_RATELIMITER); - if (StringUtils.isNotEmpty(proxyServerBatchMsgNumLimiterStr) && StringUtils.isNumeric(proxyServerBatchMsgNumLimiterStr)) { - proxyServerBatchMsgNumLimiter = RateLimiter.create(Double.valueOf(StringUtils.deleteWhitespace(proxyServerBatchMsgNumLimiterStr))); - } - - String proxyServerBatchMsgBatchEnableStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_BATCHMSG_BATCH_ENABLED); - if (StringUtils.isNotBlank(proxyServerBatchMsgBatchEnableStr)) { - proxyServerBatchMsgBatchEnabled = Boolean.valueOf(proxyServerBatchMsgBatchEnableStr); - } - - String proxyServerAsyncAccumulationThresholdStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_ASYNC_ACCUMULATION_THRESHOLD); - if (StringUtils.isNotEmpty(proxyServerAsyncAccumulationThresholdStr) && StringUtils.isNumeric(proxyServerAsyncAccumulationThresholdStr)) { - proxyServerAsyncAccumulationThreshold = Integer.valueOf(StringUtils.deleteWhitespace(proxyServerAsyncAccumulationThresholdStr)); - } - - String proxyServerSendMsgThreadNumStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_SENDMSG_THREAD_NUM); - if (StringUtils.isNotEmpty(proxyServerSendMsgThreadNumStr) && StringUtils.isNumeric(proxyServerSendMsgThreadNumStr)) { - proxyServerSendMsgThreadNum = Integer.valueOf(StringUtils.deleteWhitespace(proxyServerSendMsgThreadNumStr)); - } - - String proxyServerReplyMsgThreadNumStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_REPLYMSG_THREAD_NUM); - if (StringUtils.isNotEmpty(proxyServerReplyMsgThreadNumStr) && StringUtils.isNumeric(proxyServerReplyMsgThreadNumStr)) { - proxyServerReplyMsgThreadNum = Integer.valueOf(StringUtils.deleteWhitespace(proxyServerReplyMsgThreadNumStr)); - } - - String proxyServerPushMsgThreadNumStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_PUSHMSG_THREAD_NUM); - if (StringUtils.isNotEmpty(proxyServerPushMsgThreadNumStr) && StringUtils.isNumeric(proxyServerPushMsgThreadNumStr)) { - proxyServerPushMsgThreadNum = Integer.valueOf(StringUtils.deleteWhitespace(proxyServerPushMsgThreadNumStr)); - } - - String proxyServerRegistryThreadNumStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_REGISTRY_THREAD_NUM); - if (StringUtils.isNotEmpty(proxyServerRegistryThreadNumStr) && StringUtils.isNumeric(proxyServerRegistryThreadNumStr)) { - proxyServerRegistryThreadNum = Integer.valueOf(StringUtils.deleteWhitespace(proxyServerRegistryThreadNumStr)); - } - - String proxyServerClientManageThreadNumStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_CLIENTMANAGE_THREAD_NUM); - if (StringUtils.isNotEmpty(proxyServerClientManageThreadNumStr) && StringUtils.isNumeric(proxyServerClientManageThreadNumStr)) { - proxyServerClientManageThreadNum = Integer.valueOf(StringUtils.deleteWhitespace(proxyServerClientManageThreadNumStr)); - } - - String proxyServerPullRegistryIntervelStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_PULL_REGISTRY_INTERVEL); - if (StringUtils.isNotEmpty(proxyServerPullRegistryIntervelStr) && StringUtils.isNumeric(proxyServerPullRegistryIntervelStr)) { - proxyServerPullRegistryIntervel = Integer.valueOf(StringUtils.deleteWhitespace(proxyServerPullRegistryIntervelStr)); - } - - String proxyServerAdminThreadNumStr = configurationWraper.getProp(ConfKeys.KEYS_PROXY_ADMIN_THREAD_NUM); - if (StringUtils.isNotEmpty(proxyServerAdminThreadNumStr) && StringUtils.isNumeric(proxyServerAdminThreadNumStr)) { - proxyServerAdminThreadNum = Integer.valueOf(StringUtils.deleteWhitespace(proxyServerAdminThreadNumStr)); - } - - String proxyServerRetryBlockQSizeStr = configurationWraper.getProp(ConfKeys.KEY_PROXY_RETRY_BLOCKQ_SIZE); - if (StringUtils.isNotEmpty(proxyServerRetryBlockQSizeStr) && StringUtils.isNumeric(proxyServerRetryBlockQSizeStr)) { - proxyServerRetryBlockQSize = Integer.valueOf(StringUtils.deleteWhitespace(proxyServerRetryBlockQSizeStr)); - } - - String proxyServerBatchBlockQSizeStr = configurationWraper.getProp(ConfKeys.KEY_PROXY_BATCHMSG_BLOCKQ_SIZE); - if (StringUtils.isNotEmpty(proxyServerBatchBlockQSizeStr) && StringUtils.isNumeric(proxyServerBatchBlockQSizeStr)) { - proxyServerBatchBlockQSize = Integer.valueOf(StringUtils.deleteWhitespace(proxyServerBatchBlockQSizeStr)); - } - - String proxyServerSendMsgBlockQSizeStr = configurationWraper.getProp(ConfKeys.KEY_PROXY_SENDMSG_BLOCKQ_SIZE); - if (StringUtils.isNotEmpty(proxyServerSendMsgBlockQSizeStr) && StringUtils.isNumeric(proxyServerSendMsgBlockQSizeStr)) { - proxyServerSendMsgBlockQSize = Integer.valueOf(StringUtils.deleteWhitespace(proxyServerSendMsgBlockQSizeStr)); - } - - String proxyServerPushMsgBlockQSizeStr = configurationWraper.getProp(ConfKeys.KEY_PROXY_PUSHMSG_BLOCKQ_SIZE); - if (StringUtils.isNotEmpty(proxyServerPushMsgBlockQSizeStr) && StringUtils.isNumeric(proxyServerPushMsgBlockQSizeStr)) { - proxyServerPushMsgBlockQSize = Integer.valueOf(StringUtils.deleteWhitespace(proxyServerPushMsgBlockQSizeStr)); - } - - String proxyServerClientManageBlockQSizeStr = configurationWraper.getProp(ConfKeys.KEY_PROXY_CLIENTM_BLOCKQ_SIZE); - if (StringUtils.isNotEmpty(proxyServerClientManageBlockQSizeStr) && StringUtils.isNumeric(proxyServerClientManageBlockQSizeStr)) { - proxyServerClientManageBlockQSize = Integer.valueOf(StringUtils.deleteWhitespace(proxyServerClientManageBlockQSizeStr)); - } - - String proxyServerBusyCheckIntervelStr = configurationWraper.getProp(ConfKeys.KEY_PROXY_BUSY_CHECK_INTERVEL); - if (StringUtils.isNotEmpty(proxyServerBusyCheckIntervelStr) && StringUtils.isNumeric(proxyServerBusyCheckIntervelStr)) { - proxyServerBusyCheckIntervel = Integer.valueOf(StringUtils.deleteWhitespace(proxyServerBusyCheckIntervelStr)); - } - - String proxyServerConsumerEnabledStr = configurationWraper.getProp(ConfKeys.KEY_PROXY_CONSUMER_ENABLED); - if (StringUtils.isNotEmpty(proxyServerConsumerEnabledStr)) { - proxyServerConsumerEnabled = Boolean.valueOf(StringUtils.deleteWhitespace(proxyServerConsumerEnabledStr)); - } - - String proxyServerRetryThreadNumStr = configurationWraper.getProp(ConfKeys.KEY_PROXY_RETRY_THREAD_NUM); - if (StringUtils.isNotEmpty(proxyServerRetryThreadNumStr) && StringUtils.isNumeric(proxyServerRetryThreadNumStr)) { - proxyServerRetryThreadNum = Integer.valueOf(StringUtils.deleteWhitespace(proxyServerRetryThreadNumStr)); - } - - String proxyServerUseTlsStr = configurationWraper.getProp(ConfKeys.KEY_PROXY_HTTPS_ENABLED); - if (StringUtils.isNotEmpty(proxyServerUseTlsStr)) { - proxyServerUseTls = Boolean.valueOf(StringUtils.deleteWhitespace(proxyServerUseTlsStr)); - } - } - - static class ConfKeys{ - - public static String KEYS_PROXY_SERVER_HTTP_PORT = "proxy.server.http.port"; - - public static String KEYS_PROXY_BATCHMSG_THREAD_NUM = "proxy.server.batchmsg.threads.num"; - - public static String KEYS_PROXY_BATCHMSG_RATELIMITER = "proxy.server.batchmsg.speed.ratelimiter"; - - public static String KEYS_PROXY_BATCHMSG_BATCH_ENABLED = "proxy.server.batchmsg.batch.enabled"; - - public static String KEYS_PROXY_ASYNC_ACCUMULATION_THRESHOLD = "proxy.server.async.accumulation.threshold"; - - public static String KEY_PROXY_BUSY_CHECK_INTERVEL = "proxy.server.busy.check.intervel"; - - public static String KEYS_PROXY_SENDMSG_THREAD_NUM = "proxy.server.sendmsg.threads.num"; - - public static String KEYS_PROXY_REPLYMSG_THREAD_NUM = "proxy.server.replymsg.threads.num"; - - public static String KEYS_PROXY_PUSHMSG_THREAD_NUM = "proxy.server.pushmsg.threads.num"; - - public static String KEYS_PROXY_REGISTRY_THREAD_NUM = "proxy.server.registry.threads.num"; - - public static String KEYS_PROXY_CLIENTMANAGE_THREAD_NUM = "proxy.server.clientmanage.threads.num"; - - public static String KEYS_PROXY_ADMIN_THREAD_NUM = "proxy.server.admin.threads.num"; - - public static String KEY_PROXY_RETRY_THREAD_NUM = "proxy.server.retry.threads.num"; - - public static String KEYS_PROXY_PULL_REGISTRY_INTERVEL = "proxy.server.pull.registry.intervel"; - - public static String KEY_PROXY_RETRY_BLOCKQ_SIZE = "proxy.server.retry.blockQ.size"; - - public static String KEY_PROXY_BATCHMSG_BLOCKQ_SIZE = "proxy.server.batchmsg.blockQ.size"; - - public static String KEY_PROXY_SENDMSG_BLOCKQ_SIZE = "proxy.server.sendmsg.blockQ.size"; - - public static String KEY_PROXY_PUSHMSG_BLOCKQ_SIZE = "proxy.server.pushmsg.blockQ.size"; - - public static String KEY_PROXY_CLIENTM_BLOCKQ_SIZE = "proxy.server.clientM.blockQ.size"; - - public static String KEY_PROXY_CONSUMER_ENABLED = "proxy.server.consumer.enabled"; - - public static String KEY_PROXY_HTTPS_ENABLED = "proxy.server.useTls.enabled"; - } -} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/consumergroup/event/ConsumerGroupInstanceChangeEvent.java b/eventmesh-emesher/src/main/java/com/webank/emesher/core/consumergroup/event/ConsumerGroupInstanceChangeEvent.java deleted file mode 100644 index 2c5b1ac18a..0000000000 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/consumergroup/event/ConsumerGroupInstanceChangeEvent.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.emesher.core.consumergroup.event; - -public class ConsumerGroupInstanceChangeEvent { -} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/plugin/MQConsumerWrapper.java b/eventmesh-emesher/src/main/java/com/webank/emesher/core/plugin/MQConsumerWrapper.java deleted file mode 100644 index c65427f9ba..0000000000 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/plugin/MQConsumerWrapper.java +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.emesher.core.plugin; - -import com.webank.emesher.configuration.CommonConfiguration; -import com.webank.emesher.core.plugin.impl.DeFiMeshMQConsumerImpl; -import com.webank.emesher.core.plugin.impl.RMQMeshMQConsumerImpl; -import com.webank.emesher.patch.ProxyConsumeConcurrentlyContext; -import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently; -import org.apache.rocketmq.common.message.MessageExt; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; - -public class MQConsumerWrapper extends MQWrapper { - - public Logger logger = LoggerFactory.getLogger(this.getClass()); - - protected RMQMeshMQConsumerImpl rmqConsumerImpl; - - protected DeFiMeshMQConsumerImpl deFiConsumerImpl; - - public void setInstanceName(String instanceName) { - if (CURRENT_EVENT_STORE.equals(EVENT_STORE_ROCKETMQ)) { - rmqConsumerImpl.setInstanceName(instanceName); - return; - } - - deFiConsumerImpl.setInstanceName(instanceName); - } - - public void subscribe(String topic) throws Exception { - if (CURRENT_EVENT_STORE.equals(EVENT_STORE_ROCKETMQ)) { - rmqConsumerImpl.subscribe(topic); - return; - } - deFiConsumerImpl.subscribe(topic); - } - - public void unsubscribe(String topic) throws Exception { - if (CURRENT_EVENT_STORE.equals(EVENT_STORE_ROCKETMQ)) { - rmqConsumerImpl.unsubscribe(topic); - return; - } - deFiConsumerImpl.unsubscribe(topic); - } - - public boolean isPause() { - if (CURRENT_EVENT_STORE.equals(EVENT_STORE_ROCKETMQ)) { - return rmqConsumerImpl.isPause(); - } - - return deFiConsumerImpl.isPause(); - } - - public void pause() { - if (CURRENT_EVENT_STORE.equals(EVENT_STORE_ROCKETMQ)) { - rmqConsumerImpl.pause(); - return; - } - - deFiConsumerImpl.pause(); - } - - public synchronized void init(boolean isBroadcast, CommonConfiguration commonConfiguration, - String consumerGroup) throws Exception { - if (CURRENT_EVENT_STORE.equals(EVENT_STORE_ROCKETMQ)) { - rmqConsumerImpl = new RMQMeshMQConsumerImpl(); - rmqConsumerImpl.init(isBroadcast, commonConfiguration, consumerGroup); - inited.compareAndSet(false, true); - return; - } - - deFiConsumerImpl = new DeFiMeshMQConsumerImpl(); - deFiConsumerImpl.init(isBroadcast, commonConfiguration, consumerGroup); - inited.compareAndSet(false, true); - } - - public synchronized void start() throws Exception { - if (CURRENT_EVENT_STORE.equals(EVENT_STORE_ROCKETMQ)) { - rmqConsumerImpl.start(); - started.compareAndSet(false, true); - return; - } - - deFiConsumerImpl.start(); - started.compareAndSet(false, true); - return; - } - - public synchronized void shutdown() throws Exception { - if (CURRENT_EVENT_STORE.equals(EVENT_STORE_ROCKETMQ)) { - rmqConsumerImpl.shutdown(); - inited.compareAndSet(false, true); - started.compareAndSet(false, true); - return; - } - - deFiConsumerImpl.shutdown(); - inited.compareAndSet(false, true); - started.compareAndSet(false, true); - } - - public void registerMessageListener(MessageListenerConcurrently messageListenerConcurrently) { - if (CURRENT_EVENT_STORE.equals(EVENT_STORE_ROCKETMQ)) { - rmqConsumerImpl.registerMessageListener(messageListenerConcurrently); - return; - } - deFiConsumerImpl.registerMessageListener(messageListenerConcurrently); - } - - public void updateOffset(List msgs, ProxyConsumeConcurrentlyContext proxyConsumeConcurrentlyContext) { - if (CURRENT_EVENT_STORE.equals(EVENT_STORE_ROCKETMQ)) { - rmqConsumerImpl.updateOffset(msgs, proxyConsumeConcurrentlyContext); - return; - } - - deFiConsumerImpl.updateOffset(msgs, proxyConsumeConcurrentlyContext); - return; - } -} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/plugin/MQProducerWrapper.java b/eventmesh-emesher/src/main/java/com/webank/emesher/core/plugin/MQProducerWrapper.java deleted file mode 100644 index ab519e221e..0000000000 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/plugin/MQProducerWrapper.java +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.emesher.core.plugin; - -import com.webank.defibus.client.common.DeFiBusClientConfig; -import com.webank.defibus.client.impl.producer.RRCallback; -import com.webank.defibus.producer.DeFiBusProducer; -import com.webank.emesher.configuration.CommonConfiguration; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.eventmesh.common.ThreadUtil; -import org.apache.rocketmq.client.exception.MQBrokerException; -import org.apache.rocketmq.client.exception.MQClientException; -import org.apache.rocketmq.client.producer.DefaultMQProducer; -import org.apache.rocketmq.client.producer.SendCallback; -import org.apache.rocketmq.common.message.Message; -import org.apache.rocketmq.common.message.MessageClientIDSetter; -import org.apache.rocketmq.remoting.exception.RemotingException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class MQProducerWrapper extends MQWrapper { - - public Logger logger = LoggerFactory.getLogger(this.getClass()); - - protected DeFiBusProducer defibusProducer; - - protected DefaultMQProducer defaultMQProducer; - - public synchronized void init(CommonConfiguration commonConfiguration, String producerGroup) { - if (inited.get()) { - return; - } - - if (CURRENT_EVENT_STORE.equals(EVENT_STORE_ROCKETMQ)) { - defaultMQProducer = new DefaultMQProducer(ProxyConstants.PRODUCER_GROUP_NAME_PREFIX + producerGroup); - defaultMQProducer.setHeartbeatBrokerInterval(commonConfiguration.heartbeatBrokerInterval); - defaultMQProducer.setPollNameServerInterval(commonConfiguration.pollNameServerInteval); - defaultMQProducer.setNamesrvAddr(commonConfiguration.namesrvAddr); - MessageClientIDSetter.createUniqID(); - defaultMQProducer.setVipChannelEnabled(false); - defaultMQProducer.setCompressMsgBodyOverHowmuch(2 * 1024); - } else { - DeFiBusClientConfig wcc = new DeFiBusClientConfig(); - wcc.setClusterPrefix(commonConfiguration.proxyIDC); - wcc.setPollNameServerInterval(commonConfiguration.pollNameServerInteval); - wcc.setHeartbeatBrokerInterval(commonConfiguration.heartbeatBrokerInterval); - wcc.setProducerGroup(ProxyConstants.PRODUCER_GROUP_NAME_PREFIX + producerGroup); - wcc.setNamesrvAddr(commonConfiguration.namesrvAddr); - MessageClientIDSetter.createUniqID(); - defibusProducer = new DeFiBusProducer(wcc); - defibusProducer.getDefaultMQProducer().setVipChannelEnabled(false); - defibusProducer.getDefaultMQProducer().setCompressMsgBodyOverHowmuch(2 * 1024); - } - - inited.compareAndSet(false, true); - } - - public synchronized void start() throws Exception { - if (started.get()) { - return; - } - - if (CURRENT_EVENT_STORE.equals(EVENT_STORE_ROCKETMQ)) { - defaultMQProducer.start(); - ThreadUtil.randomSleep(500); - defaultMQProducer.getDefaultMQProducerImpl().getmQClientFactory().updateTopicRouteInfoFromNameServer(); - } else { - defibusProducer.start(); - ThreadUtil.randomSleep(500); - defibusProducer.getDefaultMQProducer().getDefaultMQProducerImpl().getmQClientFactory().updateTopicRouteInfoFromNameServer(); - } - - started.compareAndSet(false, true); - } - - public synchronized void shutdown() throws Exception { - if (!inited.get()) { - return; - } - - if (!started.get()) { - return; - } - - if (CURRENT_EVENT_STORE.equals(EVENT_STORE_ROCKETMQ)) { - defaultMQProducer.shutdown(); - } else { - defibusProducer.shutdown(); - } - - inited.compareAndSet(true, false); - started.compareAndSet(true, false); - } - - public void send(Message message, SendCallback sendCallback) throws Exception { - if (CURRENT_EVENT_STORE.equals(EVENT_STORE_ROCKETMQ)) { - defaultMQProducer.send(message, sendCallback); - return; - } - defibusProducer.publish(message, sendCallback); - } - - public void request(Message message, SendCallback sendCallback, RRCallback rrCallback, long timeout) - throws InterruptedException, RemotingException, MQClientException, MQBrokerException { - if (CURRENT_EVENT_STORE.equals(EVENT_STORE_ROCKETMQ)) { - throw new UnsupportedOperationException("not support request-reply mode when eventstore=rocketmq"); - //1.1.0 not support rr -// defaultMQProducer.request(message, new RequestCallback() { -// @Override -// public void onSuccess(Message message) { -// rrCallback.onSuccess(message); -// } -// -// @Override -// public void onException(Throwable e) { -// rrCallback.onException(e); -// } -// }, timeout); -// return; - } - defibusProducer.request(message, sendCallback, rrCallback, timeout); - } - - public Message request(Message message, long timeout) throws InterruptedException, RemotingException, MQClientException, MQBrokerException { - if (CURRENT_EVENT_STORE.equals(EVENT_STORE_ROCKETMQ)) { - //1.1.0 not support rr -// return defaultMQProducer.request(message, timeout); - throw new UnsupportedOperationException("not support request-reply mode when eventstore=rocketmq"); - } - - return defibusProducer.request(message, timeout); - } - - public boolean reply(final Message message, final SendCallback sendCallback) throws Exception { - if (CURRENT_EVENT_STORE.equals(EVENT_STORE_ROCKETMQ)) { -// defaultMQProducer.send(message, sendCallback); -// return true; - throw new UnsupportedOperationException("not support request-reply mode when eventstore=rocketmq"); - } - defibusProducer.reply(message, sendCallback); - return true; - } - - public DefaultMQProducer getDefaultMQProducer() { - if (CURRENT_EVENT_STORE.equals(EVENT_STORE_ROCKETMQ)) { - return defaultMQProducer; - } - - return defibusProducer.getDefaultMQProducer(); - } -} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/plugin/MQWrapper.java b/eventmesh-emesher/src/main/java/com/webank/emesher/core/plugin/MQWrapper.java deleted file mode 100644 index f46b0a9a94..0000000000 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/plugin/MQWrapper.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.webank.emesher.core.plugin; - -import com.webank.emesher.constants.ProxyConstants; -import org.apache.commons.lang3.StringUtils; - -import java.util.concurrent.atomic.AtomicBoolean; - -public abstract class MQWrapper { - - public static final String EVENT_STORE_ROCKETMQ = "rocketmq"; - - public static final String EVENT_STORE_DEFIBUS = "defibus"; - - public static String CURRENT_EVENT_STORE = EVENT_STORE_DEFIBUS; - - public static final String EVENT_STORE_CONF = System.getProperty(ProxyConstants.EVENT_STORE_PROPERTIES, System.getenv(ProxyConstants.EVENT_STORE_ENV)); - - static { - if (StringUtils.isNotBlank(EVENT_STORE_CONF)) { - CURRENT_EVENT_STORE = EVENT_STORE_CONF; - } - } - - public AtomicBoolean started = new AtomicBoolean(Boolean.FALSE); - - public AtomicBoolean inited = new AtomicBoolean(Boolean.FALSE); - -} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/plugin/impl/DeFiMeshMQConsumerImpl.java b/eventmesh-emesher/src/main/java/com/webank/emesher/core/plugin/impl/DeFiMeshMQConsumerImpl.java deleted file mode 100644 index 1fa130cd4d..0000000000 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/plugin/impl/DeFiMeshMQConsumerImpl.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.emesher.core.plugin.impl; - -import com.webank.defibus.client.common.DeFiBusClientConfig; -import com.webank.defibus.consumer.DeFiBusPushConsumer; -import com.webank.emesher.configuration.CommonConfiguration; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.patch.ProxyConsumeConcurrentlyContext; -import com.webank.eventmesh.common.ThreadUtil; -import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently; -import org.apache.rocketmq.client.impl.consumer.ConsumeMessageConcurrentlyService; -import org.apache.rocketmq.client.impl.consumer.ConsumeMessageService; -import org.apache.rocketmq.common.MixAll; -import org.apache.rocketmq.common.consumer.ConsumeFromWhere; -import org.apache.rocketmq.common.message.MessageExt; -import org.apache.rocketmq.common.protocol.heartbeat.MessageModel; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; - -public class DeFiMeshMQConsumerImpl implements MeshMQConsumer { - - public Logger logger = LoggerFactory.getLogger(this.getClass()); - - public Logger messageLogger = LoggerFactory.getLogger("message"); - - private DeFiBusPushConsumer deFiBusPushConsumer; - - public synchronized void init(boolean isBroadcast, CommonConfiguration commonConfiguration, - String consumerGroup) throws Exception { - DeFiBusClientConfig wcc = new DeFiBusClientConfig(); - wcc.setPollNameServerInterval(commonConfiguration.pollNameServerInteval); - wcc.setHeartbeatBrokerInterval(commonConfiguration.heartbeatBrokerInterval); - wcc.setAckWindowSize(commonConfiguration.ackWindow); - wcc.setThreadPoolCoreSize(commonConfiguration.consumeThreadMin); - wcc.setThreadPoolMaxSize(commonConfiguration.consumeThreadMax); - wcc.setConsumeTimeout(commonConfiguration.consumeTimeout); - wcc.setPubWindowSize(commonConfiguration.pubWindow); - wcc.setPullBatchSize(commonConfiguration.pullBatchSize); - wcc.setClusterPrefix(commonConfiguration.proxyIDC); - if (isBroadcast) { - wcc.setConsumerGroup(ProxyConstants.CONSUMER_GROUP_NAME_PREFIX + ProxyConstants.BROADCAST_PREFIX + consumerGroup); - } else { - wcc.setConsumerGroup(ProxyConstants.CONSUMER_GROUP_NAME_PREFIX + consumerGroup); - } - wcc.setNamesrvAddr(commonConfiguration.namesrvAddr); - deFiBusPushConsumer = new DeFiBusPushConsumer(wcc); - deFiBusPushConsumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET); - if (isBroadcast) { - deFiBusPushConsumer.getDefaultMQPushConsumer().setMessageModel(MessageModel.BROADCASTING); - } else { - deFiBusPushConsumer.getDefaultMQPushConsumer().setMessageModel(MessageModel.CLUSTERING); - } - } - - public void registerMessageListener(MessageListenerConcurrently messageListenerConcurrently) { - deFiBusPushConsumer.registerMessageListener(messageListenerConcurrently); - } - - public synchronized void start() throws Exception { - ThreadUtil.randomSleep(50); - if (deFiBusPushConsumer.getDefaultMQPushConsumer().getMessageListener() == null) { - throw new Exception("no messageListener has been registered"); - } - - deFiBusPushConsumer.start(); - deFiBusPushConsumer.getDefaultMQPushConsumer().unsubscribe(MixAll.getRetryTopic(deFiBusPushConsumer.getDefaultMQPushConsumer().getConsumerGroup())); - } - - public void subscribe(String topic) throws Exception { - deFiBusPushConsumer.subscribe(topic); - } - - public void unsubscribe(String topic) throws Exception { - deFiBusPushConsumer.unsubscribe(topic); - } - - public boolean isPause() { - return deFiBusPushConsumer.getDefaultMQPushConsumer().getDefaultMQPushConsumerImpl().isPause(); - } - - public void pause() { - deFiBusPushConsumer.getDefaultMQPushConsumer().getDefaultMQPushConsumerImpl().setPause(true); - } - - public synchronized void shutdown() throws Exception { - deFiBusPushConsumer.shutdown(); - } - - public void setInstanceName(String instanceName) { - deFiBusPushConsumer.getDefaultMQPushConsumer().setInstanceName(instanceName); - } - - public void updateOffset(List msgs, ProxyConsumeConcurrentlyContext context) { - ConsumeMessageService consumeMessageService = deFiBusPushConsumer.getDefaultMQPushConsumer().getDefaultMQPushConsumerImpl().getConsumeMessageService(); - ((ConsumeMessageConcurrentlyService) consumeMessageService).updateOffset(msgs, context); - } -} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/plugin/impl/MeshMQConsumer.java b/eventmesh-emesher/src/main/java/com/webank/emesher/core/plugin/impl/MeshMQConsumer.java deleted file mode 100644 index 3ac18f66da..0000000000 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/plugin/impl/MeshMQConsumer.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.emesher.core.plugin.impl; - -import com.webank.emesher.configuration.CommonConfiguration; -import com.webank.emesher.patch.ProxyConsumeConcurrentlyContext; -import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently; -import org.apache.rocketmq.common.message.MessageExt; - -import java.util.List; - -public interface MeshMQConsumer { - - void start() throws Exception; - - void updateOffset(List msgs, ProxyConsumeConcurrentlyContext context); - - void init(boolean isBroadcast, CommonConfiguration commonConfiguration, - String consumerGroup) throws Exception; - - void registerMessageListener(MessageListenerConcurrently messageListenerConcurrently); - - void subscribe(String topic) throws Exception; - - void unsubscribe(String topic) throws Exception; - - boolean isPause(); - - void pause(); - - void shutdown() throws Exception; - - void setInstanceName(String instanceName); -} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/plugin/impl/RMQMeshMQConsumerImpl.java b/eventmesh-emesher/src/main/java/com/webank/emesher/core/plugin/impl/RMQMeshMQConsumerImpl.java deleted file mode 100644 index 95fd408a19..0000000000 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/plugin/impl/RMQMeshMQConsumerImpl.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.emesher.core.plugin.impl; - -import com.webank.emesher.configuration.CommonConfiguration; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.patch.ProxyConsumeConcurrentlyContext; -import com.webank.eventmesh.common.ThreadUtil; -import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer; -import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently; -import org.apache.rocketmq.client.impl.consumer.ConsumeMessageConcurrentlyService; -import org.apache.rocketmq.client.impl.consumer.ConsumeMessageService; -import org.apache.rocketmq.common.MixAll; -import org.apache.rocketmq.common.consumer.ConsumeFromWhere; -import org.apache.rocketmq.common.message.MessageExt; -import org.apache.rocketmq.common.protocol.heartbeat.MessageModel; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; - -public class RMQMeshMQConsumerImpl implements MeshMQConsumer { - - public Logger logger = LoggerFactory.getLogger(this.getClass()); - - public Logger messageLogger = LoggerFactory.getLogger("message"); - - private DefaultMQPushConsumer defaultMQPushConsumer; - - public synchronized void init(boolean isBroadcast, CommonConfiguration commonConfiguration, - String consumerGroup) throws Exception { - if (isBroadcast) { - defaultMQPushConsumer = new DefaultMQPushConsumer(ProxyConstants.CONSUMER_GROUP_NAME_PREFIX + ProxyConstants.BROADCAST_PREFIX + consumerGroup); - defaultMQPushConsumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET); - defaultMQPushConsumer.setMessageModel(MessageModel.BROADCASTING); - } else { - defaultMQPushConsumer = new DefaultMQPushConsumer(ProxyConstants.CONSUMER_GROUP_NAME_PREFIX + consumerGroup); - defaultMQPushConsumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET); - defaultMQPushConsumer.setMessageModel(MessageModel.CLUSTERING); - } - - defaultMQPushConsumer.setPollNameServerInterval(commonConfiguration.pollNameServerInteval); - defaultMQPushConsumer.setHeartbeatBrokerInterval(commonConfiguration.heartbeatBrokerInterval); - defaultMQPushConsumer.setPullThresholdForQueue(commonConfiguration.ackWindow); - defaultMQPushConsumer.setNamesrvAddr(commonConfiguration.namesrvAddr); - defaultMQPushConsumer.setPullBatchSize(commonConfiguration.pullBatchSize); - defaultMQPushConsumer.setConsumeThreadMax(commonConfiguration.consumeThreadMax); - defaultMQPushConsumer.setConsumeThreadMin(commonConfiguration.consumeThreadMin); - defaultMQPushConsumer.setConsumeTimeout(commonConfiguration.consumeTimeout); - } - - public void setInstanceName(String instanceName) { - defaultMQPushConsumer.setInstanceName(instanceName); - } - - public void registerMessageListener(MessageListenerConcurrently listener) { - this.defaultMQPushConsumer.registerMessageListener(listener); - } - - public synchronized void start() throws Exception { - ThreadUtil.randomSleep(50); - - if (this.defaultMQPushConsumer.getMessageListener() == null) { - throw new Exception("no messageListener has been registered"); - } - - defaultMQPushConsumer.start(); - defaultMQPushConsumer.unsubscribe(MixAll.getRetryTopic(defaultMQPushConsumer.getConsumerGroup())); - } - - public void subscribe(String topic) throws Exception { - defaultMQPushConsumer.subscribe(topic, "*"); - } - - public void unsubscribe(String topic) throws Exception { - defaultMQPushConsumer.unsubscribe(topic); - } - - public boolean isPause() { - return defaultMQPushConsumer.getDefaultMQPushConsumerImpl().isPause(); - } - - public void pause() { - defaultMQPushConsumer.getDefaultMQPushConsumerImpl().setPause(true); - } - - public synchronized void shutdown() throws Exception { - defaultMQPushConsumer.shutdown(); - } - - public void updateOffset(List msgs, ProxyConsumeConcurrentlyContext context) { - ConsumeMessageService consumeMessageService = defaultMQPushConsumer.getDefaultMQPushConsumerImpl().getConsumeMessageService(); - ((ConsumeMessageConcurrentlyService) consumeMessageService).updateOffset(msgs, context); - } -} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/consumer/ConsumerGroupManager.java b/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/consumer/ConsumerGroupManager.java deleted file mode 100644 index b2c49552ff..0000000000 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/consumer/ConsumerGroupManager.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.emesher.core.protocol.http.consumer; - -import com.webank.emesher.boot.ProxyHTTPServer; -import com.webank.emesher.core.consumergroup.ConsumerGroupConf; - -import java.util.concurrent.atomic.AtomicBoolean; - -public class ConsumerGroupManager { - - protected AtomicBoolean started = new AtomicBoolean(Boolean.FALSE); - - protected AtomicBoolean inited = new AtomicBoolean(Boolean.FALSE); - - private ProxyHTTPServer proxyHTTPServer; - - private ProxyConsumer proxyConsumer; - - private ConsumerGroupConf consumerGroupConfig; - - public ConsumerGroupManager(ProxyHTTPServer proxyHTTPServer, ConsumerGroupConf consumerGroupConfig) { - this.proxyHTTPServer = proxyHTTPServer; - this.consumerGroupConfig = consumerGroupConfig; - proxyConsumer = new ProxyConsumer(this.proxyHTTPServer, this.consumerGroupConfig); - } - - public synchronized void init() throws Exception { - proxyConsumer.init(); - inited.compareAndSet(false, true); - } - - public synchronized void start() throws Exception { - steupProxyConsumer(consumerGroupConfig); - proxyConsumer.start(); - started.compareAndSet(false, true); - } - - private synchronized void steupProxyConsumer(ConsumerGroupConf consumerGroupConfig) throws Exception { - for(String topic:consumerGroupConfig.getConsumerGroupTopicConf().keySet()) { - proxyConsumer.subscribe(topic); - } - } - - public synchronized void shutdown() throws Exception { - proxyConsumer.shutdown(); - started.compareAndSet(true, false); - } - - public synchronized void refresh(ConsumerGroupConf consumerGroupConfig) throws Exception { - - if(consumerGroupConfig == null || this.consumerGroupConfig.equals(consumerGroupConfig)) { - return; - } - - if(started.get()) { - shutdown(); - } - - this.consumerGroupConfig = consumerGroupConfig; - init(); - start(); - } - - public ConsumerGroupConf getConsumerGroupConfig() { - return consumerGroupConfig; - } -} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/consumer/ConsumerManager.java b/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/consumer/ConsumerManager.java deleted file mode 100644 index 7ad9e74e70..0000000000 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/consumer/ConsumerManager.java +++ /dev/null @@ -1,165 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.emesher.core.protocol.http.consumer; - -import com.webank.emesher.boot.ProxyHTTPServer; -import com.webank.emesher.core.consumergroup.ConsumerGroupConf; -import com.webank.emesher.core.consumergroup.event.ConsumerGroupStateEvent; -import com.webank.emesher.core.consumergroup.event.ConsumerGroupTopicConfChangeEvent; -import com.google.common.eventbus.Subscribe; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Objects; -import java.util.concurrent.ConcurrentHashMap; - -public class ConsumerManager { - - private ProxyHTTPServer proxyHTTPServer; - - private ConcurrentHashMap consumerTable = new ConcurrentHashMap(); - - public Logger logger = LoggerFactory.getLogger(this.getClass()); - - public ConsumerManager(ProxyHTTPServer proxyHTTPServer) { - this.proxyHTTPServer = proxyHTTPServer; - } - - public void init() throws Exception { - proxyHTTPServer.getEventBus().register(this); - logger.info("consumerManager inited......"); - } - - public void start() throws Exception { - logger.info("consumerManager started......"); - } - - public void shutdown() { - proxyHTTPServer.getEventBus().unregister(this); - for (ConsumerGroupManager consumerGroupManager : consumerTable.values()) { - try { - consumerGroupManager.shutdown(); - } catch (Exception ex) { - logger.error("shutdown consumerGroupManager[{}] err", consumerGroupManager, ex); - } - } - logger.info("consumerManager shutdown......"); - } - - public boolean contains(String consumerGroup) { - return consumerTable.containsKey(consumerGroup); - } - - /** - * add consumer - * - * @param consumerGroup - * @param consumerGroupConfig - * @throws Exception - */ - public synchronized void addConsumer(String consumerGroup, ConsumerGroupConf consumerGroupConfig) throws Exception { - ConsumerGroupManager cgm = new ConsumerGroupManager(proxyHTTPServer, consumerGroupConfig); - cgm.init(); - cgm.start(); - consumerTable.put(consumerGroup, cgm); - } - - /** - * restart consumer - */ - public synchronized void restartConsumer(String consumerGroup, ConsumerGroupConf consumerGroupConfig) throws Exception { - ConsumerGroupManager cgm = consumerTable.get(consumerGroup); - cgm.refresh(consumerGroupConfig); - } - - /** - * get consumer - */ - public ConsumerGroupManager getConsumer(String consumerGroup) throws Exception { - ConsumerGroupManager cgm = consumerTable.get(consumerGroup); - return cgm; - } - - /** - * delete consumer - * - * @param consumerGroup - */ - public synchronized void delConsumer(String consumerGroup) throws Exception { - ConsumerGroupManager cgm = consumerTable.remove(consumerGroup); - cgm.shutdown(); - } - - @Subscribe - public void onChange(ConsumerGroupTopicConfChangeEvent event) { - try { - logger.info("onChange event:{}", event); - if (event.action == ConsumerGroupTopicConfChangeEvent.ConsumerGroupTopicConfChangeAction.NEW) { - ConsumerGroupManager manager = getConsumer(event.consumerGroup); - if (Objects.isNull(manager)) { - return; - } - manager.getConsumerGroupConfig().getConsumerGroupTopicConf().put(event.topic, event.newTopicConf); - return; - } - - if (event.action == ConsumerGroupTopicConfChangeEvent.ConsumerGroupTopicConfChangeAction.CHANGE) { - ConsumerGroupManager manager = getConsumer(event.consumerGroup); - if (Objects.isNull(manager)) { - return; - } - manager.getConsumerGroupConfig().getConsumerGroupTopicConf().replace(event.topic, event.newTopicConf); - return; - } - - if (event.action == ConsumerGroupTopicConfChangeEvent.ConsumerGroupTopicConfChangeAction.DELETE) { - ConsumerGroupManager manager = getConsumer(event.consumerGroup); - if (Objects.isNull(manager)) { - return; - } - manager.getConsumerGroupConfig().getConsumerGroupTopicConf().remove(event.topic); - return; - } - } catch (Exception ex) { - logger.error("onChange event:{} err", event, ex); - } - } - - @Subscribe - public void onChange(ConsumerGroupStateEvent event) { - try { - logger.info("onChange event:{}", event); - if (event.action == ConsumerGroupStateEvent.ConsumerGroupStateAction.NEW) { - addConsumer(event.consumerGroup, event.consumerGroupConfig); - return; - } - - if (event.action == ConsumerGroupStateEvent.ConsumerGroupStateAction.CHANGE) { - restartConsumer(event.consumerGroup, event.consumerGroupConfig); - return; - } - - if (event.action == ConsumerGroupStateEvent.ConsumerGroupStateAction.DELETE) { - delConsumer(event.consumerGroup); - return; - } - } catch (Exception ex) { - logger.error("onChange event:{} err", event, ex); - } - } -} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/consumer/ProxyConsumer.java b/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/consumer/ProxyConsumer.java deleted file mode 100644 index ed427ac2a6..0000000000 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/consumer/ProxyConsumer.java +++ /dev/null @@ -1,261 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.emesher.core.protocol.http.consumer; - -import com.webank.emesher.boot.ProxyHTTPServer; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.plugin.MQConsumerWrapper; -import com.webank.emesher.core.consumergroup.ConsumerGroupConf; -import com.webank.emesher.core.consumergroup.ConsumerGroupTopicConf; -import com.webank.emesher.core.protocol.http.producer.ProxyProducer; -import com.webank.emesher.core.protocol.http.producer.SendMessageContext; -import com.webank.emesher.core.protocol.http.push.HTTPMessageHandler; -import com.webank.emesher.core.protocol.http.push.MessageHandler; -import com.webank.emesher.patch.ProxyConsumeConcurrentlyContext; -import com.webank.emesher.patch.ProxyConsumeConcurrentlyStatus; -import com.webank.emesher.patch.ProxyMessageListenerConcurrently; -import com.webank.emesher.util.ProxyUtil; -import com.webank.eventmesh.common.Constants; -import org.apache.commons.collections4.MapUtils; -import org.apache.rocketmq.client.producer.SendCallback; -import org.apache.rocketmq.client.producer.SendResult; -import org.apache.rocketmq.common.message.MessageExt; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; -import java.util.concurrent.atomic.AtomicBoolean; - -public class ProxyConsumer { - - private ProxyHTTPServer proxyHTTPServer; - - private AtomicBoolean started4Persistent = new AtomicBoolean(Boolean.FALSE); - - private AtomicBoolean started4Broadcast = new AtomicBoolean(Boolean.FALSE); - - private AtomicBoolean inited4Persistent = new AtomicBoolean(Boolean.FALSE); - - private AtomicBoolean inited4Broadcast = new AtomicBoolean(Boolean.FALSE); - - public Logger logger = LoggerFactory.getLogger(this.getClass()); - - public Logger messageLogger = LoggerFactory.getLogger("message"); - - private ConsumerGroupConf consumerGroupConf; - - private MQConsumerWrapper persistentMqConsumer = new MQConsumerWrapper(); - - private MQConsumerWrapper broadcastMqConsumer = new MQConsumerWrapper(); - - public ProxyConsumer(ProxyHTTPServer proxyHTTPServer, ConsumerGroupConf consumerGroupConf) { - this.proxyHTTPServer = proxyHTTPServer; - this.consumerGroupConf = consumerGroupConf; - } - - private MessageHandler httpMessageHandler = new HTTPMessageHandler(this); - - public synchronized void init() throws Exception { - persistentMqConsumer.init(false, proxyHTTPServer.getProxyConfiguration(), consumerGroupConf.getConsumerGroup()); - broadcastMqConsumer.init(true, proxyHTTPServer.getProxyConfiguration(), consumerGroupConf.getConsumerGroup()); - broadcastMqConsumer.setInstanceName(ProxyUtil.buildProxyClientID(consumerGroupConf.getConsumerGroup(), - proxyHTTPServer.getProxyConfiguration().proxyRegion, - proxyHTTPServer.getProxyConfiguration().proxyCluster)); - persistentMqConsumer.setInstanceName(ProxyUtil.buildProxyClientID(consumerGroupConf.getConsumerGroup(), - proxyHTTPServer.getProxyConfiguration().proxyRegion, - proxyHTTPServer.getProxyConfiguration().proxyCluster)); - inited4Persistent.compareAndSet(false, true); - inited4Broadcast.compareAndSet(false, true); - logger.info("ProxyConsumer [{}] inited.............", consumerGroupConf.getConsumerGroup()); - } - - public synchronized void start() throws Exception { - persistentMqConsumer.registerMessageListener(new ProxyMessageListenerConcurrently() { - @Override - public ProxyConsumeConcurrentlyStatus handleMessage(MessageExt msg, ProxyConsumeConcurrentlyContext context) { - String topic = msg.getTopic(); - - if (!ProxyUtil.isValidRMBTopic(topic)) { - return ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS; - } - - String bizSeqNo = msg.getKeys(); - String uniqueId = MapUtils.getString(msg.getProperties(), Constants.RMB_UNIQ_ID, ""); - - msg.putUserProperty(ProxyConstants.REQ_MQ2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); - - if (messageLogger.isDebugEnabled()) { - messageLogger.debug("message|mq2proxy|topic={}|msg={}", topic, msg); - } else { - messageLogger.info("message|mq2proxy|topic={}|bizSeqNo={}|uniqueId={}", topic, bizSeqNo, uniqueId); - } - - ConsumerGroupTopicConf currentTopicConfig = MapUtils.getObject(consumerGroupConf.getConsumerGroupTopicConf(), topic, null); - - if (currentTopicConfig == null) { - logger.error("no topicConfig found, consumerGroup:{} topic:{}", consumerGroupConf.getConsumerGroup(), topic); - try { - sendMessageBack(msg, uniqueId, bizSeqNo); - } catch (Exception ex) { - } - return ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS; - } - - HandleMsgContext handleMsgContext = new HandleMsgContext(ProxyUtil.buildPushMsgSeqNo(), consumerGroupConf.getConsumerGroup(), ProxyConsumer.this, - msg.getTopic(), msg, context, consumerGroupConf, proxyHTTPServer, bizSeqNo, uniqueId, currentTopicConfig); - - if (httpMessageHandler.handle(handleMsgContext)) { - ((ProxyConsumeConcurrentlyContext) context).setManualAck(true); - return ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS; - } - - try { - sendMessageBack(msg, uniqueId, bizSeqNo); - } catch (Exception ex) { - } - - return ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS; - } - }); - - persistentMqConsumer.start(); - started4Persistent.compareAndSet(false, true); - broadcastMqConsumer.registerMessageListener(new ProxyMessageListenerConcurrently() { - @Override - public ProxyConsumeConcurrentlyStatus handleMessage(MessageExt msg, ProxyConsumeConcurrentlyContext context) { - String topic = msg.getTopic(); - - if (!ProxyUtil.isValidRMBTopic(topic)) { - return ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS; - } - - String bizSeqNo = msg.getKeys(); - String uniqueId = MapUtils.getString(msg.getProperties(), Constants.RMB_UNIQ_ID, ""); - - msg.putUserProperty(ProxyConstants.REQ_MQ2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); - - if (messageLogger.isDebugEnabled()) { - messageLogger.debug("message|mq2proxy|topic={}|msg={}", topic, msg); - } else { - messageLogger.info("message|mq2proxy|topic={}|bizSeqNo={}|uniqueId={}", topic, bizSeqNo, uniqueId); - } - - ConsumerGroupTopicConf currentTopicConfig = MapUtils.getObject(consumerGroupConf.getConsumerGroupTopicConf(), topic, null); - - if (currentTopicConfig == null) { - logger.error("no topicConfig found, consumerGroup:{} topic:{}", consumerGroupConf.getConsumerGroup(), topic); - try { - sendMessageBack(msg, uniqueId, bizSeqNo); - } catch (Exception ex) { - } - return ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS; - } - - HandleMsgContext handleMsgContext = new HandleMsgContext(ProxyUtil.buildPushMsgSeqNo(), consumerGroupConf.getConsumerGroup(), ProxyConsumer.this, - msg.getTopic(), msg, context, consumerGroupConf, proxyHTTPServer, bizSeqNo, uniqueId, currentTopicConfig); - - if (httpMessageHandler.handle(handleMsgContext)) { - ((ProxyConsumeConcurrentlyContext) context).setManualAck(true); - return ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS; - } - - try { - sendMessageBack(msg, uniqueId, bizSeqNo); - } catch (Exception ex) { - } - - return ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS; - } - }); - broadcastMqConsumer.start(); - started4Broadcast.compareAndSet(false, true); - } - - public void subscribe(String topic) throws Exception { - if (ProxyUtil.isBroadcast(topic)) { - broadcastMqConsumer.subscribe(topic); - } else { - persistentMqConsumer.subscribe(topic); - } - } - - public void unsubscribe(String topic) throws Exception { - if (ProxyUtil.isBroadcast(topic)) { - broadcastMqConsumer.unsubscribe(topic); - } else { - persistentMqConsumer.unsubscribe(topic); - } - } - - public boolean isPause() { - return persistentMqConsumer.isPause() && broadcastMqConsumer.isPause(); - } - - public void pause() { - persistentMqConsumer.pause(); - broadcastMqConsumer.pause(); - } - - public synchronized void shutdown() throws Exception { - persistentMqConsumer.shutdown(); - started4Persistent.compareAndSet(true, false); - broadcastMqConsumer.shutdown(); - started4Broadcast.compareAndSet(true, false); - } - - public void updateOffset(String topic, List msgs, ProxyConsumeConcurrentlyContext context) { - if (ProxyUtil.isBroadcast(topic)) { - broadcastMqConsumer.updateOffset(msgs, context); - } else { - persistentMqConsumer.updateOffset(msgs, context); - } - } - - public ConsumerGroupConf getConsumerGroupConf() { - return consumerGroupConf; - } - - public ProxyHTTPServer getProxyHTTPServer() { - return proxyHTTPServer; - } - - public void sendMessageBack(final MessageExt msgBack, final String uniqueId, String bizSeqNo) throws Exception { - - ProxyProducer sendMessageBack - = proxyHTTPServer.getProducerManager().getProxyProducer(ProxyConstants.PRODUCER_GROUP_NAME_PREFIX - + consumerGroupConf.getConsumerGroup()); - - if (sendMessageBack == null) { - logger.warn("consumer:{} consume fail, sendMessageBack, bizSeqNo:{}, uniqueId:{}", consumerGroupConf.getConsumerGroup(), bizSeqNo, uniqueId); - return; - } - - final SendMessageContext sendMessageBackContext = new SendMessageContext(bizSeqNo, msgBack, sendMessageBack, proxyHTTPServer); - - sendMessageBack.send(sendMessageBackContext, new SendCallback() { - @Override - public void onSuccess(SendResult sendResult) { - } - - @Override - public void onException(Throwable e) { - logger.warn("consumer:{} consume fail, sendMessageBack, bizSeqno:{}, uniqueId:{}", consumerGroupConf.getConsumerGroup(), bizSeqNo, uniqueId); - } - }); - } -} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/BatchSendMessageProcessor.java b/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/BatchSendMessageProcessor.java deleted file mode 100644 index c301c73221..0000000000 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/BatchSendMessageProcessor.java +++ /dev/null @@ -1,256 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.emesher.core.protocol.http.processor; - -import com.webank.defibus.common.DeFiBusConstant; -import com.webank.emesher.boot.ProxyHTTPServer; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.protocol.http.async.AsyncContext; -import com.webank.emesher.core.protocol.http.processor.inf.HttpRequestProcessor; -import com.webank.emesher.core.protocol.http.producer.ProxyProducer; -import com.webank.emesher.core.protocol.http.producer.SendMessageContext; -import com.webank.eventmesh.common.IPUtil; -import com.webank.eventmesh.common.command.HttpCommand; -import com.webank.eventmesh.common.protocol.http.body.message.SendMessageBatchRequestBody; -import com.webank.eventmesh.common.protocol.http.body.message.SendMessageBatchResponseBody; -import com.webank.eventmesh.common.protocol.http.common.ProxyRetCode; -import com.webank.eventmesh.common.protocol.http.common.RequestCode; -import com.webank.eventmesh.common.protocol.http.header.message.SendMessageBatchRequestHeader; -import com.webank.eventmesh.common.protocol.http.header.message.SendMessageBatchResponseHeader; -import com.webank.emesher.util.ProxyUtil; -import io.netty.channel.ChannelHandlerContext; -import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.rocketmq.client.Validators; -import org.apache.rocketmq.client.producer.SendCallback; -import org.apache.rocketmq.client.producer.SendResult; -import org.apache.rocketmq.common.message.Message; -import org.apache.rocketmq.common.message.MessageAccessor; -import org.apache.rocketmq.common.message.MessageBatch; -import org.apache.rocketmq.common.message.MessageClientIDSetter; -import org.apache.rocketmq.remoting.common.RemotingHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.TimeUnit; - -public class BatchSendMessageProcessor implements HttpRequestProcessor { - - public Logger cmdLogger = LoggerFactory.getLogger("cmd"); - - private ProxyHTTPServer proxyHTTPServer; - - public BatchSendMessageProcessor(ProxyHTTPServer proxyHTTPServer) { - this.proxyHTTPServer = proxyHTTPServer; - } - - public Logger batchMessageLogger = LoggerFactory.getLogger("batchMessage"); - - public void processRequest(ChannelHandlerContext ctx, AsyncContext asyncContext) throws Exception { - - HttpCommand responseProxyCommand; - - cmdLogger.info("cmd={}|{}|client2proxy|from={}|to={}", RequestCode.get(Integer.valueOf(asyncContext.getRequest().getRequestCode())), - ProxyConstants.PROTOCOL_HTTP, - RemotingHelper.parseChannelRemoteAddr(ctx.channel()), IPUtil.getLocalAddress()); - - SendMessageBatchRequestHeader sendMessageBatchRequestHeader = (SendMessageBatchRequestHeader) asyncContext.getRequest().getHeader(); - SendMessageBatchRequestBody sendMessageBatchRequestBody = (SendMessageBatchRequestBody) asyncContext.getRequest().getBody(); - - SendMessageBatchResponseHeader sendMessageBatchResponseHeader = - SendMessageBatchResponseHeader.buildHeader(Integer.valueOf(asyncContext.getRequest().getRequestCode()), proxyHTTPServer.getProxyConfiguration().proxyCluster, - IPUtil.getLocalAddress(), proxyHTTPServer.getProxyConfiguration().proxyEnv, - proxyHTTPServer.getProxyConfiguration().proxyRegion, - proxyHTTPServer.getProxyConfiguration().proxyDCN, proxyHTTPServer.getProxyConfiguration().proxyIDC); - - if (StringUtils.isBlank(sendMessageBatchRequestHeader.getPid()) - || !StringUtils.isNumeric(sendMessageBatchRequestHeader.getPid()) - || StringUtils.isBlank(sendMessageBatchRequestHeader.getSys())) { - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - sendMessageBatchResponseHeader, - SendMessageBatchResponseBody.buildBody(ProxyRetCode.PROXY_PROTOCOL_HEADER_ERR.getRetCode(), ProxyRetCode.PROXY_PROTOCOL_HEADER_ERR.getErrMsg())); - asyncContext.onComplete(responseProxyCommand); - return; - } - - if (CollectionUtils.isEmpty(sendMessageBatchRequestBody.getContents()) - || StringUtils.isBlank(sendMessageBatchRequestBody.getBatchId()) - || (Integer.valueOf(sendMessageBatchRequestBody.getSize()) != CollectionUtils.size(sendMessageBatchRequestBody.getContents()))) { - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - sendMessageBatchResponseHeader, - SendMessageBatchResponseBody.buildBody(ProxyRetCode.PROXY_PROTOCOL_BODY_ERR.getRetCode(), ProxyRetCode.PROXY_PROTOCOL_BODY_ERR.getErrMsg())); - asyncContext.onComplete(responseProxyCommand); - return; - } - - if (!proxyHTTPServer.getProxyConfiguration().proxyServerBatchMsgNumLimiter - .tryAcquire(Integer.valueOf(sendMessageBatchRequestBody.getSize()), ProxyConstants.DEFAULT_FASTFAIL_TIMEOUT_IN_MILLISECONDS, TimeUnit.MILLISECONDS)) { - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - sendMessageBatchResponseHeader, - SendMessageBatchResponseBody.buildBody(ProxyRetCode.PROXY_BATCH_SPEED_OVER_LIMIT_ERR.getRetCode(), ProxyRetCode.PROXY_BATCH_SPEED_OVER_LIMIT_ERR.getErrMsg())); - proxyHTTPServer.metrics.summaryMetrics - .recordSendBatchMsgDiscard(Integer.valueOf(sendMessageBatchRequestBody.getSize())); - asyncContext.onComplete(responseProxyCommand); - return; - } - - if (StringUtils.isBlank(sendMessageBatchRequestHeader.getDcn())) { - sendMessageBatchRequestHeader.setDcn("BATCH"); - } - String producerGroup = ProxyUtil.buildClientGroup(sendMessageBatchRequestHeader.getSys(), - sendMessageBatchRequestHeader.getDcn()); - ProxyProducer batchProxyProducer = proxyHTTPServer.getProducerManager().getProxyProducer(producerGroup); - - batchProxyProducer.getMqProducerWrapper().getDefaultMQProducer().setRetryTimesWhenSendFailed(0); - batchProxyProducer.getMqProducerWrapper().getDefaultMQProducer().setRetryTimesWhenSendAsyncFailed(0); - batchProxyProducer.getMqProducerWrapper().getDefaultMQProducer().setPollNameServerInterval(60000); - - batchProxyProducer.getMqProducerWrapper().getDefaultMQProducer().setCompressMsgBodyOverHowmuch(10); - batchProxyProducer.getMqProducerWrapper().getDefaultMQProducer().getDefaultMQProducerImpl().getmQClientFactory() - .getNettyClientConfig() - .setClientAsyncSemaphoreValue(proxyHTTPServer.getProxyConfiguration().proxyServerAsyncAccumulationThreshold); - - if (!batchProxyProducer.getStarted().get()) { - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - sendMessageBatchResponseHeader, - SendMessageBatchResponseBody.buildBody(ProxyRetCode.PROXY_BATCH_PRODUCER_STOPED_ERR.getRetCode(), ProxyRetCode.PROXY_BATCH_PRODUCER_STOPED_ERR.getErrMsg())); - asyncContext.onComplete(responseProxyCommand); - return; - } - - long batchStartTime = System.currentTimeMillis(); - - List msgList = new ArrayList<>(); - Map> topicBatchMessageMappings = new ConcurrentHashMap>(); - for (SendMessageBatchRequestBody.BatchMessageEntity msg : sendMessageBatchRequestBody.getContents()) { - if (StringUtils.isBlank(msg.topic) - || StringUtils.isBlank(msg.msg)) { - continue; - } - - if (StringUtils.isBlank(msg.ttl) || !StringUtils.isNumeric(msg.ttl)) { - msg.ttl = String.valueOf(ProxyConstants.DEFAULT_MSG_TTL_MILLS); - } - - try { - Message rocketMQMsg; - if (StringUtils.isBlank(msg.tag)) { - rocketMQMsg = new Message(msg.topic, msg.msg.getBytes(ProxyConstants.DEFAULT_CHARSET)); - } else { - rocketMQMsg = new Message(msg.topic, msg.tag, msg.msg.getBytes(ProxyConstants.DEFAULT_CHARSET)); - } - rocketMQMsg.putUserProperty(DeFiBusConstant.KEY, DeFiBusConstant.PERSISTENT); - MessageAccessor.putProperty(rocketMQMsg, DeFiBusConstant.PROPERTY_MESSAGE_TTL, msg.ttl); - msgList.add(rocketMQMsg); - if (topicBatchMessageMappings.containsKey(msg.topic)) { - topicBatchMessageMappings.get(msg.topic).add(rocketMQMsg); - } else { - List tmp = new ArrayList<>(); - tmp.add(rocketMQMsg); - topicBatchMessageMappings.put(msg.topic, tmp); - } - - if (batchMessageLogger.isDebugEnabled()) { - batchMessageLogger.debug("msg2MQMsg suc, msg:{}", msg.msg); - } - - } catch (Exception e) { - batchMessageLogger.error("msg2MQMsg err, msg:{}", msg, e); - } - } - - if (CollectionUtils.isEmpty(msgList)) { - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - sendMessageBatchResponseHeader, - SendMessageBatchResponseBody.buildBody(ProxyRetCode.PROXY_PROTOCOL_BODY_ERR.getRetCode(), ProxyRetCode.PROXY_PROTOCOL_BODY_ERR.getErrMsg())); - asyncContext.onComplete(responseProxyCommand); - return; - } - - proxyHTTPServer.metrics.summaryMetrics.recordSendBatchMsg(Integer.valueOf(sendMessageBatchRequestBody.getSize())); - - if (proxyHTTPServer.getProxyConfiguration().proxyServerBatchMsgBatchEnabled) { - for (List batchMsgs : topicBatchMessageMappings.values()) { - MessageBatch msgBatch; - try { - msgBatch = MessageBatch.generateFromList(batchMsgs); - for (Message message : msgBatch) { - Validators.checkMessage(message, batchProxyProducer.getMqProducerWrapper().getDefaultMQProducer()); - MessageClientIDSetter.setUniqID(message); - } - msgBatch.setBody(msgBatch.encode()); - } catch (Exception e) { - continue; - } - - final SendMessageContext sendMessageContext = new SendMessageContext(sendMessageBatchRequestBody.getBatchId(), msgBatch, batchProxyProducer, proxyHTTPServer); - batchProxyProducer.send(sendMessageContext, new SendCallback() { - @Override - public void onSuccess(SendResult sendResult) { - } - - @Override - public void onException(Throwable e) { - batchMessageLogger.warn("", e); - proxyHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000)); - } - }); - } - } else { - for (Message msg : msgList) { - final SendMessageContext sendMessageContext = new SendMessageContext(sendMessageBatchRequestBody.getBatchId(), msg, batchProxyProducer, proxyHTTPServer); - batchProxyProducer.send(sendMessageContext, new SendCallback() { - @Override - public void onSuccess(SendResult sendResult) { - - } - - @Override - public void onException(Throwable e) { - batchMessageLogger.warn("", e); - proxyHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000)); - } - }); - } - } - - long batchEndTime = System.currentTimeMillis(); - proxyHTTPServer.metrics.summaryMetrics.recordBatchSendMsgCost(batchEndTime - batchStartTime); - batchMessageLogger.debug("batchMessage|proxy2mq|REQ|ASYNC|batchId={}|send2MQCost={}ms|msgNum={}|topics={}", - sendMessageBatchRequestBody.getBatchId(), - batchEndTime - batchStartTime, - sendMessageBatchRequestBody.getSize(), - topicBatchMessageMappings.keySet()); - - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - sendMessageBatchResponseHeader, - SendMessageBatchResponseBody.buildBody(ProxyRetCode.SUCCESS.getRetCode(), ProxyRetCode.SUCCESS.getErrMsg())); - asyncContext.onComplete(responseProxyCommand); - - return; - } - - @Override - public boolean rejectRequest() { - return false; - } -} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/BatchSendMessageV2Processor.java b/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/BatchSendMessageV2Processor.java deleted file mode 100644 index 269e63af02..0000000000 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/BatchSendMessageV2Processor.java +++ /dev/null @@ -1,214 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.emesher.core.protocol.http.processor; - -import com.webank.defibus.common.DeFiBusConstant; -import com.webank.emesher.boot.ProxyHTTPServer; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.protocol.http.async.AsyncContext; -import com.webank.emesher.core.protocol.http.processor.inf.HttpRequestProcessor; -import com.webank.emesher.core.protocol.http.producer.ProxyProducer; -import com.webank.emesher.core.protocol.http.producer.SendMessageContext; -import com.webank.eventmesh.common.IPUtil; -import com.webank.eventmesh.common.command.HttpCommand; -import com.webank.eventmesh.common.protocol.http.body.message.SendMessageBatchV2RequestBody; -import com.webank.eventmesh.common.protocol.http.body.message.SendMessageBatchV2ResponseBody; -import com.webank.eventmesh.common.protocol.http.common.ProxyRetCode; -import com.webank.eventmesh.common.protocol.http.common.RequestCode; -import com.webank.eventmesh.common.protocol.http.header.message.SendMessageBatchV2RequestHeader; -import com.webank.eventmesh.common.protocol.http.header.message.SendMessageBatchV2ResponseHeader; -import com.webank.emesher.util.ProxyUtil; -import io.netty.channel.ChannelHandlerContext; -import org.apache.commons.lang3.StringUtils; -import org.apache.rocketmq.client.producer.SendCallback; -import org.apache.rocketmq.client.producer.SendResult; -import org.apache.rocketmq.common.message.Message; -import org.apache.rocketmq.common.message.MessageAccessor; -import org.apache.rocketmq.remoting.common.RemotingHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.concurrent.TimeUnit; - -public class BatchSendMessageV2Processor implements HttpRequestProcessor { - - public Logger cmdLogger = LoggerFactory.getLogger("cmd"); - - private ProxyHTTPServer proxyHTTPServer; - - public BatchSendMessageV2Processor(ProxyHTTPServer proxyHTTPServer) { - this.proxyHTTPServer = proxyHTTPServer; - } - - public Logger batchMessageLogger = LoggerFactory.getLogger("batchMessage"); - - public void processRequest(ChannelHandlerContext ctx, AsyncContext asyncContext) throws Exception { - - HttpCommand responseProxyCommand; - - cmdLogger.info("cmd={}|{}|client2proxy|from={}|to={}", RequestCode.get(Integer.valueOf(asyncContext.getRequest().getRequestCode())), - ProxyConstants.PROTOCOL_HTTP, - RemotingHelper.parseChannelRemoteAddr(ctx.channel()), IPUtil.getLocalAddress()); - - SendMessageBatchV2RequestHeader sendMessageBatchV2RequestHeader = (SendMessageBatchV2RequestHeader) asyncContext.getRequest().getHeader(); - SendMessageBatchV2RequestBody sendMessageBatchV2RequestBody = (SendMessageBatchV2RequestBody) asyncContext.getRequest().getBody(); - - SendMessageBatchV2ResponseHeader sendMessageBatchV2ResponseHeader = - SendMessageBatchV2ResponseHeader.buildHeader(Integer.valueOf(asyncContext.getRequest().getRequestCode()), proxyHTTPServer.getProxyConfiguration().proxyCluster, - IPUtil.getLocalAddress(), proxyHTTPServer.getProxyConfiguration().proxyEnv, - proxyHTTPServer.getProxyConfiguration().proxyRegion, - proxyHTTPServer.getProxyConfiguration().proxyDCN, proxyHTTPServer.getProxyConfiguration().proxyIDC); - - if (StringUtils.isBlank(sendMessageBatchV2RequestHeader.getPid()) - || !StringUtils.isNumeric(sendMessageBatchV2RequestHeader.getPid()) - || StringUtils.isBlank(sendMessageBatchV2RequestHeader.getSys())) { - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - sendMessageBatchV2ResponseHeader, - SendMessageBatchV2ResponseBody.buildBody(ProxyRetCode.PROXY_PROTOCOL_HEADER_ERR.getRetCode(), ProxyRetCode.PROXY_PROTOCOL_HEADER_ERR.getErrMsg())); - asyncContext.onComplete(responseProxyCommand); - return; - } - - if (StringUtils.isBlank(sendMessageBatchV2RequestBody.getBizSeqNo()) - || StringUtils.isBlank(sendMessageBatchV2RequestBody.getTopic()) - || StringUtils.isBlank(sendMessageBatchV2RequestBody.getMsg())) { - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - sendMessageBatchV2ResponseHeader, - SendMessageBatchV2ResponseBody.buildBody(ProxyRetCode.PROXY_PROTOCOL_BODY_ERR.getRetCode(), ProxyRetCode.PROXY_PROTOCOL_BODY_ERR.getErrMsg())); - asyncContext.onComplete(responseProxyCommand); - return; - } - - if (!proxyHTTPServer.getProxyConfiguration().proxyServerBatchMsgNumLimiter - .tryAcquire(ProxyConstants.DEFAULT_FASTFAIL_TIMEOUT_IN_MILLISECONDS, TimeUnit.MILLISECONDS)) { - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - sendMessageBatchV2ResponseHeader, - SendMessageBatchV2ResponseBody.buildBody(ProxyRetCode.PROXY_BATCH_SPEED_OVER_LIMIT_ERR.getRetCode(), ProxyRetCode.PROXY_BATCH_SPEED_OVER_LIMIT_ERR.getErrMsg())); - proxyHTTPServer.metrics.summaryMetrics - .recordSendBatchMsgDiscard(1); - asyncContext.onComplete(responseProxyCommand); - return; - } - - if (StringUtils.isBlank(sendMessageBatchV2RequestHeader.getDcn())) { - sendMessageBatchV2RequestHeader.setDcn("BATCH"); - } - String producerGroup = ProxyUtil.buildClientGroup(sendMessageBatchV2RequestHeader.getSys(), - sendMessageBatchV2RequestHeader.getDcn()); - ProxyProducer batchProxyProducer = proxyHTTPServer.getProducerManager().getProxyProducer(producerGroup); - batchProxyProducer.getMqProducerWrapper().getDefaultMQProducer().setRetryTimesWhenSendFailed(0); - batchProxyProducer.getMqProducerWrapper().getDefaultMQProducer().setRetryTimesWhenSendAsyncFailed(0); - batchProxyProducer.getMqProducerWrapper().getDefaultMQProducer().setPollNameServerInterval(60000); - - batchProxyProducer.getMqProducerWrapper().getDefaultMQProducer().getDefaultMQProducerImpl().getmQClientFactory() - .getNettyClientConfig().setClientAsyncSemaphoreValue(proxyHTTPServer.getProxyConfiguration().proxyServerAsyncAccumulationThreshold); - batchProxyProducer.getMqProducerWrapper().getDefaultMQProducer().setCompressMsgBodyOverHowmuch(10); - if (!batchProxyProducer.getStarted().get()) { - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - sendMessageBatchV2ResponseHeader, - SendMessageBatchV2ResponseBody.buildBody(ProxyRetCode.PROXY_BATCH_PRODUCER_STOPED_ERR.getRetCode(), ProxyRetCode.PROXY_BATCH_PRODUCER_STOPED_ERR.getErrMsg())); - asyncContext.onComplete(responseProxyCommand); - return; - } - - long batchStartTime = System.currentTimeMillis(); - - if (StringUtils.isBlank(sendMessageBatchV2RequestBody.getTtl()) || !StringUtils.isNumeric(sendMessageBatchV2RequestBody.getTtl())) { - sendMessageBatchV2RequestBody.setTtl(String.valueOf(ProxyConstants.DEFAULT_MSG_TTL_MILLS)); - } - - Message rocketMQMsg = null; - - try { - if (StringUtils.isBlank(sendMessageBatchV2RequestBody.getTag())) { - rocketMQMsg = new Message(sendMessageBatchV2RequestBody.getTopic(), sendMessageBatchV2RequestBody.getMsg().getBytes(ProxyConstants.DEFAULT_CHARSET)); - } else { - rocketMQMsg = new Message(sendMessageBatchV2RequestBody.getTopic(), sendMessageBatchV2RequestBody.getTag(), - sendMessageBatchV2RequestBody.getMsg().getBytes(ProxyConstants.DEFAULT_CHARSET)); - } - rocketMQMsg.putUserProperty(DeFiBusConstant.KEY, DeFiBusConstant.PERSISTENT); - MessageAccessor.putProperty(rocketMQMsg, DeFiBusConstant.PROPERTY_MESSAGE_TTL, sendMessageBatchV2RequestBody.getTtl()); - - if (batchMessageLogger.isDebugEnabled()) { - batchMessageLogger.debug("msg2MQMsg suc, topic:{}, msg:{}", sendMessageBatchV2RequestBody.getTopic(), sendMessageBatchV2RequestBody.getMsg()); - } - - } catch (Exception e) { - batchMessageLogger.error("msg2MQMsg err, topic:{}, msg:{}", sendMessageBatchV2RequestBody.getTopic(), sendMessageBatchV2RequestBody.getMsg(), e); - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - sendMessageBatchV2ResponseHeader, - SendMessageBatchV2ResponseBody.buildBody(ProxyRetCode.PROXY_PACKAGE_MSG_ERR.getRetCode(), ProxyRetCode.PROXY_PACKAGE_MSG_ERR.getErrMsg() + ProxyUtil.stackTrace(e, 2))); - asyncContext.onComplete(responseProxyCommand); - return; - } - - proxyHTTPServer.metrics.summaryMetrics.recordSendBatchMsg(1); - - final SendMessageContext sendMessageContext = new SendMessageContext(sendMessageBatchV2RequestBody.getBizSeqNo(), rocketMQMsg, batchProxyProducer, proxyHTTPServer); - - try { - batchProxyProducer.send(sendMessageContext, new SendCallback() { - @Override - public void onSuccess(SendResult sendResult) { - long batchEndTime = System.currentTimeMillis(); - proxyHTTPServer.metrics.summaryMetrics.recordBatchSendMsgCost(batchEndTime - batchStartTime); - batchMessageLogger.debug("batchMessageV2|proxy2mq|REQ|ASYNC|bizSeqNo={}|send2MQCost={}ms|topic={}", - sendMessageBatchV2RequestBody.getBizSeqNo(), - batchEndTime - batchStartTime, - sendMessageBatchV2RequestBody.getTopic()); - } - - @Override - public void onException(Throwable e) { - long batchEndTime = System.currentTimeMillis(); - proxyHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000)); - proxyHTTPServer.metrics.summaryMetrics.recordBatchSendMsgCost(batchEndTime - batchStartTime); - batchMessageLogger.error("batchMessageV2|proxy2mq|REQ|ASYNC|bizSeqNo={}|send2MQCost={}ms|topic={}", - sendMessageBatchV2RequestBody.getBizSeqNo(), - batchEndTime - batchStartTime, - sendMessageBatchV2RequestBody.getTopic(), e); - } - }); - } catch (Exception e) { - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - sendMessageBatchV2ResponseHeader, - SendMessageBatchV2ResponseBody.buildBody(ProxyRetCode.PROXY_SEND_BATCHLOG_MSG_ERR.getRetCode(), ProxyRetCode.PROXY_SEND_BATCHLOG_MSG_ERR.getErrMsg() + ProxyUtil.stackTrace(e, 2))); - asyncContext.onComplete(responseProxyCommand); - long batchEndTime = System.currentTimeMillis(); - proxyHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000)); - proxyHTTPServer.metrics.summaryMetrics.recordBatchSendMsgCost(batchEndTime - batchStartTime); - batchMessageLogger.error("batchMessageV2|proxy2mq|REQ|ASYNC|bizSeqNo={}|send2MQCost={}ms|topic={}", - sendMessageBatchV2RequestBody.getBizSeqNo(), - batchEndTime - batchStartTime, - sendMessageBatchV2RequestBody.getTopic(), e); - } - - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - sendMessageBatchV2ResponseHeader, - SendMessageBatchV2ResponseBody.buildBody(ProxyRetCode.SUCCESS.getRetCode(), ProxyRetCode.SUCCESS.getErrMsg())); - asyncContext.onComplete(responseProxyCommand); - - return; - } - - @Override - public boolean rejectRequest() { - return false; - } -} - diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/ReplyMessageProcessor.java b/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/ReplyMessageProcessor.java deleted file mode 100644 index 17cdfc0843..0000000000 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/ReplyMessageProcessor.java +++ /dev/null @@ -1,252 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.emesher.core.protocol.http.processor; - -import com.webank.defibus.common.DeFiBusConstant; -import com.webank.emesher.boot.ProxyHTTPServer; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.protocol.http.async.AsyncContext; -import com.webank.emesher.core.protocol.http.async.CompleteHandler; -import com.webank.emesher.core.protocol.http.processor.inf.HttpRequestProcessor; -import com.webank.emesher.core.protocol.http.producer.ProxyProducer; -import com.webank.emesher.core.protocol.http.producer.SendMessageContext; -import com.webank.eventmesh.common.IPUtil; -import com.webank.eventmesh.common.LiteMessage; -import com.webank.eventmesh.common.command.HttpCommand; -import com.webank.eventmesh.common.protocol.http.body.message.ReplyMessageRequestBody; -import com.webank.eventmesh.common.protocol.http.body.message.ReplyMessageResponseBody; -import com.webank.eventmesh.common.protocol.http.body.message.SendMessageResponseBody; -import com.webank.eventmesh.common.protocol.http.common.ProxyRetCode; -import com.webank.eventmesh.common.protocol.http.common.RequestCode; -import com.webank.eventmesh.common.protocol.http.header.message.ReplyMessageRequestHeader; -import com.webank.eventmesh.common.protocol.http.header.message.ReplyMessageResponseHeader; -import com.webank.emesher.util.ProxyUtil; -import io.netty.channel.ChannelHandlerContext; -import org.apache.commons.collections4.MapUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.rocketmq.client.producer.SendCallback; -import org.apache.rocketmq.client.producer.SendResult; -import org.apache.rocketmq.common.MixAll; -import org.apache.rocketmq.common.message.Message; -import org.apache.rocketmq.common.message.MessageAccessor; -import org.apache.rocketmq.common.message.MessageConst; -import org.apache.rocketmq.remoting.common.RemotingHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Map; - -public class ReplyMessageProcessor implements HttpRequestProcessor { - - public Logger messageLogger = LoggerFactory.getLogger("message"); - - public Logger cmdLogger = LoggerFactory.getLogger("cmd"); - - public Logger httpLogger = LoggerFactory.getLogger("http"); - - private ProxyHTTPServer proxyHTTPServer; - - public ReplyMessageProcessor(ProxyHTTPServer proxyHTTPServer) { - this.proxyHTTPServer = proxyHTTPServer; - } - - @Override - public void processRequest(ChannelHandlerContext ctx, AsyncContext asyncContext) throws Exception { - HttpCommand responseProxyCommand; - - cmdLogger.info("cmd={}|{}|client2proxy|from={}|to={}", RequestCode.get(Integer.valueOf(asyncContext.getRequest().getRequestCode())), - ProxyConstants.PROTOCOL_HTTP, - RemotingHelper.parseChannelRemoteAddr(ctx.channel()), IPUtil.getLocalAddress()); - - ReplyMessageRequestHeader replyMessageRequestHeader = (ReplyMessageRequestHeader) asyncContext.getRequest().getHeader(); - ReplyMessageRequestBody replyMessageRequestBody = (ReplyMessageRequestBody) asyncContext.getRequest().getBody(); - - ReplyMessageResponseHeader replyMessageResponseHeader = - ReplyMessageResponseHeader.buildHeader(Integer.valueOf(asyncContext.getRequest().getRequestCode()), proxyHTTPServer.getProxyConfiguration().proxyCluster, - IPUtil.getLocalAddress(), proxyHTTPServer.getProxyConfiguration().proxyEnv, - proxyHTTPServer.getProxyConfiguration().proxyRegion, - proxyHTTPServer.getProxyConfiguration().proxyDCN, proxyHTTPServer.getProxyConfiguration().proxyIDC); - - //HEADER校验 - if (StringUtils.isBlank(replyMessageRequestHeader.getIdc()) - || StringUtils.isBlank(replyMessageRequestHeader.getDcn()) - || StringUtils.isBlank(replyMessageRequestHeader.getPid()) - || !StringUtils.isNumeric(replyMessageRequestHeader.getPid()) - || StringUtils.isBlank(replyMessageRequestHeader.getSys())) { - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - replyMessageResponseHeader, - ReplyMessageResponseBody.buildBody(ProxyRetCode.PROXY_PROTOCOL_HEADER_ERR.getRetCode(), ProxyRetCode.PROXY_PROTOCOL_HEADER_ERR.getErrMsg())); - asyncContext.onComplete(responseProxyCommand); - return; - } - - //validate body - if (StringUtils.isBlank(replyMessageRequestBody.getBizSeqNo()) - || StringUtils.isBlank(replyMessageRequestBody.getUniqueId()) - || StringUtils.isBlank(replyMessageRequestBody.getContent())) { - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - replyMessageResponseHeader, - ReplyMessageResponseBody.buildBody(ProxyRetCode.PROXY_PROTOCOL_BODY_ERR.getRetCode(), ProxyRetCode.PROXY_PROTOCOL_BODY_ERR.getErrMsg())); - asyncContext.onComplete(responseProxyCommand); - return; - } - - String producerGroup = ProxyUtil.buildClientGroup(replyMessageRequestHeader.getSys(), - replyMessageRequestHeader.getDcn()); - ProxyProducer proxyProducer = proxyHTTPServer.getProducerManager().getProxyProducer(producerGroup); - - if (!proxyProducer.getStarted().get()) { - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - replyMessageResponseHeader, - ReplyMessageResponseBody.buildBody(ProxyRetCode.PROXY_GROUP_PRODUCER_STOPED_ERR.getRetCode(), ProxyRetCode.PROXY_GROUP_PRODUCER_STOPED_ERR.getErrMsg())); - asyncContext.onComplete(responseProxyCommand); - return; - } - - long startTime = System.currentTimeMillis(); - - Message rocketMQMsg; - - String replyTopic = DeFiBusConstant.RR_REPLY_TOPIC; - - Map extFields = replyMessageRequestBody.getExtFields(); - final String replyMQCluster = MapUtils.getString(extFields, DeFiBusConstant.PROPERTY_MESSAGE_CLUSTER, null); - if (!org.apache.commons.lang3.StringUtils.isEmpty(replyMQCluster)) { - replyTopic = replyMQCluster + "-" + replyTopic; - } else { - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - replyMessageResponseHeader, - ReplyMessageResponseBody.buildBody(ProxyRetCode.PROXY_REPLY_MSG_ERR.getRetCode(), ProxyRetCode.PROXY_REPLY_MSG_ERR.getErrMsg())); - asyncContext.onComplete(responseProxyCommand); - return; - } - - try { - rocketMQMsg = new Message(replyTopic, - replyMessageRequestBody.getContent().getBytes(ProxyConstants.DEFAULT_CHARSET)); - - rocketMQMsg.putUserProperty(DeFiBusConstant.KEY, DeFiBusConstant.PERSISTENT); - for (Map.Entry entry : extFields.entrySet()) { - rocketMQMsg.putUserProperty(entry.getKey(), entry.getValue()); - } - -// //for rocketmq support -// MessageAccessor.putProperty(rocketMQMsg, MessageConst.PROPERTY_MESSAGE_TYPE, MixAll.REPLY_MESSAGE_FLAG); -// MessageAccessor.putProperty(rocketMQMsg, MessageConst.PROPERTY_CORRELATION_ID, rocketMQMsg.getProperty(DeFiBusConstant.PROPERTY_RR_REQUEST_ID)); -// MessageAccessor.putProperty(rocketMQMsg, MessageConst.PROPERTY_MESSAGE_REPLY_TO_CLIENT, rocketMQMsg.getProperty(DeFiBusConstant.PROPERTY_MESSAGE_REPLY_TO)); - - MessageAccessor.putProperty(rocketMQMsg, DeFiBusConstant.PROPERTY_MESSAGE_TTL, String.valueOf(ProxyConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS)); - rocketMQMsg.getProperties().put(ProxyConstants.REQ_C2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); - if (messageLogger.isDebugEnabled()) { - messageLogger.debug("msg2MQMsg suc, bizSeqNo={}, topic={}", replyMessageRequestBody.getBizSeqNo(), - replyTopic); - } - - } catch (Exception e) { - messageLogger.error("msg2MQMsg err, bizSeqNo={}, topic={}", replyMessageRequestBody.getBizSeqNo(), - replyTopic, e); - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - replyMessageResponseHeader, - ReplyMessageResponseBody.buildBody(ProxyRetCode.PROXY_PACKAGE_MSG_ERR.getRetCode(), ProxyRetCode.PROXY_PACKAGE_MSG_ERR.getErrMsg() + ProxyUtil.stackTrace(e, 2))); - asyncContext.onComplete(responseProxyCommand); - return; - } - - final SendMessageContext sendMessageContext = new SendMessageContext(replyMessageRequestBody.getBizSeqNo(), rocketMQMsg, proxyProducer, proxyHTTPServer); - proxyHTTPServer.metrics.summaryMetrics.recordReplyMsg(); - - CompleteHandler handler = new CompleteHandler() { - @Override - public void onResponse(HttpCommand httpCommand) { - try { - if (httpLogger.isDebugEnabled()) { - httpLogger.debug("{}", httpCommand); - } - proxyHTTPServer.sendResponse(ctx, httpCommand.httpResponse()); - proxyHTTPServer.metrics.summaryMetrics.recordHTTPReqResTimeCost(System.currentTimeMillis() - asyncContext.getRequest().getReqTime()); - } catch (Exception ex) { - } - } - }; - - LiteMessage liteMessage = new LiteMessage(replyMessageRequestBody.getBizSeqNo(), - replyMessageRequestBody.getUniqueId(), replyMessageRequestBody.getOrigTopic(), - replyMessageRequestBody.getContent()); - - try { - sendMessageContext.getMsg().getProperties().put(ProxyConstants.REQ_PROXY2MQ_TIMESTAMP, String.valueOf(System.currentTimeMillis())); - proxyProducer.reply(sendMessageContext, new SendCallback() { - @Override - public void onSuccess(SendResult sendResult) { - HttpCommand succ = asyncContext.getRequest().createHttpCommandResponse( - replyMessageResponseHeader, - SendMessageResponseBody.buildBody(ProxyRetCode.SUCCESS.getRetCode(), ProxyRetCode.SUCCESS.getErrMsg())); - asyncContext.onComplete(succ, handler); - long endTime = System.currentTimeMillis(); - proxyHTTPServer.metrics.summaryMetrics.recordReplyMsgCost(endTime - startTime); - messageLogger.info("message|proxy2mq|RSP|SYNC|reply2MQCost={}|topic={}|origTopic={}|bizSeqNo={}|uniqueId={}", - endTime - startTime, - replyMQCluster + "-" + DeFiBusConstant.RR_REPLY_TOPIC, - replyMessageRequestBody.getOrigTopic(), - replyMessageRequestBody.getBizSeqNo(), - replyMessageRequestBody.getUniqueId()); - } - - @Override - public void onException(Throwable e) { - HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( - replyMessageResponseHeader, - SendMessageResponseBody.buildBody(ProxyRetCode.PROXY_REPLY_MSG_ERR.getRetCode(), - ProxyRetCode.PROXY_REPLY_MSG_ERR.getErrMsg() + ProxyUtil.stackTrace(e, 2))); - asyncContext.onComplete(err, handler); - long endTime = System.currentTimeMillis(); - proxyHTTPServer.metrics.summaryMetrics.recordReplyMsgFailed(); - proxyHTTPServer.metrics.summaryMetrics.recordReplyMsgCost(endTime - startTime); - messageLogger.error("message|proxy2mq|RSP|SYNC|reply2MQCost={}|topic={}|origTopic={}|bizSeqNo={}|uniqueId={}", - endTime - startTime, - replyMQCluster + "-" + DeFiBusConstant.RR_REPLY_TOPIC, - replyMessageRequestBody.getOrigTopic(), - replyMessageRequestBody.getBizSeqNo(), - replyMessageRequestBody.getUniqueId(), e); - } - }); - } catch (Exception ex) { - HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( - replyMessageResponseHeader, - SendMessageResponseBody.buildBody(ProxyRetCode.PROXY_REPLY_MSG_ERR.getRetCode(), - ProxyRetCode.PROXY_REPLY_MSG_ERR.getErrMsg() + ProxyUtil.stackTrace(ex, 2))); - asyncContext.onComplete(err); - long endTime = System.currentTimeMillis(); - messageLogger.error("message|proxy2mq|RSP|SYNC|reply2MQCost={}|topic={}|origTopic={}|bizSeqNo={}|uniqueId={}", - endTime - startTime, - replyTopic, - replyMessageRequestBody.getOrigTopic(), - replyMessageRequestBody.getBizSeqNo(), - replyMessageRequestBody.getUniqueId(), ex); - proxyHTTPServer.metrics.summaryMetrics.recordReplyMsgFailed(); - proxyHTTPServer.metrics.summaryMetrics.recordReplyMsgCost(endTime - startTime); - } - - return; - } - - @Override - public boolean rejectRequest() { - return false; - } -} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/SendAsyncMessageProcessor.java b/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/SendAsyncMessageProcessor.java deleted file mode 100644 index ce4c1881d4..0000000000 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/SendAsyncMessageProcessor.java +++ /dev/null @@ -1,241 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.emesher.core.protocol.http.processor; - -import com.webank.defibus.common.DeFiBusConstant; -import com.webank.emesher.boot.ProxyHTTPServer; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.protocol.http.async.AsyncContext; -import com.webank.emesher.core.protocol.http.async.CompleteHandler; -import com.webank.emesher.core.protocol.http.processor.inf.HttpRequestProcessor; -import com.webank.emesher.core.protocol.http.producer.ProxyProducer; -import com.webank.emesher.core.protocol.http.producer.SendMessageContext; -import com.webank.eventmesh.common.Constants; -import com.webank.eventmesh.common.IPUtil; -import com.webank.eventmesh.common.LiteMessage; -import com.webank.eventmesh.common.command.HttpCommand; -import com.webank.eventmesh.common.protocol.http.body.message.SendMessageRequestBody; -import com.webank.eventmesh.common.protocol.http.body.message.SendMessageResponseBody; -import com.webank.eventmesh.common.protocol.http.common.ProxyRetCode; -import com.webank.eventmesh.common.protocol.http.common.RequestCode; -import com.webank.eventmesh.common.protocol.http.header.message.SendMessageRequestHeader; -import com.webank.eventmesh.common.protocol.http.header.message.SendMessageResponseHeader; -import com.webank.emesher.util.ProxyUtil; -import io.netty.channel.ChannelHandlerContext; -import org.apache.commons.lang3.StringUtils; -import org.apache.rocketmq.client.producer.SendCallback; -import org.apache.rocketmq.client.producer.SendResult; -import org.apache.rocketmq.common.message.Message; -import org.apache.rocketmq.common.message.MessageAccessor; -import org.apache.rocketmq.remoting.common.RemotingHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class SendAsyncMessageProcessor implements HttpRequestProcessor { - - public Logger messageLogger = LoggerFactory.getLogger("message"); - - public Logger httpLogger = LoggerFactory.getLogger("http"); - - public Logger cmdLogger = LoggerFactory.getLogger("cmd"); - - private ProxyHTTPServer proxyHTTPServer; - - public SendAsyncMessageProcessor(ProxyHTTPServer proxyHTTPServer) { - this.proxyHTTPServer = proxyHTTPServer; - } - - @Override - public void processRequest(ChannelHandlerContext ctx, AsyncContext asyncContext) throws Exception { - - HttpCommand responseProxyCommand; - - cmdLogger.info("cmd={}|{}|client2proxy|from={}|to={}", RequestCode.get(Integer.valueOf(asyncContext.getRequest().getRequestCode())), - ProxyConstants.PROTOCOL_HTTP, - RemotingHelper.parseChannelRemoteAddr(ctx.channel()), IPUtil.getLocalAddress()); - - SendMessageRequestHeader sendMessageRequestHeader = (SendMessageRequestHeader) asyncContext.getRequest().getHeader(); - SendMessageRequestBody sendMessageRequestBody = (SendMessageRequestBody) asyncContext.getRequest().getBody(); - - SendMessageResponseHeader sendMessageResponseHeader = - SendMessageResponseHeader.buildHeader(Integer.valueOf(asyncContext.getRequest().getRequestCode()), proxyHTTPServer.getProxyConfiguration().proxyCluster, - IPUtil.getLocalAddress(), proxyHTTPServer.getProxyConfiguration().proxyEnv, - proxyHTTPServer.getProxyConfiguration().proxyRegion, - proxyHTTPServer.getProxyConfiguration().proxyDCN, proxyHTTPServer.getProxyConfiguration().proxyIDC); - - //validate header - if (StringUtils.isBlank(sendMessageRequestHeader.getIdc()) - || StringUtils.isBlank(sendMessageRequestHeader.getDcn()) - || StringUtils.isBlank(sendMessageRequestHeader.getPid()) - || !StringUtils.isNumeric(sendMessageRequestHeader.getPid()) - || StringUtils.isBlank(sendMessageRequestHeader.getSys())) { - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - sendMessageResponseHeader, - SendMessageResponseBody.buildBody(ProxyRetCode.PROXY_PROTOCOL_HEADER_ERR.getRetCode(), ProxyRetCode.PROXY_PROTOCOL_HEADER_ERR.getErrMsg())); - asyncContext.onComplete(responseProxyCommand); - return; - } - - //validate body - if (StringUtils.isBlank(sendMessageRequestBody.getBizSeqNo()) - || StringUtils.isBlank(sendMessageRequestBody.getUniqueId()) - || StringUtils.isBlank(sendMessageRequestBody.getTopic()) - || StringUtils.isBlank(sendMessageRequestBody.getContent()) - || (StringUtils.isBlank(sendMessageRequestBody.getTtl()))) { - //sync message TTL can't be empty - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - sendMessageResponseHeader, - SendMessageResponseBody.buildBody(ProxyRetCode.PROXY_PROTOCOL_BODY_ERR.getRetCode(), ProxyRetCode.PROXY_PROTOCOL_BODY_ERR.getErrMsg())); - asyncContext.onComplete(responseProxyCommand); - return; - } - - String producerGroup = ProxyUtil.buildClientGroup(sendMessageRequestHeader.getSys(), - sendMessageRequestHeader.getDcn()); - ProxyProducer proxyProducer = proxyHTTPServer.getProducerManager().getProxyProducer(producerGroup); - - if (!proxyProducer.getStarted().get()) { - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - sendMessageResponseHeader, - SendMessageResponseBody.buildBody(ProxyRetCode.PROXY_GROUP_PRODUCER_STOPED_ERR.getRetCode(), ProxyRetCode.PROXY_GROUP_PRODUCER_STOPED_ERR.getErrMsg())); - asyncContext.onComplete(responseProxyCommand); - return; - } - - String ttl = String.valueOf(ProxyConstants.DEFAULT_MSG_TTL_MILLS); - if (StringUtils.isNotBlank(sendMessageRequestBody.getTtl()) && StringUtils.isNumeric(sendMessageRequestBody.getTtl())) { - ttl = sendMessageRequestBody.getTtl(); - } - - Message rocketMQMsg; - try { - if (StringUtils.isBlank(sendMessageRequestBody.getTag())) { - rocketMQMsg = new Message(sendMessageRequestBody.getTopic(), - sendMessageRequestBody.getContent().getBytes(ProxyConstants.DEFAULT_CHARSET)); - } else { - rocketMQMsg = new Message(sendMessageRequestBody.getTopic(), sendMessageRequestBody.getTag(), - sendMessageRequestBody.getContent().getBytes(ProxyConstants.DEFAULT_CHARSET)); - } - - rocketMQMsg.putUserProperty(DeFiBusConstant.KEY, DeFiBusConstant.PERSISTENT); - // new rocketmq client can't support put DeFiBusConstant.PROPERTY_MESSAGE_TTL -// rocketMQMsg.putUserProperty(DeFiBusConstant.PROPERTY_MESSAGE_TTL, ttl); - MessageAccessor.putProperty(rocketMQMsg, DeFiBusConstant.PROPERTY_MESSAGE_TTL, ttl); - rocketMQMsg.putUserProperty(ProxyConstants.REQ_C2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); - rocketMQMsg.putUserProperty(Constants.RMB_UNIQ_ID, sendMessageRequestBody.getUniqueId()); - rocketMQMsg.setKeys(sendMessageRequestBody.getBizSeqNo()); - - if (messageLogger.isDebugEnabled()) { - messageLogger.debug("msg2MQMsg suc, bizSeqNo={}, topic={}", sendMessageRequestBody.getBizSeqNo(), - sendMessageRequestBody.getTopic()); - } - rocketMQMsg.putUserProperty(ProxyConstants.REQ_PROXY2MQ_TIMESTAMP, String.valueOf(System.currentTimeMillis())); - } catch (Exception e) { - messageLogger.error("msg2MQMsg err, bizSeqNo={}, topic={}", sendMessageRequestBody.getBizSeqNo(), - sendMessageRequestBody.getTopic(), e); - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - sendMessageResponseHeader, - SendMessageResponseBody.buildBody(ProxyRetCode.PROXY_PACKAGE_MSG_ERR.getRetCode(), ProxyRetCode.PROXY_PACKAGE_MSG_ERR.getErrMsg() + ProxyUtil.stackTrace(e, 2))); - asyncContext.onComplete(responseProxyCommand); - return; - } - - final SendMessageContext sendMessageContext = new SendMessageContext(sendMessageRequestBody.getBizSeqNo(), rocketMQMsg, proxyProducer, proxyHTTPServer); - proxyHTTPServer.metrics.summaryMetrics.recordSendMsg(); - - long startTime = System.currentTimeMillis(); - - final CompleteHandler handler = new CompleteHandler() { - @Override - public void onResponse(HttpCommand httpCommand) { - try { - if (httpLogger.isDebugEnabled()) { - httpLogger.debug("{}", httpCommand); - } - proxyHTTPServer.sendResponse(ctx, httpCommand.httpResponse()); - proxyHTTPServer.metrics.summaryMetrics.recordHTTPReqResTimeCost(System.currentTimeMillis() - asyncContext.getRequest().getReqTime()); - } catch (Exception ex) { - } - } - }; - - LiteMessage liteMessage = new LiteMessage(sendMessageRequestBody.getBizSeqNo(), - sendMessageRequestBody.getUniqueId(), sendMessageRequestBody.getTopic(), - sendMessageRequestBody.getContent()) - .setProp(sendMessageRequestBody.getExtFields()); - - try { - sendMessageContext.getMsg().getProperties().put(ProxyConstants.REQ_PROXY2MQ_TIMESTAMP, String.valueOf(System.currentTimeMillis())); - proxyProducer.send(sendMessageContext, new SendCallback() { - @Override - public void onSuccess(SendResult sendResult) { - HttpCommand succ = asyncContext.getRequest().createHttpCommandResponse( - sendMessageResponseHeader, - SendMessageResponseBody.buildBody(ProxyRetCode.SUCCESS.getRetCode(), ProxyRetCode.SUCCESS.getErrMsg() + sendResult.toString())); - asyncContext.onComplete(succ, handler); - long endTime = System.currentTimeMillis(); - proxyHTTPServer.metrics.summaryMetrics.recordSendMsgCost(endTime - startTime); - messageLogger.info("message|proxy2mq|REQ|ASYNC|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", - endTime - startTime, - sendMessageRequestBody.getTopic(), - sendMessageRequestBody.getBizSeqNo(), - sendMessageRequestBody.getUniqueId()); - } - - @Override - public void onException(Throwable e) { - HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( - sendMessageResponseHeader, - SendMessageResponseBody.buildBody(ProxyRetCode.PROXY_SEND_ASYNC_MSG_ERR.getRetCode(), - ProxyRetCode.PROXY_SEND_ASYNC_MSG_ERR.getErrMsg() + ProxyUtil.stackTrace(e, 2))); - asyncContext.onComplete(err, handler); - long endTime = System.currentTimeMillis(); - proxyHTTPServer.metrics.summaryMetrics.recordSendMsgFailed(); - proxyHTTPServer.metrics.summaryMetrics.recordSendMsgCost(endTime - startTime); - messageLogger.error("message|proxy2mq|REQ|ASYNC|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", - endTime - startTime, - sendMessageRequestBody.getTopic(), - sendMessageRequestBody.getBizSeqNo(), - sendMessageRequestBody.getUniqueId(), e); - } - }); - } catch (Exception ex) { - HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( - sendMessageResponseHeader, - SendMessageResponseBody.buildBody(ProxyRetCode.PROXY_SEND_ASYNC_MSG_ERR.getRetCode(), - ProxyRetCode.PROXY_SEND_ASYNC_MSG_ERR.getErrMsg() + ProxyUtil.stackTrace(ex, 2))); - asyncContext.onComplete(err); - long endTime = System.currentTimeMillis(); - messageLogger.error("message|proxy2mq|REQ|ASYNC|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", - endTime - startTime, - sendMessageRequestBody.getTopic(), - sendMessageRequestBody.getBizSeqNo(), - sendMessageRequestBody.getUniqueId(), ex); - proxyHTTPServer.metrics.summaryMetrics.recordSendMsgFailed(); - proxyHTTPServer.metrics.summaryMetrics.recordSendMsgCost(endTime - startTime); - } - - return; - } - - @Override - public boolean rejectRequest() { - return false; - } - -} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/SendSyncMessageProcessor.java b/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/SendSyncMessageProcessor.java deleted file mode 100644 index 5194487f4d..0000000000 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/SendSyncMessageProcessor.java +++ /dev/null @@ -1,283 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.emesher.core.protocol.http.processor; - -import com.webank.defibus.client.impl.producer.RRCallback; -import com.webank.defibus.common.DeFiBusConstant; -import com.webank.emesher.boot.ProxyHTTPServer; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.protocol.http.async.AsyncContext; -import com.webank.emesher.core.protocol.http.async.CompleteHandler; -import com.webank.emesher.core.protocol.http.processor.inf.HttpRequestProcessor; -import com.webank.emesher.core.protocol.http.producer.ProxyProducer; -import com.webank.emesher.core.protocol.http.producer.SendMessageContext; -import com.webank.eventmesh.common.Constants; -import com.webank.eventmesh.common.IPUtil; -import com.webank.eventmesh.common.LiteMessage; -import com.webank.eventmesh.common.command.HttpCommand; -import com.webank.eventmesh.common.protocol.http.body.message.SendMessageRequestBody; -import com.webank.eventmesh.common.protocol.http.body.message.SendMessageResponseBody; -import com.webank.eventmesh.common.protocol.http.common.ProxyRetCode; -import com.webank.eventmesh.common.protocol.http.common.RequestCode; -import com.webank.eventmesh.common.protocol.http.header.message.SendMessageRequestHeader; -import com.webank.eventmesh.common.protocol.http.header.message.SendMessageResponseHeader; -import com.webank.emesher.util.ProxyUtil; -import com.alibaba.fastjson.JSON; -import io.netty.channel.ChannelHandlerContext; -import org.apache.commons.lang3.StringUtils; -import org.apache.rocketmq.client.producer.SendCallback; -import org.apache.rocketmq.client.producer.SendResult; -import org.apache.rocketmq.common.message.Message; -import org.apache.rocketmq.common.message.MessageAccessor; -import org.apache.rocketmq.common.message.MessageExt; -import org.apache.rocketmq.remoting.common.RemotingHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class SendSyncMessageProcessor implements HttpRequestProcessor { - - public Logger messageLogger = LoggerFactory.getLogger("message"); - - public Logger cmdLogger = LoggerFactory.getLogger("cmd"); - - public Logger httpLogger = LoggerFactory.getLogger("http"); - - private ProxyHTTPServer proxyHTTPServer; - - public SendSyncMessageProcessor(ProxyHTTPServer proxyHTTPServer) { - this.proxyHTTPServer = proxyHTTPServer; - } - - @Override - public void processRequest(ChannelHandlerContext ctx, AsyncContext asyncContext) throws Exception { - - HttpCommand responseProxyCommand; - - cmdLogger.info("cmd={}|{}|client2proxy|from={}|to={}", RequestCode.get(Integer.valueOf(asyncContext.getRequest().getRequestCode())), - ProxyConstants.PROTOCOL_HTTP, - RemotingHelper.parseChannelRemoteAddr(ctx.channel()), IPUtil.getLocalAddress()); - - SendMessageRequestHeader sendMessageRequestHeader = (SendMessageRequestHeader) asyncContext.getRequest().getHeader(); - SendMessageRequestBody sendMessageRequestBody = (SendMessageRequestBody) asyncContext.getRequest().getBody(); - - SendMessageResponseHeader sendMessageResponseHeader = - SendMessageResponseHeader.buildHeader(Integer.valueOf(asyncContext.getRequest().getRequestCode()), proxyHTTPServer.getProxyConfiguration().proxyCluster, - IPUtil.getLocalAddress(), proxyHTTPServer.getProxyConfiguration().proxyEnv, - proxyHTTPServer.getProxyConfiguration().proxyRegion, - proxyHTTPServer.getProxyConfiguration().proxyDCN, proxyHTTPServer.getProxyConfiguration().proxyIDC); - - if (StringUtils.isBlank(sendMessageRequestHeader.getIdc()) - || StringUtils.isBlank(sendMessageRequestHeader.getDcn()) - || StringUtils.isBlank(sendMessageRequestHeader.getPid()) - || !StringUtils.isNumeric(sendMessageRequestHeader.getPid()) - || StringUtils.isBlank(sendMessageRequestHeader.getSys())) { - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - sendMessageResponseHeader, - SendMessageResponseBody.buildBody(ProxyRetCode.PROXY_PROTOCOL_HEADER_ERR.getRetCode(), ProxyRetCode.PROXY_PROTOCOL_HEADER_ERR.getErrMsg())); - asyncContext.onComplete(responseProxyCommand); - return; - } - - if (StringUtils.isBlank(sendMessageRequestBody.getBizSeqNo()) - || StringUtils.isBlank(sendMessageRequestBody.getUniqueId()) - || StringUtils.isBlank(sendMessageRequestBody.getTopic()) - || StringUtils.isBlank(sendMessageRequestBody.getContent()) - || (StringUtils.isBlank(sendMessageRequestBody.getTtl()))) { - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - sendMessageResponseHeader, - SendMessageResponseBody.buildBody(ProxyRetCode.PROXY_PROTOCOL_BODY_ERR.getRetCode(), ProxyRetCode.PROXY_PROTOCOL_BODY_ERR.getErrMsg())); - asyncContext.onComplete(responseProxyCommand); - return; - } - - String producerGroup = ProxyUtil.buildClientGroup(sendMessageRequestHeader.getSys(), - sendMessageRequestHeader.getDcn()); - ProxyProducer proxyProducer = proxyHTTPServer.getProducerManager().getProxyProducer(producerGroup); - - if (!proxyProducer.getStarted().get()) { - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - sendMessageResponseHeader, - SendMessageResponseBody.buildBody(ProxyRetCode.PROXY_GROUP_PRODUCER_STOPED_ERR.getRetCode(), ProxyRetCode.PROXY_GROUP_PRODUCER_STOPED_ERR.getErrMsg())); - asyncContext.onComplete(responseProxyCommand); - return; - } - - String ttl = String.valueOf(ProxyConstants.DEFAULT_MSG_TTL_MILLS); - if (StringUtils.isNotBlank(sendMessageRequestBody.getTtl()) && StringUtils.isNumeric(sendMessageRequestBody.getTtl())) { - ttl = sendMessageRequestBody.getTtl(); - } - - Message rocketMQMsg; - try { - if (StringUtils.isBlank(sendMessageRequestBody.getTag())) { - rocketMQMsg = new Message(sendMessageRequestBody.getTopic(), - sendMessageRequestBody.getContent().getBytes(ProxyConstants.DEFAULT_CHARSET)); - } else { - rocketMQMsg = new Message(sendMessageRequestBody.getTopic(), sendMessageRequestBody.getTag(), - sendMessageRequestBody.getContent().getBytes(ProxyConstants.DEFAULT_CHARSET)); - } - rocketMQMsg.putUserProperty(DeFiBusConstant.KEY, DeFiBusConstant.PERSISTENT); - MessageAccessor.putProperty(rocketMQMsg, DeFiBusConstant.PROPERTY_MESSAGE_TTL, ttl); -// rocketMQMsg.putUserProperty(DeFiBusConstant.PROPERTY_MESSAGE_TTL, ttl); - rocketMQMsg.putUserProperty(ProxyConstants.REQ_C2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); - rocketMQMsg.putUserProperty(Constants.RMB_UNIQ_ID, sendMessageRequestBody.getUniqueId()); - rocketMQMsg.setKeys(sendMessageRequestBody.getBizSeqNo()); - rocketMQMsg.putUserProperty(DeFiBusConstant.PROPERTY_MESSAGE_REPLY_TO, - proxyProducer.getMqProducerWrapper().getDefaultMQProducer().buildMQClientId()); - - if (messageLogger.isDebugEnabled()) { - messageLogger.debug("msg2MQMsg suc, bizSeqNo={}, topic={}", sendMessageRequestBody.getBizSeqNo(), - sendMessageRequestBody.getTopic()); - } - rocketMQMsg.putUserProperty(ProxyConstants.REQ_PROXY2MQ_TIMESTAMP, String.valueOf(System.currentTimeMillis())); - } catch (Exception e) { - messageLogger.error("msg2MQMsg err, bizSeqNo={}, topic={}", sendMessageRequestBody.getBizSeqNo(), - sendMessageRequestBody.getTopic(), e); - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - sendMessageResponseHeader, - SendMessageResponseBody.buildBody(ProxyRetCode.PROXY_PACKAGE_MSG_ERR.getRetCode(), ProxyRetCode.PROXY_PACKAGE_MSG_ERR.getErrMsg() + ProxyUtil.stackTrace(e, 2))); - asyncContext.onComplete(responseProxyCommand); - return; - } - - final SendMessageContext sendMessageContext = new SendMessageContext(sendMessageRequestBody.getBizSeqNo(), rocketMQMsg, proxyProducer, proxyHTTPServer); - proxyHTTPServer.metrics.summaryMetrics.recordSendMsg(); - - long startTime = System.currentTimeMillis(); - - final CompleteHandler handler = new CompleteHandler() { - @Override - public void onResponse(HttpCommand httpCommand) { - try { - if (httpLogger.isDebugEnabled()) { - httpLogger.debug("{}", httpCommand); - } - proxyHTTPServer.sendResponse(ctx, httpCommand.httpResponse()); - proxyHTTPServer.metrics.summaryMetrics.recordHTTPReqResTimeCost(System.currentTimeMillis() - asyncContext.getRequest().getReqTime()); - } catch (Exception ex) { - } - } - }; - - LiteMessage liteMessage = new LiteMessage(sendMessageRequestBody.getBizSeqNo(), - sendMessageRequestBody.getUniqueId(), sendMessageRequestBody.getTopic(), - sendMessageRequestBody.getContent()) - .setProp(sendMessageRequestBody.getExtFields()); - - try { - proxyProducer.request(sendMessageContext, new SendCallback() { - @Override - public void onSuccess(SendResult sendResult) { - long endTime = System.currentTimeMillis(); - proxyHTTPServer.metrics.summaryMetrics.recordSendMsgCost(endTime - startTime); - messageLogger.info("message|proxy2mq|REQ|SYNC|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", - endTime - startTime, - sendMessageRequestBody.getTopic(), - sendMessageRequestBody.getBizSeqNo(), - sendMessageRequestBody.getUniqueId()); - } - - @Override - public void onException(Throwable e) { - HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( - sendMessageResponseHeader, - SendMessageResponseBody.buildBody(ProxyRetCode.PROXY_SEND_SYNC_MSG_ERR.getRetCode(), - ProxyRetCode.PROXY_SEND_SYNC_MSG_ERR.getErrMsg() + ProxyUtil.stackTrace(e, 2))); - asyncContext.onComplete(err, handler); - long endTime = System.currentTimeMillis(); - proxyHTTPServer.metrics.summaryMetrics.recordSendMsgFailed(); - proxyHTTPServer.metrics.summaryMetrics.recordSendMsgCost(endTime - startTime); - messageLogger.error("message|proxy2mq|REQ|SYNC|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", - endTime - startTime, - sendMessageRequestBody.getTopic(), - sendMessageRequestBody.getBizSeqNo(), - sendMessageRequestBody.getUniqueId(), e); - } - }, new RRCallback() { - @Override - public void onSuccess(Message mqMsg) { - if (mqMsg instanceof MessageExt) { - mqMsg.putUserProperty(ProxyConstants.BORN_TIMESTAMP, String.valueOf(((MessageExt) mqMsg) - .getBornTimestamp())); - mqMsg.putUserProperty(ProxyConstants.STORE_TIMESTAMP, String.valueOf(((MessageExt) mqMsg) - .getStoreTimestamp())); - } - mqMsg.putUserProperty(ProxyConstants.RSP_MQ2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); - messageLogger.info("message|mq2proxy|RSP|SYNC|rrCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", - System.currentTimeMillis() - startTime, - sendMessageRequestBody.getTopic(), - sendMessageRequestBody.getBizSeqNo(), - sendMessageRequestBody.getUniqueId()); - - try { - final String rtnMsg = new String(mqMsg.getBody(), ProxyConstants.DEFAULT_CHARSET); - mqMsg.putUserProperty(ProxyConstants.RSP_PROXY2C_TIMESTAMP, String.valueOf(System.currentTimeMillis())); - HttpCommand succ = asyncContext.getRequest().createHttpCommandResponse( - sendMessageResponseHeader, - SendMessageResponseBody.buildBody(ProxyRetCode.SUCCESS.getRetCode(), - JSON.toJSONString(new SendMessageResponseBody.ReplyMessage(mqMsg.getTopic(), rtnMsg, - mqMsg.getProperties())))); - asyncContext.onComplete(succ, handler); - } catch (Exception ex) { - HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( - sendMessageResponseHeader, - SendMessageResponseBody.buildBody(ProxyRetCode.PROXY_WAITING_RR_MSG_ERR.getRetCode(), - ProxyRetCode.PROXY_WAITING_RR_MSG_ERR.getErrMsg() + ProxyUtil.stackTrace(ex, 2))); - asyncContext.onComplete(err, handler); - messageLogger.warn("message|mq2proxy|RSP", ex); - } - } - - @Override - public void onException(Throwable e) { - HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( - sendMessageResponseHeader, - SendMessageResponseBody.buildBody(ProxyRetCode.PROXY_WAITING_RR_MSG_ERR.getRetCode(), - ProxyRetCode.PROXY_WAITING_RR_MSG_ERR.getErrMsg() + ProxyUtil.stackTrace(e, 2))); - asyncContext.onComplete(err, handler); - messageLogger.error("message|mq2proxy|RSP|SYNC|rrCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", - System.currentTimeMillis() - startTime, - sendMessageRequestBody.getTopic(), - sendMessageRequestBody.getBizSeqNo(), - sendMessageRequestBody.getUniqueId(), e); - } - }, Integer.valueOf(ttl)); - } catch (Exception ex) { - HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( - sendMessageResponseHeader, - SendMessageResponseBody.buildBody(ProxyRetCode.PROXY_SEND_SYNC_MSG_ERR.getRetCode(), - ProxyRetCode.PROXY_SEND_SYNC_MSG_ERR.getErrMsg() + ProxyUtil.stackTrace(ex, 2))); - asyncContext.onComplete(err); - long endTime = System.currentTimeMillis(); - proxyHTTPServer.metrics.summaryMetrics.recordSendMsgFailed(); - proxyHTTPServer.metrics.summaryMetrics.recordSendMsgCost(endTime - startTime); - messageLogger.error("message|proxy2mq|REQ|SYNC|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", - endTime - startTime, - sendMessageRequestBody.getTopic(), - sendMessageRequestBody.getBizSeqNo(), - sendMessageRequestBody.getUniqueId(), ex); - } - - return; - } - - @Override - public boolean rejectRequest() { - return false; - } -} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/SubscribeProcessor.java b/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/SubscribeProcessor.java deleted file mode 100644 index 2a923d59ff..0000000000 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/SubscribeProcessor.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.emesher.core.protocol.http.processor; - -import com.webank.emesher.boot.ProxyHTTPServer; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.protocol.http.async.AsyncContext; -import com.webank.emesher.core.protocol.http.processor.inf.HttpRequestProcessor; -import com.webank.eventmesh.common.IPUtil; -import com.webank.eventmesh.common.command.HttpCommand; -import com.webank.eventmesh.common.protocol.http.common.RequestCode; -import io.netty.channel.ChannelHandlerContext; -import org.apache.rocketmq.remoting.common.RemotingHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class SubscribeProcessor implements HttpRequestProcessor { - - public Logger cmdLogger = LoggerFactory.getLogger("cmd"); - - private ProxyHTTPServer proxyHTTPServer; - - public SubscribeProcessor(ProxyHTTPServer proxyHTTPServer) { - this.proxyHTTPServer = proxyHTTPServer; - } - - @Override - public void processRequest(ChannelHandlerContext ctx, AsyncContext asyncContext) throws Exception { - - cmdLogger.info("cmd={}|{}|client2proxy|from={}|to={}", RequestCode.get(Integer.valueOf(asyncContext.getRequest().getRequestCode())), - ProxyConstants.PROTOCOL_HTTP, - RemotingHelper.parseChannelRemoteAddr(ctx.channel()), IPUtil.getLocalAddress()); - - } - - @Override - public boolean rejectRequest() { - return false; - } -} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/UnSubscribeProcessor.java b/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/UnSubscribeProcessor.java deleted file mode 100644 index a25507ffdd..0000000000 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/UnSubscribeProcessor.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.emesher.core.protocol.http.processor; - -import com.webank.emesher.boot.ProxyHTTPServer; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.protocol.http.async.AsyncContext; -import com.webank.emesher.core.protocol.http.processor.inf.HttpRequestProcessor; -import com.webank.eventmesh.common.IPUtil; -import com.webank.eventmesh.common.command.HttpCommand; -import com.webank.eventmesh.common.protocol.http.common.RequestCode; -import io.netty.channel.ChannelHandlerContext; -import org.apache.rocketmq.remoting.common.RemotingHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class UnSubscribeProcessor implements HttpRequestProcessor { - - public Logger cmdLogger = LoggerFactory.getLogger("cmd"); - - private ProxyHTTPServer proxyHTTPServer; - - public UnSubscribeProcessor(ProxyHTTPServer proxyHTTPServer) { - this.proxyHTTPServer = proxyHTTPServer; - } - - @Override - public void processRequest(ChannelHandlerContext ctx, AsyncContext asyncContext) throws Exception { - - cmdLogger.info("cmd={}|{}|client2proxy|from={}|to={}", RequestCode.get(Integer.valueOf(asyncContext.getRequest().getRequestCode())), - ProxyConstants.PROTOCOL_HTTP, - RemotingHelper.parseChannelRemoteAddr(ctx.channel()), IPUtil.getLocalAddress()); - - } - - @Override - public boolean rejectRequest() { - return false; - } -} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/group/ClientGroupWrapper.java b/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/group/ClientGroupWrapper.java deleted file mode 100644 index fb99000985..0000000000 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/group/ClientGroupWrapper.java +++ /dev/null @@ -1,651 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.emesher.core.protocol.tcp.client.group; - -import com.alibaba.fastjson.JSON; -import com.webank.defibus.client.common.DeFiBusClientConfig; -import com.webank.defibus.client.impl.producer.RRCallback; -import com.webank.emesher.boot.ProxyTCPServer; -import com.webank.emesher.configuration.AccessConfiguration; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.plugin.MQConsumerWrapper; -import com.webank.emesher.core.plugin.MQProducerWrapper; -import com.webank.emesher.core.protocol.tcp.client.group.dispatch.DownstreamDispatchStrategy; -import com.webank.emesher.core.protocol.tcp.client.session.Session; -import com.webank.emesher.core.protocol.tcp.client.session.push.DownStreamMsgContext; -import com.webank.emesher.core.protocol.tcp.client.session.push.retry.ProxyTcpRetryer; -import com.webank.emesher.core.protocol.tcp.client.session.send.UpStreamMsgContext; -import com.webank.emesher.metrics.tcp.ProxyTcpMonitor; -import com.webank.emesher.patch.ProxyConsumeConcurrentlyContext; -import com.webank.emesher.patch.ProxyConsumeConcurrentlyStatus; -import com.webank.emesher.patch.ProxyMessageListenerConcurrently; -import com.webank.emesher.util.ProxyUtil; -import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.collections4.MapUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.rocketmq.client.exception.MQBrokerException; -import org.apache.rocketmq.client.exception.MQClientException; -import org.apache.rocketmq.client.producer.SendCallback; -import org.apache.rocketmq.client.producer.SendResult; -import org.apache.rocketmq.common.message.MessageExt; -import org.apache.rocketmq.common.utils.HttpTinyClient; -import org.apache.rocketmq.remoting.exception.RemotingException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.*; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.locks.ReadWriteLock; -import java.util.concurrent.locks.ReentrantReadWriteLock; - -public class ClientGroupWrapper { - - public static Logger logger = LoggerFactory.getLogger(ClientGroupWrapper.class); - - private String groupName; - - private String sysId; - - private String dcn; - - private AccessConfiguration accessConfiguration; - - private ProxyTCPServer proxyTCPServer; - - private ProxyTcpRetryer proxyTcpRetryer; - - private ProxyTcpMonitor proxyTcpMonitor; - - private DownstreamDispatchStrategy downstreamDispatchStrategy; - - private final ReadWriteLock groupLock = new ReentrantReadWriteLock(); - - public Set groupConsumerSessions = new HashSet(); - - public Set groupProducerSessions = new HashSet(); - - public AtomicBoolean started4Persistent = new AtomicBoolean(Boolean.FALSE); - - public AtomicBoolean started4Broadcast = new AtomicBoolean(Boolean.FALSE); - - public AtomicBoolean inited4Persistent = new AtomicBoolean(Boolean.FALSE); - - public AtomicBoolean inited4Broadcast = new AtomicBoolean(Boolean.FALSE); - - private MQConsumerWrapper persistentMsgConsumer = new MQConsumerWrapper(); - - private MQConsumerWrapper broadCastMsgConsumer = new MQConsumerWrapper(); - - private ConcurrentHashMap> topic2sessionInGroupMapping = new ConcurrentHashMap>(); - - private ConcurrentHashMap downstreamMap = new ConcurrentHashMap(); - - public ConcurrentHashMap getDownstreamMap() { - return downstreamMap; - } - - public AtomicBoolean producerStarted = new AtomicBoolean(Boolean.FALSE); - - public ConcurrentHashMap> getTopic2sessionInGroupMapping() { - return topic2sessionInGroupMapping; - } - - public boolean hasSubscription(String topic) { - boolean has = false; - try { - this.groupLock.readLock().lockInterruptibly(); - has = topic2sessionInGroupMapping.containsKey(topic); - } catch (Exception e) { - logger.error("hasSubscription error! topic[{}]", topic); - } finally { - this.groupLock.readLock().unlock(); - } - - return has; - } - - public boolean send(UpStreamMsgContext upStreamMsgContext, SendCallback sendCallback) throws Exception { - mqProducerWrapper.send(upStreamMsgContext.getMsg(), sendCallback); - return true; - } - - public void request(UpStreamMsgContext upStreamMsgContext, SendCallback sendCallback, RRCallback rrCallback, long timeout) - throws InterruptedException, RemotingException, MQClientException, MQBrokerException { - mqProducerWrapper.request(upStreamMsgContext.getMsg(), sendCallback, rrCallback, timeout); - } - - public boolean reply(UpStreamMsgContext upStreamMsgContext) throws Exception { - mqProducerWrapper.reply(upStreamMsgContext.getMsg(), new SendCallback() { - @Override - public void onSuccess(SendResult sendResult) { - - } - - @Override - public void onException(Throwable e) { - String bizSeqNo = upStreamMsgContext.getMsg().getKeys(); - logger.error("reply err! topic:{}, bizSeqNo:{}, client:{}", upStreamMsgContext.getMsg().getTopic(), bizSeqNo, upStreamMsgContext.getSession().getClient(), e); - } - }); - return true; - } - - private MQProducerWrapper mqProducerWrapper = new MQProducerWrapper(); - - public MQProducerWrapper getMqProducerWrapper() { - return mqProducerWrapper; - } - - public boolean addSubscription(String topic, Session session) throws Exception { - if (session == null - || !StringUtils.equalsIgnoreCase(groupName, ProxyUtil.buildClientGroup(session.getClient().getSubsystem(), session.getClient().getDcn()))) { - logger.error("addSubscription param error,topic:{},session:{}",topic, session); - return false; - } - - boolean r = false; - try { - this.groupLock.writeLock().lockInterruptibly(); - if (!topic2sessionInGroupMapping.containsKey(topic)) { - Set sessions = new HashSet(); - topic2sessionInGroupMapping.put(topic, sessions); - } - r = topic2sessionInGroupMapping.get(topic).add(session); - if(r){ - logger.info("addSubscription success, group:{} topic:{} client:{}", groupName, topic, session.getClient()); - }else{ - logger.warn("addSubscription fail, group:{} topic:{} client:{}", groupName, topic, session.getClient()); - } - } catch (Exception e) { - logger.error("addSubscription error! topic:{} client:{}", topic, session.getClient(), e); - throw new Exception("addSubscription fail"); - } finally { - this.groupLock.writeLock().unlock(); - } - return r; - } - - public boolean removeSubscription(String topic, Session session) { - if (session == null - || !StringUtils.equalsIgnoreCase(groupName, ProxyUtil.buildClientGroup(session.getClient().getSubsystem(), session.getClient().getDcn()))) { - logger.error("removeSubscription param error,topic:{},session:{}",topic, session); - return false; - } - - boolean r = false; - try { - this.groupLock.writeLock().lockInterruptibly(); - if (topic2sessionInGroupMapping.containsKey(topic)) { - r = topic2sessionInGroupMapping.get(topic).remove(session); - if(r){ - logger.info("removeSubscription remove session success, group:{} topic:{} client:{}", groupName, topic, session.getClient()); - }else{ - logger.warn("removeSubscription remove session failed, group:{} topic:{} client:{}", groupName, topic, session.getClient()); - } - } - if (CollectionUtils.size(topic2sessionInGroupMapping.get(topic)) == 0) { - topic2sessionInGroupMapping.remove(topic); - logger.info("removeSubscription remove topic success, group:{} topic:{}", groupName, topic); - } - } catch (Exception e) { - logger.error("removeSubscription error! topic:{} client:{}", topic, session.getClient(), e); - } finally { - this.groupLock.writeLock().unlock(); - } - return r; - } - - public synchronized void startClientGroupProducer() throws Exception { - if (producerStarted.get()) { - return; - } - - mqProducerWrapper.init(accessConfiguration , groupName); - mqProducerWrapper.getDefaultMQProducer().setInstanceName(ProxyUtil.buildProxyTcpClientID(sysId, dcn, "PUB", accessConfiguration.proxyCluster));//set instance name - mqProducerWrapper.start(); - producerStarted.compareAndSet(false, true); - logger.info("starting producer success, group:{}", groupName); - } - - public synchronized void shutdownProducer() throws Exception { - if (!producerStarted.get()) { - return; - } - mqProducerWrapper.shutdown(); - producerStarted.compareAndSet(true, false); - logger.info("shutdown producer success for group:{}", groupName); - } - - public ClientGroupWrapper(String sysId, String dcn, - ProxyTCPServer proxyTCPServer, - DownstreamDispatchStrategy downstreamDispatchStrategy) { - this.sysId = sysId; - this.dcn = dcn; - this.proxyTCPServer = proxyTCPServer; - this.accessConfiguration = proxyTCPServer.getAccessConfiguration(); - this.proxyTcpRetryer = proxyTCPServer.getProxyTcpRetryer(); - this.proxyTcpMonitor = proxyTCPServer.getProxyTcpMonitor(); - this.groupName = ProxyUtil.buildClientGroup(sysId, dcn); - this.downstreamDispatchStrategy = downstreamDispatchStrategy; - } - - public String getGroupName() { - return groupName; - } - - public boolean addGroupConsumerSession(Session session) { - if (session == null - || !StringUtils.equalsIgnoreCase(groupName, ProxyUtil.buildClientGroup(session.getClient().getSubsystem(), session.getClient().getDcn()))) { - logger.error("addGroupConsumerSession param error,session:{}", session); - return false; - } - - boolean r = false; - try { - this.groupLock.writeLock().lockInterruptibly(); - r = groupConsumerSessions.add(session); - if (r) { - logger.info("addGroupConsumerSession success, group:{} client:{}", groupName, session.getClient()); - } - } catch (Exception e) { - logger.error("addGroupConsumerSession error! group:{} client:{}", groupName, session.getClient(), e); - } finally { - this.groupLock.writeLock().unlock(); - } - return r; - } - - public boolean addGroupProducerSession(Session session) { - if (session == null - || !StringUtils.equalsIgnoreCase(groupName, ProxyUtil.buildClientGroup(session.getClient().getSubsystem(), session.getClient().getDcn()))) { - logger.error("addGroupProducerSession param error,session:{}", session); - return false; - } - - boolean r = false; - try { - this.groupLock.writeLock().lockInterruptibly(); - r = groupProducerSessions.add(session); - if (r) { - logger.info("addGroupProducerSession success, group:{} client:{}", groupName, session.getClient()); - } - } catch (Exception e) { - logger.error("addGroupProducerSession error! group:{} client:{}", groupName, session.getClient(), e); - } finally { - this.groupLock.writeLock().unlock(); - } - return r; - } - - public boolean removeGroupConsumerSession(Session session) { - if (session == null - || !StringUtils.equalsIgnoreCase(groupName, ProxyUtil.buildClientGroup(session.getClient().getSubsystem(), session.getClient().getDcn()))) { - logger.error("removeGroupConsumerSession param error,session:{}", session); - return false; - } - - boolean r = false; - try { - this.groupLock.writeLock().lockInterruptibly(); - r = groupConsumerSessions.remove(session); - if (r) { - logger.info("removeGroupConsumerSession success, group:{} client:{}", groupName, session.getClient()); - } - } catch (Exception e) { - logger.error("removeGroupConsumerSession error! group:{} client:{}", groupName, session.getClient(), e); - } finally { - this.groupLock.writeLock().unlock(); - } - return r; - } - - public boolean removeGroupProducerSession(Session session) { - if (session == null - || !StringUtils.equalsIgnoreCase(groupName, ProxyUtil.buildClientGroup(session.getClient().getSubsystem(), session.getClient().getDcn()))) { - logger.error("removeGroupProducerSession param error,session:{}", session); - return false; - } - - boolean r = false; - try { - this.groupLock.writeLock().lockInterruptibly(); - r = groupProducerSessions.remove(session); - if (r) { - logger.info("removeGroupProducerSession success, group:{} client:{}", groupName, session.getClient()); - } - } catch (Exception e) { - logger.error("removeGroupProducerSession error! group:{} client:{}", groupName, session.getClient(), e); - } finally { - this.groupLock.writeLock().unlock(); - } - - return r; - } - - public synchronized void initClientGroupPersistentConsumer() throws Exception { - if(inited4Persistent.get()){ - return; - } - - persistentMsgConsumer.init(false, accessConfiguration, groupName); - persistentMsgConsumer.setInstanceName(ProxyUtil.buildProxyTcpClientID(sysId, dcn, "SUB", accessConfiguration.proxyCluster)); - persistentMsgConsumer.registerMessageListener(new ProxyMessageListenerConcurrently() { - - @Override - public ProxyConsumeConcurrentlyStatus handleMessage(MessageExt msg, ProxyConsumeConcurrentlyContext context) { - - if (msg == null) - return ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS; - - proxyTcpMonitor.getMq2proxyMsgNum().incrementAndGet(); - String topic = msg.getTopic(); - msg.putUserProperty(ProxyConstants.REQ_MQ2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); - msg.putUserProperty(ProxyConstants.REQ_RECEIVE_PROXY_IP, accessConfiguration.proxyServerIp); - msg.putUserProperty(ProxyConstants.BORN_TIMESTAMP, String.valueOf(msg.getBornTimestamp())); - msg.putUserProperty(ProxyConstants.STORE_TIMESTAMP, String.valueOf(msg.getStoreTimestamp())); - - if (!ProxyUtil.isValidRMBTopic(topic)) { - return ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS; - } - - Session session = downstreamDispatchStrategy.select(groupName, topic, groupConsumerSessions); - String bizSeqNo = ProxyUtil.getMessageBizSeq(msg); - if(session == null){ - try { - Integer sendBackTimes = MapUtils.getInteger(msg.getProperties(), ProxyConstants.PROXY_SEND_BACK_TIMES, new Integer(0)); - String sendBackFromProxyIp = MapUtils.getString(msg.getProperties(), ProxyConstants.PROXY_SEND_BACK_IP, ""); - logger.error("found no session to downstream msg,groupName:{}, topic:{}, bizSeqNo:{}", groupName, topic, bizSeqNo); - - if (sendBackTimes >= proxyTCPServer.getAccessConfiguration().proxyTcpSendBackMaxTimes) { - logger.error("sendBack to broker over max times:{}, groupName:{}, topic:{}, bizSeqNo:{}", proxyTCPServer.getAccessConfiguration().proxyTcpSendBackMaxTimes, groupName, topic, bizSeqNo); - } else { - sendBackTimes++; - msg.putUserProperty(ProxyConstants.PROXY_SEND_BACK_TIMES, sendBackTimes.toString()); - msg.putUserProperty(ProxyConstants.PROXY_SEND_BACK_IP, sendBackFromProxyIp); - sendMsgBackToBroker(msg, bizSeqNo); - } - } catch (Exception e){ - logger.warn("handle msg exception when no session found", e); - } - - return ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS; - } - - DownStreamMsgContext downStreamMsgContext = - new DownStreamMsgContext(msg, session, persistentMsgConsumer, (ProxyConsumeConcurrentlyContext)context, false); - - if(downstreamMap.size() < proxyTCPServer.getAccessConfiguration().proxyTcpDownStreamMapSize){ - downstreamMap.putIfAbsent(downStreamMsgContext.seq, downStreamMsgContext); - }else{ - logger.warn("downStreamMap is full,group:{}", groupName); - } - - if (session.isCanDownStream()) { - session.downstreamMsg(downStreamMsgContext); - return ProxyConsumeConcurrentlyStatus.CONSUME_FINISH; - } - - logger.warn("session is busy,dispatch retry,seq:{}, session:{}, bizSeq:{}", downStreamMsgContext.seq, downStreamMsgContext.session.getClient(), bizSeqNo); - long delayTime = ProxyUtil.isService(downStreamMsgContext.msgExt.getTopic()) ? 0 : proxyTCPServer.getAccessConfiguration().proxyTcpMsgRetryDelayInMills; - downStreamMsgContext.delay(delayTime); - proxyTcpRetryer.pushRetry(downStreamMsgContext); - - return ProxyConsumeConcurrentlyStatus.CONSUME_FINISH; - } - }); - inited4Persistent.compareAndSet(false, true); - logger.info("init persistentMsgConsumer success, group:{}", groupName); - } - - public synchronized void startClientGroupPersistentConsumer() throws Exception { - if (started4Persistent.get()) { - return; - } - persistentMsgConsumer.start(); - started4Persistent.compareAndSet(false, true); - logger.info("starting persistentMsgConsumer success, group:{}", groupName); - } - - public synchronized void initClientGroupBroadcastConsumer() throws Exception { - if(inited4Broadcast.get()){ - return; - } - broadCastMsgConsumer.init(true, accessConfiguration, groupName); - broadCastMsgConsumer.registerMessageListener(new ProxyMessageListenerConcurrently() { - @Override - public ProxyConsumeConcurrentlyStatus handleMessage(MessageExt msg, ProxyConsumeConcurrentlyContext context) { - if (msg == null) - return ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS; - - proxyTcpMonitor.getMq2proxyMsgNum().incrementAndGet(); - - String topic = msg.getTopic(); - - msg.putUserProperty(ProxyConstants.REQ_MQ2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); - msg.putUserProperty(ProxyConstants.REQ_RECEIVE_PROXY_IP, accessConfiguration.proxyServerIp); - msg.putUserProperty(ProxyConstants.BORN_TIMESTAMP, String.valueOf(msg.getBornTimestamp())); - msg.putUserProperty(ProxyConstants.STORE_TIMESTAMP, String.valueOf(msg.getStoreTimestamp())); - - if (!ProxyUtil.isValidRMBTopic(topic)) { - return ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS; - } - - if(CollectionUtils.isEmpty(groupConsumerSessions)){ - logger.warn("found no session to downstream broadcast msg"); - return ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS; - } - - Iterator sessionsItr = groupConsumerSessions.iterator(); - - while (sessionsItr.hasNext()) { - Session session = sessionsItr.next(); - - if (!session.isAvailable(topic)) { - logger.warn("downstream broadcast msg,session is not available,client:{}",session.getClient()); - continue; - } - - DownStreamMsgContext downStreamMsgContext = - new DownStreamMsgContext(msg, session, broadCastMsgConsumer, context, false); - - if (session.isCanDownStream()) { - session.downstreamMsg(downStreamMsgContext); - continue; - } - - logger.warn("downstream broadcast msg,session is busy,dispatch retry,seq:{}, session:{}, bizSeq:{}", downStreamMsgContext.seq, downStreamMsgContext.session.getClient(), ProxyUtil.getMessageBizSeq(downStreamMsgContext.msgExt)); - long delayTime = ProxyUtil.isService(downStreamMsgContext.msgExt.getTopic()) ? 0 : proxyTCPServer.getAccessConfiguration().proxyTcpMsgRetryDelayInMills; - downStreamMsgContext.delay(delayTime); - proxyTcpRetryer.pushRetry(downStreamMsgContext); - } - - return ProxyConsumeConcurrentlyStatus.CONSUME_FINISH; - } - }); - inited4Broadcast.compareAndSet(false, true); - logger.info("init broadCastMsgConsumer success, group:{}", groupName); - } - - public synchronized void startClientGroupBroadcastConsumer() throws Exception{ - if (started4Broadcast.get()) { - return; - } - broadCastMsgConsumer.start(); - started4Broadcast.compareAndSet(false, true); - logger.info("starting broadCastMsgConsumer success, group:{}", groupName); - } - - public void subscribe(String topic) throws Exception { - if (ProxyUtil.isBroadcast(topic)) { - broadCastMsgConsumer.subscribe(topic); - } else { - persistentMsgConsumer.subscribe(topic); - } - } - - public void unsubscribe(String topic) throws Exception { - if (ProxyUtil.isBroadcast(topic)) { - broadCastMsgConsumer.unsubscribe(topic); - } else { - persistentMsgConsumer.unsubscribe(topic); - } - } - - public synchronized void shutdownBroadCastConsumer() throws Exception { - if (started4Broadcast.get()) { - broadCastMsgConsumer.shutdown(); - logger.info("broadcast consumer group:{} shutdown...", groupName); - } - started4Broadcast.compareAndSet(true, false); - inited4Broadcast.compareAndSet(true, false); - broadCastMsgConsumer = null; - } - - public synchronized void shutdownPersistentConsumer() throws Exception { - - if (started4Persistent.get()) { - persistentMsgConsumer.shutdown(); - logger.info("persistent consumer group:{} shutdown...", groupName); - } - started4Persistent.compareAndSet(true, false); - inited4Persistent.compareAndSet(true,false); - persistentMsgConsumer = null; - } - - public Set getGroupConsumerSessions() { - Set res = null; - try { - this.groupLock.readLock().lockInterruptibly(); - res = groupConsumerSessions; - } catch (Exception e) { - } finally { - this.groupLock.readLock().unlock(); - } - return res; - } - - - public Set getGroupProducerSessions() { - Set res = null; - try { - this.groupLock.readLock().lockInterruptibly(); - res = groupProducerSessions; - } catch (Exception e) { - } finally { - this.groupLock.readLock().unlock(); - } - return res; - } - - public void setGroupName(String groupName) { - this.groupName = groupName; - } - - public AccessConfiguration getAccessConfiguration() { - return accessConfiguration; - } - - public void setAccessConfiguration(AccessConfiguration accessConfiguration) { - this.accessConfiguration = accessConfiguration; - } - - public ProxyTcpRetryer getProxyTcpRetryer() { - return proxyTcpRetryer; - } - - public void setProxyTcpRetryer(ProxyTcpRetryer proxyTcpRetryer) { - this.proxyTcpRetryer = proxyTcpRetryer; - } - - public ProxyTcpMonitor getProxyTcpMonitor() { - return proxyTcpMonitor; - } - - public void setProxyTcpMonitor(ProxyTcpMonitor proxyTcpMonitor) { - this.proxyTcpMonitor = proxyTcpMonitor; - } - - public DownstreamDispatchStrategy getDownstreamDispatchStrategy() { - return downstreamDispatchStrategy; - } - - public void setDownstreamDispatchStrategy(DownstreamDispatchStrategy downstreamDispatchStrategy) { - this.downstreamDispatchStrategy = downstreamDispatchStrategy; - } - - public String getSysId() { - return sysId; - } - - private String pushMsgToProxy(MessageExt msg, String ip, int port) { - StringBuilder targetUrl = new StringBuilder(); - targetUrl.append("http://").append(ip).append(":").append(port).append("/proxy/msg/push"); - HttpTinyClient.HttpResult result = null; - - try { - logger.info("pushMsgToProxy,targetUrl:{},msg:{}",targetUrl.toString(),msg.toString()); - List paramValues = new ArrayList(); - paramValues.add("msg"); - paramValues.add(JSON.toJSONString(msg)); - paramValues.add("group"); - paramValues.add(groupName); - - result = HttpTinyClient.httpPost( - targetUrl.toString(), - null, - paramValues, - "UTF-8", - 3000); - } catch (Exception e) { - throw new RuntimeException("httpPost " + targetUrl + " is fail," + e); - } - - if (200 == result.code && result.content != null) { - return result.content; - - } else { - throw new RuntimeException("httpPost targetUrl[" + targetUrl + "] is not OK when getContentThroughHttp, httpResult: " + result + "."); - } - } - - public MQConsumerWrapper getPersistentMsgConsumer() { - return persistentMsgConsumer; - } - - private void sendMsgBackToBroker(MessageExt msg, String bizSeqNo){ - try { - logger.warn("send msg back to broker, bizSeqno:{}, topic:{}",bizSeqNo, msg.getTopic()); - - send(new UpStreamMsgContext(null,null, msg), new SendCallback() { - @Override - public void onSuccess(SendResult sendResult) { - logger.info("consumerGroup:{} consume fail, sendMessageBack success, bizSeqno:{}, topic:{}", groupName, bizSeqNo, msg.getTopic()); - } - - @Override - public void onException(Throwable e) { - logger.warn("consumerGroup:{} consume fail, sendMessageBack fail, bizSeqno:{}, topic:{}", groupName, bizSeqNo, msg.getTopic()); - } - }); - proxyTcpMonitor.getProxy2mqMsgNum().incrementAndGet(); - }catch (Exception e){ - logger.warn("try send msg back to broker failed"); - } - } -} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/push/SessionPusher.java b/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/push/SessionPusher.java deleted file mode 100644 index 2a556504be..0000000000 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/push/SessionPusher.java +++ /dev/null @@ -1,149 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.emesher.core.protocol.tcp.client.session.push; - -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.protocol.tcp.client.session.Session; -import com.webank.eventmesh.common.protocol.tcp.AccessMessage; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Header; -import com.webank.eventmesh.common.protocol.tcp.OPStatus; -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.emesher.util.ProxyUtil; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; -import org.apache.rocketmq.common.message.MessageExt; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.ArrayList; -import java.util.List; - -public class SessionPusher { - - private final Logger messageLogger = LoggerFactory.getLogger("message"); - - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - - private Integer unack; - - private PushContext pushContext = new PushContext(this); - - public PushContext getPushContext() { - return pushContext; - } - - public boolean isBusy() { - return pushContext.getTotalUnackMsgs() >= Math.floor(3 * unack / 4); - } - - public boolean isCanDownStream() { - return pushContext.getTotalUnackMsgs() < unack; - } - - public void setPushContext(PushContext pushContext) { - this.pushContext = pushContext; - } - - private Session session; - - public SessionPusher(Session session) { - this.session = session; - unack = (0 == session.getClient().getUnack()) ? session.getAccessConfiguration().proxyTcpSessionDownstreamUnackSize : session.getClient().getUnack(); - } - - @Override - public String toString() { - return "SessionPusher{unack=" + unack - + ",busy=" + isBusy() - + ",canDownStream=" + isCanDownStream() - + ",pushContext=" + pushContext + "}"; - } - - public void push(final DownStreamMsgContext downStreamMsgContext) { - Command cmd; - if (ProxyUtil.isBroadcast(downStreamMsgContext.msgExt.getTopic())) { - cmd = Command.BROADCAST_MESSAGE_TO_CLIENT; - } else if (ProxyUtil.isService(downStreamMsgContext.msgExt.getTopic())) { - cmd = Command.REQUEST_TO_CLIENT; - } else { - cmd = Command.ASYNC_MESSAGE_TO_CLIENT; - } - - Package pkg = new Package(); - downStreamMsgContext.msgExt.putUserProperty(ProxyConstants.REQ_PROXY2C_TIMESTAMP, String.valueOf(System.currentTimeMillis())); - AccessMessage body = null; - int retCode = 0; - String retMsg = null; - try { - body = ProxyUtil.encodeMessage(downStreamMsgContext.msgExt); - pkg.setBody(body); - pkg.setHeader(new Header(cmd, OPStatus.SUCCESS.getCode(), null, downStreamMsgContext.seq)); - messageLogger.info("pkg|mq2proxy|cmd={}|mqMsg={}|user={}", cmd, ProxyUtil.printMqMessage(body), session.getClient()); - } catch (Exception e) { - pkg.setHeader(new Header(cmd, OPStatus.FAIL.getCode(), e.getStackTrace().toString(), downStreamMsgContext.seq)); - retCode = -1; - retMsg = e.toString(); - } finally { - session.getClientGroupWrapper().get().getProxyTcpMonitor().getProxy2clientMsgNum().incrementAndGet(); - pushContext.deliveredMsgCount(); - - //avoid ack arrives to server prior to callback of the method writeAndFlush,may cause ack problem - List msgExts = new ArrayList(); - msgExts.add(downStreamMsgContext.msgExt); - pushContext.unAckMsg(downStreamMsgContext.seq, - msgExts, - downStreamMsgContext.consumeConcurrentlyContext, - downStreamMsgContext.consumer); - - session.getContext().writeAndFlush(pkg).addListener( - new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (!future.isSuccess()) { - logger.error("downstreamMsg fail,seq:{}, retryTimes:{}, msg:{}", downStreamMsgContext.seq, downStreamMsgContext.retryTimes, downStreamMsgContext.msgExt); - pushContext.deliverFailMsgCount(); - - //push msg failed, remove the msg from unackMap - pushContext.getUnAckMsg().remove(downStreamMsgContext.seq); - - //how long to isolate client when push fail - long isolateTime = System.currentTimeMillis() + session.getAccessConfiguration().proxyTcpPushFailIsolateTimeInMills; - session.setIsolateTime(isolateTime); - logger.warn("isolate client:{},isolateTime:{}", session.getClient(), isolateTime); - - //retry - long delayTime = ProxyUtil.isService(downStreamMsgContext.msgExt.getTopic()) ? 0 : session.getAccessConfiguration().proxyTcpMsgRetryDelayInMills; - downStreamMsgContext.delay(delayTime); - session.getClientGroupWrapper().get().getProxyTcpRetryer().pushRetry(downStreamMsgContext); - } else { - pushContext.deliveredMsgCount(); - logger.info("downstreamMsg success,seq:{}, retryTimes:{}, bizSeq:{}", downStreamMsgContext.seq,downStreamMsgContext.retryTimes, ProxyUtil.getMessageBizSeq(downStreamMsgContext.msgExt)); - - session.getClientGroupWrapper().get().getDownstreamMap().remove(downStreamMsgContext.seq); - if(session.isIsolated()){ - logger.info("cancel isolated,client:{}", session.getClient()); - session.setIsolateTime(System.currentTimeMillis()); - } - } - } - } - ); - } - } -} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/metrics/tcp/ProxyTcpMonitor.java b/eventmesh-emesher/src/main/java/com/webank/emesher/metrics/tcp/ProxyTcpMonitor.java deleted file mode 100644 index 87075699bb..0000000000 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/metrics/tcp/ProxyTcpMonitor.java +++ /dev/null @@ -1,170 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.emesher.metrics.tcp; - -import com.webank.emesher.boot.ProxyTCPServer; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.protocol.tcp.client.ProxyTcpConnectionHandler; -import com.webank.emesher.core.protocol.tcp.client.session.Session; -import com.webank.emesher.metrics.MonitorMetricConstants; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.net.InetSocketAddress; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.atomic.AtomicLong; - -public class ProxyTcpMonitor { - - private ProxyTCPServer proxyTCPServer; - - private final Logger tcpLogger = LoggerFactory.getLogger("tcpMonitor"); - - private final Logger appLogger = LoggerFactory.getLogger("appMonitor"); - - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - - private static int delay = 60 * 1000; - - private static int period = 60 * 1000; - - private static int PRINT_THREADPOOLSTATE_INTERVAL = 1; - - private AtomicInteger client2proxyMsgNum; - private AtomicInteger proxy2mqMsgNum; - private AtomicInteger mq2proxyMsgNum; - private AtomicInteger proxy2clientMsgNum; - - private int client2proxyTPS; - private int proxy2clientTPS; - private int proxy2mqTPS; - private int mq2proxyTPS; - private int allTPS; - private int subTopicNum; - - public ScheduledFuture monitorTpsTask; - - public ScheduledFuture monitorThreadPoolTask; - - public ProxyTcpMonitor(ProxyTCPServer proxyTCPServer) { - this.proxyTCPServer = proxyTCPServer; - } - - public void init() throws Exception { - this.client2proxyMsgNum = new AtomicInteger(0); - this.proxy2mqMsgNum = new AtomicInteger(0); - this.mq2proxyMsgNum = new AtomicInteger(0); - this.proxy2clientMsgNum = new AtomicInteger(0); - logger.info("ProxyTcpMonitor inited......"); - } - - public void start() throws Exception { - monitorTpsTask = proxyTCPServer.scheduler.scheduleAtFixedRate((new Runnable() { - @Override - public void run() { - int msgNum = client2proxyMsgNum.intValue(); - client2proxyMsgNum = new AtomicInteger(0); - client2proxyTPS = 1000 * msgNum / period; - - msgNum = proxy2clientMsgNum.intValue(); - proxy2clientMsgNum = new AtomicInteger(0); - proxy2clientTPS = 1000 * msgNum / period; - - msgNum = proxy2mqMsgNum.intValue(); - proxy2mqMsgNum = new AtomicInteger(0); - proxy2mqTPS = 1000 * msgNum / period; - - msgNum = mq2proxyMsgNum.intValue(); - mq2proxyMsgNum = new AtomicInteger(0); - mq2proxyTPS = 1000 * msgNum / period; - - allTPS = client2proxyTPS + proxy2clientTPS; - - //count topics subscribed by client in this proxy - ConcurrentHashMap sessionMap = proxyTCPServer.getClientSessionGroupMapping().getSessionMap(); - Iterator sessionIterator = sessionMap.values().iterator(); - Set topicSet = new HashSet<>(); - while (sessionIterator.hasNext()) { - Session session = sessionIterator.next(); - AtomicLong deliveredMsgsCount = session.getPusher().getPushContext().deliveredMsgsCount; - AtomicLong deliveredFailCount = session.getPusher().getPushContext().deliverFailMsgsCount; - AtomicLong ackedMsgsCount = session.getPusher().getPushContext().ackedMsgsCount; - int unAckMsgsCount = session.getPusher().getPushContext().getTotalUnackMsgs(); - int sendTopics = session.getSessionContext().sendTopics.size(); - int subscribeTopics = session.getSessionContext().subscribeTopics.size(); - - tcpLogger.info("session|deliveredFailCount={}|deliveredMsgsCount={}|ackedMsgsCount={}|unAckMsgsCount={}|sendTopics={}|subscribeTopics={}|user={}", - deliveredFailCount.longValue(), deliveredMsgsCount.longValue(), ackedMsgsCount.longValue(), - unAckMsgsCount, sendTopics, subscribeTopics, session.getClient()); - - topicSet.addAll(session.getSessionContext().subscribeTopics.keySet()); - } - subTopicNum = topicSet.size(); - - appLogger.info(String.format(MonitorMetricConstants.PROXY_MONITOR_FORMAT_COMMON, ProxyConstants.PROTOCOL_TCP, MonitorMetricConstants.CLIENT_2_PROXY_TPS, client2proxyTPS)); - appLogger.info(String.format(MonitorMetricConstants.PROXY_MONITOR_FORMAT_COMMON, ProxyConstants.PROTOCOL_TCP, MonitorMetricConstants.PROXY_2_MQ_TPS, proxy2mqTPS)); - appLogger.info(String.format(MonitorMetricConstants.PROXY_MONITOR_FORMAT_COMMON, ProxyConstants.PROTOCOL_TCP, MonitorMetricConstants.MQ_2_PROXY_TPS, mq2proxyTPS)); - appLogger.info(String.format(MonitorMetricConstants.PROXY_MONITOR_FORMAT_COMMON, ProxyConstants.PROTOCOL_TCP, MonitorMetricConstants.PROXY_2_CLIENT_TPS, proxy2clientTPS)); - appLogger.info(String.format(MonitorMetricConstants.PROXY_MONITOR_FORMAT_COMMON, ProxyConstants.PROTOCOL_TCP, MonitorMetricConstants.ALL_TPS, allTPS)); - appLogger.info(String.format(MonitorMetricConstants.PROXY_MONITOR_FORMAT_COMMON, ProxyConstants.PROTOCOL_TCP, MonitorMetricConstants.CONNECTION, ProxyTcpConnectionHandler.connections)); - appLogger.info(String.format(MonitorMetricConstants.PROXY_MONITOR_FORMAT_COMMON, ProxyConstants.PROTOCOL_TCP, MonitorMetricConstants.SUB_TOPIC_NUM, subTopicNum)); - } - }), delay, period, TimeUnit.MILLISECONDS); - - monitorThreadPoolTask = proxyTCPServer.scheduler.scheduleAtFixedRate(new Runnable() { - @Override - public void run() { -// ThreadPoolHelper.printThreadPoolState(); - proxyTCPServer.getProxyTcpRetryer().printRetryThreadPoolState(); - - //monitor retry queue size - int retrySize = proxyTCPServer.getProxyTcpRetryer().getRetrySize(); - appLogger.info(String.format(MonitorMetricConstants.PROXY_MONITOR_FORMAT_COMMON, ProxyConstants.PROTOCOL_TCP, MonitorMetricConstants.RETRY_QUEUE_SIZE, retrySize)); - } - }, 10, PRINT_THREADPOOLSTATE_INTERVAL, TimeUnit.SECONDS); - logger.info("ProxyTcpMonitor started......"); - } - - public void shutdown() throws Exception { - monitorTpsTask.cancel(true); - monitorThreadPoolTask.cancel(true); - logger.info("ProxyTcpMonitor shutdown......"); - } - - public AtomicInteger getClient2proxyMsgNum() { - return client2proxyMsgNum; - } - - public AtomicInteger getProxy2mqMsgNum() { - return proxy2mqMsgNum; - } - - public AtomicInteger getMq2proxyMsgNum() { - return mq2proxyMsgNum; - } - - public AtomicInteger getProxy2clientMsgNum() { - return proxy2clientMsgNum; - } -} diff --git a/eventmesh-emesher/src/test/java/protocol/CmdAsync2ClientTest.java b/eventmesh-emesher/src/test/java/protocol/CmdAsync2ClientTest.java deleted file mode 100644 index 774f1325e0..0000000000 --- a/eventmesh-emesher/src/test/java/protocol/CmdAsync2ClientTest.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package protocol; - -import client.PubClient; -import client.common.Server; -import client.common.UserAgentUtils; -import client.hook.ReceiveMsgHook; -import client.impl.PubClientImpl; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Package; -import io.netty.channel.ChannelHandlerContext; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -/** - * Cmd Test: Async Message To Client - */ -public class CmdAsync2ClientTest { - - public static Server server = new Server(); - public static PubClient client = new PubClientImpl("127.0.0.1", 10000, UserAgentUtils.createSubUserAgent()); - - @BeforeClass - public static void before_Cmd_AsyncMessage() throws Exception { - server.startAccessServer(); - client.init(); - } - - @Test - public void test_Cmd_AsyncMessage() throws Exception { - client.registerBusiHandler(new ReceiveMsgHook() { - @Override - public void handle(Package msg, ChannelHandlerContext ctx) { - System.err.println("receive response from server:------------------" + msg.toString()); - if (msg.getHeader().getCommand() == Command.CLIENT_GOODBYE_RESPONSE) { - Assert.assertTrue("HEARTBEAT_RESPONSE", true); - } - } - }); - Package msg = new Package(); - client.publish(msg, 3000); -// Thread.sleep(10000); - } - - @AfterClass - public static void after_Cmd_AsyncMessage() throws Exception { -// server.shutdownAccessServer(); -// client.close(); - } -} diff --git a/eventmesh-emesher/src/test/java/protocol/CmdAsync2ServerTest.java b/eventmesh-emesher/src/test/java/protocol/CmdAsync2ServerTest.java deleted file mode 100644 index 7c3b1ae979..0000000000 --- a/eventmesh-emesher/src/test/java/protocol/CmdAsync2ServerTest.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package protocol; - -import client.PubClient; -import client.common.MessageUtils; -import client.common.Server; -import client.common.UserAgentUtils; -import client.impl.PubClientImpl; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Package; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -public class CmdAsync2ServerTest { - - public static Server server = new Server(); - public static PubClient client = new PubClientImpl("127.0.0.1", 10000, UserAgentUtils.createSubUserAgent()); - - @BeforeClass - public static void before_Cmd_AsyncMessage2Server() throws Exception { - server.startAccessServer(); - client.init(); - Thread.sleep(1000); - } - - @Test - public void test_Cmd_AsyncMessage2Server() throws Exception { - Package msg = MessageUtils.asyncMessage("FT0-e-80010000-01-1", 0); - Package replyMsg = client.publish(msg, 3000); - System.err.println("Reply Message-----------------------------------------" + replyMsg.toString()); - if (replyMsg.getHeader().getCommand() != Command.ASYNC_MESSAGE_TO_SERVER_ACK) { - Assert.assertTrue("ASYNC_MESSAGE_TO_SERVER_ACK FAIL", false); - } - } - - @AfterClass - public static void after_Cmd_AsyncMessage2Server() throws Exception { -// server.shutdownAccessServer(); -// client.close(); - } -} diff --git a/eventmesh-emesher/src/test/java/protocol/CmdBroadCast2ClientTest.java b/eventmesh-emesher/src/test/java/protocol/CmdBroadCast2ClientTest.java deleted file mode 100644 index ddd2afbcfb..0000000000 --- a/eventmesh-emesher/src/test/java/protocol/CmdBroadCast2ClientTest.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package protocol; - -import client.SubClient; -import client.common.Server; -import client.common.UserAgentUtils; -import client.hook.ReceiveMsgHook; -import client.impl.SubClientImpl; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Package; -import io.netty.channel.ChannelHandlerContext; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -/** - * Cmd Test: BroadCast To Client - */ -public class CmdBroadCast2ClientTest { - - public static Server server = new Server(); - public static SubClient client = new SubClientImpl("127.0.0.1", 10000, UserAgentUtils.createSubUserAgent()); - - @BeforeClass - public static void before_Cmd_broadcast() throws Exception { - server.startAccessServer(); - client.init(); - } - - @Test - public void test_Cmd_broadcast() throws Exception { - client.registerBusiHandler(new ReceiveMsgHook() { - @Override - public void handle(Package msg, ChannelHandlerContext ctx) { - System.err.println("receive response from server:------------------" + msg.toString()); - if (msg.getHeader().getCommand() == Command.BROADCAST_MESSAGE_TO_CLIENT) { - Assert.assertTrue("HEARTBEAT_RESPONSE", true); - } - } - }); - Package msg = new Package(); -// client.publish(msg,3000); -// Thread.sleep(10000); - } - - @AfterClass - public static void after_Cmd_broadcast() throws Exception { -// server.shutdownAccessServer(); -// client.close(); - } - -} diff --git a/eventmesh-emesher/src/test/java/protocol/CmdBroadCast2ServerTest.java b/eventmesh-emesher/src/test/java/protocol/CmdBroadCast2ServerTest.java deleted file mode 100644 index 928a76fed0..0000000000 --- a/eventmesh-emesher/src/test/java/protocol/CmdBroadCast2ServerTest.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package protocol; - -import client.PubClient; -import client.common.MessageUtils; -import client.common.Server; -import client.common.UserAgentUtils; -import client.impl.PubClientImpl; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Package; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -public class CmdBroadCast2ServerTest { - - public static Server server = new Server(); - public static PubClient client = new PubClientImpl("127.0.0.1", 10000, UserAgentUtils.createSubUserAgent()); - - @BeforeClass - public static void before_Cmd_broadcast2Server() throws Exception { - server.startAccessServer(); - client.init(); - Thread.sleep(1000); - } - - @Test - public void test_Cmd_broadcast2Server() throws Exception { - Package msg = MessageUtils.broadcastMessage("FT0-e-80030000-01-3", 0); - Package replyMsg = client.broadcast(msg, 3000); - System.err.println("Reply Message-----------------------------------------" + replyMsg.toString()); - if (replyMsg.getHeader().getCommand() != Command.BROADCAST_MESSAGE_TO_SERVER_ACK) { - Assert.assertTrue("BROADCAST_MESSAGE_TO_SERVER_ACK FAIL", false); - } - } - - @AfterClass - public static void after_Cmd_broadcast2Server() throws Exception { -// server.shutdownAccessServer(); -// client.close(); - } - -} diff --git a/eventmesh-emesher/src/test/java/protocol/CmdClientGoodByTest.java b/eventmesh-emesher/src/test/java/protocol/CmdClientGoodByTest.java deleted file mode 100644 index b0a3d51de5..0000000000 --- a/eventmesh-emesher/src/test/java/protocol/CmdClientGoodByTest.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package protocol; - -import client.SubClient; -import client.common.Server; -import client.common.UserAgentUtils; -import client.impl.SubClientImpl; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Package; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -/** - * Test Cmd: CLIENT_GOODBYE_REQUEST - */ -public class CmdClientGoodByTest { - public static Server server = new Server(); - public static SubClient client = new SubClientImpl("127.0.0.1", 10000, UserAgentUtils.createSubUserAgent()); - - @BeforeClass - public static void before_Cmd_client_goodbye() throws Exception { - server.startAccessServer(); - client.init(); - Thread.sleep(1000); - } - - @Test - public void test_Cmd_client_goodbye() throws Exception { - Package replyMsg = client.goodbye(); - System.err.println("Reply Message-----------------------------------------" + replyMsg.toString()); - if (replyMsg.getHeader().getCommand() != Command.CLIENT_GOODBYE_RESPONSE) { - Assert.assertTrue("CLIENT_GOODBYE_RESPONSE FAIL", false); - } - } - - @AfterClass - public static void after_Cmd_client_goodbye() throws Exception { -// server.shutdownAccessServer(); -// client.close(); - } -} diff --git a/eventmesh-emesher/src/test/java/protocol/CmdHeartbeatTest.java b/eventmesh-emesher/src/test/java/protocol/CmdHeartbeatTest.java deleted file mode 100644 index 978abbd159..0000000000 --- a/eventmesh-emesher/src/test/java/protocol/CmdHeartbeatTest.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package protocol; - -import client.PubClient; -import client.common.Server; -import client.common.UserAgentUtils; -import client.hook.ReceiveMsgHook; -import client.impl.PubClientImpl; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Package; -import io.netty.channel.ChannelHandlerContext; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -/** - * Cmd Test : HeartBeat - */ -public class CmdHeartbeatTest { - public static Server server = new Server(); - public static PubClient client = new PubClientImpl("127.0.0.1", 10000, UserAgentUtils.createPubUserAgent()); - - @BeforeClass - public static void before_Cmd_hearbeat() throws Exception { - server.startAccessServer(); - client.init(); - Thread.sleep(1000); - } - - @Test - public void test_Cmd_heartbeat() throws Exception { - client.registerBusiHandler(new ReceiveMsgHook() { - @Override - public void handle(Package msg, ChannelHandlerContext ctx) { - System.err.println("receive response from server:------------------" + msg.toString()); - if (msg.getHeader().getCommand() != Command.HEARTBEAT_RESPONSE) { - Assert.assertTrue("HEARTBEAT_RESPONSE", false); - } - } - }); - client.heartbeat(); - - Thread.sleep(10000); - } - - @AfterClass - public static void after_Cmd_heartbeat() throws Exception { -// server.shutdownAccessServer(); -// client.close(); - } -} diff --git a/eventmesh-emesher/src/test/java/protocol/CmdListenTest.java b/eventmesh-emesher/src/test/java/protocol/CmdListenTest.java deleted file mode 100644 index 300bb2b469..0000000000 --- a/eventmesh-emesher/src/test/java/protocol/CmdListenTest.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package protocol; - -import client.SubClient; -import client.common.Server; -import client.common.UserAgentUtils; -import client.impl.SubClientImpl; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Package; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -/** - * Cmd Test: LISTEN_REQUEST - */ -public class CmdListenTest { - public static Server server = new Server(); - public static SubClient client = new SubClientImpl("127.0.0.1", 10000, UserAgentUtils.createSubUserAgent()); - - @BeforeClass - public static void before_Cmd_listen() throws Exception { - server.startAccessServer(); - client.init(); - Thread.sleep(1000); - } - - @AfterClass - public static void after_Cmd_listen() throws Exception { -// server.shutdownAccessServer(); -// client.close(); - } - - @Test - public void test_Cmd_listen() throws Exception { - Package replyMsg = client.listen(); - System.err.println("Reply Message-----------------------------------------" + replyMsg.toString()); - if (replyMsg.getHeader().getCommand() != Command.LISTEN_RESPONSE) { - Assert.assertTrue("SUBSCRIBE_RESPONSE FAIL", false); - } - } -} diff --git a/eventmesh-emesher/src/test/java/protocol/CmdRecommendTest.java b/eventmesh-emesher/src/test/java/protocol/CmdRecommendTest.java deleted file mode 100644 index 6973511611..0000000000 --- a/eventmesh-emesher/src/test/java/protocol/CmdRecommendTest.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package protocol; - -import client.PubClient; -import client.common.Server; -import client.common.UserAgentUtils; -import client.impl.PubClientImpl; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Package; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -public class CmdRecommendTest { - public static Server server = new Server(); - public static PubClient client = new PubClientImpl("127.0.0.1", 10000, UserAgentUtils.createPubUserAgent()); - - @BeforeClass - public static void before_Cmd_Recommend() throws Exception { - server.startAccessServer(); - client.init(); - Thread.sleep(1000); - } - - @AfterClass - public static void after_Cmd_Recommend() throws Exception { -// server.shutdownAccessServer(); -// client.close(); - } - - @Test - public void test_Cmd_Recommend() throws Exception { - - Package replyMsg = client.askRecommend(); - System.err.println("Reply Message-----------------------------------------" + replyMsg.toString()); - if (replyMsg.getHeader().getCommand() != Command.RECOMMEND_RESPONSE) { - Assert.assertTrue("RECOMMEND_RESPONSE FAIL", false); - } - } - - -} diff --git a/eventmesh-emesher/src/test/java/protocol/CmdSendRRTest.java b/eventmesh-emesher/src/test/java/protocol/CmdSendRRTest.java deleted file mode 100644 index 2516437d96..0000000000 --- a/eventmesh-emesher/src/test/java/protocol/CmdSendRRTest.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package protocol; - -import client.PubClient; -import client.SubClient; -import client.common.MessageUtils; -import client.common.Server; -import client.common.UserAgentUtils; -import client.impl.PubClientImpl; -import client.impl.SubClientImpl; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Package; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -public class CmdSendRRTest { - - public static Server server = new Server(); - public static PubClient client = new PubClientImpl("127.0.0.1", 10000, UserAgentUtils.createPubUserAgent()); - public static SubClient subClient = new SubClientImpl("127.0.0.1", 10000, UserAgentUtils.createUserAgent()); - - @BeforeClass - public static void before_Cmd_RR() throws Exception { - server.startAccessServer(); - client.init(); - Thread.sleep(1000); - } - - @Test - public void test_Cmd_RR() throws Exception { - Package msg = MessageUtils.rrMesssage("FT0-s-80000000-01-0", 0); - Package replyMsg = client.rr(msg, 5000); - System.err.println("收到回复:" + replyMsg.toString()); - if (replyMsg.getHeader().getCommand() != Command.RESPONSE_TO_CLIENT) { - Assert.assertTrue("RESPONSE_TO_CLIENT FAIL", false); - } - Thread.sleep(1000); - } - - @AfterClass - public static void after_Cmd_RR() throws Exception { -// server.shutdownAccessServer(); -// client.close(); - } -} diff --git a/eventmesh-emesher/src/test/java/protocol/CmdSubscribeTest.java b/eventmesh-emesher/src/test/java/protocol/CmdSubscribeTest.java deleted file mode 100644 index 3d6a3b2e3f..0000000000 --- a/eventmesh-emesher/src/test/java/protocol/CmdSubscribeTest.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package protocol; - -import client.SubClient; -import client.common.Server; -import client.common.UserAgentUtils; -import client.impl.SubClientImpl; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Package; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; -/** - * Test Cmd: SUBSCRIBE_REQUEST - */ -public class CmdSubscribeTest { - - public static Server server = new Server(); - public static SubClient client = new SubClientImpl("127.0.0.1", 10000, UserAgentUtils.createSubUserAgent()); - - @BeforeClass - public static void before_Cmd_subscribe() throws Exception { - server.startAccessServer(); - client.init(); - Thread.sleep(1000); - } - - @AfterClass - public static void after_Cmd_subscribe() throws Exception { -// server.shutdownAccessServer(); -// client.close(); - } - - @Test - public void test_Cmd_subscribe() throws Exception { - Package replyMsg = client.justSubscribe("FT0-s-80000000-01-0"); - System.err.println("Reply Message------------------" + replyMsg.toString()); - if (replyMsg.getHeader().getCommand() != Command.SUBSCRIBE_RESPONSE) { - Assert.assertTrue("SUBSCRIBE_RESPONSE FAIL", false); - } - } -} diff --git a/eventmesh-emesher/src/test/java/protocol/CmdSysLogTest.java b/eventmesh-emesher/src/test/java/protocol/CmdSysLogTest.java deleted file mode 100644 index caf8fce570..0000000000 --- a/eventmesh-emesher/src/test/java/protocol/CmdSysLogTest.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package protocol; - -import client.SubClient; -import client.common.Server; -import client.common.UserAgentUtils; -import client.hook.ReceiveMsgHook; -import client.impl.SubClientImpl; -import com.webank.eventmesh.common.protocol.tcp.Package; -import io.netty.channel.ChannelHandlerContext; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -/** - * Test Cmd: SYS_LOG_TO_LOGSERVER - */ -public class CmdSysLogTest { - public static Server server = new Server(); - public static SubClient client = new SubClientImpl("127.0.0.1", 10000, UserAgentUtils.createSubUserAgent()); - - @BeforeClass - public static void before_Cmd_syslog() throws Exception { - server.startAccessServer(); - client.init(); - } - - @AfterClass - public static void after_Cmd_syslog() throws Exception { -// server.shutdownAccessServer(); -// client.close(); - } - - @Test - public void test_Cmd_syslog() throws Exception { - client.registerBusiHandler(new ReceiveMsgHook() { - @Override - public void handle(Package msg, ChannelHandlerContext ctx) { - System.err.println("receive response from server:------------------" + msg.toString()); - Assert.assertTrue("receive a error command", false); - } - }); -// client.sysLog(); - Thread.sleep(1000); - } -} diff --git a/eventmesh-emesher/src/test/java/protocol/CmdTraceLogTest.java b/eventmesh-emesher/src/test/java/protocol/CmdTraceLogTest.java deleted file mode 100644 index 7255adc3a6..0000000000 --- a/eventmesh-emesher/src/test/java/protocol/CmdTraceLogTest.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package protocol; - -import client.SubClient; -import client.common.Server; -import client.common.UserAgentUtils; -import client.hook.ReceiveMsgHook; -import client.impl.SubClientImpl; -import com.webank.eventmesh.common.protocol.tcp.Package; -import io.netty.channel.ChannelHandlerContext; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -/** - * Cmd Test: TRACE_LOG_TO_LOGSERVER - */ -public class CmdTraceLogTest { - - public static Server server = new Server(); - public static SubClient client = new SubClientImpl("127.0.0.1", 10000, UserAgentUtils.createSubUserAgent()); - - @BeforeClass - public static void before_Cmd_traceLog() throws Exception { - server.startAccessServer(); - client.init(); - } - - @AfterClass - public static void after_Cmd_traceLog() throws Exception { -// server.shutdownAccessServer(); -// client.close(); - } - - @Test - public void test_Cmd_traceLog() throws Exception { - client.registerBusiHandler(new ReceiveMsgHook() { - @Override - public void handle(Package msg, ChannelHandlerContext ctx) { - System.err.println("receive response from server:------------------" + msg.toString()); - Assert.assertTrue("receive a error command", false); - } - }); -// client.traceLog(); - Thread.sleep(1000); - } -} diff --git a/eventmesh-emesher/src/test/java/protocol/CmdUnSubscribeTest.java b/eventmesh-emesher/src/test/java/protocol/CmdUnSubscribeTest.java deleted file mode 100644 index 4210655195..0000000000 --- a/eventmesh-emesher/src/test/java/protocol/CmdUnSubscribeTest.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package protocol; - -import client.SubClient; -import client.common.Server; -import client.common.UserAgentUtils; -import client.impl.SubClientImpl; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Package; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -/** - * Test Cmd: UNSUBSCRIBE_REQUEST - */ -public class CmdUnSubscribeTest { - - public static Server server = new Server(); - public static SubClient client = new SubClientImpl("127.0.0.1", 10000, UserAgentUtils.createSubUserAgent()); - - @BeforeClass - public static void before_Cmd_unsubscribe() throws Exception { - server.startAccessServer(); - client.init(); - Thread.sleep(1000); - } - - @AfterClass - public static void after_Cmd_unsubscribe() throws Exception { -// server.shutdownAccessServer(); -// client.close(); - } - - @Test - public void test_Cmd_unsubscribe() throws Exception { - Package replyMsg = client.justUnsubscribe("FT0-s-80000000-01-0"); - System.err.println("Reply Message-----------------------------------------" + replyMsg.toString()); - if (replyMsg.getHeader().getCommand() != Command.UNSUBSCRIBE_RESPONSE) { - Assert.assertTrue("UNSUBSCRIBE_RESPONSE FAIL", false); - } - } -} diff --git a/eventmesh-emesher/src/test/java/test/BasicTest.java b/eventmesh-emesher/src/test/java/test/BasicTest.java deleted file mode 100644 index 38a4ecf404..0000000000 --- a/eventmesh-emesher/src/test/java/test/BasicTest.java +++ /dev/null @@ -1,159 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package test; - -import client.ProxyClient; -import client.PubClient; -import client.SubClient; -import client.common.MessageUtils; -import client.hook.ReceiveMsgHook; -import client.impl.ProxyClientImpl; -import client.impl.PubClientImpl; -import client.impl.SubClientImpl; -import com.webank.eventmesh.common.protocol.tcp.AccessMessage; -import com.webank.eventmesh.common.protocol.tcp.Package; -import io.netty.channel.ChannelHandlerContext; -import org.junit.Test; - -public class BasicTest { - @Test - public void helloTest() throws Exception { - ProxyClient client = initClient(); - client.heartbeat(); - client.close(); - } - - @Test - public void heartbeatTest() throws Exception { - ProxyClient client = initClient(); - client.heartbeat(); - Thread.sleep(4000); - client.close(); - } - - @Test - public void goodbyeTest() throws Exception { - ProxyClient client = initClient(); - client.heartbeat(); - client.goodbye(); - client.close(); - } - - @Test - public void subscribe() throws Exception { - ProxyClient client = initClient(); - client.heartbeat(); - client.justSubscribe("FT0-s-80010000-01-1"); - client.close(); - } - - @Test - public void unsubscribe() throws Exception { - ProxyClient client = initClient(); - client.heartbeat(); - client.justSubscribe("FT0-s-80000000-01-0"); - client.listen(); - client.justUnsubscribe("FT0-s-80000000-01-0"); - client.close(); - } - - @Test - public void listenTest() throws Exception { - ProxyClient client = initClient(); - client.heartbeat(); - client.listen(); - client.close(); - } - - @Test - public void syncMessage() throws Exception { - ProxyClient client = initClient(); - client.heartbeat(); - client.justSubscribe("FT0-s-80000000-01-0"); - client.listen(); - for (int i = 0; i < 100; i++) { - Package rr = client.rr(MessageUtils.rrMesssage("FT0-s-80000000-01-0", i), 3000); - if (rr.getBody() instanceof AccessMessage) { - String body = ((AccessMessage) rr.getBody()).getBody(); - System.err.println("rrMessage: " + body + " " + "rr-reply-------------------------------------------------" + rr.toString()); - } - } - Thread.sleep(100); - client.close(); - } - - @Test - public void asyncMessage() throws Exception { - ProxyClient client = initClient(); - - client.justSubscribe("FT0-e-80010000-01-1"); - client.heartbeat(); - client.listen(); - client.registerSubBusiHandler(new ReceiveMsgHook() { - @Override - public void handle(Package msg, ChannelHandlerContext ctx) { - if (msg.getBody() instanceof AccessMessage) { - String body = ((AccessMessage) msg.getBody()).getBody(); - System.err.println("receive message :------" + body + "------------------------------------------------" + msg.toString()); - } - } - }); - System.err.println("before publish"); - client.publish(MessageUtils.asyncMessage("FT0-e-80010000-01-1", 0), 3000); - Thread.sleep(500); - client.close(); - } - - @Test - public void broadcastMessage() throws Exception { - ProxyClient client = initClient(); - client.heartbeat(); - client.justSubscribe("FT0-e-80030000-01-3"); - client.listen(); - client.registerSubBusiHandler(new ReceiveMsgHook() { - @Override - public void handle(Package msg, ChannelHandlerContext ctx) { - if (msg.getBody() instanceof AccessMessage) { - String body = ((AccessMessage) msg.getBody()).getBody(); - System.err.println("receive message: ------------" + body + "-------------------------------" + msg.toString()); - } - } - }); - client.broadcast(MessageUtils.broadcastMessage("FT0-e-80030000-01-3", 0), 3000); - Thread.sleep(500); - client.close(); - } - - private PubClient initPubClient() throws Exception { - PubClientImpl pubClient = new PubClientImpl("127.0.0.1", 10000, MessageUtils.generatePubClient()); - pubClient.init(); - return pubClient; - } - - private SubClient initSubClient() throws Exception { - SubClientImpl subClient = new SubClientImpl("127.0.0.1", 10000, MessageUtils.generateSubServer()); - subClient.init(); - return subClient; - } - - private ProxyClient initClient() throws Exception { - ProxyClientImpl client = new ProxyClientImpl("127.0.0.1", 10000); - client.init(); - return client; - } -} diff --git a/eventmesh-registry/build.gradle b/eventmesh-registry/build.gradle deleted file mode 100644 index 508f08e747..0000000000 --- a/eventmesh-registry/build.gradle +++ /dev/null @@ -1,14 +0,0 @@ -plugins { - id 'java' -} - -group 'com.webank.defibus' -version '1.0.0' - -repositories { - mavenCentral() -} - -dependencies { - testCompile group: 'junit', name: 'junit', version: '4.12' -} diff --git a/eventmesh-emesher/bin/start.sh b/eventmesh-runtime/bin/start.sh similarity index 61% rename from eventmesh-emesher/bin/start.sh rename to eventmesh-runtime/bin/start.sh index 305889137b..9c0724bdd0 100644 --- a/eventmesh-emesher/bin/start.sh +++ b/eventmesh-runtime/bin/start.sh @@ -1,4 +1,21 @@ #!/bin/sh +# +# Licensed to Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Apache Software Foundation (ASF) licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. # Copyright (C) @2017 Webank Group Holding Limited @@ -38,7 +55,7 @@ function is_java8 { #0(not running), 1(is running) #function is_proxyRunning { # local _pid="$1" -# local pid=`ps ax | grep -i 'com.webank.emesher.boot.ProxyStartup' |grep java | grep -v grep | awk '{print $1}'|grep $_pid` +# local pid=`ps ax | grep -i 'org.apache.eventmesh.runtime.boot.EventMeshStartup' |grep java | grep -v grep | awk '{print $1}'|grep $_pid` # if [ -z "$pid" ] ; then # return 0 # else @@ -48,18 +65,18 @@ function is_java8 { function get_pid { local ppid="" - if [ -f ${PROXY_HOME}/bin/pid.file ]; then - ppid=$(cat ${PROXY_HOME}/bin/pid.file) + if [ -f ${EVENTMESH_HOME}/bin/pid.file ]; then + ppid=$(cat ${EVENTMESH_HOME}/bin/pid.file) else if [[ $OS =~ Msys ]]; then # 在Msys上存在可能无法kill识别出的进程的BUG - ppid=`jps -v | grep -i "com.webank.emesher.boot.ProxyStartup" | grep java | grep -v grep | awk -F ' ' {'print $1'}` + ppid=`jps -v | grep -i "org.apache.eventmesh.runtime.boot.EventMeshStartup" | grep java | grep -v grep | awk -F ' ' {'print $1'}` elif [[ $OS =~ Darwin ]]; then # 已知问题:grep java 可能无法精确识别java进程 - ppid=$(/bin/ps -o user,pid,command | grep "java" | grep -i "com.webank.emesher.boot.ProxyStartup" | grep -Ev "^root" |awk -F ' ' {'print $2'}) + ppid=$(/bin/ps -o user,pid,command | grep "java" | grep -i "org.apache.eventmesh.runtime.boot.EventMeshStartup" | grep -Ev "^root" |awk -F ' ' {'print $2'}) else #在Linux服务器上要求尽可能精确识别进程 - ppid=$(ps -C java -o user,pid,command --cols 99999 | grep -w $PROXY_HOME | grep -i "com.webank.emesher.boot.ProxyStartup" | grep -Ev "^root" |awk -F ' ' {'print $2'}) + ppid=$(ps -C java -o user,pid,command --cols 99999 | grep -w $EVENTMESH_HOME | grep -i "org.apache.eventmesh.runtime.boot.EventMeshStartup" | grep -Ev "^root" |awk -F ' ' {'print $2'}) fi fi echo "$ppid"; @@ -83,18 +100,18 @@ else exit 9; fi -echo "proxy use java location= "$JAVA +echo "eventmesh use java location= "$JAVA -PROXY_HOME=`cd "./.." && pwd` +EVENTMESH_HOME=`cd "./.." && pwd` -export PROXY_HOME +export EVENTMESH_HOME -export PROXY_LOG_HOME=${PROXY_HOME}/logs +export EVENTMESH_LOG_HOME=${EVENTMESH_HOME}/logs -echo "PROXY_HOME : ${PROXY_HOME}, PROXY_LOG_HOME : ${PROXY_LOG_HOME}" +echo "EVENTMESH_HOME : ${EVENTMESH_HOME}, EVENTMESH_LOG_HOME : ${EVENTMESH_LOG_HOME}" function make_logs_dir { - if [ ! -e "${PROXY_LOG_HOME}" ]; then mkdir -p "${PROXY_LOG_HOME}"; fi + if [ ! -e "${EVENTMESH_LOG_HOME}" ]; then mkdir -p "${EVENTMESH_LOG_HOME}"; fi } error_exit () @@ -114,10 +131,10 @@ export JAVA_HOME #fi #JAVA_OPT="${JAVA_OPT} -server -Xms2048M -Xmx4096M -Xmn2048m -XX:SurvivorRatio=4" -JAVA_OPT=`cat ${PROXY_HOME}/conf/server.env | grep APP_START_JVM_OPTION::: | awk -F ':::' {'print $2'}` +JAVA_OPT=`cat ${EVENTMESH_HOME}/conf/server.env | grep APP_START_JVM_OPTION::: | awk -F ':::' {'print $2'}` JAVA_OPT="${JAVA_OPT} -XX:+UseG1GC -XX:G1HeapRegionSize=16m -XX:G1ReservePercent=25 -XX:InitiatingHeapOccupancyPercent=30 -XX:SoftRefLRUPolicyMSPerMB=0 -XX:SurvivorRatio=8 -XX:MaxGCPauseMillis=50" -JAVA_OPT="${JAVA_OPT} -verbose:gc -Xloggc:${PROXY_HOME}/logs/proxy_gc_%p.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCApplicationStoppedTime -XX:+PrintAdaptiveSizePolicy" -JAVA_OPT="${JAVA_OPT} -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${PROXY_HOME}/logs -XX:ErrorFile=${PROXY_HOME}/logs/hs_err_%p.log" +JAVA_OPT="${JAVA_OPT} -verbose:gc -Xloggc:${EVENTMESH_HOME}/logs/eventmesh_gc_%p.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCApplicationStoppedTime -XX:+PrintAdaptiveSizePolicy" +JAVA_OPT="${JAVA_OPT} -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${EVENTMESH_HOME}/logs -XX:ErrorFile=${EVENTMESH_HOME}/logs/hs_err_%p.log" JAVA_OPT="${JAVA_OPT} -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=30m" JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow" JAVA_OPT="${JAVA_OPT} -XX:+AlwaysPreTouch" @@ -126,9 +143,9 @@ JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages -XX:-UseBiasedLocking" JAVA_OPT="${JAVA_OPT} -Dio.netty.leakDetectionLevel=advanced" JAVA_OPT="${JAVA_OPT} -Dio.netty.allocator.type=pooled" JAVA_OPT="${JAVA_OPT} -Djava.security.egd=file:/dev/./urandom" -JAVA_OPT="${JAVA_OPT} -Dlog4j.configurationFile=${PROXY_HOME}/conf/log4j2.xml" -JAVA_OPT="${JAVA_OPT} -Dproxy.log.home=${PROXY_LOG_HOME}" -JAVA_OPT="${JAVA_OPT} -DconfPath=${PROXY_HOME}/conf" +JAVA_OPT="${JAVA_OPT} -Dlog4j.configurationFile=${EVENTMESH_HOME}/conf/log4j2.xml" +JAVA_OPT="${JAVA_OPT} -Deventmesh.log.home=${EVENTMESH_LOG_HOME}" +JAVA_OPT="${JAVA_OPT} -DconfPath=${EVENTMESH_HOME}/conf" JAVA_OPT="${JAVA_OPT} -Dlog4j2.AsyncQueueFullPolicy=Discard" JAVA_OPT="${JAVA_OPT} -Drocketmq.client.logUseSlf4j=true" @@ -151,10 +168,15 @@ fi make_logs_dir -echo "using jdk[$JAVA]" >> ${PROXY_LOG_HOME}/proxy.out +echo "using jdk[$JAVA]" >> ${EVENTMESH_LOG_HOME}/eventmesh.out -PROXY_MAIN=com.webank.emesher.boot.ProxyStartup -$JAVA $JAVA_OPT -classpath ${PROXY_HOME}/conf:${PROXY_HOME}/apps/*:${PROXY_HOME}/lib/* $PROXY_MAIN >> ${PROXY_LOG_HOME}/proxy.out 2>&1 & +EVENTMESH_MAIN=org.apache.eventmesh.runtime.boot.EventMeshStartup +if [ $DOCKER ] +then + $JAVA $JAVA_OPT -classpath ${EVENTMESH_HOME}/conf:${EVENTMESH_HOME}/apps/*:${EVENTMESH_HOME}/lib/* $EVENTMESH_MAIN >> ${EVENTMESH_LOG_HOME}/eventmesh.out +else + $JAVA $JAVA_OPT -classpath ${EVENTMESH_HOME}/conf:${EVENTMESH_HOME}/apps/*:${EVENTMESH_HOME}/lib/* $EVENTMESH_MAIN >> ${EVENTMESH_LOG_HOME}/eventmesh.out 2>&1 & echo $!>pid.file +fi exit 0 diff --git a/eventmesh-runtime/bin/stop.sh b/eventmesh-runtime/bin/stop.sh new file mode 100644 index 0000000000..4897c77612 --- /dev/null +++ b/eventmesh-runtime/bin/stop.sh @@ -0,0 +1,77 @@ +#!/bin/sh +# +# Licensed to Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Apache Software Foundation (ASF) licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +#detect operating system. +OS=$(uname -o) + +EVENTMESH_HOME=`cd "./.." && pwd` + +export EVENTMESH_HOME + +function get_pid { + local ppid="" + if [ -f ${EVENTMESH_HOME}/bin/pid.file ]; then + ppid=$(cat ${EVENTMESH_HOME}/bin/pid.file) + else + if [[ $OS =~ Msys ]]; then + # 在Msys上存在可能无法kill识别出的进程的BUG + ppid=`jps -v | grep -i "org.apache.eventmesh.runtime.boot.EventMeshStartup" | grep java | grep -v grep | awk -F ' ' {'print $1'}` + elif [[ $OS =~ Darwin ]]; then + # 已知问题:grep java 可能无法精确识别java进程 + ppid=$(/bin/ps -o user,pid,command | grep "java" | grep -i "org.apache.eventmesh.runtime.boot.EventMeshStartup" | grep -Ev "^root" |awk -F ' ' {'print $2'}) + else + #在Linux服务器上要求尽可能精确识别进程 + ppid=$(ps -C java -o user,pid,command --cols 99999 | grep -w $EVENTMESH_HOME | grep -i "org.apache.eventmesh.runtime.boot.EventMeshStartup" | grep -Ev "^root" |awk -F ' ' {'print $2'}) + fi + fi + echo "$ppid"; +} + +pid=$(get_pid) +if [ -z "$pid" ];then + echo -e "No eventmesh running.." + exit 0; +fi + +kill ${pid} +echo "Send shutdown request to eventmesh(${pid}) OK" + +[[ $OS =~ Msys ]] && PS_PARAM=" -W " +stop_timeout=60 +for no in $(seq 1 $stop_timeout); do + if ps $PS_PARAM -p "$pid" 2>&1 > /dev/null; then + if [ $no -lt $stop_timeout ]; then + echo "[$no] shutdown server ..." + sleep 1 + continue + fi + + echo "shutdown server timeout, kill process: $pid" + kill -9 $pid; sleep 1; break; + echo "`date +'%Y-%m-%-d %H:%M:%S'` , pid : [$pid] , error message : abnormal shutdown which can not be closed within 60s" > ../logs/shutdown.error + else + echo "shutdown server ok!"; break; + fi +done + +if [ -f "pid.file" ]; then + rm pid.file +fi + + diff --git a/eventmesh-emesher/bin/watchdog.sh b/eventmesh-runtime/bin/watchdog.sh similarity index 73% rename from eventmesh-emesher/bin/watchdog.sh rename to eventmesh-runtime/bin/watchdog.sh index 0d0d62020d..9708e3b918 100644 --- a/eventmesh-emesher/bin/watchdog.sh +++ b/eventmesh-runtime/bin/watchdog.sh @@ -1,4 +1,21 @@ #!/bin/bash +# +# Licensed to Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Apache Software Foundation (ASF) licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. set -e; function usage(){ diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/SimpleSubClient.java b/eventmesh-runtime/build.gradle similarity index 58% rename from eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/SimpleSubClient.java rename to eventmesh-runtime/build.gradle index 04b1006994..09dd79f9c2 100644 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/SimpleSubClient.java +++ b/eventmesh-runtime/build.gradle @@ -14,29 +14,23 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +sourceCompatibility = 1.8 -package com.webank.eventmesh.client.tcp; +List metrics = [ + "io.dropwizard.metrics:metrics-core:4.1.0", + "io.dropwizard.metrics:metrics-healthchecks:4.1.0", + "io.dropwizard.metrics:metrics-annotation:4.1.0", + "io.dropwizard.metrics:metrics-json:4.1.0" +] -import com.webank.eventmesh.client.tcp.common.ReceiveMsgHook; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; -public interface SimpleSubClient { - void init() throws Exception; +List open_message = [ + "io.openmessaging:openmessaging-api:2.2.1-pubsub" +] - void close(); - void heartbeat() throws Exception; - - void reconnect() throws Exception; - - void subscribe(String topic) throws Exception; - - void unsubscribe() throws Exception; - - void listen() throws Exception; - - void registerBusiHandler(ReceiveMsgHook handler) throws Exception; - - UserAgent getUserAgent(); +dependencies { + implementation metrics, open_message,project(":eventmesh-connector-api"),project(":eventmesh-common") + testImplementation metrics,open_message,project(":eventmesh-common"),project(":eventmesh-connector-api") } diff --git a/eventmesh-runtime/conf/eventmesh.properties b/eventmesh-runtime/conf/eventmesh.properties new file mode 100644 index 0000000000..035b950fc6 --- /dev/null +++ b/eventmesh-runtime/conf/eventmesh.properties @@ -0,0 +1,54 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +###############################EVNETMESH-runtime ENV################################# +eventMesh.server.idc=DEFAULT +eventMesh.server.env=PRD +eventMesh.server.cluster=COMMON +eventMesh.server.name=EVENTMESH-runtime +eventMesh.sysid=0000 +eventMesh.server.http.port=10105 +########################## eventMesh tcp configuration ############################ +eventMesh.server.tcp.enabled=true +eventMesh.server.tcp.port=10000 +eventMesh.server.tcp.readerIdleSeconds=120 +eventMesh.server.tcp.writerIdleSeconds=120 +eventMesh.server.tcp.allIdleSeconds=120 +eventMesh.server.tcp.clientMaxNum=10000 +# client isolation time if the message send failure +eventMesh.server.tcp.pushFailIsolateTimeInMills=30000 +# rebalance internal +eventMesh.server.tcp.RebalanceIntervalInMills=30000 +# session expire time about client +eventMesh.server.session.expiredInMills=60000 +# flow control, include the global level and session level +eventMesh.server.tcp.msgReqnumPerSecond=15000 +eventMesh.server.session.upstreamBufferSize=20 + +# thread number about global scheduler +eventMesh.server.global.scheduler=5 +eventMesh.server.tcp.taskHandleExecutorPoolSize=8 +#retry +eventMesh.server.retry.pushRetryTimes=3 +eventMesh.server.retry.pushRetryDelayInMills=500 +eventMesh.server.retry.pushRetryQueueSize=10000 +#admin +eventMesh.server.admin.http.port=10106 +#registry +eventMesh.server.registry.registerIntervalInMills=10000 +eventMesh.server.registry.fetchRegistryAddrIntervalInMills=20000 +#auto-ack +#eventMesh.server.defibus.client.comsumeTimeoutInMin=5 \ No newline at end of file diff --git a/eventmesh-emesher/conf/log4j2.xml b/eventmesh-runtime/conf/log4j2.xml similarity index 93% rename from eventmesh-emesher/conf/log4j2.xml rename to eventmesh-runtime/conf/log4j2.xml index 9f18a6bd3f..8495181200 100644 --- a/eventmesh-emesher/conf/log4j2.xml +++ b/eventmesh-runtime/conf/log4j2.xml @@ -64,11 +64,7 @@ - - - - - + diff --git a/eventmesh-runtime/conf/rocketmq-client.properties b/eventmesh-runtime/conf/rocketmq-client.properties new file mode 100644 index 0000000000..1261f30e2c --- /dev/null +++ b/eventmesh-runtime/conf/rocketmq-client.properties @@ -0,0 +1,18 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +#######################rocketmq-client################## +eventMesh.server.rocketmq.namesrvAddr=127.0.0.1:9876;127.0.0.1:9876 diff --git a/eventmesh-emesher/conf/sChat2.jks b/eventmesh-runtime/conf/sChat2.jks similarity index 100% rename from eventmesh-emesher/conf/sChat2.jks rename to eventmesh-runtime/conf/sChat2.jks diff --git a/eventmesh-runtime/conf/server.env b/eventmesh-runtime/conf/server.env new file mode 100644 index 0000000000..f8c01df100 --- /dev/null +++ b/eventmesh-runtime/conf/server.env @@ -0,0 +1,18 @@ +# +# Licensed to Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Apache Software Foundation (ASF) licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +APP_START_JVM_OPTION:::-server -Xms128M -Xmx256M -Xmn128m -XX:SurvivorRatio=4 -Duser.language=zh diff --git a/eventmesh-emesher/gradle.properties b/eventmesh-runtime/gradle.properties similarity index 92% rename from eventmesh-emesher/gradle.properties rename to eventmesh-runtime/gradle.properties index 7de113ebe0..6446ff31f4 100644 --- a/eventmesh-emesher/gradle.properties +++ b/eventmesh-runtime/gradle.properties @@ -14,9 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # - -group=com.webank.eventmesh -version=1.1.1 -defibus_version=1.0.1 +group=org.apache.eventmesh +version=1.2.0-release mavenUserName= mavenPassword= diff --git a/eventmesh-runtime/scripts/before_shutdown.sh b/eventmesh-runtime/scripts/before_shutdown.sh new file mode 100644 index 0000000000..9cf8b624a9 --- /dev/null +++ b/eventmesh-runtime/scripts/before_shutdown.sh @@ -0,0 +1,31 @@ +#!/bin/bash +# +# Licensed to Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Apache Software Foundation (ASF) licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +source session.sh | awk '{print $1}' | awk -F '=' '{print $2}' | awk -F '|' '{print $1,$2}' | grep 2019 > tmp.txt + +cat tmp.txt | while read line +do + cmd="./client_manage.sh -s $line" + $cmd + sleep 10s +done + +rm tmp.txt + +./client_manage.sh -a diff --git a/eventmesh-emesher/scripts/client_manage.sh b/eventmesh-runtime/scripts/client_manage.sh similarity index 63% rename from eventmesh-emesher/scripts/client_manage.sh rename to eventmesh-runtime/scripts/client_manage.sh index d4c6bd0150..0913cc58ec 100644 --- a/eventmesh-emesher/scripts/client_manage.sh +++ b/eventmesh-runtime/scripts/client_manage.sh @@ -1,7 +1,24 @@ #!/bin/bash +# +# Licensed to Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Apache Software Foundation (ASF) licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. i_eg="sh client_manage.sh -i 10.255.34.160 24591" -s_eg="sh client_manage.sh -s FT0 5319" -r_eg="sh client_manage.sh -r FT0 9876 10.255.1.143 10000" +s_eg="sh client_manage.sh -s 5319" +r_eg="sh client_manage.sh -r 9876 10.255.1.143 10000" a_eg="sh client_manage.sh -a" x_eg="sh client_manage.sh -x 10.255.34.160 24591 10.255.1.143 10000" y_eg="sh client_manage.sh -y bq-bypass 10.255.1.143 10000" @@ -10,8 +27,8 @@ function printEg() { echo "param error." echo "reject client by ip_port, eg : ${i_eg}" echo "reject all clients, eg : ${a_eg}" - echo "reject clients by dcn_systemid, eg : ${s_eg}" - echo "redirect client by dcn_systemid, eg : ${r_eg}" + echo "reject clients by systemid, eg : ${s_eg}" + echo "redirect client by systemid, eg : ${r_eg}" echo "redirect client by ip port, eg : ${x_eg}" echo "redirect client by path, eg : ${y_eg}" } @@ -35,9 +52,8 @@ do CLIENT_PORT=$5 msg=`curl "http://${ADDR}/clientManage/rejectClientByIpPort?ip=${CLIENT_IP}&port=${CLIENT_PORT}"`;echo ${msg};break;; -s|--subsystem) - DCN=$4 - SUB_SYSTEM=$5 - msg=`curl "http://${ADDR}/clientManage/rejectClientBySubSystem?dcn=${DCN}&subSystem=${SUB_SYSTEM}"`;echo ${msg};break;; + SUB_SYSTEM=$4 + msg=`curl "http://${ADDR}/clientManage/rejectClientBySubSystem?subSystem=${SUB_SYSTEM}"`;echo ${msg};break;; -x|--redirectbyip) CLIENT_IP=$4 CLIENT_PORT=$5 @@ -50,11 +66,10 @@ do DEST_PROXY_PORT=$6 msg=`curl "http://${ADDR}/clientManage/redirectClientByPath?path=${CLIENT_PATH}&destProxyIp=${DEST_PROXY_IP}&destProxyPort=${DEST_PROXY_PORT}"`;echo ${msg};break;; -r|--redirect) - DCN=$4 - SUB_SYSTEM=$5 - DEST_PROXY_IP=$6 - DEST_PROXY_PORT=$7 - msg=`curl "http://${ADDR}/clientManage/redirectClientBySubSystem?dcn=${DCN}&subSystem=${SUB_SYSTEM}&destProxyIp=${DEST_PROXY_IP}&destProxyPort=${DEST_PROXY_PORT}"`;echo ${msg};break;; + SUB_SYSTEM=$4 + DEST_PROXY_IP=$5 + DEST_PROXY_PORT=$6 + msg=`curl "http://${ADDR}/clientManage/redirectClientBySubSystem?subSystem=${SUB_SYSTEM}&destProxyIp=${DEST_PROXY_IP}&destProxyPort=${DEST_PROXY_PORT}"`;echo ${msg};break;; --) shift; break;; diff --git a/eventmesh-runtime/scripts/connections.sh b/eventmesh-runtime/scripts/connections.sh new file mode 100644 index 0000000000..2f691c861f --- /dev/null +++ b/eventmesh-runtime/scripts/connections.sh @@ -0,0 +1,22 @@ +#!/bin/bash +# +# Licensed to Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Apache Software Foundation (ASF) licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +set -e +date; +echo -e "active eventmesh connections:\n$(netstat -lnap |grep EST | grep $(jps | grep EventMeshStartup | cut -d" " -f1) | awk -F"[\t ]+" '{print $4}' |sort| uniq -c | sort -rnk1 | grep ":10000")" +echo -e "active mq connections:\n$(netstat -lnap |grep EST | grep $(jps | grep EventMeshStartup | cut -d" " -f1) | awk -F"[\t ]+" '{print $5}' |sort | uniq -c | sort -rnk1 |grep -E "10911|9876")" diff --git a/eventmesh-runtime/scripts/histo.sh b/eventmesh-runtime/scripts/histo.sh new file mode 100644 index 0000000000..7d56351082 --- /dev/null +++ b/eventmesh-runtime/scripts/histo.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# +# Licensed to Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Apache Software Foundation (ASF) licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +set -e +script_path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +cd $script_path +echo "[`date`] WARNING this script will PAUSE jvm for seconds." +pid=$(jcmd | grep EventMeshStartup | cut -d" " -f1) +jmap -histo:live $pid | tee $script_path/../logs/histo.$(date +%m%d%H%M).$pid.log diff --git a/eventmesh-runtime/scripts/histo_suspects.sh b/eventmesh-runtime/scripts/histo_suspects.sh new file mode 100644 index 0000000000..82eaa0b8f7 --- /dev/null +++ b/eventmesh-runtime/scripts/histo_suspects.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# +# Licensed to Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Apache Software Foundation (ASF) licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +set -e +script_path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +cd $script_path +echo "[`date`] WARNING this script will PAUSE jvm for seconds." +pid=$(jcmd | grep EventMeshStartup | cut -d" " -f1) +./histo.sh | grep -E "RRResponseFurture|PushConsumer|PullMessage|RR|RunnableAdapter|instance|ProducerFactory|ClientInstance|Session" | tee $script_path/../logs/histo.suspects.$(date +%m%d%H%M).$pid.log diff --git a/eventmesh-runtime/scripts/jstack.sh b/eventmesh-runtime/scripts/jstack.sh new file mode 100644 index 0000000000..ca6e2fdbff --- /dev/null +++ b/eventmesh-runtime/scripts/jstack.sh @@ -0,0 +1,23 @@ +#!/bin/bash +# +# Licensed to Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Apache Software Foundation (ASF) licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +set -e +script_path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +echo "[`date`] WARNING this script will PAUSE jvm for seconds." +pid=$(jcmd | grep EventMeshStartup | cut -d" " -f1) +jstack $pid | tee $script_path/../logs/jstack.$(date +%m%d%H%M).$pid.log diff --git a/eventmesh-runtime/scripts/monitor_connections.sh b/eventmesh-runtime/scripts/monitor_connections.sh new file mode 100644 index 0000000000..fdaf1f63f6 --- /dev/null +++ b/eventmesh-runtime/scripts/monitor_connections.sh @@ -0,0 +1,25 @@ +#!/bin/bash +# +# Licensed to Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Apache Software Foundation (ASF) licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +set -e +script_path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +cd $script_path +while true; do + ./connections.sh + sleep 60s; +done diff --git a/eventmesh-runtime/scripts/monitor_histo_suspects.sh b/eventmesh-runtime/scripts/monitor_histo_suspects.sh new file mode 100644 index 0000000000..35732cf128 --- /dev/null +++ b/eventmesh-runtime/scripts/monitor_histo_suspects.sh @@ -0,0 +1,25 @@ +#!/bin/bash +# +# Licensed to Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Apache Software Foundation (ASF) licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +set -e +script_path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +cd $script_path +while true; do + ./histo_suspects.sh + sleep 60s; +done diff --git a/eventmesh-runtime/scripts/monitor_threads.sh b/eventmesh-runtime/scripts/monitor_threads.sh new file mode 100644 index 0000000000..acf766e353 --- /dev/null +++ b/eventmesh-runtime/scripts/monitor_threads.sh @@ -0,0 +1,25 @@ +#!/bin/bash +# +# Licensed to Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Apache Software Foundation (ASF) licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +set -e +script_path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +cd $script_path +while true; do + ./threads.sh + sleep 60s; +done diff --git a/eventmesh-runtime/scripts/session.sh b/eventmesh-runtime/scripts/session.sh new file mode 100644 index 0000000000..3540cf8546 --- /dev/null +++ b/eventmesh-runtime/scripts/session.sh @@ -0,0 +1,29 @@ +#!/bin/bash +# +# Licensed to Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Apache Software Foundation (ASF) licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +if [ $# -eq 0 ] +then + curl -s 'http://127.0.0.1:10106/clientManage/showClient?' +elif [ $# -eq 1 ] +then + TOPIC=$1 + curl -s "http://127.0.0.1:10106/clientManage/showListenClientByTopic?topic=${TOPIC}" +else + CLIENT_SYSTEM=$1 + curl -s "http://127.0.0.1:10106/clientManage/showClientBySystem?subSystem=${CLIENT_SYSTEM}" +fi diff --git a/eventmesh-runtime/scripts/threads.sh b/eventmesh-runtime/scripts/threads.sh new file mode 100644 index 0000000000..fe1eeb5d7f --- /dev/null +++ b/eventmesh-runtime/scripts/threads.sh @@ -0,0 +1,23 @@ +#!/bin/bash +# +# Licensed to Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Apache Software Foundation (ASF) licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +jstack $(jcmd | grep EventMeshStartup | cut -d" " -f1) | \ + grep -vE "(Concurrent GC|DestroyJavaVM|Gang worker|Compiler|Attach Listener|GC Thread|VM Thread|Finalizer)" | \ + grep -oE '^".*?"' | \ + sed -r 's/"(.+)(-|_)([0-9]+).*"/\1/g' | \ + sort | uniq -c | sort -nk1 diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/controller/ClientManageController.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/controller/ClientManageController.java new file mode 100644 index 0000000000..573eb02029 --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/controller/ClientManageController.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.admin.controller; + +import java.io.IOException; +import java.net.InetSocketAddress; + +import com.sun.net.httpserver.HttpServer; + +import org.apache.eventmesh.runtime.admin.handler.RedirectClientByIpPortHandler; +import org.apache.eventmesh.runtime.admin.handler.RedirectClientByPathHandler; +import org.apache.eventmesh.runtime.admin.handler.RedirectClientBySubSystemHandler; +import org.apache.eventmesh.runtime.admin.handler.RejectAllClientHandler; +import org.apache.eventmesh.runtime.admin.handler.RejectClientByIpPortHandler; +import org.apache.eventmesh.runtime.admin.handler.RejectClientBySubSystemHandler; +import org.apache.eventmesh.runtime.admin.handler.ShowClientBySystemHandler; +import org.apache.eventmesh.runtime.admin.handler.ShowClientHandler; +import org.apache.eventmesh.runtime.admin.handler.ShowListenClientByTopicHandler; +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ClientManageController { + + private static final Logger logger = LoggerFactory.getLogger(ClientManageController.class); + + private EventMeshTCPServer eventMeshTCPServer; + + public ClientManageController(EventMeshTCPServer eventMeshTCPServer) { + this.eventMeshTCPServer = eventMeshTCPServer; + } + + public void start() throws IOException { + int port = eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshServerAdminPort; + HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); + server.createContext("/clientManage/showClient", new ShowClientHandler(eventMeshTCPServer)); + server.createContext("/clientManage/showClientBySystem", new ShowClientBySystemHandler(eventMeshTCPServer)); + server.createContext("/clientManage/rejectAllClient", new RejectAllClientHandler(eventMeshTCPServer)); + server.createContext("/clientManage/rejectClientByIpPort", new RejectClientByIpPortHandler(eventMeshTCPServer)); + server.createContext("/clientManage/rejectClientBySubSystem", new RejectClientBySubSystemHandler(eventMeshTCPServer)); + server.createContext("/clientManage/redirectClientBySubSystem", new RedirectClientBySubSystemHandler(eventMeshTCPServer)); + server.createContext("/clientManage/redirectClientByPath", new RedirectClientByPathHandler(eventMeshTCPServer)); + server.createContext("/clientManage/redirectClientByIpPort", new RedirectClientByIpPortHandler(eventMeshTCPServer)); + server.createContext("/clientManage/showListenClientByTopic", new ShowListenClientByTopicHandler(eventMeshTCPServer)); + + server.start(); + logger.info("ClientManageController start success, port:{}", port); + } +} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/RedirectClientByIpPortHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/RedirectClientByIpPortHandler.java new file mode 100644 index 0000000000..79ba4bd98f --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/RedirectClientByIpPortHandler.java @@ -0,0 +1,111 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.admin.handler; + +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.EventMeshTcp2Client; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.group.ClientSessionGroupMapping; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session; +import org.apache.eventmesh.runtime.util.NetUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class RedirectClientByIpPortHandler implements HttpHandler { + + private static final Logger logger = LoggerFactory.getLogger(RedirectClientByIpPortHandler.class); + + private final EventMeshTCPServer eventMeshTCPServer; + + public RedirectClientByIpPortHandler(EventMeshTCPServer eventMeshTCPServer) { + this.eventMeshTCPServer = eventMeshTCPServer; + } + + @Override + public void handle(HttpExchange httpExchange) throws IOException { + String result = ""; + OutputStream out = httpExchange.getResponseBody(); + try { + String queryString = httpExchange.getRequestURI().getQuery(); + Map queryStringInfo = NetUtils.formData2Dic(queryString); + String ip = queryStringInfo.get(EventMeshConstants.MANAGE_IP); + String port = queryStringInfo.get(EventMeshConstants.MANAGE_PORT); + String destEventMeshIp = queryStringInfo.get(EventMeshConstants.MANAGE_DEST_IP); + String destEventMeshPort = queryStringInfo.get(EventMeshConstants.MANAGE_DEST_PORT); + + if (StringUtils.isBlank(ip) || !StringUtils.isNumeric(port) + || StringUtils.isBlank(destEventMeshIp) || StringUtils.isBlank(destEventMeshPort) + || !StringUtils.isNumeric(destEventMeshPort)) { + httpExchange.sendResponseHeaders(200, 0); + result = "params illegal!"; + out.write(result.getBytes()); + return; + } + logger.info("redirectClientByIpPort in admin,ip:{},port:{},destIp:{},destPort:{}====================", ip, port, destEventMeshIp, destEventMeshPort); + ClientSessionGroupMapping clientSessionGroupMapping = eventMeshTCPServer.getClientSessionGroupMapping(); + ConcurrentHashMap sessionMap = clientSessionGroupMapping.getSessionMap(); + String redirectResult = ""; + try { + if (!sessionMap.isEmpty()) { + for (Session session : sessionMap.values()) { + if (session.getClient().getHost().equals(ip) && String.valueOf(session.getClient().getPort()).equals(port)) { + redirectResult += "|"; + redirectResult += EventMeshTcp2Client.redirectClient2NewEventMesh(eventMeshTCPServer, destEventMeshIp, Integer.parseInt(destEventMeshPort), + session, clientSessionGroupMapping); + } + } + } + } catch (Exception e) { + logger.error("clientManage|redirectClientByIpPort|fail|ip={}|port={}|destEventMeshIp" + + "={}|destEventMeshPort={},errMsg={}", ip, port, destEventMeshIp, destEventMeshPort, e); + result = String.format("redirectClientByIpPort fail! sessionMap size {%d}, {clientIp=%s clientPort=%s " + + "destEventMeshIp=%s destEventMeshPort=%s}, result {%s}, errorMsg : %s", + sessionMap.size(), ip, port, destEventMeshIp, destEventMeshPort, redirectResult, e + .getMessage()); + httpExchange.sendResponseHeaders(200, 0); + out.write(result.getBytes()); + return; + } + result = String.format("redirectClientByIpPort success! sessionMap size {%d}, {ip=%s port=%s " + + "destEventMeshIp=%s destEventMeshPort=%s}, result {%s} ", + sessionMap.size(), ip, port, destEventMeshIp, destEventMeshPort, redirectResult); + httpExchange.sendResponseHeaders(200, 0); + out.write(result.getBytes()); + } catch (Exception e) { + logger.error("redirectClientByIpPort fail...", e); + } finally { + if (out != null) { + try { + out.close(); + } catch (IOException e) { + logger.warn("out close failed...", e); + } + } + } + + } +} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/RedirectClientByPathHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/RedirectClientByPathHandler.java new file mode 100644 index 0000000000..b605a1cc64 --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/RedirectClientByPathHandler.java @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.admin.handler; + +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.EventMeshTcp2Client; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.group.ClientSessionGroupMapping; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session; +import org.apache.eventmesh.runtime.util.NetUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * redirect subsystem for path + */ +public class RedirectClientByPathHandler implements HttpHandler { + + private static final Logger logger = LoggerFactory.getLogger(RedirectClientByPathHandler.class); + + private EventMeshTCPServer eventMeshTCPServer; + + public RedirectClientByPathHandler(EventMeshTCPServer eventMeshTCPServer) { + this.eventMeshTCPServer = eventMeshTCPServer; + } + + @Override + public void handle(HttpExchange httpExchange) throws IOException { + String result = ""; + OutputStream out = httpExchange.getResponseBody(); + try { + String queryString = httpExchange.getRequestURI().getQuery(); + Map queryStringInfo = NetUtils.formData2Dic(queryString); + String path = queryStringInfo.get(EventMeshConstants.MANAGE_PATH); + String destEventMeshIp = queryStringInfo.get(EventMeshConstants.MANAGE_DEST_IP); + String destEventMeshPort = queryStringInfo.get(EventMeshConstants.MANAGE_DEST_PORT); + + if (StringUtils.isBlank(path) || StringUtils.isBlank(destEventMeshIp) || StringUtils.isBlank(destEventMeshPort) || + !StringUtils.isNumeric(destEventMeshPort)) { + httpExchange.sendResponseHeaders(200, 0); + result = "params illegal!"; + out.write(result.getBytes()); + return; + } + logger.info("redirectClientByPath in admin,path:{},destIp:{},destPort:{}====================", path, destEventMeshIp, destEventMeshPort); + ClientSessionGroupMapping clientSessionGroupMapping = eventMeshTCPServer.getClientSessionGroupMapping(); + ConcurrentHashMap sessionMap = clientSessionGroupMapping.getSessionMap(); + String redirectResult = ""; + try { + if (!sessionMap.isEmpty()) { + for (Session session : sessionMap.values()) { + if (session.getClient().getPath().contains(path)) { + redirectResult += "|"; + redirectResult += EventMeshTcp2Client.redirectClient2NewEventMesh(eventMeshTCPServer, destEventMeshIp, Integer.parseInt(destEventMeshPort), + session, clientSessionGroupMapping); + } + } + } + } catch (Exception e) { + logger.error("clientManage|redirectClientByPath|fail|path={}|destEventMeshIp" + + "={}|destEventMeshPort={},errMsg={}", path, destEventMeshIp, destEventMeshPort, e); + result = String.format("redirectClientByPath fail! sessionMap size {%d}, {path=%s " + + "destEventMeshIp=%s destEventMeshPort=%s}, result {%s}, errorMsg : %s", + sessionMap.size(), path, destEventMeshIp, destEventMeshPort, redirectResult, e + .getMessage()); + httpExchange.sendResponseHeaders(200, 0); + out.write(result.getBytes()); + return; + } + result = String.format("redirectClientByPath success! sessionMap size {%d}, {path=%s " + + "destEventMeshIp=%s destEventMeshPort=%s}, result {%s} ", + sessionMap.size(), path, destEventMeshIp, destEventMeshPort, redirectResult); + httpExchange.sendResponseHeaders(200, 0); + out.write(result.getBytes()); + } catch (Exception e) { + logger.error("redirectClientByPath fail...", e); + } finally { + if (out != null) { + try { + out.close(); + } catch (IOException e) { + logger.warn("out close failed...", e); + } + } + } + + } +} \ No newline at end of file diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/RedirectClientBySubSystemHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/RedirectClientBySubSystemHandler.java new file mode 100644 index 0000000000..400c8c0e8e --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/RedirectClientBySubSystemHandler.java @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.admin.handler; + +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.EventMeshTcp2Client; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.group.ClientSessionGroupMapping; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session; +import org.apache.eventmesh.runtime.util.NetUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * redirect subsystem for subsys and dcn + */ +public class RedirectClientBySubSystemHandler implements HttpHandler { + + private static final Logger logger = LoggerFactory.getLogger(RedirectClientBySubSystemHandler.class); + + private final EventMeshTCPServer eventMeshTCPServer; + + public RedirectClientBySubSystemHandler(EventMeshTCPServer eventMeshTCPServer) { + this.eventMeshTCPServer = eventMeshTCPServer; + } + + @Override + public void handle(HttpExchange httpExchange) throws IOException { + String result = ""; + OutputStream out = httpExchange.getResponseBody(); + try { + String queryString = httpExchange.getRequestURI().getQuery(); + Map queryStringInfo = NetUtils.formData2Dic(queryString); + String subSystem = queryStringInfo.get(EventMeshConstants.MANAGE_SUBSYSTEM); + String destEventMeshIp = queryStringInfo.get(EventMeshConstants.MANAGE_DEST_IP); + String destEventMeshPort = queryStringInfo.get(EventMeshConstants.MANAGE_DEST_PORT); + + if (!StringUtils.isNumeric(subSystem) + || StringUtils.isBlank(destEventMeshIp) || StringUtils.isBlank(destEventMeshPort) + || !StringUtils.isNumeric(destEventMeshPort)) { + httpExchange.sendResponseHeaders(200, 0); + result = "params illegal!"; + out.write(result.getBytes()); + return; + } + logger.info("redirectClientBySubSystem in admin,subsys:{},destIp:{},destPort:{}====================", subSystem, destEventMeshIp, destEventMeshPort); + ClientSessionGroupMapping clientSessionGroupMapping = eventMeshTCPServer.getClientSessionGroupMapping(); + ConcurrentHashMap sessionMap = clientSessionGroupMapping.getSessionMap(); + String redirectResult = ""; + try { + if (!sessionMap.isEmpty()) { + for (Session session : sessionMap.values()) { + if (session.getClient().getSubsystem().equals(subSystem)) { + redirectResult += "|"; + redirectResult += EventMeshTcp2Client.redirectClient2NewEventMesh(eventMeshTCPServer, destEventMeshIp, Integer.parseInt(destEventMeshPort), + session, clientSessionGroupMapping); + } + } + } + } catch (Exception e) { + logger.error("clientManage|redirectClientBySubSystem|fail|subSystem={}|destEventMeshIp" + + "={}|destEventMeshPort={},errMsg={}", subSystem, destEventMeshIp, destEventMeshPort, e); + result = String.format("redirectClientBySubSystem fail! sessionMap size {%d}, {subSystem=%s " + + "destEventMeshIp=%s destEventMeshPort=%s}, result {%s}, errorMsg : %s", + sessionMap.size(), subSystem, destEventMeshIp, destEventMeshPort, redirectResult, e + .getMessage()); + httpExchange.sendResponseHeaders(200, 0); + out.write(result.getBytes()); + return; + } + result = String.format("redirectClientBySubSystem success! sessionMap size {%d}, {subSystem=%s " + + "destEventMeshIp=%s destEventMeshPort=%s}, result {%s} ", + sessionMap.size(), subSystem, destEventMeshIp, destEventMeshPort, redirectResult); + httpExchange.sendResponseHeaders(200, 0); + out.write(result.getBytes()); + } catch (Exception e) { + logger.error("redirectClientBySubSystem fail...", e); + } finally { + if (out != null) { + try { + out.close(); + } catch (IOException e) { + logger.warn("out close failed...", e); + } + } + } + } +} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/RejectAllClientHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/RejectAllClientHandler.java new file mode 100644 index 0000000000..6314c489ac --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/RejectAllClientHandler.java @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.admin.handler; + +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.EventMeshTcp2Client; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.group.ClientSessionGroupMapping; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session; +import org.apache.eventmesh.runtime.util.NetUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class RejectAllClientHandler implements HttpHandler { + + private static final Logger logger = LoggerFactory.getLogger(RejectAllClientHandler.class); + + private final EventMeshTCPServer eventMeshTCPServer; + + public RejectAllClientHandler(EventMeshTCPServer eventMeshTCPServer) { + this.eventMeshTCPServer = eventMeshTCPServer; + } + + /** + * remove all clients accessed by eventMesh + * + * @param httpExchange + * @throws IOException + */ + @Override + public void handle(HttpExchange httpExchange) throws IOException { + String result = ""; + OutputStream out = httpExchange.getResponseBody(); + try { + ClientSessionGroupMapping clientSessionGroupMapping = eventMeshTCPServer.getClientSessionGroupMapping(); + ConcurrentHashMap sessionMap = clientSessionGroupMapping.getSessionMap(); + final List successRemoteAddrs = new ArrayList<>(); + try { + logger.info("rejectAllClient in admin===================="); + if (!sessionMap.isEmpty()) { + for (Map.Entry entry : sessionMap.entrySet()) { + InetSocketAddress addr = EventMeshTcp2Client.serverGoodby2Client(eventMeshTCPServer, entry.getValue(), clientSessionGroupMapping); + if (addr != null) { + successRemoteAddrs.add(addr); + } + } + } + } catch (Exception e) { + logger.error("clientManage|rejectAllClient|fail", e); + result = String.format("rejectAllClient fail! sessionMap size {%d}, had reject {%s}, errorMsg : %s", + sessionMap.size(), NetUtils.addressToString(successRemoteAddrs), e.getMessage()); + httpExchange.sendResponseHeaders(200, 0); + out.write(result.getBytes()); + return; + } + result = String.format("rejectAllClient success! sessionMap size {%d}, had reject {%s}", sessionMap.size(), + NetUtils.addressToString(successRemoteAddrs)); + httpExchange.sendResponseHeaders(200, 0); + out.write(result.getBytes()); + } catch (Exception e) { + logger.error("rejectAllClient fail...", e); + } finally { + if (out != null) { + try { + out.close(); + } catch (IOException e) { + logger.warn("out close failed...", e); + } + } + } + } +} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/RejectClientByIpPortHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/RejectClientByIpPortHandler.java new file mode 100644 index 0000000000..3ddd45e492 --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/RejectClientByIpPortHandler.java @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.admin.handler; + +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.EventMeshTcp2Client; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.group.ClientSessionGroupMapping; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session; +import org.apache.eventmesh.runtime.util.NetUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class RejectClientByIpPortHandler implements HttpHandler { + + private static final Logger logger = LoggerFactory.getLogger(RejectClientByIpPortHandler.class); + + private EventMeshTCPServer eventMeshTCPServer; + + public RejectClientByIpPortHandler(EventMeshTCPServer eventMeshTCPServer) { + this.eventMeshTCPServer = eventMeshTCPServer; + } + + @Override + public void handle(HttpExchange httpExchange) throws IOException { + String result = ""; + OutputStream out = httpExchange.getResponseBody(); + try { + String queryString = httpExchange.getRequestURI().getQuery(); + Map queryStringInfo = NetUtils.formData2Dic(queryString); + String ip = queryStringInfo.get(EventMeshConstants.MANAGE_IP); + String port = queryStringInfo.get(EventMeshConstants.MANAGE_PORT); + + if (StringUtils.isBlank(ip) || StringUtils.isBlank(port)) { + httpExchange.sendResponseHeaders(200, 0); + result = "params illegal!"; + out.write(result.getBytes()); + return; + } + logger.info("rejectClientByIpPort in admin,ip:{},port:{}====================", ip, port); + ClientSessionGroupMapping clientSessionGroupMapping = eventMeshTCPServer.getClientSessionGroupMapping(); + ConcurrentHashMap sessionMap = clientSessionGroupMapping.getSessionMap(); + final List successRemoteAddrs = new ArrayList(); + try { + if (!sessionMap.isEmpty()) { + for (Map.Entry entry : sessionMap.entrySet()) { + if (entry.getKey().getHostString().equals(ip) && String.valueOf(entry.getKey().getPort()).equals(port)) { + InetSocketAddress addr = EventMeshTcp2Client.serverGoodby2Client(eventMeshTCPServer, entry.getValue(), clientSessionGroupMapping); + if (addr != null) { + successRemoteAddrs.add(addr); + } + } + } + } + } catch (Exception e) { + logger.error("clientManage|rejectClientByIpPort|fail|ip={}|port={},errMsg={}", ip, port, e); + result = String.format("rejectClientByIpPort fail! {ip=%s port=%s}, had reject {%s}, errorMsg : %s", ip, + port, NetUtils.addressToString(successRemoteAddrs), e.getMessage()); + httpExchange.sendResponseHeaders(200, 0); + out.write(result.getBytes()); + return; + } + + result = String.format("rejectClientByIpPort success! {ip=%s port=%s}, had reject {%s}", ip, port, + NetUtils.addressToString(successRemoteAddrs)); + httpExchange.sendResponseHeaders(200, 0); + out.write(result.getBytes()); + } catch (Exception e) { + logger.error("rejectClientByIpPort fail...", e); + } finally { + if (out != null) { + try { + out.close(); + } catch (IOException e) { + logger.warn("out close failed...", e); + } + } + } + + } +} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/RejectClientBySubSystemHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/RejectClientBySubSystemHandler.java new file mode 100644 index 0000000000..e5208c665c --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/RejectClientBySubSystemHandler.java @@ -0,0 +1,122 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.admin.handler; + +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.EventMeshTcp2Client; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.group.ClientSessionGroupMapping; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session; +import org.apache.eventmesh.runtime.util.NetUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class RejectClientBySubSystemHandler implements HttpHandler { + + private static final Logger logger = LoggerFactory.getLogger(RejectClientBySubSystemHandler.class); + + private EventMeshTCPServer eventMeshTCPServer; + + public RejectClientBySubSystemHandler(EventMeshTCPServer eventMeshTCPServer) { + this.eventMeshTCPServer = eventMeshTCPServer; + } + + private String printClients(List clients) { + if (clients.isEmpty()) { + return "no session had been closed"; + } + StringBuilder sb = new StringBuilder(); + for (InetSocketAddress addr : clients) { + sb.append(addr).append("|"); + } + return sb.toString(); + } + + /** + * remove c client by dcn and susysId + * @param httpExchange + * @throws IOException + */ + @Override + public void handle(HttpExchange httpExchange) throws IOException { + String result = ""; + OutputStream out = httpExchange.getResponseBody(); + try { + String queryString = httpExchange.getRequestURI().getQuery(); + Map queryStringInfo = NetUtils.formData2Dic(queryString); + String subSystem = queryStringInfo.get(EventMeshConstants.MANAGE_SUBSYSTEM); + + if (StringUtils.isBlank(subSystem)) { + httpExchange.sendResponseHeaders(200, 0); + result = "params illegal!"; + out.write(result.getBytes()); + return; + } + + logger.info("rejectClientBySubSystem in admin,subsys:{}====================", subSystem); + ClientSessionGroupMapping clientSessionGroupMapping = eventMeshTCPServer.getClientSessionGroupMapping(); + ConcurrentHashMap sessionMap = clientSessionGroupMapping.getSessionMap(); + final List successRemoteAddrs = new ArrayList(); + try { + if (!sessionMap.isEmpty()) { + for (Session session : sessionMap.values()) { + if (session.getClient().getSubsystem().equals(subSystem)) { + InetSocketAddress addr = EventMeshTcp2Client.serverGoodby2Client(eventMeshTCPServer, session, clientSessionGroupMapping); + if (addr != null) { + successRemoteAddrs.add(addr); + } + } + } + } + } catch (Exception e) { + logger.error("clientManage|rejectClientBySubSystem|fail|subSystemId={},errMsg={}", subSystem, e); + result = String.format("rejectClientBySubSystem fail! sessionMap size {%d}, had reject {%d} , {" + + "subSystemId=%s}, errorMsg : %s", sessionMap.size(), printClients(successRemoteAddrs), subSystem, e.getMessage()); + httpExchange.sendResponseHeaders(200, 0); + out.write(result.getBytes()); + return; + } + result = String.format("rejectClientBySubSystem success! sessionMap size {%d}, had reject {%s} , {" + + "subSystemId=%s}", sessionMap.size(), printClients(successRemoteAddrs), subSystem); + httpExchange.sendResponseHeaders(200, 0); + out.write(result.getBytes()); + } catch (Exception e) { + logger.error("rejectClientBySubSystem fail...", e); + } finally { + if (out != null) { + try { + out.close(); + } catch (IOException e) { + logger.warn("out close failed...", e); + } + } + } + + } +} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/ShowClientBySystemHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/ShowClientBySystemHandler.java new file mode 100644 index 0000000000..07cbbb20c8 --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/ShowClientBySystemHandler.java @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.admin.handler; + +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.group.ClientSessionGroupMapping; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session; +import org.apache.eventmesh.runtime.util.NetUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class ShowClientBySystemHandler implements HttpHandler { + + private static final Logger logger = LoggerFactory.getLogger(ShowClientBySystemHandler.class); + + private final EventMeshTCPServer eventMeshTCPServer; + + public ShowClientBySystemHandler(EventMeshTCPServer eventMeshTCPServer) { + this.eventMeshTCPServer = eventMeshTCPServer; + } + + /** + * print clientInfo by subsys + * + * @param httpExchange + * @throws IOException + */ + @Override + public void handle(HttpExchange httpExchange) throws IOException { + String result = ""; + OutputStream out = httpExchange.getResponseBody(); + try { + String queryString = httpExchange.getRequestURI().getQuery(); + Map queryStringInfo = NetUtils.formData2Dic(queryString); + String subSystem = queryStringInfo.get(EventMeshConstants.MANAGE_SUBSYSTEM); + + String newLine = System.getProperty("line.separator"); + logger.info("showClientBySubsys,subsys:{}=================", subSystem); + ClientSessionGroupMapping clientSessionGroupMapping = eventMeshTCPServer.getClientSessionGroupMapping(); + ConcurrentHashMap sessionMap = clientSessionGroupMapping.getSessionMap(); + if (!sessionMap.isEmpty()) { + for (Session session : sessionMap.values()) { + if (session.getClient().getSubsystem().equals(subSystem)) { + UserAgent userAgent = session.getClient(); + result += String.format("pid=%s | ip=%s | port=%s | path=%s | purpose=%s", userAgent.getPid(), userAgent + .getHost(), userAgent.getPort(), userAgent.getPath(), userAgent.getPurpose()) + newLine; + } + } + } + httpExchange.sendResponseHeaders(200, 0); + out.write(result.getBytes()); + } catch (Exception e) { + logger.error("ShowClientBySystemAndHandler fail...", e); + } finally { + if (out != null) { + try { + out.close(); + } catch (IOException e) { + logger.warn("out close failed...", e); + } + } + } + } + + +} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/ShowClientHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/ShowClientHandler.java new file mode 100644 index 0000000000..64d5c5df04 --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/ShowClientHandler.java @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.admin.handler; + +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; +import org.apache.commons.collections4.MapUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.group.ClientSessionGroupMapping; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.util.*; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * This handler used to print the total client info + */ +public class ShowClientHandler implements HttpHandler { + + private static final Logger logger = LoggerFactory.getLogger(ShowClientHandler.class); + + private final EventMeshTCPServer eventMeshTCPServer; + + public ShowClientHandler(EventMeshTCPServer eventMeshTCPServer) { + this.eventMeshTCPServer = eventMeshTCPServer; + } + + @Override + public void handle(HttpExchange httpExchange) throws IOException { + String result = ""; + OutputStream out = httpExchange.getResponseBody(); + try { + String newLine = System.getProperty("line.separator"); + logger.info("showAllClient================="); + ClientSessionGroupMapping clientSessionGroupMapping = eventMeshTCPServer.getClientSessionGroupMapping(); + + HashMap statMap = new HashMap(); + + Map sessionMap = clientSessionGroupMapping.getSessionMap(); + if (!sessionMap.isEmpty()) { + for (Session session : sessionMap.values()) { + String key = session.getClient().getSubsystem(); + if (!statMap.containsKey(key)) { + statMap.put(key, new AtomicInteger(1)); + } else { + statMap.get(key).incrementAndGet(); + } + } + + for (Map.Entry entry : statMap.entrySet()) { + result += String.format("System=%s | ClientNum=%d", entry.getKey(), entry.getValue().intValue()) + + newLine; + } + } + + httpExchange.sendResponseHeaders(200, 0); + out.write(result.getBytes()); + } catch (Exception e) { + logger.error("ShowClientHandler fail...", e); + } finally { + if (out != null) { + try { + out.close(); + } catch (IOException e) { + logger.warn("out close failed...", e); + } + } + } + + } +} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/ShowListenClientByTopicHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/ShowListenClientByTopicHandler.java new file mode 100644 index 0000000000..6d8cbe4bcc --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/ShowListenClientByTopicHandler.java @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.admin.handler; + +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.group.ClientGroupWrapper; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.group.ClientSessionGroupMapping; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session; +import org.apache.eventmesh.runtime.util.NetUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.OutputStream; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +/** + * query client subscription by topic + */ +public class ShowListenClientByTopicHandler implements HttpHandler { + + private Logger logger = LoggerFactory.getLogger(ShowListenClientByTopicHandler.class); + + private final EventMeshTCPServer eventMeshTCPServer; + + public ShowListenClientByTopicHandler(EventMeshTCPServer eventMeshTCPServer) { + this.eventMeshTCPServer = eventMeshTCPServer; + } + + @Override + public void handle(HttpExchange httpExchange) throws IOException { + String result = ""; + OutputStream out = httpExchange.getResponseBody(); + try { + String queryString = httpExchange.getRequestURI().getQuery(); + Map queryStringInfo = NetUtils.formData2Dic(queryString); + String topic = queryStringInfo.get(EventMeshConstants.MANAGE_TOPIC); + + String newLine = System.getProperty("line.separator"); + logger.info("showListeningClientByTopic,topic:{}=================", topic); + ClientSessionGroupMapping clientSessionGroupMapping = eventMeshTCPServer.getClientSessionGroupMapping(); + ConcurrentHashMap clientGroupMap = clientSessionGroupMapping.getClientGroupMap(); + if (!clientGroupMap.isEmpty()) { + for (ClientGroupWrapper cgw : clientGroupMap.values()) { + Set listenSessionSet = cgw.getTopic2sessionInGroupMapping().get(topic); + if (listenSessionSet != null && listenSessionSet.size() > 0) { + result += String.format("group:%s", cgw.getConsumerGroup()) + newLine; + for (Session session : listenSessionSet) { + UserAgent userAgent = session.getClient(); + result += String.format("pid=%s | ip=%s | port=%s | path=%s | version=%s", userAgent.getPid(), userAgent + .getHost(), userAgent.getPort(), userAgent.getPath(), userAgent.getVersion()) + newLine; + } + } + } + } + httpExchange.sendResponseHeaders(200, 0); + out.write(result.getBytes()); + } catch (Exception e) { + logger.error("ShowListenClientByTopicHandler fail...", e); + } finally { + if (out != null) { + try { + out.close(); + } catch (IOException e) { + logger.warn("out close failed...", e); + } + } + } + + } +} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/boot/AbrstractHTTPServer.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/AbrstractHTTPServer.java similarity index 78% rename from eventmesh-emesher/src/main/java/com/webank/emesher/boot/AbrstractHTTPServer.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/AbrstractHTTPServer.java index 1cf813f0ad..4e902a6d9d 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/boot/AbrstractHTTPServer.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/AbrstractHTTPServer.java @@ -15,52 +15,76 @@ * limitations under the License. */ -package com.webank.emesher.boot; +package org.apache.eventmesh.runtime.boot; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLEngine; import com.google.common.base.Preconditions; -import com.webank.emesher.common.Pair; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.protocol.http.async.AsyncContext; -import com.webank.emesher.core.protocol.http.processor.inf.HttpRequestProcessor; -import com.webank.emesher.metrics.http.HTTPMetricsServer; -import com.webank.emesher.util.ProxyUtil; -import com.webank.eventmesh.common.ThreadPoolFactory; -import com.webank.eventmesh.common.command.HttpCommand; -import com.webank.eventmesh.common.protocol.http.body.Body; -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import com.webank.eventmesh.common.protocol.http.common.ProtocolVersion; -import com.webank.eventmesh.common.protocol.http.common.ProxyRetCode; -import com.webank.eventmesh.common.protocol.http.common.RequestCode; -import com.webank.eventmesh.common.protocol.http.header.Header; + import io.netty.bootstrap.ServerBootstrap; -import io.netty.channel.*; +import io.netty.channel.ChannelDuplexHandler; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; +import io.netty.channel.ChannelPipeline; +import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; -import io.netty.handler.codec.http.*; -import io.netty.handler.codec.http.multipart.*; +import io.netty.handler.codec.http.DefaultFullHttpResponse; +import io.netty.handler.codec.http.FullHttpResponse; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpHeaderValues; +import io.netty.handler.codec.http.HttpMethod; +import io.netty.handler.codec.http.HttpObjectAggregator; +import io.netty.handler.codec.http.HttpRequest; +import io.netty.handler.codec.http.HttpRequestDecoder; +import io.netty.handler.codec.http.HttpResponseEncoder; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.netty.handler.codec.http.HttpVersion; +import io.netty.handler.codec.http.QueryStringDecoder; +import io.netty.handler.codec.http.multipart.Attribute; +import io.netty.handler.codec.http.multipart.DefaultHttpDataFactory; +import io.netty.handler.codec.http.multipart.DiskAttribute; +import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder; +import io.netty.handler.codec.http.multipart.InterfaceHttpData; import io.netty.handler.ssl.SslHandler; import io.netty.handler.timeout.IdleState; import io.netty.handler.timeout.IdleStateEvent; + import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; -import org.apache.rocketmq.remoting.common.RemotingHelper; +import org.apache.eventmesh.common.ThreadPoolFactory; +import org.apache.eventmesh.common.command.HttpCommand; +import org.apache.eventmesh.common.protocol.http.body.Body; +import org.apache.eventmesh.common.protocol.http.common.EventMeshRetCode; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; +import org.apache.eventmesh.common.protocol.http.common.ProtocolVersion; +import org.apache.eventmesh.common.protocol.http.common.RequestCode; +import org.apache.eventmesh.common.protocol.http.header.Header; +import org.apache.eventmesh.runtime.common.Pair; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.http.async.AsyncContext; +import org.apache.eventmesh.runtime.core.protocol.http.processor.inf.HttpRequestProcessor; +import org.apache.eventmesh.runtime.metrics.http.HTTPMetricsServer; +import org.apache.eventmesh.runtime.util.EventMeshUtil; +import org.apache.eventmesh.runtime.util.RemotingHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLEngine; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.RejectedExecutionException; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicInteger; - public abstract class AbrstractHTTPServer extends AbstractRemotingServer { - public Logger logger = LoggerFactory.getLogger(this.getClass()); + public Logger httpServerLogger = LoggerFactory.getLogger(this.getClass()); public Logger httpLogger = LoggerFactory.getLogger("http"); @@ -73,7 +97,7 @@ public abstract class AbrstractHTTPServer extends AbstractRemotingServer { private boolean useTLS; public ThreadPoolExecutor asyncContextCompleteHandler = - ThreadPoolFactory.createThreadPoolExecutor(10, 10, "proxy-http-asyncContext-"); + ThreadPoolFactory.createThreadPoolExecutor(10, 10, "eventMesh-http-asyncContext-"); static { DiskAttribute.deleteOnExitTemporaryFile = false; @@ -105,7 +129,7 @@ public void sendError(ChannelHandlerContext ctx, FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status); response.headers().add(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.TEXT_PLAIN + - "; charset=" + ProxyConstants.DEFAULT_CHARSET); + "; charset=" + EventMeshConstants.DEFAULT_CHARSET); response.headers().add(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes()); response.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); @@ -134,21 +158,21 @@ public void start() throws Exception { .channel(NioServerSocketChannel.class) .childHandler(new HttpsServerInitializer(SSLContextFactory.getSslContext())).childOption(ChannelOption.SO_KEEPALIVE, Boolean.TRUE); try { - logger.info("HTTPServer[port={}] started......", this.port); + httpServerLogger.info("HTTPServer[port={}] started......", this.port); ChannelFuture future = b.bind(this.port).sync(); future.channel().closeFuture().sync(); } catch (Exception e) { - logger.error("HTTPServer start Err!", e); + httpServerLogger.error("HTTPServer start Err!", e); try { shutdown(); } catch (Exception e1) { - logger.error("HTTPServer shutdown Err!", e); + httpServerLogger.error("HTTPServer shutdown Err!", e); } return; } }; - Thread t = new Thread(r, "proxy-http-server"); + Thread t = new Thread(r, "eventMesh-http-server"); t.start(); started.compareAndSet(false, true); } @@ -230,7 +254,7 @@ protected void channelRead0(ChannelHandlerContext ctx, HttpRequest httpRequest) HttpCommand responseCommand = null; if (!ProtocolVersion.contains(protocolVersion)) { - responseCommand = requestCommand.createHttpCommandResponse(ProxyRetCode.PROXY_PROTOCOL_HEADER_ERR.getRetCode(), ProxyRetCode.PROXY_PROTOCOL_HEADER_ERR.getErrMsg()); + responseCommand = requestCommand.createHttpCommandResponse(EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getErrMsg()); sendResponse(ctx, responseCommand.httpResponse()); return; } @@ -239,13 +263,13 @@ protected void channelRead0(ChannelHandlerContext ctx, HttpRequest httpRequest) || !StringUtils.isNumeric(requestCode) || !RequestCode.contains(Integer.valueOf(requestCode)) || !processorTable.containsKey(Integer.valueOf(requestCode))) { - responseCommand = requestCommand.createHttpCommandResponse(ProxyRetCode.PROXY_REQUESTCODE_INVALID.getRetCode(), ProxyRetCode.PROXY_REQUESTCODE_INVALID.getErrMsg()); + responseCommand = requestCommand.createHttpCommandResponse(EventMeshRetCode.EVENTMESH_REQUESTCODE_INVALID.getRetCode(), EventMeshRetCode.EVENTMESH_REQUESTCODE_INVALID.getErrMsg()); sendResponse(ctx, responseCommand.httpResponse()); return; } if (!started.get()) { - responseCommand = requestCommand.createHttpCommandResponse(ProxyRetCode.PROXY_STOP.getRetCode(), ProxyRetCode.PROXY_STOP.getErrMsg()); + responseCommand = requestCommand.createHttpCommandResponse(EventMeshRetCode.EVENTMESH_STOP.getRetCode(), EventMeshRetCode.EVENTMESH_STOP.getErrMsg()); sendResponse(ctx, responseCommand.httpResponse()); return; } @@ -254,7 +278,7 @@ protected void channelRead0(ChannelHandlerContext ctx, HttpRequest httpRequest) requestCommand.setHeader(Header.buildHeader(requestCode, parseHTTPHeader(httpRequest))); requestCommand.setBody(Body.buildBody(requestCode, bodyMap)); } catch (Exception e) { - responseCommand = requestCommand.createHttpCommandResponse(ProxyRetCode.PROXY_RUNTIME_ERR.getRetCode(), ProxyRetCode.PROXY_RUNTIME_ERR.getErrMsg() + ProxyUtil.stackTrace(e, 3)); + responseCommand = requestCommand.createHttpCommandResponse(EventMeshRetCode.EVENTMESH_RUNTIME_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_RUNTIME_ERR.getErrMsg() + EventMeshUtil.stackTrace(e, 3)); sendResponse(ctx, responseCommand.httpResponse()); return; } @@ -264,9 +288,9 @@ protected void channelRead0(ChannelHandlerContext ctx, HttpRequest httpRequest) } AsyncContext asyncContext = new AsyncContext(requestCommand, responseCommand, asyncContextCompleteHandler); - processProxyRequest(ctx, asyncContext); + processEventMeshRequest(ctx, asyncContext); } catch (Exception ex) { - logger.error("AbrstractHTTPServer.HTTPHandler.channelRead0 err", ex); + httpServerLogger.error("AbrstractHTTPServer.HTTPHandler.channelRead0 err", ex); } finally { try { decoder.destroy(); @@ -275,14 +299,14 @@ protected void channelRead0(ChannelHandlerContext ctx, HttpRequest httpRequest) } } - public void processProxyRequest(final ChannelHandlerContext ctx, - final AsyncContext asyncContext) { + public void processEventMeshRequest(final ChannelHandlerContext ctx, + final AsyncContext asyncContext) { final Pair choosed = processorTable.get(Integer.valueOf(asyncContext.getRequest().getRequestCode())); try { choosed.getObject2().submit(() -> { try { if (choosed.getObject1().rejectRequest()) { - HttpCommand responseCommand = asyncContext.getRequest().createHttpCommandResponse(ProxyRetCode.PROXY_REJECT_BY_PROCESSOR_ERROR.getRetCode(), ProxyRetCode.PROXY_REJECT_BY_PROCESSOR_ERROR.getErrMsg()); + HttpCommand responseCommand = asyncContext.getRequest().createHttpCommandResponse(EventMeshRetCode.EVENTMESH_REJECT_BY_PROCESSOR_ERROR.getRetCode(), EventMeshRetCode.EVENTMESH_REJECT_BY_PROCESSOR_ERROR.getErrMsg()); asyncContext.onComplete(responseCommand); if (asyncContext.isComplete()) { if (httpLogger.isDebugEnabled()) { @@ -306,11 +330,11 @@ public void processProxyRequest(final ChannelHandlerContext ctx, sendResponse(ctx, asyncContext.getResponse().httpResponse()); } catch (Exception e) { - logger.error("process error", e); + httpServerLogger.error("process error", e); } }); } catch (RejectedExecutionException re) { - HttpCommand responseCommand = asyncContext.getRequest().createHttpCommandResponse(ProxyRetCode.OVERLOAD.getRetCode(), ProxyRetCode.OVERLOAD.getErrMsg()); + HttpCommand responseCommand = asyncContext.getRequest().createHttpCommandResponse(EventMeshRetCode.OVERLOAD.getRetCode(), EventMeshRetCode.OVERLOAD.getErrMsg()); asyncContext.onComplete(responseCommand); metrics.summaryMetrics.recordHTTPDiscard(); metrics.summaryMetrics.recordHTTPReqResTimeCost(System.currentTimeMillis() - responseCommand.getReqTime()); @@ -352,8 +376,8 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel()); int c = connections.incrementAndGet(); if (c > 20000) { - logger.warn("client|http|channelActive|remoteAddress={}|msg={}", remoteAddress, "too many client(20000) connect " + - "this proxy server"); + httpServerLogger.warn("client|http|channelActive|remoteAddress={}|msg={}", remoteAddress, "too many client(20000) connect " + + "this eventMesh server"); ctx.close(); return; } @@ -375,7 +399,7 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc IdleStateEvent event = (IdleStateEvent) evt; if (event.state().equals(IdleState.ALL_IDLE)) { final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel()); - logger.info("client|http|userEventTriggered|remoteAddress={}|msg={}", remoteAddress, evt.getClass() + httpServerLogger.info("client|http|userEventTriggered|remoteAddress={}|msg={}", remoteAddress, evt.getClass() .getName()); ctx.close(); } @@ -398,7 +422,7 @@ protected void initChannel(SocketChannel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); - if(sslContext != null && useTLS){ + if (sslContext != null && useTLS) { SSLEngine sslEngine = sslContext.createSSLEngine(); sslEngine.setUseClientMode(false); pipeline.addFirst("ssl", new SslHandler(sslEngine)); diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/boot/AbstractRemotingServer.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/AbstractRemotingServer.java similarity index 97% rename from eventmesh-emesher/src/main/java/com/webank/emesher/boot/AbstractRemotingServer.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/AbstractRemotingServer.java index 67c4264195..e3f643629d 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/boot/AbstractRemotingServer.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/AbstractRemotingServer.java @@ -15,17 +15,18 @@ * limitations under the License. */ -package com.webank.emesher.boot; +package org.apache.eventmesh.runtime.boot; + +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.atomic.AtomicInteger; -import com.webank.eventmesh.common.ThreadUtil; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; + +import org.apache.eventmesh.common.ThreadUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.atomic.AtomicInteger; - public abstract class AbstractRemotingServer { public Logger logger = LoggerFactory.getLogger(this.getClass()); diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/boot/ProxyHTTPServer.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/EventMeshHTTPServer.java similarity index 56% rename from eventmesh-emesher/src/main/java/com/webank/emesher/boot/ProxyHTTPServer.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/EventMeshHTTPServer.java index feb08b4d9c..84a2ab79b0 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/boot/ProxyHTTPServer.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/EventMeshHTTPServer.java @@ -15,41 +15,58 @@ * limitations under the License. */ -package com.webank.emesher.boot; - -import com.google.common.eventbus.EventBus; -import com.webank.emesher.common.ServiceState; -import com.webank.emesher.configuration.ProxyConfiguration; -import com.webank.emesher.core.protocol.http.consumer.ConsumerManager; -import com.webank.emesher.core.protocol.http.processor.*; -import com.webank.emesher.core.protocol.http.producer.ProducerManager; -import com.webank.emesher.core.protocol.http.push.AbstractHTTPPushRequest; -import com.webank.emesher.core.protocol.http.retry.HttpRetryer; -import com.webank.emesher.metrics.http.HTTPMetricsServer; -import com.webank.eventmesh.common.ThreadPoolFactory; -import com.webank.eventmesh.common.protocol.http.common.RequestCode; +package org.apache.eventmesh.runtime.boot; +import java.util.List; import java.util.concurrent.BlockingQueue; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; -public class ProxyHTTPServer extends AbrstractHTTPServer { +import com.google.common.eventbus.EventBus; - private ProxyServer proxyServer; +import org.apache.eventmesh.common.ThreadPoolFactory; +import org.apache.eventmesh.common.protocol.http.common.RequestCode; +import org.apache.eventmesh.runtime.common.ServiceState; +import org.apache.eventmesh.runtime.configuration.EventMeshHTTPConfiguration; +import org.apache.eventmesh.runtime.core.consumergroup.ConsumerGroupConf; +import org.apache.eventmesh.runtime.core.protocol.http.consumer.ConsumerManager; +import org.apache.eventmesh.runtime.core.protocol.http.processor.AdminMetricsProcessor; +import org.apache.eventmesh.runtime.core.protocol.http.processor.BatchSendMessageProcessor; +import org.apache.eventmesh.runtime.core.protocol.http.processor.BatchSendMessageV2Processor; +import org.apache.eventmesh.runtime.core.protocol.http.processor.HeartBeatProcessor; +import org.apache.eventmesh.runtime.core.protocol.http.processor.ReplyMessageProcessor; +import org.apache.eventmesh.runtime.core.protocol.http.processor.SendAsyncMessageProcessor; +import org.apache.eventmesh.runtime.core.protocol.http.processor.SendSyncMessageProcessor; +import org.apache.eventmesh.runtime.core.protocol.http.processor.SubscribeProcessor; +import org.apache.eventmesh.runtime.core.protocol.http.processor.UnSubscribeProcessor; +import org.apache.eventmesh.runtime.core.protocol.http.processor.inf.Client; +import org.apache.eventmesh.runtime.core.protocol.http.producer.ProducerManager; +import org.apache.eventmesh.runtime.core.protocol.http.push.AbstractHTTPPushRequest; +import org.apache.eventmesh.runtime.core.protocol.http.retry.HttpRetryer; +import org.apache.eventmesh.runtime.metrics.http.HTTPMetricsServer; + +public class EventMeshHTTPServer extends AbrstractHTTPServer { + + private EventMeshServer eventMeshServer; public ServiceState serviceState; - private ProxyConfiguration proxyConfiguration; + private EventMeshHTTPConfiguration eventMeshHttpConfiguration; - public ProxyHTTPServer(ProxyServer proxyServer, - ProxyConfiguration proxyConfiguration) { - super(proxyConfiguration.httpServerPort, proxyConfiguration.proxyServerUseTls); - this.proxyServer = proxyServer; - this.proxyConfiguration = proxyConfiguration; + public final ConcurrentHashMap localConsumerGroupMapping = new ConcurrentHashMap<>(); + + public final ConcurrentHashMap> localClientInfoMapping = new ConcurrentHashMap<>(); + + public EventMeshHTTPServer(EventMeshServer eventMeshServer, + EventMeshHTTPConfiguration eventMeshHttpConfiguration) { + super(eventMeshHttpConfiguration.httpServerPort, eventMeshHttpConfiguration.eventMeshServerUseTls); + this.eventMeshServer = eventMeshServer; + this.eventMeshHttpConfiguration = eventMeshHttpConfiguration; } - public ProxyServer getProxyServer() { - return proxyServer; + public EventMeshServer getEventMeshServer() { + return eventMeshServer; } public EventBus eventBus = new EventBus(); @@ -83,29 +100,29 @@ public void shutdownThreadPool() throws Exception { public void initThreadPool() throws Exception { - BlockingQueue batchMsgThreadPoolQueue = new LinkedBlockingQueue(proxyConfiguration.proxyServerBatchBlockQSize); - batchMsgExecutor = ThreadPoolFactory.createThreadPoolExecutor(proxyConfiguration.proxyServerBatchMsgThreadNum, - proxyConfiguration.proxyServerBatchMsgThreadNum, batchMsgThreadPoolQueue, "proxy-batchmsg-", true); + BlockingQueue batchMsgThreadPoolQueue = new LinkedBlockingQueue(eventMeshHttpConfiguration.eventMeshServerBatchBlockQSize); + batchMsgExecutor = ThreadPoolFactory.createThreadPoolExecutor(eventMeshHttpConfiguration.eventMeshServerBatchMsgThreadNum, + eventMeshHttpConfiguration.eventMeshServerBatchMsgThreadNum, batchMsgThreadPoolQueue, "eventMesh-batchmsg-", true); - BlockingQueue sendMsgThreadPoolQueue = new LinkedBlockingQueue(proxyConfiguration.proxyServerSendMsgBlockQSize); - sendMsgExecutor = ThreadPoolFactory.createThreadPoolExecutor(proxyConfiguration.proxyServerSendMsgThreadNum, - proxyConfiguration.proxyServerSendMsgThreadNum, sendMsgThreadPoolQueue, "proxy-sendmsg-",true); + BlockingQueue sendMsgThreadPoolQueue = new LinkedBlockingQueue(eventMeshHttpConfiguration.eventMeshServerSendMsgBlockQSize); + sendMsgExecutor = ThreadPoolFactory.createThreadPoolExecutor(eventMeshHttpConfiguration.eventMeshServerSendMsgThreadNum, + eventMeshHttpConfiguration.eventMeshServerSendMsgThreadNum, sendMsgThreadPoolQueue, "eventMesh-sendmsg-", true); - BlockingQueue pushMsgThreadPoolQueue = new LinkedBlockingQueue(proxyConfiguration.proxyServerPushMsgBlockQSize); - pushMsgExecutor = ThreadPoolFactory.createThreadPoolExecutor(proxyConfiguration.proxyServerPushMsgThreadNum, - proxyConfiguration.proxyServerPushMsgThreadNum, pushMsgThreadPoolQueue, "proxy-pushmsg-",true); + BlockingQueue pushMsgThreadPoolQueue = new LinkedBlockingQueue(eventMeshHttpConfiguration.eventMeshServerPushMsgBlockQSize); + pushMsgExecutor = ThreadPoolFactory.createThreadPoolExecutor(eventMeshHttpConfiguration.eventMeshServerPushMsgThreadNum, + eventMeshHttpConfiguration.eventMeshServerPushMsgThreadNum, pushMsgThreadPoolQueue, "eventMesh-pushmsg-", true); - BlockingQueue clientManageThreadPoolQueue = new LinkedBlockingQueue(proxyConfiguration.proxyServerClientManageBlockQSize); - clientManageExecutor = ThreadPoolFactory.createThreadPoolExecutor(proxyConfiguration.proxyServerClientManageThreadNum, - proxyConfiguration.proxyServerClientManageThreadNum, clientManageThreadPoolQueue, "proxy-clientmanage-",true); + BlockingQueue clientManageThreadPoolQueue = new LinkedBlockingQueue(eventMeshHttpConfiguration.eventMeshServerClientManageBlockQSize); + clientManageExecutor = ThreadPoolFactory.createThreadPoolExecutor(eventMeshHttpConfiguration.eventMeshServerClientManageThreadNum, + eventMeshHttpConfiguration.eventMeshServerClientManageThreadNum, clientManageThreadPoolQueue, "eventMesh-clientmanage-", true); BlockingQueue adminThreadPoolQueue = new LinkedBlockingQueue(50); - adminExecutor = ThreadPoolFactory.createThreadPoolExecutor(proxyConfiguration.proxyServerAdminThreadNum, - proxyConfiguration.proxyServerAdminThreadNum, adminThreadPoolQueue, "proxy-admin-",true); + adminExecutor = ThreadPoolFactory.createThreadPoolExecutor(eventMeshHttpConfiguration.eventMeshServerAdminThreadNum, + eventMeshHttpConfiguration.eventMeshServerAdminThreadNum, adminThreadPoolQueue, "eventMesh-admin-", true); BlockingQueue replyMessageThreadPoolQueue = new LinkedBlockingQueue(100); - replyMsgExecutor = ThreadPoolFactory.createThreadPoolExecutor(proxyConfiguration.proxyServerReplyMsgThreadNum, - proxyConfiguration.proxyServerReplyMsgThreadNum, replyMessageThreadPoolQueue, "proxy-replymsg-",true); + replyMsgExecutor = ThreadPoolFactory.createThreadPoolExecutor(eventMeshHttpConfiguration.eventMeshServerReplyMsgThreadNum, + eventMeshHttpConfiguration.eventMeshServerReplyMsgThreadNum, replyMessageThreadPoolQueue, "eventMesh-replymsg-", true); } public ThreadPoolExecutor getBatchMsgExecutor() { @@ -133,8 +150,8 @@ public ThreadPoolExecutor getAdminExecutor() { } public void init() throws Exception { - logger.info("==================ProxyHTTPServer Initialing=================="); - super.init("proxy-http"); + logger.info("==================EventMeshHTTPServer Initialing=================="); + super.init("eventMesh-http"); initThreadPool(); @@ -152,18 +169,20 @@ public void init() throws Exception { registerHTTPRequestProcessor(); - logger.info("--------------------------ProxyHTTPServer inited"); + logger.info("--------------------------EventMeshHTTPServer inited"); } + @Override public void start() throws Exception { super.start(); metrics.start(); consumerManager.start(); producerManager.start(); httpRetryer.start(); - logger.info("--------------------------ProxyHTTPServer started"); + logger.info("--------------------------EventMeshHTTPServer started"); } + @Override public void shutdown() throws Exception { super.shutdown(); @@ -179,7 +198,7 @@ public void shutdown() throws Exception { producerManager.shutdown(); httpRetryer.shutdown(); - logger.info("--------------------------ProxyHTTPServer shutdown"); + logger.info("--------------------------EventMeshHTTPServer shutdown"); } public void registerHTTPRequestProcessor() { @@ -202,6 +221,9 @@ public void registerHTTPRequestProcessor() { AdminMetricsProcessor adminMetricsProcessor = new AdminMetricsProcessor(this); registerProcessor(RequestCode.ADMIN_METRICS.getRequestCode(), adminMetricsProcessor, adminExecutor); + HeartBeatProcessor heartProcessor = new HeartBeatProcessor(this); + registerProcessor(RequestCode.HEARTBEAT.getRequestCode(), heartProcessor, clientManageExecutor); + SubscribeProcessor subscribeProcessor = new SubscribeProcessor(this); registerProcessor(RequestCode.SUBSCRIBE.getRequestCode(), subscribeProcessor, clientManageExecutor); @@ -224,8 +246,8 @@ public ServiceState getServiceState() { return serviceState; } - public ProxyConfiguration getProxyConfiguration() { - return proxyConfiguration; + public EventMeshHTTPConfiguration getEventMeshHttpConfiguration() { + return eventMeshHttpConfiguration; } public EventBus getEventBus() { diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/EventMeshServer.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/EventMeshServer.java new file mode 100644 index 0000000000..19d69533ef --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/EventMeshServer.java @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.boot; + +import org.apache.eventmesh.runtime.common.ServiceState; +import org.apache.eventmesh.runtime.configuration.EventMeshHTTPConfiguration; +import org.apache.eventmesh.runtime.configuration.EventMeshTCPConfiguration; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class EventMeshServer { + + public Logger logger = LoggerFactory.getLogger(this.getClass()); + + public EventMeshHTTPServer eventMeshHTTPServer; + + private EventMeshTCPServer eventMeshTCPServer; + + private EventMeshHTTPConfiguration eventMeshHttpConfiguration; + + private EventMeshTCPConfiguration eventMeshTCPConfiguration; + + private ServiceState serviceState; + + public EventMeshServer(EventMeshHTTPConfiguration eventMeshHttpConfiguration, + EventMeshTCPConfiguration eventMeshTCPConfiguration) { + this.eventMeshHttpConfiguration = eventMeshHttpConfiguration; + this.eventMeshTCPConfiguration = eventMeshTCPConfiguration; + } + + public void init() throws Exception { + eventMeshHTTPServer = new EventMeshHTTPServer(this, eventMeshHttpConfiguration); + eventMeshHTTPServer.init(); + eventMeshTCPServer = new EventMeshTCPServer(this, eventMeshTCPConfiguration); + if (eventMeshTCPConfiguration != null && eventMeshTCPConfiguration.eventMeshTcpServerEnabled) { + eventMeshTCPServer.init(); + } + + String eventstore = System.getProperty(EventMeshConstants.EVENT_STORE_PROPERTIES, System.getenv(EventMeshConstants.EVENT_STORE_ENV)); + logger.info("eventstore : {}", eventstore); + + serviceState = ServiceState.INITED; + logger.info("server state:{}", serviceState); + } + + public void start() throws Exception { + eventMeshHTTPServer.start(); + if (eventMeshTCPConfiguration != null && eventMeshTCPConfiguration.eventMeshTcpServerEnabled) { + eventMeshTCPServer.start(); + } + serviceState = ServiceState.RUNNING; + logger.info("server state:{}", serviceState); + } + + public void shutdown() throws Exception { + serviceState = ServiceState.STOPING; + logger.info("server state:{}", serviceState); + eventMeshHTTPServer.shutdown(); + if (eventMeshTCPConfiguration != null && eventMeshTCPConfiguration.eventMeshTcpServerEnabled) { + eventMeshTCPServer.shutdown(); + } + serviceState = ServiceState.STOPED; + logger.info("server state:{}", serviceState); + } + + public EventMeshHTTPServer getEventMeshHTTPServer() { + return eventMeshHTTPServer; + } + + public EventMeshTCPServer getEventMeshTCPServer() { + return eventMeshTCPServer; + } + + public ServiceState getServiceState() { + return serviceState; + } +} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/boot/ProxyStartup.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/EventMeshStartup.java similarity index 54% rename from eventmesh-emesher/src/main/java/com/webank/emesher/boot/ProxyStartup.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/EventMeshStartup.java index 7cb732f30b..4ae1191a98 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/boot/ProxyStartup.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/EventMeshStartup.java @@ -15,47 +15,47 @@ * limitations under the License. */ -package com.webank.emesher.boot; +package org.apache.eventmesh.runtime.boot; -import com.webank.emesher.configuration.AccessConfiguration; -import com.webank.emesher.configuration.ConfigurationWraper; -import com.webank.emesher.configuration.ProxyConfiguration; -import com.webank.emesher.constants.ProxyConstants; +import java.io.File; + +import org.apache.eventmesh.common.config.ConfigurationWraper; +import org.apache.eventmesh.runtime.configuration.EventMeshHTTPConfiguration; +import org.apache.eventmesh.runtime.configuration.EventMeshTCPConfiguration; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.File; - -public class ProxyStartup { +public class EventMeshStartup { - public static Logger logger = LoggerFactory.getLogger(ProxyStartup.class); + public static Logger logger = LoggerFactory.getLogger(EventMeshStartup.class); public static void main(String[] args) throws Exception { - try{ + try { ConfigurationWraper configurationWraper = - new ConfigurationWraper(ProxyConstants.PROXY_CONF_HOME + new ConfigurationWraper(EventMeshConstants.EVENTMESH_CONF_HOME + File.separator - + ProxyConstants.PROXY_CONF_FILE, false); - ProxyConfiguration proxyConfiguration = new ProxyConfiguration(configurationWraper); - proxyConfiguration.init(); - AccessConfiguration accessConfiguration = new AccessConfiguration(configurationWraper); - accessConfiguration.init(); - ProxyServer server = new ProxyServer(proxyConfiguration, accessConfiguration); + + EventMeshConstants.EVENTMESH_CONF_FILE, false); + EventMeshHTTPConfiguration eventMeshHttpConfiguration = new EventMeshHTTPConfiguration(configurationWraper); + eventMeshHttpConfiguration.init(); + EventMeshTCPConfiguration eventMeshTCPConfiguration = new EventMeshTCPConfiguration(configurationWraper); + eventMeshTCPConfiguration.init(); + EventMeshServer server = new EventMeshServer(eventMeshHttpConfiguration, eventMeshTCPConfiguration); server.init(); server.start(); Runtime.getRuntime().addShutdownHook(new Thread(() -> { try { - logger.info("proxy shutting down hook begin..."); + logger.info("eventMesh shutting down hook begin..."); long start = System.currentTimeMillis(); server.shutdown(); long end = System.currentTimeMillis(); - logger.info("proxy shutdown cost {}ms", end - start); + logger.info("eventMesh shutdown cost {}ms", end - start); } catch (Exception e) { logger.error("exception when shutdown...", e); } })); - }catch (Throwable e){ - logger.error("Proxy start fail.", e); + } catch (Throwable e) { + logger.error("EventMesh start fail.", e); e.printStackTrace(); } diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/EventMeshTCPServer.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/EventMeshTCPServer.java new file mode 100644 index 0000000000..12f37d3300 --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/EventMeshTCPServer.java @@ -0,0 +1,299 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.boot; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; + +import com.google.common.util.concurrent.RateLimiter; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.buffer.PooledByteBufAllocator; +import io.netty.channel.*; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.handler.timeout.IdleStateHandler; +import io.netty.handler.traffic.ChannelTrafficShapingHandler; +import io.netty.handler.traffic.GlobalTrafficShapingHandler; + +import org.apache.eventmesh.common.ThreadPoolFactory; +import org.apache.eventmesh.common.protocol.tcp.codec.Codec; +import org.apache.eventmesh.runtime.admin.controller.ClientManageController; +import org.apache.eventmesh.runtime.configuration.EventMeshTCPConfiguration; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.EventMeshTcpConnectionHandler; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.EventMeshTcpExceptionHandler; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.EventMeshTcpMessageDispatcher; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.group.ClientSessionGroupMapping; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push.retry.EventMeshTcpRetryer; +import org.apache.eventmesh.runtime.metrics.tcp.EventMeshTcpMonitor; +import org.apache.eventmesh.runtime.util.EventMeshThreadFactoryImpl; + +public class EventMeshTCPServer extends AbstractRemotingServer { + + private ClientSessionGroupMapping clientSessionGroupMapping; + + private EventMeshTcpRetryer eventMeshTcpRetryer; + + private EventMeshTcpMonitor eventMeshTcpMonitor; + + private ClientManageController clientManageController; + + private EventMeshServer eventMeshServer; + + private EventMeshTCPConfiguration eventMeshTCPConfiguration; + + private GlobalTrafficShapingHandler globalTrafficShapingHandler; + + private ScheduledExecutorService scheduler; + + private ExecutorService taskHandleExecutorService; + + private ExecutorService broadcastMsgDownstreamExecutorService; + + public void setClientSessionGroupMapping(ClientSessionGroupMapping clientSessionGroupMapping) { + this.clientSessionGroupMapping = clientSessionGroupMapping; + } + + public ClientManageController getClientManageController() { + return clientManageController; + } + + public void setClientManageController(ClientManageController clientManageController) { + this.clientManageController = clientManageController; + } + + public ScheduledExecutorService getScheduler() { + return scheduler; + } + + public void setScheduler(ScheduledExecutorService scheduler) { + this.scheduler = scheduler; + } + + public ExecutorService getTaskHandleExecutorService() { + return taskHandleExecutorService; + } + + public ExecutorService getBroadcastMsgDownstreamExecutorService() { + return broadcastMsgDownstreamExecutorService; + } + + public void setTaskHandleExecutorService(ExecutorService taskHandleExecutorService) { + this.taskHandleExecutorService = taskHandleExecutorService; + } + + public RateLimiter getRateLimiter() { + return rateLimiter; + } + + public void setRateLimiter(RateLimiter rateLimiter) { + this.rateLimiter = rateLimiter; + } + + private ScheduledFuture tcpRegisterTask; + + private RateLimiter rateLimiter; + + public EventMeshTCPServer(EventMeshServer eventMeshServer, + EventMeshTCPConfiguration eventMeshTCPConfiguration) { + super(); + this.eventMeshServer = eventMeshServer; + this.eventMeshTCPConfiguration = eventMeshTCPConfiguration; + } + + private void startServer() throws Exception { + Runnable r = () -> { + ServerBootstrap bootstrap = new ServerBootstrap(); + bootstrap.group(bossGroup, ioGroup) + .channel(NioServerSocketChannel.class) + .option(ChannelOption.SO_BACKLOG, 128) + .option(ChannelOption.TCP_NODELAY, true) + .option(ChannelOption.SO_REUSEADDR, true) + .option(ChannelOption.SO_KEEPALIVE, false) + .option(ChannelOption.SO_TIMEOUT, 600000) + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000) + .option(ChannelOption.SO_LINGER, 0) + .childOption(ChannelOption.SO_SNDBUF, 65535 * 4) + .childOption(ChannelOption.SO_RCVBUF, 65535 * 4) + .option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(2048, 4096, 65536)) + .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) + .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) + .childHandler(new ChannelInitializer() { + @Override + public void initChannel(Channel ch) throws Exception { + ch.pipeline().addLast(new Codec.Encoder()) + .addLast(new Codec.Decoder()) + .addLast("global-traffic-shaping", globalTrafficShapingHandler) + .addLast("channel-traffic-shaping", newCTSHandler()) + .addLast(new EventMeshTcpConnectionHandler(EventMeshTCPServer.this)) + .addLast(workerGroup, new IdleStateHandler(eventMeshTCPConfiguration.eventMeshTcpIdleReadSeconds, + eventMeshTCPConfiguration.eventMeshTcpIdleWriteSeconds, + eventMeshTCPConfiguration.eventMeshTcpIdleAllSeconds), + new EventMeshTcpMessageDispatcher(EventMeshTCPServer.this), + new EventMeshTcpExceptionHandler(EventMeshTCPServer.this) + ); + } + }); + try { + int port = eventMeshTCPConfiguration.eventMeshTcpServerPort; + ChannelFuture f = bootstrap.bind(port).sync(); + logger.info("EventMeshTCPServer[port={}] started.....", port); + f.channel().closeFuture().sync(); + } catch (Exception e) { + logger.error("EventMeshTCPServer RemotingServer Start Err!", e); + try { + shutdown(); + } catch (Exception e1) { + logger.error("EventMeshTCPServer RemotingServer shutdown Err!", e); + } + return; + } + }; + + Thread t = new Thread(r, "eventMesh-tcp-server"); + t.start(); + } + + public void init() throws Exception { + logger.info("==================EventMeshTCPServer Initialing=================="); + initThreadPool(); + + rateLimiter = RateLimiter.create(eventMeshTCPConfiguration.eventMeshTcpMsgReqnumPerSecond); + + globalTrafficShapingHandler = newGTSHandler(); + + clientManageController = new ClientManageController(this); + + clientSessionGroupMapping = new ClientSessionGroupMapping(this); + clientSessionGroupMapping.init(); + + eventMeshTcpRetryer = new EventMeshTcpRetryer(this); + eventMeshTcpRetryer.init(); + + eventMeshTcpMonitor = new EventMeshTcpMonitor(this); + eventMeshTcpMonitor.init(); + + logger.info("--------------------------EventMeshTCPServer Inited"); + } + + @Override + public void start() throws Exception { + startServer(); + + clientSessionGroupMapping.start(); + + eventMeshTcpRetryer.start(); + + eventMeshTcpMonitor.start(); + + clientManageController.start(); + + logger.info("--------------------------EventMeshTCPServer Started"); + } + + @Override + public void shutdown() throws Exception { + if (bossGroup != null) { + bossGroup.shutdownGracefully(); + logger.info("shutdown bossGroup, no client is allowed to connect access server"); + } + + clientSessionGroupMapping.shutdown(); + try { + Thread.sleep(40 * 1000); + } catch (InterruptedException e) { + logger.error("interruptedException occurred while sleeping", e); + } + + globalTrafficShapingHandler.release(); + + if (ioGroup != null) { + ioGroup.shutdownGracefully(); + logger.info("shutdown ioGroup"); + } + if (workerGroup != null) { + workerGroup.shutdownGracefully(); + logger.info("shutdown workerGroup"); + } + + eventMeshTcpRetryer.shutdown(); + + eventMeshTcpMonitor.shutdown(); + + shutdownThreadPool(); + logger.info("--------------------------EventMeshTCPServer Shutdown"); + } + + private void initThreadPool() throws Exception { + super.init("eventMesh-tcp"); + + scheduler = ThreadPoolFactory.createScheduledExecutor(eventMeshTCPConfiguration.eventMeshTcpGlobalScheduler, new EventMeshThreadFactoryImpl("eventMesh-tcp-scheduler", true)); + + taskHandleExecutorService = ThreadPoolFactory.createThreadPoolExecutor(eventMeshTCPConfiguration.eventMeshTcpTaskHandleExecutorPoolSize, eventMeshTCPConfiguration.eventMeshTcpTaskHandleExecutorPoolSize, new LinkedBlockingQueue(10000), new EventMeshThreadFactoryImpl("eventMesh-tcp-task-handle", true)); + + broadcastMsgDownstreamExecutorService = ThreadPoolFactory.createThreadPoolExecutor(eventMeshTCPConfiguration.eventMeshTcpMsgDownStreamExecutorPoolSize, eventMeshTCPConfiguration.eventMeshTcpMsgDownStreamExecutorPoolSize, new LinkedBlockingQueue(10000), new EventMeshThreadFactoryImpl("eventMesh-tcp-msg-downstream", true)); + } + + private void shutdownThreadPool() { + scheduler.shutdown(); + taskHandleExecutorService.shutdown(); + } + + private GlobalTrafficShapingHandler newGTSHandler() { + GlobalTrafficShapingHandler handler = new GlobalTrafficShapingHandler(scheduler, 0, eventMeshTCPConfiguration.getGtc().getReadLimit()) { + @Override + protected long calculateSize(Object msg) { + return 1; + } + }; + handler.setMaxTimeWait(1000); + return handler; + } + + private ChannelTrafficShapingHandler newCTSHandler() { + ChannelTrafficShapingHandler handler = new ChannelTrafficShapingHandler(0, eventMeshTCPConfiguration.getCtc().getReadLimit()) { + @Override + protected long calculateSize(Object msg) { + return 1; + } + }; + handler.setMaxTimeWait(3000); + return handler; + } + + public ClientSessionGroupMapping getClientSessionGroupMapping() { + return clientSessionGroupMapping; + } + + public EventMeshTcpRetryer getEventMeshTcpRetryer() { + return eventMeshTcpRetryer; + } + + public EventMeshTcpMonitor getEventMeshTcpMonitor() { + return eventMeshTcpMonitor; + } + + public EventMeshServer getEventMeshServer() { + return eventMeshServer; + } + + public EventMeshTCPConfiguration getEventMeshTCPConfiguration() { + return eventMeshTCPConfiguration; + } +} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/boot/SSLContextFactory.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/SSLContextFactory.java similarity index 50% rename from eventmesh-emesher/src/main/java/com/webank/emesher/boot/SSLContextFactory.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/SSLContextFactory.java index b525c260f1..529ee37ec9 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/boot/SSLContextFactory.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/SSLContextFactory.java @@ -1,16 +1,35 @@ -package com.webank.emesher.boot; +/* + * Licensed to Apache Software Foundation (ASF) under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Apache Software Foundation (ASF) licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.eventmesh.runtime.boot; -import com.webank.emesher.constants.ProxyConstants; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import javax.net.ssl.KeyManagerFactory; -import javax.net.ssl.SSLContext; import java.io.File; import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.security.KeyStore; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; + +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class SSLContextFactory { private static Logger httpLogger = LoggerFactory.getLogger("http"); @@ -22,27 +41,27 @@ public class SSLContextFactory { private static String pass; - public static SSLContext getSslContext(){ + public static SSLContext getSslContext() { SSLContext sslContext = null; - try{ + try { protocol = System.getProperty("ssl.server.protocol", "TLSv1.1"); - fileName = System.getProperty("ssl.server.cer","sChat2.jks"); + fileName = System.getProperty("ssl.server.cer", "sChat2.jks"); char[] filePass = null; - pass = System.getProperty("ssl.server.pass","sNetty"); - if(StringUtils.isNotBlank(pass)){ + pass = System.getProperty("ssl.server.pass", "sNetty"); + if (StringUtils.isNotBlank(pass)) { filePass = pass.toCharArray(); } sslContext = SSLContext.getInstance(protocol); KeyStore keyStore = KeyStore.getInstance("JKS"); - keyStore.load(Files.newInputStream(Paths.get(ProxyConstants.PROXY_CONF_HOME + keyStore.load(Files.newInputStream(Paths.get(EventMeshConstants.EVENTMESH_CONF_HOME + File.separator + fileName), StandardOpenOption.READ), filePass); KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(keyStore, filePass); sslContext.init(kmf.getKeyManagers(), null, null); - }catch (Exception e){ + } catch (Exception e) { httpLogger.warn("sslContext init failed", e); sslContext = null; } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/common/Pair.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/common/Pair.java similarity index 96% rename from eventmesh-emesher/src/main/java/com/webank/emesher/common/Pair.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/common/Pair.java index 08e1dba8f6..5e2a428ef6 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/common/Pair.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/common/Pair.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.webank.emesher.common; +package org.apache.eventmesh.runtime.common; public class Pair { private T1 object1; diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/common/ServiceState.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/common/ServiceState.java similarity index 95% rename from eventmesh-emesher/src/main/java/com/webank/emesher/common/ServiceState.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/common/ServiceState.java index 8d9167619f..6848b2c2bc 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/common/ServiceState.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/common/ServiceState.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.webank.emesher.common; +package org.apache.eventmesh.runtime.common; public enum ServiceState { diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/configuration/EventMeshHTTPConfiguration.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/configuration/EventMeshHTTPConfiguration.java new file mode 100644 index 0000000000..a73c24a7b7 --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/configuration/EventMeshHTTPConfiguration.java @@ -0,0 +1,230 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.configuration; + +import com.google.common.base.Preconditions; +import com.google.common.util.concurrent.RateLimiter; + +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.common.config.CommonConfiguration; +import org.apache.eventmesh.common.config.ConfigurationWraper; + +public class EventMeshHTTPConfiguration extends CommonConfiguration { + + public int httpServerPort = 10105; + + public RateLimiter eventMeshServerBatchMsgNumLimiter = RateLimiter.create(20000); + + public boolean eventMeshServerBatchMsgBatchEnabled = Boolean.TRUE; + + public int eventMeshServerBatchMsgThreadNum = 10; + + public int eventMeshServerSendMsgThreadNum = 8; + + public int eventMeshServerPushMsgThreadNum = 8; + + public int eventMeshServerReplyMsgThreadNum = 8; + + public int eventMeshServerClientManageThreadNum = 4; + + public int eventMeshServerRegistryThreadNum = 10; + + public int eventMeshServerAdminThreadNum = 2; + + public int eventMeshServerRetryThreadNum = 2; + + public int eventMeshServerPullRegistryIntervel = 30000; + + public int eventMeshServerAsyncAccumulationThreshold = 1000; + + public int eventMeshServerRetryBlockQSize = 10000; + + public int eventMeshServerBatchBlockQSize = 1000; + + public int eventMeshServerSendMsgBlockQSize = 1000; + + public int eventMeshServerPushMsgBlockQSize = 1000; + + public int eventMeshServerClientManageBlockQSize = 1000; + + public int eventMeshServerBusyCheckIntervel = 1000; + + public boolean eventMeshServerConsumerEnabled = false; + + public boolean eventMeshServerUseTls = false; + + public EventMeshHTTPConfiguration(ConfigurationWraper configurationWraper) { + super(configurationWraper); + } + + @Override + public void init() { + super.init(); + + if (configurationWraper != null) { + String httpServerPortStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_SERVER_HTTP_PORT); + Preconditions.checkState(StringUtils.isNotEmpty(httpServerPortStr) && StringUtils.isNumeric(httpServerPortStr), String.format("%s error", ConfKeys.KEYS_EVENTMESH_SERVER_HTTP_PORT)); + httpServerPort = Integer.valueOf(StringUtils.deleteWhitespace(httpServerPortStr)); + + String eventMeshServerBatchMsgThreadNumStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_BATCHMSG_THREAD_NUM); + if (StringUtils.isNotEmpty(eventMeshServerBatchMsgThreadNumStr) && StringUtils.isNumeric(eventMeshServerBatchMsgThreadNumStr)) { + eventMeshServerBatchMsgThreadNum = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshServerBatchMsgThreadNumStr)); + } + + String eventMeshServerBatchMsgNumLimiterStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_BATCHMSG_RATELIMITER); + if (StringUtils.isNotEmpty(eventMeshServerBatchMsgNumLimiterStr) && StringUtils.isNumeric(eventMeshServerBatchMsgNumLimiterStr)) { + eventMeshServerBatchMsgNumLimiter = RateLimiter.create(Double.valueOf(StringUtils.deleteWhitespace(eventMeshServerBatchMsgNumLimiterStr))); + } + + String eventMeshServerBatchMsgBatchEnableStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_BATCHMSG_BATCH_ENABLED); + if (StringUtils.isNotBlank(eventMeshServerBatchMsgBatchEnableStr)) { + eventMeshServerBatchMsgBatchEnabled = Boolean.valueOf(eventMeshServerBatchMsgBatchEnableStr); + } + + String eventMeshServerAsyncAccumulationThresholdStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_ASYNC_ACCUMULATION_THRESHOLD); + if (StringUtils.isNotEmpty(eventMeshServerAsyncAccumulationThresholdStr) && StringUtils.isNumeric(eventMeshServerAsyncAccumulationThresholdStr)) { + eventMeshServerAsyncAccumulationThreshold = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshServerAsyncAccumulationThresholdStr)); + } + + String eventMeshServerSendMsgThreadNumStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_SENDMSG_THREAD_NUM); + if (StringUtils.isNotEmpty(eventMeshServerSendMsgThreadNumStr) && StringUtils.isNumeric(eventMeshServerSendMsgThreadNumStr)) { + eventMeshServerSendMsgThreadNum = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshServerSendMsgThreadNumStr)); + } + + String eventMeshServerReplyMsgThreadNumStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_REPLYMSG_THREAD_NUM); + if (StringUtils.isNotEmpty(eventMeshServerReplyMsgThreadNumStr) && StringUtils.isNumeric(eventMeshServerReplyMsgThreadNumStr)) { + eventMeshServerReplyMsgThreadNum = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshServerReplyMsgThreadNumStr)); + } + + String eventMeshServerPushMsgThreadNumStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_PUSHMSG_THREAD_NUM); + if (StringUtils.isNotEmpty(eventMeshServerPushMsgThreadNumStr) && StringUtils.isNumeric(eventMeshServerPushMsgThreadNumStr)) { + eventMeshServerPushMsgThreadNum = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshServerPushMsgThreadNumStr)); + } + + String eventMeshServerRegistryThreadNumStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_REGISTRY_THREAD_NUM); + if (StringUtils.isNotEmpty(eventMeshServerRegistryThreadNumStr) && StringUtils.isNumeric(eventMeshServerRegistryThreadNumStr)) { + eventMeshServerRegistryThreadNum = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshServerRegistryThreadNumStr)); + } + + String eventMeshServerClientManageThreadNumStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_CLIENTMANAGE_THREAD_NUM); + if (StringUtils.isNotEmpty(eventMeshServerClientManageThreadNumStr) && StringUtils.isNumeric(eventMeshServerClientManageThreadNumStr)) { + eventMeshServerClientManageThreadNum = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshServerClientManageThreadNumStr)); + } + + String eventMeshServerPullRegistryIntervelStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_PULL_REGISTRY_INTERVEL); + if (StringUtils.isNotEmpty(eventMeshServerPullRegistryIntervelStr) && StringUtils.isNumeric(eventMeshServerPullRegistryIntervelStr)) { + eventMeshServerPullRegistryIntervel = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshServerPullRegistryIntervelStr)); + } + + String eventMeshServerAdminThreadNumStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_ADMIN_THREAD_NUM); + if (StringUtils.isNotEmpty(eventMeshServerAdminThreadNumStr) && StringUtils.isNumeric(eventMeshServerAdminThreadNumStr)) { + eventMeshServerAdminThreadNum = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshServerAdminThreadNumStr)); + } + + String eventMeshServerRetryBlockQSizeStr = configurationWraper.getProp(ConfKeys.KEY_EVENTMESH_RETRY_BLOCKQ_SIZE); + if (StringUtils.isNotEmpty(eventMeshServerRetryBlockQSizeStr) && StringUtils.isNumeric(eventMeshServerRetryBlockQSizeStr)) { + eventMeshServerRetryBlockQSize = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshServerRetryBlockQSizeStr)); + } + + String eventMeshServerBatchBlockQSizeStr = configurationWraper.getProp(ConfKeys.KEY_EVENTMESH_BATCHMSG_BLOCKQ_SIZE); + if (StringUtils.isNotEmpty(eventMeshServerBatchBlockQSizeStr) && StringUtils.isNumeric(eventMeshServerBatchBlockQSizeStr)) { + eventMeshServerBatchBlockQSize = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshServerBatchBlockQSizeStr)); + } + + String eventMeshServerSendMsgBlockQSizeStr = configurationWraper.getProp(ConfKeys.KEY_EVENTMESH_SENDMSG_BLOCKQ_SIZE); + if (StringUtils.isNotEmpty(eventMeshServerSendMsgBlockQSizeStr) && StringUtils.isNumeric(eventMeshServerSendMsgBlockQSizeStr)) { + eventMeshServerSendMsgBlockQSize = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshServerSendMsgBlockQSizeStr)); + } + + String eventMeshServerPushMsgBlockQSizeStr = configurationWraper.getProp(ConfKeys.KEY_EVENTMESH_PUSHMSG_BLOCKQ_SIZE); + if (StringUtils.isNotEmpty(eventMeshServerPushMsgBlockQSizeStr) && StringUtils.isNumeric(eventMeshServerPushMsgBlockQSizeStr)) { + eventMeshServerPushMsgBlockQSize = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshServerPushMsgBlockQSizeStr)); + } + + String eventMeshServerClientManageBlockQSizeStr = configurationWraper.getProp(ConfKeys.KEY_EVENTMESH_CLIENTM_BLOCKQ_SIZE); + if (StringUtils.isNotEmpty(eventMeshServerClientManageBlockQSizeStr) && StringUtils.isNumeric(eventMeshServerClientManageBlockQSizeStr)) { + eventMeshServerClientManageBlockQSize = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshServerClientManageBlockQSizeStr)); + } + + String eventMeshServerBusyCheckIntervelStr = configurationWraper.getProp(ConfKeys.KEY_EVENTMESH_BUSY_CHECK_INTERVEL); + if (StringUtils.isNotEmpty(eventMeshServerBusyCheckIntervelStr) && StringUtils.isNumeric(eventMeshServerBusyCheckIntervelStr)) { + eventMeshServerBusyCheckIntervel = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshServerBusyCheckIntervelStr)); + } + + String eventMeshServerConsumerEnabledStr = configurationWraper.getProp(ConfKeys.KEY_EVENTMESH_CONSUMER_ENABLED); + if (StringUtils.isNotEmpty(eventMeshServerConsumerEnabledStr)) { + eventMeshServerConsumerEnabled = Boolean.valueOf(StringUtils.deleteWhitespace(eventMeshServerConsumerEnabledStr)); + } + + String eventMeshServerRetryThreadNumStr = configurationWraper.getProp(ConfKeys.KEY_EVENTMESH_RETRY_THREAD_NUM); + if (StringUtils.isNotEmpty(eventMeshServerRetryThreadNumStr) && StringUtils.isNumeric(eventMeshServerRetryThreadNumStr)) { + eventMeshServerRetryThreadNum = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshServerRetryThreadNumStr)); + } + + String eventMeshServerUseTlsStr = configurationWraper.getProp(ConfKeys.KEY_EVENTMESH_HTTPS_ENABLED); + if (StringUtils.isNotEmpty(eventMeshServerUseTlsStr)) { + eventMeshServerUseTls = Boolean.valueOf(StringUtils.deleteWhitespace(eventMeshServerUseTlsStr)); + } + } + } + + static class ConfKeys { + + public static String KEYS_EVENTMESH_SERVER_HTTP_PORT = "eventMesh.server.http.port"; + + public static String KEYS_EVENTMESH_BATCHMSG_THREAD_NUM = "eventMesh.server.batchmsg.threads.num"; + + public static String KEYS_EVENTMESH_BATCHMSG_RATELIMITER = "eventMesh.server.batchmsg.speed.ratelimiter"; + + public static String KEYS_EVENTMESH_BATCHMSG_BATCH_ENABLED = "eventMesh.server.batchmsg.batch.enabled"; + + public static String KEYS_EVENTMESH_ASYNC_ACCUMULATION_THRESHOLD = "eventMesh.server.async.accumulation.threshold"; + + public static String KEY_EVENTMESH_BUSY_CHECK_INTERVEL = "eventMesh.server.busy.check.intervel"; + + public static String KEYS_EVENTMESH_SENDMSG_THREAD_NUM = "eventMesh.server.sendmsg.threads.num"; + + public static String KEYS_EVENTMESH_REPLYMSG_THREAD_NUM = "eventMesh.server.replymsg.threads.num"; + + public static String KEYS_EVENTMESH_PUSHMSG_THREAD_NUM = "eventMesh.server.pushmsg.threads.num"; + + public static String KEYS_EVENTMESH_REGISTRY_THREAD_NUM = "eventMesh.server.registry.threads.num"; + + public static String KEYS_EVENTMESH_CLIENTMANAGE_THREAD_NUM = "eventMesh.server.clientmanage.threads.num"; + + public static String KEYS_EVENTMESH_ADMIN_THREAD_NUM = "eventMesh.server.admin.threads.num"; + + public static String KEY_EVENTMESH_RETRY_THREAD_NUM = "eventMesh.server.retry.threads.num"; + + public static String KEYS_EVENTMESH_PULL_REGISTRY_INTERVEL = "eventMesh.server.pull.registry.intervel"; + + public static String KEY_EVENTMESH_RETRY_BLOCKQ_SIZE = "eventMesh.server.retry.blockQ.size"; + + public static String KEY_EVENTMESH_BATCHMSG_BLOCKQ_SIZE = "eventMesh.server.batchmsg.blockQ.size"; + + public static String KEY_EVENTMESH_SENDMSG_BLOCKQ_SIZE = "eventMesh.server.sendmsg.blockQ.size"; + + public static String KEY_EVENTMESH_PUSHMSG_BLOCKQ_SIZE = "eventMesh.server.pushmsg.blockQ.size"; + + public static String KEY_EVENTMESH_CLIENTM_BLOCKQ_SIZE = "eventMesh.server.clientM.blockQ.size"; + + public static String KEY_EVENTMESH_CONSUMER_ENABLED = "eventMesh.server.consumer.enabled"; + + public static String KEY_EVENTMESH_HTTPS_ENABLED = "eventMesh.server.useTls.enabled"; + } +} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/configuration/EventMeshTCPConfiguration.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/configuration/EventMeshTCPConfiguration.java new file mode 100644 index 0000000000..975edf8af9 --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/configuration/EventMeshTCPConfiguration.java @@ -0,0 +1,291 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.configuration; + +import com.google.common.base.Preconditions; + +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.common.config.CommonConfiguration; +import org.apache.eventmesh.common.config.ConfigurationWraper; + +public class EventMeshTCPConfiguration extends CommonConfiguration { + public int eventMeshTcpServerPort = 10000; + + public int eventMeshTcpIdleAllSeconds = 60; + + public int eventMeshTcpIdleWriteSeconds = 60; + + public int eventMeshTcpIdleReadSeconds = 60; + + public Integer eventMeshTcpMsgReqnumPerSecond = 15000; + + /** + * TCP Server allows max client num + */ + public int eventMeshTcpClientMaxNum = 10000; + + //======================================= New add config ================================= + /** + * whether enable TCP Serer + */ + public boolean eventMeshTcpServerEnabled = Boolean.FALSE; + + public int eventMeshTcpGlobalScheduler = 5; + + public int eventMeshTcpTaskHandleExecutorPoolSize = Runtime.getRuntime().availableProcessors(); + + public int eventMeshTcpMsgDownStreamExecutorPoolSize = Runtime.getRuntime().availableProcessors() > 8 ? Runtime.getRuntime().availableProcessors() : 8; + + public int eventMeshTcpSessionExpiredInMills = 60000; + + public int eventMeshTcpSessionUpstreamBufferSize = 100; + + public int eventMeshTcpMsgRetryTimes = 3; + + public int eventMeshTcpMsgRetryDelayInMills = 500; + + public int eventMeshTcpMsgRetryQueueSize = 10000; + + public Integer eventMeshTcpRebalanceIntervalInMills = 30 * 1000; + + public int eventMeshServerAdminPort = 10106; + + public boolean eventMeshTcpSendBackEnabled = Boolean.TRUE; + + public int eventMeshTcpSendBackMaxTimes = 3; + + public int eventMeshTcpPushFailIsolateTimeInMills = 30 * 1000; + + private TrafficShapingConfig gtc = new TrafficShapingConfig(0, 10_000, 1_000, 2000); + private TrafficShapingConfig ctc = new TrafficShapingConfig(0, 2_000, 1_000, 10_000); + + public EventMeshTCPConfiguration(ConfigurationWraper configurationWraper) { + super(configurationWraper); + } + + @Override + public void init() { + super.init(); + String eventMeshTcpServerPortStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_SERVER_TCP_PORT); + Preconditions.checkState(StringUtils.isNotEmpty(eventMeshTcpServerPortStr) && StringUtils.isNumeric(eventMeshTcpServerPortStr), + String.format("%s error", ConfKeys.KEYS_EVENTMESH_SERVER_TCP_PORT)); + eventMeshTcpServerPort = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshTcpServerPortStr)); + + String eventMeshTcpIdleReadSecondsStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_SERVER_READER_IDLE_SECONDS); + Preconditions.checkState(StringUtils.isNotEmpty(eventMeshTcpIdleReadSecondsStr) && StringUtils.isNumeric(eventMeshTcpIdleReadSecondsStr), + String.format("%s error", ConfKeys.KEYS_EVENTMESH_SERVER_READER_IDLE_SECONDS)); + eventMeshTcpIdleReadSeconds = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshTcpIdleReadSecondsStr)); + + String eventMeshTcpIdleWriteSecondsStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_SERVER_WRITER_IDLE_SECONDS); + Preconditions.checkState(StringUtils.isNotEmpty(eventMeshTcpIdleWriteSecondsStr) && StringUtils.isNumeric(eventMeshTcpIdleWriteSecondsStr), + String.format("%s error", ConfKeys.KEYS_EVENTMESH_SERVER_WRITER_IDLE_SECONDS)); + eventMeshTcpIdleWriteSeconds = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshTcpIdleWriteSecondsStr)); + + String eventMeshTcpIdleAllSecondsStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_SERVER_ALL_IDLE_SECONDS); + Preconditions.checkState(StringUtils.isNotEmpty(eventMeshTcpIdleAllSecondsStr) && StringUtils.isNumeric(eventMeshTcpIdleAllSecondsStr), + String.format("%s error", ConfKeys.KEYS_EVENTMESH_SERVER_ALL_IDLE_SECONDS)); + eventMeshTcpIdleAllSeconds = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshTcpIdleAllSecondsStr)); + + String eventMeshTcpMsgReqnumPerSecondStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_SERVER_MSG_REQ_NUM_PER_SECONDS); + Preconditions.checkState(StringUtils.isNotEmpty(eventMeshTcpMsgReqnumPerSecondStr) && StringUtils.isNumeric(eventMeshTcpMsgReqnumPerSecondStr), + String.format("%s error", ConfKeys.KEYS_EVENTMESH_SERVER_MSG_REQ_NUM_PER_SECONDS)); + eventMeshTcpMsgReqnumPerSecond = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshTcpMsgReqnumPerSecondStr)); + + String eventMeshTcpClientMaxNumStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_SERVER_CLIENT_MAX_NUM); + Preconditions.checkState(StringUtils.isNotEmpty(eventMeshTcpClientMaxNumStr) && StringUtils.isNumeric(eventMeshTcpClientMaxNumStr), + String.format("%s error", ConfKeys.KEYS_EVENTMESH_SERVER_CLIENT_MAX_NUM)); + eventMeshTcpClientMaxNum = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshTcpClientMaxNumStr)); + + String eventMeshTcpServerEnabledStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_TCP_SERVER_ENABLED); + if (StringUtils.isNotEmpty(eventMeshTcpServerEnabledStr)) { + eventMeshTcpServerEnabled = Boolean.valueOf(StringUtils.deleteWhitespace(eventMeshTcpServerEnabledStr)); + } + + String eventMeshTcpGlobalSchedulerStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_SERVER_GLOBAL_SCHEDULER); + if (StringUtils.isNotEmpty(eventMeshTcpGlobalSchedulerStr)) { + Preconditions.checkState(StringUtils.isNumeric(eventMeshTcpGlobalSchedulerStr), + String.format("%s error", ConfKeys.KEYS_EVENTMESH_SERVER_GLOBAL_SCHEDULER)); + eventMeshTcpGlobalScheduler = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshTcpGlobalSchedulerStr)); + } + + String eventMeshTcpTaskHandleExecutorPoolSizeStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_SERVER_TCP_TASK_HANDLE_POOL_SIZE); + if (StringUtils.isNotEmpty(eventMeshTcpTaskHandleExecutorPoolSizeStr)) { + Preconditions.checkState(StringUtils.isNumeric(eventMeshTcpTaskHandleExecutorPoolSizeStr), + String.format("%s error", ConfKeys.KEYS_EVENTMESH_SERVER_TCP_TASK_HANDLE_POOL_SIZE)); + eventMeshTcpTaskHandleExecutorPoolSize = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshTcpTaskHandleExecutorPoolSizeStr)); + } + + String eventMeshTcpMsgDownStreamExecutorPoolSizeStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_SERVER_TCP_MSG_DOWNSTREAM_POOL_SIZE); + if(StringUtils.isNotEmpty(eventMeshTcpMsgDownStreamExecutorPoolSizeStr)){ + Preconditions.checkState(StringUtils.isNumeric(eventMeshTcpMsgDownStreamExecutorPoolSizeStr), + String.format("%s error", ConfKeys.KEYS_EVENTMESH_SERVER_TCP_MSG_DOWNSTREAM_POOL_SIZE)); + eventMeshTcpMsgDownStreamExecutorPoolSize = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshTcpMsgDownStreamExecutorPoolSizeStr)); + } + + String eventMeshTcpSessionExpiredInMillsStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_SERVER_SESSION_EXPIRED_TIME); + if (StringUtils.isNotEmpty(eventMeshTcpSessionExpiredInMillsStr)) { + Preconditions.checkState(StringUtils.isNumeric(eventMeshTcpSessionExpiredInMillsStr), String.format("%s error", ConfKeys.KEYS_EVENTMESH_SERVER_SESSION_EXPIRED_TIME)); + eventMeshTcpSessionExpiredInMills = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshTcpSessionExpiredInMillsStr)); + } + + String eventMeshTcpSessionUpstreamBufferSizeStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_SERVER_SESSION_UPSTREAM_BUFFER_SIZE); + if (StringUtils.isNotEmpty(eventMeshTcpSessionUpstreamBufferSizeStr)) { + Preconditions.checkState(StringUtils.isNumeric(eventMeshTcpSessionUpstreamBufferSizeStr), + String.format("%s error", ConfKeys.KEYS_EVENTMESH_SERVER_SESSION_UPSTREAM_BUFFER_SIZE)); + eventMeshTcpSessionUpstreamBufferSize = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshTcpSessionUpstreamBufferSizeStr)); + } + + //========================================eventMesh retry config=============================================// + String eventMeshTcpMsgRetryTimesStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_SERVER_RETRY_PUSH_RETRY_TIMES); + if (StringUtils.isNotEmpty(eventMeshTcpMsgRetryTimesStr)) { + Preconditions.checkState(StringUtils.isNumeric(eventMeshTcpMsgRetryTimesStr), String.format("%s error", ConfKeys.KEYS_EVENTMESH_SERVER_RETRY_PUSH_RETRY_TIMES)); + eventMeshTcpMsgRetryTimes = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshTcpMsgRetryTimesStr)); + } + + String eventMeshTcpMsgRetryDelayInMillsStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_SERVER_RETRY_PUSH_RETRY_DELAY); + if (StringUtils.isNotEmpty(eventMeshTcpMsgRetryDelayInMillsStr)) { + Preconditions.checkState(StringUtils.isNumeric(eventMeshTcpMsgRetryDelayInMillsStr), String.format("%s error", ConfKeys.KEYS_EVENTMESH_SERVER_RETRY_PUSH_RETRY_DELAY)); + eventMeshTcpMsgRetryDelayInMills = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshTcpMsgRetryDelayInMillsStr)); + } + + String eventMeshTcpMsgRetryQueueSizeStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_SERVER_RETRY_PUSH_RETRY_QUEUE_SIZE); + if (StringUtils.isNotEmpty(eventMeshTcpMsgRetryQueueSizeStr)) { + Preconditions.checkState(StringUtils.isNumeric(eventMeshTcpMsgRetryQueueSizeStr), String.format("%s error", ConfKeys.KEYS_EVENTMESH_SERVER_RETRY_PUSH_RETRY_QUEUE_SIZE)); + eventMeshTcpMsgRetryQueueSize = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshTcpMsgRetryQueueSizeStr)); + } + + String eventMeshTcpRebalanceIntervalStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_SERVER_TCP_REBALANCE_INTERVAL); + if (StringUtils.isNotEmpty(eventMeshTcpRebalanceIntervalStr)) { + Preconditions.checkState(StringUtils.isNumeric(eventMeshTcpRebalanceIntervalStr), + String.format("%s error", ConfKeys.KEYS_EVENTMESH_SERVER_TCP_REBALANCE_INTERVAL)); + eventMeshTcpRebalanceIntervalInMills = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshTcpRebalanceIntervalStr)); + } + + String eventMeshServerAdminPortStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_SERVER_ADMIN_HTTP_PORT); + if (StringUtils.isNotEmpty(eventMeshServerAdminPortStr)) { + Preconditions.checkState(StringUtils.isNumeric(eventMeshServerAdminPortStr), + String.format("%s error", ConfKeys.KEYS_EVENTMESH_SERVER_ADMIN_HTTP_PORT)); + eventMeshServerAdminPort = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshServerAdminPortStr)); + } + + String eventMeshTcpSendBackEnabledStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_TCP_SEND_BACK_ENABLED); + if (StringUtils.isNotEmpty(eventMeshTcpSendBackEnabledStr)) { + eventMeshTcpSendBackEnabled = Boolean.valueOf(StringUtils.deleteWhitespace(eventMeshTcpSendBackEnabledStr)); + } + + String eventMeshTcpPushFailIsolateTimeInMillsStr = configurationWraper.getProp(ConfKeys.KEYS_EVENTMESH_SERVER_PUSH_FAIL_ISOLATE_TIME); + if (StringUtils.isNotEmpty(eventMeshTcpPushFailIsolateTimeInMillsStr)) { + Preconditions.checkState(StringUtils.isNumeric(eventMeshTcpPushFailIsolateTimeInMillsStr), String.format("%s error", ConfKeys.KEYS_EVENTMESH_SERVER_PUSH_FAIL_ISOLATE_TIME)); + eventMeshTcpPushFailIsolateTimeInMills = Integer.valueOf(StringUtils.deleteWhitespace(eventMeshTcpPushFailIsolateTimeInMillsStr)); + } + } + + public TrafficShapingConfig getGtc() { + return gtc; + } + + public TrafficShapingConfig getCtc() { + return ctc; + } + + static class ConfKeys { + + public static String KEYS_EVENTMESH_SERVER_TCP_PORT = "eventMesh.server.tcp.port"; + public static String KEYS_EVENTMESH_SERVER_READER_IDLE_SECONDS = "eventMesh.server.tcp.readerIdleSeconds"; + public static String KEYS_EVENTMESH_SERVER_WRITER_IDLE_SECONDS = "eventMesh.server.tcp.writerIdleSeconds"; + public static String KEYS_EVENTMESH_SERVER_ALL_IDLE_SECONDS = "eventMesh.server.tcp.allIdleSeconds"; + public static String KEYS_EVENTMESH_SERVER_CLIENT_MAX_NUM = "eventMesh.server.tcp.clientMaxNum"; + public static String KEYS_EVENTMESH_SERVER_MSG_REQ_NUM_PER_SECONDS = "eventMesh.server.tcp.msgReqnumPerSecond"; + public static String KEYS_EVENTMESH_SERVER_TCP_REBALANCE_INTERVAL = "eventMesh.server.tcp.RebalanceIntervalInMills"; + public static String KEYS_EVENTMESH_SERVER_GLOBAL_SCHEDULER = "eventMesh.server.global.scheduler"; + public static String KEYS_EVENTMESH_SERVER_TCP_TASK_HANDLE_POOL_SIZE = "eventMesh.server.tcp.taskHandleExecutorPoolSize"; + public static String KEYS_EVENTMESH_SERVER_TCP_MSG_DOWNSTREAM_POOL_SIZE = "eventMesh.server.tcp.msgDownStreamExecutorPoolSize"; + public static String KEYS_EVENTMESH_SERVER_SESSION_EXPIRED_TIME = "eventMesh.server.session.expiredInMills"; + public static String KEYS_EVENTMESH_SERVER_SESSION_UPSTREAM_BUFFER_SIZE = "eventMesh.server.session.upstreamBufferSize"; + public static String KEYS_EVENTMESH_SERVER_SESSION_DOWNSTREAM_UNACK_SIZE = "eventMesh.server.session.downstreamUnackSize"; + public static String KEYS_EVENTMESH_SERVER_RETRY_PUSH_RETRY_TIMES = "eventMesh.server.retry.pushRetryTimes"; + public static String KEYS_EVENTMESH_SERVER_RETRY_PUSH_RETRY_DELAY = "eventMesh.server.retry.pushRetryDelayInMills"; + public static String KEYS_EVENTMESH_SERVER_RETRY_PUSH_RETRY_QUEUE_SIZE = "eventMesh.server.retry.pushRetryQueueSize"; + public static String KEYS_EVENTMESH_SERVER_ADMIN_HTTP_PORT = "eventMesh.server.admin.http.port"; + public static String KEYS_EVENTMESH_TCP_SERVER_ENABLED = "eventMesh.server.tcp.enabled"; + public static String KEYS_EVENTMESH_TCP_SEND_BACK_ENABLED = "eventMesh.server.tcp.sendBack.enabled"; + public static String KEYS_EVENTMESH_SERVER_PUSH_FAIL_ISOLATE_TIME = "eventMesh.server.tcp.pushFailIsolateTimeInMills"; + public static String KEYS_EVENTMESH_TCP_DOWNSTREAM_MAP_SIZE = "eventMesh.server.tcp.downstreamMapSize"; + } + + public static class TrafficShapingConfig { + long writeLimit = 0; + long readLimit = 1000; + long checkInterval = 1000; + long maxTime = 5000; + + public TrafficShapingConfig(long writeLimit, long readLimit, long checkInterval, long maxTime) { + this.writeLimit = writeLimit; + this.readLimit = readLimit; + this.checkInterval = checkInterval; + this.maxTime = maxTime; + } + + public TrafficShapingConfig() { + + } + + public long getWriteLimit() { + return writeLimit; + } + + public void setWriteLimit(long writeLimit) { + this.writeLimit = writeLimit; + } + + public long getReadLimit() { + return readLimit; + } + + public void setReadLimit(long readLimit) { + this.readLimit = readLimit; + } + + public long getCheckInterval() { + return checkInterval; + } + + public void setCheckInterval(long checkInterval) { + this.checkInterval = checkInterval; + } + + public long getMaxTime() { + return maxTime; + } + + public void setMaxTime(long maxTime) { + this.maxTime = maxTime; + } + + @Override + public String toString() { + return "TrafficShapingConfig{" + + "writeLimit=" + writeLimit + + ", readLimit=" + readLimit + + ", checkInterval=" + checkInterval + + ", maxTime=" + maxTime + + '}'; + } + } + +} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshConstants.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshConstants.java new file mode 100644 index 0000000000..6a5d35d3e4 --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshConstants.java @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.constants; + +public class EventMeshConstants { + + public static final String EVENT_STORE_PROPERTIES = "eventstore"; + + public static final String EVENT_STORE_ENV = "EVENT_STORE"; + + public static final String PROTOCOL_HTTP = "http"; + + public static final String PROTOCOL_TCP = "tcp"; + + public static final String DEFAULT_CHARSET = "UTF-8"; + + public static final String EVENTMESH_CONF_HOME = System.getProperty("confPath", System.getenv("confPath")); + + public static final String EVENTMESH_CONF_FILE = "eventmesh.properties"; + + public static final String REQ_C2EVENTMESH_TIMESTAMP = "req_c2eventMesh_timestamp"; + public static final String REQ_EVENTMESH2MQ_TIMESTAMP = "req_eventMesh2mq_timestamp"; + public static final String REQ_MQ2EVENTMESH_TIMESTAMP = "req_mq2eventMesh_timestamp"; + public static final String REQ_EVENTMESH2C_TIMESTAMP = "req_eventMesh2c_timestamp"; + public static final String RSP_C2EVENTMESH_TIMESTAMP = "rsp_c2eventMesh_timestamp"; + public static final String RSP_EVENTMESH2MQ_TIMESTAMP = "rsp_eventMesh2mq_timestamp"; + public static final String RSP_MQ2EVENTMESH_TIMESTAMP = "rsp_mq2eventMesh_timestamp"; + public static final String RSP_EVENTMESH2C_TIMESTAMP = "rsp_eventMesh2c_timestamp"; + + public static final String REQ_SEND_EVENTMESH_IP = "req_send_eventMesh_ip"; + public static final String REQ_RECEIVE_EVENTMESH_IP = "req_receive_eventMesh_ip"; + public static final String RSP_SEND_EVENTMESH_IP = "rsp_send_eventMesh_ip"; + public static final String RSP_RECEIVE_EVENTMESH_IP = "rsp_receive_eventMesh_ip"; + + //default TTL 4 hours + public static final Integer DEFAULT_MSG_TTL_MILLS = 14400000; + + public static final int DEFAULT_TIMEOUT_IN_MILLISECONDS = 3000; + + public static final int DEFAULT_FASTFAIL_TIMEOUT_IN_MILLISECONDS = 100; + + public static final int DEFAULT_PUSH_RETRY_TIMES = 3; + + public static final int DEFAULT_PUSH_RETRY_TIME_DISTANCE_IN_MILLSECONDS = 3000; + + public static final String PURPOSE_PUB = "pub"; + + public static final String PURPOSE_SUB = "sub"; + + public static final String PURPOSE_ALL = "all"; + + public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; + + + public static final String BORN_TIMESTAMP = "BORN_TIME"; + public static final String STORE_TIMESTAMP = "STORE_TIME"; + public static final String LEAVE_TIMESTAMP = "LEAVE_TIME"; + public static final String ARRIVE_TIMESTAMP = "ARRIVE_TIME"; + + public static final String KEYS_UPPERCASE = "KEYS"; + public static final String KEYS_LOWERCASE = "keys"; + public static final String RR_REQUEST_UNIQ_ID = "RR_REQUEST_UNIQ_ID"; + public static final String TTL = "TTL"; + + public static final String TAG = "TAG"; + + public static final String MANAGE_SUBSYSTEM = "subSystem"; + public static final String MANAGE_IP = "ip"; + public static final String MANAGE_PORT = "port"; + public static final String MANAGE_DEST_IP = "desteventMeshIp"; + public static final String MANAGE_DEST_PORT = "desteventMeshPort"; + public static final String MANAGE_PATH = "path"; + public static final String MANAGE_GROUP = "group"; + public static final String MANAGE_PURPOSE = "purpose"; + public static final String MANAGE_TOPIC = "topic"; + + public static final String EVENTMESH_SEND_BACK_TIMES = "eventMeshSendBackTimes"; + + public static final String EVENTMESH_SEND_BACK_IP = "eventMeshSendBackIp"; + + public static final String EVENTMESH_REGISTRY_ADDR_KEY = "eventMeshRegistryAddr"; + + public static int DEFAULT_TIME_OUT_MILLS = 5 * 1000; + + public static final String RR_REPLY_TOPIC = "rr-reply-topic"; + + public static final String PROPERTY_MESSAGE_CLUSTER = "CLUSTER"; + + public static final String PROPERTY_MESSAGE_TTL = "TTL"; + + public static final String PROPERTY_MESSAGE_KEYS = "KEYS"; + + public static final String PROPERTY_MESSAGE_REPLY_TO = "REPLY_TO"; //requester clientId + + public static final String PROPERTY_RR_REQUEST_ID = "RR_REQUEST_UNIQ_ID"; + + public static final String LEAVE_TIME = "LEAVE_TIME"; //leaveBrokerTime + public static final String ARRIVE_TIME = "ARRIVE_TIME"; + public static final String STORE_TIME = "STORE_TIME"; + +} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/constants/ProxyVersion.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java similarity index 93% rename from eventmesh-emesher/src/main/java/com/webank/emesher/constants/ProxyVersion.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java index c32c613213..91cc68475f 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/constants/ProxyVersion.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java @@ -15,9 +15,9 @@ * limitations under the License. */ -package com.webank.emesher.constants; +package org.apache.eventmesh.runtime.constants; -public class ProxyVersion { +public class EventMeshVersion { public static final String CURRENT_VERSION = Version.V3_0_0.name(); diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/consumergroup/ConsumerGroupConf.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/consumergroup/ConsumerGroupConf.java similarity index 97% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/consumergroup/ConsumerGroupConf.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/consumergroup/ConsumerGroupConf.java index 0a4613d509..9eda09656a 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/consumergroup/ConsumerGroupConf.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/consumergroup/ConsumerGroupConf.java @@ -15,13 +15,13 @@ * limitations under the License. */ -package com.webank.emesher.core.consumergroup; - -import com.google.common.collect.Maps; +package org.apache.eventmesh.runtime.core.consumergroup; import java.util.Map; import java.util.Objects; +import com.google.common.collect.Maps; + public class ConsumerGroupConf { //eg . 5013-1A0 private String consumerGroup; diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/consumergroup/ConsumerGroupTopicConf.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/consumergroup/ConsumerGroupTopicConf.java similarity index 80% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/consumergroup/ConsumerGroupTopicConf.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/consumergroup/ConsumerGroupTopicConf.java index 9235862d07..f29d90ae67 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/consumergroup/ConsumerGroupTopicConf.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/consumergroup/ConsumerGroupTopicConf.java @@ -15,18 +15,20 @@ * limitations under the License. */ -package com.webank.emesher.core.consumergroup; - -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +package org.apache.eventmesh.runtime.core.consumergroup; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; + +import org.apache.eventmesh.common.protocol.SubscriptionItem; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class ConsumerGroupTopicConf { public static Logger logger = LoggerFactory.getLogger(ConsumerGroupTopicConf.class); @@ -35,6 +37,11 @@ public class ConsumerGroupTopicConf { private String topic; + /** + * @see org.apache.eventmesh.common.protocol.SubscriptionItem + */ + private SubscriptionItem subscriptionItem; + /** * PUSH URL */ @@ -52,12 +59,13 @@ public boolean equals(Object o) { ConsumerGroupTopicConf that = (ConsumerGroupTopicConf) o; return consumerGroup.equals(that.consumerGroup) && Objects.equals(topic, that.topic) && + Objects.equals(subscriptionItem, that.subscriptionItem) && Objects.equals(idcUrls, that.idcUrls); } @Override public int hashCode() { - return Objects.hash(consumerGroup, topic, idcUrls); + return Objects.hash(consumerGroup, topic, subscriptionItem, idcUrls); } @Override @@ -65,6 +73,7 @@ public String toString() { StringBuilder sb = new StringBuilder(); sb.append("consumeTopicConfig={consumerGroup=").append(consumerGroup) .append(",topic=").append(topic) + .append(",subscriptionMode=").append(subscriptionItem) .append(",idcUrls=").append(idcUrls).append("}"); return sb.toString(); } @@ -85,6 +94,14 @@ public void setTopic(String topic) { this.topic = topic; } + public SubscriptionItem getSubscriptionItem() { + return subscriptionItem; + } + + public void setSubscriptionItem(SubscriptionItem subscriptionItem) { + this.subscriptionItem = subscriptionItem; + } + public Map> getIdcUrls() { return idcUrls; } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/consumergroup/ProducerGroupConf.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/consumergroup/ProducerGroupConf.java similarity index 96% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/consumergroup/ProducerGroupConf.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/consumergroup/ProducerGroupConf.java index 5a5d771f31..8518850cab 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/consumergroup/ProducerGroupConf.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/consumergroup/ProducerGroupConf.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.webank.emesher.core.consumergroup; +package org.apache.eventmesh.runtime.core.consumergroup; import java.util.Objects; diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/consumergroup/event/ConsumerGroupStateEvent.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/consumergroup/event/ConsumerGroupStateEvent.java similarity index 90% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/consumergroup/event/ConsumerGroupStateEvent.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/consumergroup/event/ConsumerGroupStateEvent.java index c9a351d757..fbf06125d3 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/consumergroup/event/ConsumerGroupStateEvent.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/consumergroup/event/ConsumerGroupStateEvent.java @@ -15,9 +15,9 @@ * limitations under the License. */ -package com.webank.emesher.core.consumergroup.event; +package org.apache.eventmesh.runtime.core.consumergroup.event; -import com.webank.emesher.core.consumergroup.ConsumerGroupConf; +import org.apache.eventmesh.runtime.core.consumergroup.ConsumerGroupConf; public class ConsumerGroupStateEvent { diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/consumergroup/event/ConsumerGroupTopicConfChangeEvent.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/consumergroup/event/ConsumerGroupTopicConfChangeEvent.java similarity index 91% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/consumergroup/event/ConsumerGroupTopicConfChangeEvent.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/consumergroup/event/ConsumerGroupTopicConfChangeEvent.java index f5f1ab221a..7b19ffb041 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/consumergroup/event/ConsumerGroupTopicConfChangeEvent.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/consumergroup/event/ConsumerGroupTopicConfChangeEvent.java @@ -15,9 +15,9 @@ * limitations under the License. */ -package com.webank.emesher.core.consumergroup.event; +package org.apache.eventmesh.runtime.core.consumergroup.event; -import com.webank.emesher.core.consumergroup.ConsumerGroupTopicConf; +import org.apache.eventmesh.runtime.core.consumergroup.ConsumerGroupTopicConf; public class ConsumerGroupTopicConfChangeEvent { diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/plugin/MQConsumerWrapper.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/plugin/MQConsumerWrapper.java new file mode 100644 index 0000000000..080b7af40a --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/plugin/MQConsumerWrapper.java @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.core.plugin; + +import java.util.List; +import java.util.Properties; +import java.util.ServiceLoader; + +import io.openmessaging.api.AsyncMessageListener; +import io.openmessaging.api.Message; + +import org.apache.eventmesh.api.AbstractContext; +import org.apache.eventmesh.api.consumer.MeshMQPushConsumer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class MQConsumerWrapper extends MQWrapper { + + public Logger logger = LoggerFactory.getLogger(this.getClass()); + + protected MeshMQPushConsumer meshMQPushConsumer; + + public void subscribe(String topic, AsyncMessageListener listener) throws Exception { + meshMQPushConsumer.subscribe(topic, listener); + } + + public void unsubscribe(String topic) throws Exception { + meshMQPushConsumer.unsubscribe(topic); + } + + public synchronized void init(Properties keyValue) throws Exception { + meshMQPushConsumer = getMeshMQPushConsumer(); + if (meshMQPushConsumer == null) { + logger.error("can't load the meshMQPushConsumer plugin, please check."); + throw new RuntimeException("doesn't load the meshMQPushConsumer plugin, please check."); + } + + meshMQPushConsumer.init(keyValue); + inited.compareAndSet(false, true); + } + + private MeshMQPushConsumer getMeshMQPushConsumer() { + ServiceLoader meshMQPushConsumerServiceLoader = ServiceLoader.load(MeshMQPushConsumer.class); + if (meshMQPushConsumerServiceLoader.iterator().hasNext()) { + return meshMQPushConsumerServiceLoader.iterator().next(); + } + return null; + } + + public synchronized void start() throws Exception { + meshMQPushConsumer.start(); + started.compareAndSet(false, true); + } + + public synchronized void shutdown() throws Exception { + meshMQPushConsumer.shutdown(); + inited.compareAndSet(false, true); + started.compareAndSet(false, true); + } + +// public void registerMessageListener(MessageListenerConcurrently messageListenerConcurrently) { +// meshMQPushConsumer.registerMessageListener(messageListenerConcurrently); +// } + + public void updateOffset(List msgs, AbstractContext eventMeshConsumeConcurrentlyContext) { + meshMQPushConsumer.updateOffset(msgs, eventMeshConsumeConcurrentlyContext); + } +} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/plugin/MQProducerWrapper.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/plugin/MQProducerWrapper.java new file mode 100644 index 0000000000..082ab3bc00 --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/plugin/MQProducerWrapper.java @@ -0,0 +1,119 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.core.plugin; + +import java.util.Properties; +import java.util.ServiceLoader; + +import io.openmessaging.api.Message; +import io.openmessaging.api.SendCallback; + +import org.apache.eventmesh.api.RRCallback; +import org.apache.eventmesh.api.producer.MeshMQProducer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class MQProducerWrapper extends MQWrapper { + + public Logger logger = LoggerFactory.getLogger(this.getClass()); + + protected MeshMQProducer meshMQProducer; + + public synchronized void init(Properties keyValue) throws Exception { + if (inited.get()) { + return; + } + + meshMQProducer = getSpiMeshMQProducer(); + if (meshMQProducer == null) { + logger.error("can't load the meshMQProducer plugin, please check."); + throw new RuntimeException("doesn't load the meshMQProducer plugin, please check."); + } + meshMQProducer.init(keyValue); + + inited.compareAndSet(false, true); + } + + private MeshMQProducer getSpiMeshMQProducer() { + ServiceLoader meshMQProducerServiceLoader = ServiceLoader.load(MeshMQProducer.class); + if (meshMQProducerServiceLoader.iterator().hasNext()) { + return meshMQProducerServiceLoader.iterator().next(); + } + return null; + } + + public synchronized void start() throws Exception { + if (started.get()) { + return; + } + + meshMQProducer.start(); + + started.compareAndSet(false, true); + } + + public synchronized void shutdown() throws Exception { + if (!inited.get()) { + return; + } + + if (!started.get()) { + return; + } + + meshMQProducer.shutdown(); + + inited.compareAndSet(true, false); + started.compareAndSet(true, false); + } + + public void send(Message message, SendCallback sendCallback) throws Exception { + meshMQProducer.send(message, sendCallback); + } + + public void request(Message message, SendCallback sendCallback, RRCallback rrCallback, long timeout) + throws Exception { + meshMQProducer.request(message, sendCallback, rrCallback, timeout); + } + + public Message request(Message message, long timeout) throws Exception { + return meshMQProducer.request(message, timeout); + } + + public boolean reply(final Message message, final SendCallback sendCallback) throws Exception { + return meshMQProducer.reply(message, sendCallback); + } + + public MeshMQProducer getMeshMQProducer() { + return meshMQProducer; + } + +// public MeshMQProducer getDefaultMQProducer() { +// return meshMQProducer.getDefaultMQProducer(); +// } + +// public static void main(String[] args) throws Exception { +// +// MQProducerWrapper mqProducerWrapper = new MQProducerWrapper(); +// CommonConfiguration commonConfiguration = new CommonConfiguration(new ConfigurationWraper(EventMeshConstants.EVENTMESH_CONF_HOME +// + File.separator +// + EventMeshConstants.EVENTMESH_CONF_FILE, false)); +// commonConfiguration.init(); +// mqProducerWrapper.init(commonConfiguration, "TEST"); +// } +} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/plugin/MQWrapper.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/plugin/MQWrapper.java new file mode 100644 index 0000000000..f8ae78c112 --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/plugin/MQWrapper.java @@ -0,0 +1,46 @@ +/* + * Licensed to Apache Software Foundation (ASF) under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Apache Software Foundation (ASF) licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.eventmesh.runtime.core.plugin; + +import java.util.concurrent.atomic.AtomicBoolean; + +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; + +public abstract class MQWrapper { + + public static final String EVENT_STORE_ROCKETMQ = "rocketmq"; + + public static final String EVENT_STORE_DEFIBUS = "defibus"; + + public static String CURRENT_EVENT_STORE = EVENT_STORE_DEFIBUS; + + public static final String EVENT_STORE_CONF = System.getProperty(EventMeshConstants.EVENT_STORE_PROPERTIES, System.getenv(EventMeshConstants.EVENT_STORE_ENV)); + + static { + if (StringUtils.isNotBlank(EVENT_STORE_CONF)) { + CURRENT_EVENT_STORE = EVENT_STORE_CONF; + } + } + + public AtomicBoolean started = new AtomicBoolean(Boolean.FALSE); + + public AtomicBoolean inited = new AtomicBoolean(Boolean.FALSE); + +} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/async/AsyncContext.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/async/AsyncContext.java similarity index 97% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/async/AsyncContext.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/async/AsyncContext.java index 45611b3363..d081e225ee 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/async/AsyncContext.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/async/AsyncContext.java @@ -15,15 +15,15 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.http.async; - -import com.google.common.base.Preconditions; +package org.apache.eventmesh.runtime.core.protocol.http.async; import java.util.Objects; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.atomic.AtomicBoolean; +import com.google.common.base.Preconditions; + public class AsyncContext { private T request; diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/async/CompleteHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/async/CompleteHandler.java similarity index 93% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/async/CompleteHandler.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/async/CompleteHandler.java index abdec1a7c5..6faa01f98d 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/async/CompleteHandler.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/async/CompleteHandler.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.http.async; +package org.apache.eventmesh.runtime.core.protocol.http.async; public interface CompleteHandler { void onResponse(T t); diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/consumer/ConsumerGroupManager.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/consumer/ConsumerGroupManager.java new file mode 100644 index 0000000000..93377ac2ea --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/consumer/ConsumerGroupManager.java @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.core.protocol.http.consumer; + +import java.util.Map; +import java.util.Set; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer; +import org.apache.eventmesh.runtime.core.consumergroup.ConsumerGroupConf; +import org.apache.eventmesh.runtime.core.consumergroup.ConsumerGroupTopicConf; + +public class ConsumerGroupManager { + + protected AtomicBoolean started = new AtomicBoolean(Boolean.FALSE); + + protected AtomicBoolean inited = new AtomicBoolean(Boolean.FALSE); + + private EventMeshHTTPServer eventMeshHTTPServer; + + private EventMeshConsumer eventMeshConsumer; + + private ConsumerGroupConf consumerGroupConfig; + + public ConsumerGroupManager(EventMeshHTTPServer eventMeshHTTPServer, ConsumerGroupConf consumerGroupConfig) { + this.eventMeshHTTPServer = eventMeshHTTPServer; + this.consumerGroupConfig = consumerGroupConfig; + eventMeshConsumer = new EventMeshConsumer(this.eventMeshHTTPServer, this.consumerGroupConfig); + } + + public synchronized void init() throws Exception { + eventMeshConsumer.init(); + inited.compareAndSet(false, true); + } + + public synchronized void start() throws Exception { + setupEventMeshConsumer(consumerGroupConfig); + eventMeshConsumer.start(); + started.compareAndSet(false, true); + } + + private synchronized void setupEventMeshConsumer(ConsumerGroupConf consumerGroupConfig) throws Exception { + for (Map.Entry conf : consumerGroupConfig.getConsumerGroupTopicConf().entrySet()) { + eventMeshConsumer.subscribe(conf.getKey(), conf.getValue().getSubscriptionItem()); + } + } + + public synchronized void shutdown() throws Exception { + eventMeshConsumer.shutdown(); + started.compareAndSet(true, false); + } + + public synchronized void refresh(ConsumerGroupConf consumerGroupConfig) throws Exception { + + if (consumerGroupConfig == null || this.consumerGroupConfig.equals(consumerGroupConfig)) { + return; + } + + if (started.get()) { + shutdown(); + } + + this.consumerGroupConfig = consumerGroupConfig; + init(); + start(); + } + + public ConsumerGroupConf getConsumerGroupConfig() { + return consumerGroupConfig; + } + + public void unsubscribe(String consumerGroup) throws Exception { + if(StringUtils.equals(consumerGroupConfig.getConsumerGroup(), consumerGroup)){ + Set topics = consumerGroupConfig.getConsumerGroupTopicConf().keySet(); + for (String topic : topics){ + ConsumerGroupTopicConf consumerGroupTopicConf = consumerGroupConfig.getConsumerGroupTopicConf().get(topic); + eventMeshConsumer.unsubscribe(topic, consumerGroupTopicConf.getSubscriptionItem().getMode()); + } + } + } +} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/consumer/ConsumerManager.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/consumer/ConsumerManager.java new file mode 100644 index 0000000000..51ede0bdd8 --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/consumer/ConsumerManager.java @@ -0,0 +1,308 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.core.protocol.http.consumer; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +import com.alibaba.fastjson.JSONObject; +import com.google.common.eventbus.Subscribe; + +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer; +import org.apache.eventmesh.runtime.core.consumergroup.ConsumerGroupConf; +import org.apache.eventmesh.runtime.core.consumergroup.ConsumerGroupTopicConf; +import org.apache.eventmesh.runtime.core.consumergroup.event.ConsumerGroupStateEvent; +import org.apache.eventmesh.runtime.core.consumergroup.event.ConsumerGroupTopicConfChangeEvent; +import org.apache.eventmesh.runtime.core.protocol.http.processor.inf.Client; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ConsumerManager { + + private EventMeshHTTPServer eventMeshHTTPServer; + + private ConcurrentHashMap consumerTable = new ConcurrentHashMap(); + + private static final int DEFAULT_UPDATE_TIME = 3 * 30 * 1000; + + public Logger logger = LoggerFactory.getLogger(this.getClass()); + + private ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); + + public ConsumerManager(EventMeshHTTPServer eventMeshHTTPServer) { + this.eventMeshHTTPServer = eventMeshHTTPServer; + } + + public void init() throws Exception { + eventMeshHTTPServer.getEventBus().register(this); + logger.info("consumerManager inited......"); + } + + public void start() throws Exception { + logger.info("consumerManager started......"); + scheduledExecutorService.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { + logger.info("clientInfo check start....."); + synchronized (eventMeshHTTPServer.localClientInfoMapping) { + Map> clientInfoMap = eventMeshHTTPServer.localClientInfoMapping; + if (clientInfoMap.size() > 0) { + for (String key : clientInfoMap.keySet()) { + String consumerGroup = key.split("@")[0]; + String topic = key.split("@")[1]; + List clientList = clientInfoMap.get(key); + Iterator clientIterator = clientList.iterator(); + boolean isChange = false; + while (clientIterator.hasNext()) { + Client client = clientIterator.next(); + //时间差大于3次心跳周期 + if (System.currentTimeMillis() - client.lastUpTime.getTime() > DEFAULT_UPDATE_TIME) { + logger.warn("client {} lastUpdate time {} over three heartbeat cycles", + JSONObject.toJSONString(client), client.lastUpTime); + clientIterator.remove(); + isChange = true; + } + } + if (isChange) { + if (clientList.size() > 0) { + //change url + logger.info("consumerGroup {} client info changing", consumerGroup); + Map> idcUrls = new HashMap<>(); + Set clientUrls = new HashSet<>(); + for (Client client : clientList) { + clientUrls.add(client.url); + if (idcUrls.containsKey(client.idc)) { + idcUrls.get(client.idc).add(StringUtils.deleteWhitespace(client.url)); + } else { + List urls = new ArrayList<>(); + urls.add(client.url); + idcUrls.put(client.idc, urls); + } + } + synchronized (eventMeshHTTPServer.localConsumerGroupMapping) { + ConsumerGroupConf consumerGroupConf = eventMeshHTTPServer.localConsumerGroupMapping.get(consumerGroup); + Map map = consumerGroupConf.getConsumerGroupTopicConf(); + for (String topicKey : map.keySet()) { + if (StringUtils.equals(topic, topicKey)) { + ConsumerGroupTopicConf latestTopicConf = new ConsumerGroupTopicConf(); + latestTopicConf.setConsumerGroup(consumerGroup); + latestTopicConf.setTopic(topic); + latestTopicConf.setSubscriptionItem(map.get(topicKey).getSubscriptionItem()); + latestTopicConf.setUrls(clientUrls); + + latestTopicConf.setIdcUrls(idcUrls); + + map.put(topic, latestTopicConf); + } + } + eventMeshHTTPServer.localConsumerGroupMapping.put(consumerGroup, consumerGroupConf); + logger.info("consumerGroup {} client info changed, consumerGroupConf {}", consumerGroup, + JSONObject.toJSONString(consumerGroupConf)); + try { + notifyConsumerManager(consumerGroup, consumerGroupConf, eventMeshHTTPServer.localConsumerGroupMapping); + } catch (Exception e) { + e.printStackTrace(); + } + } + + } else { + logger.info("consumerGroup {} client info removed", consumerGroup); + //remove + try { + notifyConsumerManager(consumerGroup, null, eventMeshHTTPServer.localConsumerGroupMapping); + } catch (Exception e) { + e.printStackTrace(); + } + + eventMeshHTTPServer.localConsumerGroupMapping.keySet().removeIf(s -> StringUtils.equals(consumerGroup, s)); + } + } + + } + } + } + } + }, 10000, 10000, TimeUnit.MILLISECONDS); + } + + /** + * notify ConsumerManager 组级别 + */ + public void notifyConsumerManager(String consumerGroup, ConsumerGroupConf latestConsumerGroupConfig, + ConcurrentHashMap localConsumerGroupMapping) throws Exception { + ConsumerGroupManager cgm = eventMeshHTTPServer.getConsumerManager().getConsumer(consumerGroup); + if (latestConsumerGroupConfig == null) { + ConsumerGroupStateEvent notification = new ConsumerGroupStateEvent(); + notification.action = ConsumerGroupStateEvent.ConsumerGroupStateAction.DELETE; + notification.consumerGroup = consumerGroup; + eventMeshHTTPServer.getEventBus().post(notification); + return; + } + + if (cgm == null) { + ConsumerGroupStateEvent notification = new ConsumerGroupStateEvent(); + notification.action = ConsumerGroupStateEvent.ConsumerGroupStateAction.NEW; + notification.consumerGroup = consumerGroup; + notification.consumerGroupConfig = latestConsumerGroupConfig; + eventMeshHTTPServer.getEventBus().post(notification); + return; + } + + if (!latestConsumerGroupConfig.equals(cgm.getConsumerGroupConfig())) { + ConsumerGroupStateEvent notification = new ConsumerGroupStateEvent(); + notification.action = ConsumerGroupStateEvent.ConsumerGroupStateAction.CHANGE; + notification.consumerGroup = consumerGroup; + notification.consumerGroupConfig = latestConsumerGroupConfig; + eventMeshHTTPServer.getEventBus().post(notification); + return; + } + } + + public void shutdown() { + eventMeshHTTPServer.getEventBus().unregister(this); + for (ConsumerGroupManager consumerGroupManager : consumerTable.values()) { + try { + consumerGroupManager.shutdown(); + } catch (Exception ex) { + logger.error("shutdown consumerGroupManager[{}] err", consumerGroupManager, ex); + } + } + logger.info("consumerManager shutdown......"); + } + + public boolean contains(String consumerGroup) { + return consumerTable.containsKey(consumerGroup); + } + + /** + * add consumer + * + * @param consumerGroup + * @param consumerGroupConfig + * @throws Exception + */ + public synchronized void addConsumer(String consumerGroup, ConsumerGroupConf consumerGroupConfig) throws Exception { + ConsumerGroupManager cgm = new ConsumerGroupManager(eventMeshHTTPServer, consumerGroupConfig); + cgm.init(); + cgm.start(); + consumerTable.put(consumerGroup, cgm); + } + + /** + * restart consumer + */ + public synchronized void restartConsumer(String consumerGroup, ConsumerGroupConf consumerGroupConfig) throws Exception { + if(consumerTable.containsKey(consumerGroup)) { + ConsumerGroupManager cgm = consumerTable.get(consumerGroup); + cgm.refresh(consumerGroupConfig); + } + } + + /** + * get consumer + */ + public ConsumerGroupManager getConsumer(String consumerGroup) throws Exception { + ConsumerGroupManager cgm = consumerTable.get(consumerGroup); + return cgm; + } + + /** + * delete consumer + * + * @param consumerGroup + */ + public synchronized void delConsumer(String consumerGroup) throws Exception { + logger.info("start delConsumer with consumerGroup {}", consumerGroup); + if(consumerTable.containsKey(consumerGroup)) { + ConsumerGroupManager cgm = consumerTable.remove(consumerGroup); + logger.info("start unsubscribe topic with consumer group manager {}", JSONObject.toJSONString(cgm)); + cgm.unsubscribe(consumerGroup); + cgm.shutdown(); + } + logger.info("end delConsumer with consumerGroup {}", consumerGroup); + } + + @Subscribe + public void onChange(ConsumerGroupTopicConfChangeEvent event) { + try { + logger.info("onChange event:{}", event); + if (event.action == ConsumerGroupTopicConfChangeEvent.ConsumerGroupTopicConfChangeAction.NEW) { + ConsumerGroupManager manager = getConsumer(event.consumerGroup); + if (Objects.isNull(manager)) { + return; + } + manager.getConsumerGroupConfig().getConsumerGroupTopicConf().put(event.topic, event.newTopicConf); + return; + } + + if (event.action == ConsumerGroupTopicConfChangeEvent.ConsumerGroupTopicConfChangeAction.CHANGE) { + ConsumerGroupManager manager = getConsumer(event.consumerGroup); + if (Objects.isNull(manager)) { + return; + } + manager.getConsumerGroupConfig().getConsumerGroupTopicConf().replace(event.topic, event.newTopicConf); + return; + } + + if (event.action == ConsumerGroupTopicConfChangeEvent.ConsumerGroupTopicConfChangeAction.DELETE) { + ConsumerGroupManager manager = getConsumer(event.consumerGroup); + if (Objects.isNull(manager)) { + return; + } + manager.getConsumerGroupConfig().getConsumerGroupTopicConf().remove(event.topic); + return; + } + } catch (Exception ex) { + logger.error("onChange event:{} err", event, ex); + } + } + + @Subscribe + public void onChange(ConsumerGroupStateEvent event) { + try { + logger.info("onChange event:{}", event); + if (event.action == ConsumerGroupStateEvent.ConsumerGroupStateAction.NEW) { + addConsumer(event.consumerGroup, event.consumerGroupConfig); + return; + } + + if (event.action == ConsumerGroupStateEvent.ConsumerGroupStateAction.CHANGE) { + restartConsumer(event.consumerGroup, event.consumerGroupConfig); + return; + } + + if (event.action == ConsumerGroupStateEvent.ConsumerGroupStateAction.DELETE) { + delConsumer(event.consumerGroup); + return; + } + } catch (Exception ex) { + logger.error("onChange event:{} err", event, ex); + } + } +} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/consumer/EventMeshConsumer.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/consumer/EventMeshConsumer.java new file mode 100644 index 0000000000..8620e682ab --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/consumer/EventMeshConsumer.java @@ -0,0 +1,282 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.core.protocol.http.consumer; + +import java.util.List; +import java.util.Properties; +import java.util.concurrent.atomic.AtomicBoolean; + +import io.openmessaging.api.AsyncConsumeContext; +import io.openmessaging.api.AsyncMessageListener; +import io.openmessaging.api.Message; +import io.openmessaging.api.OnExceptionContext; +import io.openmessaging.api.SendCallback; +import io.openmessaging.api.SendResult; + +import org.apache.commons.collections4.MapUtils; +import org.apache.eventmesh.api.AbstractContext; +import org.apache.eventmesh.api.EventMeshAction; +import org.apache.eventmesh.api.EventMeshAsyncConsumeContext; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.SubscriptionItem; +import org.apache.eventmesh.common.protocol.SubscriptionMode; +import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.consumergroup.ConsumerGroupConf; +import org.apache.eventmesh.runtime.core.consumergroup.ConsumerGroupTopicConf; +import org.apache.eventmesh.runtime.core.plugin.MQConsumerWrapper; +import org.apache.eventmesh.runtime.core.protocol.http.producer.EventMeshProducer; +import org.apache.eventmesh.runtime.core.protocol.http.producer.SendMessageContext; +import org.apache.eventmesh.runtime.core.protocol.http.push.HTTPMessageHandler; +import org.apache.eventmesh.runtime.core.protocol.http.push.MessageHandler; +import org.apache.eventmesh.runtime.util.EventMeshUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class EventMeshConsumer { + + private EventMeshHTTPServer eventMeshHTTPServer; + + private AtomicBoolean started4Persistent = new AtomicBoolean(Boolean.FALSE); + + private AtomicBoolean started4Broadcast = new AtomicBoolean(Boolean.FALSE); + + private AtomicBoolean inited4Persistent = new AtomicBoolean(Boolean.FALSE); + + private AtomicBoolean inited4Broadcast = new AtomicBoolean(Boolean.FALSE); + + public Logger logger = LoggerFactory.getLogger(this.getClass()); + + public Logger messageLogger = LoggerFactory.getLogger("message"); + + private ConsumerGroupConf consumerGroupConf; + + private MQConsumerWrapper persistentMqConsumer = new MQConsumerWrapper(); + + private MQConsumerWrapper broadcastMqConsumer = new MQConsumerWrapper(); + + public EventMeshConsumer(EventMeshHTTPServer eventMeshHTTPServer, ConsumerGroupConf consumerGroupConf) { + this.eventMeshHTTPServer = eventMeshHTTPServer; + this.consumerGroupConf = consumerGroupConf; + } + + private MessageHandler httpMessageHandler; + + public synchronized void init() throws Exception { + httpMessageHandler = new HTTPMessageHandler(this); + Properties keyValue = new Properties(); + keyValue.put("isBroadcast", "false"); + keyValue.put("consumerGroup", consumerGroupConf.getConsumerGroup()); + keyValue.put("eventMeshIDC", eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshIDC); + keyValue.put("instanceName", EventMeshUtil.buildMeshClientID(consumerGroupConf.getConsumerGroup(), + eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshCluster)); + persistentMqConsumer.init(keyValue); + + // + Properties broadcastKeyValue = new Properties(); + broadcastKeyValue.put("isBroadcast", "true"); + broadcastKeyValue.put("consumerGroup", consumerGroupConf.getConsumerGroup()); + broadcastKeyValue.put("eventMeshIDC", eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshIDC); + broadcastKeyValue.put("instanceName", EventMeshUtil.buildMeshClientID(consumerGroupConf.getConsumerGroup(), + eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshCluster)); + broadcastMqConsumer.init(broadcastKeyValue); + inited4Persistent.compareAndSet(false, true); + inited4Broadcast.compareAndSet(false, true); + logger.info("EventMeshConsumer [{}] inited.............", consumerGroupConf.getConsumerGroup()); + } + + public synchronized void start() throws Exception { + persistentMqConsumer.start(); + started4Persistent.compareAndSet(false, true); + broadcastMqConsumer.start(); + started4Broadcast.compareAndSet(false, true); + } + + public void subscribe(String topic, SubscriptionItem subscriptionItem) throws Exception { + AsyncMessageListener listener = null; + if (!SubscriptionMode.BROADCASTING.equals(subscriptionItem.getMode())) { + listener = new AsyncMessageListener() { + @Override + public void consume(Message message, AsyncConsumeContext context) { + String topic = message.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION); + String bizSeqNo = message.getSystemProperties(Constants.PROPERTY_MESSAGE_SEARCH_KEYS); + String uniqueId = message.getUserProperties(Constants.RMB_UNIQ_ID); + + message.getUserProperties().put(EventMeshConstants.REQ_MQ2EVENTMESH_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + if (messageLogger.isDebugEnabled()) { + messageLogger.debug("message|mq2eventMesh|topic={}|msg={}", topic, message); + } else { + messageLogger.info("message|mq2eventMesh|topic={}|bizSeqNo={}|uniqueId={}", topic, bizSeqNo, uniqueId); + } + + ConsumerGroupTopicConf currentTopicConfig = MapUtils.getObject(consumerGroupConf.getConsumerGroupTopicConf(), topic, null); + EventMeshAsyncConsumeContext eventMeshAsyncConsumeContext = (EventMeshAsyncConsumeContext)context; + + if (currentTopicConfig == null) { + logger.error("no topicConfig found, consumerGroup:{} topic:{}", consumerGroupConf.getConsumerGroup(), topic); + try { + sendMessageBack(message, uniqueId, bizSeqNo); +// context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, EventMeshConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); +// context.ack(); + eventMeshAsyncConsumeContext.commit(EventMeshAction.CommitMessage); + return; + } catch (Exception ex) { + } + } + HandleMsgContext handleMsgContext = new HandleMsgContext(EventMeshUtil.buildPushMsgSeqNo(), consumerGroupConf.getConsumerGroup(), EventMeshConsumer.this, + topic, message, subscriptionItem, eventMeshAsyncConsumeContext.getAbstractContext(), consumerGroupConf, eventMeshHTTPServer, bizSeqNo, uniqueId, currentTopicConfig); + + if (httpMessageHandler.handle(handleMsgContext)) { +// context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, EventMeshConsumeConcurrentlyStatus.CONSUME_FINISH.name()); +// context.ack(); + eventMeshAsyncConsumeContext.commit(EventMeshAction.ManualAck); + } else { + try { + sendMessageBack(message, uniqueId, bizSeqNo); + } catch (Exception e) { + + } +// context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, EventMeshConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); +// context.ack(); + eventMeshAsyncConsumeContext.commit(EventMeshAction.CommitMessage); + } + } + }; + persistentMqConsumer.subscribe(topic, listener); + } else { + listener = new AsyncMessageListener() { + @Override + public void consume(Message message, AsyncConsumeContext context) { + String topic = message.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION); + String bizSeqNo = message.getSystemProperties(Constants.PROPERTY_MESSAGE_SEARCH_KEYS); + String uniqueId = message.getUserProperties(Constants.RMB_UNIQ_ID); + + message.getUserProperties().put(EventMeshConstants.REQ_MQ2EVENTMESH_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + + if (messageLogger.isDebugEnabled()) { + messageLogger.debug("message|mq2eventMesh|topic={}|msg={}", topic, message); + } else { + messageLogger.info("message|mq2eventMesh|topic={}|bizSeqNo={}|uniqueId={}", topic, bizSeqNo, uniqueId); + } + + ConsumerGroupTopicConf currentTopicConfig = MapUtils.getObject(consumerGroupConf.getConsumerGroupTopicConf(), topic, null); + EventMeshAsyncConsumeContext eventMeshAsyncConsumeContext = (EventMeshAsyncConsumeContext)context; + + if (currentTopicConfig == null) { + logger.error("no topicConfig found, consumerGroup:{} topic:{}", consumerGroupConf.getConsumerGroup(), topic); + try { + sendMessageBack(message, uniqueId, bizSeqNo); +// context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, EventMeshConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); +// context.ack(); + eventMeshAsyncConsumeContext.commit(EventMeshAction.CommitMessage); + return; + } catch (Exception ex) { + } + } + HandleMsgContext handleMsgContext = new HandleMsgContext(EventMeshUtil.buildPushMsgSeqNo(), consumerGroupConf.getConsumerGroup(), EventMeshConsumer.this, + topic, message, subscriptionItem, eventMeshAsyncConsumeContext.getAbstractContext(), consumerGroupConf, eventMeshHTTPServer, bizSeqNo, uniqueId, currentTopicConfig); + + if (httpMessageHandler.handle(handleMsgContext)) { +// context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, EventMeshConsumeConcurrentlyStatus.CONSUME_FINISH.name()); +// context.ack(); + eventMeshAsyncConsumeContext.commit(EventMeshAction.ManualAck); + } else { + try { + sendMessageBack(message, uniqueId, bizSeqNo); + } catch (Exception e) { + + } +// context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, EventMeshConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); +// context.ack(); + eventMeshAsyncConsumeContext.commit(EventMeshAction.CommitMessage); + } + } + }; + broadcastMqConsumer.subscribe(topic, listener); + } + } + + public void unsubscribe(String topic, SubscriptionMode subscriptionMode) throws Exception { + if (SubscriptionMode.BROADCASTING.equals(subscriptionMode)) { + broadcastMqConsumer.unsubscribe(topic); + } else { + persistentMqConsumer.unsubscribe(topic); + } + } + +// public boolean isPause() { +// return persistentMqConsumer.isPause() && broadcastMqConsumer.isPause(); +// } +// +// public void pause() { +// persistentMqConsumer.pause(); +// broadcastMqConsumer.pause(); +// } + + public synchronized void shutdown() throws Exception { + persistentMqConsumer.shutdown(); + started4Persistent.compareAndSet(true, false); + broadcastMqConsumer.shutdown(); + started4Broadcast.compareAndSet(true, false); + } + + public void updateOffset(String topic, SubscriptionMode subscriptionMode, List msgs, AbstractContext context) { + if (SubscriptionMode.BROADCASTING.equals(subscriptionMode)) { + broadcastMqConsumer.updateOffset(msgs, context); + } else { + persistentMqConsumer.updateOffset(msgs, context); + } + } + + public ConsumerGroupConf getConsumerGroupConf() { + return consumerGroupConf; + } + + public EventMeshHTTPServer getEventMeshHTTPServer() { + return eventMeshHTTPServer; + } + + public void sendMessageBack(final Message msgBack, final String uniqueId, String bizSeqNo) throws Exception { + + EventMeshProducer sendMessageBack + = eventMeshHTTPServer.getProducerManager().getEventMeshProducer(consumerGroupConf.getConsumerGroup()); + + if (sendMessageBack == null) { + logger.warn("consumer:{} consume fail, sendMessageBack, bizSeqNo:{}, uniqueId:{}", consumerGroupConf.getConsumerGroup(), bizSeqNo, uniqueId); + return; + } + + final SendMessageContext sendMessageBackContext = new SendMessageContext(bizSeqNo, msgBack, sendMessageBack, eventMeshHTTPServer); + + sendMessageBack.send(sendMessageBackContext, new SendCallback() { + @Override + public void onSuccess(SendResult sendResult) { + } + + @Override + public void onException(OnExceptionContext context) { + logger.warn("consumer:{} consume fail, sendMessageBack, bizSeqno:{}, uniqueId:{}", consumerGroupConf.getConsumerGroup(), bizSeqNo, uniqueId); + } + +// @Override +// public void onException(Throwable e) { +// logger.warn("consumer:{} consume fail, sendMessageBack, bizSeqno:{}, uniqueId:{}", consumerGroupConf.getConsumerGroup(), bizSeqNo, uniqueId); +// } + }); + } +} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/consumer/HandleMsgContext.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/consumer/HandleMsgContext.java similarity index 60% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/consumer/HandleMsgContext.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/consumer/HandleMsgContext.java index b283400e5b..2d19174b11 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/consumer/HandleMsgContext.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/consumer/HandleMsgContext.java @@ -15,26 +15,27 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.http.consumer; - -import com.webank.defibus.common.DeFiBusConstant; -import com.webank.emesher.boot.ProxyHTTPServer; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.consumergroup.ConsumerGroupConf; -import com.webank.emesher.core.consumergroup.ConsumerGroupTopicConf; -import com.webank.eventmesh.common.Constants; -import org.apache.commons.collections4.MapUtils; -import org.apache.commons.lang3.time.DateFormatUtils; -import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext; -import com.webank.emesher.patch.ProxyConsumeConcurrentlyContext; -import org.apache.rocketmq.common.message.MessageExt; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +package org.apache.eventmesh.runtime.core.protocol.http.consumer; import java.util.Arrays; import java.util.HashMap; import java.util.Map; +import io.openmessaging.api.Message; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.time.DateFormatUtils; +import org.apache.eventmesh.api.AbstractContext; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.SubscriptionItem; +import org.apache.eventmesh.common.protocol.SubscriptionMode; +import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.consumergroup.ConsumerGroupConf; +import org.apache.eventmesh.runtime.core.consumergroup.ConsumerGroupTopicConf; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class HandleMsgContext { public Logger messageLogger = LoggerFactory.getLogger("message"); @@ -43,7 +44,7 @@ public class HandleMsgContext { private String consumerGroup; - private ProxyConsumer proxyConsumer; + private EventMeshConsumer eventMeshConsumer; private String bizSeqNo; @@ -51,38 +52,42 @@ public class HandleMsgContext { private String topic; - private MessageExt msg; + private SubscriptionItem subscriptionItem; + + private Message msg; private int ttl; private long createTime = System.currentTimeMillis(); - private ConsumeConcurrentlyContext context; + private AbstractContext context; private ConsumerGroupConf consumerGroupConfig; - private ProxyHTTPServer proxyHTTPServer; + private EventMeshHTTPServer eventMeshHTTPServer; private ConsumerGroupTopicConf consumeTopicConfig; private Map props; - public HandleMsgContext(String msgRandomNo, String consumerGroup, ProxyConsumer proxyConsumer, - String topic, MessageExt msg, - ConsumeConcurrentlyContext context, ConsumerGroupConf consumerGroupConfig, - ProxyHTTPServer proxyHTTPServer, String bizSeqNo, String uniqueId, ConsumerGroupTopicConf consumeTopicConfig) { + public HandleMsgContext(String msgRandomNo, String consumerGroup, EventMeshConsumer eventMeshConsumer, + String topic, Message msg, SubscriptionItem subscriptionItem, + AbstractContext context, ConsumerGroupConf consumerGroupConfig, + EventMeshHTTPServer eventMeshHTTPServer, String bizSeqNo, String uniqueId, ConsumerGroupTopicConf consumeTopicConfig) { this.msgRandomNo = msgRandomNo; this.consumerGroup = consumerGroup; - this.proxyConsumer = proxyConsumer; + this.eventMeshConsumer = eventMeshConsumer; this.topic = topic; this.msg = msg; - this.context = (ProxyConsumeConcurrentlyContext) context; + this.subscriptionItem = subscriptionItem; + this.context = context; this.consumerGroupConfig = consumerGroupConfig; - this.proxyHTTPServer = proxyHTTPServer; + this.eventMeshHTTPServer = eventMeshHTTPServer; this.bizSeqNo = bizSeqNo; this.uniqueId = uniqueId; this.consumeTopicConfig = consumeTopicConfig; - this.ttl = MapUtils.getIntValue(msg.getProperties(), DeFiBusConstant.PROPERTY_MESSAGE_TTL, ProxyConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS); + String ttlStr = msg.getUserProperties(Constants.PROPERTY_MESSAGE_TIMEOUT); + this.ttl = StringUtils.isNumeric(ttlStr)? Integer.parseInt(ttlStr): EventMeshConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS; } public void addProp(String key, String val) { @@ -128,12 +133,12 @@ public void setConsumerGroup(String consumerGroup) { this.consumerGroup = consumerGroup; } - public ProxyConsumer getProxyConsumer() { - return proxyConsumer; + public EventMeshConsumer getEventMeshConsumer() { + return eventMeshConsumer; } - public void setProxyConsumer(ProxyConsumer proxyConsumer) { - this.proxyConsumer = proxyConsumer; + public void setEventMeshConsumer(EventMeshConsumer eventMeshConsumer) { + this.eventMeshConsumer = eventMeshConsumer; } public String getTopic() { @@ -144,14 +149,22 @@ public void setTopic(String topic) { this.topic = topic; } - public MessageExt getMsg() { + public Message getMsg() { return msg; } - public void setMsg(MessageExt msg) { + public void setMsg(Message msg) { this.msg = msg; } + public SubscriptionItem getSubscriptionItem() { + return subscriptionItem; + } + + public void setSubscriptionItem(SubscriptionItem subscriptionItem) { + this.subscriptionItem = subscriptionItem; + } + public long getCreateTime() { return createTime; } @@ -160,11 +173,11 @@ public void setCreateTime(long createTime) { this.createTime = createTime; } - public ConsumeConcurrentlyContext getContext() { + public AbstractContext getContext() { return context; } - public void setContext(ProxyConsumeConcurrentlyContext context) { + public void setContext(AbstractContext context) { this.context = context; } @@ -176,19 +189,19 @@ public void setConsumerGroupConfig(ConsumerGroupConf consumerGroupConfig) { this.consumerGroupConfig = consumerGroupConfig; } - public ProxyHTTPServer getProxyHTTPServer() { - return proxyHTTPServer; + public EventMeshHTTPServer getEventMeshHTTPServer() { + return eventMeshHTTPServer; } public void finish() { - if (proxyConsumer != null && context != null && msg != null) { + if (eventMeshConsumer != null && context != null && msg != null) { if (messageLogger.isDebugEnabled()) { - messageLogger.debug("messageAcked|topic={}|msgId={}|cluster={}|broker={}|queueId={}|queueOffset={}", topic, - msg.getMsgId(), msg.getProperty(DeFiBusConstant.PROPERTY_MESSAGE_CLUSTER), - msg.getProperty(DeFiBusConstant.PROPERTY_MESSAGE_BROKER), - msg.getQueueId(), msg.getQueueOffset()); +// messageLogger.debug("messageAcked|topic={}|msgId={}|cluster={}|broker={}|queueId={}|queueOffset={}", topic, +// msg.getMsgId(), msg.getProperty(DeFiBusConstant.PROPERTY_MESSAGE_CLUSTER), +// msg.getProperty(DeFiBusConstant.PROPERTY_MESSAGE_BROKER), +// msg.getQueueId(), msg.getQueueOffset()); } - proxyConsumer.updateOffset(topic, Arrays.asList(msg), (ProxyConsumeConcurrentlyContext) context); + eventMeshConsumer.updateOffset(topic, subscriptionItem.getMode(), Arrays.asList(msg), context); } } @@ -214,6 +227,7 @@ public String toString() { sb.append("handleMsgContext={") .append("consumerGroup=").append(consumerGroup) .append(",topic=").append(topic) + .append(",subscriptionItem=").append(subscriptionItem) .append(",consumeTopicConfig=").append(consumeTopicConfig) .append(",bizSeqNo=").append(bizSeqNo) .append(",uniqueId=").append(uniqueId) diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/AdminMetricsProcessor.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/AdminMetricsProcessor.java similarity index 71% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/AdminMetricsProcessor.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/AdminMetricsProcessor.java index 60b1a71adc..dac27461b8 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/AdminMetricsProcessor.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/AdminMetricsProcessor.java @@ -15,13 +15,14 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.http.processor; +package org.apache.eventmesh.runtime.core.protocol.http.processor; -import com.webank.emesher.boot.ProxyHTTPServer; -import com.webank.emesher.core.protocol.http.async.AsyncContext; -import com.webank.emesher.core.protocol.http.processor.inf.HttpRequestProcessor; -import com.webank.eventmesh.common.command.HttpCommand; import io.netty.channel.ChannelHandlerContext; + +import org.apache.eventmesh.common.command.HttpCommand; +import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer; +import org.apache.eventmesh.runtime.core.protocol.http.async.AsyncContext; +import org.apache.eventmesh.runtime.core.protocol.http.processor.inf.HttpRequestProcessor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -29,10 +30,10 @@ public class AdminMetricsProcessor implements HttpRequestProcessor { public Logger cmdLogger = LoggerFactory.getLogger("cmd"); - private ProxyHTTPServer proxyHTTPServer; + private EventMeshHTTPServer eventMeshHTTPServer; - public AdminMetricsProcessor(ProxyHTTPServer proxyHTTPServer) { - this.proxyHTTPServer = proxyHTTPServer; + public AdminMetricsProcessor(EventMeshHTTPServer eventMeshHTTPServer) { + this.eventMeshHTTPServer = eventMeshHTTPServer; } public Logger logger = LoggerFactory.getLogger(this.getClass()); diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/AdminShutdownProcessor.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/AdminShutdownProcessor.java similarity index 50% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/AdminShutdownProcessor.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/AdminShutdownProcessor.java index 4cfb840105..775a7c1730 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/AdminShutdownProcessor.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/AdminShutdownProcessor.java @@ -15,18 +15,19 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.http.processor; - -import com.webank.emesher.boot.ProxyServer; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.protocol.http.async.AsyncContext; -import com.webank.emesher.core.protocol.http.processor.inf.HttpRequestProcessor; -import com.webank.eventmesh.common.IPUtil; -import com.webank.eventmesh.common.command.HttpCommand; -import com.webank.eventmesh.common.protocol.http.common.ProxyRetCode; -import com.webank.eventmesh.common.protocol.http.common.RequestCode; +package org.apache.eventmesh.runtime.core.protocol.http.processor; + import io.netty.channel.ChannelHandlerContext; -import org.apache.rocketmq.remoting.common.RemotingHelper; + +import org.apache.eventmesh.common.IPUtil; +import org.apache.eventmesh.common.command.HttpCommand; +import org.apache.eventmesh.common.protocol.http.common.EventMeshRetCode; +import org.apache.eventmesh.common.protocol.http.common.RequestCode; +import org.apache.eventmesh.runtime.boot.EventMeshServer; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.http.async.AsyncContext; +import org.apache.eventmesh.runtime.core.protocol.http.processor.inf.HttpRequestProcessor; +import org.apache.eventmesh.runtime.util.RemotingHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,25 +35,25 @@ public class AdminShutdownProcessor implements HttpRequestProcessor { public Logger cmdLogger = LoggerFactory.getLogger("cmd"); - private ProxyServer proxyServer; + private EventMeshServer eventMeshServer; - public AdminShutdownProcessor(ProxyServer proxyServer) { - this.proxyServer = proxyServer; + public AdminShutdownProcessor(EventMeshServer eventMeshServer) { + this.eventMeshServer = eventMeshServer; } @Override public void processRequest(ChannelHandlerContext ctx, AsyncContext asyncContext) throws Exception { - HttpCommand responseProxyCommand; - cmdLogger.info("cmd={}|{}|client2proxy|from={}|to={}", RequestCode.get(Integer.valueOf(asyncContext.getRequest().getRequestCode())), - ProxyConstants.PROTOCOL_HTTP, + HttpCommand responseEventMeshCommand; + cmdLogger.info("cmd={}|{}|client2eventMesh|from={}|to={}", RequestCode.get(Integer.valueOf(asyncContext.getRequest().getRequestCode())), + EventMeshConstants.PROTOCOL_HTTP, RemotingHelper.parseChannelRemoteAddr(ctx.channel()), IPUtil.getLocalAddress()); - proxyServer.shutdown(); + eventMeshServer.shutdown(); - responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( - ProxyRetCode.SUCCESS.getRetCode(), ProxyRetCode.SUCCESS.getErrMsg()); - asyncContext.onComplete(responseProxyCommand); + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + EventMeshRetCode.SUCCESS.getRetCode(), EventMeshRetCode.SUCCESS.getErrMsg()); + asyncContext.onComplete(responseEventMeshCommand); } @Override diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/BatchSendMessageProcessor.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/BatchSendMessageProcessor.java new file mode 100644 index 0000000000..df3b2afcd5 --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/BatchSendMessageProcessor.java @@ -0,0 +1,272 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.core.protocol.http.processor; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; + +import io.netty.channel.ChannelHandlerContext; +import io.openmessaging.api.Message; +import io.openmessaging.api.OnExceptionContext; +import io.openmessaging.api.SendCallback; +import io.openmessaging.api.SendResult; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.IPUtil; +import org.apache.eventmesh.common.command.HttpCommand; +import org.apache.eventmesh.common.protocol.http.body.message.SendMessageBatchRequestBody; +import org.apache.eventmesh.common.protocol.http.body.message.SendMessageBatchResponseBody; +import org.apache.eventmesh.common.protocol.http.common.EventMeshRetCode; +import org.apache.eventmesh.common.protocol.http.common.RequestCode; +import org.apache.eventmesh.common.protocol.http.header.message.SendMessageBatchRequestHeader; +import org.apache.eventmesh.common.protocol.http.header.message.SendMessageBatchResponseHeader; +import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.http.async.AsyncContext; +import org.apache.eventmesh.runtime.core.protocol.http.processor.inf.HttpRequestProcessor; +import org.apache.eventmesh.runtime.core.protocol.http.producer.EventMeshProducer; +import org.apache.eventmesh.runtime.core.protocol.http.producer.SendMessageContext; +import org.apache.eventmesh.runtime.util.EventMeshUtil; +import org.apache.eventmesh.runtime.util.RemotingHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class BatchSendMessageProcessor implements HttpRequestProcessor { + + public Logger cmdLogger = LoggerFactory.getLogger("cmd"); + + private EventMeshHTTPServer eventMeshHTTPServer; + + public BatchSendMessageProcessor(EventMeshHTTPServer eventMeshHTTPServer) { + this.eventMeshHTTPServer = eventMeshHTTPServer; + } + + public Logger batchMessageLogger = LoggerFactory.getLogger("batchMessage"); + + @Override + public void processRequest(ChannelHandlerContext ctx, AsyncContext asyncContext) throws Exception { + + HttpCommand responseEventMeshCommand; + + cmdLogger.info("cmd={}|{}|client2eventMesh|from={}|to={}", RequestCode.get(Integer.valueOf(asyncContext.getRequest().getRequestCode())), + EventMeshConstants.PROTOCOL_HTTP, + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), IPUtil.getLocalAddress()); + + SendMessageBatchRequestHeader sendMessageBatchRequestHeader = (SendMessageBatchRequestHeader) asyncContext.getRequest().getHeader(); + SendMessageBatchRequestBody sendMessageBatchRequestBody = (SendMessageBatchRequestBody) asyncContext.getRequest().getBody(); + + SendMessageBatchResponseHeader sendMessageBatchResponseHeader = + SendMessageBatchResponseHeader.buildHeader(Integer.valueOf(asyncContext.getRequest().getRequestCode()), eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshCluster, + IPUtil.getLocalAddress(), eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshEnv, + eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshIDC); + + if (StringUtils.isBlank(sendMessageBatchRequestHeader.getPid()) + || !StringUtils.isNumeric(sendMessageBatchRequestHeader.getPid()) + || StringUtils.isBlank(sendMessageBatchRequestHeader.getSys())) { + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + sendMessageBatchResponseHeader, + SendMessageBatchResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getErrMsg())); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + + if (CollectionUtils.isEmpty(sendMessageBatchRequestBody.getContents()) + || StringUtils.isBlank(sendMessageBatchRequestBody.getBatchId()) + || StringUtils.isBlank(sendMessageBatchRequestBody.getProducerGroup()) + || (Integer.valueOf(sendMessageBatchRequestBody.getSize()) != CollectionUtils.size(sendMessageBatchRequestBody.getContents()))) { + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + sendMessageBatchResponseHeader, + SendMessageBatchResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getErrMsg())); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + + if (!eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshServerBatchMsgNumLimiter + .tryAcquire(Integer.valueOf(sendMessageBatchRequestBody.getSize()), EventMeshConstants.DEFAULT_FASTFAIL_TIMEOUT_IN_MILLISECONDS, TimeUnit.MILLISECONDS)) { + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + sendMessageBatchResponseHeader, + SendMessageBatchResponseBody.buildBody(EventMeshRetCode.EVENTMESH_BATCH_SPEED_OVER_LIMIT_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_BATCH_SPEED_OVER_LIMIT_ERR.getErrMsg())); + eventMeshHTTPServer.metrics.summaryMetrics + .recordSendBatchMsgDiscard(Integer.valueOf(sendMessageBatchRequestBody.getSize())); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + + + String producerGroup = sendMessageBatchRequestBody.getProducerGroup(); + EventMeshProducer batchEventMeshProducer = eventMeshHTTPServer.getProducerManager().getEventMeshProducer(producerGroup); + + batchEventMeshProducer.getMqProducerWrapper().getMeshMQProducer().setExtFields(); + + if (!batchEventMeshProducer.getStarted().get()) { + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + sendMessageBatchResponseHeader, + SendMessageBatchResponseBody.buildBody(EventMeshRetCode.EVENTMESH_BATCH_PRODUCER_STOPED_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_BATCH_PRODUCER_STOPED_ERR.getErrMsg())); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + + long batchStartTime = System.currentTimeMillis(); + + List msgList = new ArrayList<>(); + Map> topicBatchMessageMappings = new ConcurrentHashMap>(); + for (SendMessageBatchRequestBody.BatchMessageEntity msg : sendMessageBatchRequestBody.getContents()) { + if (StringUtils.isBlank(msg.topic) + || StringUtils.isBlank(msg.msg)) { + continue; + } + + if (StringUtils.isBlank(msg.ttl) || !StringUtils.isNumeric(msg.ttl)) { + msg.ttl = String.valueOf(EventMeshConstants.DEFAULT_MSG_TTL_MILLS); + } + + try { +// Message rocketMQMsg; + Message omsMsg = new Message(); + // topic + omsMsg.setTopic(msg.topic); + // body + omsMsg.setBody(msg.msg.getBytes(EventMeshConstants.DEFAULT_CHARSET)); + if (!StringUtils.isBlank(msg.tag)) { + omsMsg.putUserProperties(EventMeshConstants.TAG, msg.tag); + } +// if (StringUtils.isBlank(msg.tag)) { +// rocketMQMsg = new Message(msg.topic, msg.msg.getBytes(EventMeshConstants.DEFAULT_CHARSET)); +// } else { +// rocketMQMsg = new Message(msg.topic, msg.tag, msg.msg.getBytes(EventMeshConstants.DEFAULT_CHARSET)); +// } + omsMsg.putUserProperties("msgType", "persistent"); + // ttl + omsMsg.putSystemProperties(Constants.PROPERTY_MESSAGE_TIMEOUT, msg.ttl); + //MessageAccessor.putProperty(rocketMQMsg, DeFiBusConstant.PROPERTY_MESSAGE_TTL, msg.ttl); + msgList.add(omsMsg); + if (topicBatchMessageMappings.containsKey(msg.topic)) { + topicBatchMessageMappings.get(msg.topic).add(omsMsg); + } else { + List tmp = new ArrayList<>(); + tmp.add(omsMsg); + topicBatchMessageMappings.put(msg.topic, tmp); + } + + if (batchMessageLogger.isDebugEnabled()) { + batchMessageLogger.debug("msg2MQMsg suc, msg:{}", msg.msg); + } + + } catch (Exception e) { + batchMessageLogger.error("msg2MQMsg err, msg:{}", msg, e); + } + } + + if (CollectionUtils.isEmpty(msgList)) { + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + sendMessageBatchResponseHeader, + SendMessageBatchResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getErrMsg())); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + + String sizeStr = sendMessageBatchRequestBody.getSize(); + long delta = StringUtils.isNumeric(sizeStr)? Integer.parseInt(sizeStr) : 0; + eventMeshHTTPServer.metrics.summaryMetrics.recordSendBatchMsg(delta); + + if (eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshServerBatchMsgBatchEnabled) { + for (List batchMsgs : topicBatchMessageMappings.values()) { + // TODO:api中的实现,考虑是否放到插件中 + Message omsMsg = new Message(); +// try { +// msgBatch = msgBatch.generateFromList(batchMsgs); +// for (Message message : msgBatch.getMessages()) { +// // TODO:未针对不同producer检测消息最大长度 +// Validators.checkMessage(message, batchEventMeshProducer.getMqProducerWrapper().getDefaultMQProducer()); +// MessageClientIDSetter.setUniqID(message); +// } +// msgBatch.setBody(msgBatch.encode()); +// } catch (Exception e) { +// continue; +// } + + final SendMessageContext sendMessageContext = new SendMessageContext(sendMessageBatchRequestBody.getBatchId(), omsMsg, batchEventMeshProducer, eventMeshHTTPServer); + sendMessageContext.setMessageList(batchMsgs); + batchEventMeshProducer.send(sendMessageContext, new SendCallback() { + @Override + public void onSuccess(SendResult sendResult) { + } + + @Override + public void onException(OnExceptionContext context) { + batchMessageLogger.warn("", context.getException()); + eventMeshHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000)); + } + +// @Override +// public void onException(Throwable e) { +// batchMessageLogger.warn("", e); +// eventMeshHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000)); +// } + }); + } + } else { + for (Message msg : msgList) { + final SendMessageContext sendMessageContext = new SendMessageContext(sendMessageBatchRequestBody.getBatchId(), msg, batchEventMeshProducer, eventMeshHTTPServer); + batchEventMeshProducer.send(sendMessageContext, new SendCallback() { + @Override + public void onSuccess(SendResult sendResult) { + + } + + @Override + public void onException(OnExceptionContext context) { + batchMessageLogger.warn("", context.getException()); + eventMeshHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000)); + } + +// @Override +// public void onException(Throwable e) { +// batchMessageLogger.warn("", e); +// eventMeshHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000)); +// } + }); + } + } + + long batchEndTime = System.currentTimeMillis(); + eventMeshHTTPServer.metrics.summaryMetrics.recordBatchSendMsgCost(batchEndTime - batchStartTime); + batchMessageLogger.debug("batchMessage|eventMesh2mq|REQ|ASYNC|batchId={}|send2MQCost={}ms|msgNum={}|topics={}", + sendMessageBatchRequestBody.getBatchId(), + batchEndTime - batchStartTime, + sendMessageBatchRequestBody.getSize(), + topicBatchMessageMappings.keySet()); + + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + sendMessageBatchResponseHeader, + SendMessageBatchResponseBody.buildBody(EventMeshRetCode.SUCCESS.getRetCode(), EventMeshRetCode.SUCCESS.getErrMsg())); + asyncContext.onComplete(responseEventMeshCommand); + + return; + } + + @Override + public boolean rejectRequest() { + return false; + } +} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/BatchSendMessageV2Processor.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/BatchSendMessageV2Processor.java new file mode 100644 index 0000000000..8d5f7b5b92 --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/BatchSendMessageV2Processor.java @@ -0,0 +1,237 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.core.protocol.http.processor; + +import java.util.concurrent.TimeUnit; + +import io.netty.channel.ChannelHandlerContext; +import io.openmessaging.api.Message; +import io.openmessaging.api.OnExceptionContext; +import io.openmessaging.api.SendCallback; +import io.openmessaging.api.SendResult; + +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.IPUtil; +import org.apache.eventmesh.common.command.HttpCommand; +import org.apache.eventmesh.common.protocol.http.body.message.SendMessageBatchV2RequestBody; +import org.apache.eventmesh.common.protocol.http.body.message.SendMessageBatchV2ResponseBody; +import org.apache.eventmesh.common.protocol.http.common.EventMeshRetCode; +import org.apache.eventmesh.common.protocol.http.common.RequestCode; +import org.apache.eventmesh.common.protocol.http.header.message.SendMessageBatchV2RequestHeader; +import org.apache.eventmesh.common.protocol.http.header.message.SendMessageBatchV2ResponseHeader; +import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.http.async.AsyncContext; +import org.apache.eventmesh.runtime.core.protocol.http.processor.inf.HttpRequestProcessor; +import org.apache.eventmesh.runtime.core.protocol.http.producer.EventMeshProducer; +import org.apache.eventmesh.runtime.core.protocol.http.producer.SendMessageContext; +import org.apache.eventmesh.runtime.util.EventMeshUtil; +import org.apache.eventmesh.runtime.util.RemotingHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class BatchSendMessageV2Processor implements HttpRequestProcessor { + + public Logger cmdLogger = LoggerFactory.getLogger("cmd"); + + private EventMeshHTTPServer eventMeshHTTPServer; + + public BatchSendMessageV2Processor(EventMeshHTTPServer eventMeshHTTPServer) { + this.eventMeshHTTPServer = eventMeshHTTPServer; + } + + public Logger batchMessageLogger = LoggerFactory.getLogger("batchMessage"); + + @Override + public void processRequest(ChannelHandlerContext ctx, AsyncContext asyncContext) throws Exception { + + HttpCommand responseEventMeshCommand; + + cmdLogger.info("cmd={}|{}|client2eventMesh|from={}|to={}", RequestCode.get(Integer.valueOf(asyncContext.getRequest().getRequestCode())), + EventMeshConstants.PROTOCOL_HTTP, + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), IPUtil.getLocalAddress()); + + SendMessageBatchV2RequestHeader sendMessageBatchV2RequestHeader = (SendMessageBatchV2RequestHeader) asyncContext.getRequest().getHeader(); + SendMessageBatchV2RequestBody sendMessageBatchV2RequestBody = (SendMessageBatchV2RequestBody) asyncContext.getRequest().getBody(); + + SendMessageBatchV2ResponseHeader sendMessageBatchV2ResponseHeader = + SendMessageBatchV2ResponseHeader.buildHeader(Integer.valueOf(asyncContext.getRequest().getRequestCode()), eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshCluster, + IPUtil.getLocalAddress(), eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshEnv, + eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshIDC); + + if (StringUtils.isBlank(sendMessageBatchV2RequestHeader.getPid()) + || !StringUtils.isNumeric(sendMessageBatchV2RequestHeader.getPid()) + || StringUtils.isBlank(sendMessageBatchV2RequestHeader.getSys())) { + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + sendMessageBatchV2ResponseHeader, + SendMessageBatchV2ResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getErrMsg())); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + + if (StringUtils.isBlank(sendMessageBatchV2RequestBody.getBizSeqNo()) + || StringUtils.isBlank(sendMessageBatchV2RequestBody.getTopic()) + || StringUtils.isBlank(sendMessageBatchV2RequestBody.getProducerGroup()) + || StringUtils.isBlank(sendMessageBatchV2RequestBody.getMsg())) { + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + sendMessageBatchV2ResponseHeader, + SendMessageBatchV2ResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getErrMsg())); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + + if (!eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshServerBatchMsgNumLimiter + .tryAcquire(EventMeshConstants.DEFAULT_FASTFAIL_TIMEOUT_IN_MILLISECONDS, TimeUnit.MILLISECONDS)) { + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + sendMessageBatchV2ResponseHeader, + SendMessageBatchV2ResponseBody.buildBody(EventMeshRetCode.EVENTMESH_BATCH_SPEED_OVER_LIMIT_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_BATCH_SPEED_OVER_LIMIT_ERR.getErrMsg())); + eventMeshHTTPServer.metrics.summaryMetrics + .recordSendBatchMsgDiscard(1); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + + String producerGroup = sendMessageBatchV2RequestBody.getProducerGroup(); + EventMeshProducer batchEventMeshProducer = eventMeshHTTPServer.getProducerManager().getEventMeshProducer(producerGroup); + batchEventMeshProducer.getMqProducerWrapper().getMeshMQProducer().setExtFields(); +// batchEventMeshProducer.getMqProducerWrapper().getDefaultMQProducer().setRetryTimesWhenSendAsyncFailed(0); +// batchEventMeshProducer.getMqProducerWrapper().getDefaultMQProducer().setPollNameServerInterval(60000); +// +// batchEventMeshProducer.getMqProducerWrapper().getDefaultMQProducer().getDefaultMQProducerImpl().getmQClientFactory() +// .getNettyClientConfig().setClientAsyncSemaphoreValue(eventMeshHTTPServer.getEventMeshConfiguration().eventMeshServerAsyncAccumulationThreshold); +// batchEventMeshProducer.getMqProducerWrapper().getDefaultMQProducer().setCompressMsgBodyOverHowmuch(10); + if (!batchEventMeshProducer.getStarted().get()) { + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + sendMessageBatchV2ResponseHeader, + SendMessageBatchV2ResponseBody.buildBody(EventMeshRetCode.EVENTMESH_BATCH_PRODUCER_STOPED_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_BATCH_PRODUCER_STOPED_ERR.getErrMsg())); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + + long batchStartTime = System.currentTimeMillis(); + + if (StringUtils.isBlank(sendMessageBatchV2RequestBody.getTtl()) || !StringUtils.isNumeric(sendMessageBatchV2RequestBody.getTtl())) { + sendMessageBatchV2RequestBody.setTtl(String.valueOf(EventMeshConstants.DEFAULT_MSG_TTL_MILLS)); + } + +// Message rocketMQMsg = null; + Message omsMsg = new Message(); + + try { +// if (StringUtils.isBlank(sendMessageBatchV2RequestBody.getTag())) { +// rocketMQMsg = new Message(sendMessageBatchV2RequestBody.getTopic(), sendMessageBatchV2RequestBody.getMsg().getBytes(EventMeshConstants.DEFAULT_CHARSET)); +// } else { +// rocketMQMsg = new Message(sendMessageBatchV2RequestBody.getTopic(), sendMessageBatchV2RequestBody.getTag(), +// sendMessageBatchV2RequestBody.getMsg().getBytes(EventMeshConstants.DEFAULT_CHARSET)); +// } + // body + omsMsg.setBody(sendMessageBatchV2RequestBody.getMsg().getBytes(EventMeshConstants.DEFAULT_CHARSET)); + // topic + // topic + omsMsg.setTopic(sendMessageBatchV2RequestBody.getTopic()); + omsMsg.putSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION, sendMessageBatchV2RequestBody.getTopic()); + if (!StringUtils.isBlank(sendMessageBatchV2RequestBody.getTag())) { + omsMsg.putUserProperties(EventMeshConstants.TAG, sendMessageBatchV2RequestBody.getTag()); + } + omsMsg.putUserProperties("msgType", "persistent"); + // ttl + omsMsg.putSystemProperties(Constants.PROPERTY_MESSAGE_TIMEOUT, sendMessageBatchV2RequestBody.getTtl()); + +// rocketMQMsg.putUserProperty(DeFiBusConstant.KEY, DeFiBusConstant.PERSISTENT); +// MessageAccessor.putProperty(rocketMQMsg, DeFiBusConstant.PROPERTY_MESSAGE_TTL, sendMessageBatchV2RequestBody.getTtl()); + + if (batchMessageLogger.isDebugEnabled()) { + batchMessageLogger.debug("msg2MQMsg suc, topic:{}, msg:{}", sendMessageBatchV2RequestBody.getTopic(), sendMessageBatchV2RequestBody.getMsg()); + } + + } catch (Exception e) { + batchMessageLogger.error("msg2MQMsg err, topic:{}, msg:{}", sendMessageBatchV2RequestBody.getTopic(), sendMessageBatchV2RequestBody.getMsg(), e); + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + sendMessageBatchV2ResponseHeader, + SendMessageBatchV2ResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PACKAGE_MSG_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PACKAGE_MSG_ERR.getErrMsg() + EventMeshUtil.stackTrace(e, 2))); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + + eventMeshHTTPServer.metrics.summaryMetrics.recordSendBatchMsg(1); + + final SendMessageContext sendMessageContext = new SendMessageContext(sendMessageBatchV2RequestBody.getBizSeqNo(), omsMsg, batchEventMeshProducer, eventMeshHTTPServer); + + try { + batchEventMeshProducer.send(sendMessageContext, new SendCallback() { + @Override + public void onSuccess(SendResult sendResult) { + long batchEndTime = System.currentTimeMillis(); + eventMeshHTTPServer.metrics.summaryMetrics.recordBatchSendMsgCost(batchEndTime - batchStartTime); + batchMessageLogger.debug("batchMessageV2|eventMesh2mq|REQ|ASYNC|bizSeqNo={}|send2MQCost={}ms|topic={}", + sendMessageBatchV2RequestBody.getBizSeqNo(), + batchEndTime - batchStartTime, + sendMessageBatchV2RequestBody.getTopic()); + } + + @Override + public void onException(OnExceptionContext context) { + long batchEndTime = System.currentTimeMillis(); + eventMeshHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000)); + eventMeshHTTPServer.metrics.summaryMetrics.recordBatchSendMsgCost(batchEndTime - batchStartTime); + batchMessageLogger.error("batchMessageV2|eventMesh2mq|REQ|ASYNC|bizSeqNo={}|send2MQCost={}ms|topic={}", + sendMessageBatchV2RequestBody.getBizSeqNo(), + batchEndTime - batchStartTime, + sendMessageBatchV2RequestBody.getTopic(), context.getException()); + } + +// @Override +// public void onException(Throwable e) { +// long batchEndTime = System.currentTimeMillis(); +// eventMeshHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000)); +// eventMeshHTTPServer.metrics.summaryMetrics.recordBatchSendMsgCost(batchEndTime - batchStartTime); +// batchMessageLogger.error("batchMessageV2|eventMesh2mq|REQ|ASYNC|bizSeqNo={}|send2MQCost={}ms|topic={}", +// sendMessageBatchV2RequestBody.getBizSeqNo(), +// batchEndTime - batchStartTime, +// sendMessageBatchV2RequestBody.getTopic(), e); +// } + }); + } catch (Exception e) { + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + sendMessageBatchV2ResponseHeader, + SendMessageBatchV2ResponseBody.buildBody(EventMeshRetCode.EVENTMESH_SEND_BATCHLOG_MSG_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_SEND_BATCHLOG_MSG_ERR.getErrMsg() + EventMeshUtil.stackTrace(e, 2))); + asyncContext.onComplete(responseEventMeshCommand); + long batchEndTime = System.currentTimeMillis(); + eventMeshHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000)); + eventMeshHTTPServer.metrics.summaryMetrics.recordBatchSendMsgCost(batchEndTime - batchStartTime); + batchMessageLogger.error("batchMessageV2|eventMesh2mq|REQ|ASYNC|bizSeqNo={}|send2MQCost={}ms|topic={}", + sendMessageBatchV2RequestBody.getBizSeqNo(), + batchEndTime - batchStartTime, + sendMessageBatchV2RequestBody.getTopic(), e); + } + + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + sendMessageBatchV2ResponseHeader, + SendMessageBatchV2ResponseBody.buildBody(EventMeshRetCode.SUCCESS.getRetCode(), EventMeshRetCode.SUCCESS.getErrMsg())); + asyncContext.onComplete(responseEventMeshCommand); + + return; + } + + @Override + public boolean rejectRequest() { + return false; + } +} + diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/HeartBeatProcessor.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/HeartBeatProcessor.java new file mode 100644 index 0000000000..9e4c41eda3 --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/HeartBeatProcessor.java @@ -0,0 +1,208 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.core.protocol.http.processor; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import io.netty.channel.ChannelHandlerContext; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.common.IPUtil; +import org.apache.eventmesh.common.command.HttpCommand; +import org.apache.eventmesh.common.protocol.http.body.client.HeartbeatRequestBody; +import org.apache.eventmesh.common.protocol.http.body.client.HeartbeatResponseBody; +import org.apache.eventmesh.common.protocol.http.common.EventMeshRetCode; +import org.apache.eventmesh.common.protocol.http.common.RequestCode; +import org.apache.eventmesh.common.protocol.http.header.client.HeartbeatRequestHeader; +import org.apache.eventmesh.common.protocol.http.header.client.HeartbeatResponseHeader; +import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.http.async.AsyncContext; +import org.apache.eventmesh.runtime.core.protocol.http.async.CompleteHandler; +import org.apache.eventmesh.runtime.core.protocol.http.processor.inf.Client; +import org.apache.eventmesh.runtime.core.protocol.http.processor.inf.HttpRequestProcessor; +import org.apache.eventmesh.runtime.util.EventMeshUtil; +import org.apache.eventmesh.runtime.util.RemotingHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HeartBeatProcessor implements HttpRequestProcessor { + + public Logger httpLogger = LoggerFactory.getLogger("http"); + + private EventMeshHTTPServer eventMeshHTTPServer; + + public HeartBeatProcessor(EventMeshHTTPServer eventMeshHTTPServer) { + this.eventMeshHTTPServer = eventMeshHTTPServer; + } + + @Override + public void processRequest(ChannelHandlerContext ctx, AsyncContext asyncContext) throws Exception { + HttpCommand responseEventMeshCommand; + httpLogger.info("cmd={}|{}|client2eventMesh|from={}|to={}", RequestCode.get(Integer.valueOf(asyncContext.getRequest().getRequestCode())), + EventMeshConstants.PROTOCOL_HTTP, + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), IPUtil.getLocalAddress()); + HeartbeatRequestHeader heartbeatRequestHeader = (HeartbeatRequestHeader) asyncContext.getRequest().getHeader(); + HeartbeatRequestBody heartbeatRequestBody = (HeartbeatRequestBody) asyncContext.getRequest().getBody(); + + HeartbeatResponseHeader heartbeatResponseHeader = + HeartbeatResponseHeader.buildHeader(Integer.valueOf(asyncContext.getRequest().getRequestCode()), eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshCluster, + IPUtil.getLocalAddress(), eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshEnv, + eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshIDC); + + + //validate header + if (StringUtils.isBlank(heartbeatRequestHeader.getIdc()) + || StringUtils.isBlank(heartbeatRequestHeader.getPid()) + || !StringUtils.isNumeric(heartbeatRequestHeader.getPid()) + || StringUtils.isBlank(heartbeatRequestHeader.getSys())) { + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + heartbeatResponseHeader, + HeartbeatResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getErrMsg())); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + + //validate body + if (StringUtils.isBlank(heartbeatRequestBody.getClientType()) + || StringUtils.isBlank(heartbeatRequestBody.getConsumerGroup()) + || CollectionUtils.isEmpty(heartbeatRequestBody.getHeartbeatEntities())) { + + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + heartbeatResponseHeader, + HeartbeatResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getErrMsg())); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + ConcurrentHashMap> tmp = new ConcurrentHashMap<>(); + String env = heartbeatRequestHeader.getEnv(); + String idc = heartbeatRequestHeader.getIdc(); + String sys = heartbeatRequestHeader.getSys(); + String ip = heartbeatRequestHeader.getIp(); + String pid = heartbeatRequestHeader.getPid(); + String consumerGroup = heartbeatRequestBody.getConsumerGroup(); + List heartbeatEntities = heartbeatRequestBody.getHeartbeatEntities(); + for (HeartbeatRequestBody.HeartbeatEntity heartbeatEntity : heartbeatEntities) { + String topic = heartbeatEntity.topic; + String url = heartbeatEntity.url; + Client client = new Client(); + client.env = env; + client.idc = idc; + client.sys = sys; + client.ip = ip; + client.pid = pid; + client.consumerGroup = consumerGroup; + client.topic = topic; + client.url = url; + + client.lastUpTime = new Date(); + + if (StringUtils.isBlank(client.topic)) { + continue; + } + + if (StringUtils.isBlank(client.url)) { + continue; + } + + String groupTopicKey = client.consumerGroup + "@" + client.topic; + + if (tmp.containsKey(groupTopicKey)) { + tmp.get(groupTopicKey).add(client); + } else { + List clients = new ArrayList<>(); + clients.add(client); + tmp.put(groupTopicKey, clients); + } + } + synchronized (eventMeshHTTPServer.localClientInfoMapping) { + for (Map.Entry> groupTopicClientMapping : tmp.entrySet()) { + List localClientList = eventMeshHTTPServer.localClientInfoMapping.get(groupTopicClientMapping.getKey()); + if (CollectionUtils.isEmpty(localClientList)) { + eventMeshHTTPServer.localClientInfoMapping.put(groupTopicClientMapping.getKey(), groupTopicClientMapping.getValue()); + } else { + List tmpClientList = groupTopicClientMapping.getValue(); + supplyClientInfoList(tmpClientList, localClientList); + eventMeshHTTPServer.localClientInfoMapping.put(groupTopicClientMapping.getKey(), localClientList); + } + + } + } + + long startTime = System.currentTimeMillis(); + try { + + final CompleteHandler handler = new CompleteHandler() { + @Override + public void onResponse(HttpCommand httpCommand) { + try { + if (httpLogger.isDebugEnabled()) { + httpLogger.debug("{}", httpCommand); + } + eventMeshHTTPServer.sendResponse(ctx, httpCommand.httpResponse()); + eventMeshHTTPServer.metrics.summaryMetrics.recordHTTPReqResTimeCost(System.currentTimeMillis() - asyncContext.getRequest().getReqTime()); + } catch (Exception ex) { + } + } + }; + + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + EventMeshRetCode.SUCCESS.getRetCode(), EventMeshRetCode.SUCCESS.getErrMsg()); + asyncContext.onComplete(responseEventMeshCommand, handler); + } catch (Exception e) { + HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( + heartbeatResponseHeader, + HeartbeatResponseBody.buildBody(EventMeshRetCode.EVENTMESH_HEARTBEAT_ERR.getRetCode(), + EventMeshRetCode.EVENTMESH_HEARTBEAT_ERR.getErrMsg() + EventMeshUtil.stackTrace(e, 2))); + asyncContext.onComplete(err); + long endTime = System.currentTimeMillis(); + httpLogger.error("message|eventMesh2mq|REQ|ASYNC|heartBeatMessageCost={}ms", + endTime - startTime, e); + eventMeshHTTPServer.metrics.summaryMetrics.recordSendMsgFailed(); + eventMeshHTTPServer.metrics.summaryMetrics.recordSendMsgCost(endTime - startTime); + } + + } + + private void supplyClientInfoList(List tmpClientList, List localClientList) { + for (Client tmpClient : tmpClientList) { + boolean isContains = false; + for (Client localClient : localClientList) { + if (StringUtils.equals(localClient.url, tmpClient.url)) { + isContains = true; + localClient.lastUpTime = tmpClient.lastUpTime; + break; + } + } + if (!isContains) { + localClientList.add(tmpClient); + } + } + } + + @Override + public boolean rejectRequest() { + return false; + } + +} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/ReplyMessageProcessor.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/ReplyMessageProcessor.java new file mode 100644 index 0000000000..5d8745b611 --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/ReplyMessageProcessor.java @@ -0,0 +1,276 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.core.protocol.http.processor; + +import java.util.Map; + +import io.netty.channel.ChannelHandlerContext; +import io.openmessaging.api.Message; +import io.openmessaging.api.OnExceptionContext; +import io.openmessaging.api.SendCallback; +import io.openmessaging.api.SendResult; + +import org.apache.commons.collections4.MapUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.IPUtil; +import org.apache.eventmesh.common.LiteMessage; +import org.apache.eventmesh.common.command.HttpCommand; +import org.apache.eventmesh.common.protocol.http.body.message.ReplyMessageRequestBody; +import org.apache.eventmesh.common.protocol.http.body.message.ReplyMessageResponseBody; +import org.apache.eventmesh.common.protocol.http.body.message.SendMessageResponseBody; +import org.apache.eventmesh.common.protocol.http.common.EventMeshRetCode; +import org.apache.eventmesh.common.protocol.http.common.RequestCode; +import org.apache.eventmesh.common.protocol.http.header.message.ReplyMessageRequestHeader; +import org.apache.eventmesh.common.protocol.http.header.message.ReplyMessageResponseHeader; +import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.http.async.AsyncContext; +import org.apache.eventmesh.runtime.core.protocol.http.async.CompleteHandler; +import org.apache.eventmesh.runtime.core.protocol.http.processor.inf.HttpRequestProcessor; +import org.apache.eventmesh.runtime.core.protocol.http.producer.EventMeshProducer; +import org.apache.eventmesh.runtime.core.protocol.http.producer.SendMessageContext; +import org.apache.eventmesh.runtime.util.EventMeshUtil; +import org.apache.eventmesh.runtime.util.RemotingHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ReplyMessageProcessor implements HttpRequestProcessor { + + public Logger messageLogger = LoggerFactory.getLogger("message"); + + public Logger cmdLogger = LoggerFactory.getLogger("cmd"); + + public Logger httpLogger = LoggerFactory.getLogger("http"); + + private EventMeshHTTPServer eventMeshHTTPServer; + + public ReplyMessageProcessor(EventMeshHTTPServer eventMeshHTTPServer) { + this.eventMeshHTTPServer = eventMeshHTTPServer; + } + + @Override + public void processRequest(ChannelHandlerContext ctx, AsyncContext asyncContext) throws Exception { + HttpCommand responseEventMeshCommand; + + cmdLogger.info("cmd={}|{}|client2eventMesh|from={}|to={}", RequestCode.get(Integer.valueOf(asyncContext.getRequest().getRequestCode())), + EventMeshConstants.PROTOCOL_HTTP, + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), IPUtil.getLocalAddress()); + + ReplyMessageRequestHeader replyMessageRequestHeader = (ReplyMessageRequestHeader) asyncContext.getRequest().getHeader(); + ReplyMessageRequestBody replyMessageRequestBody = (ReplyMessageRequestBody) asyncContext.getRequest().getBody(); + + ReplyMessageResponseHeader replyMessageResponseHeader = + ReplyMessageResponseHeader.buildHeader(Integer.valueOf(asyncContext.getRequest().getRequestCode()), eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshCluster, + IPUtil.getLocalAddress(), eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshEnv, + eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshIDC); + + //HEADER校验 + if (StringUtils.isBlank(replyMessageRequestHeader.getIdc()) + || StringUtils.isBlank(replyMessageRequestHeader.getPid()) + || !StringUtils.isNumeric(replyMessageRequestHeader.getPid()) + || StringUtils.isBlank(replyMessageRequestHeader.getSys())) { + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + replyMessageResponseHeader, + ReplyMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getErrMsg())); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + + //validate body + if (StringUtils.isBlank(replyMessageRequestBody.getBizSeqNo()) + || StringUtils.isBlank(replyMessageRequestBody.getUniqueId()) + || StringUtils.isBlank(replyMessageRequestBody.getProducerGroup()) + || StringUtils.isBlank(replyMessageRequestBody.getContent())) { + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + replyMessageResponseHeader, + ReplyMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getErrMsg())); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + + String producerGroup = replyMessageRequestBody.getProducerGroup(); + EventMeshProducer eventMeshProducer = eventMeshHTTPServer.getProducerManager().getEventMeshProducer(producerGroup); + + if (!eventMeshProducer.getStarted().get()) { + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + replyMessageResponseHeader, + ReplyMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_GROUP_PRODUCER_STOPED_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_GROUP_PRODUCER_STOPED_ERR.getErrMsg())); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + + long startTime = System.currentTimeMillis(); + +// Message rocketMQMsg; + Message omsMsg = new Message(); + String replyTopic = EventMeshConstants.RR_REPLY_TOPIC; + + Map extFields = replyMessageRequestBody.getExtFields(); + final String replyMQCluster = MapUtils.getString(extFields, EventMeshConstants.PROPERTY_MESSAGE_CLUSTER, null); + if (!org.apache.commons.lang3.StringUtils.isEmpty(replyMQCluster)) { + replyTopic = replyMQCluster + "-" + replyTopic; + } else { + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + replyMessageResponseHeader, + ReplyMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_REPLY_MSG_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_REPLY_MSG_ERR.getErrMsg())); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + + try { + // body + omsMsg.setBody(replyMessageRequestBody.getContent().getBytes(EventMeshConstants.DEFAULT_CHARSET)); + omsMsg.setTopic(replyTopic); + // topic + omsMsg.putSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION, replyTopic); +// if (!StringUtils.isBlank(sendMessageRequestBody.getTag())) { +// omsMsg.putUserHeaders("Tag", sendMessageRequestBody.getTag()); +// } +// rocketMQMsg = new Message(replyTopic, +// replyMessageRequestBody.getContent().getBytes(EventMeshConstants.DEFAULT_CHARSET)); + omsMsg.putUserProperties("msgType", "persistent"); +// rocketMQMsg.putUserProperty(DeFiBusConstant.KEY, DeFiBusConstant.PERSISTENT); + for (Map.Entry entry : extFields.entrySet()) { + omsMsg.putUserProperties(entry.getKey(), entry.getValue()); + } + +// //for rocketmq support +// MessageAccessor.putProperty(rocketMQMsg, MessageConst.PROPERTY_MESSAGE_TYPE, MixAll.REPLY_MESSAGE_FLAG); +// MessageAccessor.putProperty(rocketMQMsg, MessageConst.PROPERTY_CORRELATION_ID, rocketMQMsg.getProperty(DeFiBusConstant.PROPERTY_RR_REQUEST_ID)); +// MessageAccessor.putProperty(rocketMQMsg, MessageConst.PROPERTY_MESSAGE_REPLY_TO_CLIENT, rocketMQMsg.getProperty(DeFiBusConstant.PROPERTY_MESSAGE_REPLY_TO)); + // ttl + omsMsg.putSystemProperties(Constants.PROPERTY_MESSAGE_TIMEOUT, String.valueOf(EventMeshConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS)); +// MessageAccessor.putProperty(rocketMQMsg, DeFiBusConstant.PROPERTY_MESSAGE_TTL, String.valueOf(EventMeshConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS)); + omsMsg.putUserProperties(EventMeshConstants.REQ_C2EVENTMESH_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + if (messageLogger.isDebugEnabled()) { + messageLogger.debug("msg2MQMsg suc, bizSeqNo={}, topic={}", replyMessageRequestBody.getBizSeqNo(), + replyTopic); + } + + } catch (Exception e) { + messageLogger.error("msg2MQMsg err, bizSeqNo={}, topic={}", replyMessageRequestBody.getBizSeqNo(), + replyTopic, e); + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + replyMessageResponseHeader, + ReplyMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PACKAGE_MSG_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PACKAGE_MSG_ERR.getErrMsg() + EventMeshUtil.stackTrace(e, 2))); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + + final SendMessageContext sendMessageContext = new SendMessageContext(replyMessageRequestBody.getBizSeqNo(), omsMsg, eventMeshProducer, eventMeshHTTPServer); + eventMeshHTTPServer.metrics.summaryMetrics.recordReplyMsg(); + + CompleteHandler handler = new CompleteHandler() { + @Override + public void onResponse(HttpCommand httpCommand) { + try { + if (httpLogger.isDebugEnabled()) { + httpLogger.debug("{}", httpCommand); + } + eventMeshHTTPServer.sendResponse(ctx, httpCommand.httpResponse()); + eventMeshHTTPServer.metrics.summaryMetrics.recordHTTPReqResTimeCost(System.currentTimeMillis() - asyncContext.getRequest().getReqTime()); + } catch (Exception ex) { + } + } + }; + +// LiteMessage liteMessage = new LiteMessage(replyMessageRequestBody.getBizSeqNo(), +// replyMessageRequestBody.getUniqueId(), replyMessageRequestBody.getOrigTopic(), +// replyMessageRequestBody.getContent()); + + try { + sendMessageContext.getMsg().getUserProperties().put(EventMeshConstants.REQ_EVENTMESH2MQ_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + eventMeshProducer.reply(sendMessageContext, new SendCallback() { + @Override + public void onSuccess(SendResult sendResult) { + HttpCommand succ = asyncContext.getRequest().createHttpCommandResponse( + replyMessageResponseHeader, + SendMessageResponseBody.buildBody(EventMeshRetCode.SUCCESS.getRetCode(), EventMeshRetCode.SUCCESS.getErrMsg())); + asyncContext.onComplete(succ, handler); + long endTime = System.currentTimeMillis(); + eventMeshHTTPServer.metrics.summaryMetrics.recordReplyMsgCost(endTime - startTime); + messageLogger.info("message|eventMesh2mq|RSP|SYNC|reply2MQCost={}|topic={}|origTopic={}|bizSeqNo={}|uniqueId={}", + endTime - startTime, + replyMQCluster + "-" + EventMeshConstants.RR_REPLY_TOPIC, + replyMessageRequestBody.getOrigTopic(), + replyMessageRequestBody.getBizSeqNo(), + replyMessageRequestBody.getUniqueId()); + } + + @Override + public void onException(OnExceptionContext context) { + HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( + replyMessageResponseHeader, + SendMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_REPLY_MSG_ERR.getRetCode(), + EventMeshRetCode.EVENTMESH_REPLY_MSG_ERR.getErrMsg() + EventMeshUtil.stackTrace(context.getException(), 2))); + asyncContext.onComplete(err, handler); + long endTime = System.currentTimeMillis(); + eventMeshHTTPServer.metrics.summaryMetrics.recordReplyMsgFailed(); + eventMeshHTTPServer.metrics.summaryMetrics.recordReplyMsgCost(endTime - startTime); + messageLogger.error("message|eventMesh2mq|RSP|SYNC|reply2MQCost={}|topic={}|origTopic={}|bizSeqNo={}|uniqueId={}", + endTime - startTime, + replyMQCluster + "-" + EventMeshConstants.RR_REPLY_TOPIC, + replyMessageRequestBody.getOrigTopic(), + replyMessageRequestBody.getBizSeqNo(), + replyMessageRequestBody.getUniqueId(), context.getException()); + } + +// @Override +// public void onException(Throwable e) { +// HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( +// replyMessageResponseHeader, +// SendMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_REPLY_MSG_ERR.getRetCode(), +// EventMeshRetCode.EVENTMESH_REPLY_MSG_ERR.getErrMsg() + EventMeshUtil.stackTrace(e, 2))); +// asyncContext.onComplete(err, handler); +// long endTime = System.currentTimeMillis(); +// eventMeshHTTPServer.metrics.summaryMetrics.recordReplyMsgFailed(); +// eventMeshHTTPServer.metrics.summaryMetrics.recordReplyMsgCost(endTime - startTime); +// messageLogger.error("message|eventMesh2mq|RSP|SYNC|reply2MQCost={}|topic={}|origTopic={}|bizSeqNo={}|uniqueId={}", +// endTime - startTime, +// replyMQCluster + "-" + EventMeshConstants.RR_REPLY_TOPIC, +// replyMessageRequestBody.getOrigTopic(), +// replyMessageRequestBody.getBizSeqNo(), +// replyMessageRequestBody.getUniqueId(), e); +// } + }); + } catch (Exception ex) { + HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( + replyMessageResponseHeader, + SendMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_REPLY_MSG_ERR.getRetCode(), + EventMeshRetCode.EVENTMESH_REPLY_MSG_ERR.getErrMsg() + EventMeshUtil.stackTrace(ex, 2))); + asyncContext.onComplete(err); + long endTime = System.currentTimeMillis(); + messageLogger.error("message|eventMesh2mq|RSP|SYNC|reply2MQCost={}|topic={}|origTopic={}|bizSeqNo={}|uniqueId={}", + endTime - startTime, + replyTopic, + replyMessageRequestBody.getOrigTopic(), + replyMessageRequestBody.getBizSeqNo(), + replyMessageRequestBody.getUniqueId(), ex); + eventMeshHTTPServer.metrics.summaryMetrics.recordReplyMsgFailed(); + eventMeshHTTPServer.metrics.summaryMetrics.recordReplyMsgCost(endTime - startTime); + } + + return; + } + + @Override + public boolean rejectRequest() { + return false; + } +} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/SendAsyncMessageProcessor.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/SendAsyncMessageProcessor.java new file mode 100644 index 0000000000..cd3d4bed35 --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/SendAsyncMessageProcessor.java @@ -0,0 +1,244 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.core.protocol.http.processor; + +import io.netty.channel.ChannelHandlerContext; +import io.openmessaging.api.Message; +import io.openmessaging.api.OnExceptionContext; +import io.openmessaging.api.SendCallback; +import io.openmessaging.api.SendResult; + +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.IPUtil; +import org.apache.eventmesh.common.LiteMessage; +import org.apache.eventmesh.common.command.HttpCommand; +import org.apache.eventmesh.common.protocol.http.body.message.SendMessageRequestBody; +import org.apache.eventmesh.common.protocol.http.body.message.SendMessageResponseBody; +import org.apache.eventmesh.common.protocol.http.common.EventMeshRetCode; +import org.apache.eventmesh.common.protocol.http.common.RequestCode; +import org.apache.eventmesh.common.protocol.http.header.message.SendMessageRequestHeader; +import org.apache.eventmesh.common.protocol.http.header.message.SendMessageResponseHeader; +import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.http.async.AsyncContext; +import org.apache.eventmesh.runtime.core.protocol.http.async.CompleteHandler; +import org.apache.eventmesh.runtime.core.protocol.http.processor.inf.HttpRequestProcessor; +import org.apache.eventmesh.runtime.core.protocol.http.producer.EventMeshProducer; +import org.apache.eventmesh.runtime.core.protocol.http.producer.SendMessageContext; +import org.apache.eventmesh.runtime.util.EventMeshUtil; +import org.apache.eventmesh.runtime.util.RemotingHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SendAsyncMessageProcessor implements HttpRequestProcessor { + + public Logger messageLogger = LoggerFactory.getLogger("message"); + + public Logger httpLogger = LoggerFactory.getLogger("http"); + + public Logger cmdLogger = LoggerFactory.getLogger("cmd"); + + private EventMeshHTTPServer eventMeshHTTPServer; + + public SendAsyncMessageProcessor(EventMeshHTTPServer eventMeshHTTPServer) { + this.eventMeshHTTPServer = eventMeshHTTPServer; + } + + @Override + public void processRequest(ChannelHandlerContext ctx, AsyncContext asyncContext) throws Exception { + + HttpCommand responseEventMeshCommand; + + cmdLogger.info("cmd={}|{}|client2eventMesh|from={}|to={}", RequestCode.get(Integer.valueOf(asyncContext.getRequest().getRequestCode())), + EventMeshConstants.PROTOCOL_HTTP, + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), IPUtil.getLocalAddress()); + + SendMessageRequestHeader sendMessageRequestHeader = (SendMessageRequestHeader) asyncContext.getRequest().getHeader(); + SendMessageRequestBody sendMessageRequestBody = (SendMessageRequestBody) asyncContext.getRequest().getBody(); + + SendMessageResponseHeader sendMessageResponseHeader = + SendMessageResponseHeader.buildHeader(Integer.valueOf(asyncContext.getRequest().getRequestCode()), eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshCluster, + IPUtil.getLocalAddress(), eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshEnv, + eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshIDC); + + //validate header + if (StringUtils.isBlank(sendMessageRequestHeader.getIdc()) + || StringUtils.isBlank(sendMessageRequestHeader.getPid()) + || !StringUtils.isNumeric(sendMessageRequestHeader.getPid()) + || StringUtils.isBlank(sendMessageRequestHeader.getSys())) { + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + sendMessageResponseHeader, + SendMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getErrMsg())); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + + //validate body + if (StringUtils.isBlank(sendMessageRequestBody.getBizSeqNo()) + || StringUtils.isBlank(sendMessageRequestBody.getUniqueId()) + || StringUtils.isBlank(sendMessageRequestBody.getProducerGroup()) + || StringUtils.isBlank(sendMessageRequestBody.getTopic()) + || StringUtils.isBlank(sendMessageRequestBody.getContent()) + || (StringUtils.isBlank(sendMessageRequestBody.getTtl()))) { + //sync message TTL can't be empty + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + sendMessageResponseHeader, + SendMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getErrMsg())); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + + String producerGroup = sendMessageRequestBody.getProducerGroup(); + EventMeshProducer eventMeshProducer = eventMeshHTTPServer.getProducerManager().getEventMeshProducer(producerGroup); + + if (!eventMeshProducer.getStarted().get()) { + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + sendMessageResponseHeader, + SendMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_GROUP_PRODUCER_STOPED_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_GROUP_PRODUCER_STOPED_ERR.getErrMsg())); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + + String ttl = String.valueOf(EventMeshConstants.DEFAULT_MSG_TTL_MILLS); + if (StringUtils.isNotBlank(sendMessageRequestBody.getTtl()) && StringUtils.isNumeric(sendMessageRequestBody.getTtl())) { + ttl = sendMessageRequestBody.getTtl(); + } + + Message omsMsg = new Message(); + try { + // body + omsMsg.setBody(sendMessageRequestBody.getContent().getBytes(EventMeshConstants.DEFAULT_CHARSET)); + // topic + omsMsg.setTopic(sendMessageRequestBody.getTopic()); + omsMsg.putSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION, sendMessageRequestBody.getTopic()); + + if (!StringUtils.isBlank(sendMessageRequestBody.getTag())) { + omsMsg.putUserProperties(EventMeshConstants.TAG, sendMessageRequestBody.getTag()); + } + // ttl + omsMsg.putUserProperties(Constants.PROPERTY_MESSAGE_TIMEOUT, ttl); + // bizNo + omsMsg.putSystemProperties(Constants.PROPERTY_MESSAGE_SEARCH_KEYS, sendMessageRequestBody.getBizSeqNo()); + omsMsg.putUserProperties("msgType", "persistent"); + omsMsg.putUserProperties(EventMeshConstants.REQ_C2EVENTMESH_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + omsMsg.putUserProperties(Constants.RMB_UNIQ_ID, sendMessageRequestBody.getUniqueId()); + omsMsg.putUserProperties(EventMeshConstants.REQ_EVENTMESH2MQ_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + + // new rocketmq client can't support put DeFiBusConstant.PROPERTY_MESSAGE_TTL +// rocketMQMsg.putUserProperty(DeFiBusConstant.PROPERTY_MESSAGE_TTL, ttl); + + if (messageLogger.isDebugEnabled()) { + messageLogger.debug("msg2MQMsg suc, bizSeqNo={}, topic={}", sendMessageRequestBody.getBizSeqNo(), + sendMessageRequestBody.getTopic()); + } + } catch (Exception e) { + messageLogger.error("msg2MQMsg err, bizSeqNo={}, topic={}", sendMessageRequestBody.getBizSeqNo(), + sendMessageRequestBody.getTopic(), e); + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + sendMessageResponseHeader, + SendMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PACKAGE_MSG_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PACKAGE_MSG_ERR.getErrMsg() + EventMeshUtil.stackTrace(e, 2))); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + + final SendMessageContext sendMessageContext = new SendMessageContext(sendMessageRequestBody.getBizSeqNo(), omsMsg, eventMeshProducer, eventMeshHTTPServer); + eventMeshHTTPServer.metrics.summaryMetrics.recordSendMsg(); + + long startTime = System.currentTimeMillis(); + + final CompleteHandler handler = new CompleteHandler() { + @Override + public void onResponse(HttpCommand httpCommand) { + try { + if (httpLogger.isDebugEnabled()) { + httpLogger.debug("{}", httpCommand); + } + eventMeshHTTPServer.sendResponse(ctx, httpCommand.httpResponse()); + eventMeshHTTPServer.metrics.summaryMetrics.recordHTTPReqResTimeCost(System.currentTimeMillis() - asyncContext.getRequest().getReqTime()); + } catch (Exception ex) { + } + } + }; + + LiteMessage liteMessage = new LiteMessage(sendMessageRequestBody.getBizSeqNo(), + sendMessageRequestBody.getUniqueId(), sendMessageRequestBody.getTopic(), + sendMessageRequestBody.getContent()) + .setProp(sendMessageRequestBody.getExtFields()); + + try { + sendMessageContext.getMsg().getUserProperties().put(EventMeshConstants.REQ_EVENTMESH2MQ_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + eventMeshProducer.send(sendMessageContext, new SendCallback() { + + @Override + public void onSuccess(SendResult sendResult) { + HttpCommand succ = asyncContext.getRequest().createHttpCommandResponse( + sendMessageResponseHeader, + SendMessageResponseBody.buildBody(EventMeshRetCode.SUCCESS.getRetCode(), EventMeshRetCode.SUCCESS.getErrMsg() + sendResult.toString())); + asyncContext.onComplete(succ, handler); + long endTime = System.currentTimeMillis(); + eventMeshHTTPServer.metrics.summaryMetrics.recordSendMsgCost(endTime - startTime); + messageLogger.info("message|eventMesh2mq|REQ|ASYNC|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", + endTime - startTime, + sendMessageRequestBody.getTopic(), + sendMessageRequestBody.getBizSeqNo(), + sendMessageRequestBody.getUniqueId()); + } + + @Override + public void onException(OnExceptionContext context) { + HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( + sendMessageResponseHeader, + SendMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_SEND_ASYNC_MSG_ERR.getRetCode(), + EventMeshRetCode.EVENTMESH_SEND_ASYNC_MSG_ERR.getErrMsg() + EventMeshUtil.stackTrace(context.getException(), 2))); + asyncContext.onComplete(err, handler); + long endTime = System.currentTimeMillis(); + eventMeshHTTPServer.metrics.summaryMetrics.recordSendMsgFailed(); + eventMeshHTTPServer.metrics.summaryMetrics.recordSendMsgCost(endTime - startTime); + messageLogger.error("message|eventMesh2mq|REQ|ASYNC|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", + endTime - startTime, + sendMessageRequestBody.getTopic(), + sendMessageRequestBody.getBizSeqNo(), + sendMessageRequestBody.getUniqueId(), context.getException()); + } + }); + } catch (Exception ex) { + HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( + sendMessageResponseHeader, + SendMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_SEND_ASYNC_MSG_ERR.getRetCode(), + EventMeshRetCode.EVENTMESH_SEND_ASYNC_MSG_ERR.getErrMsg() + EventMeshUtil.stackTrace(ex, 2))); + asyncContext.onComplete(err); + long endTime = System.currentTimeMillis(); + messageLogger.error("message|eventMesh2mq|REQ|ASYNC|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", + endTime - startTime, + sendMessageRequestBody.getTopic(), + sendMessageRequestBody.getBizSeqNo(), + sendMessageRequestBody.getUniqueId(), ex); + eventMeshHTTPServer.metrics.summaryMetrics.recordSendMsgFailed(); + eventMeshHTTPServer.metrics.summaryMetrics.recordSendMsgCost(endTime - startTime); + } + + return; + } + + @Override + public boolean rejectRequest() { + return false; + } + +} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/SendSyncMessageProcessor.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/SendSyncMessageProcessor.java new file mode 100644 index 0000000000..5511e384e3 --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/SendSyncMessageProcessor.java @@ -0,0 +1,299 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.core.protocol.http.processor; + +import com.alibaba.fastjson.JSON; + +import io.netty.channel.ChannelHandlerContext; +import io.openmessaging.api.Message; +import io.openmessaging.api.OnExceptionContext; +import io.openmessaging.api.SendCallback; +import io.openmessaging.api.SendResult; + +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.api.RRCallback; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.IPUtil; +import org.apache.eventmesh.common.LiteMessage; +import org.apache.eventmesh.common.command.HttpCommand; +import org.apache.eventmesh.common.protocol.http.body.message.SendMessageRequestBody; +import org.apache.eventmesh.common.protocol.http.body.message.SendMessageResponseBody; +import org.apache.eventmesh.common.protocol.http.common.EventMeshRetCode; +import org.apache.eventmesh.common.protocol.http.common.RequestCode; +import org.apache.eventmesh.common.protocol.http.header.message.SendMessageRequestHeader; +import org.apache.eventmesh.common.protocol.http.header.message.SendMessageResponseHeader; +import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.http.async.AsyncContext; +import org.apache.eventmesh.runtime.core.protocol.http.async.CompleteHandler; +import org.apache.eventmesh.runtime.core.protocol.http.processor.inf.HttpRequestProcessor; +import org.apache.eventmesh.runtime.core.protocol.http.producer.EventMeshProducer; +import org.apache.eventmesh.runtime.core.protocol.http.producer.SendMessageContext; +import org.apache.eventmesh.runtime.util.EventMeshUtil; +import org.apache.eventmesh.runtime.util.OMSUtil; +import org.apache.eventmesh.runtime.util.RemotingHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SendSyncMessageProcessor implements HttpRequestProcessor { + + public Logger messageLogger = LoggerFactory.getLogger("message"); + + public Logger cmdLogger = LoggerFactory.getLogger("cmd"); + + public Logger httpLogger = LoggerFactory.getLogger("http"); + + private EventMeshHTTPServer eventMeshHTTPServer; + + public SendSyncMessageProcessor(EventMeshHTTPServer eventMeshHTTPServer) { + this.eventMeshHTTPServer = eventMeshHTTPServer; + } + + @Override + public void processRequest(ChannelHandlerContext ctx, AsyncContext asyncContext) throws Exception { + + HttpCommand responseEventMeshCommand; + + cmdLogger.info("cmd={}|{}|client2eventMesh|from={}|to={}", RequestCode.get(Integer.valueOf(asyncContext.getRequest().getRequestCode())), + EventMeshConstants.PROTOCOL_HTTP, + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), IPUtil.getLocalAddress()); + + SendMessageRequestHeader sendMessageRequestHeader = (SendMessageRequestHeader) asyncContext.getRequest().getHeader(); + SendMessageRequestBody sendMessageRequestBody = (SendMessageRequestBody) asyncContext.getRequest().getBody(); + + SendMessageResponseHeader sendMessageResponseHeader = + SendMessageResponseHeader.buildHeader(Integer.valueOf(asyncContext.getRequest().getRequestCode()), eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshCluster, + IPUtil.getLocalAddress(), eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshEnv, + eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshIDC); + + if (StringUtils.isBlank(sendMessageRequestHeader.getIdc()) + || StringUtils.isBlank(sendMessageRequestHeader.getPid()) + || !StringUtils.isNumeric(sendMessageRequestHeader.getPid()) + || StringUtils.isBlank(sendMessageRequestHeader.getSys())) { + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + sendMessageResponseHeader, + SendMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getErrMsg())); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + + if (StringUtils.isBlank(sendMessageRequestBody.getBizSeqNo()) + || StringUtils.isBlank(sendMessageRequestBody.getUniqueId()) + || StringUtils.isBlank(sendMessageRequestBody.getProducerGroup()) + || StringUtils.isBlank(sendMessageRequestBody.getTopic()) + || StringUtils.isBlank(sendMessageRequestBody.getContent()) + || (StringUtils.isBlank(sendMessageRequestBody.getTtl()))) { + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + sendMessageResponseHeader, + SendMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getErrMsg())); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + + String producerGroup = sendMessageRequestBody.getProducerGroup(); + EventMeshProducer eventMeshProducer = eventMeshHTTPServer.getProducerManager().getEventMeshProducer(producerGroup); + + if (!eventMeshProducer.getStarted().get()) { + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + sendMessageResponseHeader, + SendMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_GROUP_PRODUCER_STOPED_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_GROUP_PRODUCER_STOPED_ERR.getErrMsg())); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + + String ttl = String.valueOf(EventMeshConstants.DEFAULT_MSG_TTL_MILLS); + if (StringUtils.isNotBlank(sendMessageRequestBody.getTtl()) && StringUtils.isNumeric(sendMessageRequestBody.getTtl())) { + ttl = sendMessageRequestBody.getTtl(); + } + + Message omsMsg = new Message(); + try { + // body + omsMsg.setBody(sendMessageRequestBody.getContent().getBytes(EventMeshConstants.DEFAULT_CHARSET)); + // topic + omsMsg.setTopic(sendMessageRequestBody.getTopic()); + omsMsg.putSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION, sendMessageRequestBody.getTopic()); + if (!StringUtils.isBlank(sendMessageRequestBody.getTag())) { + omsMsg.putUserProperties("Tag", sendMessageRequestBody.getTag()); + } + // ttl + omsMsg.putSystemProperties(Constants.PROPERTY_MESSAGE_TIMEOUT, ttl); + // bizNo + omsMsg.putSystemProperties(Constants.PROPERTY_MESSAGE_SEARCH_KEYS, sendMessageRequestBody.getBizSeqNo()); + omsMsg.putUserProperties("msgType", "persistent"); + omsMsg.putUserProperties(EventMeshConstants.REQ_C2EVENTMESH_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + omsMsg.putUserProperties(Constants.RMB_UNIQ_ID, sendMessageRequestBody.getUniqueId()); +// omsMsg.putUserProperties("REPLY_TO", eventMeshProducer.getMqProducerWrapper().getMeshMQProducer().buildMQClientId()); + + if (messageLogger.isDebugEnabled()) { + messageLogger.debug("msg2MQMsg suc, bizSeqNo={}, topic={}", sendMessageRequestBody.getBizSeqNo(), + sendMessageRequestBody.getTopic()); + } + omsMsg.putUserProperties(EventMeshConstants.REQ_EVENTMESH2MQ_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + } catch (Exception e) { + messageLogger.error("msg2MQMsg err, bizSeqNo={}, topic={}", sendMessageRequestBody.getBizSeqNo(), + sendMessageRequestBody.getTopic(), e); + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + sendMessageResponseHeader, + SendMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PACKAGE_MSG_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PACKAGE_MSG_ERR.getErrMsg() + EventMeshUtil.stackTrace(e, 2))); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + + final SendMessageContext sendMessageContext = new SendMessageContext(sendMessageRequestBody.getBizSeqNo(), omsMsg, eventMeshProducer, eventMeshHTTPServer); + eventMeshHTTPServer.metrics.summaryMetrics.recordSendMsg(); + + long startTime = System.currentTimeMillis(); + + final CompleteHandler handler = new CompleteHandler() { + @Override + public void onResponse(HttpCommand httpCommand) { + try { + if (httpLogger.isDebugEnabled()) { + httpLogger.debug("{}", httpCommand); + } + eventMeshHTTPServer.sendResponse(ctx, httpCommand.httpResponse()); + eventMeshHTTPServer.metrics.summaryMetrics.recordHTTPReqResTimeCost(System.currentTimeMillis() - asyncContext.getRequest().getReqTime()); + } catch (Exception ex) { + } + } + }; + + LiteMessage liteMessage = new LiteMessage(sendMessageRequestBody.getBizSeqNo(), + sendMessageRequestBody.getUniqueId(), sendMessageRequestBody.getTopic(), + sendMessageRequestBody.getContent()) + .setProp(sendMessageRequestBody.getExtFields()); + + try { + eventMeshProducer.request(sendMessageContext, new SendCallback() { + @Override + public void onSuccess(SendResult sendResult) { + long endTime = System.currentTimeMillis(); + eventMeshHTTPServer.metrics.summaryMetrics.recordSendMsgCost(endTime - startTime); + messageLogger.info("message|eventMesh2mq|REQ|SYNC|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", + endTime - startTime, + sendMessageRequestBody.getTopic(), + sendMessageRequestBody.getBizSeqNo(), + sendMessageRequestBody.getUniqueId()); + } + + @Override + public void onException(OnExceptionContext context) { + HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( + sendMessageResponseHeader, + SendMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_SEND_SYNC_MSG_ERR.getRetCode(), + EventMeshRetCode.EVENTMESH_SEND_SYNC_MSG_ERR.getErrMsg() + EventMeshUtil.stackTrace(context.getException(), 2))); + asyncContext.onComplete(err, handler); + long endTime = System.currentTimeMillis(); + eventMeshHTTPServer.metrics.summaryMetrics.recordSendMsgFailed(); + eventMeshHTTPServer.metrics.summaryMetrics.recordSendMsgCost(endTime - startTime); + messageLogger.error("message|eventMesh2mq|REQ|SYNC|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", + endTime - startTime, + sendMessageRequestBody.getTopic(), + sendMessageRequestBody.getBizSeqNo(), + sendMessageRequestBody.getUniqueId(), context.getException()); + } +// } +// +// @Override +// public void onException(Throwable e) { +// HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( +// sendMessageResponseHeader, +// SendMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_SEND_SYNC_MSG_ERR.getRetCode(), +// EventMeshRetCode.EVENTMESH_SEND_SYNC_MSG_ERR.getErrMsg() + EventMeshUtil.stackTrace(e, 2))); +// asyncContext.onComplete(err, handler); +// long endTime = System.currentTimeMillis(); +// eventMeshHTTPServer.metrics.summaryMetrics.recordSendMsgFailed(); +// eventMeshHTTPServer.metrics.summaryMetrics.recordSendMsgCost(endTime - startTime); +// messageLogger.error("message|eventMesh2mq|REQ|SYNC|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", +// endTime - startTime, +// sendMessageRequestBody.getTopic(), +// sendMessageRequestBody.getBizSeqNo(), +// sendMessageRequestBody.getUniqueId(), e); +// } + }, new RRCallback() { + @Override + public void onSuccess(Message omsMsg) { + omsMsg.getUserProperties().put(Constants.PROPERTY_MESSAGE_BORN_TIMESTAMP, omsMsg.getSystemProperties("BORN_TIMESTAMP")); + omsMsg.getUserProperties().put(EventMeshConstants.STORE_TIMESTAMP, omsMsg.getSystemProperties("STORE_TIMESTAMP")); + omsMsg.getUserProperties().put(EventMeshConstants.RSP_MQ2EVENTMESH_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + messageLogger.info("message|mq2eventMesh|RSP|SYNC|rrCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", + System.currentTimeMillis() - startTime, + sendMessageRequestBody.getTopic(), + sendMessageRequestBody.getBizSeqNo(), + sendMessageRequestBody.getUniqueId()); + + try { + final String rtnMsg = new String(omsMsg.getBody(), EventMeshConstants.DEFAULT_CHARSET); + omsMsg.getUserProperties().put(EventMeshConstants.RSP_EVENTMESH2C_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + HttpCommand succ = asyncContext.getRequest().createHttpCommandResponse( + sendMessageResponseHeader, + SendMessageResponseBody.buildBody(EventMeshRetCode.SUCCESS.getRetCode(), + JSON.toJSONString(new SendMessageResponseBody.ReplyMessage(omsMsg.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION), rtnMsg, + OMSUtil.combineProp(omsMsg.getSystemProperties(), + omsMsg.getUserProperties())) + ))); + asyncContext.onComplete(succ, handler); + } catch (Exception ex) { + HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( + sendMessageResponseHeader, + SendMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_WAITING_RR_MSG_ERR.getRetCode(), + EventMeshRetCode.EVENTMESH_WAITING_RR_MSG_ERR.getErrMsg() + EventMeshUtil.stackTrace(ex, 2))); + asyncContext.onComplete(err, handler); + messageLogger.warn("message|mq2eventMesh|RSP", ex); + } + } + + @Override + public void onException(Throwable e) { + HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( + sendMessageResponseHeader, + SendMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_WAITING_RR_MSG_ERR.getRetCode(), + EventMeshRetCode.EVENTMESH_WAITING_RR_MSG_ERR.getErrMsg() + EventMeshUtil.stackTrace(e, 2))); + asyncContext.onComplete(err, handler); + messageLogger.error("message|mq2eventMesh|RSP|SYNC|rrCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", + System.currentTimeMillis() - startTime, + sendMessageRequestBody.getTopic(), + sendMessageRequestBody.getBizSeqNo(), + sendMessageRequestBody.getUniqueId(), e); + } + }, Integer.valueOf(ttl)); + } catch (Exception ex) { + HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( + sendMessageResponseHeader, + SendMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_SEND_SYNC_MSG_ERR.getRetCode(), + EventMeshRetCode.EVENTMESH_SEND_SYNC_MSG_ERR.getErrMsg() + EventMeshUtil.stackTrace(ex, 2))); + asyncContext.onComplete(err); + long endTime = System.currentTimeMillis(); + eventMeshHTTPServer.metrics.summaryMetrics.recordSendMsgFailed(); + eventMeshHTTPServer.metrics.summaryMetrics.recordSendMsgCost(endTime - startTime); + messageLogger.error("message|eventMesh2mq|REQ|SYNC|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", + endTime - startTime, + sendMessageRequestBody.getTopic(), + sendMessageRequestBody.getBizSeqNo(), + sendMessageRequestBody.getUniqueId(), ex); + } + + return; + } + + @Override + public boolean rejectRequest() { + return false; + } +} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/SubscribeProcessor.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/SubscribeProcessor.java new file mode 100644 index 0000000000..e22a86c7c1 --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/SubscribeProcessor.java @@ -0,0 +1,245 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.core.protocol.http.processor; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import com.alibaba.fastjson.JSONObject; +import io.netty.channel.ChannelHandlerContext; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.common.IPUtil; +import org.apache.eventmesh.common.command.HttpCommand; +import org.apache.eventmesh.common.protocol.SubscriptionItem; +import org.apache.eventmesh.common.protocol.http.body.client.SubscribeRequestBody; +import org.apache.eventmesh.common.protocol.http.body.client.SubscribeResponseBody; +import org.apache.eventmesh.common.protocol.http.common.EventMeshRetCode; +import org.apache.eventmesh.common.protocol.http.common.RequestCode; +import org.apache.eventmesh.common.protocol.http.header.client.SubscribeRequestHeader; +import org.apache.eventmesh.common.protocol.http.header.client.SubscribeResponseHeader; +import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.consumergroup.ConsumerGroupConf; +import org.apache.eventmesh.runtime.core.consumergroup.ConsumerGroupTopicConf; +import org.apache.eventmesh.runtime.core.protocol.http.async.AsyncContext; +import org.apache.eventmesh.runtime.core.protocol.http.async.CompleteHandler; +import org.apache.eventmesh.runtime.core.protocol.http.processor.inf.Client; +import org.apache.eventmesh.runtime.core.protocol.http.processor.inf.HttpRequestProcessor; +import org.apache.eventmesh.runtime.util.EventMeshUtil; +import org.apache.eventmesh.runtime.util.RemotingHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SubscribeProcessor implements HttpRequestProcessor { + + public Logger httpLogger = LoggerFactory.getLogger("http"); + + private EventMeshHTTPServer eventMeshHTTPServer; + + public SubscribeProcessor(EventMeshHTTPServer eventMeshHTTPServer) { + this.eventMeshHTTPServer = eventMeshHTTPServer; + } + + @Override + public void processRequest(ChannelHandlerContext ctx, AsyncContext asyncContext) throws Exception { + HttpCommand responseEventMeshCommand; + httpLogger.info("cmd={}|{}|client2eventMesh|from={}|to={}", RequestCode.get(Integer.valueOf(asyncContext.getRequest().getRequestCode())), + EventMeshConstants.PROTOCOL_HTTP, + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), IPUtil.getLocalAddress()); + SubscribeRequestHeader subscribeRequestHeader = (SubscribeRequestHeader) asyncContext.getRequest().getHeader(); + SubscribeRequestBody subscribeRequestBody = (SubscribeRequestBody) asyncContext.getRequest().getBody(); + + SubscribeResponseHeader subscribeResponseHeader = + SubscribeResponseHeader.buildHeader(Integer.valueOf(asyncContext.getRequest().getRequestCode()), eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshCluster, + IPUtil.getLocalAddress(), eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshEnv, + eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshIDC); + + + //validate header + if (StringUtils.isBlank(subscribeRequestHeader.getIdc()) + || StringUtils.isBlank(subscribeRequestHeader.getPid()) + || !StringUtils.isNumeric(subscribeRequestHeader.getPid()) + || StringUtils.isBlank(subscribeRequestHeader.getSys())) { + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + subscribeResponseHeader, + SubscribeResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getErrMsg())); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + + //validate body + if (StringUtils.isBlank(subscribeRequestBody.getUrl()) + || CollectionUtils.isEmpty(subscribeRequestBody.getTopics()) + || StringUtils.isBlank(subscribeRequestBody.getConsumerGroup())) { + + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + subscribeResponseHeader, + SubscribeResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getErrMsg())); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + List subTopicList = subscribeRequestBody.getTopics(); + + String url = subscribeRequestBody.getUrl(); + String consumerGroup = subscribeRequestBody.getConsumerGroup(); + + synchronized (eventMeshHTTPServer.localClientInfoMapping) { + + registerClient(subscribeRequestHeader, consumerGroup, subTopicList, url); + + for (SubscriptionItem subTopic : subTopicList) { + List groupTopicClients = eventMeshHTTPServer.localClientInfoMapping.get(consumerGroup + "@" + subTopic.getTopic()); + + if (CollectionUtils.isEmpty(groupTopicClients)) { + httpLogger.error("group {} topic {} clients is empty", consumerGroup, subTopic); + } + + Map> idcUrls = new HashMap<>(); + for (Client client : groupTopicClients) { + if (idcUrls.containsKey(client.idc)) { + idcUrls.get(client.idc).add(StringUtils.deleteWhitespace(client.url)); + } else { + List urls = new ArrayList<>(); + urls.add(client.url); + idcUrls.put(client.idc, urls); + } + } + ConsumerGroupConf consumerGroupConf = eventMeshHTTPServer.localConsumerGroupMapping.get(consumerGroup); + if (consumerGroupConf == null) { + // 新订阅 + consumerGroupConf = new ConsumerGroupConf(consumerGroup); + ConsumerGroupTopicConf consumeTopicConfig = new ConsumerGroupTopicConf(); + consumeTopicConfig.setConsumerGroup(consumerGroup); + consumeTopicConfig.setTopic(subTopic.getTopic()); + consumeTopicConfig.setSubscriptionItem(subTopic); + consumeTopicConfig.setUrls(new HashSet<>(Arrays.asList(url))); + + consumeTopicConfig.setIdcUrls(idcUrls); + + Map map = new HashMap<>(); + map.put(subTopic.getTopic(), consumeTopicConfig); + consumerGroupConf.setConsumerGroupTopicConf(map); + } else { + // 已有订阅 + Map map = consumerGroupConf.getConsumerGroupTopicConf(); + for (String key : map.keySet()) { + if (StringUtils.equals(subTopic.getTopic(), key)) { + ConsumerGroupTopicConf latestTopicConf = new ConsumerGroupTopicConf(); + ConsumerGroupTopicConf currentTopicConf = map.get(key); + latestTopicConf.setConsumerGroup(consumerGroup); + latestTopicConf.setTopic(subTopic.getTopic()); + latestTopicConf.setSubscriptionItem(subTopic); + latestTopicConf.setUrls(new HashSet<>(Arrays.asList(url))); + latestTopicConf.getUrls().addAll(currentTopicConf.getUrls()); + + latestTopicConf.setIdcUrls(idcUrls); + + map.put(key, latestTopicConf); + } + } + } + eventMeshHTTPServer.localConsumerGroupMapping.put(consumerGroup, consumerGroupConf); + } + + long startTime = System.currentTimeMillis(); + try { + // 订阅关系变化通知 + eventMeshHTTPServer.getConsumerManager().notifyConsumerManager(consumerGroup, eventMeshHTTPServer.localConsumerGroupMapping.get(consumerGroup), + eventMeshHTTPServer.localConsumerGroupMapping); + + final CompleteHandler handler = new CompleteHandler() { + @Override + public void onResponse(HttpCommand httpCommand) { + try { + if (httpLogger.isDebugEnabled()) { + httpLogger.debug("{}", httpCommand); + } + eventMeshHTTPServer.sendResponse(ctx, httpCommand.httpResponse()); + eventMeshHTTPServer.metrics.summaryMetrics.recordHTTPReqResTimeCost(System.currentTimeMillis() - asyncContext.getRequest().getReqTime()); + } catch (Exception ex) { + } + } + }; + + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + EventMeshRetCode.SUCCESS.getRetCode(), EventMeshRetCode.SUCCESS.getErrMsg()); + asyncContext.onComplete(responseEventMeshCommand, handler); + } catch (Exception e) { + HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( + subscribeResponseHeader, + SubscribeResponseBody.buildBody(EventMeshRetCode.EVENTMESH_SUBSCRIBE_ERR.getRetCode(), + EventMeshRetCode.EVENTMESH_SUBSCRIBE_ERR.getErrMsg() + EventMeshUtil.stackTrace(e, 2))); + asyncContext.onComplete(err); + long endTime = System.currentTimeMillis(); + httpLogger.error("message|eventMesh2mq|REQ|ASYNC|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", + endTime - startTime, + JSONObject.toJSONString(subscribeRequestBody.getTopics()), + subscribeRequestBody.getUrl(), e); + eventMeshHTTPServer.metrics.summaryMetrics.recordSendMsgFailed(); + eventMeshHTTPServer.metrics.summaryMetrics.recordSendMsgCost(endTime - startTime); + } + } + } + + @Override + public boolean rejectRequest() { + return false; + } + + private void registerClient(SubscribeRequestHeader subscribeRequestHeader, String consumerGroup, + List subscriptionItems, String url) { + for(SubscriptionItem item: subscriptionItems) { + Client client = new Client(); + client.env = subscribeRequestHeader.getEnv(); + client.idc = subscribeRequestHeader.getIdc(); + client.sys = subscribeRequestHeader.getSys(); + client.ip = subscribeRequestHeader.getIp(); + client.pid = subscribeRequestHeader.getPid(); + client.consumerGroup = consumerGroup; + client.topic = item.getTopic(); + client.url = url; + client.lastUpTime = new Date(); + + String groupTopicKey = client.consumerGroup + "@" + client.topic; + + if (eventMeshHTTPServer.localClientInfoMapping.containsKey(groupTopicKey)) { + List localClients = eventMeshHTTPServer.localClientInfoMapping.get(groupTopicKey); + boolean isContains = false; + for (Client localClient : localClients) { + if (StringUtils.equals(localClient.url, client.url)) { + isContains = true; + localClient.lastUpTime = client.lastUpTime; + break; + } + } + if (!isContains) { + localClients.add(client); + } + } else { + List clients = new ArrayList<>(); + clients.add(client); + eventMeshHTTPServer.localClientInfoMapping.put(groupTopicKey, clients); + } + } + } +} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/UnSubscribeProcessor.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/UnSubscribeProcessor.java new file mode 100644 index 0000000000..d45781293b --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/UnSubscribeProcessor.java @@ -0,0 +1,274 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.core.protocol.http.processor; + +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import com.alibaba.fastjson.JSONObject; +import io.netty.channel.ChannelHandlerContext; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.common.IPUtil; +import org.apache.eventmesh.common.command.HttpCommand; +import org.apache.eventmesh.common.protocol.http.body.client.UnSubscribeRequestBody; +import org.apache.eventmesh.common.protocol.http.body.client.UnSubscribeResponseBody; +import org.apache.eventmesh.common.protocol.http.common.EventMeshRetCode; +import org.apache.eventmesh.common.protocol.http.common.RequestCode; +import org.apache.eventmesh.common.protocol.http.header.client.UnSubscribeRequestHeader; +import org.apache.eventmesh.common.protocol.http.header.client.UnSubscribeResponseHeader; +import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.consumergroup.ConsumerGroupConf; +import org.apache.eventmesh.runtime.core.consumergroup.ConsumerGroupTopicConf; +import org.apache.eventmesh.runtime.core.protocol.http.async.AsyncContext; +import org.apache.eventmesh.runtime.core.protocol.http.async.CompleteHandler; +import org.apache.eventmesh.runtime.core.protocol.http.processor.inf.Client; +import org.apache.eventmesh.runtime.core.protocol.http.processor.inf.HttpRequestProcessor; +import org.apache.eventmesh.runtime.util.EventMeshUtil; +import org.apache.eventmesh.runtime.util.RemotingHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class UnSubscribeProcessor implements HttpRequestProcessor { + + public Logger httpLogger = LoggerFactory.getLogger("http"); + + private EventMeshHTTPServer eventMeshHTTPServer; + + public UnSubscribeProcessor(EventMeshHTTPServer eventMeshHTTPServer) { + this.eventMeshHTTPServer = eventMeshHTTPServer; + } + + @Override + public void processRequest(ChannelHandlerContext ctx, AsyncContext asyncContext) throws Exception { + HttpCommand responseEventMeshCommand; + httpLogger.info("cmd={}|{}|client2eventMesh|from={}|to={}", RequestCode.get(Integer.valueOf(asyncContext.getRequest().getRequestCode())), + EventMeshConstants.PROTOCOL_HTTP, + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), IPUtil.getLocalAddress()); + UnSubscribeRequestHeader unSubscribeRequestHeader = (UnSubscribeRequestHeader) asyncContext.getRequest().getHeader(); + UnSubscribeRequestBody unSubscribeRequestBody = (UnSubscribeRequestBody) asyncContext.getRequest().getBody(); + + UnSubscribeResponseHeader unSubscribeResponseHeader = + UnSubscribeResponseHeader.buildHeader(Integer.valueOf(asyncContext.getRequest().getRequestCode()), eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshCluster, + IPUtil.getLocalAddress(), eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshEnv, + eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshIDC); + + + //validate header + if (StringUtils.isBlank(unSubscribeRequestHeader.getIdc()) + || StringUtils.isBlank(unSubscribeRequestHeader.getPid()) + || !StringUtils.isNumeric(unSubscribeRequestHeader.getPid()) + || StringUtils.isBlank(unSubscribeRequestHeader.getSys())) { + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + unSubscribeResponseHeader, + UnSubscribeResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getErrMsg())); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + + //validate body + if (StringUtils.isBlank(unSubscribeRequestBody.getUrl()) + || CollectionUtils.isEmpty(unSubscribeRequestBody.getTopics()) + || StringUtils.isBlank(unSubscribeRequestBody.getConsumerGroup())) { + + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + unSubscribeResponseHeader, + UnSubscribeResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getErrMsg())); + asyncContext.onComplete(responseEventMeshCommand); + return; + } + String env = unSubscribeRequestHeader.getEnv(); + String idc = unSubscribeRequestHeader.getIdc(); + String sys = unSubscribeRequestHeader.getSys(); + String ip = unSubscribeRequestHeader.getIp(); + String pid = unSubscribeRequestHeader.getPid(); + String consumerGroup = unSubscribeRequestBody.getConsumerGroup(); + String unSubscribeUrl = unSubscribeRequestBody.getUrl(); + List unSubTopicList = unSubscribeRequestBody.getTopics(); + + final CompleteHandler handler = new CompleteHandler() { + @Override + public void onResponse(HttpCommand httpCommand) { + try { + if (httpLogger.isDebugEnabled()) { + httpLogger.debug("{}", httpCommand); + } + eventMeshHTTPServer.sendResponse(ctx, httpCommand.httpResponse()); + eventMeshHTTPServer.metrics.summaryMetrics.recordHTTPReqResTimeCost(System.currentTimeMillis() - asyncContext.getRequest().getReqTime()); + } catch (Exception ex) { + } + } + }; + + synchronized (eventMeshHTTPServer.localClientInfoMapping) { + boolean isChange = true; + + registerClient(unSubscribeRequestHeader, consumerGroup, unSubTopicList, unSubscribeUrl); + + for (String unSubTopic : unSubTopicList) { + List groupTopicClients = eventMeshHTTPServer.localClientInfoMapping.get(consumerGroup + "@" + unSubTopic); + Iterator clientIterator = groupTopicClients.iterator(); + while (clientIterator.hasNext()) { + Client client = clientIterator.next(); + if (StringUtils.equals(client.pid, pid) && StringUtils.equals(client.url, unSubscribeUrl)) { + httpLogger.warn("client {} start unsubscribe", JSONObject.toJSONString(client)); + clientIterator.remove(); + } + } + if (groupTopicClients.size() > 0) { + //change url + Map> idcUrls = new HashMap<>(); + Set clientUrls = new HashSet<>(); + for (Client client : groupTopicClients) { + // 去除订阅的url + if (!StringUtils.equals(unSubscribeUrl, client.url)) { + clientUrls.add(client.url); + if (idcUrls.containsKey(client.idc)) { + idcUrls.get(client.idc).add(StringUtils.deleteWhitespace(client.url)); + } else { + List urls = new ArrayList<>(); + urls.add(client.url); + idcUrls.put(client.idc, urls); + } + } + + } + synchronized (eventMeshHTTPServer.localConsumerGroupMapping) { + ConsumerGroupConf consumerGroupConf = eventMeshHTTPServer.localConsumerGroupMapping.get(consumerGroup); + Map map = consumerGroupConf.getConsumerGroupTopicConf(); + for (String topicKey : map.keySet()) { + // 仅修改去订阅的topic + if (StringUtils.equals(unSubTopic, topicKey)) { + ConsumerGroupTopicConf latestTopicConf = new ConsumerGroupTopicConf(); + latestTopicConf.setConsumerGroup(consumerGroup); + latestTopicConf.setTopic(unSubTopic); + latestTopicConf.setSubscriptionItem(map.get(topicKey).getSubscriptionItem()); + latestTopicConf.setUrls(clientUrls); + + latestTopicConf.setIdcUrls(idcUrls); + + map.put(unSubTopic, latestTopicConf); + } + } + eventMeshHTTPServer.localConsumerGroupMapping.put(consumerGroup, consumerGroupConf); + } + } else { + isChange = false; + break; + } + } + long startTime = System.currentTimeMillis(); + if (isChange) { + try { + eventMeshHTTPServer.getConsumerManager().notifyConsumerManager(consumerGroup, eventMeshHTTPServer.localConsumerGroupMapping.get(consumerGroup), + eventMeshHTTPServer.localConsumerGroupMapping); + + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + EventMeshRetCode.SUCCESS.getRetCode(), EventMeshRetCode.SUCCESS.getErrMsg()); + asyncContext.onComplete(responseEventMeshCommand, handler); + + } catch (Exception e) { + HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( + unSubscribeResponseHeader, + UnSubscribeResponseBody.buildBody(EventMeshRetCode.EVENTMESH_UNSUBSCRIBE_ERR.getRetCode(), + EventMeshRetCode.EVENTMESH_UNSUBSCRIBE_ERR.getErrMsg() + EventMeshUtil.stackTrace(e, 2))); + asyncContext.onComplete(err); + long endTime = System.currentTimeMillis(); + httpLogger.error("message|eventMesh2mq|REQ|ASYNC|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", + endTime - startTime, + JSONObject.toJSONString(unSubscribeRequestBody.getTopics()), + unSubscribeRequestBody.getUrl(), e); + eventMeshHTTPServer.metrics.summaryMetrics.recordSendMsgFailed(); + eventMeshHTTPServer.metrics.summaryMetrics.recordSendMsgCost(endTime - startTime); + } + } else { + //remove + try { + eventMeshHTTPServer.getConsumerManager().notifyConsumerManager(consumerGroup, null, eventMeshHTTPServer.localConsumerGroupMapping); + responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( + EventMeshRetCode.SUCCESS.getRetCode(), EventMeshRetCode.SUCCESS.getErrMsg()); + asyncContext.onComplete(responseEventMeshCommand, handler); + // 清理ClientInfo + eventMeshHTTPServer.localClientInfoMapping.keySet().removeIf(s -> StringUtils.contains(s, consumerGroup)); + // 清理ConsumerGroupInfo + eventMeshHTTPServer.localConsumerGroupMapping.keySet().removeIf(s -> StringUtils.equals(consumerGroup, s)); + } catch (Exception e) { + HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( + unSubscribeResponseHeader, + UnSubscribeResponseBody.buildBody(EventMeshRetCode.EVENTMESH_UNSUBSCRIBE_ERR.getRetCode(), + EventMeshRetCode.EVENTMESH_UNSUBSCRIBE_ERR.getErrMsg() + EventMeshUtil.stackTrace(e, 2))); + asyncContext.onComplete(err); + long endTime = System.currentTimeMillis(); + httpLogger.error("message|eventMesh2mq|REQ|ASYNC|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", + endTime - startTime, + JSONObject.toJSONString(unSubscribeRequestBody.getTopics()), + unSubscribeRequestBody.getUrl(), e); + eventMeshHTTPServer.metrics.summaryMetrics.recordSendMsgFailed(); + eventMeshHTTPServer.metrics.summaryMetrics.recordSendMsgCost(endTime - startTime); + } + } + } + } + + @Override + public boolean rejectRequest() { + return false; + } + + private void registerClient(UnSubscribeRequestHeader unSubscribeRequestHeader, String consumerGroup, + List topicList, String url) { + for(String topic: topicList) { + Client client = new Client(); + client.env = unSubscribeRequestHeader.getEnv(); + client.idc = unSubscribeRequestHeader.getIdc(); + client.sys = unSubscribeRequestHeader.getSys(); + client.ip = unSubscribeRequestHeader.getIp(); + client.pid = unSubscribeRequestHeader.getPid(); + client.consumerGroup = consumerGroup; + client.topic = topic; + client.url = url; + client.lastUpTime = new Date(); + + String groupTopicKey = client.consumerGroup + "@" + client.topic; + if (eventMeshHTTPServer.localClientInfoMapping.containsKey(groupTopicKey)) { + List localClients = eventMeshHTTPServer.localClientInfoMapping.get(groupTopicKey); + boolean isContains = false; + for (Client localClient : localClients) { + if (StringUtils.equals(localClient.url, client.url)) { + isContains = true; + localClient.lastUpTime = client.lastUpTime; + break; + } + } + if (!isContains) { + localClients.add(client); + } + } else { + List clients = new ArrayList<>(); + clients.add(client); + eventMeshHTTPServer.localClientInfoMapping.put(groupTopicKey, clients); + } + } + } +} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/inf/Client.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/inf/Client.java new file mode 100644 index 0000000000..1b834d90d7 --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/inf/Client.java @@ -0,0 +1,92 @@ +/* + * Licensed to Apache Software Foundation (ASF) under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Apache Software Foundation (ASF) licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.eventmesh.runtime.core.protocol.http.processor.inf; + +import java.util.Date; + +import com.alibaba.fastjson.JSONObject; + +import org.apache.commons.lang3.StringUtils; + +public class Client { + + public String env; + + public String idc; + + public String consumerGroup; + + public String topic; + + public String url; + + public String sys; + + public String ip; + + public String pid; + + public String hostname; + + public String apiVersion; + + public Date lastUpTime; + + public static Client buildClientFromJSONObject(JSONObject jsonObject) { + if (jsonObject == null) { + return null; + } + + Client client = null; + try { + client = new Client(); + + client.env = StringUtils.trim(jsonObject.getString("env")); + client.consumerGroup = StringUtils.trim(jsonObject.getString("groupName")); + client.topic = StringUtils.trim(jsonObject.getString("topic")); + client.url = StringUtils.trim(jsonObject.getString("url")); + client.sys = StringUtils.trim(jsonObject.getString("sys")); + client.idc = StringUtils.trim(jsonObject.getString("idc")); + client.ip = StringUtils.trim(jsonObject.getString("ip")); + client.pid = StringUtils.trim(jsonObject.getString("pid")); + client.hostname = StringUtils.trim(jsonObject.getString("hostname")); + client.apiVersion = StringUtils.trim(jsonObject.getString("apiversion")); + } catch (Exception ex) { + } + return client; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("endPoint={env=").append(env) + .append(",idc=").append(idc) + .append(",consumerGroup=").append(consumerGroup) + .append(",topic=").append(topic) + .append(",url=").append(url) + .append(",sys=").append(sys) + .append(",ip=").append(ip) + .append(",pid=").append(pid) + .append(",hostname=").append(hostname) + .append(",apiVersion=").append(apiVersion) + .append(",registerTime=").append("}"); + return sb.toString(); + } +} + diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/inf/HttpRequestProcessor.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/inf/HttpRequestProcessor.java similarity index 83% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/inf/HttpRequestProcessor.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/inf/HttpRequestProcessor.java index d7972a556d..cafddac0ff 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/processor/inf/HttpRequestProcessor.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/inf/HttpRequestProcessor.java @@ -15,12 +15,13 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.http.processor.inf; +package org.apache.eventmesh.runtime.core.protocol.http.processor.inf; -import com.webank.emesher.core.protocol.http.async.AsyncContext; -import com.webank.eventmesh.common.command.HttpCommand; import io.netty.channel.ChannelHandlerContext; +import org.apache.eventmesh.common.command.HttpCommand; +import org.apache.eventmesh.runtime.core.protocol.http.async.AsyncContext; + public interface HttpRequestProcessor { void processRequest(final ChannelHandlerContext ctx, final AsyncContext asyncContext) diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/producer/ProxyProducer.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/producer/EventMeshProducer.java similarity index 65% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/producer/ProxyProducer.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/producer/EventMeshProducer.java index ac08e47853..cf41ca2b9e 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/producer/ProxyProducer.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/producer/EventMeshProducer.java @@ -15,24 +15,23 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.http.producer; - -import com.webank.defibus.client.impl.producer.RRCallback; -import com.webank.emesher.configuration.ProxyConfiguration; -import com.webank.emesher.core.consumergroup.ProducerGroupConf; -import com.webank.emesher.core.plugin.MQProducerWrapper; -import com.webank.emesher.util.ProxyUtil; -import org.apache.rocketmq.client.exception.MQBrokerException; -import org.apache.rocketmq.client.exception.MQClientException; -import org.apache.rocketmq.client.producer.SendCallback; -import org.apache.rocketmq.common.message.Message; -import org.apache.rocketmq.remoting.exception.RemotingException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +package org.apache.eventmesh.runtime.core.protocol.http.producer; +import java.util.Properties; import java.util.concurrent.atomic.AtomicBoolean; -public class ProxyProducer { +import io.openmessaging.api.Message; +import io.openmessaging.api.SendCallback; + +import org.apache.eventmesh.api.RRCallback; +import org.apache.eventmesh.runtime.configuration.EventMeshHTTPConfiguration; +import org.apache.eventmesh.runtime.core.consumergroup.ProducerGroupConf; +import org.apache.eventmesh.runtime.core.plugin.MQProducerWrapper; +import org.apache.eventmesh.runtime.util.EventMeshUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class EventMeshProducer { protected AtomicBoolean started = new AtomicBoolean(Boolean.FALSE); @@ -50,18 +49,18 @@ public AtomicBoolean getStarted() { protected ProducerGroupConf producerGroupConfig; - protected ProxyConfiguration proxyConfiguration; + protected EventMeshHTTPConfiguration eventMeshHttpConfiguration; public void send(SendMessageContext sendMsgContext, SendCallback sendCallback) throws Exception { mqProducerWrapper.send(sendMsgContext.getMsg(), sendCallback); } public void request(SendMessageContext sendMsgContext, SendCallback sendCallback, RRCallback rrCallback, long timeout) - throws InterruptedException, RemotingException, MQClientException, MQBrokerException { + throws Exception { mqProducerWrapper.request(sendMsgContext.getMsg(), sendCallback, rrCallback, timeout); } - public Message request(SendMessageContext sendMessageContext, long timeout) throws InterruptedException, RemotingException, MQClientException, MQBrokerException { + public Message request(SendMessageContext sendMessageContext, long timeout) throws Exception { return mqProducerWrapper.request(sendMessageContext.getMsg(), timeout); } @@ -76,14 +75,20 @@ public MQProducerWrapper getMqProducerWrapper() { return mqProducerWrapper; } - public synchronized void init(ProxyConfiguration proxyConfiguration, ProducerGroupConf producerGroupConfig) { + public synchronized void init(EventMeshHTTPConfiguration eventMeshHttpConfiguration, ProducerGroupConf producerGroupConfig) throws Exception { this.producerGroupConfig = producerGroupConfig; - this.proxyConfiguration = proxyConfiguration; - mqProducerWrapper.init(proxyConfiguration, producerGroupConfig.getGroupName()); - mqProducerWrapper.getDefaultMQProducer().setInstanceName(ProxyUtil.buildProxyClientID(producerGroupConfig.getGroupName(), - proxyConfiguration.proxyRegion, proxyConfiguration.proxyCluster)); + this.eventMeshHttpConfiguration = eventMeshHttpConfiguration; + + Properties keyValue = new Properties(); + keyValue.put("producerGroup", producerGroupConfig.getGroupName()); + keyValue.put("instanceName", EventMeshUtil.buildMeshClientID(producerGroupConfig.getGroupName(), eventMeshHttpConfiguration.eventMeshCluster)); + + //TODO for defibus + keyValue.put("eventMeshIDC", eventMeshHttpConfiguration.eventMeshIDC); + + mqProducerWrapper.init(keyValue); inited.compareAndSet(false, true); - logger.info("ProxyProducer [{}] inited.............", producerGroupConfig.getGroupName()); + logger.info("EventMeshProducer [{}] inited.............", producerGroupConfig.getGroupName()); } @@ -94,7 +99,7 @@ public synchronized void start() throws Exception { mqProducerWrapper.start(); started.compareAndSet(false, true); - logger.info("ProxyProducer [{}] started.............", producerGroupConfig.getGroupName()); + logger.info("EventMeshProducer [{}] started.............", producerGroupConfig.getGroupName()); } public synchronized void shutdown() throws Exception { @@ -108,13 +113,13 @@ public synchronized void shutdown() throws Exception { mqProducerWrapper.shutdown(); inited.compareAndSet(true, false); started.compareAndSet(true, false); - logger.info("ProxyProducer [{}] shutdown.............", producerGroupConfig.getGroupName()); + logger.info("EventMeshProducer [{}] shutdown.............", producerGroupConfig.getGroupName()); } @Override public String toString() { StringBuilder sb = new StringBuilder(); - sb.append("proxyProducer={") + sb.append("eventMeshProducer={") .append("inited=").append(inited.get()).append(",") .append("started=").append(started.get()).append(",") .append("producerGroupConfig=").append(producerGroupConfig).append("}"); diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/producer/ProducerManager.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/producer/ProducerManager.java similarity index 51% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/producer/ProducerManager.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/producer/ProducerManager.java index d4356bc6df..7e66dab935 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/producer/ProducerManager.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/producer/ProducerManager.java @@ -15,25 +15,25 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.http.producer; +package org.apache.eventmesh.runtime.core.protocol.http.producer; -import com.webank.emesher.boot.ProxyHTTPServer; -import com.webank.emesher.core.consumergroup.ProducerGroupConf; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer; +import org.apache.eventmesh.runtime.core.consumergroup.ProducerGroupConf; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.concurrent.ConcurrentHashMap; - public class ProducerManager { public Logger logger = LoggerFactory.getLogger(this.getClass()); - private ProxyHTTPServer proxyHTTPServer; + private EventMeshHTTPServer eventMeshHTTPServer; - private ConcurrentHashMap producerTable = new ConcurrentHashMap(); + private ConcurrentHashMap producerTable = new ConcurrentHashMap(); - public ProducerManager(ProxyHTTPServer proxyHTTPServer) { - this.proxyHTTPServer = proxyHTTPServer; + public ProducerManager(EventMeshHTTPServer eventMeshHTTPServer) { + this.eventMeshHTTPServer = eventMeshHTTPServer; } public void init() throws Exception { @@ -44,53 +44,53 @@ public void start() throws Exception { logger.info("producerManager started......"); } - public ProxyProducer getProxyProducer(String producerGroup) throws Exception { - ProxyProducer proxyProducer = null; + public EventMeshProducer getEventMeshProducer(String producerGroup) throws Exception { + EventMeshProducer eventMeshProducer = null; if (!producerTable.containsKey(producerGroup)) { synchronized (producerTable) { if (!producerTable.containsKey(producerGroup)) { ProducerGroupConf producerGroupConfig = new ProducerGroupConf(producerGroup); - proxyProducer = createProxyProducer(producerGroupConfig); - proxyProducer.start(); + eventMeshProducer = createEventMeshProducer(producerGroupConfig); + eventMeshProducer.start(); } } } - proxyProducer = producerTable.get(producerGroup); + eventMeshProducer = producerTable.get(producerGroup); - if (!proxyProducer.getStarted().get()) { - proxyProducer.start(); + if (!eventMeshProducer.getStarted().get()) { + eventMeshProducer.start(); } - return proxyProducer; + return eventMeshProducer; } - public synchronized ProxyProducer createProxyProducer(ProducerGroupConf producerGroupConfig) throws Exception { + public synchronized EventMeshProducer createEventMeshProducer(ProducerGroupConf producerGroupConfig) throws Exception { if (producerTable.containsKey(producerGroupConfig.getGroupName())) { return producerTable.get(producerGroupConfig.getGroupName()); } - ProxyProducer proxyProducer = new ProxyProducer(); - proxyProducer.init(proxyHTTPServer.getProxyConfiguration(), producerGroupConfig); - producerTable.put(producerGroupConfig.getGroupName(), proxyProducer); - return proxyProducer; + EventMeshProducer eventMeshProducer = new EventMeshProducer(); + eventMeshProducer.init(eventMeshHTTPServer.getEventMeshHttpConfiguration(), producerGroupConfig); + producerTable.put(producerGroupConfig.getGroupName(), eventMeshProducer); + return eventMeshProducer; } public void shutdown() { - for (ProxyProducer proxyProducer : producerTable.values()) { + for (EventMeshProducer eventMeshProducer : producerTable.values()) { try { - proxyProducer.shutdown(); + eventMeshProducer.shutdown(); } catch (Exception ex) { - logger.error("shutdown proxyProducer[{}] err", proxyProducer, ex); + logger.error("shutdown eventMeshProducer[{}] err", eventMeshProducer, ex); } } logger.info("producerManager shutdown......"); } - public ProxyHTTPServer getProxyHTTPServer() { - return proxyHTTPServer; + public EventMeshHTTPServer getEventMeshHTTPServer() { + return eventMeshHTTPServer; } - public ConcurrentHashMap getProducerTable() { + public ConcurrentHashMap getProducerTable() { return producerTable; } } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/producer/SendMessageContext.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/producer/SendMessageContext.java similarity index 63% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/producer/SendMessageContext.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/producer/SendMessageContext.java index 1a542cfc6d..be368b4386 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/producer/SendMessageContext.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/producer/SendMessageContext.java @@ -15,21 +15,24 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.http.producer; +package org.apache.eventmesh.runtime.core.protocol.http.producer; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import io.openmessaging.api.Message; +import io.openmessaging.api.OnExceptionContext; +import io.openmessaging.api.SendCallback; +import io.openmessaging.api.SendResult; -import com.webank.emesher.boot.ProxyHTTPServer; -import com.webank.emesher.core.protocol.http.retry.RetryContext; -import com.webank.eventmesh.common.Constants; import org.apache.commons.lang3.time.DateFormatUtils; -import org.apache.rocketmq.client.producer.SendCallback; -import org.apache.rocketmq.client.producer.SendResult; -import org.apache.rocketmq.common.message.Message; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer; +import org.apache.eventmesh.runtime.core.protocol.http.retry.RetryContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.HashMap; -import java.util.Map; - public class SendMessageContext extends RetryContext { public static Logger logger = LoggerFactory.getLogger("retry"); @@ -38,19 +41,21 @@ public class SendMessageContext extends RetryContext { private String bizSeqNo; - private ProxyProducer proxyProducer; + private EventMeshProducer eventMeshProducer; private long createTime = System.currentTimeMillis(); private Map props; - public ProxyHTTPServer proxyHTTPServer; + public EventMeshHTTPServer eventMeshHTTPServer; + + private List messageList; - public SendMessageContext(String bizSeqNo, Message msg, ProxyProducer proxyProducer, ProxyHTTPServer proxyHTTPServer) { + public SendMessageContext(String bizSeqNo, Message msg, EventMeshProducer eventMeshProducer, EventMeshHTTPServer eventMeshHTTPServer) { this.bizSeqNo = bizSeqNo; this.msg = msg; - this.proxyProducer = proxyProducer; - this.proxyHTTPServer = proxyHTTPServer; + this.eventMeshProducer = eventMeshProducer; + this.eventMeshHTTPServer = eventMeshHTTPServer; } public void addProp(String key, String val) { @@ -80,12 +85,12 @@ public void setMsg(Message msg) { this.msg = msg; } - public ProxyProducer getProxyProducer() { - return proxyProducer; + public EventMeshProducer getEventMeshProducer() { + return eventMeshProducer; } - public void setProxyProducer(ProxyProducer proxyProducer) { - this.proxyProducer = proxyProducer; + public void setEventMeshProducer(EventMeshProducer eventMeshProducer) { + this.eventMeshProducer = eventMeshProducer; } public long getCreateTime() { @@ -96,13 +101,21 @@ public void setCreateTime(long createTime) { this.createTime = createTime; } + public List getMessageList() { + return messageList; + } + + public void setMessageList(List messageList) { + this.messageList = messageList; + } + @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("sendMessageContext={") .append("bizSeqNo=").append(bizSeqNo) .append(",retryTimes=").append(retryTimes) - .append(",producer=").append(proxyProducer != null ? proxyProducer.producerGroupConfig.getGroupName() : null) + .append(",producer=").append(eventMeshProducer != null ? eventMeshProducer.producerGroupConfig.getGroupName() : null) .append(",executeTime=").append(DateFormatUtils.format(executeTime, Constants.DATE_FORMAT)) .append(",createTime=").append(DateFormatUtils.format(createTime, Constants.DATE_FORMAT)).append("}"); return sb.toString(); @@ -110,7 +123,7 @@ public String toString() { @Override public boolean retry() throws Exception { - if (proxyProducer == null) { + if (eventMeshProducer == null) { return false; } @@ -119,17 +132,18 @@ public boolean retry() throws Exception { } retryTimes++; - proxyProducer.send(this, new SendCallback() { + eventMeshProducer.send(this, new SendCallback() { + @Override public void onSuccess(SendResult sendResult) { - } @Override - public void onException(Throwable e) { - logger.warn("", e); - proxyHTTPServer.metrics.summaryMetrics.recordSendBatchMsgFailed(1); + public void onException(OnExceptionContext context) { + logger.warn("", context.getException()); + eventMeshHTTPServer.metrics.summaryMetrics.recordSendBatchMsgFailed(1); } + }); return true; diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/push/AbstractHTTPPushRequest.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/push/AbstractHTTPPushRequest.java similarity index 74% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/push/AbstractHTTPPushRequest.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/push/AbstractHTTPPushRequest.java index b490e55287..e7294b837c 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/push/AbstractHTTPPushRequest.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/push/AbstractHTTPPushRequest.java @@ -15,27 +15,28 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.http.push; - -import com.webank.emesher.boot.ProxyHTTPServer; -import com.webank.emesher.configuration.ProxyConfiguration; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.protocol.http.consumer.HandleMsgContext; -import com.webank.emesher.core.protocol.http.retry.HttpRetryer; -import com.webank.emesher.core.protocol.http.retry.RetryContext; -import com.google.common.collect.Lists; -import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.collections4.MapUtils; -import org.apache.commons.lang3.RandomUtils; +package org.apache.eventmesh.runtime.core.protocol.http.push; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; +import com.google.common.collect.Lists; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.collections4.MapUtils; +import org.apache.commons.lang3.RandomUtils; +import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer; +import org.apache.eventmesh.runtime.configuration.EventMeshHTTPConfiguration; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.http.consumer.HandleMsgContext; +import org.apache.eventmesh.runtime.core.protocol.http.retry.HttpRetryer; +import org.apache.eventmesh.runtime.core.protocol.http.retry.RetryContext; + public abstract class AbstractHTTPPushRequest extends RetryContext { - public ProxyHTTPServer proxyHTTPServer; + public EventMeshHTTPServer eventMeshHTTPServer; public long createTime = System.currentTimeMillis(); @@ -47,7 +48,7 @@ public abstract class AbstractHTTPPushRequest extends RetryContext { public volatile int startIdx; - public ProxyConfiguration proxyConfiguration; + public EventMeshHTTPConfiguration eventMeshHttpConfiguration; public HttpRetryer retryer; @@ -60,12 +61,12 @@ public abstract class AbstractHTTPPushRequest extends RetryContext { private AtomicBoolean complete = new AtomicBoolean(Boolean.FALSE); public AbstractHTTPPushRequest(HandleMsgContext handleMsgContext) { - this.proxyHTTPServer = handleMsgContext.getProxyHTTPServer(); + this.eventMeshHTTPServer = handleMsgContext.getEventMeshHTTPServer(); this.handleMsgContext = handleMsgContext; this.urls = handleMsgContext.getConsumeTopicConfig().getIdcUrls(); this.totalUrls = Lists.newArrayList(handleMsgContext.getConsumeTopicConfig().getUrls()); - this.proxyConfiguration = handleMsgContext.getProxyHTTPServer().getProxyConfiguration(); - this.retryer = handleMsgContext.getProxyHTTPServer().getHttpRetryer(); + this.eventMeshHttpConfiguration = handleMsgContext.getEventMeshHTTPServer().getEventMeshHttpConfiguration(); + this.retryer = handleMsgContext.getEventMeshHTTPServer().getHttpRetryer(); this.ttl = handleMsgContext.getTtl(); this.startIdx = RandomUtils.nextInt(0, totalUrls.size()); } @@ -74,9 +75,9 @@ public void tryHTTPRequest() { } public void delayRetry() { - if (retryTimes < ProxyConstants.DEFAULT_PUSH_RETRY_TIMES) { + if (retryTimes < EventMeshConstants.DEFAULT_PUSH_RETRY_TIMES) { retryTimes++; - delay(retryTimes * ProxyConstants.DEFAULT_PUSH_RETRY_TIME_DISTANCE_IN_MILLSECONDS); + delay(retryTimes * EventMeshConstants.DEFAULT_PUSH_RETRY_TIME_DISTANCE_IN_MILLSECONDS); retryer.pushRetry(this); } else { complete.compareAndSet(Boolean.FALSE, Boolean.TRUE); @@ -85,7 +86,7 @@ public void delayRetry() { public String getUrl() { List localIDCUrl = MapUtils.getObject(urls, - proxyConfiguration.proxyIDC, null); + eventMeshHttpConfiguration.eventMeshIDC, null); if (CollectionUtils.isNotEmpty(localIDCUrl)) { return localIDCUrl.get((startIdx + retryTimes) % localIDCUrl.size()); } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/push/AsyncHTTPPushRequest.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/push/AsyncHTTPPushRequest.java similarity index 76% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/push/AsyncHTTPPushRequest.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/push/AsyncHTTPPushRequest.java index 5f358fb759..3ab5649b89 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/push/AsyncHTTPPushRequest.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/push/AsyncHTTPPushRequest.java @@ -15,25 +15,36 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.http.push; - -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.protocol.http.consumer.HandleMsgContext; -import com.webank.eventmesh.common.Constants; -import com.webank.eventmesh.common.IPUtil; -import com.webank.eventmesh.common.protocol.http.body.message.PushMessageRequestBody; -import com.webank.eventmesh.common.protocol.http.common.ClientRetCode; -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import com.webank.eventmesh.common.protocol.http.common.ProtocolVersion; -import com.webank.eventmesh.common.protocol.http.common.RequestCode; -import com.webank.emesher.util.ProxyUtil; +package org.apache.eventmesh.runtime.core.protocol.http.push; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; + import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONException; import com.alibaba.fastjson.JSONObject; import com.google.common.collect.Sets; -import org.apache.commons.lang3.RandomStringUtils; + import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.DateFormatUtils; +import org.apache.commons.text.RandomStringGenerator; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.IPUtil; +import org.apache.eventmesh.common.protocol.SubcriptionType; +import org.apache.eventmesh.common.protocol.http.body.message.PushMessageRequestBody; +import org.apache.eventmesh.common.protocol.http.common.ClientRetCode; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; +import org.apache.eventmesh.common.protocol.http.common.ProtocolVersion; +import org.apache.eventmesh.common.protocol.http.common.RequestCode; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.http.consumer.HandleMsgContext; +import org.apache.eventmesh.runtime.util.EventMeshUtil; +import org.apache.eventmesh.runtime.util.OMSUtil; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; @@ -45,14 +56,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.nio.charset.Charset; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Set; - public class AsyncHTTPPushRequest extends AbstractHTTPPushRequest { public Logger messageLogger = LoggerFactory.getLogger("message"); @@ -65,11 +68,15 @@ public class AsyncHTTPPushRequest extends AbstractHTTPPushRequest { public String currPushUrl; + private final static RandomStringGenerator randomGenerator = new RandomStringGenerator.Builder() + .withinRange('0', '9').build(); + public AsyncHTTPPushRequest(HandleMsgContext handleMsgContext, Map> waitingRequests) { super(handleMsgContext); this.waitingRequests = waitingRequests; } + @Override public void tryHTTPRequest() { currPushUrl = getUrl(); @@ -82,7 +89,7 @@ public void tryHTTPRequest() { String requestCode = ""; - if (ProxyUtil.isService(handleMsgContext.getTopic())) { + if (SubcriptionType.SYNC.equals(handleMsgContext.getSubscriptionItem().getType())) { requestCode = String.valueOf(RequestCode.HTTP_PUSH_CLIENT_SYNC.getRequestCode()); } else { requestCode = String.valueOf(RequestCode.HTTP_PUSH_CLIENT_ASYNC.getRequestCode()); @@ -91,18 +98,16 @@ public void tryHTTPRequest() { builder.addHeader(ProtocolKey.REQUEST_CODE, requestCode); builder.addHeader(ProtocolKey.LANGUAGE, Constants.LANGUAGE_JAVA); builder.addHeader(ProtocolKey.VERSION, ProtocolVersion.V1.getVersion()); - builder.addHeader(ProtocolKey.ProxyInstanceKey.PROXYCLUSTER, handleMsgContext.getProxyHTTPServer().getProxyConfiguration().proxyCluster); - builder.addHeader(ProtocolKey.ProxyInstanceKey.PROXYIP, IPUtil.getLocalAddress()); - builder.addHeader(ProtocolKey.ProxyInstanceKey.PROXYDCN, handleMsgContext.getProxyHTTPServer().getProxyConfiguration().proxyDCN); - builder.addHeader(ProtocolKey.ProxyInstanceKey.PROXYENV, handleMsgContext.getProxyHTTPServer().getProxyConfiguration().proxyEnv); - builder.addHeader(ProtocolKey.ProxyInstanceKey.PROXYREGION, handleMsgContext.getProxyHTTPServer().getProxyConfiguration().proxyRegion); - builder.addHeader(ProtocolKey.ProxyInstanceKey.PROXYIDC, handleMsgContext.getProxyHTTPServer().getProxyConfiguration().proxyIDC); + builder.addHeader(ProtocolKey.EventMeshInstanceKey.EVENTMESHCLUSTER, handleMsgContext.getEventMeshHTTPServer().getEventMeshHttpConfiguration().eventMeshCluster); + builder.addHeader(ProtocolKey.EventMeshInstanceKey.EVENTMESHIP, IPUtil.getLocalAddress()); + builder.addHeader(ProtocolKey.EventMeshInstanceKey.EVENTMESHENV, handleMsgContext.getEventMeshHTTPServer().getEventMeshHttpConfiguration().eventMeshEnv); + builder.addHeader(ProtocolKey.EventMeshInstanceKey.EVENTMESHIDC, handleMsgContext.getEventMeshHTTPServer().getEventMeshHttpConfiguration().eventMeshIDC); - handleMsgContext.getMsg().putUserProperty(ProxyConstants.REQ_PROXY2C_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + handleMsgContext.getMsg().getUserProperties().put(EventMeshConstants.REQ_EVENTMESH2C_TIMESTAMP, String.valueOf(System.currentTimeMillis())); String content = ""; try { - content = new String(handleMsgContext.getMsg().getBody(), ProxyConstants.DEFAULT_CHARSET); + content = new String(handleMsgContext.getMsg().getBody(), EventMeshConstants.DEFAULT_CHARSET); } catch (Exception ex) { return; } @@ -110,12 +115,12 @@ public void tryHTTPRequest() { List body = new ArrayList(); body.add(new BasicNameValuePair(PushMessageRequestBody.CONTENT, content)); if (StringUtils.isBlank(handleMsgContext.getBizSeqNo())) { - body.add(new BasicNameValuePair(PushMessageRequestBody.BIZSEQNO, RandomStringUtils.randomNumeric(20))); + body.add(new BasicNameValuePair(PushMessageRequestBody.BIZSEQNO, randomGenerator.generate(20))); } else { body.add(new BasicNameValuePair(PushMessageRequestBody.BIZSEQNO, handleMsgContext.getBizSeqNo())); } if (StringUtils.isBlank(handleMsgContext.getUniqueId())) { - body.add(new BasicNameValuePair(PushMessageRequestBody.UNIQUEID, RandomStringUtils.randomNumeric(20))); + body.add(new BasicNameValuePair(PushMessageRequestBody.UNIQUEID, randomGenerator.generate(20))); } else { body.add(new BasicNameValuePair(PushMessageRequestBody.UNIQUEID, handleMsgContext.getUniqueId())); } @@ -123,7 +128,7 @@ public void tryHTTPRequest() { body.add(new BasicNameValuePair(PushMessageRequestBody.RANDOMNO, handleMsgContext.getMsgRandomNo())); body.add(new BasicNameValuePair(PushMessageRequestBody.TOPIC, handleMsgContext.getTopic())); - body.add(new BasicNameValuePair(PushMessageRequestBody.EXTFIELDS, JSON.toJSONString(handleMsgContext.getMsg().getProperties()))); + body.add(new BasicNameValuePair(PushMessageRequestBody.EXTFIELDS, JSON.toJSONString(OMSUtil.getMessageProp(handleMsgContext.getMsg())))); try { builder.setEntity(new UrlEncodedFormEntity(body)); @@ -131,13 +136,13 @@ public void tryHTTPRequest() { return; } - proxyHTTPServer.metrics.summaryMetrics.recordPushMsg(); + eventMeshHTTPServer.metrics.summaryMetrics.recordPushMsg(); this.lastPushTime = System.currentTimeMillis(); addToWaitingMap(this); - cmdLogger.info("cmd={}|proxy2client|from={}|to={}", requestCode, + cmdLogger.info("cmd={}|eventMesh2client|from={}|to={}", requestCode, IPUtil.getLocalAddress(), currPushUrl); try { @@ -146,10 +151,10 @@ public void tryHTTPRequest() { public Object handleResponse(HttpResponse response) { removeWaitingMap(AsyncHTTPPushRequest.this); long cost = System.currentTimeMillis() - lastPushTime; - proxyHTTPServer.metrics.summaryMetrics.recordHTTPPushTimeCost(cost); + eventMeshHTTPServer.metrics.summaryMetrics.recordHTTPPushTimeCost(cost); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { - proxyHTTPServer.metrics.summaryMetrics.recordHttpPushMsgFailed(); - messageLogger.info("message|proxy2client|exception|url={}|topic={}|bizSeqNo={}|uniqueId={}|cost={}", currPushUrl, handleMsgContext.getTopic(), + eventMeshHTTPServer.metrics.summaryMetrics.recordHttpPushMsgFailed(); + messageLogger.info("message|eventMesh2client|exception|url={}|topic={}|bizSeqNo={}|uniqueId={}|cost={}", currPushUrl, handleMsgContext.getTopic(), handleMsgContext.getBizSeqNo(), handleMsgContext.getUniqueId(), cost); delayRetry(); @@ -159,13 +164,13 @@ public Object handleResponse(HttpResponse response) { } else { String res = ""; try { - res = EntityUtils.toString(response.getEntity(), Charset.forName(ProxyConstants.DEFAULT_CHARSET)); + res = EntityUtils.toString(response.getEntity(), Charset.forName(EventMeshConstants.DEFAULT_CHARSET)); } catch (IOException e) { handleMsgContext.finish(); return new Object(); } ClientRetCode result = processResponseContent(res); - messageLogger.info("message|proxy2client|{}|url={}|topic={}|bizSeqNo={}|uniqueId={}|cost={}", result, currPushUrl, handleMsgContext.getTopic(), + messageLogger.info("message|eventMesh2client|{}|url={}|topic={}|bizSeqNo={}|uniqueId={}|cost={}", result, currPushUrl, handleMsgContext.getTopic(), handleMsgContext.getBizSeqNo(), handleMsgContext.getUniqueId(), cost); if (result == ClientRetCode.OK) { complete(); @@ -194,10 +199,10 @@ public Object handleResponse(HttpResponse response) { }); if (messageLogger.isDebugEnabled()) { - messageLogger.debug("message|proxy2client|url={}|topic={}|msg={}", currPushUrl, handleMsgContext.getTopic(), + messageLogger.debug("message|eventMesh2client|url={}|topic={}|msg={}", currPushUrl, handleMsgContext.getTopic(), handleMsgContext.getMsg()); } else { - messageLogger.info("message|proxy2client|url={}|topic={}|bizSeqNo={}|uniqueId={}", currPushUrl, handleMsgContext.getTopic(), + messageLogger.info("message|eventMesh2client|url={}|topic={}|bizSeqNo={}|uniqueId={}", currPushUrl, handleMsgContext.getTopic(), handleMsgContext.getBizSeqNo(), handleMsgContext.getUniqueId()); } } catch (IOException e) { diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/push/HTTPClientPool.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/push/HTTPClientPool.java similarity index 96% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/push/HTTPClientPool.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/push/HTTPClientPool.java index 674e0955ef..c2480f92da 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/push/HTTPClientPool.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/push/HTTPClientPool.java @@ -15,17 +15,18 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.http.push; +package org.apache.eventmesh.runtime.core.protocol.http.push; + +import java.util.Iterator; +import java.util.List; import com.google.common.collect.Lists; + import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.RandomUtils; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; -import java.util.Iterator; -import java.util.List; - public class HTTPClientPool { private List clients = Lists.newArrayList(); diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/push/HTTPMessageHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/push/HTTPMessageHandler.java similarity index 78% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/push/HTTPMessageHandler.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/push/HTTPMessageHandler.java index 7c6025a88d..f0e0ee87f7 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/push/HTTPMessageHandler.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/push/HTTPMessageHandler.java @@ -15,16 +15,7 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.http.push; - -import com.webank.emesher.core.protocol.http.consumer.HandleMsgContext; -import com.webank.emesher.core.protocol.http.consumer.ProxyConsumer; -import com.webank.eventmesh.common.ThreadPoolFactory; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; -import org.apache.commons.collections4.MapUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +package org.apache.eventmesh.runtime.core.protocol.http.push; import java.util.Map; import java.util.Set; @@ -33,13 +24,23 @@ import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; + +import org.apache.commons.collections4.MapUtils; +import org.apache.eventmesh.common.ThreadPoolFactory; +import org.apache.eventmesh.runtime.core.protocol.http.consumer.EventMeshConsumer; +import org.apache.eventmesh.runtime.core.protocol.http.consumer.HandleMsgContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class HTTPMessageHandler implements MessageHandler { public Logger logger = LoggerFactory.getLogger(this.getClass()); - private ProxyConsumer proxyConsumer; + private EventMeshConsumer eventMeshConsumer; - private static final ScheduledExecutorService SCHEDULER = ThreadPoolFactory.createSingleScheduledExecutor("proxy-pushMsgTimeout-"); + private static final ScheduledExecutorService SCHEDULER = ThreadPoolFactory.createSingleScheduledExecutor("eventMesh-pushMsgTimeout-"); private ThreadPoolExecutor pushExecutor; @@ -56,10 +57,10 @@ private void checkTimeout() { public static Map> waitingRequests = Maps.newConcurrentMap(); - public HTTPMessageHandler(ProxyConsumer proxyConsumer) { - this.proxyConsumer = proxyConsumer; - this.pushExecutor = proxyConsumer.getProxyHTTPServer().pushMsgExecutor; - waitingRequests.put(this.proxyConsumer.getConsumerGroupConf().getConsumerGroup(), Sets.newConcurrentHashSet()); + public HTTPMessageHandler(EventMeshConsumer eventMeshConsumer) { + this.eventMeshConsumer = eventMeshConsumer; + this.pushExecutor = eventMeshConsumer.getEventMeshHTTPServer().pushMsgExecutor; + waitingRequests.put(this.eventMeshConsumer.getConsumerGroupConf().getConsumerGroup(), Sets.newConcurrentHashSet()); SCHEDULER.scheduleAtFixedRate(this::checkTimeout, 0, 1000, TimeUnit.MILLISECONDS); } @@ -80,7 +81,7 @@ public boolean handle(final HandleMsgContext handleMsgContext) { }); return true; } catch (RejectedExecutionException e) { - logger.warn("pushMsgThreadPoolQueue is full, so reject, current task size {}", handleMsgContext.getProxyHTTPServer().getPushMsgExecutor().getQueue().size(), e); + logger.warn("pushMsgThreadPoolQueue is full, so reject, current task size {}", handleMsgContext.getEventMeshHTTPServer().getPushMsgExecutor().getQueue().size(), e); return false; } } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/push/MessageHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/push/MessageHandler.java similarity index 86% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/push/MessageHandler.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/push/MessageHandler.java index 0d52466b80..403ed8e3f5 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/push/MessageHandler.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/push/MessageHandler.java @@ -15,10 +15,10 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.http.push; +package org.apache.eventmesh.runtime.core.protocol.http.push; -import com.webank.emesher.core.protocol.http.consumer.HandleMsgContext; +import org.apache.eventmesh.runtime.core.protocol.http.consumer.HandleMsgContext; public interface MessageHandler { boolean handle(HandleMsgContext handleMsgContext); -} +} \ No newline at end of file diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/retry/DelayRetryable.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/retry/DelayRetryable.java similarity index 93% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/retry/DelayRetryable.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/retry/DelayRetryable.java index db792fd429..194322576a 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/retry/DelayRetryable.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/retry/DelayRetryable.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.http.retry; +package org.apache.eventmesh.runtime.core.protocol.http.retry; import java.util.concurrent.Delayed; diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/retry/HttpRetryer.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/retry/HttpRetryer.java similarity index 82% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/retry/HttpRetryer.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/retry/HttpRetryer.java index f44b0fdfd4..6147f7b3d6 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/retry/HttpRetryer.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/retry/HttpRetryer.java @@ -15,11 +15,7 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.http.retry; - -import com.webank.emesher.boot.ProxyHTTPServer; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +package org.apache.eventmesh.runtime.core.protocol.http.retry; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.DelayQueue; @@ -28,16 +24,20 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; +import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class HttpRetryer { private Logger retryLogger = LoggerFactory.getLogger("retry"); private Logger logger = LoggerFactory.getLogger(this.getClass()); - private ProxyHTTPServer proxyHTTPServer; + private EventMeshHTTPServer eventMeshHTTPServer; - public HttpRetryer(ProxyHTTPServer proxyHTTPServer) { - this.proxyHTTPServer = proxyHTTPServer; + public HttpRetryer(EventMeshHTTPServer eventMeshHTTPServer) { + this.eventMeshHTTPServer = eventMeshHTTPServer; } private DelayQueue failed = new DelayQueue(); @@ -47,7 +47,7 @@ public HttpRetryer(ProxyHTTPServer proxyHTTPServer) { private Thread dispatcher; public void pushRetry(DelayRetryable delayRetryable) { - if (failed.size() >= proxyHTTPServer.getProxyConfiguration().proxyServerRetryBlockQSize) { + if (failed.size() >= eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshServerRetryBlockQSize) { retryLogger.error("[RETRY-QUEUE] is full!"); return; } @@ -55,10 +55,10 @@ public void pushRetry(DelayRetryable delayRetryable) { } public void init() { - pool = new ThreadPoolExecutor(proxyHTTPServer.getProxyConfiguration().proxyServerRetryThreadNum, - proxyHTTPServer.getProxyConfiguration().proxyServerRetryThreadNum, + pool = new ThreadPoolExecutor(eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshServerRetryThreadNum, + eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshServerRetryThreadNum, 60000, - TimeUnit.MILLISECONDS, new ArrayBlockingQueue(proxyHTTPServer.getProxyConfiguration().proxyServerRetryBlockQSize), + TimeUnit.MILLISECONDS, new ArrayBlockingQueue(eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshServerRetryBlockQSize), new ThreadFactory() { private AtomicInteger ai = new AtomicInteger(); @@ -81,7 +81,7 @@ public void run() { pool.execute(() -> { try { delayRetryable.retry(); - if(retryLogger.isDebugEnabled()) { + if (retryLogger.isDebugEnabled()) { retryLogger.debug("retryObj : {}", delayRetryable); } } catch (Exception e) { diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/retry/RetryContext.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/retry/RetryContext.java similarity index 96% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/retry/RetryContext.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/retry/RetryContext.java index 93cb63b250..7841cca1e4 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/http/retry/RetryContext.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/retry/RetryContext.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.http.retry; +package org.apache.eventmesh.runtime.core.protocol.http.retry; import java.util.concurrent.Delayed; import java.util.concurrent.TimeUnit; diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/ProxyTcp2Client.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/EventMeshTcp2Client.java similarity index 63% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/ProxyTcp2Client.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/EventMeshTcp2Client.java index b7143ff1ec..17868c6e97 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/ProxyTcp2Client.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/EventMeshTcp2Client.java @@ -15,44 +15,45 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client; - -import com.webank.emesher.boot.ProxyTCPServer; -import com.webank.emesher.core.protocol.tcp.client.group.ClientSessionGroupMapping; -import com.webank.emesher.core.protocol.tcp.client.session.Session; -import com.webank.emesher.core.protocol.tcp.client.session.SessionState; -import com.webank.eventmesh.common.protocol.tcp.Header; -import com.webank.eventmesh.common.protocol.tcp.OPStatus; -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.eventmesh.common.protocol.tcp.RedirectInfo; -import com.webank.emesher.metrics.tcp.ProxyTcpMonitor; -import com.webank.emesher.util.Utils; +package org.apache.eventmesh.runtime.core.protocol.tcp.client; + +import static org.apache.eventmesh.common.protocol.tcp.Command.REDIRECT_TO_CLIENT; +import static org.apache.eventmesh.common.protocol.tcp.Command.SERVER_GOODBYE_REQUEST; + +import java.net.InetSocketAddress; +import java.util.concurrent.TimeUnit; + import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; -import org.apache.rocketmq.remoting.common.RemotingHelper; + +import org.apache.eventmesh.common.protocol.tcp.Header; +import org.apache.eventmesh.common.protocol.tcp.OPStatus; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.common.protocol.tcp.RedirectInfo; +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.group.ClientSessionGroupMapping; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.SessionState; +import org.apache.eventmesh.runtime.metrics.tcp.EventMeshTcpMonitor; +import org.apache.eventmesh.runtime.util.RemotingHelper; +import org.apache.eventmesh.runtime.util.Utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.net.InetSocketAddress; -import java.util.concurrent.TimeUnit; - -import static com.webank.eventmesh.common.protocol.tcp.Command.REDIRECT_TO_CLIENT; -import static com.webank.eventmesh.common.protocol.tcp.Command.SERVER_GOODBYE_REQUEST; +public class EventMeshTcp2Client { -public class ProxyTcp2Client { + private static final Logger logger = LoggerFactory.getLogger(EventMeshTcp2Client.class); - private static final Logger logger = LoggerFactory.getLogger(ProxyTcp2Client.class); - - public static InetSocketAddress serverGoodby2Client(Session session, ClientSessionGroupMapping mapping) { + public static InetSocketAddress serverGoodby2Client(EventMeshTCPServer eventMeshTCPServer,Session session, ClientSessionGroupMapping mapping) { logger.info("serverGoodby2Client client[{}]", session.getClient()); - try{ + try { long startTime = System.currentTimeMillis(); Package msg = new Package(); - msg.setHeader(new Header(SERVER_GOODBYE_REQUEST, OPStatus.SUCCESS.getCode(), "graceful normal quit from access", + msg.setHeader(new Header(SERVER_GOODBYE_REQUEST, OPStatus.SUCCESS.getCode(), "graceful normal quit from eventmesh", null)); - ProxyTCPServer.scheduler.submit(new Runnable() { + eventMeshTCPServer.getScheduler().submit(new Runnable() { @Override public void run() { long taskExecuteTime = System.currentTimeMillis(); @@ -61,23 +62,24 @@ public void run() { }); InetSocketAddress address = (InetSocketAddress) session.getContext().channel().remoteAddress(); - closeSessionIfTimeout(session, mapping); + closeSessionIfTimeout(eventMeshTCPServer,session, mapping); return address; - }catch (Exception e){ + } catch (Exception e) { logger.error("exception occur while serverGoodby2Client", e); return null; } } - public static InetSocketAddress goodBye2Client(Session session, + public static InetSocketAddress goodBye2Client(EventMeshTCPServer eventMeshTCPServer, + Session session, String errMsg, - int proxyStatus, + int eventMeshStatus, ClientSessionGroupMapping mapping) { try { long startTime = System.currentTimeMillis(); Package msg = new Package(); - msg.setHeader(new Header(SERVER_GOODBYE_REQUEST, proxyStatus, errMsg,null)); - ProxyTCPServer.scheduler.schedule(new Runnable() { + msg.setHeader(new Header(SERVER_GOODBYE_REQUEST, eventMeshStatus, errMsg, null)); + eventMeshTCPServer.getScheduler().schedule(new Runnable() { @Override public void run() { long taskExecuteTime = System.currentTimeMillis(); @@ -85,7 +87,7 @@ public void run() { } }, 1 * 1000, TimeUnit.MILLISECONDS); - closeSessionIfTimeout(session, mapping); + closeSessionIfTimeout(eventMeshTCPServer,session, mapping); return session.getRemoteAddress(); } catch (Exception e) { @@ -97,11 +99,11 @@ public void run() { public static void goodBye2Client(ChannelHandlerContext ctx, String errMsg, ClientSessionGroupMapping mapping, - ProxyTcpMonitor proxyTcpMonitor) { + EventMeshTcpMonitor eventMeshTcpMonitor) { long startTime = System.currentTimeMillis(); Package pkg = new Package(new Header(SERVER_GOODBYE_REQUEST, OPStatus.FAIL.getCode(), errMsg, null)); - proxyTcpMonitor.getProxy2clientMsgNum().incrementAndGet(); + eventMeshTcpMonitor.getEventMesh2clientMsgNum().incrementAndGet(); logger.info("goodBye2Client client[{}]", RemotingHelper.parseChannelRemoteAddr(ctx.channel())); ctx.writeAndFlush(pkg).addListener( new ChannelFutureListener() { @@ -118,36 +120,36 @@ public void operationComplete(ChannelFuture future) throws Exception { ); } - public static String redirectClient2NewProxy(String newProxyIp, int port, Session session, ClientSessionGroupMapping mapping) { - logger.info("begin to gracefully redirect Client {}, newIPPort[{}]", session.getClient(), newProxyIp + ":" + port); + public static String redirectClient2NewEventMesh(EventMeshTCPServer eventMeshTCPServer,String newEventMeshIp, int port, Session session, ClientSessionGroupMapping mapping) { + logger.info("begin to gracefully redirect Client {}, newIPPort[{}]", session.getClient(), newEventMeshIp + ":" + port); try { long startTime = System.currentTimeMillis(); Package pkg = new Package(); pkg.setHeader(new Header(REDIRECT_TO_CLIENT, OPStatus.SUCCESS.getCode(), null, null)); - pkg.setBody(new RedirectInfo(newProxyIp, port)); - ProxyTCPServer.scheduler.schedule(new Runnable() { + pkg.setBody(new RedirectInfo(newEventMeshIp, port)); + eventMeshTCPServer.getScheduler().schedule(new Runnable() { @Override public void run() { long taskExecuteTime = System.currentTimeMillis(); Utils.writeAndFlush(pkg, startTime, taskExecuteTime, session.getContext(), session); } }, 5 * 1000, TimeUnit.MILLISECONDS); - closeSessionIfTimeout(session, mapping); - return session.getRemoteAddress() + "--->" + newProxyIp + ":" + port; + closeSessionIfTimeout(eventMeshTCPServer,session, mapping); + return session.getRemoteAddress() + "--->" + newEventMeshIp + ":" + port; } catch (Exception e) { - logger.error("exception occur while redirectClient2NewProxy", e); + logger.error("exception occur while redirectClient2NewEventMesh", e); return null; } } - public static void closeSessionIfTimeout(Session session, ClientSessionGroupMapping mapping) { - ProxyTCPServer.scheduler.schedule(new Runnable() { + public static void closeSessionIfTimeout(EventMeshTCPServer eventMeshTCPServer,Session session, ClientSessionGroupMapping mapping) { + eventMeshTCPServer.getScheduler().schedule(new Runnable() { @Override public void run() { try { - if(!session.getSessionState().equals(SessionState.CLOSED)){ + if (!session.getSessionState().equals(SessionState.CLOSED)) { mapping.closeSession(session.getContext()); logger.info("closeSessionIfTimeout success, session[{}]", session.getClient()); } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/ProxyTcpConnectionHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/EventMeshTcpConnectionHandler.java similarity index 81% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/ProxyTcpConnectionHandler.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/EventMeshTcpConnectionHandler.java index 529f1c3499..f091ac41aa 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/ProxyTcpConnectionHandler.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/EventMeshTcpConnectionHandler.java @@ -15,27 +15,28 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client; +package org.apache.eventmesh.runtime.core.protocol.tcp.client; + +import java.util.concurrent.atomic.AtomicInteger; -import com.webank.emesher.boot.ProxyTCPServer; import io.netty.channel.ChannelDuplexHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.timeout.IdleState; import io.netty.handler.timeout.IdleStateEvent; -import org.apache.rocketmq.remoting.common.RemotingHelper; + +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.apache.eventmesh.runtime.util.RemotingHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.concurrent.atomic.AtomicInteger; - -public class ProxyTcpConnectionHandler extends ChannelDuplexHandler { +public class EventMeshTcpConnectionHandler extends ChannelDuplexHandler { public static AtomicInteger connections = new AtomicInteger(0); private final Logger logger = LoggerFactory.getLogger(this.getClass()); - private ProxyTCPServer proxyTCPServer; + private EventMeshTCPServer eventMeshTCPServer; - public ProxyTcpConnectionHandler(ProxyTCPServer proxyTCPServer) { - this.proxyTCPServer = proxyTCPServer; + public EventMeshTcpConnectionHandler(EventMeshTCPServer eventMeshTCPServer) { + this.eventMeshTCPServer = eventMeshTCPServer; } @Override @@ -58,9 +59,9 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { logger.info("client|tcp|channelActive|remoteAddress={}|msg={}", remoteAddress, ""); int c = connections.incrementAndGet(); - if (c > proxyTCPServer.getAccessConfiguration().proxyTcpClientMaxNum) { + if (c > eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshTcpClientMaxNum) { logger.warn("client|tcp|channelActive|remoteAddress={}|msg={}", remoteAddress, "too many client connect " + - "this proxy server"); + "this eventMesh server"); ctx.close(); return; } @@ -73,7 +74,7 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { connections.decrementAndGet(); final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel()); logger.info("client|tcp|channelInactive|remoteAddress={}|msg={}", remoteAddress, ""); - proxyTCPServer.getClientSessionGroupMapping().closeSession(ctx); + eventMeshTCPServer.getClientSessionGroupMapping().closeSession(ctx); super.channelInactive(ctx); } @@ -85,7 +86,7 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc if (event.state().equals(IdleState.ALL_IDLE)) { final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel()); logger.info("client|tcp|userEventTriggered|remoteAddress={}|msg={}", remoteAddress, evt.getClass().getName()); - proxyTCPServer.getClientSessionGroupMapping().closeSession(ctx); + eventMeshTCPServer.getClientSessionGroupMapping().closeSession(ctx); } } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/ProxyTcpExceptionHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/EventMeshTcpExceptionHandler.java similarity index 62% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/ProxyTcpExceptionHandler.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/EventMeshTcpExceptionHandler.java index 8fd7499226..d6ed12a30f 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/ProxyTcpExceptionHandler.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/EventMeshTcpExceptionHandler.java @@ -15,29 +15,30 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client; +package org.apache.eventmesh.runtime.core.protocol.tcp.client; -import com.webank.emesher.boot.ProxyTCPServer; -import com.webank.emesher.core.protocol.tcp.client.session.Session; -import com.webank.eventmesh.common.protocol.tcp.OPStatus; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; import io.netty.channel.ChannelDuplexHandler; import io.netty.channel.ChannelHandlerContext; + +import org.apache.eventmesh.common.protocol.tcp.OPStatus; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class ProxyTcpExceptionHandler extends ChannelDuplexHandler { +public class EventMeshTcpExceptionHandler extends ChannelDuplexHandler { private final Logger logger = LoggerFactory.getLogger(this.getClass()); - private ProxyTCPServer proxyTCPServer; + private EventMeshTCPServer eventMeshTCPServer; - public ProxyTcpExceptionHandler(ProxyTCPServer proxyTCPServer) { - this.proxyTCPServer = proxyTCPServer; + public EventMeshTcpExceptionHandler(EventMeshTCPServer eventMeshTCPServer) { + this.eventMeshTCPServer = eventMeshTCPServer; } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - Session session = proxyTCPServer.getClientSessionGroupMapping().getSession(ctx); + Session session = eventMeshTCPServer.getClientSessionGroupMapping().getSession(ctx); UserAgent client = session == null ? null : session.getClient(); logger.error("exceptionCaught, push goodbye to client|user={},errMsg={}", client, cause.fillInStackTrace()); String errMsg; @@ -48,9 +49,9 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E } if (session != null) { - ProxyTcp2Client.goodBye2Client(session, errMsg, OPStatus.FAIL.getCode(), proxyTCPServer.getClientSessionGroupMapping()); + EventMeshTcp2Client.goodBye2Client(eventMeshTCPServer, session, errMsg, OPStatus.FAIL.getCode(), eventMeshTCPServer.getClientSessionGroupMapping()); } else { - ProxyTcp2Client.goodBye2Client(ctx, errMsg, proxyTCPServer.getClientSessionGroupMapping(), proxyTCPServer.getProxyTcpMonitor()); + EventMeshTcp2Client.goodBye2Client(ctx, errMsg, eventMeshTCPServer.getClientSessionGroupMapping(), eventMeshTCPServer.getEventMeshTcpMonitor()); } } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/ProxyTcpMessageDispatcher.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/EventMeshTcpMessageDispatcher.java similarity index 53% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/ProxyTcpMessageDispatcher.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/EventMeshTcpMessageDispatcher.java index 6fe18dc2cc..cbe74f4a2e 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/ProxyTcpMessageDispatcher.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/EventMeshTcpMessageDispatcher.java @@ -15,76 +15,79 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client; +package org.apache.eventmesh.runtime.core.protocol.tcp.client; -import com.webank.emesher.boot.ProxyTCPServer; -import com.webank.emesher.core.protocol.tcp.client.session.SessionState; -import com.webank.emesher.core.protocol.tcp.client.task.GoodbyeTask; -import com.webank.emesher.core.protocol.tcp.client.task.HeartBeatTask; -import com.webank.emesher.core.protocol.tcp.client.task.HelloTask; -import com.webank.emesher.core.protocol.tcp.client.task.ListenTask; -import com.webank.emesher.core.protocol.tcp.client.task.MessageAckTask; -import com.webank.emesher.core.protocol.tcp.client.task.MessageTransferTask; -import com.webank.emesher.core.protocol.tcp.client.task.SubscribeTask; -import com.webank.emesher.core.protocol.tcp.client.task.UnSubscribeTask; -import com.webank.eventmesh.common.protocol.tcp.AccessMessage; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.emesher.util.ProxyUtil; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; + +import org.apache.eventmesh.common.protocol.tcp.Command; +import org.apache.eventmesh.common.protocol.tcp.EventMeshMessage; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.SessionState; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.task.GoodbyeTask; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.task.HeartBeatTask; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.task.HelloTask; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.task.ListenTask; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.task.MessageAckTask; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.task.MessageTransferTask; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.task.SubscribeTask; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.task.UnSubscribeTask; +import org.apache.eventmesh.runtime.util.EventMeshUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class ProxyTcpMessageDispatcher extends SimpleChannelInboundHandler { +public class EventMeshTcpMessageDispatcher extends SimpleChannelInboundHandler { private final Logger logger = LoggerFactory.getLogger(this.getClass()); private final Logger messageLogger = LoggerFactory.getLogger("message"); - private ProxyTCPServer proxyTCPServer; + private EventMeshTCPServer eventMeshTCPServer; - public ProxyTcpMessageDispatcher(ProxyTCPServer proxyTCPServer) { - this.proxyTCPServer = proxyTCPServer; + public EventMeshTcpMessageDispatcher(EventMeshTCPServer eventMeshTCPServer) { + this.eventMeshTCPServer = eventMeshTCPServer; } + @Override protected void channelRead0(ChannelHandlerContext ctx, Package pkg) throws Exception { long startTime = System.currentTimeMillis(); validateMsg(pkg); - proxyTCPServer.getProxyTcpMonitor().getClient2proxyMsgNum().incrementAndGet(); + eventMeshTCPServer.getEventMeshTcpMonitor().getClient2EventMeshMsgNum().incrementAndGet(); Command cmd = null; try { Runnable task; cmd = pkg.getHeader().getCommand(); if (cmd.equals(Command.HELLO_REQUEST)) { - messageLogger.info("pkg|c2proxy|cmd={}|pkg={}", cmd, pkg); - task = new HelloTask(pkg, ctx, startTime, proxyTCPServer); - ProxyTCPServer.taskHandleExecutorService.submit(task); + messageLogger.info("pkg|c2eventMesh|cmd={}|pkg={}", cmd, pkg); + task = new HelloTask(pkg, ctx, startTime, eventMeshTCPServer); + eventMeshTCPServer.getTaskHandleExecutorService().submit(task); return; } - if (proxyTCPServer.getClientSessionGroupMapping().getSession(ctx) == null) { - messageLogger.info("pkg|c2proxy|cmd={}|pkg={},no session is found", cmd, pkg); + if (eventMeshTCPServer.getClientSessionGroupMapping().getSession(ctx) == null) { + messageLogger.info("pkg|c2eventMesh|cmd={}|pkg={},no session is found", cmd, pkg); throw new Exception("no session is found"); } logMessageFlow(ctx, pkg, cmd); - if (proxyTCPServer.getClientSessionGroupMapping().getSession(ctx).getSessionState() == SessionState.CLOSED) { - throw new Exception("this proxy tcp session will be closed, may be reboot or version change!"); + if (eventMeshTCPServer.getClientSessionGroupMapping().getSession(ctx).getSessionState() == SessionState.CLOSED) { + throw new Exception("this eventMesh tcp session will be closed, may be reboot or version change!"); } dispatch(ctx, pkg, startTime, cmd); } catch (Exception e) { logger.error("exception occurred while pkg|cmd={}|pkg={}|errMsg={}", cmd, pkg, e); - throw new RuntimeException(e); + //throw new RuntimeException(e); + throw e; } } private void logMessageFlow(ChannelHandlerContext ctx, Package pkg, Command cmd) { - if (pkg.getBody() instanceof AccessMessage) { - messageLogger.info("pkg|c2proxy|cmd={}|Msg={}|user={}", cmd, ProxyUtil.printMqMessage((AccessMessage) pkg - .getBody()), proxyTCPServer.getClientSessionGroupMapping().getSession(ctx).getClient()); + if (pkg.getBody() instanceof EventMeshMessage) { + messageLogger.info("pkg|c2eventMesh|cmd={}|Msg={}|user={}", cmd, EventMeshUtil.printMqMessage((EventMeshMessage) pkg + .getBody()), eventMeshTCPServer.getClientSessionGroupMapping().getSession(ctx).getClient()); } else { - messageLogger.info("pkg|c2proxy|cmd={}|pkg={}|user={}", cmd, pkg, proxyTCPServer.getClientSessionGroupMapping().getSession(ctx).getClient()); + messageLogger.info("pkg|c2eventMesh|cmd={}|pkg={}|user={}", cmd, pkg, eventMeshTCPServer.getClientSessionGroupMapping().getSession(ctx).getClient()); } } @@ -107,36 +110,36 @@ private void dispatch(ChannelHandlerContext ctx, Package pkg, long startTime, Co Runnable task; switch (cmd) { case HEARTBEAT_REQUEST: - task = new HeartBeatTask(pkg, ctx, startTime, proxyTCPServer); + task = new HeartBeatTask(pkg, ctx, startTime, eventMeshTCPServer); break; case CLIENT_GOODBYE_REQUEST: case SERVER_GOODBYE_RESPONSE: - task = new GoodbyeTask(pkg, ctx, startTime, proxyTCPServer); + task = new GoodbyeTask(pkg, ctx, startTime, eventMeshTCPServer); break; case SUBSCRIBE_REQUEST: - task = new SubscribeTask(pkg, ctx, startTime, proxyTCPServer); + task = new SubscribeTask(pkg, ctx, startTime, eventMeshTCPServer); break; case UNSUBSCRIBE_REQUEST: - task = new UnSubscribeTask(pkg, ctx, startTime, proxyTCPServer); + task = new UnSubscribeTask(pkg, ctx, startTime, eventMeshTCPServer); break; case LISTEN_REQUEST: - task = new ListenTask(pkg, ctx, startTime, proxyTCPServer); + task = new ListenTask(pkg, ctx, startTime, eventMeshTCPServer); break; case REQUEST_TO_SERVER: case RESPONSE_TO_SERVER: case ASYNC_MESSAGE_TO_SERVER: case BROADCAST_MESSAGE_TO_SERVER: - task = new MessageTransferTask(pkg, ctx, startTime, proxyTCPServer); + task = new MessageTransferTask(pkg, ctx, startTime, eventMeshTCPServer); break; case RESPONSE_TO_CLIENT_ACK: case ASYNC_MESSAGE_TO_CLIENT_ACK: case BROADCAST_MESSAGE_TO_CLIENT_ACK: case REQUEST_TO_CLIENT_ACK: - task = new MessageAckTask(pkg, ctx, startTime, proxyTCPServer); + task = new MessageAckTask(pkg, ctx, startTime, eventMeshTCPServer); break; default: throw new Exception("unknown cmd"); } - ProxyTCPServer.taskHandleExecutorService.submit(task); + eventMeshTCPServer.getTaskHandleExecutorService().submit(task); } } diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/group/ClientGroupWrapper.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/group/ClientGroupWrapper.java new file mode 100644 index 0000000000..5829e4941a --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/group/ClientGroupWrapper.java @@ -0,0 +1,774 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.core.protocol.tcp.client.group; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Properties; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +import com.alibaba.fastjson.JSON; + +import io.openmessaging.api.AsyncConsumeContext; +import io.openmessaging.api.AsyncMessageListener; +import io.openmessaging.api.Message; +import io.openmessaging.api.OnExceptionContext; +import io.openmessaging.api.SendCallback; +import io.openmessaging.api.SendResult; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.api.EventMeshAction; +import org.apache.eventmesh.api.EventMeshAsyncConsumeContext; +import org.apache.eventmesh.api.RRCallback; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.SubscriptionItem; +import org.apache.eventmesh.common.protocol.SubscriptionMode; +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.apache.eventmesh.runtime.configuration.EventMeshTCPConfiguration; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.plugin.MQConsumerWrapper; +import org.apache.eventmesh.runtime.core.plugin.MQProducerWrapper; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.group.dispatch.DownstreamDispatchStrategy; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push.DownStreamMsgContext; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push.retry.EventMeshTcpRetryer; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.send.UpStreamMsgContext; +import org.apache.eventmesh.runtime.metrics.tcp.EventMeshTcpMonitor; +import org.apache.eventmesh.runtime.util.EventMeshUtil; +import org.apache.eventmesh.runtime.util.HttpTinyClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ClientGroupWrapper { + + public static Logger logger = LoggerFactory.getLogger(ClientGroupWrapper.class); + + private String producerGroup; + + private String consumerGroup; + + //can be sysid + ext(eg dcn) + private String sysId; + + private EventMeshTCPConfiguration eventMeshTCPConfiguration; + + private EventMeshTCPServer eventMeshTCPServer; + + private EventMeshTcpRetryer eventMeshTcpRetryer; + + private EventMeshTcpMonitor eventMeshTcpMonitor; + + private DownstreamDispatchStrategy downstreamDispatchStrategy; + + private final ReadWriteLock groupLock = new ReentrantReadWriteLock(); + + public Set groupConsumerSessions = new HashSet(); + + public Set groupProducerSessions = new HashSet(); + + public AtomicBoolean started4Persistent = new AtomicBoolean(Boolean.FALSE); + + public AtomicBoolean started4Broadcast = new AtomicBoolean(Boolean.FALSE); + + public AtomicBoolean inited4Persistent = new AtomicBoolean(Boolean.FALSE); + + public AtomicBoolean inited4Broadcast = new AtomicBoolean(Boolean.FALSE); + + private MQConsumerWrapper persistentMsgConsumer = new MQConsumerWrapper(); + + private MQConsumerWrapper broadCastMsgConsumer = new MQConsumerWrapper(); + + private ConcurrentHashMap> topic2sessionInGroupMapping = new ConcurrentHashMap>(); + + public AtomicBoolean producerStarted = new AtomicBoolean(Boolean.FALSE); + + public ClientGroupWrapper(String sysId, String producerGroup, String consumerGroup, + EventMeshTCPServer eventMeshTCPServer, + DownstreamDispatchStrategy downstreamDispatchStrategy) { + this.sysId = sysId; + this.producerGroup = producerGroup; + this.consumerGroup = consumerGroup; + this.eventMeshTCPServer = eventMeshTCPServer; + this.eventMeshTCPConfiguration = eventMeshTCPServer.getEventMeshTCPConfiguration(); + this.eventMeshTcpRetryer = eventMeshTCPServer.getEventMeshTcpRetryer(); + this.eventMeshTcpMonitor = eventMeshTCPServer.getEventMeshTcpMonitor(); + this.downstreamDispatchStrategy = downstreamDispatchStrategy; + } + + public ConcurrentHashMap> getTopic2sessionInGroupMapping() { + return topic2sessionInGroupMapping; + } + + public boolean hasSubscription(String topic) { + boolean has = false; + try { + this.groupLock.readLock().lockInterruptibly(); + has = topic2sessionInGroupMapping.containsKey(topic); + } catch (Exception e) { + logger.error("hasSubscription error! topic[{}]", topic); + } finally { + this.groupLock.readLock().unlock(); + } + + return has; + } + + public boolean send(UpStreamMsgContext upStreamMsgContext, SendCallback sendCallback) throws Exception { + mqProducerWrapper.send(upStreamMsgContext.getMsg(), sendCallback); + return true; + } + + public void request(UpStreamMsgContext upStreamMsgContext, SendCallback sendCallback, RRCallback rrCallback, long timeout) + throws Exception { + mqProducerWrapper.request(upStreamMsgContext.getMsg(), sendCallback, rrCallback, timeout); + } + + public boolean reply(UpStreamMsgContext upStreamMsgContext) throws Exception { + mqProducerWrapper.reply(upStreamMsgContext.getMsg(), new SendCallback() { + @Override + public void onSuccess(SendResult sendResult) { + + } + + @Override + public void onException(OnExceptionContext context) { + String bizSeqNo = upStreamMsgContext.getMsg().getSystemProperties(EventMeshConstants.PROPERTY_MESSAGE_KEYS); + logger.error("reply err! topic:{}, bizSeqNo:{}, client:{}", + upStreamMsgContext.getMsg().getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION), bizSeqNo, + upStreamMsgContext.getSession().getClient(), context.getException()); + } + }); + return true; + } + + private MQProducerWrapper mqProducerWrapper = new MQProducerWrapper(); + + public MQProducerWrapper getMqProducerWrapper() { + return mqProducerWrapper; + } + + public boolean addSubscription(String topic, Session session) throws Exception { + if (session == null || !StringUtils.equalsIgnoreCase(consumerGroup, EventMeshUtil.buildClientGroup(session.getClient().getConsumerGroup()))) { + logger.error("addSubscription param error,topic:{},session:{}", topic, session); + return false; + } + + boolean r = false; + try { + this.groupLock.writeLock().lockInterruptibly(); + if (!topic2sessionInGroupMapping.containsKey(topic)) { + Set sessions = new HashSet(); + topic2sessionInGroupMapping.put(topic, sessions); + } + r = topic2sessionInGroupMapping.get(topic).add(session); + if (r) { + logger.info("addSubscription success, group:{} topic:{} client:{}", consumerGroup, topic, session.getClient()); + } else { + logger.warn("addSubscription fail, group:{} topic:{} client:{}", consumerGroup, topic, session.getClient()); + } + } catch (Exception e) { + logger.error("addSubscription error! topic:{} client:{}", topic, session.getClient(), e); + throw new Exception("addSubscription fail"); + } finally { + this.groupLock.writeLock().unlock(); + } + return r; + } + + public boolean removeSubscription(String topic, Session session) { + if (session == null + || !StringUtils.equalsIgnoreCase(consumerGroup, EventMeshUtil.buildClientGroup(session.getClient().getConsumerGroup()))) { + logger.error("removeSubscription param error,topic:{},session:{}", topic, session); + return false; + } + + boolean r = false; + try { + this.groupLock.writeLock().lockInterruptibly(); + if (topic2sessionInGroupMapping.containsKey(topic)) { + r = topic2sessionInGroupMapping.get(topic).remove(session); + if (r) { + logger.info("removeSubscription remove session success, group:{} topic:{} client:{}", consumerGroup, topic, session.getClient()); + } else { + logger.warn("removeSubscription remove session failed, group:{} topic:{} client:{}", consumerGroup, topic, session.getClient()); + } + } + if (CollectionUtils.size(topic2sessionInGroupMapping.get(topic)) == 0) { + topic2sessionInGroupMapping.remove(topic); + logger.info("removeSubscription remove topic success, group:{} topic:{}", consumerGroup, topic); + } + } catch (Exception e) { + logger.error("removeSubscription error! topic:{} client:{}", topic, session.getClient(), e); + } finally { + this.groupLock.writeLock().unlock(); + } + return r; + } + + public synchronized void startClientGroupProducer() throws Exception { + if (producerStarted.get()) { + return; + } + + Properties keyValue = new Properties(); +// KeyValue keyValue = OMS.newKeyValue(); + keyValue.put("producerGroup", producerGroup); + keyValue.put("instanceName", EventMeshUtil.buildMeshTcpClientID(sysId, "PUB", eventMeshTCPConfiguration.eventMeshCluster)); + + //TODO for defibus + keyValue.put("eventMeshIDC", eventMeshTCPConfiguration.eventMeshIDC); + + mqProducerWrapper.init(keyValue); + mqProducerWrapper.start(); + producerStarted.compareAndSet(false, true); + logger.info("starting producer success, group:{}", producerGroup); + } + + public synchronized void shutdownProducer() throws Exception { + if (!producerStarted.get()) { + return; + } + mqProducerWrapper.shutdown(); + producerStarted.compareAndSet(true, false); + logger.info("shutdown producer success for group:{}", producerGroup); + } + + public String getProducerGroup() { + return producerGroup; + } + + public void setProducerGroup(String producerGroup) { + this.producerGroup = producerGroup; + } + + public String getConsumerGroup() { + return consumerGroup; + } + + public void setConsumerGroup(String consumerGroup) { + this.consumerGroup = consumerGroup; + } + + public boolean addGroupConsumerSession(Session session) { + if (session == null + || !StringUtils.equalsIgnoreCase(consumerGroup, EventMeshUtil.buildClientGroup(session.getClient().getConsumerGroup()))) { + logger.error("addGroupConsumerSession param error,session:{}", session); + return false; + } + + boolean r = false; + try { + this.groupLock.writeLock().lockInterruptibly(); + r = groupConsumerSessions.add(session); + if (r) { + logger.info("addGroupConsumerSession success, group:{} client:{}", consumerGroup, session.getClient()); + } + } catch (Exception e) { + logger.error("addGroupConsumerSession error! group:{} client:{}", consumerGroup, session.getClient(), e); + } finally { + this.groupLock.writeLock().unlock(); + } + return r; + } + + public boolean addGroupProducerSession(Session session) { + if (session == null + || !StringUtils.equalsIgnoreCase(producerGroup, EventMeshUtil.buildClientGroup(session.getClient().getProducerGroup()))) { + logger.error("addGroupProducerSession param error,session:{}", session); + return false; + } + + boolean r = false; + try { + this.groupLock.writeLock().lockInterruptibly(); + r = groupProducerSessions.add(session); + if (r) { + logger.info("addGroupProducerSession success, group:{} client:{}", producerGroup, session.getClient()); + } + } catch (Exception e) { + logger.error("addGroupProducerSession error! group:{} client:{}", producerGroup, session.getClient(), e); + } finally { + this.groupLock.writeLock().unlock(); + } + return r; + } + + public boolean removeGroupConsumerSession(Session session) { + if (session == null + || !StringUtils.equalsIgnoreCase(consumerGroup, EventMeshUtil.buildClientGroup(session.getClient().getConsumerGroup()))) { + logger.error("removeGroupConsumerSession param error,session:{}", session); + return false; + } + + boolean r = false; + try { + this.groupLock.writeLock().lockInterruptibly(); + r = groupConsumerSessions.remove(session); + if (r) { + logger.info("removeGroupConsumerSession success, group:{} client:{}", consumerGroup, session.getClient()); + } + } catch (Exception e) { + logger.error("removeGroupConsumerSession error! group:{} client:{}", consumerGroup, session.getClient(), e); + } finally { + this.groupLock.writeLock().unlock(); + } + return r; + } + + public boolean removeGroupProducerSession(Session session) { + if (session == null + || !StringUtils.equalsIgnoreCase(producerGroup, EventMeshUtil.buildClientGroup(session.getClient().getProducerGroup()))) { + logger.error("removeGroupProducerSession param error,session:{}", session); + return false; + } + + boolean r = false; + try { + this.groupLock.writeLock().lockInterruptibly(); + r = groupProducerSessions.remove(session); + if (r) { + logger.info("removeGroupProducerSession success, group:{} client:{}", producerGroup, session.getClient()); + } + } catch (Exception e) { + logger.error("removeGroupProducerSession error! group:{} client:{}", producerGroup, session.getClient(), e); + } finally { + this.groupLock.writeLock().unlock(); + } + + return r; + } + + public synchronized void initClientGroupPersistentConsumer() throws Exception { + if (inited4Persistent.get()) { + return; + } + + Properties keyValue = new Properties(); + keyValue.put("isBroadcast", "false"); + keyValue.put("consumerGroup", consumerGroup); + keyValue.put("eventMeshIDC", eventMeshTCPConfiguration.eventMeshIDC); + keyValue.put("instanceName", EventMeshUtil.buildMeshTcpClientID(sysId, "SUB", eventMeshTCPConfiguration.eventMeshCluster)); + + persistentMsgConsumer.init(keyValue); +// persistentMsgConsumer.registerMessageListener(new EventMeshMessageListenerConcurrently() { +// +// @Override +// public EventMeshConsumeConcurrentlyStatus handleMessage(MessageExt msg, EventMeshConsumeConcurrentlyContext context) { +// +// if (msg == null) +// return EventMeshConsumeConcurrentlyStatus.CONSUME_SUCCESS; +// +// eventMeshTcpMonitor.getMq2eventMeshMsgNum().incrementAndGet(); +// String topic = msg.getTopic(); +// msg.putUserProperty(EventMeshConstants.REQ_MQ2EVENTMESH_TIMESTAMP, String.valueOf(System.currentTimeMillis())); +// msg.putUserProperty(EventMeshConstants.REQ_RECEIVE_EVENTMESH_IP, accessConfiguration.eventMeshServerIp); +// msg.putUserProperty(EventMeshConstants.BORN_TIMESTAMP, String.valueOf(msg.getBornTimestamp())); +// msg.putUserProperty(EventMeshConstants.STORE_TIMESTAMP, String.valueOf(msg.getStoreTimestamp())); +// +// if (!EventMeshUtil.isValidRMBTopic(topic)) { +// return EventMeshConsumeConcurrentlyStatus.CONSUME_SUCCESS; +// } +// +// Session session = downstreamDispatchStrategy.select(groupName, topic, groupConsumerSessions); +// String bizSeqNo = EventMeshUtil.getMessageBizSeq(msg); +// if(session == null){ +// try { +// Integer sendBackTimes = MapUtils.getInteger(msg.getProperties(), EventMeshConstants.EVENTMESH_SEND_BACK_TIMES, new Integer(0)); +// String sendBackFromEventMeshIp = MapUtils.getString(msg.getProperties(), EventMeshConstants.EVENTMESH_SEND_BACK_IP, ""); +// logger.error("found no session to downstream msg,groupName:{}, topic:{}, bizSeqNo:{}", groupName, topic, bizSeqNo); +// +// if (sendBackTimes >= eventMeshTCPServer.getAccessConfiguration().eventMeshTcpSendBackMaxTimes) { +// logger.error("sendBack to broker over max times:{}, groupName:{}, topic:{}, bizSeqNo:{}", eventMeshTCPServer.getAccessConfiguration().eventMeshTcpSendBackMaxTimes, groupName, topic, bizSeqNo); +// } else { +// sendBackTimes++; +// msg.putUserProperty(EventMeshConstants.EVENTMESH_SEND_BACK_TIMES, sendBackTimes.toString()); +// msg.putUserProperty(EventMeshConstants.EVENTMESH_SEND_BACK_IP, sendBackFromEventMeshIp); +// sendMsgBackToBroker(msg, bizSeqNo); +// } +// } catch (Exception e){ +// logger.warn("handle msg exception when no session found", e); +// } +// +// return EventMeshConsumeConcurrentlyStatus.CONSUME_SUCCESS; +// } +// +// DownStreamMsgContext downStreamMsgContext = +// new DownStreamMsgContext(msg, session, persistentMsgConsumer, (EventMeshConsumeConcurrentlyContext)context, false); +// +// if(downstreamMap.size() < eventMeshTCPServer.getAccessConfiguration().eventMeshTcpDownStreamMapSize){ +// downstreamMap.putIfAbsent(downStreamMsgContext.seq, downStreamMsgContext); +// }else{ +// logger.warn("downStreamMap is full,group:{}", groupName); +// } +// +// if (session.isCanDownStream()) { +// session.downstreamMsg(downStreamMsgContext); +// return EventMeshConsumeConcurrentlyStatus.CONSUME_FINISH; +// } +// +// logger.warn("session is busy,dispatch retry,seq:{}, session:{}, bizSeq:{}", downStreamMsgContext.seq, downStreamMsgContext.session.getClient(), bizSeqNo); +// long delayTime = EventMeshUtil.isService(downStreamMsgContext.msgExt.getTopic()) ? 0 : eventMeshTCPServer.getAccessConfiguration().eventMeshTcpMsgRetryDelayInMills; +// downStreamMsgContext.delay(delayTime); +// eventMeshTcpRetryer.pushRetry(downStreamMsgContext); +// +// return EventMeshConsumeConcurrentlyStatus.CONSUME_FINISH; +// } +// }); + inited4Persistent.compareAndSet(false, true); + logger.info("init persistentMsgConsumer success, group:{}", consumerGroup); + } + + public synchronized void startClientGroupPersistentConsumer() throws Exception { + if (started4Persistent.get()) { + return; + } + persistentMsgConsumer.start(); + started4Persistent.compareAndSet(false, true); + logger.info("starting persistentMsgConsumer success, group:{}", consumerGroup); + } + + public synchronized void initClientGroupBroadcastConsumer() throws Exception { + if (inited4Broadcast.get()) { + return; + } + + Properties keyValue = new Properties(); + keyValue.put("isBroadcast", "true"); + keyValue.put("consumerGroup", consumerGroup); + keyValue.put("eventMeshIDC", eventMeshTCPConfiguration.eventMeshIDC); + keyValue.put("instanceName", EventMeshUtil.buildMeshTcpClientID(sysId, "SUB", eventMeshTCPConfiguration.eventMeshCluster)); + broadCastMsgConsumer.init(keyValue); +// broadCastMsgConsumer.registerMessageListener(new EventMeshMessageListenerConcurrently() { +// @Override +// public EventMeshConsumeConcurrentlyStatus handleMessage(MessageExt msg, EventMeshConsumeConcurrentlyContext context) { +// if (msg == null) +// return EventMeshConsumeConcurrentlyStatus.CONSUME_SUCCESS; +// +// eventMeshTcpMonitor.getMq2eventMeshMsgNum().incrementAndGet(); +// +// String topic = msg.getTopic(); +// +// msg.putUserProperty(EventMeshConstants.REQ_MQ2EVENTMESH_TIMESTAMP, String.valueOf(System.currentTimeMillis())); +// msg.putUserProperty(EventMeshConstants.REQ_RECEIVE_EVENTMESH_IP, accessConfiguration.eventMeshServerIp); +// msg.putUserProperty(EventMeshConstants.BORN_TIMESTAMP, String.valueOf(msg.getBornTimestamp())); +// msg.putUserProperty(EventMeshConstants.STORE_TIMESTAMP, String.valueOf(msg.getStoreTimestamp())); +// +// if (!EventMeshUtil.isValidRMBTopic(topic)) { +// return EventMeshConsumeConcurrentlyStatus.CONSUME_SUCCESS; +// } +// +// if(CollectionUtils.isEmpty(groupConsumerSessions)){ +// logger.warn("found no session to downstream broadcast msg"); +// return EventMeshConsumeConcurrentlyStatus.CONSUME_SUCCESS; +// } +// +// Iterator sessionsItr = groupConsumerSessions.iterator(); +// +// while (sessionsItr.hasNext()) { +// Session session = sessionsItr.next(); +// +// if (!session.isAvailable(topic)) { +// logger.warn("downstream broadcast msg,session is not available,client:{}",session.getClient()); +// continue; +// } +// +// DownStreamMsgContext downStreamMsgContext = +// new DownStreamMsgContext(msg, session, broadCastMsgConsumer, context, false); +// +// if (session.isCanDownStream()) { +// session.downstreamMsg(downStreamMsgContext); +// continue; +// } +// +// logger.warn("downstream broadcast msg,session is busy,dispatch retry,seq:{}, session:{}, bizSeq:{}", downStreamMsgContext.seq, downStreamMsgContext.session.getClient(), EventMeshUtil.getMessageBizSeq(downStreamMsgContext.msgExt)); +// long delayTime = EventMeshUtil.isService(downStreamMsgContext.msgExt.getTopic()) ? 0 : eventMeshTCPServer.getAccessConfiguration().eventMeshTcpMsgRetryDelayInMills; +// downStreamMsgContext.delay(delayTime); +// eventMeshTcpRetryer.pushRetry(downStreamMsgContext); +// } +// +// return EventMeshConsumeConcurrentlyStatus.CONSUME_FINISH; +// } +// }); + inited4Broadcast.compareAndSet(false, true); + logger.info("init broadCastMsgConsumer success, group:{}", consumerGroup); + } + + public synchronized void startClientGroupBroadcastConsumer() throws Exception { + if (started4Broadcast.get()) { + return; + } + broadCastMsgConsumer.start(); + started4Broadcast.compareAndSet(false, true); + logger.info("starting broadCastMsgConsumer success, group:{}", consumerGroup); + } + + public void subscribe(SubscriptionItem subscriptionItem) throws Exception { + AsyncMessageListener listener = null; + if (SubscriptionMode.BROADCASTING.equals(subscriptionItem.getMode())) { + listener = new AsyncMessageListener() { + @Override + public void consume(Message message, AsyncConsumeContext context) { + + eventMeshTcpMonitor.getMq2EventMeshMsgNum().incrementAndGet(); + String topic = message.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION); + message.getSystemProperties().put(EventMeshConstants.REQ_MQ2EVENTMESH_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + message.getSystemProperties().put(EventMeshConstants.REQ_RECEIVE_EVENTMESH_IP, eventMeshTCPConfiguration.eventMeshServerIp); + + EventMeshAsyncConsumeContext eventMeshAsyncConsumeContext = (EventMeshAsyncConsumeContext) context; + if (CollectionUtils.isEmpty(groupConsumerSessions)) { + logger.warn("found no session to downstream broadcast msg"); +// context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, EventMeshConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); +// context.ack(); + eventMeshAsyncConsumeContext.commit(EventMeshAction.CommitMessage); + return; + } + + Iterator sessionsItr = groupConsumerSessions.iterator(); + + DownStreamMsgContext downStreamMsgContext = + new DownStreamMsgContext(message, null, broadCastMsgConsumer, eventMeshAsyncConsumeContext.getAbstractContext(), false, subscriptionItem); + + while (sessionsItr.hasNext()) { + Session session = sessionsItr.next(); + + if (!session.isAvailable(topic)) { + logger.warn("downstream broadcast msg,session is not available,client:{}", session.getClient()); + continue; + } + + downStreamMsgContext.session = session; + + //downstream broadcast msg asynchronously + eventMeshTCPServer.getBroadcastMsgDownstreamExecutorService().submit(new Runnable() { + @Override + public void run() { + //msg put in eventmesh,waiting client ack + session.getPusher().unAckMsg(downStreamMsgContext.seq, downStreamMsgContext); + session.downstreamMsg(downStreamMsgContext); + } + }); + } + +// context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, EventMeshConsumeConcurrentlyStatus.CONSUME_FINISH.name()); +// context.ack(); + eventMeshAsyncConsumeContext.commit(EventMeshAction.ManualAck); + } + }; + broadCastMsgConsumer.subscribe(subscriptionItem.getTopic(), listener); + } else { + listener = new AsyncMessageListener() { + @Override + public void consume(Message message, AsyncConsumeContext context) { + eventMeshTcpMonitor.getMq2EventMeshMsgNum().incrementAndGet(); + String topic = message.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION); + message.getSystemProperties().put(EventMeshConstants.REQ_MQ2EVENTMESH_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + message.getSystemProperties().put(EventMeshConstants.REQ_RECEIVE_EVENTMESH_IP, eventMeshTCPConfiguration.eventMeshServerIp); + + EventMeshAsyncConsumeContext eventMeshAsyncConsumeContext = (EventMeshAsyncConsumeContext) context; + Session session = downstreamDispatchStrategy.select(consumerGroup, topic, groupConsumerSessions); + String bizSeqNo = EventMeshUtil.getMessageBizSeq(message); + if (session == null) { + try { + Integer sendBackTimes = new Integer(0); + String sendBackFromEventMeshIp = ""; + if (StringUtils.isNotBlank(message.getSystemProperties(EventMeshConstants.EVENTMESH_SEND_BACK_TIMES))) { + sendBackTimes = Integer.valueOf(message.getSystemProperties(EventMeshConstants.EVENTMESH_SEND_BACK_TIMES)); + } + if (StringUtils.isNotBlank(message.getSystemProperties(EventMeshConstants.EVENTMESH_SEND_BACK_IP))) { + sendBackFromEventMeshIp = message.getSystemProperties(EventMeshConstants.EVENTMESH_SEND_BACK_IP); + } + + logger.error("found no session to downstream msg,groupName:{}, topic:{}, bizSeqNo:{}, sendBackTimes:{}, sendBackFromEventMeshIp:{}", consumerGroup, topic, bizSeqNo, sendBackTimes, sendBackFromEventMeshIp); + + if (sendBackTimes >= eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshTcpSendBackMaxTimes) { + logger.error("sendBack to broker over max times:{}, groupName:{}, topic:{}, bizSeqNo:{}", eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshTcpSendBackMaxTimes, consumerGroup, topic, bizSeqNo); + } else { + sendBackTimes++; + message.getSystemProperties().put(EventMeshConstants.EVENTMESH_SEND_BACK_TIMES, sendBackTimes.toString()); + message.getSystemProperties().put(EventMeshConstants.EVENTMESH_SEND_BACK_IP, eventMeshTCPConfiguration.eventMeshServerIp); + sendMsgBackToBroker(message, bizSeqNo); + } + } catch (Exception e) { + logger.warn("handle msg exception when no session found", e); + } + +// context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, EventMeshConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); +// context.ack(); + eventMeshAsyncConsumeContext.commit(EventMeshAction.CommitMessage); + return; + } + + DownStreamMsgContext downStreamMsgContext = + new DownStreamMsgContext(message, session, persistentMsgConsumer, eventMeshAsyncConsumeContext.getAbstractContext(), false, subscriptionItem); + //msg put in eventmesh,waiting client ack + session.getPusher().unAckMsg(downStreamMsgContext.seq, downStreamMsgContext); + session.downstreamMsg(downStreamMsgContext); +// context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, EventMeshConsumeConcurrentlyStatus.CONSUME_FINISH.name()); +// context.ack(); + eventMeshAsyncConsumeContext.commit(EventMeshAction.ManualAck); + } + }; + persistentMsgConsumer.subscribe(subscriptionItem.getTopic(), listener); + } + } + + public void unsubscribe(SubscriptionItem subscriptionItem) throws Exception { + if (SubscriptionMode.BROADCASTING.equals(subscriptionItem.getMode())) { + broadCastMsgConsumer.unsubscribe(subscriptionItem.getTopic()); + } else { + persistentMsgConsumer.unsubscribe(subscriptionItem.getTopic()); + } + } + + public synchronized void shutdownBroadCastConsumer() throws Exception { + if (started4Broadcast.get()) { + broadCastMsgConsumer.shutdown(); + logger.info("broadcast consumer group:{} shutdown...", consumerGroup); + } + started4Broadcast.compareAndSet(true, false); + inited4Broadcast.compareAndSet(true, false); + broadCastMsgConsumer = null; + } + + public synchronized void shutdownPersistentConsumer() throws Exception { + + if (started4Persistent.get()) { + persistentMsgConsumer.shutdown(); + logger.info("persistent consumer group:{} shutdown...", consumerGroup); + } + started4Persistent.compareAndSet(true, false); + inited4Persistent.compareAndSet(true, false); + persistentMsgConsumer = null; + } + + public Set getGroupConsumerSessions() { + return groupConsumerSessions; + } + + public Set getGroupProducerSessions() { + return groupProducerSessions; + } + + public EventMeshTCPConfiguration getEventMeshTCPConfiguration() { + return eventMeshTCPConfiguration; + } + + public void setEventMeshTCPConfiguration(EventMeshTCPConfiguration eventMeshTCPConfiguration) { + this.eventMeshTCPConfiguration = eventMeshTCPConfiguration; + } + + public EventMeshTcpRetryer getEventMeshTcpRetryer() { + return eventMeshTcpRetryer; + } + + public void setEventMeshTcpRetryer(EventMeshTcpRetryer eventMeshTcpRetryer) { + this.eventMeshTcpRetryer = eventMeshTcpRetryer; + } + + public EventMeshTcpMonitor getEventMeshTcpMonitor() { + return eventMeshTcpMonitor; + } + + public void setEventMeshTcpMonitor(EventMeshTcpMonitor eventMeshTcpMonitor) { + this.eventMeshTcpMonitor = eventMeshTcpMonitor; + } + + public DownstreamDispatchStrategy getDownstreamDispatchStrategy() { + return downstreamDispatchStrategy; + } + + public void setDownstreamDispatchStrategy(DownstreamDispatchStrategy downstreamDispatchStrategy) { + this.downstreamDispatchStrategy = downstreamDispatchStrategy; + } + + public String getSysId() { + return sysId; + } + + private String pushMsgToEventMesh(Message msg, String ip, int port) throws Exception { + StringBuilder targetUrl = new StringBuilder(); + targetUrl.append("http://").append(ip).append(":").append(port).append("/eventMesh/msg/push"); + HttpTinyClient.HttpResult result = null; + + try { + logger.info("pushMsgToEventMesh,targetUrl:{},msg:{}", targetUrl.toString(), msg.toString()); + List paramValues = new ArrayList(); + paramValues.add("msg"); + paramValues.add(JSON.toJSONString(msg)); + paramValues.add("group"); + paramValues.add(consumerGroup); + + result = HttpTinyClient.httpPost( + targetUrl.toString(), + null, + paramValues, + "UTF-8", + 3000); + } catch (Exception e) { + logger.error("httpPost " + targetUrl + " is fail,", e); + //throw new RuntimeException("httpPost " + targetUrl + " is fail," , e); + throw e; + } + + if (200 == result.code && result.content != null) { + return result.content; + + } else { + throw new Exception("httpPost targetUrl[" + targetUrl + "] is not OK when getContentThroughHttp, httpResult: " + result + "."); + } + } + + public MQConsumerWrapper getPersistentMsgConsumer() { + return persistentMsgConsumer; + } + + private void sendMsgBackToBroker(Message msg, String bizSeqNo) throws Exception { + try { + String topic = msg.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION); + logger.warn("send msg back to broker, bizSeqno:{}, topic:{}", bizSeqNo, topic); + + send(new UpStreamMsgContext(null, null, msg), new SendCallback() { + @Override + public void onSuccess(SendResult sendResult) { + logger.info("consumerGroup:{} consume fail, sendMessageBack success, bizSeqno:{}, topic:{}", consumerGroup, bizSeqNo, topic); + } + + @Override + public void onException(OnExceptionContext context) { + logger.warn("consumerGroup:{} consume fail, sendMessageBack fail, bizSeqno:{}, topic:{}", consumerGroup, bizSeqNo, topic); + } + +// @Override +// public void onException(Throwable e) { +// logger.warn("consumerGroup:{} consume fail, sendMessageBack fail, bizSeqno:{}, topic:{}", groupName, bizSeqNo, topic); +// } + }); + eventMeshTcpMonitor.getEventMesh2mqMsgNum().incrementAndGet(); + } catch (Exception e) { + logger.warn("try send msg back to broker failed"); + throw e; + } + } +} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/group/ClientSessionGroupMapping.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/group/ClientSessionGroupMapping.java similarity index 59% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/group/ClientSessionGroupMapping.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/group/ClientSessionGroupMapping.java index d425b1e6e5..a5a4b409aa 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/group/ClientSessionGroupMapping.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/group/ClientSessionGroupMapping.java @@ -15,35 +15,40 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client.group; - -import com.webank.emesher.boot.ProxyTCPServer; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.protocol.tcp.client.ProxyTcp2Client; -import com.webank.emesher.core.protocol.tcp.client.group.dispatch.FreePriorityDispatchStrategy; -import com.webank.emesher.core.protocol.tcp.client.session.Session; -import com.webank.emesher.core.protocol.tcp.client.session.SessionState; -import com.webank.emesher.core.protocol.tcp.client.session.push.ClientAckContext; -import com.webank.emesher.core.protocol.tcp.client.session.push.DownStreamMsgContext; -import com.webank.emesher.util.ProxyUtil; -import com.webank.eventmesh.common.ThreadUtil; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; +package org.apache.eventmesh.runtime.core.protocol.tcp.client.group; + +import java.lang.ref.WeakReference; +import java.net.InetSocketAddress; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; + import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; + import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; -import org.apache.rocketmq.remoting.common.RemotingHelper; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.ThreadUtil; +import org.apache.eventmesh.common.protocol.SubscriptionItem; +import org.apache.eventmesh.common.protocol.SubscriptionMode; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.EventMeshTcp2Client; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.group.dispatch.DownstreamDispatchStrategy; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.group.dispatch.FreePriorityDispatchStrategy; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.SessionState; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push.DownStreamMsgContext; +import org.apache.eventmesh.runtime.util.EventMeshUtil; +import org.apache.eventmesh.runtime.util.RemotingHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.lang.ref.WeakReference; -import java.net.InetSocketAddress; -import java.util.*; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicInteger; - public class ClientSessionGroupMapping { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @@ -51,26 +56,26 @@ public class ClientSessionGroupMapping { private ConcurrentHashMap sessionTable = new ConcurrentHashMap<>(); - private ConcurrentHashMap clientGroupMap = new ConcurrentHashMap(); + private ConcurrentHashMap clientGroupMap = new ConcurrentHashMap(); - private ConcurrentHashMap lockMap = new ConcurrentHashMap(); + private ConcurrentHashMap lockMap = new ConcurrentHashMap(); - private ProxyTCPServer proxyTCPServer; + private EventMeshTCPServer eventMeshTCPServer; - public ClientSessionGroupMapping(ProxyTCPServer proxyTCPServer) { - this.proxyTCPServer = proxyTCPServer; + public ClientSessionGroupMapping(EventMeshTCPServer eventMeshTCPServer) { + this.eventMeshTCPServer = eventMeshTCPServer; } - public ProxyTCPServer getProxyTCPServer() { - return proxyTCPServer; + public EventMeshTCPServer getEventMeshTCPServer() { + return eventMeshTCPServer; } - public void setProxyTCPServer(ProxyTCPServer proxyTCPServer) { - this.proxyTCPServer = proxyTCPServer; + public void setEventMeshTCPServer(EventMeshTCPServer eventMeshTCPServer) { + this.eventMeshTCPServer = eventMeshTCPServer; } - public ClientGroupWrapper getClientGroupWrapper(String groupName) { - return MapUtils.getObject(clientGroupMap, groupName, null); + public ClientGroupWrapper getClientGroupWrapper(String sysId) { + return MapUtils.getObject(clientGroupMap, sysId, null); } public Session getSession(ChannelHandlerContext ctx) { @@ -87,13 +92,13 @@ public Session createSession(UserAgent user, ChannelHandlerContext ctx) throws E user.setHost(addr.getHostString()); user.setPort(addr.getPort()); Session session = null; - if(!sessionTable.containsKey(addr)){ + if (!sessionTable.containsKey(addr)) { logger.info("createSession client[{}]", RemotingHelper.parseChannelRemoteAddr(ctx.channel())); - session = new Session(user, ctx, proxyTCPServer.getAccessConfiguration()); + session = new Session(user, ctx, eventMeshTCPServer.getEventMeshTCPConfiguration()); initClientGroupWrapper(user, session); sessionTable.put(addr, session); sessionLogger.info("session|open|succeed|user={}", user); - }else { + } else { session = sessionTable.get(addr); sessionLogger.error("session|open|failed|user={}|msg={}", user, "session has been created!"); } @@ -101,7 +106,7 @@ public Session createSession(UserAgent user, ChannelHandlerContext ctx) throws E } public void readySession(Session session) throws Exception { - if (!ProxyConstants.PURPOSE_SUB.equals(session.getClient().getPurpose())) { + if (!EventMeshConstants.PURPOSE_SUB.equals(session.getClient().getPurpose())) { throw new Exception("client purpose config is not sub"); } startClientGroupConsumer(session); @@ -141,7 +146,7 @@ private void closeSession(Session session) throws Exception { } //session must be synchronized to avoid SessionState be confound, for example adding subscribe when session closing - synchronized (session){ + synchronized (session) { if (SessionState.CLOSED == session.getSessionState()) { logger.info("session has been closed in sync, addr:{}", remoteAddress); @@ -150,11 +155,11 @@ private void closeSession(Session session) throws Exception { session.setSessionState(SessionState.CLOSED); - if (ProxyConstants.PURPOSE_SUB.equals(session.getClient().getPurpose())) { + if (EventMeshConstants.PURPOSE_SUB.equals(session.getClient().getPurpose())) { cleanClientGroupWrapperByCloseSub(session); - }else if (ProxyConstants.PURPOSE_PUB.equals(session.getClient().getPurpose())) { + } else if (EventMeshConstants.PURPOSE_PUB.equals(session.getClient().getPurpose())) { cleanClientGroupWrapperByClosePub(session); - }else{ + } else { logger.error("client purpose config is error:{}", session.getClient().getPurpose()); } @@ -171,29 +176,34 @@ public void operationComplete(ChannelFuture future) throws Exception { } } - private void initClientGroupWrapper(UserAgent user, Session session) throws Exception { - final String clientGroup = ProxyUtil.buildClientGroup(user.getSubsystem(), user.getDcn()); - if(!lockMap.containsKey(clientGroup)){ - Object obj = lockMap.putIfAbsent(clientGroup, new Object()); - if(obj == null) { - logger.info("add lock to map for group:{}", clientGroup); + private ClientGroupWrapper constructClientGroupWrapper(String sysId, String producerGroup, String consumerGroup, + EventMeshTCPServer eventMeshTCPServer, + DownstreamDispatchStrategy downstreamDispatchStrategy) { + return new ClientGroupWrapper(sysId, producerGroup, consumerGroup, eventMeshTCPServer, downstreamDispatchStrategy); + } + + private void initClientGroupWrapper(UserAgent user, Session session) throws Exception { + if (!lockMap.containsKey(user.getSubsystem())) { + Object obj = lockMap.putIfAbsent(user.getSubsystem(), new Object()); + if (obj == null) { + logger.info("add lock to map for subsystem:{}", user.getSubsystem()); } } - synchronized (lockMap.get(clientGroup)) { - if (!clientGroupMap.containsKey(clientGroup)) { - ClientGroupWrapper cgw = new ClientGroupWrapper(user.getSubsystem(), user.getDcn() - , proxyTCPServer, new FreePriorityDispatchStrategy()); - clientGroupMap.put(clientGroup, cgw); - logger.info("create new ClientGroupWrapper,group:{}", clientGroup); + synchronized (lockMap.get(user.getSubsystem())) { + if (!clientGroupMap.containsKey(user.getSubsystem())) { + ClientGroupWrapper cgw = constructClientGroupWrapper(user.getSubsystem(), user.getProducerGroup(), + user.getConsumerGroup(), eventMeshTCPServer, new FreePriorityDispatchStrategy()); + clientGroupMap.put(user.getSubsystem(), cgw); + logger.info("create new ClientGroupWrapper, subsystem:{}", user.getSubsystem()); } - ClientGroupWrapper cgw = clientGroupMap.get(clientGroup); + ClientGroupWrapper cgw = clientGroupMap.get(user.getSubsystem()); - if (ProxyConstants.PURPOSE_PUB.equals(user.getPurpose())){ + if (EventMeshConstants.PURPOSE_PUB.equals(user.getPurpose())) { startClientGroupProducer(cgw, session); - }else if (ProxyConstants.PURPOSE_SUB.equals(user.getPurpose())) { + } else if (EventMeshConstants.PURPOSE_SUB.equals(user.getPurpose())) { initClientGroupConsumser(cgw); - }else{ + } else { logger.error("unknown client purpose:{}", user.getPurpose()); throw new Exception("client purpose config is error"); } @@ -207,7 +217,7 @@ private void startClientGroupProducer(ClientGroupWrapper cgw, Session session) t cgw.startClientGroupProducer(); } boolean flag = cgw.addGroupProducerSession(session); - if(!flag){ + if (!flag) { throw new Exception("addGroupProducerSession fail"); } session.setSessionState(SessionState.RUNNING); @@ -228,16 +238,15 @@ private void initClientGroupConsumser(ClientGroupWrapper cgw) throws Exception { } private void startClientGroupConsumer(Session session) throws Exception { - final String clientGroup = ProxyUtil.buildClientGroup(session.getClient().getSubsystem(), session.getClient().getDcn()); - if(!lockMap.containsKey(clientGroup)){ - lockMap.putIfAbsent(clientGroup, new Object()); + if (!lockMap.containsKey(session.getClient().getSubsystem())) { + lockMap.putIfAbsent(session.getClient().getSubsystem(), new Object()); } - synchronized (lockMap.get(clientGroup)) { + synchronized (lockMap.get(session.getClient().getSubsystem())) { logger.info("readySession session[{}]", session); ClientGroupWrapper cgw = session.getClientGroupWrapper().get(); boolean flag = cgw.addGroupConsumerSession(session); - if(!flag){ + if (!flag) { throw new Exception("addGroupConsumerSession fail"); } @@ -269,10 +278,10 @@ private void cleanClientGroupWrapperByClosePub(Session session) throws Exception * @param session */ private void cleanSubscriptionInSession(Session session) throws Exception { - for (String topic : session.getSessionContext().subscribeTopics.values()) { - session.getClientGroupWrapper().get().removeSubscription(topic, session); - if (!session.getClientGroupWrapper().get().hasSubscription(topic)) { - session.getClientGroupWrapper().get().unsubscribe(topic); + for (SubscriptionItem item : session.getSessionContext().subscribeTopics.values()) { + session.getClientGroupWrapper().get().removeSubscription(item.getTopic(), session); + if (!session.getClientGroupWrapper().get().hasSubscription(item.getTopic())) { + session.getClientGroupWrapper().get().unsubscribe(item); } } } @@ -282,22 +291,25 @@ private void cleanSubscriptionInSession(Session session) throws Exception { * * @param session */ - private void handleUnackMsgsInSession(Session session){ - ConcurrentHashMap unAckMsg = session.getPusher().getPushContext().getUnAckMsg(); - if(unAckMsg.size() > 0 && session.getClientGroupWrapper().get().getGroupConsumerSessions().size() > 0){ - for(Map.Entry entry : unAckMsg.entrySet()){ - ClientAckContext ackContext = entry.getValue(); - if(ProxyUtil.isBroadcast(ackContext.getMsgs().get(0).getTopic())){ - logger.warn("exist broadcast msg unack when closeSession,seq:{},bizSeq:{},client:{}",ackContext.getSeq(),ProxyUtil.getMessageBizSeq(ackContext.getMsgs().get(0)),session.getClient()); + private void handleUnackMsgsInSession(Session session) { + ConcurrentHashMap unAckMsg = session.getPusher().getUnAckMsg(); + if (unAckMsg.size() > 0 && session.getClientGroupWrapper().get().getGroupConsumerSessions().size() > 0) { + for (Map.Entry entry : unAckMsg.entrySet()) { + DownStreamMsgContext downStreamMsgContext = entry.getValue(); + if (SubscriptionMode.BROADCASTING.equals(downStreamMsgContext.subscriptionItem.getMode())) { + logger.warn("exist broadcast msg unack when closeSession,seq:{},bizSeq:{},client:{}", downStreamMsgContext.seq, EventMeshUtil.getMessageBizSeq(downStreamMsgContext.msgExt), session.getClient()); continue; } - List list = new ArrayList(session.getClientGroupWrapper().get().getGroupConsumerSessions()); - Collections.shuffle(list); - DownStreamMsgContext downStreamMsgContext= new DownStreamMsgContext(ackContext.getMsgs().get(0),list.get(0),ackContext.getConsumer(), ackContext.getContext(), false); - - downStreamMsgContext.delay(0L); - proxyTCPServer.getProxyTcpRetryer().pushRetry(downStreamMsgContext); - logger.warn("rePush msg form unAckMsgs,seq:{},rePushSeq:{},rePushClient:{}",entry.getKey(), downStreamMsgContext.seq, downStreamMsgContext.session.getClient()); + Session reChooseSession = session.getClientGroupWrapper().get().getDownstreamDispatchStrategy().select(session.getClientGroupWrapper().get().getConsumerGroup() + , downStreamMsgContext.msgExt.getTopic() + , session.getClientGroupWrapper().get().groupConsumerSessions); + if(reChooseSession != null){ + downStreamMsgContext.session = reChooseSession; + reChooseSession.downstreamMsg(downStreamMsgContext); + logger.info("rePush msg form unAckMsgs,seq:{},rePushClient:{}", entry.getKey(), downStreamMsgContext.session.getClient()); + }else{ + logger.warn("select session fail in handleUnackMsgsInSession,seq:{},topic:{}", entry.getKey(), downStreamMsgContext.msgExt.getTopic()); + } } } } @@ -313,9 +325,9 @@ private void cleanClientGroupWrapperCommon(Session session) throws Exception { && (session.getClientGroupWrapper().get().getGroupProducerSessions().size() == 0)) { shutdownClientGroupProducer(session); - clientGroupMap.remove(session.getClientGroupWrapper().get().getGroupName()); - lockMap.remove(session.getClientGroupWrapper().get().getGroupName()); - logger.info("remove clientGroupWrapper group[{}]", session.getClientGroupWrapper().get().getGroupName()); + clientGroupMap.remove(session.getClientGroupWrapper().get().getSysId()); + lockMap.remove(session.getClientGroupWrapper().get().getSysId()); + logger.info("remove clientGroupWrapper subsystem[{}]", session.getClientGroupWrapper().get().getSysId()); } } @@ -337,13 +349,13 @@ private void shutdownClientGroupProducer(Session session) throws Exception { } private void initSessionCleaner() { - ProxyTCPServer.scheduler.scheduleAtFixedRate(new Runnable() { + eventMeshTCPServer.getScheduler().scheduleAtFixedRate(new Runnable() { @Override public void run() { Iterator sessionIterator = sessionTable.values().iterator(); while (sessionIterator.hasNext()) { Session tmp = sessionIterator.next(); - if (System.currentTimeMillis() - tmp.getLastHeartbeatTime() > proxyTCPServer.getAccessConfiguration().proxyTcpSessionExpiredInMills) { + if (System.currentTimeMillis() - tmp.getLastHeartbeatTime() > eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshTcpSessionExpiredInMills) { try { logger.warn("clean expired session,client:{}", tmp.getClient()); closeSession(tmp.getContext()); @@ -353,47 +365,28 @@ public void run() { } } } - }, 1000, proxyTCPServer.getAccessConfiguration().proxyTcpSessionExpiredInMills, TimeUnit.MILLISECONDS); + }, 1000, eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshTcpSessionExpiredInMills, TimeUnit.MILLISECONDS); } - private void initSessionAckContextCleaner() { - proxyTCPServer.scheduler.scheduleAtFixedRate(new Runnable() { + private void initDownStreamMsgContextCleaner() { + eventMeshTCPServer.getScheduler().scheduleAtFixedRate(new Runnable() { @Override public void run() { + + //scan non-broadcast msg Iterator sessionIterator = sessionTable.values().iterator(); while (sessionIterator.hasNext()) { Session tmp = sessionIterator.next(); - for (Map.Entry entry : tmp.getPusher().getPushContext().getUnAckMsg().entrySet()) { + for (Map.Entry entry : tmp.getPusher().getUnAckMsg().entrySet()) { String seqKey = entry.getKey(); - ClientAckContext clientAckContext = entry.getValue(); - if (!clientAckContext.isExpire()) { - continue; - } - tmp.getPusher().getPushContext().ackMsg(seqKey); - tmp.getPusher().getPushContext().getUnAckMsg().remove(seqKey); - logger.warn("remove expire clientAckContext, session:{}, topic:{}, seq:{}", tmp, clientAckContext.getMsgs().get(0).getTopic(), seqKey); - } - } - } - }, 1000, 5 * 1000, TimeUnit.MILLISECONDS); - } - - private void initDownStreamMsgContextCleaner() { - proxyTCPServer.scheduler.scheduleAtFixedRate(new Runnable() { - @Override - public void run() { - Iterator cgwIterator = clientGroupMap.values().iterator(); - while (cgwIterator.hasNext()) { - ClientGroupWrapper cgw = cgwIterator.next(); - for (Map.Entry entry : cgw.getDownstreamMap().entrySet()) { - String seq = entry.getKey(); DownStreamMsgContext downStreamMsgContext = entry.getValue(); if (!downStreamMsgContext.isExpire()) { continue; } - cgw.getDownstreamMap().get(seq).ackMsg(); - cgw.getDownstreamMap().remove(seq); - logger.warn("remove expire DownStreamMsgContext,group:{}, topic:{}, seq:{}", cgw.getGroupName(), downStreamMsgContext.msgExt.getTopic(), seq); + downStreamMsgContext.ackMsg(); + tmp.getPusher().getUnAckMsg().remove(seqKey); + logger.warn("remove expire downStreamMsgContext, session:{}, topic:{}, seq:{}", tmp, + downStreamMsgContext.msgExt.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION), seqKey); } } } @@ -403,7 +396,6 @@ public void run() { public void init() throws Exception { initSessionCleaner(); - initSessionAckContextCleaner(); initDownStreamMsgContextCleaner(); logger.info("ClientSessionGroupMapping inited......"); } @@ -416,7 +408,7 @@ public void shutdown() throws Exception { logger.info("begin to close sessions gracefully"); sessionTable.values().parallelStream().forEach(itr -> { try { - ProxyTcp2Client.serverGoodby2Client(itr, this); + EventMeshTcp2Client.serverGoodby2Client(this.eventMeshTCPServer,itr, this); } catch (Exception e) { logger.error("say goodbye to session error! {}", itr, e); } @@ -433,48 +425,15 @@ public ConcurrentHashMap getClientGroupMap() { return clientGroupMap; } - public HashMap statDCNSystemInfo() { - HashMap result = new HashMap(); - if (!sessionTable.isEmpty()) { - for (Session session : sessionTable.values()) { - String key = session.getClient().getDcn() + "|" + session.getClient().getSubsystem(); - if (!result.containsKey(key)) { - result.put(key, new AtomicInteger(1)); - } else { - result.get(key).incrementAndGet(); - } - } - } - return result; - } - - public HashMap statDCNSystemInfoByPurpose(String purpose) { - HashMap result = new HashMap(); - if (!sessionTable.isEmpty()) { - for (Session session : sessionTable.values()) { - if(!StringUtils.equals(session.getClient().getPurpose(), purpose)) - continue; - - String key = session.getClient().getDcn() + "|" + session.getClient().getSubsystem()+ "|" + purpose; - if (!result.containsKey(key)) { - result.put(key, new AtomicInteger(1)); - } else { - result.get(key).incrementAndGet(); - } - } - } - return result; - } - - public Map> prepareProxyClientDistributionData(){ + public Map> prepareEventMeshClientDistributionData() { Map> result = null; - if(!clientGroupMap.isEmpty()){ + if (!clientGroupMap.isEmpty()) { result = new HashMap<>(); - for(Map.Entry entry : clientGroupMap.entrySet()){ - Map map = new HashMap(); - map.put(ProxyConstants.PURPOSE_SUB,entry.getValue().getGroupConsumerSessions().size()); - map.put(ProxyConstants.PURPOSE_PUB,entry.getValue().getGroupProducerSessions().size()); + for (Map.Entry entry : clientGroupMap.entrySet()) { + Map map = new HashMap<>(); + map.put(EventMeshConstants.PURPOSE_SUB, entry.getValue().getGroupConsumerSessions().size()); + map.put(EventMeshConstants.PURPOSE_PUB, entry.getValue().getGroupProducerSessions().size()); result.put(entry.getKey(), map); } } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/group/dispatch/DownstreamDispatchStrategy.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/group/dispatch/DownstreamDispatchStrategy.java similarity index 86% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/group/dispatch/DownstreamDispatchStrategy.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/group/dispatch/DownstreamDispatchStrategy.java index f12570b416..27169ff47a 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/group/dispatch/DownstreamDispatchStrategy.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/group/dispatch/DownstreamDispatchStrategy.java @@ -15,16 +15,17 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client.group.dispatch; +package org.apache.eventmesh.runtime.core.protocol.tcp.client.group.dispatch; -import com.webank.emesher.core.protocol.tcp.client.session.Session; - import java.util.Set; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session; + public interface DownstreamDispatchStrategy { /** * 选择一个SESSION + * * @param group * @param consumeSessions * @return diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/group/dispatch/FreePriorityDispatchStrategy.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/group/dispatch/FreePriorityDispatchStrategy.java similarity index 73% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/group/dispatch/FreePriorityDispatchStrategy.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/group/dispatch/FreePriorityDispatchStrategy.java index 2d44cc6fb6..4e9e4d5936 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/group/dispatch/FreePriorityDispatchStrategy.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/group/dispatch/FreePriorityDispatchStrategy.java @@ -15,51 +15,52 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client.group.dispatch; - -import com.webank.emesher.core.protocol.tcp.client.session.Session; -import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +package org.apache.eventmesh.runtime.core.protocol.tcp.client.group.dispatch; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Set; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class FreePriorityDispatchStrategy implements DownstreamDispatchStrategy { private static final Logger logger = LoggerFactory.getLogger(FreePriorityDispatchStrategy.class); @Override public Session select(String group, String topic, Set groupConsumerSessions) { - if(CollectionUtils.isEmpty(groupConsumerSessions) + if (CollectionUtils.isEmpty(groupConsumerSessions) || StringUtils.isBlank(topic) || StringUtils.isBlank(group)) { return null; } List filtered = new ArrayList(); - List canDownSessions = new ArrayList<>(); + List isolatedSessions = new ArrayList<>(); for (Session session : groupConsumerSessions) { if (!session.isAvailable(topic)) { continue; } - if(session.isDownStreamBusy()){ - canDownSessions.add(session); + if (session.isIsolated()) { + isolatedSessions.add(session); + logger.info("session is not available because session is isolated,isolateTime:{},client:{}",session.getIsolateTime(), session.getClient()); continue; } filtered.add(session); } if (CollectionUtils.isEmpty(filtered)) { - if(CollectionUtils.isEmpty(canDownSessions)){ + if (CollectionUtils.isEmpty(isolatedSessions)) { logger.warn("all sessions can't downstream msg"); return null; - }else{ - logger.warn("all sessions are busy,group:{},topic:{}",group,topic); - filtered.addAll(canDownSessions); + } else { + logger.warn("all sessions are isolated,group:{},topic:{}", group, topic); + filtered.addAll(isolatedSessions); } } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/Session.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/Session.java similarity index 68% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/Session.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/Session.java index 929c516c3a..ddfced04fc 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/Session.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/Session.java @@ -15,36 +15,37 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client.session; - -import com.webank.emesher.configuration.AccessConfiguration; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.protocol.tcp.client.group.ClientGroupWrapper; -import com.webank.emesher.core.protocol.tcp.client.session.push.DownStreamMsgContext; -import com.webank.emesher.core.protocol.tcp.client.session.push.SessionPusher; -import com.webank.emesher.core.protocol.tcp.client.session.send.ProxyTcpSendResult; -import com.webank.emesher.core.protocol.tcp.client.session.send.SessionSender; -import com.webank.eventmesh.common.protocol.tcp.Header; -import com.webank.eventmesh.common.protocol.tcp.OPStatus; -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; -import com.webank.emesher.util.Utils; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; -import io.netty.channel.ChannelHandlerContext; -import org.apache.commons.lang3.time.DateFormatUtils; -import org.apache.rocketmq.client.producer.SendCallback; -import org.apache.rocketmq.common.message.Message; -import org.apache.rocketmq.remoting.common.RemotingHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +package org.apache.eventmesh.runtime.core.protocol.tcp.client.session; + +import static org.apache.eventmesh.common.protocol.tcp.Command.LISTEN_RESPONSE; import java.lang.ref.WeakReference; import java.net.InetSocketAddress; import java.util.List; import java.util.concurrent.locks.ReentrantLock; -import static com.webank.eventmesh.common.protocol.tcp.Command.LISTEN_RESPONSE; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelHandlerContext; +import io.openmessaging.api.Message; +import io.openmessaging.api.SendCallback; + +import org.apache.commons.lang3.time.DateFormatUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.SubscriptionItem; +import org.apache.eventmesh.common.protocol.tcp.*; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.runtime.configuration.EventMeshTCPConfiguration; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.group.ClientGroupWrapper; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push.DownStreamMsgContext; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push.SessionPusher; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.send.EventMeshTcpSendResult; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.send.SessionSender; +import org.apache.eventmesh.runtime.util.RemotingHelper; +import org.apache.eventmesh.runtime.util.Utils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class Session { @@ -62,7 +63,7 @@ public class Session { private WeakReference clientGroupWrapper; - private AccessConfiguration accessConfiguration; + private EventMeshTCPConfiguration eventMeshTCPConfiguration; private SessionPusher pusher; @@ -160,34 +161,33 @@ public void setListenRequestSeq(String listenRequestSeq) { this.listenRequestSeq = listenRequestSeq; } - public void subscribe(List topics) throws Exception { - for (String topic : topics) { - sessionContext.subscribeTopics.putIfAbsent(topic, topic); - clientGroupWrapper.get().subscribe(topic); + public void subscribe(List items) throws Exception { + for (SubscriptionItem item : items) { + sessionContext.subscribeTopics.putIfAbsent(item.getTopic(), item); + clientGroupWrapper.get().subscribe(item); - clientGroupWrapper.get().getMqProducerWrapper().getDefaultMQProducer().getDefaultMQProducerImpl() - .getmQClientFactory().getMQClientAPIImpl().getDefaultTopicRouteInfoFromNameServer(topic, - ProxyConstants.DEFAULT_TIME_OUT_MILLS); + clientGroupWrapper.get().getMqProducerWrapper().getMeshMQProducer().checkTopicExist(item.getTopic()); - clientGroupWrapper.get().addSubscription(topic, this); - subscribeLogger.info("subscribe|succeed|topic={}|user={}", topic, client); + clientGroupWrapper.get().addSubscription(item.getTopic(), this); + subscribeLogger.info("subscribe|succeed|topic={}|user={}", item.getTopic(), client); } } - public void unsubscribe(List topics) throws Exception { - for (String topic : topics) { - sessionContext.subscribeTopics.remove(topic); - clientGroupWrapper.get().removeSubscription(topic, this); + public void unsubscribe(List items) throws Exception { + for (SubscriptionItem item : items) { + sessionContext.subscribeTopics.remove(item.getTopic()); + clientGroupWrapper.get().removeSubscription(item.getTopic(), this); - if (!clientGroupWrapper.get().hasSubscription(topic)) { - clientGroupWrapper.get().unsubscribe(topic); - subscribeLogger.info("unSubscribe|succeed|topic={}|lastUser={}", topic, client); + if (!clientGroupWrapper.get().hasSubscription(item.getTopic())) { + clientGroupWrapper.get().unsubscribe(item); + subscribeLogger.info("unSubscribe|succeed|topic={}|lastUser={}", item.getTopic(), client); } } } - public ProxyTcpSendResult upstreamMsg(Header header, Message msg, SendCallback sendCallback, long startTime, long taskExecuteTime) { - sessionContext.sendTopics.putIfAbsent(msg.getTopic(), msg.getTopic()); + public EventMeshTcpSendResult upstreamMsg(Header header, Message msg, SendCallback sendCallback, long startTime, long taskExecuteTime) { + String topic = msg.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION); + sessionContext.sendTopics.putIfAbsent(topic, topic); return sender.send(header, msg, sendCallback, startTime, taskExecuteTime); } @@ -198,15 +198,7 @@ public void downstreamMsg(DownStreamMsgContext downStreamMsgContext) { pusher.push(downStreamMsgContext); } - public boolean isDownStreamBusy() { - return pusher.isBusy(); - } - - public boolean isCanDownStream() { - return pusher.isCanDownStream(); - } - - public boolean isIsolated(){ + public boolean isIsolated() { return System.currentTimeMillis() < isolateTime; } @@ -222,8 +214,8 @@ public void write2Client(final Package pkg) { public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { messageLogger.error("write2Client fail, pkg[{}] session[{}]", pkg, this); - }else{ - clientGroupWrapper.get().getProxyTcpMonitor().getProxy2clientMsgNum().incrementAndGet(); + } else { + clientGroupWrapper.get().getEventMeshTcpMonitor().getEventMesh2clientMsgNum().incrementAndGet(); } } } @@ -233,29 +225,18 @@ public void operationComplete(ChannelFuture future) throws Exception { } } - /** - * ACK MSG - * - * @param seq - */ - public void ackMsg(String seq) { - logger.info("ackMsg start,seq:{}", seq); - pusher.getPushContext().ackMsg(seq); - logger.info("ackMsg end,seq:{}", seq); - } - @Override public String toString() { return "Session{" + - "group=" + clientGroupWrapper.get().getGroupName() + + "sysId=" + clientGroupWrapper.get().getSysId() + ",remoteAddr=" + RemotingHelper.parseSocketAddressAddr(remoteAddress) + ",client=" + client + ",sessionState=" + sessionState + ",sessionContext=" + sessionContext + ",pusher=" + pusher + ",sender=" + sender + - ",createTime=" + DateFormatUtils.format(createTime, ProxyConstants.DATE_FORMAT) + - ",lastHeartbeatTime=" + DateFormatUtils.format(lastHeartbeatTime, ProxyConstants.DATE_FORMAT) + '}'; + ",createTime=" + DateFormatUtils.format(createTime, EventMeshConstants.DATE_FORMAT) + + ",lastHeartbeatTime=" + DateFormatUtils.format(lastHeartbeatTime, EventMeshConstants.DATE_FORMAT) + '}'; } @Override @@ -287,21 +268,21 @@ public void setClientGroupWrapper(WeakReference clientGroupW this.clientGroupWrapper = clientGroupWrapper; } - public Session(UserAgent client, ChannelHandlerContext context, AccessConfiguration accessConfiguration) { + public Session(UserAgent client, ChannelHandlerContext context, EventMeshTCPConfiguration eventMeshTCPConfiguration) { this.client = client; this.context = context; - this.accessConfiguration = accessConfiguration; + this.eventMeshTCPConfiguration = eventMeshTCPConfiguration; this.remoteAddress = (InetSocketAddress) context.channel().remoteAddress(); this.sender = new SessionSender(this); this.pusher = new SessionPusher(this); } - public AccessConfiguration getAccessConfiguration() { - return accessConfiguration; + public EventMeshTCPConfiguration getEventMeshTCPConfiguration() { + return eventMeshTCPConfiguration; } - public void setAccessConfiguration(AccessConfiguration accessConfiguration) { - this.accessConfiguration = accessConfiguration; + public void setEventMeshTCPConfiguration(EventMeshTCPConfiguration eventMeshTCPConfiguration) { + this.eventMeshTCPConfiguration = eventMeshTCPConfiguration; } public void trySendListenResponse(Header header, long startTime, long taskExecuteTime) { @@ -331,20 +312,24 @@ public void setIsolateTime(long isolateTime) { this.isolateTime = isolateTime; } - public boolean isAvailable(String topic){ - if(SessionState.CLOSED == sessionState){ - logger.warn("session is not available because session has been closed"); + public boolean isAvailable(String topic) { + if (SessionState.CLOSED == sessionState) { + logger.warn("session is not available because session has been closed,topic:{},client:{}", topic, client); return false; } - if(!sessionContext.subscribeTopics.containsKey(topic)){ - logger.warn("session is not available because session has not subscribe topic:{}",topic); - return false; - } - if(isIsolated()){ - logger.warn("session is not available because session is isolated,isolateTime:{}",isolateTime); + if (!sessionContext.subscribeTopics.containsKey(topic)) { + logger.warn("session is not available because session has not subscribe topic:{},client:{}", topic,client); return false; } + return true; } + +// @Override +// public int hashCode() { +// int code = 37 + (client != null ? client.hashCode() : 0) + (context != null ? context.hashCode() : 0) +// + (sessionState != null ? sessionState.hashCode() : 0); +// return code; +// } } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/SessionContext.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/SessionContext.java similarity index 79% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/SessionContext.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/SessionContext.java index 9a5f4dcc84..e8f851baec 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/SessionContext.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/SessionContext.java @@ -15,20 +15,21 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client.session; - -import com.webank.emesher.constants.ProxyConstants; -import org.apache.commons.lang3.time.DateFormatUtils; +package org.apache.eventmesh.runtime.core.protocol.tcp.client.session; import java.util.concurrent.ConcurrentHashMap; +import org.apache.commons.lang3.time.DateFormatUtils; +import org.apache.eventmesh.common.protocol.SubscriptionItem; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; + public class SessionContext { private Session session; public ConcurrentHashMap sendTopics = new ConcurrentHashMap(); - public ConcurrentHashMap subscribeTopics = new ConcurrentHashMap(); + public ConcurrentHashMap subscribeTopics = new ConcurrentHashMap(); public long createTime = System.currentTimeMillis(); @@ -38,8 +39,8 @@ public SessionContext(Session session) { @Override public String toString() { - return "SessionContext{subscribeTopics=" + subscribeTopics.keySet() + return "SessionContext{subscribeTopics=" + subscribeTopics + ",sendTopics=" + sendTopics.keySet() - + ",createTime=" + DateFormatUtils.format(createTime, ProxyConstants.DATE_FORMAT) + "}"; + + ",createTime=" + DateFormatUtils.format(createTime, EventMeshConstants.DATE_FORMAT) + "}"; } } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/SessionState.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/SessionState.java similarity index 92% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/SessionState.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/SessionState.java index 9d0ef17a5a..2093a8e4e0 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/SessionState.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/SessionState.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client.session; +package org.apache.eventmesh.runtime.core.protocol.tcp.client.session; public enum SessionState { CREATED, diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/push/ClientAckContext.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/push/ClientAckContext.java similarity index 68% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/push/ClientAckContext.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/push/ClientAckContext.java index cd7ca2481f..015fb467a6 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/push/ClientAckContext.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/push/ClientAckContext.java @@ -15,47 +15,48 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client.session.push; +package org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push; + +import java.util.List; + +import io.openmessaging.api.Message; -import com.webank.defibus.common.DeFiBusConstant; -import com.webank.defibus.consumer.DeFiBusPushConsumer; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.plugin.MQConsumerWrapper; -import com.webank.emesher.util.ProxyUtil; import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.DateFormatUtils; -import com.webank.emesher.patch.ProxyConsumeConcurrentlyContext; -import org.apache.rocketmq.client.impl.consumer.ConsumeMessageConcurrentlyService; -import org.apache.rocketmq.client.impl.consumer.ConsumeMessageService; -import org.apache.rocketmq.common.message.MessageExt; +import org.apache.eventmesh.api.AbstractContext; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.plugin.MQConsumerWrapper; +import org.apache.eventmesh.runtime.util.EventMeshUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.List; - public class ClientAckContext { private final Logger logger = LoggerFactory.getLogger(this.getClass()); private String seq; - private ProxyConsumeConcurrentlyContext context; + private AbstractContext context; private long createTime; private long expireTime; - private List msgs; + private List msgs; private MQConsumerWrapper consumer; - public ClientAckContext(String seq, ProxyConsumeConcurrentlyContext context, List msgs, MQConsumerWrapper consumer) { + public ClientAckContext(String seq, AbstractContext context, List msgs, MQConsumerWrapper consumer) { this.seq = seq; this.context = context; this.msgs = msgs; this.consumer = consumer; this.createTime = System.currentTimeMillis(); - this.expireTime = System.currentTimeMillis() + Long.valueOf(msgs.get(0).getProperty(DeFiBusConstant.PROPERTY_MESSAGE_TTL)); + String ttlStr = msgs.get(0).getUserProperties(EventMeshConstants.PROPERTY_MESSAGE_TTL); + long ttl = StringUtils.isNumeric(ttlStr)? Long.parseLong(ttlStr) : EventMeshConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS; + this.expireTime = System.currentTimeMillis() + ttl; } public boolean isExpire() { @@ -70,11 +71,11 @@ public void setSeq(String seq) { this.seq = seq; } - public ProxyConsumeConcurrentlyContext getContext() { + public AbstractContext getContext() { return context; } - public void setContext(ProxyConsumeConcurrentlyContext context) { + public void setContext(AbstractContext context) { this.context = context; } @@ -86,11 +87,11 @@ public void setCreateTime(long createTime) { this.createTime = createTime; } - public List getMsgs() { + public List getMsgs() { return msgs; } - public void setMsgs(List msgs) { + public void setMsgs(List msgs) { this.msgs = msgs; } @@ -111,9 +112,9 @@ public void ackMsg() { consumer.updateOffset(msgs, context); // ConsumeMessageService consumeMessageService = consumer..getDefaultMQPushConsumerImpl().getConsumeMessageService(); // ((ConsumeMessageConcurrentlyService)consumeMessageService).updateOffset(msgs, context); - logger.info("ackMsg topic:{}, bizSeq:{}", msgs.get(0).getTopic(), ProxyUtil.getMessageBizSeq(msgs.get(0))); - }else{ - logger.warn("ackMsg failed,consumer is null:{}, context is null:{} , msgs is null:{}",consumer == null, context == null, msgs == null); + logger.info("ackMsg topic:{}, bizSeq:{}", msgs.get(0).getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION), EventMeshUtil.getMessageBizSeq(msgs.get(0))); + } else { + logger.warn("ackMsg failed,consumer is null:{}, context is null:{} , msgs is null:{}", consumer == null, context == null, msgs == null); } } @@ -123,8 +124,8 @@ public String toString() { ",seq=" + seq + // TODO ",consumer=" + consumer.getDefaultMQPushConsumer().getMessageModel() + // ",consumerGroup=" + consumer.getDefaultMQPushConsumer().getConsumerGroup() + - ",topic=" + (CollectionUtils.size(msgs) > 0 ? msgs.get(0).getTopic() : null) + - ",createTime=" + DateFormatUtils.format(createTime, ProxyConstants.DATE_FORMAT) + - ",expireTime=" + DateFormatUtils.format(expireTime, ProxyConstants.DATE_FORMAT) + '}'; + ",topic=" + (CollectionUtils.size(msgs) > 0 ? msgs.get(0).getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION) : null) + + ",createTime=" + DateFormatUtils.format(createTime, EventMeshConstants.DATE_FORMAT) + + ",expireTime=" + DateFormatUtils.format(expireTime, EventMeshConstants.DATE_FORMAT) + '}'; } } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/push/DownStreamMsgContext.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/push/DownStreamMsgContext.java similarity index 61% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/push/DownStreamMsgContext.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/push/DownStreamMsgContext.java index 6052daf7b0..8e441bd5aa 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/push/DownStreamMsgContext.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/push/DownStreamMsgContext.java @@ -15,40 +15,46 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client.session.push; - -import com.webank.defibus.common.DeFiBusConstant; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.plugin.MQConsumerWrapper; -import com.webank.emesher.core.protocol.tcp.client.session.Session; -import com.webank.emesher.util.ServerGlobal; -import org.apache.commons.lang3.time.DateFormatUtils; -import com.webank.emesher.patch.ProxyConsumeConcurrentlyContext; -import org.apache.rocketmq.common.message.MessageExt; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +package org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Delayed; import java.util.concurrent.TimeUnit; +import io.openmessaging.api.Message; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.time.DateFormatUtils; +import org.apache.eventmesh.api.AbstractContext; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.SubscriptionItem; +import org.apache.eventmesh.common.protocol.SubscriptionMode; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.plugin.MQConsumerWrapper; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session; +import org.apache.eventmesh.runtime.util.ServerGlobal; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class DownStreamMsgContext implements Delayed { private final Logger logger = LoggerFactory.getLogger(this.getClass()); public String seq; - public MessageExt msgExt; + public Message msgExt; public Session session; - public ProxyConsumeConcurrentlyContext consumeConcurrentlyContext; + public AbstractContext consumeConcurrentlyContext; public MQConsumerWrapper consumer; public int retryTimes; + public SubscriptionItem subscriptionItem; + private long executeTime; public long lastPushTime; @@ -57,9 +63,9 @@ public class DownStreamMsgContext implements Delayed { private long expireTime; - public boolean msgFromOtherProxy; + public boolean msgFromOtherEventMesh; - public DownStreamMsgContext(MessageExt msgExt, Session session, MQConsumerWrapper consumer, ProxyConsumeConcurrentlyContext consumeConcurrentlyContext, boolean msgFromOtherProxy) { + public DownStreamMsgContext(Message msgExt, Session session, MQConsumerWrapper consumer, AbstractContext consumeConcurrentlyContext, boolean msgFromOtherEventMesh, SubscriptionItem subscriptionItem) { this.seq = String.valueOf(ServerGlobal.getInstance().getMsgCounter().incrementAndGet()); this.msgExt = msgExt; this.session = session; @@ -69,8 +75,11 @@ public DownStreamMsgContext(MessageExt msgExt, Session session, MQConsumerWrappe this.lastPushTime = System.currentTimeMillis(); this.executeTime = System.currentTimeMillis(); this.createTime = System.currentTimeMillis(); - this.expireTime = System.currentTimeMillis() + Long.valueOf(msgExt.getProperty(DeFiBusConstant.PROPERTY_MESSAGE_TTL)); - this.msgFromOtherProxy = msgFromOtherProxy; + this.subscriptionItem = subscriptionItem; + String ttlStr = msgExt.getUserProperties("TTL"); + long ttl = StringUtils.isNumeric(ttlStr) ? Long.parseLong(ttlStr) : EventMeshConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS; + this.expireTime = System.currentTimeMillis() + ttl; + this.msgFromOtherEventMesh = msgFromOtherEventMesh; } public boolean isExpire() { @@ -79,14 +88,15 @@ public boolean isExpire() { public void ackMsg() { if (consumer != null && consumeConcurrentlyContext != null && msgExt != null) { - List msgs = new ArrayList(); + List msgs = new ArrayList(); msgs.add(msgExt); consumer.updateOffset(msgs, consumeConcurrentlyContext); // ConsumeMessageService consumeMessageService = consumer.getDefaultMQPushConsumer().getDefaultMQPushConsumerImpl().getConsumeMessageService(); // ((ConsumeMessageConcurrentlyService)consumeMessageService).updateOffset(msgs, consumeConcurrentlyContext); - logger.info("ackMsg topic:{}, bizSeq:{}", msgs.get(0).getTopic(), msgs.get(0).getKeys()); - }else{ - logger.warn("ackMsg failed,consumer is null:{}, context is null:{} , msgs is null:{}",consumer == null, consumeConcurrentlyContext == null, msgExt == null); + logger.info("ackMsg seq:{}, topic:{}, bizSeq:{}", seq, msgs.get(0).getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION), + msgs.get(0).getSystemProperties(EventMeshConstants.PROPERTY_MESSAGE_KEYS)); + } else { + logger.warn("ackMsg seq:{} failed,consumer is null:{}, context is null:{} , msgs is null:{}", seq, consumer == null, consumeConcurrentlyContext == null, msgExt == null); } } @@ -98,14 +108,15 @@ public void delay(long delay) { public String toString() { return "DownStreamMsgContext{" + ",seq=" + seq + - ",client=" + session.getClient() + + ",client=" + (session == null ? null : session.getClient()) + ",retryTimes=" + retryTimes + ",consumer=" + consumer + // todo ",consumerGroup=" + consumer.getClass().getConsumerGroup() + - ",topic=" + msgExt.getTopic() + - ",createTime=" + DateFormatUtils.format(createTime, ProxyConstants.DATE_FORMAT) + - ",executeTime=" + DateFormatUtils.format(executeTime, ProxyConstants.DATE_FORMAT) + - ",lastPushTime=" + DateFormatUtils.format(lastPushTime, ProxyConstants.DATE_FORMAT) + '}'; + ",topic=" + msgExt.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION) + + ",subscriptionItem=" + subscriptionItem + + ",createTime=" + DateFormatUtils.format(createTime, EventMeshConstants.DATE_FORMAT) + + ",executeTime=" + DateFormatUtils.format(executeTime, EventMeshConstants.DATE_FORMAT) + + ",lastPushTime=" + DateFormatUtils.format(lastPushTime, EventMeshConstants.DATE_FORMAT) + '}'; } @Override diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/push/PushContext.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/push/PushContext.java similarity index 63% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/push/PushContext.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/push/PushContext.java index 288dea372f..d236e5b72c 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/push/PushContext.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/push/PushContext.java @@ -15,22 +15,16 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client.session.push; +package org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push; -import com.webank.defibus.consumer.DeFiBusPushConsumer; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.plugin.MQConsumerWrapper; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.time.DateFormatUtils; -import com.webank.emesher.patch.ProxyConsumeConcurrentlyContext; -import org.apache.rocketmq.common.message.MessageExt; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.List; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.AtomicLong; - public class PushContext { public Logger logger = LoggerFactory.getLogger(this.getClass()); @@ -39,11 +33,9 @@ public class PushContext { public AtomicLong deliveredMsgsCount = new AtomicLong(0); - public AtomicLong ackedMsgsCount = new AtomicLong(0); - public AtomicLong deliverFailMsgsCount = new AtomicLong(0); - private ConcurrentHashMap unAckMsg = new ConcurrentHashMap(); + private ConcurrentHashMap unAckMsg = new ConcurrentHashMap(); private long createTime = System.currentTimeMillis(); @@ -59,9 +51,8 @@ public void deliverFailMsgCount() { deliverFailMsgsCount.incrementAndGet(); } - public void unAckMsg(String seq, List msg, ProxyConsumeConcurrentlyContext context, MQConsumerWrapper consumer) { - ClientAckContext ackContext = new ClientAckContext(seq,context, msg, consumer); - unAckMsg.put(seq, ackContext); + public void unAckMsg(String seq, DownStreamMsgContext downStreamMsgContext) { + unAckMsg.put(seq, downStreamMsgContext); logger.info("put msg in unAckMsg,seq:{},unAckMsgSize:{}", seq, getTotalUnackMsgs()); } @@ -69,17 +60,8 @@ public int getTotalUnackMsgs() { return unAckMsg.size(); } - public void ackMsg(String seq) { - if (unAckMsg.containsKey(seq)) { - unAckMsg.get(seq).ackMsg(); - unAckMsg.remove(seq); - ackedMsgsCount.incrementAndGet(); - }else{ - logger.warn("ackMsg failed,the seq:{} is not in unAckMsg map", seq); - } - } - public ConcurrentHashMap getUnAckMsg() { + public ConcurrentHashMap getUnAckMsg() { return unAckMsg; } @@ -88,8 +70,7 @@ public String toString() { return "PushContext{" + "deliveredMsgsCount=" + deliveredMsgsCount.longValue() + ",deliverFailCount=" + deliverFailMsgsCount.longValue() + - ",ackedMsgsCount=" + ackedMsgsCount.longValue() + ",unAckMsg=" + CollectionUtils.size(unAckMsg) + - ",createTime=" + DateFormatUtils.format(createTime, ProxyConstants.DATE_FORMAT) + '}'; + ",createTime=" + DateFormatUtils.format(createTime, EventMeshConstants.DATE_FORMAT) + '}'; } } diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/push/SessionPusher.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/push/SessionPusher.java new file mode 100644 index 0000000000..12c7cd14e7 --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/push/SessionPusher.java @@ -0,0 +1,142 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push; + +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.SubcriptionType; +import org.apache.eventmesh.common.protocol.SubscriptionMode; +import org.apache.eventmesh.common.protocol.tcp.*; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session; +import org.apache.eventmesh.runtime.util.EventMeshUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; + +public class SessionPusher { + + private final Logger messageLogger = LoggerFactory.getLogger("message"); + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + private AtomicLong deliveredMsgsCount = new AtomicLong(0); + + private AtomicLong deliverFailMsgsCount = new AtomicLong(0); + + private ConcurrentHashMap downStreamMap = new ConcurrentHashMap(); + + private Session session; + + public SessionPusher(Session session) { + this.session = session; + } + + @Override + public String toString() { + return "SessionPusher{" + + "deliveredMsgsCount=" + deliveredMsgsCount.longValue() + + ",deliverFailCount=" + deliverFailMsgsCount.longValue() + + ",unAckMsg=" + CollectionUtils.size(downStreamMap) + '}'; + } + + public void push(final DownStreamMsgContext downStreamMsgContext) { + Command cmd; + if (SubscriptionMode.BROADCASTING.equals(downStreamMsgContext.subscriptionItem.getMode())) { + cmd = Command.BROADCAST_MESSAGE_TO_CLIENT; + } else if (SubcriptionType.SYNC.equals(downStreamMsgContext.subscriptionItem.getType())) { + cmd = Command.REQUEST_TO_CLIENT; + } else { + cmd = Command.ASYNC_MESSAGE_TO_CLIENT; + } + + Package pkg = new Package(); + downStreamMsgContext.msgExt.getSystemProperties().put(EventMeshConstants.REQ_EVENTMESH2C_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + EventMeshMessage body = null; + int retCode = 0; + String retMsg = null; + try { + body = EventMeshUtil.encodeMessage(downStreamMsgContext.msgExt); + pkg.setBody(body); + pkg.setHeader(new Header(cmd, OPStatus.SUCCESS.getCode(), null, downStreamMsgContext.seq)); + messageLogger.info("pkg|mq2eventMesh|cmd={}|mqMsg={}|user={}", cmd, EventMeshUtil.printMqMessage(body), session.getClient()); + } catch (Exception e) { + pkg.setHeader(new Header(cmd, OPStatus.FAIL.getCode(), e.getStackTrace().toString(), downStreamMsgContext.seq)); + retCode = -1; + retMsg = e.toString(); + } finally { + session.getClientGroupWrapper().get().getEventMeshTcpMonitor().getEventMesh2clientMsgNum().incrementAndGet(); + + session.getContext().writeAndFlush(pkg).addListener( + new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (!future.isSuccess()) { + logger.error("downstreamMsg fail,seq:{}, retryTimes:{}, msg:{}", downStreamMsgContext.seq, downStreamMsgContext.retryTimes, downStreamMsgContext.msgExt); + deliverFailMsgsCount.incrementAndGet(); + + //how long to isolate client when push fail + long isolateTime = System.currentTimeMillis() + session.getEventMeshTCPConfiguration().eventMeshTcpPushFailIsolateTimeInMills; + session.setIsolateTime(isolateTime); + logger.warn("isolate client:{},isolateTime:{}", session.getClient(), isolateTime); + + //retry + long delayTime = SubcriptionType.SYNC.equals(downStreamMsgContext.subscriptionItem.getType()) ? 0 : session.getEventMeshTCPConfiguration().eventMeshTcpMsgRetryDelayInMills; + downStreamMsgContext.delay(delayTime); + session.getClientGroupWrapper().get().getEventMeshTcpRetryer().pushRetry(downStreamMsgContext); + } else { + deliveredMsgsCount.incrementAndGet(); + logger.info("downstreamMsg success,seq:{}, retryTimes:{}, bizSeq:{}", downStreamMsgContext.seq, downStreamMsgContext.retryTimes, EventMeshUtil.getMessageBizSeq(downStreamMsgContext.msgExt)); + + if (session.isIsolated()) { + logger.info("cancel isolated,client:{}", session.getClient()); + session.setIsolateTime(System.currentTimeMillis()); + } + } + } + } + ); + } + } + + public void unAckMsg(String seq, DownStreamMsgContext downStreamMsgContext) { + downStreamMap.put(seq, downStreamMsgContext); + logger.info("put msg in unAckMsg,seq:{},unAckMsgSize:{}", seq, getTotalUnackMsgs()); + } + + public int getTotalUnackMsgs() { + return downStreamMap.size(); + } + + public ConcurrentHashMap getUnAckMsg() { + return downStreamMap; + } + + public AtomicLong getDeliveredMsgsCount() { + return deliveredMsgsCount; + } + + public AtomicLong getDeliverFailMsgsCount() { + return deliverFailMsgsCount; + } +} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/push/retry/ProxyTcpRetryer.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/push/retry/EventMeshTcpRetryer.java similarity index 53% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/push/retry/ProxyTcpRetryer.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/push/retry/EventMeshTcpRetryer.java index 33f866a61a..57165a9d85 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/push/retry/ProxyTcpRetryer.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/push/retry/EventMeshTcpRetryer.java @@ -15,21 +15,7 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client.session.push.retry; - -import com.webank.defibus.common.DeFiBusConstant; -import com.webank.defibus.common.message.DeFiBusMessageConst; -import com.webank.emesher.boot.ProxyTCPServer; -import com.webank.emesher.core.protocol.tcp.client.session.Session; -import com.webank.emesher.core.protocol.tcp.client.session.push.DownStreamMsgContext; -import com.webank.emesher.util.ProxyThreadFactoryImpl; -import com.webank.emesher.util.ProxyUtil; -import org.apache.commons.lang3.StringUtils; -import org.apache.rocketmq.client.impl.consumer.ConsumeMessageConcurrentlyService; -import org.apache.rocketmq.client.impl.consumer.ConsumeMessageService; -import org.apache.rocketmq.common.message.MessageExt; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +package org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push.retry; import java.util.ArrayList; import java.util.List; @@ -38,11 +24,26 @@ import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; -public class ProxyTcpRetryer { +import io.openmessaging.api.Message; + +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.SubcriptionType; +import org.apache.eventmesh.common.protocol.SubscriptionMode; +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push.DownStreamMsgContext; +import org.apache.eventmesh.runtime.util.EventMeshThreadFactoryImpl; +import org.apache.eventmesh.runtime.util.EventMeshUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class EventMeshTcpRetryer { - public static Logger logger = LoggerFactory.getLogger(ProxyTcpRetryer.class); + public static Logger logger = LoggerFactory.getLogger(EventMeshTcpRetryer.class); - private ProxyTCPServer proxyTCPServer; + private EventMeshTCPServer eventMeshTCPServer; private DelayQueue retrys = new DelayQueue(); @@ -50,40 +51,40 @@ public class ProxyTcpRetryer { 3, 60000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1000), - new ProxyThreadFactoryImpl("proxy-tcp-retry",true), + new EventMeshThreadFactoryImpl("eventMesh-tcp-retry", true), new ThreadPoolExecutor.AbortPolicy()); private Thread dispatcher; - public ProxyTcpRetryer(ProxyTCPServer proxyTCPServer) { - this.proxyTCPServer = proxyTCPServer; + public EventMeshTcpRetryer(EventMeshTCPServer eventMeshTCPServer) { + this.eventMeshTCPServer = eventMeshTCPServer; } - public ProxyTCPServer getProxyTCPServer() { - return proxyTCPServer; + public EventMeshTCPServer getEventMeshTCPServer() { + return eventMeshTCPServer; } - public void setProxyTCPServer(ProxyTCPServer proxyTCPServer) { - this.proxyTCPServer = proxyTCPServer; + public void setEventMeshTCPServer(EventMeshTCPServer eventMeshTCPServer) { + this.eventMeshTCPServer = eventMeshTCPServer; } public void pushRetry(DownStreamMsgContext downStreamMsgContext) { - if (retrys.size() >= proxyTCPServer.getAccessConfiguration().proxyTcpMsgRetryQueueSize) { + if (retrys.size() >= eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshTcpMsgRetryQueueSize) { logger.error("pushRetry fail,retrys is too much,allow max retryQueueSize:{}, retryTimes:{}, seq:{}, bizSeq:{}", - proxyTCPServer.getAccessConfiguration().proxyTcpMsgRetryQueueSize, downStreamMsgContext.retryTimes, - downStreamMsgContext.seq, ProxyUtil.getMessageBizSeq(downStreamMsgContext.msgExt)); + eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshTcpMsgRetryQueueSize, downStreamMsgContext.retryTimes, + downStreamMsgContext.seq, EventMeshUtil.getMessageBizSeq(downStreamMsgContext.msgExt)); return; } - int maxRetryTimes = ProxyUtil.isService(downStreamMsgContext.msgExt.getTopic()) ? 1 : proxyTCPServer.getAccessConfiguration().proxyTcpMsgRetryTimes; + int maxRetryTimes = SubcriptionType.SYNC.equals(downStreamMsgContext.subscriptionItem.getType()) ? 1 : eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshTcpMsgRetryTimes; if (downStreamMsgContext.retryTimes >= maxRetryTimes) { logger.warn("pushRetry fail,retry over maxRetryTimes:{}, retryTimes:{}, seq:{}, bizSeq:{}", maxRetryTimes, downStreamMsgContext.retryTimes, - downStreamMsgContext.seq, ProxyUtil.getMessageBizSeq(downStreamMsgContext.msgExt)); + downStreamMsgContext.seq, EventMeshUtil.getMessageBizSeq(downStreamMsgContext.msgExt)); return; } retrys.offer(downStreamMsgContext); - logger.info("pushRetry success,seq:{}, retryTimes:{}, bizSeq:{}",downStreamMsgContext.seq, downStreamMsgContext.retryTimes, ProxyUtil.getMessageBizSeq(downStreamMsgContext.msgExt)); + logger.info("pushRetry success,seq:{}, retryTimes:{}, bizSeq:{}", downStreamMsgContext.seq, downStreamMsgContext.retryTimes, EventMeshUtil.getMessageBizSeq(downStreamMsgContext.msgExt)); } public void init() { @@ -104,110 +105,106 @@ public void run() { } }, "retry-dispatcher"); dispatcher.setDaemon(true); - logger.info("ProxyTcpRetryer inited......"); + logger.info("EventMeshTcpRetryer inited......"); } - private void retryHandle(DownStreamMsgContext downStreamMsgContext){ + private void retryHandle(DownStreamMsgContext downStreamMsgContext) { try { - logger.info("retry downStream msg start,seq:{},retryTimes:{},bizSeq:{}",downStreamMsgContext.seq, downStreamMsgContext.retryTimes, ProxyUtil.getMessageBizSeq(downStreamMsgContext.msgExt)); + logger.info("retry downStream msg start,seq:{},retryTimes:{},bizSeq:{}", downStreamMsgContext.seq, downStreamMsgContext.retryTimes, EventMeshUtil.getMessageBizSeq(downStreamMsgContext.msgExt)); - if(isRetryMsgTimeout(downStreamMsgContext)) + if (isRetryMsgTimeout(downStreamMsgContext)) { return; - + } downStreamMsgContext.retryTimes++; downStreamMsgContext.lastPushTime = System.currentTimeMillis(); Session rechoosen = null; - if(!ProxyUtil.isBroadcast(downStreamMsgContext.msgExt.getTopic())){ + String topic = downStreamMsgContext.msgExt.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION); + if (!SubscriptionMode.BROADCASTING.equals(downStreamMsgContext.subscriptionItem.getMode())) { rechoosen = downStreamMsgContext.session.getClientGroupWrapper() - .get().getDownstreamDispatchStrategy().select(downStreamMsgContext.session.getClientGroupWrapper().get().getGroupName() - , downStreamMsgContext.msgExt.getTopic() + .get().getDownstreamDispatchStrategy().select(downStreamMsgContext.session.getClientGroupWrapper().get().getSysId() + , topic , downStreamMsgContext.session.getClientGroupWrapper().get().getGroupConsumerSessions()); - }else{ + } else { rechoosen = downStreamMsgContext.session; } - - if(rechoosen == null){ - logger.warn("retry, found no session to downstream msg,seq:{}, retryTimes:{}, bizSeq:{}", downStreamMsgContext.seq, downStreamMsgContext.retryTimes, ProxyUtil.getMessageBizSeq(downStreamMsgContext.msgExt)); + if (rechoosen == null) { + logger.warn("retry, found no session to downstream msg,seq:{}, retryTimes:{}, bizSeq:{}", downStreamMsgContext.seq, downStreamMsgContext.retryTimes, EventMeshUtil.getMessageBizSeq(downStreamMsgContext.msgExt)); // //需要手动ack掉没有下发成功的消息 -// proxyAckMsg(downStreamMsgContext); +// eventMeshAckMsg(downStreamMsgContext); -// //重试找不到下发session不再回发broker或者重试其它proxy +// //重试找不到下发session不再回发broker或者重试其它eventMesh // String bizSeqNo = finalDownStreamMsgContext.msgExt.getKeys(); // String uniqueId = MapUtils.getString(finalDownStreamMsgContext.msgExt.getProperties(), WeMQConstant.RMB_UNIQ_ID, ""); -// if(weMQProxyTCPServer.getAccessConfiguration().proxyTcpSendBackEnabled){ +// if(EventMeshTCPServer.getAccessConfiguration().eventMeshTcpSendBackEnabled){ // sendMsgBackToBroker(finalDownStreamMsgContext.msgExt, bizSeqNo, uniqueId); // }else{ -// //TODO 将消息推给其它proxy,待定 -// sendMsgToOtherProxy(finalDownStreamMsgContext.msgExt, bizSeqNo, uniqueId); +// //TODO 将消息推给其它eventMesh,待定 +// sendMsgToOtherEventMesh(finalDownStreamMsgContext.msgExt, bizSeqNo, uniqueId); // } - }else { + } else { downStreamMsgContext.session = rechoosen; - - if (rechoosen.isCanDownStream()) { - rechoosen.downstreamMsg(downStreamMsgContext); - logger.info("retry downStream msg end,seq:{},retryTimes:{},bizSeq:{}",downStreamMsgContext.seq, downStreamMsgContext.retryTimes, ProxyUtil.getMessageBizSeq(downStreamMsgContext.msgExt)); - }else{ - logger.warn("session is busy,push retry again,seq:{}, session:{}, bizSeq:{}", downStreamMsgContext.seq, downStreamMsgContext.session.getClient(), ProxyUtil.getMessageBizSeq(downStreamMsgContext.msgExt)); - long delayTime = ProxyUtil.isService(downStreamMsgContext.msgExt.getTopic()) ? 0 : proxyTCPServer.getAccessConfiguration().proxyTcpMsgRetryDelayInMills; - downStreamMsgContext.delay(delayTime); - pushRetry(downStreamMsgContext); - } + rechoosen.downstreamMsg(downStreamMsgContext); + logger.info("retry downStream msg end,seq:{},retryTimes:{},bizSeq:{}", downStreamMsgContext.seq, downStreamMsgContext.retryTimes, EventMeshUtil.getMessageBizSeq(downStreamMsgContext.msgExt)); } } catch (Exception e) { logger.error("retry-dispatcher error!", e); } } - private boolean isRetryMsgTimeout(DownStreamMsgContext downStreamMsgContext){ - boolean flag =false; - long ttl = Long.valueOf(downStreamMsgContext.msgExt.getUserProperty(DeFiBusConstant.PROPERTY_MESSAGE_TTL)); - long storeTimestamp = downStreamMsgContext.msgExt.getStoreTimestamp(); - String leaveTimeStr = downStreamMsgContext.msgExt.getProperties().get(DeFiBusMessageConst.LEAVE_TIME); - long brokerCost = StringUtils.isNumeric(leaveTimeStr) ? Long.valueOf(leaveTimeStr) - storeTimestamp : 0; + private boolean isRetryMsgTimeout(DownStreamMsgContext downStreamMsgContext) { + boolean flag = false; + String ttlStr = downStreamMsgContext.msgExt.getUserProperties(EventMeshConstants.PROPERTY_MESSAGE_TTL); + long ttl = StringUtils.isNumeric(ttlStr)? Long.parseLong(ttlStr) : EventMeshConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS;; + + String storeTimeStr = downStreamMsgContext.msgExt.getUserProperties(EventMeshConstants.STORE_TIME); + long storeTimestamp = StringUtils.isNumeric(storeTimeStr)? Long.parseLong(storeTimeStr) : 0; + String leaveTimeStr = downStreamMsgContext.msgExt.getUserProperties(EventMeshConstants.LEAVE_TIME); + long brokerCost = StringUtils.isNumeric(leaveTimeStr) ? Long.parseLong(leaveTimeStr) - storeTimestamp : 0; - String arriveTimeStr = downStreamMsgContext.msgExt.getProperties().get(DeFiBusMessageConst.ARRIVE_TIME); - long accessCost = StringUtils.isNumeric(arriveTimeStr) ? System.currentTimeMillis() - Long.valueOf(arriveTimeStr) : 0; + String arriveTimeStr = downStreamMsgContext.msgExt.getUserProperties(EventMeshConstants.ARRIVE_TIME); + long accessCost = StringUtils.isNumeric(arriveTimeStr) ? System.currentTimeMillis() - Long.parseLong(arriveTimeStr) : 0; double elapseTime = brokerCost + accessCost; if (elapseTime >= ttl) { - logger.warn("discard the retry because timeout, seq:{}, retryTimes:{}, bizSeq:{}", downStreamMsgContext.seq, downStreamMsgContext.retryTimes, ProxyUtil.getMessageBizSeq(downStreamMsgContext.msgExt)); + logger.warn("discard the retry because timeout, seq:{}, retryTimes:{}, bizSeq:{}", downStreamMsgContext.seq, downStreamMsgContext.retryTimes, EventMeshUtil.getMessageBizSeq(downStreamMsgContext.msgExt)); flag = true; - proxyAckMsg(downStreamMsgContext); + eventMeshAckMsg(downStreamMsgContext); } return flag; } public void start() throws Exception { dispatcher.start(); - logger.info("ProxyTcpRetryer started......"); + logger.info("EventMeshTcpRetryer started......"); } public void shutdown() { pool.shutdown(); - logger.info("ProxyTcpRetryer shutdown......"); + logger.info("EventMeshTcpRetryer shutdown......"); } - public int getRetrySize(){ + public int getRetrySize() { return retrys.size(); } /** - * proxy ack msg + * eventMesh ack msg * * @param downStreamMsgContext */ - private void proxyAckMsg(DownStreamMsgContext downStreamMsgContext){ - List msgExts = new ArrayList(); + private void eventMeshAckMsg(DownStreamMsgContext downStreamMsgContext) { + List msgExts = new ArrayList(); msgExts.add(downStreamMsgContext.msgExt); - logger.warn("proxyAckMsg topic:{}, seq:{}, bizSeq:{}",downStreamMsgContext.msgExt.getTopic(), downStreamMsgContext.seq, downStreamMsgContext.msgExt.getKeys()); + logger.warn("eventMeshAckMsg topic:{}, seq:{}, bizSeq:{}", downStreamMsgContext.msgExt.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION), + downStreamMsgContext.seq, downStreamMsgContext.msgExt.getSystemProperties(EventMeshConstants.PROPERTY_MESSAGE_KEYS)); downStreamMsgContext.consumer.updateOffset(msgExts, downStreamMsgContext.consumeConcurrentlyContext); // ConsumeMessageService consumeMessageService = downStreamMsgContext.consumer.getDefaultMQPushConsumer().getDefaultMQPushConsumerImpl().getConsumeMessageService(); // ((ConsumeMessageConcurrentlyService)consumeMessageService).updateOffset(msgExts, downStreamMsgContext.consumeConcurrentlyContext); } - public void printRetryThreadPoolState(){ + public void printRetryThreadPoolState() { // ThreadPoolHelper.printState(pool); } } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/send/ProxyTcpSendResult.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/send/EventMeshTcpSendResult.java similarity index 77% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/send/ProxyTcpSendResult.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/send/EventMeshTcpSendResult.java index c38bdbfd36..61c4d041e4 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/send/ProxyTcpSendResult.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/send/EventMeshTcpSendResult.java @@ -15,17 +15,17 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client.session.send; +package org.apache.eventmesh.runtime.core.protocol.tcp.client.session.send; -public class ProxyTcpSendResult { +public class EventMeshTcpSendResult { private String seq; - private ProxyTcpSendStatus sendStatus; + private EventMeshTcpSendStatus sendStatus; private String detail; - public ProxyTcpSendResult(String seq, ProxyTcpSendStatus sendStatus, String detail) { + public EventMeshTcpSendResult(String seq, EventMeshTcpSendStatus sendStatus, String detail) { this.seq = seq; this.sendStatus = sendStatus; this.detail = detail; @@ -35,7 +35,7 @@ public String getSeq() { return seq; } - public ProxyTcpSendStatus getSendStatus() { + public EventMeshTcpSendStatus getSendStatus() { return sendStatus; } @@ -45,7 +45,7 @@ public String getDetail() { @Override public String toString() { - return "ProxyTcpSendResult{seq=" + seq + return "EventMeshTcpSendResult{seq=" + seq + ",sendStatus=" + sendStatus + ",detail=" + detail + "}"; } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/send/ProxyTcpSendStatus.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/send/EventMeshTcpSendStatus.java similarity index 88% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/send/ProxyTcpSendStatus.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/send/EventMeshTcpSendStatus.java index 4c6a6f66db..75d0823630 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/send/ProxyTcpSendStatus.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/send/EventMeshTcpSendStatus.java @@ -15,9 +15,9 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client.session.send; +package org.apache.eventmesh.runtime.core.protocol.tcp.client.session.send; -public enum ProxyTcpSendStatus { +public enum EventMeshTcpSendStatus { SUCCESS, SEND_TOO_FAST, OTHER_EXCEPTION; diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/send/SessionSender.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/send/SessionSender.java similarity index 62% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/send/SessionSender.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/send/SessionSender.java index e218cb8fde..e75241cc20 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/send/SessionSender.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/send/SessionSender.java @@ -15,33 +15,30 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client.session.send; - -import com.webank.defibus.client.impl.producer.RRCallback; -import com.webank.defibus.common.DeFiBusConstant; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.protocol.tcp.client.session.Session; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Header; -import com.webank.eventmesh.common.protocol.tcp.OPStatus; -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.emesher.util.ProxyUtil; -import com.webank.emesher.util.Utils; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.time.DateFormatUtils; -import org.apache.rocketmq.client.producer.SendCallback; -import org.apache.rocketmq.common.MixAll; -import org.apache.rocketmq.common.message.Message; -import org.apache.rocketmq.common.message.MessageAccessor; -import org.apache.rocketmq.common.message.MessageConst; -import org.apache.rocketmq.common.message.MessageExt; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +package org.apache.eventmesh.runtime.core.protocol.tcp.client.session.send; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; +import io.openmessaging.api.Message; +import io.openmessaging.api.SendCallback; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.time.DateFormatUtils; +import org.apache.eventmesh.api.RRCallback; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.protocol.tcp.Command; +import org.apache.eventmesh.common.protocol.tcp.Header; +import org.apache.eventmesh.common.protocol.tcp.OPStatus; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session; +import org.apache.eventmesh.runtime.util.EventMeshUtil; +import org.apache.eventmesh.runtime.util.Utils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class SessionSender { private final Logger messageLogger = LoggerFactory.getLogger("message"); @@ -62,37 +59,36 @@ public String toString() { return "SessionSender{upstreamBuff=" + upstreamBuff.availablePermits() + ",upMsgs=" + upMsgs.longValue() + ",failMsgCount=" + failMsgCount.longValue() + - ",createTime=" + DateFormatUtils.format(createTime, ProxyConstants.DATE_FORMAT) + '}'; + ",createTime=" + DateFormatUtils.format(createTime, EventMeshConstants.DATE_FORMAT) + '}'; } public Semaphore getUpstreamBuff() { return upstreamBuff; } - private Semaphore upstreamBuff ; + private Semaphore upstreamBuff; public SessionSender(Session session) { this.session = session; - this.upstreamBuff = new Semaphore(session.getAccessConfiguration().proxyTcpSessionUpstreamBufferSize); + this.upstreamBuff = new Semaphore(session.getEventMeshTCPConfiguration().eventMeshTcpSessionUpstreamBufferSize); } - public ProxyTcpSendResult send(Header header, Message msg, SendCallback sendCallback, long startTime, long taskExecuteTime) { + public EventMeshTcpSendResult send(Header header, Message msg, SendCallback sendCallback, long startTime, long taskExecuteTime) { try { if (upstreamBuff.tryAcquire(TRY_PERMIT_TIME_OUT, TimeUnit.MILLISECONDS)) { upMsgs.incrementAndGet(); UpStreamMsgContext upStreamMsgContext = null; Command cmd = header.getCommand(); if (Command.REQUEST_TO_SERVER == cmd) { - long ttl = msg.getProperty(DeFiBusConstant.PROPERTY_MESSAGE_TTL) != null ? Long.valueOf(msg.getProperty - (DeFiBusConstant.PROPERTY_MESSAGE_TTL)) : ProxyConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS; + long ttl = msg.getSystemProperties(EventMeshConstants.PROPERTY_MESSAGE_TTL) != null ? Long.parseLong(msg.getSystemProperties(EventMeshConstants.PROPERTY_MESSAGE_TTL)) : EventMeshConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS; upStreamMsgContext = new UpStreamMsgContext(header.getSeq(), session, msg); session.getClientGroupWrapper().get().request(upStreamMsgContext, sendCallback, initSyncRRCallback(header, startTime, taskExecuteTime), ttl); } else if (Command.RESPONSE_TO_SERVER == cmd) { - String cluster = msg.getUserProperty(DeFiBusConstant.PROPERTY_MESSAGE_CLUSTER); + String cluster = msg.getUserProperties(EventMeshConstants.PROPERTY_MESSAGE_CLUSTER); if (!StringUtils.isEmpty(cluster)) { - String replyTopic = DeFiBusConstant.RR_REPLY_TOPIC; + String replyTopic = EventMeshConstants.RR_REPLY_TOPIC; replyTopic = cluster + "-" + replyTopic; - msg.setTopic(replyTopic); + msg.getSystemProperties().put(Constants.PROPERTY_MESSAGE_DESTINATION, replyTopic); } // //for rocketmq support @@ -108,20 +104,20 @@ public ProxyTcpSendResult send(Header header, Message msg, SendCallback sendCall session.getClientGroupWrapper().get().send(upStreamMsgContext, sendCallback); } - session.getClientGroupWrapper().get().getProxyTcpMonitor().getProxy2mqMsgNum().incrementAndGet(); + session.getClientGroupWrapper().get().getEventMeshTcpMonitor().getEventMesh2mqMsgNum().incrementAndGet(); } else { - logger.warn("send too fast,session flow control,session:{}",session.getClient()); - return new ProxyTcpSendResult(header.getSeq(), ProxyTcpSendStatus.SEND_TOO_FAST, ProxyTcpSendStatus.SEND_TOO_FAST.name()); + logger.warn("send too fast,session flow control,session:{}", session.getClient()); + return new EventMeshTcpSendResult(header.getSeq(), EventMeshTcpSendStatus.SEND_TOO_FAST, EventMeshTcpSendStatus.SEND_TOO_FAST.name()); } } catch (Exception e) { logger.warn("SessionSender send failed", e); - if(!(e instanceof InterruptedException)) { + if (!(e instanceof InterruptedException)) { upstreamBuff.release(); } failMsgCount.incrementAndGet(); - return new ProxyTcpSendResult(header.getSeq(), ProxyTcpSendStatus.OTHER_EXCEPTION, e.getCause().toString()); + return new EventMeshTcpSendResult(header.getSeq(), EventMeshTcpSendStatus.OTHER_EXCEPTION, e.getCause().toString()); } - return new ProxyTcpSendResult(header.getSeq(), ProxyTcpSendStatus.SUCCESS, ProxyTcpSendStatus.SUCCESS.name()); + return new EventMeshTcpSendResult(header.getSeq(), EventMeshTcpSendStatus.SUCCESS, EventMeshTcpSendStatus.SUCCESS.name()); } private RRCallback initSyncRRCallback(Header header, long startTime, long taskExecuteTime) { @@ -129,15 +125,17 @@ private RRCallback initSyncRRCallback(Header header, long startTime, long taskEx @Override public void onSuccess(Message msg) { String seq = header.getSeq(); - if (msg instanceof MessageExt) { - msg.putUserProperty(ProxyConstants.BORN_TIMESTAMP, String.valueOf(((MessageExt) msg) - .getBornTimestamp())); - msg.putUserProperty(ProxyConstants.STORE_TIMESTAMP, String.valueOf(((MessageExt) msg) - .getStoreTimestamp())); - } - msg.putUserProperty(ProxyConstants.RSP_MQ2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); - msg.putUserProperty(ProxyConstants.RSP_RECEIVE_PROXY_IP, session.getAccessConfiguration().proxyServerIp); - session.getClientGroupWrapper().get().getProxyTcpMonitor().getMq2proxyMsgNum().incrementAndGet(); + //TODO 此处如何赋值 +// if (msg instanceof MessageExt) { +// msg.putUserProperty(EventMeshConstants.BORN_TIMESTAMP, String.valueOf(((MessageExt) msg) +// .getBornTimestamp())); +// msg.putUserProperty(EventMeshConstants.STORE_TIMESTAMP, String.valueOf(((MessageExt) msg) +// .getStoreTimestamp())); +// } + + msg.getSystemProperties().put(EventMeshConstants.RSP_MQ2EVENTMESH_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + msg.getSystemProperties().put(EventMeshConstants.RSP_RECEIVE_EVENTMESH_IP, session.getEventMeshTCPConfiguration().eventMeshServerIp); + session.getClientGroupWrapper().get().getEventMeshTcpMonitor().getMq2EventMeshMsgNum().incrementAndGet(); Command cmd; if (header.getCommand().equals(Command.REQUEST_TO_SERVER)) { @@ -148,9 +146,9 @@ public void onSuccess(Message msg) { } Package pkg = new Package(); pkg.setHeader(new Header(cmd, OPStatus.SUCCESS.getCode(), null, seq)); - msg.putUserProperty(ProxyConstants.RSP_PROXY2C_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + msg.getSystemProperties().put(EventMeshConstants.RSP_EVENTMESH2C_TIMESTAMP, String.valueOf(System.currentTimeMillis())); try { - pkg.setBody(ProxyUtil.encodeMessage(msg)); + pkg.setBody(EventMeshUtil.encodeMessage(msg)); pkg.setHeader(new Header(cmd, OPStatus.SUCCESS.getCode(), null, seq)); } catch (Exception e) { pkg.setHeader(new Header(cmd, OPStatus.FAIL.getCode(), null, seq)); diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/send/UpStreamMsgContext.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/send/UpStreamMsgContext.java similarity index 78% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/send/UpStreamMsgContext.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/send/UpStreamMsgContext.java index f1b0b0cb3b..1618ce72da 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/session/send/UpStreamMsgContext.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/send/UpStreamMsgContext.java @@ -15,12 +15,14 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client.session.send; +package org.apache.eventmesh.runtime.core.protocol.tcp.client.session.send; + +import io.openmessaging.api.Message; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.protocol.tcp.client.session.Session; import org.apache.commons.lang3.time.DateFormatUtils; -import org.apache.rocketmq.common.message.Message; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session; public class UpStreamMsgContext { @@ -53,8 +55,8 @@ public long getCreateTime() { @Override public String toString() { return "UpStreamMsgContext{seq=" + seq - + ",topic=" + msg.getTopic() + + ",topic=" + msg.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION) + ",client=" + session.getClient() - + ",createTime=" + DateFormatUtils.format(createTime, ProxyConstants.DATE_FORMAT) + "}"; + + ",createTime=" + DateFormatUtils.format(createTime, EventMeshConstants.DATE_FORMAT) + "}"; } } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/AbstractTask.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/AbstractTask.java similarity index 71% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/AbstractTask.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/AbstractTask.java index a86eb644d0..64e0eefb5b 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/AbstractTask.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/AbstractTask.java @@ -15,12 +15,13 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client.task; +package org.apache.eventmesh.runtime.core.protocol.tcp.client.task; -import com.webank.emesher.boot.ProxyTCPServer; -import com.webank.emesher.core.protocol.tcp.client.session.Session; -import com.webank.eventmesh.common.protocol.tcp.Package; import io.netty.channel.ChannelHandlerContext; + +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,13 +32,13 @@ public abstract class AbstractTask implements Runnable { protected ChannelHandlerContext ctx; protected Session session; protected long startTime; - protected ProxyTCPServer proxyTCPServer; + protected EventMeshTCPServer eventMeshTCPServer; - public AbstractTask(Package pkg, ChannelHandlerContext ctx, long startTime, ProxyTCPServer proxyTCPServer) { - this.proxyTCPServer = proxyTCPServer; + public AbstractTask(Package pkg, ChannelHandlerContext ctx, long startTime, EventMeshTCPServer eventMeshTCPServer) { + this.eventMeshTCPServer = eventMeshTCPServer; this.pkg = pkg; this.ctx = ctx; - this.session = proxyTCPServer.getClientSessionGroupMapping().getSession(ctx); + this.session = eventMeshTCPServer.getClientSessionGroupMapping().getSession(ctx); this.startTime = startTime; } } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/GoodbyeTask.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/GoodbyeTask.java similarity index 70% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/GoodbyeTask.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/GoodbyeTask.java index cb5c31e96e..67c0d5cf84 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/GoodbyeTask.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/GoodbyeTask.java @@ -15,23 +15,24 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client.task; +package org.apache.eventmesh.runtime.core.protocol.tcp.client.task; + +import static org.apache.eventmesh.common.protocol.tcp.Command.CLIENT_GOODBYE_RESPONSE; -import com.webank.emesher.boot.ProxyTCPServer; -import com.webank.emesher.core.protocol.tcp.client.ProxyTcp2Client; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Header; -import com.webank.eventmesh.common.protocol.tcp.OPStatus; -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.emesher.util.Utils; import io.netty.channel.ChannelHandlerContext; -import static com.webank.eventmesh.common.protocol.tcp.Command.CLIENT_GOODBYE_RESPONSE; +import org.apache.eventmesh.common.protocol.tcp.Command; +import org.apache.eventmesh.common.protocol.tcp.Header; +import org.apache.eventmesh.common.protocol.tcp.OPStatus; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.EventMeshTcp2Client; +import org.apache.eventmesh.runtime.util.Utils; public class GoodbyeTask extends AbstractTask { - public GoodbyeTask(Package pkg, ChannelHandlerContext ctx, long startTime, ProxyTCPServer proxyTCPServer) { - super(pkg, ctx, startTime, proxyTCPServer); + public GoodbyeTask(Package pkg, ChannelHandlerContext ctx, long startTime, EventMeshTCPServer eventMeshTCPServer) { + super(pkg, ctx, startTime, eventMeshTCPServer); } @Override @@ -50,7 +51,7 @@ public void run() { msg.setHeader(new Header(CLIENT_GOODBYE_RESPONSE, OPStatus.FAIL.getCode(), e.getStackTrace().toString(), pkg .getHeader().getSeq())); } finally { - ProxyTCPServer.scheduler.submit(new Runnable() { + this.eventMeshTCPServer.getScheduler().submit(new Runnable() { @Override public void run() { Utils.writeAndFlush(msg, startTime, taskExecuteTime, session.getContext(), session); @@ -58,6 +59,6 @@ public void run() { }); //session.write2Client(msg); } - ProxyTcp2Client.closeSessionIfTimeout(session, proxyTCPServer.getClientSessionGroupMapping()); + EventMeshTcp2Client.closeSessionIfTimeout(this.eventMeshTCPServer,session, eventMeshTCPServer.getClientSessionGroupMapping()); } } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/HeartBeatTask.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/HeartBeatTask.java similarity index 75% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/HeartBeatTask.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/HeartBeatTask.java index bad4a2fccb..eac1354cbe 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/HeartBeatTask.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/HeartBeatTask.java @@ -15,21 +15,22 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client.task; +package org.apache.eventmesh.runtime.core.protocol.tcp.client.task; + +import static org.apache.eventmesh.common.protocol.tcp.Command.HEARTBEAT_RESPONSE; -import com.webank.emesher.boot.ProxyTCPServer; -import com.webank.eventmesh.common.protocol.tcp.Header; -import com.webank.eventmesh.common.protocol.tcp.OPStatus; -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.emesher.util.Utils; import io.netty.channel.ChannelHandlerContext; -import static com.webank.eventmesh.common.protocol.tcp.Command.HEARTBEAT_RESPONSE; +import org.apache.eventmesh.common.protocol.tcp.Header; +import org.apache.eventmesh.common.protocol.tcp.OPStatus; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.apache.eventmesh.runtime.util.Utils; public class HeartBeatTask extends AbstractTask { - public HeartBeatTask(Package pkg, ChannelHandlerContext ctx, long startTime, ProxyTCPServer proxyTCPServer) { - super(pkg, ctx, startTime, proxyTCPServer); + public HeartBeatTask(Package pkg, ChannelHandlerContext ctx, long startTime, EventMeshTCPServer eventMeshTCPServer) { + super(pkg, ctx, startTime, eventMeshTCPServer); } @Override @@ -41,11 +42,11 @@ public void run() { session.notifyHeartbeat(startTime); } res.setHeader(new Header(HEARTBEAT_RESPONSE, OPStatus.SUCCESS.getCode(), OPStatus.SUCCESS.getDesc(), pkg.getHeader().getSeq())); - }catch (Exception e){ + } catch (Exception e) { logger.error("HeartBeatTask failed|user={}|errMsg={}", session.getClient(), e); res.setHeader(new Header(HEARTBEAT_RESPONSE, OPStatus.FAIL.getCode(), "exception while " + "heartbeating", pkg.getHeader().getSeq())); - }finally { + } finally { Utils.writeAndFlush(res, startTime, taskExecuteTime, session.getContext(), session); } } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/HelloTask.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/HelloTask.java similarity index 63% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/HelloTask.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/HelloTask.java index bbe26dcd16..161b1d79ae 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/HelloTask.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/HelloTask.java @@ -15,32 +15,33 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client.task; +package org.apache.eventmesh.runtime.core.protocol.tcp.client.task; + +import static org.apache.eventmesh.common.protocol.tcp.Command.HELLO_RESPONSE; -import com.webank.emesher.boot.ProxyTCPServer; -import com.webank.emesher.common.ServiceState; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.protocol.tcp.client.session.Session; -import com.webank.eventmesh.common.protocol.tcp.Header; -import com.webank.eventmesh.common.protocol.tcp.OPStatus; -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; -import com.webank.emesher.util.Utils; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; + import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.common.protocol.tcp.Header; +import org.apache.eventmesh.common.protocol.tcp.OPStatus; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.apache.eventmesh.runtime.common.ServiceState; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session; +import org.apache.eventmesh.runtime.util.Utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import static com.webank.eventmesh.common.protocol.tcp.Command.HELLO_RESPONSE; - public class HelloTask extends AbstractTask { private final Logger messageLogger = LoggerFactory.getLogger("message"); - public HelloTask(Package pkg, ChannelHandlerContext ctx, long startTime, ProxyTCPServer proxyTCPServer) { - super(pkg, ctx, startTime, proxyTCPServer); + public HelloTask(Package pkg, ChannelHandlerContext ctx, long startTime, EventMeshTCPServer eventMeshTCPServer) { + super(pkg, ctx, startTime, eventMeshTCPServer); } @Override @@ -50,13 +51,13 @@ public void run() { Session session = null; UserAgent user = (UserAgent) pkg.getBody(); try { - if(proxyTCPServer.getProxyServer().getServiceState() != ServiceState.RUNNING){ - logger.error("server state is not running:{}", proxyTCPServer.getProxyServer().getServiceState()); + if (eventMeshTCPServer.getEventMeshServer().getServiceState() != ServiceState.RUNNING) { + logger.error("server state is not running:{}", eventMeshTCPServer.getEventMeshServer().getServiceState()); throw new Exception("server state is not running, maybe deploying..."); } validateUserAgent(user); - session = proxyTCPServer.getClientSessionGroupMapping().createSession(user, ctx); + session = eventMeshTCPServer.getClientSessionGroupMapping().createSession(user, ctx); res.setHeader(new Header(HELLO_RESPONSE, OPStatus.SUCCESS.getCode(), OPStatus.SUCCESS.getDesc(), pkg.getHeader().getSeq())); Utils.writeAndFlush(res, startTime, taskExecuteTime, session.getContext(), session); } catch (Throwable e) { @@ -73,7 +74,7 @@ public void operationComplete(ChannelFuture future) throws Exception { Utils.logSucceedMessageFlow(res, user, startTime, taskExecuteTime); } logger.warn("HelloTask failed,close session,addr:{}", ctx.channel().remoteAddress()); - proxyTCPServer.getClientSessionGroupMapping().closeSession(ctx); + eventMeshTCPServer.getClientSessionGroupMapping().closeSession(ctx); } } ); @@ -90,15 +91,25 @@ private void validateUserAgent(UserAgent user) throws Exception { } // if (user.getUsername() == null) { -// throw new Exception("client wemqUser cannot be null"); +// throw new Exception("client EventMeshUser cannot be null"); // } // // if (user.getPassword() == null) { -// throw new Exception("client wemqPasswd cannot be null"); +// throw new Exception("client EventMeshPasswd cannot be null"); // } - if (!(StringUtils.equals(ProxyConstants.PURPOSE_PUB, user.getPurpose()) || StringUtils.equals(ProxyConstants.PURPOSE_SUB, user.getPurpose()))) { + if (!(StringUtils.equals(EventMeshConstants.PURPOSE_PUB, user.getPurpose()) || StringUtils.equals(EventMeshConstants.PURPOSE_SUB, user.getPurpose()))) { throw new Exception("client purpose config is error"); } + + if (StringUtils.equals(EventMeshConstants.PURPOSE_PUB, user.getPurpose()) + && StringUtils.isBlank(user.getProducerGroup())) { + throw new Exception("client producerGroup cannot be null"); + } + + if (StringUtils.equals(EventMeshConstants.PURPOSE_SUB, user.getPurpose()) + && StringUtils.isBlank(user.getConsumerGroup())) { + throw new Exception("client consumerGroup cannot be null"); + } } } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/ListenTask.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/ListenTask.java similarity index 76% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/ListenTask.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/ListenTask.java index 77ce58f5b9..4ce653a403 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/ListenTask.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/ListenTask.java @@ -15,20 +15,21 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client.task; +package org.apache.eventmesh.runtime.core.protocol.tcp.client.task; + +import static org.apache.eventmesh.common.protocol.tcp.Command.LISTEN_RESPONSE; -import com.webank.emesher.boot.ProxyTCPServer; -import com.webank.eventmesh.common.protocol.tcp.Header; -import com.webank.eventmesh.common.protocol.tcp.OPStatus; -import com.webank.eventmesh.common.protocol.tcp.Package; import io.netty.channel.ChannelHandlerContext; -import static com.webank.eventmesh.common.protocol.tcp.Command.LISTEN_RESPONSE; +import org.apache.eventmesh.common.protocol.tcp.Header; +import org.apache.eventmesh.common.protocol.tcp.OPStatus; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; public class ListenTask extends AbstractTask { - public ListenTask(Package pkg, ChannelHandlerContext ctx, long startTime, ProxyTCPServer proxyTCPServer) { - super(pkg, ctx, startTime, proxyTCPServer); + public ListenTask(Package pkg, ChannelHandlerContext ctx, long startTime, EventMeshTCPServer eventMeshTCPServer) { + super(pkg, ctx, startTime, eventMeshTCPServer); } @Override @@ -38,13 +39,13 @@ public void run() { session.setListenRequestSeq(pkg.getHeader().getSeq()); try { synchronized (session) { - proxyTCPServer.getClientSessionGroupMapping().readySession(session); + eventMeshTCPServer.getClientSessionGroupMapping().readySession(session); } } catch (Exception e) { logger.error("ListenTask failed|user={}|errMsg={}", session.getClient(), e); Integer status = OPStatus.FAIL.getCode(); header = new Header(LISTEN_RESPONSE, status, e.toString(), pkg.getHeader().getSeq()); - }finally { + } finally { // res.setHeader(header); // writeAndFlush(res, startTime, session.getContext(), session); //session.write2Client(res); diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/MessageAckTask.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/MessageAckTask.java similarity index 59% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/MessageAckTask.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/MessageAckTask.java index 5bea141a90..9ddbe4ebeb 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/MessageAckTask.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/MessageAckTask.java @@ -15,13 +15,15 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client.task; +package org.apache.eventmesh.runtime.core.protocol.tcp.client.task; -import com.webank.emesher.boot.ProxyTCPServer; -import com.webank.emesher.core.protocol.tcp.client.session.push.ClientAckContext; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Package; import io.netty.channel.ChannelHandlerContext; + +import org.apache.eventmesh.common.protocol.tcp.Command; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push.ClientAckContext; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push.DownStreamMsgContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -29,8 +31,8 @@ public class MessageAckTask extends AbstractTask { private final Logger messageLogger = LoggerFactory.getLogger("message"); - public MessageAckTask(Package pkg, ChannelHandlerContext ctx, long startTime, ProxyTCPServer proxyTCPServer) { - super(pkg, ctx, startTime, proxyTCPServer); + public MessageAckTask(Package pkg, ChannelHandlerContext ctx, long startTime, EventMeshTCPServer eventMeshTCPServer) { + super(pkg, ctx, startTime, eventMeshTCPServer); } @Override @@ -43,12 +45,14 @@ public void run() { logger.error("MessageAckTask failed, seq cannot be null|user={}", session.getClient()); return; } - ClientAckContext clientAckContext = session.getPusher().getPushContext().getUnAckMsg().get(seq); - if(clientAckContext != null) { - session.ackMsg(seq); - session.getClientGroupWrapper().get().getDownstreamMap().remove(seq); + DownStreamMsgContext downStreamMsgContext = session.getPusher().getUnAckMsg().get(seq); + if (downStreamMsgContext != null) {// ack non-broadcast msg + downStreamMsgContext.ackMsg(); + session.getPusher().getUnAckMsg().remove(seq); + }else { + logger.warn("MessageAckTask, seq:{}, downStreamMsgContext not in downStreamMap,client:{}", seq, session.getClient()); } - messageLogger.info("pkg|c2proxy|cmd={}|seq=[{}]|user={}|wait={}ms|cost={}ms", cmd, seq, session.getClient(), + messageLogger.info("pkg|c2eventMesh|cmd={}|seq=[{}]|user={}|wait={}ms|cost={}ms", cmd, seq, session.getClient(), taskExecuteTime - startTime, System.currentTimeMillis() - startTime); } } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/MessageTransferTask.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/MessageTransferTask.java similarity index 54% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/MessageTransferTask.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/MessageTransferTask.java index 8c2c10ee7e..028214d865 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/MessageTransferTask.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/MessageTransferTask.java @@ -15,41 +15,42 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client.task; - -import com.webank.defibus.common.DeFiBusConstant; -import com.webank.emesher.boot.ProxyTCPServer; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.protocol.tcp.client.session.send.ProxyTcpSendResult; -import com.webank.emesher.core.protocol.tcp.client.session.send.ProxyTcpSendStatus; -import com.webank.eventmesh.common.protocol.tcp.AccessMessage; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Header; -import com.webank.eventmesh.common.protocol.tcp.OPStatus; -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.emesher.util.ProxyUtil; -import com.webank.emesher.util.Utils; +package org.apache.eventmesh.runtime.core.protocol.tcp.client.task; + +import static org.apache.eventmesh.common.protocol.tcp.Command.RESPONSE_TO_SERVER; + +import java.util.concurrent.TimeUnit; + import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; +import io.openmessaging.api.OnExceptionContext; +import io.openmessaging.api.SendCallback; +import io.openmessaging.api.SendResult; + import org.apache.commons.lang3.StringUtils; -import org.apache.rocketmq.client.producer.SendCallback; -import org.apache.rocketmq.client.producer.SendResult; +import org.apache.eventmesh.common.protocol.tcp.Command; +import org.apache.eventmesh.common.protocol.tcp.EventMeshMessage; +import org.apache.eventmesh.common.protocol.tcp.Header; +import org.apache.eventmesh.common.protocol.tcp.OPStatus; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.send.EventMeshTcpSendResult; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.send.EventMeshTcpSendStatus; +import org.apache.eventmesh.runtime.util.EventMeshUtil; +import org.apache.eventmesh.runtime.util.Utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.concurrent.TimeUnit; - -import static com.webank.eventmesh.common.protocol.tcp.Command.RESPONSE_TO_SERVER; - public class MessageTransferTask extends AbstractTask { private final Logger messageLogger = LoggerFactory.getLogger("message"); private final int TRY_PERMIT_TIME_OUT = 5; - public MessageTransferTask(Package pkg, ChannelHandlerContext ctx, long startTime, ProxyTCPServer proxyTCPServer) { - super(pkg, ctx, startTime, proxyTCPServer); + public MessageTransferTask(Package pkg, ChannelHandlerContext ctx, long startTime, EventMeshTCPServer eventMeshTCPServer) { + super(pkg, ctx, startTime, eventMeshTCPServer); } @Override @@ -58,15 +59,15 @@ public void run() { Command cmd = pkg.getHeader().getCommand(); Command replyCmd = getReplyCmd(cmd); Package msg = new Package(); - AccessMessage accessMessage = (AccessMessage) pkg.getBody(); + EventMeshMessage eventMeshMessage = (EventMeshMessage) pkg.getBody(); int retCode = 0; - ProxyTcpSendResult sendStatus; + EventMeshTcpSendResult sendStatus; try { - if (accessMessage == null) { - throw new Exception("accessMessage is null"); + if (eventMeshMessage == null) { + throw new Exception("eventMeshMessage is null"); } - if (!cmd.equals(RESPONSE_TO_SERVER) && !proxyTCPServer.rateLimiter.tryAcquire(TRY_PERMIT_TIME_OUT, TimeUnit.MILLISECONDS)) { + if (!cmd.equals(RESPONSE_TO_SERVER) && !eventMeshTCPServer.getRateLimiter().tryAcquire(TRY_PERMIT_TIME_OUT, TimeUnit.MILLISECONDS)) { msg.setHeader(new Header(replyCmd, OPStatus.FAIL.getCode(), "Tps overload, global flow control", pkg.getHeader().getSeq())); ctx.writeAndFlush(msg).addListener( new ChannelFutureListener() { @@ -76,29 +77,25 @@ public void operationComplete(ChannelFuture future) throws Exception { } } ); - logger.warn("======Tps overload, global flow control, rate:{}! PLEASE CHECK!========", proxyTCPServer.rateLimiter.getRate()); + logger.warn("======Tps overload, global flow control, rate:{}! PLEASE CHECK!========", eventMeshTCPServer.getRateLimiter().getRate()); return; } synchronized (session) { long sendTime = System.currentTimeMillis(); - addTimestamp(accessMessage, cmd, sendTime); - if (cmd.equals(Command.REQUEST_TO_SERVER)) { - accessMessage.getProperties().put(DeFiBusConstant.PROPERTY_MESSAGE_REPLY_TO, session.getClientGroupWrapper() - .get().getMqProducerWrapper().getDefaultMQProducer().buildMQClientId()); - } + addTimestamp(eventMeshMessage, cmd, sendTime); - sendStatus = session.upstreamMsg(pkg.getHeader(), ProxyUtil.decodeMessage(accessMessage), createSendCallback(replyCmd, taskExecuteTime, accessMessage), startTime, taskExecuteTime); + sendStatus = session.upstreamMsg(pkg.getHeader(), EventMeshUtil.decodeMessage(eventMeshMessage), createSendCallback(replyCmd, taskExecuteTime, eventMeshMessage), startTime, taskExecuteTime); - if (StringUtils.equals(ProxyTcpSendStatus.SUCCESS.name(), sendStatus.getSendStatus().name())) { - messageLogger.info("pkg|proxy2mq|cmd={}|Msg={}|user={}|wait={}ms|cost={}ms", cmd, ProxyUtil.printMqMessage - (accessMessage), session.getClient(), taskExecuteTime - startTime, sendTime - startTime); + if (StringUtils.equals(EventMeshTcpSendStatus.SUCCESS.name(), sendStatus.getSendStatus().name())) { + messageLogger.info("pkg|eventMesh2mq|cmd={}|Msg={}|user={}|wait={}ms|cost={}ms", cmd, EventMeshUtil.printMqMessage + (eventMeshMessage), session.getClient(), taskExecuteTime - startTime, sendTime - startTime); } else { throw new Exception(sendStatus.getDetail()); } } } catch (Exception e) { - logger.error("MessageTransferTask failed|cmd={}|Msg={}|user={}|errMsg={}", cmd, accessMessage, session.getClient(), e); + logger.error("MessageTransferTask failed|cmd={}|Msg={}|user={}|errMsg={}", cmd, eventMeshMessage, session.getClient(), e); if (!cmd.equals(RESPONSE_TO_SERVER)) { msg.setHeader(new Header(replyCmd, OPStatus.FAIL.getCode(), e.getStackTrace().toString(), pkg.getHeader() .getSeq())); @@ -107,15 +104,15 @@ public void operationComplete(ChannelFuture future) throws Exception { } } - private void addTimestamp(AccessMessage accessMessage, Command cmd, long sendTime) { + private void addTimestamp(EventMeshMessage eventMeshMessage, Command cmd, long sendTime) { if (cmd.equals(RESPONSE_TO_SERVER)) { - accessMessage.getProperties().put(ProxyConstants.RSP_C2PROXY_TIMESTAMP, String.valueOf(startTime)); - accessMessage.getProperties().put(ProxyConstants.RSP_PROXY2MQ_TIMESTAMP, String.valueOf(sendTime)); - accessMessage.getProperties().put(ProxyConstants.RSP_SEND_PROXY_IP, proxyTCPServer.getAccessConfiguration().proxyServerIp); + eventMeshMessage.getProperties().put(EventMeshConstants.RSP_C2EVENTMESH_TIMESTAMP, String.valueOf(startTime)); + eventMeshMessage.getProperties().put(EventMeshConstants.RSP_EVENTMESH2MQ_TIMESTAMP, String.valueOf(sendTime)); + eventMeshMessage.getProperties().put(EventMeshConstants.RSP_SEND_EVENTMESH_IP, eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshServerIp); } else { - accessMessage.getProperties().put(ProxyConstants.REQ_C2PROXY_TIMESTAMP, String.valueOf(startTime)); - accessMessage.getProperties().put(ProxyConstants.REQ_PROXY2MQ_TIMESTAMP, String.valueOf(sendTime)); - accessMessage.getProperties().put(ProxyConstants.REQ_SEND_PROXY_IP, proxyTCPServer.getAccessConfiguration().proxyServerIp); + eventMeshMessage.getProperties().put(EventMeshConstants.REQ_C2EVENTMESH_TIMESTAMP, String.valueOf(startTime)); + eventMeshMessage.getProperties().put(EventMeshConstants.REQ_EVENTMESH2MQ_TIMESTAMP, String.valueOf(sendTime)); + eventMeshMessage.getProperties().put(EventMeshConstants.REQ_SEND_EVENTMESH_IP, eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshServerIp); } } @@ -132,7 +129,7 @@ private Command getReplyCmd(Command cmd) { } } - protected SendCallback createSendCallback(Command replyCmd, long taskExecuteTime, AccessMessage accessMessage) { + protected SendCallback createSendCallback(Command replyCmd, long taskExecuteTime, EventMeshMessage eventMeshMessage) { final long createTime = System.currentTimeMillis(); Package msg = new Package(); @@ -145,21 +142,32 @@ public void onSuccess(SendResult sendResult) { if (replyCmd.equals(Command.BROADCAST_MESSAGE_TO_SERVER_ACK) || replyCmd.equals(Command .ASYNC_MESSAGE_TO_SERVER_ACK)) { msg.setHeader(new Header(replyCmd, OPStatus.SUCCESS.getCode(), OPStatus.SUCCESS.getDesc(), pkg.getHeader().getSeq())); - msg.setBody(accessMessage); + msg.setBody(eventMeshMessage); Utils.writeAndFlush(msg, startTime, taskExecuteTime, session.getContext(), session); } } @Override - public void onException(Throwable e) { + public void onException(OnExceptionContext context) { session.getSender().getUpstreamBuff().release(); session.getSender().failMsgCount.incrementAndGet(); messageLogger.error("upstreamMsg mq message error|user={}|callback cost={}, errMsg={}", session.getClient(), String.valueOf - (System.currentTimeMillis() - createTime), new Exception(e)); - msg.setHeader(new Header(replyCmd, OPStatus.FAIL.getCode(), e.toString(), pkg.getHeader().getSeq())); - msg.setBody(accessMessage); + (System.currentTimeMillis() - createTime), new Exception(context.getException())); + msg.setHeader(new Header(replyCmd, OPStatus.FAIL.getCode(), context.getException().toString(), pkg.getHeader().getSeq())); + msg.setBody(eventMeshMessage); Utils.writeAndFlush(msg, startTime, taskExecuteTime, session.getContext(), session); } + +// @Override +// public void onException(Throwable e) { +// session.getSender().getUpstreamBuff().release(); +// session.getSender().failMsgCount.incrementAndGet(); +// messageLogger.error("upstreamMsg mq message error|user={}|callback cost={}, errMsg={}", session.getClient(), String.valueOf +// (System.currentTimeMillis() - createTime), new Exception(e)); +// msg.setHeader(new Header(replyCmd, OPStatus.FAIL.getCode(), e.toString(), pkg.getHeader().getSeq())); +// msg.setBody(accessMessage); +// Utils.writeAndFlush(msg, startTime, taskExecuteTime, session.getContext(), session); +// } }; } } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/SubscribeTask.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/SubscribeTask.java similarity index 70% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/SubscribeTask.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/SubscribeTask.java index 79e5065cc7..ccacf56b2d 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/SubscribeTask.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/SubscribeTask.java @@ -15,29 +15,28 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client.task; +package org.apache.eventmesh.runtime.core.protocol.tcp.client.task; + +import java.util.ArrayList; +import java.util.List; -import com.webank.emesher.boot.ProxyTCPServer; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Header; -import com.webank.eventmesh.common.protocol.tcp.OPStatus; -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.eventmesh.common.protocol.tcp.Subscription; -import com.webank.emesher.util.ProxyUtil; -import com.webank.emesher.util.Utils; import io.netty.channel.ChannelHandlerContext; + +import org.apache.eventmesh.common.protocol.tcp.Subscription; +import org.apache.eventmesh.common.protocol.SubscriptionItem; +import org.apache.eventmesh.common.protocol.tcp.*; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.apache.eventmesh.runtime.util.Utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.ArrayList; -import java.util.List; - public class SubscribeTask extends AbstractTask { private final Logger messageLogger = LoggerFactory.getLogger("message"); - public SubscribeTask(Package pkg, ChannelHandlerContext ctx, long startTime, ProxyTCPServer proxyTCPServer) { - super(pkg, ctx, startTime, proxyTCPServer); + public SubscribeTask(Package pkg, ChannelHandlerContext ctx, long startTime, EventMeshTCPServer eventMeshTCPServer) { + super(pkg, ctx, startTime, eventMeshTCPServer); } @Override @@ -50,17 +49,14 @@ public void run() { throw new Exception("subscriptionInfo is null"); } - List topicList = new ArrayList<>(); + List subscriptionItems = new ArrayList<>(); for (int i = 0; i < subscriptionInfo.getTopicList().size(); i++) { - String topic = subscriptionInfo.getTopicList().get(i); - if (!ProxyUtil.isValidRMBTopic(topic)) { - throw new Exception("invalid topic!"); - } - topicList.add(topic); + SubscriptionItem item = subscriptionInfo.getTopicList().get(i); + subscriptionItems.add(item); } synchronized (session) { - session.subscribe(topicList); - messageLogger.info("SubscribeTask succeed|user={}|topics={}", session.getClient(), topicList); + session.subscribe(subscriptionItems); + messageLogger.info("SubscribeTask succeed|user={}|topics={}", session.getClient(), subscriptionItems); } msg.setHeader(new Header(Command.SUBSCRIBE_RESPONSE, OPStatus.SUCCESS.getCode(), OPStatus.SUCCESS.getDesc(), pkg.getHeader() .getSeq())); diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/UnSubscribeTask.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/UnSubscribeTask.java similarity index 72% rename from eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/UnSubscribeTask.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/UnSubscribeTask.java index 2526808e2a..c35245adad 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/core/protocol/tcp/client/task/UnSubscribeTask.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/UnSubscribeTask.java @@ -15,28 +15,31 @@ * limitations under the License. */ -package com.webank.emesher.core.protocol.tcp.client.task; +package org.apache.eventmesh.runtime.core.protocol.tcp.client.task; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; -import com.webank.emesher.boot.ProxyTCPServer; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Header; -import com.webank.eventmesh.common.protocol.tcp.OPStatus; -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.emesher.util.Utils; import io.netty.channel.ChannelHandlerContext; + import org.apache.commons.collections4.MapUtils; +import org.apache.eventmesh.common.protocol.SubscriptionItem; +import org.apache.eventmesh.common.protocol.tcp.Command; +import org.apache.eventmesh.common.protocol.tcp.Header; +import org.apache.eventmesh.common.protocol.tcp.OPStatus; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.apache.eventmesh.runtime.util.Utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.ArrayList; -import java.util.List; - public class UnSubscribeTask extends AbstractTask { private final Logger messageLogger = LoggerFactory.getLogger("message"); - public UnSubscribeTask(Package pkg, ChannelHandlerContext ctx, long startTime, ProxyTCPServer proxyTCPServer) { - super(pkg, ctx, startTime, proxyTCPServer); + public UnSubscribeTask(Package pkg, ChannelHandlerContext ctx, long startTime, EventMeshTCPServer eventMeshTCPServer) { + super(pkg, ctx, startTime, eventMeshTCPServer); } @Override @@ -45,10 +48,10 @@ public void run() { Package msg = new Package(); try { synchronized (session) { - List topics = new ArrayList(); + List topics = new ArrayList(); if (MapUtils.isNotEmpty(session.getSessionContext().subscribeTopics)) { - for (String topic : session.getSessionContext().subscribeTopics.keySet()) { - topics.add(topic); + for (Map.Entry entry : session.getSessionContext().subscribeTopics.entrySet()) { + topics.add(entry.getValue()); } session.unsubscribe(topics); messageLogger.info("UnSubscriberTask succeed|user={}|topics={}", session.getClient(), topics); diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/domain/BytesMessageImpl.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/domain/BytesMessageImpl.java new file mode 100644 index 0000000000..a91c9a3381 --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/domain/BytesMessageImpl.java @@ -0,0 +1,113 @@ +///* +// * Licensed to the Apache Software Foundation (ASF) under one or more +// * contributor license agreements. See the NOTICE file distributed with +// * this work for additional information regarding copyright ownership. +// * The ASF licenses this file to You under the Apache License, Version 2.0 +// * (the "License"); you may not use this file except in compliance with +// * the License. You may obtain a copy of the License at +// * +// * http://www.apache.org/licenses/LICENSE-2.0 +// * +// * Unless required by applicable law or agreed to in writing, software +// * distributed under the License is distributed on an "AS IS" BASIS, +// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// * See the License for the specific language governing permissions and +// * limitations under the License. +// */ +//package org.apache.eventmesh.runtime.domain; +// +//import io.openmessaging.BytesMessage; +//import io.openmessaging.KeyValue; +//import io.openmessaging.Message; +//import io.openmessaging.OMS; +//import io.openmessaging.exception.OMSMessageFormatException; +//import org.apache.commons.lang3.builder.ToStringBuilder; +// +//public class BytesMessageImpl implements BytesMessage { +// private KeyValue sysHeaders; +// private KeyValue userHeaders; +// private byte[] body; +// +// public BytesMessageImpl() { +// this.sysHeaders = OMS.newKeyValue(); +// this.userHeaders = OMS.newKeyValue(); +// } +// +// @Override +// public T getBody(Class type) throws OMSMessageFormatException { +// if (type == byte[].class) { +// return (T)body; +// } +// +// throw new OMSMessageFormatException("", "Cannot assign byte[] to " + type.getName()); +// } +// +// @Override +// public BytesMessage setBody(final byte[] body) { +// this.body = body; +// return this; +// } +// +// @Override +// public KeyValue sysHeaders() { +// return sysHeaders; +// } +// +// @Override +// public KeyValue userHeaders() { +// return userHeaders; +// } +// +// @Override +// public Message putSysHeaders(String key, int value) { +// sysHeaders.put(key, value); +// return this; +// } +// +// @Override +// public Message putSysHeaders(String key, long value) { +// sysHeaders.put(key, value); +// return this; +// } +// +// @Override +// public Message putSysHeaders(String key, double value) { +// sysHeaders.put(key, value); +// return this; +// } +// +// @Override +// public Message putSysHeaders(String key, String value) { +// sysHeaders.put(key, value); +// return this; +// } +// +// @Override +// public Message putUserHeaders(String key, int value) { +// userHeaders.put(key, value); +// return this; +// } +// +// @Override +// public Message putUserHeaders(String key, long value) { +// userHeaders.put(key, value); +// return this; +// } +// +// @Override +// public Message putUserHeaders(String key, double value) { +// userHeaders.put(key, value); +// return this; +// } +// +// @Override +// public Message putUserHeaders(String key, String value) { +// userHeaders.put(key, value); +// return this; +// } +// +// @Override +// public String toString() { +// return ToStringBuilder.reflectionToString(this); +// } +//} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/domain/ConsumeRequest.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/domain/ConsumeRequest.java new file mode 100644 index 0000000000..38c9b2e58c --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/domain/ConsumeRequest.java @@ -0,0 +1,55 @@ +///* +// * Licensed to the Apache Software Foundation (ASF) under one or more +// * contributor license agreements. See the NOTICE file distributed with +// * this work for additional information regarding copyright ownership. +// * The ASF licenses this file to You under the Apache License, Version 2.0 +// * (the "License"); you may not use this file except in compliance with +// * the License. You may obtain a copy of the License at +// * +// * http://www.apache.org/licenses/LICENSE-2.0 +// * +// * Unless required by applicable law or agreed to in writing, software +// * distributed under the License is distributed on an "AS IS" BASIS, +// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// * See the License for the specific language governing permissions and +// * limitations under the License. +// */ +//package com.webank.runtime.domain; +// +//import org.apache.rocketmq.client.impl.consumer.ProcessQueue; +//import org.apache.rocketmq.common.message.MessageExt; +//import org.apache.rocketmq.common.message.MessageQueue; +// +//public class ConsumeRequest { +// private final MessageExt messageExt; +// private final MessageQueue messageQueue; +// private final ProcessQueue processQueue; +// private long startConsumeTimeMillis; +// +// public ConsumeRequest(final MessageExt messageExt, final MessageQueue messageQueue, +// final ProcessQueue processQueue) { +// this.messageExt = messageExt; +// this.messageQueue = messageQueue; +// this.processQueue = processQueue; +// } +// +// public MessageExt getMessageExt() { +// return messageExt; +// } +// +// public MessageQueue getMessageQueue() { +// return messageQueue; +// } +// +// public ProcessQueue getProcessQueue() { +// return processQueue; +// } +// +// public long getStartConsumeTimeMillis() { +// return startConsumeTimeMillis; +// } +// +// public void setStartConsumeTimeMillis(final long startConsumeTimeMillis) { +// this.startConsumeTimeMillis = startConsumeTimeMillis; +// } +//} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/domain/NonStandardKeys.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/domain/NonStandardKeys.java new file mode 100644 index 0000000000..779b3dccef --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/domain/NonStandardKeys.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.eventmesh.runtime.domain; + +public interface NonStandardKeys { + String CONSUMER_GROUP = "rmq.consumer.group"; + String PRODUCER_GROUP = "rmq.producer.group"; + String MAX_REDELIVERY_TIMES = "rmq.max.redelivery.times"; + String MESSAGE_CONSUME_TIMEOUT = "rmq.message.consume.timeout"; + String MAX_CONSUME_THREAD_NUMS = "rmq.max.consume.thread.nums"; + String MIN_CONSUME_THREAD_NUMS = "rmq.min.consume.thread.nums"; + String MESSAGE_CONSUME_STATUS = "rmq.message.consume.status"; + String MESSAGE_DESTINATION = "rmq.message.destination"; + String PULL_MESSAGE_BATCH_NUMS = "rmq.pull.message.batch.nums"; + String PULL_MESSAGE_CACHE_CAPACITY = "rmq.pull.message.cache.capacity"; +} diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/UnSubscribeRequestHeader.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/domain/RocketMQConstants.java similarity index 80% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/UnSubscribeRequestHeader.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/domain/RocketMQConstants.java index e1ee35bde9..a8f08533df 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/header/client/UnSubscribeRequestHeader.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/domain/RocketMQConstants.java @@ -14,8 +14,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package org.apache.eventmesh.runtime.domain; -package com.webank.eventmesh.common.protocol.http.header.client; +public interface RocketMQConstants { + + /** + * Key of scheduled message delivery time + */ + String START_DELIVER_TIME = "__STARTDELIVERTIME"; -public class UnSubscribeRequestHeader { } diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/domain/SendResultImpl.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/domain/SendResultImpl.java new file mode 100644 index 0000000000..3a8d12f999 --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/domain/SendResultImpl.java @@ -0,0 +1,39 @@ +///* +// * Licensed to the Apache Software Foundation (ASF) under one or more +// * contributor license agreements. See the NOTICE file distributed with +// * this work for additional information regarding copyright ownership. +// * The ASF licenses this file to You under the Apache License, Version 2.0 +// * (the "License"); you may not use this file except in compliance with +// * the License. You may obtain a copy of the License at +// * +// * http://www.apache.org/licenses/LICENSE-2.0 +// * +// * Unless required by applicable law or agreed to in writing, software +// * distributed under the License is distributed on an "AS IS" BASIS, +// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// * See the License for the specific language governing permissions and +// * limitations under the License. +// */ +//package org.apache.eventmesh.runtime.domain; +// +//import io.openmessaging.KeyValue; +//import io.openmessaging.producer.SendResult; +// +//public class SendResultImpl implements SendResult { +// private String messageId; +// private KeyValue properties; +// +// public SendResultImpl(final String messageId, final KeyValue properties) { +// this.messageId = messageId; +// this.properties = properties; +// } +// +// @Override +// public String messageId() { +// return messageId; +// } +// +// public KeyValue properties() { +// return properties; +// } +//} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/metrics/MonitorMetricConstants.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/metrics/MonitorMetricConstants.java similarity index 69% rename from eventmesh-emesher/src/main/java/com/webank/emesher/metrics/MonitorMetricConstants.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/metrics/MonitorMetricConstants.java index 3482a0e3d5..60a2360ca1 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/metrics/MonitorMetricConstants.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/metrics/MonitorMetricConstants.java @@ -15,17 +15,17 @@ * limitations under the License. */ -package com.webank.emesher.metrics; +package org.apache.eventmesh.runtime.metrics; public class MonitorMetricConstants { - public static final String PROXY_MONITOR_FORMAT_COMMON = "{\"protocol\":\"%s\",\"s\":\"%s\",\"t\":\"%s\"}"; + public static final String EVENTMESH_MONITOR_FORMAT_COMMON = "{\"protocol\":\"%s\",\"s\":\"%s\",\"t\":\"%s\"}"; - public static final String PROXY_TCP_MONITOR_FORMAT_THREADPOOL = "{\"threadPoolName\":\"%s\",\"s\":\"%s\",\"t\":\"%s\"}"; + public static final String EVENTMESH_TCP_MONITOR_FORMAT_THREADPOOL = "{\"threadPoolName\":\"%s\",\"s\":\"%s\",\"t\":\"%s\"}"; - public static final String CLIENT_2_PROXY_TPS = "client2proxyTPS"; - public static final String PROXY_2_MQ_TPS = "proxy2mqTPS"; - public static final String MQ_2_PROXY_TPS = "mq2proxyTPS"; - public static final String PROXY_2_CLIENT_TPS = "proxy2clientTPS"; + public static final String CLIENT_2_EVENTMESH_TPS = "client2eventMeshTPS"; + public static final String EVENTMESH_2_MQ_TPS = "eventMesh2mqTPS"; + public static final String MQ_2_EVENTMESH_TPS = "mq2eventMeshTPS"; + public static final String EVENTMESH_2_CLIENT_TPS = "eventMesh2clientTPS"; public static final String ALL_TPS = "allTPS"; public static final String CONNECTION = "connection"; public static final String SUB_TOPIC_NUM = "subTopicNum"; diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/metrics/http/GroupMetrics.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/metrics/http/GroupMetrics.java similarity index 75% rename from eventmesh-emesher/src/main/java/com/webank/emesher/metrics/http/GroupMetrics.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/metrics/http/GroupMetrics.java index 667b97ddb3..75f3d5d939 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/metrics/http/GroupMetrics.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/metrics/http/GroupMetrics.java @@ -15,19 +15,20 @@ * limitations under the License. */ -package com.webank.emesher.metrics.http; +package org.apache.eventmesh.runtime.metrics.http; -import com.webank.emesher.boot.ProxyHTTPServer; import com.codahale.metrics.MetricRegistry; +import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer; + public class GroupMetrics { - private ProxyHTTPServer proxyHTTPServer; + private EventMeshHTTPServer eventMeshHTTPServer; private MetricRegistry metricRegistry; - public GroupMetrics(ProxyHTTPServer proxyHTTPServer, MetricRegistry metricRegistry) { - this.proxyHTTPServer = proxyHTTPServer; + public GroupMetrics(EventMeshHTTPServer eventMeshHTTPServer, MetricRegistry metricRegistry) { + this.eventMeshHTTPServer = eventMeshHTTPServer; this.metricRegistry = metricRegistry; } } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/metrics/http/HTTPMetricsServer.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/metrics/http/HTTPMetricsServer.java similarity index 76% rename from eventmesh-emesher/src/main/java/com/webank/emesher/metrics/http/HTTPMetricsServer.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/metrics/http/HTTPMetricsServer.java index 52bc2a7b43..3e333a0446 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/metrics/http/HTTPMetricsServer.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/metrics/http/HTTPMetricsServer.java @@ -15,12 +15,7 @@ * limitations under the License. */ -package com.webank.emesher.metrics.http; - -import com.webank.emesher.boot.ProxyHTTPServer; -import com.codahale.metrics.MetricRegistry; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +package org.apache.eventmesh.runtime.metrics.http; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -28,9 +23,15 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; +import com.codahale.metrics.MetricRegistry; + +import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class HTTPMetricsServer { - private ProxyHTTPServer proxyHTTPServer; + private EventMeshHTTPServer eventMeshHTTPServer; private MetricRegistry metricRegistry = new MetricRegistry(); @@ -46,15 +47,15 @@ public class HTTPMetricsServer { private Logger logger = LoggerFactory.getLogger(this.getClass()); - public HTTPMetricsServer(ProxyHTTPServer proxyHTTPServer) { - this.proxyHTTPServer = proxyHTTPServer; + public HTTPMetricsServer(EventMeshHTTPServer eventMeshHTTPServer) { + this.eventMeshHTTPServer = eventMeshHTTPServer; } public void init() throws Exception { - summaryMetrics = new SummaryMetrics(this.proxyHTTPServer, this.metricRegistry); - topicMetrics = new TopicMetrics(this.proxyHTTPServer, this.metricRegistry); - groupMetrics = new GroupMetrics(this.proxyHTTPServer, this.metricRegistry); - healthMetrics = new HealthMetrics(this.proxyHTTPServer, this.metricRegistry); + summaryMetrics = new SummaryMetrics(this.eventMeshHTTPServer, this.metricRegistry); + topicMetrics = new TopicMetrics(this.eventMeshHTTPServer, this.metricRegistry); + groupMetrics = new GroupMetrics(this.eventMeshHTTPServer, this.metricRegistry); + healthMetrics = new HealthMetrics(this.eventMeshHTTPServer, this.metricRegistry); logger.info("HTTPMetricsServer inited......"); } @@ -68,7 +69,7 @@ public void run() { summaryMetrics.snapshotSendMsgTPS(); summaryMetrics.snapshotPushMsgTPS(); } catch (Exception ex) { - logger.warn("proxy snapshot tps metrics err", ex); + logger.warn("eventMesh snapshot tps metrics err", ex); } } }, 0, 1000, TimeUnit.MILLISECONDS); @@ -79,7 +80,7 @@ public void run() { try { logPrintServerMetrics(); } catch (Exception ex) { - logger.warn("proxy print metrics err", ex); + logger.warn("eventMesh print metrics err", ex); } } }, 1000, SummaryMetrics.STATIC_PERIOD, TimeUnit.MILLISECONDS); @@ -98,7 +99,7 @@ public void shutdown() throws Exception { @Override public Thread newThread(Runnable r) { seq.incrementAndGet(); - Thread t = new Thread(r, "proxy-metrics-" + seq.get()); + Thread t = new Thread(r, "eventMesh-metrics-" + seq.get()); t.setDaemon(true); return t; } @@ -107,7 +108,7 @@ public Thread newThread(Runnable r) { private void logPrintServerMetrics() { httpLogger.info("===========================================SERVER METRICS=================================================="); - httpLogger.info(String.format(SummaryMetrics.PROXY_MONITOR_FORMAT_HTTP, + httpLogger.info(String.format(SummaryMetrics.EVENTMESH_MONITOR_FORMAT_HTTP, summaryMetrics.maxHTTPTPS(), summaryMetrics.avgHTTPTPS(), summaryMetrics.maxHTTPCost(), @@ -116,7 +117,7 @@ private void logPrintServerMetrics() { summaryMetrics.getHttpDiscard())); summaryMetrics.httpStatInfoClear(); - httpLogger.info(String.format(SummaryMetrics.PROXY_MONITOR_FORMAT_BATCHSENDMSG, + httpLogger.info(String.format(SummaryMetrics.EVENTMESH_MONITOR_FORMAT_BATCHSENDMSG, summaryMetrics.maxSendBatchMsgTPS(), summaryMetrics.avgSendBatchMsgTPS(), summaryMetrics.getSendBatchMsgNumSum(), @@ -126,7 +127,7 @@ private void logPrintServerMetrics() { )); summaryMetrics.cleanSendBatchStat(); - httpLogger.info(String.format(SummaryMetrics.PROXY_MONITOR_FORMAT_SENDMSG, + httpLogger.info(String.format(SummaryMetrics.EVENTMESH_MONITOR_FORMAT_SENDMSG, summaryMetrics.maxSendMsgTPS(), summaryMetrics.avgSendMsgTPS(), summaryMetrics.getSendMsgNumSum(), @@ -137,7 +138,7 @@ private void logPrintServerMetrics() { )); summaryMetrics.cleanSendMsgStat(); - httpLogger.info(String.format(SummaryMetrics.PROXY_MONITOR_FORMAT_PUSHMSG, + httpLogger.info(String.format(SummaryMetrics.EVENTMESH_MONITOR_FORMAT_PUSHMSG, summaryMetrics.maxPushMsgTPS(), summaryMetrics.avgPushMsgTPS(), summaryMetrics.getHttpPushMsgNumSum(), @@ -148,13 +149,13 @@ private void logPrintServerMetrics() { )); summaryMetrics.cleanHttpPushMsgStat(); - httpLogger.info(String.format(SummaryMetrics.PROXY_MONITOR_FORMAT_BLOCKQ, - proxyHTTPServer.getBatchMsgExecutor().getQueue().size(), - proxyHTTPServer.getSendMsgExecutor().getQueue().size(), - proxyHTTPServer.getPushMsgExecutor().getQueue().size(), - proxyHTTPServer.getHttpRetryer().size())); + httpLogger.info(String.format(SummaryMetrics.EVENTMESH_MONITOR_FORMAT_BLOCKQ, + eventMeshHTTPServer.getBatchMsgExecutor().getQueue().size(), + eventMeshHTTPServer.getSendMsgExecutor().getQueue().size(), + eventMeshHTTPServer.getPushMsgExecutor().getQueue().size(), + eventMeshHTTPServer.getHttpRetryer().size())); - httpLogger.info(String.format(SummaryMetrics.PROXY_MONITOR_FORMAT_MQ_CLIENT, + httpLogger.info(String.format(SummaryMetrics.EVENTMESH_MONITOR_FORMAT_MQ_CLIENT, summaryMetrics.avgBatchSendMsgCost(), summaryMetrics.avgSendMsgCost(), summaryMetrics.avgReplyMsgCost())); diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/metrics/http/HealthMetrics.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/metrics/http/HealthMetrics.java similarity index 77% rename from eventmesh-emesher/src/main/java/com/webank/emesher/metrics/http/HealthMetrics.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/metrics/http/HealthMetrics.java index a36a375ab4..6e59fb6369 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/metrics/http/HealthMetrics.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/metrics/http/HealthMetrics.java @@ -15,22 +15,23 @@ * limitations under the License. */ -package com.webank.emesher.metrics.http; +package org.apache.eventmesh.runtime.metrics.http; -import com.webank.emesher.boot.ProxyHTTPServer; import com.codahale.metrics.MetricRegistry; + +import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class HealthMetrics { - private ProxyHTTPServer proxyHTTPServer; + private EventMeshHTTPServer eventMeshHTTPServer; private MetricRegistry metricRegistry; public Logger logger = LoggerFactory.getLogger("httpMonitor"); - public HealthMetrics(ProxyHTTPServer proxyHTTPServer, MetricRegistry metricRegistry) { - this.proxyHTTPServer = proxyHTTPServer; + public HealthMetrics(EventMeshHTTPServer eventMeshHTTPServer, MetricRegistry metricRegistry) { + this.eventMeshHTTPServer = eventMeshHTTPServer; this.metricRegistry = metricRegistry; } } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/metrics/http/SummaryMetrics.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/metrics/http/SummaryMetrics.java similarity index 91% rename from eventmesh-emesher/src/main/java/com/webank/emesher/metrics/http/SummaryMetrics.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/metrics/http/SummaryMetrics.java index 237c6f9012..01ef0439cd 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/metrics/http/SummaryMetrics.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/metrics/http/SummaryMetrics.java @@ -15,28 +15,29 @@ * limitations under the License. */ -package com.webank.emesher.metrics.http; - -import com.webank.emesher.boot.ProxyHTTPServer; -import com.codahale.metrics.MetricRegistry; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +package org.apache.eventmesh.runtime.metrics.http; import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; import java.util.concurrent.atomic.AtomicLong; +import com.codahale.metrics.MetricRegistry; + +import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class SummaryMetrics { public Logger logger = LoggerFactory.getLogger("httpMonitor"); - private ProxyHTTPServer proxyHTTPServer; + private EventMeshHTTPServer eventMeshHTTPServer; private MetricRegistry metricRegistry; - public SummaryMetrics(ProxyHTTPServer proxyHTTPServer, MetricRegistry metricRegistry) { - this.proxyHTTPServer = proxyHTTPServer; + public SummaryMetrics(EventMeshHTTPServer eventMeshHTTPServer, MetricRegistry metricRegistry) { + this.eventMeshHTTPServer = eventMeshHTTPServer; this.metricRegistry = metricRegistry; } @@ -58,7 +59,7 @@ private float avg(LinkedList linkedList) { } //////////////////////////////////////////////////////////////////////////////// - public static final String PROXY_MONITOR_FORMAT_HTTP = "{\"maxHTTPTPS\":\"%.1f\",\"avgHTTPTPS\":\"%.1f\"," + //PROXY 接受外部HTTP 请求的TPS相关 + public static final String EVENTMESH_MONITOR_FORMAT_HTTP = "{\"maxHTTPTPS\":\"%.1f\",\"avgHTTPTPS\":\"%.1f\"," + //EVENTMESH 接受外部HTTP 请求的TPS相关 "\"maxHTTPCOST\":\"%s\",\"avgHTTPCOST\":\"%.1f\",\"avgHTTPBodyDecodeCost\":\"%.1f\", \"httpDiscard\":\"%s\"}"; private float wholeCost = 0f; @@ -147,7 +148,7 @@ public float avgHTTPBodyDecodeCost() { ////////////////////////////////////////////////////////////////////////// - public static final String PROXY_MONITOR_FORMAT_BATCHSENDMSG = "{\"maxBatchSendMsgTPS\":\"%.1f\",\"avgBatchSendMsgTPS\":\"%.1f\"," + + public static final String EVENTMESH_MONITOR_FORMAT_BATCHSENDMSG = "{\"maxBatchSendMsgTPS\":\"%.1f\",\"avgBatchSendMsgTPS\":\"%.1f\"," + " \"sum\":\"%s\", \"sumFail\":\"%s\", \"sumFailRate\":\"%.2f\", \"discard\":\"%s\"}"; private AtomicLong sendBatchMsgNumPerSecond = new AtomicLong(0); @@ -215,7 +216,7 @@ public long getSendBatchMsgDiscardNumSum() { } ////////////////////////////////////////////////////////////////////////// - public static final String PROXY_MONITOR_FORMAT_SENDMSG = "{\"maxSendMsgTPS\":\"%.1f\",\"avgSendMsgTPS\":\"%.1f\"," + + public static final String EVENTMESH_MONITOR_FORMAT_SENDMSG = "{\"maxSendMsgTPS\":\"%.1f\",\"avgSendMsgTPS\":\"%.1f\"," + " \"sum\":\"%s\", \"sumFail\":\"%s\", \"sumFailRate\":\"%.2f\", \"replyMsg\":\"%s\", \"replyFail\":\"%s\"}"; private AtomicLong sendMsgNumSum = new AtomicLong(0); @@ -294,7 +295,7 @@ public void cleanSendMsgStat() { } //////////////////////////////////////////////////////////////////////////// - public static final String PROXY_MONITOR_FORMAT_PUSHMSG = "{\"maxPushMsgTPS\":\"%.1f\",\"avgPushMsgTPS\":\"%.1f\"," + + public static final String EVENTMESH_MONITOR_FORMAT_PUSHMSG = "{\"maxPushMsgTPS\":\"%.1f\",\"avgPushMsgTPS\":\"%.1f\"," + " \"sum\":\"%s\", \"sumFail\":\"%s\", \"sumFailRate\":\"%.1f\", \"maxClientLatency\":\"%.1f\", \"avgClientLatency\":\"%.1f\"}"; private float wholePushCost = 0f; @@ -376,11 +377,11 @@ public void cleanHttpPushMsgStat() { } /////////////////////////////////////////////////////////////////////////////////////////////////////////////// - public static final String PROXY_MONITOR_FORMAT_BLOCKQ = "{\"batchMsgQ\":\"%s\",\"sendMsgQ\":\"%s\"," + + public static final String EVENTMESH_MONITOR_FORMAT_BLOCKQ = "{\"batchMsgQ\":\"%s\",\"sendMsgQ\":\"%s\"," + "\"pushMsgQ\":\"%s\",\"httpRetryQ\":\"%s\"}"; /////////////////////////////////////////////////////////////////////////// - public static final String PROXY_MONITOR_FORMAT_MQ_CLIENT = "{\"batchAvgSend2MQCost\":\"%.1f\", \"avgSend2MQCost\":\"%.1f\", \"avgReply2MQCost\":\"%.1f\"}"; + public static final String EVENTMESH_MONITOR_FORMAT_MQ_CLIENT = "{\"batchAvgSend2MQCost\":\"%.1f\", \"avgSend2MQCost\":\"%.1f\", \"avgReply2MQCost\":\"%.1f\"}"; private float batchSend2MQWholeCost = 0f; diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/metrics/http/TcpSummaryMetrics.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/metrics/http/TcpSummaryMetrics.java similarity index 90% rename from eventmesh-emesher/src/main/java/com/webank/emesher/metrics/http/TcpSummaryMetrics.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/metrics/http/TcpSummaryMetrics.java index 08461655dc..4c69c67729 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/metrics/http/TcpSummaryMetrics.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/metrics/http/TcpSummaryMetrics.java @@ -15,27 +15,28 @@ * limitations under the License. */ -package com.webank.emesher.metrics.http; - -import com.webank.emesher.boot.ProxyHTTPServer; -import com.codahale.metrics.MetricRegistry; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +package org.apache.eventmesh.runtime.metrics.http; import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; import java.util.concurrent.atomic.AtomicLong; +import com.codahale.metrics.MetricRegistry; + +import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class TcpSummaryMetrics { public Logger logger = LoggerFactory.getLogger("httpMonitor"); - private ProxyHTTPServer proxyHTTPServer; + private EventMeshHTTPServer eventMeshHTTPServer; private MetricRegistry metricRegistry; - public TcpSummaryMetrics(ProxyHTTPServer proxyHTTPServer, MetricRegistry metricRegistry) { - this.proxyHTTPServer = proxyHTTPServer; + public TcpSummaryMetrics(EventMeshHTTPServer eventMeshHTTPServer, MetricRegistry metricRegistry) { + this.eventMeshHTTPServer = eventMeshHTTPServer; this.metricRegistry = metricRegistry; } @@ -57,7 +58,7 @@ private float avg(LinkedList linkedList) { } //////////////////////////////////////////////////////////////////////////////// - public static final String PROXY_MONITOR_FORMAT_HTTP = "%15s : {\"maxHTTPTPS\":\"%.1f\",\"avgHTTPTPS\":\"%.1f\"," + //PROXY 接受外部HTTP 请求的TPS相关 + public static final String EVENTMESH_MONITOR_FORMAT_HTTP = "%15s : {\"maxHTTPTPS\":\"%.1f\",\"avgHTTPTPS\":\"%.1f\"," + //EVENTMESH 接受外部HTTP 请求的TPS相关 "\"maxHTTPCOST\":\"%s\",\"avgHTTPCOST\":\"%.1f\",\"avgHTTPBodyDecodeCost\":\"%.1f\"}"; private float wholeCost = 0f; @@ -135,7 +136,7 @@ public float avgHTTPBodyDecodeCost() { ////////////////////////////////////////////////////////////////////////// - public static final String PROXY_MONITOR_FORMAT_BATCHSENDMSG = "%15s : {\"maxBatchSendMsgTPS\":\"%.1f\",\"avgBatchSendMsgTPS\":\"%.1f\"," + + public static final String EVENTMESH_MONITOR_FORMAT_BATCHSENDMSG = "%15s : {\"maxBatchSendMsgTPS\":\"%.1f\",\"avgBatchSendMsgTPS\":\"%.1f\"," + " \"sum\":\"%s\", \"sumFail\":\"%s\", \"sumFailRate\":\"%.2f\", \"discard\":\"%s\"}"; private AtomicLong sendBatchMsgNumPerSecond = new AtomicLong(0); @@ -202,7 +203,7 @@ public long getSendBatchMsgDiscardNumSum() { } ////////////////////////////////////////////////////////////////////////// - public static final String PROXY_MONITOR_FORMAT_SENDMSG = "%15s : {\"maxSendMsgTPS\":\"%.1f\",\"avgSendMsgTPS\":\"%.1f\"," + + public static final String EVENTMESH_MONITOR_FORMAT_SENDMSG = "%15s : {\"maxSendMsgTPS\":\"%.1f\",\"avgSendMsgTPS\":\"%.1f\"," + " \"sum\":\"%s\", \"sumFail\":\"%s\", \"sumFailRate\":\"%.2f\", \"discard\":\"%s\"}"; private AtomicLong sendMsgNumSum = new AtomicLong(0); @@ -269,7 +270,7 @@ public long getSendMsgDiscardNumSum() { } //////////////////////////////////////////////////////////////////////////// - public static final String PROXY_MONITOR_FORMAT_PUSHMSG = "%15s : {\"maxPushMsgTPS\":\"%.1f\",\"avgPushMsgTPS\":\"%.1f\"," + + public static final String EVENTMESH_MONITOR_FORMAT_PUSHMSG = "%15s : {\"maxPushMsgTPS\":\"%.1f\",\"avgPushMsgTPS\":\"%.1f\"," + " \"sum\":\"%s\", \"sumFail\":\"%s\", \"sumFailRate\":\"%.1f\", \"maxClientLatency\":\"%.1f\", \"avgClientLatency\":\"%.1f\"}"; private float wholePushCost = 0f; @@ -351,11 +352,11 @@ public void cleanHttpPushMsgStat() { } /////////////////////////////////////////////////////////////////////////////////////////////////////////////// - public static final String PROXY_MONITOR_FORMAT_BLOCKQ = "%15s : {\"batchMsgQ\":\"%s\",\"sendMsgQ\":\"%s\"," + + public static final String EVENTMESH_MONITOR_FORMAT_BLOCKQ = "%15s : {\"batchMsgQ\":\"%s\",\"sendMsgQ\":\"%s\"," + "\"pushMsgQ\":\"%s\",\"consumeRetryQ\":\"%s\"}"; /////////////////////////////////////////////////////////////////////////// - public static final String PROXY_MONITOR_FORMAT_MQ_CLIENT = "%15s : {\"batchAvgSend2MQCost\":\"%.1f\", \"avgSend2MQCost\":\"%.1f\"}"; + public static final String EVENTMESH_MONITOR_FORMAT_MQ_CLIENT = "%15s : {\"batchAvgSend2MQCost\":\"%.1f\", \"avgSend2MQCost\":\"%.1f\"}"; private float batchSend2MQWholeCost = 0f; diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/metrics/http/TopicMetrics.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/metrics/http/TopicMetrics.java similarity index 75% rename from eventmesh-emesher/src/main/java/com/webank/emesher/metrics/http/TopicMetrics.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/metrics/http/TopicMetrics.java index a72a7d9d88..6a0afabdc9 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/metrics/http/TopicMetrics.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/metrics/http/TopicMetrics.java @@ -15,18 +15,19 @@ * limitations under the License. */ -package com.webank.emesher.metrics.http; +package org.apache.eventmesh.runtime.metrics.http; -import com.webank.emesher.boot.ProxyHTTPServer; import com.codahale.metrics.MetricRegistry; +import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer; + public class TopicMetrics { - private ProxyHTTPServer proxyHTTPServer; + private EventMeshHTTPServer eventMeshHTTPServer; private MetricRegistry metricRegistry; - public TopicMetrics(ProxyHTTPServer proxyHTTPServer, MetricRegistry metricRegistry) { - this.proxyHTTPServer = proxyHTTPServer; + public TopicMetrics(EventMeshHTTPServer eventMeshHTTPServer, MetricRegistry metricRegistry) { + this.eventMeshHTTPServer = eventMeshHTTPServer; this.metricRegistry = metricRegistry; } } diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/metrics/tcp/EventMeshTcpMonitor.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/metrics/tcp/EventMeshTcpMonitor.java new file mode 100644 index 0000000000..7120218517 --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/metrics/tcp/EventMeshTcpMonitor.java @@ -0,0 +1,169 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.metrics.tcp; + +import java.net.InetSocketAddress; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; + +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.EventMeshTcpConnectionHandler; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session; +import org.apache.eventmesh.runtime.metrics.MonitorMetricConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class EventMeshTcpMonitor { + + private EventMeshTCPServer eventMeshTCPServer; + + private final Logger tcpLogger = LoggerFactory.getLogger("tcpMonitor"); + + private final Logger appLogger = LoggerFactory.getLogger("appMonitor"); + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + private static int delay = 60 * 1000; + + private static int period = 60 * 1000; + + private static int PRINT_THREADPOOLSTATE_INTERVAL = 1; + + private AtomicInteger client2eventMeshMsgNum; + private AtomicInteger eventMesh2mqMsgNum; + private AtomicInteger mq2eventMeshMsgNum; + private AtomicInteger eventMesh2clientMsgNum; + + private int client2eventMeshTPS; + private int eventMesh2clientTPS; + private int eventMesh2mqTPS; + private int mq2eventMeshTPS; + private int allTPS; + private int subTopicNum; + + public ScheduledFuture monitorTpsTask; + + public ScheduledFuture monitorThreadPoolTask; + + public EventMeshTcpMonitor(EventMeshTCPServer eventMeshTCPServer) { + this.eventMeshTCPServer = eventMeshTCPServer; + } + + public void init() throws Exception { + this.client2eventMeshMsgNum = new AtomicInteger(0); + this.eventMesh2mqMsgNum = new AtomicInteger(0); + this.mq2eventMeshMsgNum = new AtomicInteger(0); + this.eventMesh2clientMsgNum = new AtomicInteger(0); + logger.info("EventMeshTcpMonitor inited......"); + } + + public void start() throws Exception { + monitorTpsTask = eventMeshTCPServer.getScheduler().scheduleAtFixedRate((new Runnable() { + @Override + public void run() { + int msgNum = client2eventMeshMsgNum.intValue(); + client2eventMeshMsgNum = new AtomicInteger(0); + client2eventMeshTPS = 1000 * msgNum / period; + + msgNum = eventMesh2clientMsgNum.intValue(); + eventMesh2clientMsgNum = new AtomicInteger(0); + eventMesh2clientTPS = 1000 * msgNum / period; + + msgNum = eventMesh2mqMsgNum.intValue(); + eventMesh2mqMsgNum = new AtomicInteger(0); + eventMesh2mqTPS = 1000 * msgNum / period; + + msgNum = mq2eventMeshMsgNum.intValue(); + mq2eventMeshMsgNum = new AtomicInteger(0); + mq2eventMeshTPS = 1000 * msgNum / period; + + allTPS = client2eventMeshTPS + eventMesh2clientTPS; + + //count topics subscribed by client in this eventMesh + ConcurrentHashMap sessionMap = eventMeshTCPServer.getClientSessionGroupMapping().getSessionMap(); + Iterator sessionIterator = sessionMap.values().iterator(); + Set topicSet = new HashSet<>(); + while (sessionIterator.hasNext()) { + Session session = sessionIterator.next(); + AtomicLong deliveredMsgsCount = session.getPusher().getDeliveredMsgsCount(); + AtomicLong deliveredFailCount = session.getPusher().getDeliverFailMsgsCount(); + int unAckMsgsCount = session.getPusher().getTotalUnackMsgs(); + int sendTopics = session.getSessionContext().sendTopics.size(); + int subscribeTopics = session.getSessionContext().subscribeTopics.size(); + + tcpLogger.info("session|deliveredFailCount={}|deliveredMsgsCount={}|unAckMsgsCount={}|sendTopics={}|subscribeTopics={}|user={}", + deliveredFailCount.longValue(), deliveredMsgsCount.longValue(), + unAckMsgsCount, sendTopics, subscribeTopics, session.getClient()); + + topicSet.addAll(session.getSessionContext().subscribeTopics.keySet()); + } + subTopicNum = topicSet.size(); + + appLogger.info(String.format(MonitorMetricConstants.EVENTMESH_MONITOR_FORMAT_COMMON, EventMeshConstants.PROTOCOL_TCP, MonitorMetricConstants.CLIENT_2_EVENTMESH_TPS, client2eventMeshTPS)); + appLogger.info(String.format(MonitorMetricConstants.EVENTMESH_MONITOR_FORMAT_COMMON, EventMeshConstants.PROTOCOL_TCP, MonitorMetricConstants.EVENTMESH_2_MQ_TPS, eventMesh2mqTPS)); + appLogger.info(String.format(MonitorMetricConstants.EVENTMESH_MONITOR_FORMAT_COMMON, EventMeshConstants.PROTOCOL_TCP, MonitorMetricConstants.MQ_2_EVENTMESH_TPS, mq2eventMeshTPS)); + appLogger.info(String.format(MonitorMetricConstants.EVENTMESH_MONITOR_FORMAT_COMMON, EventMeshConstants.PROTOCOL_TCP, MonitorMetricConstants.EVENTMESH_2_CLIENT_TPS, eventMesh2clientTPS)); + appLogger.info(String.format(MonitorMetricConstants.EVENTMESH_MONITOR_FORMAT_COMMON, EventMeshConstants.PROTOCOL_TCP, MonitorMetricConstants.ALL_TPS, allTPS)); + appLogger.info(String.format(MonitorMetricConstants.EVENTMESH_MONITOR_FORMAT_COMMON, EventMeshConstants.PROTOCOL_TCP, MonitorMetricConstants.CONNECTION, EventMeshTcpConnectionHandler.connections)); + appLogger.info(String.format(MonitorMetricConstants.EVENTMESH_MONITOR_FORMAT_COMMON, EventMeshConstants.PROTOCOL_TCP, MonitorMetricConstants.SUB_TOPIC_NUM, subTopicNum)); + } + }), delay, period, TimeUnit.MILLISECONDS); + + monitorThreadPoolTask = eventMeshTCPServer.getScheduler().scheduleAtFixedRate(new Runnable() { + @Override + public void run() { +// ThreadPoolHelper.printThreadPoolState(); + eventMeshTCPServer.getEventMeshTcpRetryer().printRetryThreadPoolState(); + + //monitor retry queue size + int retrySize = eventMeshTCPServer.getEventMeshTcpRetryer().getRetrySize(); + appLogger.info(String.format(MonitorMetricConstants.EVENTMESH_MONITOR_FORMAT_COMMON, EventMeshConstants.PROTOCOL_TCP, MonitorMetricConstants.RETRY_QUEUE_SIZE, retrySize)); + } + }, 10, PRINT_THREADPOOLSTATE_INTERVAL, TimeUnit.SECONDS); + logger.info("EventMeshTcpMonitor started......"); + } + + public void shutdown() throws Exception { + monitorTpsTask.cancel(true); + monitorThreadPoolTask.cancel(true); + logger.info("EventMeshTcpMonitor shutdown......"); + } + + public AtomicInteger getClient2EventMeshMsgNum() { + return client2eventMeshMsgNum; + } + + public AtomicInteger getEventMesh2mqMsgNum() { + return eventMesh2mqMsgNum; + } + + public AtomicInteger getMq2EventMeshMsgNum() { + return mq2eventMeshMsgNum; + } + + public AtomicInteger getEventMesh2clientMsgNum() { + return eventMesh2clientMsgNum; + } +} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/patch/EventMeshConsumeConcurrentlyContext.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/patch/EventMeshConsumeConcurrentlyContext.java new file mode 100644 index 0000000000..80c90bf4a5 --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/patch/EventMeshConsumeConcurrentlyContext.java @@ -0,0 +1,44 @@ +///* +// * Licensed to the Apache Software Foundation (ASF) under one or more +// * contributor license agreements. See the NOTICE file distributed with +// * this work for additional information regarding copyright ownership. +// * The ASF licenses this file to You under the Apache License, Version 2.0 +// * (the "License"); you may not use this file except in compliance with +// * the License. You may obtain a copy of the License at +// * +// * http://www.apache.org/licenses/LICENSE-2.0 +// * +// * Unless required by applicable law or agreed to in writing, software +// * distributed under the License is distributed on an "AS IS" BASIS, +// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// * See the License for the specific language governing permissions and +// * limitations under the License. +// */ +// +//package org.apache.eventmesh.runtime.patch; +// +//import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext; +//import org.apache.rocketmq.client.impl.consumer.ProcessQueue; +//import org.apache.rocketmq.common.message.MessageQueue; +// +//public class EventMeshConsumeConcurrentlyContext extends ConsumeConcurrentlyContext { +// private final ProcessQueue processQueue; +// private boolean manualAck = true; +// +// public EventMeshConsumeConcurrentlyContext(MessageQueue messageQueue, ProcessQueue processQueue) { +// super(messageQueue); +// this.processQueue = processQueue; +// } +// +// public ProcessQueue getProcessQueue() { +// return processQueue; +// } +// +// public boolean isManualAck() { +// return manualAck; +// } +// +// public void setManualAck(boolean manualAck) { +// this.manualAck = manualAck; +// } +//} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/patch/ProxyConsumeConcurrentlyStatus.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/patch/EventMeshConsumeConcurrentlyStatus.java similarity index 91% rename from eventmesh-emesher/src/main/java/com/webank/emesher/patch/ProxyConsumeConcurrentlyStatus.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/patch/EventMeshConsumeConcurrentlyStatus.java index 06c6033c36..008f459f18 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/patch/ProxyConsumeConcurrentlyStatus.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/patch/EventMeshConsumeConcurrentlyStatus.java @@ -15,9 +15,9 @@ * limitations under the License. */ -package com.webank.emesher.patch; +package org.apache.eventmesh.runtime.patch; -public enum ProxyConsumeConcurrentlyStatus { +public enum EventMeshConsumeConcurrentlyStatus { /** * Success consumption */ diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/patch/EventMeshMessageListenerConcurrently.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/patch/EventMeshMessageListenerConcurrently.java new file mode 100644 index 0000000000..202c0e19d1 --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/patch/EventMeshMessageListenerConcurrently.java @@ -0,0 +1,69 @@ +///* +// * Licensed to the Apache Software Foundation (ASF) under one or more +// * contributor license agreements. See the NOTICE file distributed with +// * this work for additional information regarding copyright ownership. +// * The ASF licenses this file to You under the Apache License, Version 2.0 +// * (the "License"); you may not use this file except in compliance with +// * the License. You may obtain a copy of the License at +// * +// * http://www.apache.org/licenses/LICENSE-2.0 +// * +// * Unless required by applicable law or agreed to in writing, software +// * distributed under the License is distributed on an "AS IS" BASIS, +// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// * See the License for the specific language governing permissions and +// * limitations under the License. +// */ +// +//package org.apache.eventmesh.runtime.patch; +// +//import org.apache.commons.collections4.CollectionUtils; +//import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext; +//import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus; +//import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently; +//import org.apache.rocketmq.common.message.MessageExt; +//import org.slf4j.Logger; +//import org.slf4j.LoggerFactory; +// +//import java.util.List; +// +//public abstract class EventMeshMessageListenerConcurrently implements MessageListenerConcurrently { +// +// private static final Logger LOG = LoggerFactory.getLogger(EventMeshMessageListenerConcurrently.class); +// +// @Override +// public ConsumeConcurrentlyStatus consumeMessage(final List msgs, +// final ConsumeConcurrentlyContext context) { +// ConsumeConcurrentlyStatus status = null; +// +// if (CollectionUtils.isEmpty(msgs)) { +// return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; +// } +// +// MessageExt msg = msgs.get(0); +// try { +// EventMeshConsumeConcurrentlyContext eventMeshConsumeConcurrentlyContext = (EventMeshConsumeConcurrentlyContext) context; +// EventMeshConsumeConcurrentlyStatus eventMeshConsumeStatus = handleMessage(msg, eventMeshConsumeConcurrentlyContext); +// try { +// switch (eventMeshConsumeStatus) { +// case CONSUME_SUCCESS: +// return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; +// case RECONSUME_LATER: +// return ConsumeConcurrentlyStatus.RECONSUME_LATER; +// case CONSUME_FINISH: +// eventMeshConsumeConcurrentlyContext.setManualAck(true); +// return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; +// } +// } catch (Throwable e) { +// LOG.info("handleMessage fail", e); +// return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; +// } +// } catch (Throwable e) { +// LOG.info("handleMessage fail", e); +// return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; +// } +// return status; +// } +// +// public abstract EventMeshConsumeConcurrentlyStatus handleMessage(MessageExt msg, EventMeshConsumeConcurrentlyContext context); +//} diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/util/ProxyThreadFactoryImpl.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/EventMeshThreadFactoryImpl.java similarity index 79% rename from eventmesh-emesher/src/main/java/com/webank/emesher/util/ProxyThreadFactoryImpl.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/EventMeshThreadFactoryImpl.java index 010b6fb6d6..cf83904115 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/util/ProxyThreadFactoryImpl.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/EventMeshThreadFactoryImpl.java @@ -15,21 +15,21 @@ * limitations under the License. */ -package com.webank.emesher.util; +package org.apache.eventmesh.runtime.util; import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicLong; -public class ProxyThreadFactoryImpl implements ThreadFactory { +public class EventMeshThreadFactoryImpl implements ThreadFactory { private final AtomicLong threadIndex = new AtomicLong(0); private final String threadNamePrefix; private Boolean isDaemonSpecified = null; - public ProxyThreadFactoryImpl(final String threadNamePrefix) { + public EventMeshThreadFactoryImpl(final String threadNamePrefix) { this.threadNamePrefix = threadNamePrefix; } - public ProxyThreadFactoryImpl(final String threadNamePrefix, final boolean isDaemonSpecified) { + public EventMeshThreadFactoryImpl(final String threadNamePrefix, final boolean isDaemonSpecified) { this.threadNamePrefix = threadNamePrefix; this.isDaemonSpecified = isDaemonSpecified; } @@ -40,7 +40,7 @@ public String getThreadNamePrefix() { @Override public Thread newThread(Runnable r) { - Thread t = new Thread(r, threadNamePrefix +'-'+ this.threadIndex.incrementAndGet()); + Thread t = new Thread(r, threadNamePrefix + '-' + this.threadIndex.incrementAndGet()); if (isDaemonSpecified != null) { t.setDaemon(isDaemonSpecified); } diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/util/ProxyUtil.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/EventMeshUtil.java similarity index 56% rename from eventmesh-emesher/src/main/java/com/webank/emesher/util/ProxyUtil.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/EventMeshUtil.java index c9dae00589..eca44b9ee6 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/util/ProxyUtil.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/EventMeshUtil.java @@ -15,152 +15,68 @@ * limitations under the License. */ -package com.webank.emesher.util; +package org.apache.eventmesh.runtime.util; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.constants.ProxyVersion; -import com.webank.eventmesh.common.ThreadUtil; -import com.webank.eventmesh.common.protocol.tcp.AccessMessage; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; -import org.apache.commons.lang3.ArrayUtils; -import org.apache.commons.lang3.RandomStringUtils; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import static org.apache.eventmesh.runtime.util.OMSUtil.isOMSHeader; import java.net.Inet6Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Enumeration; import java.util.Map; +import java.util.Properties; +import java.util.Set; import java.util.TimeZone; -public class ProxyUtil { +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; + +import io.openmessaging.api.Message; + +import org.apache.commons.lang3.ArrayUtils; +import org.apache.commons.lang3.RandomStringUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.ThreadUtil; +import org.apache.eventmesh.common.protocol.tcp.EventMeshMessage; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.constants.EventMeshVersion; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class EventMeshUtil { - public static Logger logger = LoggerFactory.getLogger(ProxyUtil.class); + public static Logger logger = LoggerFactory.getLogger(EventMeshUtil.class); public static String buildPushMsgSeqNo() { return StringUtils.rightPad(String.valueOf(System.currentTimeMillis()), 6) + String.valueOf(RandomStringUtils.randomNumeric(4)); } - public static String buildProxyClientID(String clientGroup, String proxyRegion, String proxyCluster) { + public static String buildMeshClientID(String clientGroup, String meshCluster) { return StringUtils.trim(clientGroup) - + "-" + StringUtils.trim(proxyRegion) + "(" + StringUtils.trim(proxyCluster) + ")" - + "-" + ProxyVersion.getCurrentVersionDesc() + + "(" + StringUtils.trim(meshCluster) + ")" + + "-" + EventMeshVersion.getCurrentVersionDesc() + "-" + ThreadUtil.getPID(); } - public static String buildProxyTcpClientID(String clientSysId, String clientDcn, String purpose, String proxyCluster) { + public static String buildMeshTcpClientID(String clientSysId, String purpose, String meshCluster) { return StringUtils.trim(clientSysId) - + "-" + StringUtils.trim(clientDcn) + "-" + StringUtils.trim(purpose) - + "-" + StringUtils.trim(proxyCluster) - + "-" + ProxyVersion.getCurrentVersionDesc() + + "-" + StringUtils.trim(meshCluster) + + "-" + EventMeshVersion.getCurrentVersionDesc() + "-" + ThreadUtil.getPID(); } - public static String buildProxyTcpRRReplyerProducerGroup() { - return "Proxy-Tcp-RRReplyer"; - } - - public static String buildProxyTcpRRReplyerClientID(String proxySysId, String proxyRegion, String proxyDcn, String proxyCluster) { - return proxySysId - + "-" + StringUtils.trim(proxyRegion) - + "-" + StringUtils.trim(proxyDcn) - + "-" + StringUtils.trim(proxyCluster) - + "-" + ProxyVersion.getCurrentVersionDesc() - + "-" + ThreadUtil.getPID() - + "-RRReplyer"; - //return ProxyVersion.getCurrentVersionDesc() + "-" + ThreadUtil.getPID() + "(" + proxyConfiguration.proxyCluster + ")"; - } - - public static String buildBroadcastClientConsumerGroup(String systemId, String dcn) { - return ProxyConstants.CONSUMER_GROUP_NAME_PREFIX + ProxyConstants.BROADCAST_PREFIX + systemId + "-" + dcn; - } - - public static String buildPersistentClientConsumerGroup(String systemId, String dcn) { - return ProxyConstants.CONSUMER_GROUP_NAME_PREFIX + systemId + "-" + dcn; - } - public static String buildClientGroup(String systemId, String dcn) { - return systemId + "-" + dcn; - } - - public static String buildClientProducerGroup(String systemId, String dcn) { - return ProxyConstants.PRODUCER_GROUP_NAME_PREFIX + systemId + "-" + dcn; - } - - public static String buildCCAddr(String str) { - return str + "/namesrvAddr"; - } - - public static String buildCCAddr(String str, String idc) { - return str + "/namesrvAddr/" + idc; - } - - public static boolean isValidRMBTopic(String topic) { - if (StringUtils.isEmpty(topic) || !StringUtils.contains(topic, "-")) { - return false; - } - - String[] args = StringUtils.split(topic, "-"); - if (ArrayUtils.getLength(args) != 5) { - return false; - } - - String s0e = args[1]; - if (!StringUtils.equalsIgnoreCase("s", s0e) && !StringUtils.equalsIgnoreCase("e", s0e)) { - return false; - } - - String service = args[2]; - if (!StringUtils.isNumeric(service)) { - return false; - } - - return true; - } - - public static String getServiceIDStr(String topic) { - if (!isValidRMBTopic(topic)) { - return ""; - } - - String[] args = StringUtils.split(topic, "-"); - return args[2]; - } - - public static String getPidStr(String topic) { - if (!isValidRMBTopic(topic)) { - return ""; - } - - String[] args = StringUtils.split(topic, "-"); - return args[3]; - } - - public static boolean isService(String topic) { - String serviceStr = getServiceIDStr(topic); - if (StringUtils.isEmpty(serviceStr)) { - return false; - } - return "0".equals(StringUtils.substring(serviceStr, 3, 4)); - } - - public static boolean isBroadcast(String topic) { - String serviceStr = getServiceIDStr(topic); - if (StringUtils.isEmpty(serviceStr)) { - return false; - } - String[] args = StringUtils.split(topic, "-"); - return "3".equals(StringUtils.substring(args[2], 3, 4)) || "4".equals(StringUtils.substring(args[2], 3, 4)); + public static String buildClientGroup(String systemId) { + return systemId; } /** @@ -205,51 +121,111 @@ public static ObjectMapper createJsoner() { /** * 打印mq消息的一部分内容 * - * @param accessMessage + * @param eventMeshMessage * @return */ - public static String printMqMessage(AccessMessage accessMessage) { - Map properties = accessMessage.getProperties(); + public static String printMqMessage(EventMeshMessage eventMeshMessage) { + Map properties = eventMeshMessage.getProperties(); - String keys = properties.get(ProxyConstants.KEYS_UPPERCASE); + String keys = properties.get(EventMeshConstants.KEYS_UPPERCASE); if (!StringUtils.isNotBlank(keys)) { - keys = properties.get(ProxyConstants.KEYS_LOWERCASE); + keys = properties.get(EventMeshConstants.KEYS_LOWERCASE); } - String result = String.format("Message [topic=%s,TTL=%s,uniqueId=%s,bizSeq=%s]", accessMessage.getTopic(), - properties.get(ProxyConstants.TTL), properties.get(ProxyConstants.RR_REQUEST_UNIQ_ID), keys); + String result = String.format("Message [topic=%s,TTL=%s,uniqueId=%s,bizSeq=%s]", eventMeshMessage.getTopic(), + properties.get(EventMeshConstants.TTL), properties.get(EventMeshConstants.RR_REQUEST_UNIQ_ID), keys); return result; } - public static String getMessageBizSeq(org.apache.rocketmq.common.message.Message msg) { - Map properties = msg.getProperties(); + public static String getMessageBizSeq(Message msg) { + Properties properties = msg.getSystemProperties(); - String keys = properties.get(ProxyConstants.KEYS_UPPERCASE); + String keys = properties.getProperty(EventMeshConstants.KEYS_UPPERCASE); if (!StringUtils.isNotBlank(keys)) { - keys = properties.get(ProxyConstants.KEYS_LOWERCASE); + keys = properties.getProperty(EventMeshConstants.KEYS_LOWERCASE); } return keys; } - public static org.apache.rocketmq.common.message.Message decodeMessage(AccessMessage accessMessage) { - org.apache.rocketmq.common.message.Message msg = new org.apache.rocketmq.common.message.Message(); - msg.setTopic(accessMessage.getTopic()); - msg.setBody(accessMessage.getBody().getBytes()); - msg.getProperty("init"); - for (Map.Entry property : accessMessage.getProperties().entrySet()) { - msg.getProperties().put(property.getKey(), property.getValue()); +// public static org.apache.rocketmq.common.message.Message decodeMessage(AccessMessage accessMessage) { +// org.apache.rocketmq.common.message.Message msg = new org.apache.rocketmq.common.message.Message(); +// msg.setTopic(accessMessage.getTopic()); +// msg.setBody(accessMessage.getBody().getBytes()); +// msg.getProperty("init"); +// for (Map.Entry property : accessMessage.getProperties().entrySet()) { +// msg.getProperties().put(property.getKey(), property.getValue()); +// } +// return msg; +// } + + public static Message decodeMessage(EventMeshMessage eventMeshMessage) { + Message omsMsg = new Message(); + omsMsg.setBody(eventMeshMessage.getBody().getBytes()); + omsMsg.setTopic(eventMeshMessage.getTopic()); + Properties systemProperties = new Properties(); + Properties userProperties = new Properties(); + + final Set> entries = eventMeshMessage.getProperties().entrySet(); + + for (final Map.Entry entry : entries) { + if (isOMSHeader(entry.getKey())) { + systemProperties.put(entry.getKey(), entry.getValue()); + } else { + userProperties.put(entry.getKey(), entry.getValue()); + } } - return msg; + + systemProperties.put(Constants.PROPERTY_MESSAGE_DESTINATION, eventMeshMessage.getTopic()); + omsMsg.setSystemProperties(systemProperties); + omsMsg.setUserProperties(userProperties); + return omsMsg; } - public static AccessMessage encodeMessage(org.apache.rocketmq.common.message.Message msg) throws Exception { - AccessMessage accessMessage = new AccessMessage(); - accessMessage.setBody(new String(msg.getBody(), "UTF-8")); - accessMessage.setTopic(msg.getTopic()); - for (Map.Entry property : msg.getProperties().entrySet()) { - accessMessage.getProperties().put(property.getKey(), property.getValue()); +// public static AccessMessage encodeMessage(org.apache.rocketmq.common.message.Message msg) throws Exception { +// AccessMessage accessMessage = new AccessMessage(); +// accessMessage.setBody(new String(msg.getBody(), "UTF-8")); +// accessMessage.setTopic(msg.getTopic()); +// for (Map.Entry property : msg.getProperties().entrySet()) { +// accessMessage.getProperties().put(property.getKey(), property.getValue()); +// } +// return accessMessage; +// } + + public static EventMeshMessage encodeMessage(Message omsMessage) throws Exception { + + EventMeshMessage eventMeshMessage = new EventMeshMessage(); + eventMeshMessage.setBody(new String(omsMessage.getBody(), StandardCharsets.UTF_8)); + + Properties sysHeaders = omsMessage.getSystemProperties(); + Properties userHeaders = omsMessage.getUserProperties(); + + //All destinations in RocketMQ use Topic + eventMeshMessage.setTopic(sysHeaders.getProperty(Constants.PROPERTY_MESSAGE_DESTINATION)); + + if (sysHeaders.containsKey("START_TIME")) { + long deliverTime; + if (StringUtils.isBlank(sysHeaders.getProperty("START_TIME"))) { + deliverTime = 0; + } else { + deliverTime = Long.parseLong(sysHeaders.getProperty("START_TIME")); + } + + if (deliverTime > 0) { +// rmqMessage.putUserProperty(RocketMQConstants.START_DELIVER_TIME, String.valueOf(deliverTime)); + eventMeshMessage.getProperties().put("START_TIME", String.valueOf(deliverTime)); + } } - return accessMessage; + + for (String key : userHeaders.stringPropertyNames()) { + eventMeshMessage.getProperties().put(key, userHeaders.getProperty(key)); + } + + //System headers has a high priority + for (String key : sysHeaders.stringPropertyNames()) { + eventMeshMessage.getProperties().put(key, sysHeaders.getProperty(key)); + } + + return eventMeshMessage; } public static String getLocalAddr() { @@ -344,13 +320,13 @@ private static void getIpResult(ArrayList ipv4Result, ArrayList } } - public static String buildUserAgentClientId(UserAgent client){ - if(client == null){ + public static String buildUserAgentClientId(UserAgent client) { + if (client == null) { return null; } StringBuilder sb = new StringBuilder(); sb.append(client.getSubsystem()).append("-") - .append(client.getDcn()).append("-") + .append("-") .append(client.getPid()).append("-") .append(client.getHost()).append(":").append(client.getPort()); return sb.toString(); diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/HttpTinyClient.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/HttpTinyClient.java new file mode 100644 index 0000000000..f3a930ca0b --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/HttpTinyClient.java @@ -0,0 +1,136 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.util; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLEncoder; +import java.util.Iterator; +import java.util.List; + +import org.apache.eventmesh.runtime.constants.EventMeshConstants; + +public class HttpTinyClient { + + static public HttpResult httpGet(String url, List headers, List paramValues, + String encoding, long readTimeoutMs) throws IOException { + String encodedContent = encodingParams(paramValues, encoding); + url += (null == encodedContent) ? "" : ("?" + encodedContent); + + HttpURLConnection conn = null; + try { + conn = (HttpURLConnection) new URL(url).openConnection(); + conn.setRequestMethod("GET"); + conn.setConnectTimeout((int) readTimeoutMs); + conn.setReadTimeout((int) readTimeoutMs); + setHeaders(conn, headers, encoding); + + conn.connect(); + int respCode = conn.getResponseCode(); + String resp = null; + + if (HttpURLConnection.HTTP_OK == respCode) { + resp = IOTinyUtils.toString(conn.getInputStream(), encoding); + } else { + resp = IOTinyUtils.toString(conn.getErrorStream(), encoding); + } + return new HttpResult(respCode, resp); + } finally { + if (conn != null) { + conn.disconnect(); + } + } + } + + static private String encodingParams(List paramValues, String encoding) + throws UnsupportedEncodingException { + StringBuilder sb = new StringBuilder(); + if (null == paramValues) { + return null; + } + + for (Iterator iter = paramValues.iterator(); iter.hasNext(); ) { + sb.append(iter.next()).append("="); + sb.append(URLEncoder.encode(iter.next(), encoding)); + if (iter.hasNext()) { + sb.append("&"); + } + } + return sb.toString(); + } + + static private void setHeaders(HttpURLConnection conn, List headers, String encoding) { + if (null != headers) { + for (Iterator iter = headers.iterator(); iter.hasNext(); ) { + conn.addRequestProperty(iter.next(), iter.next()); + } + } +// conn.addRequestProperty("Client-Version", MQVersion.getVersionDesc(MQVersion.CURRENT_VERSION)); + conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + encoding); + + String ts = String.valueOf(System.currentTimeMillis()); + conn.addRequestProperty("Metaq-Client-RequestTS", ts); + } + + /** + * @return the http response of given http post request + */ + static public HttpResult httpPost(String url, List headers, List paramValues, + String encoding, long readTimeoutMs) throws IOException { + String encodedContent = encodingParams(paramValues, encoding); + + HttpURLConnection conn = null; + try { + conn = (HttpURLConnection) new URL(url).openConnection(); + conn.setRequestMethod("POST"); + conn.setConnectTimeout(3000); + conn.setReadTimeout((int) readTimeoutMs); + conn.setDoOutput(true); + conn.setDoInput(true); + setHeaders(conn, headers, encoding); + + conn.getOutputStream().write(encodedContent.getBytes(EventMeshConstants.DEFAULT_CHARSET)); + + int respCode = conn.getResponseCode(); + String resp = null; + + if (HttpURLConnection.HTTP_OK == respCode) { + resp = IOTinyUtils.toString(conn.getInputStream(), encoding); + } else { + resp = IOTinyUtils.toString(conn.getErrorStream(), encoding); + } + return new HttpResult(respCode, resp); + } finally { + if (null != conn) { + conn.disconnect(); + } + } + } + + static public class HttpResult { + final public int code; + final public String content; + + public HttpResult(int code, String content) { + this.code = code; + this.content = content; + } + } +} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/IOTinyUtils.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/IOTinyUtils.java new file mode 100644 index 0000000000..7ce4d154aa --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/IOTinyUtils.java @@ -0,0 +1,159 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.util; + +import java.io.BufferedReader; +import java.io.CharArrayWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.Reader; +import java.io.Writer; +import java.nio.channels.FileChannel; +import java.util.ArrayList; +import java.util.List; + +import org.apache.eventmesh.runtime.constants.EventMeshConstants; + +public class IOTinyUtils { + + static public String toString(InputStream input, String encoding) throws IOException { + return (null == encoding) ? toString(new InputStreamReader(input, EventMeshConstants.DEFAULT_CHARSET)) : toString(new InputStreamReader( + input, encoding)); + } + + static public String toString(Reader reader) throws IOException { + CharArrayWriter sw = new CharArrayWriter(); + copy(reader, sw); + return sw.toString(); + } + + static public long copy(Reader input, Writer output) throws IOException { + char[] buffer = new char[1 << 12]; + long count = 0; + for (int n = 0; (n = input.read(buffer)) >= 0; ) { + output.write(buffer, 0, n); + count += n; + } + return count; + } + + static public List readLines(Reader input) throws IOException { + BufferedReader reader = toBufferedReader(input); + List list = new ArrayList(); + String line; + for (; ; ) { + line = reader.readLine(); + if (null != line) { + list.add(line); + } else { + break; + } + } + return list; + } + + static private BufferedReader toBufferedReader(Reader reader) { + return reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader); + } + + static public void copyFile(String source, String target) throws IOException { + File sf = new File(source); + if (!sf.exists()) { + throw new IllegalArgumentException("source file does not exist."); + } + File tf = new File(target); + tf.getParentFile().mkdirs(); + if (!tf.exists() && !tf.createNewFile()) { + throw new RuntimeException("failed to create target file."); + } + + FileChannel sc = null; + FileChannel tc = null; + try { + tc = new FileOutputStream(tf).getChannel(); + sc = new FileInputStream(sf).getChannel(); + sc.transferTo(0, sc.size(), tc); + } finally { + if (null != sc) { + sc.close(); + } + if (null != tc) { + tc.close(); + } + } + } + + public static void delete(File fileOrDir) throws IOException { + if (fileOrDir == null) { + return; + } + + if (fileOrDir.isDirectory()) { + cleanDirectory(fileOrDir); + } + + fileOrDir.delete(); + } + + public static void cleanDirectory(File directory) throws IOException { + if (!directory.exists()) { + String message = directory + " does not exist"; + throw new IllegalArgumentException(message); + } + + if (!directory.isDirectory()) { + String message = directory + " is not a directory"; + throw new IllegalArgumentException(message); + } + + File[] files = directory.listFiles(); + if (files == null) { // null if security restricted + throw new IOException("Failed to list contents of " + directory); + } + + IOException exception = null; + for (File file : files) { + try { + delete(file); + } catch (IOException ioe) { + exception = ioe; + } + } + + if (null != exception) { + throw exception; + } + } + + public static void writeStringToFile(File file, String data, String encoding) throws IOException { + OutputStream os = null; + try { + os = new FileOutputStream(file); + os.write(data.getBytes(encoding)); + } finally { + if (null != os) { + os.close(); + } + } + } +} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/NetUtils.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/NetUtils.java new file mode 100644 index 0000000000..a2563bce3b --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/NetUtils.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.runtime.util; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.UnsupportedEncodingException; +import java.net.InetSocketAddress; +import java.net.URLDecoder; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class NetUtils { + + private static final Logger logger = LoggerFactory.getLogger(NetUtils.class); + + /** + * Transform the url form string to Map + * + * @param formData + * @return + */ + public static Map formData2Dic(String formData) { + Map result = new HashMap<>(); + if (formData == null || formData.trim().length() == 0) { + return result; + } + final String[] items = formData.split("&"); + Arrays.stream(items).forEach(item -> { + final String[] keyAndVal = item.split("="); + if (keyAndVal.length == 2) { + try { + final String key = URLDecoder.decode(keyAndVal[0], "utf8"); + final String val = URLDecoder.decode(keyAndVal[1], "utf8"); + result.put(key, val); + } catch (UnsupportedEncodingException e) { + logger.warn("formData2Dic:param decode failed...", e); + } + } + }); + return result; + } + + public static String addressToString(List clients) { + if (clients.isEmpty()) { + return "no session had been closed"; + } + StringBuilder sb = new StringBuilder(); + for (InetSocketAddress addr : clients) { + sb.append(addr).append("|"); + } + return sb.toString(); + } +} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/OMSUtil.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/OMSUtil.java new file mode 100644 index 0000000000..32e310f84e --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/OMSUtil.java @@ -0,0 +1,68 @@ +/* + * Licensed to Apache Software Foundation (ASF) under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Apache Software Foundation (ASF) licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.eventmesh.runtime.util; + +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import io.openmessaging.api.Message; +import io.openmessaging.api.OMSBuiltinKeys; + +public class OMSUtil { + + public static boolean isOMSHeader(String value) { + for (Field field : OMSBuiltinKeys.class.getDeclaredFields()) { + try { + if (field.get(OMSBuiltinKeys.class).equals(value)) { + return true; + } + } catch (IllegalAccessException e) { + return false; + } + } + return false; + } + +// public static Properties convertKeyValue2Prop(KeyValue keyValue){ +// Properties properties = new Properties(); +// for (String key : keyValue.keySet()){ +// properties.put(key, keyValue.getString(key)); +// } +// return properties; +// } + + @SuppressWarnings("unchecked") + public static Map combineProp(Properties p1, Properties p2) { + Properties properties = new Properties(); + properties.putAll(p1); + properties.putAll(p2); + + return new HashMap<>((Map) properties); + } + + public static Map getMessageProp(Message message) { + Properties p1 = message.getSystemProperties(); + Properties p2 = message.getUserProperties(); + return combineProp(p1, p2); + } + +} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/RemotingHelper.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/RemotingHelper.java new file mode 100644 index 0000000000..0266a924dd --- /dev/null +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/RemotingHelper.java @@ -0,0 +1,181 @@ +/* + * Licensed to Apache Software Foundation (ASF) under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Apache Software Foundation (ASF) licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.eventmesh.runtime.util; + +import java.net.InetSocketAddress; +import java.net.SocketAddress; + +import io.netty.channel.Channel; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RemotingHelper { + public static final String DEFAULT_CHARSET = "UTF-8"; + + public static Logger logger = LoggerFactory.getLogger(RemotingHelper.class); + + public static String exceptionSimpleDesc(final Throwable e) { + StringBuilder sb = new StringBuilder(); + if (e != null) { + sb.append(e.toString()); + + StackTraceElement[] stackTrace = e.getStackTrace(); + if (stackTrace != null && stackTrace.length > 0) { + StackTraceElement elment = stackTrace[0]; + sb.append(", "); + sb.append(elment.toString()); + } + } + + return sb.toString(); + } + + public static SocketAddress string2SocketAddress(final String addr) { + int split = addr.lastIndexOf(":"); + String host = addr.substring(0, split); + String port = addr.substring(split + 1); + InetSocketAddress isa = new InetSocketAddress(host, Integer.parseInt(port)); + return isa; + } + +// public static RemotingCommand invokeSync(final String addr, final RemotingCommand request, +// final long timeoutMillis) throws InterruptedException, RemotingConnectException, +// RemotingSendRequestException, RemotingTimeoutException { +// long beginTime = System.currentTimeMillis(); +// SocketAddress socketAddress = RemotingUtil.string2SocketAddress(addr); +// SocketChannel socketChannel = RemotingUtil.connect(socketAddress); +// if (socketChannel != null) { +// boolean sendRequestOK = false; +// +// try { +// +// socketChannel.configureBlocking(true); +// +// //bugfix http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4614802 +// socketChannel.socket().setSoTimeout((int) timeoutMillis); +// +// ByteBuffer byteBufferRequest = request.encode(); +// while (byteBufferRequest.hasRemaining()) { +// int length = socketChannel.write(byteBufferRequest); +// if (length > 0) { +// if (byteBufferRequest.hasRemaining()) { +// if ((System.currentTimeMillis() - beginTime) > timeoutMillis) { +// +// throw new RemotingSendRequestException(addr); +// } +// } +// } else { +// throw new RemotingSendRequestException(addr); +// } +// +// Thread.sleep(1); +// } +// +// sendRequestOK = true; +// +// ByteBuffer byteBufferSize = ByteBuffer.allocate(4); +// while (byteBufferSize.hasRemaining()) { +// int length = socketChannel.read(byteBufferSize); +// if (length > 0) { +// if (byteBufferSize.hasRemaining()) { +// if ((System.currentTimeMillis() - beginTime) > timeoutMillis) { +// +// throw new RemotingTimeoutException(addr, timeoutMillis); +// } +// } +// } else { +// throw new RemotingTimeoutException(addr, timeoutMillis); +// } +// +// Thread.sleep(1); +// } +// +// int size = byteBufferSize.getInt(0); +// ByteBuffer byteBufferBody = ByteBuffer.allocate(size); +// while (byteBufferBody.hasRemaining()) { +// int length = socketChannel.read(byteBufferBody); +// if (length > 0) { +// if (byteBufferBody.hasRemaining()) { +// if ((System.currentTimeMillis() - beginTime) > timeoutMillis) { +// +// throw new RemotingTimeoutException(addr, timeoutMillis); +// } +// } +// } else { +// throw new RemotingTimeoutException(addr, timeoutMillis); +// } +// +// Thread.sleep(1); +// } +// +// byteBufferBody.flip(); +// return RemotingCommand.decode(byteBufferBody); +// } catch (IOException e) { +// log.error("invokeSync failure", e); +// +// if (sendRequestOK) { +// throw new RemotingTimeoutException(addr, timeoutMillis); +// } else { +// throw new RemotingSendRequestException(addr); +// } +// } finally { +// try { +// socketChannel.close(); +// } catch (IOException e) { +// e.printStackTrace(); +// } +// } +// } else { +// throw new RemotingConnectException(addr); +// } +// } + + public static String parseChannelRemoteAddr(final Channel channel) { + if (null == channel) { + return ""; + } + SocketAddress remote = channel.remoteAddress(); + final String addr = remote != null ? remote.toString() : ""; + + if (addr.length() > 0) { + int index = addr.lastIndexOf("/"); + if (index >= 0) { + return addr.substring(index + 1); + } + + return addr; + } + + return ""; + } + + public static String parseSocketAddressAddr(SocketAddress socketAddress) { + if (socketAddress != null) { + final String addr = socketAddress.toString(); + + if (addr.length() > 0) { + return addr.substring(1); + } + } + return ""; + } + +} + diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/util/ServerGlobal.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/ServerGlobal.java similarity index 96% rename from eventmesh-emesher/src/main/java/com/webank/emesher/util/ServerGlobal.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/ServerGlobal.java index 9de9fd2c57..9ef4422946 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/util/ServerGlobal.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/ServerGlobal.java @@ -16,7 +16,7 @@ * limitations under the License. */ -package com.webank.emesher.util; +package org.apache.eventmesh.runtime.util; import java.util.concurrent.atomic.AtomicLong; diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/util/Utils.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/Utils.java similarity index 51% rename from eventmesh-emesher/src/main/java/com/webank/emesher/util/Utils.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/Utils.java index ea9d9c3886..f3596e6a74 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/util/Utils.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/Utils.java @@ -15,21 +15,23 @@ * limitations under the License. */ -package com.webank.emesher.util; - -import com.webank.emesher.constants.ProxyConstants; -import com.webank.emesher.core.protocol.tcp.client.session.Session; -import com.webank.emesher.core.protocol.tcp.client.session.SessionState; -import com.webank.eventmesh.common.protocol.tcp.AccessMessage; -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; +package org.apache.eventmesh.runtime.util; + +import java.util.Map; + import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; + import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.common.protocol.tcp.EventMeshMessage; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session; +import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.SessionState; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Map; public class Utils { private final static Logger logger = LoggerFactory.getLogger(Utils.class); @@ -61,7 +63,7 @@ public void operationComplete(ChannelFuture future) throws Exception { logSucceedMessageFlow(pkg, user, startTime, taskExecuteTime); if (session != null) { - session.getClientGroupWrapper().get().getProxyTcpMonitor().getProxy2clientMsgNum().incrementAndGet(); + session.getClientGroupWrapper().get().getEventMeshTcpMonitor().getEventMesh2clientMsgNum().incrementAndGet(); } } } @@ -85,11 +87,11 @@ public static void logFailedMessageFlow(ChannelFuture future, Package pkg, UserA } private static void logFailedMessageFlow(Package pkg, UserAgent user, long startTime, long taskExecuteTime, Throwable e) { - if (pkg.getBody() instanceof AccessMessage) { - messageLogger.error("pkg|proxy2c|failed|cmd={}|mqMsg={}|user={}|wait={}ms|cost={}ms|errMsg={}", pkg.getHeader().getCommand(), - printMqMessage((AccessMessage) pkg.getBody()), user, taskExecuteTime - startTime, System.currentTimeMillis() - startTime, e); + if (pkg.getBody() instanceof EventMeshMessage) { + messageLogger.error("pkg|eventMesh2c|failed|cmd={}|mqMsg={}|user={}|wait={}ms|cost={}ms|errMsg={}", pkg.getHeader().getCommand(), + printMqMessage((EventMeshMessage) pkg.getBody()), user, taskExecuteTime - startTime, System.currentTimeMillis() - startTime, e); } else { - messageLogger.error("pkg|proxy2c|failed|cmd={}|pkg={}|user={}|wait={}ms|cost={}ms|errMsg={}", pkg.getHeader().getCommand(), + messageLogger.error("pkg|eventMesh2c|failed|cmd={}|pkg={}|user={}|wait={}ms|cost={}ms|errMsg={}", pkg.getHeader().getCommand(), pkg, user, taskExecuteTime - startTime, System.currentTimeMillis() - startTime, e); } } @@ -102,64 +104,64 @@ private static void logFailedMessageFlow(Package pkg, UserAgent user, long start * @param startTime */ public static void logSucceedMessageFlow(Package pkg, UserAgent user, long startTime, long taskExecuteTime) { - if (pkg.getBody() instanceof AccessMessage) { - messageLogger.info("pkg|proxy2c|cmd={}|mqMsg={}|user={}|wait={}ms|cost={}ms", pkg.getHeader().getCommand(), - printMqMessage((AccessMessage) pkg.getBody()), user, taskExecuteTime - startTime, System.currentTimeMillis() - startTime); + if (pkg.getBody() instanceof EventMeshMessage) { + messageLogger.info("pkg|eventMesh2c|cmd={}|mqMsg={}|user={}|wait={}ms|cost={}ms", pkg.getHeader().getCommand(), + printMqMessage((EventMeshMessage) pkg.getBody()), user, taskExecuteTime - startTime, System.currentTimeMillis() - startTime); } else { - messageLogger.info("pkg|proxy2c|cmd={}|pkg={}|user={}|wait={}ms|cost={}ms", pkg.getHeader().getCommand(), pkg, + messageLogger.info("pkg|eventMesh2c|cmd={}|pkg={}|user={}|wait={}ms|cost={}ms", pkg.getHeader().getCommand(), pkg, user, taskExecuteTime - startTime, System.currentTimeMillis() - startTime); } } - public static org.apache.rocketmq.common.message.Message decodeMessage(AccessMessage accessMessage) { - org.apache.rocketmq.common.message.Message msg = new org.apache.rocketmq.common.message.Message(); - msg.setTopic(accessMessage.getTopic()); - msg.setBody(accessMessage.getBody().getBytes()); - msg.getProperty("init"); - for (Map.Entry property : accessMessage.getProperties().entrySet()) { - msg.getProperties().put(property.getKey(), property.getValue()); - } - return msg; - } - - public static AccessMessage encodeMessage(org.apache.rocketmq.common.message.Message msg) throws Exception { - AccessMessage accessMessage = new AccessMessage(); - accessMessage.setBody(new String(msg.getBody(), "UTF-8")); - accessMessage.setTopic(msg.getTopic()); - for (Map.Entry property : msg.getProperties().entrySet()) { - accessMessage.getProperties().put(property.getKey(), property.getValue()); - } - return accessMessage; - } - - public static org.apache.rocketmq.common.message.Message messageMapper(org.apache.rocketmq.common.message.Message - message) { - org.apache.rocketmq.common.message.Message msg = new org.apache.rocketmq.common.message.Message(); - msg.setTopic(message.getTopic()); - msg.setBody(message.getBody()); - msg.getProperty("init"); - for (Map.Entry property : message.getProperties().entrySet()) { - msg.getProperties().put(property.getKey(), property.getValue()); - } - return msg; - } +// public static org.apache.rocketmq.common.message.Message decodeMessage(AccessMessage accessMessage) { +// org.apache.rocketmq.common.message.Message msg = new org.apache.rocketmq.common.message.Message(); +// msg.setTopic(accessMessage.getTopic()); +// msg.setBody(accessMessage.getBody().getBytes()); +// msg.getProperty("init"); +// for (Map.Entry property : accessMessage.getProperties().entrySet()) { +// msg.getProperties().put(property.getKey(), property.getValue()); +// } +// return msg; +// } + +// public static AccessMessage encodeMessage(org.apache.rocketmq.common.message.Message msg) throws Exception { +// AccessMessage accessMessage = new AccessMessage(); +// accessMessage.setBody(new String(msg.getBody(), "UTF-8")); +// accessMessage.setTopic(msg.getTopic()); +// for (Map.Entry property : msg.getProperties().entrySet()) { +// accessMessage.getProperties().put(property.getKey(), property.getValue()); +// } +// return accessMessage; +// } + +// public static org.apache.rocketmq.common.message.Message messageMapper(org.apache.rocketmq.common.message.Message +// message) { +// org.apache.rocketmq.common.message.Message msg = new org.apache.rocketmq.common.message.Message(); +// msg.setTopic(message.getTopic()); +// msg.setBody(message.getBody()); +// msg.getProperty("init"); +// for (Map.Entry property : message.getProperties().entrySet()) { +// msg.getProperties().put(property.getKey(), property.getValue()); +// } +// return msg; +// } /** * 打印mq消息的一部分内容 * - * @param accessMessage + * @param eventMeshMessage * @return */ - public static String printMqMessage(AccessMessage accessMessage) { - Map properties = accessMessage.getProperties(); + public static String printMqMessage(EventMeshMessage eventMeshMessage) { + Map properties = eventMeshMessage.getProperties(); - String bizSeqNo = properties.get(ProxyConstants.KEYS_UPPERCASE); + String bizSeqNo = properties.get(EventMeshConstants.KEYS_UPPERCASE); if (!StringUtils.isNotBlank(bizSeqNo)) { - bizSeqNo = properties.get(ProxyConstants.KEYS_LOWERCASE); + bizSeqNo = properties.get(EventMeshConstants.KEYS_LOWERCASE); } - String result = String.format("Message [topic=%s,TTL=%s,uniqueId=%s,bizSeq=%s]", accessMessage - .getTopic(), properties.get(ProxyConstants.TTL), properties.get(ProxyConstants.RR_REQUEST_UNIQ_ID), bizSeqNo); + String result = String.format("Message [topic=%s,TTL=%s,uniqueId=%s,bizSeq=%s]", eventMeshMessage + .getTopic(), properties.get(EventMeshConstants.TTL), properties.get(EventMeshConstants.RR_REQUEST_UNIQ_ID), bizSeqNo); return result; } @@ -169,13 +171,13 @@ public static String printMqMessage(AccessMessage accessMessage) { * @param message * @return */ - public static String printMqMessage(org.apache.rocketmq.common.message.Message message) { - Map properties = message.getProperties(); - String bizSeqNo = message.getKeys(); - String result = String.format("Message [topic=%s,TTL=%s,uniqueId=%s,bizSeq=%s]", message.getTopic() - , properties.get(ProxyConstants.TTL), properties.get(ProxyConstants.RR_REQUEST_UNIQ_ID), bizSeqNo); - return result; - } +// public static String printMqMessage(org.apache.rocketmq.common.message.Message message) { +// Map properties = message.getProperties(); +// String bizSeqNo = message.getKeys(); +// String result = String.format("Message [topic=%s,TTL=%s,uniqueId=%s,bizSeq=%s]", message.getTopic() +// , properties.get(EventMeshConstants.TTL), properties.get(EventMeshConstants.RR_REQUEST_UNIQ_ID), bizSeqNo); +// return result; +// } /** * 根据topic获取serviceId diff --git a/eventmesh-emesher/src/main/java/com/webank/emesher/util/ValueComparator.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/ValueComparator.java similarity index 96% rename from eventmesh-emesher/src/main/java/com/webank/emesher/util/ValueComparator.java rename to eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/ValueComparator.java index a01e38605f..2eb24e57fd 100644 --- a/eventmesh-emesher/src/main/java/com/webank/emesher/util/ValueComparator.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/ValueComparator.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.webank.emesher.util; +package org.apache.eventmesh.runtime.util; import java.util.Comparator; import java.util.Map; diff --git a/eventmesh-emesher/src/test/java/client/ProxyClient.java b/eventmesh-runtime/src/test/java/client/EventMeshClient.java similarity index 74% rename from eventmesh-emesher/src/test/java/client/ProxyClient.java rename to eventmesh-runtime/src/test/java/client/EventMeshClient.java index 0e6a6bc43e..547bf61f6b 100644 --- a/eventmesh-emesher/src/test/java/client/ProxyClient.java +++ b/eventmesh-runtime/src/test/java/client/EventMeshClient.java @@ -17,9 +17,13 @@ package client; +import org.apache.eventmesh.common.protocol.SubcriptionType; +import org.apache.eventmesh.common.protocol.tcp.Package; + import client.hook.ReceiveMsgHook; -import com.webank.eventmesh.common.protocol.tcp.Package; -public interface ProxyClient { +import org.apache.eventmesh.common.protocol.SubscriptionMode; + +public interface EventMeshClient { Package rr(Package msg, long timeout) throws Exception; @@ -35,13 +39,9 @@ public interface ProxyClient { Package listen() throws Exception; - Package justSubscribe(String serviceId, String scenario, String dcn) throws Exception; - - Package justUnsubscribe(String serviceId, String scenario, String dcn) throws Exception; - - Package justSubscribe(String topic) throws Exception; + Package justSubscribe(String topic, SubscriptionMode subscriptionMode, SubcriptionType subcriptionType) throws Exception; - Package justUnsubscribe(String topic) throws Exception; + Package justUnsubscribe(String topic, SubscriptionMode subscriptionMode, SubcriptionType subcriptionType) throws Exception; void registerPubBusiHandler(ReceiveMsgHook handler) throws Exception; diff --git a/eventmesh-emesher/src/test/java/client/PubClient.java b/eventmesh-runtime/src/test/java/client/PubClient.java similarity index 92% rename from eventmesh-emesher/src/test/java/client/PubClient.java rename to eventmesh-runtime/src/test/java/client/PubClient.java index 90c8c57ef2..452279d400 100644 --- a/eventmesh-emesher/src/test/java/client/PubClient.java +++ b/eventmesh-runtime/src/test/java/client/PubClient.java @@ -17,9 +17,10 @@ package client; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; + import client.hook.ReceiveMsgHook; -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; public interface PubClient { diff --git a/eventmesh-emesher/src/test/java/client/SubClient.java b/eventmesh-runtime/src/test/java/client/SubClient.java similarity index 71% rename from eventmesh-emesher/src/test/java/client/SubClient.java rename to eventmesh-runtime/src/test/java/client/SubClient.java index a15ab114f2..e591ea67e3 100644 --- a/eventmesh-emesher/src/test/java/client/SubClient.java +++ b/eventmesh-runtime/src/test/java/client/SubClient.java @@ -17,9 +17,12 @@ package client; +import org.apache.eventmesh.common.protocol.SubcriptionType; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.common.protocol.SubscriptionMode; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; + import client.hook.ReceiveMsgHook; -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; public interface SubClient { @@ -31,13 +34,9 @@ public interface SubClient { void reconnect() throws Exception; - Package justSubscribe(String serviceId, String scenario, String dcn) throws Exception; - - Package justSubscribe(String topic) throws Exception; - - Package justUnsubscribe(String serviceId, String scenario, String dcn) throws Exception; + Package justSubscribe(String topic, SubscriptionMode subscriptionMode, SubcriptionType subcriptionType) throws Exception; - Package justUnsubscribe(String topic) throws Exception; + Package justUnsubscribe(String topic, SubscriptionMode subscriptionMode, SubcriptionType subcriptionType) throws Exception; Package listen() throws Exception; diff --git a/eventmesh-emesher/src/test/java/client/common/ClientConstants.java b/eventmesh-runtime/src/test/java/client/common/ClientConstants.java similarity index 86% rename from eventmesh-emesher/src/test/java/client/common/ClientConstants.java rename to eventmesh-runtime/src/test/java/client/common/ClientConstants.java index 6c34774e17..3a9ec48ee6 100644 --- a/eventmesh-emesher/src/test/java/client/common/ClientConstants.java +++ b/eventmesh-runtime/src/test/java/client/common/ClientConstants.java @@ -27,7 +27,7 @@ public interface ClientConstants { long DEFAULT_TIMEOUT_IN_MILLISECONDS = 3000; - String SYNC_TOPIC = "FT0-s-80000000-01-0"; - String ASYNC_TOPIC = "FT0-e-80010000-01-1"; - String BROADCAST_TOPIC = "FT0-e-80030000-01-3"; + String SYNC_TOPIC = "TEST-TOPIC-TCP-SYNC"; + String ASYNC_TOPIC = "TEST-TOPIC-TCP-ASYNC"; + String BROADCAST_TOPIC = "TEST-TOPIC-TCP-BROADCAST"; } diff --git a/eventmesh-emesher/src/test/java/client/common/ClientGlobal.java b/eventmesh-runtime/src/test/java/client/common/ClientGlobal.java similarity index 99% rename from eventmesh-emesher/src/test/java/client/common/ClientGlobal.java rename to eventmesh-runtime/src/test/java/client/common/ClientGlobal.java index 91bf50773f..d4ce76abea 100644 --- a/eventmesh-emesher/src/test/java/client/common/ClientGlobal.java +++ b/eventmesh-runtime/src/test/java/client/common/ClientGlobal.java @@ -17,15 +17,16 @@ package client.common; +import java.util.TimeZone; + import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.TimeZone; - public class ClientGlobal { diff --git a/eventmesh-emesher/src/test/java/client/common/Codec.java b/eventmesh-runtime/src/test/java/client/common/Codec.java similarity index 89% rename from eventmesh-emesher/src/test/java/client/common/Codec.java rename to eventmesh-runtime/src/test/java/client/common/Codec.java index 4ca36a9b32..82ffcb0aec 100644 --- a/eventmesh-emesher/src/test/java/client/common/Codec.java +++ b/eventmesh-runtime/src/test/java/client/common/Codec.java @@ -17,31 +17,32 @@ package client.common; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.eventmesh.common.protocol.tcp.AccessMessage; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Header; -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.eventmesh.common.protocol.tcp.RedirectInfo; -import com.webank.eventmesh.common.protocol.tcp.Subscription; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; +import java.nio.charset.Charset; +import java.util.Arrays; +import java.util.List; + import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToByteEncoder; import io.netty.handler.codec.ReplayingDecoder; + +import org.apache.eventmesh.common.protocol.tcp.Command; +import org.apache.eventmesh.common.protocol.tcp.EventMeshMessage; +import org.apache.eventmesh.common.protocol.tcp.Header; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.common.protocol.tcp.RedirectInfo; +import org.apache.eventmesh.common.protocol.tcp.Subscription; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.nio.charset.Charset; -import java.util.Arrays; -import java.util.List; - public class Codec { private final static Logger logger = LoggerFactory.getLogger(Codec.class); private static final int FRAME_MAX_LENGTH = 1024 * 1024 * 4; - private static Charset UTF8 = Charset.forName(ProxyConstants.DEFAULT_CHARSET); - private static final byte[] CONSTANT_MAGIC_FLAG = "WEMQ".getBytes(UTF8); + private static Charset UTF8 = Charset.forName(EventMeshConstants.DEFAULT_CHARSET); + private static final byte[] CONSTANT_MAGIC_FLAG = "EventMesh".getBytes(UTF8); private static final byte[] VERSION = "0000".getBytes(UTF8); public static class Encoder extends MessageToByteEncoder { @@ -143,7 +144,7 @@ private static Object parseFromJson(Command cmd, String data) throws Exception { Command.BROADCAST_MESSAGE_TO_SERVER || cmd == Command.BROADCAST_MESSAGE_TO_CLIENT || cmd == Command.BROADCAST_MESSAGE_TO_CLIENT_ACK || cmd == Command.ASYNC_MESSAGE_TO_CLIENT_ACK || cmd == Command.REQUEST_TO_CLIENT_ACK || cmd == Command.RESPONSE_TO_CLIENT_ACK) { - return ClientGlobal.jsonMapper.readValue(data, AccessMessage.class); + return ClientGlobal.jsonMapper.readValue(data, EventMeshMessage.class); } else if (cmd == (Command.REDIRECT_TO_CLIENT)) { return ClientGlobal.jsonMapper.readValue(data, RedirectInfo.class); } else { diff --git a/eventmesh-emesher/src/test/java/client/common/MessageUtils.java b/eventmesh-runtime/src/test/java/client/common/MessageUtils.java similarity index 78% rename from eventmesh-emesher/src/test/java/client/common/MessageUtils.java rename to eventmesh-runtime/src/test/java/client/common/MessageUtils.java index 7be2c53d71..2be91670cd 100644 --- a/eventmesh-emesher/src/test/java/client/common/MessageUtils.java +++ b/eventmesh-runtime/src/test/java/client/common/MessageUtils.java @@ -17,17 +17,17 @@ package client.common; -import com.webank.eventmesh.common.protocol.tcp.AccessMessage; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Header; -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.eventmesh.common.protocol.tcp.Subscription; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; - import java.util.ArrayList; import java.util.List; import java.util.concurrent.ThreadLocalRandom; +import org.apache.eventmesh.common.protocol.SubcriptionType; +import org.apache.eventmesh.common.protocol.tcp.Subscription; +import org.apache.eventmesh.common.protocol.SubscriptionItem; +import org.apache.eventmesh.common.protocol.SubscriptionMode; +import org.apache.eventmesh.common.protocol.tcp.*; +import org.apache.eventmesh.common.protocol.tcp.Package; + public class MessageUtils { public static int seqLength = 10; @@ -63,10 +63,10 @@ public static Package subscribe() { return msg; } - public static Package subscribe(String topic) { + public static Package subscribe(String topic, SubscriptionMode subscriptionMode, SubcriptionType subcriptionType) { Package msg = new Package(); msg.setHeader(new Header(Command.SUBSCRIBE_REQUEST, 0, null, generateRandomString(seqLength))); - msg.setBody(generateSubscription(topic)); + msg.setBody(generateSubscription(topic, subscriptionMode, subcriptionType)); return msg; } @@ -76,10 +76,10 @@ public static Package unsubscribe() { return msg; } - public static Package unsubscribe(String topic) { + public static Package unsubscribe(String topic, SubscriptionMode subscriptionMode, SubcriptionType subcriptionType) { Package msg = new Package(); msg.setHeader(new Header(Command.UNSUBSCRIBE_REQUEST, 0, null, generateRandomString(seqLength))); - msg.setBody(generateSubscription(topic)); + msg.setBody(generateSubscription(topic, subscriptionMode, subcriptionType)); return msg; } @@ -141,7 +141,6 @@ public static Package responseToClientAck(Package in) { public static UserAgent generatePubClient() { UserAgent user = new UserAgent(); - user.setDcn("AC0"); user.setHost("127.0.0.1"); user.setPassword(generateRandomString(8)); user.setUsername("PU4283"); @@ -156,7 +155,6 @@ public static UserAgent generatePubClient() { public static UserAgent generateSubServer() { UserAgent user = new UserAgent(); - user.setDcn("AC0"); user.setHost("127.0.0.1"); user.setPassword(generateRandomString(8)); user.setUsername("PU4283"); @@ -170,25 +168,25 @@ public static UserAgent generateSubServer() { public static Subscription generateSubscription() { Subscription subscription = new Subscription(); - List topicList = new ArrayList<>(); - topicList.add("FT0-s-80000000-01-0"); - topicList.add("FT0-s-80000000-02-0"); - topicList.add("FT0-s-80000000-03-0"); - topicList.add("FT0-s-80000000-04-0"); - subscription.setTopicList(topicList); + List subscriptionItems = new ArrayList<>(); + subscriptionItems.add(new SubscriptionItem("TEST-TOPIC-TCP-SYNC", SubscriptionMode.CLUSTERING, SubcriptionType.SYNC)); + subscriptionItems.add(new SubscriptionItem("TEST-TOPIC-TCP-SYNC2", SubscriptionMode.CLUSTERING, SubcriptionType.SYNC)); + subscriptionItems.add(new SubscriptionItem("TEST-TOPIC-TCP-SYNC3", SubscriptionMode.CLUSTERING, SubcriptionType.SYNC)); + subscriptionItems.add(new SubscriptionItem("TEST-TOPIC-TCP-SYNC4", SubscriptionMode.CLUSTERING, SubcriptionType.SYNC)); + subscription.setTopicList(subscriptionItems); return subscription; } - public static Subscription generateSubscription(String topic) { + public static Subscription generateSubscription(String topic, SubscriptionMode subscriptionMode, SubcriptionType subcriptionType) { Subscription subscription = new Subscription(); - List topicList = new ArrayList<>(); - topicList.add(topic); - subscription.setTopicList(topicList); + List subscriptionItems = new ArrayList<>(); + subscriptionItems.add(new SubscriptionItem(topic, subscriptionMode, subcriptionType)); + subscription.setTopicList(subscriptionItems); return subscription; } - public static AccessMessage generateRRMsg(String topic, int i) { - AccessMessage msg = new AccessMessage(); + public static EventMeshMessage generateRRMsg(String topic, int i) { + EventMeshMessage msg = new EventMeshMessage(); msg.setTopic(topic); msg.getProperties().put("msgType", "persistent"); msg.getProperties().put("TTL", "300000"); @@ -197,8 +195,8 @@ public static AccessMessage generateRRMsg(String topic, int i) { return msg; } - public static AccessMessage generateAsyncEventMsg(String topic, int i) { - AccessMessage msg = new AccessMessage(); + public static EventMeshMessage generateAsyncEventMsg(String topic, int i) { + EventMeshMessage msg = new EventMeshMessage(); msg.setTopic(topic); msg.getProperties().put("REPLY_TO", "10.36.0.109@ProducerGroup-producerPool-9-access#V1_4_0#CI"); msg.getProperties().put("TTL", "30000"); @@ -207,8 +205,8 @@ public static AccessMessage generateAsyncEventMsg(String topic, int i) { return msg; } - public static AccessMessage generateBroadcastMsg(String topic, int i) { - AccessMessage msg = new AccessMessage(); + public static EventMeshMessage generateBroadcastMsg(String topic, int i) { + EventMeshMessage msg = new EventMeshMessage(); msg.setTopic(topic); msg.getProperties().put("REPLY_TO", ""); msg.getProperties().put("TTL", "30000"); diff --git a/eventmesh-emesher/src/test/java/client/common/RequestContext.java b/eventmesh-runtime/src/test/java/client/common/RequestContext.java similarity index 97% rename from eventmesh-emesher/src/test/java/client/common/RequestContext.java rename to eventmesh-runtime/src/test/java/client/common/RequestContext.java index 149b4b4e0d..2319403490 100644 --- a/eventmesh-emesher/src/test/java/client/common/RequestContext.java +++ b/eventmesh-runtime/src/test/java/client/common/RequestContext.java @@ -17,12 +17,12 @@ package client.common; -import com.webank.eventmesh.common.protocol.tcp.Package; +import java.util.concurrent.CountDownLatch; + +import org.apache.eventmesh.common.protocol.tcp.Package; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.concurrent.CountDownLatch; - public class RequestContext { private static Logger logger = LoggerFactory.getLogger(RequestContext.class); diff --git a/eventmesh-emesher/src/test/java/client/common/Server.java b/eventmesh-runtime/src/test/java/client/common/Server.java similarity index 78% rename from eventmesh-emesher/src/test/java/client/common/Server.java rename to eventmesh-runtime/src/test/java/client/common/Server.java index 6187c49249..85402890fa 100644 --- a/eventmesh-emesher/src/test/java/client/common/Server.java +++ b/eventmesh-runtime/src/test/java/client/common/Server.java @@ -17,12 +17,12 @@ package client.common; -import com.webank.emesher.boot.ProxyServer; -import com.webank.emesher.configuration.ProxyConfiguration; +import org.apache.eventmesh.runtime.boot.EventMeshServer; +import org.apache.eventmesh.runtime.configuration.EventMeshHTTPConfiguration; public class Server { - ProxyServer server; + EventMeshServer server; static { System.setProperty("proxy.home", "E:\\projects\\external-1\\proxy"); @@ -32,9 +32,9 @@ public class Server { } public void startAccessServer() throws Exception { - ProxyConfiguration proxyConfiguration = new ProxyConfiguration(null); - proxyConfiguration.init(); - server = new ProxyServer(proxyConfiguration, null); + EventMeshHTTPConfiguration eventMeshHttpConfiguration = new EventMeshHTTPConfiguration(null); + eventMeshHttpConfiguration.init(); + server = new EventMeshServer(eventMeshHttpConfiguration, null); server.init(); server.start(); } diff --git a/eventmesh-emesher/src/test/java/client/common/TCPClient.java b/eventmesh-runtime/src/test/java/client/common/TCPClient.java similarity index 99% rename from eventmesh-emesher/src/test/java/client/common/TCPClient.java rename to eventmesh-runtime/src/test/java/client/common/TCPClient.java index 4a7d30c13f..c0068de8eb 100644 --- a/eventmesh-emesher/src/test/java/client/common/TCPClient.java +++ b/eventmesh-runtime/src/test/java/client/common/TCPClient.java @@ -17,7 +17,17 @@ package client.common; -import com.webank.eventmesh.common.protocol.tcp.Package; +import java.io.Closeable; +import java.net.InetSocketAddress; +import java.util.Random; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicInteger; + import io.netty.bootstrap.Bootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.AdaptiveRecvByteBufAllocator; @@ -32,20 +42,11 @@ import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; + +import org.apache.eventmesh.common.protocol.tcp.Package; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.Closeable; -import java.net.InetSocketAddress; -import java.util.Random; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.ScheduledThreadPoolExecutor; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; -import java.util.concurrent.atomic.AtomicInteger; - /** * 一个Client连一个ACCESS diff --git a/eventmesh-emesher/src/test/java/client/common/UserAgentUtils.java b/eventmesh-runtime/src/test/java/client/common/UserAgentUtils.java similarity index 78% rename from eventmesh-emesher/src/test/java/client/common/UserAgentUtils.java rename to eventmesh-runtime/src/test/java/client/common/UserAgentUtils.java index 92a65e2273..fdcdff43eb 100644 --- a/eventmesh-emesher/src/test/java/client/common/UserAgentUtils.java +++ b/eventmesh-runtime/src/test/java/client/common/UserAgentUtils.java @@ -17,18 +17,15 @@ package client.common; -import com.webank.emesher.constants.ProxyConstants; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; -import org.apache.rocketmq.common.UtilAll; -import org.apache.rocketmq.remoting.common.RemotingUtil; - import java.util.concurrent.ThreadLocalRandom; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; + public class UserAgentUtils { public static UserAgent createPubUserAgent() { UserAgent userAgent = new UserAgent(); userAgent.setSubsystem("5023"); - userAgent.setDcn("AC0"); userAgent.setPid(32893); userAgent.setVersion("2.0.11"); userAgent.setIdc("FT"); @@ -37,7 +34,7 @@ public static UserAgent createPubUserAgent() { userAgent.setPort(8362); userAgent.setUsername("PU4283"); userAgent.setPassword(generateRandomString(8)); - userAgent.setPurpose(ProxyConstants.PURPOSE_PUB); + userAgent.setPurpose(EventMeshConstants.PURPOSE_PUB); return userAgent; } @@ -45,9 +42,8 @@ public static UserAgent createPubUserAgent() { public static UserAgent createUserAgent() { UserAgent userAgent = new UserAgent(); userAgent.setSubsystem("5123"); - userAgent.setDcn("WAC"); - userAgent.setPid(UtilAll.getPid()); - userAgent.setHost(RemotingUtil.getLocalAddress()); +// userAgent.setPid(UtilAll.getPid()); +// userAgent.setHost(RemotingUtil.getLocalAddress()); userAgent.setVersion("2.0.8"); userAgent.setUsername("username"); userAgent.setPassword("1234"); @@ -57,15 +53,14 @@ public static UserAgent createUserAgent() { public static UserAgent createSubUserAgent() { UserAgent userAgent = new UserAgent(); userAgent.setSubsystem("5243"); - userAgent.setDcn("WAC"); - userAgent.setPid(UtilAll.getPid()); - userAgent.setHost(RemotingUtil.getLocalAddress()); +// userAgent.setPid(UtilAll.getPid()); +// userAgent.setHost(RemotingUtil.getLocalAddress()); userAgent.setPort(8888); userAgent.setVersion("2.0.8"); userAgent.setUsername("username"); userAgent.setPassword("1234"); userAgent.setPath("/data/app/defibus-acl/"); - userAgent.setPurpose(ProxyConstants.PURPOSE_SUB); + userAgent.setPurpose(EventMeshConstants.PURPOSE_SUB); return userAgent; } diff --git a/eventmesh-emesher/src/test/java/client/hook/ReceiveMsgHook.java b/eventmesh-runtime/src/test/java/client/hook/ReceiveMsgHook.java similarity index 94% rename from eventmesh-emesher/src/test/java/client/hook/ReceiveMsgHook.java rename to eventmesh-runtime/src/test/java/client/hook/ReceiveMsgHook.java index bd6622c88b..6aca270f46 100644 --- a/eventmesh-emesher/src/test/java/client/hook/ReceiveMsgHook.java +++ b/eventmesh-runtime/src/test/java/client/hook/ReceiveMsgHook.java @@ -17,8 +17,10 @@ package client.hook; -import com.webank.eventmesh.common.protocol.tcp.Package; import io.netty.channel.ChannelHandlerContext; + +import org.apache.eventmesh.common.protocol.tcp.Package; + /** * 业务回调钩子, 这是针对所有类型的消息都会进行的回调 */ diff --git a/eventmesh-emesher/src/test/java/client/impl/ProxyClientImpl.java b/eventmesh-runtime/src/test/java/client/impl/EventMeshClientImpl.java similarity index 79% rename from eventmesh-emesher/src/test/java/client/impl/ProxyClientImpl.java rename to eventmesh-runtime/src/test/java/client/impl/EventMeshClientImpl.java index 625b670722..3913bcdd01 100644 --- a/eventmesh-emesher/src/test/java/client/impl/ProxyClientImpl.java +++ b/eventmesh-runtime/src/test/java/client/impl/EventMeshClientImpl.java @@ -17,15 +17,18 @@ package client.impl; -import client.ProxyClient; +import org.apache.eventmesh.common.protocol.SubcriptionType; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.common.protocol.SubscriptionMode; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; + +import client.EventMeshClient; import client.PubClient; import client.SubClient; import client.common.UserAgentUtils; import client.hook.ReceiveMsgHook; -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; -public class ProxyClientImpl implements ProxyClient { +public class EventMeshClientImpl implements EventMeshClient { protected UserAgent agent; private String accessHost; private int accessPort; @@ -33,7 +36,7 @@ public class ProxyClientImpl implements ProxyClient { private PubClient pubClient; private SubClient subClient; - public ProxyClientImpl(String accessHost, int accessPort, UserAgent agent) { + public EventMeshClientImpl(String accessHost, int accessPort, UserAgent agent) { this.accessHost = accessHost; this.accessPort = accessPort; this.agent = agent; @@ -41,7 +44,7 @@ public ProxyClientImpl(String accessHost, int accessPort, UserAgent agent) { this.pubClient = new PubClientImpl(accessHost, accessPort, agent); } - public ProxyClientImpl(String accessHost, int accessPort) { + public EventMeshClientImpl(String accessHost, int accessPort) { this.accessHost = accessHost; this.accessPort = accessPort; this.subClient = new SubClientImpl(accessHost, accessPort, UserAgentUtils.createSubUserAgent()); @@ -75,26 +78,19 @@ public void heartbeat() throws Exception { this.subClient.heartbeat(); } - public Package justSubscribe(String serviceId, String scenario, String dcn) throws Exception { - return this.subClient.justSubscribe(serviceId, scenario, dcn); - } public Package listen() throws Exception { return this.subClient.listen(); } - public Package justUnsubscribe(String serviceId, String scenario, String dcn) throws Exception { - return this.subClient.justUnsubscribe(serviceId, scenario, dcn); - } - @Override - public Package justSubscribe(String topic) throws Exception { - return this.subClient.justSubscribe(topic); + public Package justSubscribe(String topic, SubscriptionMode subscriptionMode, SubcriptionType subcriptionType) throws Exception { + return this.subClient.justSubscribe(topic, subscriptionMode, subcriptionType); } @Override - public Package justUnsubscribe(String topic) throws Exception { - return this.subClient.justUnsubscribe(topic); + public Package justUnsubscribe(String topic, SubscriptionMode subscriptionMode, SubcriptionType subcriptionType) throws Exception { + return this.subClient.justUnsubscribe(topic, subscriptionMode, subcriptionType); } @@ -116,7 +112,7 @@ public String toString() { } @Deprecated - public ProxyClientImpl(String accessServer, String busiTag, String subSystem) { + public EventMeshClientImpl(String accessServer, String busiTag, String subSystem) { // this.accessServer = accessServer; // this.pubClient = new PubClientImpl(StringUtils.split(this.accessServer, ":")[0], // Integer.parseInt(StringUtils.split(this.accessServer, ":")[1]), OldTestUserAgentFactory.createPubUserAgent diff --git a/eventmesh-emesher/src/test/java/client/impl/PubClientImpl.java b/eventmesh-runtime/src/test/java/client/impl/PubClientImpl.java similarity index 97% rename from eventmesh-emesher/src/test/java/client/impl/PubClientImpl.java rename to eventmesh-runtime/src/test/java/client/impl/PubClientImpl.java index 2cb8f3238b..ee01069dc1 100644 --- a/eventmesh-emesher/src/test/java/client/impl/PubClientImpl.java +++ b/eventmesh-runtime/src/test/java/client/impl/PubClientImpl.java @@ -17,25 +17,27 @@ package client.impl; -import client.PubClient; -import client.common.ClientConstants; -import client.common.MessageUtils; -import client.common.RequestContext; -import client.common.TCPClient; -import client.hook.ReceiveMsgHook; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.OPStatus; -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; + +import org.apache.eventmesh.common.protocol.tcp.Command; +import org.apache.eventmesh.common.protocol.tcp.OPStatus; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; import org.junit.Assert; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; +import client.PubClient; +import client.common.ClientConstants; +import client.common.MessageUtils; +import client.common.RequestContext; +import client.common.TCPClient; +import client.hook.ReceiveMsgHook; public class PubClientImpl extends TCPClient implements PubClient { diff --git a/eventmesh-emesher/src/test/java/client/impl/SubClientImpl.java b/eventmesh-runtime/src/test/java/client/impl/SubClientImpl.java similarity index 87% rename from eventmesh-emesher/src/test/java/client/impl/SubClientImpl.java rename to eventmesh-runtime/src/test/java/client/impl/SubClientImpl.java index 0f578567b1..e77171558c 100644 --- a/eventmesh-emesher/src/test/java/client/impl/SubClientImpl.java +++ b/eventmesh-runtime/src/test/java/client/impl/SubClientImpl.java @@ -17,28 +17,31 @@ package client.impl; -import client.SubClient; -import client.common.ClientConstants; -import client.common.MessageUtils; -import client.common.RequestContext; -import client.common.TCPClient; -import client.hook.ReceiveMsgHook; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.OPStatus; -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; + import org.apache.commons.collections4.CollectionUtils; +import org.apache.eventmesh.common.protocol.SubcriptionType; +import org.apache.eventmesh.common.protocol.SubscriptionItem; +import org.apache.eventmesh.common.protocol.SubscriptionMode; +import org.apache.eventmesh.common.protocol.tcp.*; +import org.apache.eventmesh.common.protocol.tcp.Package; import org.junit.Assert; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; +import client.SubClient; +import client.common.ClientConstants; +import client.common.MessageUtils; +import client.common.RequestContext; +import client.common.TCPClient; +import client.hook.ReceiveMsgHook; public class SubClientImpl extends TCPClient implements SubClient { @@ -48,7 +51,7 @@ public class SubClientImpl extends TCPClient implements SubClient { private ReceiveMsgHook callback; - private List topics = new ArrayList(); + private List subscriptionItems = new ArrayList(); private ScheduledFuture task; @@ -70,9 +73,9 @@ public void init() throws Exception { public void reconnect() throws Exception { super.reconnect(); hello(); - if (!CollectionUtils.isEmpty(topics)) { - for (String topic : topics) { - Package request = MessageUtils.subscribe(topic); + if (!CollectionUtils.isEmpty(subscriptionItems)) { + for (SubscriptionItem item : subscriptionItems) { + Package request = MessageUtils.subscribe(item.getTopic(), item.getMode(), item.getType()); this.dispatcher(request, ClientConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS); } } @@ -115,14 +118,9 @@ private void hello() throws Exception { this.dispatcher(msg, ClientConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS); } - public Package justSubscribe(String serviceId, String scenario, String dcn) throws Exception { - Package msg = MessageUtils.subscribe(); - return this.dispatcher(msg, ClientConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS); - } - - public Package justSubscribe(String topic) throws Exception { - topics.add(topic); - Package msg = MessageUtils.subscribe(topic); + public Package justSubscribe(String topic, SubscriptionMode subscriptionMode, SubcriptionType subcriptionType) throws Exception { + subscriptionItems.add(new SubscriptionItem(topic, subscriptionMode, subcriptionType)); + Package msg = MessageUtils.subscribe(topic, subscriptionMode, subcriptionType); return this.dispatcher(msg, ClientConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS); } @@ -142,14 +140,9 @@ public Package listen() throws Exception { // this.dispatcher(msg, ClientConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS); // } - public Package justUnsubscribe(String topic) throws Exception { - topics.remove(topic); - Package msg = MessageUtils.unsubscribe(topic); - return this.dispatcher(msg, ClientConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS); - } - - public Package justUnsubscribe(String serviceId, String scenario, String dcn) throws Exception { - Package msg = MessageUtils.unsubscribe(); + public Package justUnsubscribe(String topic, SubscriptionMode subscriptionMode, SubcriptionType subcriptionType) throws Exception { + subscriptionItems.remove(topic); + Package msg = MessageUtils.unsubscribe(topic, subscriptionMode, subcriptionType); return this.dispatcher(msg, ClientConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS); } diff --git a/eventmesh-emesher/src/test/java/demo/AsyncPubClient.java b/eventmesh-runtime/src/test/java/demo/AsyncPubClient.java similarity index 94% rename from eventmesh-emesher/src/test/java/demo/AsyncPubClient.java rename to eventmesh-runtime/src/test/java/demo/AsyncPubClient.java index 115537c4ec..911105a426 100644 --- a/eventmesh-emesher/src/test/java/demo/AsyncPubClient.java +++ b/eventmesh-runtime/src/test/java/demo/AsyncPubClient.java @@ -17,14 +17,16 @@ package demo; +import io.netty.channel.ChannelHandlerContext; + +import org.apache.eventmesh.common.ThreadUtil; +import org.apache.eventmesh.common.protocol.tcp.Package; + import client.common.ClientConstants; import client.common.MessageUtils; import client.common.UserAgentUtils; import client.hook.ReceiveMsgHook; import client.impl.PubClientImpl; -import com.webank.eventmesh.common.ThreadUtil; -import com.webank.eventmesh.common.protocol.tcp.Package; -import io.netty.channel.ChannelHandlerContext; public class AsyncPubClient { public static void main(String[] args) throws Exception { diff --git a/eventmesh-emesher/src/test/java/demo/AsyncSubClient.java b/eventmesh-runtime/src/test/java/demo/AsyncSubClient.java similarity index 73% rename from eventmesh-emesher/src/test/java/demo/AsyncSubClient.java rename to eventmesh-runtime/src/test/java/demo/AsyncSubClient.java index 4145841cb7..fc479ca75e 100644 --- a/eventmesh-emesher/src/test/java/demo/AsyncSubClient.java +++ b/eventmesh-runtime/src/test/java/demo/AsyncSubClient.java @@ -17,25 +17,29 @@ package demo; +import io.netty.channel.ChannelHandlerContext; + +import org.apache.eventmesh.common.protocol.SubcriptionType; +import org.apache.eventmesh.common.protocol.tcp.EventMeshMessage; +import org.apache.eventmesh.common.protocol.tcp.Package; + import client.common.ClientConstants; import client.common.MessageUtils; import client.hook.ReceiveMsgHook; import client.impl.SubClientImpl; -import com.webank.eventmesh.common.protocol.tcp.AccessMessage; -import com.webank.eventmesh.common.protocol.tcp.Package; -import io.netty.channel.ChannelHandlerContext; +import org.apache.eventmesh.common.protocol.SubscriptionMode; public class AsyncSubClient { public static void main(String[] args) throws Exception { - SubClientImpl client = new SubClientImpl("127.0.0.1", 10000, MessageUtils.generateSubServer()); + SubClientImpl client = new SubClientImpl("127.0.0.1", 10002, MessageUtils.generateSubServer()); client.init(); client.heartbeat(); - client.justSubscribe(ClientConstants.ASYNC_TOPIC); + client.justSubscribe(ClientConstants.ASYNC_TOPIC, SubscriptionMode.CLUSTERING, SubcriptionType.ASYNC); client.registerBusiHandler(new ReceiveMsgHook() { @Override public void handle(Package msg, ChannelHandlerContext ctx) { - if (msg.getBody() instanceof AccessMessage) { - String body = ((AccessMessage) msg.getBody()).getBody(); + if (msg.getBody() instanceof EventMeshMessage) { + String body = ((EventMeshMessage) msg.getBody()).getBody(); System.err.println("receive message -------------------------------" + body); } } diff --git a/eventmesh-emesher/src/test/java/demo/BroadCastPubClient.java b/eventmesh-runtime/src/test/java/demo/BroadCastPubClient.java similarity index 96% rename from eventmesh-emesher/src/test/java/demo/BroadCastPubClient.java rename to eventmesh-runtime/src/test/java/demo/BroadCastPubClient.java index e0e2841873..637898509f 100644 --- a/eventmesh-emesher/src/test/java/demo/BroadCastPubClient.java +++ b/eventmesh-runtime/src/test/java/demo/BroadCastPubClient.java @@ -17,11 +17,12 @@ package demo; +import org.apache.eventmesh.common.ThreadUtil; + import client.common.ClientConstants; import client.common.MessageUtils; import client.common.UserAgentUtils; import client.impl.PubClientImpl; -import com.webank.eventmesh.common.ThreadUtil; public class BroadCastPubClient { public static void main(String[] args) throws Exception { diff --git a/eventmesh-emesher/src/test/java/demo/BroadCastSubClient.java b/eventmesh-runtime/src/test/java/demo/BroadCastSubClient.java similarity index 74% rename from eventmesh-emesher/src/test/java/demo/BroadCastSubClient.java rename to eventmesh-runtime/src/test/java/demo/BroadCastSubClient.java index 30c2e01c67..3d9385d750 100644 --- a/eventmesh-emesher/src/test/java/demo/BroadCastSubClient.java +++ b/eventmesh-runtime/src/test/java/demo/BroadCastSubClient.java @@ -17,27 +17,31 @@ package demo; +import io.netty.channel.ChannelHandlerContext; + +import org.apache.eventmesh.common.protocol.SubcriptionType; +import org.apache.eventmesh.common.protocol.tcp.Command; +import org.apache.eventmesh.common.protocol.tcp.EventMeshMessage; +import org.apache.eventmesh.common.protocol.tcp.Package; + import client.common.ClientConstants; import client.common.MessageUtils; import client.hook.ReceiveMsgHook; import client.impl.SubClientImpl; -import com.webank.eventmesh.common.protocol.tcp.AccessMessage; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Package; -import io.netty.channel.ChannelHandlerContext; +import org.apache.eventmesh.common.protocol.SubscriptionMode; public class BroadCastSubClient { public static void main(String[] args) throws Exception { SubClientImpl client = new SubClientImpl("127.0.0.1", 10000, MessageUtils.generateSubServer()); client.init(); client.heartbeat(); - client.justSubscribe(ClientConstants.BROADCAST_TOPIC); + client.justSubscribe(ClientConstants.BROADCAST_TOPIC, SubscriptionMode.BROADCASTING, SubcriptionType.ASYNC); client.registerBusiHandler(new ReceiveMsgHook() { @Override public void handle(Package msg, ChannelHandlerContext ctx) { if (msg.getHeader().getCommand() == Command.BROADCAST_MESSAGE_TO_CLIENT) { - if (msg.getBody() instanceof AccessMessage) { - String body = ((AccessMessage) msg.getBody()).getBody(); + if (msg.getBody() instanceof EventMeshMessage) { + String body = ((EventMeshMessage) msg.getBody()).getBody(); System.err.println("receive message -------------------------------" + body); } } diff --git a/eventmesh-emesher/src/test/java/demo/CCPubClient.java b/eventmesh-runtime/src/test/java/demo/CCPubClient.java similarity index 100% rename from eventmesh-emesher/src/test/java/demo/CCPubClient.java rename to eventmesh-runtime/src/test/java/demo/CCPubClient.java diff --git a/eventmesh-emesher/src/test/java/demo/CCSubClient.java b/eventmesh-runtime/src/test/java/demo/CCSubClient.java similarity index 83% rename from eventmesh-emesher/src/test/java/demo/CCSubClient.java rename to eventmesh-runtime/src/test/java/demo/CCSubClient.java index 6c1e21fef9..c72597410b 100644 --- a/eventmesh-emesher/src/test/java/demo/CCSubClient.java +++ b/eventmesh-runtime/src/test/java/demo/CCSubClient.java @@ -17,13 +17,17 @@ package demo; +import io.netty.channel.ChannelHandlerContext; + +import org.apache.eventmesh.common.protocol.SubcriptionType; +import org.apache.eventmesh.common.protocol.tcp.Command; +import org.apache.eventmesh.common.protocol.tcp.Package; + import client.common.MessageUtils; import client.common.UserAgentUtils; import client.hook.ReceiveMsgHook; import client.impl.SubClientImpl; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Package; -import io.netty.channel.ChannelHandlerContext; +import org.apache.eventmesh.common.protocol.SubscriptionMode; public class CCSubClient { @@ -32,7 +36,7 @@ public static void main(String[] args) throws Exception { subClient.init(); subClient.heartbeat(); subClient.listen(); - subClient.justSubscribe("FT0-s-80000000-01-0"); + subClient.justSubscribe("TEST-TOPIC-TCP-SYNC", SubscriptionMode.CLUSTERING, SubcriptionType.SYNC); subClient.registerBusiHandler(new ReceiveMsgHook() { @Override public void handle(Package msg, ChannelHandlerContext ctx) { diff --git a/eventmesh-emesher/src/test/java/demo/CClientDemo.java b/eventmesh-runtime/src/test/java/demo/CClientDemo.java similarity index 72% rename from eventmesh-emesher/src/test/java/demo/CClientDemo.java rename to eventmesh-runtime/src/test/java/demo/CClientDemo.java index 0792d7f3bf..a0ade78c96 100644 --- a/eventmesh-emesher/src/test/java/demo/CClientDemo.java +++ b/eventmesh-runtime/src/test/java/demo/CClientDemo.java @@ -17,15 +17,19 @@ package demo; -import client.common.MessageUtils; -import client.hook.ReceiveMsgHook; -import client.impl.ProxyClientImpl; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Package; import io.netty.channel.ChannelHandlerContext; + +import org.apache.eventmesh.common.protocol.SubcriptionType; +import org.apache.eventmesh.common.protocol.tcp.Command; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.common.protocol.SubscriptionMode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import client.common.MessageUtils; +import client.hook.ReceiveMsgHook; +import client.impl.EventMeshClientImpl; + /** * SIMPLE客户端使用样例 */ @@ -33,23 +37,23 @@ public class CClientDemo { public static Logger logger = LoggerFactory.getLogger(CClientDemo.class); - private static final String SYNC_TOPIC = "FT0-s-80000000-01-0"; - private static final String ASYNC_TOPIC = "FT0-e-80010000-01-1"; - private static final String BROADCAST_TOPIC = "FT0-e-80030000-01-3"; + private static final String SYNC_TOPIC = "TEST-TOPIC-TCP-SYNC"; + private static final String ASYNC_TOPIC = "TEST-TOPIC-TCP-ASYNC"; + private static final String BROADCAST_TOPIC = "TEST-TOPIC-TCP-BROADCAST"; public static void main(String[] args) throws Exception { - ProxyClientImpl client = new ProxyClientImpl("127.0.0.1", 10000); + EventMeshClientImpl client = new EventMeshClientImpl("127.0.0.1", 10000); client.init(); client.heartbeat(); - client.justSubscribe(ASYNC_TOPIC); - client.justSubscribe(BROADCAST_TOPIC); + client.justSubscribe(ASYNC_TOPIC, SubscriptionMode.CLUSTERING, SubcriptionType.ASYNC); + client.justSubscribe(BROADCAST_TOPIC, SubscriptionMode.BROADCASTING, SubcriptionType.ASYNC); client.listen(); // for (int i = 0; i < 10000; i++) { // Package rr = null; // AccessMessage rrMessage = null; // try { -// rr = client.rr(MessageUtils.rrMesssage("FT0-s-80000000-01-0"), 3000); +// rr = client.rr(MessageUtils.rrMesssage("TEST-TOPIC-TCP-SYNC"), 3000); // Thread.sleep(100); // //rrMessage = (AccessMessage) rr.getBody(); // System.err.println( "rr-reply-------------------------------------------------" + rr.toString()); @@ -68,7 +72,7 @@ public void handle(Package msg, ChannelHandlerContext ctx) { for (int i = 0; i < 10000; i++) { // ThreadUtil.randomSleep(0,200); //广播消息 - client.broadcast(MessageUtils.broadcastMessage("FT0-e-80030000-01-3", i), 5000); + client.broadcast(MessageUtils.broadcastMessage("TEST-TOPIC-TCP-BROADCAST", i), 5000); //异步消息 client.publish(MessageUtils.asyncMessage(ASYNC_TOPIC, i), 5000); } diff --git a/eventmesh-emesher/src/test/java/demo/SyncPubClient.java b/eventmesh-runtime/src/test/java/demo/SyncPubClient.java similarity index 79% rename from eventmesh-emesher/src/test/java/demo/SyncPubClient.java rename to eventmesh-runtime/src/test/java/demo/SyncPubClient.java index 927dce9cb0..f7b5b72fe8 100644 --- a/eventmesh-emesher/src/test/java/demo/SyncPubClient.java +++ b/eventmesh-runtime/src/test/java/demo/SyncPubClient.java @@ -17,11 +17,12 @@ package demo; +import org.apache.eventmesh.common.protocol.tcp.EventMeshMessage; +import org.apache.eventmesh.common.protocol.tcp.Package; + import client.common.MessageUtils; import client.common.UserAgentUtils; import client.impl.PubClientImpl; -import com.webank.eventmesh.common.protocol.tcp.AccessMessage; -import com.webank.eventmesh.common.protocol.tcp.Package; public class SyncPubClient { public static void main(String[] args) throws Exception { @@ -30,9 +31,9 @@ public static void main(String[] args) throws Exception { pubClient.heartbeat(); for (int i = 0; i < 100; i++) { - Package rr = pubClient.rr(MessageUtils.rrMesssage("FT0-s-80000000-01-0", i), 3000); - if (rr.getBody() instanceof AccessMessage) { - String body = ((AccessMessage) rr.getBody()).getBody(); + Package rr = pubClient.rr(MessageUtils.rrMesssage("TEST-TOPIC-TCP-SYNC", i), 3000); + if (rr.getBody() instanceof EventMeshMessage) { + String body = ((EventMeshMessage) rr.getBody()).getBody(); System.err.println("rrMessage: " + body + " " + "rr-reply-------------------------------------------------" + rr.toString()); } } diff --git a/eventmesh-emesher/src/test/java/demo/SyncSubClient.java b/eventmesh-runtime/src/test/java/demo/SyncSubClient.java similarity index 82% rename from eventmesh-emesher/src/test/java/demo/SyncSubClient.java rename to eventmesh-runtime/src/test/java/demo/SyncSubClient.java index 50a62adf8b..9bc11415a3 100644 --- a/eventmesh-emesher/src/test/java/demo/SyncSubClient.java +++ b/eventmesh-runtime/src/test/java/demo/SyncSubClient.java @@ -17,20 +17,24 @@ package demo; +import io.netty.channel.ChannelHandlerContext; + +import org.apache.eventmesh.common.protocol.SubcriptionType; +import org.apache.eventmesh.common.protocol.tcp.Command; +import org.apache.eventmesh.common.protocol.tcp.Package; + import client.common.ClientConstants; import client.common.MessageUtils; import client.hook.ReceiveMsgHook; import client.impl.SubClientImpl; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Package; -import io.netty.channel.ChannelHandlerContext; +import org.apache.eventmesh.common.protocol.SubscriptionMode; public class SyncSubClient { public static void main(String[] args) throws Exception { SubClientImpl client = new SubClientImpl("127.0.0.1", 10000, MessageUtils.generateSubServer()); client.init(); client.heartbeat(); - client.justSubscribe(ClientConstants.SYNC_TOPIC); + client.justSubscribe(ClientConstants.SYNC_TOPIC, SubscriptionMode.CLUSTERING, SubcriptionType.SYNC); client.registerBusiHandler(new ReceiveMsgHook() { @Override public void handle(Package msg, ChannelHandlerContext ctx) { diff --git a/eventmesh-runtime/src/test/java/org/apache/eventmesh/runtime/admin/handler/RedirectClientByIpPortHandlerTest.java b/eventmesh-runtime/src/test/java/org/apache/eventmesh/runtime/admin/handler/RedirectClientByIpPortHandlerTest.java new file mode 100644 index 0000000000..c28a460d7a --- /dev/null +++ b/eventmesh-runtime/src/test/java/org/apache/eventmesh/runtime/admin/handler/RedirectClientByIpPortHandlerTest.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.eventmesh.runtime.admin.handler; + +import com.sun.net.httpserver.HttpExchange; +import org.apache.eventmesh.runtime.boot.EventMeshTCPServer; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.powermock.api.mockito.PowerMockito; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.net.URI; + +public class RedirectClientByIpPortHandlerTest { + + private RedirectClientByIpPortHandler redirectClientByIpPortHandler; + + @Before + public void init() { + EventMeshTCPServer mockServer = PowerMockito.mock(EventMeshTCPServer.class); + redirectClientByIpPortHandler = new RedirectClientByIpPortHandler(mockServer); + } + + @Test + public void testHandleParamIllegal() throws IOException { + OutputStream outputStream = new ByteArrayOutputStream(); + URI uri = URI.create("ip=127.0.0.1&port=1234&desteventMeshIp=127.0.0.1&desteventMeshPort="); + + HttpExchange mockExchange = PowerMockito.mock(HttpExchange.class); + PowerMockito.when(mockExchange.getResponseBody()).thenReturn(outputStream); + PowerMockito.when(mockExchange.getRequestURI()).thenReturn(uri); + + redirectClientByIpPortHandler.handle(mockExchange); + + String response = outputStream.toString(); + Assert.assertEquals("params illegal!", response); + + } +} \ No newline at end of file diff --git a/eventmesh-emesher/src/test/resources/log4j2.xml b/eventmesh-runtime/src/test/resources/log4j2.xml similarity index 100% rename from eventmesh-emesher/src/test/resources/log4j2.xml rename to eventmesh-runtime/src/test/resources/log4j2.xml diff --git a/eventmesh-sdk-java/build.gradle b/eventmesh-sdk-java/build.gradle index f421980042..70a51362e6 100644 --- a/eventmesh-sdk-java/build.gradle +++ b/eventmesh-sdk-java/build.gradle @@ -15,84 +15,34 @@ * limitations under the License. */ -apply plugin: 'java' -apply plugin: "maven" -apply plugin: "eclipse" -apply plugin: "idea" -if (project.findProperty("jdk") == "1.7") { - sourceCompatibility = 1.7 - targetCompatibility = 1.7 -} else { - sourceCompatibility = 1.8 - targetCompatibility = 1.8 - version = version + "-jdk8" -} - -if (project.findProperty("snapshot") instanceof String) { - if (project.property("snapshot").toBoolean()) version = version + "-SNAPSHOT" -} +//plugins { +// id 'org.springframework.boot' version '2.1.6.RELEASE' +//} -repositories { - mavenLocal() - maven { - url "https://maven.aliyun.com/repository/public" - } - mavenCentral() -} -List log4j2 = [ - "org.apache.logging.log4j:log4j-api:2.13.3", - "org.apache.logging.log4j:log4j-core:2.13.3", - "org.apache.logging.log4j:log4j-slf4j-impl:2.13.3", - "org.apache.logging.log4j:log4j-web:2.13.3", - "com.lmax:disruptor:3.4.2" -] +//ext['commons-pool2.version'] = '2.6.1' -List sl4j = [ - "org.slf4j:slf4j-api:1.7.30" -] dependencies { - compile log4j2, sl4j - testCompile log4j2, sl4j - compile project(":eventmesh-common") - testCompile project(":eventmesh-common") -} - -uploadArchives { - repositories { - mavenDeployer { -// snapshotRepository(url: 'Your target repo address') { -// authentication(userName: 'Your user name', password: 'Your password') -// } -// repository(url: 'Your target repo address') { -// authentication(userName: 'Your user name', password: 'Your password') -// } - } - } -} - -task zip(type: Zip, dependsOn: ['clean', 'jar']) { - extension = 'zip' - archiveName = project.name + '_' + version + '.' + extension - destinationDir = new File(projectDir, 'dist') - - into("/") { - from project.jar.getArchivePath() - } -} - -task tar(type: Tar, dependsOn: ['clean', 'jar']) { - extension = 'tar.gz' - compression = Compression.GZIP - archiveName = project.name + '_' + version + '.' + extension - destinationDir = new File(projectDir, 'dist') - into("/") { - from project.jar.getArchivePath() - } + //implementation log4j2, sl4j,httpclient,netty + // log4j2, sl4j,httpclient,netty + implementation project(":eventmesh-common") + testImplementation project(":eventmesh-common") + +// //Spring依赖的第三方模块(2.1.6依赖的是3.8.1) +// compile("org.apache.commons:commons-lang3") +// //Spring依赖的第三方模块(2.1.6依赖的是2.6.2)指定依赖版本为2.6.1 +// compile('org.apache.commons:commons-pool2') +// //未被依赖的库需要手动:我司开源的 轻代码开发框架核心包,文末源码链接 +// compile('com.diboot:diboot-core:2.0.0') + +// implementation('org.springframework.boot:spring-boot-starter-web') { +// exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging' +// } +// +// implementation('org.springframework.boot:spring-boot-starter') { +// exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging' +// } } -tasks.withType(JavaCompile) { - options.encoding = "UTF-8" -} \ No newline at end of file diff --git a/eventmesh-sdk-java/gradle.properties b/eventmesh-sdk-java/gradle.properties index e45361ffa4..ba5ffa736e 100644 --- a/eventmesh-sdk-java/gradle.properties +++ b/eventmesh-sdk-java/gradle.properties @@ -14,8 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # - -group=com.webank.eventmesh -version=1.1.1 +group=org.apache.eventmesh +version=1.2.0-release jdk=1.8 snapshot=false diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/AbstractLiteClient.java b/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/AbstractLiteClient.java deleted file mode 100644 index be1c5d7cae..0000000000 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/AbstractLiteClient.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.eventmesh.client.http; - -import com.webank.eventmesh.client.http.conf.LiteClientConfig; -import com.webank.eventmesh.common.ProxyException; -import com.google.common.collect.Lists; -import org.apache.commons.lang3.ArrayUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; - -public abstract class AbstractLiteClient { - - public Logger logger = LoggerFactory.getLogger(AbstractLiteClient.class); - - private static CloseableHttpClient wpcli = HttpClients.createDefault(); - - public LiteClientConfig liteClientConfig; - - public static final String REGEX_VALIDATE_FOR_RPOXY_DEFAULT = - "^(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}:\\d{4,5};)*(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}:\\d{4,5})$"; - - public List proxyServerList = Lists.newArrayList(); - - public AbstractLiteClient(LiteClientConfig liteClientConfig) { - this.liteClientConfig = liteClientConfig; - } - - public void start() throws ProxyException { - proxyServerList = process(liteClientConfig.getLiteProxyAddr()); - if(proxyServerList == null || proxyServerList.size() < 1){ - throw new ProxyException("liteProxyAddr param illegal,please check"); - } - } - - private List process(String format) { - List list = Lists.newArrayList(); - if (StringUtils.isNotBlank(format) && format.matches(REGEX_VALIDATE_FOR_RPOXY_DEFAULT)) { - - String[] serversArr = StringUtils.split(format, ";"); - if (ArrayUtils.isNotEmpty(serversArr)) { - for (String server : serversArr) { - list.add(server); - } - } - - return list; - } - - logger.error("servers is bad format, servers:{}", format); - return null; - } - - public LiteClientConfig getLiteClientConfig() { - return liteClientConfig; - } - - public void shutdown() throws Exception { - logger.info("AbstractLiteClient shutdown"); - } -} diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/consumer/LiteConsumer.java b/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/consumer/LiteConsumer.java deleted file mode 100644 index 996357631c..0000000000 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/consumer/LiteConsumer.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.eventmesh.client.http.consumer; - -import com.webank.eventmesh.client.http.AbstractLiteClient; -import com.webank.eventmesh.client.http.RemotingServer; -import com.webank.eventmesh.client.http.conf.LiteClientConfig; -import com.webank.eventmesh.client.http.consumer.listener.LiteMessageListener; -import com.webank.eventmesh.common.ProxyException; -import com.webank.eventmesh.common.ThreadPoolFactory; -import com.google.common.base.Preconditions; -import com.google.common.collect.Lists; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.atomic.AtomicBoolean; - -public class LiteConsumer extends AbstractLiteClient { - - public Logger logger = LoggerFactory.getLogger(LiteConsumer.class); - - private RemotingServer remotingServer; - - private ThreadPoolExecutor consumeExecutor; - - private static CloseableHttpClient httpClient = HttpClients.createDefault(); - - protected LiteClientConfig weMQProxyClientConfig; - - private List subscription = Lists.newArrayList(); - - private LiteMessageListener messageListener; - - public LiteConsumer(LiteClientConfig liteClientConfig) { - super(liteClientConfig); - this.consumeExecutor = ThreadPoolFactory.createThreadPoolExecutor(weMQProxyClientConfig.getConsumeThreadCore(), - weMQProxyClientConfig.getConsumeThreadMax(), "proxy-client-consume-"); - this.remotingServer = new RemotingServer(consumeExecutor); - } - - public LiteConsumer(LiteClientConfig liteClientConfig, - ThreadPoolExecutor customExecutor) { - super(liteClientConfig); - this.consumeExecutor = customExecutor; - this.remotingServer = new RemotingServer(this.consumeExecutor); - } - - private AtomicBoolean started = new AtomicBoolean(Boolean.FALSE); - - public void start() throws ProxyException { - Preconditions.checkState(weMQProxyClientConfig != null, "weMQProxyClientConfig can't be null"); - Preconditions.checkState(consumeExecutor != null, "consumeExecutor can't be null"); - Preconditions.checkState(messageListener != null, "messageListener can't be null"); - logger.info("LiteConsumer starting"); - super.start(); - started.compareAndSet(false, true); - logger.info("LiteConsumer started"); - } - - public void shutdown() throws Exception { - logger.info("LiteConsumer shutting down"); - super.shutdown(); - httpClient.close(); - started.compareAndSet(true, false); - logger.info("LiteConsumer shutdown"); - } - - - public boolean subscribe(String topic) throws ProxyException { - subscription.add(topic); - //做一次心跳 - return Boolean.TRUE; - } - - public boolean unsubscribe(String topic) throws ProxyException { - subscription.remove(topic); - //做一次心跳 - return Boolean.TRUE; - } - - public void registerMessageListener(LiteMessageListener messageListener) throws ProxyException { - this.messageListener = messageListener; - remotingServer.registerMessageListener(this.messageListener); - } -} diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/consumer/context/LiteConsumeContext.java b/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/consumer/context/LiteConsumeContext.java deleted file mode 100644 index e17d139ef4..0000000000 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/consumer/context/LiteConsumeContext.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.webank.eventmesh.client.http.consumer.context; - -import com.webank.eventmesh.common.Constants; -import org.apache.commons.lang3.time.DateFormatUtils; - -public class LiteConsumeContext { - - private String proxyIp; - - private String proxyEnv; - - private String proxyIdc; - - private String proxyRegion; - - private String proxyCluster; - - private String proxyDcn; - - //本地RETRY次数 - private int retryTimes = 0; - - private long createTime = System.currentTimeMillis(); - - public LiteConsumeContext(String proxyIp, String proxyEnv, - String proxyIdc,String proxyRegion, - String proxyCluster, String proxyDcn) { - this.proxyIp = proxyIp; - this.proxyEnv = proxyEnv; - this.proxyIdc = proxyIdc; - this.proxyRegion = proxyRegion; - this.proxyCluster = proxyCluster; - this.proxyDcn = proxyDcn; - - } - - public String getProxyIp() { - return proxyIp; - } - - public void setProxyIp(String proxyIp) { - this.proxyIp = proxyIp; - } - - public String getProxyEnv() { - return proxyEnv; - } - - public void setProxyEnv(String proxyEnv) { - this.proxyEnv = proxyEnv; - } - - public String getProxyIdc() { - return proxyIdc; - } - - public void setProxyIdc(String proxyIdc) { - this.proxyIdc = proxyIdc; - } - - public String getProxyRegion() { - return proxyRegion; - } - - public void setProxyRegion(String proxyRegion) { - this.proxyRegion = proxyRegion; - } - - public String getProxyCluster() { - return proxyCluster; - } - - public void setProxyCluster(String proxyCluster) { - this.proxyCluster = proxyCluster; - } - - public String getProxyDcn() { - return proxyDcn; - } - - public void setProxyDcn(String proxyDcn) { - this.proxyDcn = proxyDcn; - } - - public int getRetryTimes() { - return retryTimes; - } - - public void setRetryTimes(int retryTimes) { - this.retryTimes = retryTimes; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("liteConsumeContext={") - .append("proxyIp=").append(proxyIp).append(",") - .append("proxyEnv=").append(proxyEnv).append(",") - .append("proxyRegion=").append(proxyRegion).append(",") - .append("proxyIdc=").append(proxyIdc).append(",") - .append("proxyCluster=").append(proxyCluster).append(",") - .append("proxyDcn=").append(proxyDcn).append(",") - .append("retryTimes=").append(retryTimes).append(",") - .append("createTime=").append(DateFormatUtils.format(createTime, Constants.DATE_FORMAT)) - .append("}"); - return sb.toString(); - } - -} diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/AbstractLiteClient.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/AbstractLiteClient.java new file mode 100644 index 0000000000..20302af8e6 --- /dev/null +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/AbstractLiteClient.java @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.client.http; + +import org.apache.eventmesh.client.http.conf.LiteClientConfig; +import org.apache.eventmesh.client.http.ssl.MyX509TrustManager; +import org.apache.eventmesh.client.http.util.HttpLoadBalanceUtils; +import org.apache.eventmesh.common.loadbalance.LoadBalanceSelector; +import org.apache.http.conn.ssl.DefaultHostnameVerifier; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import java.security.SecureRandom; + +public abstract class AbstractLiteClient { + + public Logger logger = LoggerFactory.getLogger(AbstractLiteClient.class); + + protected LiteClientConfig liteClientConfig; + + protected LoadBalanceSelector eventMeshServerSelector; + + public AbstractLiteClient(LiteClientConfig liteClientConfig) { + this.liteClientConfig = liteClientConfig; + } + + public void start() throws Exception { + eventMeshServerSelector = HttpLoadBalanceUtils.createEventMeshServerLoadBalanceSelector(liteClientConfig); + } + + public LiteClientConfig getLiteClientConfig() { + return liteClientConfig; + } + + public void shutdown() throws Exception { + logger.info("AbstractLiteClient shutdown"); + } + + public CloseableHttpClient setHttpClient() throws Exception { + if (!liteClientConfig.isUseTls()) { + return HttpClients.createDefault(); + } + SSLContext sslContext = null; + try { + String protocol = System.getProperty("ssl.client.protocol", "TLSv1.2"); + TrustManager[] tm = new TrustManager[]{new MyX509TrustManager()}; + sslContext = SSLContext.getInstance(protocol); + sslContext.init(null, tm, new SecureRandom()); + return HttpClients.custom().setSSLContext(sslContext) + .setSSLHostnameVerifier(new DefaultHostnameVerifier()).build(); + } catch (Exception e) { + logger.error("Error in creating HttpClient.", e); + throw e; + } + } +} diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/ProxyRetObj.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/EventMeshRetObj.java similarity index 94% rename from eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/ProxyRetObj.java rename to eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/EventMeshRetObj.java index 7209a31a0c..fe024fcf5f 100644 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/ProxyRetObj.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/EventMeshRetObj.java @@ -15,9 +15,9 @@ * limitations under the License. */ -package com.webank.eventmesh.client.http; +package org.apache.eventmesh.client.http; -public class ProxyRetObj { +public class EventMeshRetObj { private long resTime; diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/RemotingServer.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/RemotingServer.java similarity index 76% rename from eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/RemotingServer.java rename to eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/RemotingServer.java index b4c498312c..c24198d042 100644 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/RemotingServer.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/RemotingServer.java @@ -15,47 +15,66 @@ * limitations under the License. */ -package com.webank.eventmesh.client.http; -import com.webank.eventmesh.client.http.consumer.HandleResult; -import com.webank.eventmesh.client.http.consumer.context.LiteConsumeContext; -import com.webank.eventmesh.client.http.consumer.listener.LiteMessageListener; -import com.webank.eventmesh.common.Constants; -import com.webank.eventmesh.common.IPUtil; -import com.webank.eventmesh.common.LiteMessage; -import com.webank.eventmesh.common.ThreadUtil; -import com.webank.eventmesh.common.command.HttpCommand; -import com.webank.eventmesh.common.protocol.http.body.Body; -import com.webank.eventmesh.common.protocol.http.body.message.PushMessageRequestBody; -import com.webank.eventmesh.common.protocol.http.common.ClientRetCode; -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import com.webank.eventmesh.common.protocol.http.common.ProtocolVersion; -import com.webank.eventmesh.common.protocol.http.common.RequestCode; -import com.webank.eventmesh.common.protocol.http.header.Header; -import com.webank.eventmesh.common.protocol.http.header.message.PushMessageRequestHeader; +package org.apache.eventmesh.client.http; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; + import io.netty.bootstrap.ServerBootstrap; -import io.netty.channel.*; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; -import io.netty.handler.codec.http.*; +import io.netty.handler.codec.http.DefaultFullHttpResponse; +import io.netty.handler.codec.http.FullHttpResponse; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpHeaderValues; +import io.netty.handler.codec.http.HttpMethod; +import io.netty.handler.codec.http.HttpObjectAggregator; +import io.netty.handler.codec.http.HttpRequest; +import io.netty.handler.codec.http.HttpRequestDecoder; +import io.netty.handler.codec.http.HttpResponseEncoder; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.netty.handler.codec.http.HttpVersion; +import io.netty.handler.codec.http.QueryStringDecoder; import io.netty.handler.codec.http.multipart.Attribute; import io.netty.handler.codec.http.multipart.DefaultHttpDataFactory; import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder; import io.netty.handler.codec.http.multipart.InterfaceHttpData; + import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.RandomUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.client.http.consumer.HandleResult; +import org.apache.eventmesh.client.http.consumer.context.LiteConsumeContext; +import org.apache.eventmesh.client.http.consumer.listener.LiteMessageListener; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.IPUtil; +import org.apache.eventmesh.common.LiteMessage; +import org.apache.eventmesh.common.ThreadUtil; +import org.apache.eventmesh.common.command.HttpCommand; +import org.apache.eventmesh.common.protocol.http.body.Body; +import org.apache.eventmesh.common.protocol.http.body.message.PushMessageRequestBody; +import org.apache.eventmesh.common.protocol.http.common.ClientRetCode; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; +import org.apache.eventmesh.common.protocol.http.common.ProtocolVersion; +import org.apache.eventmesh.common.protocol.http.common.RequestCode; +import org.apache.eventmesh.common.protocol.http.header.Header; +import org.apache.eventmesh.common.protocol.http.header.message.PushMessageRequestHeader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicInteger; - public class RemotingServer { public Logger logger = LoggerFactory.getLogger(this.getClass()); @@ -100,8 +119,9 @@ public void setConsumeExecutor(ThreadPoolExecutor consumeExecutor) { this.consumeExecutor = consumeExecutor; } - public void registerMessageListener(LiteMessageListener proxyMessageListener) { - this.messageListener = proxyMessageListener; + //TODO:不同的topic有不同的listener + public void registerMessageListener(LiteMessageListener eventMeshMessageListener) { + this.messageListener = eventMeshMessageListener; } private EventLoopGroup initBossGroup() { @@ -136,6 +156,7 @@ public String getEndpointURL() { return String.format("http://%s:%s", IPUtil.getLocalAddress(), port); } + class HTTPHandler extends SimpleChannelInboundHandler { /** @@ -208,7 +229,7 @@ protected void channelRead0(final ChannelHandlerContext ctx, HttpRequest httpReq if (StringUtils.isBlank(requestCode) || !StringUtils.isNumeric(requestCode) || (!String.valueOf(RequestCode.HTTP_PUSH_CLIENT_ASYNC.getRequestCode()).equals(requestCode) - && !String.valueOf(RequestCode.HTTP_PUSH_CLIENT_SYNC.getRequestCode()).equals(requestCode))) { + && !String.valueOf(RequestCode.HTTP_PUSH_CLIENT_SYNC.getRequestCode()).equals(requestCode))) { logger.error("receive invalid requestCode, {}", requestCode); responseCommand = requestCommand.createHttpCommandResponse(ClientRetCode.OK.getRetCode(), ClientRetCode.OK.getErrMsg()); sendResponse(ctx, responseCommand.httpResponse()); @@ -229,16 +250,15 @@ protected void channelRead0(final ChannelHandlerContext ctx, HttpRequest httpReq //检查是否有该TOPIC的listener // if (!listenerTable.containsKey(topic)) { -// logger.error("no listenning for this topic, {}", pushMessageRequest); +// logger.error("no listenning for this topic, {}", topic); // responseCommand = requestCommand.createHttpCommandResponse(ClientRetCode.NOLISTEN.getRetCode(), ClientRetCode.NOLISTEN.getErrMsg()); // sendResponse(ctx, responseCommand.httpResponse()); // return; // } - final LiteConsumeContext proxyConsumeContext = new LiteConsumeContext(pushMessageRequestHeader.getProxyIp(), - pushMessageRequestHeader.getProxyEnv(), pushMessageRequestHeader.getProxyIdc(), - pushMessageRequestHeader.getProxyRegion(), - pushMessageRequestHeader.getProxyCluster(), pushMessageRequestHeader.getProxyDcn()); + final LiteConsumeContext eventMeshConsumeContext = new LiteConsumeContext(pushMessageRequestHeader.getEventMeshIp(), + pushMessageRequestHeader.getEventMeshEnv(), pushMessageRequestHeader.getEventMeshIdc(), + pushMessageRequestHeader.getEventMeshCluster()); final LiteMessage liteMessage = new LiteMessage(pushMessageRequestBody.getBizSeqNo(), pushMessageRequestBody.getUniqueId(), topic, pushMessageRequestBody.getContent()); @@ -258,7 +278,7 @@ public void run() { return; } - HandleResult handleResult = messageListener.handle(liteMessage, proxyConsumeContext); + HandleResult handleResult = messageListener.handle(liteMessage, eventMeshConsumeContext); if (logger.isDebugEnabled()) { logger.info("bizSeqNo:{}, topic:{}, handleResult:{}", liteMessage.getBizSeqNo(), liteMessage.getTopic(), handleResult); @@ -345,12 +365,6 @@ public void operationComplete(ChannelFuture f) throws Exception { }); } - public void init() throws Exception { - initBossGroup(); - initWokerGroup(); - inited.compareAndSet(false, true); - } - public void shutdown() throws Exception { if (bossGroup != null) { bossGroup.shutdownGracefully(); @@ -365,40 +379,46 @@ public void shutdown() throws Exception { started.compareAndSet(true, false); inited.compareAndSet(true, false); } + } - public void start() throws Exception { - Runnable r = new Runnable() { - @Override - public void run() { - ServerBootstrap b = new ServerBootstrap(); - b.group(bossGroup, workerGroup) - .channel(NioServerSocketChannel.class) - .childHandler(new ChannelInitializer() { - @Override - public void initChannel(SocketChannel ch) - throws Exception { - ch.pipeline() - .addLast(new HttpRequestDecoder(), - new HttpResponseEncoder(), - new HttpObjectAggregator(Integer.MAX_VALUE), - new HTTPHandler()); // 4 - } - }).childOption(ChannelOption.SO_KEEPALIVE, Boolean.TRUE); - try { - logger.info("WeMQProxy Client[{}] Started......", port); - ChannelFuture future = b.bind(port).sync(); - future.channel().closeFuture().sync(); - started.compareAndSet(false, true); - } catch (Exception e) { - bossGroup.shutdownGracefully(); - workerGroup.shutdownGracefully(); - } + public void init() throws Exception { + initBossGroup(); + initWokerGroup(); + inited.compareAndSet(false, true); + } + + public void start() throws Exception { + Runnable r = new Runnable() { + @Override + public void run() { + ServerBootstrap b = new ServerBootstrap(); + b.group(bossGroup, workerGroup) + .channel(NioServerSocketChannel.class) + .childHandler(new ChannelInitializer() { + @Override + public void initChannel(SocketChannel ch) + throws Exception { + ch.pipeline() + .addLast(new HttpRequestDecoder(), + new HttpResponseEncoder(), + new HttpObjectAggregator(Integer.MAX_VALUE), + new HTTPHandler()); // 4 + } + }).childOption(ChannelOption.SO_KEEPALIVE, Boolean.TRUE); + try { + logger.info("EventMesh Client[{}] Started......", port); + ChannelFuture future = b.bind(port).sync(); + future.channel().closeFuture().sync(); + started.compareAndSet(false, true); + } catch (Exception e) { + bossGroup.shutdownGracefully(); + workerGroup.shutdownGracefully(); } - }; + } + }; - Thread t = new Thread(r, "proxy-client-remoting-server"); - t.start(); - } + Thread t = new Thread(r, "eventMesh-client-remoting-server"); + t.start(); } } diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/conf/LiteClientConfig.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/conf/LiteClientConfig.java similarity index 66% rename from eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/conf/LiteClientConfig.java rename to eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/conf/LiteClientConfig.java index 6e2907ec9b..fc5368a139 100644 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/conf/LiteClientConfig.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/conf/LiteClientConfig.java @@ -15,11 +15,24 @@ * limitations under the License. */ -package com.webank.eventmesh.client.http.conf; +package org.apache.eventmesh.client.http.conf; + +import org.apache.eventmesh.common.loadbalance.LoadBalanceType; public class LiteClientConfig { - private String liteProxyAddr = "127.0.0.1:10105"; + /** + * The event server address list + *

+ * If it's a cluster, please use ; to split, and the address format is related to loadBalanceType. + *

+ * E.g. + *

If you use Random strategy, the format like: 127.0.0.1:10105;127.0.0.2:10105 + *

If you use weighted round robin strategy, the format like: 127.0.0.1:10105:1;127.0.0.2:10105:2 + */ + private String liteEventMeshAddr = "127.0.0.1:10105"; + + private LoadBalanceType loadBalanceType = LoadBalanceType.RANDOM; private int consumeThreadCore = 2; @@ -27,11 +40,11 @@ public class LiteClientConfig { private String env; - private String region; + private String consumerGroup = "DefaultConsumerGroup"; - private String idc; + private String producerGroup = "DefaultProducerGroup"; - private String dcn; + private String idc; private String ip = "127.0.0.1"; @@ -45,12 +58,21 @@ public class LiteClientConfig { private boolean useTls = false; - public int getConsumeThreadMax() { - return consumeThreadMax; + public String getLiteEventMeshAddr() { + return liteEventMeshAddr; } - public LiteClientConfig setConsumeThreadMax(int consumeThreadMax) { - this.consumeThreadMax = consumeThreadMax; + public LiteClientConfig setLiteEventMeshAddr(String liteEventMeshAddr) { + this.liteEventMeshAddr = liteEventMeshAddr; + return this; + } + + public LoadBalanceType getLoadBalanceType() { + return loadBalanceType; + } + + public LiteClientConfig setLoadBalanceType(LoadBalanceType loadBalanceType) { + this.loadBalanceType = loadBalanceType; return this; } @@ -63,21 +85,21 @@ public LiteClientConfig setConsumeThreadCore(int consumeThreadCore) { return this; } - public String getEnv() { - return env; + public int getConsumeThreadMax() { + return consumeThreadMax; } - public LiteClientConfig setEnv(String env) { - this.env = env; + public LiteClientConfig setConsumeThreadMax(int consumeThreadMax) { + this.consumeThreadMax = consumeThreadMax; return this; } - public String getRegion() { - return region; + public String getEnv() { + return env; } - public LiteClientConfig setRegion(String region) { - this.region = region; + public LiteClientConfig setEnv(String env) { + this.env = env; return this; } @@ -90,15 +112,6 @@ public LiteClientConfig setIdc(String idc) { return this; } - public String getDcn() { - return dcn; - } - - public LiteClientConfig setDcn(String dcn) { - this.dcn = dcn; - return this; - } - public String getIp() { return ip; } @@ -144,34 +157,45 @@ public LiteClientConfig setPassword(String password) { return this; } - public String getLiteProxyAddr() { - return liteProxyAddr; + public boolean isUseTls() { + return useTls; } - public LiteClientConfig setLiteProxyAddr(String liteProxyAddr) { - this.liteProxyAddr = liteProxyAddr; + public LiteClientConfig setUseTls(boolean useTls) { + this.useTls = useTls; return this; } - public boolean isUseTls() { - return useTls; + public String getConsumerGroup() { + return consumerGroup; } - public void setUseTls(boolean useTls) { - this.useTls = useTls; + public LiteClientConfig setConsumerGroup(String consumerGroup) { + this.consumerGroup = consumerGroup; + return this; + } + + public String getProducerGroup() { + return producerGroup; + } + + public LiteClientConfig setProducerGroup(String producerGroup) { + this.producerGroup = producerGroup; + return this; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("liteClientConfig={") - .append("liteProxyAddr=").append(liteProxyAddr).append(",") + .append("liteEventMeshAddr=").append(liteEventMeshAddr).append(",") + .append("loadBalanceType=").append(loadBalanceType).append(",") .append("consumeThreadCore=").append(consumeThreadCore).append(",") .append("consumeThreadMax=").append(consumeThreadMax).append(",") .append("env=").append(env).append(",") - .append("region=").append(region).append(",") .append("idc=").append(idc).append(",") - .append("dcn=").append(dcn).append(",") + .append("producerGroup=").append(producerGroup).append(",") + .append("consumerGroup=").append(consumerGroup).append(",") .append("ip=").append(ip).append(",") .append("pid=").append(pid).append(",") .append("sys=").append(sys).append(",") diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/consumer/HandleResult.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/consumer/HandleResult.java similarity index 94% rename from eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/consumer/HandleResult.java rename to eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/consumer/HandleResult.java index 0b88101989..c5a9a85bd3 100644 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/consumer/HandleResult.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/consumer/HandleResult.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.webank.eventmesh.client.http.consumer; +package org.apache.eventmesh.client.http.consumer; public enum HandleResult { OK, diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/consumer/LiteConsumer.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/consumer/LiteConsumer.java new file mode 100644 index 0000000000..7841a67eaa --- /dev/null +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/consumer/LiteConsumer.java @@ -0,0 +1,304 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.client.http.consumer; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.*; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; + +import io.netty.handler.codec.http.HttpMethod; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.RandomUtils; +import org.apache.eventmesh.client.http.AbstractLiteClient; +import org.apache.eventmesh.client.http.EventMeshRetObj; +import org.apache.eventmesh.client.http.RemotingServer; +import org.apache.eventmesh.client.http.conf.LiteClientConfig; +import org.apache.eventmesh.client.http.consumer.listener.LiteMessageListener; +import org.apache.eventmesh.client.http.http.HttpUtil; +import org.apache.eventmesh.client.http.http.RequestParam; +import org.apache.eventmesh.client.tcp.common.EventMeshCommon; +import org.apache.eventmesh.client.tcp.common.EventMeshThreadFactoryImpl; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.EventMeshException; +import org.apache.eventmesh.common.ThreadPoolFactory; +import org.apache.eventmesh.common.protocol.SubscriptionItem; +import org.apache.eventmesh.common.protocol.http.body.client.HeartbeatRequestBody; +import org.apache.eventmesh.common.protocol.http.body.client.SubscribeRequestBody; +import org.apache.eventmesh.common.protocol.http.body.client.UnSubscribeRequestBody; +import org.apache.eventmesh.common.protocol.http.common.ClientType; +import org.apache.eventmesh.common.protocol.http.common.EventMeshRetCode; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; +import org.apache.eventmesh.common.protocol.http.common.ProtocolVersion; +import org.apache.eventmesh.common.protocol.http.common.RequestCode; +import org.apache.eventmesh.common.protocol.tcp.Subscription; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class LiteConsumer extends AbstractLiteClient { + + public Logger logger = LoggerFactory.getLogger(LiteConsumer.class); + + private RemotingServer remotingServer; + + private ThreadPoolExecutor consumeExecutor; + + protected LiteClientConfig eventMeshClientConfig; + + private List subscription = Lists.newArrayList(); + + private LiteMessageListener messageListener; + + protected final ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(4, new EventMeshThreadFactoryImpl("TCPClientScheduler", true)); + + public LiteConsumer(LiteClientConfig liteClientConfig) throws Exception { + super(liteClientConfig); + this.consumeExecutor = ThreadPoolFactory.createThreadPoolExecutor(liteClientConfig.getConsumeThreadCore(), + liteClientConfig.getConsumeThreadMax(), "eventMesh-client-consume-"); + this.eventMeshClientConfig = liteClientConfig; +// this.remotingServer = new RemotingServer(10106, consumeExecutor); +// this.remotingServer.init(); + } + + public LiteConsumer(LiteClientConfig liteClientConfig, + ThreadPoolExecutor customExecutor) { + super(liteClientConfig); + this.consumeExecutor = customExecutor; + this.eventMeshClientConfig = liteClientConfig; +// this.remotingServer = new RemotingServer(this.consumeExecutor); + } + + private AtomicBoolean started = new AtomicBoolean(Boolean.FALSE); + + @Override + public void start() throws Exception { + Preconditions.checkState(eventMeshClientConfig != null, "eventMeshClientConfig can't be null"); + Preconditions.checkState(consumeExecutor != null, "consumeExecutor can't be null"); +// Preconditions.checkState(messageListener != null, "messageListener can't be null"); + logger.info("LiteConsumer starting"); + super.start(); + started.compareAndSet(false, true); + logger.info("LiteConsumer started"); +// this.remotingServer.start(); + } + + @Override + public void shutdown() throws Exception { + logger.info("LiteConsumer shutting down"); + super.shutdown(); + if (consumeExecutor != null) { + consumeExecutor.shutdown(); + } + scheduler.shutdown(); + started.compareAndSet(true, false); + logger.info("LiteConsumer shutdown"); + } + + public boolean subscribe(List topicList, String url) throws Exception { + subscription.addAll(topicList); + if (!started.get()) { + start(); + } + + RequestParam subscribeParam = generateSubscribeRequestParam(topicList, url); + + long startTime = System.currentTimeMillis(); + String target = selectEventMesh(); + String subRes = ""; + + try (CloseableHttpClient httpClient = setHttpClient()){ + subRes = HttpUtil.post(httpClient, target, subscribeParam); + } + + if (logger.isDebugEnabled()) { + logger.debug("subscribe message by await, targetEventMesh:{}, cost:{}ms, subscribeParam:{}, rtn:{}", target, System.currentTimeMillis() - startTime, JSON.toJSONString(subscribeParam), subRes); + } + + EventMeshRetObj ret = JSON.parseObject(subRes, EventMeshRetObj.class); + + if (ret.getRetCode() == EventMeshRetCode.SUCCESS.getRetCode()) { + return Boolean.TRUE; + } else { + throw new EventMeshException(ret.getRetCode(), ret.getRetMsg()); + } + + } + + private RequestParam generateSubscribeRequestParam(List topicList, String url) { +// final LiteMessage liteMessage = new LiteMessage(); +// liteMessage.setBizSeqNo(RandomStringUtils.randomNumeric(30)) +// .setContent("subscribe message") +// .setUniqueId(RandomStringUtils.randomNumeric(30)); + RequestParam requestParam = new RequestParam(HttpMethod.POST); + requestParam.addHeader(ProtocolKey.REQUEST_CODE, String.valueOf(RequestCode.SUBSCRIBE.getRequestCode())) + .addHeader(ProtocolKey.ClientInstanceKey.ENV, eventMeshClientConfig.getEnv()) + .addHeader(ProtocolKey.ClientInstanceKey.IDC, eventMeshClientConfig.getIdc()) + .addHeader(ProtocolKey.ClientInstanceKey.IP, eventMeshClientConfig.getIp()) + .addHeader(ProtocolKey.ClientInstanceKey.PID, eventMeshClientConfig.getPid()) + .addHeader(ProtocolKey.ClientInstanceKey.SYS, eventMeshClientConfig.getSys()) + .addHeader(ProtocolKey.ClientInstanceKey.USERNAME, eventMeshClientConfig.getUserName()) + .addHeader(ProtocolKey.ClientInstanceKey.PASSWD, eventMeshClientConfig.getPassword()) + .addHeader(ProtocolKey.VERSION, ProtocolVersion.V1.getVersion()) + .addHeader(ProtocolKey.LANGUAGE, Constants.LANGUAGE_JAVA) + .setTimeout(Constants.DEFAULT_HTTP_TIME_OUT) + .addBody(SubscribeRequestBody.TOPIC, JSONObject.toJSONString(topicList)) + .addBody(SubscribeRequestBody.CONSUMERGROUP, eventMeshClientConfig.getConsumerGroup()) + .addBody(SubscribeRequestBody.URL, url); + return requestParam; + } + + private RequestParam generateHeartBeatRequestParam(List topics, String url) { + List heartbeatEntities = new ArrayList<>(); + for (SubscriptionItem item : topics) { + HeartbeatRequestBody.HeartbeatEntity heartbeatEntity = new HeartbeatRequestBody.HeartbeatEntity(); + heartbeatEntity.topic = item.getTopic(); + heartbeatEntity.url = url; + heartbeatEntities.add(heartbeatEntity); + } + + RequestParam requestParam = new RequestParam(HttpMethod.POST); + requestParam.addHeader(ProtocolKey.REQUEST_CODE, String.valueOf(RequestCode.HEARTBEAT.getRequestCode())) + .addHeader(ProtocolKey.ClientInstanceKey.ENV, eventMeshClientConfig.getEnv()) + .addHeader(ProtocolKey.ClientInstanceKey.IDC, eventMeshClientConfig.getIdc()) + .addHeader(ProtocolKey.ClientInstanceKey.IP, eventMeshClientConfig.getIp()) + .addHeader(ProtocolKey.ClientInstanceKey.PID, eventMeshClientConfig.getPid()) + .addHeader(ProtocolKey.ClientInstanceKey.SYS, eventMeshClientConfig.getSys()) + .addHeader(ProtocolKey.ClientInstanceKey.USERNAME, eventMeshClientConfig.getUserName()) + .addHeader(ProtocolKey.ClientInstanceKey.PASSWD, eventMeshClientConfig.getPassword()) + .addHeader(ProtocolKey.VERSION, ProtocolVersion.V1.getVersion()) + .addHeader(ProtocolKey.LANGUAGE, Constants.LANGUAGE_JAVA) + .setTimeout(Constants.DEFAULT_HTTP_TIME_OUT) + .addBody(HeartbeatRequestBody.CLIENTTYPE, ClientType.SUB.name()) + .addBody(HeartbeatRequestBody.CONSUMERGROUP, eventMeshClientConfig.getConsumerGroup()) + .addBody(HeartbeatRequestBody.HEARTBEATENTITIES, JSON.toJSONString(heartbeatEntities)); + return requestParam; + } + + public void heartBeat(List topicList, String url) throws Exception { + scheduler.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { + try { + if (!started.get()) { + start(); + } + RequestParam requestParam = generateHeartBeatRequestParam(topicList, url); + + long startTime = System.currentTimeMillis(); + String target = selectEventMesh(); + String res = ""; + + try (CloseableHttpClient httpClient = setHttpClient()) { + res = HttpUtil.post(httpClient, target, requestParam); + } + + if (logger.isDebugEnabled()) { + logger.debug("heartBeat message by await, targetEventMesh:{}, cost:{}ms, rtn:{}", target, System.currentTimeMillis() - startTime, res); + } + + EventMeshRetObj ret = JSON.parseObject(res, EventMeshRetObj.class); + + if (ret.getRetCode() == EventMeshRetCode.SUCCESS.getRetCode()) { + } else { + throw new EventMeshException(ret.getRetCode(), ret.getRetMsg()); + } + } catch (Exception e) { + logger.error("send heartBeat error", e); + } + } + }, EventMeshCommon.HEATBEAT, EventMeshCommon.HEATBEAT, TimeUnit.MILLISECONDS); + } + + public boolean unsubscribe(List topicList, String url) throws Exception { + Set unSub = new HashSet<>(topicList); + Iterator itr = subscription.iterator(); + while(itr.hasNext()) { + SubscriptionItem item = itr.next(); + if (unSub.contains(item.getTopic())) { + itr.remove(); + } + } + + RequestParam unSubscribeParam = generateUnSubscribeRequestParam(topicList, url); + + long startTime = System.currentTimeMillis(); + String target = selectEventMesh(); + String unSubRes = ""; + + try (CloseableHttpClient httpClient = setHttpClient()) { + unSubRes = HttpUtil.post(httpClient, target, unSubscribeParam); + } + + if (logger.isDebugEnabled()) { + logger.debug("unSubscribe message by await, targetEventMesh:{}, cost:{}ms, unSubscribeParam:{}, rtn:{}", target, System.currentTimeMillis() - startTime, JSON.toJSONString(unSubscribeParam), unSubRes); + } + + EventMeshRetObj ret = JSON.parseObject(unSubRes, EventMeshRetObj.class); + + if (ret.getRetCode() == EventMeshRetCode.SUCCESS.getRetCode()) { + return Boolean.TRUE; + } else { + throw new EventMeshException(ret.getRetCode(), ret.getRetMsg()); + } + } + + private RequestParam generateUnSubscribeRequestParam(List topicList, String url) { + RequestParam requestParam = new RequestParam(HttpMethod.POST); + requestParam.addHeader(ProtocolKey.REQUEST_CODE, String.valueOf(RequestCode.UNSUBSCRIBE.getRequestCode())) + .addHeader(ProtocolKey.ClientInstanceKey.ENV, eventMeshClientConfig.getEnv()) + .addHeader(ProtocolKey.ClientInstanceKey.IDC, eventMeshClientConfig.getIdc()) + .addHeader(ProtocolKey.ClientInstanceKey.IP, eventMeshClientConfig.getIp()) + .addHeader(ProtocolKey.ClientInstanceKey.PID, eventMeshClientConfig.getPid()) + .addHeader(ProtocolKey.ClientInstanceKey.SYS, eventMeshClientConfig.getSys()) + .addHeader(ProtocolKey.ClientInstanceKey.USERNAME, eventMeshClientConfig.getUserName()) + .addHeader(ProtocolKey.ClientInstanceKey.PASSWD, eventMeshClientConfig.getPassword()) + .addHeader(ProtocolKey.VERSION, ProtocolVersion.V1.getVersion()) + .addHeader(ProtocolKey.LANGUAGE, Constants.LANGUAGE_JAVA) + .setTimeout(Constants.DEFAULT_HTTP_TIME_OUT) + .addBody(UnSubscribeRequestBody.TOPIC, JSONObject.toJSONString(topicList)) + .addBody(UnSubscribeRequestBody.CONSUMERGROUP, eventMeshClientConfig.getConsumerGroup()) + .addBody(UnSubscribeRequestBody.URL, url); + return requestParam; + } + + public void registerMessageListener(LiteMessageListener messageListener) throws EventMeshException { + this.messageListener = messageListener; + remotingServer.registerMessageListener(this.messageListener); + } + + public String selectEventMesh() { + if (liteClientConfig.isUseTls()) { + return Constants.HTTPS_PROTOCOL_PREFIX + eventMeshServerSelector.select(); + } else { + return Constants.HTTP_PROTOCOL_PREFIX + eventMeshServerSelector.select(); + } + } +} diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/consumer/context/LiteConsumeContext.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/consumer/context/LiteConsumeContext.java new file mode 100644 index 0000000000..91d84a5738 --- /dev/null +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/consumer/context/LiteConsumeContext.java @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.client.http.consumer.context; + +import org.apache.commons.lang3.time.DateFormatUtils; +import org.apache.eventmesh.common.Constants; + +public class LiteConsumeContext { + + private String eventMeshIp; + + private String eventMeshEnv; + + private String eventMeshIdc; + + private String eventMeshCluster; + + //本地RETRY次数 + private int retryTimes = 0; + + private long createTime = System.currentTimeMillis(); + + public LiteConsumeContext(String eventMeshIp, String eventMeshEnv, + String eventMeshIdc, + String eventMeshCluster) { + this.eventMeshIp = eventMeshIp; + this.eventMeshEnv = eventMeshEnv; + this.eventMeshIdc = eventMeshIdc; + this.eventMeshCluster = eventMeshCluster; + + } + + public String getEventMeshIp() { + return eventMeshIp; + } + + public void setEventMeshIp(String eventMeshIp) { + this.eventMeshIp = eventMeshIp; + } + + public String getEventMeshEnv() { + return eventMeshEnv; + } + + public void setEventMeshEnv(String eventMeshEnv) { + this.eventMeshEnv = eventMeshEnv; + } + + public String getEventMeshIdc() { + return eventMeshIdc; + } + + public void setEventMeshIdc(String eventMeshIdc) { + this.eventMeshIdc = eventMeshIdc; + } + + public String getEventMeshCluster() { + return eventMeshCluster; + } + + public void setEventMeshCluster(String eventMeshCluster) { + this.eventMeshCluster = eventMeshCluster; + } + + public int getRetryTimes() { + return retryTimes; + } + + public void setRetryTimes(int retryTimes) { + this.retryTimes = retryTimes; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("liteConsumeContext={") + .append("eventMeshIp=").append(eventMeshIp).append(",") + .append("eventMeshEnv=").append(eventMeshEnv).append(",") + .append("eventMeshIdc=").append(eventMeshIdc).append(",") + .append("eventMeshCluster=").append(eventMeshCluster).append(",") + .append("retryTimes=").append(retryTimes).append(",") + .append("createTime=").append(DateFormatUtils.format(createTime, Constants.DATE_FORMAT)) + .append("}"); + return sb.toString(); + } + +} diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/consumer/listener/LiteMessageListener.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/consumer/listener/LiteMessageListener.java similarity index 80% rename from eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/consumer/listener/LiteMessageListener.java rename to eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/consumer/listener/LiteMessageListener.java index 3e2aa367d8..c7f5ac99af 100644 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/consumer/listener/LiteMessageListener.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/consumer/listener/LiteMessageListener.java @@ -15,11 +15,12 @@ * limitations under the License. */ -package com.webank.eventmesh.client.http.consumer.listener; +package org.apache.eventmesh.client.http.consumer.listener; -import com.webank.eventmesh.client.http.consumer.HandleResult; -import com.webank.eventmesh.client.http.consumer.context.LiteConsumeContext; -import com.webank.eventmesh.common.LiteMessage; + +import org.apache.eventmesh.client.http.consumer.HandleResult; +import org.apache.eventmesh.client.http.consumer.context.LiteConsumeContext; +import org.apache.eventmesh.common.LiteMessage; public interface LiteMessageListener { diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/http/HttpUtil.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/http/HttpUtil.java similarity index 92% rename from eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/http/HttpUtil.java rename to eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/http/HttpUtil.java index 05f9a266cf..4093a89ea1 100644 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/http/HttpUtil.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/http/HttpUtil.java @@ -15,12 +15,23 @@ * limitations under the License. */ -package com.webank.eventmesh.client.http.http; -import com.webank.eventmesh.common.Constants; +package org.apache.eventmesh.client.http.http; + +import java.io.IOException; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + import com.google.common.base.Preconditions; + import io.netty.handler.codec.http.HttpMethod; + import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.common.Constants; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; @@ -35,14 +46,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; -import java.nio.charset.Charset; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - public class HttpUtil { public static Logger logger = LoggerFactory.getLogger(HttpUtil.class); @@ -129,11 +132,11 @@ public static void post(CloseableHttpClient client, //ttl RequestConfig.Builder configBuilder = RequestConfig.custom(); - configBuilder.setSocketTimeout(Integer.valueOf(String.valueOf(requestParam.getTimeout()))) - .setConnectTimeout(Integer.valueOf(String.valueOf(requestParam.getTimeout()))) - .setConnectionRequestTimeout(Integer.valueOf(String.valueOf(requestParam.getTimeout()))); + configBuilder.setSocketTimeout(Integer.parseInt(String.valueOf(requestParam.getTimeout()))) + .setConnectTimeout(Integer.parseInt(String.valueOf(requestParam.getTimeout()))) + .setConnectionRequestTimeout(Integer.parseInt(String.valueOf(requestParam.getTimeout()))); - if(forwardAgent != null) { + if (forwardAgent != null) { configBuilder.setProxy(forwardAgent); } @@ -172,11 +175,11 @@ public static void get(CloseableHttpClient client, //ttl RequestConfig.Builder configBuilder = RequestConfig.custom(); - configBuilder.setSocketTimeout(Integer.valueOf(String.valueOf(requestParam.getTimeout()))) - .setConnectTimeout(Integer.valueOf(String.valueOf(requestParam.getTimeout()))) - .setConnectionRequestTimeout(Integer.valueOf(String.valueOf(requestParam.getTimeout()))); + configBuilder.setSocketTimeout(Integer.parseInt(String.valueOf(requestParam.getTimeout()))) + .setConnectTimeout(Integer.parseInt(String.valueOf(requestParam.getTimeout()))) + .setConnectionRequestTimeout(Integer.parseInt(String.valueOf(requestParam.getTimeout()))); - if(forwardAgent != null) { + if (forwardAgent != null) { configBuilder.setProxy(forwardAgent); } diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/http/RequestParam.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/http/RequestParam.java similarity index 98% rename from eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/http/RequestParam.java rename to eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/http/RequestParam.java index ce03e0be51..4d23222352 100644 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/http/RequestParam.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/http/RequestParam.java @@ -15,11 +15,7 @@ * limitations under the License. */ -package com.webank.eventmesh.client.http.http; - -import io.netty.handler.codec.http.HttpMethod; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +package org.apache.eventmesh.client.http.http; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; @@ -27,6 +23,11 @@ import java.util.HashMap; import java.util.Map; +import io.netty.handler.codec.http.HttpMethod; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class RequestParam { public Logger logger = LoggerFactory.getLogger(this.getClass()); diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/producer/LiteProducer.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/producer/LiteProducer.java similarity index 63% rename from eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/producer/LiteProducer.java rename to eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/producer/LiteProducer.java index bc32546991..a240b95354 100644 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/producer/LiteProducer.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/producer/LiteProducer.java @@ -15,67 +15,63 @@ * limitations under the License. */ -package com.webank.eventmesh.client.http.producer; +package org.apache.eventmesh.client.http.producer; + +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.util.concurrent.atomic.AtomicBoolean; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; + -import com.webank.eventmesh.client.http.AbstractLiteClient; -import com.webank.eventmesh.client.http.ProxyRetObj; -import com.webank.eventmesh.client.http.conf.LiteClientConfig; -import com.webank.eventmesh.client.http.http.HttpUtil; -import com.webank.eventmesh.client.http.http.RequestParam; -import com.webank.eventmesh.client.http.ssl.MyX509TrustManager; -import com.webank.eventmesh.common.Constants; -import com.webank.eventmesh.common.LiteMessage; -import com.webank.eventmesh.common.ProxyException; -import com.webank.eventmesh.common.protocol.http.body.message.SendMessageRequestBody; -import com.webank.eventmesh.common.protocol.http.body.message.SendMessageResponseBody; -import com.webank.eventmesh.common.protocol.http.common.ProtocolKey; -import com.webank.eventmesh.common.protocol.http.common.ProtocolVersion; -import com.webank.eventmesh.common.protocol.http.common.ProxyRetCode; -import com.webank.eventmesh.common.protocol.http.common.RequestCode; import com.alibaba.fastjson.JSON; import com.google.common.base.Preconditions; + import io.netty.handler.codec.http.HttpMethod; + import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.RandomUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.client.http.AbstractLiteClient; +import org.apache.eventmesh.client.http.EventMeshRetObj; +import org.apache.eventmesh.client.http.conf.LiteClientConfig; +import org.apache.eventmesh.client.http.http.HttpUtil; +import org.apache.eventmesh.client.http.http.RequestParam; +import org.apache.eventmesh.client.http.ssl.MyX509TrustManager; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.EventMeshException; +import org.apache.eventmesh.common.LiteMessage; +import org.apache.eventmesh.common.protocol.http.body.message.SendMessageRequestBody; +import org.apache.eventmesh.common.protocol.http.body.message.SendMessageResponseBody; +import org.apache.eventmesh.common.protocol.http.common.EventMeshRetCode; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; +import org.apache.eventmesh.common.protocol.http.common.ProtocolVersion; +import org.apache.eventmesh.common.protocol.http.common.RequestCode; +import org.apache.http.conn.ssl.DefaultHostnameVerifier; +import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.net.ssl.KeyManagerFactory; -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManager; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.nio.file.StandardOpenOption; -import java.security.KeyManagementException; -import java.security.KeyStore; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; -import java.util.concurrent.atomic.AtomicBoolean; - public class LiteProducer extends AbstractLiteClient { public Logger logger = LoggerFactory.getLogger(LiteProducer.class); - private static CloseableHttpClient httpClient = HttpClients.createDefault(); - public LiteProducer(LiteClientConfig liteClientConfig) { super(liteClientConfig); - if(liteClientConfig.isUseTls()){ - setHttpClient(); - } } private AtomicBoolean started = new AtomicBoolean(Boolean.FALSE); - public void start() throws ProxyException { + @Override + public void start() throws Exception { Preconditions.checkState(liteClientConfig != null, "liteClientConfig can't be null"); - Preconditions.checkState(liteClientConfig.getLiteProxyAddr() != null, "liteClientConfig.liteServerAddr can't be null"); - if(started.get()) { + Preconditions.checkState(liteClientConfig.getLiteEventMeshAddr() != null, "liteClientConfig.liteServerAddr can't be null"); + if (started.get()) { return; } logger.info("LiteProducer starting"); @@ -84,13 +80,13 @@ public void start() throws ProxyException { logger.info("LiteProducer started"); } + @Override public void shutdown() throws Exception { - if(!started.get()) { + if (!started.get()) { return; } logger.info("LiteProducer shutting down"); super.shutdown(); - httpClient.close(); started.compareAndSet(true, false); logger.info("LiteProducer shutdown"); } @@ -99,20 +95,18 @@ public AtomicBoolean getStarted() { return started; } - public boolean publish(LiteMessage message) throws ProxyException { + public boolean publish(LiteMessage message) throws Exception { if (!started.get()) { start(); } Preconditions.checkState(StringUtils.isNotBlank(message.getTopic()), - "proxyMessage[topic] invalid"); + "eventMeshMessage[topic] invalid"); Preconditions.checkState(StringUtils.isNotBlank(message.getContent()), - "proxyMessage[content] invalid"); + "eventMeshMessage[content] invalid"); RequestParam requestParam = new RequestParam(HttpMethod.POST); requestParam.addHeader(ProtocolKey.REQUEST_CODE, String.valueOf(RequestCode.MSG_SEND_ASYNC.getRequestCode())) .addHeader(ProtocolKey.ClientInstanceKey.ENV, liteClientConfig.getEnv()) - .addHeader(ProtocolKey.ClientInstanceKey.REGION, liteClientConfig.getRegion()) .addHeader(ProtocolKey.ClientInstanceKey.IDC, liteClientConfig.getIdc()) - .addHeader(ProtocolKey.ClientInstanceKey.DCN, liteClientConfig.getDcn()) .addHeader(ProtocolKey.ClientInstanceKey.IP, liteClientConfig.getIp()) .addHeader(ProtocolKey.ClientInstanceKey.PID, liteClientConfig.getPid()) .addHeader(ProtocolKey.ClientInstanceKey.SYS, liteClientConfig.getSys()) @@ -121,60 +115,55 @@ public boolean publish(LiteMessage message) throws ProxyException { .addHeader(ProtocolKey.VERSION, ProtocolVersion.V1.getVersion()) .addHeader(ProtocolKey.LANGUAGE, Constants.LANGUAGE_JAVA) .setTimeout(Constants.DEFAULT_HTTP_TIME_OUT) + .addBody(SendMessageRequestBody.PRODUCERGROUP, liteClientConfig.getProducerGroup()) .addBody(SendMessageRequestBody.TOPIC, message.getTopic()) .addBody(SendMessageRequestBody.CONTENT, message.getContent()) - .addBody(SendMessageRequestBody.TTL, message.getPropKey(Constants.PROXY_MESSAGE_CONST_TTL)) + .addBody(SendMessageRequestBody.TTL, message.getPropKey(Constants.EVENTMESH_MESSAGE_CONST_TTL)) .addBody(SendMessageRequestBody.BIZSEQNO, message.getBizSeqNo()) .addBody(SendMessageRequestBody.UNIQUEID, message.getUniqueId()); long startTime = System.currentTimeMillis(); - String target = selectProxy(); + String target = selectEventMesh(); String res = ""; - try { + + try (CloseableHttpClient httpClient = setHttpClient()) { res = HttpUtil.post(httpClient, target, requestParam); - } catch (Exception ex) { - throw new ProxyException(ex); } - if(logger.isDebugEnabled()) { - logger.debug("publish async message, targetProxy:{}, cost:{}ms, message:{}, rtn:{}", + if (logger.isDebugEnabled()) { + logger.debug("publish async message, targetEventMesh:{}, cost:{}ms, message:{}, rtn:{}", target, System.currentTimeMillis() - startTime, message, res); } - ProxyRetObj ret = JSON.parseObject(res, ProxyRetObj.class); + EventMeshRetObj ret = JSON.parseObject(res, EventMeshRetObj.class); - if (ret.getRetCode() == ProxyRetCode.SUCCESS.getRetCode()) { + if (ret.getRetCode() == EventMeshRetCode.SUCCESS.getRetCode()) { return Boolean.TRUE; } else { - throw new ProxyException(ret.getRetCode(), ret.getRetMsg()); + throw new EventMeshException(ret.getRetCode(), ret.getRetMsg()); } } - public String selectProxy() { - if (CollectionUtils.isEmpty(proxyServerList)) { - return null; - } - if(liteClientConfig.isUseTls()){ - return Constants.HTTPS_PROTOCOL_PREFIX + proxyServerList.get(RandomUtils.nextInt(0, proxyServerList.size())); - }else{ - return Constants.HTTP_PROTOCOL_PREFIX + proxyServerList.get(RandomUtils.nextInt(0, proxyServerList.size())); + public String selectEventMesh() { + if (liteClientConfig.isUseTls()) { + return Constants.HTTPS_PROTOCOL_PREFIX + eventMeshServerSelector.select(); + } else { + return Constants.HTTP_PROTOCOL_PREFIX + eventMeshServerSelector.select(); } } - public LiteMessage request(LiteMessage message, long timeout) throws ProxyException { - if(!started.get()) { + public LiteMessage request(LiteMessage message, long timeout) throws Exception { + if (!started.get()) { start(); } Preconditions.checkState(StringUtils.isNotBlank(message.getTopic()), - "proxyMessage[topic] invalid"); + "eventMeshMessage[topic] invalid"); Preconditions.checkState(StringUtils.isNotBlank(message.getContent()), - "proxyMessage[content] invalid"); + "eventMeshMessage[content] invalid"); RequestParam requestParam = new RequestParam(HttpMethod.POST); requestParam.addHeader(ProtocolKey.REQUEST_CODE, String.valueOf(RequestCode.MSG_SEND_SYNC.getRequestCode())) .addHeader(ProtocolKey.ClientInstanceKey.ENV, liteClientConfig.getEnv()) - .addHeader(ProtocolKey.ClientInstanceKey.REGION, liteClientConfig.getRegion()) .addHeader(ProtocolKey.ClientInstanceKey.IDC, liteClientConfig.getIdc()) - .addHeader(ProtocolKey.ClientInstanceKey.DCN, liteClientConfig.getDcn()) .addHeader(ProtocolKey.ClientInstanceKey.IP, liteClientConfig.getIp()) .addHeader(ProtocolKey.ClientInstanceKey.PID, liteClientConfig.getPid()) .addHeader(ProtocolKey.ClientInstanceKey.SYS, liteClientConfig.getSys()) @@ -183,6 +172,7 @@ public LiteMessage request(LiteMessage message, long timeout) throws ProxyExcept .addHeader(ProtocolKey.VERSION, ProtocolVersion.V1.getVersion()) .addHeader(ProtocolKey.LANGUAGE, Constants.LANGUAGE_JAVA) .setTimeout(timeout) + .addBody(SendMessageRequestBody.PRODUCERGROUP, liteClientConfig.getProducerGroup()) .addBody(SendMessageRequestBody.TOPIC, message.getTopic()) .addBody(SendMessageRequestBody.CONTENT, message.getContent()) .addBody(SendMessageRequestBody.TTL, String.valueOf(timeout)) @@ -190,47 +180,44 @@ public LiteMessage request(LiteMessage message, long timeout) throws ProxyExcept .addBody(SendMessageRequestBody.UNIQUEID, message.getUniqueId()); long startTime = System.currentTimeMillis(); - String target = selectProxy(); + String target = selectEventMesh(); String res = ""; - try { + + try (CloseableHttpClient httpClient = setHttpClient()) { res = HttpUtil.post(httpClient, target, requestParam); - } catch (Exception ex) { - throw new ProxyException(ex); } - if(logger.isDebugEnabled()) { - logger.debug("publish sync message by await, targetProxy:{}, cost:{}ms, message:{}, rtn:{}", target, System.currentTimeMillis() - startTime, message, res); + if (logger.isDebugEnabled()) { + logger.debug("publish sync message by await, targetEventMesh:{}, cost:{}ms, message:{}, rtn:{}", target, System.currentTimeMillis() - startTime, message, res); } - ProxyRetObj ret = JSON.parseObject(res, ProxyRetObj.class); - if (ret.getRetCode() == ProxyRetCode.SUCCESS.getRetCode()) { - LiteMessage proxyMessage = new LiteMessage(); + EventMeshRetObj ret = JSON.parseObject(res, EventMeshRetObj.class); + if (ret.getRetCode() == EventMeshRetCode.SUCCESS.getRetCode()) { + LiteMessage eventMeshMessage = new LiteMessage(); SendMessageResponseBody.ReplyMessage replyMessage = JSON.parseObject(ret.getRetMsg(), SendMessageResponseBody.ReplyMessage.class); - proxyMessage.setContent(replyMessage.body).setProp(replyMessage.properties) + eventMeshMessage.setContent(replyMessage.body).setProp(replyMessage.properties) .setTopic(replyMessage.topic); - return proxyMessage; + return eventMeshMessage; } return null; } - public void request(LiteMessage message, RRCallback rrCallback, long timeout) throws ProxyException { - if(!started.get()) { + public void request(LiteMessage message, RRCallback rrCallback, long timeout) throws Exception { + if (!started.get()) { start(); } Preconditions.checkState(StringUtils.isNotBlank(message.getTopic()), - "proxyMessage[topic] invalid"); + "eventMeshMessage[topic] invalid"); Preconditions.checkState(StringUtils.isNotBlank(message.getContent()), - "proxyMessage[content] invalid"); + "eventMeshMessage[content] invalid"); Preconditions.checkState(ObjectUtils.allNotNull(rrCallback), "rrCallback invalid"); RequestParam requestParam = new RequestParam(HttpMethod.POST); requestParam.addHeader(ProtocolKey.REQUEST_CODE, String.valueOf(RequestCode.MSG_SEND_SYNC.getRequestCode())) .addHeader(ProtocolKey.ClientInstanceKey.ENV, liteClientConfig.getEnv()) - .addHeader(ProtocolKey.ClientInstanceKey.REGION, liteClientConfig.getRegion()) .addHeader(ProtocolKey.ClientInstanceKey.IDC, liteClientConfig.getIdc()) - .addHeader(ProtocolKey.ClientInstanceKey.DCN, liteClientConfig.getDcn()) .addHeader(ProtocolKey.ClientInstanceKey.IP, liteClientConfig.getIp()) .addHeader(ProtocolKey.ClientInstanceKey.PID, liteClientConfig.getPid()) .addHeader(ProtocolKey.ClientInstanceKey.SYS, liteClientConfig.getSys()) @@ -239,6 +226,7 @@ public void request(LiteMessage message, RRCallback rrCallback, long timeout) th .addHeader(ProtocolKey.VERSION, ProtocolVersion.V1.getVersion()) .addHeader(ProtocolKey.LANGUAGE, Constants.LANGUAGE_JAVA) .setTimeout(timeout) + .addBody(SendMessageRequestBody.PRODUCERGROUP, liteClientConfig.getProducerGroup()) .addBody(SendMessageRequestBody.TOPIC, message.getTopic()) .addBody(SendMessageRequestBody.CONTENT, message.getContent()) .addBody(SendMessageRequestBody.TTL, String.valueOf(timeout)) @@ -246,33 +234,14 @@ public void request(LiteMessage message, RRCallback rrCallback, long timeout) th .addBody(SendMessageRequestBody.UNIQUEID, message.getUniqueId()); long startTime = System.currentTimeMillis(); - String target = selectProxy(); - try { + String target = selectEventMesh(); + + try (CloseableHttpClient httpClient = setHttpClient()) { HttpUtil.post(httpClient, null, target, requestParam, new RRCallbackResponseHandlerAdapter(message, rrCallback, timeout)); - } catch (Exception ex) { - throw new ProxyException(ex); } - if(logger.isDebugEnabled()) { + if (logger.isDebugEnabled()) { logger.debug("publish sync message by async, target:{}, cost:{}, message:{}", target, System.currentTimeMillis() - startTime, message); } } - - public static void setHttpClient() { - SSLContext sslContext = null; - try { - String protocol = System.getProperty("ssl.client.protocol", "TLSv1.1"); - TrustManager[] tm = new TrustManager[] { new MyX509TrustManager() }; - sslContext = SSLContext.getInstance(protocol); - sslContext.init(null, tm, new SecureRandom()); - httpClient = HttpClients.custom().setSslcontext(sslContext) - .setHostnameVerifier(SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER).build(); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (KeyManagementException e) { - e.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); - } - } } diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/producer/RRCallback.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/producer/RRCallback.java similarity index 90% rename from eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/producer/RRCallback.java rename to eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/producer/RRCallback.java index 53fbaea84c..c5187984de 100644 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/producer/RRCallback.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/producer/RRCallback.java @@ -15,10 +15,10 @@ * limitations under the License. */ -package com.webank.eventmesh.client.http.producer; +package org.apache.eventmesh.client.http.producer; -import com.webank.eventmesh.common.LiteMessage; +import org.apache.eventmesh.common.LiteMessage; public interface RRCallback { diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/producer/RRCallbackResponseHandlerAdapter.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/producer/RRCallbackResponseHandlerAdapter.java similarity index 79% rename from eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/producer/RRCallbackResponseHandlerAdapter.java rename to eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/producer/RRCallbackResponseHandlerAdapter.java index a70f8c548b..aba948bc32 100644 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/producer/RRCallbackResponseHandlerAdapter.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/producer/RRCallbackResponseHandlerAdapter.java @@ -15,16 +15,21 @@ * limitations under the License. */ -package com.webank.eventmesh.client.http.producer; - -import com.webank.eventmesh.client.http.ProxyRetObj; -import com.webank.eventmesh.common.Constants; -import com.webank.eventmesh.common.LiteMessage; -import com.webank.eventmesh.common.ProxyException; -import com.webank.eventmesh.common.protocol.http.body.message.SendMessageResponseBody; -import com.webank.eventmesh.common.protocol.http.common.ProxyRetCode; +package org.apache.eventmesh.client.http.producer; + +import java.io.IOException; +import java.nio.charset.Charset; + + import com.alibaba.fastjson.JSON; + import org.apache.commons.lang3.time.DateFormatUtils; +import org.apache.eventmesh.client.http.EventMeshRetObj; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.EventMeshException; +import org.apache.eventmesh.common.LiteMessage; +import org.apache.eventmesh.common.protocol.http.body.message.SendMessageResponseBody; +import org.apache.eventmesh.common.protocol.http.common.EventMeshRetCode; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; @@ -33,9 +38,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; -import java.nio.charset.Charset; - public class RRCallbackResponseHandlerAdapter implements ResponseHandler { public Logger logger = LoggerFactory.getLogger(RRCallbackResponseHandlerAdapter.class); @@ -58,7 +60,7 @@ public RRCallbackResponseHandlerAdapter(LiteMessage liteMessage, RRCallback rrCa @Override public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { - rrCallback.onException(new ProxyException(response.toString())); + rrCallback.onException(new EventMeshException(response.toString())); return response.toString(); } @@ -70,14 +72,14 @@ public String handleResponse(HttpResponse response) throws ClientProtocolExcepti timeout, System.currentTimeMillis() - createTime); logger.warn(err); - rrCallback.onException(new ProxyException(err)); + rrCallback.onException(new EventMeshException(err)); return err; } String res = EntityUtils.toString(response.getEntity(), Charset.forName(Constants.DEFAULT_CHARSET)); - ProxyRetObj ret = JSON.parseObject(res, ProxyRetObj.class); - if (ret.getRetCode() != ProxyRetCode.SUCCESS.getRetCode()) { - rrCallback.onException(new ProxyException(ret.getRetCode(), ret.getRetMsg())); + EventMeshRetObj ret = JSON.parseObject(res, EventMeshRetObj.class); + if (ret.getRetCode() != EventMeshRetCode.SUCCESS.getRetCode()) { + rrCallback.onException(new EventMeshException(ret.getRetCode(), ret.getRetMsg())); return res; } @@ -89,7 +91,7 @@ public String handleResponse(HttpResponse response) throws ClientProtocolExcepti .setTopic(replyMessage.topic); rrCallback.onSuccess(liteMessage); } catch (Exception ex) { - rrCallback.onException(new ProxyException(ex)); + rrCallback.onException(new EventMeshException(ex)); return ex.toString(); } diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/ssl/MyX509TrustManager.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/ssl/MyX509TrustManager.java similarity index 68% rename from eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/ssl/MyX509TrustManager.java rename to eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/ssl/MyX509TrustManager.java index 8576f57aaa..6128b38fd0 100644 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/http/ssl/MyX509TrustManager.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/ssl/MyX509TrustManager.java @@ -1,6 +1,22 @@ -package com.webank.eventmesh.client.http.ssl; +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.client.http.ssl; -import org.apache.commons.lang3.StringUtils; import java.io.File; import java.nio.file.Files; import java.nio.file.Paths; @@ -12,15 +28,18 @@ import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactory; import javax.net.ssl.X509TrustManager; + +import org.apache.commons.lang3.StringUtils; + public class MyX509TrustManager implements X509TrustManager { X509TrustManager myTrustManager; public MyX509TrustManager() throws Exception { KeyStore keyStore = KeyStore.getInstance("JKS"); - String fileName = System.getProperty("ssl.client.cer","sChat2.jks"); + String fileName = System.getProperty("ssl.client.cer", "sChat2.jks"); String pass = System.getProperty("ssl.client.pass", "sNetty"); char[] filePass = null; - if(StringUtils.isNotBlank(pass)){ + if (StringUtils.isNotBlank(pass)) { filePass = pass.toCharArray(); } keyStore.load(Files.newInputStream(Paths.get(System.getProperty("confPath", System.getenv("confPath")) diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/util/HttpLoadBalanceUtils.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/util/HttpLoadBalanceUtils.java new file mode 100644 index 0000000000..4350613133 --- /dev/null +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/util/HttpLoadBalanceUtils.java @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.client.http.util; + +import com.google.common.base.Splitter; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.eventmesh.client.http.conf.LiteClientConfig; +import org.apache.eventmesh.common.EventMeshException; +import org.apache.eventmesh.common.loadbalance.LoadBalanceSelector; +import org.apache.eventmesh.common.loadbalance.RandomLoadBalanceSelector; +import org.apache.eventmesh.common.loadbalance.Weight; +import org.apache.eventmesh.common.loadbalance.WeightRoundRobinLoadBalanceSelector; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Pattern; + +public class HttpLoadBalanceUtils { + + private static final Pattern IP_PORT_PATTERN = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}:\\d{4,5}"); + private static final Pattern IP_PORT_WEIGHT_PATTERN = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}:\\d{4,5}:\\d{1,6}"); + + public static LoadBalanceSelector createEventMeshServerLoadBalanceSelector(LiteClientConfig liteClientConfig) + throws EventMeshException { + List eventMeshAddrs = Splitter.on(";").splitToList(liteClientConfig.getLiteEventMeshAddr()); + if (CollectionUtils.isEmpty(eventMeshAddrs)) { + throw new EventMeshException("liteEventMeshAddr can not be empty"); + } + + LoadBalanceSelector eventMeshServerSelector = null; + switch (liteClientConfig.getLoadBalanceType()) { + case RANDOM: + List eventMeshAddrList = new ArrayList<>(); + for (String eventMeshAddr : eventMeshAddrs) { + if (!IP_PORT_PATTERN.matcher(eventMeshAddr).matches()) { + throw new EventMeshException( + String.format("liteEventMeshAddr:%s is not illegal", liteClientConfig.getLiteEventMeshAddr())); + } + eventMeshAddrList.add(eventMeshAddr); + } + eventMeshServerSelector = new RandomLoadBalanceSelector<>(eventMeshAddrList); + break; + case WEIGHT_ROUND_ROBIN: + List> eventMeshAddrWeightList = new ArrayList<>(); + for (String eventMeshAddrWight : eventMeshAddrs) { + if (!IP_PORT_WEIGHT_PATTERN.matcher(eventMeshAddrWight).matches()) { + throw new EventMeshException( + String.format("liteEventMeshAddr:%s is not illegal", liteClientConfig.getLiteEventMeshAddr())); + } + int splitIndex = eventMeshAddrWight.lastIndexOf(":"); + Weight weight = new Weight<>( + eventMeshAddrWight.substring(0, splitIndex), + Integer.parseInt(eventMeshAddrWight.substring(splitIndex + 1)) + ); + eventMeshAddrWeightList.add(weight); + } + eventMeshServerSelector = new WeightRoundRobinLoadBalanceSelector<>(eventMeshAddrWeightList); + break; + default: + // ignore + } + if (eventMeshServerSelector == null) { + throw new EventMeshException("liteEventMeshAddr param illegal,please check"); + } + return eventMeshServerSelector; + } + +} diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/WemqAccessClient.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/EventMeshClient.java similarity index 74% rename from eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/WemqAccessClient.java rename to eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/EventMeshClient.java index 01f0532189..afdb90ac5f 100644 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/WemqAccessClient.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/EventMeshClient.java @@ -15,13 +15,15 @@ * limitations under the License. */ -package com.webank.eventmesh.client.tcp; +package org.apache.eventmesh.client.tcp; -import com.webank.eventmesh.client.tcp.common.AsyncRRCallback; -import com.webank.eventmesh.client.tcp.common.ReceiveMsgHook; -import com.webank.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.client.tcp.common.AsyncRRCallback; +import org.apache.eventmesh.client.tcp.common.ReceiveMsgHook; +import org.apache.eventmesh.common.protocol.SubcriptionType; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.common.protocol.SubscriptionMode; -public interface WemqAccessClient { +public interface EventMeshClient { Package rr(Package msg, long timeout) throws Exception; @@ -39,7 +41,7 @@ public interface WemqAccessClient { void listen() throws Exception; - void subscribe(String topic) throws Exception; + void subscribe(String topic, SubscriptionMode subscriptionMode, SubcriptionType subcriptionType) throws Exception; void unsubscribe() throws Exception; diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/SimplePubClient.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/SimplePubClient.java similarity index 82% rename from eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/SimplePubClient.java rename to eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/SimplePubClient.java index 072b121b91..239cfa423d 100644 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/SimplePubClient.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/SimplePubClient.java @@ -15,13 +15,13 @@ * limitations under the License. */ -package com.webank.eventmesh.client.tcp; +package org.apache.eventmesh.client.tcp; -import com.webank.eventmesh.client.tcp.common.AsyncRRCallback; -import com.webank.eventmesh.client.tcp.common.ReceiveMsgHook; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; -import com.webank.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.client.tcp.common.AsyncRRCallback; +import org.apache.eventmesh.client.tcp.common.ReceiveMsgHook; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; public interface SimplePubClient { diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/SimpleSubClient.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/SimpleSubClient.java new file mode 100644 index 0000000000..3803edcb07 --- /dev/null +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/SimpleSubClient.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.client.tcp; + + +import org.apache.eventmesh.client.tcp.common.ReceiveMsgHook; +import org.apache.eventmesh.common.protocol.SubcriptionType; +import org.apache.eventmesh.common.protocol.SubscriptionMode; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; + +public interface SimpleSubClient { + void init() throws Exception; + + void close(); + + void heartbeat() throws Exception; + + void reconnect() throws Exception; + + void subscribe(String topic, SubscriptionMode subscriptionMode, SubcriptionType subcriptionType) throws Exception; + + void unsubscribe() throws Exception; + + void listen() throws Exception; + + void registerBusiHandler(ReceiveMsgHook handler) throws Exception; + + UserAgent getUserAgent(); +} diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/common/AsyncRRCallback.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/AsyncRRCallback.java similarity index 89% rename from eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/common/AsyncRRCallback.java rename to eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/AsyncRRCallback.java index 39621f58bd..135e88dd88 100644 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/common/AsyncRRCallback.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/AsyncRRCallback.java @@ -15,8 +15,7 @@ * limitations under the License. */ -package com.webank.eventmesh.client.tcp.common; -import com.webank.eventmesh.common.protocol.tcp.Package; +package org.apache.eventmesh.client.tcp.common; public interface AsyncRRCallback { void callback(Package msg); diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/common/WemqAccessCommon.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/EventMeshCommon.java similarity index 92% rename from eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/common/WemqAccessCommon.java rename to eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/EventMeshCommon.java index 4f878cd244..65aa40e918 100644 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/common/WemqAccessCommon.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/EventMeshCommon.java @@ -15,9 +15,9 @@ * limitations under the License. */ -package com.webank.eventmesh.client.tcp.common; +package org.apache.eventmesh.client.tcp.common; -public class WemqAccessCommon { +public class EventMeshCommon { /** * 打印线程池状态的间隔时间 */ @@ -49,14 +49,14 @@ public class WemqAccessCommon { public static int SESSION_CLEAN_EXPIRE = 5 * 1000; /** - * WEMQ校验用户名 + * EventMesh校验用户名 */ - public static String WEMQ_USER = "wemq"; + public static String EventMesh_USER = "EventMesh"; /** - * WEMQ校验用户密码 + * EventMesh校验用户密码 */ - public static String WEMQ_PASS = "wemq@123"; + public static String EventMesh_PASS = "EventMesh@123"; /** * 服务器共有的超时时间 diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/common/WemqAccessThreadFactoryImpl.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/EventMeshThreadFactoryImpl.java similarity index 79% rename from eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/common/WemqAccessThreadFactoryImpl.java rename to eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/EventMeshThreadFactoryImpl.java index 3033c07986..49692baa92 100644 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/common/WemqAccessThreadFactoryImpl.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/EventMeshThreadFactoryImpl.java @@ -15,21 +15,21 @@ * limitations under the License. */ -package com.webank.eventmesh.client.tcp.common; +package org.apache.eventmesh.client.tcp.common; import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicLong; -public class WemqAccessThreadFactoryImpl implements ThreadFactory { +public class EventMeshThreadFactoryImpl implements ThreadFactory { private final AtomicLong threadIndex = new AtomicLong(0); private final String threadNamePrefix; private Boolean isDaemonSpecified = null; - public WemqAccessThreadFactoryImpl(final String threadNamePrefix) { + public EventMeshThreadFactoryImpl(final String threadNamePrefix) { this.threadNamePrefix = threadNamePrefix; } - public WemqAccessThreadFactoryImpl(final String threadNamePrefix, final boolean isDaemonSpecified) { + public EventMeshThreadFactoryImpl(final String threadNamePrefix, final boolean isDaemonSpecified) { this.threadNamePrefix = threadNamePrefix; this.isDaemonSpecified = isDaemonSpecified; } @@ -40,7 +40,7 @@ public String getThreadNamePrefix() { @Override public Thread newThread(Runnable r) { - Thread t = new Thread(r, threadNamePrefix +'-'+ this.threadIndex.incrementAndGet()); + Thread t = new Thread(r, threadNamePrefix + '-' + this.threadIndex.incrementAndGet()); if (isDaemonSpecified != null) { t.setDaemon(isDaemonSpecified); } diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/common/MessageUtils.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/MessageUtils.java similarity index 78% rename from eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/common/MessageUtils.java rename to eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/MessageUtils.java index 56a92a3cc7..2d6c7ca834 100644 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/common/MessageUtils.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/MessageUtils.java @@ -15,17 +15,19 @@ * limitations under the License. */ -package com.webank.eventmesh.client.tcp.common; +package org.apache.eventmesh.client.tcp.common; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Header; -import com.webank.eventmesh.common.protocol.tcp.Subscription; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; -import com.webank.eventmesh.common.protocol.tcp.Package; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ThreadLocalRandom; +import org.apache.eventmesh.common.protocol.SubcriptionType; +import org.apache.eventmesh.common.protocol.tcp.Subscription; +import org.apache.eventmesh.common.protocol.SubscriptionItem; +import org.apache.eventmesh.common.protocol.SubscriptionMode; +import org.apache.eventmesh.common.protocol.tcp.*; +import org.apache.eventmesh.common.protocol.tcp.Package; + public class MessageUtils { private static final int seqLength = 10; @@ -54,10 +56,10 @@ public static Package listen() { return msg; } - public static Package subscribe(String topic) { + public static Package subscribe(String topic, SubscriptionMode subscriptionMode, SubcriptionType subcriptionType) { Package msg = new Package(); msg.setHeader(new Header(Command.SUBSCRIBE_REQUEST, 0, null, generateRandomString(seqLength))); - msg.setBody(generateSubscription(topic)); + msg.setBody(generateSubscription(topic, subscriptionMode, subcriptionType)); return msg; } @@ -97,7 +99,7 @@ public static Package responseToClientAck(Package in) { public static UserAgent generateSubClient(UserAgent agent) { UserAgent user = new UserAgent(); - user.setDcn(agent.getDcn()); + user.setEnv(agent.getEnv()); user.setHost(agent.getHost()); user.setPassword(agent.getPassword()); user.setUsername(agent.getUsername()); @@ -107,14 +109,15 @@ public static UserAgent generateSubClient(UserAgent agent) { user.setPid(agent.getPid()); user.setVersion(agent.getVersion()); user.setIdc(agent.getIdc()); - - user.setPurpose(WemqAccessCommon.USER_AGENT_PURPOSE_SUB); + user.setConsumerGroup(agent.getConsumerGroup()); + user.setProducerGroup(agent.getProducerGroup()); + user.setPurpose(EventMeshCommon.USER_AGENT_PURPOSE_SUB); return user; } public static UserAgent generatePubClient(UserAgent agent) { UserAgent user = new UserAgent(); - user.setDcn(agent.getDcn()); + user.setEnv(agent.getEnv()); user.setHost(agent.getHost()); user.setPassword(agent.getPassword()); user.setUsername(agent.getUsername()); @@ -124,16 +127,16 @@ public static UserAgent generatePubClient(UserAgent agent) { user.setPid(agent.getPid()); user.setVersion(agent.getVersion()); user.setIdc(agent.getIdc()); - - user.setPurpose(WemqAccessCommon.USER_AGENT_PURPOSE_PUB); + user.setProducerGroup(agent.getProducerGroup()); + user.setPurpose(EventMeshCommon.USER_AGENT_PURPOSE_PUB); return user; } - private static Subscription generateSubscription(String topic) { + private static Subscription generateSubscription(String topic, SubscriptionMode subscriptionMode, SubcriptionType subcriptionType) { Subscription subscription = new Subscription(); - List topicList = new ArrayList<>(); - topicList.add(topic); - subscription.setTopicList(topicList); + List subscriptionItems = new ArrayList<>(); + subscriptionItems.add(new SubscriptionItem(topic, subscriptionMode, subcriptionType)); + subscription.setTopicList(subscriptionItems); return subscription; } @@ -145,4 +148,3 @@ private static String generateRandomString(int length) { return builder.toString(); } } - diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/common/ReceiveMsgHook.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/ReceiveMsgHook.java similarity index 90% rename from eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/common/ReceiveMsgHook.java rename to eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/ReceiveMsgHook.java index 24e8a92e15..bc60a6a8a2 100644 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/common/ReceiveMsgHook.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/ReceiveMsgHook.java @@ -15,10 +15,10 @@ * limitations under the License. */ -package com.webank.eventmesh.client.tcp.common; +package org.apache.eventmesh.client.tcp.common; import io.netty.channel.ChannelHandlerContext; -import com.webank.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.common.protocol.tcp.Package; public interface ReceiveMsgHook { void handle(Package msg, ChannelHandlerContext ctx); diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/common/RequestContext.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/RequestContext.java similarity index 96% rename from eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/common/RequestContext.java rename to eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/RequestContext.java index d58dc32781..ae36e6a746 100644 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/common/RequestContext.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/RequestContext.java @@ -15,12 +15,13 @@ * limitations under the License. */ -package com.webank.eventmesh.client.tcp.common; +package org.apache.eventmesh.client.tcp.common; +import java.util.concurrent.CountDownLatch; + +import org.apache.eventmesh.common.protocol.tcp.Package; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.webank.eventmesh.common.protocol.tcp.Package; -import java.util.concurrent.CountDownLatch; public class RequestContext { diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/common/TcpClient.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/TcpClient.java similarity index 82% rename from eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/common/TcpClient.java rename to eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/TcpClient.java index e82ef75be2..7df1e29f30 100644 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/common/TcpClient.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/TcpClient.java @@ -15,23 +15,39 @@ * limitations under the License. */ -package com.webank.eventmesh.client.tcp.common; +package org.apache.eventmesh.client.tcp.common; + +import java.io.Closeable; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.Random; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; -import com.webank.eventmesh.common.protocol.tcp.codec.Codec; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.PooledByteBufAllocator; -import io.netty.channel.*; +import io.netty.channel.AdaptiveRecvByteBufAllocator; +import io.netty.channel.Channel; +import io.netty.channel.ChannelDuplexHandler; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; + +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.common.protocol.tcp.codec.Codec; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.webank.eventmesh.common.protocol.tcp.Package; -import java.io.Closeable; -import java.io.IOException; -import java.net.InetSocketAddress; -import java.util.Random; -import java.util.concurrent.*; public abstract class TcpClient implements Closeable { private Logger logger = LoggerFactory.getLogger(this.getClass()); @@ -49,11 +65,11 @@ public abstract class TcpClient implements Closeable { private Channel channel; - protected static final ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(4, new WemqAccessThreadFactoryImpl("TCPClientScheduler", true)); + protected static final ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(4, new EventMeshThreadFactoryImpl("TCPClientScheduler", true)); private ScheduledFuture task; - public TcpClient(String host, int port){ + public TcpClient(String host, int port) { this.host = host; this.port = port; } @@ -130,7 +146,7 @@ private ChannelDuplexHandler newExceptionHandler() { return new ChannelDuplexHandler() { @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - logger.info("exceptionCaught, close connection.|remote address={}",ctx.channel().remoteAddress(),cause); + logger.info("exceptionCaught, close connection.|remote address={}", ctx.channel().remoteAddress(), cause); ctx.close(); } }; diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/impl/DefaultWemqAccessClient.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/DefaultEventMeshClient.java similarity index 76% rename from eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/impl/DefaultWemqAccessClient.java rename to eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/DefaultEventMeshClient.java index 7172d6994b..8e645fa35f 100644 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/impl/DefaultWemqAccessClient.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/DefaultEventMeshClient.java @@ -15,19 +15,21 @@ * limitations under the License. */ -package com.webank.eventmesh.client.tcp.impl; +package org.apache.eventmesh.client.tcp.impl; -import com.webank.eventmesh.client.tcp.SimplePubClient; -import com.webank.eventmesh.client.tcp.SimpleSubClient; -import com.webank.eventmesh.client.tcp.WemqAccessClient; -import com.webank.eventmesh.client.tcp.common.AsyncRRCallback; -import com.webank.eventmesh.client.tcp.common.MessageUtils; -import com.webank.eventmesh.client.tcp.common.ReceiveMsgHook; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; -import com.webank.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.client.tcp.EventMeshClient; +import org.apache.eventmesh.client.tcp.SimplePubClient; +import org.apache.eventmesh.client.tcp.SimpleSubClient; +import org.apache.eventmesh.client.tcp.common.AsyncRRCallback; +import org.apache.eventmesh.client.tcp.common.MessageUtils; +import org.apache.eventmesh.client.tcp.common.ReceiveMsgHook; +import org.apache.eventmesh.common.protocol.SubcriptionType; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.common.protocol.SubscriptionMode; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; -public class DefaultWemqAccessClient implements WemqAccessClient { +public class DefaultEventMeshClient implements EventMeshClient { protected UserAgent agent; private String accessHost; private int accessPort; @@ -35,7 +37,7 @@ public class DefaultWemqAccessClient implements WemqAccessClient { private SimplePubClient pubClient; private SimpleSubClient subClient; - public DefaultWemqAccessClient(String accessHost, int accessPort, UserAgent agent) { + public DefaultEventMeshClient(String accessHost, int accessPort, UserAgent agent) { this.accessHost = accessHost; this.accessPort = accessPort; this.agent = agent; @@ -95,8 +97,8 @@ public void listen() throws Exception { } @Override - public void subscribe(String topic) throws Exception { - this.subClient.subscribe(topic); + public void subscribe(String topic, SubscriptionMode subscriptionMode, SubcriptionType subcriptionType) throws Exception { + this.subClient.subscribe(topic, subscriptionMode, subcriptionType); } @Override @@ -119,7 +121,7 @@ public void registerPubBusiHandler(ReceiveMsgHook handler) throws Exception { @Override public String toString() { - return "DefaultWemqAccessClient{" + + return "DefaultEventMeshClient{" + "accessHost='" + accessHost + '\'' + ", accessPort=" + accessPort + ", agent=" + agent + diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/impl/SimplePubClientImpl.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/SimplePubClientImpl.java similarity index 84% rename from eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/impl/SimplePubClientImpl.java rename to eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/SimplePubClientImpl.java index 600aefdd5b..da5691a79f 100644 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/impl/SimplePubClientImpl.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/SimplePubClientImpl.java @@ -15,28 +15,30 @@ * limitations under the License. */ -package com.webank.eventmesh.client.tcp.impl; - -import com.webank.eventmesh.client.tcp.SimplePubClient; -import com.webank.eventmesh.client.tcp.common.AsyncRRCallback; -import com.webank.eventmesh.client.tcp.common.MessageUtils; -import com.webank.eventmesh.client.tcp.common.ReceiveMsgHook; -import com.webank.eventmesh.client.tcp.common.RequestContext; -import com.webank.eventmesh.client.tcp.common.TcpClient; -import com.webank.eventmesh.client.tcp.common.WemqAccessCommon; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; +package org.apache.eventmesh.client.tcp.impl; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; + +import org.apache.eventmesh.client.tcp.SimplePubClient; +import org.apache.eventmesh.client.tcp.common.AsyncRRCallback; +import org.apache.eventmesh.client.tcp.common.EventMeshCommon; +import org.apache.eventmesh.client.tcp.common.MessageUtils; +import org.apache.eventmesh.client.tcp.common.ReceiveMsgHook; +import org.apache.eventmesh.client.tcp.common.RequestContext; +import org.apache.eventmesh.client.tcp.common.TcpClient; +import org.apache.eventmesh.common.protocol.tcp.Command; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; -import com.webank.eventmesh.common.protocol.tcp.Package; - public class SimplePubClientImpl extends TcpClient implements SimplePubClient { private Logger logger = LoggerFactory.getLogger(this.getClass()); @@ -87,21 +89,21 @@ public void run() { SimplePubClientImpl.this.reconnect(); } Package msg = MessageUtils.heartBeat(); - io(msg, WemqAccessCommon.DEFAULT_TIME_OUT_MILLS); + io(msg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); } catch (Exception e) { } } - }, WemqAccessCommon.HEATBEAT, WemqAccessCommon.HEATBEAT, TimeUnit.MILLISECONDS); + }, EventMeshCommon.HEATBEAT, EventMeshCommon.HEATBEAT, TimeUnit.MILLISECONDS); } private void goodbye() throws Exception { Package msg = MessageUtils.goodbye(); - this.io(msg, WemqAccessCommon.DEFAULT_TIME_OUT_MILLS); + this.io(msg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); } private void hello() throws Exception { Package msg = MessageUtils.hello(userAgent); - this.io(msg, WemqAccessCommon.DEFAULT_TIME_OUT_MILLS); + this.io(msg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); } /** @@ -133,7 +135,7 @@ public void asyncRR(Package msg, AsyncRRCallback callback, long timeout) throws } /** - * 发送事件消息, 只所以有返回值是ACCESS 给了ACK + * 发送事件消息, 只所以有返回值是EventMesh 给了ACK * * @param msg * @throws Exception diff --git a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/impl/SimpleSubClientImpl.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/SimpleSubClientImpl.java similarity index 72% rename from eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/impl/SimpleSubClientImpl.java rename to eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/SimpleSubClientImpl.java index 5104a520aa..38d52f653d 100644 --- a/eventmesh-sdk-java/src/main/java/com/webank/eventmesh/client/tcp/impl/SimpleSubClientImpl.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/SimpleSubClientImpl.java @@ -15,29 +15,33 @@ * limitations under the License. */ -package com.webank.eventmesh.client.tcp.impl; - -import com.webank.eventmesh.client.tcp.SimpleSubClient; -import com.webank.eventmesh.client.tcp.common.MessageUtils; -import com.webank.eventmesh.client.tcp.common.ReceiveMsgHook; -import com.webank.eventmesh.client.tcp.common.RequestContext; -import com.webank.eventmesh.client.tcp.common.TcpClient; -import com.webank.eventmesh.client.tcp.common.WemqAccessCommon; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; +package org.apache.eventmesh.client.tcp.impl; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; + import org.apache.commons.collections4.CollectionUtils; +import org.apache.eventmesh.client.tcp.SimpleSubClient; +import org.apache.eventmesh.client.tcp.common.EventMeshCommon; +import org.apache.eventmesh.client.tcp.common.MessageUtils; +import org.apache.eventmesh.client.tcp.common.ReceiveMsgHook; +import org.apache.eventmesh.client.tcp.common.RequestContext; +import org.apache.eventmesh.client.tcp.common.TcpClient; +import org.apache.eventmesh.common.protocol.SubcriptionType; +import org.apache.eventmesh.common.protocol.SubscriptionItem; +import org.apache.eventmesh.common.protocol.SubscriptionMode; +import org.apache.eventmesh.common.protocol.tcp.*; + +import org.apache.eventmesh.common.protocol.tcp.Package; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; -import com.webank.eventmesh.common.protocol.tcp.Package; - public class SimpleSubClientImpl extends TcpClient implements SimpleSubClient { private Logger logger = LoggerFactory.getLogger(this.getClass()); @@ -46,7 +50,7 @@ public class SimpleSubClientImpl extends TcpClient implements SimpleSubClient { private ReceiveMsgHook callback; - private List topics = new ArrayList(); + private List subscriptionItems = new ArrayList(); private ScheduledFuture task; @@ -68,10 +72,10 @@ public void init() throws Exception { public void reconnect() throws Exception { super.reconnect(); hello(); - if (!CollectionUtils.isEmpty(topics)) { - for (String topic : topics) { - Package request = MessageUtils.subscribe(topic); - this.io(request, WemqAccessCommon.DEFAULT_TIME_OUT_MILLS); + if (!CollectionUtils.isEmpty(subscriptionItems)) { + for (SubscriptionItem item : subscriptionItems) { + Package request = MessageUtils.subscribe(item.getTopic(), item.getMode(), item.getType()); + this.io(request, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); } } listen(); @@ -96,38 +100,38 @@ public void run() { SimpleSubClientImpl.this.reconnect(); } Package msg = MessageUtils.heartBeat(); - SimpleSubClientImpl.this.io(msg, WemqAccessCommon.DEFAULT_TIME_OUT_MILLS); + SimpleSubClientImpl.this.io(msg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); } catch (Exception e) { } } - }, WemqAccessCommon.HEATBEAT, WemqAccessCommon.HEATBEAT, TimeUnit.MILLISECONDS); + }, EventMeshCommon.HEATBEAT, EventMeshCommon.HEATBEAT, TimeUnit.MILLISECONDS); } private void goodbye() throws Exception { Package msg = MessageUtils.goodbye(); - this.io(msg, WemqAccessCommon.DEFAULT_TIME_OUT_MILLS); + this.io(msg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); } private void hello() throws Exception { Package msg = MessageUtils.hello(userAgent); - this.io(msg, WemqAccessCommon.DEFAULT_TIME_OUT_MILLS); + this.io(msg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); } public void listen() throws Exception { Package request = MessageUtils.listen(); - this.io(request, WemqAccessCommon.DEFAULT_TIME_OUT_MILLS); + this.io(request, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); } - public void subscribe(String topic) throws Exception { - topics.add(topic); - Package request = MessageUtils.subscribe(topic); - this.io(request, WemqAccessCommon.DEFAULT_TIME_OUT_MILLS); + public void subscribe(String topic, SubscriptionMode subscriptionMode, SubcriptionType subcriptionType) throws Exception { + subscriptionItems.add(new SubscriptionItem(topic, subscriptionMode, subcriptionType)); + Package request = MessageUtils.subscribe(topic, subscriptionMode, subcriptionType); + this.io(request, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); } public void unsubscribe() throws Exception { Package request = MessageUtils.unsubscribe(); - this.io(request, WemqAccessCommon.DEFAULT_TIME_OUT_MILLS); + this.io(request, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); } public UserAgent getUserAgent() { diff --git a/eventmesh-sdk-java/src/main/resources/log4j2.xml b/eventmesh-sdk-java/src/main/resources/log4j2.xml index e3f037b9cb..90f56982b0 100644 --- a/eventmesh-sdk-java/src/main/resources/log4j2.xml +++ b/eventmesh-sdk-java/src/main/resources/log4j2.xml @@ -28,7 +28,7 @@ - + diff --git a/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/http/demo/AsyncPublishInstance.java b/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/http/demo/AsyncPublishInstance.java deleted file mode 100644 index ded8b24ed5..0000000000 --- a/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/http/demo/AsyncPublishInstance.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.webank.eventmesh.client.http.demo; -import com.webank.eventmesh.client.http.conf.LiteClientConfig; -import com.webank.eventmesh.client.http.producer.LiteProducer; -import com.webank.eventmesh.common.Constants; -import com.webank.eventmesh.common.IPUtil; -import com.webank.eventmesh.common.LiteMessage; -import com.webank.eventmesh.common.ThreadUtil; -import org.apache.commons.lang3.RandomStringUtils; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class AsyncPublishInstance { - - public static Logger logger = LoggerFactory.getLogger(AsyncPublishInstance.class); - - public static void main(String[] args) throws Exception { - - LiteProducer liteProducer = null; - try{ - String proxyIPPort = args[0]; - - String topic = args[1]; - - if (StringUtils.isBlank(proxyIPPort)) { - // if has multi value, can config as: 127.0.0.1:10105;127.0.0.2:10105 - proxyIPPort = "127.0.0.1:10105"; - } - - LiteClientConfig weMQProxyClientConfig = new LiteClientConfig(); - weMQProxyClientConfig.setLiteProxyAddr(proxyIPPort) - .setEnv("env") - .setIdc("idc") - .setDcn("dcn") - .setIp(IPUtil.getLocalAddress()) - .setSys("1234") - .setPid(String.valueOf(ThreadUtil.getPID())); - - liteProducer = new LiteProducer(weMQProxyClientConfig); - liteProducer.start(); - - LiteMessage liteMessage = new LiteMessage(); - liteMessage.setBizSeqNo(RandomStringUtils.randomNumeric(30)) - .setContent("contentStr with special protocal") - .setTopic(topic) - .setUniqueId(RandomStringUtils.randomNumeric(30)) - .addProp(Constants.PROXY_MESSAGE_CONST_TTL, String.valueOf(4 * 3600 * 1000)); - - boolean flag = liteProducer.publish(liteMessage); - Thread.sleep(1000); - logger.info("publish result , {}", flag); - }catch (Exception e){ - logger.warn("publish msg failed", e); - } - - try{ - Thread.sleep(30000); - if(liteProducer != null){ - liteProducer.shutdown(); - } - }catch (Exception e1){ - logger.warn("producer shutdown exception", e1); - } - } -} diff --git a/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/http/demo/AsyncSyncRequestInstance.java b/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/http/demo/AsyncSyncRequestInstance.java deleted file mode 100644 index ff201bbc68..0000000000 --- a/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/http/demo/AsyncSyncRequestInstance.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.webank.eventmesh.client.http.demo; -import com.webank.eventmesh.client.http.conf.LiteClientConfig; -import com.webank.eventmesh.client.http.producer.LiteProducer; -import com.webank.eventmesh.client.http.producer.RRCallback; -import com.webank.eventmesh.common.IPUtil; -import com.webank.eventmesh.common.LiteMessage; -import com.webank.eventmesh.common.ThreadUtil; -import org.apache.commons.lang3.RandomStringUtils; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class AsyncSyncRequestInstance { - - public static Logger logger = LoggerFactory.getLogger(SyncRequestInstance.class); - - public static void main(String[] args) throws Exception { - - LiteProducer liteProducer = null; - try { - String proxyIPPort = args[0]; - - final String topic = args[1]; - - if (StringUtils.isBlank(proxyIPPort)) { - // if has multi value, can config as: 127.0.0.1:10105;127.0.0.2:10105 - proxyIPPort = "127.0.0.1:10105"; - } - - LiteClientConfig weMQProxyClientConfig = new LiteClientConfig(); - weMQProxyClientConfig.setLiteProxyAddr(proxyIPPort) - .setEnv("env") - .setIdc("idc") - .setDcn("dcn") - .setIp(IPUtil.getLocalAddress()) - .setSys("1234") - .setPid(String.valueOf(ThreadUtil.getPID())); - - liteProducer = new LiteProducer(weMQProxyClientConfig); - - final long startTime = System.currentTimeMillis(); - final LiteMessage liteMessage = new LiteMessage(); - liteMessage.setBizSeqNo(RandomStringUtils.randomNumeric(30)) - .setContent("contentStr with special protocal") - .setTopic(topic) - .setUniqueId(RandomStringUtils.randomNumeric(30)); - - liteProducer.request(liteMessage, new RRCallback() { - @Override - public void onSuccess(LiteMessage o) { - logger.debug("sendmsg : {}, return : {}, cost:{}ms", liteMessage.getContent(), System.currentTimeMillis() - startTime); - } - - @Override - public void onException(Throwable e) { - logger.debug("sendmsg failed", e); - } - }, 3000); - - Thread.sleep(2000); - } catch (Exception e) { - logger.warn("async send msg failed", e); - } - - try{ - Thread.sleep(30000); - if(liteProducer != null){ - liteProducer.shutdown(); - } - }catch (Exception e1){ - logger.warn("producer shutdown exception", e1); - } - } -} diff --git a/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/tcp/common/AccessTestCaseTopicSet.java b/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/tcp/common/AccessTestCaseTopicSet.java deleted file mode 100644 index bc1cc66fe1..0000000000 --- a/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/tcp/common/AccessTestCaseTopicSet.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.webank.eventmesh.client.tcp.common; - -/** - * Testcase set - */ -public class AccessTestCaseTopicSet { - - public static final String TOPIC_PRX_WQ2ClientBroadCast = "topic-broadcast-test"; - - public static final String TOPIC_PRX_SyncSubscribeTest = "topic-sync-test"; - - public static final String TOPIC_PRX_WQ2ClientUniCast = "topic-async-test"; - -} diff --git a/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/tcp/common/AccessTestUtils.java b/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/tcp/common/AccessTestUtils.java deleted file mode 100644 index 4bee96adcb..0000000000 --- a/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/tcp/common/AccessTestUtils.java +++ /dev/null @@ -1,130 +0,0 @@ -package com.webank.eventmesh.client.tcp.common; - -import com.webank.eventmesh.common.protocol.tcp.AccessMessage; -import com.webank.eventmesh.common.protocol.tcp.Command; -import com.webank.eventmesh.common.protocol.tcp.Header; -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; - -import java.util.concurrent.ThreadLocalRandom; - -import static com.webank.eventmesh.client.tcp.common.AccessTestCaseTopicSet.*; -import static com.webank.eventmesh.common.protocol.tcp.Command.RESPONSE_TO_SERVER; - -public class AccessTestUtils { - private static final int seqLength = 10; - - public static UserAgent generateClient1() { - UserAgent user = new UserAgent(); - user.setDcn("AC0"); - user.setHost("127.0.0.1"); - user.setPassword(generateRandomString(8)); - user.setUsername("PU4283"); - user.setPath("/data/app/umg_proxy"); - user.setPort(8362); - user.setSubsystem("5023"); - user.setPid(32893); - user.setVersion("2.0.11"); - user.setIdc("FT"); - return user; - } - - public static UserAgent generateClient2() { - UserAgent user = new UserAgent(); - user.setDcn("AC0"); - user.setHost("127.0.0.1"); - user.setPassword(generateRandomString(8)); - user.setUsername("PU4283"); - user.setPath("/data/app/umg_proxy"); - user.setPort(9362); - user.setSubsystem("5017"); - user.setPid(42893); - user.setVersion("2.0.11"); - user.setIdc("FT"); - return user; - } - - public static Package syncRR() { - Package msg = new Package(); - msg.setHeader(new Header(Command.REQUEST_TO_SERVER, 0, null, generateRandomString(seqLength))); - msg.setBody(generateSyncRRWemqMsg()); - return msg; - } - - public static Package asyncRR() { - Package msg = new Package(); - msg.setHeader(new Header(Command.REQUEST_TO_SERVER, 0, null, generateRandomString(seqLength))); - msg.setBody(generateAsyncRRWemqMsg()); - return msg; - } - - public static Package asyncMessage() { - Package msg = new Package(); - msg.setHeader(new Header(Command.ASYNC_MESSAGE_TO_SERVER, 0, null, generateRandomString(seqLength))); - msg.setBody(generateAsyncEventWemqMsg()); - return msg; - } - - public static Package broadcastMessage() { - Package msg = new Package(); - msg.setHeader(new Header(Command.BROADCAST_MESSAGE_TO_SERVER, 0, null, generateRandomString(seqLength))); - msg.setBody(generateBroadcastWemqMsg()); - return msg; - } - - public static Package rrResponse(Package request) { - Package msg = new Package(); - msg.setHeader(new Header(RESPONSE_TO_SERVER, 0, null, generateRandomString(seqLength))); - msg.setBody(request.getBody()); - return msg; - } - - private static AccessMessage generateSyncRRWemqMsg() { - AccessMessage wemqMsg = new AccessMessage(); - wemqMsg.setTopic(TOPIC_PRX_SyncSubscribeTest); - wemqMsg.getProperties().put("msgType", "persistent"); - wemqMsg.getProperties().put("TTL", "300000"); - wemqMsg.getProperties().put("KEYS", generateRandomString(16)); - wemqMsg.setBody("testSyncRR"); - return wemqMsg; - } - - - private static AccessMessage generateAsyncRRWemqMsg() { - AccessMessage wemqMsg = new AccessMessage(); - wemqMsg.setTopic(TOPIC_PRX_SyncSubscribeTest); - wemqMsg.getProperties().put("REPLY_TO", "10.36.0.109@ProducerGroup-producerPool-9-access#V1_4_0#CI"); - wemqMsg.getProperties().put("TTL", "300000"); - wemqMsg.getProperties().put("PROPERTY_MESSAGE_REPLY_TO", "notnull"); - wemqMsg.setBody("testAsyncRR"); - return wemqMsg; - } - - private static AccessMessage generateAsyncEventWemqMsg() { - AccessMessage wemqMsg = new AccessMessage(); - wemqMsg.setTopic(TOPIC_PRX_WQ2ClientUniCast); - wemqMsg.getProperties().put("REPLY_TO", "10.36.0.109@ProducerGroup-producerPool-9-access#V1_4_0#CI"); - wemqMsg.getProperties().put("TTL", "30000"); - wemqMsg.getProperties().put("PROPERTY_MESSAGE_REPLY_TO", "notnull"); - wemqMsg.setBody("testAsyncMessage"); - return wemqMsg; - } - - private static AccessMessage generateBroadcastWemqMsg() { - AccessMessage wemqMsg = new AccessMessage(); - wemqMsg.setTopic(TOPIC_PRX_WQ2ClientBroadCast); - wemqMsg.getProperties().put("REPLY_TO", "10.36.0.109@ProducerGroup-producerPool-9-access#V1_4_0#CI"); - wemqMsg.getProperties().put("TTL", "30000"); - wemqMsg.getProperties().put("PROPERTY_MESSAGE_REPLY_TO", "notnull"); - wemqMsg.setBody("testAsyncMessage"); - return wemqMsg; - } - - private static String generateRandomString(int length) { - StringBuilder builder = new StringBuilder(length); - for (int i = 0; i < length; i++) { - builder.append((char) ThreadLocalRandom.current().nextInt(48, 57)); - } - return builder.toString(); - } -} diff --git a/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/tcp/demo/AsyncPublish.java b/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/tcp/demo/AsyncPublish.java deleted file mode 100644 index 0e8da98386..0000000000 --- a/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/tcp/demo/AsyncPublish.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.webank.eventmesh.client.tcp.demo; - -import com.webank.eventmesh.client.tcp.WemqAccessClient; -import com.webank.eventmesh.client.tcp.common.AccessTestUtils; -import com.webank.eventmesh.client.tcp.common.WemqAccessCommon; -import com.webank.eventmesh.client.tcp.impl.DefaultWemqAccessClient; -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class AsyncPublish{ - - public static Logger logger = LoggerFactory.getLogger(AsyncPublish.class); - - private static WemqAccessClient client; - - public static AsyncPublish handler = new AsyncPublish(); - - public static void main(String[] agrs)throws Exception{ - try{ - UserAgent userAgent = AccessTestUtils.generateClient1(); - client = new DefaultWemqAccessClient("127.0.0.1",10000,userAgent); - client.init(); - client.heartbeat(); - - Package asyncMsg = AccessTestUtils.asyncMessage(); - logger.info("开始发送异步单播消息:{}", asyncMsg); - client.publish(asyncMsg, WemqAccessCommon.DEFAULT_TIME_OUT_MILLS); - - Thread.sleep(2000); - //退出,销毁资源 -// client.close(); - }catch (Exception e){ - logger.warn("AsyncPublish failed", e); - } - } -} diff --git a/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/tcp/demo/AsyncPublishBroadcast.java b/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/tcp/demo/AsyncPublishBroadcast.java deleted file mode 100644 index 57dc0b6a5d..0000000000 --- a/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/tcp/demo/AsyncPublishBroadcast.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.webank.eventmesh.client.tcp.demo; - -import com.webank.eventmesh.client.tcp.WemqAccessClient; -import com.webank.eventmesh.client.tcp.common.AccessTestUtils; -import com.webank.eventmesh.client.tcp.common.WemqAccessCommon; -import com.webank.eventmesh.client.tcp.impl.DefaultWemqAccessClient; -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class AsyncPublishBroadcast { - - public static Logger logger = LoggerFactory.getLogger(AsyncPublishBroadcast.class); - - private static WemqAccessClient client; - - public static void main(String[] agrs)throws Exception{ - try{ - UserAgent userAgent = AccessTestUtils.generateClient1(); - client = new DefaultWemqAccessClient("127.0.0.1",10000,userAgent); - client.init(); - client.heartbeat(); - - Package broadcastMsg = AccessTestUtils.broadcastMessage(); - logger.info("开始发送广播消息:{}", broadcastMsg); - client.broadcast(broadcastMsg, WemqAccessCommon.DEFAULT_TIME_OUT_MILLS); - - Thread.sleep(2000); - //退出,销毁资源 -// client.close(); - }catch (Exception e){ - logger.warn("AsyncPublishBroadcast failed", e); - } - } -} diff --git a/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/tcp/demo/AsyncSubscribe.java b/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/tcp/demo/AsyncSubscribe.java deleted file mode 100644 index c910150497..0000000000 --- a/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/tcp/demo/AsyncSubscribe.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.webank.eventmesh.client.tcp.demo; - -import com.webank.eventmesh.client.tcp.WemqAccessClient; -import com.webank.eventmesh.client.tcp.common.AccessTestUtils; -import com.webank.eventmesh.client.tcp.common.ReceiveMsgHook; -import com.webank.eventmesh.client.tcp.impl.DefaultWemqAccessClient; -import com.webank.eventmesh.common.protocol.tcp.AccessMessage; -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; -import io.netty.channel.ChannelHandlerContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class AsyncSubscribe implements ReceiveMsgHook { - - public static Logger logger = LoggerFactory.getLogger(AsyncSubscribe.class); - - private static WemqAccessClient client; - - public static AsyncSubscribe handler = new AsyncSubscribe(); - - public static void main(String[] agrs)throws Exception{ - try{ - UserAgent userAgent = AccessTestUtils.generateClient2(); - client = new DefaultWemqAccessClient("127.0.0.1",10000,userAgent); - client.init(); - client.heartbeat(); - - client.subscribe("FT0-e-80010000-01-1"); - client.registerSubBusiHandler(handler); - - client.listen(); - - //client.unsubscribe(); - - //退出,销毁资源 -// client.close(); - }catch (Exception e){ - logger.warn("AsyncSubscribe failed", e); - } - } - - @Override - public void handle(Package msg, ChannelHandlerContext ctx) { - AccessMessage accessMessage = (AccessMessage)msg.getBody(); - logger.info("sub收到异步单播消息:{}", accessMessage); - } -} diff --git a/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/tcp/demo/AsyncSubscribeBroadcast.java b/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/tcp/demo/AsyncSubscribeBroadcast.java deleted file mode 100644 index 90614a70b7..0000000000 --- a/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/tcp/demo/AsyncSubscribeBroadcast.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.webank.eventmesh.client.tcp.demo; - -import com.webank.eventmesh.client.tcp.WemqAccessClient; -import com.webank.eventmesh.client.tcp.common.AccessTestUtils; -import com.webank.eventmesh.client.tcp.common.ReceiveMsgHook; -import com.webank.eventmesh.client.tcp.impl.DefaultWemqAccessClient; -import com.webank.eventmesh.common.protocol.tcp.AccessMessage; -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; -import io.netty.channel.ChannelHandlerContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class AsyncSubscribeBroadcast implements ReceiveMsgHook { - - public static Logger logger = LoggerFactory.getLogger(AsyncSubscribeBroadcast.class); - - private static WemqAccessClient client; - - public static AsyncSubscribeBroadcast handler = new AsyncSubscribeBroadcast(); - - public static void main(String[] agrs)throws Exception{ - try{ - UserAgent userAgent = AccessTestUtils.generateClient2(); - client = new DefaultWemqAccessClient("127.0.0.1",10000,userAgent); - client.init(); - client.heartbeat(); - - client.subscribe("FT0-e-80030000-01-3"); - client.registerSubBusiHandler(handler); - - client.listen(); - - //client.unsubscribe(); - - //退出,销毁资源 -// client.close(); - }catch (Exception e){ - logger.warn("AsyncSubscribeBroadcast failed", e); - } - } - - @Override - public void handle(Package msg, ChannelHandlerContext ctx) { - AccessMessage accessMessage = (AccessMessage)msg.getBody(); - logger.info("sub收到广播消息:{}", accessMessage); - } -} diff --git a/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/tcp/demo/SyncRequest.java b/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/tcp/demo/SyncRequest.java deleted file mode 100644 index a4ff5ed713..0000000000 --- a/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/tcp/demo/SyncRequest.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.webank.eventmesh.client.tcp.demo; - -import com.webank.eventmesh.client.tcp.WemqAccessClient; -import com.webank.eventmesh.client.tcp.common.AccessTestUtils; -import com.webank.eventmesh.client.tcp.common.WemqAccessCommon; -import com.webank.eventmesh.client.tcp.impl.DefaultWemqAccessClient; -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class SyncRequest { - - public static Logger logger = LoggerFactory.getLogger(SyncRequest.class); - - private static WemqAccessClient client; - - public static void main(String[] agrs)throws Exception{ - try{ - UserAgent userAgent = AccessTestUtils.generateClient1(); - client = new DefaultWemqAccessClient("127.0.0.1",10000,userAgent); - client.init(); - client.heartbeat(); - - Package rrMsg = AccessTestUtils.syncRR(); - logger.info("开始发送同步RR消息:{}",rrMsg); - Package response = client.rr(rrMsg, WemqAccessCommon.DEFAULT_TIME_OUT_MILLS); - logger.info("收到同步RR回包:{}",response); - - //退出,销毁资源 -// client.close(); - }catch (Exception e){ - logger.warn("SyncRequest failed", e); - } - } -} diff --git a/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/tcp/demo/SyncResponse.java b/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/tcp/demo/SyncResponse.java deleted file mode 100644 index 962d10c2df..0000000000 --- a/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/tcp/demo/SyncResponse.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.webank.eventmesh.client.tcp.demo; - -import com.webank.eventmesh.client.tcp.WemqAccessClient; -import com.webank.eventmesh.client.tcp.common.AccessTestUtils; -import com.webank.eventmesh.client.tcp.common.ReceiveMsgHook; -import com.webank.eventmesh.client.tcp.impl.DefaultWemqAccessClient; -import com.webank.eventmesh.common.protocol.tcp.Package; -import com.webank.eventmesh.common.protocol.tcp.UserAgent; -import io.netty.channel.ChannelHandlerContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class SyncResponse implements ReceiveMsgHook { - - public static Logger logger = LoggerFactory.getLogger(SyncResponse.class); - - private static WemqAccessClient client; - - public static SyncResponse handler = new SyncResponse(); - - public static void main(String[] agrs)throws Exception{ - try{ - UserAgent userAgent = AccessTestUtils.generateClient2(); - client = new DefaultWemqAccessClient("127.0.0.1",10000,userAgent); - client.init(); - client.heartbeat(); - - client.subscribe("FT0-s-80000000-01-0"); - //同步RR消息 - client.registerSubBusiHandler(handler); - - client.listen(); - - //client.unsubscribe(); - - //退出,销毁资源 -// client.close(); - }catch (Exception e){ - logger.warn("SyncResponse failed", e); - } - } - - @Override - public void handle(Package msg, ChannelHandlerContext ctx) { - logger.info("sub收到消息:{}", msg); - Package pkg = AccessTestUtils.rrResponse(msg); - ctx.writeAndFlush(pkg); - } -} diff --git a/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/http/demo/AsyncPublishInstance.java b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/http/demo/AsyncPublishInstance.java new file mode 100644 index 0000000000..e1598d8958 --- /dev/null +++ b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/http/demo/AsyncPublishInstance.java @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.client.http.demo; + +import org.apache.commons.lang3.RandomStringUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.client.http.conf.LiteClientConfig; +import org.apache.eventmesh.client.http.producer.LiteProducer; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.IPUtil; +import org.apache.eventmesh.common.LiteMessage; +import org.apache.eventmesh.common.ThreadUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AsyncPublishInstance { + + public static Logger logger = LoggerFactory.getLogger(AsyncPublishInstance.class); + + public static void main(String[] args) throws Exception { + + LiteProducer liteProducer = null; + try { +// String eventMeshIPPort = args[0]; + String eventMeshIPPort = ""; +// final String topic = args[1]; + final String topic = "TEST-TOPIC-HTTP-ASYNC"; + if (StringUtils.isBlank(eventMeshIPPort)) { + // if has multi value, can config as: 127.0.0.1:10105;127.0.0.2:10105 + eventMeshIPPort = "127.0.0.1:10105"; + } + + LiteClientConfig eventMeshClientConfig = new LiteClientConfig(); + eventMeshClientConfig.setLiteEventMeshAddr(eventMeshIPPort) + .setProducerGroup("EventMeshTest-producerGroup") + .setEnv("env") + .setIdc("idc") + .setIp(IPUtil.getLocalAddress()) + .setSys("1234") + .setPid(String.valueOf(ThreadUtil.getPID())); + + liteProducer = new LiteProducer(eventMeshClientConfig); + liteProducer.start(); + for (int i = 0; i < 1; i++) { + LiteMessage liteMessage = new LiteMessage(); + liteMessage.setBizSeqNo(RandomStringUtils.randomNumeric(30)) +// .setContent("contentStr with special protocal") + .setContent("testPublishMessage") + .setTopic(topic) + .setUniqueId(RandomStringUtils.randomNumeric(30)) + .addProp(Constants.EVENTMESH_MESSAGE_CONST_TTL, String.valueOf(4 * 1000)); + + boolean flag = liteProducer.publish(liteMessage); + Thread.sleep(1000); + logger.info("publish result , {}", flag); + } + } catch (Exception e) { + logger.warn("publish msg failed", e); + } + + try { + Thread.sleep(30000); + if (liteProducer != null) { + liteProducer.shutdown(); + } + } catch (Exception e1) { + logger.warn("producer shutdown exception", e1); + } + } +} diff --git a/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/http/demo/AsyncSyncRequestInstance.java b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/http/demo/AsyncSyncRequestInstance.java new file mode 100644 index 0000000000..596bf95645 --- /dev/null +++ b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/http/demo/AsyncSyncRequestInstance.java @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.client.http.demo; + + +import org.apache.commons.lang3.RandomStringUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.client.http.conf.LiteClientConfig; +import org.apache.eventmesh.client.http.producer.LiteProducer; +import org.apache.eventmesh.client.http.producer.RRCallback; +import org.apache.eventmesh.common.IPUtil; +import org.apache.eventmesh.common.LiteMessage; +import org.apache.eventmesh.common.ThreadUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AsyncSyncRequestInstance { + + public static Logger logger = LoggerFactory.getLogger(AsyncSyncRequestInstance.class); + + public static void main(String[] args) throws Exception { + + LiteProducer liteProducer = null; + try { +// String eventMeshIPPort = args[0]; + String eventMeshIPPort = ""; +// final String topic = args[1]; + final String topic = "TEST-TOPIC-HTTP-ASYNC"; + if (StringUtils.isBlank(eventMeshIPPort)) { + // if has multi value, can config as: 127.0.0.1:10105;127.0.0.2:10105 + eventMeshIPPort = "127.0.0.1:10105"; + } + + LiteClientConfig eventMeshClientConfig = new LiteClientConfig(); + eventMeshClientConfig.setLiteEventMeshAddr(eventMeshIPPort) + .setProducerGroup("EventMeshTest-producerGroup") + .setEnv("env") + .setIdc("idc") + .setIp(IPUtil.getLocalAddress()) + .setSys("1234") + .setPid(String.valueOf(ThreadUtil.getPID())); + + liteProducer = new LiteProducer(eventMeshClientConfig); + + final long startTime = System.currentTimeMillis(); + final LiteMessage liteMessage = new LiteMessage(); + liteMessage.setBizSeqNo(RandomStringUtils.randomNumeric(30)) + .setContent("testAsyncMessage") + .setTopic(topic) + .setUniqueId(RandomStringUtils.randomNumeric(30)); + + liteProducer.request(liteMessage, new RRCallback() { + @Override + public void onSuccess(LiteMessage o) { + logger.debug("sendmsg : {}, return : {}, cost:{}ms", liteMessage.getContent(), o.getContent(), System.currentTimeMillis() - startTime); + } + + @Override + public void onException(Throwable e) { + logger.debug("sendmsg failed", e); + } + }, 3000); + + Thread.sleep(2000); + } catch (Exception e) { + logger.warn("async send msg failed", e); + } + + try { + Thread.sleep(30000); + if (liteProducer != null) { + liteProducer.shutdown(); + } + } catch (Exception e1) { + logger.warn("producer shutdown exception", e1); + } + } +} diff --git a/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/http/demo/SyncRequestInstance.java b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/http/demo/SyncRequestInstance.java new file mode 100644 index 0000000000..b62d2948aa --- /dev/null +++ b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/http/demo/SyncRequestInstance.java @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.client.http.demo; + +import org.apache.commons.lang3.RandomStringUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.client.http.conf.LiteClientConfig; +import org.apache.eventmesh.client.http.producer.LiteProducer; +import org.apache.eventmesh.common.IPUtil; +import org.apache.eventmesh.common.LiteMessage; +import org.apache.eventmesh.common.ThreadUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SyncRequestInstance { + + public static Logger logger = LoggerFactory.getLogger(SyncRequestInstance.class); + + public static void main(String[] args) throws Exception { + + LiteProducer liteProducer = null; + try { + String eventMeshIPPort = args[0]; + + final String topic = args[1]; + + if (StringUtils.isBlank(eventMeshIPPort)) { + // if has multi value, can config as: 127.0.0.1:10105;127.0.0.2:10105 + eventMeshIPPort = "127.0.0.1:10105"; + } + + LiteClientConfig eventMeshClientConfig = new LiteClientConfig(); + eventMeshClientConfig.setLiteEventMeshAddr(eventMeshIPPort) + .setProducerGroup("EventMeshTest-producerGroup") + .setEnv("env") + .setIdc("idc") + .setIp(IPUtil.getLocalAddress()) + .setSys("1234") + .setPid(String.valueOf(ThreadUtil.getPID())); + + liteProducer = new LiteProducer(eventMeshClientConfig); + liteProducer.start(); + + long startTime = System.currentTimeMillis(); + LiteMessage liteMessage = new LiteMessage(); + liteMessage.setBizSeqNo(RandomStringUtils.randomNumeric(30)) + .setContent("contentStr with special protocal") + .setTopic(topic) + .setUniqueId(RandomStringUtils.randomNumeric(30)); + + LiteMessage rsp = liteProducer.request(liteMessage, 10000); + if (logger.isDebugEnabled()) { + logger.debug("sendmsg : {}, return : {}, cost:{}ms", liteMessage.getContent(), rsp.getContent(), System.currentTimeMillis() - startTime); + } + } catch (Exception e) { + logger.warn("send msg failed", e); + } + + try { + Thread.sleep(30000); + if (liteProducer != null) { + liteProducer.shutdown(); + } + } catch (Exception e1) { + logger.warn("producer shutdown exception", e1); + } + } +} diff --git a/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/http/util/HttpLoadBalanceUtilsTest.java b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/http/util/HttpLoadBalanceUtilsTest.java new file mode 100644 index 0000000000..d393c24a4c --- /dev/null +++ b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/http/util/HttpLoadBalanceUtilsTest.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.client.http.util; + +import org.apache.eventmesh.client.http.conf.LiteClientConfig; +import org.apache.eventmesh.common.EventMeshException; +import org.apache.eventmesh.common.loadbalance.LoadBalanceSelector; +import org.apache.eventmesh.common.loadbalance.LoadBalanceType; +import org.junit.Assert; +import org.junit.Test; + +public class HttpLoadBalanceUtilsTest { + + @Test + public void testCreateRandomSelector() throws EventMeshException { + LiteClientConfig liteClientConfig = new LiteClientConfig() + .setLiteEventMeshAddr("127.0.0.1:1001;127.0.0.2:1002"); + LoadBalanceSelector randomSelector = HttpLoadBalanceUtils + .createEventMeshServerLoadBalanceSelector(liteClientConfig); + Assert.assertEquals(LoadBalanceType.RANDOM, randomSelector.getType()); + } + + @Test + public void testCreateWeightRoundRobinSelector() throws EventMeshException { + LiteClientConfig liteClientConfig = new LiteClientConfig() + .setLiteEventMeshAddr("127.0.0.1:1001:1;127.0.0.2:1001:2") + .setLoadBalanceType(LoadBalanceType.WEIGHT_ROUND_ROBIN); + LoadBalanceSelector weightRoundRobinSelector = HttpLoadBalanceUtils + .createEventMeshServerLoadBalanceSelector(liteClientConfig); + Assert.assertEquals(LoadBalanceType.WEIGHT_ROUND_ROBIN, weightRoundRobinSelector.getType()); + } +} \ No newline at end of file diff --git a/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/common/EventMeshTestCaseTopicSet.java b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/common/EventMeshTestCaseTopicSet.java new file mode 100644 index 0000000000..669e598a8c --- /dev/null +++ b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/common/EventMeshTestCaseTopicSet.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.client.tcp.common; + +/** + * Testcase set + */ +public class EventMeshTestCaseTopicSet { + + // public static final String TOPIC_PRX_WQ2ClientBroadCast = "topic-broadcast-test"; + public static final String TOPIC_PRX_WQ2ClientBroadCast = "TEST-TOPIC-TCP-BROADCAST"; + + // public static final String TOPIC_PRX_SyncSubscribeTest = "topic-sync-test"; + public static final String TOPIC_PRX_SyncSubscribeTest = "TEST-TOPIC-TCP-SYNC"; + + // public static final String TOPIC_PRX_WQ2ClientUniCast = "topic-async-test"; + public static final String TOPIC_PRX_WQ2ClientUniCast = "TEST-TOPIC-TCP-ASYNC"; + +} diff --git a/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/common/EventMeshTestUtils.java b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/common/EventMeshTestUtils.java new file mode 100644 index 0000000000..78e1016756 --- /dev/null +++ b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/common/EventMeshTestUtils.java @@ -0,0 +1,155 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.client.tcp.common; + +import static org.apache.eventmesh.client.tcp.common.EventMeshTestCaseTopicSet.TOPIC_PRX_SyncSubscribeTest; +import static org.apache.eventmesh.client.tcp.common.EventMeshTestCaseTopicSet.TOPIC_PRX_WQ2ClientBroadCast; +import static org.apache.eventmesh.client.tcp.common.EventMeshTestCaseTopicSet.TOPIC_PRX_WQ2ClientUniCast; + +import java.util.concurrent.ThreadLocalRandom; + +import org.apache.eventmesh.common.protocol.tcp.Command; +import org.apache.eventmesh.common.protocol.tcp.EventMeshMessage; +import org.apache.eventmesh.common.protocol.tcp.Header; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import org.apache.eventmesh.common.protocol.tcp.Package; + +import static org.apache.eventmesh.common.protocol.tcp.Command.RESPONSE_TO_SERVER; + + + + +public class EventMeshTestUtils { + private static final int seqLength = 10; + + public static UserAgent generateClient1() { + UserAgent user = new UserAgent(); + user.setHost("127.0.0.1"); + user.setPassword(generateRandomString(8)); + user.setUsername("PU4283"); + user.setConsumerGroup("EventmeshTest-ConsumerGroup"); + user.setProducerGroup("EventmeshTest-ProducerGroup"); + user.setPath("/data/app/umg_proxy"); + user.setPort(8362); + user.setSubsystem("5023"); + user.setPid(32893); + user.setVersion("2.0.11"); + user.setIdc("FT"); + return user; + } + + public static UserAgent generateClient2() { + UserAgent user = new UserAgent(); + user.setHost("127.0.0.1"); + user.setPassword(generateRandomString(8)); + user.setUsername("PU4283"); + user.setConsumerGroup("EventmeshTest-ConsumerGroup"); + user.setProducerGroup("EventmeshTest-ProducerGroup"); + user.setPath("/data/app/umg_proxy"); + user.setPort(9362); + user.setSubsystem("5017"); + user.setPid(42893); + user.setVersion("2.0.11"); + user.setIdc("FT"); + return user; + } + + public static Package syncRR() { + Package msg = new Package(); + msg.setHeader(new Header(Command.REQUEST_TO_SERVER, 0, null, generateRandomString(seqLength))); + msg.setBody(generateSyncRRMqMsg()); + return msg; + } + + public static Package asyncRR() { + Package msg = new Package(); + msg.setHeader(new Header(Command.REQUEST_TO_SERVER, 0, null, generateRandomString(seqLength))); + msg.setBody(generateAsyncRRMqMsg()); + return msg; + } + + public static Package asyncMessage() { + Package msg = new Package(); + msg.setHeader(new Header(Command.ASYNC_MESSAGE_TO_SERVER, 0, null, generateRandomString(seqLength))); + msg.setBody(generateAsyncEventMqMsg()); + return msg; + } + + public static Package broadcastMessage() { + Package msg = new Package(); + msg.setHeader(new Header(Command.BROADCAST_MESSAGE_TO_SERVER, 0, null, generateRandomString(seqLength))); + msg.setBody(generateBroadcastMqMsg()); + return msg; + } + + public static Package rrResponse(Package request) { + Package msg = new Package(); + msg.setHeader(new Header(RESPONSE_TO_SERVER, 0, null, generateRandomString(seqLength))); + msg.setBody(request.getBody()); + return msg; + } + + private static EventMeshMessage generateSyncRRMqMsg() { + EventMeshMessage mqMsg = new EventMeshMessage(); + mqMsg.setTopic(TOPIC_PRX_SyncSubscribeTest); + mqMsg.getProperties().put("msgType", "persistent"); + mqMsg.getProperties().put("TTL", "300000"); + mqMsg.getProperties().put("KEYS", generateRandomString(16)); + mqMsg.setBody("testSyncRR"); + return mqMsg; + } + + + private static EventMeshMessage generateAsyncRRMqMsg() { + EventMeshMessage mqMsg = new EventMeshMessage(); + mqMsg.setTopic(TOPIC_PRX_SyncSubscribeTest); + mqMsg.getProperties().put("REPLY_TO", "10.36.0.109@ProducerGroup-producerPool-9-access#V1_4_0#CI"); + mqMsg.getProperties().put("TTL", "300000"); + mqMsg.getProperties().put("PROPERTY_MESSAGE_REPLY_TO", "notnull"); + mqMsg.setBody("testAsyncRR"); + return mqMsg; + } + + private static EventMeshMessage generateAsyncEventMqMsg() { + EventMeshMessage mqMsg = new EventMeshMessage(); + mqMsg.setTopic(TOPIC_PRX_WQ2ClientUniCast); + mqMsg.getProperties().put("REPLY_TO", "10.36.0.109@ProducerGroup-producerPool-9-access#V1_4_0#CI"); + mqMsg.getProperties().put("TTL", "30000"); + mqMsg.getProperties().put("PROPERTY_MESSAGE_REPLY_TO", "notnull"); + mqMsg.setBody("testAsyncMessage"); + return mqMsg; + } + + private static EventMeshMessage generateBroadcastMqMsg() { + EventMeshMessage mqMsg = new EventMeshMessage(); + mqMsg.setTopic(TOPIC_PRX_WQ2ClientBroadCast); + mqMsg.getProperties().put("REPLY_TO", "10.36.0.109@ProducerGroup-producerPool-9-access#V1_4_0#CI"); + mqMsg.getProperties().put("TTL", "30000"); + mqMsg.getProperties().put("PROPERTY_MESSAGE_REPLY_TO", "notnull"); + mqMsg.setBody("testAsyncMessage"); + return mqMsg; + } + + private static String generateRandomString(int length) { + StringBuilder builder = new StringBuilder(length); + for (int i = 0; i < length; i++) { + builder.append((char) ThreadLocalRandom.current().nextInt(48, 57)); + } + return builder.toString(); + } +} diff --git a/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/demo/AsyncPublish.java b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/demo/AsyncPublish.java new file mode 100644 index 0000000000..4768127176 --- /dev/null +++ b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/demo/AsyncPublish.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.client.tcp.demo; + +import org.apache.eventmesh.client.tcp.EventMeshClient; +import org.apache.eventmesh.client.tcp.common.EventMeshCommon; +import org.apache.eventmesh.client.tcp.common.EventMeshTestUtils; +import org.apache.eventmesh.client.tcp.impl.DefaultEventMeshClient; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AsyncPublish { + + public static Logger logger = LoggerFactory.getLogger(AsyncPublish.class); + + private static EventMeshClient client; + + public static AsyncPublish handler = new AsyncPublish(); + + public static void main(String[] agrs) throws Exception { + try { + UserAgent userAgent = EventMeshTestUtils.generateClient1(); + client = new DefaultEventMeshClient("127.0.0.1", 10002, userAgent); + client.init(); + client.heartbeat(); + + for (int i = 0; i < 5; i++) { + Package asyncMsg = EventMeshTestUtils.asyncMessage(); + logger.info("begin send async msg[{}]==================={}", i, asyncMsg); + client.publish(asyncMsg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); + + Thread.sleep(1000); + } + + Thread.sleep(2000); + //退出,销毁资源 +// client.close(); + } catch (Exception e) { + logger.warn("AsyncPublish failed", e); + } + } +} diff --git a/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/demo/AsyncPublishBroadcast.java b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/demo/AsyncPublishBroadcast.java new file mode 100644 index 0000000000..a4d527fc12 --- /dev/null +++ b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/demo/AsyncPublishBroadcast.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.client.tcp.demo; + +import org.apache.eventmesh.client.tcp.EventMeshClient; +import org.apache.eventmesh.client.tcp.common.EventMeshCommon; +import org.apache.eventmesh.client.tcp.common.EventMeshTestUtils; +import org.apache.eventmesh.client.tcp.impl.DefaultEventMeshClient; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AsyncPublishBroadcast { + + public static Logger logger = LoggerFactory.getLogger(AsyncPublishBroadcast.class); + + private static EventMeshClient client; + + public static void main(String[] agrs) throws Exception { + try { + UserAgent userAgent = EventMeshTestUtils.generateClient1(); + client = new DefaultEventMeshClient("127.0.0.1", 10002, userAgent); + client.init(); + client.heartbeat(); + + Package broadcastMsg = EventMeshTestUtils.broadcastMessage(); + logger.info("begin send broadcast msg============={}", broadcastMsg); + client.broadcast(broadcastMsg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); + + Thread.sleep(2000); + //退出,销毁资源 +// client.close(); + } catch (Exception e) { + logger.warn("AsyncPublishBroadcast failed", e); + } + } +} diff --git a/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/demo/AsyncSubscribe.java b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/demo/AsyncSubscribe.java new file mode 100644 index 0000000000..9f0fb0c4b1 --- /dev/null +++ b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/demo/AsyncSubscribe.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.client.tcp.demo; + + +import io.netty.channel.ChannelHandlerContext; + +import org.apache.eventmesh.client.tcp.EventMeshClient; +import org.apache.eventmesh.client.tcp.common.EventMeshTestUtils; +import org.apache.eventmesh.client.tcp.common.ReceiveMsgHook; +import org.apache.eventmesh.client.tcp.impl.DefaultEventMeshClient; +import org.apache.eventmesh.common.protocol.SubcriptionType; +import org.apache.eventmesh.common.protocol.SubscriptionMode; +import org.apache.eventmesh.common.protocol.tcp.EventMeshMessage; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AsyncSubscribe implements ReceiveMsgHook { + + public static Logger logger = LoggerFactory.getLogger(AsyncSubscribe.class); + + private static EventMeshClient client; + + public static AsyncSubscribe handler = new AsyncSubscribe(); + + public static void main(String[] agrs) throws Exception { + try { + UserAgent userAgent = EventMeshTestUtils.generateClient2(); + client = new DefaultEventMeshClient("127.0.0.1", 10002, userAgent); + client.init(); + client.heartbeat(); + + client.subscribe("TEST-TOPIC-TCP-ASYNC", SubscriptionMode.CLUSTERING, SubcriptionType.ASYNC); + client.registerSubBusiHandler(handler); + + client.listen(); + + //client.unsubscribe(); + + //退出,销毁资源 +// client.close(); + } catch (Exception e) { + logger.warn("AsyncSubscribe failed", e); + } + } + + @Override + public void handle(Package msg, ChannelHandlerContext ctx) { + EventMeshMessage eventMeshMessage = (EventMeshMessage) msg.getBody(); + logger.info("receive async msg====================={}", eventMeshMessage); + } +} diff --git a/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/demo/AsyncSubscribeBroadcast.java b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/demo/AsyncSubscribeBroadcast.java new file mode 100644 index 0000000000..4a5e121f10 --- /dev/null +++ b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/demo/AsyncSubscribeBroadcast.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.client.tcp.demo; + + +import io.netty.channel.ChannelHandlerContext; + +import org.apache.eventmesh.client.tcp.EventMeshClient; +import org.apache.eventmesh.client.tcp.common.EventMeshTestUtils; +import org.apache.eventmesh.client.tcp.common.ReceiveMsgHook; +import org.apache.eventmesh.client.tcp.impl.DefaultEventMeshClient; +import org.apache.eventmesh.common.protocol.SubcriptionType; +import org.apache.eventmesh.common.protocol.SubscriptionMode; +import org.apache.eventmesh.common.protocol.tcp.EventMeshMessage; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AsyncSubscribeBroadcast implements ReceiveMsgHook { + + public static Logger logger = LoggerFactory.getLogger(AsyncSubscribeBroadcast.class); + + private static EventMeshClient client; + + public static AsyncSubscribeBroadcast handler = new AsyncSubscribeBroadcast(); + + public static void main(String[] agrs) throws Exception { + try { + UserAgent userAgent = EventMeshTestUtils.generateClient2(); + client = new DefaultEventMeshClient("127.0.0.1", 10002, userAgent); + client.init(); + client.heartbeat(); + + client.subscribe("TEST-TOPIC-TCP-BROADCAST", SubscriptionMode.BROADCASTING, SubcriptionType.ASYNC); + client.registerSubBusiHandler(handler); + + client.listen(); + + //client.unsubscribe(); + + //退出,销毁资源 +// client.close(); + } catch (Exception e) { + logger.warn("AsyncSubscribeBroadcast failed", e); + } + } + + @Override + public void handle(Package msg, ChannelHandlerContext ctx) { + EventMeshMessage eventMeshMessage = (EventMeshMessage) msg.getBody(); + logger.info("receive broadcast msg==============={}", eventMeshMessage); + } +} diff --git a/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/demo/SyncRequest.java b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/demo/SyncRequest.java new file mode 100644 index 0000000000..c4c14f5c0d --- /dev/null +++ b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/demo/SyncRequest.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.client.tcp.demo; + +import org.apache.eventmesh.client.tcp.EventMeshClient; +import org.apache.eventmesh.client.tcp.common.EventMeshCommon; +import org.apache.eventmesh.client.tcp.common.EventMeshTestUtils; +import org.apache.eventmesh.client.tcp.impl.DefaultEventMeshClient; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SyncRequest { + + public static Logger logger = LoggerFactory.getLogger(SyncRequest.class); + + private static EventMeshClient client; + + public static void main(String[] agrs) throws Exception { + try { + UserAgent userAgent = EventMeshTestUtils.generateClient1(); + client = new DefaultEventMeshClient("127.0.0.1", 10000, userAgent); + client.init(); + client.heartbeat(); + + Package rrMsg = EventMeshTestUtils.syncRR(); + logger.info("begin send rr msg=================={}", rrMsg); + Package response = client.rr(rrMsg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); + logger.info("receive rr reply==================={}", response); + + //退出,销毁资源 +// client.close(); + } catch (Exception e) { + logger.warn("SyncRequest failed", e); + } + } +} diff --git a/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/demo/SyncResponse.java b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/demo/SyncResponse.java new file mode 100644 index 0000000000..d4b93a9fcf --- /dev/null +++ b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/demo/SyncResponse.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.client.tcp.demo; + +import io.netty.channel.ChannelHandlerContext; + +import org.apache.eventmesh.client.tcp.EventMeshClient; +import org.apache.eventmesh.client.tcp.common.EventMeshTestUtils; +import org.apache.eventmesh.client.tcp.common.ReceiveMsgHook; +import org.apache.eventmesh.client.tcp.impl.DefaultEventMeshClient; +import org.apache.eventmesh.common.protocol.SubcriptionType; +import org.apache.eventmesh.common.protocol.SubscriptionMode; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SyncResponse implements ReceiveMsgHook { + + public static Logger logger = LoggerFactory.getLogger(SyncResponse.class); + + private static EventMeshClient client; + + public static SyncResponse handler = new SyncResponse(); + + public static void main(String[] agrs) throws Exception { + try { + UserAgent userAgent = EventMeshTestUtils.generateClient2(); + client = new DefaultEventMeshClient("127.0.0.1", 10000, userAgent); + client.init(); + client.heartbeat(); + + client.subscribe("TEST-TOPIC-TCP-SYNC", SubscriptionMode.CLUSTERING, SubcriptionType.SYNC); + //同步RR消息 + client.registerSubBusiHandler(handler); + + client.listen(); + + //client.unsubscribe(); + + //退出,销毁资源 +// client.close(); + } catch (Exception e) { + logger.warn("SyncResponse failed", e); + } + } + + @Override + public void handle(Package msg, ChannelHandlerContext ctx) { + logger.info("receive sync rr msg================{}", msg); + Package pkg = EventMeshTestUtils.rrResponse(msg); + ctx.writeAndFlush(pkg); + } +} diff --git a/eventmesh-sdk-java/src/test/resources/application.properties b/eventmesh-sdk-java/src/test/resources/application.properties new file mode 100644 index 0000000000..8bc9ce9a40 --- /dev/null +++ b/eventmesh-sdk-java/src/test/resources/application.properties @@ -0,0 +1,17 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +server.port=8088 \ No newline at end of file diff --git a/eventmesh-sdk-java/src/test/resources/log4j2.xml b/eventmesh-sdk-java/src/test/resources/log4j2.xml index 99f79985fb..4297021f90 100644 --- a/eventmesh-sdk-java/src/test/resources/log4j2.xml +++ b/eventmesh-sdk-java/src/test/resources/log4j2.xml @@ -28,7 +28,7 @@ - + diff --git a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/UnSubscribeRequestBody.java b/eventmesh-starter/build.gradle similarity index 74% rename from eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/UnSubscribeRequestBody.java rename to eventmesh-starter/build.gradle index c6b6e30571..7b81c6bf08 100644 --- a/eventmesh-common/src/main/java/com/webank/eventmesh/common/protocol/http/body/client/UnSubscribeRequestBody.java +++ b/eventmesh-starter/build.gradle @@ -15,7 +15,8 @@ * limitations under the License. */ -package com.webank.eventmesh.common.protocol.http.body.client; - -public class UnSubscribeRequestBody { -} +dependencies { + implementation project(":eventmesh-runtime"), project(":eventmesh-connector-rocketmq") + testImplementation project(":eventmesh-runtime"), project(":eventmesh-connector-rocketmq") + //testImplementation group: 'junit', name: 'junit', version: '4.12' +} \ No newline at end of file diff --git a/eventmesh-starter/gradle.properties b/eventmesh-starter/gradle.properties new file mode 100644 index 0000000000..6446ff31f4 --- /dev/null +++ b/eventmesh-starter/gradle.properties @@ -0,0 +1,20 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +group=org.apache.eventmesh +version=1.2.0-release +mavenUserName= +mavenPassword= diff --git a/eventmesh-starter/src/main/java/org/apache/eventmesh/starter/StartUp.java b/eventmesh-starter/src/main/java/org/apache/eventmesh/starter/StartUp.java new file mode 100644 index 0000000000..4a2ab9d5ac --- /dev/null +++ b/eventmesh-starter/src/main/java/org/apache/eventmesh/starter/StartUp.java @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.starter; + +import org.apache.eventmesh.runtime.boot.EventMeshStartup; + +public class StartUp { + public static void main(String[] args) throws Exception { + EventMeshStartup.main(args); + } +} diff --git a/eventmesh-test/bin/http_pub.sh b/eventmesh-test/bin/http_pub.sh new file mode 100644 index 0000000000..5c34d0bc31 --- /dev/null +++ b/eventmesh-test/bin/http_pub.sh @@ -0,0 +1,138 @@ +#!/bin/sh +# +# Licensed to Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Apache Software Foundation (ASF) licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +#=========================================================================================== +# Java Environment Setting +#=========================================================================================== +set -e +#服务器配置可能不一致,增加这些配置避免乱码问题 +export LANG=en_US.UTF-8 +export LC_CTYPE=en_US.UTF-8 +export LC_ALL=en_US.UTF-8 + +TMP_JAVA_HOME="/nemo/jdk1.8.0_152" + +#detect operating system. +OS=$(uname -o) + +function is_java8 { + local _java="$1" + [[ -x "$_java" ]] || return 1 + [[ "$("$_java" -version 2>&1)" =~ 'java version "1.8' || "$("$_java" -version 2>&1)" =~ 'openjdk version "1.8' ]] || return 2 + return 0 +} + + +function get_pid { + local ppid="" + if [ -f ${DEMO_HOME}/bin/pid_http_pub.file ]; then + ppid=$(cat ${DEMO_HOME}/bin/pid_http_pub.file) + else + if [[ $OS =~ Msys ]]; then + # 在Msys上存在可能无法kill识别出的进程的BUG + ppid=`jps -v | grep -i "org.apache.eventmesh.http.demo.AsyncPublishInstance" | grep java | grep -v grep | awk -F ' ' {'print $1'}` + elif [[ $OS =~ Darwin ]]; then + # 已知问题:grep java 可能无法精确识别java进程 + ppid=$(/bin/ps -o user,pid,command | grep "java" | grep -i "org.apache.eventmesh.http.demo.AsyncPublishInstance" | grep -Ev "^root" |awk -F ' ' {'print $2'}) + else + #在Linux服务器上要求尽可能精确识别进程 + ppid=$(ps -C java -o user,pid,command --cols 99999 | grep -w $EVENTMESH_HOME | grep -i "org.apache.eventmesh.http.demo.AsyncPublishInstance" | grep -Ev "^root" |awk -F ' ' {'print $2'}) + fi + fi + echo "$ppid"; +} + + +if [[ -d "$TMP_JAVA_HOME" ]] && is_java8 "$TMP_JAVA_HOME/bin/java"; then + JAVA="$TMP_JAVA_HOME/bin/java" +elif [[ -d "$JAVA_HOME" ]] && is_java8 "$JAVA_HOME/bin/java"; then + JAVA="$JAVA_HOME/bin/java" +elif is_java8 "/nemo/jdk8/bin/java"; then + JAVA="/nemo/jdk8/bin/java"; +elif is_java8 "/nemo/jdk1.8/bin/java"; then + JAVA="/nemo/jdk1.8/bin/java"; +elif is_java8 "/nemo/jdk/bin/java"; then + JAVA="/nemo/jdk/bin/java"; +elif is_java8 "$(which java)"; then + JAVA="$(which java)" +else + echo -e "ERROR\t java(1.8) not found, operation abort." + exit 9; +fi + +echo "http_pub_demo use java location= "$JAVA + +DEMO_HOME=`cd "./.." && pwd` + +export DEMO_HOME + +export DEMO_LOG_HOME=${DEMO_HOME}/logs + +echo "DEMO_HOME : ${DEMO_HOME}, DEMO_LOG_HOME : ${DEMO_LOG_HOME}" + +function make_logs_dir { + if [ ! -e "${DEMO_LOG_HOME}" ]; then mkdir -p "${DEMO_LOG_HOME}"; fi +} + +error_exit () +{ + echo "ERROR: $1 !!" + exit 1 +} + +export JAVA_HOME + +JAVA_OPT=`cat ${DEMO_HOME}/conf/server.env | grep APP_START_JVM_OPTION::: | awk -F ':::' {'print $2'}` +JAVA_OPT="${JAVA_OPT} -XX:+UseG1GC -XX:G1HeapRegionSize=16m -XX:G1ReservePercent=25 -XX:InitiatingHeapOccupancyPercent=30 -XX:SoftRefLRUPolicyMSPerMB=0 -XX:SurvivorRatio=8 -XX:MaxGCPauseMillis=50" +JAVA_OPT="${JAVA_OPT} -verbose:gc -Xloggc:${DEMO_HOME}/logs/demo_http_pub_gc_%p.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCApplicationStoppedTime -XX:+PrintAdaptiveSizePolicy" +JAVA_OPT="${JAVA_OPT} -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${DEMO_HOME}/logs -XX:ErrorFile=${EVENTMESH_HOME}/logs/hs_err_%p.log" +JAVA_OPT="${JAVA_OPT} -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=30m" +JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow" +JAVA_OPT="${JAVA_OPT} -XX:+AlwaysPreTouch" +JAVA_OPT="${JAVA_OPT} -XX:MaxDirectMemorySize=8G" +JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages -XX:-UseBiasedLocking" +JAVA_OPT="${JAVA_OPT} -Dio.netty.leakDetectionLevel=advanced" +JAVA_OPT="${JAVA_OPT} -Dio.netty.allocator.type=pooled" +JAVA_OPT="${JAVA_OPT} -Djava.security.egd=file:/dev/./urandom" +JAVA_OPT="${JAVA_OPT} -Dlog4j.configurationFile=${DEMO_HOME}/conf/log4j2.xml" +#JAVA_OPT="${JAVA_OPT} -Deventmesh.log.home=${DEMO_LOG_HOME}" +JAVA_OPT="${JAVA_OPT} -DconfPath=${DEMO_HOME}/conf" +JAVA_OPT="${JAVA_OPT} -Dlog4j2.AsyncQueueFullPolicy=Discard" +JAVA_OPT="${JAVA_OPT} -Drocketmq.client.logUseSlf4j=true" + +pid=$(get_pid) +if [ -n "$pid" ];then + echo -e "ERROR\t the server is already running (pid=$pid), there is no need to execute start.sh again." + exit 9; +fi + +make_logs_dir + +echo "using jdk[$JAVA]" >> ${DEMO_LOG_HOME}/demo_http_pub.out + + +DEMO_MAIN=org.apache.eventmesh.http.demo.AsyncPublishInstance +if [ $DOCKER ] +then + $JAVA $JAVA_OPT -classpath ${DEMO_HOME}/conf:${DEMO_HOME}/apps/*:${DEMO_HOME}/lib/* $DEMO_MAIN >> ${DEMO_LOG_HOME}/demo_http_pub.out +else + $JAVA $JAVA_OPT -classpath ${DEMO_HOME}/conf:${DEMO_HOME}/apps/*:${DEMO_HOME}/lib/* $DEMO_MAIN >> ${DEMO_LOG_HOME}/demo_http_pub.out 2>&1 & +echo $!>pid_http_pub.file +fi +exit 0 diff --git a/eventmesh-test/bin/http_sub.sh b/eventmesh-test/bin/http_sub.sh new file mode 100644 index 0000000000..f5413ca64d --- /dev/null +++ b/eventmesh-test/bin/http_sub.sh @@ -0,0 +1,138 @@ +#!/bin/sh +# +# Licensed to Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Apache Software Foundation (ASF) licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +#=========================================================================================== +# Java Environment Setting +#=========================================================================================== +set -e +#服务器配置可能不一致,增加这些配置避免乱码问题 +export LANG=en_US.UTF-8 +export LC_CTYPE=en_US.UTF-8 +export LC_ALL=en_US.UTF-8 + +TMP_JAVA_HOME="/nemo/jdk1.8.0_152" + +#detect operating system. +OS=$(uname -o) + +function is_java8 { + local _java="$1" + [[ -x "$_java" ]] || return 1 + [[ "$("$_java" -version 2>&1)" =~ 'java version "1.8' || "$("$_java" -version 2>&1)" =~ 'openjdk version "1.8' ]] || return 2 + return 0 +} + + +function get_pid { + local ppid="" + if [ -f ${DEMO_HOME}/bin/pid_http_sub.file ]; then + ppid=$(cat ${DEMO_HOME}/bin/pid_http_sub.file) + else + if [[ $OS =~ Msys ]]; then + # 在Msys上存在可能无法kill识别出的进程的BUG + ppid=`jps -v | grep -i "org.apache.eventmesh.http.demo.sub.SpringBootDemoApplication" | grep java | grep -v grep | awk -F ' ' {'print $1'}` + elif [[ $OS =~ Darwin ]]; then + # 已知问题:grep java 可能无法精确识别java进程 + ppid=$(/bin/ps -o user,pid,command | grep "java" | grep -i "org.apache.eventmesh.http.demo.sub.SpringBootDemoApplication" | grep -Ev "^root" |awk -F ' ' {'print $2'}) + else + #在Linux服务器上要求尽可能精确识别进程 + ppid=$(ps -C java -o user,pid,command --cols 99999 | grep -w $EVENTMESH_HOME | grep -i "org.apache.eventmesh.http.demo.sub.SpringBootDemoApplication" | grep -Ev "^root" |awk -F ' ' {'print $2'}) + fi + fi + echo "$ppid"; +} + + +if [[ -d "$TMP_JAVA_HOME" ]] && is_java8 "$TMP_JAVA_HOME/bin/java"; then + JAVA="$TMP_JAVA_HOME/bin/java" +elif [[ -d "$JAVA_HOME" ]] && is_java8 "$JAVA_HOME/bin/java"; then + JAVA="$JAVA_HOME/bin/java" +elif is_java8 "/nemo/jdk8/bin/java"; then + JAVA="/nemo/jdk8/bin/java"; +elif is_java8 "/nemo/jdk1.8/bin/java"; then + JAVA="/nemo/jdk1.8/bin/java"; +elif is_java8 "/nemo/jdk/bin/java"; then + JAVA="/nemo/jdk/bin/java"; +elif is_java8 "$(which java)"; then + JAVA="$(which java)" +else + echo -e "ERROR\t java(1.8) not found, operation abort." + exit 9; +fi + +echo "http_sub_demo use java location= "$JAVA + +DEMO_HOME=`cd "./.." && pwd` + +export DEMO_HOME + +export DEMO_LOG_HOME=${DEMO_HOME}/logs + +echo "DEMO_HOME : ${DEMO_HOME}, DEMO_LOG_HOME : ${DEMO_LOG_HOME}" + +function make_logs_dir { + if [ ! -e "${DEMO_LOG_HOME}" ]; then mkdir -p "${DEMO_LOG_HOME}"; fi +} + +error_exit () +{ + echo "ERROR: $1 !!" + exit 1 +} + +export JAVA_HOME + +JAVA_OPT=`cat ${DEMO_HOME}/conf/server.env | grep APP_START_JVM_OPTION::: | awk -F ':::' {'print $2'}` +JAVA_OPT="${JAVA_OPT} -XX:+UseG1GC -XX:G1HeapRegionSize=16m -XX:G1ReservePercent=25 -XX:InitiatingHeapOccupancyPercent=30 -XX:SoftRefLRUPolicyMSPerMB=0 -XX:SurvivorRatio=8 -XX:MaxGCPauseMillis=50" +JAVA_OPT="${JAVA_OPT} -verbose:gc -Xloggc:${DEMO_HOME}/logs/demo_http_sub_gc_%p.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCApplicationStoppedTime -XX:+PrintAdaptiveSizePolicy" +JAVA_OPT="${JAVA_OPT} -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${DEMO_HOME}/logs -XX:ErrorFile=${EVENTMESH_HOME}/logs/hs_err_%p.log" +JAVA_OPT="${JAVA_OPT} -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=30m" +JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow" +JAVA_OPT="${JAVA_OPT} -XX:+AlwaysPreTouch" +JAVA_OPT="${JAVA_OPT} -XX:MaxDirectMemorySize=8G" +JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages -XX:-UseBiasedLocking" +JAVA_OPT="${JAVA_OPT} -Dio.netty.leakDetectionLevel=advanced" +JAVA_OPT="${JAVA_OPT} -Dio.netty.allocator.type=pooled" +JAVA_OPT="${JAVA_OPT} -Djava.security.egd=file:/dev/./urandom" +JAVA_OPT="${JAVA_OPT} -Dlog4j.configurationFile=${DEMO_HOME}/conf/log4j2.xml" +#JAVA_OPT="${JAVA_OPT} -Deventmesh.log.home=${DEMO_LOG_HOME}" +JAVA_OPT="${JAVA_OPT} -DconfPath=${DEMO_HOME}/conf" +JAVA_OPT="${JAVA_OPT} -Dlog4j2.AsyncQueueFullPolicy=Discard" +JAVA_OPT="${JAVA_OPT} -Drocketmq.client.logUseSlf4j=true" + +pid=$(get_pid) +if [ -n "$pid" ];then + echo -e "ERROR\t the server is already running (pid=$pid), there is no need to execute start.sh again." + exit 9; +fi + +make_logs_dir + +echo "using jdk[$JAVA]" >> ${DEMO_LOG_HOME}/demo_http_sub.out + + +DEMO_MAIN=org.apache.eventmesh.http.demo.sub.SpringBootDemoApplication +if [ $DOCKER ] +then + $JAVA $JAVA_OPT -classpath ${DEMO_HOME}/conf:${DEMO_HOME}/apps/*:${DEMO_HOME}/lib/* $DEMO_MAIN >> ${DEMO_LOG_HOME}/demo_http_sub.out +else + $JAVA $JAVA_OPT -classpath ${DEMO_HOME}/conf:${DEMO_HOME}/apps/*:${DEMO_HOME}/lib/* $DEMO_MAIN >> ${DEMO_LOG_HOME}/demo_http_sub.out 2>&1 & +echo $!>pid_http_sub.file +fi +exit 0 diff --git a/eventmesh-test/bin/tcp_pub.sh b/eventmesh-test/bin/tcp_pub.sh new file mode 100644 index 0000000000..fcf7c51214 --- /dev/null +++ b/eventmesh-test/bin/tcp_pub.sh @@ -0,0 +1,138 @@ +#!/bin/sh +# +# Licensed to Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Apache Software Foundation (ASF) licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +#=========================================================================================== +# Java Environment Setting +#=========================================================================================== +set -e +#服务器配置可能不一致,增加这些配置避免乱码问题 +export LANG=en_US.UTF-8 +export LC_CTYPE=en_US.UTF-8 +export LC_ALL=en_US.UTF-8 + +TMP_JAVA_HOME="/nemo/jdk1.8.0_152" + +#detect operating system. +OS=$(uname -o) + +function is_java8 { + local _java="$1" + [[ -x "$_java" ]] || return 1 + [[ "$("$_java" -version 2>&1)" =~ 'java version "1.8' || "$("$_java" -version 2>&1)" =~ 'openjdk version "1.8' ]] || return 2 + return 0 +} + + +function get_pid { + local ppid="" + if [ -f ${DEMO_HOME}/bin/pid_tcp_pub.file ]; then + ppid=$(cat ${DEMO_HOME}/bin/pid_tcp_pub.file) + else + if [[ $OS =~ Msys ]]; then + # 在Msys上存在可能无法kill识别出的进程的BUG + ppid=`jps -v | grep -i "org.apache.eventmesh.tcp.demo.AsyncPublish" | grep java | grep -v grep | awk -F ' ' {'print $1'}` + elif [[ $OS =~ Darwin ]]; then + # 已知问题:grep java 可能无法精确识别java进程 + ppid=$(/bin/ps -o user,pid,command | grep "java" | grep -i "org.apache.eventmesh.tcp.demo.AsyncPublish" | grep -Ev "^root" |awk -F ' ' {'print $2'}) + else + #在Linux服务器上要求尽可能精确识别进程 + ppid=$(ps -C java -o user,pid,command --cols 99999 | grep -w $EVENTMESH_HOME | grep -i "org.apache.eventmesh.tcp.demo.AsyncPublish" | grep -Ev "^root" |awk -F ' ' {'print $2'}) + fi + fi + echo "ppid"; +} + + +if [[ -d "$TMP_JAVA_HOME" ]] && is_java8 "$TMP_JAVA_HOME/bin/java"; then + JAVA="$TMP_JAVA_HOME/bin/java" +elif [[ -d "$JAVA_HOME" ]] && is_java8 "$JAVA_HOME/bin/java"; then + JAVA="$JAVA_HOME/bin/java" +elif is_java8 "/nemo/jdk8/bin/java"; then + JAVA="/nemo/jdk8/bin/java"; +elif is_java8 "/nemo/jdk1.8/bin/java"; then + JAVA="/nemo/jdk1.8/bin/java"; +elif is_java8 "/nemo/jdk/bin/java"; then + JAVA="/nemo/jdk/bin/java"; +elif is_java8 "$(which java)"; then + JAVA="$(which java)" +else + echo -e "ERROR\t java(1.8) not found, operation abort." + exit 9; +fi + +echo "tcp_pub_demo use java location= "$JAVA + +DEMO_HOME=`cd "./.." && pwd` + +export DEMO_HOME + +export DEMO_LOG_HOME=${DEMO_HOME}/logs + +echo "DEMO_HOME : ${DEMO_HOME}, DEMO_LOG_HOME : ${DEMO_LOG_HOME}" + +function make_logs_dir { + if [ ! -e "${DEMO_LOG_HOME}" ]; then mkdir -p "${DEMO_LOG_HOME}"; fi +} + +error_exit () +{ + echo "ERROR: $1 !!" + exit 1 +} + +export JAVA_HOME + +JAVA_OPT=`cat ${DEMO_HOME}/conf/server.env | grep APP_START_JVM_OPTION::: | awk -F ':::' {'print $2'}` +JAVA_OPT="${JAVA_OPT} -XX:+UseG1GC -XX:G1HeapRegionSize=16m -XX:G1ReservePercent=25 -XX:InitiatingHeapOccupancyPercent=30 -XX:SoftRefLRUPolicyMSPerMB=0 -XX:SurvivorRatio=8 -XX:MaxGCPauseMillis=50" +JAVA_OPT="${JAVA_OPT} -verbose:gc -Xloggc:${DEMO_HOME}/logs/demo_tcp_pub_gc_%p.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCApplicationStoppedTime -XX:+PrintAdaptiveSizePolicy" +JAVA_OPT="${JAVA_OPT} -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${DEMO_HOME}/logs -XX:ErrorFile=${EVENTMESH_HOME}/logs/hs_err_%p.log" +JAVA_OPT="${JAVA_OPT} -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=30m" +JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow" +JAVA_OPT="${JAVA_OPT} -XX:+AlwaysPreTouch" +JAVA_OPT="${JAVA_OPT} -XX:MaxDirectMemorySize=8G" +JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages -XX:-UseBiasedLocking" +JAVA_OPT="${JAVA_OPT} -Dio.netty.leakDetectionLevel=advanced" +JAVA_OPT="${JAVA_OPT} -Dio.netty.allocator.type=pooled" +JAVA_OPT="${JAVA_OPT} -Djava.security.egd=file:/dev/./urandom" +JAVA_OPT="${JAVA_OPT} -Dlog4j.configurationFile=${DEMO_HOME}/conf/log4j2.xml" +#JAVA_OPT="${JAVA_OPT} -Deventmesh.log.home=${DEMO_LOG_HOME}" +JAVA_OPT="${JAVA_OPT} -DconfPath=${DEMO_HOME}/conf" +JAVA_OPT="${JAVA_OPT} -Dlog4j2.AsyncQueueFullPolicy=Discard" +JAVA_OPT="${JAVA_OPT} -Drocketmq.client.logUseSlf4j=true" + +pid=$(get_pid) +if [ -n "$pid" ];then + echo -e "ERROR\t the server is already running (pid=$pid), there is no need to execute start.sh again." + exit 9; +fi + +make_logs_dir + +echo "using jdk[$JAVA]" >> ${DEMO_LOG_HOME}/demo_tcp_pub.out + + +DEMO_MAIN=org.apache.eventmesh.tcp.demo.AsyncPublish +if [ $DOCKER ] +then + $JAVA $JAVA_OPT -classpath ${DEMO_HOME}/conf:${DEMO_HOME}/apps/*:${DEMO_HOME}/lib/* $DEMO_MAIN >> ${DEMO_LOG_HOME}/demo_tcp_pub.out +else + $JAVA $JAVA_OPT -classpath ${DEMO_HOME}/conf:${DEMO_HOME}/apps/*:${DEMO_HOME}/lib/* $DEMO_MAIN >> ${DEMO_LOG_HOME}/demo_tcp_pub.out 2>&1 & +echo $!>pid_tcp_pub.file +fi +exit 0 diff --git a/eventmesh-test/bin/tcp_pub_broadcast.sh b/eventmesh-test/bin/tcp_pub_broadcast.sh new file mode 100644 index 0000000000..25f2bc5619 --- /dev/null +++ b/eventmesh-test/bin/tcp_pub_broadcast.sh @@ -0,0 +1,138 @@ +#!/bin/sh +# +# Licensed to Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Apache Software Foundation (ASF) licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +#=========================================================================================== +# Java Environment Setting +#=========================================================================================== +set -e +#服务器配置可能不一致,增加这些配置避免乱码问题 +export LANG=en_US.UTF-8 +export LC_CTYPE=en_US.UTF-8 +export LC_ALL=en_US.UTF-8 + +TMP_JAVA_HOME="/nemo/jdk1.8.0_152" + +#detect operating system. +OS=$(uname -o) + +function is_java8 { + local _java="$1" + [[ -x "$_java" ]] || return 1 + [[ "$("$_java" -version 2>&1)" =~ 'java version "1.8' || "$("$_java" -version 2>&1)" =~ 'openjdk version "1.8' ]] || return 2 + return 0 +} + + +function get_pid { + local ppid="" + if [ -f ${DEMO_HOME}/bin/pid_tcp_pub_broadcast.file ]; then + ppid=$(cat ${DEMO_HOME}/bin/pid_tcp_pub_broadcast.file) + else + if [[ $OS =~ Msys ]]; then + # 在Msys上存在可能无法kill识别出的进程的BUG + ppid=`jps -v | grep -i "org.apache.eventmesh.tcp.demo.AsyncPublishBroadcast" | grep java | grep -v grep | awk -F ' ' {'print $1'}` + elif [[ $OS =~ Darwin ]]; then + # 已知问题:grep java 可能无法精确识别java进程 + ppid=$(/bin/ps -o user,pid,command | grep "java" | grep -i "org.apache.eventmesh.tcp.demo.AsyncPublishBroadcast" | grep -Ev "^root" |awk -F ' ' {'print $2'}) + else + #在Linux服务器上要求尽可能精确识别进程 + ppid=$(ps -C java -o user,pid,command --cols 99999 | grep -w $DEMO_HOME | grep -i "org.apache.eventmesh.tcp.demo.AsyncPublishBroadcast" | grep -Ev "^root" |awk -F ' ' {'print $2'}) + fi + fi + echo "$ppid"; +} + + +if [[ -d "$TMP_JAVA_HOME" ]] && is_java8 "$TMP_JAVA_HOME/bin/java"; then + JAVA="$TMP_JAVA_HOME/bin/java" +elif [[ -d "$JAVA_HOME" ]] && is_java8 "$JAVA_HOME/bin/java"; then + JAVA="$JAVA_HOME/bin/java" +elif is_java8 "/nemo/jdk8/bin/java"; then + JAVA="/nemo/jdk8/bin/java"; +elif is_java8 "/nemo/jdk1.8/bin/java"; then + JAVA="/nemo/jdk1.8/bin/java"; +elif is_java8 "/nemo/jdk/bin/java"; then + JAVA="/nemo/jdk/bin/java"; +elif is_java8 "$(which java)"; then + JAVA="$(which java)" +else + echo -e "ERROR\t java(1.8) not found, operation abort." + exit 9; +fi + +echo "tcp_pub_demo use java location= "$JAVA + +DEMO_HOME=`cd "./.." && pwd` + +export DEMO_HOME + +export DEMO_LOG_HOME=${DEMO_HOME}/logs + +echo "DEMO_HOME : ${DEMO_HOME}, DEMO_LOG_HOME : ${DEMO_LOG_HOME}" + +function make_logs_dir { + if [ ! -e "${DEMO_LOG_HOME}" ]; then mkdir -p "${DEMO_LOG_HOME}"; fi +} + +error_exit () +{ + echo "ERROR: $1 !!" + exit 1 +} + +export JAVA_HOME + +JAVA_OPT=`cat ${DEMO_HOME}/conf/server.env | grep APP_START_JVM_OPTION::: | awk -F ':::' {'print $2'}` +JAVA_OPT="${JAVA_OPT} -XX:+UseG1GC -XX:G1HeapRegionSize=16m -XX:G1ReservePercent=25 -XX:InitiatingHeapOccupancyPercent=30 -XX:SoftRefLRUPolicyMSPerMB=0 -XX:SurvivorRatio=8 -XX:MaxGCPauseMillis=50" +JAVA_OPT="${JAVA_OPT} -verbose:gc -Xloggc:${DEMO_HOME}/logs/demo_tcp_pub_gc_%p.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCApplicationStoppedTime -XX:+PrintAdaptiveSizePolicy" +JAVA_OPT="${JAVA_OPT} -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${DEMO_HOME}/logs -XX:ErrorFile=${DEMO_HOME}/logs/hs_err_%p.log" +JAVA_OPT="${JAVA_OPT} -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=30m" +JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow" +JAVA_OPT="${JAVA_OPT} -XX:+AlwaysPreTouch" +JAVA_OPT="${JAVA_OPT} -XX:MaxDirectMemorySize=8G" +JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages -XX:-UseBiasedLocking" +JAVA_OPT="${JAVA_OPT} -Dio.netty.leakDetectionLevel=advanced" +JAVA_OPT="${JAVA_OPT} -Dio.netty.allocator.type=pooled" +JAVA_OPT="${JAVA_OPT} -Djava.security.egd=file:/dev/./urandom" +JAVA_OPT="${JAVA_OPT} -Dlog4j.configurationFile=${DEMO_HOME}/conf/log4j2.xml" +#JAVA_OPT="${JAVA_OPT} -Deventmesh.log.home=${DEMO_LOG_HOME}" +JAVA_OPT="${JAVA_OPT} -DconfPath=${DEMO_HOME}/conf" +JAVA_OPT="${JAVA_OPT} -Dlog4j2.AsyncQueueFullPolicy=Discard" +JAVA_OPT="${JAVA_OPT} -Drocketmq.client.logUseSlf4j=true" + +pid=$(get_pid) +if [ -n "$pid" ];then + echo -e "ERROR\t the server is already running (pid=$pid), there is no need to execute start.sh again." + exit 9; +fi + +make_logs_dir + +echo "using jdk[$JAVA]" >> ${DEMO_LOG_HOME}/demo_tcp_pub_broadcast.out + + +DEMO_MAIN=org.apache.eventmesh.tcp.demo.AsyncPublishBroadcast +if [ $DOCKER ] +then + $JAVA $JAVA_OPT -classpath ${DEMO_HOME}/conf:${DEMO_HOME}/apps/*:${DEMO_HOME}/lib/* $DEMO_MAIN >> ${DEMO_LOG_HOME}/demo_tcp_pub_broadcast.out +else + $JAVA $JAVA_OPT -classpath ${DEMO_HOME}/conf:${DEMO_HOME}/apps/*:${DEMO_HOME}/lib/* $DEMO_MAIN >> ${DEMO_LOG_HOME}/demo_tcp_pub_broadcast.out 2>&1 & +echo $!>pid_tcp_pub_broadcast.file +fi +exit 0 diff --git a/eventmesh-test/bin/tcp_sub.sh b/eventmesh-test/bin/tcp_sub.sh new file mode 100644 index 0000000000..5a1a572cac --- /dev/null +++ b/eventmesh-test/bin/tcp_sub.sh @@ -0,0 +1,138 @@ +#!/bin/sh +# +# Licensed to Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Apache Software Foundation (ASF) licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +#=========================================================================================== +# Java Environment Setting +#=========================================================================================== +set -e +#服务器配置可能不一致,增加这些配置避免乱码问题 +export LANG=en_US.UTF-8 +export LC_CTYPE=en_US.UTF-8 +export LC_ALL=en_US.UTF-8 + +TMP_JAVA_HOME="/nemo/jdk1.8.0_152" + +#detect operating system. +OS=$(uname -o) + +function is_java8 { + local _java="$1" + [[ -x "$_java" ]] || return 1 + [[ "$("$_java" -version 2>&1)" =~ 'java version "1.8' || "$("$_java" -version 2>&1)" =~ 'openjdk version "1.8' ]] || return 2 + return 0 +} + + +function get_pid { + local ppid="" + if [ -f ${DEMO_HOME}/bin/pid_tcp_sub.file ]; then + ppid=$(cat ${DEMO_HOME}/bin/pid_tcp_sub.file) + else + if [[ $OS =~ Msys ]]; then + # 在Msys上存在可能无法kill识别出的进程的BUG + ppid=`jps -v | grep -i "org.apache.eventmesh.tcp.demo.AsyncSubscribe" | grep java | grep -v grep | awk -F ' ' {'print $1'}` + elif [[ $OS =~ Darwin ]]; then + # 已知问题:grep java 可能无法精确识别java进程 + ppid=$(/bin/ps -o user,pid,command | grep "java" | grep -i "org.apache.eventmesh.tcp.demo.AsyncSubscribe" | grep -Ev "^root" |awk -F ' ' {'print $2'}) + else + #在Linux服务器上要求尽可能精确识别进程 + ppid=$(ps -C java -o user,pid,command --cols 99999 | grep -w $DEMO_HOME | grep -i "org.apache.eventmesh.tcp.demo.AsyncSubscribe" | grep -Ev "^root" |awk -F ' ' {'print $2'}) + fi + fi + echo "$ppid"; +} + + +if [[ -d "$TMP_JAVA_HOME" ]] && is_java8 "$TMP_JAVA_HOME/bin/java"; then + JAVA="$TMP_JAVA_HOME/bin/java" +elif [[ -d "$JAVA_HOME" ]] && is_java8 "$JAVA_HOME/bin/java"; then + JAVA="$JAVA_HOME/bin/java" +elif is_java8 "/nemo/jdk8/bin/java"; then + JAVA="/nemo/jdk8/bin/java"; +elif is_java8 "/nemo/jdk1.8/bin/java"; then + JAVA="/nemo/jdk1.8/bin/java"; +elif is_java8 "/nemo/jdk/bin/java"; then + JAVA="/nemo/jdk/bin/java"; +elif is_java8 "$(which java)"; then + JAVA="$(which java)" +else + echo -e "ERROR\t java(1.8) not found, operation abort." + exit 9; +fi + +echo "tcp_sub_demo use java location= "$JAVA + +DEMO_HOME=`cd "./.." && pwd` + +export DEMO_HOME + +export DEMO_LOG_HOME=${DEMO_HOME}/logs + +echo "DEMO_HOME : ${DEMO_HOME}, DEMO_LOG_HOME : ${DEMO_LOG_HOME}" + +function make_logs_dir { + if [ ! -e "${DEMO_LOG_HOME}" ]; then mkdir -p "${DEMO_LOG_HOME}"; fi +} + +error_exit () +{ + echo "ERROR: $1 !!" + exit 1 +} + +export JAVA_HOME + +JAVA_OPT=`cat ${DEMO_HOME}/conf/server.env | grep APP_START_JVM_OPTION::: | awk -F ':::' {'print $2'}` +JAVA_OPT="${JAVA_OPT} -XX:+UseG1GC -XX:G1HeapRegionSize=16m -XX:G1ReservePercent=25 -XX:InitiatingHeapOccupancyPercent=30 -XX:SoftRefLRUPolicyMSPerMB=0 -XX:SurvivorRatio=8 -XX:MaxGCPauseMillis=50" +JAVA_OPT="${JAVA_OPT} -verbose:gc -Xloggc:${DEMO_HOME}/logs/demo_tcp_sub_gc_%p.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCApplicationStoppedTime -XX:+PrintAdaptiveSizePolicy" +JAVA_OPT="${JAVA_OPT} -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${DEMO_HOME}/logs -XX:ErrorFile=${DEMO_HOME}/logs/hs_err_%p.log" +JAVA_OPT="${JAVA_OPT} -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=30m" +JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow" +JAVA_OPT="${JAVA_OPT} -XX:+AlwaysPreTouch" +JAVA_OPT="${JAVA_OPT} -XX:MaxDirectMemorySize=8G" +JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages -XX:-UseBiasedLocking" +JAVA_OPT="${JAVA_OPT} -Dio.netty.leakDetectionLevel=advanced" +JAVA_OPT="${JAVA_OPT} -Dio.netty.allocator.type=pooled" +JAVA_OPT="${JAVA_OPT} -Djava.security.egd=file:/dev/./urandom" +JAVA_OPT="${JAVA_OPT} -Dlog4j.configurationFile=${DEMO_HOME}/conf/log4j2.xml" +#JAVA_OPT="${JAVA_OPT} -Deventmesh.log.home=${DEMO_LOG_HOME}" +JAVA_OPT="${JAVA_OPT} -DconfPath=${DEMO_HOME}/conf" +JAVA_OPT="${JAVA_OPT} -Dlog4j2.AsyncQueueFullPolicy=Discard" +JAVA_OPT="${JAVA_OPT} -Drocketmq.client.logUseSlf4j=true" + +pid=$(get_pid) +if [ -n "$pid" ];then + echo -e "ERROR\t the server is already running (pid=$pid), there is no need to execute start.sh again." + exit 9; +fi + +make_logs_dir + +echo "using jdk[$JAVA]" >> ${DEMO_LOG_HOME}/demo_tcp_sub.out + + +DEMO_MAIN=org.apache.eventmesh.tcp.demo.AsyncSubscribe +if [ $DOCKER ] +then + $JAVA $JAVA_OPT -classpath ${DEMO_HOME}/conf:${DEMO_HOME}/apps/*:${DEMO_HOME}/lib/* $DEMO_MAIN >> ${DEMO_LOG_HOME}/demo_tcp_sub.out +else + $JAVA $JAVA_OPT -classpath ${DEMO_HOME}/conf:${DEMO_HOME}/apps/*:${DEMO_HOME}/lib/* $DEMO_MAIN >> ${DEMO_LOG_HOME}/demo_tcp_sub.out 2>&1 & +echo $!>pid_tcp_sub.file +fi +exit 0 diff --git a/eventmesh-test/bin/tcp_sub_broadcast.sh b/eventmesh-test/bin/tcp_sub_broadcast.sh new file mode 100644 index 0000000000..472f711b7d --- /dev/null +++ b/eventmesh-test/bin/tcp_sub_broadcast.sh @@ -0,0 +1,138 @@ +#!/bin/sh +# +# Licensed to Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Apache Software Foundation (ASF) licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +#=========================================================================================== +# Java Environment Setting +#=========================================================================================== +set -e +#服务器配置可能不一致,增加这些配置避免乱码问题 +export LANG=en_US.UTF-8 +export LC_CTYPE=en_US.UTF-8 +export LC_ALL=en_US.UTF-8 + +TMP_JAVA_HOME="/nemo/jdk1.8.0_152" + +#detect operating system. +OS=$(uname -o) + +function is_java8 { + local _java="$1" + [[ -x "$_java" ]] || return 1 + [[ "$("$_java" -version 2>&1)" =~ 'java version "1.8' || "$("$_java" -version 2>&1)" =~ 'openjdk version "1.8' ]] || return 2 + return 0 +} + + +function get_pid { + local ppid="" + if [ -f ${EVENTMESH_HOME}/bin/pid_tcp_sub_broadcast.file ]; then + ppid=$(cat ${EVENTMESH_HOME}/bin/pid_tcp_sub_broadcast.file) + else + if [[ $OS =~ Msys ]]; then + # 在Msys上存在可能无法kill识别出的进程的BUG + ppid=`jps -v | grep -i "org.apache.eventmesh.tcp.demo.AsyncSubscribeBroadcast" | grep java | grep -v grep | awk -F ' ' {'print $1'}` + elif [[ $OS =~ Darwin ]]; then + # 已知问题:grep java 可能无法精确识别java进程 + ppid=$(/bin/ps -o user,pid,command | grep "java" | grep -i "org.apache.eventmesh.tcp.demo.AsyncSubscribeBroadcast" | grep -Ev "^root" |awk -F ' ' {'print $2'}) + else + #在Linux服务器上要求尽可能精确识别进程 + ppid=$(ps -C java -o user,pid,command --cols 99999 | grep -w $EVENTMESH_HOME | grep -i "org.apache.eventmesh.tcp.demo.AsyncSubscribeBroadcast" | grep -Ev "^root" |awk -F ' ' {'print $2'}) + fi + fi + echo "$ppid"; +} + + +if [[ -d "$TMP_JAVA_HOME" ]] && is_java8 "$TMP_JAVA_HOME/bin/java"; then + JAVA="$TMP_JAVA_HOME/bin/java" +elif [[ -d "$JAVA_HOME" ]] && is_java8 "$JAVA_HOME/bin/java"; then + JAVA="$JAVA_HOME/bin/java" +elif is_java8 "/nemo/jdk8/bin/java"; then + JAVA="/nemo/jdk8/bin/java"; +elif is_java8 "/nemo/jdk1.8/bin/java"; then + JAVA="/nemo/jdk1.8/bin/java"; +elif is_java8 "/nemo/jdk/bin/java"; then + JAVA="/nemo/jdk/bin/java"; +elif is_java8 "$(which java)"; then + JAVA="$(which java)" +else + echo -e "ERROR\t java(1.8) not found, operation abort." + exit 9; +fi + +echo "tcp_sub_demo use java location= "$JAVA + +DEMO_HOME=`cd "./.." && pwd` + +export DEMO_HOME + +export DEMO_LOG_HOME=${DEMO_HOME}/logs + +echo "DEMO_HOME : ${DEMO_HOME}, DEMO_LOG_HOME : ${DEMO_LOG_HOME}" + +function make_logs_dir { + if [ ! -e "${DEMO_LOG_HOME}" ]; then mkdir -p "${DEMO_LOG_HOME}"; fi +} + +error_exit () +{ + echo "ERROR: $1 !!" + exit 1 +} + +export JAVA_HOME + +JAVA_OPT=`cat ${DEMO_HOME}/conf/server.env | grep APP_START_JVM_OPTION::: | awk -F ':::' {'print $2'}` +JAVA_OPT="${JAVA_OPT} -XX:+UseG1GC -XX:G1HeapRegionSize=16m -XX:G1ReservePercent=25 -XX:InitiatingHeapOccupancyPercent=30 -XX:SoftRefLRUPolicyMSPerMB=0 -XX:SurvivorRatio=8 -XX:MaxGCPauseMillis=50" +JAVA_OPT="${JAVA_OPT} -verbose:gc -Xloggc:${DEMO_HOME}/logs/demo_tcp_sub_gc_%p.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCApplicationStoppedTime -XX:+PrintAdaptiveSizePolicy" +JAVA_OPT="${JAVA_OPT} -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${DEMO_HOME}/logs -XX:ErrorFile=${DEMO_HOME}/logs/hs_err_%p.log" +JAVA_OPT="${JAVA_OPT} -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=30m" +JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow" +JAVA_OPT="${JAVA_OPT} -XX:+AlwaysPreTouch" +JAVA_OPT="${JAVA_OPT} -XX:MaxDirectMemorySize=8G" +JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages -XX:-UseBiasedLocking" +JAVA_OPT="${JAVA_OPT} -Dio.netty.leakDetectionLevel=advanced" +JAVA_OPT="${JAVA_OPT} -Dio.netty.allocator.type=pooled" +JAVA_OPT="${JAVA_OPT} -Djava.security.egd=file:/dev/./urandom" +JAVA_OPT="${JAVA_OPT} -Dlog4j.configurationFile=${DEMO_HOME}/conf/log4j2.xml" +#JAVA_OPT="${JAVA_OPT} -Deventmesh.log.home=${DEMO_LOG_HOME}" +JAVA_OPT="${JAVA_OPT} -DconfPath=${DEMO_HOME}/conf" +JAVA_OPT="${JAVA_OPT} -Dlog4j2.AsyncQueueFullPolicy=Discard" +JAVA_OPT="${JAVA_OPT} -Drocketmq.client.logUseSlf4j=true" + +pid=$(get_pid) +if [ -n "$pid" ];then + echo -e "ERROR\t the server is already running (pid=$pid), there is no need to execute start.sh again." + exit 9; +fi + +make_logs_dir + +echo "using jdk[$JAVA]" >> ${DEMO_LOG_HOME}/demo_tcp_sub_broadcast.out + + +DEMO_MAIN=org.apache.eventmesh.tcp.demo.AsyncSubscribeBroadcast +if [ $DOCKER ] +then + $JAVA $JAVA_OPT -classpath ${DEMO_HOME}/conf:${DEMO_HOME}/apps/*:${DEMO_HOME}/lib/* $DEMO_MAIN >> ${DEMO_LOG_HOME}/demo_tcp_sub_broadcast.out +else + $JAVA $JAVA_OPT -classpath ${DEMO_HOME}/conf:${DEMO_HOME}/apps/*:${DEMO_HOME}/lib/* $DEMO_MAIN >> ${DEMO_LOG_HOME}/demo_tcp_sub_broadcast.out 2>&1 & +echo $!>pid_tcp_sub_broadcast.file +fi +exit 0 diff --git a/eventmesh-test/build.gradle b/eventmesh-test/build.gradle new file mode 100644 index 0000000000..d433a5bbad --- /dev/null +++ b/eventmesh-test/build.gradle @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +//group 'org.apache.eventmesh' +//version '1.2.0-SNAPSHOT' + + +List spring_framework = [ + "org.springframework.boot:spring-boot-starter-web:2.1.6.RELEASE" +] + +dependencies { +// compile log4j2, sl4j +// testCompile log4j2, sl4j + implementation project(":eventmesh-sdk-java"),project(":eventmesh-connector-api"),project(":eventmesh-common"),spring_framework + //Spring模块 + testImplementation project(":eventmesh-sdk-java"),project(":eventmesh-connector-api"),project(":eventmesh-common"),spring_framework + +} + +configurations.all { + exclude group: "ch.qos.logback" + exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging' +} + +task testdist(dependsOn: ['jar']) { + + doFirst {BasicTest + new File(projectDir.getPath(), 'dist/bin').mkdirs() + new File(projectDir.getPath(), 'dist/apps').mkdirs() + new File(projectDir.getPath(), 'dist/conf').mkdirs() + new File(projectDir.getPath(), 'dist/lib').mkdirs() + } + + doLast { + copy { + into('dist/apps/') + from project.jar.getArchivePath() + } + copy { + into 'dist/lib' + from project.configurations.runtime + } + + copy { + into 'dist/bin' + from '../eventmesh-test/bin' + } + + copy { + into 'dist/conf' + from 'conf' + } + } +} + +task testtar(type: Tar) { + extension = 'tar.gz' + compression = Compression.GZIP + archiveName = project.name + '_' + project.version + '.' + extension + destinationDir = new File(projectDir.getPath(), 'build') + into('/') { + from 'dist' + } +} + +task packageTestSources(type: Jar) { + from project.sourceSets.main.allSource + classifier = 'sources' // either here or in artifacts block +} + +artifacts { + archives jar + archives packageJavadoc + archives packageSources +} \ No newline at end of file diff --git a/eventmesh-test/conf/application.properties b/eventmesh-test/conf/application.properties new file mode 100644 index 0000000000..8289cb6b01 --- /dev/null +++ b/eventmesh-test/conf/application.properties @@ -0,0 +1,20 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +server.port=8088 +eventmesh.ip=127.0.0.1 +eventmesh.http.port=10105 +eventmesh.tcp.port=10000 \ No newline at end of file diff --git a/eventmesh-test/conf/log4j2.xml b/eventmesh-test/conf/log4j2.xml new file mode 100644 index 0000000000..87bd36cde3 --- /dev/null +++ b/eventmesh-test/conf/log4j2.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/eventmesh-test/conf/server.env b/eventmesh-test/conf/server.env new file mode 100644 index 0000000000..3bef8c5fcf --- /dev/null +++ b/eventmesh-test/conf/server.env @@ -0,0 +1,17 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +APP_START_JVM_OPTION:::-server -Xms64M -Xmx128M -Xmn64m -XX:SurvivorRatio=4 -Duser.language=zh diff --git a/eventmesh-test/gradle.properties b/eventmesh-test/gradle.properties new file mode 100644 index 0000000000..6446ff31f4 --- /dev/null +++ b/eventmesh-test/gradle.properties @@ -0,0 +1,20 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +group=org.apache.eventmesh +version=1.2.0-release +mavenUserName= +mavenPassword= diff --git a/eventmesh-test/src/main/java/org/apache/eventmesh/http/demo/AsyncPublishInstance.java b/eventmesh-test/src/main/java/org/apache/eventmesh/http/demo/AsyncPublishInstance.java new file mode 100644 index 0000000000..c58c7bf9c0 --- /dev/null +++ b/eventmesh-test/src/main/java/org/apache/eventmesh/http/demo/AsyncPublishInstance.java @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.http.demo; + +import java.util.Properties; + +import org.apache.commons.lang3.RandomStringUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.client.http.conf.LiteClientConfig; +import org.apache.eventmesh.client.http.producer.LiteProducer; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.IPUtil; +import org.apache.eventmesh.common.LiteMessage; +import org.apache.eventmesh.common.ThreadUtil; +import org.apache.eventmesh.util.Utils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AsyncPublishInstance { + + public static Logger logger = LoggerFactory.getLogger(AsyncPublishInstance.class); + + // This messageSize is also used in SubService.java (Subscriber) + public static int messageSize = 5; + + public static void main(String[] args) throws Exception { + Properties properties = Utils.readPropertiesFile("application.properties"); + final String eventMeshIp = properties.getProperty("eventmesh.ip"); + final String eventMeshHttpPort = properties.getProperty("eventmesh.http.port"); + + LiteProducer liteProducer = null; + try { +// String eventMeshIPPort = args[0]; + String eventMeshIPPort = eventMeshIp + ":" + eventMeshHttpPort; +// final String topic = args[1]; + final String topic = "TEST-TOPIC-HTTP-ASYNC"; + if (StringUtils.isBlank(eventMeshIPPort)) { + // if has multi value, can config as: 127.0.0.1:10105;127.0.0.2:10105 + eventMeshIPPort = "127.0.0.1:10105"; + } + + LiteClientConfig eventMeshClientConfig = new LiteClientConfig(); + eventMeshClientConfig.setLiteEventMeshAddr(eventMeshIPPort) + .setProducerGroup("EventMeshTest-producerGroup") + .setEnv("env") + .setIdc("idc") + .setIp(IPUtil.getLocalAddress()) + .setSys("1234") + .setPid(String.valueOf(ThreadUtil.getPID())); + + liteProducer = new LiteProducer(eventMeshClientConfig); + liteProducer.start(); + for (int i = 0; i < messageSize; i++) { + LiteMessage liteMessage = new LiteMessage(); + liteMessage.setBizSeqNo(RandomStringUtils.randomNumeric(30)) +// .setContent("contentStr with special protocal") + .setContent("testPublishMessage") + .setTopic(topic) + .setUniqueId(RandomStringUtils.randomNumeric(30)) + .addProp(Constants.EVENTMESH_MESSAGE_CONST_TTL, String.valueOf(4 * 1000)); + + boolean flag = liteProducer.publish(liteMessage); + Thread.sleep(1000); + logger.info("publish result , {}", flag); + } + } catch (Exception e) { + logger.warn("publish msg failed", e); + } + + try { + Thread.sleep(30000); + if (liteProducer != null) { + liteProducer.shutdown(); + } + } catch (Exception e1) { + logger.warn("producer shutdown exception", e1); + } + } +} diff --git a/eventmesh-test/src/main/java/org/apache/eventmesh/http/demo/AsyncSyncRequestInstance.java b/eventmesh-test/src/main/java/org/apache/eventmesh/http/demo/AsyncSyncRequestInstance.java new file mode 100644 index 0000000000..cc8a536011 --- /dev/null +++ b/eventmesh-test/src/main/java/org/apache/eventmesh/http/demo/AsyncSyncRequestInstance.java @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.http.demo; + +import java.util.Properties; + +import org.apache.commons.lang3.RandomStringUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.client.http.conf.LiteClientConfig; +import org.apache.eventmesh.client.http.producer.LiteProducer; +import org.apache.eventmesh.client.http.producer.RRCallback; +import org.apache.eventmesh.common.IPUtil; +import org.apache.eventmesh.common.LiteMessage; +import org.apache.eventmesh.common.ThreadUtil; +import org.apache.eventmesh.util.Utils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AsyncSyncRequestInstance { + + public static Logger logger = LoggerFactory.getLogger(AsyncSyncRequestInstance.class); + + public static void main(String[] args) throws Exception { + + Properties properties = Utils.readPropertiesFile("application.properties"); + final String eventMeshIp = properties.getProperty("eventmesh.ip"); + final String eventMeshHttpPort = properties.getProperty("eventmesh.http.port"); + + LiteProducer liteProducer = null; + try { +// String eventMeshIPPort = args[0]; + String eventMeshIPPort = eventMeshIp + ":" + eventMeshHttpPort; +// final String topic = args[1]; + final String topic = "TEST-TOPIC-TCP-ASYNC"; + if (StringUtils.isBlank(eventMeshIPPort)) { + // if has multi value, can config as: 127.0.0.1:10105;127.0.0.2:10105 + eventMeshIPPort = "127.0.0.1:10105"; + } + + LiteClientConfig eventMeshClientConfig = new LiteClientConfig(); + eventMeshClientConfig.setLiteEventMeshAddr(eventMeshIPPort) + .setProducerGroup("EventMeshTest-producerGroup") + .setEnv("env") + .setIdc("idc") + .setIp(IPUtil.getLocalAddress()) + .setSys("1234") + .setPid(String.valueOf(ThreadUtil.getPID())); + + liteProducer = new LiteProducer(eventMeshClientConfig); + + final long startTime = System.currentTimeMillis(); + final LiteMessage liteMessage = new LiteMessage(); + liteMessage.setBizSeqNo(RandomStringUtils.randomNumeric(30)) + .setContent("testAsyncMessage") + .setTopic(topic) + .setUniqueId(RandomStringUtils.randomNumeric(30)); + + liteProducer.request(liteMessage, new RRCallback() { + @Override + public void onSuccess(LiteMessage o) { + logger.debug("sendmsg : {}, return : {}, cost:{}ms", liteMessage.getContent(), o.getContent(), System.currentTimeMillis() - startTime); + } + + @Override + public void onException(Throwable e) { + logger.debug("sendmsg failed", e); + } + }, 3000); + + Thread.sleep(2000); + } catch (Exception e) { + logger.warn("async send msg failed", e); + } + + try { + Thread.sleep(30000); + if (liteProducer != null) { + liteProducer.shutdown(); + } + } catch (Exception e1) { + logger.warn("producer shutdown exception", e1); + } + } +} diff --git a/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/http/demo/SyncRequestInstance.java b/eventmesh-test/src/main/java/org/apache/eventmesh/http/demo/SyncRequestInstance.java similarity index 51% rename from eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/http/demo/SyncRequestInstance.java rename to eventmesh-test/src/main/java/org/apache/eventmesh/http/demo/SyncRequestInstance.java index 7a06f809bf..9d3af8ffe4 100644 --- a/eventmesh-sdk-java/src/test/java/com/webank/eventmesh/client/http/demo/SyncRequestInstance.java +++ b/eventmesh-test/src/main/java/org/apache/eventmesh/http/demo/SyncRequestInstance.java @@ -1,20 +1,32 @@ -package com.webank.eventmesh.client.http.demo; -import com.webank.eventmesh.client.http.conf.LiteClientConfig; -import com.webank.eventmesh.client.http.producer.LiteProducer; -import com.webank.eventmesh.common.IPUtil; -import com.webank.eventmesh.common.LiteMessage; -import com.webank.eventmesh.common.ThreadPoolFactory; -import com.webank.eventmesh.common.ThreadUtil; +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.http.demo; + import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.client.http.conf.LiteClientConfig; +import org.apache.eventmesh.client.http.producer.LiteProducer; +import org.apache.eventmesh.common.IPUtil; +import org.apache.eventmesh.common.LiteMessage; +import org.apache.eventmesh.common.ThreadUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.LinkedList; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicLong; - public class SyncRequestInstance { public static Logger logger = LoggerFactory.getLogger(SyncRequestInstance.class); @@ -23,25 +35,25 @@ public static void main(String[] args) throws Exception { LiteProducer liteProducer = null; try { - String proxyIPPort = args[0]; + String eventMeshIPPort = args[0]; final String topic = args[1]; - if (StringUtils.isBlank(proxyIPPort)) { + if (StringUtils.isBlank(eventMeshIPPort)) { // if has multi value, can config as: 127.0.0.1:10105;127.0.0.2:10105 - proxyIPPort = "127.0.0.1:10105"; + eventMeshIPPort = "127.0.0.1:10105"; } - LiteClientConfig weMQProxyClientConfig = new LiteClientConfig(); - weMQProxyClientConfig.setLiteProxyAddr(proxyIPPort) + LiteClientConfig eventMeshClientConfig = new LiteClientConfig(); + eventMeshClientConfig.setLiteEventMeshAddr(eventMeshIPPort) + .setProducerGroup("EventMeshTest-producerGroup") .setEnv("env") .setIdc("idc") - .setDcn("dcn") .setIp(IPUtil.getLocalAddress()) .setSys("1234") .setPid(String.valueOf(ThreadUtil.getPID())); - liteProducer = new LiteProducer(weMQProxyClientConfig); + liteProducer = new LiteProducer(eventMeshClientConfig); liteProducer.start(); long startTime = System.currentTimeMillis(); @@ -59,12 +71,12 @@ public static void main(String[] args) throws Exception { logger.warn("send msg failed", e); } - try{ + try { Thread.sleep(30000); - if(liteProducer != null){ + if (liteProducer != null) { liteProducer.shutdown(); } - }catch (Exception e1){ + } catch (Exception e1) { logger.warn("producer shutdown exception", e1); } } diff --git a/eventmesh-test/src/main/java/org/apache/eventmesh/http/demo/sub/SpringBootDemoApplication.java b/eventmesh-test/src/main/java/org/apache/eventmesh/http/demo/sub/SpringBootDemoApplication.java new file mode 100644 index 0000000000..931e608a27 --- /dev/null +++ b/eventmesh-test/src/main/java/org/apache/eventmesh/http/demo/sub/SpringBootDemoApplication.java @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.http.demo.sub; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; + +@SpringBootApplication +@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class}) +public class SpringBootDemoApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootDemoApplication.class, args); + } +} diff --git a/eventmesh-test/src/main/java/org/apache/eventmesh/http/demo/sub/controller/SubController.java b/eventmesh-test/src/main/java/org/apache/eventmesh/http/demo/sub/controller/SubController.java new file mode 100644 index 0000000000..92ca09d700 --- /dev/null +++ b/eventmesh-test/src/main/java/org/apache/eventmesh/http/demo/sub/controller/SubController.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.http.demo.sub.controller; + +import com.alibaba.fastjson.JSONObject; + +import org.apache.eventmesh.http.demo.sub.service.SubService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + + +@RestController +@RequestMapping("/sub") +public class SubController { + + public static Logger logger = LoggerFactory.getLogger(SubController.class); + + @Autowired + private SubService subService; + + @RequestMapping(value = "/test", method = RequestMethod.POST) + public String subTest(@RequestBody String message) { + logger.info("=======receive message======= {}", JSONObject.toJSONString(message)); + subService.consumeMessage(message); + + JSONObject result = new JSONObject(); + result.put("retCode", 1); + return result.toJSONString(); + } + +} diff --git a/eventmesh-test/src/main/java/org/apache/eventmesh/http/demo/sub/service/SubService.java b/eventmesh-test/src/main/java/org/apache/eventmesh/http/demo/sub/service/SubService.java new file mode 100644 index 0000000000..570b24d7e6 --- /dev/null +++ b/eventmesh-test/src/main/java/org/apache/eventmesh/http/demo/sub/service/SubService.java @@ -0,0 +1,130 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.eventmesh.http.demo.sub.service; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Properties; +import java.util.concurrent.CountDownLatch; +import org.apache.commons.lang3.StringUtils; +import org.apache.eventmesh.client.http.conf.LiteClientConfig; +import org.apache.eventmesh.client.http.consumer.LiteConsumer; +import org.apache.eventmesh.common.EventMeshException; +import org.apache.eventmesh.common.IPUtil; +import org.apache.eventmesh.common.ThreadUtil; +import org.apache.eventmesh.common.protocol.SubcriptionType; +import org.apache.eventmesh.common.protocol.SubscriptionItem; +import org.apache.eventmesh.common.protocol.SubscriptionMode; +import org.apache.eventmesh.http.demo.AsyncPublishInstance; +import org.apache.eventmesh.util.Utils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.stereotype.Component; +import javax.annotation.PreDestroy; + +@Component +public class SubService implements InitializingBean { + + public static Logger logger = LoggerFactory.getLogger(SubService.class); + + private LiteConsumer liteConsumer; + + private String eventMeshIPPort = ""; + + final Properties properties = Utils.readPropertiesFile("application.properties"); + + final List topicList = Arrays.asList(new SubscriptionItem("TEST-TOPIC-HTTP-ASYNC", SubscriptionMode.CLUSTERING, SubcriptionType.ASYNC)); + final String localIp = IPUtil.getLocalAddress(); + final String localPort = properties.getProperty("server.port"); + final String eventMeshIp = properties.getProperty("eventmesh.ip"); + final String eventMeshHttpPort = properties.getProperty("eventmesh.http.port"); + final String url = "http://" + localIp + ":" + localPort + "/sub/test"; + final String env = "P"; + final String idc = "FT"; + final String subsys = "1234"; + + // CountDownLatch size is the same as messageSize in AsyncPublishInstance.java (Publisher) + private CountDownLatch countDownLatch = new CountDownLatch(AsyncPublishInstance.messageSize); + + @Override + public void afterPropertiesSet() throws Exception { + + if (StringUtils.isBlank(eventMeshIPPort)) { + // if has multi value, can config as: 127.0.0.1:10105;127.0.0.2:10105 + eventMeshIPPort = eventMeshIp + ":" + eventMeshHttpPort; + } + LiteClientConfig eventMeshClientConfig = new LiteClientConfig(); + eventMeshClientConfig.setLiteEventMeshAddr(eventMeshIPPort) + .setConsumerGroup("EventMeshTest-consumerGroup") + .setEnv(env) + .setIdc(idc) + .setIp(IPUtil.getLocalAddress()) + .setSys(subsys) + .setPid(String.valueOf(ThreadUtil.getPID())); + + liteConsumer = new LiteConsumer(eventMeshClientConfig); + liteConsumer.start(); + liteConsumer.heartBeat(topicList, url); + liteConsumer.subscribe(topicList, url); + + // Wait for all messaged to be consumed + Thread stopThread = new Thread(() -> { + try { + countDownLatch.await(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + logger.info("stopThread start...."); + System.exit(0); + }); + stopThread.start(); + } + + @PreDestroy + public void cleanup() { + logger.info("start destory ...."); + try { + List unSubList = new ArrayList<>(); + for (SubscriptionItem item:topicList) { + unSubList.add(item.getTopic()); + } + liteConsumer.unsubscribe(unSubList, url); + } catch (Exception e) { + e.printStackTrace(); + } + try { + liteConsumer.shutdown(); + } catch (Exception e) { + e.printStackTrace(); + } + logger.info("end destory."); + } + + /** + * Count the message already consumed + */ + public void consumeMessage(String msg) { + logger.info("consume message {}", msg); + countDownLatch.countDown(); + logger.info("remaining number of messages to be consumed {}", countDownLatch.getCount()); + } +} diff --git a/eventmesh-test/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestCaseTopicSet.java b/eventmesh-test/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestCaseTopicSet.java new file mode 100644 index 0000000000..dd7908cb55 --- /dev/null +++ b/eventmesh-test/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestCaseTopicSet.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.tcp.common; + +/** + * Testcase set + */ +public class EventMeshTestCaseTopicSet { + + // public static final String TOPIC_PRX_WQ2ClientBroadCast = "topic-broadcast-test"; + public static final String TOPIC_PRX_WQ2ClientBroadCast = "TEST-TOPIC-TCP-BROADCAST"; + + // public static final String TOPIC_PRX_SyncSubscribeTest = "topic-sync-test"; + public static final String TOPIC_PRX_SyncSubscribeTest = "TEST-TOPIC-TCP-SYNC"; + + // public static final String TOPIC_PRX_WQ2ClientUniCast = "topic-async-test"; + public static final String TOPIC_PRX_WQ2ClientUniCast = "TEST-TOPIC-TCP-ASYNC"; + +} diff --git a/eventmesh-test/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java b/eventmesh-test/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java new file mode 100644 index 0000000000..88f5016e9d --- /dev/null +++ b/eventmesh-test/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java @@ -0,0 +1,154 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.tcp.common; + +import static org.apache.eventmesh.common.protocol.tcp.Command.RESPONSE_TO_SERVER; +import static org.apache.eventmesh.tcp.common.EventMeshTestCaseTopicSet.TOPIC_PRX_SyncSubscribeTest; +import static org.apache.eventmesh.tcp.common.EventMeshTestCaseTopicSet.TOPIC_PRX_WQ2ClientBroadCast; +import static org.apache.eventmesh.tcp.common.EventMeshTestCaseTopicSet.TOPIC_PRX_WQ2ClientUniCast; + +import java.util.concurrent.ThreadLocalRandom; + +import org.apache.eventmesh.client.tcp.common.EventMeshCommon; +import org.apache.eventmesh.common.protocol.tcp.Command; +import org.apache.eventmesh.common.protocol.tcp.EventMeshMessage; +import org.apache.eventmesh.common.protocol.tcp.Header; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; + +public class EventMeshTestUtils { + private static final int seqLength = 10; + + public static UserAgent generateClient1() { + UserAgent user = new UserAgent(); + user.setEnv("test"); + user.setHost("127.0.0.1"); + user.setPassword(generateRandomString(8)); + user.setUsername("PU4283"); + user.setProducerGroup("EventmeshTest-ProducerGroup"); + user.setConsumerGroup("EventmeshTest-ConsumerGroup"); + user.setPath("/data/app/umg_proxy"); + user.setPort(8362); + user.setSubsystem("5023"); + user.setPid(32893); + user.setVersion("2.0.11"); + user.setIdc("FT"); + return user; + } + + public static UserAgent generateClient2() { + UserAgent user = new UserAgent(); + user.setEnv("test"); + user.setHost("127.0.0.1"); + user.setPassword(generateRandomString(8)); + user.setUsername("PU4283"); + user.setConsumerGroup("EventmeshTest-ConsumerGroup"); + user.setProducerGroup("EventmeshTest-ProducerGroup"); + user.setPath("/data/app/umg_proxy"); + user.setPort(9362); + user.setSubsystem("5017"); + user.setPid(42893); + user.setVersion("2.0.11"); + user.setIdc("FT"); + return user; + } + + public static Package syncRR() { + Package msg = new Package(); + msg.setHeader(new Header(Command.REQUEST_TO_SERVER, 0, null, generateRandomString(seqLength))); + msg.setBody(generateSyncRRMqMsg()); + return msg; + } + + public static Package asyncRR() { + Package msg = new Package(); + msg.setHeader(new Header(Command.REQUEST_TO_SERVER, 0, null, generateRandomString(seqLength))); + msg.setBody(generateAsyncRRMqMsg()); + return msg; + } + + public static Package asyncMessage() { + Package msg = new Package(); + msg.setHeader(new Header(Command.ASYNC_MESSAGE_TO_SERVER, 0, null, generateRandomString(seqLength))); + msg.setBody(generateAsyncEventMqMsg()); + return msg; + } + + public static Package broadcastMessage() { + Package msg = new Package(); + msg.setHeader(new Header(Command.BROADCAST_MESSAGE_TO_SERVER, 0, null, generateRandomString(seqLength))); + msg.setBody(generateBroadcastMqMsg()); + return msg; + } + + public static Package rrResponse(Package request) { + Package msg = new Package(); + msg.setHeader(new Header(RESPONSE_TO_SERVER, 0, null, generateRandomString(seqLength))); + msg.setBody(request.getBody()); + return msg; + } + + private static EventMeshMessage generateSyncRRMqMsg() { + EventMeshMessage mqMsg = new EventMeshMessage(); + mqMsg.setTopic(TOPIC_PRX_SyncSubscribeTest); + mqMsg.getProperties().put("msgType", "persistent"); + mqMsg.getProperties().put("TTL", "300000"); + mqMsg.getProperties().put("KEYS", generateRandomString(16)); + mqMsg.setBody("testSyncRR"); + return mqMsg; + } + + + private static EventMeshMessage generateAsyncRRMqMsg() { + EventMeshMessage mqMsg = new EventMeshMessage(); + mqMsg.setTopic(TOPIC_PRX_SyncSubscribeTest); + mqMsg.getProperties().put("REPLY_TO", "10.36.0.109@ProducerGroup-producerPool-9-access#V1_4_0#CI"); + mqMsg.getProperties().put("TTL", "300000"); + mqMsg.getProperties().put("PROPERTY_MESSAGE_REPLY_TO", "notnull"); + mqMsg.setBody("testAsyncRR"); + return mqMsg; + } + + private static EventMeshMessage generateAsyncEventMqMsg() { + EventMeshMessage mqMsg = new EventMeshMessage(); + mqMsg.setTopic(TOPIC_PRX_WQ2ClientUniCast); + mqMsg.getProperties().put("REPLY_TO", "10.36.0.109@ProducerGroup-producerPool-9-access#V1_4_0#CI"); + mqMsg.getProperties().put("TTL", "30000"); + mqMsg.getProperties().put("PROPERTY_MESSAGE_REPLY_TO", "notnull"); + mqMsg.setBody("testAsyncMessage"); + return mqMsg; + } + + private static EventMeshMessage generateBroadcastMqMsg() { + EventMeshMessage mqMsg = new EventMeshMessage(); + mqMsg.setTopic(TOPIC_PRX_WQ2ClientBroadCast); + mqMsg.getProperties().put("REPLY_TO", "10.36.0.109@ProducerGroup-producerPool-9-access#V1_4_0#CI"); + mqMsg.getProperties().put("TTL", "30000"); + mqMsg.getProperties().put("PROPERTY_MESSAGE_REPLY_TO", "notnull"); + mqMsg.setBody("testAsyncMessage"); + return mqMsg; + } + + private static String generateRandomString(int length) { + StringBuilder builder = new StringBuilder(length); + for (int i = 0; i < length; i++) { + builder.append((char) ThreadLocalRandom.current().nextInt(48, 57)); + } + return builder.toString(); + } +} diff --git a/eventmesh-test/src/main/java/org/apache/eventmesh/tcp/demo/AsyncPublish.java b/eventmesh-test/src/main/java/org/apache/eventmesh/tcp/demo/AsyncPublish.java new file mode 100644 index 0000000000..73cc045c64 --- /dev/null +++ b/eventmesh-test/src/main/java/org/apache/eventmesh/tcp/demo/AsyncPublish.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.tcp.demo; + +import java.util.Properties; + +import org.apache.eventmesh.client.tcp.EventMeshClient; +import org.apache.eventmesh.client.tcp.common.EventMeshCommon; +import org.apache.eventmesh.client.tcp.impl.DefaultEventMeshClient; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import org.apache.eventmesh.tcp.common.EventMeshTestUtils; +import org.apache.eventmesh.util.Utils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AsyncPublish { + + public static Logger logger = LoggerFactory.getLogger(AsyncPublish.class); + + private static EventMeshClient client; + + public static AsyncPublish handler = new AsyncPublish(); + + + public static void main(String[] agrs) throws Exception { + Properties properties = Utils.readPropertiesFile("application.properties"); + final String eventMeshIp = properties.getProperty("eventmesh.ip"); + final int eventMeshTcpPort = Integer.parseInt(properties.getProperty("eventmesh.tcp.port")); + try { + UserAgent userAgent = EventMeshTestUtils.generateClient1(); + client = new DefaultEventMeshClient(eventMeshIp, eventMeshTcpPort, userAgent); + client.init(); + client.heartbeat(); + + for (int i = 0; i < 5; i++) { + Package asyncMsg = EventMeshTestUtils.asyncMessage(); + logger.info("begin send async msg[{}]==================={}", i, asyncMsg); + client.publish(asyncMsg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); + + Thread.sleep(1000); + } + + Thread.sleep(2000); + //退出,销毁资源 +// client.close(); + } catch (Exception e) { + logger.warn("AsyncPublish failed", e); + } + } +} diff --git a/eventmesh-test/src/main/java/org/apache/eventmesh/tcp/demo/AsyncPublishBroadcast.java b/eventmesh-test/src/main/java/org/apache/eventmesh/tcp/demo/AsyncPublishBroadcast.java new file mode 100644 index 0000000000..923c04a90b --- /dev/null +++ b/eventmesh-test/src/main/java/org/apache/eventmesh/tcp/demo/AsyncPublishBroadcast.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.tcp.demo; + +import java.util.Properties; + +import org.apache.eventmesh.client.tcp.EventMeshClient; +import org.apache.eventmesh.client.tcp.common.EventMeshCommon; +import org.apache.eventmesh.client.tcp.impl.DefaultEventMeshClient; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import org.apache.eventmesh.tcp.common.EventMeshTestUtils; +import org.apache.eventmesh.util.Utils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AsyncPublishBroadcast { + + public static Logger logger = LoggerFactory.getLogger(AsyncPublishBroadcast.class); + + private static EventMeshClient client; + + public static void main(String[] agrs) throws Exception { + Properties properties = Utils.readPropertiesFile("application.properties"); + final String eventMeshIp = properties.getProperty("eventmesh.ip"); + final int eventMeshTcpPort = Integer.parseInt(properties.getProperty("eventmesh.tcp.port")); + try { + UserAgent userAgent = EventMeshTestUtils.generateClient1(); + client = new DefaultEventMeshClient(eventMeshIp, eventMeshTcpPort, userAgent); + client.init(); + client.heartbeat(); + + Package broadcastMsg = EventMeshTestUtils.broadcastMessage(); + logger.info("begin send broadcast msg============={}", broadcastMsg); + client.broadcast(broadcastMsg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); + + Thread.sleep(2000); + //退出,销毁资源 +// client.close(); + } catch (Exception e) { + logger.warn("AsyncPublishBroadcast failed", e); + } + } +} diff --git a/eventmesh-test/src/main/java/org/apache/eventmesh/tcp/demo/AsyncSubscribe.java b/eventmesh-test/src/main/java/org/apache/eventmesh/tcp/demo/AsyncSubscribe.java new file mode 100644 index 0000000000..e1db99e909 --- /dev/null +++ b/eventmesh-test/src/main/java/org/apache/eventmesh/tcp/demo/AsyncSubscribe.java @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.tcp.demo; + +import java.util.Properties; + +import io.netty.channel.ChannelHandlerContext; + +import org.apache.eventmesh.client.tcp.EventMeshClient; +import org.apache.eventmesh.client.tcp.common.ReceiveMsgHook; +import org.apache.eventmesh.client.tcp.impl.DefaultEventMeshClient; +import org.apache.eventmesh.common.protocol.SubcriptionType; +import org.apache.eventmesh.common.protocol.tcp.EventMeshMessage; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.common.protocol.SubscriptionMode; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import org.apache.eventmesh.tcp.common.EventMeshTestUtils; +import org.apache.eventmesh.util.Utils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AsyncSubscribe implements ReceiveMsgHook { + + public static Logger logger = LoggerFactory.getLogger(AsyncSubscribe.class); + + private static EventMeshClient client; + + public static AsyncSubscribe handler = new AsyncSubscribe(); + + public static void main(String[] agrs) throws Exception { + Properties properties = Utils.readPropertiesFile("application.properties"); + final String eventMeshIp = properties.getProperty("eventmesh.ip"); + final int eventMeshTcpPort = Integer.parseInt(properties.getProperty("eventmesh.tcp.port")); + try { + UserAgent userAgent = EventMeshTestUtils.generateClient2(); + client = new DefaultEventMeshClient(eventMeshIp, eventMeshTcpPort, userAgent); + client.init(); + client.heartbeat(); + + client.subscribe("TEST-TOPIC-TCP-ASYNC", SubscriptionMode.CLUSTERING, SubcriptionType.ASYNC); + client.registerSubBusiHandler(handler); + + client.listen(); + + //client.unsubscribe(); + + //退出,销毁资源 +// client.close(); + } catch (Exception e) { + logger.warn("AsyncSubscribe failed", e); + } + } + + @Override + public void handle(Package msg, ChannelHandlerContext ctx) { + EventMeshMessage eventMeshMessage = (EventMeshMessage) msg.getBody(); + logger.info("receive async msg====================={}", eventMeshMessage); + } +} diff --git a/eventmesh-test/src/main/java/org/apache/eventmesh/tcp/demo/AsyncSubscribeBroadcast.java b/eventmesh-test/src/main/java/org/apache/eventmesh/tcp/demo/AsyncSubscribeBroadcast.java new file mode 100644 index 0000000000..38936f99c8 --- /dev/null +++ b/eventmesh-test/src/main/java/org/apache/eventmesh/tcp/demo/AsyncSubscribeBroadcast.java @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.tcp.demo; + +import java.util.Properties; + +import io.netty.channel.ChannelHandlerContext; + +import org.apache.eventmesh.client.tcp.EventMeshClient; +import org.apache.eventmesh.client.tcp.common.ReceiveMsgHook; +import org.apache.eventmesh.client.tcp.impl.DefaultEventMeshClient; +import org.apache.eventmesh.common.protocol.SubcriptionType; +import org.apache.eventmesh.common.protocol.tcp.EventMeshMessage; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.common.protocol.SubscriptionMode; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import org.apache.eventmesh.tcp.common.EventMeshTestUtils; +import org.apache.eventmesh.util.Utils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AsyncSubscribeBroadcast implements ReceiveMsgHook { + + public static Logger logger = LoggerFactory.getLogger(AsyncSubscribeBroadcast.class); + + private static EventMeshClient client; + + public static AsyncSubscribeBroadcast handler = new AsyncSubscribeBroadcast(); + + public static void main(String[] agrs) throws Exception { + Properties properties = Utils.readPropertiesFile("application.properties"); + final String eventMeshIp = properties.getProperty("eventmesh.ip"); + final int eventMeshTcpPort = Integer.parseInt(properties.getProperty("eventmesh.tcp.port")); + try { + UserAgent userAgent = EventMeshTestUtils.generateClient2(); + client = new DefaultEventMeshClient(eventMeshIp, eventMeshTcpPort, userAgent); + client.init(); + client.heartbeat(); + + client.subscribe("TEST-TOPIC-TCP-BROADCAST", SubscriptionMode.BROADCASTING, SubcriptionType.ASYNC); + client.registerSubBusiHandler(handler); + + client.listen(); + + //client.unsubscribe(); + + //退出,销毁资源 +// client.close(); + } catch (Exception e) { + logger.warn("AsyncSubscribeBroadcast failed", e); + } + } + + @Override + public void handle(Package msg, ChannelHandlerContext ctx) { + EventMeshMessage eventMeshMessage = (EventMeshMessage) msg.getBody(); + logger.info("receive broadcast msg==============={}", eventMeshMessage); + } +} diff --git a/eventmesh-test/src/main/java/org/apache/eventmesh/tcp/demo/SyncRequest.java b/eventmesh-test/src/main/java/org/apache/eventmesh/tcp/demo/SyncRequest.java new file mode 100644 index 0000000000..d25f29dfbb --- /dev/null +++ b/eventmesh-test/src/main/java/org/apache/eventmesh/tcp/demo/SyncRequest.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.tcp.demo; + +import org.apache.eventmesh.client.tcp.EventMeshClient; +import org.apache.eventmesh.client.tcp.common.EventMeshCommon; +import org.apache.eventmesh.client.tcp.impl.DefaultEventMeshClient; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import org.apache.eventmesh.tcp.common.EventMeshTestUtils; +import org.apache.eventmesh.common.protocol.tcp.Package; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SyncRequest { + + public static Logger logger = LoggerFactory.getLogger(SyncRequest.class); + + private static EventMeshClient client; + + public static void main(String[] agrs) throws Exception { + try { + UserAgent userAgent = EventMeshTestUtils.generateClient1(); + client = new DefaultEventMeshClient("127.0.0.1", 10000, userAgent); + client.init(); + client.heartbeat(); + + Package rrMsg = EventMeshTestUtils.syncRR(); + logger.info("begin send rr msg=================={}", rrMsg); + Package response = client.rr(rrMsg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); + logger.info("receive rr reply==================={}", response); + + //退出,销毁资源 +// client.close(); + } catch (Exception e) { + logger.warn("SyncRequest failed", e); + } + } +} diff --git a/eventmesh-test/src/main/java/org/apache/eventmesh/tcp/demo/SyncResponse.java b/eventmesh-test/src/main/java/org/apache/eventmesh/tcp/demo/SyncResponse.java new file mode 100644 index 0000000000..d87d4f465e --- /dev/null +++ b/eventmesh-test/src/main/java/org/apache/eventmesh/tcp/demo/SyncResponse.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.tcp.demo; + +import io.netty.channel.ChannelHandlerContext; + +import org.apache.eventmesh.client.tcp.EventMeshClient; +import org.apache.eventmesh.client.tcp.common.ReceiveMsgHook; +import org.apache.eventmesh.client.tcp.impl.DefaultEventMeshClient; +import org.apache.eventmesh.common.protocol.SubcriptionType; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.common.protocol.SubscriptionMode; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import org.apache.eventmesh.tcp.common.EventMeshTestUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SyncResponse implements ReceiveMsgHook { + + public static Logger logger = LoggerFactory.getLogger(SyncResponse.class); + + private static EventMeshClient client; + + + public static SyncResponse handler = new SyncResponse(); + + public static void main(String[] agrs) throws Exception { + try { + UserAgent userAgent = EventMeshTestUtils.generateClient2(); + client = new DefaultEventMeshClient("127.0.0.1", 10000, userAgent); + client.init(); + client.heartbeat(); + + client.subscribe("TEST-TOPIC-TCP-SYNC", SubscriptionMode.CLUSTERING, SubcriptionType.SYNC); + //同步RR消息 + client.registerSubBusiHandler(handler); + + client.listen(); + + //client.unsubscribe(); + + //退出,销毁资源 +// client.close(); + } catch (Exception e) { + logger.warn("SyncResponse failed", e); + } + } + + @Override + public void handle(Package msg, ChannelHandlerContext ctx) { + logger.info("receive sync rr msg================{}", msg); + Package pkg = EventMeshTestUtils.rrResponse(msg); + ctx.writeAndFlush(pkg); + } +} diff --git a/eventmesh-test/src/main/java/org/apache/eventmesh/util/Utils.java b/eventmesh-test/src/main/java/org/apache/eventmesh/util/Utils.java new file mode 100644 index 0000000000..c20b91b6e5 --- /dev/null +++ b/eventmesh-test/src/main/java/org/apache/eventmesh/util/Utils.java @@ -0,0 +1,108 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.util; + +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.net.UnknownHostException; +import java.util.Enumeration; +import java.util.Properties; + +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.core.io.support.PropertiesLoaderUtils; + +public class Utils { + + /** + * 获取本地IP地址 + * + * @throws SocketException + */ + public static String getLocalIP() throws UnknownHostException, SocketException { + if (isWindowsOS()) { + return InetAddress.getLocalHost().getHostAddress(); + } else { + return getLinuxLocalIp(); + } + } + + /** + * 判断操作系统是否是Windows + * + * @return + */ + public static boolean isWindowsOS() { + boolean isWindowsOS = false; + // JRE (runtime)system,not OS + String osName = System.getProperty("os.name"); + if (osName.toLowerCase().contains("windows")) { + isWindowsOS = true; + } + return isWindowsOS; + } + + /** + * 获取Linux下的IP地址 + * + * @return IP地址 + * @throws SocketException + */ + private static String getLinuxLocalIp() throws SocketException { + String ip = ""; + try { + for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) { + NetworkInterface intf = en.nextElement(); + String name = intf.getName(); + if (!name.contains("docker") && !name.contains("lo")) { + for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) { + InetAddress inetAddress = enumIpAddr.nextElement(); + if (!inetAddress.isLoopbackAddress()) { + String ipaddress = inetAddress.getHostAddress().toString(); + if (!ipaddress.contains("::") && !ipaddress.contains("0:0:") + && !ipaddress.contains("fe80")) { + ip = ipaddress; + } + } + } + } + } + } catch (SocketException ex) { + ip = "127.0.0.1"; + ex.printStackTrace(); + } + return ip; + } + + /** + * @param fileName + * @return + */ + public static Properties readPropertiesFile(String fileName) { + try { + Resource resource = new ClassPathResource(fileName); + Properties props = PropertiesLoaderUtils.loadProperties(resource); + return props; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + +} diff --git a/gradle.properties b/gradle.properties index ef43d49932..38938bbb06 100644 --- a/gradle.properties +++ b/gradle.properties @@ -14,11 +14,18 @@ # See the License for the specific language governing permissions and # limitations under the License. # -group=com.webank.eventmesh -version=1.1.0 -rocketmqVersion=4.4.0 jdk=1.8 snapshot=false -mavenUserName= -mavenPassword= -#org.gradle.java.home=C:\\Program Files\\Java\\jdk1.7.0_67 +group=org.apache.eventmesh +version=1.2.0-release +#last eight bits of public key +signing.keyId= +#passphrase for key pairs +signing.password= +#path of exporting private key +signing.secretKeyRingFile= +#apache id +apacheUserName= +#apache password +apachePassWord= +signEnabled=false diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000000..62d4c05355 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000000..1ad74a57e2 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,21 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew index fbd7c51583..7b957476e1 100755 --- a/gradlew +++ b/gradlew @@ -1,19 +1,21 @@ #!/usr/bin/env sh - -# -# Copyright 2015 the original author or authors. # -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at # -# https://www.apache.org/licenses/LICENSE-2.0 +# http://www.apache.org/licenses/LICENSE-2.0 # -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. # ############################################################################## diff --git a/gradlew.bat b/gradlew.bat index 5093609d51..192f884777 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -1,17 +1,20 @@ @rem -@rem Copyright 2015 the original author or authors. +@rem Licensed to the Apache Software Foundation (ASF) under one +@rem or more contributor license agreements. See the NOTICE file +@rem distributed with this work for additional information +@rem regarding copyright ownership. The ASF licenses this file +@rem to you under the Apache License, Version 2.0 (the +@rem "License"); you may not use this file except in compliance +@rem with the License. You may obtain a copy of the License at @rem -@rem Licensed under the Apache License, Version 2.0 (the "License"); -@rem you may not use this file except in compliance with the License. -@rem You may obtain a copy of the License at +@rem http://www.apache.org/licenses/LICENSE-2.0 @rem -@rem https://www.apache.org/licenses/LICENSE-2.0 -@rem -@rem Unless required by applicable law or agreed to in writing, software -@rem distributed under the License is distributed on an "AS IS" BASIS, -@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -@rem See the License for the specific language governing permissions and -@rem limitations under the License. +@rem Unless required by applicable law or agreed to in writing, +@rem software distributed under the License is distributed on an +@rem "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@rem KIND, either express or implied. See the License for the +@rem specific language governing permissions and limitations +@rem under the License. @rem @if "%DEBUG%" == "" @echo off diff --git a/install.sh b/install.sh index 04e66998ef..19397ac752 100644 --- a/install.sh +++ b/install.sh @@ -1,5 +1,23 @@ #!/usr/bin/env bash # +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + # -Pdev=true : snapshot version # -Pjdk=1.8 : jdk version,default 1.8 # dist : resource collect diff --git a/package.json b/package.json deleted file mode 100644 index 77057b3165..0000000000 --- a/package.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "name": "zqq", - "version": "1.0.0", - "description": "[![Build Status](https://www.travis-ci.org/WeBankFinTech/DeFiBus.svg?branch=master)](https://www.travis-ci.org/WeBankFinTech/EventMesh) [![Coverage Status](https://coveralls.io/repos/github/WeBankFinTech/DeFiBus/badge.svg?branch=master)](https://coveralls.io/github/WeBankFinTech/EventMesh?branch=master) [![GitHub release](https://img.shields.io/badge/release-download-orange.svg)](https://github.com/WeBankFinTech/EventMesh/releases) [![License](https://img.shields.io/badge/license-Apache%202-4EB1BA.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)", - "main": "index.js", - "directories": { - "doc": "docs" - }, - "scripts": { - "docs:dev": "vuepress dev docs", - "docs:build": "vuepress build docs" - }, - "keywords": [], - "author": "", - "license": "ISC", - "devDependencies": { - "vuepress": "^1.7.1" - } -} diff --git a/settings.gradle b/settings.gradle index e12f08c4e4..ea31ffdc16 100644 --- a/settings.gradle +++ b/settings.gradle @@ -17,8 +17,5 @@ rootProject.name = 'EventMesh' String jdkVersion = "${jdk}" -include 'eventmesh-emesher' -include 'eventmesh-registry' -include 'eventmesh-sdk-java' -include 'eventmesh-common' +include 'eventmesh-runtime','eventmesh-connector-rocketmq','eventmesh-sdk-java','eventmesh-common','eventmesh-connector-api','eventmesh-starter','eventmesh-test' diff --git a/style/codeStyle.xml b/style/codeStyle.xml index 9b4e561f9c..6b60186f16 100644 --- a/style/codeStyle.xml +++ b/style/codeStyle.xml @@ -15,7 +15,7 @@ ~ limitations under the License. --> - +