remove the direct dependency towards ha

remove the direct dependency towards ha
This commit is contained in:
coderfengyun 2014-05-06 09:38:13 +08:00
parent 6110e4eba8
commit f524e36a2f
11 changed files with 284 additions and 25 deletions

View File

@ -3,6 +3,9 @@ package org.bench4q.agent.test;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties; import java.util.Properties;
import org.bench4q.agent.Main; import org.bench4q.agent.Main;
@ -151,4 +154,205 @@ public class MainTest {
} }
return num; return num;
} }
@Test
public void testRegular() {
assertTrue(isMatch("aab", "c*a*b"));
assertTrue(isMatch("aaaa", ".*"));
assertTrue(isMatch("aa", "a*"));
assertFalse(isMatch("aaaa", "aa"));
assertTrue(isMatch("aaa", "a.a"));
assertTrue(isMatch("aaa", "a*a"));
assertFalse(isMatch("aaba", "ab*a*c*a"));
assertTrue(isMatch("", "a*"));
assertTrue(isMatch("aasdfasdfasdfasdfas", "aasdf.*asdf.*asdf.*asdf.*s"));
assertFalse(isMatch("", "..*"));
assertTrue(isMatch("ba", ".*."));
}
public boolean isMatch(String s, String p) {
if (s == null || p == null) {
return false;
}
if (p.length() == 0 && s.length() == 0) {
return true;
}
List<RegularPart> regularParts = getRegularParts(p);
List<StartPointRange> startPointRanges = buildOneItemList(new StartPointRange(
0, 0));
for (int i = 0; i < regularParts.size(); i++) {
startPointRanges = matchForNextStartRanges(s, startPointRanges,
regularParts.get(i));
if (startPointRanges.size() == 0) {
return false;
}
}
if (startPointRanges.get(startPointRanges.size() - 1).end != s.length()) {
return false;
}
return true;
}
private List<StartPointRange> matchForNextStartRanges(String toBeMatched,
List<StartPointRange> startPointRanges, RegularPart regularPart) {
List<StartPointRange> resut = new ArrayList<StartPointRange>();
for (StartPointRange startRange : startPointRanges) {
resut.addAll(matchForNextStartRange(toBeMatched, startRange,
regularPart));
}
return resut;
}
private List<StartPointRange> matchForNextStartRange(String toBeMatched,
StartPointRange thisRange, RegularPart regularPart) {
List<StartPointRange> result = new ArrayList<StartPointRange>();
if (!regularPart.hasAsterisk) {
if (regularPart.regularContent.equals(".")) {
if (thisRange.begin > toBeMatched.length() - 1) {
return new ArrayList<StartPointRange>();
}
return buildOneItemList(new StartPointRange(
thisRange.begin + 1, thisRange.end + 1));
} else {
return doForNotAsteriskNotDot(toBeMatched, thisRange,
regularPart);
}
} else {
if (regularPart.regularContent.equals(".")) {
return buildOneItemList(new StartPointRange(thisRange.begin,
toBeMatched.length() - 1));
} else {
result.addAll(doForAsteriskNotDot(toBeMatched, thisRange,
regularPart));
return result;
}
}
}
private List<StartPointRange> doForNotAsteriskNotDot(String toBeMatched,
StartPointRange thisRange, RegularPart regularPart) {
List<StartPointRange> result = new ArrayList<StartPointRange>();
for (int i = thisRange.begin; i <= thisRange.end; i++) {
int endIndex = i + regularPart.regularContent.length();
if (endIndex > toBeMatched.length()) {
break;
}
String substring = toBeMatched.substring(i, endIndex);
if (substring.equals(regularPart.regularContent)) {
result.add(new StartPointRange(i + 1, endIndex));
}
}
return result;
}
private List<StartPointRange> doForAsteriskNotDot(String toBeMatched,
StartPointRange thisRange, RegularPart regularPart) {
List<StartPointRange> result = new ArrayList<StartPointRange>();
int begin = -1, end = -1;
for (int i = thisRange.begin; i < toBeMatched.length(); i++) {
if (toBeMatched.charAt(i) == regularPart.regularContent.charAt(0)) {
if (begin == -1 && i <= thisRange.end) {
begin = i;
}
} else {
if (begin == -1) {
continue;
}
end = i;
result.add(new StartPointRange(begin, end));
begin = -1;
end = -1;
if (i > thisRange.end) {
break;
}
}
}
if (begin > -1 && end == -1) {
result.add(new StartPointRange(begin, toBeMatched.length()));
}
if (result.size() == 0) {
result.add(thisRange);
}
return result;
}
private List<StartPointRange> buildOneItemList(
StartPointRange startPointRange) {
List<StartPointRange> result = new ArrayList<StartPointRange>();
result.add(startPointRange);
return result;
}
/**
* It do it in a reverse way
*
* @param regularExpression
* @return
*/
private List<RegularPart> getRegularParts(String regularExpression) {
List<RegularPart> result = new ArrayList<RegularPart>();
if (regularExpression == null || regularExpression.isEmpty()) {
return result;
}
int index = regularExpression.length() - 1;
char[] array = regularExpression.toCharArray();
StringBuffer temp = new StringBuffer();
for (; index >= 0; index--) {
if (array[index] == '*') {
if (index == 0) {
temp.append(array[index]);
} else {
if (temp.length() != 0) {
result.add(new RegularPart(temp.reverse().toString(),
false));
temp.delete(0, temp.length());
}
result.add(new RegularPart(new String(
new char[] { array[index - 1] }), true));
index--;
}
} else if (array[index] == '.') {
if (temp.length() != 0) {
result.add(new RegularPart(temp.reverse().toString(), false));
temp.delete(0, temp.length());
}
result.add(new RegularPart(new String(
new char[] { array[index] }), false));
} else {
temp.append(array[index]);
}
}
if (temp.length() > 0) {
result.add(new RegularPart(temp.reverse().toString(), false));
}
Collections.reverse(result);
return result;
}
static class StartPointRange {
int begin;
int end;
StartPointRange() {
StartPointRange.this.begin = -1;
StartPointRange.this.end = -1;
}
StartPointRange(int begin, int end) {
StartPointRange.this.begin = begin;
StartPointRange.this.end = end;
}
}
static class RegularPart {
public String regularContent;
public boolean hasAsterisk;
RegularPart(String content, boolean hasAsterisk) {
this.regularContent = content;
this.hasAsterisk = hasAsterisk;
}
}
} }

