Skip to content

Commit

Permalink
fix interval error message
Browse files Browse the repository at this point in the history
  • Loading branch information
rash67 committed Dec 14, 2023
1 parent 857693f commit a3fc2f3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.apache.druid.java.util.common;

import com.google.common.collect.ImmutableList;
import org.apache.druid.error.InvalidInput;
import org.apache.druid.java.util.common.guava.Comparators;
import org.joda.time.DateTime;
import org.joda.time.Interval;
Expand All @@ -39,7 +40,12 @@ public static Interval utc(long startInstant, long endInstant)

public static Interval of(String interval)
{
return new Interval(interval, ISOChronology.getInstanceUTC());
try {
return new Interval(interval, ISOChronology.getInstanceUTC());
}
catch (IllegalArgumentException e) {
throw InvalidInput.exception(e, "Bad Interval[%s]: [%s]", interval, e.getMessage());
}
}

public static Interval of(String format, Object... formatArgs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.apache.druid.java.util.common;

import org.apache.druid.error.DruidException;
import org.apache.druid.java.util.common.guava.Comparators;
import org.joda.time.Interval;
import org.junit.Assert;
Expand Down Expand Up @@ -78,4 +79,16 @@ public void testFindOverlappingInterval()
);
}

@Test(expected = DruidException.class)
public void testInvalidInterval()
{
try {
Intervals.of("invalid string");
}
catch (DruidException e) {
Assert.assertTrue(e.getMessage().contains("Bad Interval[invalid string]"));

throw e;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.common.collect.Iterables;
import org.apache.druid.client.DataSourcesSnapshot;
import org.apache.druid.client.ImmutableDruidDataSource;
import org.apache.druid.error.DruidExceptionMatcher;
import org.apache.druid.java.util.common.DateTimes;
import org.apache.druid.java.util.common.Intervals;
import org.apache.druid.java.util.common.StringUtils;
Expand Down Expand Up @@ -697,7 +698,7 @@ public void testMarkAsUsedNonOvershadowedSegmentsInInterval() throws IOException
);
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testMarkAsUsedNonOvershadowedSegmentsInIntervalWithInvalidInterval() throws IOException
{
sqlSegmentsMetadataManager.startPollingDatabasePeriodically();
Expand All @@ -712,8 +713,12 @@ public void testMarkAsUsedNonOvershadowedSegmentsInIntervalWithInvalidInterval()
publish(newSegment1, false);
publish(newSegment2, false);
// invalid interval start > end
final Interval theInterval = Intervals.of("2017-10-22T00:00:00.000/2017-10-02T00:00:00.000");
sqlSegmentsMetadataManager.markAsUsedNonOvershadowedSegmentsInInterval(newDataSource, theInterval);
DruidExceptionMatcher.invalidInput().assertThrowsAndMatches(
() -> {
Interval theInterval = Intervals.of("2017-10-22T00:00:00.000/2017-10-02T00:00:00.000");
sqlSegmentsMetadataManager.markAsUsedNonOvershadowedSegmentsInInterval(newDataSource, theInterval);
}
);
}

@Test
Expand Down Expand Up @@ -833,7 +838,7 @@ public void testMarkAsUnusedSegmentsInInterval() throws IOException
);
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testMarkAsUnusedSegmentsInIntervalWithInvalidInterval() throws IOException
{
sqlSegmentsMetadataManager.startPollingDatabasePeriodically();
Expand All @@ -848,8 +853,12 @@ public void testMarkAsUnusedSegmentsInIntervalWithInvalidInterval() throws IOExc
publisher.publishSegment(newSegment1);
publisher.publishSegment(newSegment2);
// invalid interval start > end
final Interval theInterval = Intervals.of("2017-10-22T00:00:00.000/2017-10-02T00:00:00.000");
sqlSegmentsMetadataManager.markAsUnusedSegmentsInInterval(newDataSource, theInterval);
DruidExceptionMatcher.invalidInput().assertThrowsAndMatches(
() -> {
Interval theInterval = Intervals.of("2017-10-22T00:00:00.000/2017-10-02T00:00:00.000");
sqlSegmentsMetadataManager.markAsUnusedSegmentsInInterval(newDataSource, theInterval);
}
);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.druid.client.ImmutableDruidDataSource;
import org.apache.druid.client.ImmutableSegmentLoadInfo;
import org.apache.druid.client.SegmentLoadInfo;
import org.apache.druid.error.DruidException;
import org.apache.druid.java.util.common.Intervals;
import org.apache.druid.metadata.MetadataRuleManager;
import org.apache.druid.metadata.SegmentsMetadataManager;
Expand Down Expand Up @@ -621,7 +622,8 @@ public void testMarkAsUnusedAllSegmentsInDataSource()
Assert.assertNotNull(response.getEntity());
Assert.assertTrue(response.getEntity().toString().contains("java.lang.IllegalArgumentException"));
}
catch (IllegalArgumentException ignore) {
catch (DruidException ignore) {
Assert.assertEquals(DruidException.Category.INVALID_INPUT, ignore.getCategory());
// expected
}

Expand Down

0 comments on commit a3fc2f3

Please sign in to comment.