odbozhou commented on a change in pull request #800: URL: https://github.com/apache/rocketmq-externals/pull/800#discussion_r698207363
########## File path: rocketmq-connect-hudi/src/main/java/org/apache/rocketmq/connect/hudi/config/CloneUtils.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.rocketmq.connect.hudi.config; + +import java.io.*; + +public class CloneUtils { + @SuppressWarnings("unchecked") + public static <T extends Serializable> T clone(T obj) { + T clonedObj = null; + try { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos); + oos.writeObject(obj); + oos.close(); + + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ObjectInputStream ois = new ObjectInputStream(bais); + clonedObj = (T) ois.readObject(); + ois.close(); + } catch (Exception e) { + e.printStackTrace(); Review comment: At least print the log, otherwise the exception will be swallowed ########## File path: rocketmq-connect-hudi/src/main/java/org/apache/rocketmq/connect/hudi/connector/HudiSinkTask.java ########## @@ -0,0 +1,124 @@ + +/* + * 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.connect.hudi.connector; + +import io.openmessaging.KeyValue; +import io.openmessaging.connector.api.common.QueueMetaData; +import io.openmessaging.connector.api.data.SinkDataEntry; +import io.openmessaging.connector.api.sink.SinkTask; +import org.apache.rocketmq.connect.hudi.config.HudiConnectConfig; +import org.apache.rocketmq.connect.hudi.config.ConfigUtil; +import org.apache.rocketmq.connect.hudi.sink.Updater; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collection; +import java.util.Map; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; + + +/** + * In the naming, we are using database for "keyspaces" and table for "columnFamily" + * This is because we kind of want the abstract data source to be aligned with SQL databases + */ +public class HudiSinkTask extends SinkTask { + private static final Logger log = LoggerFactory.getLogger(HudiSinkTask.class); + + private HudiConnectConfig hudiConnectConfig; + private Updater updater; + private BlockingQueue<Updater> tableQueue = new LinkedBlockingQueue<Updater>(); + + public HudiSinkTask() { + this.hudiConnectConfig = new HudiConnectConfig(); + } + + @Override + public void put(Collection<SinkDataEntry> sinkDataEntries) { + try { + if (tableQueue.size() > 1) { + updater = tableQueue.poll(1000, TimeUnit.MILLISECONDS); + } else { + updater = tableQueue.peek(); + } + log.info("Hudi Sink Task trying to put()"); + for (SinkDataEntry record : sinkDataEntries) { + log.info("Hudi Sink Task trying to call updater.push()"); + Boolean isSuccess = updater.push(record); + if (!isSuccess) { + log.error("push data error, record:{}", record); + } + } + } catch (Exception e) { + log.error("put sinkDataEntries error, {}", e); + } + } + + @Override + public void commit(Map<QueueMetaData, Long> map) { + + } + + /** + * Remember always close the CqlSession according to + * https://docs.datastax.com/en/developer/java-driver/4.5/manual/core/ + * @param props + */ + @Override + public void start(KeyValue props) { + try { + ConfigUtil.load(props, this.hudiConnectConfig); + log.info("init data source success"); + } catch (Exception e) { + log.error("Cannot start Hudi Sink Task because of configuration error{}", e); + } + try { + Updater updater = new Updater(hudiConnectConfig); + updater.start(); + tableQueue.add(updater); + } catch (Exception e) { + log.error("fail to start updater{}", e); + } + + + } + + @Override + public void stop() { + try { + for(Updater updater : tableQueue) { + updater.start(); Review comment: start ? ########## File path: rocketmq-connect-hudi/src/main/java/org/apache/rocketmq/connect/hudi/connector/HudiSinkConnector.java ########## @@ -0,0 +1,232 @@ + +/* + * 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.connect.hudi.connector; + + +import io.openmessaging.KeyValue; +import io.openmessaging.connector.api.Task; +import io.openmessaging.connector.api.sink.SinkConnector; +import io.openmessaging.internal.DefaultKeyValue; +import org.apache.commons.lang3.concurrent.BasicThreadFactory; +import org.apache.rocketmq.client.exception.MQClientException; +import org.apache.rocketmq.common.protocol.route.BrokerData; +import org.apache.rocketmq.common.protocol.route.QueueData; +import org.apache.rocketmq.common.protocol.route.TopicRouteData; +import org.apache.rocketmq.connect.hudi.config.*; +import org.apache.rocketmq.remoting.RPCHook; +import org.apache.rocketmq.tools.admin.DefaultMQAdminExt; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.*; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + + +public class HudiSinkConnector extends SinkConnector{ + private static final Logger log = LoggerFactory.getLogger(HudiSinkConnector.class); + private volatile boolean configValid = false; + private ScheduledExecutorService executor; + private HashMap<String, Set<TaskTopicInfo>> topicRouteMap; + + private DefaultMQAdminExt srcMQAdminExt; + private SinkConnectConfig sinkConnectConfig; + + private volatile boolean adminStarted; + + private ScheduledFuture<?> listenerHandle; + public static String HUDI_CONNECTOR_ADMIN_PREFIX = "HUDI-CONNECTOR-ADMIN"; + public static final String PREFIX = "hudi"; + + public HudiSinkConnector() { + topicRouteMap = new HashMap<>(); + sinkConnectConfig = new SinkConnectConfig(); + executor = Executors.newSingleThreadScheduledExecutor(new BasicThreadFactory.Builder().namingPattern("HudiFSinkConnector-SinkWatcher-%d").daemon(true).build()); + } + + private synchronized void startMQAdminTools() { + if (!configValid || adminStarted) { + return; + } + RPCHook rpcHook = null; + this.srcMQAdminExt = new DefaultMQAdminExt(rpcHook); + this.srcMQAdminExt.setNamesrvAddr(this.sinkConnectConfig.getSrcNamesrvs()); + this.srcMQAdminExt.setAdminExtGroup(Utils.createGroupName(HUDI_CONNECTOR_ADMIN_PREFIX)); + this.srcMQAdminExt.setInstanceName(Utils.createInstanceName(this.sinkConnectConfig.getSrcNamesrvs())); + + try { + log.info("Trying to start srcMQAdminExt"); + this.srcMQAdminExt.start(); + log.info("RocketMQ srcMQAdminExt started"); + + } catch (MQClientException e) { + log.error("Hudi Sink Task start failed for `srcMQAdminExt` exception.", e); + } + + adminStarted = true; + } + + @Override + public String verifyAndSetConfig(KeyValue config) { + for (String requestKey : HudiConnectConfig.REQUEST_CONFIG) { + if (!config.containsKey(requestKey)) { + return "Request config key: " + requestKey; + } + } + try { + this.sinkConnectConfig.validate(config); + } catch (IllegalArgumentException e) { + return e.getMessage(); + } + this.configValid = true; + + return ""; + } + + @Override + public void start() { + startMQAdminTools(); + startListener(); + } + + public void startListener() { Review comment: What is the function of the startListener method? ########## File path: rocketmq-connect-hudi/src/main/java/org/apache/rocketmq/connect/hudi/connector/HudiSinkConnector.java ########## @@ -0,0 +1,232 @@ + +/* + * 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.connect.hudi.connector; + + +import io.openmessaging.KeyValue; +import io.openmessaging.connector.api.Task; +import io.openmessaging.connector.api.sink.SinkConnector; +import io.openmessaging.internal.DefaultKeyValue; +import org.apache.commons.lang3.concurrent.BasicThreadFactory; +import org.apache.rocketmq.client.exception.MQClientException; +import org.apache.rocketmq.common.protocol.route.BrokerData; +import org.apache.rocketmq.common.protocol.route.QueueData; +import org.apache.rocketmq.common.protocol.route.TopicRouteData; +import org.apache.rocketmq.connect.hudi.config.*; +import org.apache.rocketmq.remoting.RPCHook; +import org.apache.rocketmq.tools.admin.DefaultMQAdminExt; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.*; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + + +public class HudiSinkConnector extends SinkConnector{ + private static final Logger log = LoggerFactory.getLogger(HudiSinkConnector.class); + private volatile boolean configValid = false; + private ScheduledExecutorService executor; + private HashMap<String, Set<TaskTopicInfo>> topicRouteMap; + + private DefaultMQAdminExt srcMQAdminExt; + private SinkConnectConfig sinkConnectConfig; + + private volatile boolean adminStarted; + + private ScheduledFuture<?> listenerHandle; + public static String HUDI_CONNECTOR_ADMIN_PREFIX = "HUDI-CONNECTOR-ADMIN"; + public static final String PREFIX = "hudi"; + + public HudiSinkConnector() { + topicRouteMap = new HashMap<>(); + sinkConnectConfig = new SinkConnectConfig(); + executor = Executors.newSingleThreadScheduledExecutor(new BasicThreadFactory.Builder().namingPattern("HudiFSinkConnector-SinkWatcher-%d").daemon(true).build()); + } + + private synchronized void startMQAdminTools() { Review comment: What is the function of the startMQAdminTools method? ########## File path: rocketmq-connect-hudi/src/main/java/example/avro/User.java ########## @@ -0,0 +1,501 @@ +package example.avro; /** + * Autogenerated by Avro + * + * DO NOT EDIT DIRECTLY + */ + +import org.apache.avro.message.BinaryMessageDecoder; +import org.apache.avro.message.BinaryMessageEncoder; +import org.apache.avro.message.SchemaStore; +import org.apache.avro.specific.SpecificData; +import org.apache.avro.util.Utf8; + [email protected] +public class User extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord { Review comment: Is this file necessary? If it is a necessary class, please modify the package name and include the apache license ########## File path: rocketmq-connect-hudi/src/main/java/org/apache/rocketmq/connect/hudi/connector/HudiSinkConnector.java ########## @@ -0,0 +1,232 @@ + +/* + * 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.connect.hudi.connector; + + +import io.openmessaging.KeyValue; +import io.openmessaging.connector.api.Task; +import io.openmessaging.connector.api.sink.SinkConnector; +import io.openmessaging.internal.DefaultKeyValue; +import org.apache.commons.lang3.concurrent.BasicThreadFactory; +import org.apache.rocketmq.client.exception.MQClientException; +import org.apache.rocketmq.common.protocol.route.BrokerData; +import org.apache.rocketmq.common.protocol.route.QueueData; +import org.apache.rocketmq.common.protocol.route.TopicRouteData; +import org.apache.rocketmq.connect.hudi.config.*; +import org.apache.rocketmq.remoting.RPCHook; +import org.apache.rocketmq.tools.admin.DefaultMQAdminExt; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.*; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + + +public class HudiSinkConnector extends SinkConnector{ + private static final Logger log = LoggerFactory.getLogger(HudiSinkConnector.class); + private volatile boolean configValid = false; + private ScheduledExecutorService executor; + private HashMap<String, Set<TaskTopicInfo>> topicRouteMap; + + private DefaultMQAdminExt srcMQAdminExt; + private SinkConnectConfig sinkConnectConfig; + + private volatile boolean adminStarted; + + private ScheduledFuture<?> listenerHandle; + public static String HUDI_CONNECTOR_ADMIN_PREFIX = "HUDI-CONNECTOR-ADMIN"; + public static final String PREFIX = "hudi"; + + public HudiSinkConnector() { + topicRouteMap = new HashMap<>(); + sinkConnectConfig = new SinkConnectConfig(); + executor = Executors.newSingleThreadScheduledExecutor(new BasicThreadFactory.Builder().namingPattern("HudiFSinkConnector-SinkWatcher-%d").daemon(true).build()); + } + + private synchronized void startMQAdminTools() { + if (!configValid || adminStarted) { + return; + } + RPCHook rpcHook = null; + this.srcMQAdminExt = new DefaultMQAdminExt(rpcHook); + this.srcMQAdminExt.setNamesrvAddr(this.sinkConnectConfig.getSrcNamesrvs()); + this.srcMQAdminExt.setAdminExtGroup(Utils.createGroupName(HUDI_CONNECTOR_ADMIN_PREFIX)); + this.srcMQAdminExt.setInstanceName(Utils.createInstanceName(this.sinkConnectConfig.getSrcNamesrvs())); + + try { + log.info("Trying to start srcMQAdminExt"); + this.srcMQAdminExt.start(); + log.info("RocketMQ srcMQAdminExt started"); + + } catch (MQClientException e) { + log.error("Hudi Sink Task start failed for `srcMQAdminExt` exception.", e); + } + + adminStarted = true; + } + + @Override + public String verifyAndSetConfig(KeyValue config) { + for (String requestKey : HudiConnectConfig.REQUEST_CONFIG) { + if (!config.containsKey(requestKey)) { + return "Request config key: " + requestKey; + } + } + try { + this.sinkConnectConfig.validate(config); + } catch (IllegalArgumentException e) { + return e.getMessage(); + } + this.configValid = true; + + return ""; + } + + @Override + public void start() { + startMQAdminTools(); + startListener(); + } + + public void startListener() { + listenerHandle = executor.scheduleAtFixedRate(new Runnable() { + boolean first = true; + HashMap<String, Set<TaskTopicInfo>> origin = null; + + @Override + public void run() { + buildRoute(); + if (first) { + origin = CloneUtils.clone(topicRouteMap); + first = false; + } + if (!compare(origin, topicRouteMap)) { + context.requestTaskReconfiguration(); + origin = CloneUtils.clone(topicRouteMap); + } + } + }, sinkConnectConfig.getRefreshInterval(), sinkConnectConfig.getRefreshInterval(), TimeUnit.SECONDS); + } + + public boolean compare(Map<String, Set<TaskTopicInfo>> origin, Map<String, Set<TaskTopicInfo>> updated) { + if (origin.size() != updated.size()) { + return false; + } + for (Map.Entry<String, Set<TaskTopicInfo>> entry : origin.entrySet()) { + if (!updated.containsKey(entry.getKey())) { + return false; + } + Set<TaskTopicInfo> originTasks = entry.getValue(); + Set<TaskTopicInfo> updateTasks = updated.get(entry.getKey()); + if (originTasks.size() != updateTasks.size()) { + return false; + } + + if (!originTasks.containsAll(updateTasks)) { + return false; + } + } + + return true; + } + + public void buildRoute() { Review comment: What is the function of the buildRoute method? ########## File path: rocketmq-connect-hudi/src/main/java/org/apache/rocketmq/connect/hudi/connector/HudiSinkTask.java ########## @@ -0,0 +1,124 @@ + +/* + * 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.connect.hudi.connector; + +import io.openmessaging.KeyValue; +import io.openmessaging.connector.api.common.QueueMetaData; +import io.openmessaging.connector.api.data.SinkDataEntry; +import io.openmessaging.connector.api.sink.SinkTask; +import org.apache.rocketmq.connect.hudi.config.HudiConnectConfig; +import org.apache.rocketmq.connect.hudi.config.ConfigUtil; +import org.apache.rocketmq.connect.hudi.sink.Updater; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collection; +import java.util.Map; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; + + +/** + * In the naming, we are using database for "keyspaces" and table for "columnFamily" + * This is because we kind of want the abstract data source to be aligned with SQL databases + */ +public class HudiSinkTask extends SinkTask { + private static final Logger log = LoggerFactory.getLogger(HudiSinkTask.class); + + private HudiConnectConfig hudiConnectConfig; + private Updater updater; + private BlockingQueue<Updater> tableQueue = new LinkedBlockingQueue<Updater>(); + + public HudiSinkTask() { + this.hudiConnectConfig = new HudiConnectConfig(); + } + + @Override + public void put(Collection<SinkDataEntry> sinkDataEntries) { + try { + if (tableQueue.size() > 1) { + updater = tableQueue.poll(1000, TimeUnit.MILLISECONDS); Review comment: Sink starts with only one updater, why should a blocking queue be introduced? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
