Search in sources :

Example 1 with ClusterModelStats

use of com.linkedin.kafka.cruisecontrol.model.ClusterModelStats in project cruise-control by linkedin.

the class KafkaCruiseControlServlet method rebalance.

private boolean rebalance(HttpServletRequest request, HttpServletResponse response) throws Exception {
    boolean dryrun;
    DataFrom dataFrom;
    List<String> goals;
    boolean json = wantJSON(request);
    try {
        dryrun = getDryRun(request);
        goals = getGoals(request);
        dataFrom = getDataFrom(request);
    } catch (Exception e) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        setErrorResponse(response, sw.toString(), e.getMessage(), SC_BAD_REQUEST, json);
        // Close session
        return true;
    }
    GoalsAndRequirements goalsAndRequirements = getGoalsAndRequirements(request, response, goals, dataFrom, false);
    if (goalsAndRequirements == null) {
        return false;
    }
    GoalOptimizer.OptimizerResult optimizerResult = getAndMaybeReturnProgress(request, response, () -> _asyncKafkaCruiseControl.rebalance(goalsAndRequirements.goals(), dryrun, goalsAndRequirements.requirements()));
    if (optimizerResult == null) {
        return false;
    }
    setResponseCode(response, SC_OK);
    OutputStream out = response.getOutputStream();
    out.write(KafkaCruiseControlServletUtils.getProposalSummary(optimizerResult).getBytes(StandardCharsets.UTF_8));
    for (Map.Entry<Goal, ClusterModelStats> entry : optimizerResult.statsByGoalPriority().entrySet()) {
        Goal goal = entry.getKey();
        out.write(String.format("%n%nStats for goal %s%s:%n", goal.name(), goalResultDescription(goal, optimizerResult)).getBytes(StandardCharsets.UTF_8));
        out.write(entry.getValue().toString().getBytes(StandardCharsets.UTF_8));
    }
    out.write(String.format("%nCluster load after rebalance:%n").getBytes(StandardCharsets.UTF_8));
    out.write(optimizerResult.brokerStatsAfterOptimization().toString().getBytes(StandardCharsets.UTF_8));
    out.flush();
    return true;
}
Also used : ClusterModelStats(com.linkedin.kafka.cruisecontrol.model.ClusterModelStats) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) TimeoutException(java.util.concurrent.TimeoutException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) KafkaAssignerEvenRackAwareGoal(com.linkedin.kafka.cruisecontrol.analyzer.kafkaassigner.KafkaAssignerEvenRackAwareGoal) Goal(com.linkedin.kafka.cruisecontrol.analyzer.goals.Goal) KafkaAssignerDiskUsageDistributionGoal(com.linkedin.kafka.cruisecontrol.analyzer.kafkaassigner.KafkaAssignerDiskUsageDistributionGoal) StringWriter(java.io.StringWriter) GoalOptimizer(com.linkedin.kafka.cruisecontrol.analyzer.GoalOptimizer) Map(java.util.Map) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) PrintWriter(java.io.PrintWriter)

Example 2 with ClusterModelStats

use of com.linkedin.kafka.cruisecontrol.model.ClusterModelStats in project cruise-control by linkedin.

the class KafkaCruiseControlServlet method addOrRemoveBroker.

