You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by ab...@apache.org on 2024/09/10 09:31:59 UTC
(druid) branch master updated: Implement segment range threshold for automatic query prioritization (#17009)
This is an automated email from the ASF dual-hosted git repository.
abhishek pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/druid.git
The following commit(s) were added to refs/heads/master by this push:
new 2427972c102 Implement segment range threshold for automatic query prioritization (#17009)
2427972c102 is described below
commit 2427972c102dc45246b8373466fe81397e7a0b66
Author: aho135 <as...@ucsd.edu>
AuthorDate: Tue Sep 10 02:31:52 2024 -0700
Implement segment range threshold for automatic query prioritization (#17009)
Implements threshold based automatic query prioritization using the time period of the actual segments scanned. This differs from the current implementation of durationThreshold which uses the duration in the user supplied query. There are some usability constraints with using durationThreshold from the user supplied query, especially when using SQL. For example, if a client does not explicitly specify both start and end timestamps then the duration is extremely large and will always [...]
"interval":["2024-08-30T08:05:41.944Z/146140482-04-24T15:36:27.903Z"]. This interval is generated from a query like SELECT * FROM table WHERE __time > CURRENT_TIMESTAMP - INTERVAL '15' HOUR. Using the time period of the actual segments scanned allows proper prioritization without explicitly having to specify start and end timestamps. This PR adds onto #9493
---
docs/configuration/index.md | 1 +
.../ThresholdBasedQueryPrioritizationStrategy.java | 22 +++++++--
.../org/apache/druid/server/QueryResourceTest.java | 2 +-
.../apache/druid/server/QuerySchedulerTest.java | 2 +-
...esholdBasedQueryPrioritizationStrategyTest.java | 57 +++++++++++++++++++++-
5 files changed, 78 insertions(+), 6 deletions(-)
diff --git a/docs/configuration/index.md b/docs/configuration/index.md
index 64797c402c0..57b88b55fc2 100644
--- a/docs/configuration/index.md
+++ b/docs/configuration/index.md
@@ -1795,6 +1795,7 @@ This strategy can be enabled by setting `druid.query.scheduler.prioritization.st
|`druid.query.scheduler.prioritization.periodThreshold`|ISO duration threshold for how old data can be queried before automatically adjusting query priority.|none|
|`druid.query.scheduler.prioritization.durationThreshold`|ISO duration threshold for maximum duration a queries interval can span before the priority is automatically adjusted.|none|
|`druid.query.scheduler.prioritization.segmentCountThreshold`|Number threshold for maximum number of segments that can take part in a query before its priority is automatically adjusted.|none|
+|`druid.query.scheduler.prioritization.segmentRangeThreshold`|ISO duration threshold for maximum segment range a query can span before the priority is automatically adjusted.|none|
|`druid.query.scheduler.prioritization.adjustment`|Amount to reduce the priority of queries which cross any threshold.|none|
##### Laning strategies
diff --git a/server/src/main/java/org/apache/druid/server/scheduling/ThresholdBasedQueryPrioritizationStrategy.java b/server/src/main/java/org/apache/druid/server/scheduling/ThresholdBasedQueryPrioritizationStrategy.java
index 3f76951352c..9d3909b9358 100644
--- a/server/src/main/java/org/apache/druid/server/scheduling/ThresholdBasedQueryPrioritizationStrategy.java
+++ b/server/src/main/java/org/apache/druid/server/scheduling/ThresholdBasedQueryPrioritizationStrategy.java
@@ -30,6 +30,7 @@ import org.apache.druid.server.QueryPrioritizationStrategy;
import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.Period;
+import org.joda.time.base.AbstractInterval;
import javax.annotation.Nullable;
@@ -49,12 +50,14 @@ public class ThresholdBasedQueryPrioritizationStrategy implements QueryPrioritiz
private final Optional<Duration> periodThreshold;
private final Optional<Duration> durationThreshold;
+ private final Optional<Duration> segmentRangeThreshold;
@JsonCreator
public ThresholdBasedQueryPrioritizationStrategy(
@JsonProperty("periodThreshold") @Nullable String periodThresholdString,
@JsonProperty("durationThreshold") @Nullable String durationThresholdString,
@JsonProperty("segmentCountThreshold") @Nullable Integer segmentCountThreshold,
+ @JsonProperty("segmentRangeThreshold") @Nullable String segmentRangeThresholdString,
@JsonProperty("adjustment") @Nullable Integer adjustment
)
{
@@ -66,9 +69,12 @@ public class ThresholdBasedQueryPrioritizationStrategy implements QueryPrioritiz
this.durationThreshold = durationThresholdString == null
? Optional.empty()
: Optional.of(new Period(durationThresholdString).toStandardDuration());
+ this.segmentRangeThreshold = segmentRangeThresholdString == null
+ ? Optional.empty()
+ : Optional.of(new Period(segmentRangeThresholdString).toStandardDuration());
Preconditions.checkArgument(
- segmentCountThreshold != null || periodThreshold.isPresent() || durationThreshold.isPresent(),
- "periodThreshold, durationThreshold, or segmentCountThreshold must be set"
+ segmentCountThreshold != null || periodThreshold.isPresent() || durationThreshold.isPresent() || segmentRangeThreshold.isPresent(),
+ "periodThreshold, durationThreshold, segmentCountThreshold or segmentRangeThreshold must be set"
);
}
@@ -84,9 +90,19 @@ public class ThresholdBasedQueryPrioritizationStrategy implements QueryPrioritiz
}).orElse(false);
final boolean violatesDurationThreshold =
durationThreshold.map(duration -> theQuery.getDuration().isLongerThan(duration)).orElse(false);
+ boolean violatesSegmentRangeThreshold = false;
+ if (segmentRangeThreshold.isPresent()) {
+ long segmentRange = segments.stream().filter(segment -> segment.getSegmentDescriptor() != null)
+ .map(segment -> segment.getSegmentDescriptor().getInterval())
+ .distinct()
+ .mapToLong(AbstractInterval::toDurationMillis)
+ .sum();
+ violatesSegmentRangeThreshold =
+ segmentRangeThreshold.map(duration -> new Duration(segmentRange).isLongerThan(duration)).orElse(false);
+ }
boolean violatesSegmentThreshold = segments.size() > segmentCountThreshold;
- if (violatesPeriodThreshold || violatesDurationThreshold || violatesSegmentThreshold) {
+ if (violatesPeriodThreshold || violatesDurationThreshold || violatesSegmentThreshold || violatesSegmentRangeThreshold) {
final int adjustedPriority = theQuery.context().getPriority() - adjustment;
return Optional.of(adjustedPriority);
}
diff --git a/server/src/test/java/org/apache/druid/server/QueryResourceTest.java b/server/src/test/java/org/apache/druid/server/QueryResourceTest.java
index 43bce13e2c8..f4746032047 100644
--- a/server/src/test/java/org/apache/druid/server/QueryResourceTest.java
+++ b/server/src/test/java/org/apache/druid/server/QueryResourceTest.java
@@ -1190,7 +1190,7 @@ public class QueryResourceTest
final CountDownLatch waitOneScheduled = new CountDownLatch(1);
final QueryScheduler scheduler = new QueryScheduler(
40,
- new ThresholdBasedQueryPrioritizationStrategy(null, "P90D", null, null),
+ new ThresholdBasedQueryPrioritizationStrategy(null, "P90D", null, null, null),
new HiLoQueryLaningStrategy(1),
new ServerConfig()
);
diff --git a/server/src/test/java/org/apache/druid/server/QuerySchedulerTest.java b/server/src/test/java/org/apache/druid/server/QuerySchedulerTest.java
index d7af61370be..b670cd4dd78 100644
--- a/server/src/test/java/org/apache/druid/server/QuerySchedulerTest.java
+++ b/server/src/test/java/org/apache/druid/server/QuerySchedulerTest.java
@@ -553,7 +553,7 @@ public class QuerySchedulerTest
Assert.assertEquals(
"Unable to provision, see the following errors:\n"
+ "\n"
- + "1) Problem parsing object at prefix[druid.query.scheduler]: Cannot construct instance of `org.apache.druid.server.scheduling.ThresholdBasedQueryPrioritizationStrategy`, problem: periodThreshold, durationThreshold, or segmentCountThreshold must be set\n"
+ + "1) Problem parsing object at prefix[druid.query.scheduler]: Cannot construct instance of `org.apache.druid.server.scheduling.ThresholdBasedQueryPrioritizationStrategy`, problem: periodThreshold, durationThreshold, segmentCountThreshold or segmentRangeThreshold must be set\n"
+ " at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: org.apache.druid.server.QuerySchedulerProvider[\"prioritization\"]).\n"
+ "\n"
+ "1 error",
diff --git a/server/src/test/java/org/apache/druid/server/scheduling/ThresholdBasedQueryPrioritizationStrategyTest.java b/server/src/test/java/org/apache/druid/server/scheduling/ThresholdBasedQueryPrioritizationStrategyTest.java
index 705337feae2..1cc46783d03 100644
--- a/server/src/test/java/org/apache/druid/server/scheduling/ThresholdBasedQueryPrioritizationStrategyTest.java
+++ b/server/src/test/java/org/apache/druid/server/scheduling/ThresholdBasedQueryPrioritizationStrategyTest.java
@@ -27,6 +27,7 @@ import org.apache.druid.java.util.common.DateTimes;
import org.apache.druid.java.util.common.granularity.Granularities;
import org.apache.druid.query.Druids;
import org.apache.druid.query.QueryPlus;
+import org.apache.druid.query.SegmentDescriptor;
import org.apache.druid.query.aggregation.CountAggregatorFactory;
import org.apache.druid.query.timeseries.TimeseriesQuery;
import org.apache.druid.server.QueryPrioritizationStrategy;
@@ -59,7 +60,7 @@ public class ThresholdBasedQueryPrioritizationStrategyTest
public void testPrioritizationPeriodThresholdInsidePeriod()
{
QueryPrioritizationStrategy strategy = new ThresholdBasedQueryPrioritizationStrategy(
- "P90D", null, null, adjustment);
+ "P90D", null, null, null, adjustment);
DateTime startDate = DateTimes.nowUtc().minusDays(1);
DateTime endDate = DateTimes.nowUtc();
TimeseriesQuery query = queryBuilder.intervals(ImmutableList.of(new Interval(startDate, endDate)))
@@ -79,6 +80,7 @@ public class ThresholdBasedQueryPrioritizationStrategyTest
"P90D",
null,
null,
+ null,
adjustment
);
DateTime startDate = DateTimes.nowUtc().minusDays(100);
@@ -101,6 +103,7 @@ public class ThresholdBasedQueryPrioritizationStrategyTest
null,
"P7D",
null,
+ null,
adjustment
);
DateTime startDate = DateTimes.nowUtc().minusDays(1);
@@ -122,6 +125,7 @@ public class ThresholdBasedQueryPrioritizationStrategyTest
null,
"P7D",
null,
+ null,
adjustment
);
DateTime startDate = DateTimes.nowUtc().minusDays(20);
@@ -144,6 +148,7 @@ public class ThresholdBasedQueryPrioritizationStrategyTest
null,
null,
2,
+ null,
adjustment
);
DateTime startDate = DateTimes.nowUtc().minusDays(1);
@@ -168,6 +173,7 @@ public class ThresholdBasedQueryPrioritizationStrategyTest
null,
null,
2,
+ null,
adjustment
);
DateTime startDate = DateTimes.nowUtc().minusDays(20);
@@ -189,4 +195,53 @@ public class ThresholdBasedQueryPrioritizationStrategyTest
).get()
);
}
+
+ @Test
+ public void testPrioritizationSegmentRangeWithinThreshold()
+ {
+ QueryPrioritizationStrategy strategy = new ThresholdBasedQueryPrioritizationStrategy(
+ null,
+ null,
+ null,
+ "P7D",
+ adjustment
+ );
+ DateTime startDate = DateTimes.nowUtc().minusDays(1);
+ DateTime endDate = DateTimes.nowUtc();
+ TimeseriesQuery query = queryBuilder.intervals(ImmutableList.of(new Interval(startDate, endDate)))
+ .granularity(Granularities.MINUTE)
+ .context(ImmutableMap.of())
+ .build();
+ SegmentServerSelector segmentServerSelector = EasyMock.createMock(SegmentServerSelector.class);
+ EasyMock.expect(segmentServerSelector.getSegmentDescriptor()).andReturn(new SegmentDescriptor(new Interval(startDate, endDate), "", 0)).times(2);
+ EasyMock.replay(segmentServerSelector);
+ Assert.assertFalse(
+ strategy.computePriority(QueryPlus.wrap(query), ImmutableSet.of(segmentServerSelector)).isPresent()
+ );
+ }
+
+ @Test
+ public void testPrioritizationSegmentRangeOverThreshold()
+ {
+ QueryPrioritizationStrategy strategy = new ThresholdBasedQueryPrioritizationStrategy(
+ null,
+ null,
+ null,
+ "P7D",
+ adjustment
+ );
+ DateTime startDate = DateTimes.nowUtc().minusDays(20);
+ DateTime endDate = DateTimes.nowUtc();
+ TimeseriesQuery query = queryBuilder.intervals(ImmutableList.of(new Interval(startDate, endDate)))
+ .granularity(Granularities.HOUR)
+ .context(ImmutableMap.of())
+ .build();
+ SegmentServerSelector segmentServerSelector = EasyMock.createMock(SegmentServerSelector.class);
+ EasyMock.expect(segmentServerSelector.getSegmentDescriptor()).andReturn(new SegmentDescriptor(new Interval(startDate, endDate), "", 0)).times(2);
+ EasyMock.replay(segmentServerSelector);
+ Assert.assertEquals(
+ -adjustment,
+ (int) strategy.computePriority(QueryPlus.wrap(query), ImmutableSet.of(segmentServerSelector)).get()
+ );
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org