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>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.python</groupId>
<artifactId>jython</artifactId>
<version>2.7-b1</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.python</groupId>
<artifactId>jython</artifactId>
<version>2.7-b1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>

View File

@ -1,26 +1,16 @@
package org.bench4q.master.api;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.bench4q.master.api.model.AgentBriefModel;
import org.bench4q.master.api.model.ScriptIdRequireLoadModel;
import org.bench4q.master.api.model.TestPlanModel;
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.RunScenarioResultModel;
import org.bench4q.master.communication.agent.TestBriefStatusModel;
import org.bench4q.master.service.AgentService;
import org.bench4q.master.service.ScriptService;
import org.bench4q.master.testPlan.TestPlanRunner;
import org.springframework.beans.factory.annotation.Autowired;
@ -34,9 +24,6 @@ import org.springframework.web.bind.annotation.ResponseBody;
@RequestMapping("/testPlan")
public class TestPlanController extends BaseController {
private ScriptService scriptService = new ScriptService();
private AgentService agentPoolService = new AgentService();
private AgentStateService agentStateService = new AgentStateService();
private HttpRequester httpRequester = new HttpRequester();
private TestPlanRunner testPlanRunner;
public ScriptService getScriptService() {
@ -48,33 +35,6 @@ public class TestPlanController extends BaseController {
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() {
return testPlanRunner;
}
@ -89,14 +49,14 @@ public class TestPlanController extends BaseController {
public TestPlanResponseModel runTestPlanWithScriptId(
@RequestParam int scriptId, @RequestParam int requireLoad) {
// TODO:add the scriptId-requireLoad to taskList, and return a UUID
RunScenarioModel runScenarioModel = this.getScriptService()
RunScenarioModel runScenarioModel = this.scriptService
.getRunSceniroModelByScriptId(scriptId);
if (runScenarioModel == null) {
return setTestPlanResponseModel(false,
"RunScenarioModel's content is null", null);
}
try {
List<RunScenarioResultModel> list = this.getTestPlanRunner()
List<RunScenarioResultModel> list = this.testPlanRunner
.runTestWithRunScenarioModel(runScenarioModel, requireLoad);
return setTestPlanResponseModel(true, "success", list);
} catch (JAXBException e) {
@ -120,57 +80,10 @@ public class TestPlanController extends BaseController {
return resultModel;
}
@RequestMapping(value = "/getBriefStatusModelFormAgent", method = RequestMethod.GET)
@RequestMapping(value = "/getBriefStatusModelByRunId", method = RequestMethod.GET)
@ResponseBody
public AgentBriefModel getBriefStatusModelFromAgent(
@RequestParam String hostName, @RequestParam int port,
@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;
public AgentBriefModel getBriefStatusModelFromAgent(@RequestParam UUID runId) {
return this.testPlanRunner.getBriefStatusFromAgent(runId);
}
@RequestMapping(value = "/runTestPlanWithTestPlanModel", method = RequestMethod.GET)

View File

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

View File

@ -6,27 +6,37 @@ import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
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.HttpRequester;
import org.bench4q.master.communication.HttpRequester.HttpResponse;
import org.bench4q.master.communication.agent.RunScenarioModel;
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.service.AgentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.sun.xml.internal.ws.wsdl.writer.document.Port;
@Component
public class TestPlanRunner {
private AgentService agentService;
private AgentStateService agentStateService;
private HttpRequester httpRequester;
private AgentContext agentContext;
private static final int port = 6565;
public AgentService getAgentService() {
return agentService;
@ -55,6 +65,15 @@ public class TestPlanRunner {
this.httpRequester = httpRequester;
}
public AgentContext getAgentContext() {
return agentContext;
}
@Autowired
public void setAgentContext(AgentContext agentContext) {
this.agentContext = agentContext;
}
public List<RunScenarioResultModel> runTestWithRunScenarioModel(
RunScenarioModel runScenarioModel, int requireLoad)
throws JAXBException {
@ -63,15 +82,12 @@ public class TestPlanRunner {
Agent agent;
List<RunScenarioResultModel> resulList = new ArrayList<RunScenarioResultModel>();
RunScenarioResultModel runScenarioResultModel = new RunScenarioResultModel();
int min, remainLoadByStart;
// TODO:i think this should be done by HA and Ballancer, now i just do
// the ballance
// by solo
for (iterator = this.getAgentService().loadAgentPoolFromDB().iterator(); iterator
.hasNext() && requireLoad > 0;) {
if (requireLoad <= 0) {
break;
}
agent = iterator.next();
if (!this.getAgentStateService().askLiving(agent.getHostName(),
@ -83,39 +99,36 @@ public class TestPlanRunner {
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) {
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
resulList.add(runScenarioResultModel);
requireLoad = 0;
break;
} 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;
}
}
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 {
Marshaller marshaller;
marshaller = JAXBContext.newInstance(runScenarioModel.getClass())
@ -156,4 +169,55 @@ public class TestPlanRunner {
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;
}
}