private boolean addOrRemoveBroker(HttpServletRequest request, HttpServletResponse response, EndPoint endPoint) throws Exception {
    List<Integer> brokerIds = new ArrayList<>();
    boolean dryrun;
    DataFrom dataFrom;
    boolean throttleAddedOrRemovedBrokers;
    List<String> goals;
    boolean json = wantJSON(request);
    try {
        String[] brokerIdsString = request.getParameter(BROKER_ID_PARAM).split(",");
        for (String brokerIdString : brokerIdsString) {
            brokerIds.add(Integer.parseInt(brokerIdString));
        }
        dryrun = getDryRun(request);
        goals = getGoals(request);
        dataFrom = getDataFrom(request);
        String throttleBrokerString = endPoint == ADD_BROKER ? request.getParameter(THROTTLE_ADDED_BROKER_PARAM) : request.getParameter(THROTTLE_REMOVED_BROKER_PARAM);
        throttleAddedOrRemovedBrokers = throttleBrokerString == null || Boolean.parseBoolean(throttleBrokerString);
    } catch (Exception e) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        setErrorResponse(response, sw.toString(), e.getMessage(), SC_BAD_REQUEST, json);
        // Close session
        return true;
    }
    GoalsAndRequirements goalsAndRequirements = getGoalsAndRequirements(request, response, goals, dataFrom, false);
    if (goalsAndRequirements == null) {
        return false;
    }
    // Get proposals asynchronously.
    GoalOptimizer.OptimizerResult optimizerResult;
    if (endPoint == ADD_BROKER) {
        optimizerResult = getAndMaybeReturnProgress(request, response, () -> _asyncKafkaCruiseControl.addBrokers(brokerIds, dryrun, throttleAddedOrRemovedBrokers, goalsAndRequirements.goals(), goalsAndRequirements.requirements()));
    } else {
        optimizerResult = getAndMaybeReturnProgress(request, response, () -> _asyncKafkaCruiseControl.decommissionBrokers(brokerIds, dryrun, throttleAddedOrRemovedBrokers, goalsAndRequirements.goals(), goalsAndRequirements.requirements()));
    }
    if (optimizerResult == null) {
        return false;
    }
    setResponseCode(response, SC_OK);
    OutputStream out = response.getOutputStream();
    out.write(KafkaCruiseControlServletUtils.getProposalSummary(optimizerResult).getBytes(StandardCharsets.UTF_8));
    for (Map.Entry<Goal, ClusterModelStats> entry : optimizerResult.statsByGoalPriority().entrySet()) {
        Goal goal = entry.getKey();
        out.write(String.format("%n%nStats for goal %s%s:%n", goal.name(), goalResultDescription(goal, optimizerResult)).getBytes(StandardCharsets.UTF_8));
        out.write(entry.getValue().toString().getBytes(StandardCharsets.UTF_8));
    }
    out.write(String.format("%nCluster load after %s broker %s:%n", endPoint == ADD_BROKER ? "adding" : "removing", brokerIds).getBytes(StandardCharsets.UTF_8));
    out.write(optimizerResult.brokerStatsAfterOptimization().toString().getBytes(StandardCharsets.UTF_8));
    out.flush();
    return true;
}
Also used : ClusterModelStats(com.linkedin.kafka.cruisecontrol.model.ClusterModelStats) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) TimeoutException(java.util.concurrent.TimeoutException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) KafkaAssignerEvenRackAwareGoal(com.linkedin.kafka.cruisecontrol.analyzer.kafkaassigner.KafkaAssignerEvenRackAwareGoal) Goal(com.linkedin.kafka.cruisecontrol.analyzer.goals.Goal) KafkaAssignerDiskUsageDistributionGoal(com.linkedin.kafka.cruisecontrol.analyzer.kafkaassigner.KafkaAssignerDiskUsageDistributionGoal) StringWriter(java.io.StringWriter) GoalOptimizer(com.linkedin.kafka.cruisecontrol.analyzer.GoalOptimizer) Map(java.util.Map) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) PrintWriter(java.io.PrintWriter)

Example 3 with ClusterModelStats

use of com.linkedin.kafka.cruisecontrol.model.ClusterModelStats in project cruise-control by linkedin.

the class OptimizationVerifier method verifyRegression.

private static boolean verifyRegression(GoalOptimizer.OptimizerResult optimizerResult, ClusterModelStats preOptimizationStats) {
    // Check whether test has failed for rebalance: fails if rebalance caused a worse goal state after rebalance.
    Map<Goal, ClusterModelStats> clusterStatsByPriority = optimizerResult.statsByGoalPriority();
    ClusterModelStats preStats = preOptimizationStats;
    for (Map.Entry<Goal, ClusterModelStats> entry : clusterStatsByPriority.entrySet()) {
        Goal.ClusterModelStatsComparator comparator = entry.getKey().clusterModelStatsComparator();
        boolean success = comparator.compare(entry.getValue(), preStats) >= 0;
        if (!success) {
            LOG.error("Failed goal comparison " + entry.getKey().name() + ". " + comparator.explainLastComparison());
            return false;
        }
        preStats = entry.getValue();
    }
    return true;
}
Also used : Goal(com.linkedin.kafka.cruisecontrol.analyzer.goals.Goal) ClusterModelStats(com.linkedin.kafka.cruisecontrol.model.ClusterModelStats) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 4 with ClusterModelStats

use of com.linkedin.kafka.cruisecontrol.model.ClusterModelStats in project cruise-control by linkedin.

the class KafkaCruiseControlServlet method getProposals.

