ValidationManager { public static List<Interval> computeMissingIntervals(List<Interval> segmentIntervals, Duration frequency) { if (frequency == null) { return Collections.emptyList(); } if (frequency.getMillis() < Duration.standardHours(1).getMillis()) { frequency = Duration.standardDays(1); } if (segmentIntervals.size() < 2) { return Collections.emptyList(); } List<Interval> sortedSegmentIntervals = new ArrayList<Interval>(segmentIntervals); Collections.sort(sortedSegmentIntervals, new Comparator<Interval>() { @Override public int compare(Interval first, Interval second) { if (first.getStartMillis() < second.getStartMillis()) return -1; else if (second.getStartMillis() < first.getStartMillis()) return 1; return 0; } }); final long startTime = sortedSegmentIntervals.get(0).getStartMillis(); long endTime = Long.MIN_VALUE; for (Interval sortedSegmentInterval : sortedSegmentIntervals) { if (endTime < sortedSegmentInterval.getEndMillis()) { endTime = sortedSegmentInterval.getEndMillis(); } } final long frequencyMillis = frequency.getMillis(); int lastEndIntervalCount = 0; List<Interval> missingIntervals = new ArrayList<Interval>(10); for (Interval segmentInterval : sortedSegmentIntervals) { int startIntervalCount = (int) ((segmentInterval.getStartMillis() - startTime) / frequencyMillis); int endIntervalCount = (int) ((segmentInterval.getEndMillis() - startTime) / frequencyMillis); if (lastEndIntervalCount < startIntervalCount - 1) { for (int missingIntervalIndex = lastEndIntervalCount + 1; missingIntervalIndex < startIntervalCount; ++missingIntervalIndex) { missingIntervals.add(new Interval(startTime + frequencyMillis * missingIntervalIndex, startTime + frequencyMillis * (missingIntervalIndex + 1) - 1)); } } lastEndIntervalCount = Math.max(lastEndIntervalCount, endIntervalCount); } return missingIntervals; } ValidationManager(ValidationMetrics validationMetrics, PinotHelixResourceManager pinotHelixResourceManager,
      ControllerConf config); void start(); void stop(); void runValidation(); static List<Interval> computeMissingIntervals(List<Interval> segmentIntervals, Duration frequency); static int countMissingSegments(long[] sortedStartTimes, TimeUnit frequency);  }
@Test public void testComputeMissingIntervals() { Interval jan1st = new Interval(new DateTime(2015, 1, 1, 0, 0, 0), new DateTime(2015, 1, 1, 23, 59, 59)); Interval jan2nd = new Interval(new DateTime(2015, 1, 2, 0, 0, 0), new DateTime(2015, 1, 2, 23, 59, 59)); Interval jan3rd = new Interval(new DateTime(2015, 1, 3, 0, 0, 0), new DateTime(2015, 1, 3, 23, 59, 59)); Interval jan4th = new Interval(new DateTime(2015, 1, 4, 0, 0, 0), new DateTime(2015, 1, 4, 23, 59, 59)); Interval jan5th = new Interval(new DateTime(2015, 1, 5, 0, 0, 0), new DateTime(2015, 1, 5, 23, 59, 59)); ArrayList<Interval> jan1st2nd3rd = new ArrayList<Interval>(); jan1st2nd3rd.add(jan1st); jan1st2nd3rd.add(jan2nd); jan1st2nd3rd.add(jan3rd); List<Interval> missingIntervalsForJan1st2nd3rd = ValidationManager.computeMissingIntervals(jan1st2nd3rd, Duration.standardDays(1)); Assert.assertTrue(missingIntervalsForJan1st2nd3rd.isEmpty()); ArrayList<Interval> jan1st2nd3rd5th = new ArrayList<Interval>(jan1st2nd3rd); jan1st2nd3rd5th.add(jan5th); List<Interval> missingIntervalsForJan1st2nd3rd5th = ValidationManager.computeMissingIntervals(jan1st2nd3rd5th, Duration.standardDays(1)); Assert.assertEquals(missingIntervalsForJan1st2nd3rd5th.size(), 1); ArrayList<Interval> jan5th2nd1st = new ArrayList<Interval>(); jan5th2nd1st.add(jan5th); jan5th2nd1st.add(jan2nd); jan5th2nd1st.add(jan1st); List<Interval> missingIntervalsForJan5th2nd1st = ValidationManager.computeMissingIntervals(jan5th2nd1st, Duration.standardDays(1)); Assert.assertEquals(missingIntervalsForJan5th2nd1st.size(), 2); Interval jan1stAnd2nd = new Interval(new DateTime(2015, 1, 1, 0, 0, 0), new DateTime(2015, 1, 2, 23, 59, 59)); ArrayList<Interval> jan1st2nd4th5th = new ArrayList<Interval>(); jan1st2nd4th5th.add(jan1stAnd2nd); jan1st2nd4th5th.add(jan4th); jan1st2nd4th5th.add(jan5th); List<Interval> missingIntervalsForJan1st2nd4th5th = ValidationManager.computeMissingIntervals(jan1st2nd4th5th, Duration.standardDays(1)); Assert.assertEquals(missingIntervalsForJan1st2nd4th5th.size(), 1); }
@Test public void testExtremeSenario() { List<Interval> intervals = new ArrayList<Interval>(); intervals.add(new Interval(1, 2)); intervals.add(new Interval(Integer.MAX_VALUE - 5, Integer.MAX_VALUE)); intervals.add(new Interval(Integer.MAX_VALUE / 2 - 5, Integer.MAX_VALUE / 2)); Duration frequency = new Duration(1); List<Interval> computeMissingIntervals = ValidationManager.computeMissingIntervals(intervals, frequency); Assert.assertEquals(computeMissingIntervals.size(), 22); }