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 #4824] Add HTTP Sink Connector #4837

Merged
merged 27 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
8275b59
feat: Add HTTP Sink Connector
cnzakii Apr 13, 2024
6776490
refactor: Replace okHttpClient with vertx.WebClient
cnzakii Apr 13, 2024
003f078
fix: Resolving dependency conflicts
cnzakii Apr 14, 2024
cd21ea3
test: Add HttpSinkConnectorTest
cnzakii Apr 14, 2024
dc08ce1
Merge branch 'apache:master' into feat_4824
cnzakii Apr 14, 2024
8486080
fix: Add License
cnzakii Apr 14, 2024
f2e3835
fix: Solving dependency issues
cnzakii Apr 14, 2024
eda1d14
fix: License Check
cnzakii Apr 14, 2024
611c8d6
feat: Add HTTPS/SSL support
cnzakii Apr 14, 2024
64f2822
fix: Optimize logging
cnzakii Apr 15, 2024
77ad32f
feat: Add webhook functionality
cnzakii Apr 16, 2024
3eb3af6
fix: Fix some bugs
cnzakii Apr 16, 2024
3c59c8e
test: add callback test
cnzakii Apr 16, 2024
408aaa6
refactor: Add webhook Support
cnzakii Apr 17, 2024
5cb55a8
fix: Optimization tests and configuration additions
cnzakii Apr 18, 2024
1050f8a
fix: code style
cnzakii Apr 18, 2024
fe2b732
feat: rebuild WebhookHttpSinkHandler and add RetryHttpSinkHandler
cnzakii Apr 27, 2024
7e2d978
fix: fix ci
cnzakii Apr 27, 2024
9c9e4a8
refactor: Use failsafe alternative resilience4j and optimize webhook …
cnzakii Apr 28, 2024
33b14e4
fix: fix License Check
cnzakii Apr 28, 2024
4af7455
fix: update something
cnzakii Apr 29, 2024
5e2bf52
fix: fix ci
cnzakii Apr 29, 2024
3dcc5a9
fix: update something
cnzakii Apr 29, 2024
6f3b361
fix: Optimized naming
cnzakii Apr 29, 2024
71c12e8
fix: fix ci
cnzakii Apr 29, 2024
6732e53
fix: fix style check error
cnzakii Apr 29, 2024
2f2e81e
test: update HttpSinkConnectorTest
cnzakii Apr 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion eventmesh-connectors/eventmesh-connector-http/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
dependencies {
api project(":eventmesh-openconnect:eventmesh-openconnect-java")
implementation project(":eventmesh-common")
implementation 'io.cloudevents:cloudevents-http-vertx:2.3.0'

implementation 'io.cloudevents:cloudevents-http-vertx:3.0.0'
implementation 'io.vertx:vertx-web:4.4.6'
implementation 'io.vertx:vertx-web-client:4.4.6'

testImplementation "org.apache.httpcomponents:httpclient"
testImplementation 'org.mock-server:mockserver-netty:5.15.0'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.eventmesh.connector.http.server;

import org.apache.eventmesh.connector.http.config.HttpServerConfig;
import org.apache.eventmesh.connector.http.sink.connector.HttpSinkConnector;
import org.apache.eventmesh.connector.http.source.connector.HttpSourceConnector;
import org.apache.eventmesh.openconnect.Application;
import org.apache.eventmesh.openconnect.util.ConfigUtil;
Expand All @@ -33,7 +34,8 @@ public static void main(String[] args) throws Exception {
}

if (serverConfig.isSinkEnable()) {
// TODO support sink connector
Application httpSinkApp = new Application();
httpSinkApp.run(HttpSinkConnector.class);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.http.sink.config;


import org.apache.eventmesh.openconnect.api.config.SinkConfig;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = true)
public class HttpSinkConfig extends SinkConfig {

public SinkConnectorConfig connectorConfig;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.http.sink.config;

import lombok.Data;

@Data
public class SinkConnectorConfig {

private String connectorName;

private String host;

private int port;

private String path;

private boolean ssl;

private int idleTimeout;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* 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.http.sink.connector;

import org.apache.eventmesh.connector.http.sink.config.HttpSinkConfig;
import org.apache.eventmesh.openconnect.api.config.Config;
import org.apache.eventmesh.openconnect.api.connector.ConnectorContext;
import org.apache.eventmesh.openconnect.api.connector.SinkConnectorContext;
import org.apache.eventmesh.openconnect.api.sink.Sink;
import org.apache.eventmesh.openconnect.offsetmgmt.api.data.ConnectRecord;

import java.util.List;
import java.util.Objects;

import io.netty.handler.codec.http.HttpResponseStatus;
import io.vertx.core.Vertx;
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.client.WebClientOptions;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;


@Slf4j
public class HttpSinkConnector implements Sink {

private HttpSinkConfig httpSinkConfig;

private WebClient webClient;

private volatile boolean isRunning = false;

@Override
public Class<? extends Config> configClass() {
return HttpSinkConfig.class;
}

@Override
public void init(Config config) throws Exception {
httpSinkConfig = (HttpSinkConfig) config;
doInit();
}

@Override
public void init(ConnectorContext connectorContext) throws Exception {
SinkConnectorContext sinkConnectorContext = (SinkConnectorContext) connectorContext;
this.httpSinkConfig = (HttpSinkConfig) sinkConnectorContext.getSinkConfig();
doInit();
}

@SneakyThrows
private void doInit() {
final Vertx vertx = Vertx.vertx();
// TODO Add more configurations
WebClientOptions options = new WebClientOptions()
cnzakii marked this conversation as resolved.
Show resolved Hide resolved
.setDefaultHost(this.httpSinkConfig.connectorConfig.getHost())
.setDefaultPort(this.httpSinkConfig.connectorConfig.getPort())
.setSsl(this.httpSinkConfig.connectorConfig.isSsl())
.setIdleTimeout(this.httpSinkConfig.connectorConfig.getIdleTimeout());
this.webClient = WebClient.create(vertx, options);
}

@Override
public void start() throws Exception {
this.isRunning = true;
}

@Override
public void commit(ConnectRecord record) {

}

@Override
public String name() {
return this.httpSinkConfig.connectorConfig.getConnectorName();
}

@Override
public void stop() throws Exception {
this.isRunning = false;
this.webClient.close();
}

public boolean isRunning() {
return isRunning;
}

@Override
public void put(List<ConnectRecord> sinkRecords) {
for (ConnectRecord sinkRecord : sinkRecords) {
try {
if (Objects.isNull(sinkRecord)) {
log.warn("ConnectRecord data is null, ignore.");
continue;
}
sendMessage(sinkRecord);
} catch (Exception e) {
log.error("Failed to sink message via HTTP. ", e);
}
}
}

private void sendMessage(ConnectRecord record) {
this.webClient.post(this.httpSinkConfig.connectorConfig.getPath())
.putHeader("Content-Type", "application/json; charset=utf-8")
.sendJson(record, ar -> {
if (ar.succeeded()) {
if (ar.result().statusCode() != HttpResponseStatus.OK.code()) {
log.error("[HttpSinkConnector] Failed to send message via HTTP. Response: {}", ar.result());
} else {
log.info("[HttpSinkConnector] Successfully send message via HTTP. ");
}
Pil0tXia marked this conversation as resolved.
Show resolved Hide resolved
} else {
// This function is accessed only when an error occurs at the network level
log.error("[HttpSinkConnector] Failed to send message via HTTP. Exception: {}", ar.cause().getMessage());
}
Pil0tXia marked this conversation as resolved.
Show resolved Hide resolved
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#
# 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.
#

pubSubConfig:
meshAddress: 127.0.0.1:10000
subject: TopicTest
idc: FT
env: PRD
group: httpSink
appId: 5032
userName: httpSinkUser
passWord: httpPassWord
connectorConfig:
connectorName: httpSink
host: 127.0.0.1
port: 8987
path: /test
ssl: false
cnzakii marked this conversation as resolved.
Show resolved Hide resolved
idleTimeout: 5
Original file line number Diff line number Diff line change
@@ -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.connector.http.source.connector;


import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockserver.model.HttpRequest.request;

import org.apache.eventmesh.connector.http.sink.config.HttpSinkConfig;
import org.apache.eventmesh.connector.http.sink.config.SinkConnectorConfig;
import org.apache.eventmesh.connector.http.sink.connector.HttpSinkConnector;
import org.apache.eventmesh.openconnect.offsetmgmt.api.data.ConnectRecord;
import org.apache.eventmesh.openconnect.offsetmgmt.api.data.RecordOffset;
import org.apache.eventmesh.openconnect.offsetmgmt.api.data.RecordPartition;
import org.apache.eventmesh.openconnect.util.ConfigUtil;

import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockserver.client.MockServerClient;
import org.mockserver.integration.ClientAndServer;
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse;

import io.vertx.core.http.HttpMethod;

public class HttpSinkConnectorTest {

private HttpSinkConnector sinkConnector;
private SinkConnectorConfig sinkConnectorConfig;

private ClientAndServer mockServer;

@BeforeEach
public void setUp() throws Exception {
sinkConnector = new HttpSinkConnector();
HttpSinkConfig sinkConfig = (HttpSinkConfig) ConfigUtil.parse(sinkConnector.configClass());
sinkConnectorConfig = sinkConfig.connectorConfig;
sinkConnector.init(sinkConfig);
sinkConnector.start();
mockServer = ClientAndServer.startClientAndServer(sinkConnectorConfig.getPort());
}

@AfterEach
public void stopMockServer() throws Exception {
sinkConnector.stop();
mockServer.close();
}

@Test
void testPut() throws InterruptedException {
new MockServerClient(sinkConnectorConfig.getHost(), sinkConnectorConfig.getPort())
.when(
request()
.withMethod("POST")
.withPath(sinkConnectorConfig.getPath())
)
.respond(
HttpResponse.response()
.withStatusCode(200)
);

final int times = 10;
List<ConnectRecord> connectRecords = new ArrayList<>();
for (int i = 0; i < times; i++) {
RecordPartition partition = new RecordPartition();
RecordOffset offset = new RecordOffset();
long timestamp = System.currentTimeMillis();
ConnectRecord connectRecord = new ConnectRecord(partition, offset,
timestamp, "test-http " + i);
connectRecords.add(connectRecord);
}

sinkConnector.put(connectRecords);
// Sleeps for 3 seconds, waiting for the webClient to finish sending all requests
Thread.sleep(3000);

HttpRequest[] allRequests = mockServer.retrieveRecordedRequests(null);
// Determine the total number of requests
assertEquals(times, allRequests.length);

for (int i = 0; i < times; i++) {
HttpRequest actualRequest = allRequests[0];
// Determine the request method
assertEquals(HttpMethod.POST.name(), actualRequest.getMethod().getValue());
}
}

}
Loading
Loading