Search in sources :

Example 1 with Flow2Handler

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

the class HeartbeatServiceTest method testOneNodeTakesAllFlowsWithTerminationFlowShouldBeDistributed.

@Test
public void testOneNodeTakesAllFlowsWithTerminationFlowShouldBeDistributed() {
    List<Node> clusterNodes = getClusterNodes();
    // myself
    clusterNodes.get(0).setLastUpdated(200_000L);
    // set all nodes to failed except myself
    for (int i = 1; i < clusterNodes.size(); i++) {
        Node node = clusterNodes.get(i);
        node.setLastUpdated(50_000L);
    }
    when(nodeService.findAll()).thenReturn(clusterNodes);
    when(clock.getCurrentTimeMillis()).thenReturn(200_000L);
    // all flows that need to be re-distributed
    List<FlowLog> node1FlowLogs = getFlowLogs(2, 5000);
    node1FlowLogs.forEach(fl -> fl.setFlowType(ClassValue.of(HelloWorldFlowConfig.class)));
    List<String> suspendedFlows = node1FlowLogs.stream().map(FlowLog::getFlowId).distinct().collect(Collectors.toList());
    when(flowLogService.findAllByCloudbreakNodeId(NODE_1_ID)).thenReturn(new HashSet<>(node1FlowLogs));
    Set<FlowLog> node2FlowLogs = new HashSet<>(getFlowLogs(3, 3000));
    node2FlowLogs.forEach(fl -> fl.setFlowType(ClassValue.of(HelloWorldFlowConfig.class)));
    suspendedFlows.addAll(node2FlowLogs.stream().map(FlowLog::getFlowId).distinct().collect(Collectors.toList()));
    when(flowLogService.findAllByCloudbreakNodeId(NODE_2_ID)).thenReturn(node2FlowLogs);
    Map<Node, List<String>> distribution = new HashMap<>();
    distribution.computeIfAbsent(clusterNodes.get(0), v -> new ArrayList<>()).addAll(Arrays.asList(suspendedFlows.get(0), suspendedFlows.get(1), suspendedFlows.get(2), suspendedFlows.get(3), suspendedFlows.get(4)));
    when(flowDistributor.distribute(any(), any())).thenReturn(distribution);
    Set<FlowLog> myNewFlowLogs = new HashSet<>();
    myNewFlowLogs.addAll(node1FlowLogs);
    myNewFlowLogs.addAll(node2FlowLogs);
    when(flowLogService.findAllByCloudbreakNodeId(MY_ID)).thenReturn(myNewFlowLogs);
    when(runningFlows.get(any())).thenReturn(null);
    List<Long> stackIds = myNewFlowLogs.stream().map(FlowLog::getResourceId).distinct().collect(Collectors.toList());
    when(haApplication.getDeletingResources(anySet())).thenReturn(Set.of(stackIds.get(0), stackIds.get(2)));
    doReturn(Collections.singletonList(HelloWorldFlowConfig.class)).when(applicationFlowInformation).getTerminationFlow();
    heartbeatService.scheduledFlowDistribution();
    verify(flowLogService).saveAll(flowLogListCaptor.capture());
    List<FlowLog> updatedFlows = flowLogListCaptor.getValue();
    assertEquals(myNewFlowLogs.size(), updatedFlows.size());
    for (FlowLog updatedFlow : updatedFlows) {
        assertEquals(MY_ID, updatedFlow.getCloudbreakNodeId());
    }
    verify(flow2Handler, times(5)).restartFlow(stringCaptor.capture());
    List<String> allFlowIds = stringCaptor.getAllValues();
    assertEquals(5L, allFlowIds.size());
    for (String flowId : suspendedFlows) {
        assertTrue(allFlowIds.contains(flowId));
    }
}
Also used : Arrays(java.util.Arrays) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Random(java.util.Random) HaApplication(com.sequenceiq.cloudbreak.service.ha.HaApplication) SecureRandom(java.security.SecureRandom) TransactionService(com.sequenceiq.cloudbreak.common.service.TransactionService) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) InMemoryCleanup(com.sequenceiq.flow.cleanup.InMemoryCleanup) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) ZoneOffset(java.time.ZoneOffset) Mockito.doReturn(org.mockito.Mockito.doReturn) FlowLog(com.sequenceiq.flow.domain.FlowLog) Node(com.sequenceiq.cloudbreak.ha.domain.Node) Collection(java.util.Collection) Set(java.util.Set) UUID(java.util.UUID) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) Collectors(java.util.stream.Collectors) HeartbeatService(com.sequenceiq.cloudbreak.service.ha.HeartbeatService) List(java.util.List) TransactionExecutionException(com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionExecutionException) ClassValue(com.sequenceiq.flow.domain.ClassValue) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) FlowRegister(com.sequenceiq.flow.core.FlowRegister) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) Clock(com.sequenceiq.cloudbreak.common.service.Clock) NodeService(com.sequenceiq.cloudbreak.ha.service.NodeService) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) Mock(org.mockito.Mock) LocalDateTime(java.time.LocalDateTime) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) Captor(org.mockito.Captor) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ArgumentCaptor(org.mockito.ArgumentCaptor) ApplicationFlowInformation(com.sequenceiq.flow.core.ApplicationFlowInformation) OperationType(com.sequenceiq.flow.api.model.operation.OperationType) FlowLogService(com.sequenceiq.flow.core.FlowLogService) StateStatus(com.sequenceiq.flow.domain.StateStatus) Before(org.junit.Before) InjectMocks(org.mockito.InjectMocks) Flow2Handler(com.sequenceiq.flow.core.Flow2Handler) HelloWorldFlowConfig(com.sequenceiq.flow.core.helloworld.config.HelloWorldFlowConfig) InMemoryStateStore(com.sequenceiq.cloudbreak.cloud.store.InMemoryStateStore) Assert.assertTrue(org.junit.Assert.assertTrue) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) ArgumentMatchers.anyCollection(org.mockito.ArgumentMatchers.anyCollection) Assert.assertNull(org.junit.Assert.assertNull) FlowDistributor(com.sequenceiq.cloudbreak.ha.service.FlowDistributor) NodeConfig(com.sequenceiq.flow.ha.NodeConfig) FlowConfiguration(com.sequenceiq.flow.core.config.FlowConfiguration) PollGroup(com.sequenceiq.cloudbreak.cloud.scheduler.PollGroup) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) FlowLog(com.sequenceiq.flow.domain.FlowLog) HashMap(java.util.HashMap) Node(com.sequenceiq.cloudbreak.ha.domain.Node) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) List(java.util.List) ArrayList(java.util.ArrayList) HelloWorldFlowConfig(com.sequenceiq.flow.core.helloworld.config.HelloWorldFlowConfig) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with Flow2Handler

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

