refactor with little step

refactor with little step
This commit is contained in:
coderfengyun 2014-05-08 15:04:28 +08:00
parent e3ca176d68
commit 9af631ad1c
28 changed files with 404 additions and 280 deletions

View File

@ -3,10 +3,6 @@ package org.bench4q.master.domain;
import java.util.UUID;
import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.domain.entity.RunningAgentDB;
import org.bench4q.master.domain.entity.TestPlanScript;
import org.bench4q.master.helper.ApplicationContextHelper;
import org.bench4q.master.infrastructure.communication.AgentMessenger;
public interface RunningAgentInterface {
public Agent getAgent();
@ -25,17 +21,4 @@ public interface RunningAgentInterface {
public boolean stop();
public abstract class AbstractRunningAgent {
public static RunningAgentInterface buildRunningAgentDBWithoutId(
Agent agent, int loadInUse,
RunningScriptInterface runningScript, UUID agentRunId) {
RunningAgentDB runningAgent = RunningAgentDB
.buildRunningAgentDBWithoutId(agent, loadInUse,
(TestPlanScript) runningScript, agentRunId);
runningAgent.setAgentMessenger(ApplicationContextHelper
.getBean(AgentMessenger.class));
return runningAgent;
}
}
}

View File

@ -8,12 +8,7 @@ import javax.persistence.Id;
import javax.persistence.Table;
import org.apache.log4j.Logger;
import org.bench4q.master.domain.repository.AgentRepository;
import org.bench4q.master.domain.service.AgentService;
import org.bench4q.master.helper.ApplicationContextHelper;
import org.bench4q.master.infrastructure.communication.AgentMessenger;
import org.bench4q.share.models.agent.RunScenarioResultModel;
import org.bench4q.share.models.agent.ServerStatusModel;
@Entity
@Table(name = "agent")
@ -120,17 +115,4 @@ public class Agent {
return true;
}
public RunScenarioResultModel bookTest(int requireLoad) {
RunScenarioResultModel resultModel = ApplicationContextHelper.getBean(
AgentMessenger.class).bookTest(this, requireLoad);
this.setCurrentStatus(AgentService.AGENT_STATUS_InRun);
this.setRemainLoad(this.getRemainLoad() - requireLoad);
ApplicationContextHelper.getBean(AgentRepository.class).update(this);
return resultModel;
}
public ServerStatusModel queryStatus() {
return ApplicationContextHelper.getBean(AgentMessenger.class)
.askLiving(getHostName(), getPort());
}
}

View File

