Search in sources :

Example 6 with KafkaCruiseControlState

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

the class KafkaCruiseControlServlet method getGoalsAndRequirements.

// package private for testing.
GoalsAndRequirements getGoalsAndRequirements(HttpServletRequest request, HttpServletResponse response, List<String> userProvidedGoals, DataFrom dataFrom, boolean ignoreCache) throws Exception {
    if (!userProvidedGoals.isEmpty() || dataFrom == DataFrom.VALID_PARTITIONS) {
        return new GoalsAndRequirements(userProvidedGoals, getRequirements(dataFrom));
    }
    KafkaCruiseControlState state = getAndMaybeReturnProgress(request, response, _asyncKafkaCruiseControl::state);
    if (state == null) {
        return null;
    }
    int availableWindows = state.monitorState().numValidWindows();
    List<String> allGoals = new ArrayList<>();
    List<String> readyGoals = new ArrayList<>();
    state.analyzerState().readyGoals().forEach((goal, ready) -> {
        allGoals.add(goal.name());
        if (ready) {
            readyGoals.add(goal.name());
        }
    });
    if (allGoals.size() == readyGoals.size()) {
        // If all the goals work, use it.
        return new GoalsAndRequirements(ignoreCache ? allGoals : Collections.emptyList(), null);
    } else if (availableWindows > 0) {
        // If some valid windows are available, use it.
        return new GoalsAndRequirements(ignoreCache ? allGoals : Collections.emptyList(), getRequirements(VALID_WINDOWS));
    } else if (readyGoals.size() > 0) {
        // If no window is valid but some goals are ready, use them.
        return new GoalsAndRequirements(readyGoals, null);
    } else {
        // Ok, use default setting and let it throw exception.
        return new GoalsAndRequirements(Collections.emptyList(), null);
    }
}
Also used : KafkaCruiseControlState(com.linkedin.kafka.cruisecontrol.KafkaCruiseControlState) ArrayList(java.util.ArrayList) EndPoint(com.linkedin.kafka.cruisecontrol.servlet.KafkaCruiseControlServlet.EndPoint)

Example 7 with KafkaCruiseControlState

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

the class AnomalyDetectorTest method testFix.