private boolean getProposals(HttpServletRequest request, HttpServletResponse response) throws Exception {
    boolean verbose;
    boolean ignoreProposalCache;
    DataFrom dataFrom;
    List<String> goals;
    boolean json = wantJSON(request);
    try {
        String verboseString = request.getParameter(VERBOSE_PARAM);
        verbose = verboseString != null && Boolean.parseBoolean(verboseString);
        goals = getGoals(request);
        dataFrom = getDataFrom(request);
        String ignoreProposalCacheString = request.getParameter(IGNORE_PROPOSAL_CACHE_PARAM);
        ignoreProposalCache = (ignoreProposalCacheString != null && Boolean.parseBoolean(ignoreProposalCacheString)) || !goals.isEmpty();
    } catch (Exception e) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        setErrorResponse(response, sw.toString(), e.getMessage(), SC_BAD_REQUEST, json);
        // Close session
        return true;
    }
    GoalsAndRequirements goalsAndRequirements = getGoalsAndRequirements(request, response, goals, dataFrom, ignoreProposalCache);
    if (goalsAndRequirements == null) {
        return false;
    }
    // Get the optimization result asynchronously.
    GoalOptimizer.OptimizerResult optimizerResult = getAndMaybeReturnProgress(request, response, () -> _asyncKafkaCruiseControl.getOptimizationProposals(goalsAndRequirements.goals(), goalsAndRequirements.requirements()));
    if (optimizerResult == null) {
        return false;
    }
    setResponseCode(response, SC_OK);
    OutputStream out = response.getOutputStream();
    if (!json) {
        String loadBeforeOptimization = optimizerResult.brokerStatsBeforeOptimization().toString();
        String loadAfterOptimization = optimizerResult.brokerStatsAfterOptimization().toString();
        if (!verbose) {
            out.write(KafkaCruiseControlServletUtils.getProposalSummary(optimizerResult).getBytes(StandardCharsets.UTF_8));
        } else {
            out.write(optimizerResult.goalProposals().toString().getBytes(StandardCharsets.UTF_8));
        }
        for (Map.Entry<Goal, ClusterModelStats> entry : optimizerResult.statsByGoalPriority().entrySet()) {
            Goal goal = entry.getKey();
            out.write(String.format("%n%nStats for goal %s%s:%n", goal.name(), goalResultDescription(goal, optimizerResult)).getBytes(StandardCharsets.UTF_8));
            out.write(entry.getValue().toString().getBytes(StandardCharsets.UTF_8));
        }
        setResponseCode(response, SC_OK);
        // Print summary before & after optimization
        out.write(String.format("%n%nCurrent load:").getBytes(StandardCharsets.UTF_8));
        out.write(loadBeforeOptimization.getBytes(StandardCharsets.UTF_8));
        out.write(String.format("%n%nOptimized load:").getBytes(StandardCharsets.UTF_8));
        out.write(loadAfterOptimization.getBytes(StandardCharsets.UTF_8));
    } else {
        if (!verbose) {
            out.write(KafkaCruiseControlServletUtils.getProposalSummary(optimizerResult).getBytes(StandardCharsets.UTF_8));
        }
        // Build all the goal summary
        List<Map<String, Object>> allGoals = new ArrayList<>();
        for (Map.Entry<Goal, ClusterModelStats> entry : optimizerResult.statsByGoalPriority().entrySet()) {
            Goal goal = entry.getKey();
            String goalViolation = goalResultDescription(goal, optimizerResult);
            Map<String, Object> goalMap = new HashMap<>();
            goalMap.put("goal", goal.name());
            goalMap.put("goalViolated", goalViolation);
            goalMap.put("clusterModelStats", entry.getValue().getJsonStructure());
            allGoals.add(goalMap);
        }
        Map<String, Object> proposalMap = new HashMap<>();
        proposalMap.put("version", JSON_VERSION);
        proposalMap.put("goals", allGoals);
        proposalMap.put("loadBeforeOptimization", optimizerResult.brokerStatsBeforeOptimization().getJsonStructure());
        proposalMap.put("loadAfterOptimization", optimizerResult.brokerStatsAfterOptimization().getJsonStructure());
        Gson gson = new GsonBuilder().serializeNulls().serializeSpecialFloatingPointValues().create();
        String proposalsString = gson.toJson(proposalMap);
        setJSONResponseCode(response, SC_OK);
        out.write(proposalsString.getBytes(StandardCharsets.UTF_8));
    }
    out.flush();
    return true;
}
Also used : ClusterModelStats(com.linkedin.kafka.cruisecontrol.model.ClusterModelStats) HashMap(java.util.HashMap) GsonBuilder(com.google.gson.GsonBuilder) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) TimeoutException(java.util.concurrent.TimeoutException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) KafkaAssignerEvenRackAwareGoal(com.linkedin.kafka.cruisecontrol.analyzer.kafkaassigner.KafkaAssignerEvenRackAwareGoal) Goal(com.linkedin.kafka.cruisecontrol.analyzer.goals.Goal) KafkaAssignerDiskUsageDistributionGoal(com.linkedin.kafka.cruisecontrol.analyzer.kafkaassigner.KafkaAssignerDiskUsageDistributionGoal) StringWriter(java.io.StringWriter) GoalOptimizer(com.linkedin.kafka.cruisecontrol.analyzer.GoalOptimizer) Map(java.util.Map) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) PrintWriter(java.io.PrintWriter)

