Search in sources :

Example 1 with UserRequestException

use of com.linkedin.kafka.cruisecontrol.servlet.UserRequestException in project cruise-control by linkedin.

the class ParameterUtils method getReplicaMovementStrategy.

/**
 * Get a composite replica movement strategy for {@link com.linkedin.kafka.cruisecontrol.executor.ExecutionTaskPlanner}.
 *
 * @param request The http request.
 * @param config The configuration of Cruise Control.
 * @return A composite strategy generated by chaining all the strategies specified by the http request.
 */
static ReplicaMovementStrategy getReplicaMovementStrategy(HttpServletRequest request, KafkaCruiseControlConfig config) throws UnsupportedEncodingException {
    if (getDryRun(request)) {
        return null;
    }
    List<String> strategies = getListParam(request, REPLICA_MOVEMENT_STRATEGIES_PARAM);
    if (strategies.isEmpty()) {
        return null;
    }
    List<ReplicaMovementStrategy> supportedStrategies = config.getConfiguredInstances(ExecutorConfig.REPLICA_MOVEMENT_STRATEGIES_CONFIG, ReplicaMovementStrategy.class);
    Map<String, ReplicaMovementStrategy> supportedStrategiesByName = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
    for (ReplicaMovementStrategy strategy : supportedStrategies) {
        supportedStrategiesByName.put(strategy.name(), strategy);
    }
    ReplicaMovementStrategy strategy = null;
    for (String strategyName : strategies) {
        if (supportedStrategiesByName.containsKey(strategyName)) {
            strategy = strategy == null ? supportedStrategiesByName.get(strategyName) : strategy.chain(supportedStrategiesByName.get(strategyName));
        } else {
            throw new UserRequestException("Strategy " + strategyName + " is not supported. Supported: " + supportedStrategiesByName.keySet());
        }
    }
    return strategy.chainBaseReplicaMovementStrategyIfAbsent();
}
Also used : ReplicaMovementStrategy(com.linkedin.kafka.cruisecontrol.executor.strategy.ReplicaMovementStrategy) TreeMap(java.util.TreeMap) UserRequestException(com.linkedin.kafka.cruisecontrol.servlet.UserRequestException)

Example 2 with UserRequestException

use of com.linkedin.kafka.cruisecontrol.servlet.UserRequestException in project cruise-control by linkedin.

the class ParameterUtils method numBrokersToAdd.

/**
 * Get the {@link #NUM_BROKERS_TO_ADD} from the request.
 *
 * Default: {@link ProvisionRecommendation#DEFAULT_OPTIONAL_INT}
 * @param request Http servlet request.
 * @return The value of {@link #NUM_BROKERS_TO_ADD} parameter.
 * @throws UserRequestException if the number of brokers to add is not a positive integer.
 */
static int numBrokersToAdd(HttpServletRequest request) {
    String parameterString = caseSensitiveParameterName(request.getParameterMap(), NUM_BROKERS_TO_ADD);
    if (parameterString == null) {
        return ProvisionRecommendation.DEFAULT_OPTIONAL_INT;
    }
    int numBrokersToAdd = Integer.parseInt(request.getParameter(parameterString));
    if (numBrokersToAdd <= 0) {
        throw new UserRequestException("The requested number of brokers to add must be positive (Requested: " + numBrokersToAdd + ").");
    }
    return numBrokersToAdd;
}
Also used : UserRequestException(com.linkedin.kafka.cruisecontrol.servlet.UserRequestException) CruiseControlEndPoint(com.linkedin.kafka.cruisecontrol.servlet.CruiseControlEndPoint) EndPoint(com.linkedin.cruisecontrol.servlet.EndPoint)

Example 3 with UserRequestException

use of com.linkedin.kafka.cruisecontrol.servlet.UserRequestException in project cruise-control by linkedin.

the class ParameterUtils method reviewId.

/**
 * Mutually exclusive with the other parameters and can only be used if two step verification is enabled.
 * @param request Http servlet request.
 * @param twoStepVerificationEnabled {@code true} if two-step verification is enabled, {@code false} otherwise.
 * @return Review Id.
 */