@Test
public void testFix() throws InterruptedException, KafkaCruiseControlException {
    LinkedBlockingDeque<Anomaly> anomalies = new LinkedBlockingDeque<>();
    AnomalyNotifier mockAnomalyNotifier = EasyMock.mock(AnomalyNotifier.class);
    BrokerFailureDetector mockBrokerFailureDetector = EasyMock.createNiceMock(BrokerFailureDetector.class);
    GoalViolationDetector mockGoalViolationDetector = EasyMock.createNiceMock(GoalViolationDetector.class);
    ScheduledExecutorService mockDetectorScheduler = EasyMock.mock(ScheduledExecutorService.class);
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    KafkaCruiseControl mockKafkaCruiseControl = EasyMock.mock(KafkaCruiseControl.class);
    EasyMock.expect(mockAnomalyNotifier.onGoalViolation(EasyMock.isA(GoalViolations.class))).andReturn(AnomalyNotificationResult.fix());
    // Starting periodic goal violation detection.
    EasyMock.expect(mockDetectorScheduler.scheduleAtFixedRate(EasyMock.eq(mockGoalViolationDetector), EasyMock.anyLong(), EasyMock.eq(3000L), EasyMock.eq(TimeUnit.MILLISECONDS))).andReturn(null);
    // Starting anomaly handler
    EasyMock.expect(mockDetectorScheduler.submit(EasyMock.isA(AnomalyDetector.AnomalyHandlerTask.class))).andDelegateTo(executorService);
    mockDetectorScheduler.shutdown();
    EasyMock.expectLastCall().andDelegateTo(executorService);
    EasyMock.expect(mockDetectorScheduler.awaitTermination(3000L, TimeUnit.MILLISECONDS)).andDelegateTo(executorService);
    EasyMock.expect(mockDetectorScheduler.isTerminated()).andDelegateTo(executorService);
    // The following state are used to test the delayed check when executor is busy.
    EasyMock.expect(mockKafkaCruiseControl.state(EasyMock.anyObject())).andReturn(new KafkaCruiseControlState(ExecutorState.noTaskInProgress(), null, null));
    EasyMock.expect(mockKafkaCruiseControl.rebalance(EasyMock.eq(Collections.emptyList()), EasyMock.eq(false), EasyMock.eq(null), EasyMock.anyObject(OperationProgress.class))).andReturn(null);
    EasyMock.expect(mockKafkaCruiseControl.meetCompletenessRequirements(EasyMock.anyObject())).andReturn(true);
    EasyMock.replay(mockAnomalyNotifier);
    EasyMock.replay(mockBrokerFailureDetector);
    EasyMock.replay(mockGoalViolationDetector);
    EasyMock.replay(mockDetectorScheduler);
    EasyMock.replay(mockKafkaCruiseControl);
    AnomalyDetector anomalyDetector = new AnomalyDetector(anomalies, 3000L, mockKafkaCruiseControl, mockAnomalyNotifier, mockGoalViolationDetector, mockBrokerFailureDetector, mockDetectorScheduler, EasyMock.mock(LoadMonitor.class));
    try {
        anomalyDetector.startDetection();
        anomalies.add(new GoalViolations());
        while (!anomalies.isEmpty()) {
        // Just wait for the anomalies to be drained.
        }
        anomalyDetector.shutdown();
        assertTrue(executorService.awaitTermination(5000, TimeUnit.MILLISECONDS));
        EasyMock.verify(mockAnomalyNotifier, mockDetectorScheduler, mockKafkaCruiseControl);
    } finally {
        executorService.shutdown();
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) KafkaCruiseControlState(com.linkedin.kafka.cruisecontrol.KafkaCruiseControlState) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) AnomalyNotifier(com.linkedin.kafka.cruisecontrol.detector.notifier.AnomalyNotifier) KafkaCruiseControl(com.linkedin.kafka.cruisecontrol.KafkaCruiseControl) OperationProgress(com.linkedin.kafka.cruisecontrol.async.progress.OperationProgress) LoadMonitor(com.linkedin.kafka.cruisecontrol.monitor.LoadMonitor) Test(org.junit.Test)

Aggregations

KafkaCruiseControlState (com.linkedin.kafka.cruisecontrol.KafkaCruiseControlState)7 Test (org.junit.Test)4 KafkaCruiseControl (com.linkedin.kafka.cruisecontrol.KafkaCruiseControl)3 AnomalyNotifier (com.linkedin.kafka.cruisecontrol.detector.notifier.AnomalyNotifier)3 LoadMonitor (com.linkedin.kafka.cruisecontrol.monitor.LoadMonitor)3 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 Goal (com.linkedin.kafka.cruisecontrol.analyzer.goals.Goal)2 ExecutorState (com.linkedin.kafka.cruisecontrol.executor.ExecutorState)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 AnalyzerState (com.linkedin.kafka.cruisecontrol.analyzer.AnalyzerState)1 KafkaAssignerDiskUsageDistributionGoal (com.linkedin.kafka.cruisecontrol.analyzer.kafkaassigner.KafkaAssignerDiskUsageDistributionGoal)1 KafkaAssignerEvenRackAwareGoal (com.linkedin.kafka.cruisecontrol.analyzer.kafkaassigner.KafkaAssignerEvenRackAwareGoal)1 AsyncKafkaCruiseControl (com.linkedin.kafka.cruisecontrol.async.AsyncKafkaCruiseControl)1 OperationFuture (com.linkedin.kafka.cruisecontrol.async.OperationFuture)1 OperationProgress (com.linkedin.kafka.cruisecontrol.async.progress.OperationProgress)1 ExecutionTask (com.linkedin.kafka.cruisecontrol.executor.ExecutionTask)1 LoadMonitorState (com.linkedin.kafka.cruisecontrol.monitor.LoadMonitorState)1