@ -14,7 +14,7 @@ import javax.persistence.Transient;
import org.bench4q.master.domain.RunningAgentInterface;
import org.bench4q.master.domain.service.AgentService;
import org.bench4q.master.infrastructure.communication.AgentMessenger;
import org.bench4q.master.infrastructure.communication.impl.AgentMessengerImpl;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.RunScenarioResultModel;
@ -29,7 +29,7 @@ public class RunningAgentDB implements RunningAgentInterface {
private Script script;
private UUID agentRunId;
private TestPlanScript testPlanScript;
private AgentMessenger agentMessenger;
private AgentMessengerImpl agentMessenger;
private boolean hasSubstitute;
@Id
@ -102,11 +102,11 @@ public class RunningAgentDB implements RunningAgentInterface {
}
@Transient
public AgentMessenger getAgentMessenger() {
public AgentMessengerImpl getAgentMessenger() {
return agentMessenger;
}
public void setAgentMessenger(AgentMessenger agentMessenger) {
public void setAgentMessenger(AgentMessengerImpl agentMessenger) {
this.agentMessenger = agentMessenger;
}

View File

@ -33,7 +33,7 @@ import org.bench4q.master.domain.valueobject.schedulscript.WarmUpOverTask;
import org.bench4q.master.domain.valueobject.transaction.Transaction;
import org.bench4q.master.domain.valueobject.transaction.TransactionFactory;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.infrastructure.communication.AgentMessenger;
import org.bench4q.master.infrastructure.communication.impl.AgentMessengerImpl;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.StopTestModel;
@ -52,7 +52,8 @@ public class TestPlanScript implements RunningScriptInterface {
private boolean finish;
private TestPlanFactory testPlanFactory;
private AgentMessenger agentMessenger;
private AgentMessengerImpl agentMessenger;
private TransactionFactory transactionFactory;
private RunningScriptSampler sampler;
private AgentService agentService;
private Logger logger = Logger.getLogger(TestPlanScript.class);
@ -183,11 +184,11 @@ public class TestPlanScript implements RunningScriptInterface {
}
@Transient
private AgentMessenger getAgentMessenger() {
private AgentMessengerImpl getAgentMessenger() {
return agentMessenger;
}
public void setAgentMessenger(AgentMessenger agentMessenger) {
public void setAgentMessenger(AgentMessengerImpl agentMessenger) {
this.agentMessenger = agentMessenger;
}
@ -200,6 +201,15 @@ public class TestPlanScript implements RunningScriptInterface {
this.agentService = agentService;
}
@Transient
private TransactionFactory getTransactionFactory() {
return transactionFactory;
}
public void setTransactionFactory(TransactionFactory transactionFactory) {
this.transactionFactory = transactionFactory;
}
/**
* This should be called when really in running
*
@ -238,8 +248,8 @@ public class TestPlanScript implements RunningScriptInterface {
for (RunningAgentInterface runningAgent : agentsAfterDistribute) {
this.getRunningAgentsDB().add((RunningAgentDB) runningAgent);
}
this.setSampler(new RunningScriptSampler(Collections
.unmodifiableCollection(this.getRunningAgentsDB())));
this.setSampler(new RunningScriptSampler(this.getAgentMessenger(),
Collections.unmodifiableCollection(this.getRunningAgentsDB())));
}
public List<TestPlanScriptResult> doAfterRun() {
@ -263,7 +273,7 @@ public class TestPlanScript implements RunningScriptInterface {
@SuppressWarnings("unchecked")
public boolean applyForLoad(int requiredLoad) {
Transaction scriptLoadCommand = TransactionFactory
Transaction scriptLoadCommand = this.getTransactionFactory()
.buildScriptTransaction(this, this.getRequireLoad());
List<? extends RunningAgentInterface> runningAgents = null;
try {

View File

@ -0,0 +1,27 @@
package org.bench4q.master.domain.factory;
import java.util.UUID;
import org.bench4q.master.domain.RunningAgentInterface;
import org.bench4q.master.domain.RunningScriptInterface;
import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.domain.entity.RunningAgentDB;
import org.bench4q.master.domain.entity.TestPlanScript;
import org.bench4q.master.infrastructure.communication.impl.AgentMessengerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class RunningAgentFactory {
@Autowired
private AgentMessengerImpl agentMessenger;
public RunningAgentInterface buildRunningAgentDBWithoutId(Agent agent,
int loadInUse, RunningScriptInterface runningScript, UUID agentRunId) {
RunningAgentDB runningAgent = RunningAgentDB
.buildRunningAgentDBWithoutId(agent, loadInUse,
(TestPlanScript) runningScript, agentRunId);
runningAgent.setAgentMessenger(this.agentMessenger);
return runningAgent;
}
}

View File

@ -24,9 +24,10 @@ import org.bench4q.master.domain.repository.TestPlanRepository;
import org.bench4q.master.domain.service.AgentService;
import org.bench4q.master.domain.service.ScriptService;
import org.bench4q.master.domain.service.TestResultSave;
import org.bench4q.master.domain.valueobject.transaction.TransactionFactory;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.exception.ExceptionUtils.IllegalParameterException;
import org.bench4q.master.infrastructure.communication.AgentMessenger;
import org.bench4q.master.infrastructure.communication.impl.AgentMessengerImpl;
import org.bench4q.share.enums.master.TestPlanStatus;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.MonitorModel;
@ -42,17 +43,18 @@ import org.springframework.stereotype.Component;
public class TestPlanFactory {
private Logger logger = Logger.getLogger(TestPlanFactory.class);
private ScriptService scriptService;
private AgentMessenger agentMessenger;
private AgentMessengerImpl agentMessenger;
private AgentService agentService;
private TestResultSave testResultSave;
private TestPlanRepository testPlanRepository;
private TransactionFactory transactionFactory;
public AgentMessenger getAgentMessenger() {
public AgentMessengerImpl getAgentMessenger() {
return agentMessenger;
}
@Autowired
public void setAgentMessenger(AgentMessenger agentMessenger) {
public void setAgentMessenger(AgentMessengerImpl agentMessenger) {
this.agentMessenger = agentMessenger;
}
@ -92,6 +94,15 @@ public class TestPlanFactory {
this.testPlanRepository = testPlanRepository;
}
private TransactionFactory getTransactionFactory() {
return transactionFactory;
}
@Autowired
private void setTransactionFactory(TransactionFactory transactionFactory) {
this.transactionFactory = transactionFactory;
}
public TestPlan createATestPlanWithoutIdentity(TestPlanModel testPlanModel,
User user, UUID runId) throws IllegalParameterException {
Logger.getLogger(TestPlanFactory.class).info(
@ -256,6 +267,7 @@ public class TestPlanFactory {
testPlanScript.setTestPlanFactory(this);
testPlanScript.setAgentMessenger(this.getAgentMessenger());
testPlanScript.setAgentService(this.getAgentService());
testPlanScript.setTransactionFactory(this.getTransactionFactory());
for (RunningAgentDB runningAgent : testPlanScript.getRunningAgentsDB()) {
runningAgent.setAgentMessenger(this.getAgentMessenger());
}

View File

@ -120,6 +120,17 @@ public class AgentRepository extends AbstractRepositoty {
}
}
public Agent getEntity(int id) {
Session session = this.getSessionHelper().openSession();
try {
return (Agent) session.get(Agent.class, id);
} catch (Exception e) {
return null;
} finally {
releaseSession(session);
}
}
@SuppressWarnings("unchecked")
public List<Agent> loadEntities() {
Session session = this.getSessionHelper().openSession();

View File

@ -68,6 +68,8 @@ public class AgentService {
public boolean removeAgentFromPool(int agentId) {
// TODO: check if the agent is idle
synchronized (this.getAgentRepository().getAddDeleteLock()) {
this.getHighAvailablePool().remove(
getAgentRepository().getEntity(agentId));
return this.getAgentRepository().detach(agentId);
}
}

View File

@ -9,7 +9,7 @@ import javax.xml.bind.JAXBException;
import org.bench4q.master.domain.RunningAgentInterface;
import org.bench4q.master.helper.ApplicationContextHelper;
import org.bench4q.master.infrastructure.communication.AgentMessenger;
import org.bench4q.master.infrastructure.communication.impl.AgentMessengerImpl;
import org.bench4q.share.models.agent.TestBriefStatusModel;
import org.bench4q.share.models.agent.statistics.AgentBehaviorsBriefModel;
import org.bench4q.share.models.agent.statistics.AgentBriefStatusModel;
@ -23,7 +23,7 @@ import org.bench4q.share.models.master.statistics.ScriptResultModel;
public class RunningScriptSampler {
private List<DataStatistics> dataStatisticsList;
private List<RunningAgentInterface> runningAgents;
private AgentMessenger agentMessenger;
private AgentMessengerImpl agentMessenger;
private List<DataStatistics> getDataStatisticsList() {
return dataStatisticsList;
@ -41,11 +41,11 @@ public class RunningScriptSampler {
this.runningAgents = runningAgents;
}
private AgentMessenger getAgentMessenger() {
private AgentMessengerImpl getAgentMessenger() {
return agentMessenger;
}
private void setAgentMessenger(AgentMessenger agentMessenger) {
private void setAgentMessenger(AgentMessengerImpl agentMessenger) {
this.agentMessenger = agentMessenger;
}
@ -55,11 +55,11 @@ public class RunningScriptSampler {
this.getDataStatisticsList().add(new BehaviorsBriefStatistics());
this.getDataStatisticsList().add(new PagesBriefStatistics());
this.setAgentMessenger(ApplicationContextHelper.getContext().getBean(
AgentMessenger.class));
AgentMessengerImpl.class));
this.setRunningAgents(new LinkedList<RunningAgentInterface>());
}
public RunningScriptSampler(
public RunningScriptSampler(AgentMessengerImpl agentMessenger,
Collection<? extends RunningAgentInterface> runningAgents) {
this();
this.getRunningAgents().addAll(runningAgents);
@ -102,7 +102,8 @@ public class RunningScriptSampler {
}
private void addScriptBehaviorsBriefModel(
AgentBehaviorsBriefModel agentBehaviorsBriefModel) throws JAXBException {
AgentBehaviorsBriefModel agentBehaviorsBriefModel)
throws JAXBException {
if (agentBehaviorsBriefModel != null) {
getBehaviorsBriefStatistics().add(agentBehaviorsBriefModel);
}

View File

@ -1,7 +1,7 @@
package org.bench4q.master.domain.valueobject.datastatistics;
import org.bench4q.master.infrastructure.communication.MonitorMessenger;
import org.bench4q.master.infrastructure.communication.impl.MonitorMessenger;
import org.bench4q.share.models.monitor.MonitorMain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

View File

@ -1,16 +1,34 @@
package org.bench4q.master.domain.valueobject.transaction;
import org.bench4q.master.domain.RunningScriptInterface;
import org.bench4q.master.domain.factory.RunningAgentFactory;
import org.bench4q.master.domain.valueobject.transaction.impl.ScriptLoadApplication;
import org.bench4q.master.domain.valueobject.transaction.impl.TestPlanDistribute;
import org.bench4q.master.infrastructure.communication.impl.AgentMessengerImpl;
import org.bench4q.master.infrastructure.highavailable.HighAvailablePool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class TransactionFactory {
public static Transaction buildScriptTransaction(
@Autowired
private AgentMessengerImpl agentMessenger;
@Autowired
private RunningAgentFactory runningAgentFactory;
@Autowired
private HighAvailablePool highAvailablePool;
public Transaction buildScriptTransaction(
RunningScriptInterface runningScript, int requiredLoad) {
return new ScriptLoadApplication(runningScript, requiredLoad);
ScriptLoadApplication result = new ScriptLoadApplication(runningScript,
requiredLoad, this.agentMessenger, this.runningAgentFactory,
this.highAvailablePool);
return result;
}
public static Transaction buildTestPlanTransaction() {
public Transaction buildTestPlanTransaction() {
return new TestPlanDistribute();
}
}

View File

@ -1,6 +1,9 @@
package org.bench4q.master.domain.valueobject.transaction.impl;
import org.bench4q.master.domain.RunningScriptInterface;
import org.bench4q.master.domain.factory.RunningAgentFactory;
import org.bench4q.master.infrastructure.communication.impl.AgentMessengerImpl;
import org.bench4q.master.infrastructure.highavailable.HighAvailablePool;
public class ScriptLoadApplication extends ScriptLoadBase {
private int requiredLoad;
@ -14,13 +17,16 @@ public class ScriptLoadApplication extends ScriptLoadBase {
}
public ScriptLoadApplication(RunningScriptInterface runningScript,
int requiredLoad) {
int requiredLoad, AgentMessengerImpl agentMessenger,
RunningAgentFactory runningAgentFactory,
HighAvailablePool highAvailablePool) {
super(runningScript);
this.setRequiredLoad(requiredLoad);
this.setHighAvailableAgentPool(highAvailablePool);
this.setAgentMessenger(agentMessenger);
this.setRunningAgentFactory(runningAgentFactory);
}
@Override
protected int getRequireLoad() {
return this.getRequiredLoad();

View File

@ -3,27 +3,28 @@ package org.bench4q.master.domain.valueobject.transaction.impl;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import javax.xml.bind.JAXBException;
import org.apache.log4j.Logger;
import org.bench4q.master.domain.RunningAgentInterface;
import org.bench4q.master.domain.RunningScriptInterface;
import org.bench4q.master.domain.RunningAgentInterface.AbstractRunningAgent;
import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.domain.factory.RunningAgentFactory;
import org.bench4q.master.domain.valueobject.transaction.Transaction;
import org.bench4q.master.domain.valueobject.transaction.exception.ScriptLoadDistributeException;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.helper.ApplicationContextHelper;
import org.bench4q.master.infrastructure.communication.impl.AgentMessengerImpl;
import org.bench4q.master.infrastructure.highavailable.HighAvailablePool;
import org.bench4q.master.infrastructure.highavailable.impl.HighAvailablePoolImpl;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.RunScenarioResultModel;
import org.springframework.beans.factory.annotation.Autowired;
public abstract class ScriptLoadBase implements Transaction {
private RunningScriptInterface runningScript;
private List<RunningAgentInterface> agentListThisTime;
private HighAvailablePool highAvailableAgentPool;
private RunningAgentFactory runningAgentFactory;
private AgentMessengerImpl agentMessenger;
private static Logger logger = Logger
.getLogger(ScriptLoadApplication.class);
@ -48,12 +49,28 @@ public abstract class ScriptLoadBase implements Transaction {
return highAvailableAgentPool;
}
@Autowired
private void setHighAvailableAgentPool(
protected void setHighAvailableAgentPool(
HighAvailablePool highAvailableAgentPool) {
this.highAvailableAgentPool = highAvailableAgentPool;
}
private RunningAgentFactory getRunningAgentFactory() {
return runningAgentFactory;
}
protected void setRunningAgentFactory(
RunningAgentFactory runningAgentFactory) {
this.runningAgentFactory = runningAgentFactory;
}
private AgentMessengerImpl getAgentMessenger() {
return agentMessenger;
}
protected void setAgentMessenger(AgentMessengerImpl agentMessenger) {
this.agentMessenger = agentMessenger;
}
public static Logger getLogger() {
return logger;
}
@ -68,8 +85,6 @@ public abstract class ScriptLoadBase implements Transaction {
private void initialize(RunningScriptInterface runningScript) {
this.setRunningScript(runningScript);
this.setHighAvailableAgentPool(ApplicationContextHelper.getContext()
.getBean(HighAvailablePoolImpl.class));
this.setAgentListThisTime(new ArrayList<RunningAgentInterface>());
}
@ -126,7 +141,8 @@ public abstract class ScriptLoadBase implements Transaction {
continue;
}
loadForRunCurrent = getMin(totalRequireLoad, agent.getRemainLoad());
runScenarioResultModel = agent.bookTest(loadForRunCurrent);
runScenarioResultModel = this.getAgentMessenger().bookTest(agent,
loadForRunCurrent);
if (runScenarioResultModel == null
|| runScenarioResultModel.getRunId() == null) {
logger.error(runScenarioResultModel == null ? "runScenarioResultModel is null"
@ -134,10 +150,11 @@ public abstract class ScriptLoadBase implements Transaction {
+ runScenarioResultModel.getRunId());
continue;
}
RunningAgentInterface runningAgent = AbstractRunningAgent
RunningAgentInterface runningAgent = this.getRunningAgentFactory()
.buildRunningAgentDBWithoutId(agent, loadForRunCurrent,
this.getRunningScript(),
runScenarioResultModel.getRunId());
result.add(runningAgent);
this.getHighAvailableAgentPool().addABlotter(runningAgent);
totalRequireLoad -= loadForRunCurrent;

View File

@ -1,166 +1,28 @@
package org.bench4q.master.infrastructure.communication;
import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import org.apache.log4j.Logger;
import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.infrastructure.faultolerence.FaultTolerenceFactory;
import org.bench4q.share.communication.HttpRequester;
import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.RunScenarioResultModel;
import org.bench4q.share.models.agent.ServerStatusModel;
import org.bench4q.share.models.agent.StopTestModel;
import org.bench4q.share.models.agent.TestBriefStatusModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class AgentMessenger {
private HttpRequester httpRequester;
private Logger logger = Logger.getLogger(AgentMessenger.class);
private HttpRequester getHttpRequester() {
return httpRequester;
}
@Autowired
private void setHttpRequester(HttpRequester httpRequester) {
this.httpRequester = httpRequester;
}
private void logIt(HttpResponse httpResponse, Throwable e) {
logger.error(HttpRequester.isInvalidResponse(httpResponse) ? ""
: httpResponse.getContent(), e);
}
public RunScenarioResultModel bookTest(Agent agent, int requireLoad) {
HttpResponse httpResponse = null;
try {
httpResponse = this.getHttpRequester().sendGet(
buildBaseUrl(agent) + "/test/bookTest/" + requireLoad,
null, null);
System.out.println(buildBaseUrl(agent) + "/test/bookTest/"
+ requireLoad);
return (RunScenarioResultModel) MarshalHelper.unmarshal(
RunScenarioResultModel.class, httpResponse.getContent());
} catch (Exception e) {
logger.error(httpResponse.getContent(), e);
return null;
}
}
public interface AgentMessenger {
public RunScenarioResultModel bookTest(Agent agent, int requireLoad);
public RunScenarioResultModel submitScenrioWithParams(Agent agent,
UUID agentRunId, List<File> paramFiles,
final RunScenarioModel runScenarioModel) {
HttpResponse httpResponse = null;
try {
final String modelContent = MarshalHelper
.tryMarshal(runScenarioModel);
httpResponse = this.httpRequester.postFiles(null,
buildBaseUrl(agent) + "/test/submitScenarioWithParams/"
+ agentRunId, "files[]", paramFiles,
"scenarioModel", new LinkedList<String>() {
private static final long serialVersionUID = 1L;
{
add(modelContent);
}
});
return (RunScenarioResultModel) MarshalHelper.unmarshal(
RunScenarioResultModel.class, httpResponse.getContent());
} catch (Exception e) {
logIt(httpResponse, e);
return null;
}
}
final RunScenarioModel runScenarioModel);
public RunScenarioResultModel runWithParams(Agent agent, UUID agentRunId) {
HttpResponse httpResponse = null;
try {
if (agent == null || agentRunId == null) {
return null;
}
httpResponse = this.httpRequester.sendPost(buildBaseUrl(agent)
+ "/test/runWithParams/" + agentRunId.toString(), null,
null);
if (HttpRequester.isInvalidResponse(httpResponse)) {
logger.error("the response is not valid");
return null;
}
return (RunScenarioResultModel) MarshalHelper.unmarshal(
RunScenarioResultModel.class, httpResponse.getContent());
} catch (Exception e) {
logIt(httpResponse, e);
return null;
}
}
public RunScenarioResultModel runWithParams(Agent agent, UUID agentRunId);
// there is bug in here
public TestBriefStatusModel scriptBriefAll(Agent agent, UUID runId) {
HttpResponse httpResponse = null;
try {
if (agent == null || runId == null) {
return null;
}
httpResponse = this.httpRequester.sendGet(buildBaseUrl(agent)
+ "/test/briefAll/" + runId, null, null);
if (HttpRequester.isInvalidResponse(httpResponse)) {
return null;
}
logger.info(httpResponse.getContent());
return (TestBriefStatusModel) MarshalHelper.unmarshal(
TestBriefStatusModel.class, httpResponse.getContent());
} catch (Exception e) {
logIt(httpResponse, e);
logger.error(e.toString()
+ " When get script all brief the agent with hostName "
+ agent.getHostName());
FaultTolerenceFactory.getBriefFaultTolerance(agent).doTolerance();
return null;
}
}
public TestBriefStatusModel scriptBriefAll(Agent agent, UUID runId);
public StopTestModel stop(Agent agent, UUID runId) {
HttpResponse httpResponse;
try {
if (agent == null || runId == null) {
return null;
}
httpResponse = this.httpRequester.sendGet(buildBaseUrl(agent)
+ "/test/stop/" + runId.toString(), null, null);
if (HttpRequester.isInvalidResponse(httpResponse)) {
return null;
}
return (StopTestModel) MarshalHelper.unmarshal(StopTestModel.class,
httpResponse.getContent());
} catch (Exception e) {
logger.error(e.toString() + " when stop the agent with hostName "
+ agent.getHostName());
FaultTolerenceFactory.getStopAgentFault(agent, runId).doTolerance();
return null;
}
}
public StopTestModel stop(Agent agent, UUID runId);
private String buildBaseUrl(Agent agent) {
return agent.getHostName() + ":" + agent.getPort();
}
public ServerStatusModel askLiving(String hostName, int port) {
try {
HttpResponse httpResponse = this.getHttpRequester().sendGet(
hostName + ":" + port + "/", null, null);
if (HttpRequester.isInvalidResponse(httpResponse)) {
return null;
}
return (ServerStatusModel) MarshalHelper.unmarshal(
ServerStatusModel.class, httpResponse.getContent());
} catch (Exception e) {
logger.error("agent :" + hostName + " break down!");
return null;
}
}
public ServerStatusModel askLiving(String hostName, int port);
}

View File

@ -0,0 +1,167 @@
package org.bench4q.master.infrastructure.communication.impl;
import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import org.apache.log4j.Logger;
import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.infrastructure.communication.AgentMessenger;
import org.bench4q.master.infrastructure.faultolerence.FaultTolerenceFactory;
import org.bench4q.share.communication.HttpRequester;
import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.RunScenarioResultModel;
import org.bench4q.share.models.agent.ServerStatusModel;
import org.bench4q.share.models.agent.StopTestModel;
import org.bench4q.share.models.agent.TestBriefStatusModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class AgentMessengerImpl implements AgentMessenger {
private HttpRequester httpRequester;
private Logger logger = Logger.getLogger(AgentMessengerImpl.class);
private HttpRequester getHttpRequester() {
return httpRequester;
}
@Autowired
private void setHttpRequester(HttpRequester httpRequester) {
this.httpRequester = httpRequester;
}
private void logIt(HttpResponse httpResponse, Throwable e) {
logger.error(HttpRequester.isInvalidResponse(httpResponse) ? ""
: httpResponse.getContent(), e);
}
public RunScenarioResultModel bookTest(Agent agent, int requireLoad) {
HttpResponse httpResponse = null;
try {
httpResponse = this.getHttpRequester().sendGet(
buildBaseUrl(agent) + "/test/bookTest/" + requireLoad,
null, null);
System.out.println(buildBaseUrl(agent) + "/test/bookTest/"
+ requireLoad);
return (RunScenarioResultModel) MarshalHelper.unmarshal(
RunScenarioResultModel.class, httpResponse.getContent());
} catch (Exception e) {
logger.error(httpResponse.getContent(), e);
return null;
}
}
public RunScenarioResultModel submitScenrioWithParams(Agent agent,
UUID agentRunId, List<File> paramFiles,
final RunScenarioModel runScenarioModel) {
HttpResponse httpResponse = null;
try {
final String modelContent = MarshalHelper
.tryMarshal(runScenarioModel);
httpResponse = this.httpRequester.postFiles(null,
buildBaseUrl(agent) + "/test/submitScenarioWithParams/"
+ agentRunId, "files[]", paramFiles,
"scenarioModel", new LinkedList<String>() {
private static final long serialVersionUID = 1L;
{
add(modelContent);
}
});
return (RunScenarioResultModel) MarshalHelper.unmarshal(
RunScenarioResultModel.class, httpResponse.getContent());
} catch (Exception e) {
logIt(httpResponse, e);
return null;
}
}
public RunScenarioResultModel runWithParams(Agent agent, UUID agentRunId) {
HttpResponse httpResponse = null;
try {
if (agent == null || agentRunId == null) {
return null;
}
httpResponse = this.httpRequester.sendPost(buildBaseUrl(agent)
+ "/test/runWithParams/" + agentRunId.toString(), null,
null);
if (HttpRequester.isInvalidResponse(httpResponse)) {
logger.error("the response is not valid");
return null;
}
return (RunScenarioResultModel) MarshalHelper.unmarshal(
RunScenarioResultModel.class, httpResponse.getContent());
} catch (Exception e) {
logIt(httpResponse, e);
return null;
}
}
// there is bug in here
public TestBriefStatusModel scriptBriefAll(Agent agent, UUID runId) {
HttpResponse httpResponse = null;
try {
if (agent == null || runId == null) {
return null;
}
httpResponse = this.httpRequester.sendGet(buildBaseUrl(agent)
+ "/test/briefAll/" + runId, null, null);
if (HttpRequester.isInvalidResponse(httpResponse)) {
return null;
}
logger.info(httpResponse.getContent());
return (TestBriefStatusModel) MarshalHelper.unmarshal(
TestBriefStatusModel.class, httpResponse.getContent());
} catch (Exception e) {
logIt(httpResponse, e);
logger.error(e.toString()
+ " When get script all brief the agent with hostName "
+ agent.getHostName());
FaultTolerenceFactory.getBriefFaultTolerance(agent).doTolerance();
return null;
}
}
public StopTestModel stop(Agent agent, UUID runId) {
HttpResponse httpResponse;
try {
if (agent == null || runId == null) {
return null;
}
httpResponse = this.httpRequester.sendGet(buildBaseUrl(agent)
+ "/test/stop/" + runId.toString(), null, null);
if (HttpRequester.isInvalidResponse(httpResponse)) {
return null;
}
return (StopTestModel) MarshalHelper.unmarshal(StopTestModel.class,
httpResponse.getContent());
} catch (Exception e) {
logger.error(e.toString() + " when stop the agent with hostName "
+ agent.getHostName());
FaultTolerenceFactory.getStopAgentFault(agent, runId).doTolerance();
return null;
}
}
private String buildBaseUrl(Agent agent) {
return agent.getHostName() + ":" + agent.getPort();
}
public ServerStatusModel askLiving(String hostName, int port) {
try {
HttpResponse httpResponse = this.getHttpRequester().sendGet(
hostName + ":" + port + "/", null, null);
if (HttpRequester.isInvalidResponse(httpResponse)) {
return null;
}
return (ServerStatusModel) MarshalHelper.unmarshal(
ServerStatusModel.class, httpResponse.getContent());
} catch (Exception e) {
logger.error("agent :" + hostName + " break down!");
return null;
}
}
}

View File

@ -1,4 +1,4 @@
package org.bench4q.master.infrastructure.communication;
package org.bench4q.master.infrastructure.communication.impl;
import java.io.IOException;
import java.util.Date;

View File

@ -14,6 +14,8 @@ public interface HighAvailablePool {
public void add(Agent agent);
public void remove(Agent agent);
public void addABlotter(RunningAgentInterface runningAgent);
public void removeABlotter(UUID agentRunId);

View File

@ -13,6 +13,7 @@ import org.bench4q.master.domain.RunningScriptInterface;
import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.domain.repository.AgentRepository;
import org.bench4q.master.domain.service.AgentService;
import org.bench4q.master.infrastructure.communication.impl.AgentMessengerImpl;
import org.bench4q.master.infrastructure.highavailable.CurrentLoadSubject;
import org.bench4q.master.infrastructure.highavailable.HighAvailablePool;
import org.bench4q.share.models.agent.ServerStatusModel;
@ -24,6 +25,7 @@ import org.springframework.stereotype.Component;
public class HighAvailablePoolImpl extends CurrentLoadSubject implements
HighAvailablePool {
private AgentRepository agentRepository;
private AgentMessengerImpl agentMessenger;
private Map<String, Agent> pool;
private Map<String, ServerStatusModel> agentStatusOfPreviousBeatMap;
private Map<UUID, RunningAgentInterface> agentRunBlotters;
@ -49,6 +51,15 @@ public class HighAvailablePoolImpl extends CurrentLoadSubject implements
this.agentRepository = agentRepository;
}
private AgentMessengerImpl getAgentMessenger() {
return agentMessenger;
}
@Autowired
private void setAgentMessenger(AgentMessengerImpl agentMessenger) {
this.agentMessenger = agentMessenger;
}
public Long getMaxAvailableLoad() {
this.calculateHAPoolLoadStatusInMonopolize();
return maxAvailableLoad;
@ -62,6 +73,10 @@ public class HighAvailablePoolImpl extends CurrentLoadSubject implements
this.getPool().put(agent.getHostName(), agent);
}
public void remove(Agent agent) {
this.getPool().put(agent.getHostName(), agent);
}
public int getCurrentAvailableLoad() {
this.calculateHAPoolLoadStatusInMonopolize();
return currentAvailableLoad;
@ -161,7 +176,8 @@ public class HighAvailablePoolImpl extends CurrentLoadSubject implements
}
public ServerStatusModel queryAgentStatus(Agent agent) {
return agent.queryStatus();
return this.getAgentMessenger().askLiving(agent.getHostName(),
agent.getPort());
}
public void addABlotter(RunningAgentInterface runningAgent) {

View File

@ -1,7 +1,7 @@
package MonitorModelTest;
import static org.junit.Assert.*;
import org.bench4q.master.infrastructure.communication.MonitorMessenger;
import org.bench4q.master.infrastructure.communication.impl.MonitorMessenger;
import org.bench4q.share.models.master.MonitorModel;
import org.junit.Assert;
import org.junit.Before;

View File

@ -40,8 +40,8 @@ public class Test_RunningScriptSampler extends TestBase_MakeUpTestPlan {
TestPlan testPlanInRunning = fetchTestPlan();
TestPlanScript testPlanScript = testPlanInRunning
.extracSpecifiedScript(getScriptId());
this.setRunningScriptSampler(new RunningScriptSampler(testPlanScript
.getRunningAgentsDB()));
this.setRunningScriptSampler(new RunningScriptSampler(this
.getAgentMessenger(), testPlanScript.getRunningAgentsDB()));
}
@After
@ -49,17 +49,17 @@ public class Test_RunningScriptSampler extends TestBase_MakeUpTestPlan {
cleanUpForTestPlanRunning();
}
@Test
public void testGetScriptResult() throws InterruptedException, JAXBException {
public void testGetScriptResult() throws InterruptedException,
JAXBException {
ScriptResultModel scriptResultModel = this.getRunningScriptSampler()
.getResultModelFromAgent();
Thread.sleep(1000);
assertNotNull(scriptResultModel);
assertNotNull(scriptResultModel.getScriptBriefResultModel());
assertTrue(scriptResultModel.getScriptBehaviorsBriefModel()
.getTestBehaviorsBriefModel().getBehaviorBriefModels().size() > 0);
assertNotNull(scriptResultModel.getScriptBriefResultModel());
assertTrue(scriptResultModel.getScriptPagesBriefModel()
.getScriptPageBriefModels().size() >0);
.getScriptPageBriefModels().size() > 0);
}
}

View File

@ -19,7 +19,7 @@ import org.bench4q.master.domain.service.TestPlanScriptService;
import org.bench4q.master.domain.service.TestPlanService;
import org.bench4q.master.domain.service.UserService;
import org.bench4q.master.helper.SessionHelper;
import org.bench4q.master.infrastructure.communication.AgentMessenger;
import org.bench4q.master.infrastructure.communication.impl.AgentMessengerImpl;
import org.bench4q.master.infrastructure.highavailable.HighAvailablePool;
import org.bench4q.master.unitTest.controller.TestBase;
import org.bench4q.share.enums.master.TestPlanStatus;
@ -41,7 +41,7 @@ public class TestBase_MakeUpTestPlan extends TestBase {
private ScriptService scriptService;
private HighAvailablePool haPool;
private AgentRepository agentRepository;
private AgentMessenger agentMessenger;
private AgentMessengerImpl agentMessenger;
private TestPlanEngine testPlanEngine;
private UUID testPlanRunIdUuid;
private int scriptId;
@ -149,12 +149,12 @@ public class TestBase_MakeUpTestPlan extends TestBase {
this.agentRepository = agentRepository;
}
protected AgentMessenger getAgentMessenger() {
protected AgentMessengerImpl getAgentMessenger() {
return agentMessenger;
}
@Autowired
private void setAgentMessenger(AgentMessenger agentMessenger) {
private void setAgentMessenger(AgentMessengerImpl agentMessenger) {
this.agentMessenger = agentMessenger;
}

View File

@ -1,4 +1,4 @@
package org.bench4q.master.unitTest.communication;
package org.bench4q.master.unitTest.infrastructure.communication;
import static org.junit.Assert.*;
@ -6,22 +6,15 @@ import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.xml.bind.JAXBException;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.domain.entity.TestPlan;
import org.bench4q.master.domain.entity.TestPlanScript;
import org.bench4q.master.infrastructure.communication.AgentMessenger;
import org.bench4q.master.infrastructure.communication.impl.AgentMessengerImpl;
import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan;
import org.bench4q.share.enums.master.TestPlanStatus;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.RunScenarioResultModel;
import org.bench4q.share.models.agent.TestBriefStatusModel;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@ -35,15 +28,14 @@ public class Test_AgentMessenger extends TestBase_MakeUpTestPlan {
.getProperty("file.separator");
private static final String Test_HOSTNAME = "http://127.0.0.1";
private static final int Test_PORT = 6565;
private Logger logger = Logger.getLogger(Test_AgentMessenger.class);
private AgentMessenger agentMessenger;
private AgentMessengerImpl agentMessenger;
public AgentMessenger getAgentMessenger() {
public AgentMessengerImpl getAgentMessenger() {
return agentMessenger;
}
@Autowired
public void setAgentMessenger(AgentMessenger agentMessenger) {
public void setAgentMessenger(AgentMessengerImpl agentMessenger) {
this.agentMessenger = agentMessenger;
}
@ -105,37 +97,7 @@ public class Test_AgentMessenger extends TestBase_MakeUpTestPlan {
@Test
public void testGetScriptResult() throws InterruptedException,
JAXBException {
this.submitATestPlanWithOneScript();
this.getHaPool().checkAllHeartBeat();
assertEquals(Long.valueOf(500), this.getHaPool().getMaxAvailableLoad());
TestPlan testPlan = this.getTestPlanRepository().getTestPlanInDomainBy(
this.getTestPlanRunIdUuid());
testPlan.run();
assertEquals(TestPlanStatus.InRunning.name(),
testPlan.getCurrentStatus());
Thread.sleep(500);
TestPlanScript testPlanScript = testPlan.getTestPlanScripts()
.iterator().next();
Agent agent = testPlanScript.getRunningAgents().iterator().next()
.getAgent();
assertTrue(agent.getHostName().equals("133.133.12.4"));
TestBriefStatusModel testBriefStatusModel = this.getAgentMessenger()
.scriptBriefAll(agent,
UUID.fromString(testPlan.getTestPlanRunId()));
assertNotNull(testBriefStatusModel);
logger.info("testBriefStatusModel:"
+ MarshalHelper.marshal(TestBriefStatusModel.class,
testBriefStatusModel));
assertNotNull(testBriefStatusModel.getScenarioBriefModel());
assertNotNull(testBriefStatusModel.getBehaviorsBriefModel());
assertNotNull(testBriefStatusModel.getPagesBriefModel());
assertTrue(testBriefStatusModel.getBehaviorsBriefModel()
.getBehaviorBriefModels().size() > 0);
assertTrue(testBriefStatusModel.getPagesBriefModel()
.getPageBriefModels().size() > 0);
cleanUpForTestPlanRunning();
// Just test for if it can run properly;
}
}

View File

@ -1,7 +1,7 @@
package org.bench4q.master.unitTest.communication;
package org.bench4q.master.unitTest.infrastructure.communication;
import org.bench4q.master.domain.entity.Monitor;
import org.bench4q.master.infrastructure.communication.MonitorMessenger;
import org.bench4q.master.infrastructure.communication.impl.MonitorMessenger;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

View File

@ -0,0 +1,5 @@
package org.bench4q.master.unitTest.infrastructure.ha;
public class Mock_AgentMessenger {
}

View File

@ -0,0 +1,28 @@
package org.bench4q.master.unitTest.infrastructure.ha;
import static org.junit.Assert.*;
import org.bench4q.master.domain.repository.AgentRepository;
import org.bench4q.master.helper.SessionHelper;
import org.bench4q.master.infrastructure.communication.impl.AgentMessengerImpl;
import org.bench4q.master.infrastructure.highavailable.impl.HighAvailablePoolImpl;
import org.bench4q.share.communication.HttpRequester;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { HighAvailablePoolImpl.class,
AgentRepository.class, AgentMessengerImpl.class, HttpRequester.class,
SessionHelper.class })
public class Test_highAvailable {
@Autowired
private HighAvailablePoolImpl ha;
@Test
public void test_BreakDown() {
assertNotNull(this.ha);
}
}

View File

@ -2,7 +2,7 @@ package org.bench4q.master.unitTest.service;
import static org.junit.Assert.*;
import org.bench4q.master.infrastructure.communication.MonitorMessenger;
import org.bench4q.master.infrastructure.communication.impl.MonitorMessenger;
import org.bench4q.share.models.monitor.MemoryModel;
import org.bench4q.share.models.monitor.MonitorMain;
import org.bench4q.share.models.monitor.NetworkInterfaceModel;

View File

@ -8,12 +8,14 @@ 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.factory.RunningAgentFactory;
import org.bench4q.master.domain.valueobject.transaction.impl.ScriptLoadApplication;
import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ -23,6 +25,9 @@ public class Test_ScriptLoadCommand extends TestBase_MakeUpTestPlan {
private static final String Test_AGENT_HOSTNAME = "127.0.0.1";
private UUID agentRunId;
@Autowired
private RunningAgentFactory runningAgentFactory;
private UUID getAgentRunId() {
return agentRunId;
}
@ -55,7 +60,9 @@ public class Test_ScriptLoadCommand extends TestBase_MakeUpTestPlan {
.getTestPlanInDomainBy(getTestPlanRunIdUuid())
.extracSpecifiedScript(this.getScriptId());
ScriptLoadApplication scriptLoadCommand = new ScriptLoadApplication(
testPlanScript, testPlanScript.getRequireLoad());
testPlanScript, testPlanScript.getRequireLoad(),
this.getAgentMessenger(), this.runningAgentFactory,
this.getHaPool());
List<? extends RunningAgentInterface> runningAgents = null;
synchronized (this.getHaPool().getPool()) {
runningAgents = scriptLoadCommand.execute();

View File

@ -101,4 +101,10 @@ public class HighAvailableImpl implements HighAvailablePool {
}
@Override
public void remove(Agent agent) {
// TODO Auto-generated method stub
}
}