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);
}
}
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();
}
}
Aggregations