Update usages of io.druid.client.selector.Server to build URL or URI directly instead of using String.format

This commit is contained in:
Charles Allen 2015-10-12 11:22:23 -07:00
parent e450877a78
commit bf11723a52
5 changed files with 64 additions and 19 deletions

View File

@ -155,7 +155,7 @@ public class RemoteTaskActionClient implements TaskActionClient
private URI makeServiceUri(final Server instance) throws URISyntaxException private URI makeServiceUri(final Server instance) throws URISyntaxException
{ {
return new URI(String.format("%s://%s%s", instance.getScheme(), instance.getHost(), "/druid/indexer/v1/action")); return new URI(instance.getScheme(), null, instance.getAddress(), instance.getPort(), "/druid/indexer/v1/action", null, null);
} }
private Server getServiceInstance() private Server getServiceInstance()

View File

@ -34,6 +34,7 @@ import org.joda.time.Interval;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import java.io.InputStream; import java.io.InputStream;
import java.net.URI;
import java.net.URL; import java.net.URL;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -115,7 +116,15 @@ public class IndexingServiceClient
throw new ISE("Cannot find instance of indexingService"); throw new ISE("Cannot find instance of indexingService");
} }
return String.format("http://%s/druid/indexer/v1", instance.getHost()); return new URI(
instance.getScheme(),
null,
instance.getAddress(),
instance.getPort(),
"/druid/indexer/v1",
null,
null
).toString();
} }
catch (Exception e) { catch (Exception e) {
throw Throwables.propagate(e); throw Throwables.propagate(e);

View File

@ -95,16 +95,18 @@ public class BridgeQuerySegmentWalker implements QuerySegmentWalker
if (instance == null) { if (instance == null) {
return Sequences.empty(); return Sequences.empty();
} }
final Server brokerServer = brokerSelector.pick();
final String url = String.format( final URL url = new URL(
"http://%s/druid/v2/", brokerServer.getScheme(),
brokerSelector.pick().getHost() brokerServer.getAddress(),
brokerServer.getPort(),
"/druid/v2/"
); );
StatusResponseHolder response = httpClient.go( StatusResponseHolder response = httpClient.go(
new Request( new Request(
HttpMethod.POST, HttpMethod.POST,
new URL(url) url
).setContent( ).setContent(
MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON,
jsonMapper.writeValueAsBytes(query) jsonMapper.writeValueAsBytes(query)

View File

@ -42,6 +42,8 @@ import org.jboss.netty.handler.codec.http.HttpMethod;
import org.jboss.netty.handler.codec.http.HttpResponseStatus; import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.joda.time.Duration; import org.joda.time.Duration;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -197,7 +199,7 @@ public class CoordinatorRuleManager
return retVal; return retVal;
} }
private String getRuleURL() private String getRuleURL() throws URISyntaxException
{ {
Server server = selector.pick(); Server server = selector.pick();
@ -206,6 +208,14 @@ public class CoordinatorRuleManager
return null; return null;
} }
return String.format("http://%s%s", server.getHost(), config.get().getRulesEndpoint()); return new URI(
server.getScheme(),
null,
server.getAddress(),
server.getPort(),
config.get().getRulesEndpoint(),
null,
null
).toString();
} }
} }

View File

