liutaohua commented on a change in pull request #1828: URL: https://github.com/apache/iotdb/pull/1828#discussion_r544782273
########## File path: server/src/main/java/org/apache/iotdb/db/query/udf/service/UDFRegistrationService.java ########## @@ -0,0 +1,308 @@ +/* + * 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.query.udf.service; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map.Entry; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import org.apache.commons.io.FileUtils; +import org.apache.iotdb.db.conf.IoTDBDescriptor; +import org.apache.iotdb.db.engine.fileSystem.SystemFileFactory; +import org.apache.iotdb.db.exception.StartupException; +import org.apache.iotdb.db.exception.UDFRegistrationException; +import org.apache.iotdb.db.exception.query.QueryProcessException; +import org.apache.iotdb.db.query.udf.api.UDF; +import org.apache.iotdb.db.query.udf.core.context.UDFContext; +import org.apache.iotdb.db.service.IService; +import org.apache.iotdb.db.service.ServiceType; +import org.apache.iotdb.db.utils.TestOnly; +import org.apache.iotdb.tsfile.fileSystem.FSFactoryProducer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class UDFRegistrationService implements IService { + + private static final Logger logger = LoggerFactory.getLogger(UDFRegistrationService.class); + + private static final String ULOG_FILE_DIR = + IoTDBDescriptor.getInstance().getConfig().getSystemDir() + + File.separator + "udf" + File.separator; + private static final String LOG_FILE_NAME = ULOG_FILE_DIR + "ulog.txt"; + private static final String TEMPORARY_LOG_FILE_NAME = LOG_FILE_NAME + ".tmp"; + + private final ConcurrentHashMap<String, UDFRegistrationInformation> registrationInformation; + + private final ReentrantReadWriteLock lock; + private UDFLogWriter temporaryLogWriter; + + private UDFClassLoader udfClassLoader; + + private UDFRegistrationService() { + registrationInformation = new ConcurrentHashMap<>(); + lock = new ReentrantReadWriteLock(); + } + + @SuppressWarnings("squid:S3776") // Suppress high Cognitive Complexity warning + public void register(String functionName, String className, boolean isTemporary, Review comment: register(UDFRegistrationInformation info, boolean writeToLog) ########## File path: server/src/main/java/org/apache/iotdb/db/query/udf/service/UDFClassLoader.java ########## @@ -0,0 +1,58 @@ +/* + * 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.query.udf.service; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.HashSet; +import java.util.Set; +import org.apache.commons.io.FileUtils; +import org.apache.iotdb.db.engine.fileSystem.SystemFileFactory; + +public class UDFClassLoader extends URLClassLoader { + + private final String libRoot; + private final Set<File> fileSet; Review comment: It is recommended to use a URLClassLoader for each UDF, manage with HashMap, and close the corresponding URLClassLoader when the user uninstalls the UDF ########## File path: server/src/main/java/org/apache/iotdb/db/query/udf/service/UDFRegistrationService.java ########## @@ -0,0 +1,308 @@ +/* + * 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.query.udf.service; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map.Entry; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import org.apache.commons.io.FileUtils; +import org.apache.iotdb.db.conf.IoTDBDescriptor; +import org.apache.iotdb.db.engine.fileSystem.SystemFileFactory; +import org.apache.iotdb.db.exception.StartupException; +import org.apache.iotdb.db.exception.UDFRegistrationException; +import org.apache.iotdb.db.exception.query.QueryProcessException; +import org.apache.iotdb.db.query.udf.api.UDF; +import org.apache.iotdb.db.query.udf.core.context.UDFContext; +import org.apache.iotdb.db.service.IService; +import org.apache.iotdb.db.service.ServiceType; +import org.apache.iotdb.db.utils.TestOnly; +import org.apache.iotdb.tsfile.fileSystem.FSFactoryProducer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class UDFRegistrationService implements IService { + + private static final Logger logger = LoggerFactory.getLogger(UDFRegistrationService.class); + + private static final String ULOG_FILE_DIR = + IoTDBDescriptor.getInstance().getConfig().getSystemDir() + + File.separator + "udf" + File.separator; + private static final String LOG_FILE_NAME = ULOG_FILE_DIR + "ulog.txt"; + private static final String TEMPORARY_LOG_FILE_NAME = LOG_FILE_NAME + ".tmp"; + + private final ConcurrentHashMap<String, UDFRegistrationInformation> registrationInformation; + + private final ReentrantReadWriteLock lock; + private UDFLogWriter temporaryLogWriter; + + private UDFClassLoader udfClassLoader; + + private UDFRegistrationService() { + registrationInformation = new ConcurrentHashMap<>(); + lock = new ReentrantReadWriteLock(); + } + + @SuppressWarnings("squid:S3776") // Suppress high Cognitive Complexity warning + public void register(String functionName, String className, boolean isTemporary, + boolean writeToTemporaryLogFile) throws UDFRegistrationException { + UDFRegistrationInformation information = registrationInformation.get(functionName); + if (information != null) { + if (information.getClassName().equals(className)) { + String errorMessage; + if (information.isTemporary() == isTemporary) { + errorMessage = String + .format("UDF %s(%s) has already been registered successfully.", + functionName, className); + } else { + errorMessage = String.format( + "Failed to register %sTEMPORARY UDF %s(%s), because a %sTEMPORARY UDF %s(%s) with the same function name and the class name has already been registered.", + isTemporary ? "" : "non-", functionName, className, + information.isTemporary() ? "" : "non-", information.getFunctionName(), + information.getClassName()); Review comment: +1 ,or use a uniform error description prefix ########## File path: server/src/main/java/org/apache/iotdb/db/query/udf/service/UDFRegistrationService.java ########## @@ -0,0 +1,320 @@ +/* + * 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.query.udf.service; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map.Entry; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import org.apache.commons.io.FileUtils; +import org.apache.iotdb.db.conf.IoTDBDescriptor; +import org.apache.iotdb.db.engine.fileSystem.SystemFileFactory; +import org.apache.iotdb.db.exception.StartupException; +import org.apache.iotdb.db.exception.UDFRegistrationException; +import org.apache.iotdb.db.exception.query.QueryProcessException; +import org.apache.iotdb.db.query.udf.api.UDF; +import org.apache.iotdb.db.query.udf.core.context.UDFContext; +import org.apache.iotdb.db.service.IService; +import org.apache.iotdb.db.service.ServiceType; +import org.apache.iotdb.db.utils.TestOnly; +import org.apache.iotdb.tsfile.fileSystem.FSFactoryProducer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class UDFRegistrationService implements IService { + + private static final Logger logger = LoggerFactory.getLogger(UDFRegistrationService.class); + + private static final String ULOG_FILE_DIR = + IoTDBDescriptor.getInstance().getConfig().getSystemDir() + + File.separator + "udf" + File.separator; + private static final String LOG_FILE_NAME = ULOG_FILE_DIR + "ulog.txt"; + private static final String TEMPORARY_LOG_FILE_NAME = LOG_FILE_NAME + ".tmp"; + + private final ConcurrentHashMap<String, UDFRegistrationInformation> registrationInformation; + + private final ReentrantReadWriteLock lock; + private UDFLogWriter temporaryLogWriter; + + private final String libRoot; + + private URLClassLoader udfClassLoader; + + private UDFRegistrationService() { + registrationInformation = new ConcurrentHashMap<>(); + lock = new ReentrantReadWriteLock(); + libRoot = parseLibRoot(); + } + + private String parseLibRoot() { + String jarPath = (new File( + getClass().getProtectionDomain().getCodeSource().getLocation().getPath())) + .getAbsolutePath(); + int lastIndex = jarPath.lastIndexOf(File.separatorChar); + String libPath = jarPath.substring(0, lastIndex + 1); + logger.info("System lib root: {}", libPath); + return libPath; + } + + @SuppressWarnings("squid:S3776") // Suppress high Cognitive Complexity warning + public void register(String functionName, String className, boolean isTemporary, + boolean writeToTemporaryLogFile) throws UDFRegistrationException { + UDFRegistrationInformation information = registrationInformation.get(functionName); + if (information != null) { + if (information.getClassName().equals(className)) { + String errorMessage; + if (information.isTemporary() == isTemporary) { + errorMessage = String + .format("UDF %s(%s) has already been registered successfully.", + functionName, className); + } else { + errorMessage = String.format( + "Failed to register %sTEMPORARY UDF %s(%s), because a %sTEMPORARY UDF %s(%s) with the same function name and the class name has already been registered.", + isTemporary ? "" : "non-", functionName, className, + information.isTemporary() ? "" : "non-", information.getFunctionName(), + information.getClassName()); + } + logger.warn(errorMessage); + throw new UDFRegistrationException(errorMessage); + } else { + String errorMessage = String.format( + "Failed to register UDF %s(%s), because a UDF %s(%s) with the same function name but a different class name has already been registered.", + functionName, className, + information.getFunctionName(), information.getClassName()); + logger.warn(errorMessage); + throw new UDFRegistrationException(errorMessage); + } + } + + Class<?> functionClass; + try { + udfClassLoader = getUDFClassLoader(); + functionClass = Class.forName(className, true, udfClassLoader); + functionClass.getDeclaredConstructor().newInstance(); Review comment: it's necessary to delete, or use the `findClass` method ########## File path: server/src/main/java/org/apache/iotdb/db/qp/physical/sys/DropFunctionPlan.java ########## @@ -0,0 +1,45 @@ +/* + * 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.qp.physical.sys; + +import java.util.ArrayList; +import java.util.List; +import org.apache.iotdb.db.metadata.PartialPath; +import org.apache.iotdb.db.qp.logical.Operator.OperatorType; +import org.apache.iotdb.db.qp.physical.PhysicalPlan; + +public class DropFunctionPlan extends PhysicalPlan { + + private final String udfName; + + public DropFunctionPlan(String udfName) { + super(false, OperatorType.DROP_FUNCTION); Review comment: canBeSplit ? ########## File path: server/src/main/java/org/apache/iotdb/db/query/udf/core/executor/UDTFExecutor.java ########## @@ -0,0 +1,91 @@ +/* + * 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.query.udf.core.executor; + +import java.time.ZoneId; +import org.apache.iotdb.db.exception.query.QueryProcessException; +import org.apache.iotdb.db.query.udf.api.UDTF; +import org.apache.iotdb.db.query.udf.api.access.Row; +import org.apache.iotdb.db.query.udf.api.access.RowWindow; +import org.apache.iotdb.db.query.udf.api.customizer.config.UDTFConfigurations; +import org.apache.iotdb.db.query.udf.api.customizer.parameter.UDFParameters; +import org.apache.iotdb.db.query.udf.core.context.UDFContext; +import org.apache.iotdb.db.query.udf.datastructure.tv.ElasticSerializableTVList; +import org.apache.iotdb.db.query.udf.service.UDFRegistrationService; + +public class UDTFExecutor { + + protected final UDFContext context; + protected UDTFConfigurations configurations; + protected UDTF udtf; + protected ElasticSerializableTVList collector; + + public UDTFExecutor(UDFContext context, ZoneId zoneId) throws QueryProcessException { + this.context = context; + configurations = new UDTFConfigurations(zoneId); + udtf = (UDTF) UDFRegistrationService.getInstance().reflect(context); + try { + udtf.beforeStart(new UDFParameters(context.getPaths(), context.getAttributes()), + configurations); + } catch (Exception e) { + throw new QueryProcessException(e.getMessage()); + } + configurations.check(); + } + + public void initCollector(long queryId, float collectorMemoryBudgetInMB) + throws QueryProcessException { + collector = ElasticSerializableTVList + .newElasticSerializableTVList(configurations.getOutputDataType(), queryId, + collectorMemoryBudgetInMB, 1); + } + + public void execute(Row row) throws QueryProcessException { + try { + udtf.transform(row, collector); + } catch (Exception e) { + throw new QueryProcessException(e.getMessage()); Review comment: Pay attention to the errors, such as when `ArrayIndexOutOfBoundsException` occurs, will only hint `Msg: 500: 1` ########## File path: server/src/main/java/org/apache/iotdb/db/qp/strategy/PhysicalGenerator.java ########## @@ -397,7 +419,13 @@ private PhysicalPlan transformQuery(QueryOperator queryOperator, int fetchSize) } else if (queryOperator.getIndexType() != null) { queryPlan = new QueryIndexPlan(); } else { - queryPlan = new RawDataQueryPlan(); + queryPlan = queryOperator.hasUdf() Review comment: use `if( queryOperator.hasUdf()) ` to remove redundant judgments ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected]
