This is an automated email from the ASF dual-hosted git repository.

jihoonson pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-druid.git


The following commit(s) were added to refs/heads/master by this push:
     new 601183b  Add period drop before rule (#6415)
601183b is described below

commit 601183b4c7b2a30c43c0eca41ea8a0b67ef5cd40
Author: QiuMM <csurj...@gmail.com>
AuthorDate: Thu Oct 25 03:44:30 2018 +0800

    Add period drop before rule (#6415)
    
    * Add period drop before rule
    
    * add license header
    
    * support period drop before rule in coordinator console
    
    * address comments
---
 docs/content/operations/rule-configuration.md      | 16 ++++
 .../coordinator/rules/PeriodDropBeforeRule.java    | 66 +++++++++++++++++
 .../druid/server/coordinator/rules/Rule.java       |  1 +
 .../resources/static/old-console/js/rules-0.0.2.js |  8 ++
 .../rules/PeriodDropBeforeRuleTest.java            | 86 ++++++++++++++++++++++
 5 files changed, 177 insertions(+)

diff --git a/docs/content/operations/rule-configuration.md 
b/docs/content/operations/rule-configuration.md
index 404e583..8d5c95d 100644
--- a/docs/content/operations/rule-configuration.md
+++ b/docs/content/operations/rule-configuration.md
@@ -129,6 +129,22 @@ Period drop rules are of the form:
 
 The interval of a segment will be compared against the specified period. The 
period is from some time in the past to the current time. The rule matches if 
the period contains the interval.
 
+### Period Drop Before Rule
+
+Period drop before rules are of the form:
+
+```json
+{
+  "type" : "dropBeforeByPeriod",
+  "period" : "P1M"
+}
+```
+
+* `type` - this should always be "dropBeforeByPeriod"
+* `period` - A JSON Object representing ISO-8601 Periods
+
+The interval of a segment will be compared against the specified period. The 
period is from some time in the past to the current time. The rule matches if 
the interval before the period. If you just want to retain recent data, you can 
use this rule to drop the old data that before a specified period and add a 
`loadForever` rule to follow it. Notes, `dropBeforeByPeriod + loadForever` is 
equivalent to `loadByPeriod(includeFuture = true) + dropForever`.
+
 Broadcast Rules
 ---------------
 
diff --git 
a/server/src/main/java/org/apache/druid/server/coordinator/rules/PeriodDropBeforeRule.java
 
b/server/src/main/java/org/apache/druid/server/coordinator/rules/PeriodDropBeforeRule.java
new file mode 100644
index 0000000..6654b38
--- /dev/null
+++ 
b/server/src/main/java/org/apache/druid/server/coordinator/rules/PeriodDropBeforeRule.java
@@ -0,0 +1,66 @@
+/*
+ * 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.druid.server.coordinator.rules;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.apache.druid.timeline.DataSegment;
+import org.joda.time.DateTime;
+import org.joda.time.Interval;
+import org.joda.time.Period;
+
+public class PeriodDropBeforeRule extends DropRule
+{
+  private final Period period;
+
+  @JsonCreator
+  public PeriodDropBeforeRule(
+      @JsonProperty("period") Period period
+  )
+  {
+    this.period = period;
+  }
+
+  @Override
+  @JsonProperty
+  public String getType()
+  {
+    return "dropBeforeByPeriod";
+  }
+
+  @JsonProperty
+  public Period getPeriod()
+  {
+    return period;
+  }
+
+  @Override
+  public boolean appliesTo(DataSegment segment, DateTime referenceTimestamp)
+  {
+    return appliesTo(segment.getInterval(), referenceTimestamp);
+  }
+
+  @Override
+  public boolean appliesTo(Interval theInterval, DateTime referenceTimestamp)
+  {
+    final DateTime periodAgo = referenceTimestamp.minus(period);
+    return theInterval.getEndMillis() <= periodAgo.getMillis();
+  }
+}
diff --git 
a/server/src/main/java/org/apache/druid/server/coordinator/rules/Rule.java 
b/server/src/main/java/org/apache/druid/server/coordinator/rules/Rule.java
index cbde064..f2c52a1 100644
--- a/server/src/main/java/org/apache/druid/server/coordinator/rules/Rule.java
+++ b/server/src/main/java/org/apache/druid/server/coordinator/rules/Rule.java
@@ -36,6 +36,7 @@ import org.joda.time.Interval;
     @JsonSubTypes.Type(name = "loadByInterval", value = 
IntervalLoadRule.class),
     @JsonSubTypes.Type(name = "loadForever", value = ForeverLoadRule.class),
     @JsonSubTypes.Type(name = "dropByPeriod", value = PeriodDropRule.class),
+    @JsonSubTypes.Type(name = "dropBeforeByPeriod", value = 
PeriodDropBeforeRule.class),
     @JsonSubTypes.Type(name = "dropByInterval", value = 
IntervalDropRule.class),
     @JsonSubTypes.Type(name = "dropForever", value = ForeverDropRule.class),
     @JsonSubTypes.Type(name = ForeverBroadcastDistributionRule.TYPE, value = 
ForeverBroadcastDistributionRule.class),
diff --git a/server/src/main/resources/static/old-console/js/rules-0.0.2.js 
b/server/src/main/resources/static/old-console/js/rules-0.0.2.js
index 81e77af..49841cb 100644
--- a/server/src/main/resources/static/old-console/js/rules-0.0.2.js
+++ b/server/src/main/resources/static/old-console/js/rules-0.0.2.js
@@ -24,6 +24,7 @@ var ruleTypes = [
   "loadByPeriod",
   "dropByInterval",
   "dropByPeriod",
+  "dropBeforeByPeriod",
   "loadForever",
   "dropForever",
   "JSON"
@@ -84,6 +85,9 @@ function makeRuleBody(rule) {
       case "dropByPeriod":
         retVal += makeDropByPeriod(rule);
         break;
+      case "dropBeforeByPeriod":
+        retVal += makeDropBeforeByPeriod(rule);
+        break;
       case "dropForever":
         retVal += "";
         break;
@@ -151,6 +155,10 @@ function makeDropByPeriod(rule) {
   return "<span class='rule_label'>period</span><input type='text' 
name='period' " + "value='" + rule.period + "'/>";
 }
 
+function makeDropBeforeByPeriod(rule) {
+  return "<span class='rule_label'>period</span><input type='text' 
name='period' " + "value='" + rule.period + "'/>";
+}
+
 function makeJSON() {
   return "<span class='rule_label'>JSON</span><input type='text' 
class='very_long_text' name='JSON'/>";
 }
diff --git 
a/server/src/test/java/org/apache/druid/server/coordinator/rules/PeriodDropBeforeRuleTest.java
 
b/server/src/test/java/org/apache/druid/server/coordinator/rules/PeriodDropBeforeRuleTest.java
new file mode 100644
index 0000000..c0d64cb
--- /dev/null
+++ 
b/server/src/test/java/org/apache/druid/server/coordinator/rules/PeriodDropBeforeRuleTest.java
@@ -0,0 +1,86 @@
+/*
+ * 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.druid.server.coordinator.rules;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.druid.jackson.DefaultObjectMapper;
+import org.apache.druid.java.util.common.DateTimes;
+import org.apache.druid.timeline.DataSegment;
+import org.apache.druid.timeline.partition.NoneShardSpec;
+import org.joda.time.DateTime;
+import org.joda.time.Interval;
+import org.joda.time.Period;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class PeriodDropBeforeRuleTest
+{
+  private static final DataSegment.Builder builder = DataSegment.builder()
+                                                                
.dataSource("test")
+                                                                
.version(DateTimes.of("2012-12-31T01:00:00").toString())
+                                                                
.shardSpec(NoneShardSpec.instance());
+
+  @Test
+  public void testSerde() throws Exception
+  {
+    PeriodDropBeforeRule rule = new PeriodDropBeforeRule(
+        new Period("P1D")
+    );
+
+    ObjectMapper jsonMapper = new DefaultObjectMapper();
+    Rule reread = jsonMapper.readValue(jsonMapper.writeValueAsString(rule), 
Rule.class);
+
+    Assert.assertEquals(rule.getPeriod(), ((PeriodDropBeforeRule) 
reread).getPeriod());
+  }
+
+  @Test
+  public void testDropBefore()
+  {
+    DateTime now = DateTimes.of("2012-12-31T01:00:00");
+    PeriodDropBeforeRule rule = new PeriodDropBeforeRule(
+        new Period("P1D")
+    );
+
+    Assert.assertTrue(
+        rule.appliesTo(
+            builder.interval(new Interval(now.minusDays(3), 
now.minusDays(2))).build(),
+            now
+        )
+    );
+    Assert.assertTrue(
+        rule.appliesTo(
+            builder.interval(new Interval(now.minusDays(2), 
now.minusDays(1))).build(),
+            now
+        )
+    );
+    Assert.assertFalse(
+        rule.appliesTo(
+            builder.interval(new Interval(now.minusDays(1), now)).build(),
+            now
+        )
+    );
+    Assert.assertFalse(
+        rule.appliesTo(
+            builder.interval(new Interval(now, now.plusDays(1))).build(),
+            now
+        )
+    );
+  }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@druid.apache.org
For additional commands, e-mail: commits-h...@druid.apache.org

Reply via email to