You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by GitBox <gi...@apache.org> on 2019/01/15 23:42:48 UTC

[incubator-pinot] Diff for: [GitHub] akshayrai merged pull request #3692: [TE] Annotate and update the registry with alert filter types

diff --git a/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/alert/filter/DimensionDetectionAlertFilter.java b/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/alert/filter/DimensionDetectionAlertFilter.java
index dc25707a9f..11fb7039d6 100644
--- a/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/alert/filter/DimensionDetectionAlertFilter.java
+++ b/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/alert/filter/DimensionDetectionAlertFilter.java
@@ -38,6 +38,7 @@
 import java.util.Map;
 import java.util.Set;
 import org.apache.commons.collections.MapUtils;
+import org.apache.pinot.thirdeye.detection.annotation.AlertFilter;
 
 
 /**
@@ -45,6 +46,7 @@
  * of unconditional and another set of conditional recipients, based on the value
  * of a specified anomaly dimension
  */
+@AlertFilter(type = "DIMENSION_ALERTER_PIPELINE")
 public class DimensionDetectionAlertFilter extends StatefulDetectionAlertFilter {
   private static final String PROP_DETECTION_CONFIG_IDS = "detectionConfigIds";
   private static final String PROP_TO = "to";
diff --git a/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/alert/filter/LegacyAlertFilter.java b/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/alert/filter/LegacyAlertFilter.java
index 1572d4ba30..e96ab88d7c 100644
--- a/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/alert/filter/LegacyAlertFilter.java
+++ b/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/alert/filter/LegacyAlertFilter.java
@@ -24,6 +24,7 @@
 import org.apache.pinot.thirdeye.datalayer.dto.DetectionAlertConfigDTO;
 import org.apache.pinot.thirdeye.datalayer.dto.MergedAnomalyResultDTO;
 import org.apache.pinot.thirdeye.detection.alert.DetectionAlertFilterRecipients;
+import org.apache.pinot.thirdeye.detection.annotation.AlertFilter;
 import org.apache.pinot.thirdeye.detection.spi.model.AnomalySlice;
 import org.apache.pinot.thirdeye.detection.ConfigUtils;
 import org.apache.pinot.thirdeye.detection.DataProvider;
@@ -43,6 +44,7 @@
 import org.slf4j.LoggerFactory;
 
 
+@AlertFilter(type = "LEGACY_ALERTER_PIPELINE")
 public class LegacyAlertFilter extends DetectionAlertFilter {
   private final static Logger LOG = LoggerFactory.getLogger(LegacyAlertFilter.class);
 
diff --git a/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/alert/filter/ToAllRecipientsDetectionAlertFilter.java b/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/alert/filter/ToAllRecipientsDetectionAlertFilter.java
index e369ce609b..18a6771edf 100644
--- a/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/alert/filter/ToAllRecipientsDetectionAlertFilter.java
+++ b/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/alert/filter/ToAllRecipientsDetectionAlertFilter.java
@@ -32,11 +32,13 @@
 import java.util.Map;
 import java.util.Set;
 import org.apache.commons.collections.MapUtils;
+import org.apache.pinot.thirdeye.detection.annotation.AlertFilter;
 
 
 /**
  * The detection alert filter that sends the anomaly email to all recipients
  */
+@AlertFilter(type = "DEFAULT_ALERTER_PIPELINE")
 public class ToAllRecipientsDetectionAlertFilter extends StatefulDetectionAlertFilter {
 
   private static final String PROP_RECIPIENTS = "recipients";
diff --git a/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/annotation/AlertFilter.java b/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/annotation/AlertFilter.java
new file mode 100644
index 0000000000..0888e6f430
--- /dev/null
+++ b/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/annotation/AlertFilter.java
@@ -0,0 +1,35 @@
+/*
+ * 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.pinot.thirdeye.detection.annotation;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+
+@Target({ElementType.TYPE})
+@Retention(RetentionPolicy.RUNTIME)
+@Inherited
+public @interface AlertFilter {
+  @JsonProperty String type() default "";
+}
diff --git a/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/annotation/registry/DetectionAlertRegistry.java b/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/annotation/registry/DetectionAlertRegistry.java
index 0ef021a11a..fa1cd9890d 100644
--- a/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/annotation/registry/DetectionAlertRegistry.java
+++ b/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/annotation/registry/DetectionAlertRegistry.java
@@ -20,8 +20,11 @@
 package org.apache.pinot.thirdeye.detection.annotation.registry;
 
 import com.google.common.base.Preconditions;
+import org.apache.commons.collections.MapUtils;
+import org.apache.pinot.thirdeye.detection.alert.DetectionAlertFilter;
 import org.apache.pinot.thirdeye.detection.alert.scheme.DetectionAlertScheme;
 import org.apache.pinot.thirdeye.detection.alert.suppress.DetectionAlertSuppressor;
+import org.apache.pinot.thirdeye.detection.annotation.AlertFilter;
 import org.apache.pinot.thirdeye.detection.annotation.AlertScheme;
 import org.apache.pinot.thirdeye.detection.annotation.AlertSuppressor;
 import java.lang.annotation.Annotation;
@@ -45,6 +48,9 @@
   // Alert Suppressor type to Alert Suppressor class name
   private static final Map<String, String> ALERT_SUPPRESSOR_MAP = new HashMap<>();
 
+  // Alert Filter Type Map
+  private static final Map<String, String> ALERT_FILTER_MAP = new HashMap<>();
+
   private static final DetectionAlertRegistry INSTANCE = new DetectionAlertRegistry();
 
   public static DetectionAlertRegistry getInstance() {
@@ -58,6 +64,17 @@ public static void init() {
     try {
       Reflections reflections = new Reflections();
 
+      // register alert filters
+      Set<Class<? extends DetectionAlertFilter>> alertFilterClasses =
+          reflections.getSubTypesOf(DetectionAlertFilter.class);
+      for (Class clazz : alertFilterClasses) {
+        for (Annotation annotation : clazz.getAnnotations()) {
+          if (annotation instanceof AlertFilter) {
+            ALERT_FILTER_MAP.put(((AlertFilter) annotation).type(), clazz.getName());
+          }
+        }
+      }
+
       // register alert schemes
       Set<Class<? extends DetectionAlertScheme>> alertSchemeClasses =
           reflections.getSubTypesOf(DetectionAlertScheme.class);
@@ -84,6 +101,10 @@ public static void init() {
     }
   }
 
+  public void registerAlertFilter(String type, String className) {
+    ALERT_FILTER_MAP.put(type, className);
+  }
+
   public void registerAlertScheme(String type, String className) {
     ALERT_SCHEME_MAP.put(type, className);
   }
@@ -92,6 +113,15 @@ public void registerAlertSuppressor(String type, String className) {
     ALERT_SUPPRESSOR_MAP.put(type, className);
   }
 
+  /**
+   * Look up the class name for a given alert filter
+   * @param type the type used in the YAML configs
+   */
+  public String lookupAlertFilters(String type) {
+    Preconditions.checkArgument(ALERT_FILTER_MAP.containsKey(type.toUpperCase()), type + " not found in registry");
+    return ALERT_FILTER_MAP.get(type.toUpperCase());
+  }
+
   /**
    * Look up the {@link #ALERT_SCHEME_MAP} for the Alert scheme class name from the type
    */
diff --git a/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/validators/DetectionAlertConfigValidator.java b/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/validators/DetectionAlertConfigValidator.java
index 82bf497fc7..e432a5129d 100644
--- a/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/validators/DetectionAlertConfigValidator.java
+++ b/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/validators/DetectionAlertConfigValidator.java
@@ -30,6 +30,7 @@
 public class DetectionAlertConfigValidator extends ConfigValidator {
 
   private static final DetectionAlertConfigValidator INSTANCE = new DetectionAlertConfigValidator();
+  private static final String PROP_CLASS_NAME = "className";
 
   public static DetectionAlertConfigValidator getInstance() {
     return INSTANCE;
@@ -55,6 +56,11 @@ public boolean validateConfig(DetectionAlertConfigDTO alertConfig,  Map<String,
       responseMessage.put("message", "From address field cannot be left empty");
       return false;
     }
+    if (alertConfig.getProperties() == null || alertConfig.getProperties().get(PROP_CLASS_NAME) == null
+        || StringUtils.isEmpty(alertConfig.getProperties().get(PROP_CLASS_NAME).toString())) {
+      responseMessage.put("message", "'Type' field cannot be left empty.");
+      return false;
+    }
 
     // At least one alertScheme is required
     if (alertConfig.getAlertSchemes() == null || alertConfig.getAlertSchemes().size() == 0) {
diff --git a/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/yaml/YamlDetectionAlertConfigTranslator.java b/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/yaml/YamlDetectionAlertConfigTranslator.java
index bbbd1144ba..1cfa75c8bc 100644
--- a/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/yaml/YamlDetectionAlertConfigTranslator.java
+++ b/thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/yaml/YamlDetectionAlertConfigTranslator.java
@@ -64,7 +64,6 @@
   static final String PROP_TIME_WINDOWS = "timeWindows";
   static final String CRON_SCHEDULE_DEFAULT = "0 0/5 * * * ? *"; // Every 5 min
 
-  private static final DetectionRegistry DETECTION_REGISTRY = DetectionRegistry.getInstance();
   private static final DetectionAlertRegistry DETECTION_ALERT_REGISTRY = DetectionAlertRegistry.getInstance();
   private static final Set<String> PROPERTY_KEYS = new HashSet<>(
       Arrays.asList(PROP_DETECTION_CONFIG_IDS, PROP_RECIPIENTS, PROP_DIMENSION, PROP_DIMENSION_RECIPIENTS));
@@ -115,7 +114,7 @@ public DetectionAlertConfigDTO generateDetectionAlertConfig(Map<String, Object>
     Map<String, Object> properties = new HashMap<>();
     for (Map.Entry<String, Object> entry : alertYamlConfigs.entrySet()) {
       if (entry.getKey().equals(PROP_TYPE)) {
-        properties.put(PROP_CLASS_NAME, DETECTION_REGISTRY.lookup(MapUtils.getString(alertYamlConfigs, PROP_TYPE)));
+        properties.put(PROP_CLASS_NAME, DETECTION_ALERT_REGISTRY.lookupAlertFilters(MapUtils.getString(alertYamlConfigs, PROP_TYPE)));
       } else {
         if (PROPERTY_KEYS.contains(entry.getKey())) {
           properties.put(entry.getKey(), entry.getValue());
diff --git a/thirdeye/thirdeye-pinot/src/test/java/org/apache/pinot/thirdeye/detection/yaml/YamlDetectionAlertConfigTranslatorTest.java b/thirdeye/thirdeye-pinot/src/test/java/org/apache/pinot/thirdeye/detection/yaml/YamlDetectionAlertConfigTranslatorTest.java
index dc88d4852b..8aec34145d 100644
--- a/thirdeye/thirdeye-pinot/src/test/java/org/apache/pinot/thirdeye/detection/yaml/YamlDetectionAlertConfigTranslatorTest.java
+++ b/thirdeye/thirdeye-pinot/src/test/java/org/apache/pinot/thirdeye/detection/yaml/YamlDetectionAlertConfigTranslatorTest.java
@@ -132,10 +132,10 @@ public void testTranslateAlert() {
 
   @BeforeMethod
   public void setUp() {
-    DetectionRegistry.registerComponent("testclassname", "TO_ALL_RECIPIENTS");
+    DetectionAlertRegistry.getInstance().registerAlertFilter("DEFAULT_ALERTER_PIPELINE", "RECIPIENTClass");
     this.alertYamlConfigs = new HashMap<>();
     alertYamlConfigs.put(PROP_SUBS_GROUP_NAME, "test_alert");
-    alertYamlConfigs.put("type", "TO_ALL_RECIPIEnts");
+    alertYamlConfigs.put("type", "DEFAULT_ALerTeR_PipeLIne");
     Map<String, Object> recipients = new HashMap<>();
     recipients.put("to", Arrays.asList("test1", "test2"));
     alertYamlConfigs.put("recipients", recipients);
diff --git a/thirdeye/thirdeye-pinot/src/test/java/org/apache/pinot/thirdeye/detection/yaml/YamlResourceTest.java b/thirdeye/thirdeye-pinot/src/test/java/org/apache/pinot/thirdeye/detection/yaml/YamlResourceTest.java
index e5f59a23b8..946af6ad65 100644
--- a/thirdeye/thirdeye-pinot/src/test/java/org/apache/pinot/thirdeye/detection/yaml/YamlResourceTest.java
+++ b/thirdeye/thirdeye-pinot/src/test/java/org/apache/pinot/thirdeye/detection/yaml/YamlResourceTest.java
@@ -31,7 +31,7 @@ public void beforeClass() {
     DetectionAlertRegistry.getInstance().registerAlertScheme("EMAIL", "EmailClass");
     DetectionAlertRegistry.getInstance().registerAlertScheme("IRIS", "IrisClass");
     DetectionAlertRegistry.getInstance().registerAlertSuppressor("TIME_WINDOW", "TimeWindowClass");
-    DetectionRegistry.registerComponent("TimeWindowClass", "DIMENSIONAL_ALERTER_PIPELINE");
+    DetectionAlertRegistry.getInstance().registerAlertFilter("DIMENSIONAL_ALERTER_PIPELINE", "DimClass");
   }
 
   @AfterClass(alwaysRun = true)


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org