refactor agentService ing

This commit is contained in:
coderfengyun 2014-02-20 21:18:08 +08:00
parent a00c584ed0
commit 1063dc1256
9 changed files with 402 additions and 154 deletions

View File

@ -15,13 +15,13 @@ import org.bench4q.master.domain.RunningScript;
import org.bench4q.master.domain.TestPlanContext;
import org.bench4q.master.domain.TestPlanInBusiness;
import org.bench4q.master.domain.entity.TestPlanDB;
import org.bench4q.master.domain.service.TestPlanEngine;
import org.bench4q.master.domain.service.TestPlanScriptResultService;
import org.bench4q.master.domain.service.TestPlanService;
import org.bench4q.master.domain.service.UserService;
import org.bench4q.master.exception.Bench4QException;
import org.bench4q.master.report.ReportService;
import org.bench4q.master.testplan.TestPlanContainer;
import org.bench4q.master.testplan.TestPlanEngine;
import org.bench4q.share.enums.master.TestPlanStatus;
import org.bench4q.share.models.master.MonitorModel;
import org.bench4q.share.models.master.RunningScriptModel;

View File

@ -11,10 +11,10 @@ import java.util.UUID;
import org.apache.log4j.Logger;
import org.bench4q.master.domain.service.AgentService;
import org.bench4q.master.domain.service.ScriptService;
import org.bench4q.master.domain.service.TestPlanEngine;
import org.bench4q.master.domain.service.TestPlanService;
import org.bench4q.master.helper.ApplicationContextHelper;
import org.bench4q.master.infrastructure.communication.AgentMessenger;
import org.bench4q.master.testplan.TestPlanEngine;
import org.bench4q.master.testplan.datastatistics.BehaviorsBriefStatistics;
import org.bench4q.master.testplan.datastatistics.DataStatistics;
import org.bench4q.master.testplan.datastatistics.PagesBriefStatistics;

View File

@ -7,6 +7,8 @@ import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.bench4q.master.domain.service.AgentService;
@Entity
@Table(name = "agent")
public class Agent {
@ -73,4 +75,13 @@ public class Agent {
this.currentStatus = currentStatus;
}
public static Agent createAgentWithoutId(String hostName, int port) {
Agent agent = new Agent();
agent.setCurrentStatus(AgentService.AGENT_STATUS_Idel);
agent.setHostName(hostName);
agent.setMaxLoad(500);
agent.setPort(port);
agent.setRemainLoad(500);
return agent;
}
}

View File

@ -0,0 +1,97 @@
package org.bench4q.master.domain.repository;
import java.util.List;
import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.exception.ExceptionLog;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
@Component
public class AgentRepository extends AbstractRepositoty {
public boolean attach(Agent agentWithoutId) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
Agent agent = (Agent) session
.createCriteria(Agent.class)
.add(Restrictions.eq("hostName",
agentWithoutId.getHostName())).uniqueResult();
if (agent != null) {
return false;
}
session.merge(agentWithoutId);
transaction.commit();
return true;
} catch (Exception e) {
transaction.rollback();
return false;
} finally {
releaseSession(session);
}
}
public boolean detach(int id) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
Agent agent = (Agent) session.get(Agent.class, id);
if (agent == null) {
return false;
}
session.delete(agent);
transaction.commit();
return true;
} catch (Exception e) {
transaction.rollback();
logger.error(ExceptionLog.getStackTrace(e));
return false;
}
}
public boolean update(Agent agentForUpdate) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
session.update(agentForUpdate);
transaction.commit();
return true;
} catch (Exception e) {
transaction.rollback();
logger.error(ExceptionLog.getStackTrace(e));
return false;
} finally {
releaseSession(session);
}
}
public Agent getAgentBy(String hostName) {
Session session = this.getSessionHelper().openSession();
try {
Agent agent = (Agent) session.createCriteria(Agent.class)
.add(Restrictions.eq("hostName", hostName)).uniqueResult();
return agent;
} catch (Exception e) {
return null;
} finally {
releaseSession(session);
}
}
@SuppressWarnings("unchecked")
public List<Agent> loadEntities() {
Session session = this.getSessionHelper().openSession();
try {
return session.createCriteria(Agent.class)
.addOrder(Order.desc("remainLoad")).list();
} catch (Exception e) {
return null;
} finally {
releaseSession(session);
}
}
}