the class HeartbeatServiceTest method testDistributionConcurrency.

@Test
public void testDistributionConcurrency() {
    List<Node> clusterNodes = getClusterNodes();
    // myself
    clusterNodes.get(0).setLastUpdated(200_000L);
    // failed node
    clusterNodes.get(1).setLastUpdated(50_000L);
    // active node
    clusterNodes.get(2).setLastUpdated(200_000L);
    when(nodeService.findAll()).thenReturn(clusterNodes);
    when(clock.getCurrentTimeMillis()).thenReturn(200_000L);
    // all flows that need to be re-distributed
    List<FlowLog> node1FlowLogs = getFlowLogs(3, 5000);
    List<String> suspendedFlows = node1FlowLogs.stream().map(FlowLog::getFlowId).distinct().collect(Collectors.toList());
    when(flowLogService.findAllByCloudbreakNodeId(NODE_1_ID)).thenReturn(new HashSet<>(node1FlowLogs));
    Map<Node, 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);
    Set<FlowLog> myNewFlowLogs = new HashSet<>();
    myNewFlowLogs.addAll(node1FlowLogs.stream().filter(fl -> fl.getFlowId().equalsIgnoreCase(suspendedFlows.get(0))).collect(Collectors.toList()));
    myNewFlowLogs.addAll(node1FlowLogs.stream().filter(fl -> fl.getFlowId().equalsIgnoreCase(suspendedFlows.get(2))).collect(Collectors.toList()));
    when(flowLogService.findAllByCloudbreakNodeId(MY_ID)).thenReturn(myNewFlowLogs);
    when(runningFlows.get(any())).thenReturn(null);
    when(flowLogService.saveAll(anyCollection())).thenThrow(new OptimisticLockingFailureException("Someone already distributed the flows.."));
    heartbeatService.scheduledFlowDistribution();
    verify(flow2Handler, times(2)).restartFlow(stringCaptor.capture());
    List<String> allFlowIds = stringCaptor.getAllValues();
    assertEquals(2L, allFlowIds.size());
    for (FlowLog flowLog : myNewFlowLogs) {
        assertTrue(allFlowIds.contains(flowLog.getFlowId()));
    }
}
Also used : Arrays(java.util.Arrays) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Random(java.util.Random) HaApplication(com.sequenceiq.cloudbreak.service.ha.HaApplication) SecureRandom(java.security.SecureRandom) TransactionService(com.sequenceiq.cloudbreak.common.service.TransactionService) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) InMemoryCleanup(com.sequenceiq.flow.cleanup.InMemoryCleanup) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) ZoneOffset(java.time.ZoneOffset) Mockito.doReturn(org.mockito.Mockito.doReturn) FlowLog(com.sequenceiq.flow.domain.FlowLog) Node(com.sequenceiq.cloudbreak.ha.domain.Node) Collection(java.util.Collection) Set(java.util.Set) UUID(java.util.UUID) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) Collectors(java.util.stream.Collectors) HeartbeatService(com.sequenceiq.cloudbreak.service.ha.HeartbeatService) List(java.util.List) TransactionExecutionException(com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionExecutionException) ClassValue(com.sequenceiq.flow.domain.ClassValue) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) FlowRegister(com.sequenceiq.flow.core.FlowRegister) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) Clock(com.sequenceiq.cloudbreak.common.service.Clock) NodeService(com.sequenceiq.cloudbreak.ha.service.NodeService) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) Mock(org.mockito.Mock) LocalDateTime(java.time.LocalDateTime) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) Captor(org.mockito.Captor) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ArgumentCaptor(org.mockito.ArgumentCaptor) ApplicationFlowInformation(com.sequenceiq.flow.core.ApplicationFlowInformation) OperationType(com.sequenceiq.flow.api.model.operation.OperationType) FlowLogService(com.sequenceiq.flow.core.FlowLogService) StateStatus(com.sequenceiq.flow.domain.StateStatus) Before(org.junit.Before) InjectMocks(org.mockito.InjectMocks) Flow2Handler(com.sequenceiq.flow.core.Flow2Handler) HelloWorldFlowConfig(com.sequenceiq.flow.core.helloworld.config.HelloWorldFlowConfig) InMemoryStateStore(com.sequenceiq.cloudbreak.cloud.store.InMemoryStateStore) Assert.assertTrue(org.junit.Assert.assertTrue) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) ArgumentMatchers.anyCollection(org.mockito.ArgumentMatchers.anyCollection) Assert.assertNull(org.junit.Assert.assertNull) FlowDistributor(com.sequenceiq.cloudbreak.ha.service.FlowDistributor) NodeConfig(com.sequenceiq.flow.ha.NodeConfig) FlowConfiguration(com.sequenceiq.flow.core.config.FlowConfiguration) PollGroup(com.sequenceiq.cloudbreak.cloud.scheduler.PollGroup) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) FlowLog(com.sequenceiq.flow.domain.FlowLog) HashMap(java.util.HashMap) Node(com.sequenceiq.cloudbreak.ha.domain.Node) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with Flow2Handler

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

