jihoonson closed pull request #6415: Add period drop before rule URL: https://github.com/apache/incubator-druid/pull/6415
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/docs/content/operations/rule-configuration.md b/docs/content/operations/rule-configuration.md index 404e583663f..8d5c95d0f23 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 00000000000..6654b385eac --- /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 cbde064b447..f2c52a1eb86 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 @@ @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 81e77af4137..49841cbe51b 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 00000000000..c0d64cb11bc --- /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 + ) + ); + } +} ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@druid.apache.org For additional commands, e-mail: commits-h...@druid.apache.org