SchedulingMachinesFilter { public List<AssignableVirtualMachine> filter(List<AssignableVirtualMachine> machines) { if (!schedulerConfiguration.isSchedulingMachinesFilterEnabled()) { return machines; } Map<String, Task> tasksById = new HashMap<>(); for (Task task : v3JobOperations.getTasks()) { tasksById.put(task.getId(), task); } int maxLaunchingTasksPerMachine = schedulerConfiguration.getMaxLaunchingTasksPerMachine(); List<AssignableVirtualMachine> filteredMachines = new ArrayList<>(); for (AssignableVirtualMachine machine : machines) { VirtualMachineCurrentState vmCurrentState = machine.getVmCurrentState(); Optional<AgentInstance> instanceOpt = SchedulerUtils.findInstance(agentManagementService, schedulerConfiguration.getInstanceAttributeName(), vmCurrentState); if (!instanceOpt.isPresent()) { continue; } AgentInstance instance = instanceOpt.get(); Optional<AgentInstanceGroup> instanceGroupOpt = agentManagementService.findInstanceGroup(instance.getInstanceGroupId()); if (!instanceGroupOpt.isPresent()) { continue; } AgentInstanceGroup instanceGroup = instanceGroupOpt.get(); if (!isInstanceGroupSchedulable(instanceGroup) || !isInstanceSchedulable(instance) || isInstanceLaunchingTooManyTasks(tasksById, vmCurrentState, maxLaunchingTasksPerMachine)) { continue; } filteredMachines.add(machine); } if (schedulerConfiguration.isSchedulingMachinesFilterShuffleEnabled()) { Collections.shuffle(filteredMachines); } return filteredMachines; } @Inject  SchedulingMachinesFilter(SchedulerConfiguration schedulerConfiguration,
                                    AgentManagementService agentManagementService,
                                    AgentStatusMonitor agentStatusMonitor,
                                    V3JobOperations v3JobOperations); List<AssignableVirtualMachine> filter(List<AssignableVirtualMachine> machines);  }
@Test public void instanceGroupNotFound() { AgentInstance instance = createAgentInstance(INSTANCE_ID, ""); when(agentManagementService.findAgentInstance(INSTANCE_ID)).thenReturn(Optional.of(instance)); VirtualMachineCurrentState currentState = createVirtualMachineCurrentStateMock(INSTANCE_ID); AssignableVirtualMachine machine = createAssignableVirtualMachineMock(currentState); List<AssignableVirtualMachine> filteredMachines = machinesFilter.filter(Collections.singletonList(machine)); assertThat(filteredMachines).isEmpty(); }
@Test public void instanceGroupNotActive() { AgentInstance instance = createAgentInstance(INSTANCE_ID, UNKNOWN_INSTANCE_GROUP_ID); when(agentManagementService.findAgentInstance(INSTANCE_ID)).thenReturn(Optional.of(instance)); AgentInstanceGroup agentInstanceGroup = createAgentInstanceGroup(UNKNOWN_INSTANCE_GROUP_ID, InstanceGroupLifecycleState.Inactive, Tier.Flex); when(agentManagementService.findInstanceGroup(INSTANCE_GROUP_ID)).thenReturn(Optional.of(agentInstanceGroup)); VirtualMachineCurrentState currentState = createVirtualMachineCurrentStateMock(INSTANCE_ID); AssignableVirtualMachine machine = createAssignableVirtualMachineMock(currentState); List<AssignableVirtualMachine> filteredMachines = machinesFilter.filter(Collections.singletonList(machine)); assertThat(filteredMachines).isEmpty(); }
@Test public void instanceGroupSystemNoPlacement() { AgentInstance instance = createAgentInstance(INSTANCE_ID, INSTANCE_GROUP_ID); when(agentManagementService.findAgentInstance(INSTANCE_ID)).thenReturn(Optional.of(instance)); AgentInstanceGroup agentInstanceGroup = createAgentInstanceGroup(INSTANCE_GROUP_ID, InstanceGroupLifecycleState.Active, Tier.Flex, Collections.singletonMap(SchedulerAttributes.SYSTEM_NO_PLACEMENT, "true")); when(agentManagementService.findInstanceGroup(INSTANCE_GROUP_ID)).thenReturn(Optional.of(agentInstanceGroup)); VirtualMachineCurrentState currentState = createVirtualMachineCurrentStateMock(INSTANCE_ID); AssignableVirtualMachine machine = createAssignableVirtualMachineMock(currentState); List<AssignableVirtualMachine> filteredMachines = machinesFilter.filter(Collections.singletonList(machine)); assertThat(filteredMachines).isEmpty(); }
@Test public void instanceGroupNoPlacement() { AgentInstance instance = createAgentInstance(INSTANCE_ID, INSTANCE_GROUP_ID); when(agentManagementService.findAgentInstance(INSTANCE_ID)).thenReturn(Optional.of(instance)); AgentInstanceGroup agentInstanceGroup = createAgentInstanceGroup(INSTANCE_GROUP_ID, InstanceGroupLifecycleState.Active, Tier.Flex, Collections.singletonMap(SchedulerAttributes.NO_PLACEMENT, "true")); when(agentManagementService.findInstanceGroup(INSTANCE_GROUP_ID)).thenReturn(Optional.of(agentInstanceGroup)); VirtualMachineCurrentState currentState = createVirtualMachineCurrentStateMock(INSTANCE_ID); AssignableVirtualMachine machine = createAssignableVirtualMachineMock(currentState); List<AssignableVirtualMachine> filteredMachines = machinesFilter.filter(Collections.singletonList(machine)); assertThat(filteredMachines).isEmpty(); }
@Test public void agentHasConcurrentLaunchLimitFromPreviousEvaluation() { Task startedTask = createStartLaunchedTask(TASK_ID); when(jobOperations.getTasks()).thenReturn(Collections.singletonList(startedTask)); TaskRequest taskRequest = createTaskRequest(TASK_ID); VirtualMachineCurrentState currentState = createVirtualMachineCurrentStateMock(INSTANCE_ID, Collections.singletonList(taskRequest), Collections.emptyList()); AssignableVirtualMachine machine = createAssignableVirtualMachineMock(currentState); List<AssignableVirtualMachine> filteredMachines = machinesFilter.filter(Collections.singletonList(machine)); assertThat(filteredMachines).isEmpty(); }