the class HeartbeatServiceTest method testOneNodeTakesAllFlowsWithInvalidFlows.

@Test
public void testOneNodeTakesAllFlowsWithInvalidFlows() {
    List<Node> clusterNodes = getClusterNodes();
    // myself
    clusterNodes.get(0).setLastUpdated(200_000L);
    // set all nodes to failed except myself
    for (int i = 1; i < clusterNodes.size(); i++) {
        Node node = clusterNodes.get(i);
        node.setLastUpdated(50_000L);
    }
    when(nodeService.findAll()).thenReturn(clusterNodes);
    when(clock.getCurrentTimeMillis()).thenReturn(200_000L);
    // all flows that need to be re-distributed
    List<FlowLog> node1FlowLogs = getFlowLogs(2, 5000);
    List<String> suspendedFlows = node1FlowLogs.stream().map(FlowLog::getFlowId).distinct().collect(Collectors.toList());
    when(flowLogService.findAllByCloudbreakNodeId(NODE_1_ID)).thenReturn(new HashSet<>(node1FlowLogs));
    Set<FlowLog> node2FlowLogs = new HashSet<>(getFlowLogs(3, 3000));
    suspendedFlows.addAll(node2FlowLogs.stream().map(FlowLog::getFlowId).distinct().collect(Collectors.toList()));
    when(flowLogService.findAllByCloudbreakNodeId(NODE_2_ID)).thenReturn(node2FlowLogs);
    Map<Node, List<String>> distribution = new HashMap<>();
    distribution.computeIfAbsent(clusterNodes.get(0), v -> new ArrayList<>()).addAll(Arrays.asList(suspendedFlows.get(0), suspendedFlows.get(1), suspendedFlows.get(2), suspendedFlows.get(3), suspendedFlows.get(4)));
    when(flowDistributor.distribute(any(), any())).thenReturn(distribution);
    Set<FlowLog> myNewFlowLogs = new HashSet<>();
    myNewFlowLogs.addAll(node1FlowLogs);
    myNewFlowLogs.addAll(node2FlowLogs);
    when(flowLogService.findAllByCloudbreakNodeId(MY_ID)).thenReturn(myNewFlowLogs);
    when(runningFlows.get(any())).thenReturn(null);
    List<Long> stackIds = myNewFlowLogs.stream().map(FlowLog::getResourceId).distinct().collect(Collectors.toList());
    when(haApplication.getDeletingResources(anySet())).thenReturn(Set.of(stackIds.get(0), stackIds.get(2)));
    doReturn(Collections.singletonList(HelloWorldFlowConfig.class)).when(applicationFlowInformation).getTerminationFlow();
    List<FlowLog> invalidFlowLogs = myNewFlowLogs.stream().filter(fl -> fl.getResourceId().equals(stackIds.get(0)) || fl.getResourceId().equals(stackIds.get(2))).collect(Collectors.toList());
    heartbeatService.scheduledFlowDistribution();
    verify(flowLogService).saveAll(flowLogListCaptor.capture());
    List<FlowLog> updatedFlows = flowLogListCaptor.getValue();
    assertEquals(myNewFlowLogs.size(), updatedFlows.size());
    for (FlowLog updatedFlow : updatedFlows) {
        if (invalidFlowLogs.contains(updatedFlow)) {
            assertEquals(StateStatus.SUCCESSFUL, updatedFlow.getStateStatus());
            assertNull(updatedFlow.getCloudbreakNodeId());
        } else {
            assertEquals(MY_ID, updatedFlow.getCloudbreakNodeId());
        }
    }
    verify(flow2Handler, times(5)).restartFlow(stringCaptor.capture());
    List<String> allFlowIds = stringCaptor.getAllValues();
    assertEquals(5L, allFlowIds.size());
    for (String flowId : suspendedFlows) {
        assertTrue(allFlowIds.contains(flowId));
    }
}
Also used : Arrays(java.util.Arrays) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Random(java.util.Random) HaApplication(com.sequenceiq.cloudbreak.service.ha.HaApplication) SecureRandom(java.security.SecureRandom) TransactionService(com.sequenceiq.cloudbreak.common.service.TransactionService) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) InMemoryCleanup(com.sequenceiq.flow.cleanup.InMemoryCleanup) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) ZoneOffset(java.time.ZoneOffset) Mockito.doReturn(org.mockito.Mockito.doReturn) FlowLog(com.sequenceiq.flow.domain.FlowLog) Node(com.sequenceiq.cloudbreak.ha.domain.Node) Collection(java.util.Collection) Set(java.util.Set) UUID(java.util.UUID) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) Collectors(java.util.stream.Collectors) HeartbeatService(com.sequenceiq.cloudbreak.service.ha.HeartbeatService) List(java.util.List) TransactionExecutionException(com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionExecutionException) ClassValue(com.sequenceiq.flow.domain.ClassValue) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) FlowRegister(com.sequenceiq.flow.core.FlowRegister) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) Clock(com.sequenceiq.cloudbreak.common.service.Clock) NodeService(com.sequenceiq.cloudbreak.ha.service.NodeService) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) Mock(org.mockito.Mock) LocalDateTime(java.time.LocalDateTime) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) Captor(org.mockito.Captor) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ArgumentCaptor(org.mockito.ArgumentCaptor) ApplicationFlowInformation(com.sequenceiq.flow.core.ApplicationFlowInformation) OperationType(com.sequenceiq.flow.api.model.operation.OperationType) FlowLogService(com.sequenceiq.flow.core.FlowLogService) StateStatus(com.sequenceiq.flow.domain.StateStatus) Before(org.junit.Before) InjectMocks(org.mockito.InjectMocks) Flow2Handler(com.sequenceiq.flow.core.Flow2Handler) HelloWorldFlowConfig(com.sequenceiq.flow.core.helloworld.config.HelloWorldFlowConfig) InMemoryStateStore(com.sequenceiq.cloudbreak.cloud.store.InMemoryStateStore) Assert.assertTrue(org.junit.Assert.assertTrue) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) ArgumentMatchers.anyCollection(org.mockito.ArgumentMatchers.anyCollection) Assert.assertNull(org.junit.Assert.assertNull) FlowDistributor(com.sequenceiq.cloudbreak.ha.service.FlowDistributor) NodeConfig(com.sequenceiq.flow.ha.NodeConfig) FlowConfiguration(com.sequenceiq.flow.core.config.FlowConfiguration) PollGroup(com.sequenceiq.cloudbreak.cloud.scheduler.PollGroup) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) FlowLog(com.sequenceiq.flow.domain.FlowLog) HashMap(java.util.HashMap) Node(com.sequenceiq.cloudbreak.ha.domain.Node) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) List(java.util.List) ArrayList(java.util.ArrayList) HelloWorldFlowConfig(com.sequenceiq.flow.core.helloworld.config.HelloWorldFlowConfig) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with Flow2Handler

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

