From d186075bebd933e0f9ee1fd8402b6469817bde99 Mon Sep 17 00:00:00 2001 From: hmm Date: Mon, 22 Sep 2014 10:35:59 +0800 Subject: [PATCH] add limit flag to MonitorMain add Observer to testplan for stopping --- .../bench4q/master/domain/entity/Monitor.java | 2 +- .../master/domain/entity/TestPlan.java | 28 ++++++++++--------- .../domain/factory/TestPlanFactory.java | 6 ++-- .../master/domain/service/TestPlanEngine.java | 9 ++++-- .../limitation/TestPlanObserver.java | 9 ++++++ .../bench4q/monitor/model/MonitorMain.java | 11 ++++++++ .../share/models/monitor/MonitorMain.java | 16 +++++++++++ 7 files changed, 62 insertions(+), 19 deletions(-) create mode 100644 Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/limitation/TestPlanObserver.java diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/Monitor.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/Monitor.java index b9be9e78..e844b68c 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/Monitor.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/Monitor.java @@ -131,7 +131,7 @@ public class Monitor { hostName, port); System.out.println(MarshalHelper.tryMarshal(monitorMain)); //tell test plan has to stop itself - testPlan.setHasToStop(monitorMain.isFinished()); + testPlan.setHasToStop(monitorMain.isTouchLimit()); List monitorResults = this.getTestPlanFactory() .createMonitorResultListWithOutId(monitorMain, testPlan, this, sampeTime); diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/TestPlan.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/TestPlan.java index 8a021b2c..731f07f2 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/TestPlan.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/TestPlan.java @@ -1,5 +1,6 @@ package org.bench4q.master.domain.entity; +import java.util.ArrayList; import java.util.Date; import java.util.LinkedList; import java.util.List; @@ -25,8 +26,8 @@ import javax.persistence.Transient; import org.apache.log4j.Logger; import org.bench4q.master.domain.IAggregate; import org.bench4q.master.domain.repository.TestPlanRepository; -import org.bench4q.master.domain.service.TestPlanEngine; import org.bench4q.master.domain.service.TestResultSave; +import org.bench4q.master.domain.valueobject.limitation.TestPlanObserver; import org.bench4q.master.domain.valueobject.transaction.impl.TestPlanLoadApplication; import org.bench4q.share.enums.master.TestPlanStatus; import org.bench4q.share.helper.MarshalHelper; @@ -49,9 +50,9 @@ public class TestPlan implements IAggregate { private Set monitors; private TestResultSave testResultSave; private TestPlanRepository repository; - private TestPlanEngine testPlanEngine; private boolean hasToStop; private LimitModel limitModel; + private List observers = new ArrayList(); @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -288,7 +289,7 @@ public class TestPlan implements IAggregate { getTestResultSave().update(TestPlan.this, collectResult(new Date())); if(isHasToStop()){ - testPlanEngine.stop(UUID.fromString(testPlanRunId)); + submitToObserver(); } } }, 0, this.getSampleCycleInSeconds(), TimeUnit.SECONDS); @@ -348,22 +349,23 @@ public class TestPlan implements IAggregate { this.hasToStop = hasToStop; } - @Transient - public TestPlanEngine getTestPlanEngine() { - return testPlanEngine; - } - - public void setTestPlanEngine(TestPlanEngine testPlanEngine) { - this.testPlanEngine = testPlanEngine; - } - @Transient public LimitModel getLimitModel() { return limitModel; } public void setLimitModel(LimitModel limitModel) { - limitModel = limitModel; + this.limitModel = limitModel; } + public void addObserver(TestPlanObserver testPlanObserver){ + observers.add(testPlanObserver); + } + + private void submitToObserver(){ + UUID testPlanIdUuid = UUID.fromString(testPlanRunId); + for(TestPlanObserver testPlanObserver: observers){ + testPlanObserver.processStop(testPlanIdUuid); + } + } } diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/factory/TestPlanFactory.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/factory/TestPlanFactory.java index 8f6978bf..95c230a0 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/factory/TestPlanFactory.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/factory/TestPlanFactory.java @@ -25,7 +25,6 @@ import org.bench4q.master.domain.entity.TestPlanScriptResult; import org.bench4q.master.domain.entity.User; import org.bench4q.master.domain.repository.TestPlanRepository; import org.bench4q.master.domain.service.ScriptService; -import org.bench4q.master.domain.service.TestPlanEngine; import org.bench4q.master.domain.service.TestResultSave; import org.bench4q.master.domain.valueobject.transaction.TransactionFactory; import org.bench4q.master.exception.ExceptionLog; @@ -103,7 +102,7 @@ public class TestPlanFactory { } public TestPlan createATestPlanWithoutId(TestPlanModel testPlanModel, - User user, UUID runId, TestPlanEngine testPlanEngine) throws IllegalParameterException { + User user, UUID runId) throws IllegalParameterException { this.logger.info("testPlanName:" + testPlanModel.getName()); TestPlan result = new TestPlan(); result.setCreateDateTime(new Date()); @@ -143,7 +142,6 @@ public class TestPlanFactory { monitors.add(monitorInBusiness); } result.setMonitors(monitors); - result.setTestPlanEngine(testPlanEngine); result.setLimitModel(testPlanModel.getLimitModel()); return result; } @@ -283,7 +281,9 @@ public class TestPlanFactory { List monitorResults = new LinkedList(); Field[] fields = monitorMain.getClass().getDeclaredFields(); + for (int i = 0; i < fields.length; i++) { + if(fields[i].getAnnotation(MonitorMain.Transient.class) != null) continue; MonitorResult monitorResult = new MonitorResult(); monitorResult.setTestPlanDB(testPlan); monitorResult.setMonitor(monitor); diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/TestPlanEngine.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/TestPlanEngine.java index d6638876..6229b84a 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/TestPlanEngine.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/TestPlanEngine.java @@ -16,6 +16,7 @@ import org.bench4q.master.domain.entity.TestPlan; import org.bench4q.master.domain.entity.User; import org.bench4q.master.domain.factory.TestPlanFactory; import org.bench4q.master.domain.repository.TestPlanRepository; +import org.bench4q.master.domain.valueobject.limitation.TestPlanObserver; import org.bench4q.master.domain.valueobject.schedulscript.TaskCompleteCallback; import org.bench4q.master.exception.ExceptionUtils.IllegalParameterException; import org.bench4q.master.infrastructure.highavailable.CurrentLoadObserver; @@ -30,7 +31,7 @@ import org.springframework.stereotype.Component; @Component public class TestPlanEngine implements TaskCompleteCallback, - CurrentLoadObserver { + CurrentLoadObserver, TestPlanObserver { private HighAvailablePool haPool; private TestPlanRepository testPlanRepository; private TestPlanFactory testPlanFactory; @@ -126,7 +127,7 @@ public class TestPlanEngine implements TaskCompleteCallback, final User user, final UUID testPlanRunId) throws IllegalParameterException { TestPlan testPlan = this.getTestPlanFactory().createATestPlanWithoutId( - testPlanBusinessModel, user, testPlanRunId, this); + testPlanBusinessModel, user, testPlanRunId); Logger.getLogger(TestPlanService.class).info( "test plan name:" + testPlan.getName()); return this.getTestPlanRepository().attach(testPlan); @@ -198,4 +199,8 @@ public class TestPlanEngine implements TaskCompleteCallback, public void executePendingTestPlan() { pickATestPlan(); } + + public void processStop(final UUID testPlanId) { + this.stop(testPlanId); + } } diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/limitation/TestPlanObserver.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/limitation/TestPlanObserver.java new file mode 100644 index 00000000..7e94d975 --- /dev/null +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/limitation/TestPlanObserver.java @@ -0,0 +1,9 @@ +package org.bench4q.master.domain.valueobject.limitation; + +import java.util.UUID; + + +public interface TestPlanObserver{ + + public abstract void processStop(final UUID testPlanId); +} diff --git a/Bench4Q-Monitor-Sigar/src/main/java/org/bench4q/monitor/model/MonitorMain.java b/Bench4Q-Monitor-Sigar/src/main/java/org/bench4q/monitor/model/MonitorMain.java index 5454fab7..7677a843 100644 --- a/Bench4Q-Monitor-Sigar/src/main/java/org/bench4q/monitor/model/MonitorMain.java +++ b/Bench4Q-Monitor-Sigar/src/main/java/org/bench4q/monitor/model/MonitorMain.java @@ -29,6 +29,9 @@ public class MonitorMain extends SampleModel { private NetworkInterfaceModel networkInterfaceModel; @XmlElement(name = "process_info") private ProcessModel processModel; + @XmlElement(name = "touch_limit") + private boolean isTouchLimit; + private SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd-HH-mm-ss"); @@ -76,4 +79,12 @@ public class MonitorMain extends SampleModel { NetworkInterfaceModel networkInterfaceModel) { this.networkInterfaceModel = networkInterfaceModel; } + + public boolean isTouchLimit() { + return isTouchLimit; + } + + public void setTouchLimit(boolean isTouchLimit) { + this.isTouchLimit = isTouchLimit; + } } diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/models/monitor/MonitorMain.java b/Bench4Q-Share/src/main/java/org/bench4q/share/models/monitor/MonitorMain.java index 9e02823d..3c82320a 100644 --- a/Bench4Q-Share/src/main/java/org/bench4q/share/models/monitor/MonitorMain.java +++ b/Bench4Q-Share/src/main/java/org/bench4q/share/models/monitor/MonitorMain.java @@ -1,5 +1,7 @@ package org.bench4q.share.models.monitor; +import java.beans.Transient; + import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @@ -19,6 +21,8 @@ public class MonitorMain extends SampleModel{ private NetworkInterfaceModel networkInterfaceModel; private ProcessModel processModel; + + private boolean isTouchLimit; public MonitorMain() { @@ -63,4 +67,16 @@ public class MonitorMain extends SampleModel{ public void setProcessModel(ProcessModel processModel) { this.processModel = processModel; } + + @MonitorMain.Transient + @XmlElement(name = "touch_limit") + public boolean isTouchLimit() { + return isTouchLimit; + } + + public void setTouchLimit(boolean isTouchLimit) { + this.isTouchLimit = isTouchLimit; + } + public @interface Transient{} + }