abhishekrb19 opened a new pull request, #16206:
URL: https://github.com/apache/druid/pull/16206
#### Problem:
Before this patch, the `/datasources/{dataSourceName}/unusedSegments` API
throws a 500 with a huge stack trace for any invalid parameters specified in
the request. This is because `DruidException`s are thrown without mapping them
to a HTTP response.
<details>
<summary>Click here to see the stacktrace with a 500 error code</summary>
### Some Javascript
```bash
curl -X 'GET' -H 'Content-Type:application/json'
http://localhost:8081/druid/coordinator/v1/metadata/datasources/foo_it/unusedSegments\?interval=2024-04-01T00%3A00%3A00.000Z/2024-03-01T00%3A00%3A00.000Z
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
<title>Error 500 org.apache.druid.error.DruidException: Bad
Interval[2024-04-01T00:00:00.000Z/2024-03-01T00:00:00.000Z]: [The end instant
must be greater than the start instant]</title>
</head>
<body><h2>HTTP ERROR 500 org.apache.druid.error.DruidException: Bad
Interval[2024-04-01T00:00:00.000Z/2024-03-01T00:00:00.000Z]: [The end instant
must be greater than the start instant]</h2>
<table>
<tr><th>URI:</th><td>/druid/coordinator/v1/metadata/datasources/foo_it/unusedSegments</td></tr>
<tr><th>STATUS:</th><td>500</td></tr>
<tr><th>MESSAGE:</th><td>org.apache.druid.error.DruidException: Bad
Interval[2024-04-01T00:00:00.000Z/2024-03-01T00:00:00.000Z]: [The end instant
must be greater than the start instant]</td></tr>
<tr><th>SERVLET:</th><td>default</td></tr>
<tr><th>CAUSED BY:</th><td>org.apache.druid.error.DruidException: Bad
Interval[2024-04-01T00:00:00.000Z/2024-03-01T00:00:00.000Z]: [The end instant
must be greater than the start instant]</td></tr>
<tr><th>CAUSED BY:</th><td>java.lang.IllegalArgumentException: The end
instant must be greater than the start instant</td></tr>
</table>
<h3>Caused by:</h3><pre>org.apache.druid.error.DruidException: Bad
Interval[2024-04-01T00:00:00.000Z/2024-03-01T00:00:00.000Z]: [The end instant
must be greater than the start instant]
at
org.apache.druid.error.DruidException$DruidExceptionBuilder.build(DruidException.java:460)
at org.apache.druid.error.BaseFailure.makeException(BaseFailure.java:57)
at
org.apache.druid.error.DruidException.fromFailure(DruidException.java:154)
at org.apache.druid.error.InvalidInput.exception(InvalidInput.java:35)
at org.apache.druid.java.util.common.Intervals.of(Intervals.java:47)
at
org.apache.druid.server.http.MetadataResource.getUnusedSegmentsInDataSource(MetadataResource.java:365)
at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
```
</details>
#### Fix:
- Handle exceptions in the API and map them to a `Response` object with the
appropriate error code.
- Break up the large unit test `testGetUnusedSegmentsInDataSource()` into
smaller unit tests for each test case. Also, validate the error codes.
- Lower case "Interval" in the bad interval exception message.
With the fix in this patch, a 400 error code is returned without the
stacktrace:
```bash
curl -X 'GET' -H 'Content-Type:application/json'
http://localhost:8081/druid/coordinator/v1/metadata/datasources/foo_it/unusedSegments\?interval=2024-04-01T00%3A00%3A00.000Z/2024-03-01T00%3A00%3A00.000Z
{"error":"druidException","errorCode":"invalidInput","persona":"USER","category":"INVALID_INPUT","errorMessage":"Bad
Interval[2024-04-01T00:00:00.000Z/2024-03-01T00:00:00.000Z]: [The end instant
must be greater than the start instant]","context":{}}
```
<!-- Thanks for trying to help us make Apache Druid be the best it can be!
Please fill out as much of the following information as is possible (where
relevant, and remove it when irrelevant) to help make the intention and scope
of this PR clear in order to ease review. -->
<!-- Please read the doc for contribution
(https://github.com/apache/druid/blob/master/CONTRIBUTING.md) before making
this PR. Also, once you open a PR, please _avoid using force pushes and
rebasing_ since these make it difficult for reviewers to see what you've
changed in response to their reviews. See [the 'If your pull request shows
conflicts with master'
section](https://github.com/apache/druid/blob/master/CONTRIBUTING.md#if-your-pull-request-shows-conflicts-with-master)
for more details. -->
<!-- Replace XXXX with the id of the issue fixed in this PR. Remove this
section if there is no corresponding issue. Don't reference the issue in the
title of this pull-request. -->
<!-- If you are a committer, follow the PR action item checklist for
committers:
https://github.com/apache/druid/blob/master/dev/committer-instructions.md#pr-and-issue-action-item-checklist-for-committers.
-->
This PR has:
- [x] been self-reviewed.
- [x] added unit tests or modified existing tests to cover new code paths,
ensuring the threshold for [code
coverage](https://github.com/apache/druid/blob/master/dev/code-review/code-coverage.md)
is met.
- [x] been tested in a test Druid cluster.
--
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]