View File

@ -4,7 +4,7 @@ import java.util.List;
import org.bench4q.master.domain.entity.Agent; import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.domain.repository.AgentRepository; import org.bench4q.master.domain.repository.AgentRepository;
import org.bench4q.master.infrastructure.highavailable.impl.HighAvailablePoolImpl; import org.bench4q.master.infrastructure.highavailable.HighAvailablePool;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -16,18 +16,18 @@ public class AgentService {
public static int AGENT_STATUS_BreakDown = 4; public static int AGENT_STATUS_BreakDown = 4;
private AgentRepository agentRepository; private AgentRepository agentRepository;
private Object AGENT_LOCK; private Object AGENT_LOCK;
private HighAvailablePoolImpl highAvailablePool; private HighAvailablePool highAvailablePool;
public AgentService() { public AgentService() {
this.setAgentLock(new Object()); this.setAgentLock(new Object());
} }
private HighAvailablePoolImpl getHighAvailablePool() { private HighAvailablePool getHighAvailablePool() {
return highAvailablePool; return highAvailablePool;
} }
@Autowired @Autowired
private void setHighAvailablePool(HighAvailablePoolImpl highAvailablePool) { private void setHighAvailablePool(HighAvailablePool highAvailablePool) {
this.highAvailablePool = highAvailablePool; this.highAvailablePool = highAvailablePool;
} }

View File

@ -20,7 +20,7 @@ import org.bench4q.master.domain.valueobject.schedulscript.TaskCompleteCallback;
import org.bench4q.master.exception.ExceptionLog; import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.exception.ExceptionUtils.IllegalParameterException; import org.bench4q.master.exception.ExceptionUtils.IllegalParameterException;
import org.bench4q.master.infrastructure.highavailable.CurrentLoadObserver; import org.bench4q.master.infrastructure.highavailable.CurrentLoadObserver;
import org.bench4q.master.infrastructure.highavailable.impl.HighAvailablePoolImpl; import org.bench4q.master.infrastructure.highavailable.HighAvailablePool;
import org.bench4q.share.enums.master.TestPlanStatus; import org.bench4q.share.enums.master.TestPlanStatus;
import org.bench4q.share.models.master.TestPlanModel; import org.bench4q.share.models.master.TestPlanModel;
import org.hibernate.criterion.Criterion; import org.hibernate.criterion.Criterion;
@ -32,7 +32,7 @@ import org.springframework.stereotype.Component;
@Component @Component
public class TestPlanEngine implements TaskCompleteCallback, public class TestPlanEngine implements TaskCompleteCallback,
CurrentLoadObserver { CurrentLoadObserver {
private HighAvailablePoolImpl haPool; private HighAvailablePool haPool;
private TestPlanRepository testPlanRepository; private TestPlanRepository testPlanRepository;
private TestPlanFactory testPlanFactory; private TestPlanFactory testPlanFactory;
private ScheduledExecutorService scheduleExecutor; private ScheduledExecutorService scheduleExecutor;
@ -57,12 +57,12 @@ public class TestPlanEngine implements TaskCompleteCallback,
this.testPlanFactory = testPlanFactory; this.testPlanFactory = testPlanFactory;
} }
public HighAvailablePoolImpl getHaPool() { private HighAvailablePool getHaPool() {
return haPool; return haPool;
} }
@Autowired @Autowired
private void setHaPool(HighAvailablePoolImpl haPool) { private void setHaPool(HighAvailablePool haPool) {
this.haPool = haPool; this.haPool = haPool;
this.getHaPool().setObserver(this); this.getHaPool().setObserver(this);
} }

View File

@ -16,18 +16,20 @@ import org.bench4q.master.domain.valueobject.transaction.exception.ScriptLoadDis
import org.bench4q.master.exception.ExceptionLog; import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.helper.ApplicationContextHelper; import org.bench4q.master.helper.ApplicationContextHelper;
import org.bench4q.master.infrastructure.communication.AgentMessenger; 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.AgentRunBlotter;
import org.bench4q.master.infrastructure.highavailable.impl.HighAvailablePoolImpl; import org.bench4q.master.infrastructure.highavailable.impl.HighAvailablePoolImpl;
import org.bench4q.share.models.agent.RunScenarioModel; import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.RunScenarioResultModel; import org.bench4q.share.models.agent.RunScenarioResultModel;
import org.bench4q.share.models.agent.StopTestModel; import org.bench4q.share.models.agent.StopTestModel;
import org.springframework.beans.factory.annotation.Autowired;
public abstract class ScriptLoadBase implements Transaction { public abstract class ScriptLoadBase implements Transaction {
private RunningScriptInterface runningScript; private RunningScriptInterface runningScript;
private UUID testPlanRunID; private UUID testPlanRunID;
private List<RunningAgentInterface> agentListThisTime; private List<RunningAgentInterface> agentListThisTime;
private AgentMessenger runningAgentService; private AgentMessenger runningAgentService;
private HighAvailablePoolImpl highAvailableAgentPool; private HighAvailablePool highAvailableAgentPool;
private static Logger logger = Logger private static Logger logger = Logger
.getLogger(ScriptLoadApplication.class); .getLogger(ScriptLoadApplication.class);
@ -64,12 +66,13 @@ public abstract class ScriptLoadBase implements Transaction {
this.runningAgentService = runningAgentService; this.runningAgentService = runningAgentService;
} }
private HighAvailablePoolImpl getHighAvailableAgentPool() { private HighAvailablePool getHighAvailableAgentPool() {
return highAvailableAgentPool; return highAvailableAgentPool;
} }
@Autowired
private void setHighAvailableAgentPool( private void setHighAvailableAgentPool(
HighAvailablePoolImpl highAvailableAgentPool) { HighAvailablePool highAvailableAgentPool) {
this.highAvailableAgentPool = highAvailableAgentPool; this.highAvailableAgentPool = highAvailableAgentPool;
} }

View File

@ -5,21 +5,22 @@ import org.bench4q.master.domain.entity.TestPlan;
import org.bench4q.master.domain.entity.TestPlanScript; import org.bench4q.master.domain.entity.TestPlanScript;
import org.bench4q.master.domain.valueobject.transaction.Transaction; import org.bench4q.master.domain.valueobject.transaction.Transaction;
import org.bench4q.master.helper.ApplicationContextHelper; import org.bench4q.master.helper.ApplicationContextHelper;
import org.bench4q.master.infrastructure.highavailable.HighAvailablePool;
import org.bench4q.master.infrastructure.highavailable.impl.HighAvailablePoolImpl; import org.bench4q.master.infrastructure.highavailable.impl.HighAvailablePoolImpl;
import org.bench4q.share.enums.master.TestPlanStatus; import org.bench4q.share.enums.master.TestPlanStatus;
public class TestPlanLoadApplication implements Transaction { public class TestPlanLoadApplication implements Transaction {
private TestPlan testPlan; private TestPlan testPlan;
private HighAvailablePoolImpl highAvailableAgentPool; private HighAvailablePool highAvailableAgentPool;
private static Logger logger = Logger private static Logger logger = Logger
.getLogger(TestPlanLoadApplication.class); .getLogger(TestPlanLoadApplication.class);
private HighAvailablePoolImpl getHighAvailableAgentPool() { private HighAvailablePool getHighAvailableAgentPool() {
return highAvailableAgentPool; return highAvailableAgentPool;
} }
private void setHighAvailableAgentPool( private void setHighAvailableAgentPool(
HighAvailablePoolImpl highAvailableAgentPool) { HighAvailablePool highAvailableAgentPool) {
this.highAvailableAgentPool = highAvailableAgentPool; this.highAvailableAgentPool = highAvailableAgentPool;
} }

View File

@ -2,17 +2,18 @@ package org.bench4q.master.infrastructure.faultolerence;
import org.bench4q.master.domain.entity.Agent; import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.helper.ApplicationContextHelper; import org.bench4q.master.helper.ApplicationContextHelper;
import org.bench4q.master.infrastructure.highavailable.HighAvailablePool;
import org.bench4q.master.infrastructure.highavailable.impl.HighAvailablePoolImpl; import org.bench4q.master.infrastructure.highavailable.impl.HighAvailablePoolImpl;
public class BriefAgentFault implements FaultTolerance { public class BriefAgentFault implements FaultTolerance {
private HighAvailablePoolImpl haPool; private HighAvailablePool haPool;
private Agent agent; private Agent agent;
private HighAvailablePoolImpl getHaPool() { private HighAvailablePool getHaPool() {
return haPool; return haPool;
} }
private void setHaPool(HighAvailablePoolImpl haPool) { private void setHaPool(HighAvailablePool haPool) {
this.haPool = haPool; this.haPool = haPool;
} }

View File

@ -2,14 +2,18 @@ package org.bench4q.master.infrastructure.highavailable;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import org.bench4q.master.domain.RunningScriptInterface; import org.bench4q.master.domain.RunningScriptInterface;
import org.bench4q.master.domain.entity.Agent; import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.infrastructure.highavailable.impl.AgentRunBlotter;
import org.bench4q.share.models.agent.ServerStatusModel; import org.bench4q.share.models.agent.ServerStatusModel;
public interface HighAvailablePool { public interface HighAvailablePool {
public Map<String, Agent> getPool(); public Map<String, Agent> getPool();
public void add(Agent agent);
public Long getMaxAvailableLoad(); public Long getMaxAvailableLoad();
public int getCurrentAvailableLoad(); public int getCurrentAvailableLoad();
@ -23,4 +27,7 @@ public interface HighAvailablePool {
public void cleanUpAboutTestPlan( public void cleanUpAboutTestPlan(
final Collection<? extends RunningScriptInterface> runningScripts); final Collection<? extends RunningScriptInterface> runningScripts);
public void setObserver(CurrentLoadObserver currentLoadObserver);
public Map<UUID, AgentRunBlotter> getAgentRunBlotters();
} }

View File

@ -19,7 +19,7 @@ import org.bench4q.master.domain.service.TestPlanService;
import org.bench4q.master.domain.service.UserService; import org.bench4q.master.domain.service.UserService;
import org.bench4q.master.helper.SessionHelper; import org.bench4q.master.helper.SessionHelper;
import org.bench4q.master.infrastructure.communication.AgentMessenger; import org.bench4q.master.infrastructure.communication.AgentMessenger;
import org.bench4q.master.infrastructure.highavailable.impl.HighAvailablePoolImpl; import org.bench4q.master.infrastructure.highavailable.HighAvailablePool;
import org.bench4q.master.test.controller.TestBase; import org.bench4q.master.test.controller.TestBase;
import org.bench4q.share.enums.master.TestPlanStatus; import org.bench4q.share.enums.master.TestPlanStatus;
import org.bench4q.share.models.master.MonitorModel; import org.bench4q.share.models.master.MonitorModel;
@ -38,7 +38,7 @@ public class TestBase_MakeUpTestPlan extends TestBase {
private UserService userService; private UserService userService;
private UserRepository userRepository; private UserRepository userRepository;
private ScriptService scriptService; private ScriptService scriptService;
private HighAvailablePoolImpl haPool; private HighAvailablePool haPool;
private AgentRepository agentRepository; private AgentRepository agentRepository;
private AgentMessenger agentMessenger; private AgentMessenger agentMessenger;
private TestPlanEngine testPlanEngine; private TestPlanEngine testPlanEngine;
@ -130,12 +130,12 @@ public class TestBase_MakeUpTestPlan extends TestBase {
this.testPlanRepository = testPlanRepository; this.testPlanRepository = testPlanRepository;
} }
protected HighAvailablePoolImpl getHaPool() { protected HighAvailablePool getHaPool() {
return haPool; return haPool;
} }
@Autowired @Autowired
protected void setHaPool(HighAvailablePoolImpl haPool) { protected void setHaPool(HighAvailablePool haPool) {
this.haPool = haPool; this.haPool = haPool;
} }

View File

@ -0,0 +1,22 @@
package org.bench4q.master.test;
import static org.junit.Assert.*;
import org.bench4q.master.infrastructure.highavailable.HighAvailablePool;
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_InterfaceInjection {
@Autowired
private HighAvailablePool highAvailablePool;
@Test
public void test() {
assertNotNull(this.highAvailablePool);
}
}

View File

@ -5,7 +5,7 @@ import static org.junit.Assert.*;
import org.bench4q.master.domain.entity.Agent; import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.domain.repository.AgentRepository; import org.bench4q.master.domain.repository.AgentRepository;
import org.bench4q.master.domain.service.AgentService; import org.bench4q.master.domain.service.AgentService;
import org.bench4q.master.infrastructure.highavailable.impl.HighAvailablePoolImpl; import org.bench4q.master.infrastructure.highavailable.HighAvailablePool;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -20,7 +20,7 @@ public class Test_AgentService {
private static final String hostNameForTest = "133.133.12.5"; private static final String hostNameForTest = "133.133.12.5";
private AgentService agentService; private AgentService agentService;
private AgentRepository agentRepoitory; private AgentRepository agentRepoitory;
private HighAvailablePoolImpl highAvailablePool; private HighAvailablePool highAvailablePool;
private AgentService getAgentService() { private AgentService getAgentService() {
return agentService; return agentService;
@ -40,12 +40,12 @@ public class Test_AgentService {
this.agentRepoitory = agentRepoitory; this.agentRepoitory = agentRepoitory;
} }
private HighAvailablePoolImpl getHighAvailablePool() { private HighAvailablePool getHighAvailablePool() {
return highAvailablePool; return highAvailablePool;
} }
@Autowired @Autowired
private void setHighAvailablePool(HighAvailablePoolImpl highAvailablePool) { private void setHighAvailablePool(HighAvailablePool highAvailablePool) {
this.highAvailablePool = highAvailablePool; this.highAvailablePool = highAvailablePool;
} }

View File

@ -3,10 +3,13 @@ package stubs;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import org.bench4q.master.domain.RunningScriptInterface; import org.bench4q.master.domain.RunningScriptInterface;
import org.bench4q.master.domain.entity.Agent; 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.HighAvailablePool;
import org.bench4q.master.infrastructure.highavailable.impl.AgentRunBlotter;
import org.bench4q.share.models.agent.ServerStatusModel; import org.bench4q.share.models.agent.ServerStatusModel;
public class HighAvailableImpl implements HighAvailablePool { public class HighAvailableImpl implements HighAvailablePool {
@ -62,4 +65,22 @@ public class HighAvailableImpl implements HighAvailablePool {
return null; return null;
} }
@Override
public void add(Agent agent) {
// TODO Auto-generated method stub
}
@Override
public void setObserver(CurrentLoadObserver currentLoadObserver) {
// TODO Auto-generated method stub
}
@Override
public Map<UUID, AgentRunBlotter> getAgentRunBlotters() {
// TODO Auto-generated method stub
return null;
}
} }