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,
UUID testPlanId) {
synchronized (this.highAvailableAgentPool.getPool()) {
if (!hasEnoughLoad(testPlanContext)) {
logger.info("There is no enough load agent in the pool!");
if (!hasEnoughCurrentLoad(testPlanContext)) {
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;
}
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
.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 org.apache.log4j.Logger;
import org.bench4q.master.api.model.RunningAgentModel;
import org.bench4q.master.api.model.ScriptBriefResultModel;
import org.bench4q.master.api.model.TestScriptConfig;
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.TestPlanScriptService;
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.TaskCompleteCallback;
import org.bench4q.master.testPlan.schedulscript.WarmUpOverTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class TestPlanRunner {
public class TestPlanRunner implements TaskCompleteCallback {
private ScriptService scriptService;
private TestPlanService testPlanService;
private TestPlanScriptService testPlanScriptService;
private LoadDistribute loadBallancer;
private TestPlanContainer testPlanContainer;
private HighAvailablePool haPool;
private static Logger logger = Logger.getLogger(TestPlanRunner.class);
public ScriptService getScriptService() {
@ -75,6 +79,15 @@ public class TestPlanRunner {
this.testPlanScriptService = testPlanScriptService;
}
public HighAvailablePool getHaPool() {
return haPool;
}
@Autowired
public void setHaPool(HighAvailablePool haPool) {
this.haPool = haPool;
}
public UUID runTestPlanWithModel(final TestPlanModel testPlanModel,
User user) {
ExecutorService executorService = Executors.newCachedThreadPool();
@ -133,7 +146,7 @@ public class TestPlanRunner {
* TestPlanService.TIME_UNIT);
ExecutionOverTask executionOverTask = new ExecutionOverTask(
runningScriptModel);
runningScriptModel, this);
timer.schedule(
executionOverTask,
(testScriptConfig.getWarmUp() + testScriptConfig
@ -153,6 +166,32 @@ public class TestPlanRunner {
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) {
RunningScriptModel runningScriptModel = this.testPlanContainer
.queryRunningScriptModel(testPlanId, scriptId);
@ -164,15 +203,7 @@ public class TestPlanRunner {
long planedExecuteTime = this.testPlanService.queryExecuteTime(
testPlanId, scriptId);
result.setPlanedRunningTime(planedExecuteTime);
// if (result.isFinished()
// && this.getTestPlanContainer().verifyTestPlanFinish(testPlanId)) {
// clearUpAndUpdateComplete(testPlanId);
// }
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
public class HighAvailablePool {
private Map<String, Agent> pool;
private AgentService agentService;
private ScriptService scriptService;
private AgentStateService agentStateService;
private TestPlanContainer testPlanContainer;
private Map<String, Agent> pool;
private Map<String, ServerStatusModel> agentStatusOfPreviousBeatMap;
private Map<UUID, AgentRunBlotter> agentRunBlotters;
private long maxAvailableLoad;

View File

@ -1,30 +1,22 @@
package org.bench4q.master.testPlan.schedulscript;
import java.util.TimerTask;
import java.util.UUID;
import org.apache.log4j.Logger;
import org.bench4q.master.api.model.RunningAgentModel;
import org.bench4q.master.api.model.RunningScriptModel;
import org.bench4q.master.communication.agent.StopTestModel;
import org.bench4q.master.helper.ApplicationContextHelper;
import org.bench4q.master.service.AgentService;
import org.bench4q.master.service.TestPlanService;
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;
public class ExecutionOverTask extends TimerTask {
private AgentService agentService;
private RunningScriptModel runningScriptModel;
private TestPlanContainer testPlanContainer;
private TestPlanService testPlanService;
private HighAvailablePool highAvailablePool;
private TaskCompleteCallback taskCompleteCallback;
private Logger logger = Logger.getLogger(ExecutionOverTask.class);
public AgentService getAgentService() {
private AgentService getAgentService() {
return agentService;
}
@ -32,48 +24,29 @@ public class ExecutionOverTask extends TimerTask {
this.agentService = agentService;
}
public RunningScriptModel getRunningScriptModel() {
private RunningScriptModel getRunningScriptModel() {
return runningScriptModel;
}
public void setRunningScriptModel(RunningScriptModel runningScriptModel) {
private void setRunningScriptModel(RunningScriptModel runningScriptModel) {
this.runningScriptModel = runningScriptModel;
}
public TestPlanContainer getTestPlanContainer() {
return testPlanContainer;
private TaskCompleteCallback getTaskCompleteCallback() {
return taskCompleteCallback;
}
public void setTestPlanContainer(TestPlanContainer testPlanContainer) {
this.testPlanContainer = testPlanContainer;
private void setTaskCompleteCallback(
TaskCompleteCallback taskCompleteCallback) {
this.taskCompleteCallback = taskCompleteCallback;
}
public TestPlanService getTestPlanService() {
return testPlanService;
}
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) {
public ExecutionOverTask(RunningScriptModel runningScriptModel,
TaskCompleteCallback compCallback) {
this.setAgentService(ApplicationContextHelper.getContext().getBean(
AgentService.class));
this.setRunningScriptModel(runningScriptModel);
this.setTestPlanService(ApplicationContextHelper.getContext().getBean(
TestPlanService.class));
this.setTestPlanContainer(ApplicationContextHelper.getContext()
.getBean(TestPlanContainer.class));
this.setHighAvailablePool(ApplicationContextHelper.getContext()
.getBean(HighAvailablePool.class));
this.setTaskCompleteCallback(compCallback);
}
@Override
@ -93,7 +66,8 @@ public class ExecutionOverTask extends TimerTask {
backLoadAndCleanUp(runningAgentModel);
}
this.getRunningScriptModel().setFinish(true);
cleanUpTestPlan();
this.getTaskCompleteCallback().doTaskComplete(
this.getRunningScriptModel().getTestPlanID());
System.out.println("execute ExecutionOverTask");
}
@ -106,36 +80,9 @@ public class ExecutionOverTask extends TimerTask {
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) {
String hostName = runningAgentModel.getAgent().getHostName();
this.getAgentService().backLoadToAgent(hostName,
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);
}