github-advanced-security[bot] commented on code in PR #132:
URL: https://github.com/apache/iotdb-extras/pull/132#discussion_r4036036279


##########
iotdb-collector/collector-core/src/main/java/org/apache/iotdb/collector/plugin/builtin/source/iotdb/IoTDBPushSource.java:
##########
@@ -76,42 +82,96 @@
 
   @Override
   public void start() throws Exception {
-    if (workerThread == null || !workerThread.isAlive()) {
-      isStarted = true;
-      workerThread = new Thread(this::doWork);
-      workerThread.start();
+    if (workerThread != null && workerThread.isAlive()) {
+      return;
     }
-  }
 
-  private void doWork() {
+    // Validate the topic and subscribe on the calling thread so that a 
missing topic, a
+    // tsfile-format topic, an unreachable broker or a server without 
subscription support fails
+    // task creation with the cause, instead of leaving a task that looks 
alive but never delivers.
+    requireRecordFormatTopic();
+
     final Properties pullProperties = new Properties();
     pullProperties.put(IoTDBPushSourceConstant.HOST_KEY, host);
     pullProperties.put(IoTDBPushSourceConstant.PORT_KEY, port);
     pullProperties.put(ConsumerConstant.CONSUMER_ID_KEY, "r1");
     pullProperties.put(ConsumerConstant.CONSUMER_GROUP_ID_KEY, "rg1");
 
-    try (final SubscriptionTreePullConsumer consumer =
-        new SubscriptionTreePullConsumer(pullProperties)) {
-      consumer.open();
-      consumer.subscribe(topic);
+    final SubscriptionTreePullConsumer pullConsumer =
+        new SubscriptionTreePullConsumer(pullProperties);
+    try {
+      pullConsumer.open();
+      pullConsumer.subscribe(topic);
+    } catch (final Exception e) {
+      try {
+        pullConsumer.close();
+      } catch (final Exception closeException) {
+        e.addSuppressed(closeException);
+      }
+      throw e;
+    }
+
+    consumer = pullConsumer;
+    isStarted = true;
+    workerThread = new Thread(this::doWork, "iotdb-push-source-" + topic);
+    workerThread.start();
+  }
+
+  private void requireRecordFormatTopic() throws Exception {
+    try (final SubscriptionTreeSession session = new 
SubscriptionTreeSession(host, port)) {

Review Comment:
   ## CodeQL / Deprecated method or constructor invocation
   
   Invoking [SubscriptionTreeSession.SubscriptionTreeSession](1) should be 
avoided because it has been deprecated.
   
   [Show more 
details](https://github.com/apache/iotdb-extras/security/code-scanning/302)



##########
mybatis-generator/src/test/java/org/apache/iotdb/mybatis/plugin/BatchInsertPluginTest.java:
##########
@@ -0,0 +1,270 @@
+/*
+ * 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.mybatis.plugin;
+
+import org.apache.ibatis.builder.xml.XMLMapperBuilder;
+import org.apache.ibatis.session.Configuration;
+import org.junit.Test;
+import org.mybatis.generator.api.IntrospectedColumn;
+import org.mybatis.generator.api.IntrospectedTable;
+import org.mybatis.generator.api.dom.DefaultJavaFormatter;
+import org.mybatis.generator.api.dom.DefaultXmlFormatter;
+import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
+import org.mybatis.generator.api.dom.java.Interface;
+import org.mybatis.generator.api.dom.java.JavaVisibility;
+import org.mybatis.generator.api.dom.xml.Attribute;
+import org.mybatis.generator.api.dom.xml.Document;
+import org.mybatis.generator.api.dom.xml.XmlElement;
+import org.mybatis.generator.codegen.mybatis3.IntrospectedTableMyBatis3Impl;
+import org.mybatis.generator.config.Context;
+import org.mybatis.generator.config.ModelType;
+import org.mybatis.generator.config.TableConfiguration;
+import org.mybatis.generator.internal.rules.FlatModelRules;
+import org.mybatis.generator.internal.rules.HierarchicalModelRules;
+
+import javax.tools.ToolProvider;
+
+import java.io.StringReader;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Proxy;
+import java.net.URLClassLoader;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.sql.Types;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+
+public class BatchInsertPluginTest {
+  private final Context context = new Context(ModelType.FLAT);
+  private final List<String> warnings = new ArrayList<>();
+  private final BatchInsertPlugin plugin = new BatchInsertPlugin();
+
+  private IntrospectedTable table() {
+    context.addProperty("beginningDelimiter", "\"");
+    context.addProperty("endingDelimiter", "\"");
+    plugin.setContext(context);
+    assertTrue(plugin.validate(warnings));
+    IntrospectedTable table =
+        new IntrospectedTableMyBatis3Impl() {
+          @Override
+          public String getFullyQualifiedTableNameAtRuntime() {
+            return "measurements";
+          }
+
+          @Override
+          public String getAliasedFullyQualifiedTableNameAtRuntime() {
+            return "measurements m";
+          }
+        };
+    TableConfiguration configuration = new TableConfiguration(context);
+    configuration.setTableName("measurements");
+    table.setTableConfiguration(configuration);
+    table.setContext(context);
+    table.setBaseRecordType("example.Measurement");
+    table.setRecordWithBLOBsType("example.MeasurementWithBLOBs");
+    table.setRules(new FlatModelRules(table));
+    column(table, "time", Types.TIMESTAMP, "TIMESTAMP", "java.lang.Long");
+    return table;
+  }
+
+  private IntrospectedColumn column(
+      IntrospectedTable table, String name, int type, String jdbc, String 
javaType) {
+    IntrospectedColumn column = new IntrospectedColumn();
+    column.setContext(context);
+    column.setActualColumnName(name);
+    column.setJavaProperty(name);
+    column.setJdbcType(type);
+    column.setJdbcTypeName(jdbc);
+    column.setFullyQualifiedJavaType(new FullyQualifiedJavaType(javaType));
+    table.addColumn(column);
+    return column;
+  }
+
+  private Interface mapper(IntrospectedTable table) {
+    Interface mapper = new Interface("example.MeasurementMapper");
+    mapper.setVisibility(JavaVisibility.PUBLIC);
+    plugin.clientGenerated(mapper, table);
+    return mapper;
+  }
+
+  private String xml(IntrospectedTable table) {
+    Document document =
+        new Document(
+            "-//mybatis.org//DTD Mapper 3.0//EN", 
"https://mybatis.org/dtd/mybatis-3-mapper.dtd";);
+    XmlElement root = new XmlElement("mapper");
+    root.addAttribute(new Attribute("namespace", "example.MeasurementMapper"));
+    document.setRootElement(root);
+    plugin.sqlMapDocumentGenerated(document, table);
+    return new DefaultXmlFormatter().getFormattedContent(document);
+  }
+
+  @Test
+  public void generatedMapperCompilesAndGuardsAndSplitsBatches() throws 
Exception {
+    IntrospectedTable table = table();
+    Properties properties = new Properties();
+    properties.setProperty("batchSize", "2");
+    plugin.setProperties(properties);
+    assertTrue(plugin.validate(warnings));
+    Path dir = Files.createTempDirectory("iotdb-mbg-test-");
+    try {
+      Path source = Files.createDirectories(dir.resolve("example"));
+      Files.writeString(
+          source.resolve("Measurement.java"), "package example; public class 
Measurement {}");
+      Files.writeString(
+          source.resolve("MeasurementMapper.java"),
+          new DefaultJavaFormatter().getFormattedContent(mapper(table)));
+      assertEquals(
+          0,
+          ToolProvider.getSystemJavaCompiler()
+              .run(
+                  null,
+                  null,
+                  null,
+                  "-proc:none",
+                  "-classpath",
+                  System.getProperty("java.class.path"),
+                  "-d",
+                  dir.toString(),
+                  source.resolve("Measurement.java").toString(),
+                  source.resolve("MeasurementMapper.java").toString()));
+      try (URLClassLoader loader =
+          new URLClassLoader(
+              new java.net.URL[] {dir.toUri().toURL()}, 
getClass().getClassLoader())) {
+        Class<?> mapper = loader.loadClass("example.MeasurementMapper");
+        Object row = 
loader.loadClass("example.Measurement").getConstructor().newInstance();
+        List<Integer> chunks = new ArrayList<>();
+        java.util.concurrent.atomic.AtomicBoolean unknownCount =
+            new java.util.concurrent.atomic.AtomicBoolean();
+        Object instance =
+            Proxy.newProxyInstance(
+                loader,
+                new Class<?>[] {mapper},
+                (proxy, method, args) -> {
+                  if (method.isDefault())
+                    return InvocationHandler.invokeDefault(proxy, method, 
args);
+                  chunks.add(((List<?>) args[0]).size());
+                  return unknownCount.get() ? -1 : ((List<?>) args[0]).size();
+                });
+        java.lang.reflect.Method insert = mapper.getMethod("batchInsert", 
List.class);
+        assertEquals(0, insert.invoke(instance, List.of()));
+        assertTrue(chunks.isEmpty());
+        assertEquals(5, insert.invoke(instance, List.of(row, row, row, row, 
row)));
+        assertEquals(List.of(2, 2, 1), chunks);
+        unknownCount.set(true);
+        assertEquals(-1, insert.invoke(instance, List.of(row, row, row)));
+        chunks.clear();
+        InvocationTargetException failure =
+            assertThrows(
+                InvocationTargetException.class,
+                () -> insert.invoke(instance, Arrays.asList(row, row, row, 
null)));
+        assertTrue(failure.getCause() instanceof IllegalArgumentException);
+        assertTrue(chunks.isEmpty());
+        assertThrows(
+            InvocationTargetException.class, () -> insert.invoke(instance, new 
Object[] {null}));
+      }
+    } finally {
+      try (java.util.stream.Stream<Path> files = Files.walk(dir)) {
+        for (Path path :
+            files
+                .sorted(java.util.Comparator.reverseOrder())
+                .collect(java.util.stream.Collectors.toList())) {
+          Files.delete(path);
+        }
+      }
+    }
+  }
+
+  @Test
+  public void preservesEscapingHandlersAndParameterCounts() {
+    IntrospectedTable table = table();
+    IntrospectedColumn order = column(table, "order", Types.BIGINT, "BIGINT", 
"java.lang.Long");
+    order.setColumnNameDelimited(true);
+    order.setTypeHandler("org.apache.ibatis.type.LongTypeHandler");
+    String xml = xml(table);
+    assertTrue(xml.contains("\"order\""));
+    
assertTrue(xml.contains("typeHandler=org.apache.ibatis.type.LongTypeHandler"));
+    assertFalse(xml.contains("measurements m"));
+    Configuration configuration = new Configuration();
+    new XMLMapperBuilder(
+            new StringReader(xml), configuration, "mapper.xml", 
configuration.getSqlFragments())

Review Comment:
   ## CodeQL / Deprecated method or constructor invocation
   
   Invoking [XMLMapperBuilder.XMLMapperBuilder](1) should be avoided because it 
has been deprecated.
   
   [Show more 
details](https://github.com/apache/iotdb-extras/security/code-scanning/304)



##########
mybatis-generator/src/test/java/org/apache/iotdb/mybatis/plugin/IoTDBKeyPluginTest.java:
##########
@@ -0,0 +1,180 @@
+/*
+ * 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.mybatis.plugin;
+
+import org.apache.ibatis.builder.xml.XMLMapperBuilder;
+import org.apache.ibatis.mapping.BoundSql;
+import org.apache.ibatis.session.Configuration;
+import org.junit.Test;
+import org.mybatis.generator.api.IntrospectedColumn;
+import org.mybatis.generator.api.IntrospectedTable;
+import org.mybatis.generator.api.dom.DefaultXmlFormatter;
+import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
+import org.mybatis.generator.api.dom.xml.Attribute;
+import org.mybatis.generator.api.dom.xml.Document;
+import org.mybatis.generator.api.dom.xml.TextElement;
+import org.mybatis.generator.api.dom.xml.XmlElement;
+import org.mybatis.generator.codegen.mybatis3.IntrospectedTableMyBatis3Impl;
+import org.mybatis.generator.config.Context;
+import org.mybatis.generator.config.ModelType;
+import org.mybatis.generator.config.TableConfiguration;
+import org.mybatis.generator.internal.rules.FlatModelRules;
+
+import java.io.StringReader;
+import java.sql.Types;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class IoTDBKeyPluginTest {
+  private final Context context = new Context(ModelType.FLAT);
+  private final List<String> warnings = new ArrayList<>();
+  private final IoTDBKeyPlugin plugin = new IoTDBKeyPlugin();
+
+  private IntrospectedTable table() {
+    context.addProperty("beginningDelimiter", "\"");
+    context.addProperty("endingDelimiter", "\"");
+    plugin.setContext(context);
+    assertTrue(plugin.validate(warnings));
+    IntrospectedTable table =
+        new IntrospectedTableMyBatis3Impl() {
+          @Override
+          public String getFullyQualifiedTableNameAtRuntime() {
+            return "mix";
+          }
+        };
+    TableConfiguration configuration = new TableConfiguration(context);
+    configuration.setTableName("mix");
+    table.setTableConfiguration(configuration);
+    table.setContext(context);
+    table.setBaseRecordType("example.Mix");
+    table.setRules(new FlatModelRules(table));
+    column(table, "time", Types.TIMESTAMP, "TIMESTAMP", "java.lang.Long");
+    column(table, "device_id", Types.VARCHAR, "VARCHAR", "java.lang.String");
+    column(table, "temperature", Types.FLOAT, "FLOAT", "java.lang.Float");
+    table.addPrimaryKeyColumn("time");
+    table.addPrimaryKeyColumn("device_id");
+    return table;
+  }
+
+  private void column(
+      IntrospectedTable table, String name, int type, String jdbc, String 
javaType) {
+    IntrospectedColumn column = new IntrospectedColumn();
+    column.setContext(context);
+    column.setActualColumnName(name);
+    column.setJavaProperty(
+        name.equals("device_id") ? "deviceId" : name); // MBG camel-cases the 
property name
+    column.setColumnNameDelimited(true); // matches delimitAllColumns in the 
example configs
+    column.setJdbcType(type);
+    column.setJdbcTypeName(jdbc);
+    column.setFullyQualifiedJavaType(new FullyQualifiedJavaType(javaType));
+    table.addColumn(column);
+  }
+
+  /** Mirrors the trailing "where ... and ..." text elements MBG emits for the 
key predicate. */
+  private XmlElement keyedStatement(String tag, String id, String prefix) {
+    XmlElement element = new XmlElement(tag);
+    element.addAttribute(new Attribute("id", id));
+    if (tag.equals("select")) {
+      element.addAttribute(new Attribute("resultType", "map"));
+    }
+    element.addElement(new TextElement(prefix + " \"mix\""));
+    element.addElement(new TextElement("where \"time\" = 
#{time,jdbcType=TIMESTAMP}"));
+    element.addElement(new TextElement("  and \"device_id\" = 
#{deviceId,jdbcType=VARCHAR}"));
+    return element;
+  }
+
+  private Configuration parse(XmlElement... statements) {
+    Document document =
+        new Document(
+            "-//mybatis.org//DTD Mapper 3.0//EN", 
"https://mybatis.org/dtd/mybatis-3-mapper.dtd";);
+    XmlElement root = new XmlElement("mapper");
+    root.addAttribute(new Attribute("namespace", "example.MixMapper"));
+    for (XmlElement statement : statements) {
+      root.addElement(statement);
+    }
+    document.setRootElement(root);
+    Configuration configuration = new Configuration();
+    new XMLMapperBuilder(
+            new StringReader(new 
DefaultXmlFormatter().getFormattedContent(document)),
+            configuration,
+            "mapper.xml",
+            configuration.getSqlFragments())

Review Comment:
   ## CodeQL / Deprecated method or constructor invocation
   
   Invoking [XMLMapperBuilder.XMLMapperBuilder](1) should be avoided because it 
has been deprecated.
   
   [Show more 
details](https://github.com/apache/iotdb-extras/security/code-scanning/303)



-- 
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]

Reply via email to