Example 5 with ClusterModelStats

use of com.linkedin.kafka.cruisecontrol.model.ClusterModelStats in project cruise-control by linkedin.

the class OfflineProposalGenerator method main.

public static void main(String[] argv) throws Exception {
    // TODO: probably need to save this in the original model file
    Properties props = KafkaCruiseControlUnitTestUtils.getKafkaCruiseControlProperties();
    KafkaCruiseControlConfig config = new KafkaCruiseControlConfig(props);
    ModelUtils.init(config);
    ModelParameters.init(config);
    BalancingConstraint balancingConstraint = new BalancingConstraint(config);
    long start = System.currentTimeMillis();
    ClusterModel clusterModel = clusterModelFromFile(argv[0]);
    long end = System.currentTimeMillis();
    double duration = (end - start) / 1000.0;
    System.out.println("Model loaded in " + duration + "s.");
    ClusterModelStats origStats = clusterModel.getClusterStats(balancingConstraint);
    String loadBeforeOptimization = clusterModel.brokerStats().toString();
    // Instantiate the components.
    GoalOptimizer goalOptimizer = new GoalOptimizer(config, null, new SystemTime(), new MetricRegistry());
    start = System.currentTimeMillis();
    GoalOptimizer.OptimizerResult optimizerResult = goalOptimizer.optimizations(clusterModel, new OperationProgress());
    end = System.currentTimeMillis();
    duration = (end - start) / 1000.0;
    String loadAfterOptimization = clusterModel.brokerStats().toString();
    System.out.println("Optimize goals in " + duration + "s.");
    System.out.println(optimizerResult.goalProposals().size());
    System.out.println(loadBeforeOptimization);
    System.out.println(loadAfterOptimization);
    ClusterModelStats optimizedStats = clusterModel.getClusterStats(balancingConstraint);
    double[] testStatistics = AnalyzerUtils.testDifference(origStats.utilizationMatrix(), optimizedStats.utilizationMatrix());
    System.out.println(Arrays.stream(RawAndDerivedResource.values()).map(x -> x.toString()).collect(Collectors.joining(", ")));
    System.out.println(Arrays.stream(testStatistics).boxed().map(pValue -> Double.toString(pValue)).collect(Collectors.joining(", ")));
}
Also used : ClusterModelStats(com.linkedin.kafka.cruisecontrol.model.ClusterModelStats) OperationProgress(com.linkedin.kafka.cruisecontrol.async.progress.OperationProgress) MetricRegistry(com.codahale.metrics.MetricRegistry) Properties(java.util.Properties) ClusterModel(com.linkedin.kafka.cruisecontrol.model.ClusterModel) KafkaCruiseControlConfig(com.linkedin.kafka.cruisecontrol.config.KafkaCruiseControlConfig) SystemTime(org.apache.kafka.common.utils.SystemTime)

Aggregations

ClusterModelStats (com.linkedin.kafka.cruisecontrol.model.ClusterModelStats)8 Goal (com.linkedin.kafka.cruisecontrol.analyzer.goals.Goal)6 Map (java.util.Map)6 SortedMap (java.util.SortedMap)6 TreeMap (java.util.TreeMap)6 GoalOptimizer (com.linkedin.kafka.cruisecontrol.analyzer.GoalOptimizer)3 KafkaAssignerDiskUsageDistributionGoal (com.linkedin.kafka.cruisecontrol.analyzer.kafkaassigner.KafkaAssignerDiskUsageDistributionGoal)3 KafkaAssignerEvenRackAwareGoal (com.linkedin.kafka.cruisecontrol.analyzer.kafkaassigner.KafkaAssignerEvenRackAwareGoal)3 IOException (java.io.IOException)3 OutputStream (java.io.OutputStream)3 PrintWriter (java.io.PrintWriter)3 StringWriter (java.io.StringWriter)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ExecutionException (java.util.concurrent.ExecutionException)3 TimeoutException (java.util.concurrent.TimeoutException)3 ServletOutputStream (javax.servlet.ServletOutputStream)3 MetricRegistry (com.codahale.metrics.MetricRegistry)2 OperationProgress (com.linkedin.kafka.cruisecontrol.async.progress.OperationProgress)2