public static Integer reviewId(HttpServletRequest request, boolean twoStepVerificationEnabled) {
    String parameterString = caseSensitiveParameterName(request.getParameterMap(), REVIEW_ID_PARAM);
    if (parameterString == null) {
        return null;
    } else if (!twoStepVerificationEnabled) {
        throw new UserRequestException(String.format("%s parameter is not relevant when two-step verification is disabled.", REVIEW_ID_PARAM));
    }
    Integer reviewId = Integer.parseInt(request.getParameter(parameterString));
    // Sanity check: Ensure that if a review id is provided, no other parameter is in the request.
    if (request.getParameterMap().size() != 1) {
        throw new UserRequestException(String.format("%s parameter must be mutually exclusive with other parameters (Request parameters: %s).", REVIEW_ID_PARAM, request.getParameterMap()));
    } else if (reviewId < 0) {
        throw new UserRequestException(String.format("%s cannot be negative (requested: %d).", REVIEW_ID_PARAM, reviewId));
    }
    return reviewId;
}
Also used : UserRequestException(com.linkedin.kafka.cruisecontrol.servlet.UserRequestException)

Example 4 with UserRequestException

use of com.linkedin.kafka.cruisecontrol.servlet.UserRequestException in project cruise-control by linkedin.

the class ParameterUtils method anomalyTypes.

private static Set<AnomalyType> anomalyTypes(HttpServletRequest request, boolean isEnable) throws UnsupportedEncodingException {
    Set<String> selfHealingForString = parseParamToStringSet(request, isEnable ? ENABLE_SELF_HEALING_FOR_PARAM : DISABLE_SELF_HEALING_FOR_PARAM);
    Set<AnomalyType> anomalyTypes = new HashSet<>();
    try {
        for (String shfString : selfHealingForString) {
            anomalyTypes.add(KafkaAnomalyType.valueOf(shfString.toUpperCase()));
        }
    } catch (IllegalArgumentException iae) {
        throw new UserRequestException(String.format("Unsupported anomaly types in %s. Supported: %s", selfHealingForString, KafkaAnomalyType.cachedValues()));
    }
    return Collections.unmodifiableSet(anomalyTypes);
}
Also used : KafkaAnomalyType(com.linkedin.kafka.cruisecontrol.detector.notifier.KafkaAnomalyType) AnomalyType(com.linkedin.cruisecontrol.detector.AnomalyType) UserRequestException(com.linkedin.kafka.cruisecontrol.servlet.UserRequestException) HashSet(java.util.HashSet)

Example 5 with UserRequestException

use of com.linkedin.kafka.cruisecontrol.servlet.UserRequestException in project cruise-control by linkedin.

the class ParameterUtils method substates.

/**
 * Empty parameter means all substates are requested.
 * @param request Http servlet request.
 * @return The value of {@link #SUBSTATES_PARAM} parameter.
 */
static Set<CruiseControlState.SubState> substates(HttpServletRequest request) throws UnsupportedEncodingException {
    Set<String> substatesString = parseParamToStringSet(request, SUBSTATES_PARAM);
    Set<CruiseControlState.SubState> substates = new HashSet<>();
    try {
        for (String substateString : substatesString) {
            substates.add(CruiseControlState.SubState.valueOf(substateString.toUpperCase()));
        }
    } catch (IllegalArgumentException iae) {
        throw new UserRequestException(String.format("Unsupported substates in %s. Supported: %s", substatesString, CruiseControlState.SubState.cachedValues()));
    }
    return Collections.unmodifiableSet(substates);
}
Also used : UserRequestException(com.linkedin.kafka.cruisecontrol.servlet.UserRequestException) HashSet(java.util.HashSet)

Aggregations

UserRequestException (com.linkedin.kafka.cruisecontrol.servlet.UserRequestException)15 CruiseControlEndPoint (com.linkedin.kafka.cruisecontrol.servlet.CruiseControlEndPoint)7 EndPoint (com.linkedin.cruisecontrol.servlet.EndPoint)5 HashSet (java.util.HashSet)3 TreeMap (java.util.TreeMap)2 Pattern (java.util.regex.Pattern)2 Gson (com.google.gson.Gson)1 AnomalyType (com.linkedin.cruisecontrol.detector.AnomalyType)1 KafkaAnomalyType (com.linkedin.kafka.cruisecontrol.detector.notifier.KafkaAnomalyType)1 ConcurrencyType (com.linkedin.kafka.cruisecontrol.executor.ConcurrencyType)1 ReplicaMovementStrategy (com.linkedin.kafka.cruisecontrol.executor.strategy.ReplicaMovementStrategy)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1