the class HeartbeatServiceTest method testDistributionConcurrencyWithDifferentFlows.

@Test
public void testDistributionConcurrencyWithDifferentFlows() {
    List<Node> clusterNodes = getClusterNodes();
    // myself
    clusterNodes.get(0).setLastUpdated(200_000L);
    // failed node
    clusterNodes.get(1).setLastUpdated(50_000L);
    // active node
    clusterNodes.get(2).setLastUpdated(200_000L);
    when(nodeService.findAll()).thenReturn(clusterNodes);
    when(clock.getCurrentTimeMillis()).thenReturn(200_000L);
    // all flows that need to be re-distributed
    List<FlowLog> node1FlowLogs = getFlowLogs(3, 5000);
    List<String> suspendedFlows = node1FlowLogs.stream().map(FlowLog::getFlowId).distinct().collect(Collectors.toList());
    when(flowLogService.findAllByCloudbreakNodeId(NODE_1_ID)).thenReturn(new HashSet<>(node1FlowLogs));
    Map<Node, 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(flowLogService.findAllByCloudbreakNodeId(MY_ID)).thenReturn(new HashSet<>(myNewFlowLogs));
    when(runningFlows.get(any())).thenReturn(null);
    when(flowLogService.saveAll(anyCollection())).thenThrow(new OptimisticLockingFailureException("Someone already distributed the flows.."));
    heartbeatService.scheduledFlowDistribution();
    verify(flow2Handler, times(1)).restartFlow(stringCaptor.capture());
    List<String> allFlowIds = stringCaptor.getAllValues();
    assertEquals(1L, allFlowIds.size());
    for (FlowLog flowLog : myNewFlowLogs) {
        assertTrue(allFlowIds.contains(flowLog.getFlowId()));
    }
}
Also used : Arrays(java.util.Arrays) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Random(java.util.Random) HaApplication(com.sequenceiq.cloudbreak.service.ha.HaApplication) SecureRandom(java.security.SecureRandom) TransactionService(com.sequenceiq.cloudbreak.common.service.TransactionService) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) InMemoryCleanup(com.sequenceiq.flow.cleanup.InMemoryCleanup) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) ZoneOffset(java.time.ZoneOffset) Mockito.doReturn(org.mockito.Mockito.doReturn) FlowLog(com.sequenceiq.flow.domain.FlowLog) Node(com.sequenceiq.cloudbreak.ha.domain.Node) Collection(java.util.Collection) Set(java.util.Set) UUID(java.util.UUID) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) Collectors(java.util.stream.Collectors) HeartbeatService(com.sequenceiq.cloudbreak.service.ha.HeartbeatService) List(java.util.List) TransactionExecutionException(com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionExecutionException) ClassValue(com.sequenceiq.flow.domain.ClassValue) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) FlowRegister(com.sequenceiq.flow.core.FlowRegister) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) Clock(com.sequenceiq.cloudbreak.common.service.Clock) NodeService(com.sequenceiq.cloudbreak.ha.service.NodeService) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) Mock(org.mockito.Mock) LocalDateTime(java.time.LocalDateTime) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) Captor(org.mockito.Captor) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ArgumentCaptor(org.mockito.ArgumentCaptor) ApplicationFlowInformation(com.sequenceiq.flow.core.ApplicationFlowInformation) OperationType(com.sequenceiq.flow.api.model.operation.OperationType) FlowLogService(com.sequenceiq.flow.core.FlowLogService) StateStatus(com.sequenceiq.flow.domain.StateStatus) Before(org.junit.Before) InjectMocks(org.mockito.InjectMocks) Flow2Handler(com.sequenceiq.flow.core.Flow2Handler) HelloWorldFlowConfig(com.sequenceiq.flow.core.helloworld.config.HelloWorldFlowConfig) InMemoryStateStore(com.sequenceiq.cloudbreak.cloud.store.InMemoryStateStore) Assert.assertTrue(org.junit.Assert.assertTrue) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) ArgumentMatchers.anyCollection(org.mockito.ArgumentMatchers.anyCollection) Assert.assertNull(org.junit.Assert.assertNull) FlowDistributor(com.sequenceiq.cloudbreak.ha.service.FlowDistributor) NodeConfig(com.sequenceiq.flow.ha.NodeConfig) FlowConfiguration(com.sequenceiq.flow.core.config.FlowConfiguration) PollGroup(com.sequenceiq.cloudbreak.cloud.scheduler.PollGroup) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) FlowLog(com.sequenceiq.flow.domain.FlowLog) HashMap(java.util.HashMap) Node(com.sequenceiq.cloudbreak.ha.domain.Node) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 5 with Flow2Handler

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

