From 0d3c5a3b6c21b8c985f0a411fca8967885a39b33 Mon Sep 17 00:00:00 2001 From: jisookim Date: Tue, 15 Mar 2016 15:33:58 -0700 Subject: [PATCH] remove serialization of Druid Server and add tests for ServersResource --- .../java/io/druid/client/DruidServer.java | 7 -- .../io/druid/client/ServerInventoryView.java | 22 ---- .../CuratorInventoryManagerStrategy.java | 2 - .../io/druid/server/http/ServersResource.java | 31 ++++- .../CuratorInventoryManagerTest.java | 12 -- .../server/http/ServersResourceTest.java | 113 ++++++++++++++++++ 6 files changed, 142 insertions(+), 45 deletions(-) create mode 100644 server/src/test/java/io/druid/server/http/ServersResourceTest.java diff --git a/server/src/main/java/io/druid/client/DruidServer.java b/server/src/main/java/io/druid/client/DruidServer.java index 9d5564b42540..43d5fe561d0e 100644 --- a/server/src/main/java/io/druid/client/DruidServer.java +++ b/server/src/main/java/io/druid/client/DruidServer.java @@ -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(); @@ -130,13 +125,11 @@ public boolean isAssignable() return metadata.isAssignable(); } - @JsonProperty public int getPriority() { return metadata.getPriority(); } - @JsonProperty public Map getSegments() { // Copying the map slows things down a lot here, don't use Immutable Map here diff --git a/server/src/main/java/io/druid/client/ServerInventoryView.java b/server/src/main/java/io/druid/client/ServerInventoryView.java index a2682e3d412a..e103e9f7f642 100644 --- a/server/src/main/java/io/druid/client/ServerInventoryView.java +++ b/server/src/main/java/io/druid/client/ServerInventoryView.java @@ -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) { @@ -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) { diff --git a/server/src/main/java/io/druid/curator/inventory/CuratorInventoryManagerStrategy.java b/server/src/main/java/io/druid/curator/inventory/CuratorInventoryManagerStrategy.java index 3101351539df..8f20b4694959 100644 --- a/server/src/main/java/io/druid/curator/inventory/CuratorInventoryManagerStrategy.java +++ b/server/src/main/java/io/druid/curator/inventory/CuratorInventoryManagerStrategy.java @@ -24,10 +24,8 @@ public interface CuratorInventoryManagerStrategy { 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); diff --git a/server/src/main/java/io/druid/server/http/ServersResource.java b/server/src/main/java/io/druid/server/http/ServersResource.java index e3d40dd934b4..33665fda81d2 100644 --- a/server/src/main/java/io/druid/server/http/ServersResource.java +++ b/server/src/main/java/io/druid/server/http/ServersResource.java @@ -55,6 +55,19 @@ private static Map makeSimpleServer(DruidServer input) .build(); } + private static Map makeFullServer(DruidServer input) + { + return new ImmutableMap.Builder() + .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 @@ -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>() + { + @Override + public Map apply(DruidServer input) + { + return makeFullServer(input); + } + } + ) + ) + ).build(); } else if (simple != null) { return builder.entity( Lists.newArrayList( @@ -130,7 +157,7 @@ public Response getServer( return builder.entity(makeSimpleServer(server)).build(); } - return builder.entity(server) + return builder.entity(makeFullServer(server)) .build(); } diff --git a/server/src/test/java/io/druid/curator/inventory/CuratorInventoryManagerTest.java b/server/src/test/java/io/druid/curator/inventory/CuratorInventoryManagerTest.java index d64910b22ccd..4cb6899c7a17 100644 --- a/server/src/test/java/io/druid/curator/inventory/CuratorInventoryManagerTest.java +++ b/server/src/test/java/io/druid/curator/inventory/CuratorInventoryManagerTest.java @@ -186,24 +186,12 @@ public Map deserializeContainer(byte[] bytes) return Maps.newTreeMap(); } - @Override - public byte[] serializeContainer(Map 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 newContainer) { diff --git a/server/src/test/java/io/druid/server/http/ServersResourceTest.java b/server/src/test/java/io/druid/server/http/ServersResourceTest.java new file mode 100644 index 000000000000..0695bf6750bf --- /dev/null +++ b/server/src/test/java/io/druid/server/http/ServersResourceTest.java @@ -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); + } + +}