Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #790] Knative Connector: Initial Implementation of Producer. #1027

Merged
merged 12 commits into from
Aug 26, 2022
Merged

[ISSUE #790] Knative Connector: Initial Implementation of Producer. #1027

merged 12 commits into from
Aug 26, 2022

Conversation

pchengma
Copy link
Member

@pchengma pchengma commented Jul 16, 2022

Fixing ISSUE #790.

Motivation

Implemented and tested Producer module to publish an event to Knative (ISSUE #790).
The PRs for ISSUE #790 will be merged into knative-connector branch.

Modifications

Followed the API design style of eventmesh-connector-api and made the following modifications.

Implementation

  • Producer for publishing an event message to Knative
  • CloudEvent Reader, Writer, and Header
  • Connector
  • Unit test for Producer
  • Included eventmesh-connector-knative in setting.gradle

How to Test

Assume the cloudevents-player Knative service has already been set up on your local machine; otherwise, please follow the steps in https://knative.dev/docs/getting-started/first-source/#sending-an-event to set up a Knative service (cloudevents-player) as a sink.

# Set eventMesh.connector.plugin.type=knative in eventmesh-runtime/conf/eventmesh.properties
# Do unit test:
$ cd incubator-eventmesh/eventmesh-connector-plugin/eventmesh-connector-knative
$ ../../gradlew clean test --tests KnativeConnectorTest.testConnect
# See the delivered event:
$ curl http://cloudevents-player.default.127.0.0.1.sslip.io/messages

Documentation

  • Does this pull request introduce a new feature? yes.
  • If yes, how is the feature documented? not documented.
  • If a feature is not documented yet in this PR, please create a followup issue for adding the documentation. The followup issues or pull requests for documentation will be created after almost all implementation work has been finished.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

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

Mailing Lists:

Name Description Subscribe Unsubscribe Archive
Users User support and questions mailing list Subscribe Unsubscribe Mail Archives
Development Development related discussions Subscribe Unsubscribe Mail Archives
Commits All commits to repositories Subscribe Unsubscribe Mail Archives

@pchengma
Copy link
Member Author

@xwm1992 Please have a look when you have time. Thanks very much in advance!

@codecov
Copy link

codecov bot commented Jul 19, 2022

Codecov Report

Merging #1027 (14810a0) into knative-connector (72a7b33) will increase coverage by 0.17%.
The diff coverage is 52.98%.

❗ Current head 14810a0 differs from pull request most recent head f1e9245. Consider uploading reports for the commit f1e9245 to get more accurate results

@@                  Coverage Diff                   @@
##             knative-connector   #1027      +/-   ##
======================================================
+ Coverage                 9.69%   9.87%   +0.17%     
- Complexity                 610     629      +19     
======================================================
  Files                      362     374      +12     
  Lines                    23118   23244     +126     
  Branches                  2546    2541       -5     
======================================================
+ Hits                      2241    2295      +54     
- Misses                   20675   20740      +65     
- Partials                   202     209       +7     
Impacted Files Coverage Δ
...ve/cloudevent/impl/KnativeBinaryMessageReader.java 0.00% <0.00%> (ø)
...nector/knative/cloudevent/impl/KnativeHeaders.java 0.00% <0.00%> (ø)
...h/connector/knative/common/EventMeshConstants.java 0.00% <0.00%> (ø)
...tmesh/connector/knative/utils/CloudEventUtils.java 0.00% <0.00%> (ø)
...onnector/knative/producer/KnativeProducerImpl.java 47.05% <47.05%> (ø)
...tmesh/connector/knative/producer/ProducerImpl.java 50.00% <50.00%> (ø)
...connector/knative/config/ConfigurationWrapper.java 52.63% <52.63%> (ø)
.../knative/cloudevent/impl/KnativeMessageWriter.java 66.66% <66.66%> (ø)
...h/connector/knative/producer/AbstractProducer.java 66.66% <66.66%> (ø)
.../connector/knative/config/ClientConfiguration.java 90.00% <90.00%> (ø)
... and 10 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

}

@Override
public void sendOneway(CloudEvent cloudEvent) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please not implement this method, you should impelement the publish method with the send call back.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for your comments. I will change to the publish method instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

Implemented publish method with SendCallback.

Comment on lines 41 to 46
// Set HTTP header for CloudEvent:
this.httpUrlConnection.setRequestProperty(KnativeHeaders.CONTENT_TYPE, properties.getProperty(KnativeHeaders.CONTENT_TYPE));
this.httpUrlConnection.setRequestProperty(KnativeHeaders.CE_ID, properties.getProperty(KnativeHeaders.CE_ID));
this.httpUrlConnection.setRequestProperty(KnativeHeaders.CE_SPECVERSION, properties.getProperty(KnativeHeaders.CE_SPECVERSION));
this.httpUrlConnection.setRequestProperty(KnativeHeaders.CE_TYPE, properties.getProperty(KnativeHeaders.CE_TYPE));
this.httpUrlConnection.setRequestProperty(KnativeHeaders.CE_SOURCE, properties.getProperty(KnativeHeaders.CE_SOURCE));
Copy link
Contributor

Choose a reason for hiding this comment

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

These code for HTTP header attributes with the request, I recommend that you can write them when you send the cloud events not when you init the producer`.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for your comments. I will move this part out of the init period.

Copy link
Member Author

@pchengma pchengma Jul 19, 2022

Choose a reason for hiding this comment

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

Moved HTTP header setting into send method.

Comment on lines 39 to 61
public void sendOneway(CloudEvent cloudEvent) {
// Get CloudEvent data:
try {
String data = KnativeMessageFactory.createReader(cloudEvent);
super.getHttpUrlConnection().getOutputStream().write(data.getBytes(StandardCharsets.UTF_8));

// Send CloudEvent message:
String s = "";
int code = super.getHttpUrlConnection().getResponseCode();
if (code == 200) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(super.getHttpUrlConnection().getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
s += line + "\n";
}
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need to have this method, and when you implement the publish method with the callback, you can use the Async Http Client, here don't implement synchronously.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for your comments. I will change to the asynchronous way to call the publish method.

Copy link
Member Author

@pchengma pchengma Jul 19, 2022

Choose a reason for hiding this comment

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

  • Implemented sendAsync method by using AsyncHttpClient.
  • Added a simple exception checker method checkProducerException and a utility method convertSendResult to help implement methods with SendCallback.
  • Added AsyncHttpClient dependency to build.gradle file.

Comment on lines 37 to 46
public KnativeMessageWriter(String data) {
String s = "{ \"msg\": [\"" + data + "\"]}";
this.message = new CloudEventBuilder()
.withId("my-id")
.withSource(URI.create("/myClient"))
.withType("dev.knative.cronjob.event")
.withDataContentType("application/json")
.withData(s.getBytes(StandardCharsets.UTF_8))
.build();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

These codes look strange, you use the cloudeventbuilder to build the cloud events, but you only want to put the data, other attributes are useless.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for your comments. I will remove the unnecessary attributes.

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed unnecessary attributes in KnativeMessageWriter.

@pchengma pchengma requested a review from xwm1992 July 20, 2022 01:24
pchengma added 8 commits July 21, 2022 10:45
1. Changed to load Knative service URL from properties file (added common/EventMeshConstants, config/ClientConfiguration, and config/ConfigurationWrapper).

2. Deleted unused sendCallbackConvert method.

3. Added exception handling for HTTP response code error in send method.

4. Added related build.gradle dependencies, tools/known-dependencies.txt dependency, and third-party-licenses/LICENSE-httpasyncclient.txt file.
1. Fixed issue in Continuous Integration / Build (ubuntu-latest, 8) (pull_request).
2. Fixed issue in Continuous Integration / Build (ubuntu-latest, 11) (pull_request).
3. Fixed issue in Continuous Integration / Build (macOS-latest, 8) (pull_request).
4. Fixed issue in Continuous Integration / Build (macOS-latest, 11) (pull_request).
5. Fixed issue in Continuous Integration / License Check (pull_request).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants