You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by GitBox <gi...@apache.org> on 2021/03/11 12:39:55 UTC

[GitHub] [iotdb] SteveYurongSu opened a new pull request #2815: [IOTDB-1217] Trigger module: API, executor and management services

SteveYurongSu opened a new pull request #2815:
URL: https://github.com/apache/iotdb/pull/2815


   Runtime core for trigger module, including:
   1. user API
   2. trigger executor
   3. several management services


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #2815: [IOTDB-1217] Trigger module: API, executor and management services

Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #2815:
URL: https://github.com/apache/iotdb/pull/2815#discussion_r614618222



##########
File path: server/src/main/java/org/apache/iotdb/db/engine/trigger/executor/TriggerExecutor.java
##########
@@ -0,0 +1,238 @@
+/*
+ * 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.engine.trigger.executor;
+
+import org.apache.iotdb.db.engine.trigger.api.Trigger;
+import org.apache.iotdb.db.engine.trigger.api.TriggerAttributes;
+import org.apache.iotdb.db.engine.trigger.service.TriggerClassLoader;
+import org.apache.iotdb.db.engine.trigger.service.TriggerRegistrationInformation;
+import org.apache.iotdb.db.exception.TriggerExecutionException;
+import org.apache.iotdb.db.exception.TriggerManagementException;
+import org.apache.iotdb.db.metadata.mnode.MeasurementMNode;
+import org.apache.iotdb.db.utils.TestOnly;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.utils.Binary;
+
+import java.lang.reflect.InvocationTargetException;
+
+public class TriggerExecutor {
+
+  private final TriggerRegistrationInformation registrationInformation;
+  private final TriggerAttributes attributes;
+
+  private final TriggerClassLoader classLoader;
+
+  private final MeasurementMNode measurementMNode;
+  private final TSDataType seriesDataType;
+
+  private final Trigger trigger;
+
+  public TriggerExecutor(
+      TriggerRegistrationInformation registrationInformation,
+      TriggerClassLoader classLoader,
+      MeasurementMNode measurementMNode)
+      throws TriggerManagementException {
+    this.registrationInformation = registrationInformation;
+    attributes = new TriggerAttributes(registrationInformation.getAttributes());
+
+    this.classLoader = classLoader;
+
+    this.measurementMNode = measurementMNode;
+    seriesDataType = measurementMNode.getSchema().getType();
+
+    trigger = constructTriggerInstance();
+  }
+
+  private Trigger constructTriggerInstance() throws TriggerManagementException {
+    try {
+      Class<?> triggerClass =
+          Class.forName(registrationInformation.getClassName(), true, classLoader);
+      return (Trigger) triggerClass.getDeclaredConstructor().newInstance();
+    } catch (InstantiationException
+        | InvocationTargetException
+        | NoSuchMethodException
+        | IllegalAccessException
+        | ClassNotFoundException e) {
+      throw new TriggerManagementException(
+          String.format(
+              "Failed to reflect Trigger %s(%s) instance, because %s",
+              registrationInformation.getTriggerName(), registrationInformation.getClassName(), e));
+    }
+  }
+
+  public void onCreate() throws TriggerExecutionException {
+    Thread.currentThread().setContextClassLoader(classLoader);
+
+    try {
+      trigger.onCreate(attributes);
+    } catch (Exception e) {
+      onTriggerExecutionError("onConfig(TriggerAttributes)", e);
+    }
+
+    registrationInformation.markAsStarted();

Review comment:
       explain why not synchronized.

##########
File path: server/src/test/java/org/apache/iotdb/db/integration/IoTDBTriggerExecutionIT.java
##########
@@ -0,0 +1,491 @@
+/*
+ * 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.integration;
+
+import org.apache.iotdb.db.engine.trigger.example.Counter;
+import org.apache.iotdb.db.engine.trigger.service.TriggerRegistrationService;
+import org.apache.iotdb.db.exception.TriggerManagementException;
+import org.apache.iotdb.db.exception.metadata.MetadataException;
+import org.apache.iotdb.db.metadata.PartialPath;
+import org.apache.iotdb.db.service.IoTDB;
+import org.apache.iotdb.db.utils.EnvironmentUtils;
+import org.apache.iotdb.jdbc.Config;
+import org.apache.iotdb.tsfile.file.metadata.enums.CompressionType;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSEncoding;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Arrays;
+import java.util.Collections;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+@SuppressWarnings("squid:S2925") // enable to use Thread.sleep(long) without warnings
+public class IoTDBTriggerExecutionIT {
+
+  private static final Logger LOGGER = LoggerFactory.getLogger(IoTDBTriggerExecutionIT.class);
+
+  private volatile Exception exception = null;
+
+  private final Thread dataGenerator =
+      new Thread() {
+
+        @Override
+        public void run() {
+
+          try (Connection connection =
+                  DriverManager.getConnection(
+                      Config.IOTDB_URL_PREFIX + "127.0.0.1:6667/", "root", "root");
+              Statement statement = connection.createStatement()) {
+            long count = 0;
+            do {
+              ++count;
+              statement.execute(
+                  String.format(
+                      "insert into root.vehicle.d1(timestamp,s1,s2,s3,s4,s5,s6) values(%d,%d,%d,%d,%d,%s,\"%d\")",
+                      count, count, count, count, count, count % 2 == 0 ? "true" : "false", count));
+            } while (!isInterrupted());
+          } catch (SQLException e) {
+            exception = e;
+          }
+        }
+      };
+
+  private void startDataGenerator() {
+    dataGenerator.start();
+  }
+
+  private void stopDataGenerator() throws InterruptedException {
+    dataGenerator.interrupt();
+    dataGenerator.join();
+    if (exception != null) {
+      fail(exception.getMessage());
+    }
+  }
+
+  @Before
+  public void setUp() throws Exception {
+    EnvironmentUtils.envSetUp();
+    createTimeseries();
+    Class.forName(Config.JDBC_DRIVER_NAME);
+  }
+
+  private void createTimeseries() throws MetadataException {
+    IoTDB.metaManager.createTimeseries(
+        new PartialPath("root.vehicle.d1.s1"),
+        TSDataType.INT32,
+        TSEncoding.PLAIN,
+        CompressionType.UNCOMPRESSED,
+        null);
+    IoTDB.metaManager.createTimeseries(
+        new PartialPath("root.vehicle.d1.s2"),
+        TSDataType.INT64,
+        TSEncoding.PLAIN,
+        CompressionType.UNCOMPRESSED,
+        null);
+    IoTDB.metaManager.createTimeseries(
+        new PartialPath("root.vehicle.d1.s3"),
+        TSDataType.FLOAT,
+        TSEncoding.PLAIN,
+        CompressionType.UNCOMPRESSED,
+        null);
+    IoTDB.metaManager.createTimeseries(
+        new PartialPath("root.vehicle.d1.s4"),
+        TSDataType.DOUBLE,
+        TSEncoding.PLAIN,
+        CompressionType.UNCOMPRESSED,
+        null);
+    IoTDB.metaManager.createTimeseries(
+        new PartialPath("root.vehicle.d1.s5"),
+        TSDataType.BOOLEAN,
+        TSEncoding.PLAIN,
+        CompressionType.UNCOMPRESSED,
+        null);
+    IoTDB.metaManager.createTimeseries(
+        new PartialPath("root.vehicle.d1.s6"),
+        TSDataType.TEXT,
+        TSEncoding.PLAIN,
+        CompressionType.UNCOMPRESSED,
+        null);
+  }
+
+  @After
+  public void tearDown() throws Exception {
+    EnvironmentUtils.cleanEnv();
+  }
+
+  @Test
+  public void checkFireTimes() {
+    try (Connection connection =
+            DriverManager.getConnection(
+                Config.IOTDB_URL_PREFIX + "127.0.0.1:6667/", "root", "root");
+        Statement statement = connection.createStatement()) {
+      statement.execute(
+          "create trigger trigger-1 before insert on root.vehicle.d1.s1 as \"org.apache.iotdb.db.engine.trigger.example.Counter\"");
+      statement.execute(
+          "create trigger trigger-2 after insert on root.vehicle.d1.s2 as \"org.apache.iotdb.db.engine.trigger.example.Counter\"");
+      statement.execute(
+          "create trigger trigger-3 before insert on root.vehicle.d1.s3 as \"org.apache.iotdb.db.engine.trigger.example.Counter\"");
+      statement.execute(
+          "create trigger trigger-4 after insert on root.vehicle.d1.s4 as \"org.apache.iotdb.db.engine.trigger.example.Counter\"");
+      statement.execute(
+          "create trigger trigger-5 before insert on root.vehicle.d1.s5 as \"org.apache.iotdb.db.engine.trigger.example.Counter\"");
+      statement.execute(
+          "create trigger trigger-6 after insert on root.vehicle.d1.s6 as \"org.apache.iotdb.db.engine.trigger.example.Counter\"");
+
+      int[] counters1 = getCounters(6);
+      LOGGER.info(Arrays.toString(counters1));
+      for (int i = 1; i < 6; ++i) {
+        assertEquals(Counter.BASE, counters1[i]);
+      }
+
+      startDataGenerator();
+      Thread.sleep(500);
+      stopDataGenerator();
+
+      int[] counters2 = getCounters(6);
+      LOGGER.info(Arrays.toString(counters2));
+      int expectedTimes = counters2[0] - counters1[0];
+      for (int i = 1; i < 6; ++i) {
+        assertEquals(expectedTimes, counters2[i] - counters1[i]);
+      }
+    } catch (SQLException | TriggerManagementException | InterruptedException e) {
+      fail(e.getMessage());
+    }
+  }
+
+  @Test
+  public void testCreateTriggersMultipleTimesWhileInserting() throws InterruptedException {

Review comment:
       testCreateAndDropTriggersMultipleTimesWhileInserting




-- 
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:
users@infra.apache.org



[GitHub] [iotdb] JackieTien97 merged pull request #2815: [IOTDB-1217] Trigger module: API, executor and management services

Posted by GitBox <gi...@apache.org>.
JackieTien97 merged pull request #2815:
URL: https://github.com/apache/iotdb/pull/2815


   


-- 
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:
users@infra.apache.org