This repository has been archived by the owner on Jun 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #236 from rndsolutions/refactor-service-result
Service Result refactored
- Loading branch information
Showing
85 changed files
with
1,155 additions
and
1,064 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 87 additions & 60 deletions
147
Server/src/main/java/net/hawkengine/core/pipelinescheduler/JobAssignerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,115 @@ | ||
package net.hawkengine.core.pipelinescheduler; | ||
|
||
import net.hawkengine.model.Agent; | ||
import net.hawkengine.model.Job; | ||
import net.hawkengine.model.*; | ||
import net.hawkengine.model.enums.JobStatus; | ||
import net.hawkengine.model.enums.NotificationType; | ||
import net.hawkengine.model.enums.StageStatus; | ||
import net.hawkengine.model.enums.Status; | ||
import net.hawkengine.services.AgentService; | ||
import net.hawkengine.services.PipelineService; | ||
import net.hawkengine.services.interfaces.IAgentService; | ||
import net.hawkengine.services.interfaces.IPipelineService; | ||
import net.hawkengine.ws.EndpointConnector; | ||
import org.apache.log4j.Logger; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class JobAssignerService { | ||
private static final Logger LOGGER = Logger.getLogger(JobAssignerService.class.getName()); | ||
|
||
public Agent assignAgentToJob(Job job, List<Agent> agents) { | ||
Agent result = null; | ||
if (job.getStatus() == JobStatus.SCHEDULED) { | ||
Agent assignedAgent = agents.stream().filter(a -> a.getId().equals(job.getAssignedAgentId())).findFirst().orElse(null); | ||
result = assignedAgent; | ||
boolean isEligible = this.isAgentEligibleForJob(job, assignedAgent); | ||
if (!isEligible) { | ||
job.setStatus(JobStatus.AWAITING); | ||
assignedAgent.setAssigned(false); | ||
result = assignedAgent; | ||
LOGGER.info(String.format("Job %s unassigned from Agent %s", job.getJobDefinitionName(), assignedAgent.getName())); | ||
} | ||
} | ||
|
||
if (job.getStatus() == JobStatus.AWAITING) { | ||
List<Agent> eligibleAgents = this.getEligibleAgentsForJob(job, agents); | ||
Agent agentForJob = this.pickMostSuitableAgent(eligibleAgents); | ||
if (agentForJob != null) { | ||
job.setAssignedAgentId(agentForJob.getId()); | ||
job.setStatus(JobStatus.SCHEDULED); | ||
agentForJob.setAssigned(true); | ||
result = agentForJob; | ||
LOGGER.info(String.format("Job %s assigned to Agent %s", job.getJobDefinitionName(), agentForJob.getName())); | ||
} | ||
} | ||
private IAgentService agentService; | ||
private IPipelineService pipelineService; | ||
private JobAssignerUtilities jobAssignerUtilities; | ||
|
||
return result; | ||
public JobAssignerService() { | ||
this.agentService = new AgentService(); | ||
this.pipelineService = new PipelineService(); | ||
this.jobAssignerUtilities = new JobAssignerUtilities(); | ||
} | ||
|
||
public List<Agent> getEligibleAgentsForJob(Job job, List<Agent> agents) { | ||
List<Agent> eligibleAgents = new ArrayList<>(); | ||
for (Agent agent : agents) { | ||
boolean isEligible = this.isAgentEligibleForJob(job, agent); | ||
public void checkUnassignedJobs(List<Agent> agents) { | ||
List<Agent> filteredAgents = agents.stream().filter(a -> a.isConnected() && a.isEnabled()).collect(Collectors.toList()); | ||
List<Pipeline> pipelinesInProgress = (List<Pipeline>) this.pipelineService.getAllPreparedPipelinesInProgress().getObject(); | ||
for (Pipeline pipeline : pipelinesInProgress) { | ||
boolean isSetToAwaiting = false; | ||
Stage stageInProgress = pipeline.getStages().stream().filter(s -> s.getStatus() == StageStatus.IN_PROGRESS).findFirst().orElse(null); | ||
if (stageInProgress == null) { | ||
continue; | ||
} | ||
|
||
if (isEligible) { | ||
eligibleAgents.add(agent); | ||
for (Job job : stageInProgress.getJobs()) { | ||
if (job.getStatus() == JobStatus.UNASSIGNED) { | ||
boolean hasAssignableAgent = this.jobAssignerUtilities.hasAssignableAgent(job, filteredAgents); | ||
if (!hasAssignableAgent) { | ||
job.setStatus(JobStatus.AWAITING); | ||
isSetToAwaiting = true; | ||
LOGGER.info(String.format("Job %s has no assignable Agents.", job.getJobDefinitionName())); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return eligibleAgents; | ||
if (isSetToAwaiting) { | ||
stageInProgress.setStatus(StageStatus.AWAITING); | ||
pipeline.setStatus(Status.AWAITING); | ||
this.pipelineService.update(pipeline); | ||
String message = String.format("Pipeline %s set to AWAITING.", pipeline.getPipelineDefinitionName()); | ||
LOGGER.info(message); | ||
ServiceResult notification = new ServiceResult(null, NotificationType.WARNING, message); | ||
EndpointConnector.passResultToEndpoint("NotificationService", "sendMessage", notification); | ||
} | ||
} | ||
} | ||
|
||
public Agent pickMostSuitableAgent(List<Agent> agents) { | ||
Agent agentForJob = null; | ||
if (agents.size() == 1) { | ||
agentForJob = agents.get(0); | ||
} else if (agents.size() > 1) { | ||
int numberOfResources = Integer.MAX_VALUE; | ||
for (Agent agent : agents) { | ||
if (agent.getResources().size() < numberOfResources) { | ||
numberOfResources = agent.getResources().size(); | ||
agentForJob = agent; | ||
public void checkAwaitingJobs(List<Agent> agents) { | ||
List<Agent> filteredAgents = agents.stream().filter(a -> a.isConnected() && a.isEnabled()).collect(Collectors.toList()); | ||
List<Pipeline> awaitingPipelines = (List<Pipeline>) this.pipelineService.getAllPreparedAwaitingPipelines().getObject(); | ||
for (Pipeline pipeline : awaitingPipelines) { | ||
Stage awaitingStage = pipeline.getStages().stream().filter(s -> s.getStatus() == StageStatus.AWAITING).findFirst().orElse(null); | ||
if (awaitingStage == null) { | ||
continue; | ||
} | ||
|
||
for (Job job : awaitingStage.getJobs()) { | ||
if (job.getStatus() == JobStatus.AWAITING) { | ||
boolean hasAssignableAgent = this.jobAssignerUtilities.hasAssignableAgent(job, filteredAgents); | ||
if (hasAssignableAgent) { | ||
job.setStatus(JobStatus.UNASSIGNED); | ||
LOGGER.info(String.format("Job %s set back to IN_PROGRESS.", job.getJobDefinitionName())); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return agentForJob; | ||
boolean hasAwaitingJobs = awaitingStage.getJobs().stream().anyMatch(j -> j.getStatus() == JobStatus.AWAITING); | ||
if (!hasAwaitingJobs) { | ||
awaitingStage.setStatus(StageStatus.IN_PROGRESS); | ||
pipeline.setStatus(Status.IN_PROGRESS); | ||
this.pipelineService.update(pipeline); | ||
LOGGER.info(String.format("Pipeline %s set back to IN_PROGRESS.", pipeline.getPipelineDefinitionName())); | ||
} | ||
} | ||
} | ||
|
||
public boolean isAgentEligibleForJob(Job job, Agent agent) { | ||
boolean isEligible = true; | ||
if ((agent == null) || !agent.isConnected() || !agent.isEnabled() || agent.isRunning() || agent.isAssigned()) { | ||
isEligible = false; | ||
} else { | ||
for (String resource : job.getResources()) { | ||
if (!(agent.getResources().contains(resource))) { | ||
isEligible = false; | ||
break; | ||
public void assignJobs(List<Agent> agents) { | ||
List<Agent> filteredAgents = agents.stream().filter(a -> a.isConnected() && a.isEnabled() && !a.isRunning() && !a.isAssigned()).collect(Collectors.toList()); | ||
List<Pipeline> pipelines = (List<Pipeline>) this.pipelineService.getAllPreparedPipelinesInProgress().getObject(); | ||
for (Pipeline pipeline : pipelines) { | ||
for (Stage stage : pipeline.getStages()) { | ||
if (stage.getStatus() == StageStatus.IN_PROGRESS) { | ||
for (Job job : stage.getJobs()) { | ||
if (filteredAgents.size() != 0) { | ||
Agent agent = this.jobAssignerUtilities.assignAgentToJob(job, filteredAgents); | ||
if (agent != null) { | ||
ServiceResult result = this.agentService.update(agent); | ||
EndpointConnector.passResultToEndpoint(AgentService.class.getSimpleName(), "update", result); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return isEligible; | ||
this.pipelineService.update(pipeline); | ||
// EndpointConnector.passResultToEndpoint(PipelineService.class.getSimpleName(), "update", result); | ||
} | ||
} | ||
} |
119 changes: 119 additions & 0 deletions
119
Server/src/main/java/net/hawkengine/core/pipelinescheduler/JobAssignerUtilities.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package net.hawkengine.core.pipelinescheduler; | ||
|
||
import net.hawkengine.model.Agent; | ||
import net.hawkengine.model.Job; | ||
import net.hawkengine.model.enums.JobStatus; | ||
import org.apache.log4j.Logger; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class JobAssignerUtilities { | ||
private static final Logger LOGGER = Logger.getLogger(JobAssignerUtilities.class.getName()); | ||
|
||
public Agent assignAgentToJob(Job job, List<Agent> agents) { | ||
Agent result = null; | ||
if (job.getStatus() == JobStatus.ASSIGNED) { | ||
Agent assignedAgent = agents.stream().filter(a -> a.getId().equals(job.getAssignedAgentId())).findFirst().orElse(null); | ||
result = assignedAgent; | ||
boolean isEligible = this.isAgentEligibleForJob(job, assignedAgent); | ||
if (!isEligible) { | ||
job.setStatus(JobStatus.UNASSIGNED); | ||
assignedAgent.setAssigned(false); | ||
result = assignedAgent; | ||
LOGGER.info(String.format("Job %s unassigned from Agent %s", job.getJobDefinitionName(), assignedAgent.getName())); | ||
} | ||
} | ||
|
||
if (job.getStatus() == JobStatus.UNASSIGNED) { | ||
List<Agent> eligibleAgents = this.getEligibleAgentsForJob(job, agents); | ||
Agent agentForJob = this.pickMostSuitableAgent(eligibleAgents); | ||
if (agentForJob != null) { | ||
job.setAssignedAgentId(agentForJob.getId()); | ||
job.setStatus(JobStatus.ASSIGNED); | ||
agentForJob.setAssigned(true); | ||
result = agentForJob; | ||
LOGGER.info(String.format("Job %s assigned to Agent %s", job.getJobDefinitionName(), agentForJob.getName())); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public List<Agent> getEligibleAgentsForJob(Job job, List<Agent> agents) { | ||
List<Agent> eligibleAgents = new ArrayList<>(); | ||
for (Agent agent : agents) { | ||
boolean isEligible = this.isAgentEligibleForJob(job, agent); | ||
|
||
if (isEligible) { | ||
eligibleAgents.add(agent); | ||
} | ||
} | ||
|
||
return eligibleAgents; | ||
} | ||
|
||
public Agent pickMostSuitableAgent(List<Agent> agents) { | ||
Agent agentForJob = null; | ||
int numberOfResources = Integer.MAX_VALUE; | ||
for (Agent agent : agents) { | ||
if (agent.getResources().size() < numberOfResources) { | ||
numberOfResources = agent.getResources().size(); | ||
agentForJob = agent; | ||
} | ||
} | ||
|
||
return agentForJob; | ||
} | ||
|
||
|
||
// public Agent pickMostSuitableAgent(List<Agent> agents) { | ||
// Agent agentForJob = null; | ||
// if (agents.size() == 1) { | ||
// agentForJob = agents.get(0); | ||
// } else if (agents.size() > 1) { | ||
// int numberOfResources = Integer.MAX_VALUE; | ||
// for (Agent agent : agents) { | ||
// if (agent.getResources().size() < numberOfResources) { | ||
// numberOfResources = agent.getResources().size(); | ||
// agentForJob = agent; | ||
// } | ||
// } | ||
// } | ||
// | ||
// return agentForJob; | ||
// } | ||
|
||
public boolean isAgentEligibleForJob(Job job, Agent agent) { | ||
boolean isEligible = true; | ||
if ((agent == null) || !agent.isConnected() || !agent.isEnabled() || agent.isRunning() || agent.isAssigned()) { | ||
isEligible = false; | ||
} else { | ||
for (String resource : job.getResources()) { | ||
if (!(agent.getResources().contains(resource))) { | ||
isEligible = false; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return isEligible; | ||
} | ||
|
||
public boolean hasAssignableAgent(Job job, List<Agent> agents) { | ||
boolean hasAssignableAgent = true; | ||
for (Agent agent : agents) { | ||
for (String resource : job.getResources()) { | ||
if (!(agent.getResources().contains(resource))) { | ||
hasAssignableAgent = false; | ||
} | ||
} | ||
|
||
if (hasAssignableAgent) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
Oops, something went wrong.