jihoonson commented on a change in pull request #12334:
URL: https://github.com/apache/druid/pull/12334#discussion_r827680502



##########
File path: 
server/src/main/java/org/apache/druid/client/indexing/ClientCompactionIntervalSpec.java
##########
@@ -44,12 +45,27 @@
   @Nullable
   private final String sha256OfSortedSegmentIds;
 
-  public static ClientCompactionIntervalSpec fromSegments(List<DataSegment> 
segments)
+  public static ClientCompactionIntervalSpec fromSegments(List<DataSegment> 
segments, Granularity segmentGranularity)
   {
+    Interval interval = 
JodaUtils.umbrellaInterval(segments.stream().map(DataSegment::getInterval).collect(Collectors.toList()));
+    if (segmentGranularity != null) {
+      // If segmentGranularity is set, then the segmentGranularity of the 
segments may not align with the configured segmentGranularity
+      // We must adjust the interval of the compaction task to fully cover and 
align with the segmentGranularity
+      // For example,
+      // - The umbrella interval of the segments is 2015-04-11/2015-04-12 but 
configured segmentGranularity is YEAR,
+      // if the compaction task's interval is 2015-04-11/2015-04-12 then we 
can run into race condition where after submitting
+      // the compaction task, a new segment outside of the interval (i.e. 
2015-02-11/2015-02-12) got created will be lost as it is
+      // overshadowed by the compacted segment (compacted segment has interval 
2015-01-01/2016-01-01.
+      // Hence, in this case, we must adjust the compaction task interval to 
2015-01-01/2016-01-01.
+      // - The umbrella interval of the segments is 2015-02-01/2015-03-01 but 
configured segmentGranularity is MONTH,
+      // if the compaction task's interval is 2015-02-01/2015-03-01 then 
compacted segments created will be
+      // 2015-01-26/2015-02-02, 2015-02-02/2015-02-09, 2015-02-09/2015-02-16, 
2015-02-16/2015-02-23, 2015-02-23/2015-03-02.
+      // The compacted segment would cause existing data from 2015-01-26 to 
2015-02-01 and 2015-03-01 to 2015-03-02 to be lost.
+      //  Hence, in this case, we must adjust the compaction task interval to 
2015-01-26/2015-03-02
+      interval = 
JodaUtils.umbrellaInterval(segmentGranularity.getIterable(interval));

Review comment:
       I agree that the guardrail should be somewhere else than here.

##########
File path: core/src/main/java/org/apache/druid/java/util/common/JodaUtils.java
##########
@@ -182,17 +185,16 @@ private static void verifyAscendingSortOrder(Interval 
previous, Interval current
 
   public static Interval umbrellaInterval(Iterable<Interval> intervals)
   {
-    ArrayList<DateTime> startDates = new ArrayList<>();
-    ArrayList<DateTime> endDates = new ArrayList<>();
+    DateTimeComparator dateTimeComp = DateTimeComparator.getInstance();
+
+    DateTime minStart = new DateTime(Long.MAX_VALUE);
+    DateTime maxEnd = new DateTime(Long.MIN_VALUE);
 
     for (Interval interval : intervals) {
-      startDates.add(interval.getStart());
-      endDates.add(interval.getEnd());
+      minStart = Collections.min(ImmutableList.of(minStart, 
interval.getStart()), dateTimeComp);
+      maxEnd = Collections.max(ImmutableList.of(maxEnd, interval.getEnd()), 
dateTimeComp);
     }
 
-    DateTime minStart = minDateTime(startDates.toArray(new DateTime[0]));
-    DateTime maxEnd = maxDateTime(endDates.toArray(new DateTime[0]));
-
     if (minStart == null || maxEnd == null) {

Review comment:
       This function now will return an interval of `Long.MIN_VALUE, 
Long.MAX_VALUE` when the input `intervals` is empty. It should throw an 
exception instead.

##########
File path: core/src/main/java/org/apache/druid/java/util/common/JodaUtils.java
##########
@@ -182,17 +185,16 @@ private static void verifyAscendingSortOrder(Interval 
previous, Interval current
 
   public static Interval umbrellaInterval(Iterable<Interval> intervals)
   {
-    ArrayList<DateTime> startDates = new ArrayList<>();
-    ArrayList<DateTime> endDates = new ArrayList<>();
+    DateTimeComparator dateTimeComp = DateTimeComparator.getInstance();
+
+    DateTime minStart = new DateTime(Long.MAX_VALUE);
+    DateTime maxEnd = new DateTime(Long.MIN_VALUE);
 
     for (Interval interval : intervals) {
-      startDates.add(interval.getStart());
-      endDates.add(interval.getEnd());
+      minStart = Collections.min(ImmutableList.of(minStart, 
interval.getStart()), dateTimeComp);
+      maxEnd = Collections.max(ImmutableList.of(maxEnd, interval.getEnd()), 
dateTimeComp);
     }
 
-    DateTime minStart = minDateTime(startDates.toArray(new DateTime[0]));
-    DateTime maxEnd = maxDateTime(endDates.toArray(new DateTime[0]));
-
     if (minStart == null || maxEnd == null) {

Review comment:
       BTW, I'm OK if you don't want to make this change in this PR. It's 
optional and up to you.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to