Search in sources :

Example 6 with Flow2Handler

use of com.sequenceiq.cloudbreak.core.flow2.Flow2Handler in project cloudbreak by hortonworks.

the class HeartbeatServiceTest method testDistributionConcurrencyWithDifferentFlows.

@Test
public void testDistributionConcurrencyWithDifferentFlows() {
    List<CloudbreakNode> clusterNodes = getClusterNodes();
    // myself
    clusterNodes.get(0).setLastUpdated(200_000);
    // failed node
    clusterNodes.get(1).setLastUpdated(50_000);
    // active node
    clusterNodes.get(2).setLastUpdated(200_000);
    when(cloudbreakNodeRepository.findAll()).thenReturn(clusterNodes);
    when(clock.getCurrentTime()).thenReturn(200_000L);
    // all flows that need to be re-distributed
    List<String> suspendedFlows = new ArrayList<>();
    List<FlowLog> node1FlowLogs = getFlowLogs(3, 5000);
    suspendedFlows.addAll(node1FlowLogs.stream().map(FlowLog::getFlowId).distinct().collect(Collectors.toList()));
    when(flowLogRepository.findAllByCloudbreakNodeId(NODE_1_ID)).thenReturn(new HashSet<>(node1FlowLogs));
    Map<CloudbreakNode, List<String>> distribution = new HashMap<>();
    distribution.computeIfAbsent(clusterNodes.get(0), v -> new ArrayList<>()).addAll(Arrays.asList(suspendedFlows.get(0), suspendedFlows.get(2)));
    distribution.computeIfAbsent(clusterNodes.get(2), v -> new ArrayList<>()).addAll(Collections.singletonList(suspendedFlows.get(1)));
    when(flowDistributor.distribute(any(), any())).thenReturn(distribution);
    List<FlowLog> myNewFlowLogs = node1FlowLogs.stream().filter(fl -> fl.getFlowId().equalsIgnoreCase(suspendedFlows.get(0))).collect(Collectors.toList());
    when(flowLogRepository.findAllByCloudbreakNodeId(MY_ID)).thenReturn(new HashSet<>(myNewFlowLogs));
    when(runningFlows.get(any())).thenReturn(null);
    when(flowLogRepository.save(anyCollection())).thenThrow(new OptimisticLockingFailureException("Someone already distributed the flows.."));
    heartbeatService.scheduledFlowDistribution();
    verify(flow2Handler, times(1)).restartFlow(stringCaptor.capture());
    List<String> allFlowIds = stringCaptor.getAllValues();
    assertEquals(1, allFlowIds.size());
    for (FlowLog flowLog : myNewFlowLogs) {
        assertTrue(allFlowIds.contains(flowLog.getFlowId()));
    }
}
Also used : Arrays(java.util.Arrays) StackRepository(com.sequenceiq.cloudbreak.repository.StackRepository) Mock(org.mockito.Mock) Matchers.anyCollection(org.mockito.Matchers.anyCollection) RunWith(org.junit.runner.RunWith) FlowLog(com.sequenceiq.cloudbreak.domain.FlowLog) HashMap(java.util.HashMap) Random(java.util.Random) Supplier(java.util.function.Supplier) Captor(org.mockito.Captor) Matchers.anyString(org.mockito.Matchers.anyString) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Status(com.sequenceiq.cloudbreak.api.model.Status) Flow2Handler(com.sequenceiq.cloudbreak.core.flow2.Flow2Handler) ArgumentCaptor(org.mockito.ArgumentCaptor) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) Map(java.util.Map) StackTerminationFlowConfig(com.sequenceiq.cloudbreak.core.flow2.stack.termination.StackTerminationFlowConfig) Before(org.junit.Before) InjectMocks(org.mockito.InjectMocks) CloudbreakNodeRepository(com.sequenceiq.cloudbreak.repository.CloudbreakNodeRepository) FlowRegister(com.sequenceiq.cloudbreak.core.flow2.FlowRegister) CloudbreakNode(com.sequenceiq.cloudbreak.domain.CloudbreakNode) InMemoryStateStore(com.sequenceiq.cloudbreak.cloud.store.InMemoryStateStore) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) StackCreationFlowConfig(com.sequenceiq.cloudbreak.core.flow2.stack.provision.StackCreationFlowConfig) Collectors(java.util.stream.Collectors) FlowDistributor(com.sequenceiq.cloudbreak.service.ha.FlowDistributor) Mockito.verify(org.mockito.Mockito.verify) Matchers.any(org.mockito.Matchers.any) HeartbeatService(com.sequenceiq.cloudbreak.service.ha.HeartbeatService) List(java.util.List) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) FlowLogRepository(com.sequenceiq.cloudbreak.repository.FlowLogRepository) PollGroup(com.sequenceiq.cloudbreak.cloud.scheduler.PollGroup) Collections(java.util.Collections) CloudbreakNodeConfig(com.sequenceiq.cloudbreak.ha.CloudbreakNodeConfig) Assert.assertEquals(org.junit.Assert.assertEquals) FlowLog(com.sequenceiq.cloudbreak.domain.FlowLog) HashMap(java.util.HashMap) CloudbreakNode(com.sequenceiq.cloudbreak.domain.CloudbreakNode) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

Status (com.sequenceiq.cloudbreak.api.model.Status)6 PollGroup (com.sequenceiq.cloudbreak.cloud.scheduler.PollGroup)6 InMemoryStateStore (com.sequenceiq.cloudbreak.cloud.store.InMemoryStateStore)6 Flow2Handler (com.sequenceiq.cloudbreak.core.flow2.Flow2Handler)6 FlowRegister (com.sequenceiq.cloudbreak.core.flow2.FlowRegister)6 StackCreationFlowConfig (com.sequenceiq.cloudbreak.core.flow2.stack.provision.StackCreationFlowConfig)6 StackTerminationFlowConfig (com.sequenceiq.cloudbreak.core.flow2.stack.termination.StackTerminationFlowConfig)6 CloudbreakNode (com.sequenceiq.cloudbreak.domain.CloudbreakNode)6 FlowLog (com.sequenceiq.cloudbreak.domain.FlowLog)6 CloudbreakNodeConfig (com.sequenceiq.cloudbreak.ha.CloudbreakNodeConfig)6 CloudbreakNodeRepository (com.sequenceiq.cloudbreak.repository.CloudbreakNodeRepository)6 FlowLogRepository (com.sequenceiq.cloudbreak.repository.FlowLogRepository)6 StackRepository (com.sequenceiq.cloudbreak.repository.StackRepository)6 FlowDistributor (com.sequenceiq.cloudbreak.service.ha.FlowDistributor)6 HeartbeatService (com.sequenceiq.cloudbreak.service.ha.HeartbeatService)6 ArrayList (java.util.ArrayList)6 Arrays (java.util.Arrays)6 Collections (java.util.Collections)6 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6