the class HeartbeatServiceTest method testOneNodeTakesAllFlows.

@Test
public void testOneNodeTakesAllFlows() {
    List<Node> clusterNodes = getClusterNodes();
    // myself
    clusterNodes.get(0).setLastUpdated(200_000L);
    // set all nodes to failed except myself
    for (int i = 1; i < clusterNodes.size(); i++) {
        Node node = clusterNodes.get(i);
        node.setLastUpdated(50_000L);
    }
    when(nodeService.findAll()).thenReturn(clusterNodes);
    when(clock.getCurrentTimeMillis()).thenReturn(200_000L);
    // all flows that need to be re-distributed
    List<FlowLog> node1FlowLogs = getFlowLogs(2, 5000);
    List<String> suspendedFlows = node1FlowLogs.stream().map(FlowLog::getFlowId).distinct().collect(Collectors.toList());
    when(flowLogService.findAllByCloudbreakNodeId(NODE_1_ID)).thenReturn(new HashSet<>(node1FlowLogs));
    Set<FlowLog> node2FlowLogs = new HashSet<>(getFlowLogs(3, 3000));
    suspendedFlows.addAll(node2FlowLogs.stream().map(FlowLog::getFlowId).distinct().collect(Collectors.toList()));
    when(flowLogService.findAllByCloudbreakNodeId(NODE_2_ID)).thenReturn(node2FlowLogs);
    Map<Node, List<String>> distribution = new HashMap<>();
    distribution.computeIfAbsent(clusterNodes.get(0), v -> new ArrayList<>()).addAll(Arrays.asList(suspendedFlows.get(0), suspendedFlows.get(1), suspendedFlows.get(2), suspendedFlows.get(3), suspendedFlows.get(4)));
    when(flowDistributor.distribute(any(), any())).thenReturn(distribution);
    Set<FlowLog> myNewFlowLogs = new HashSet<>();
    myNewFlowLogs.addAll(node1FlowLogs);
    myNewFlowLogs.addAll(node2FlowLogs);
    when(flowLogService.findAllByCloudbreakNodeId(MY_ID)).thenReturn(myNewFlowLogs);
    when(runningFlows.get(any())).thenReturn(null);
    heartbeatService.scheduledFlowDistribution();
    verify(flowLogService).saveAll(flowLogListCaptor.capture());
    List<FlowLog> updatedFlows = flowLogListCaptor.getValue();
    assertEquals(myNewFlowLogs.size(), updatedFlows.size());
    for (FlowLog updatedFlow : updatedFlows) {
        assertEquals(MY_ID, updatedFlow.getCloudbreakNodeId());
    }
    verify(flow2Handler, times(5)).restartFlow(stringCaptor.capture());
    List<String> allFlowIds = stringCaptor.getAllValues();
    assertEquals(5L, allFlowIds.size());
    for (String flowId : suspendedFlows) {
        assertTrue(allFlowIds.contains(flowId));
    }
}
Also used : Arrays(java.util.Arrays) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Random(java.util.Random) HaApplication(com.sequenceiq.cloudbreak.service.ha.HaApplication) SecureRandom(java.security.SecureRandom) TransactionService(com.sequenceiq.cloudbreak.common.service.TransactionService) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) InMemoryCleanup(com.sequenceiq.flow.cleanup.InMemoryCleanup) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) ZoneOffset(java.time.ZoneOffset) Mockito.doReturn(org.mockito.Mockito.doReturn) FlowLog(com.sequenceiq.flow.domain.FlowLog) Node(com.sequenceiq.cloudbreak.ha.domain.Node) Collection(java.util.Collection) Set(java.util.Set) UUID(java.util.UUID) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) Collectors(java.util.stream.Collectors) HeartbeatService(com.sequenceiq.cloudbreak.service.ha.HeartbeatService) List(java.util.List) TransactionExecutionException(com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionExecutionException) ClassValue(com.sequenceiq.flow.domain.ClassValue) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) FlowRegister(com.sequenceiq.flow.core.FlowRegister) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) Clock(com.sequenceiq.cloudbreak.common.service.Clock) NodeService(com.sequenceiq.cloudbreak.ha.service.NodeService) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) Mock(org.mockito.Mock) LocalDateTime(java.time.LocalDateTime) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) Captor(org.mockito.Captor) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ArgumentCaptor(org.mockito.ArgumentCaptor) ApplicationFlowInformation(com.sequenceiq.flow.core.ApplicationFlowInformation) OperationType(com.sequenceiq.flow.api.model.operation.OperationType) FlowLogService(com.sequenceiq.flow.core.FlowLogService) StateStatus(com.sequenceiq.flow.domain.StateStatus) Before(org.junit.Before) InjectMocks(org.mockito.InjectMocks) Flow2Handler(com.sequenceiq.flow.core.Flow2Handler) HelloWorldFlowConfig(com.sequenceiq.flow.core.helloworld.config.HelloWorldFlowConfig) InMemoryStateStore(com.sequenceiq.cloudbreak.cloud.store.InMemoryStateStore) Assert.assertTrue(org.junit.Assert.assertTrue) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) ArgumentMatchers.anyCollection(org.mockito.ArgumentMatchers.anyCollection) Assert.assertNull(org.junit.Assert.assertNull) FlowDistributor(com.sequenceiq.cloudbreak.ha.service.FlowDistributor) NodeConfig(com.sequenceiq.flow.ha.NodeConfig) FlowConfiguration(com.sequenceiq.flow.core.config.FlowConfiguration) PollGroup(com.sequenceiq.cloudbreak.cloud.scheduler.PollGroup) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) FlowLog(com.sequenceiq.flow.domain.FlowLog) HashMap(java.util.HashMap) Node(com.sequenceiq.cloudbreak.ha.domain.Node) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

