refactor the ha

refactor the ha
This commit is contained in:
coderfengyun 2014-05-07 16:40:32 +08:00
parent 13665bfe89
commit 9b56f26f5e
10 changed files with 105 additions and 74 deletions

View File

@ -19,6 +19,10 @@ public interface RunningAgentInterface {
public boolean isBreakDown();
public boolean hasSubstitute();
public void substituteOnBoard();
public abstract class AbstractRunningAgent {
public static RunningAgentInterface buildRunningAgentDBWithoutId(

View File

@ -14,7 +14,7 @@ public interface RunningScriptInterface {
public RunScenarioModel getScenario();
public void doAfterDistributeLoad(
public void doAfterApplyLoad(
List<? extends RunningAgentInterface> agentsAfterDistribute);
public Set<? extends RunningAgentInterface> getRunningAgents();

View File

@ -29,6 +29,7 @@ public class RunningAgentDB implements RunningAgentInterface {
private UUID agentRunId;
private TestPlanScript testPlanScript;
private AgentMessenger agentMessenger;
private boolean hasSubstitute;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@ -108,6 +109,15 @@ public class RunningAgentDB implements RunningAgentInterface {
this.agentMessenger = agentMessenger;
}
@Transient
public boolean hasSubstitute() {
return hasSubstitute;
}
private void setHasSubstitute(boolean hasSubstitute) {
this.hasSubstitute = hasSubstitute;
}
public static RunningAgentDB buildRunningAgentDBWithoutId(Agent agent,
int loadInUse, TestPlanScript testPlanScript, UUID agentRunId) {
RunningAgentDB runningAgentDB = new RunningAgentDB();
@ -157,4 +167,10 @@ public class RunningAgentDB implements RunningAgentInterface {
// this.getAgentMessenger().
}
public void substituteOnBoard() {
this.getTestPlanScript().applyForLoad(this.getLoadInUse());
this.distributeScriptAndParams();
this.run();
this.setHasSubstitute(true);
}
}

View File

@ -233,7 +233,7 @@ public class TestPlanScript implements RunningScriptInterface {
return true;
}
public void doAfterDistributeLoad(
public void doAfterApplyLoad(
List<? extends RunningAgentInterface> agentsAfterDistribute) {
for (RunningAgentInterface runningAgent : agentsAfterDistribute) {
this.getRunningAgentsDB().add((RunningAgentDB) runningAgent);
@ -274,7 +274,7 @@ public class TestPlanScript implements RunningScriptInterface {
scriptLoadCommand.rollBack();
return false;
}
this.doAfterDistributeLoad(runningAgents);
this.doAfterApplyLoad(runningAgents);
this.getTestPlan().update();
return true;
}

View File

@ -17,7 +17,6 @@ import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.helper.ApplicationContextHelper;
import org.bench4q.master.infrastructure.communication.AgentMessenger;
import org.bench4q.master.infrastructure.highavailable.HighAvailablePool;
import org.bench4q.master.infrastructure.highavailable.impl.AgentRunBlotter;
import org.bench4q.master.infrastructure.highavailable.impl.HighAvailablePoolImpl;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.RunScenarioResultModel;
@ -113,8 +112,7 @@ public abstract class ScriptLoadBase implements Transaction {
+ this.getRunningScript().getScriptId());
throw new RuntimeException("Running Script not valid!");
}
this.applyForLoad(getRequireLoad(), this.getRunningScript()
.getScriptId(), this.getTestPlanRunID());
this.applyForLoad(getRequireLoad(), this.getTestPlanRunID());
return this.getAgentListThisTime();
} catch (Exception e) {
@ -133,9 +131,8 @@ public abstract class ScriptLoadBase implements Transaction {
protected abstract int getRequireLoad();
private void applyForLoad(int totalRequireLoad, int scriptId,
UUID testPlanId) throws JAXBException,
ScriptLoadDistributeException {
private void applyForLoad(int totalRequireLoad, UUID testPlanId)
throws JAXBException, ScriptLoadDistributeException {
RunScenarioResultModel runScenarioResultModel = null;
int loadForRunCurrent;
@ -192,11 +189,8 @@ public abstract class ScriptLoadBase implements Transaction {
private void addAgentBlotterToHA(UUID testPlanId,
RunningAgentInterface runningAgent) {
this.getHighAvailableAgentPool()
.getAgentRunBlotters()
.put(runningAgent.getAgentRunId(),
AgentRunBlotter.buildAgentRunBlotter(testPlanId,
runningAgent));
this.getHighAvailableAgentPool().addABlotter(runningAgent);
// AgentRunBlotter.buildAgentRunBlotter(testPlanId, runningAgent));
}
public void rollBack() {
@ -208,13 +202,12 @@ public abstract class ScriptLoadBase implements Transaction {
return;
}
for (RunningAgentInterface runningAgent : this.getAgentListThisTime()) {
// TODO: there's a problem
AgentRunBlotter agentRunBlotter = this.getHighAvailableAgentPool()
.getAgentRunBlotters().get(runningAgent.getAgentRunId());
if (agentRunBlotter.getRunningAgent().isBreakDown()) {
if (runningAgent.isBreakDown()) {
} else if (stopAgentSuccess(runningAgent)) {
}
this.getAgentListThisTime().remove(runningAgent);
this.highAvailableAgentPool.removeABlotter(runningAgent
.getAgentRunId());
}
}

View File

@ -4,9 +4,9 @@ import java.util.Collection;
import java.util.Map;
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.infrastructure.highavailable.impl.AgentRunBlotter;
import org.bench4q.share.models.agent.ServerStatusModel;
public interface HighAvailablePool {
@ -14,6 +14,12 @@ public interface HighAvailablePool {
public void add(Agent agent);
public void addABlotter(RunningAgentInterface runningAgent);
public void removeABlotter(UUID agentRunId);
public int blotterSize();
public Long getMaxAvailableLoad();
public int getCurrentAvailableLoad();
@ -29,5 +35,5 @@ public interface HighAvailablePool {
public void setObserver(CurrentLoadObserver currentLoadObserver);
public Map<UUID, AgentRunBlotter> getAgentRunBlotters();
public Map<UUID, RunningAgentInterface> getAgentRunBlotters();
}

View File

@ -2,18 +2,12 @@ package org.bench4q.master.infrastructure.highavailable.impl;
import java.util.UUID;
import org.apache.log4j.Logger;
import org.bench4q.master.domain.RunningAgentInterface;
import org.bench4q.master.domain.entity.TestPlan;
import org.bench4q.master.domain.entity.TestPlanScript;
import org.bench4q.master.domain.repository.TestPlanRepository;
import org.bench4q.master.helper.ApplicationContextHelper;
public class AgentRunBlotter {
private RunningAgentInterface runningAgent;
private UUID testPlanId;
private boolean hasSubstitute;
private TestPlanRepository testPlanRepository;
public RunningAgentInterface getRunningAgent() {
return runningAgent;
@ -39,18 +33,8 @@ public class AgentRunBlotter {
this.hasSubstitute = hasSubstitute;
}
private TestPlanRepository getTestPlanRepository() {
return testPlanRepository;
}
private void setTestPlanRepository(TestPlanRepository testPlanRepository) {
this.testPlanRepository = testPlanRepository;
}
private AgentRunBlotter() {
this.setHasSubstitute(false);
this.setTestPlanRepository(ApplicationContextHelper.getContext()
.getBean(TestPlanRepository.class));
}
private AgentRunBlotter(UUID testPlanID, RunningAgentInterface runningAgent) {
@ -64,24 +48,11 @@ public class AgentRunBlotter {
return new AgentRunBlotter(testPlanID, runningAgent);
}
private void handleAfterSubstituteOn() {
this.setHasSubstitute(true);
}
void substituteOnBoard(UUID deadRunId) {
if (hasSubstitute()) {
if (this.getRunningAgent().hasSubstitute()) {
return;
}
Logger.getLogger(this.getClass()).info("enter substituteOnBoard");
TestPlan testPlan = this.getTestPlanRepository().getRunningTestPlanBy(
getTestPlanId());
if (testPlan == null) {
return;
}
final TestPlanScript testPlanScript = testPlan
.extracSpecifiedScript(getRunningAgent().getScriptId());
testPlanScript.applyForLoad(this.getRunningAgent().getLoadInUse());
handleAfterSubstituteOn();
this.getRunningAgent().substituteOnBoard();
}
}

View File

@ -26,7 +26,7 @@ public class HighAvailablePoolImpl extends CurrentLoadSubject implements
private AgentRepository agentRepository;
private Map<String, Agent> pool;
private Map<String, ServerStatusModel> agentStatusOfPreviousBeatMap;
private Map<UUID, AgentRunBlotter> agentRunBlotters;
private Map<UUID, RunningAgentInterface> agentRunBlotters;
private List<UUID> agentRunIdShouldBeSubstitute;
private long maxAvailableLoad;
private int currentAvailableLoad;
@ -76,18 +76,28 @@ public class HighAvailablePoolImpl extends CurrentLoadSubject implements
this.agentStatusOfPreviousBeatMap = agentStatusOfPreviousBeatMap;
}
public Map<UUID, AgentRunBlotter> getAgentRunBlotters() {
return agentRunBlotters;
}
private void setAgentRunBlotters(Map<UUID, AgentRunBlotter> agentRunningInfo) {
this.agentRunBlotters = agentRunningInfo;
}
// public Map<UUID, AgentRunBlotter> getAgentRunBlotters() {
// return agentRunBlotters;
// }
//
// private void setAgentRunBlotters(Map<UUID, AgentRunBlotter>
// agentRunningInfo) {
// this.agentRunBlotters = agentRunningInfo;
// }
private List<UUID> getAgentRunIdShouldBeSubstitute() {
return agentRunIdShouldBeSubstitute;
}
public Map<UUID, RunningAgentInterface> getAgentRunBlotters() {
return agentRunBlotters;
}
private void setAgentRunBlotters(
Map<UUID, RunningAgentInterface> agentRunBlotters) {
this.agentRunBlotters = agentRunBlotters;
}
private void setAgentRunIdShouldBeSubstitute(
List<UUID> agentRunIdShouldBeSubstitute) {
this.agentRunIdShouldBeSubstitute = agentRunIdShouldBeSubstitute;
@ -95,7 +105,7 @@ public class HighAvailablePoolImpl extends CurrentLoadSubject implements
public HighAvailablePoolImpl() {
this.setPool(new HashMap<String, Agent>());
this.setAgentRunBlotters(new HashMap<UUID, AgentRunBlotter>());
this.setAgentRunBlotters(new HashMap<UUID, RunningAgentInterface>());
this.setAgentStatusOfPreviousBeatMap(new HashMap<String, ServerStatusModel>());
this.setCurrentAvailableLoad(0);
this.setAgentRunIdShouldBeSubstitute(new LinkedList<UUID>());
@ -113,13 +123,13 @@ public class HighAvailablePoolImpl extends CurrentLoadSubject implements
private void doSubstituteIfRequired() {
for (UUID agentRunId : this.getAgentRunIdShouldBeSubstitute()) {
AgentRunBlotter agentRunBlotter = this.getAgentRunBlotters().get(
agentRunId);
if (agentRunBlotter == null) {
RunningAgentInterface runningAgent = this.getAgentRunBlotters()
.get(agentRunId);
if (runningAgent == null) {
// TODO:Actually this is an error
continue;
}
agentRunBlotter.substituteOnBoard(agentRunId);
runningAgent.substituteOnBoard();
}
}
@ -163,6 +173,19 @@ public class HighAvailablePoolImpl extends CurrentLoadSubject implements
return agent.queryStatus();
}
public void addABlotter(RunningAgentInterface runningAgent) {
this.getAgentRunBlotters().put(runningAgent.getAgentRunId(),
runningAgent);
}
public void removeABlotter(UUID agentRunId) {
this.getAgentRunBlotters().remove(agentRunId);
}
public int blotterSize() {
return this.getAgentRunBlotters().size();
}
private void doForBreakDown(Agent agent) {
updateAgentStatus(AgentService.AGENT_STATUS_BreakDown, agent);
List<UUID> runIDs = queryUnfinishedTest(null, agent.getHostName());

View File

@ -5,9 +5,9 @@ import static org.junit.Assert.*;
import java.util.Map;
import java.util.UUID;
import org.bench4q.master.domain.RunningAgentInterface;
import org.bench4q.master.domain.entity.TestPlan;
import org.bench4q.master.domain.entity.TestPlanScript;
import org.bench4q.master.infrastructure.highavailable.impl.AgentRunBlotter;
import org.bench4q.master.test.TestBase_MakeUpTestPlan;
import org.junit.After;
import org.junit.Before;
@ -30,6 +30,7 @@ public class Test_HighAvailable extends TestBase_MakeUpTestPlan {
}
@Deprecated
@Test
public void testSubstituteOnBoard() throws InterruptedException {
submitATestPlanWithOneScript();
@ -45,20 +46,19 @@ public class Test_HighAvailable extends TestBase_MakeUpTestPlan {
getTestPlanRunIdUuid());
TestPlanScript testPlanScriptBeforeSubmit = testPlan
.extracSpecifiedScript(getScriptId());
Map<UUID, AgentRunBlotter> agentRunBlottersBeforeKill = this
Map<UUID, RunningAgentInterface> agentRunBlottersBeforeKill = this
.getHaPool().getAgentRunBlotters();
assertEquals(1, agentRunBlottersBeforeKill.size());
assertEquals(1, this.getHaPool().blotterSize());
assertEquals(1, testPlanScriptBeforeSubmit.getSampler()
.getRunningAgents().size());
this.getHaPool().checkAllHeartBeat();
for (AgentRunBlotter agentRunBlotter : agentRunBlottersBeforeKill
for (RunningAgentInterface runingAgent : agentRunBlottersBeforeKill
.values()) {
while (this.getAgentMessenger().askLiving(
agentRunBlotter.getRunningAgent().getAgent().getHostName(),
6565) != null) {
runingAgent.getAgent().getHostName(), 6565) != null) {
System.out.println("This need agent "
+ agentRunBlotter.getRunningAgent().getAgent()
.getHostName() + " to be killed");
+ runingAgent.getAgent().getHostName()
+ " to be killed");
Thread.sleep(1000);
}
}

View File

@ -5,11 +5,11 @@ import java.util.HashMap;
import java.util.Map;
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.infrastructure.highavailable.CurrentLoadObserver;
import org.bench4q.master.infrastructure.highavailable.HighAvailablePool;
import org.bench4q.master.infrastructure.highavailable.impl.AgentRunBlotter;
import org.bench4q.share.models.agent.ServerStatusModel;
public class HighAvailableImpl implements HighAvailablePool {
@ -78,9 +78,27 @@ public class HighAvailableImpl implements HighAvailablePool {
}
@Override
public Map<UUID, AgentRunBlotter> getAgentRunBlotters() {
public Map<UUID, RunningAgentInterface> getAgentRunBlotters() {
// TODO Auto-generated method stub
return null;
}
@Override
public void removeABlotter(UUID agentRunId) {
// TODO Auto-generated method stub
}
@Override
public int blotterSize() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void addABlotter(RunningAgentInterface runningAgent) {
// TODO Auto-generated method stub
}
}