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

First Connect SDK #67

Merged
merged 7 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 20 additions & 0 deletions src/config/connect-knet-sink.properties
Original file line number Diff line number Diff line change
@@ -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.

name=local-knet-sink
connector.class=KNetSinkConnector
tasks.max=1
knet.dotnet.classname=MASES.KNetTemplate.KNetConnect.KNetConnectSink, knetConnectSink
topics=topic-perf
20 changes: 20 additions & 0 deletions src/config/connect-knet-source.properties
Original file line number Diff line number Diff line change
@@ -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.

name=local-knet-source
connector.class=KNetSourceConnector
tasks.max=1
knet.dotnet.classname=MASES.KNetTemplate.KNetConnect.KNetConnectSource, knetConnectSource
topic=topic-perf
9 changes: 7 additions & 2 deletions src/java/knet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<name>mases.knet</name>
<description>Interface bridging implementation for Apache Kafka</description>
<url>https://github.com/masesgroup/KNet</url>
<version>1.2.4.0</version>
<version>1.3.0.0</version>

<licenses>
<license>
Expand Down Expand Up @@ -40,7 +40,7 @@
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<classpathfile>${basedir}/classpathfile.classpath</classpathfile>
<kafkaVersion>3.1.0</kafkaVersion>
<kafkaVersion>3.1.1</kafkaVersion>
</properties>
<distributionManagement>
<snapshotRepository>
Expand Down Expand Up @@ -228,6 +228,11 @@
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 2022 MASES s.r.l.
*
* 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
*
* 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.
*
* Refer to LICENSE for more information.
*/

package org.mases.knet.connect;

import org.apache.kafka.common.config.AbstractConfig;
import org.apache.kafka.common.config.ConfigDef;
import org.apache.kafka.common.config.ConfigException;
import org.mases.jcobridge.*;
import org.mases.knet.connect.source.KNetSourceTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Map;

public class KNetConnectProxy {
private static final Logger log = LoggerFactory.getLogger(KNetSourceTask.class);

public static final String DOTNET_CLASSNAME_CONFIG = "knet.dotnet.classname";

public static final ConfigDef CONFIG_DEF = new ConfigDef()
.define(DOTNET_CLASSNAME_CONFIG, ConfigDef.Type.STRING, null, ConfigDef.Importance.HIGH, ".NET class name in the form usable from .NET like \"classname, assembly name\".");

static JCObject knetConnectProxy = null;
static String sinkConnectorName = null;
static JCObject sinkConnector = null;
static String sourceConnectorName = null;
static JCObject sourceConnector = null;

static synchronized JCObject getConnectProxy() throws JCException, IOException {
if (knetConnectProxy == null) {
JCOBridge.Initialize();
knetConnectProxy = (JCObject) JCOBridge.GetCLRGlobal("KNetConnectProxy");
}
return knetConnectProxy;
}

public static synchronized boolean initializeSinkConnector(Map<String, String> props) throws JCException, IOException {
AbstractConfig parsedConfig = new AbstractConfig(CONFIG_DEF, props);
String className = parsedConfig.getString(DOTNET_CLASSNAME_CONFIG);
if (className == null)
throw new ConfigException("'classname' in KNetSinkConnector configuration requires a definition");
return (boolean) getConnectProxy().Invoke("AllocateSinkConnector", className);
}

public static synchronized boolean initializeSourceConnector(Map<String, String> props) throws JCException, IOException {
AbstractConfig parsedConfig = new AbstractConfig(CONFIG_DEF, props);
String className = parsedConfig.getString(DOTNET_CLASSNAME_CONFIG);
if (className == null)
throw new ConfigException("'classname' in KNetSinkConnector configuration requires a definition");
return (boolean) getConnectProxy().Invoke("AllocateSourceConnector", className);
}

public static synchronized JCObject getSinkConnector() throws JCException, IOException {
if (sinkConnector == null) {
sinkConnectorName = (String) getConnectProxy().Invoke("SinkConnectorName");
if (sinkConnectorName != null) {
sinkConnector = (JCObject) JCOBridge.GetCLRGlobal(sinkConnectorName);
}
}
return sinkConnector;
}

public static synchronized JCObject getSourceConnector() throws JCException, IOException {
if (sourceConnector == null) {
sourceConnectorName = (String) getConnectProxy().Invoke("SourceConnectorName");
if (sourceConnectorName != null) {
sourceConnector = (JCObject) JCOBridge.GetCLRGlobal(sourceConnectorName);
}
}
return sourceConnector;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright 2022 MASES s.r.l.
*
* 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
*
* 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.
*
* Refer to LICENSE for more information.
*/

package org.mases.knet.connect.sink;

import org.apache.kafka.common.config.ConfigDef;
import org.apache.kafka.common.config.ConfigException;
import org.apache.kafka.connect.connector.Task;
import org.apache.kafka.connect.sink.SinkConnector;
import org.mases.jcobridge.*;
import org.mases.knet.connect.KNetConnectProxy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class KNetSinkConnector extends SinkConnector {
private static final Logger log = LoggerFactory.getLogger(KNetSinkConnector.class);

private static final String registrationName = "KNetSinkConnector";

Object dataToExchange = null;

public Object getDataToExchange() {
return dataToExchange;
}

public void setDataToExchange(Object dte) {
dataToExchange = dte;
}

@Override
public void start(Map<String, String> props) {
try {
if (!KNetConnectProxy.initializeSinkConnector(props)) {
log.error("Failed Invoke of \"initializeSinkConnector\"");
throw new ConfigException("Failed Invoke of \"initializeSinkConnector\"");
} else {
JCOBridge.RegisterJVMGlobal(registrationName, this);
try {
dataToExchange = props;
JCObject sink = KNetConnectProxy.getSinkConnector();
if (sink == null) throw new ConfigException("getSinkConnector returned null.");
sink.Invoke("StartInternal");
} finally {
dataToExchange = null;
}
}
} catch (JCException | IOException jcne) {
log.error("Failed Invoke of \"start\"", jcne);
}
}

@Override
public Class<? extends Task> taskClass() {
return KNetSinkTask.class;
}

@Override
public List<Map<String, String>> taskConfigs(int maxTasks) {
ArrayList<Map<String, String>> configs = new ArrayList<>();
for (int i = 0; i < maxTasks; i++) {
Map<String, String> config = new HashMap<>();
try {
dataToExchange = config;
JCObject sink = KNetConnectProxy.getSinkConnector();
if (sink == null) throw new ConfigException("getSinkConnector returned null.");
sink.Invoke("TaskConfigsInternal", i);
} catch (JCException | IOException jcne) {
log.error("Failed Invoke of \"start\"", jcne);
} finally {
dataToExchange = null;
}
configs.add(config);
}
return configs;
}

@Override
public void stop() {
try {
try {
JCObject sink = KNetConnectProxy.getSinkConnector();
if (sink == null) throw new ConfigException("getSinkConnector returned null.");
sink.Invoke("StopInternal");
} finally {
JCOBridge.UnregisterJVMGlobal(registrationName);
}
} catch (JCException | IOException jcne) {
log.error("Failed Invoke of \"stop\"", jcne);
}
}

@Override
public ConfigDef config() {
return KNetConnectProxy.CONFIG_DEF;
}

@Override
public String version() {
try {
JCObject sink = KNetConnectProxy.getSinkConnector();
if (sink != null) {
return (String) sink.Invoke("VersionInternal");
}
} catch (JCException | IOException jcne) {
log.error("Failed Invoke of \"version\"", jcne);
}
return "NOT AVAILABLE";
}
}
Loading