Copilot commented on code in PR #15275: URL: https://github.com/apache/iotdb/pull/15275#discussion_r2086420333
########## iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/extractor/mqtt/MQTTPublishHandler.java: ########## @@ -0,0 +1,324 @@ +/* + * 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.iotdb.db.pipe.extractor.mqtt; + +import org.apache.iotdb.commons.conf.IoTDBConstant.ClientVersion; +import org.apache.iotdb.commons.exception.IllegalPathException; +import org.apache.iotdb.commons.pipe.agent.task.connection.UnboundedBlockingPendingQueue; +import org.apache.iotdb.commons.pipe.agent.task.meta.PipeTaskMeta; +import org.apache.iotdb.commons.pipe.config.constant.PipeExtractorConstant; +import org.apache.iotdb.commons.pipe.config.plugin.env.PipeTaskExtractorRuntimeEnvironment; +import org.apache.iotdb.commons.pipe.event.EnrichedEvent; +import org.apache.iotdb.commons.schema.table.column.TsTableColumnCategory; +import org.apache.iotdb.db.pipe.event.common.statement.PipeStatementInsertionEvent; +import org.apache.iotdb.db.protocol.mqtt.Message; +import org.apache.iotdb.db.protocol.mqtt.PayloadFormatManager; +import org.apache.iotdb.db.protocol.mqtt.PayloadFormatter; +import org.apache.iotdb.db.protocol.mqtt.TableMessage; +import org.apache.iotdb.db.protocol.mqtt.TreeMessage; +import org.apache.iotdb.db.protocol.session.IClientSession; +import org.apache.iotdb.db.protocol.session.MqttClientSession; +import org.apache.iotdb.db.protocol.session.SessionManager; +import org.apache.iotdb.db.queryengine.plan.Coordinator; +import org.apache.iotdb.db.queryengine.plan.analyze.cache.schema.DataNodeDevicePathCache; +import org.apache.iotdb.db.queryengine.plan.statement.crud.InsertRowStatement; +import org.apache.iotdb.db.queryengine.plan.statement.crud.InsertTabletStatement; +import org.apache.iotdb.db.utils.CommonUtils; +import org.apache.iotdb.db.utils.TimestampPrecisionUtils; +import org.apache.iotdb.pipe.api.customizer.parameter.PipeParameters; +import org.apache.iotdb.service.rpc.thrift.TSProtocolVersion; + +import io.moquette.interception.AbstractInterceptHandler; +import io.moquette.interception.messages.InterceptConnectMessage; +import io.moquette.interception.messages.InterceptDisconnectMessage; +import io.moquette.interception.messages.InterceptPublishMessage; +import io.netty.buffer.ByteBuf; +import io.netty.handler.codec.mqtt.MqttQoS; +import org.apache.tsfile.enums.TSDataType; +import org.apache.tsfile.utils.BitMap; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.time.ZoneId; +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** PublishHandler handle the messages from MQTT clients. */ +public class MQTTPublishHandler extends AbstractInterceptHandler { + + private static final Logger LOGGER = LoggerFactory.getLogger(MQTTPublishHandler.class); + + private final SessionManager sessionManager = SessionManager.getInstance(); + + private final ConcurrentHashMap<String, MqttClientSession> clientIdToSessionMap = + new ConcurrentHashMap<>(); + private final PayloadFormatter payloadFormat; + private final boolean useTableInsert; + private final UnboundedBlockingPendingQueue<EnrichedEvent> pendingQueue; + private final String pipeName; + private final long creationTime; + private final PipeTaskMeta pipeTaskMeta; + + public MQTTPublishHandler( + final PipeParameters pipeParameters, + final PipeTaskExtractorRuntimeEnvironment environment, + final UnboundedBlockingPendingQueue<EnrichedEvent> pendingQueue) { + this.payloadFormat = + PayloadFormatManager.getPayloadFormat( + pipeParameters.getStringOrDefault( + PipeExtractorConstant.MQTT_PAYLOAD_FORMATTER_KEY, + PipeExtractorConstant.MQTT_PAYLOAD_FORMATTER_DEFAULT_VALUE)); + useTableInsert = PayloadFormatter.TABLE_TYPE.equals(this.payloadFormat.getType()); + pipeName = environment.getPipeName(); + creationTime = environment.getCreationTime(); + pipeTaskMeta = environment.getPipeTaskMeta(); + this.pendingQueue = pendingQueue; + } + + @Override + public String getID() { + return "mqtt-source-broker-listener"; + } + + @Override + public void onConnect(InterceptConnectMessage msg) { + if (!clientIdToSessionMap.containsKey(msg.getClientID())) { + final MqttClientSession session = new MqttClientSession(msg.getClientID()); + sessionManager.login( + session, + msg.getUsername(), + new String(msg.getPassword()), + ZoneId.systemDefault().toString(), + TSProtocolVersion.IOTDB_SERVICE_PROTOCOL_V3, + ClientVersion.V_1_0, + useTableInsert ? IClientSession.SqlDialect.TABLE : IClientSession.SqlDialect.TREE); + sessionManager.registerSession(session); + clientIdToSessionMap.put(msg.getClientID(), session); + } + } + + @Override + public void onDisconnect(InterceptDisconnectMessage msg) { + final MqttClientSession session = clientIdToSessionMap.remove(msg.getClientID()); + if (null != session) { + sessionManager.removeCurrSession(); + sessionManager.closeSession(session, Coordinator.getInstance()::cleanupQueryExecution); + } + } + + @Override + public void onPublish(InterceptPublishMessage msg) { + try { + final String clientId = msg.getClientID(); + if (!clientIdToSessionMap.containsKey(clientId)) { + return; + } + final MqttClientSession session = clientIdToSessionMap.get(msg.getClientID()); + final ByteBuf payload = msg.getPayload(); + final String topic = msg.getTopicName(); + final String username = msg.getUsername(); + final MqttQoS qos = msg.getQos(); + + LOGGER.debug( + "Receive publish message. clientId: {}, username: {}, qos: {}, topic: {}, payload: {}", + clientId, + username, + qos, + topic, + payload); + + final List<Message> messages = payloadFormat.format(payload); + if (messages == null) { + return; + } + + for (Message message : messages) { + if (message == null) { + continue; + } + if (useTableInsert) { + final TableMessage tableMessage = (TableMessage) message; + // '/' previously defined as a database name + final String database = + !msg.getTopicName().contains("/") + ? msg.getTopicName() + : msg.getTopicName().substring(0, msg.getTopicName().indexOf("/")); + tableMessage.setDatabase(database.toLowerCase()); + extractTable(tableMessage, session); + } else { + extractTree((TreeMessage) message, session); + } + } + } catch (Throwable t) { + LOGGER.warn("onPublish execution exception, msg is [{}], error is ", msg, t); Review Comment: Catching Throwable can mask serious errors (e.g., OutOfMemoryError). Consider catching Exception instead unless there is a specific reason to handle all Throwables. ```suggestion } catch (Exception e) { LOGGER.warn("onPublish execution exception, msg is [{}], error is ", msg, e); ``` ########## iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/pipe/util/ExternalLoadBalancer.java: ########## @@ -0,0 +1,192 @@ +/* + * 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.iotdb.confignode.procedure.impl.pipe.util; + +import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId; +import org.apache.iotdb.common.rpc.thrift.TConsensusGroupType; +import org.apache.iotdb.commons.cluster.NodeStatus; +import org.apache.iotdb.commons.pipe.agent.task.meta.PipeStaticMeta; +import org.apache.iotdb.commons.pipe.config.constant.PipeExtractorConstant; +import org.apache.iotdb.confignode.manager.ConfigManager; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * The ExternalLoadBalancer is responsible for assigning parallel extraction tasks from an external + * source to available DataNodes in the cluster. + */ +public class ExternalLoadBalancer { + private final BalanceStrategy strategy; + + public ExternalLoadBalancer(final String balanceStrategy) { + switch (balanceStrategy) { + case PipeExtractorConstant.EXTERNAL_EXTRACTOR_BALANCE_PROPORTION_STRATEGY: + this.strategy = new ProportionalBalanceStrategy(); + break; + default: + throw new IllegalArgumentException("Unknown load balance strategy: " + balanceStrategy); + } + } + + /** + * Balances the given number of parallel tasks across available nodes. + * + * @param parallelCount number of external source tasks to distribute + * @param pipeStaticMeta metadata about the pipe extractor + * @param configManager reference to ConfigManager for cluster information + * @return a mapping from task index to leader node id + */ + public Map<Integer, Integer> balance( + final int parallelCount, + final PipeStaticMeta pipeStaticMeta, + final ConfigManager configManager) { + return strategy.balance(parallelCount, pipeStaticMeta, configManager); + } + + public interface BalanceStrategy { + Map<Integer, Integer> balance( + final int parallelCount, + final PipeStaticMeta pipeStaticMeta, + final ConfigManager configManager); + } + + public static class ProportionalBalanceStrategy implements BalanceStrategy { + @Override + public Map<Integer, Integer> balance( + final int parallelCount, + final PipeStaticMeta pipeStaticMeta, + final ConfigManager configManager) { + final Map<TConsensusGroupId, Integer> regionLeaderMap = + configManager.getLoadManager().getRegionLeaderMap(); + final Map<Integer, Integer> parallelAssignment = new HashMap<>(); + + // Check if the external extractor is single instance per node + if (pipeStaticMeta + .getExtractorParameters() + .getBooleanOrDefault( + Arrays.asList( + PipeExtractorConstant.EXTERNAL_EXTRACTOR_SINGLE_INSTANCE_PER_NODE_KEY, + PipeExtractorConstant.EXTERNAL_SOURCE_SINGLE_INSTANCE_PER_NODE_KEY), + PipeExtractorConstant.EXTERNAL_EXTRACTOR_SINGLE_INSTANCE_PER_NODE_DEFAULT_VALUE)) { + final List<Integer> runningDataNodes = + configManager.getLoadManager().filterDataNodeThroughStatus(NodeStatus.Running).stream() + .sorted() + .collect(Collectors.toList()); + if (runningDataNodes.isEmpty()) { + throw new RuntimeException("No available datanode to assign tasks"); + } + final int numNodes = runningDataNodes.size(); + for (int i = 1; i <= Math.min(numNodes, parallelCount); i++) { + final int datanodeId = runningDataNodes.get(i - 1); Review Comment: [nitpick] The use of negative numbers as task IDs (e.g., -i) may be confusing to maintainers. Consider adding a comment or using a more descriptive key mechanism to clarify the intent. ```suggestion final int datanodeId = runningDataNodes.get(i - 1); // Use negative task IDs (-i) to represent tasks assigned to DataNodes in a specific order. // This ensures that task IDs are unique and distinguishable from other potential task ID ranges. ``` -- 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]
