refactor
This commit is contained in:
coderfengyun 2014-04-22 09:13:11 +08:00
parent 856a71c38e
commit 0f9e403731
10 changed files with 153 additions and 19 deletions

View File

@ -279,6 +279,7 @@ public class TestController {
@RequestMapping(value = "/clean/{runId}", method = RequestMethod.GET)
@ResponseBody
public CleanTestResultModel clean(@PathVariable UUID runId) {
this.getScenarioEngine().getRunningTests().remove(runId);
System.gc();
return new CleanTestResultModel() {
{

View File

@ -19,7 +19,7 @@ public interface RunningAgentInterface {
public abstract class AbstractRunningAgent {
public static RunningAgentInterface buildDegendRunningAgentDB(Agent agent,
public static RunningAgentInterface buildRunningAgentDBWithoutId(Agent agent,
int loadInUse, final int scriptId, UUID agentRunId) {
RunningAgentDB runningAgentDB = new RunningAgentDB();
runningAgentDB.setAgent(agent);

View File

@ -1,5 +1,7 @@
package org.bench4q.master.domain.entity;
import java.io.IOException;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
@ -7,7 +9,17 @@ import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.apache.log4j.Logger;
import org.bench4q.master.domain.service.AgentService;
import org.bench4q.master.domain.transaction.Transaction;
import org.bench4q.master.domain.transaction.exception.AgentRunException;
import org.bench4q.master.domain.transaction.impl.AgentDistribute;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.helper.ApplicationContextHelper;
import org.bench4q.master.infrastructure.communication.AgentMessenger;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.RunScenarioResultModel;
import org.python.antlr.PythonParser.return_stmt_return;
@Entity
@Table(name = "agent")
@ -84,4 +96,121 @@ public class Agent {
agent.setRemainLoad(500);
return agent;
}
public boolean runWithouParams(RunScenarioModel scenarioModel) {
AgentDistribute agentDistribute = new AgentDistribute(hostName, port,
scenarioModel);
try {
agentDistribute.execute();
return true;
} catch (AgentRunException e) {
agentDistribute.rollBack();
return false;
}
}
public class AgentDistribute implements Transaction {
private String agentHostName;
private int agentPort;
private RunScenarioModel runScenarioModel;
private AgentService agentService;
private AgentMessenger agentMessenger;
private Logger logger = Logger.getLogger(AgentDistribute.class);
private String getAgentHostName() {
return agentHostName;
}
private void setAgentHostName(String agentHostName) {
this.agentHostName = agentHostName;
}
private int getAgentPort() {
return agentPort;
}
private void setAgentPort(int agentPort) {
this.agentPort = agentPort;
}
private RunScenarioModel getRunScenarioModel() {
return runScenarioModel;
}
private void setRunScenarioModel(RunScenarioModel runScenarioModel) {
this.runScenarioModel = runScenarioModel;
}
private AgentService getAgentService() {
return agentService;
}
private void setAgentService(AgentService agentService) {
this.agentService = agentService;
}
public AgentMessenger getAgentMessenger() {
return agentMessenger;
}
public void setAgentMessenger(AgentMessenger agentMessenger) {
this.agentMessenger = agentMessenger;
}
public AgentDistribute(String agentHostName, int port,
RunScenarioModel runScenarioModel) {
this.setAgentHostName(agentHostName);
this.setAgentPort(port);
this.setRunScenarioModel(runScenarioModel);
this.setAgentService(ApplicationContextHelper.getContext().getBean(
AgentService.class));
this.setAgentMessenger(ApplicationContextHelper.getContext()
.getBean(AgentMessenger.class));
}
public Object execute() throws AgentRunException {
try {
if (!this.getAgentService().getLoadFromAgent(
this.getAgentHostName(),
this.getRunScenarioModel().getPoolSize())) {
throw new AgentRunException();
}
if (!isValidRunScenarioModel(runScenarioModel)) {
throw new AgentRunException();
}
RunScenarioResultModel ret = this.getAgentMessenger()
.runWithoutParams(
buildAgent(this.getAgentHostName(),
this.getAgentPort()),
this.getRunScenarioModel());
if (ret == null) {
throw new AgentRunException();
}
return ret;
} catch (IOException e) {
this.logger.error(ExceptionLog.getStackTrace(e).toString());
throw new AgentRunException();
}
}
private Agent buildAgent(String hostName, int port) {
Agent agent = new Agent();
agent.setHostName(hostName);
agent.setPort(port);
return agent;
}
private boolean isValidRunScenarioModel(
RunScenarioModel runScenarioModel) {
return runScenarioModel == null ? false : true;
}
public void rollBack() {
this.getAgentService().backLoadToAgent(this.getAgentHostName(),
this.getRunScenarioModel().getPoolSize());
}
}
}

View File

@ -1,16 +1,16 @@
package org.bench4q.master.domain.transaction;
import org.bench4q.master.domain.RunningScriptInterface;
import org.bench4q.master.domain.transaction.impl.ScriptLoadCommand;
import org.bench4q.master.domain.transaction.impl.TestPlanLoadCommand;
import org.bench4q.master.domain.transaction.impl.ScriptDistribute;
import org.bench4q.master.domain.transaction.impl.TestPlanDistribute;
public class TransactionFactory {
public static Transaction buildScriptTransaction(
RunningScriptInterface runningScript, int requiredLoad) {
return new ScriptLoadCommand(runningScript, requiredLoad);
return new ScriptDistribute(runningScript, requiredLoad);
}
public static Transaction buildTestPlanTransaction() {
return new TestPlanLoadCommand();
return new TestPlanDistribute();
}
}

View File

@ -13,14 +13,14 @@ import org.bench4q.master.infrastructure.communication.AgentMessenger;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.RunScenarioResultModel;
public class AgentTransaction implements Transaction {
public class AgentDistribute implements Transaction {
private String agentHostName;
private int agentPort;
private RunScenarioModel runScenarioModel;
private AgentService agentService;
private AgentMessenger agentMessenger;
private Logger logger = Logger.getLogger(AgentTransaction.class);
private Logger logger = Logger.getLogger(AgentDistribute.class);
private String getAgentHostName() {
return agentHostName;
@ -62,7 +62,7 @@ public class AgentTransaction implements Transaction {
this.agentMessenger = agentMessenger;
}
public AgentTransaction(String agentHostName, int port,
public AgentDistribute(String agentHostName, int port,
RunScenarioModel runScenarioModel) {
this.setAgentHostName(agentHostName);
this.setAgentPort(port);
@ -83,9 +83,11 @@ public class AgentTransaction implements Transaction {
if (!isValidRunScenarioModel(runScenarioModel)) {
throw new AgentRunException();
}
RunScenarioResultModel ret = this.getAgentMessenger().runWithoutParams(
buildAgent(this.getAgentHostName(), this.getAgentPort()),
this.getRunScenarioModel());
RunScenarioResultModel ret = this.getAgentMessenger()
.runWithoutParams(
buildAgent(this.getAgentHostName(),
this.getAgentPort()),
this.getRunScenarioModel());
if (ret == null) {
throw new AgentRunException();
}
@ -111,4 +113,5 @@ public class AgentTransaction implements Transaction {
this.getAgentService().backLoadToAgent(this.getAgentHostName(),
this.getRunScenarioModel().getPoolSize());
}
}

View File

@ -2,7 +2,7 @@ package org.bench4q.master.domain.transaction.impl;
import org.bench4q.master.domain.RunningScriptInterface;
public class ScriptLoadCommand extends ScriptLoadBase {
public class ScriptDistribute extends ScriptLoadBase {
private int requiredLoad;
private int getRequiredLoad() {
@ -13,7 +13,7 @@ public class ScriptLoadCommand extends ScriptLoadBase {
this.requiredLoad = requiredLoad;
}
public ScriptLoadCommand(RunningScriptInterface runningScript,
public ScriptDistribute(RunningScriptInterface runningScript,
int requiredLoad) {
super(runningScript);
this.setRequiredLoad(requiredLoad);

View File

@ -30,7 +30,7 @@ public abstract class ScriptLoadBase implements Transaction {
private List<RunningAgentInterface> agentListThisTime;
private AgentMessenger runningAgentService;
private HighAvailablePool highAvailableAgentPool;
private static Logger logger = Logger.getLogger(ScriptLoadCommand.class);
private static Logger logger = Logger.getLogger(ScriptDistribute.class);
protected RunningScriptInterface getRunningScript() {
return runningScript;
@ -158,7 +158,7 @@ public abstract class ScriptLoadBase implements Transaction {
}
loadForRunCurrent = getMin(totalRequireLoad, agent.getRemainLoad());
runScenarioModel.setPoolSize(loadForRunCurrent);
AgentTransaction agentTransaction = new AgentTransaction(
AgentDistribute agentTransaction = new AgentDistribute(
agent.getHostName(), agent.getPort(), runScenarioModel);
try {
runScenarioResultModel = (RunScenarioResultModel) agentTransaction
@ -171,7 +171,7 @@ public abstract class ScriptLoadBase implements Transaction {
agent.setCurrentStatus(AgentService.AGENT_STATUS_InRun);
agent.setRemainLoad(agent.getRemainLoad() - loadForRunCurrent);
RunningAgentInterface runningAgent = AbstractRunningAgent
.buildDegendRunningAgentDB(agent, loadForRunCurrent,
.buildRunningAgentDBWithoutId(agent, loadForRunCurrent,
scriptId, runScenarioResultModel.getRunId());
this.getAgentListThisTime().add(runningAgent);
updateAgentBlotterInHA(testPlanId, runningAgent);

View File

@ -2,7 +2,7 @@ package org.bench4q.master.domain.transaction.impl;
import org.bench4q.master.domain.transaction.Transaction;
public class TestPlanLoadCommand implements Transaction {
public class TestPlanDistribute implements Transaction {
public Object execute() {
return null;

View File

@ -13,6 +13,7 @@ import org.bench4q.master.infrastructure.highavailable.HighAvailablePool;
import org.bench4q.share.enums.master.TestPlanStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class LoadDistribute {

View File

@ -8,7 +8,7 @@ import java.util.UUID;
import org.bench4q.master.domain.RunningAgentInterface;
import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.domain.entity.TestPlanScript;
import org.bench4q.master.domain.transaction.impl.ScriptLoadCommand;
import org.bench4q.master.domain.transaction.impl.ScriptDistribute;
import org.bench4q.master.test.TestBase_MakeUpTestPlan;
import org.junit.After;
import org.junit.Before;
@ -54,7 +54,7 @@ public class Test_ScriptLoadCommand extends TestBase_MakeUpTestPlan {
TestPlanScript testPlanScript = this.getTestPlanRepository()
.getTestPlanBy(getTestPlanRunIdUuid())
.extracSpecifiedScript(this.getScriptId());
ScriptLoadCommand scriptLoadCommand = new ScriptLoadCommand(
ScriptDistribute scriptLoadCommand = new ScriptDistribute(
testPlanScript, testPlanScript.getRequireLoad());
List<? extends RunningAgentInterface> runningAgents = null;
synchronized (this.getHaPool().getPool()) {