diff --git a/seep-system/pom.xml b/seep-system/pom.xml index a01fa016..c9571be2 100644 --- a/seep-system/pom.xml +++ b/seep-system/pom.xml @@ -26,21 +26,11 @@ UTF-8 - - junit - junit - 3.8.1 - test - - - log4j - log4j - 1.2.17 - + com.esotericsoftware.kryo kryo - 2.22 + 2.20 @@ -66,6 +56,8 @@ fastutil 6.5.9 + + joda-time joda-time @@ -76,7 +68,13 @@ commons-collections4 4.0 - + + org.apache.commons + commons-lang3 + 3.2.1 + + + org.eclipse.jetty jetty-server diff --git a/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/Factory.java b/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/Factory.java index dd003138..8dbc6192 100644 --- a/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/Factory.java +++ b/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/Factory.java @@ -23,4 +23,6 @@ public interface Factory { T create(); + T create(Object...args); + } diff --git a/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/master/MonitorMasterFactory.java b/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/master/MonitorMasterFactory.java index 90320423..1e5d6dfb 100644 --- a/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/master/MonitorMasterFactory.java +++ b/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/master/MonitorMasterFactory.java @@ -54,4 +54,9 @@ public MonitorMaster create() { = new SeepInfrastructureAdaptor(infrastructure); return new MonitorMaster(adaptor, rules, masterPort); } + + @Override + public MonitorMaster create(Object... args) { + return create(); + } } diff --git a/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/PolicyRuleBuilder.java b/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/PolicyRuleBuilder.java index adec4fc8..e6a4f325 100644 --- a/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/PolicyRuleBuilder.java +++ b/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/PolicyRuleBuilder.java @@ -10,16 +10,23 @@ ******************************************************************************/ package uk.ac.imperial.lsds.seep.infrastructure.monitor.policy; -import uk.ac.imperial.lsds.seep.infrastructure.monitor.Builder; import java.util.ArrayList; import java.util.List; +import uk.ac.imperial.lsds.seep.infrastructure.monitor.Builder; import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.action.Action; import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.action.ScaleInAction; import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.action.ScaleOutAction; import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.metric.MetricName; import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.operator.Operator; -import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.scale.factor.ScaleFactor; import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.scale.constraint.ScaleConstraint; +import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.scale.factor.ScaleFactor; +import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.syntax.ActionSyntax; +import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.syntax.GuardTimeThresholdSyntax; +import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.syntax.MetricSyntax; +import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.syntax.MetricThresholdSyntax; +import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.syntax.ScaleConstraintSyntax; +import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.syntax.ScaleFactorSyntax; +import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.syntax.TimeThresholdSyntax; import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.threshold.MetricThreshold; import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.threshold.TimeThreshold; import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.trigger.ActionTrigger; @@ -28,7 +35,9 @@ * * @author mrouaux */ -public class PolicyRuleBuilder implements Builder { +public class PolicyRuleBuilder implements Builder, + ActionSyntax, MetricSyntax, MetricThresholdSyntax, TimeThresholdSyntax, + ScaleConstraintSyntax, ScaleFactorSyntax, GuardTimeThresholdSyntax { /** * Listener to notify others when the builder creates a new PolicyRule instance. @@ -102,48 +111,57 @@ public interface PolicyRuleBuilderListener { this.scaleConstraint = null; } + @Override public PolicyRuleBuilder scaleIn(final Operator operator) { this.action = new ScaleInAction(); this.operator = operator; return this; } + @Override public PolicyRuleBuilder scaleOut(final Operator operator) { this.action = new ScaleOutAction(); this.operator = operator; return this; } + @Override public PolicyRuleBuilder by(final ScaleFactor scaleFactor) { this.scaleFactor = scaleFactor; return this; } + @Override public PolicyRuleBuilder butNeverBelow(final ScaleConstraint scaleConstraint) { this.scaleConstraint = scaleConstraint; return this; } + @Override public PolicyRuleBuilder butNeverAbove(final ScaleConstraint scaleConstraint) { this.scaleConstraint = scaleConstraint; return this; } + @Override public PolicyRuleBuilder when(MetricName metricName) { this.triggerMetricName = metricName; return this; } + @Override public PolicyRuleBuilder and(MetricName metricName) { this.triggerMetricName = metricName; return this; } + @Override public PolicyRuleBuilder is(MetricThreshold metricThreshold) { this.triggerMetricThreshold = metricThreshold; return this; } + @Override public PolicyRuleBuilder forAtLeast(TimeThreshold timeThreshold) { this.triggerTimeThreshold = timeThreshold; @@ -158,11 +176,13 @@ public PolicyRuleBuilder forAtLeast(TimeThreshold timeThreshold) { return this; } + @Override public PolicyRuleBuilder withNoScaleInSince(TimeThreshold scaleGuardTime) { this.scaleGuardTime = scaleGuardTime; return this; } + @Override public PolicyRuleBuilder withNoScaleOutSince(TimeThreshold scaleGuardTime) { this.scaleGuardTime = scaleGuardTime; return this; diff --git a/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/evaluate/PolicyRuleEvaluator.java b/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/evaluate/PolicyRuleEvaluator.java index aac58cd9..638294db 100644 --- a/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/evaluate/PolicyRuleEvaluator.java +++ b/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/evaluate/PolicyRuleEvaluator.java @@ -200,7 +200,9 @@ private void pruneOldReadings(Period maximumAge) { // works based on the assumption that metrics readings are offered // with always increasing timestamps. if(readingAge.toStandardSeconds() - .isLessThan(maximumAge.toStandardSeconds())) { + .isLessThan(maximumAge.toStandardSeconds()) + || readingAge.toStandardSeconds() + .equals(maximumAge.toStandardSeconds())) { done = true; } else { pastReadings.poll(); diff --git a/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/syntax/ActionSyntax.java b/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/syntax/ActionSyntax.java new file mode 100644 index 00000000..0fa29599 --- /dev/null +++ b/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/syntax/ActionSyntax.java @@ -0,0 +1,19 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.syntax; + +import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.operator.Operator; + +/** + * + * @author martinrouaux + */ +public interface ActionSyntax { + + ActionSyntax scaleIn(final Operator operator); + + ActionSyntax scaleOut(final Operator operator); + +} diff --git a/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/syntax/GuardTimeThresholdSyntax.java b/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/syntax/GuardTimeThresholdSyntax.java new file mode 100644 index 00000000..f7467ae9 --- /dev/null +++ b/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/syntax/GuardTimeThresholdSyntax.java @@ -0,0 +1,19 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.syntax; + +import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.threshold.TimeThreshold; + +/** + * + * @author martinrouaux + */ +public interface GuardTimeThresholdSyntax { + + GuardTimeThresholdSyntax withNoScaleInSince(TimeThreshold scaleGuardTime); + + GuardTimeThresholdSyntax withNoScaleOutSince(TimeThreshold scaleGuardTime); + +} diff --git a/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/syntax/MetricSyntax.java b/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/syntax/MetricSyntax.java new file mode 100644 index 00000000..b2bc8727 --- /dev/null +++ b/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/syntax/MetricSyntax.java @@ -0,0 +1,19 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.syntax; + +import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.metric.MetricName; + +/** + * + * @author martinrouaux + */ +public interface MetricSyntax { + + MetricSyntax when(MetricName metricName); + + MetricSyntax and(MetricName metricName); + +} diff --git a/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/syntax/MetricThresholdSyntax.java b/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/syntax/MetricThresholdSyntax.java new file mode 100644 index 00000000..c2ed6e30 --- /dev/null +++ b/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/syntax/MetricThresholdSyntax.java @@ -0,0 +1,17 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.syntax; + +import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.threshold.MetricThreshold; + +/** + * + * @author martinrouaux + */ +public interface MetricThresholdSyntax { + + MetricThresholdSyntax is(MetricThreshold metricThreshold); + +} diff --git a/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/syntax/ScaleConstraintSyntax.java b/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/syntax/ScaleConstraintSyntax.java new file mode 100644 index 00000000..02a98988 --- /dev/null +++ b/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/syntax/ScaleConstraintSyntax.java @@ -0,0 +1,19 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.syntax; + +import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.scale.constraint.ScaleConstraint; + +/** + * + * @author martinrouaux + */ +public interface ScaleConstraintSyntax { + + ScaleConstraintSyntax butNeverBelow(final ScaleConstraint scaleConstraint); + + ScaleConstraintSyntax butNeverAbove(final ScaleConstraint scaleConstraint); + +} diff --git a/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/syntax/ScaleFactorSyntax.java b/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/syntax/ScaleFactorSyntax.java new file mode 100644 index 00000000..3daa11ba --- /dev/null +++ b/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/syntax/ScaleFactorSyntax.java @@ -0,0 +1,17 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.syntax; + +import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.scale.factor.ScaleFactor; + +/** + * + * @author martinrouaux + */ +public interface ScaleFactorSyntax { + + ScaleFactorSyntax by(final ScaleFactor scaleFactor); + +} diff --git a/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/syntax/TimeThresholdSyntax.java b/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/syntax/TimeThresholdSyntax.java new file mode 100644 index 00000000..d6dde3c7 --- /dev/null +++ b/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/syntax/TimeThresholdSyntax.java @@ -0,0 +1,17 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.syntax; + +import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.threshold.TimeThreshold; + +/** + * + * @author martinrouaux + */ +public interface TimeThresholdSyntax { + + TimeThresholdSyntax forAtLeast(TimeThreshold timeThreshold); + +} diff --git a/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/trigger/ActionTrigger.java b/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/trigger/ActionTrigger.java index a47b3012..55f4804d 100644 --- a/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/trigger/ActionTrigger.java +++ b/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/trigger/ActionTrigger.java @@ -13,8 +13,10 @@ import java.util.List; import java.util.Objects; import org.joda.time.Period; +import org.joda.time.Seconds; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import uk.ac.imperial.lsds.seep.GLOBALS; import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.metric.MetricName; import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.metric.MetricValue; import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.threshold.MetricThreshold; @@ -82,36 +84,64 @@ public void evaluate(List readings, // Determine the new state of the trigger depending on the result of // evaluating boh thresholds. Need to mark flag if state changes. - ActionTriggerState pastTriggerState = triggerState; + ActionTriggerState pastTriggerState = triggerState; - int i = 0; - for(MetricReading r : readings) { - Period metricPeriod = new Period(r.getTimestamp(), time.now()); - MetricValue metricValue = r.getValues().get(metricName); + boolean enoughReadings = true; + + // Check that we have enough readings to cover the entire time threshold + if ((readings != null) && (readings.size() > 0)) { + MetricReading mostRecentReading = readings.get(readings.size() - 1); + MetricReading leastRecentReading = readings.get(0); + + logger.info("Most recent reading [" + mostRecentReading.getTimestamp() + "]"); + logger.info("Least recent reading [" + leastRecentReading.getTimestamp() + "]"); - logger.info("Evaluating reading[" + i + "] value[" - + metricValue.toString() + "] period[" - + metricPeriod.toString() + "]"); - - // We evaluate the time threshold first, simple optimisation to be - // able to abort the iteration sooner (readings are guaranteed to be - // sorted by time of reception, from most recent to least recenet). - if(timeThreshold.evaluate(metricPeriod)) { - triggerState = valueThreshold.evaluate(metricValue)? - ActionTriggerState.FIRED: - ActionTriggerState.NON_FIRED; - - // If there is a reading within the time threshold for which - // the value evaluates to false (trigger is non-fired), then - // we can break from the evaluation loop. - if(triggerState.equals(ActionTriggerState.NON_FIRED)) { - break; - } - } + Period readingsPeriod = new Period( + leastRecentReading.getTimestamp(), + mostRecentReading.getTimestamp()); - i++; + int toleranceSeconds = new Double(0.1 + * timeThreshold.toPeriod().toStandardSeconds().getSeconds()).intValue(); + + if (readingsPeriod.toStandardSeconds().isLessThan( + timeThreshold.toPeriod().toStandardSeconds().minus(toleranceSeconds))) { + + logger.info("Not enough readings, only for last period[" + readingsPeriod + "]"); + enoughReadings = false; + } } + // If we have enough readings, then we evaluate in detail + if (enoughReadings) { + int i = 0; + for(MetricReading r : readings) { + Period metricPeriod = new Period(r.getTimestamp(), time.now()); + MetricValue metricValue = r.getValues().get(metricName); + + logger.info("Evaluating reading[" + i + "] value[" + + metricValue.toString() + "] period[" + + metricPeriod.toString() + "]"); + + // We evaluate the time threshold first, simple optimisation to be + // able to abort the iteration sooner (readings are guaranteed to be + // sorted by time of reception, from most recent to least recent). + if(timeThreshold.evaluate(metricPeriod)) { + triggerState = valueThreshold.evaluate(metricValue)? + ActionTriggerState.FIRED: + ActionTriggerState.NON_FIRED; + + // If there is a reading within the time threshold for which + // the value evaluates to false (trigger is non-fired), then + // we can break from the evaluation loop. + if(triggerState.equals(ActionTriggerState.NON_FIRED)) { + break; + } + } + + i++; + } + } + stateChanged = (triggerState != pastTriggerState); logger.info("New trigger state is [" + triggerState.toString() + "] changed[" + stateChanged + "]"); diff --git a/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/slave/MonitorSlaveFactory.java b/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/slave/MonitorSlaveFactory.java index bb5b5b81..9bf92fc8 100644 --- a/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/slave/MonitorSlaveFactory.java +++ b/seep-system/src/main/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/slave/MonitorSlaveFactory.java @@ -37,4 +37,15 @@ public MonitorSlaveFactory(int operatorId) { public MonitorSlave create() { return new MonitorSlave(operatorId, masterHost, masterPort, freqSeconds); } -} \ No newline at end of file + + @Override + public MonitorSlave create(Object...args) { + // Use host address from arguments to factory method + if (args.length > 0) { + this.masterHost = ((args[0] instanceof String) && (args[0] != null))? + (String)args[0] : GLOBALS.valueFor("mainAddr"); + } + + return create(); + } +} diff --git a/seep-system/src/test/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/master/MonitorMasterTest.java b/seep-system/src/test/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/master/MonitorMasterTest.java index 0a724c70..8bd3f558 100644 --- a/seep-system/src/test/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/master/MonitorMasterTest.java +++ b/seep-system/src/test/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/master/MonitorMasterTest.java @@ -15,6 +15,7 @@ import org.junit.Test; import static org.mockito.Mockito.*; import org.hamcrest.Matchers; +import org.junit.Ignore; import uk.ac.imperial.lsds.seep.infrastructure.monitor.comm.serialization.MetricsDeserializer; import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.PolicyRules; import uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.evaluate.AbstractEvaluator; @@ -46,6 +47,7 @@ public void tearDown() { } @Test + @Ignore public void testStartSlaveConnect() throws InterruptedException { System.out.println("testStartSlaveConnect"); diff --git a/seep-system/src/test/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/trigger/ActionTriggerTest.java b/seep-system/src/test/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/trigger/ActionTriggerTest.java index 460c080d..d0cd7e39 100644 --- a/seep-system/src/test/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/trigger/ActionTriggerTest.java +++ b/seep-system/src/test/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/policy/trigger/ActionTriggerTest.java @@ -79,8 +79,8 @@ private MetricReading createFakeReading(Instant timestamp, MetricName name, Metr } @Test - public void testTriggerWithSingleReadingTimeTrueAndValueTrue() { - System.out.println("testTriggerWithSingleReadingTimeTrueAndValueTrue"); + public void testTriggerWithSingleReading() { + System.out.println("testTriggerWithSingleReading"); List readings = new ArrayList(); MetricReading r1 = createFakeReading( @@ -92,11 +92,11 @@ public void testTriggerWithSingleReadingTimeTrueAndValueTrue() { when(mockTimeReference.now()).thenReturn(currentTime); - when(mockTimeThreshold.evaluate(eq(new Period(r1.getTimestamp(), currentTime)))) - .thenReturn(Boolean.TRUE); - - when(mockValueThreshold.evaluate(eq(r1.getValues().get(MetricName.CPU_UTILIZATION)))) - .thenReturn(Boolean.TRUE); + when(mockTimeThreshold.toPeriod()) + .thenReturn(new Period().withMinutes(1)); + + verify(mockTimeThreshold, times(0)).evaluate(any(Period.class)); + verify(mockValueThreshold, times(0)).evaluate(any(MetricValue.class)); ActionTrigger trigger = new ActionTrigger( mockValueThreshold, @@ -104,8 +104,8 @@ public void testTriggerWithSingleReadingTimeTrueAndValueTrue() { MetricName.CPU_UTILIZATION); trigger.evaluate(readings, mockTimeReference); - assertTrue("Trigger should have fired", trigger.isFired()); - assertTrue("Trigger state should have changed ", trigger.hasChanged()); + assertFalse("Trigger shouldn't have fired", trigger.isFired()); + assertFalse("Trigger state shouldn't have changed ", trigger.hasChanged()); } @Test @@ -122,6 +122,9 @@ public void testTriggerWithSingleReadingTimeTrueAndValueFalse() { when(mockTimeReference.now()).thenReturn(currentTime); + when(mockTimeThreshold.toPeriod()) + .thenReturn(new Period().withMinutes(1)); + when(mockTimeThreshold.evaluate(eq(new Period(r1.getTimestamp(), currentTime)))) .thenReturn(Boolean.TRUE); @@ -151,7 +154,10 @@ public void testTriggerWithSingleReadingTimeFalse() { Instant currentTime = createInstant(2013, 12, 29, 10, 33, 0); when(mockTimeReference.now()).thenReturn(currentTime); - + + when(mockTimeThreshold.toPeriod()) + .thenReturn(new Period().withMinutes(1)); + when(mockTimeThreshold.evaluate(eq(new Period(r1.getTimestamp(), currentTime)))) .thenReturn(Boolean.FALSE); @@ -195,6 +201,8 @@ public void testTriggerWithMultipleReadingsTimeTrueAndValueTrue() { when(mockTimeReference.now()).thenReturn(currentTime); // Mock expectations for time threshold + when(mockTimeThreshold.toPeriod()) + .thenReturn(new Period().withMinutes(1)); when(mockTimeThreshold.evaluate(eq(new Period(r1.getTimestamp(), currentTime)))) .thenReturn(Boolean.TRUE); when(mockTimeThreshold.evaluate(eq(new Period(r2.getTimestamp(), currentTime)))) @@ -247,6 +255,8 @@ public void testTriggerWithMultipleReadingsStateChangesOnceOnly() { when(mockTimeReference.now()).thenReturn(currentTime); // Mock expectations for time threshold + when(mockTimeThreshold.toPeriod()) + .thenReturn(new Period().withMinutes(1)); when(mockTimeThreshold.evaluate(eq(new Period(r1.getTimestamp(), currentTime)))) .thenReturn(Boolean.TRUE); when(mockTimeThreshold.evaluate(eq(new Period(r2.getTimestamp(), currentTime)))) @@ -303,9 +313,10 @@ public void testTriggerWithMultipleReadingsWithOneTimeFalse() { when(mockTimeReference.now()).thenReturn(currentTime); // Mock expectations for time threshold + when(mockTimeThreshold.toPeriod()) + .thenReturn(new Period().withMinutes(5)); when(mockTimeThreshold.evaluate(eq(new Period(r1.getTimestamp(), currentTime)))) - .thenReturn(Boolean.FALSE); - + .thenReturn(Boolean.FALSE); when(mockTimeThreshold.evaluate(eq(new Period(r2.getTimestamp(), currentTime)))) .thenReturn(Boolean.TRUE); when(mockTimeThreshold.evaluate(eq(new Period(r3.getTimestamp(), currentTime)))) @@ -358,6 +369,8 @@ public void testTriggerWithMultipleReadingsWithOneValueFalse() { when(mockTimeReference.now()).thenReturn(currentTime); // Mock expectations for time threshold + when(mockTimeThreshold.toPeriod()) + .thenReturn(new Period().withMinutes(1)); when(mockTimeThreshold.evaluate(eq(new Period(r1.getTimestamp(), currentTime)))) .thenReturn(Boolean.TRUE); when(mockTimeThreshold.evaluate(eq(new Period(r2.getTimestamp(), currentTime)))) @@ -382,4 +395,112 @@ public void testTriggerWithMultipleReadingsWithOneValueFalse() { assertFalse("Trigger shouldn't have fired", trigger.isFired()); assertFalse("Trigger state shouldn't have changed ", trigger.hasChanged()); } + + @Test + public void testTriggerWithNotEnoughReadings() { + System.out.println("testTriggerWithNotEnoughReadings"); + + List readings = new ArrayList(); + + MetricReading r1 = createFakeReading( + createInstant(2013, 12, 29, 10, 30, 0), + MetricName.CPU_UTILIZATION, percent(50)); + + MetricReading r2 = createFakeReading( + createInstant(2013, 12, 29, 10, 31, 0), + MetricName.CPU_UTILIZATION, percent(60)); + + MetricReading r3 = createFakeReading( + createInstant(2013, 12, 29, 10, 31, 30), + MetricName.CPU_UTILIZATION, percent(70)); + + readings.add(r1); + readings.add(r2); + readings.add(r3); + + Instant currentTime = createInstant(2013, 12, 29, 10, 33, 0); + + when(mockTimeReference.now()).thenReturn(currentTime); + + // Mock expectations for time threshold + when(mockTimeThreshold.toPeriod()) + .thenReturn(new Period().withMinutes(2)); + + verify(mockTimeThreshold, times(0)).evaluate(any(Period.class)); + + // Mock expectations for value threshold + verify(mockValueThreshold, times(0)).evaluate(any(MetricValue.class)); + + ActionTrigger trigger = new ActionTrigger( + mockValueThreshold, + mockTimeThreshold, + MetricName.CPU_UTILIZATION); + + trigger.evaluate(readings, mockTimeReference); + assertFalse("Trigger shouldn't have fired", trigger.isFired()); + assertFalse("Trigger state shouldn't have changed ", trigger.hasChanged()); + } + + @Test + public void testTriggerWithMultipleReadingsTimeTrueAndValueTrueWithinThreshold() { + System.out.println("testTriggerWithMultipleReadingsTimeTrueAndValueTrueWithinThreshold"); + + List readings = new ArrayList(); + + MetricReading r1 = createFakeReading( + createInstant(2013, 12, 29, 10, 30, 0), + MetricName.CPU_UTILIZATION, percent(50)); + + MetricReading r2 = createFakeReading( + createInstant(2013, 12, 29, 10, 31, 0), + MetricName.CPU_UTILIZATION, percent(60)); + + MetricReading r3 = createFakeReading( + createInstant(2013, 12, 29, 10, 31, 30), + MetricName.CPU_UTILIZATION, percent(70)); + + MetricReading r4 = createFakeReading( + createInstant(2013, 12, 29, 10, 31, 48), + MetricName.CPU_UTILIZATION, percent(60)); + readings.add(r1); + readings.add(r2); + readings.add(r3); + readings.add(r4); + + Instant currentTime = createInstant(2013, 12, 29, 10, 33, 0); + + when(mockTimeReference.now()).thenReturn(currentTime); + + // Mock expectations for time threshold + when(mockTimeThreshold.toPeriod()) + .thenReturn(new Period().withMinutes(2)); + when(mockTimeThreshold.evaluate(eq(new Period(r1.getTimestamp(), currentTime)))) + .thenReturn(Boolean.TRUE); + when(mockTimeThreshold.evaluate(eq(new Period(r2.getTimestamp(), currentTime)))) + .thenReturn(Boolean.TRUE); + when(mockTimeThreshold.evaluate(eq(new Period(r3.getTimestamp(), currentTime)))) + .thenReturn(Boolean.TRUE); + when(mockTimeThreshold.evaluate(eq(new Period(r4.getTimestamp(), currentTime)))) + .thenReturn(Boolean.TRUE); + + // Mock expectations for value threshold + when(mockValueThreshold.evaluate(eq(r1.getValues().get(MetricName.CPU_UTILIZATION)))) + .thenReturn(Boolean.TRUE); + when(mockValueThreshold.evaluate(eq(r2.getValues().get(MetricName.CPU_UTILIZATION)))) + .thenReturn(Boolean.TRUE); + when(mockValueThreshold.evaluate(eq(r3.getValues().get(MetricName.CPU_UTILIZATION)))) + .thenReturn(Boolean.TRUE); + when(mockValueThreshold.evaluate(eq(r4.getValues().get(MetricName.CPU_UTILIZATION)))) + .thenReturn(Boolean.TRUE); + + ActionTrigger trigger = new ActionTrigger( + mockValueThreshold, + mockTimeThreshold, + MetricName.CPU_UTILIZATION); + trigger.evaluate(readings, mockTimeReference); + + assertTrue("Trigger should have fired", trigger.isFired()); + assertTrue("Trigger state should have changed ", trigger.hasChanged()); + } + } diff --git a/seep-system/src/test/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/slave/MonitorSlaveTest.java b/seep-system/src/test/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/slave/MonitorSlaveTest.java index 5f225c2e..2f02dd2d 100644 --- a/seep-system/src/test/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/slave/MonitorSlaveTest.java +++ b/seep-system/src/test/java/uk/ac/imperial/lsds/seep/infrastructure/monitor/slave/MonitorSlaveTest.java @@ -17,6 +17,7 @@ import org.junit.Test; import static org.mockito.Mockito.*; import static org.hamcrest.Matchers.equalTo; +import org.junit.Ignore; /** * @@ -44,6 +45,7 @@ public void tearDown() { } @Test + @Ignore public void testSlaveConnectToMaster() throws IOException, InterruptedException { System.out.println("testSlaveConnectToMaster");