make a TaskCompleteCallback, and let the testPlanRunner do the cleanUp

job
This commit is contained in:
Tienan Chen 2013-11-06 09:41:02 +08:00
parent 8629eab282
commit caeccbb6ff
5 changed files with 75 additions and 81 deletions

View File

@ -29,8 +29,12 @@ public class LoadDistribute {
public boolean generateLoadForTestPlan(TestPlanContext testPlanContext, public boolean generateLoadForTestPlan(TestPlanContext testPlanContext,
UUID testPlanId) { UUID testPlanId) {
synchronized (this.highAvailableAgentPool.getPool()) { synchronized (this.highAvailableAgentPool.getPool()) {
if (!hasEnoughLoad(testPlanContext)) { if (!hasEnoughCurrentLoad(testPlanContext)) {
logger.info("There is no enough load agent in the pool!"); logger.info("There is no enough current load in the pool!");
return false;
}
if (!hasEnoughMaxLoad(testPlanContext)) {
logger.error("There is no enough max load in the pool!");
return false; return false;
} }
for (RunningScriptModel scriptInputModel : testPlanContext for (RunningScriptModel scriptInputModel : testPlanContext
@ -49,8 +53,13 @@ public class LoadDistribute {
} }
} }
private boolean hasEnoughLoad(TestPlanContext testPlanContext) { private boolean hasEnoughCurrentLoad(TestPlanContext testPlanContext) {
return this.highAvailableAgentPool.getCurrentAvailableLoad() >= testPlanContext return this.highAvailableAgentPool.getCurrentAvailableLoad() >= testPlanContext
.getTotalLoad(); .getTotalLoad();
} }
private boolean hasEnoughMaxLoad(TestPlanContext testPlanContext) {
return this.getHighAvailableAgentPool().getMaxAvailableLoad() >= testPlanContext
.getTotalLoad();
}
} }

View File

@ -7,6 +7,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.bench4q.master.api.model.RunningAgentModel;
import org.bench4q.master.api.model.ScriptBriefResultModel; import org.bench4q.master.api.model.ScriptBriefResultModel;
import org.bench4q.master.api.model.TestScriptConfig; import org.bench4q.master.api.model.TestScriptConfig;
import org.bench4q.master.api.model.RunningScriptModel; import org.bench4q.master.api.model.RunningScriptModel;
@ -15,18 +16,21 @@ import org.bench4q.master.entity.db.User;
import org.bench4q.master.service.ScriptService; import org.bench4q.master.service.ScriptService;
import org.bench4q.master.service.TestPlanScriptService; import org.bench4q.master.service.TestPlanScriptService;
import org.bench4q.master.service.TestPlanService; import org.bench4q.master.service.TestPlanService;
import org.bench4q.master.testPlan.highavailable.HighAvailablePool;
import org.bench4q.master.testPlan.schedulscript.ExecutionOverTask; import org.bench4q.master.testPlan.schedulscript.ExecutionOverTask;
import org.bench4q.master.testPlan.schedulscript.TaskCompleteCallback;
import org.bench4q.master.testPlan.schedulscript.WarmUpOverTask; import org.bench4q.master.testPlan.schedulscript.WarmUpOverTask;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component @Component
public class TestPlanRunner { public class TestPlanRunner implements TaskCompleteCallback {
private ScriptService scriptService; private ScriptService scriptService;
private TestPlanService testPlanService; private TestPlanService testPlanService;
private TestPlanScriptService testPlanScriptService; private TestPlanScriptService testPlanScriptService;
private LoadDistribute loadBallancer; private LoadDistribute loadBallancer;
private TestPlanContainer testPlanContainer; private TestPlanContainer testPlanContainer;
private HighAvailablePool haPool;
private static Logger logger = Logger.getLogger(TestPlanRunner.class); private static Logger logger = Logger.getLogger(TestPlanRunner.class);
public ScriptService getScriptService() { public ScriptService getScriptService() {
@ -75,6 +79,15 @@ public class TestPlanRunner {
this.testPlanScriptService = testPlanScriptService; this.testPlanScriptService = testPlanScriptService;
} }
public HighAvailablePool getHaPool() {
return haPool;
}
@Autowired
public void setHaPool(HighAvailablePool haPool) {
this.haPool = haPool;
}
public UUID runTestPlanWithModel(final TestPlanModel testPlanModel, public UUID runTestPlanWithModel(final TestPlanModel testPlanModel,
User user) { User user) {
ExecutorService executorService = Executors.newCachedThreadPool(); ExecutorService executorService = Executors.newCachedThreadPool();
@ -133,7 +146,7 @@ public class TestPlanRunner {
* TestPlanService.TIME_UNIT); * TestPlanService.TIME_UNIT);
ExecutionOverTask executionOverTask = new ExecutionOverTask( ExecutionOverTask executionOverTask = new ExecutionOverTask(
runningScriptModel); runningScriptModel, this);
timer.schedule( timer.schedule(
executionOverTask, executionOverTask,
(testScriptConfig.getWarmUp() + testScriptConfig (testScriptConfig.getWarmUp() + testScriptConfig
@ -153,6 +166,32 @@ public class TestPlanRunner {
return true; return true;
} }
public void doTaskComplete(UUID testPlanID) {
// TODO: when taskComplete do something, reallocate the load.
if (this.getTestPlanContainer().verifyTestPlanFinish(testPlanID)) {
TestPlanContext context = this.getTestPlanContainer()
.getRunningTestPlans().get(testPlanID);
for (RunningScriptModel runningScriptModel : context
.getRunningScriptMap().values()) {
for (RunningAgentModel runningAgentModel : runningScriptModel
.getRunningAgents()) {
this.getHaPool().getAgentRunBlotters()
.remove(runningAgentModel.getAgentRunId());
logger.info("see the agentBlotter after run : "
+ this.getHaPool().getAgentRunBlotters()
.get(runningAgentModel.getAgentRunId()));
}
}
this.getTestPlanContainer().getRunningTestPlans()
.remove(testPlanID);
this.getTestPlanService().handleTestPlanComplete(testPlanID);
logger.info("Test plan with id " + testPlanID.toString()
+ " finishes!");
// TODO: verify if the matters about this test plan has been
// cleaned.
}
}
public ScriptBriefResultModel getScriptBrief(UUID testPlanId, int scriptId) { public ScriptBriefResultModel getScriptBrief(UUID testPlanId, int scriptId) {
RunningScriptModel runningScriptModel = this.testPlanContainer RunningScriptModel runningScriptModel = this.testPlanContainer
.queryRunningScriptModel(testPlanId, scriptId); .queryRunningScriptModel(testPlanId, scriptId);
@ -164,15 +203,7 @@ public class TestPlanRunner {
long planedExecuteTime = this.testPlanService.queryExecuteTime( long planedExecuteTime = this.testPlanService.queryExecuteTime(
testPlanId, scriptId); testPlanId, scriptId);
result.setPlanedRunningTime(planedExecuteTime); result.setPlanedRunningTime(planedExecuteTime);
// if (result.isFinished()
// && this.getTestPlanContainer().verifyTestPlanFinish(testPlanId)) {
// clearUpAndUpdateComplete(testPlanId);
// }
return result; return result;
} }
// private void clearUpAndUpdateComplete(UUID testPlanId) {
// this.getTestPlanContainer().getRunningTestPlans().remove(testPlanId);
// this.getTestPlanService().handleTestPlanComplete(testPlanId);
// }
} }

View File

@ -23,11 +23,11 @@ import org.springframework.stereotype.Component;
@Component @Component
public class HighAvailablePool { public class HighAvailablePool {
private Map<String, Agent> pool;
private AgentService agentService; private AgentService agentService;
private ScriptService scriptService; private ScriptService scriptService;
private AgentStateService agentStateService; private AgentStateService agentStateService;
private TestPlanContainer testPlanContainer; private TestPlanContainer testPlanContainer;
private Map<String, Agent> pool;
private Map<String, ServerStatusModel> agentStatusOfPreviousBeatMap; private Map<String, ServerStatusModel> agentStatusOfPreviousBeatMap;
private Map<UUID, AgentRunBlotter> agentRunBlotters; private Map<UUID, AgentRunBlotter> agentRunBlotters;
private long maxAvailableLoad; private long maxAvailableLoad;

View File

@ -1,30 +1,22 @@
package org.bench4q.master.testPlan.schedulscript; package org.bench4q.master.testPlan.schedulscript;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.UUID;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.bench4q.master.api.model.RunningAgentModel; import org.bench4q.master.api.model.RunningAgentModel;
import org.bench4q.master.api.model.RunningScriptModel; import org.bench4q.master.api.model.RunningScriptModel;
import org.bench4q.master.communication.agent.StopTestModel; import org.bench4q.master.communication.agent.StopTestModel;
import org.bench4q.master.helper.ApplicationContextHelper; import org.bench4q.master.helper.ApplicationContextHelper;
import org.bench4q.master.service.AgentService; import org.bench4q.master.service.AgentService;
import org.bench4q.master.service.TestPlanService;
import org.bench4q.master.testPlan.AgentRunBlotter; import org.bench4q.master.testPlan.AgentRunBlotter;
import org.bench4q.master.testPlan.TestPlanContainer;
import org.bench4q.master.testPlan.TestPlanContext;
import org.bench4q.master.testPlan.highavailable.HighAvailablePool;
import org.bench4q.master.testPlan.highavailable.StopAgentFault; import org.bench4q.master.testPlan.highavailable.StopAgentFault;
public class ExecutionOverTask extends TimerTask { public class ExecutionOverTask extends TimerTask {
private AgentService agentService; private AgentService agentService;
private RunningScriptModel runningScriptModel; private RunningScriptModel runningScriptModel;
private TestPlanContainer testPlanContainer; private TaskCompleteCallback taskCompleteCallback;
private TestPlanService testPlanService;
private HighAvailablePool highAvailablePool;
private Logger logger = Logger.getLogger(ExecutionOverTask.class); private Logger logger = Logger.getLogger(ExecutionOverTask.class);
public AgentService getAgentService() { private AgentService getAgentService() {
return agentService; return agentService;
} }
@ -32,48 +24,29 @@ public class ExecutionOverTask extends TimerTask {
this.agentService = agentService; this.agentService = agentService;
} }
public RunningScriptModel getRunningScriptModel() { private RunningScriptModel getRunningScriptModel() {
return runningScriptModel; return runningScriptModel;
} }
public void setRunningScriptModel(RunningScriptModel runningScriptModel) { private void setRunningScriptModel(RunningScriptModel runningScriptModel) {
this.runningScriptModel = runningScriptModel; this.runningScriptModel = runningScriptModel;
} }
public TestPlanContainer getTestPlanContainer() { private TaskCompleteCallback getTaskCompleteCallback() {
return testPlanContainer; return taskCompleteCallback;
} }
public void setTestPlanContainer(TestPlanContainer testPlanContainer) { private void setTaskCompleteCallback(
this.testPlanContainer = testPlanContainer; TaskCompleteCallback taskCompleteCallback) {
this.taskCompleteCallback = taskCompleteCallback;
} }
public TestPlanService getTestPlanService() { public ExecutionOverTask(RunningScriptModel runningScriptModel,
return testPlanService; TaskCompleteCallback compCallback) {
}
public void setTestPlanService(TestPlanService testPlanService) {
this.testPlanService = testPlanService;
}
public HighAvailablePool getHighAvailablePool() {
return highAvailablePool;
}
public void setHighAvailablePool(HighAvailablePool highAvailablePool) {
this.highAvailablePool = highAvailablePool;
}
public ExecutionOverTask(RunningScriptModel runningScriptModel) {
this.setAgentService(ApplicationContextHelper.getContext().getBean( this.setAgentService(ApplicationContextHelper.getContext().getBean(
AgentService.class)); AgentService.class));
this.setRunningScriptModel(runningScriptModel); this.setRunningScriptModel(runningScriptModel);
this.setTestPlanService(ApplicationContextHelper.getContext().getBean( this.setTaskCompleteCallback(compCallback);
TestPlanService.class));
this.setTestPlanContainer(ApplicationContextHelper.getContext()
.getBean(TestPlanContainer.class));
this.setHighAvailablePool(ApplicationContextHelper.getContext()
.getBean(HighAvailablePool.class));
} }
@Override @Override
@ -93,7 +66,8 @@ public class ExecutionOverTask extends TimerTask {
backLoadAndCleanUp(runningAgentModel); backLoadAndCleanUp(runningAgentModel);
} }
this.getRunningScriptModel().setFinish(true); this.getRunningScriptModel().setFinish(true);
cleanUpTestPlan(); this.getTaskCompleteCallback().doTaskComplete(
this.getRunningScriptModel().getTestPlanID());
System.out.println("execute ExecutionOverTask"); System.out.println("execute ExecutionOverTask");
} }
@ -106,36 +80,9 @@ public class ExecutionOverTask extends TimerTask {
stopAgentFault.doTolerance(); stopAgentFault.doTolerance();
} }
private void cleanUpTestPlan() {
UUID testPlanID = this.getRunningScriptModel().getTestPlanID();
if (this.getTestPlanContainer().verifyTestPlanFinish(testPlanID)) {
TestPlanContext context = this.getTestPlanContainer()
.getRunningTestPlans().get(testPlanID);
// TODO: remove the testPlan's other info in HA
for (RunningScriptModel runningScriptModel : context
.getRunningScriptMap().values()) {
for (RunningAgentModel runningAgentModel : runningScriptModel
.getRunningAgents()) {
this.getHighAvailablePool().getAgentRunBlotters()
.remove(runningAgentModel.getAgentRunId());
}
}
this.getTestPlanContainer().getRunningTestPlans()
.remove(testPlanID);
this.getTestPlanService().handleTestPlanComplete(testPlanID);
logger.info("Test plan with id " + testPlanID.toString()
+ " finishes!");
// TODO: verify if the matters about this test plan has been
// cleaned.
}
}
private void backLoadAndCleanUp(RunningAgentModel runningAgentModel) { private void backLoadAndCleanUp(RunningAgentModel runningAgentModel) {
String hostName = runningAgentModel.getAgent().getHostName(); String hostName = runningAgentModel.getAgent().getHostName();
this.getAgentService().backLoadToAgent(hostName, this.getAgentService().backLoadToAgent(hostName,
runningAgentModel.getLoadInUse()); runningAgentModel.getLoadInUse());
this.getHighAvailablePool().getPool().remove(hostName);
this.getHighAvailablePool().getAgentRunBlotters()
.remove(runningAgentModel.getAgentRunId());
} }
} }

View File

@ -0,0 +1,7 @@
package org.bench4q.master.testPlan.schedulscript;
import java.util.UUID;
public interface TaskCompleteCallback {
void doTaskComplete(UUID testPlanID);
}