@ -34,7 +34,7 @@ public class ServerDiscoverySelectorTest
{ {
private ServiceProvider serviceProvider; private ServiceProvider serviceProvider;
private ServerDiscoverySelector serverDiscoverySelector; private ServerDiscoverySelector serverDiscoverySelector;
private ServiceInstance instance; private ServiceInstance instance;
private static final int PORT = 8080; private static final int PORT = 8080;
private static final String ADDRESS = "localhost"; private static final String ADDRESS = "localhost";
@ -57,11 +57,19 @@ public class ServerDiscoverySelectorTest
Server server = serverDiscoverySelector.pick(); Server server = serverDiscoverySelector.pick();
Assert.assertEquals(PORT, server.getPort()); Assert.assertEquals(PORT, server.getPort());
Assert.assertEquals(ADDRESS, server.getAddress()); Assert.assertEquals(ADDRESS, server.getAddress());
Assert.assertTrue(server.getHost().contains(new Integer(PORT).toString())); Assert.assertTrue(server.getHost().contains(Integer.toString(PORT)));
Assert.assertTrue(server.getHost().contains(ADDRESS)); Assert.assertTrue(server.getHost().contains(ADDRESS));
Assert.assertEquals(new String("http"), server.getScheme()); Assert.assertEquals("http", server.getScheme());
EasyMock.verify(instance, serviceProvider); EasyMock.verify(instance, serviceProvider);
final URI uri = new URI(String.format("%s://%s%s", server.getScheme(), server.getHost(), "/druid/indexer/v1/action")); final URI uri = new URI(
server.getScheme(),
null,
server.getAddress(),
server.getPort(),
"/druid/indexer/v1/action",
null,
null
);
Assert.assertEquals(PORT, uri.getPort()); Assert.assertEquals(PORT, uri.getPort());
Assert.assertEquals(ADDRESS, uri.getHost()); Assert.assertEquals(ADDRESS, uri.getHost());
Assert.assertEquals("http", uri.getScheme()); Assert.assertEquals("http", uri.getScheme());
@ -79,11 +87,19 @@ public class ServerDiscoverySelectorTest
Server server = serverDiscoverySelector.pick(); Server server = serverDiscoverySelector.pick();
Assert.assertEquals(PORT, server.getPort()); Assert.assertEquals(PORT, server.getPort());
Assert.assertEquals(ADDRESS, server.getAddress()); Assert.assertEquals(ADDRESS, server.getAddress());
Assert.assertTrue(server.getHost().contains(new Integer(PORT).toString())); Assert.assertTrue(server.getHost().contains(Integer.toString(PORT)));
Assert.assertTrue(server.getHost().contains(ADDRESS)); Assert.assertTrue(server.getHost().contains(ADDRESS));
Assert.assertEquals(new String("http"), server.getScheme()); Assert.assertEquals("http", server.getScheme());
EasyMock.verify(instance, serviceProvider); EasyMock.verify(instance, serviceProvider);
final URI uri = new URI(String.format("%s://%s%s", server.getScheme(), server.getHost(), "/druid/indexer/v1/action")); final URI uri = new URI(
server.getScheme(),
null,
server.getAddress(),
server.getPort(),
"/druid/indexer/v1/action",
null,
null
);
Assert.assertEquals(PORT, uri.getPort()); Assert.assertEquals(PORT, uri.getPort());
Assert.assertEquals(String.format("[%s]", ADDRESS), uri.getHost()); Assert.assertEquals(String.format("[%s]", ADDRESS), uri.getHost());
Assert.assertEquals("http", uri.getScheme()); Assert.assertEquals("http", uri.getScheme());
@ -101,11 +117,19 @@ public class ServerDiscoverySelectorTest
Server server = serverDiscoverySelector.pick(); Server server = serverDiscoverySelector.pick();
Assert.assertEquals(PORT, server.getPort()); Assert.assertEquals(PORT, server.getPort());
Assert.assertEquals(ADDRESS, server.getAddress()); Assert.assertEquals(ADDRESS, server.getAddress());
Assert.assertTrue(server.getHost().contains(new Integer(PORT).toString())); Assert.assertTrue(server.getHost().contains(Integer.toString(PORT)));
Assert.assertTrue(server.getHost().contains(ADDRESS)); Assert.assertTrue(server.getHost().contains(ADDRESS));
Assert.assertEquals(new String("http"), server.getScheme()); Assert.assertEquals("http", server.getScheme());
EasyMock.verify(instance, serviceProvider); EasyMock.verify(instance, serviceProvider);
final URI uri = new URI(String.format("%s://%s%s", server.getScheme(), server.getHost(), "/druid/indexer/v1/action")); final URI uri = new URI(
server.getScheme(),
null,
server.getAddress(),
server.getPort(),
"/druid/indexer/v1/action",
null,
null
);
Assert.assertEquals(PORT, uri.getPort()); Assert.assertEquals(PORT, uri.getPort());
Assert.assertEquals(ADDRESS, uri.getHost()); Assert.assertEquals(ADDRESS, uri.getHost());
Assert.assertEquals("http", uri.getScheme()); Assert.assertEquals("http", uri.getScheme());