How to implement a trigger

You need to implement the trigger by writing a Java class, where the dependency shown below is required. If you use MavenHow to implement a trigger - 图1open in new window, you can search for them directly from the Maven repositoryHow to implement a trigger - 图2open in new window.

Dependency

  1. <dependency>
  2. <groupId>org.apache.iotdb</groupId>
  3. <artifactId>iotdb-server</artifactId>
  4. <version>1.0.0</version>
  5. <scope>provided</scope>
  6. </dependency>

Note that the dependency version should be correspondent to the target server version.

Interface Description

To implement a trigger, you need to implement the org.apache.iotdb.trigger.api.Trigger class.

  1. import org.apache.iotdb.trigger.api.enums.FailureStrategy;
  2. import org.apache.iotdb.tsfile.write.record.Tablet;
  3. public interface Trigger {
  4. /**
  5. * This method is mainly used to validate {@link TriggerAttributes} before calling {@link
  6. * Trigger#onCreate(TriggerAttributes)}.
  7. *
  8. * @param attributes TriggerAttributes
  9. * @throws Exception e
  10. */
  11. default void validate(TriggerAttributes attributes) throws Exception {}
  12. /**
  13. * This method will be called when creating a trigger after validation.
  14. *
  15. * @param attributes TriggerAttributes
  16. * @throws Exception e
  17. */
  18. default void onCreate(TriggerAttributes attributes) throws Exception {}
  19. /**
  20. * This method will be called when dropping a trigger.
  21. *
  22. * @throws Exception e
  23. */
  24. default void onDrop() throws Exception {}
  25. /**
  26. * When restarting a DataNode, Triggers that have been registered will be restored and this method
  27. * will be called during the process of restoring.
  28. *
  29. * @throws Exception e
  30. */
  31. default void restore() throws Exception {}
  32. /**
  33. * Overrides this method to set the expected FailureStrategy, {@link FailureStrategy#OPTIMISTIC}
  34. * is the default strategy.
  35. *
  36. * @return {@link FailureStrategy}
  37. */
  38. default FailureStrategy getFailureStrategy() {
  39. return FailureStrategy.OPTIMISTIC;
  40. }
  41. /**
  42. * @param tablet see {@link Tablet} for detailed information of data structure. Data that is
  43. * inserted will be constructed as a Tablet and you can define process logic with {@link
  44. * Tablet}.
  45. * @return true if successfully fired
  46. * @throws Exception e
  47. */
  48. default boolean fire(Tablet tablet) throws Exception {
  49. return true;
  50. }
  51. }

This class provides two types of programming interfaces: Lifecycle related interfaces and data change listening related interfaces. All the interfaces in this class are not required to be implemented. When the interfaces are not implemented, the trigger will not respond to the data changes. You can implement only some of these interfaces according to your needs.

Descriptions of the interfaces are as followed.

InterfaceDescription
default void validate(TriggerAttributes attributes) throws Exception {}When you creates a trigger using the CREATE TRIGGER statement, you can specify the parameters that the trigger needs to use, and this interface will be used to verify the correctness of the parameters。
default void onCreate(TriggerAttributes attributes) throws Exception {}This interface is called once when you create a trigger using the CREATE TRIGGER statement. During the lifetime of each trigger instance, this interface will be called only once. This interface is mainly used for the following functions: helping users to parse custom attributes in SQL statements (using TriggerAttributes). You can create or apply for resources, such as establishing external links, opening files, etc.
default void onDrop() throws Exception {}This interface is called when you drop a trigger using the DROP TRIGGER statement. During the lifetime of each trigger instance, this interface will be called only once. This interface mainly has the following functions: it can perform the operation of resource release and can be used to persist the results of trigger calculations.
default void restore() throws Exception {}When the DataNode is restarted, the cluster will restore the trigger instance registered on the DataNode, and this interface will be called once for stateful trigger during the process. After the DataNode where the stateful trigger instance is located goes down, the cluster will restore the trigger instance on another available DataNode, calling this interface once in the process. This interface can be used to customize recovery logic.

Listening interface

  1. /**
  2. * @param tablet see {@link Tablet} for detailed information of data structure. Data that is
  3. * inserted will be constructed as a Tablet and you can define process logic with {@link
  4. * Tablet}.
  5. * @return true if successfully fired
  6. * @throws Exception e
  7. */
  8. default boolean fire(Tablet tablet) throws Exception {
  9. return true;
  10. }

When the data changes, the trigger uses the Tablet as the unit of firing operation. You can obtain the metadata and data of the corresponding sequence through Tablet, and then perform the corresponding trigger operation. If the fire process is successful, the return value should be true. If the interface returns false or throws an exception, we consider the trigger fire process as failed. When the trigger fire process fails, we will perform corresponding operations according to the listening strategy interface.

When performing an INSERT operation, for each time series in it, we will detect whether there is a trigger that listens to the path pattern, and then assemble the time series data that matches the path pattern listened by the same trigger into a new Tablet for trigger fire interface. Can be understood as:

  1. Map<PartialPath, List<Trigger>> pathToTriggerListMap => Map<Trigger, Tablet>

Note that currently we do not make any guarantees about the order in which triggers fire.

Here is an example:

Suppose there are three triggers, and the trigger event of the triggers are all BEFORE INSERT:

  • Trigger1 listens on root.sg.*
  • Trigger2 listens on root.sg.a
  • Trigger3 listens on root.sg.b

