Skip to content

Commit

Permalink
Remove QueryResource's ObjectMapper::copy() in favor of using the inj…
Browse files Browse the repository at this point in the history
…ected ObjectMapper directly

* Workaround for FasterXML/jackson-databind#696
  • Loading branch information
drcrallen committed Feb 11, 2015
1 parent c5e99bf commit d51b37c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
14 changes: 5 additions & 9 deletions server/src/main/java/io/druid/server/QueryResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package io.druid.server;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.jaxrs.smile.SmileMediaTypes;
Expand Down Expand Up @@ -89,12 +88,8 @@ public QueryResource(
)
{
this.config = config;
this.jsonMapper = jsonMapper.copy();
this.jsonMapper.getFactory().configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);

this.smileMapper = smileMapper.copy();
this.smileMapper.getFactory().configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);

this.jsonMapper = jsonMapper;
this.smileMapper = smileMapper;
this.texasRanger = texasRanger;
this.emitter = emitter;
this.requestLogger = requestLogger;
Expand Down Expand Up @@ -125,7 +120,8 @@ public Response doPost(
String queryId = null;

final String reqContentType = req.getContentType();
final boolean isSmile = SmileMediaTypes.APPLICATION_JACKSON_SMILE.equals(reqContentType) || APPLICATION_SMILE.equals(reqContentType);
final boolean isSmile = SmileMediaTypes.APPLICATION_JACKSON_SMILE.equals(reqContentType)
|| APPLICATION_SMILE.equals(reqContentType);
final String contentType = isSmile ? SmileMediaTypes.APPLICATION_JACKSON_SMILE : MediaType.APPLICATION_JSON;

ObjectMapper objectMapper = isSmile ? smileMapper : jsonMapper;
Expand Down Expand Up @@ -210,7 +206,7 @@ public void write(OutputStream outputStream) throws IOException, WebApplicationE
}
},
contentType
)
)
.header("X-Druid-Query-Id", queryId)
.header("X-Druid-Response-Context", jsonMapper.writeValueAsString(responseContext))
.build();
Expand Down
32 changes: 25 additions & 7 deletions server/src/test/java/io/druid/server/QueryResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,17 @@
public class QueryResourceTest
{
private static final ObjectMapper jsonMapper = new DefaultObjectMapper();
public static final ServerConfig serverConfig = new ServerConfig(){
public static final ServerConfig serverConfig = new ServerConfig()
{
@Override
public int getNumThreads(){
public int getNumThreads()
{
return 1;
}

@Override
public Period getMaxIdleTime(){
public Period getMaxIdleTime()
{
return Period.seconds(1);
}
};
Expand Down Expand Up @@ -90,17 +94,21 @@ public <T> QueryRunner<T> getQueryRunnerForSegments(
};

private static final ServiceEmitter noopServiceEmitter = new NoopServiceEmitter();

@BeforeClass
public static void staticSetup(){
public static void staticSetup()
{
com.metamx.emitter.EmittingLogger.registerEmitter(noopServiceEmitter);
}

@Before
public void setup()
{
EasyMock.expect(testServletRequest.getContentType()).andReturn(MediaType.APPLICATION_JSON);
EasyMock.expect(testServletRequest.getRemoteAddr()).andReturn("localhost").anyTimes();
EasyMock.replay(testServletRequest);
}

private static final String simpleTimeSeriesQuery = "{\n"
+ " \"queryType\": \"timeseries\",\n"
+ " \"dataSource\": \"mmx_metrics\",\n"
Expand All @@ -115,6 +123,7 @@ public void setup()
+ " }\n"
+ " ]\n"
+ "}";

@Test
public void testGoodQuery() throws IOException
{
Expand All @@ -126,10 +135,15 @@ public void testGoodQuery() throws IOException
new NoopServiceEmitter(),
new NoopRequestLogger(),
new QueryManager()
);
Response respone = queryResource.doPost(new ByteArrayInputStream(simpleTimeSeriesQuery.getBytes("UTF-8")), null /*pretty*/, testServletRequest);
);
Response respone = queryResource.doPost(
new ByteArrayInputStream(simpleTimeSeriesQuery.getBytes("UTF-8")),
null /*pretty*/,
testServletRequest
);
Assert.assertNotNull(respone);
}

@Test
public void testBadQuery() throws IOException
{
Expand All @@ -143,7 +157,11 @@ public void testBadQuery() throws IOException
new NoopRequestLogger(),
new QueryManager()
);
Response respone = queryResource.doPost(new ByteArrayInputStream("Meka Leka Hi Meka Hiney Ho".getBytes("UTF-8")), null /*pretty*/, testServletRequest);
Response respone = queryResource.doPost(
new ByteArrayInputStream("Meka Leka Hi Meka Hiney Ho".getBytes("UTF-8")),
null /*pretty*/,
testServletRequest
);
Assert.assertNotNull(respone);
Assert.assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), respone.getStatus());
}
Expand Down

0 comments on commit d51b37c

Please sign in to comment.