Add Copyright and minor refactoring

This commit is contained in:
Ashvin Agrawal 2018-07-30 16:26:43 -07:00 коммит произвёл Ashvin
Родитель 7344de21d0
Коммит 6eacf3b062
17 изменённых файлов: 187 добавлений и 81 удалений

Просмотреть файл

@ -1,20 +1,15 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This program is made available under the terms of the MIT License.
* See the LICENSE file in the project root for more information.
*/
package com.microsoft.dhalion;
import java.util.ArrayList;
import static java.awt.SystemColor.text;
public class Utils {
public static String getCompositeName(String... names) {
if (names.length > 0) {
String text = names[0];
for (int i = 1; i < names.length; i++) {
text = text + "_" + names[i];
}
return text;
}
return "";
return names.length > 0 ? String.join("_", names) : "";
}
}

Просмотреть файл

@ -1,33 +1,39 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This program is made available under the terms of the MIT License.
* See the LICENSE file in the project root for more information.
*/
package com.microsoft.dhalion.detectors;
import com.microsoft.dhalion.Utils;
import com.microsoft.dhalion.conf.PolicyConfig;
import com.microsoft.dhalion.core.Measurement;
import com.microsoft.dhalion.core.MeasurementsTable;
import com.microsoft.dhalion.core.MeasurementsTable.SortKey;
import com.microsoft.dhalion.core.Symptom;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.logging.Logger;
/**
* The AboveThreshold Detector evaluates whether the values of a certain metric are above a user-defined
* threshold during a time window and in this case returns a symptom. The detector takes as input the metric name,
* the threshold and the time window specified as the number of latest checkpoints. It generates a
* SYMPTOM_HIGH_metricName symptom that is parameterized by the metric name.
*
* {@link AboveThresholdDetector}_metricName symptom that is parameterized by the metric name.
* <p>
* For example, if the threshold is set to 90, the number of latest checkpoints is set to 2 and the metric is set to
* CPU_UTILIZATION, then the detector will return a symptom SYMPTOM_HIGH_CPU_UTILIZATION only when the CPU_UTILIZATION
* is above 90 consistently during the last 2 invocations of the policy. By default the number of checkpoints is set
* to 1 which means that the detector considers only the current value when determining whether to return a symptom.
* CPU_UTILIZATION, then the detector will return a symptom AboveThresholdDetector_CPU_UTILIZATION only when the
* CPU_UTILIZATION is above 90 consistently during the last 2 invocations of the policy. By default the number of
* checkpoints is set to 1 which means that the detector considers only the current value when determining whether to
* return a symptom.
*/
public class AboveThresholdDetector extends Detector {
public static String SYMPTOM_HIGH = AboveThresholdDetector.class.getSimpleName();
public static final String HIGH_THRESHOLD_CONF = "AboveThresholdDetector.threshold";
public static final String ABOVE_THRESHOLD_NO_CHECKPOINTS = "AboveThresholdDetector.noCheckpoints";
public static final String SYMPTOM_HIGH = AboveThresholdDetector.class.getSimpleName();
static final String HIGH_THRESHOLD_CONF = "AboveThresholdDetector.threshold";
static final String ABOVE_THRESHOLD_NO_CHECKPOINTS = "AboveThresholdDetector.noCheckpoints";
private final double highThreshold;
private String metricName;
@ -35,38 +41,38 @@ public class AboveThresholdDetector extends Detector {
private static final Logger LOG = Logger.getLogger(AboveThresholdDetector.class.getSimpleName());
@Inject
public AboveThresholdDetector(PolicyConfig policyConfig, String metricName) {
this.highThreshold = (Double) policyConfig.getConfig(Utils.getCompositeName(HIGH_THRESHOLD_CONF, metricName));
this.noCheckpoints = (Double) policyConfig.getConfig(Utils.getCompositeName(ABOVE_THRESHOLD_NO_CHECKPOINTS,
metricName), 1);
this.highThreshold = (Double) policyConfig.getConfig(String.join("_", HIGH_THRESHOLD_CONF, metricName));
this.noCheckpoints
= (Double) policyConfig.getConfig(String.join("_", ABOVE_THRESHOLD_NO_CHECKPOINTS, metricName), 1);
this.metricName = metricName;
}
@Override
public Collection<Symptom> detect(Collection<Measurement> measurements) {
ArrayList<Symptom> result = new ArrayList<>();
if (measurements.size() > 0) {
MeasurementsTable measurementsTable = context.measurements().type(metricName).sort(false, SortKey.TIME_STAMP);
Collection<String> aboveThresholdAssignments = new ArrayList();
for (String component : measurementsTable.uniqueComponents()) {
MeasurementsTable componentData = measurementsTable.component(component);
for (String instance : componentData.uniqueInstances()) {
MeasurementsTable instanceData = measurementsTable.instance(instance).last((int) noCheckpoints);
if (instanceData.valueBetween(highThreshold, Double.MAX_VALUE).size() == noCheckpoints) {
LOG.fine(String.format("Instance %s has values above the limit for the last %s checkpoints", instance,
noCheckpoints));
aboveThresholdAssignments.add(instance);
}
if (measurements.isEmpty()) {
return Collections.emptyList();
}
Collection<String> assignments = new ArrayList<>();
MeasurementsTable measurementsTable = context.measurements().type(metricName).sort(false, SortKey.TIME_STAMP);
for (String component : measurementsTable.uniqueComponents()) {
MeasurementsTable componentData = measurementsTable.component(component);
for (String instance : componentData.uniqueInstances()) {
MeasurementsTable instanceData = componentData.instance(instance).last((int) noCheckpoints);
if (instanceData.valueBetween(highThreshold, Double.MAX_VALUE).size() == noCheckpoints) {
LOG.fine(String.format("Instance %s has values above the limit (%s) for the last %s checkpoints",
instance, highThreshold, noCheckpoints));
assignments.add(instance);
}
}
if (aboveThresholdAssignments.size() > 0) {
Symptom s = new Symptom(Utils.getCompositeName(SYMPTOM_HIGH, metricName), context.checkpoint(),
aboveThresholdAssignments);
result.add(s);
}
}
return result;
if (assignments.isEmpty()) {
return Collections.emptyList();
}
Symptom s = new Symptom(String.join("_", SYMPTOM_HIGH, metricName), context.checkpoint(), assignments);
return Collections.singletonList(s);
}
}

Просмотреть файл

@ -1,33 +1,40 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This program is made available under the terms of the MIT License.
* See the LICENSE file in the project root for more information.
*/
package com.microsoft.dhalion.detectors;
import com.microsoft.dhalion.Utils;
import com.microsoft.dhalion.conf.PolicyConfig;
import com.microsoft.dhalion.core.Measurement;
import com.microsoft.dhalion.core.MeasurementsTable;
import com.microsoft.dhalion.core.MeasurementsTable.SortKey;
import com.microsoft.dhalion.core.Symptom;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.logging.Logger;
/**
* The BelowThreshold Detector evaluates whether the values of a certain metric are below a user-defined
* threshold during a time window and in this case returns a symptom. The detector takes as input the metric name,
* the threshold and the time window specified as the number of latest checkpoints. It generates a
* SYMPTOM_LOW_metricName symptom that is parameterized by the metric name.
* {@link BelowThresholdDetector}_metricName symptom that is parameterized by the metric name.
* <p>
* For example, if the threshold is set to 10, the number of latest checkpoints is set to 2 and the metric is set to
* CPU_UTILIZATION, then the detector will return a symptom SYMPTOM_LOW_CPU_UTILIZATION only when the CPU_UTILIZATION
* is below 10 consistently during the last 2 invocations of the policy. By default the number of checkpoints is set
* to 1 which means that the detector considers only the current value when determining whether to return a symptom.
* CPU_UTILIZATION, then the detector will return a symptom {@link BelowThresholdDetector}_CPU_UTILIZATION only when
* the CPU_UTILIZATION is below 10 consistently during the last 2 invocations of the policy. By default the number of
* checkpoints is set to 1 which means that the detector considers only the current value when determining whether to
* return a symptom.
*/
public class BelowThresholdDetector extends Detector {
public static String SYMPTOM_LOW = BelowThresholdDetector.class.getSimpleName();
public static final String SYMPTOM_LOW = BelowThresholdDetector.class.getSimpleName();
public static final String LOW_THRESHOLD_CONF = "BelowThresholdDetector.threshold";
public static final String BELOW_THRESHOLD_NO_CHECKPOINTS = "BelowThresholdDetector.noCheckpoints";
static final String LOW_THRESHOLD_CONF = "BelowThresholdDetector.threshold";
static final String BELOW_THRESHOLD_NO_CHECKPOINTS = "BelowThresholdDetector.noCheckpoints";
private final double lowThreshold;
private String metricName;
@ -35,37 +42,38 @@ public class BelowThresholdDetector extends Detector {
private static final Logger LOG = Logger.getLogger(BelowThresholdDetector.class.getSimpleName());
@Inject
public BelowThresholdDetector(PolicyConfig policyConfig, String metricName) {
this.lowThreshold = (Double) policyConfig.getConfig(Utils.getCompositeName(LOW_THRESHOLD_CONF, metricName));
this.noCheckpoints = (Double) policyConfig.getConfig(Utils.getCompositeName(BELOW_THRESHOLD_NO_CHECKPOINTS,
metricName), 1);
this.lowThreshold = (Double) policyConfig.getConfig(String.join("_", LOW_THRESHOLD_CONF, metricName));
this.noCheckpoints
= (Double) policyConfig.getConfig(String.join("_", BELOW_THRESHOLD_NO_CHECKPOINTS, metricName), 1);
this.metricName = metricName;
}
@Override
public Collection<Symptom> detect(Collection<Measurement> measurements) {
ArrayList<Symptom> result = new ArrayList<>();
if (measurements.size() > 0) {
MeasurementsTable measurementsTable = context.measurements().type(metricName).sort(false, SortKey.TIME_STAMP);
Collection<String> belowThresholdAssignments = new ArrayList();
for (String component : measurementsTable.uniqueComponents()) {
MeasurementsTable componentData = measurementsTable.component(component);
for (String instance : componentData.uniqueInstances()) {
MeasurementsTable instanceData = measurementsTable.instance(instance).last((int) noCheckpoints);
if (instanceData.valueBetween(Double.MIN_VALUE, lowThreshold).size() == noCheckpoints) {
LOG.fine(String.format("Instance %s has values below the limit for the last %s checkpoints", instance,
noCheckpoints));
belowThresholdAssignments.add(instance);
}
if (measurements.isEmpty()) {
return Collections.emptyList();
}
MeasurementsTable measurementsTable = context.measurements().type(metricName).sort(false, SortKey.TIME_STAMP);
Collection<String> assignments = new ArrayList<>();
for (String component : measurementsTable.uniqueComponents()) {
MeasurementsTable componentData = measurementsTable.component(component);
for (String instance : componentData.uniqueInstances()) {
MeasurementsTable instanceData = componentData.instance(instance).last((int) noCheckpoints);
if (instanceData.valueBetween(Double.MIN_VALUE, lowThreshold).size() == noCheckpoints) {
LOG.fine(String.format("Instance %s has values below the limit %s for the last %s checkpoints",
instance, lowThreshold, noCheckpoints));
assignments.add(instance);
}
}
if (belowThresholdAssignments.size() > 0) {
Symptom s = new Symptom(Utils.getCompositeName(SYMPTOM_LOW, metricName), context.checkpoint(),
belowThresholdAssignments);
result.add(s);
}
}
return result;
if (assignments.isEmpty()) {
return Collections.emptyList();
}
Symptom s = new Symptom(String.join("_", SYMPTOM_LOW, metricName), context.checkpoint(), assignments);
return Collections.singletonList(s);
}
}

Просмотреть файл

@ -1,3 +1,10 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This program is made available under the terms of the MIT License.
* See the LICENSE file in the project root for more information.
*/
package com.microsoft.dhalion.detectors;
import com.microsoft.dhalion.api.IDetector;
@ -11,5 +18,4 @@ public class Detector implements IDetector {
public void initialize(ExecutionContext context) {
this.context = context;
}
}

Просмотреть файл

@ -1,3 +1,10 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This program is made available under the terms of the MIT License.
* See the LICENSE file in the project root for more information.
*/
package com.microsoft.dhalion.examples;
import com.microsoft.dhalion.api.IHealthPolicy;

Просмотреть файл

@ -1,3 +1,10 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This program is made available under the terms of the MIT License.
* See the LICENSE file in the project root for more information.
*/
package com.microsoft.dhalion.examples;
import com.microsoft.dhalion.api.IResolver;

Просмотреть файл

@ -1,3 +1,10 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This program is made available under the terms of the MIT License.
* See the LICENSE file in the project root for more information.
*/
package com.microsoft.dhalion.examples;
import com.microsoft.dhalion.api.MetricsProvider;

Просмотреть файл

@ -1,3 +1,10 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This program is made available under the terms of the MIT License.
* See the LICENSE file in the project root for more information.
*/
package com.microsoft.dhalion.examples;
/**

Просмотреть файл

@ -1,3 +1,10 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This program is made available under the terms of the MIT License.
* See the LICENSE file in the project root for more information.
*/
package com.microsoft.dhalion.examples;
import com.google.common.annotations.VisibleForTesting;

Просмотреть файл

@ -1,3 +1,10 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This program is made available under the terms of the MIT License.
* See the LICENSE file in the project root for more information.
*/
package com.microsoft.dhalion.examples;
import com.microsoft.dhalion.Utils;

Просмотреть файл

@ -1,3 +1,10 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This program is made available under the terms of the MIT License.
* See the LICENSE file in the project root for more information.
*/
package com.microsoft.dhalion.sensors;
import com.microsoft.dhalion.api.ISensor;

Просмотреть файл

@ -1,3 +1,10 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This program is made available under the terms of the MIT License.
* See the LICENSE file in the project root for more information.
*/
package com.microsoft.dhalion.detectors;
import com.microsoft.dhalion.Utils;

Просмотреть файл

@ -1,3 +1,10 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This program is made available under the terms of the MIT License.
* See the LICENSE file in the project root for more information.
*/
package com.microsoft.dhalion.detectors;
import com.microsoft.dhalion.Utils;

Просмотреть файл

@ -1,3 +1,10 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This program is made available under the terms of the MIT License.
* See the LICENSE file in the project root for more information.
*/
package com.microsoft.dhalion.examples;
import com.microsoft.dhalion.conf.Config;

Просмотреть файл

@ -1,3 +1,10 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This program is made available under the terms of the MIT License.
* See the LICENSE file in the project root for more information.
*/
package com.microsoft.dhalion.examples;
import com.microsoft.dhalion.core.Measurement;

Просмотреть файл

@ -1,3 +1,10 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This program is made available under the terms of the MIT License.
* See the LICENSE file in the project root for more information.
*/
package com.microsoft.dhalion.examples;
import com.microsoft.dhalion.Utils;

Просмотреть файл

@ -1,3 +1,10 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This program is made available under the terms of the MIT License.
* See the LICENSE file in the project root for more information.
*/
package com.microsoft.dhalion.sensors;
import com.microsoft.dhalion.api.MetricsProvider;