PollGroup (com.sequenceiq.cloudbreak.cloud.scheduler.PollGroup)6 InMemoryStateStore (com.sequenceiq.cloudbreak.cloud.store.InMemoryStateStore)6 Clock (com.sequenceiq.cloudbreak.common.service.Clock)6 TransactionService (com.sequenceiq.cloudbreak.common.service.TransactionService)6 TransactionExecutionException (com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionExecutionException)6 Node (com.sequenceiq.cloudbreak.ha.domain.Node)6 FlowDistributor (com.sequenceiq.cloudbreak.ha.service.FlowDistributor)6 NodeService (com.sequenceiq.cloudbreak.ha.service.NodeService)6 HaApplication (com.sequenceiq.cloudbreak.service.ha.HaApplication)6 HeartbeatService (com.sequenceiq.cloudbreak.service.ha.HeartbeatService)6 OperationType (com.sequenceiq.flow.api.model.operation.OperationType)6 InMemoryCleanup (com.sequenceiq.flow.cleanup.InMemoryCleanup)6 ApplicationFlowInformation (com.sequenceiq.flow.core.ApplicationFlowInformation)6 Flow2Handler (com.sequenceiq.flow.core.Flow2Handler)6 FlowLogService (com.sequenceiq.flow.core.FlowLogService)6 FlowRegister (com.sequenceiq.flow.core.FlowRegister)6 FlowConfiguration (com.sequenceiq.flow.core.config.FlowConfiguration)6 HelloWorldFlowConfig (com.sequenceiq.flow.core.helloworld.config.HelloWorldFlowConfig)6 ClassValue (com.sequenceiq.flow.domain.ClassValue)6 FlowLog (com.sequenceiq.flow.domain.FlowLog)6