Skip to content

Commit

Permalink
remove serialization of Druid Server and add tests for ServersResource
Browse files Browse the repository at this point in the history
  • Loading branch information
jisookim0513 committed Mar 25, 2016
1 parent 177b575 commit 0d3c5a3
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 45 deletions.
7 changes: 0 additions & 7 deletions server/src/main/java/io/druid/client/DruidServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,31 +95,26 @@ public DruidServerMetadata getMetadata()
return metadata;
}

@JsonProperty
public String getHost()
{
return metadata.getHost();
}

@JsonProperty
public long getCurrSize()
{
return currSize;
}

@JsonProperty
public long getMaxSize()
{
return metadata.getMaxSize();
}

@JsonProperty
public String getType()
{
return metadata.getType();
}

@JsonProperty
public String getTier()
{
return metadata.getTier();
Expand All @@ -130,13 +125,11 @@ public boolean isAssignable()
return metadata.isAssignable();
}

@JsonProperty
public int getPriority()
{
return metadata.getPriority();
}

@JsonProperty
public Map<String, DataSegment> getSegments()
{
// Copying the map slows things down a lot here, don't use Immutable Map here
Expand Down
22 changes: 0 additions & 22 deletions server/src/main/java/io/druid/client/ServerInventoryView.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,6 @@ public DruidServer deserializeContainer(byte[] bytes)
}
}

@Override
public byte[] serializeContainer(DruidServer container)
{
try {
return jsonMapper.writeValueAsBytes(container);
}
catch (JsonProcessingException e) {
throw Throwables.propagate(e);
}
}

@Override
public InventoryType deserializeInventory(byte[] bytes)
{
Expand All @@ -122,17 +111,6 @@ public InventoryType deserializeInventory(byte[] bytes)
}
}

@Override
public byte[] serializeInventory(InventoryType inventory)
{
try {
return jsonMapper.writeValueAsBytes(inventory);
}
catch (JsonProcessingException e) {
throw Throwables.propagate(e);
}
}

@Override
public void newContainer(DruidServer container)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@
public interface CuratorInventoryManagerStrategy<ContainerClass, InventoryClass>
{
public ContainerClass deserializeContainer(byte[] bytes);
public byte[] serializeContainer(ContainerClass container);

public InventoryClass deserializeInventory(byte[] bytes);
public byte[] serializeInventory(InventoryClass inventory);

public void newContainer(ContainerClass newContainer);
public void deadContainer(ContainerClass deadContainer);
Expand Down
31 changes: 29 additions & 2 deletions server/src/main/java/io/druid/server/http/ServersResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ private static Map<String, Object> makeSimpleServer(DruidServer input)
.build();
}

private static Map<String, Object> makeFullServer(DruidServer input)
{
return new ImmutableMap.Builder<String, Object>()
.put("host", input.getHost())
.put("maxSize", input.getMaxSize())
.put("type", input.getType())
.put("tier", input.getTier())
.put("priority", input.getPriority())
.put("segments", input.getSegments())
.put("currSize", input.getCurrSize())
.build();
}

private final InventoryView serverInventoryView;