View File

@ -1,15 +1,14 @@
package org.bench4q.master.domain.service;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.domain.repository.AgentRepository;
import org.bench4q.master.helper.SessionHelper;
import org.bench4q.master.testplan.highavailable.HighAvailablePool;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -21,6 +20,7 @@ public class AgentService {
public static int AGENT_STATUS_BackUp = 3;
public static int AGENT_STATUS_BreakDown = 4;
private SessionHelper sessionHelper;
private AgentRepository agentRepository;
private Object AGENT_LOCK;
private HighAvailablePool highAvailablePool;
private static Logger logger = Logger.getLogger(AgentService.class);
@ -47,6 +47,15 @@ public class AgentService {
this.highAvailablePool = highAvailablePool;
}
private AgentRepository getAgentRepository() {
return agentRepository;
}
@Autowired
private void setAgentRepository(AgentRepository agentRepository) {
this.agentRepository = agentRepository;
}
public Object getAgentLock() {
return AGENT_LOCK;
}
@ -55,87 +64,27 @@ public class AgentService {
this.AGENT_LOCK = object;
}
public boolean addAgentToPool(Agent agentInParam) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
Agent agent = (Agent) session
.createCriteria(Agent.class)
.add(Restrictions.eq("hostName", agentInParam.getHostName()))
.uniqueResult();
if (agent != null) {
return false;
}
agent = new Agent();
agent.setHostName(agentInParam.getHostName());
agent.setMaxLoad(agentInParam.getMaxLoad());
agent.setPort(agentInParam.getPort());
agent.setRemainLoad(agentInParam.getRemainLoad());
agent.setCurrentStatus(AGENT_STATUS_Idel);
session.merge(agent);
transaction.commit();
this.getHighAvailablePool().getPool()
.put(agent.getHostName(), agent);
return true;
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
public boolean addAgentToPool(Agent agentWithoutId) {
if (!this.getAgentRepository().attach(
Agent.createAgentWithoutId(agentWithoutId.getHostName(),
agentWithoutId.getPort()))) {
return false;
} finally {
if (session != null) {
session.close();
}
}
Agent agent = this.getAgentRepository().getAgentBy(
agentWithoutId.getHostName());
if (agent == null) {
return false;
}
this.getHighAvailablePool().getPool().put(agent.getHostName(), agent);
return true;
}
public boolean removeAgentFromPool(int agentId) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
Agent agent = (Agent) session.createCriteria(Agent.class)
.add(Restrictions.eq("id", agentId)).uniqueResult();
if (agent == null) {
return false;
}
session.delete(agent);
transaction.commit();
this.getHighAvailablePool().getPool().remove(agent.getHostName());
return true;
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
return false;
} finally {
if (session != null) {
session.close();
}
}
}
public boolean recomputeRemainLoadOfAgent(Agent agent) {
return false;
return this.getAgentRepository().detach(agentId);
}
public List<Agent> loadAgentPoolFromDB() {
return this.getAgentList();
}
@SuppressWarnings("unchecked")
private List<Agent> getAgentList() {
List<Agent> agentList = new ArrayList<Agent>();
Session session = this.getSessionHelper().openSession();
try {
agentList = session.createCriteria(Agent.class)
.addOrder(Order.desc("remainLoad")).list();
return agentList;
} catch (Exception e) {
e.printStackTrace();
return agentList;
} finally {
if (session != null) {
session.close();
}
}
return this.getAgentRepository().loadEntities();
}
public boolean backLoadToAgent(String hostName, int backLoad) {
@ -144,7 +93,7 @@ public class AgentService {
try {
Agent agent = (Agent) session.createCriteria(Agent.class)
.add(Restrictions.eq("hostName", hostName)).uniqueResult();
if (agent == null) {
if (!isValidBackLoad(agent, backLoad) || agent == null) {
return false;
}
agent.setRemainLoad(agent.getRemainLoad() + backLoad);
@ -164,30 +113,23 @@ public class AgentService {
}
}
private boolean isValidBackLoad(Agent agentToOperate, int backLoad) {
return backLoad >= 0
&& agentToOperate.getMaxLoad() - agentToOperate.getRemainLoad() >= backLoad;
}
public boolean getLoadFromAgent(String hostName, int requireLoad) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
Agent agent = (Agent) session.createCriteria(Agent.class)
.add(Restrictions.eq("hostName", hostName)).uniqueResult();
if (agent == null) {
return false;
}
agent.setRemainLoad(agent.getRemainLoad() - requireLoad);
session.merge(agent);
transaction.commit();
logger.info("agent with hostname " + agent.getHostName()
+ "remain load is " + agent.getRemainLoad());
return true;
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
Agent agent = this.getAgentRepository().getAgentBy(hostName);
if (agent == null || !isValidRequireLoad(agent, requireLoad)) {
return false;
} finally {
if (session != null) {
session.close();
}
}
agent.setRemainLoad(agent.getRemainLoad() - requireLoad);
return this.getAgentRepository().update(agent);
}
private boolean isValidRequireLoad(Agent agentToOperate, int requireLoad) {
return requireLoad >= 0
&& agentToOperate.getRemainLoad() >= requireLoad;
}
public boolean resetAgent(Agent agentInParam) {

View File

@ -1,4 +1,4 @@
package org.bench4q.master.testplan;
package org.bench4q.master.domain.service;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
@ -9,7 +9,8 @@ import org.bench4q.master.domain.RunningScript;
import org.bench4q.master.domain.TestPlanContext;
import org.bench4q.master.domain.TestPlanInBusiness;
import org.bench4q.master.domain.entity.User;
import org.bench4q.master.domain.service.TestPlanService;
import org.bench4q.master.testplan.LoadDistribute;
import org.bench4q.master.testplan.TestPlanContainer;
import org.bench4q.master.testplan.highavailable.CurrentLoadObserver;
import org.bench4q.master.testplan.highavailable.HighAvailablePool;
import org.bench4q.master.testplan.schedulscript.TaskCompleteCallback;

View File

@ -1,53 +1,53 @@
package org.bench4q.master.testplan.schedulscript;
import java.util.TimerTask;
import org.bench4q.master.domain.RunningScript;
import org.bench4q.master.helper.ApplicationContextHelper;
import org.bench4q.master.testplan.TestPlanEngine;
/**
*
* @author coderfengyun
*
* @description This Task will do two things: one is to remove the runningAgent
* from testPlanContext, and the other is to remove the
* runningAgentFrom HA Blotters
*/
public class ExecutionOverTask extends TimerTask {
private RunningScript runningScript;
private TaskCompleteCallback taskCompleteCallback;
public RunningScript getRunningScript() {
return runningScript;
}
public void setRunningScriptModel(RunningScript runningScript) {
this.runningScript = runningScript;
}
private TaskCompleteCallback getTaskCompleteCallback() {
return taskCompleteCallback;
}
private void setTaskCompleteCallback(
TaskCompleteCallback taskCompleteCallback) {
this.taskCompleteCallback = taskCompleteCallback;
}
public ExecutionOverTask(RunningScript runningScript) {
this.setRunningScriptModel(runningScript);
this.setTaskCompleteCallback(ApplicationContextHelper.getContext()
.getBean(TestPlanEngine.class));
}
@Override
public void run() {
this.getRunningScript().doForComplete();
// 2: do for runningAgent in HA blotters, And TestPlanContainer
this.getTaskCompleteCallback().doTaskComplete(
this.getRunningScript().getTestPlanID());
System.out.println("execute ExecutionOverTask");
}
}
package org.bench4q.master.testplan.schedulscript;
import java.util.TimerTask;
import org.bench4q.master.domain.RunningScript;
import org.bench4q.master.domain.service.TestPlanEngine;
import org.bench4q.master.helper.ApplicationContextHelper;
/**
*
* @author coderfengyun
*
* @description This Task will do two things: one is to remove the runningAgent
* from testPlanContext, and the other is to remove the
* runningAgentFrom HA Blotters
*/
public class ExecutionOverTask extends TimerTask {
private RunningScript runningScript;
private TaskCompleteCallback taskCompleteCallback;
public RunningScript getRunningScript() {
return runningScript;
}
public void setRunningScriptModel(RunningScript runningScript) {
this.runningScript = runningScript;
}
private TaskCompleteCallback getTaskCompleteCallback() {
return taskCompleteCallback;
}
private void setTaskCompleteCallback(
TaskCompleteCallback taskCompleteCallback) {
this.taskCompleteCallback = taskCompleteCallback;
}
public ExecutionOverTask(RunningScript runningScript) {
this.setRunningScriptModel(runningScript);
this.setTaskCompleteCallback(ApplicationContextHelper.getContext()
.getBean(TestPlanEngine.class));
}
@Override
public void run() {
this.getRunningScript().doForComplete();
// 2: do for runningAgent in HA blotters, And TestPlanContainer
this.getTaskCompleteCallback().doTaskComplete(
this.getRunningScript().getTestPlanID());
System.out.println("execute ExecutionOverTask");
}
}

View File

@ -0,0 +1,42 @@
package org.bench4q.master.test.repository;
import static org.junit.Assert.*;
import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.domain.repository.AgentRepository;
import org.bench4q.master.domain.service.AgentService;
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(locations = { "classpath:repository-test-context.xml" })
public class Test_AgentRepository {
private AgentRepository agentRepoitory;
private AgentRepository getAgentRepoitory() {
return agentRepoitory;
}
@Autowired
private void setAgentRepoitory(AgentRepository agentRepoitory) {
this.agentRepoitory = agentRepoitory;
}
@Test
public void testAddAgent() {
String hostNameForTest = "133.133.12.95";
int portForTest = 6565;
assertTrue(this.getAgentRepoitory().attach(
Agent.createAgentWithoutId(hostNameForTest, portForTest)));
Agent agent = this.getAgentRepoitory().getAgentBy(hostNameForTest);
assertNotNull(agent);
assertEquals(hostNameForTest, agent.getHostName());
assertEquals(portForTest, agent.getPort());
assertEquals(500, agent.getMaxLoad());
assertEquals(AgentService.AGENT_STATUS_Idel, agent.getCurrentStatus());
assertTrue(this.getAgentRepoitory().detach(agent.getId()));
}
}

View File

@ -0,0 +1,155 @@
package org.bench4q.master.test.service;
import static org.junit.Assert.*;
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.testplan.highavailable.HighAvailablePool;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:service-test-context.xml" })
public class Test_AgentService {
private static final String hostNameForTest = "133.133.12.5";
private AgentService agentService;
private AgentRepository agentRepoitory;
private HighAvailablePool highAvailablePool;
private AgentService getAgentService() {
return agentService;
}
@Autowired
private void setAgentService(AgentService agentService) {
this.agentService = agentService;
}
private AgentRepository getAgentRepoitory() {
return agentRepoitory;
}
@Autowired
private void setAgentRepoitory(AgentRepository agentRepoitory) {
this.agentRepoitory = agentRepoitory;
}
private HighAvailablePool getHighAvailablePool() {
return highAvailablePool;
}
@Autowired
private void setHighAvailablePool(HighAvailablePool highAvailablePool) {
this.highAvailablePool = highAvailablePool;
}
@Test
public void testAddAgentToPool() {
Agent agent = getAgentForTest();
assertNotNull(this.getHighAvailablePool().getPool()
.get(hostNameForTest));
assertNotNull(agent);
}
private Agent getAgentForTest() {
return this.getAgentRepoitory().getAgentBy(hostNameForTest);
}
private boolean doAddAgent() {
return this.getAgentService().addAgentToPool(
Agent.createAgentWithoutId(hostNameForTest, 6565));
}
@Before
public void prepare() {
doAddAgent();
}
@After
public void cleanUp() {
this.getAgentService().removeAgentFromPool(getAgentForTest().getId());
}
@Test
public void tsetRemoveAgentFromPool() {
Agent agent = getAgentForTest();
assertTrue(agent != null);
assertTrue(this.getAgentService().removeAgentFromPool(agent.getId()));
assertNull(getAgentForTest());
doAddAgent();
}
@Test
public void testLoadAgentPoolFromDB() {
this.getAgentService().removeAgentFromPool(getAgentForTest().getId());
int sizeBeforeAdd = this.getAgentService().loadAgentPoolFromDB().size();
doAddAgent();
int sizeAfterAdd = this.getAgentService().loadAgentPoolFromDB().size();
assertEquals(sizeBeforeAdd + 1, sizeAfterAdd);
}
@Test
public void testGetLoadFromAgentWhereRequireLoad_LT_RemainLoad() {
doAddAgent();
assertTrue(this.getAgentService()
.getLoadFromAgent(hostNameForTest, 100));
assertEquals(400, getAgentForTest().getRemainLoad());
}
@Test
public void testGetLoadFromAgentWhereRequireLoad_GT_RemainLoad() {
int initialRemainLoad = getAgentForTest().getRemainLoad();
assertFalse(this.getAgentService().getLoadFromAgent(hostNameForTest,
initialRemainLoad + 100));
assertEquals(initialRemainLoad, getAgentForTest().getRemainLoad());
}
@Test
public void testGetLoadFromAgentWhereRequireLoad_LT_Zero() {
assertFalse(this.getAgentService().getLoadFromAgent(hostNameForTest,
-100));
assertEquals(getAgentForTest().getMaxLoad(), getAgentForTest()
.getRemainLoad());
}
@Test
public void testBackLoadFromAgentWhereBackLoad_Plus_RemainLoad__LE_MaxLoad() {
Agent agent = getAgentForTest();
int initialDifferenceBetweenMaxLoadAndRemainLoad = agent.getMaxLoad()
- agent.getRemainLoad();
assertTrue(this.getAgentService().backLoadToAgent(hostNameForTest,
initialDifferenceBetweenMaxLoadAndRemainLoad));
assertEquals(getAgentForTest().getMaxLoad(), getAgentForTest()
.getRemainLoad());
}
@Test
public void testBackLoadToAgentWhereBackLoad_Plus_RemainLoad_GE_MaxLoad() {
Agent agent = getAgentForTest();
int initialDifferenceBetweenMaxLoadAndRemainLoad = agent.getMaxLoad()
- agent.getRemainLoad();
assertTrue(initialDifferenceBetweenMaxLoadAndRemainLoad >= 0);
assertFalse(this.getAgentService().backLoadToAgent(hostNameForTest,
initialDifferenceBetweenMaxLoadAndRemainLoad + 100));
assertEquals(initialDifferenceBetweenMaxLoadAndRemainLoad,
getAgentForTest().getMaxLoad()
- getAgentForTest().getRemainLoad());
}
@Test
public void testBackLoadToAgentWhereBackLoad_LT_Zero() {
int initialDifferenceBetweenMaxLoadAndRemainLoad = getAgentForTest()
.getMaxLoad() - getAgentForTest().getRemainLoad();
assertFalse(this.getAgentService().backLoadToAgent(hostNameForTest,
-100));
assertEquals(initialDifferenceBetweenMaxLoadAndRemainLoad,
getAgentForTest().getMaxLoad()
- getAgentForTest().getRemainLoad());
}
}