diff --git a/contrib/restclient/src/java/voldemort/restclient/admin/CoordinatorAdminClient.java b/contrib/restclient/src/java/voldemort/restclient/admin/CoordinatorAdminClient.java index e486dd95b4..3b37b89935 100644 --- a/contrib/restclient/src/java/voldemort/restclient/admin/CoordinatorAdminClient.java +++ b/contrib/restclient/src/java/voldemort/restclient/admin/CoordinatorAdminClient.java @@ -3,7 +3,6 @@ import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; @@ -35,9 +34,12 @@ public class CoordinatorAdminClient { private static final String URL_SEPARATOR = "/"; private static final String STORE_CLIENT_CONFIG_OPS = "store-client-config-ops"; - private static final String GET = "GET"; - private static final String POST = "POST"; - private static final String DELETE = "DELETE"; + private static enum requestType { + GET, + POST, + DELETE + } + public static final String CONTENT_TYPE = "Content-Type"; public static final String CONTENT_LENGTH = "Content-Length"; public static final String CUSTOM_RESOLVING_STRATEGY = "custom"; @@ -59,97 +61,104 @@ public CoordinatorAdminClient() { public CoordinatorAdminClient(RESTClientConfig config) { this.config = config; this.httpClientFactory = new HttpClientFactory(); - Map properties = new HashMap(); + Map properties = Maps.newHashMap(); properties.put(HttpClientFactory.HTTP_POOL_SIZE, Integer.toString(this.config.getMaxR2ConnectionPoolSize())); this.client = new TransportClientAdapter(httpClientFactory.getClient(properties)); } - public String getStoreClientConfigString(List storeNames, String coordinatorUrl) { + private RestRequestBuilder setCommonRequestHeader(RestRequestBuilder requestBuilder) { + requestBuilder.setHeader(RestMessageHeaders.X_VOLD_REQUEST_ORIGIN_TIME_MS, + String.valueOf(System.currentTimeMillis())); + if(this.routingTypeCode != null) { + requestBuilder.setHeader(RestMessageHeaders.X_VOLD_ROUTING_TYPE_CODE, + this.routingTypeCode); + } + if(this.zoneId != INVALID_ZONE_ID) { + requestBuilder.setHeader(RestMessageHeaders.X_VOLD_ZONE_ID, String.valueOf(this.zoneId)); + } + return requestBuilder; + } - String result = null; + private void handleRequestAndResponseException(Exception e) { + if(e instanceof ExecutionException) { + if(e.getCause() instanceof RestException) { + RestException re = (RestException) e.getCause(); + if(logger.isDebugEnabled()) { + logger.debug("REST Exception Status: " + re.getResponse().getStatus()); + } + } else { + throw new VoldemortException("Unknown HTTP request execution exception: " + + e.getMessage(), e); + } + } else if(e instanceof InterruptedException) { + if(logger.isDebugEnabled()) { + logger.debug("Operation interrupted : " + e.getMessage(), e); + } + throw new VoldemortException("Operation interrupted exception: " + e.getMessage(), e); + } else if(e instanceof URISyntaxException) { + throw new VoldemortException("Illegal HTTP URL " + e.getMessage(), e); + } else if(e instanceof UnsupportedEncodingException) { + throw new VoldemortException("Illegal Encoding Type " + e.getMessage()); + } else { + throw new VoldemortException("Unknown exception: " + e.getMessage(), e); + } + } + public String getStoreClientConfigString(List storeNames, String coordinatorUrl) { try { // Create the REST request - RestRequestBuilder requestBuilder = new RestRequestBuilder(new URI(coordinatorUrl - + URL_SEPARATOR - + STORE_CLIENT_CONFIG_OPS - + URL_SEPARATOR - + Joiner.on(",") - .join(storeNames))); + StringBuilder URIStringBuilder = new StringBuilder().append(coordinatorUrl) + .append(URL_SEPARATOR) + .append(STORE_CLIENT_CONFIG_OPS) + .append(URL_SEPARATOR) + .append(Joiner.on(",") + .join(storeNames)); + + RestRequestBuilder requestBuilder = new RestRequestBuilder(new URI(URIStringBuilder.toString())); String timeoutStr = Long.toString(this.config.getTimeoutConfig() .getOperationTimeout(VoldemortOpCode.GET_OP_CODE)); - requestBuilder.setMethod(GET); + requestBuilder.setMethod(requestType.GET.toString()); requestBuilder.setHeader(RestMessageHeaders.X_VOLD_REQUEST_TIMEOUT_MS, timeoutStr); - requestBuilder.setHeader(RestMessageHeaders.X_VOLD_REQUEST_ORIGIN_TIME_MS, - String.valueOf(System.currentTimeMillis())); - if(this.routingTypeCode != null) { - requestBuilder.setHeader(RestMessageHeaders.X_VOLD_ROUTING_TYPE_CODE, - this.routingTypeCode); - } - if(this.zoneId != INVALID_ZONE_ID) { - requestBuilder.setHeader(RestMessageHeaders.X_VOLD_ZONE_ID, - String.valueOf(this.zoneId)); - } + requestBuilder = setCommonRequestHeader(requestBuilder); RestRequest request = requestBuilder.build(); Future future = client.restRequest(request); // This will block RestResponse response = future.get(); ByteString entity = response.getEntity(); - result = entity.asString("UTF-8"); - } catch(ExecutionException e) { + return entity.asString("UTF-8"); + } catch(Exception e) { if(e.getCause() instanceof RestException) { - RestException exception = (RestException) e.getCause(); - if(logger.isDebugEnabled()) { - logger.debug("REST EXCEPTION STATUS : " + exception.getResponse().getStatus()); - } - - } else { - throw new VoldemortException("Unknown HTTP request execution exception: " - + e.getMessage(), e); + return ((RestException) e.getCause()).getResponse().getEntity().asString("UTF-8"); } - } catch(InterruptedException e) { - if(logger.isDebugEnabled()) { - logger.debug("Operation interrupted : " + e.getMessage(), e); - } - throw new VoldemortException("Operation interrupted exception: " + e.getMessage(), e); - } catch(URISyntaxException e) { - throw new VoldemortException("Illegal HTTP URL" + e.getMessage(), e); + handleRequestAndResponseException(e); } - return result; + return null; } public boolean putStoreClientConfigString(String storeClientConfigAvro, String coordinatorUrl) { try { // Create the REST request - RestRequestBuilder requestBuilder = new RestRequestBuilder(new URI(coordinatorUrl - + URL_SEPARATOR - + STORE_CLIENT_CONFIG_OPS - + URL_SEPARATOR)); + StringBuilder URIStringBuilder = new StringBuilder().append(coordinatorUrl) + .append(URL_SEPARATOR) + .append(STORE_CLIENT_CONFIG_OPS) + .append(URL_SEPARATOR); + RestRequestBuilder requestBuilder = new RestRequestBuilder(new URI(URIStringBuilder.toString())); byte[] payload = storeClientConfigAvro.getBytes("UTF-8"); // Create a HTTP POST request - requestBuilder.setMethod(POST); + requestBuilder.setMethod(requestType.POST.toString()); requestBuilder.setEntity(payload); requestBuilder.setHeader(CONTENT_TYPE, "binary"); requestBuilder.setHeader(CONTENT_LENGTH, "" + payload.length); String timeoutStr = Long.toString(this.config.getTimeoutConfig() .getOperationTimeout(VoldemortOpCode.PUT_OP_CODE)); requestBuilder.setHeader(RestMessageHeaders.X_VOLD_REQUEST_TIMEOUT_MS, timeoutStr); - requestBuilder.setHeader(RestMessageHeaders.X_VOLD_REQUEST_ORIGIN_TIME_MS, - String.valueOf(System.currentTimeMillis())); - if(this.routingTypeCode != null) { - requestBuilder.setHeader(RestMessageHeaders.X_VOLD_ROUTING_TYPE_CODE, - this.routingTypeCode); - } - if(this.zoneId != INVALID_ZONE_ID) { - requestBuilder.setHeader(RestMessageHeaders.X_VOLD_ZONE_ID, - String.valueOf(this.zoneId)); - } + requestBuilder = setCommonRequestHeader(requestBuilder); RestRequest request = requestBuilder.build(); Future future = client.restRequest(request); @@ -163,31 +172,10 @@ public boolean putStoreClientConfigString(String storeClientConfigAvro, String c } return false; } - System.out.println(entity.asString("UTF-8")); - return true; - - } catch(ExecutionException e) { - if(e.getCause() instanceof RestException) { - RestException exception = (RestException) e.getCause(); - if(logger.isDebugEnabled()) { - logger.debug("REST EXCEPTION STATUS : " + exception.getResponse().getStatus()); - } - - } else { - throw new VoldemortException("Unknown HTTP request execution exception: " - + e.getMessage(), e); - } - } catch(InterruptedException e) { - if(logger.isDebugEnabled()) { - logger.debug("Operation interrupted : " + e.getMessage(), e); - } - throw new VoldemortException("Operation interrupted exception: " + e.getMessage(), e); - } catch(URISyntaxException e) { - throw new VoldemortException("Illegal HTTP URL" + e.getMessage(), e); - } catch(UnsupportedEncodingException e) { - throw new VoldemortException("Illegal Encoding Type " + e.getMessage()); + } catch(Exception e) { + handleRequestAndResponseException(e); } return false; } @@ -216,30 +204,22 @@ public boolean putStoreClientConfigMap(Map storeClientConfigMap, } public boolean deleteStoreClientConfig(List storeNames, String coordinatorUrl) { - try { // Create the REST request - RestRequestBuilder requestBuilder = new RestRequestBuilder(new URI(coordinatorUrl - + URL_SEPARATOR - + STORE_CLIENT_CONFIG_OPS - + URL_SEPARATOR - + Joiner.on(",") - .join(storeNames))); + StringBuilder URIStringBuilder = new StringBuilder().append(coordinatorUrl) + .append(URL_SEPARATOR) + .append(STORE_CLIENT_CONFIG_OPS) + .append(URL_SEPARATOR) + .append(Joiner.on(",") + .join(storeNames)); + RestRequestBuilder requestBuilder = new RestRequestBuilder(new URI(URIStringBuilder.toString())); String timeoutStr = Long.toString(this.config.getTimeoutConfig() .getOperationTimeout(VoldemortOpCode.GET_OP_CODE)); // Create a HTTP POST request - requestBuilder.setMethod(DELETE); + requestBuilder.setMethod(requestType.DELETE.toString()); requestBuilder.setHeader(RestMessageHeaders.X_VOLD_REQUEST_TIMEOUT_MS, timeoutStr); - requestBuilder.setHeader(RestMessageHeaders.X_VOLD_REQUEST_ORIGIN_TIME_MS, - String.valueOf(System.currentTimeMillis())); - if(this.routingTypeCode != null) { - requestBuilder.setHeader(RestMessageHeaders.X_VOLD_ROUTING_TYPE_CODE, - this.routingTypeCode); - } - if(this.zoneId != INVALID_ZONE_ID) { - requestBuilder.setHeader(RestMessageHeaders.X_VOLD_ZONE_ID, - String.valueOf(this.zoneId)); - } + requestBuilder = setCommonRequestHeader(requestBuilder); + RestRequest request = requestBuilder.build(); Future future = client.restRequest(request); // This will block @@ -251,29 +231,10 @@ public boolean deleteStoreClientConfig(List storeNames, String coordinat } return false; } - System.out.println(entity.asString("UTF-8")); - return true; - - } catch(ExecutionException e) { - if(e.getCause() instanceof RestException) { - RestException exception = (RestException) e.getCause(); - if(logger.isDebugEnabled()) { - logger.debug("REST EXCEPTION STATUS : " + exception.getResponse().getStatus()); - } - - } else { - throw new VoldemortException("Unknown HTTP request execution exception: " - + e.getMessage(), e); - } - } catch(InterruptedException e) { - if(logger.isDebugEnabled()) { - logger.debug("Operation interrupted : " + e.getMessage(), e); - } - throw new VoldemortException("Operation interrupted exception: " + e.getMessage(), e); - } catch(URISyntaxException e) { - throw new VoldemortException("Illegal HTTP URL" + e.getMessage(), e); + } catch(Exception e) { + handleRequestAndResponseException(e); } return false; } diff --git a/contrib/restclient/src/java/voldemort/restclient/admin/CoordinatorAdminCommand.java b/contrib/restclient/src/java/voldemort/restclient/admin/CoordinatorAdminCommand.java index 0fb7d98760..21875910df 100644 --- a/contrib/restclient/src/java/voldemort/restclient/admin/CoordinatorAdminCommand.java +++ b/contrib/restclient/src/java/voldemort/restclient/admin/CoordinatorAdminCommand.java @@ -56,7 +56,7 @@ public static void printHelp(PrintStream stream) { stream.println("Voldemort Coordinator Admin Tool"); stream.println("--------------------------------"); stream.println("get Get store client config for stores."); - stream.println("put Put store client config for stroes from input string or given avro file"); + stream.println("put Put store client config for stores from input string or given avro file"); stream.println("delete Delete store client config for stores"); stream.println(); stream.println("To get more information on each command, please try \'help \'."); diff --git a/contrib/restclient/test/voldemort/client/CoordinatorAdminClientTest.java b/contrib/restclient/test/voldemort/client/CoordinatorAdminClientTest.java index 67b898a591..896400b1be 100644 --- a/contrib/restclient/test/voldemort/client/CoordinatorAdminClientTest.java +++ b/contrib/restclient/test/voldemort/client/CoordinatorAdminClientTest.java @@ -108,7 +108,7 @@ public void tearDown() throws Exception { @Test public void test() { String configAvro1 = "{\"test\": {\"connection_timeout_ms\": \"1111\", \"socket_timeout_ms\": \"1024\"}}"; - String configAvro2 = "{\"test\": {\"connection_timeout_ms\": \"1111\", \"socket_timeout_ms\": \"1024\"}}"; + String configAvro2 = "{\"test\": {\"connection_timeout_ms\": \"2222\", \"socket_timeout_ms\": \"2048\"}}"; String getConfigAvro; // put avro 1 assertTrue(adminClient.putStoreClientConfigString(configAvro1, ADMIN_URL)); diff --git a/test/common/coordinator/config/clientConfigs.avro b/test/common/coordinator/config/clientConfigs.avro index 2ce20a6b22..cca5ced99a 100644 --- a/test/common/coordinator/config/clientConfigs.avro +++ b/test/common/coordinator/config/clientConfigs.avro @@ -1,11 +1,6 @@ { - "test": - { - "socket_timeout_ms": "1500" - }, - "best": - { - "connection_timeout_ms": "500", - "socket_timeout_ms": "1500" - } -} + "best": { + "socket_timeout_ms": "1500", + "connection_timeout_ms": "500" + } +} \ No newline at end of file