@Inject
Expand All @@ -75,7 +88,21 @@ public Response getClusterServers(
Response.ResponseBuilder builder = Response.status(Response.Status.OK);

if (full != null) {
return builder.entity(Lists.newArrayList(serverInventoryView.getInventory())).build();
return builder.entity(
Lists.newArrayList(
Iterables.transform(
serverInventoryView.getInventory(),
new Function<DruidServer, Map<String, Object>>()
{
@Override
public Map<String, Object> apply(DruidServer input)
{
return makeFullServer(input);
}
}
)
)
).build();
} else if (simple != null) {
return builder.entity(
Lists.newArrayList(
Expand Down Expand Up @@ -130,7 +157,7 @@ public Response getServer(
return builder.entity(makeSimpleServer(server)).build();
}

return builder.entity(server)
return builder.entity(makeFullServer(server))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,24 +186,12 @@ public Map<String, Integer> deserializeContainer(byte[] bytes)
return Maps.newTreeMap();
}

@Override
public byte[] serializeContainer(Map<String, Integer> container)
{
return new byte[]{};
}

@Override
public Integer deserializeInventory(byte[] bytes)
{
return Ints.fromByteArray(bytes);
}

@Override
public byte[] serializeInventory(Integer inventory)
{
return Ints.toByteArray(inventory);
}

@Override
public void newContainer(Map<String, Integer> newContainer)
{
Expand Down
113 changes: 113 additions & 0 deletions server/src/test/java/io/druid/server/http/ServersResourceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.druid.server.http;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import io.druid.client.CoordinatorServerView;
import io.druid.client.DruidServer;
import io.druid.jackson.DefaultObjectMapper;
import io.druid.timeline.DataSegment;
import org.easymock.EasyMock;
import org.joda.time.Interval;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import javax.ws.rs.core.Response;

public class ServersResourceTest {
private DruidServer server;
private ServersResource serversResource;
private ObjectMapper objectMapper = new DefaultObjectMapper();

@Before
public void setUp()
{
DruidServer dummyServer = new DruidServer("dummy", "host", 1234L, "type", "tier", 0);
DataSegment segment = DataSegment.builder()
.dataSource("dataSource")
.interval(new Interval("2016-03-22T14Z/2016-03-22T15Z"))
.version("v0")
.size(1L)
.build();
dummyServer.addDataSegment(segment.getIdentifier(), segment);

CoordinatorServerView inventoryView = EasyMock.createMock(CoordinatorServerView.class);
EasyMock.expect(inventoryView.getInventory()).andReturn(ImmutableList.of(dummyServer)).anyTimes();
EasyMock.expect(inventoryView.getInventoryValue(dummyServer.getName())).andReturn(dummyServer).anyTimes();
EasyMock.replay(inventoryView);
server = dummyServer;
serversResource = new ServersResource(inventoryView);
}

@Test
public void testGetClusterServersFull() throws Exception
{
Response res = serversResource.getClusterServers("full", null);
String result = objectMapper.writeValueAsString(res.getEntity());
String expected = "[{\"host\":\"host\","
+ "\"maxSize\":1234,"
+ "\"type\":\"type\","
+ "\"tier\":\"tier\","
+ "\"priority\":0,"
+ "\"segments\":{\"dataSource_2016-03-22T14:00:00.000Z_2016-03-22T15:00:00.000Z_v0\":"
+ "{\"dataSource\":\"dataSource\",\"interval\":\"2016-03-22T14:00:00.000Z/2016-03-22T15:00:00.000Z\",\"version\":\"v0\",\"loadSpec\":{},\"dimensions\":\"\",\"metrics\":\"\","
+ "\"shardSpec\":{\"type\":\"none\"},\"binaryVersion\":null,\"size\":1,\"identifier\":\"dataSource_2016-03-22T14:00:00.000Z_2016-03-22T15:00:00.000Z_v0\"}},"
+ "\"currSize\":1}]";
Assert.assertEquals(expected, result);
}

@Test
public void testGetClusterServersSimple() throws Exception
{
Response res = serversResource.getClusterServers(null, "simple");
String result = objectMapper.writeValueAsString(res.getEntity());
String expected = "[{\"host\":\"host\",\"tier\":\"tier\",\"type\":\"type\",\"priority\":0,\"currSize\":1,\"maxSize\":1234}]";
Assert.assertEquals(expected, result);
}

@Test
public void testGetServerFull() throws Exception
{
Response res = serversResource.getServer(server.getName(), null);
String result = objectMapper.writeValueAsString(res.getEntity());
String expected = "{\"host\":\"host\","
+ "\"maxSize\":1234,"
+ "\"type\":\"type\","
+ "\"tier\":\"tier\","
+ "\"priority\":0,"
+ "\"segments\":{\"dataSource_2016-03-22T14:00:00.000Z_2016-03-22T15:00:00.000Z_v0\":"
+ "{\"dataSource\":\"dataSource\",\"interval\":\"2016-03-22T14:00:00.000Z/2016-03-22T15:00:00.000Z\",\"version\":\"v0\",\"loadSpec\":{},\"dimensions\":\"\",\"metrics\":\"\","
+ "\"shardSpec\":{\"type\":\"none\"},\"binaryVersion\":null,\"size\":1,\"identifier\":\"dataSource_2016-03-22T14:00:00.000Z_2016-03-22T15:00:00.000Z_v0\"}},"
+ "\"currSize\":1}";
Assert.assertEquals(expected, result);
}

@Test
public void testGetServerSimple() throws Exception
{
Response res = serversResource.getServer(server.getName(), "simple");
String result = objectMapper.writeValueAsString(res.getEntity());
String expected = "{\"host\":\"host\",\"tier\":\"tier\",\"type\":\"type\",\"priority\":0,\"currSize\":1,\"maxSize\":1234}";
Assert.assertEquals(expected, result);
}

}

0 comments on commit 0d3c5a3

Please sign in to comment.