Skip to content
This repository has been archived by the owner on Jun 10, 2021. It is now read-only.

Commit

Permalink
Merge pull request #236 from rndsolutions/refactor-service-result
Browse files Browse the repository at this point in the history
Service Result refactored
  • Loading branch information
VladislavNikolov authored Sep 2, 2016
2 parents 1050e06 + 24a5154 commit a7456e6
Show file tree
Hide file tree
Showing 85 changed files with 1,155 additions and 1,064 deletions.
2 changes: 0 additions & 2 deletions Server/src/main/java/net/hawkengine/core/HawkServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public class HawkServer {
private Thread pipelinePreparer;
private Thread jobAssigner;
private Thread materialTracker;
private EndpointFinder endpointFinder;
private DataImporter dataImporter;

public HawkServer() {
Expand All @@ -32,7 +31,6 @@ public HawkServer() {
this.pipelinePreparer = new Thread(new PipelinePreparer());
this.jobAssigner = new Thread(new JobAssigner());
this.materialTracker = new Thread(new MaterialTracker());
this.endpointFinder = new EndpointFinder();
this.dataImporter = new DataImporter();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void run() {
if (isPipelineUpdated) {
pipeline.setMaterialsUpdated(true);
ServiceResult result = this.pipelineService.update(pipeline);
EndpointConnector.passResultToEndpoint(PipelineService.class.getSimpleName(), "update", result);
// EndpointConnector.passResultToEndpoint(PipelineService.class.getSimpleName(), "update", result);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,40 @@

import net.hawkengine.core.ServerConfiguration;
import net.hawkengine.core.utilities.constants.LoggerMessages;
import net.hawkengine.model.*;
import net.hawkengine.model.enums.StageStatus;
import net.hawkengine.model.Agent;
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.List;

public class JobAssigner implements Runnable {
private IAgentService agentService;
private IPipelineService pipelineService;
private static final Logger LOGGER = Logger.getLogger(PipelinePreparer.class.getName());
private static final int POLL_INTERVAL = ServerConfiguration.getConfiguration().getPipelineSchedulerPollInterval() * 1000;

private JobAssignerService jobAssignerService;
private StatusUpdaterService statusUpdaterService;
private static final Logger LOGGER = Logger.getLogger(PipelinePreparer.class.getName());
private IAgentService agentService;

public JobAssigner() {
this.agentService = new AgentService();
this.pipelineService = new PipelineService();
this.jobAssignerService = new JobAssignerService();
this.statusUpdaterService = new StatusUpdaterService();
this.agentService = new AgentService();
}

@Override
public void run() {
LOGGER.info(String.format(LoggerMessages.WORKER_STARTED, this.getClass().getSimpleName()));
try {
while (true) {
this.statusUpdaterService.updateStatuses();
List<Agent> agents = (List<Agent>) this.agentService.getAllAssignableAgents().getObject();
List<Pipeline> pipelines = (List<Pipeline>) this.pipelineService.getAllPreparedPipelinesInProgress().getObject();
List<Agent> agents = (List<Agent>) this.agentService.getAll().getObject();

for (Pipeline pipeline : pipelines) {
for (Stage stage : pipeline.getStages()) {
if (stage.getStatus() == StageStatus.IN_PROGRESS) {
for (Job job : stage.getJobs()) {
if (agents.size() != 0) {
Agent agent = this.jobAssignerService.assignAgentToJob(job, agents);
if (agent != null) {
ServiceResult result = this.agentService.update(agent);
EndpointConnector.passResultToEndpoint(AgentService.class.getSimpleName(), "update", result);
}
}
}
}
}

ServiceResult result = this.pipelineService.update(pipeline);
EndpointConnector.passResultToEndpoint(PipelineService.class.getSimpleName(), "update", result);
}
this.statusUpdaterService.updateStatuses();
this.jobAssignerService.checkUnassignedJobs(agents);
this.jobAssignerService.checkAwaitingJobs(agents);
this.jobAssignerService.assignJobs(agents);

Thread.sleep(ServerConfiguration.getConfiguration().getMaterialTrackerPollInterval() * 1000);
Thread.sleep(POLL_INTERVAL);
}
} catch (InterruptedException e) {
e.printStackTrace();
Expand Down
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);
}
}
}
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;
}
}
Loading

0 comments on commit a7456e6

Please sign in to comment.