refactor, do the real contact with agent in the testPlanRunner, and next

step i will create the ha
This commit is contained in:
coderfengyun 2013-08-07 23:25:13 +08:00
parent cb8d901946
commit e1ccf55b0b
4 changed files with 111 additions and 130 deletions

11
pom.xml
View File

@ -73,17 +73,16 @@
<artifactId>log4j</artifactId> <artifactId>log4j</artifactId>
<version>1.2.17</version> <version>1.2.17</version>
</dependency> </dependency>
<dependency>
<groupId>org.python</groupId>
<artifactId>jython</artifactId>
<version>2.7-b1</version>
</dependency>
<dependency> <dependency>
<groupId>commons-httpclient</groupId> <groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId> <artifactId>commons-httpclient</artifactId>
<version>3.1</version> <version>3.1</version>
</dependency> </dependency>
<dependency>
<groupId>org.python</groupId>
<artifactId>jython</artifactId>
<version>2.7-b1</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId> <artifactId>spring-test</artifactId>

View File

@ -1,26 +1,16 @@
package org.bench4q.master.api; package org.bench4q.master.api;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.bench4q.master.api.model.AgentBriefModel; import org.bench4q.master.api.model.AgentBriefModel;
import org.bench4q.master.api.model.ScriptIdRequireLoadModel; import org.bench4q.master.api.model.ScriptIdRequireLoadModel;
import org.bench4q.master.api.model.TestPlanModel; import org.bench4q.master.api.model.TestPlanModel;
import org.bench4q.master.api.model.TestPlanResponseModel; import org.bench4q.master.api.model.TestPlanResponseModel;
import org.bench4q.master.communication.AgentStateService;
import org.bench4q.master.communication.HttpRequester;
import org.bench4q.master.communication.HttpRequester.HttpResponse;
import org.bench4q.master.communication.agent.RunScenarioModel; import org.bench4q.master.communication.agent.RunScenarioModel;
import org.bench4q.master.communication.agent.RunScenarioResultModel; import org.bench4q.master.communication.agent.RunScenarioResultModel;
import org.bench4q.master.communication.agent.TestBriefStatusModel;
import org.bench4q.master.service.AgentService;
import org.bench4q.master.service.ScriptService; import org.bench4q.master.service.ScriptService;
import org.bench4q.master.testPlan.TestPlanRunner; import org.bench4q.master.testPlan.TestPlanRunner;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -34,9 +24,6 @@ import org.springframework.web.bind.annotation.ResponseBody;
@RequestMapping("/testPlan") @RequestMapping("/testPlan")
public class TestPlanController extends BaseController { public class TestPlanController extends BaseController {
private ScriptService scriptService = new ScriptService(); private ScriptService scriptService = new ScriptService();
private AgentService agentPoolService = new AgentService();
private AgentStateService agentStateService = new AgentStateService();
private HttpRequester httpRequester = new HttpRequester();
private TestPlanRunner testPlanRunner; private TestPlanRunner testPlanRunner;
public ScriptService getScriptService() { public ScriptService getScriptService() {
@ -48,33 +35,6 @@ public class TestPlanController extends BaseController {
this.scriptService = scriptService; this.scriptService = scriptService;
} }
public AgentService getAgentPoolService() {
return agentPoolService;
}
@Autowired
public void setAgentPoolService(AgentService agentPoolService) {
this.agentPoolService = agentPoolService;
}
public AgentStateService getAgentStateService() {
return agentStateService;
}
@Autowired
public void setAgentStateService(AgentStateService agentStateService) {
this.agentStateService = agentStateService;
}
public HttpRequester getHttpRequester() {
return httpRequester;
}
@Autowired
public void setHttpRequester(HttpRequester httpRequester) {
this.httpRequester = httpRequester;
}
public TestPlanRunner getTestPlanRunner() { public TestPlanRunner getTestPlanRunner() {
return testPlanRunner; return testPlanRunner;
} }
@ -89,14 +49,14 @@ public class TestPlanController extends BaseController {
public TestPlanResponseModel runTestPlanWithScriptId( public TestPlanResponseModel runTestPlanWithScriptId(
@RequestParam int scriptId, @RequestParam int requireLoad) { @RequestParam int scriptId, @RequestParam int requireLoad) {
// TODO:add the scriptId-requireLoad to taskList, and return a UUID // TODO:add the scriptId-requireLoad to taskList, and return a UUID
RunScenarioModel runScenarioModel = this.getScriptService() RunScenarioModel runScenarioModel = this.scriptService
.getRunSceniroModelByScriptId(scriptId); .getRunSceniroModelByScriptId(scriptId);
if (runScenarioModel == null) { if (runScenarioModel == null) {
return setTestPlanResponseModel(false, return setTestPlanResponseModel(false,
"RunScenarioModel's content is null", null); "RunScenarioModel's content is null", null);
} }
try { try {
List<RunScenarioResultModel> list = this.getTestPlanRunner() List<RunScenarioResultModel> list = this.testPlanRunner
.runTestWithRunScenarioModel(runScenarioModel, requireLoad); .runTestWithRunScenarioModel(runScenarioModel, requireLoad);
return setTestPlanResponseModel(true, "success", list); return setTestPlanResponseModel(true, "success", list);
} catch (JAXBException e) { } catch (JAXBException e) {
@ -120,57 +80,10 @@ public class TestPlanController extends BaseController {
return resultModel; return resultModel;
} }
@RequestMapping(value = "/getBriefStatusModelFormAgent", method = RequestMethod.GET) @RequestMapping(value = "/getBriefStatusModelByRunId", method = RequestMethod.GET)
@ResponseBody @ResponseBody
public AgentBriefModel getBriefStatusModelFromAgent( public AgentBriefModel getBriefStatusModelFromAgent(@RequestParam UUID runId) {
@RequestParam String hostName, @RequestParam int port, return this.testPlanRunner.getBriefStatusFromAgent(runId);
@RequestParam UUID runId) {
AgentBriefModel result = new AgentBriefModel();
TestBriefStatusModel briefStatusModel = new TestBriefStatusModel();
try {
HttpResponse httpResponse = httpRequester.sendGet(hostName + ":"
+ port + "/test/brief/" + runId, null, null);
briefStatusModel = extractTestBriefStatusModel(httpResponse
.getContent());
result = setAgentBriefModel(briefStatusModel);
this.giveBackTheLoad(hostName, result);
return result;
} catch (IOException e) {
e.printStackTrace();
return result;
} catch (JAXBException e) {
e.printStackTrace();
return result;
}
}
private void giveBackTheLoad(String hostName, AgentBriefModel result) {
// TODO:judge have i back the load already?
if (result.isFinish()) {
int backLoad = result.getTestStatusModel().getTotalCount() / 2;
this.getAgentPoolService().backLoadToAgent(hostName, backLoad);
}
}
private TestBriefStatusModel extractTestBriefStatusModel(String content)
throws JAXBException {
TestBriefStatusModel resultModel = new TestBriefStatusModel();
Unmarshaller ummarshaller = JAXBContext.newInstance(
resultModel.getClass()).createUnmarshaller();
resultModel = (TestBriefStatusModel) ummarshaller
.unmarshal(new ByteArrayInputStream(content.getBytes()));
return resultModel;
}
private AgentBriefModel setAgentBriefModel(
TestBriefStatusModel briefStatusModel) {
AgentBriefModel result = new AgentBriefModel();
result.setTestStatusModel(briefStatusModel);
result.setFinish(briefStatusModel.getFinishedCount() == briefStatusModel
.getTotalCount());
return result;
} }
@RequestMapping(value = "/runTestPlanWithTestPlanModel", method = RequestMethod.GET) @RequestMapping(value = "/runTestPlanWithTestPlanModel", method = RequestMethod.GET)

View File

@ -1,23 +1,28 @@
package org.bench4q.master.api.model; package org.bench4q.master.api.model;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import org.bench4q.master.communication.agent.RunScenarioResultModel; import org.bench4q.master.communication.agent.RunScenarioResultModel;
@XmlRootElement(name = "scriptID-hostName-runId") @XmlRootElement(name = "scriptID-hostName-runId")
public class TestResponseChildModel { public class TestResponseChildModel {
private RunScenarioResultModel runScenarioResultModel; private List<RunScenarioResultModel> runScenarioResultModels = new ArrayList<RunScenarioResultModel>();
private int ScriptId; private int ScriptId;
@XmlElementWrapper(name = "runId-hostNames")
@XmlElement @XmlElement
public RunScenarioResultModel getRunScenarioResultModel() { public List<RunScenarioResultModel> getRunScenarioResultModel() {
return runScenarioResultModel; return runScenarioResultModels;
} }
public void setRunScenarioResultModel( public void setRunScenarioResultModel(
RunScenarioResultModel runScenarioResultModel) { List<RunScenarioResultModel> runScenarioResultModels) {
this.runScenarioResultModel = runScenarioResultModel; this.runScenarioResultModels = runScenarioResultModels;
} }
@XmlElement @XmlElement

View File

@ -6,27 +6,37 @@ import java.io.StringWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.UUID;
import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller; import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller; import javax.xml.bind.Unmarshaller;
import org.bench4q.master.api.TestPlanController;
import org.bench4q.master.api.model.AgentBriefModel;
import org.bench4q.master.communication.AgentStateService; import org.bench4q.master.communication.AgentStateService;
import org.bench4q.master.communication.HttpRequester; import org.bench4q.master.communication.HttpRequester;
import org.bench4q.master.communication.HttpRequester.HttpResponse; import org.bench4q.master.communication.HttpRequester.HttpResponse;
import org.bench4q.master.communication.agent.RunScenarioModel; import org.bench4q.master.communication.agent.RunScenarioModel;
import org.bench4q.master.communication.agent.RunScenarioResultModel; import org.bench4q.master.communication.agent.RunScenarioResultModel;
import org.bench4q.master.communication.agent.TestBriefStatusModel;
import org.bench4q.master.entity.db.Agent; import org.bench4q.master.entity.db.Agent;
import org.bench4q.master.service.AgentService; import org.bench4q.master.service.AgentService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.sun.xml.internal.ws.wsdl.writer.document.Port;
@Component @Component
public class TestPlanRunner { public class TestPlanRunner {
private AgentService agentService; private AgentService agentService;
private AgentStateService agentStateService; private AgentStateService agentStateService;
private HttpRequester httpRequester; private HttpRequester httpRequester;
private AgentContext agentContext;
private static final int port = 6565;
public AgentService getAgentService() { public AgentService getAgentService() {
return agentService; return agentService;
@ -55,6 +65,15 @@ public class TestPlanRunner {
this.httpRequester = httpRequester; this.httpRequester = httpRequester;
} }
public AgentContext getAgentContext() {
return agentContext;
}
@Autowired
public void setAgentContext(AgentContext agentContext) {
this.agentContext = agentContext;
}
public List<RunScenarioResultModel> runTestWithRunScenarioModel( public List<RunScenarioResultModel> runTestWithRunScenarioModel(
RunScenarioModel runScenarioModel, int requireLoad) RunScenarioModel runScenarioModel, int requireLoad)
throws JAXBException { throws JAXBException {
@ -63,15 +82,12 @@ public class TestPlanRunner {
Agent agent; Agent agent;
List<RunScenarioResultModel> resulList = new ArrayList<RunScenarioResultModel>(); List<RunScenarioResultModel> resulList = new ArrayList<RunScenarioResultModel>();
RunScenarioResultModel runScenarioResultModel = new RunScenarioResultModel(); RunScenarioResultModel runScenarioResultModel = new RunScenarioResultModel();
int min, remainLoadByStart;
// TODO:i think this should be done by HA and Ballancer, now i just do // TODO:i think this should be done by HA and Ballancer, now i just do
// the ballance // the ballance
// by solo // by solo
for (iterator = this.getAgentService().loadAgentPoolFromDB().iterator(); iterator for (iterator = this.getAgentService().loadAgentPoolFromDB().iterator(); iterator
.hasNext() && requireLoad > 0;) { .hasNext() && requireLoad > 0;) {
if (requireLoad <= 0) {
break;
}
agent = iterator.next(); agent = iterator.next();
if (!this.getAgentStateService().askLiving(agent.getHostName(), if (!this.getAgentStateService().askLiving(agent.getHostName(),
@ -83,39 +99,36 @@ public class TestPlanRunner {
continue; continue;
} }
int remainLoadByStart = agent.getRemainLoad(); remainLoadByStart = agent.getRemainLoad();
min = getMin(requireLoad, remainLoadByStart);
runScenarioModel.setPoolSize(min);
runScenarioModel.setTotalCount(min);
this.getAgentService().getLoadFromAgent(agent.getHostName(), min);
runScenarioResultModel = this.sendScriptContentToAgent(
agent.getHostName(), agent.getPort(),
this.makeRunScenarioModelToString(runScenarioModel));
runScenarioResultModel.setHostName(agent.getHostName());
this.agentContext.addToRunIdHostLoadMap(runScenarioResultModel
.getRunId(), new HostNameAndLoad(agent.getHostName(), min));
resulList.add(runScenarioResultModel);
if (remainLoadByStart >= requireLoad) { if (remainLoadByStart >= requireLoad) {
runScenarioModel.setPoolSize(requireLoad);
runScenarioModel.setTotalCount(requireLoad);
this.getAgentService().getLoadFromAgent(agent.getHostName(),
requireLoad);
runScenarioResultModel = this.sendScriptContentToAgent(
agent.getHostName(), agent.getPort(),
this.makeRunScenarioModelString(runScenarioModel));
// TODO:for now, i just think it's stable // TODO:for now, i just think it's stable
resulList.add(runScenarioResultModel); break;
requireLoad = 0;
} else { } else {
runScenarioModel.setPoolSize(agent.getRemainLoad());
runScenarioModel.setPoolSize(agent.getRemainLoad());
this.getAgentService().getLoadFromAgent(agent.getHostName(),
agent.getRemainLoad());
runScenarioResultModel = this.sendScriptContentToAgent(
agent.getHostName(), agent.getPort(),
this.makeRunScenarioModelString(runScenarioModel));
resulList.add(runScenarioResultModel);
requireLoad -= remainLoadByStart; requireLoad -= remainLoadByStart;
} }
} }
return resulList; return resulList;
} }
public String makeRunScenarioModelString(RunScenarioModel runScenarioModel) private int getMin(int requireLoad, int remainLoadByStart) {
return remainLoadByStart >= requireLoad ? requireLoad
: remainLoadByStart;
}
public String makeRunScenarioModelToString(RunScenarioModel runScenarioModel)
throws JAXBException { throws JAXBException {
Marshaller marshaller; Marshaller marshaller;
marshaller = JAXBContext.newInstance(runScenarioModel.getClass()) marshaller = JAXBContext.newInstance(runScenarioModel.getClass())
@ -156,4 +169,55 @@ public class TestPlanRunner {
return resultModel; return resultModel;
} }
public AgentBriefModel getBriefStatusFromAgent(UUID runId) {
AgentBriefModel result = new AgentBriefModel();
TestBriefStatusModel briefStatusModel = new TestBriefStatusModel();
try {
HostNameAndLoad hostNameAndLoad = this.agentContext
.getRunIdAgentMap().get(runId);
if (hostNameAndLoad == null) {
return null;
}
HttpResponse httpResponse = this.httpRequester.sendGet(
hostNameAndLoad.getHostName() + ":" + TestPlanRunner.port
+ "/test/brief/" + runId, null, null);
briefStatusModel = this.extractTestBriefStatusModel(httpResponse
.getContent());
result = this.makeAgentBriefModel(briefStatusModel);
if (result.isFinish()) {
this.agentService.backLoadToAgent(
hostNameAndLoad.getHostName(),
hostNameAndLoad.getLoad());
}
return result;
} catch (IOException e) {
e.printStackTrace();
return result;
} catch (JAXBException e) {
e.printStackTrace();
return result;
}
}
private TestBriefStatusModel extractTestBriefStatusModel(String content)
throws JAXBException {
TestBriefStatusModel resultModel = new TestBriefStatusModel();
Unmarshaller ummarshaller = JAXBContext.newInstance(
resultModel.getClass()).createUnmarshaller();
resultModel = (TestBriefStatusModel) ummarshaller
.unmarshal(new ByteArrayInputStream(content.getBytes()));
return resultModel;
}
private AgentBriefModel makeAgentBriefModel(
TestBriefStatusModel briefStatusModel) {
AgentBriefModel result = new AgentBriefModel();
result.setTestStatusModel(briefStatusModel);
result.setFinish(briefStatusModel.getFinishedCount() == briefStatusModel
.getTotalCount());
return result;
}
} }