Skip to content

Commit

Permalink
Merge pull request #80 from jshaughn/hwkalerts-74
Browse files Browse the repository at this point in the history
Hwkalerts 74
  • Loading branch information
lucasponce committed Aug 17, 2015
2 parents 2a4ab97 + f9c88fc commit ed3ef82
Show file tree
Hide file tree
Showing 9 changed files with 285 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public class FilePlugin implements ActionPluginListener {
private ObjectMapper objectMapper;

public FilePlugin() {
defaultProperties.put("path", "/tmp/hawkular/actions/file");
defaultProperties.put("path",
new File(System.getProperty("java.io.tmpdir"), "hawkular/actions/file").getAbsolutePath());
objectMapper = new ObjectMapper();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public Alert(String tenantId, String triggerId, Severity severity, List<Set<Cond
this.ctime = System.currentTimeMillis();
this.status = Status.OPEN;

this.alertId = triggerId + "|" + ctime;
this.alertId = triggerId + "-" + ctime;
}

public String getTenantId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ public abstract class ConditionEval {
@JsonInclude
protected long dataTimestamp;

// flag noting whether this condition eval was used in a tested Tuple and already applied to dampening
@JsonIgnore
protected boolean used;

@JsonInclude
protected Condition.Type type;

Expand All @@ -66,7 +62,6 @@ public ConditionEval(Type type, boolean match, long dataTimestamp, Map<String, S
this.match = match;
this.dataTimestamp = dataTimestamp;
this.evalTimestamp = System.currentTimeMillis();
this.used = false;
this.context = context;
}

Expand Down Expand Up @@ -94,14 +89,6 @@ public void setDataTimestamp(long dataTimestamp) {
this.dataTimestamp = dataTimestamp;
}

public boolean isUsed() {
return used;
}

public void setUsed(boolean used) {
this.used = used;
}

public Condition.Type getType() {
return type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hawkular.alerts.api.model.condition.ConditionEval;
import org.hawkular.alerts.api.model.trigger.Trigger.Mode;
import org.hawkular.alerts.api.model.trigger.TriggerTemplate.Match;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
Expand Down Expand Up @@ -83,6 +86,10 @@ public enum Type {
@JsonIgnore
private transient long trueEvalsStartTime;

@JsonIgnore
// This Map<conditionSetIndex,ConditionEval> holds the most recent eval for each member of the condition set
private transient Map<Integer, ConditionEval> currentEvals = new HashMap<>(5);

@JsonIgnore
private transient boolean satisfied;

Expand All @@ -101,10 +108,13 @@ public Dampening() {
* no time limit for the evaluations.
* @param triggerId the triggerId
* @param triggerMode the trigger mode for when this dampening is active
* @param numConsecutiveTrueEvals the numConsecutiveTrueEvals
* @param numConsecutiveTrueEvals the numConsecutiveTrueEvals, >= 1.
* @return the configured Dampening
*/
public static Dampening forStrict(String triggerId, Mode triggerMode, int numConsecutiveTrueEvals) {
if (numConsecutiveTrueEvals < 1) {
throw new IllegalArgumentException("NumConsecutiveTrueEvals must be >= 1");
}
return new Dampening(triggerId, triggerMode, Type.STRICT, numConsecutiveTrueEvals, numConsecutiveTrueEvals, 0);
}

Expand All @@ -113,11 +123,17 @@ public static Dampening forStrict(String triggerId, Mode triggerMode, int numCon
* no time limit for the evaluations.
* @param triggerId the triggerId
* @param triggerMode the trigger mode for when this dampening is active
* @param numTrueEvals the numTrueEvals
* @param numTotalEvals the numTotalEvals
* @param numTrueEvals the numTrueEvals, >=1
* @param numTotalEvals the numTotalEvals, > numTotalEvals
* @return the configured Dampening
*/
public static Dampening forRelaxedCount(String triggerId, Mode triggerMode, int numTrueEvals, int numTotalEvals) {
if (numTrueEvals < 1) {
throw new IllegalArgumentException("NumTrueEvals must be >= 1");
}
if (numTotalEvals <= numTrueEvals) {
throw new IllegalArgumentException("NumTotalEvals must be > NumTrueEvals");
}
return new Dampening(triggerId, triggerMode, Type.RELAXED_COUNT, numTrueEvals, numTotalEvals, 0);
}

Expand All @@ -127,12 +143,18 @@ public static Dampening forRelaxedCount(String triggerId, Mode triggerMode, int
* the requisite data must be supplied in a timely manner.
* @param triggerId the triggerId
* @param triggerMode the trigger mode for when this dampening is active
* @param numTrueEvals the numTrueEvals
* @param numTrueEvals the numTrueEvals, >= 1.
* @param evalPeriod Elapsed real time, in milliseconds. In other words, this is not measured against
* collectionTimes (i.e. the timestamp on the data) but rather the evaluation times.
* collectionTimes (i.e. the timestamp on the data) but rather the evaluation times. >=1ms.
* @return the configured Dampening
*/
public static Dampening forRelaxedTime(String triggerId, Mode triggerMode, int numTrueEvals, long evalPeriod) {
if (numTrueEvals < 1) {
throw new IllegalArgumentException("NumTrueEvals must be >= 1");
}
if (evalPeriod < 1) {
throw new IllegalArgumentException("EvalPeriod must be >= 1ms");
}
return new Dampening(triggerId, triggerMode, Type.RELAXED_TIME, numTrueEvals, 0, evalPeriod);
}

Expand All @@ -143,10 +165,13 @@ public static Dampening forRelaxedTime(String triggerId, Mode triggerMode, int n
* @param triggerId the triggerId
* @param triggerMode the trigger mode for when this dampening is active
* @param evalPeriod Elapsed real time, in milliseconds. In other words, this is not measured against
* collectionTimes (i.e. the timestamp on the data) but rather the evaluation times.
* collectionTimes (i.e. the timestamp on the data) but rather the evaluation times. >=1ms.
* @return the configured Dampening
*/
public static Dampening forStrictTime(String triggerId, Mode triggerMode, long evalPeriod) {
if (evalPeriod < 1) {
throw new IllegalArgumentException("EvalPeriod must be >= 1ms");
}
return new Dampening(triggerId, triggerMode, Type.STRICT_TIME, 0, 0, evalPeriod);
}

Expand All @@ -157,10 +182,13 @@ public static Dampening forStrictTime(String triggerId, Mode triggerMode, long e
* @param triggerId the triggerId
* @param triggerMode the trigger mode for when this dampening is active
* @param evalPeriod Elapsed real time, in milliseconds. In other words, this is not measured against
* collectionTimes (i.e. the timestamp on the data) but rather the clock starts at true-evaluation-time-1.
* collectionTimes (i.e. the timestamp on the data) but rather the clock starts at true-evaluation-time-1. >=1ms.
* @return the configured Dampening
*/
public static Dampening forStrictTimeout(String triggerId, Mode triggerMode, long evalPeriod) {
if (evalPeriod < 1) {
throw new IllegalArgumentException("EvalPeriod must be >= 1ms");
}
return new Dampening(triggerId, triggerMode, Type.STRICT_TIMEOUT, 0, 0, evalPeriod);
}

Expand Down Expand Up @@ -263,6 +291,10 @@ public long getEvalTimeSetting() {
return evalTimeSetting;
}

public Map<Integer, ConditionEval> getCurrentEvals() {
return currentEvals;
}

@JsonIgnore
public boolean isSatisfied() {
return satisfied;
Expand Down Expand Up @@ -292,13 +324,45 @@ public void setTenantId(String tenantId) {
this.tenantId = tenantId;
}

public void perform(ConditionEval... conditionEvals) {
boolean trueEval = true;
for (ConditionEval ce : conditionEvals) {
if (!ce.isMatch()) {
public void perform(Match match, ConditionEval conditionEval) {
if (null == match) {
throw new IllegalArgumentException("Match can not be null");
}
if (null == conditionEval) {
throw new IllegalArgumentException("ConditionEval can not be null");
}

// The currentEvals map holds the most recent eval for each condition in the condition set.
currentEvals.put(conditionEval.getConditionSetIndex(), conditionEval);

boolean trueEval = false;
switch (match) {
case ALL:
// Don't perform a dampening eval until we have a conditionEval for each member of the ConditionSet.
if (currentEvals.size() < conditionEval.getConditionSetSize()) {
return;
}
// Otherwise, all condition evals must be true for the condition set eval to be true
trueEval = true;
for (ConditionEval ce : currentEvals.values()) {
if (!ce.isMatch()) {
trueEval = false;
break;
}
}
break;
case ANY:
// we only need one true condition eval for the condition set eval to be true
trueEval = false;
for (ConditionEval ce : currentEvals.values()) {
if (ce.isMatch()) {
trueEval = true;
break;
}
}
break;
}
default:
throw new IllegalArgumentException("Unexpected Match type: " + match.name());
}

// If we had previously started our time and now have exceeded our time limit then we must start over
Expand All @@ -312,7 +376,7 @@ public void perform(ConditionEval... conditionEvals) {
numEvals += 1;
if (trueEval) {
numTrueEvals += 1;
addSatisfyingEvals(conditionEvals);
addSatisfyingEvals(new HashSet<>(currentEvals.values()));

switch (type) {
case STRICT:
Expand Down Expand Up @@ -416,7 +480,7 @@ public boolean equals(Object obj) {
return false;
if (getClass() != obj.getClass())
return false;
Dampening other = (Dampening) obj;
Dampening other = (Dampening)obj;
if (dampeningId == null) {
if (other.dampeningId != null)
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private void initScheme(Session session, String keyspace) throws IOException {
keyspace = AlertProperties.getProperty(ALERTS_CASSANDRA_KEYSPACE, "hawkular_alerts");
}

log.debugf("Creating Schema for keyspace " + keyspace);
log.debugf("Checking Schema existence for keyspace: %s", keyspace);

ResultSet resultSet = session.execute("SELECT * FROM system.schema_keyspaces WHERE keyspace_name = '" +
keyspace + "'");
Expand All @@ -72,21 +72,28 @@ private void initScheme(Session session, String keyspace) throws IOException {
return;
}

log.infof("Creating Schema for keyspace %s", keyspace);

ImmutableMap<String, String> schemaVars = ImmutableMap.of("keyspace", keyspace);

String updatedCQL = null;
try (InputStream inputStream = CassCluster.class.getResourceAsStream("/hawkular-alerts-schema.cql");
InputStreamReader reader = new InputStreamReader(inputStream)) {
String content = CharStreams.toString(reader);

for (String cql : content.split("(?m)^-- #.*$")) {
if (!cql.startsWith("--")) {
String updatedCQL = substituteVars(cql.trim(), schemaVars);
updatedCQL = substituteVars(cql.trim(), schemaVars);
log.debugf("Executing CQL:\n" + updatedCQL + "\n");
session.execute(updatedCQL);
}
}
} catch (Exception e) {
log.errorf("Failed schema creation: %s\nEXECUTING CQL:\n%s", e, updatedCQL);
}
initialized = true;

log.infof("Done creating Schema for keyspace: " + keyspace);
}

private String substituteVars(String cql, Map<String, String> vars) {
Expand All @@ -103,7 +110,7 @@ private String substituteVars(String cql, Map<String, String> vars) {
}
}

public static Session getSession() throws Exception {
public static synchronized Session getSession() throws Exception {
if (cluster == null && session == null) {
String cqlPort = AlertProperties.getProperty(ALERTS_CASSANDRA_PORT, ALERTS_CASSANDRA_PORT_ENV, "9042");
String nodes = AlertProperties.getProperty(ALERTS_CASSANDRA_NODES, ALERTS_CASSANDRA_NODES_ENV, "127.0.0.1");
Expand Down

0 comments on commit ed3ef82

Please sign in to comment.