use of com.linkedin.kafka.cruisecontrol.servlet.UserRequestException in project cruise-control by linkedin.
the class ParameterUtils method topicPatternByReplicationFactorFromBody.
@SuppressWarnings("unchecked")
private static Map<Short, Pattern> topicPatternByReplicationFactorFromBody(HttpServletRequest request) {
Map<Short, Pattern> topicPatternByReplicationFactor;
try {
Gson gson = new Gson();
Map<String, Object> json = gson.fromJson(request.getReader(), Map.class);
if (json == null) {
return null;
}
String replicationFactorKey = REPLICATION_FACTOR.name().toLowerCase();
if (!json.containsKey(replicationFactorKey)) {
return null;
}
Map<String, Object> replicationFactorParams = (Map<String, Object>) json.get(replicationFactorKey);
if (!replicationFactorParams.containsKey(TOPIC_BY_REPLICATION_FACTOR)) {
return null;
}
topicPatternByReplicationFactor = new HashMap<>();
for (Map.Entry<String, String> entry : ((Map<String, String>) replicationFactorParams.get(TOPIC_BY_REPLICATION_FACTOR)).entrySet()) {
short replicationFactor = Short.parseShort(entry.getKey().trim());
Pattern topicPattern = Pattern.compile(entry.getValue().trim());
topicPatternByReplicationFactor.putIfAbsent(replicationFactor, topicPattern);
}
} catch (IOException ioe) {
throw new UserRequestException(String.format("Illegal value for field %s in body, please specify in pairs of \"target replication " + "factor\" : \"topic name regex\".", TOPIC_BY_REPLICATION_FACTOR));
}
return topicPatternByReplicationFactor;
}
use of com.linkedin.kafka.cruisecontrol.servlet.UserRequestException in project cruise-control by linkedin.
the class ParameterUtils method endPoint.
/**
* @param request The Http request.
* @return The endpoint specified in the given request.
*/
public static CruiseControlEndPoint endPoint(HttpServletRequest request) {
List<CruiseControlEndPoint> supportedEndpoints;
switch(request.getMethod()) {
case GET_METHOD:
supportedEndpoints = CruiseControlEndPoint.getEndpoints();
break;
case POST_METHOD:
supportedEndpoints = CruiseControlEndPoint.postEndpoints();
break;
default:
throw new UserRequestException("Unsupported request method: " + request.getMethod() + ".");
}
String pathInfo = request.getPathInfo();
if (pathInfo == null) {
// URL does not have any extra path information
return null;
}
// Skip the first character '/'
String path = pathInfo.substring(1);
for (CruiseControlEndPoint endPoint : supportedEndpoints) {
if (endPoint.toString().equalsIgnoreCase(path)) {
return endPoint;
}
}
return null;
}
use of com.linkedin.kafka.cruisecontrol.servlet.UserRequestException in project cruise-control by linkedin.
the class ParameterUtils method topicPatternByReplicationFactor.
static Map<Short, Pattern> topicPatternByReplicationFactor(HttpServletRequest request) {
Pattern topic = topic(request);
Short replicationFactor = replicationFactor(request);
Map<Short, Pattern> topicPatternByReplicationFactorFromBody = topicPatternByReplicationFactorFromBody(request);
if (topicPatternByReplicationFactorFromBody != null) {
if (topic != null || replicationFactor != null) {
throw new UserRequestException("Requesting topic replication factor change from both HTTP request parameter and body" + " is forbidden.");
}
return topicPatternByReplicationFactorFromBody;
} else {
if (topic == null && replicationFactor != null) {
throw new UserRequestException("Topic is not specified in URL while target replication factor is specified.");
}
if ((topic != null && replicationFactor == null)) {
throw new UserRequestException("Topic's replication factor is not specified in URL while subject topic is specified.");
}
if (topic != null) {
return Collections.singletonMap(replicationFactor, topic);
}
}
return Collections.emptyMap();
}
use of com.linkedin.kafka.cruisecontrol.servlet.UserRequestException in project cruise-control by linkedin.
the class ParameterUtils method maxPartitionMovements.
/**
* Get the max inter broker partition movements requirement dynamically set from the Http request.
*
* @param request The HTTP Request
* @return The execution upper bound requirement dynamically set from the Http request.
*/
static Integer maxPartitionMovements(HttpServletRequest request) {
String parameterString = caseSensitiveParameterName(request.getParameterMap(), MAX_PARTITION_MOVEMENTS_IN_CLUSTER_PARAM);
if (parameterString == null) {
return null;
}
int maxPartitionMovements = Integer.parseInt(request.getParameter(parameterString));
if (maxPartitionMovements <= 0) {
throw new UserRequestException("The requested max partition movement must be positive (Requested: " + maxPartitionMovements + ").");
}
return maxPartitionMovements;
}
use of com.linkedin.kafka.cruisecontrol.servlet.UserRequestException in project cruise-control by linkedin.
the class PartitionLoadParameters method initParameters.
@Override
protected void initParameters() throws UnsupportedEncodingException {
super.initParameters();
String resourceString = ParameterUtils.resourceString(_request);
try {
_resource = Resource.valueOf(resourceString.toUpperCase());
} catch (IllegalArgumentException iae) {
throw new UserRequestException(String.format("Invalid resource type %s. The resource type must be one of the " + "following: CPU, DISK, NW_IN, NW_OUT", resourceString));
}
_wantMaxLoad = ParameterUtils.wantMaxLoad(_request);
_wantAvgLoad = ParameterUtils.wantAvgLoad(_request);
if (_wantMaxLoad && _wantAvgLoad) {
throw new UserRequestException("Parameters to ask for max and avg load are mutually exclusive to each other.");
}
_topic = ParameterUtils.topic(_request);
_partitionLowerBoundary = ParameterUtils.partitionBoundary(_request, false);
_partitionUpperBoundary = ParameterUtils.partitionBoundary(_request, true);
_entries = ParameterUtils.entries(_request);
_minValidPartitionRatio = ParameterUtils.minValidPartitionRatio(_request);
_allowCapacityEstimation = ParameterUtils.allowCapacityEstimation(_request);
_brokerIds = ParameterUtils.brokerIds(_request, true);
_startMs = ParameterUtils.startMsOrDefault(_request, ParameterUtils.DEFAULT_START_TIME_FOR_CLUSTER_MODEL);
_endMs = ParameterUtils.endMsOrDefault(_request, System.currentTimeMillis());
ParameterUtils.validateTimeRange(_startMs, _endMs);
}
Aggregations