Skip to content

Commit

Permalink
AMBARI-10063. CapSched View: Add Support for Node labels and versions…
Browse files Browse the repository at this point in the history
… (alexantonenko)
  • Loading branch information
hiveww committed Mar 14, 2015
1 parent a5f99f4 commit 95103a9
Show file tree
Hide file tree
Showing 34 changed files with 2,869 additions and 1,149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,12 @@ public ConfigurationService(ViewContext context) {
// ================================================================================

private final String versionTagUrl = "%s?fields=Clusters/desired_configs/capacity-scheduler";
private final String configurationUrl = "%%s/configurations?type=capacity-scheduler&tag=%s";
private final String configurationUrl = "%s/configurations?type=capacity-scheduler";
private final String configurationUrlByTag = "%%s/configurations?type=capacity-scheduler&tag=%s";
private final String rmHostUrl = "%s/services/YARN/components/RESOURCEMANAGER?fields=host_components/host_name";

private final String rmGetNodeLabelUrl = "http://%s:8088/ws/v1/cluster/get-node-labels";

// ================================================================================
// Privilege Reading
// ================================================================================
Expand All @@ -131,7 +134,7 @@ public ConfigurationService(ViewContext context) {
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response readConfiguration() {
public Response readLatestConfiguration() {
Response response = null;
try {
validateViewConfiguration();
Expand All @@ -148,6 +151,55 @@ public Response readConfiguration() {
return response;
}

/**
* Gets capacity scheduler configuration by all tags.
*
* @return scheduler configuration
*/
@GET
@Path("all")
@Produces(MediaType.APPLICATION_JSON)
public Response readAllConfigurations() {
Response response = null;
try {
validateViewConfiguration();

String url = String.format(configurationUrl, baseUrl);
JSONObject configurations = proxy.request(url).get().asJSON();
response = Response.ok(configurations).build();
} catch (WebApplicationException ex) {
throw ex;
} catch (Exception ex) {
throw new ServiceFormattedException(ex.getMessage(), ex);
}

return response;
}

/**
* Gets capacity scheduler configuration by specific tag.
*
* @return scheduler configuration
*/
@GET
@Path("byTag/{tag}")
@Produces(MediaType.APPLICATION_JSON)
public Response readConfigurationByTag(@PathParam("tag") String tag) {
Response response = null;
try {
validateViewConfiguration();

JSONObject configurations = getConfigurationFromAmbari(tag);
response = Response.ok(configurations).build();
} catch (WebApplicationException ex) {
throw ex;
} catch (Exception ex) {
throw new ServiceFormattedException(ex.getMessage(), ex);
}

return response;
}

/**
* Gets the privilege for this user.
*
Expand All @@ -172,6 +224,31 @@ public Response getPrivilege() {
return response;
}

/**
* Gets node labels from RM
*
* @return node labels
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/nodeLabels")
public Response getNodeLabels() {
Response response;

try {
String nodeLabels = proxy.request(String.format(rmGetNodeLabelUrl, getRMHost())).get().asString();

response = Response.ok(nodeLabels).build();
} catch (WebApplicationException ex) {
throw ex;
} catch (Exception ex) {
throw new ServiceFormattedException(ex.getMessage(), ex);
}

return response;
}


/**
* Checks if the user is an operator.
*
Expand All @@ -183,7 +260,7 @@ private boolean isOperator() {
// first check if the user is an CLUSTER.OPERATOR
String url = String.format(clusterOperatorPrivilegeUrl, baseUrl, context.getUsername());
JSONObject json = proxy.request(url).get().asJSON();

if (json == null || json.size() <= 0) {
// user is not a CLUSTER.OPERATOR but might be an AMBARI.ADMIN
url = String.format(ambariAdminPrivilegeUrl, serverUrl, context.getUsername());
Expand Down Expand Up @@ -215,7 +292,7 @@ private void validateViewConfiguration() {
}

private JSONObject getConfigurationFromAmbari(String versionTag) {
String urlTemplate = String.format(configurationUrl, versionTag);
String urlTemplate = String.format(configurationUrlByTag, versionTag);
String url = String.format(urlTemplate, baseUrl);
return proxy.request(url).get().asJSON();
}
Expand Down Expand Up @@ -267,24 +344,25 @@ private JSONObject getDesiredConfigs() {
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response writeConfiguration(JSONObject request) {
JSONObject response;
try {
validateViewConfiguration();

if (isOperator() == false) {
return Response.status(401).build();
}

proxy.request(baseUrl).
setData(makeConfigUpdateData(request)).
put();
response = proxy.request(baseUrl).
setData(request).
put().asJSON();

} catch (WebApplicationException ex) {
throw ex;
} catch (Exception ex) {
throw new ServiceFormattedException(ex.getMessage(), ex);
}

return readConfiguration();
return Response.ok(response).build();
}

/**
Expand All @@ -302,8 +380,6 @@ public Response writeAndRefreshConfiguration(JSONObject request) {
if (isOperator() == false) {
return Response.status(401).build();
}

writeConfiguration(request);

String rmHost = getRMHost();
JSONObject data = (JSONObject) JSONValue.parse(String.format(refreshRMRequestData, rmHost));
Expand All @@ -316,7 +392,7 @@ public Response writeAndRefreshConfiguration(JSONObject request) {
} catch (Exception ex) {
throw new ServiceFormattedException(ex.getMessage(), ex);
}
return readConfiguration();
return readLatestConfiguration();
}

/**
Expand All @@ -335,8 +411,6 @@ public Response writeAndRestartConfiguration(JSONObject request) {
return Response.status(401).build();
}

writeConfiguration(request);

String rmHost = getRMHost();
JSONObject data = (JSONObject) JSONValue.parse(String.format(restartRMRequestData, rmHost, rmHost));
proxy.request(baseUrl + "/requests/").
Expand All @@ -348,7 +422,7 @@ public Response writeAndRestartConfiguration(JSONObject request) {
} catch (Exception ex) {
throw new ServiceFormattedException(ex.getMessage(), ex);
}
return readConfiguration();
return readLatestConfiguration();
}

private String getRMHost() {
Expand All @@ -367,17 +441,4 @@ private String getRMHost() {
return rmHost;
}

private JSONObject makeConfigUpdateData(JSONObject request) {
JSONObject desiredConfigs = (JSONObject) request.clone();
desiredConfigs.put("type", "capacity-scheduler");
desiredConfigs.put("tag", "version" + String.valueOf(System.currentTimeMillis()));

JSONObject clusters = new JSONObject();
clusters.put("desired_configs", desiredConfigs);

JSONObject data = new JSONObject();
data.put("Clusters", clusters);
return data;
}

}
Loading

0 comments on commit 95103a9

Please sign in to comment.