Skip to content

Commit

Permalink
addressed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Xu Ha committed Oct 2, 2014
1 parent 0ec00a1 commit 5ae570f
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 131 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand All @@ -59,97 +61,104 @@ public CoordinatorAdminClient() {
public CoordinatorAdminClient(RESTClientConfig config) {
this.config = config;
this.httpClientFactory = new HttpClientFactory();
Map<String, String> properties = new HashMap<String, String>();
Map<String, String> 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<String> 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<String> 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<RestResponse> 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<RestResponse> future = client.restRequest(request);
Expand All @@ -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;
}
Expand Down Expand Up @@ -216,30 +204,22 @@ public boolean putStoreClientConfigMap(Map<String, String> storeClientConfigMap,
}

public boolean deleteStoreClientConfig(List<String> 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<RestResponse> future = client.restRequest(request);
// This will block
Expand All @@ -251,29 +231,10 @@ public boolean deleteStoreClientConfig(List<String> 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;
}
Expand Down
Expand Up @@ -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 <command-name>\'.");
Expand Down
Expand Up @@ -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));
Expand Down
15 changes: 5 additions & 10 deletions 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"
}
}

0 comments on commit 5ae570f

Please sign in to comment.