Insertion statement:

  1. insert into root.sg(time, a, b) values (1, 1, 1);

The time series root.sg.a matches Trigger1 and Trigger2, and the sequence root.sg.b matches Trigger1 and Trigger3, then:

  • The data of root.sg.a and root.sg.b will be assembled into a new tablet1, and Trigger1.fire(tablet1) will be executed at the corresponding Trigger Event.
  • The data of root.sg.a will be assembled into a new tablet2, and Trigger2.fire(tablet2) will be executed at the corresponding Trigger Event.
  • The data of root.sg.b will be assembled into a new tablet3, and Trigger3.fire(tablet3) will be executed at the corresponding Trigger Event.

Listening strategy interface

When the trigger fails to fire, we will take corresponding actions according to the strategy set by the listening strategy interface. You can set org.apache.iotdb.trigger.api.enums.FailureStrategy. There are currently two strategies, optimistic and pessimistic:

  • Optimistic strategy: The trigger that fails to fire does not affect the firing of subsequent triggers, nor does it affect the writing process, that is, we do not perform additional processing on the sequence involved in the trigger failure, only log the failure to record the failure, and finally inform user that data insertion is successful, but the trigger fire part failed.
  • Pessimistic strategy: The failure trigger affects the processing of all subsequent Pipelines, that is, we believe that the firing failure of the trigger will cause all subsequent triggering processes to no longer be carried out. If the trigger event of the trigger is BEFORE INSERT, then the insertion will no longer be performed, and the insertion failure will be returned directly.
  1. /**
  2. * Overrides this method to set the expected FailureStrategy, {@link FailureStrategy#OPTIMISTIC}
  3. * is the default strategy.
  4. *
  5. * @return {@link FailureStrategy}
  6. */
  7. default FailureStrategy getFailureStrategy() {
  8. return FailureStrategy.OPTIMISTIC;
  9. }

Example

If you use MavenHow to implement a trigger - 图3open in new window, you can refer to our sample project trigger-example.

You can find it hereHow to implement a trigger - 图4open in new window.

Here is the code from one of the sample projects:

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.apache.iotdb.trigger;
  20. import org.apache.iotdb.db.engine.trigger.sink.alertmanager.AlertManagerConfiguration;
  21. import org.apache.iotdb.db.engine.trigger.sink.alertmanager.AlertManagerEvent;
  22. import org.apache.iotdb.db.engine.trigger.sink.alertmanager.AlertManagerHandler;
  23. import org.apache.iotdb.trigger.api.Trigger;
  24. import org.apache.iotdb.trigger.api.TriggerAttributes;
  25. import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
  26. import org.apache.iotdb.tsfile.write.record.Tablet;
  27. import org.apache.iotdb.tsfile.write.schema.MeasurementSchema;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import java.io.IOException;
  31. import java.util.HashMap;
  32. import java.util.List;
  33. public class ClusterAlertingExample implements Trigger {
  34. private static final Logger LOGGER = LoggerFactory.getLogger(ClusterAlertingExample.class);
  35. private final AlertManagerHandler alertManagerHandler = new AlertManagerHandler();
  36. private final AlertManagerConfiguration alertManagerConfiguration =
  37. new AlertManagerConfiguration("http://127.0.0.1:9093/api/v2/alerts");
  38. private String alertname;
  39. private final HashMap<String, String> labels = new HashMap<>();
  40. private final HashMap<String, String> annotations = new HashMap<>();
  41. @Override
  42. public void onCreate(TriggerAttributes attributes) throws Exception {
  43. alertname = "alert_test";
  44. labels.put("series", "root.ln.wf01.wt01.temperature");
  45. labels.put("value", "");
  46. labels.put("severity", "");
  47. annotations.put("summary", "high temperature");
  48. annotations.put("description", "{{.alertname}}: {{.series}} is {{.value}}");
  49. alertManagerHandler.open(alertManagerConfiguration);
  50. }
  51. @Override
  52. public void onDrop() throws IOException {
  53. alertManagerHandler.close();
  54. }
  55. @Override
  56. public boolean fire(Tablet tablet) throws Exception {
  57. List<MeasurementSchema> measurementSchemaList = tablet.getSchemas();
  58. for (int i = 0, n = measurementSchemaList.size(); i < n; i++) {
  59. if (measurementSchemaList.get(i).getType().equals(TSDataType.DOUBLE)) {
  60. // for example, we only deal with the columns of Double type
  61. double[] values = (double[]) tablet.values[i];
  62. for (double value : values) {
  63. if (value > 100.0) {
  64. LOGGER.info("trigger value > 100");
  65. labels.put("value", String.valueOf(value));
  66. labels.put("severity", "critical");
  67. AlertManagerEvent alertManagerEvent =
  68. new AlertManagerEvent(alertname, labels, annotations);
  69. alertManagerHandler.onEvent(alertManagerEvent);
  70. } else if (value > 50.0) {
  71. LOGGER.info("trigger value > 50");
  72. labels.put("value", String.valueOf(value));
  73. labels.put("severity", "warning");
  74. AlertManagerEvent alertManagerEvent =
  75. new AlertManagerEvent(alertname, labels, annotations);
  76. alertManagerHandler.onEvent(alertManagerEvent);
  77. }
  78. }
  79. }
  80. }
  81. return true;
  82. }
  83. }