refactoring and adding HA and balancer, and i will do it as a different

way and add the task to TaskList, then i will use the balancer to do the
balancing, and HA will really contact with the agent.
This commit is contained in:
coderfengyun 2013-07-26 17:31:54 +08:00
parent a18219bc98
commit cb8d901946
8 changed files with 285 additions and 111 deletions

View File

@ -2,18 +2,17 @@ package org.bench4q.master.api;
import java.io.ByteArrayInputStream;
import java.io.IOException;
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.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;
@ -21,9 +20,9 @@ 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.bench4q.master.service.ScriptService;
import org.bench4q.master.testPlan.TestPlanRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@ -38,6 +37,7 @@ public class TestPlanController extends BaseController {
private AgentService agentPoolService = new AgentService();
private AgentStateService agentStateService = new AgentStateService();
private HttpRequester httpRequester = new HttpRequester();
private TestPlanRunner testPlanRunner;
public ScriptService getScriptService() {
return scriptService;
@ -75,11 +75,20 @@ public class TestPlanController extends BaseController {
this.httpRequester = httpRequester;
}
public TestPlanRunner getTestPlanRunner() {
return testPlanRunner;
}
@Autowired
public void setTestPlanRunner(TestPlanRunner testPlanRunner) {
this.testPlanRunner = testPlanRunner;
}
@RequestMapping(value = "/runTestPlanWithScriptId", method = RequestMethod.GET)
@ResponseBody
public TestPlanResponseModel runTestPlanWithScriptId(
@RequestParam int scriptId, @RequestParam int requireLoad) {
// TODO:let it can give me a script list
// TODO:add the scriptId-requireLoad to taskList, and return a UUID
RunScenarioModel runScenarioModel = this.getScriptService()
.getRunSceniroModelByScriptId(scriptId);
if (runScenarioModel == null) {
@ -87,7 +96,7 @@ public class TestPlanController extends BaseController {
"RunScenarioModel's content is null", null);
}
try {
List<RunScenarioResultModel> list = this
List<RunScenarioResultModel> list = this.getTestPlanRunner()
.runTestWithRunScenarioModel(runScenarioModel, requireLoad);
return setTestPlanResponseModel(true, "success", list);
} catch (JAXBException e) {
@ -102,107 +111,6 @@ public class TestPlanController extends BaseController {
}
public List<RunScenarioResultModel> runTestWithRunScenarioModel(
RunScenarioModel runScenarioModel, int requireLoad)
throws JAXBException {
// TODO: is it needed to be synchronized, but i think it is;
Iterator<Agent> iterator;
Agent agent;
List<RunScenarioResultModel> resulList = new ArrayList<RunScenarioResultModel>();
RunScenarioResultModel runScenarioResultModel = new RunScenarioResultModel();
// TODO:i think this should be done by HA and Ballancer, now i just do
// the ballance
// by solo
for (iterator = this.getAgentPoolService().loadAgentPoolFromDB()
.iterator(); iterator.hasNext() && requireLoad > 0;) {
if (requireLoad <= 0) {
break;
}
agent = iterator.next();
if (!this.getAgentStateService().askLiving(agent.getHostName(),
agent.getPort())) {
continue;
}
if (agent.getRemainLoad() < agent.getMaxLoad()) {
continue;
}
int remainLoadByStart = agent.getRemainLoad();
if (remainLoadByStart >= requireLoad) {
runScenarioModel.setPoolSize(requireLoad);
runScenarioModel.setTotalCount(requireLoad);
this.getAgentPoolService().getLoadFromAgent(
agent.getHostName(), requireLoad);
runScenarioResultModel = this.sendScriptContentToAgent(
agent.getHostName(), agent.getPort(),
this.marShallRunScenarioModel(runScenarioModel));
// TODO:for now, i just think it's stable
resulList.add(runScenarioResultModel);
requireLoad = 0;
} else {
runScenarioModel.setPoolSize(agent.getRemainLoad());
runScenarioModel.setPoolSize(agent.getRemainLoad());
this.getAgentPoolService().getLoadFromAgent(
agent.getHostName(), agent.getRemainLoad());
runScenarioResultModel = this.sendScriptContentToAgent(
agent.getHostName(), agent.getPort(),
this.marShallRunScenarioModel(runScenarioModel));
resulList.add(runScenarioResultModel);
requireLoad -= remainLoadByStart;
}
}
return resulList;
}
public String marShallRunScenarioModel(RunScenarioModel runScenarioModel)
throws JAXBException {
Marshaller marshaller;
marshaller = JAXBContext.newInstance(runScenarioModel.getClass())
.createMarshaller();
StringWriter stringWriter = new StringWriter();
marshaller.marshal(runScenarioModel, stringWriter);
return stringWriter.toString();
}
public RunScenarioResultModel sendScriptContentToAgent(
String hostNameString, int port, String scriptContent) {
RunScenarioResultModel runScenarioResultModel = new RunScenarioResultModel();
try {
HttpResponse httpResponse = this.getHttpRequester().sendPostXml(
hostNameString + ":" + port + "/test/run", scriptContent);
runScenarioResultModel = extractRunSenarioResultModel(httpResponse
.getContent());
return runScenarioResultModel;
} catch (IOException e) {
e.printStackTrace();
return runScenarioResultModel;
} catch (JAXBException e) {
e.printStackTrace();
return runScenarioResultModel;
}
}
private RunScenarioResultModel extractRunSenarioResultModel(
String responseContent) throws JAXBException {
RunScenarioResultModel resultModel = new RunScenarioResultModel();
Unmarshaller unmarshaller = JAXBContext.newInstance(
resultModel.getClass()).createUnmarshaller();
resultModel = (RunScenarioResultModel) unmarshaller
.unmarshal(new ByteArrayInputStream(responseContent.getBytes()));
return resultModel;
}
private TestPlanResponseModel setTestPlanResponseModel(boolean success,
String failCause, List<RunScenarioResultModel> list) {
TestPlanResponseModel resultModel = new TestPlanResponseModel();
@ -241,8 +149,7 @@ public class TestPlanController extends BaseController {
private void giveBackTheLoad(String hostName, AgentBriefModel result) {
// TODO:judge have i back the load already?
if (result.isFinish()) {
int backLoad = result.getTestStatusModel().getTotalCount()
/ result.getTestStatusModel().getScenarioBehaviorCount();
int backLoad = result.getTestStatusModel().getTotalCount() / 2;
this.getAgentPoolService().backLoadToAgent(hostName, backLoad);
}
}
@ -265,4 +172,19 @@ public class TestPlanController extends BaseController {
.getTotalCount());
return result;
}
@RequestMapping(value = "/runTestPlanWithTestPlanModel", method = RequestMethod.GET)
@ResponseBody
public TestPlanResponseModel runTestPlanWithTestPlanModel(
@RequestParam TestPlanModel testPlanModel) {
TestPlanResponseModel result = new TestPlanResponseModel();
Iterator<ScriptIdRequireLoadModel> iterator = testPlanModel
.getScriptIdRequireLoads().iterator();
while (iterator.hasNext()) {
ScriptIdRequireLoadModel sModel = iterator.next();
this.runTestPlanWithScriptId(sModel.getScriptId(),
sModel.getRequireLoad());
}
return result;
}
}

View File

@ -0,0 +1,29 @@
package org.bench4q.master.api.model;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "ScriptIdRequireLoad")
public class ScriptIdRequireLoadModel {
private int scriptId;
private int requireLoad;
@XmlElement
public int getScriptId() {
return scriptId;
}
public void setScriptId(int scriptId) {
this.scriptId = scriptId;
}
@XmlElement
public int getRequireLoad() {
return requireLoad;
}
public void setRequireLoad(int requireLoad) {
this.requireLoad = requireLoad;
}
}

View File

@ -0,0 +1,22 @@
package org.bench4q.master.api.model;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
public class TestPlanModel {
private List<ScriptIdRequireLoadModel> scriptIdRequireLoads;
@XmlElementWrapper(name = "scriptIdRequireLoads")
@XmlElement
public List<ScriptIdRequireLoadModel> getScriptIdRequireLoads() {
return scriptIdRequireLoads;
}
public void setScriptIdRequireLoads(
List<ScriptIdRequireLoadModel> scriptIdRequireLoads) {
this.scriptIdRequireLoads = scriptIdRequireLoads;
}
}

View File

@ -0,0 +1,32 @@
package org.bench4q.master.api.model;
import javax.xml.bind.annotation.XmlElement;
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 int ScriptId;
@XmlElement
public RunScenarioResultModel getRunScenarioResultModel() {
return runScenarioResultModel;
}
public void setRunScenarioResultModel(
RunScenarioResultModel runScenarioResultModel) {
this.runScenarioResultModel = runScenarioResultModel;
}
@XmlElement
public int getScriptId() {
return ScriptId;
}
public void setScriptId(int scriptId) {
ScriptId = scriptId;
}
}

View File

@ -0,0 +1,5 @@
package org.bench4q.master.testPlan;
public class HighAvailable {
}

View File

@ -0,0 +1,5 @@
package org.bench4q.master.testPlan;
public class LoadBallancerForAgent {
}

View File

@ -0,0 +1,159 @@
package org.bench4q.master.testPlan;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
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.entity.db.Agent;
import org.bench4q.master.service.AgentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class TestPlanRunner {
private AgentService agentService;
private AgentStateService agentStateService;
private HttpRequester httpRequester;
public AgentService getAgentService() {
return agentService;
}
@Autowired
public void setAgentService(AgentService agentService) {
this.agentService = agentService;
}
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 List<RunScenarioResultModel> runTestWithRunScenarioModel(
RunScenarioModel runScenarioModel, int requireLoad)
throws JAXBException {
// TODO: is it needed to be synchronized, but i think it is;
Iterator<Agent> iterator;
Agent agent;
List<RunScenarioResultModel> resulList = new ArrayList<RunScenarioResultModel>();
RunScenarioResultModel runScenarioResultModel = new RunScenarioResultModel();
// 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(),
agent.getPort())) {
continue;
}
if (agent.getRemainLoad() < agent.getMaxLoad()) {
continue;
}
int remainLoadByStart = agent.getRemainLoad();
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;
} 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)
throws JAXBException {
Marshaller marshaller;
marshaller = JAXBContext.newInstance(runScenarioModel.getClass())
.createMarshaller();
StringWriter stringWriter = new StringWriter();
marshaller.marshal(runScenarioModel, stringWriter);
return stringWriter.toString();
}
public RunScenarioResultModel sendScriptContentToAgent(
String hostNameString, int port, String scriptContent) {
RunScenarioResultModel runScenarioResultModel = new RunScenarioResultModel();
try {
HttpResponse httpResponse = this.getHttpRequester().sendPostXml(
hostNameString + ":" + port + "/test/run", scriptContent);
runScenarioResultModel = extractRunSenarioResultModel(httpResponse
.getContent());
return runScenarioResultModel;
} catch (IOException e) {
e.printStackTrace();
return runScenarioResultModel;
} catch (JAXBException e) {
e.printStackTrace();
return runScenarioResultModel;
}
}
private RunScenarioResultModel extractRunSenarioResultModel(
String responseContent) throws JAXBException {
RunScenarioResultModel resultModel = new RunScenarioResultModel();
Unmarshaller unmarshaller = JAXBContext.newInstance(
resultModel.getClass()).createUnmarshaller();
resultModel = (RunScenarioResultModel) unmarshaller
.unmarshal(new ByteArrayInputStream(responseContent.getBytes()));
return resultModel;
}
}

View File

@ -79,8 +79,8 @@ public class TestPlanTester {
// String content = stringWriter.toString();
List<RunScenarioResultModel> map = testPlanController
.runTestWithRunScenarioModel(runScenarioModel,
runScenarioModel.getTotalCount());
.getTestPlanRunner().runTestWithRunScenarioModel(
runScenarioModel, runScenarioModel.getTotalCount());
Iterator<RunScenarioResultModel> iterator = map.iterator();
RunScenarioResultModel runResultModel;
while (iterator.hasNext()) {