Search in sources :

Example 1 with EndPoint

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

the class KafkaCruiseControlServlet method doPost.

/**
 * The POST method allows user to perform the following actions:
 *
 * <pre>
 * 1. Decommission a broker.
 *    POST /kafkacruisecontrol/remove_broker?brokerid=[id1,id2...]&dryrun=[true/false]&throttle_removed_broker=[true/false]&goals=[goal1,goal2...]
 *
 * 2. Add a broker
 *    POST /kafkacruisecontrol/add_broker?brokerid=[id1,id2...]&dryrun=[true/false]&throttle_added_broker=[true/false]&goals=[goal1,goal2...]
 *
 * 3. Trigger a workload balance.
 *    POST /kafkacruisecontrol/rebalance?dryrun=[true/false]&force=[true/false]&goals=[goal1,goal2...]
 *
 * 4. Stop the proposal execution.
 *    POST /kafkacruisecontrol/stop_proposal_execution
 *
 * 5.Pause metrics sampling. (RUNNING -> PAUSED).
 *    POST /kafkacruisecontrol/pause_sampling
 *
 * 6.Resume metrics sampling. (PAUSED -> RUNNING).
 *    POST /kafkacruisecontrol/resume_sampling
 *
 * <b>NOTE: All the timestamps are epoch time in second granularity.</b>
 * </pre>
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    ACCESS_LOG.info("Received {}, {} from {}", urlEncode(request.toString()), urlEncode(request.getRequestURL().toString()), KafkaCruiseControlServletUtils.getClientIpAddress(request));
    try {
        _asyncOperationStep.set(0);
        EndPoint endPoint = endPoint(request);
        if (endPoint != null) {
            Set<String> validParamNames = VALID_ENDPOINT_PARAM_NAMES.get(endPoint);
            Set<String> userParams = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
            if (validParamNames != null) {
                userParams.addAll(request.getParameterMap().keySet());
                userParams.removeAll(validParamNames);
            }
            if (!userParams.isEmpty()) {
                // User request specifies parameters that are not a subset of the valid parameters.
                String errorResp = String.format("Unrecognized endpoint parameters in %s post request: %s.", endPoint, userParams.toString());
                setErrorResponse(response, "", errorResp, SC_BAD_REQUEST, wantJSON(request));
            } else {
                switch(endPoint) {
                    case ADD_BROKER:
                    case REMOVE_BROKER:
                        if (addOrRemoveBroker(request, response, endPoint)) {
                            _sessionManager.closeSession(request);
                        }
                        break;
                    case REBALANCE:
                        if (rebalance(request, response)) {
                            _sessionManager.closeSession(request);
                        }
                        break;
                    case STOP_PROPOSAL_EXECUTION:
                        stopProposalExecution();
                        break;
                    case PAUSE_SAMPLING:
                        pauseSampling();
                        break;
                    case RESUME_SAMPLING:
                        resumeSampling();
                        break;
                    default:
                        throw new UserRequestException("Invalid URL for POST");
                }
            }
        } else {
            String errorMessage = String.format("Unrecognized endpoint in POST request '%s'%nSupported POST endpoints: %s", request.getPathInfo(), EndPoint.postEndpoint());
            setErrorResponse(response, "", errorMessage, SC_NOT_FOUND, wantJSON(request));
        }
    } catch (UserRequestException ure) {
        LOG.error("Why are you failing?", ure);
        String errorMessage = String.format("Bad POST request '%s'", request.getPathInfo());
        StringWriter sw = new StringWriter();
        ure.printStackTrace(new PrintWriter(sw));
        setErrorResponse(response, sw.toString(), errorMessage, SC_BAD_REQUEST, wantJSON(request));
        _sessionManager.closeSession(request);
    } catch (Exception e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        String errorMessage = String.format("Error processing POST request '%s'", request.getPathInfo());
        LOG.error(errorMessage, e);
        setErrorResponse(response, sw.toString(), errorMessage, SC_INTERNAL_SERVER_ERROR, wantJSON(request));
        _sessionManager.closeSession(request);
    } finally {
        try {
            response.getOutputStream().close();
        } catch (IOException e) {
            LOG.warn("Error closing output stream: ", e);
        }
    }
}
Also used : StringWriter(java.io.StringWriter) TreeSet(java.util.TreeSet) EndPoint(com.linkedin.kafka.cruisecontrol.servlet.KafkaCruiseControlServlet.EndPoint) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) PrintWriter(java.io.PrintWriter)

Example 2 with EndPoint

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

the class KafkaCruiseControlServlet method endPoint.

private EndPoint endPoint(HttpServletRequest request) {
    List<EndPoint> supportedEndpoints;
    switch(request.getMethod()) {
        case "GET":
            supportedEndpoints = EndPoint.getEndpoint();
            break;
        case "POST":
            supportedEndpoints = EndPoint.postEndpoint();
            break;
        default:
            throw new IllegalArgumentException("Unsupported request method: " + request.getMethod() + ".");
    }
    String path = request.getRequestURI().toUpperCase().replace("/KAFKACRUISECONTROL/", "");
    for (EndPoint endPoint : supportedEndpoints) {
        if (endPoint.toString().equalsIgnoreCase(path)) {
            return endPoint;
        }
    }
    return null;
}
Also used : EndPoint(com.linkedin.kafka.cruisecontrol.servlet.KafkaCruiseControlServlet.EndPoint)

Example 3 with EndPoint

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

the class KafkaCruiseControlServlet method doGet.

/**
 * The GET requests can do the following:
 *
 * NOTE: ADD json=true to the query parameters to get 200/OK response in JSON format.
 *
 * <pre>
 * 1. Bootstrap the load monitor
 *    RANGE MODE:
 *      GET /kafkacruisecontrol/bootstrap?start=[START_TIMESTAMP]&end=[END_TIMESTAMP]
 *    SINCE MODE:
 *      GET /kafkacruisecontrol/bootstrap?start=[START_TIMESTAMP]
 *    RECENT MODE:
 *      GET /kafkacruisecontrol/bootstrap
 *
 * 2. Train the Kafka Cruise Control linear regression model. The trained model will only be used if
 *    use.linear.regression.model is set to true.
 *    GET /kafkacruisecontrol/train?start=[START_TIMESTAMP]&end=[END_TIMESTAMP]
 *
 * 3. Get the cluster load
 *    GET /kafkacruisecontrol/load?time=[TIMESTAMP]&granularity=[GRANULARITY]
 *    The valid granularity value are "replica" and "broker". The default is broker level.
 *
 * 4. Get the partition load sorted by the utilization of a given resource
 *    GET /kafkacruisecontrol/partition_load?resource=[RESOURCE]&start=[START_TIMESTAMP]&end=[END_TIMESTAMP]
 *
 * 5. Get an optimization proposal
 *    GET /kafkacruisecontrol/proposals?verbose=[ENABLE_VERBOSE]&ignore_proposal_cache=[true/false]
 *    &goals=[goal1,goal2...]&data_from=[valid_windows/valid_partitions]
 *
 * 6. query the state of Kafka Cruise Control
 *    GET /kafkacruisecontrol/state
 *
 * 7. query the Kafka cluster state
 *    GET /kafkacruisecontrol/kafka_cluster_state
 *
 * <b>NOTE: All the timestamps are epoch time in second granularity.</b>
 * </pre>
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    ACCESS_LOG.info("Received {}, {} from {}", urlEncode(request.toString()), urlEncode(request.getRequestURL().toString()), KafkaCruiseControlServletUtils.getClientIpAddress(request));
    try {
        _asyncOperationStep.set(0);
        EndPoint endPoint = endPoint(request);
        if (endPoint != null) {
            Set<String> validParamNames = VALID_ENDPOINT_PARAM_NAMES.get(endPoint);
            Set<String> userParams = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
            if (validParamNames != null) {
                userParams.addAll(request.getParameterMap().keySet());
                userParams.removeAll(validParamNames);
            }
            if (!userParams.isEmpty()) {
                // User request specifies parameters that are not a subset of the valid parameters.
                String errorResp = String.format("Unrecognized endpoint parameters in %s get request: %s.", endPoint, userParams.toString());
                setErrorResponse(response, "", errorResp, SC_BAD_REQUEST, wantJSON(request));
            } else {
                switch(endPoint) {
                    case BOOTSTRAP:
                        bootstrap(request, response);
                        break;
                    case TRAIN:
                        train(request, response);
                        break;
                    case LOAD:
                        if (getClusterLoad(request, response)) {
                            _sessionManager.closeSession(request);
                        }
                        break;
                    case PARTITION_LOAD:
                        if (getPartitionLoad(request, response)) {
                            _sessionManager.closeSession(request);
                        }
                        break;
                    case PROPOSALS:
                        if (getProposals(request, response)) {
                            _sessionManager.closeSession(request);
                        }
                        break;
                    case STATE:
                        if (getState(request, response)) {
                            _sessionManager.closeSession(request);
                        }
                        break;
                    case KAFKA_CLUSTER_STATE:
                        getKafkaClusterState(request, response);
                        break;
                    default:
                        throw new UserRequestException("Invalid URL for GET");
                }
            }
        } else {
            String errorMessage = String.format("Unrecognized endpoint in GET request '%s'%nSupported GET endpoints: %s", request.getPathInfo(), EndPoint.getEndpoint());
            setErrorResponse(response, "", errorMessage, SC_NOT_FOUND, wantJSON(request));
        }
    } catch (UserRequestException ure) {
        LOG.error("Why are you failing?", ure);
        String errorMessage = String.format("Bad GET request '%s'", request.getPathInfo());
        StringWriter sw = new StringWriter();
        ure.printStackTrace(new PrintWriter(sw));
        setErrorResponse(response, sw.toString(), errorMessage, SC_BAD_REQUEST, wantJSON(request));
        _sessionManager.closeSession(request);
    } catch (Exception e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        String errorMessage = String.format("Error processing GET request '%s'", request.getPathInfo());
        LOG.error(errorMessage, e);
        setErrorResponse(response, sw.toString(), errorMessage, SC_INTERNAL_SERVER_ERROR, wantJSON(request));
        _sessionManager.closeSession(request);
    } finally {
        try {
            response.getOutputStream().close();
        } catch (IOException e) {
            LOG.warn("Error closing output stream: ", e);
        }
    }
}
Also used : StringWriter(java.io.StringWriter) TreeSet(java.util.TreeSet) EndPoint(com.linkedin.kafka.cruisecontrol.servlet.KafkaCruiseControlServlet.EndPoint) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) PrintWriter(java.io.PrintWriter)

Aggregations

EndPoint (com.linkedin.kafka.cruisecontrol.servlet.KafkaCruiseControlServlet.EndPoint)3 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 TreeSet (java.util.TreeSet)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2