Merge pull request #2665 from jisookim0513/remove-druid-server-serialization

remove serialization of DruidServer
This commit is contained in:
Xavier Léauté 2016-03-25 15:54:05 -07:00
commit 01f3221a62
6 changed files with 142 additions and 45 deletions

View File

@ -95,31 +95,26 @@ public class DruidServer implements Comparable
return metadata; return metadata;
} }
@JsonProperty
public String getHost() public String getHost()
{ {
return metadata.getHost(); return metadata.getHost();
} }
@JsonProperty
public long getCurrSize() public long getCurrSize()
{ {
return currSize; return currSize;
} }
@JsonProperty
public long getMaxSize() public long getMaxSize()
{ {
return metadata.getMaxSize(); return metadata.getMaxSize();
} }
@JsonProperty
public String getType() public String getType()
{ {
return metadata.getType(); return metadata.getType();
} }
@JsonProperty
public String getTier() public String getTier()
{ {
return metadata.getTier(); return metadata.getTier();
@ -130,13 +125,11 @@ public class DruidServer implements Comparable
return metadata.isAssignable(); return metadata.isAssignable();
} }
@JsonProperty
public int getPriority() public int getPriority()
{ {
return metadata.getPriority(); return metadata.getPriority();
} }
@JsonProperty
public Map<String, DataSegment> getSegments() public Map<String, DataSegment> getSegments()
{ {
// Copying the map slows things down a lot here, don't use Immutable Map here // Copying the map slows things down a lot here, don't use Immutable Map here

View File

@ -97,17 +97,6 @@ public abstract class ServerInventoryView<InventoryType> implements ServerView,
} }
} }
@Override
public byte[] serializeContainer(DruidServer container)
{
try {
return jsonMapper.writeValueAsBytes(container);
}
catch (JsonProcessingException e) {
throw Throwables.propagate(e);
}
}
@Override @Override
public InventoryType deserializeInventory(byte[] bytes) public InventoryType deserializeInventory(byte[] bytes)
{ {
@ -122,17 +111,6 @@ public abstract class ServerInventoryView<InventoryType> implements ServerView,
} }
} }
@Override
public byte[] serializeInventory(InventoryType inventory)
{
try {
return jsonMapper.writeValueAsBytes(inventory);
}
catch (JsonProcessingException e) {
throw Throwables.propagate(e);
}
}
@Override @Override
public void newContainer(DruidServer container) public void newContainer(DruidServer container)
{ {

View File

@ -24,10 +24,8 @@ package io.druid.curator.inventory;
public interface CuratorInventoryManagerStrategy<ContainerClass, InventoryClass> public interface CuratorInventoryManagerStrategy<ContainerClass, InventoryClass>
{ {
public ContainerClass deserializeContainer(byte[] bytes); public ContainerClass deserializeContainer(byte[] bytes);
public byte[] serializeContainer(ContainerClass container);
public InventoryClass deserializeInventory(byte[] bytes); public InventoryClass deserializeInventory(byte[] bytes);
public byte[] serializeInventory(InventoryClass inventory);
public void newContainer(ContainerClass newContainer); public void newContainer(ContainerClass newContainer);
public void deadContainer(ContainerClass deadContainer); public void deadContainer(ContainerClass deadContainer);

View File

@ -55,6 +55,19 @@ public class ServersResource
.build(); .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; private final InventoryView serverInventoryView;
@Inject @Inject
@ -75,7 +88,21 @@ public class ServersResource
Response.ResponseBuilder builder = Response.status(Response.Status.OK); Response.ResponseBuilder builder = Response.status(Response.Status.OK);
if (full != null) { 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) { } else if (simple != null) {
return builder.entity( return builder.entity(
Lists.newArrayList( Lists.newArrayList(
@ -130,7 +157,7 @@ public class ServersResource
return builder.entity(makeSimpleServer(server)).build(); return builder.entity(makeSimpleServer(server)).build();
} }
return builder.entity(server) return builder.entity(makeFullServer(server))
.build(); .build();
} }

View File

@ -186,24 +186,12 @@ public class CuratorInventoryManagerTest extends io.druid.curator.CuratorTestBas
return Maps.newTreeMap(); return Maps.newTreeMap();
} }
@Override
public byte[] serializeContainer(Map<String, Integer> container)
{
return new byte[]{};
}
@Override @Override
public Integer deserializeInventory(byte[] bytes) public Integer deserializeInventory(byte[] bytes)
{ {
return Ints.fromByteArray(bytes); return Ints.fromByteArray(bytes);
} }
@Override
public byte[] serializeInventory(Integer inventory)
{
return Ints.toByteArray(inventory);
}
@Override @Override
public void newContainer(Map<String, Integer> newContainer) public void newContainer(Map<String, Integer> newContainer)
{ {

View File

@ -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);
}
}