From a4353aa1f42a51a1d218547b3a033550e49e5025 Mon Sep 17 00:00:00 2001 From: Agustin Gonzalez Date: Thu, 18 Nov 2021 10:29:28 -0700 Subject: [PATCH] =?UTF-8?q?Fix=20bug=20Unrecognized=20token=20'No':=20was?= =?UTF-8?q?=20expecting=20(JSON=20String,...)=20when=E2=80=A6=20(#11934)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix bug Unrecognized token 'No': was expecting (JSON String,...) when calling the API /druid/indexer/v1/task/taskId/reports and the report is not found * Also log other non-OK statuses --- .../indexing/HttpIndexingServiceClient.java | 10 ++++- .../HttpIndexingServiceClientTest.java | 41 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/org/apache/druid/client/indexing/HttpIndexingServiceClient.java b/server/src/main/java/org/apache/druid/client/indexing/HttpIndexingServiceClient.java index c2f80a0ee63..67db10fd30f 100644 --- a/server/src/main/java/org/apache/druid/client/indexing/HttpIndexingServiceClient.java +++ b/server/src/main/java/org/apache/druid/client/indexing/HttpIndexingServiceClient.java @@ -32,6 +32,7 @@ import org.apache.druid.java.util.common.DateTimes; import org.apache.druid.java.util.common.ISE; import org.apache.druid.java.util.common.StringUtils; import org.apache.druid.java.util.common.jackson.JacksonUtils; +import org.apache.druid.java.util.common.logger.Logger; import org.apache.druid.java.util.http.client.response.StringFullResponseHolder; import org.apache.druid.timeline.DataSegment; import org.jboss.netty.handler.codec.http.HttpMethod; @@ -53,6 +54,7 @@ import java.util.Set; public class HttpIndexingServiceClient implements IndexingServiceClient { + private static final Logger log = new Logger(HttpIndexingServiceClient.class); private final DruidLeaderClient druidLeaderClient; private final ObjectMapper jsonMapper; @@ -350,7 +352,13 @@ public class HttpIndexingServiceClient implements IndexingServiceClient ) ); - if (responseHolder.getContent().length() == 0) { + if (responseHolder.getContent().length() == 0 || responseHolder.getStatus() != HttpResponseStatus.OK) { + if (responseHolder.getStatus() == HttpResponseStatus.NOT_FOUND) { + log.info("Report not found for taskId [%s] because [%s]", taskId, responseHolder.getContent()); + } else { + // also log other non-ok statuses: + log.info("Non OK response for taskId [%s] because [%s]", taskId, responseHolder.getContent()); + } return null; } diff --git a/server/src/test/java/org/apache/druid/client/indexing/HttpIndexingServiceClientTest.java b/server/src/test/java/org/apache/druid/client/indexing/HttpIndexingServiceClientTest.java index 67b99b95a64..36caf90e956 100644 --- a/server/src/test/java/org/apache/druid/client/indexing/HttpIndexingServiceClientTest.java +++ b/server/src/test/java/org/apache/druid/client/indexing/HttpIndexingServiceClientTest.java @@ -28,6 +28,8 @@ import org.apache.druid.java.util.http.client.Request; import org.apache.druid.java.util.http.client.response.StringFullResponseHolder; import org.easymock.EasyMock; import org.jboss.netty.buffer.BigEndianHeapChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.handler.codec.http.HttpMethod; import org.jboss.netty.handler.codec.http.HttpResponse; import org.jboss.netty.handler.codec.http.HttpResponseStatus; @@ -197,6 +199,43 @@ public class HttpIndexingServiceClientTest EasyMock.verify(druidLeaderClient, response); } + @Test + public void testGetTaskReportStatusNotFound() throws Exception + { + String taskId = "testTaskId"; + HttpResponse response = EasyMock.createMock(HttpResponse.class); + String errorMsg = "No task reports were found for this task. " + + "The task may not exist, or it may not have completed yet."; + ChannelBuffer buf = ChannelBuffers.buffer(errorMsg.length()); + buf.writeBytes(errorMsg.getBytes(StandardCharsets.UTF_8)); + + EasyMock.expect(response.getContent()).andReturn(buf); + EasyMock.replay(response); + + StringFullResponseHolder responseHolder = new StringFullResponseHolder( + HttpResponseStatus.NOT_FOUND, + response, + StandardCharsets.UTF_8 + ).addChunk(""); + + EasyMock.expect(druidLeaderClient.go(EasyMock.anyObject(Request.class))) + .andReturn(responseHolder) + .anyTimes(); + + EasyMock.expect(druidLeaderClient.makeRequest(HttpMethod.GET, "/druid/indexer/v1/task/testTaskId/reports")) + .andReturn(new Request( + HttpMethod.GET, + new URL("http://localhost:8090/druid/indexer/v1/task/testTaskId/reports") + )) + .anyTimes(); + EasyMock.replay(druidLeaderClient); + + final Map actualResponse = httpIndexingServiceClient.getTaskReport(taskId); + Assert.assertNull(actualResponse); + + EasyMock.verify(druidLeaderClient, response); + } + @Test public void testGetTaskReportEmpty() throws Exception { @@ -228,4 +267,6 @@ public class HttpIndexingServiceClientTest EasyMock.verify(druidLeaderClient, response); } + } +