Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions src/main/java/io/cryostat/agent/remote/SmartTriggersContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright The Cryostat Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.cryostat.agent.remote;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import io.cryostat.agent.triggers.TriggerEvaluator;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.net.httpserver.HttpExchange;
import jakarta.inject.Inject;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SmartTriggersContext implements RemoteContext {

private Logger log = LoggerFactory.getLogger(getClass());
private TriggerEvaluator evaluator;
private ObjectMapper mapper;

@Inject
SmartTriggersContext(ObjectMapper mapper, TriggerEvaluator evaluator) {
this.evaluator = evaluator;
this.mapper = mapper;
}

@Override
public void handle(HttpExchange exchange) throws IOException {
try {
String mtd = exchange.getRequestMethod();
switch (mtd) {
case "GET":
// Query the currently loaded smart triggers
exchange.sendResponseHeaders(HttpStatus.SC_OK, BODY_LENGTH_UNKNOWN);
try (OutputStream response = exchange.getResponseBody()) {
mapper.writeValue(response, evaluator.getDefinitions());
}
break;
case "POST":
try (InputStream body = exchange.getRequestBody()) {
SmartTriggerRequest req = mapper.readValue(body, SmartTriggerRequest.class);
boolean resp = evaluator.append(req.definitions);
if (!resp) {
exchange.sendResponseHeaders(
HttpStatus.SC_BAD_REQUEST, BODY_LENGTH_NONE);
} else {
exchange.sendResponseHeaders(HttpStatus.SC_ACCEPTED, BODY_LENGTH_NONE);
}
} catch (Exception e) {
log.warn("Smart trigger serialization failure", e);
exchange.sendResponseHeaders(HttpStatus.SC_BAD_GATEWAY, BODY_LENGTH_NONE);
}
break;
case "DELETE":
try (InputStream body = exchange.getRequestBody()) {
SmartTriggerRequest req = mapper.readValue(body, SmartTriggerRequest.class);
boolean resp = evaluator.remove(req.definitions);
if (!resp) {
exchange.sendResponseHeaders(
HttpStatus.SC_BAD_REQUEST, BODY_LENGTH_NONE);
} else {
exchange.sendResponseHeaders(HttpStatus.SC_ACCEPTED, BODY_LENGTH_NONE);
}
} catch (Exception e) {
log.warn("Smart trigger serialization failure", e);
exchange.sendResponseHeaders(HttpStatus.SC_BAD_GATEWAY, BODY_LENGTH_NONE);
}
break;
default:
log.warn("Unknown request method {}", mtd);
exchange.sendResponseHeaders(
HttpStatus.SC_METHOD_NOT_ALLOWED, BODY_LENGTH_NONE);
break;
}
} finally {
exchange.close();
}
}

@Override
public String path() {
return "/smart-triggers/";
}

static class SmartTriggerRequest {

// This is fine as one string, the TriggerParser can handle it
// if there are multiple triggers defined.
public String definitions;
}
}
153 changes: 0 additions & 153 deletions src/main/java/io/cryostat/agent/triggers/SmartTrigger.java

This file was deleted.

40 changes: 39 additions & 1 deletion src/main/java/io/cryostat/agent/triggers/TriggerEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
import io.cryostat.agent.FlightRecorderHelper;
import io.cryostat.agent.harvest.Harvester;
import io.cryostat.agent.model.MBeanInfo;
import io.cryostat.agent.triggers.SmartTrigger.TriggerState;
import io.cryostat.libcryostat.triggers.SmartTrigger;
import io.cryostat.libcryostat.triggers.SmartTrigger.TriggerState;

import com.google.api.expr.v1alpha1.Decl;
import com.google.api.expr.v1alpha1.Type;
Expand Down Expand Up @@ -86,6 +87,39 @@ public void start(String args) {
this.start();
}

// start(args) will re-parse the triggers directory, we don't need to do that
// for requests that come in later through the api since existing triggers
// are already stored.
public boolean append(String definitions) {
// Sanity check the trigger definitions before we stop/start the evaluation
if (!parser.isValid(definitions)) {
log.warn("Invalid Trigger definition {}", definitions);
return false;
}
this.stop();
parser.parse(definitions).forEach(this::registerTrigger);
this.start();
return true;
}

public boolean remove(String definitions) {
// Sanity check the trigger definitions before touching the thread
if (!parser.isValid(definitions)) {
log.warn("Invalid Trigger definition {}", definitions);
return false;
}
List<SmartTrigger> triggers = parser.parse(definitions);
// Sanity check the triggers actually exist
if (!this.triggers.containsAll(triggers)) {
return false;
}

this.stop();
this.triggers.removeAll(triggers);
this.start();
return true;
}

public void stop() {
if (this.task != null) {
this.task.cancel(false);
Expand Down Expand Up @@ -261,4 +295,8 @@ else if (obj.getClass().equals(Long.class))
// Default to String so we can still do some comparison
return Decls.String;
}

public List<String> getDefinitions() {
return definitions;
}
}
12 changes: 12 additions & 0 deletions src/main/java/io/cryostat/agent/triggers/TriggerParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import io.cryostat.agent.FlightRecorderHelper;
import io.cryostat.agent.util.StringUtils;
import io.cryostat.libcryostat.triggers.SmartTrigger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -117,4 +118,15 @@ public List<SmartTrigger> parse(String str) {
}
return triggers;
}

public boolean isValid(String definition) {
String[] expressions = definition.split(",");
for (String s : expressions) {
Matcher m = EXPRESSION_PATTERN.matcher(s);
if (!m.matches()) {
return false;
}
}
return true;
}
}
12 changes: 11 additions & 1 deletion src/test/java/io/cryostat/agent/triggers/TriggerParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.cryostat.agent.triggers;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
Expand All @@ -24,7 +26,8 @@
import java.util.stream.Stream;

import io.cryostat.agent.FlightRecorderHelper;
import io.cryostat.agent.triggers.SmartTrigger.TriggerState;
import io.cryostat.libcryostat.triggers.SmartTrigger;
import io.cryostat.libcryostat.triggers.SmartTrigger.TriggerState;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
Expand Down Expand Up @@ -267,6 +270,13 @@ void testMultipleComplexTriggerWithWhitespaceWhenOnlyOneTemplateValid() {
MatcherAssert.assertThat(trigger1.getTimeConditionFirstMet(), Matchers.nullValue());
}

@Test
void testTriggerValidation() {
assertEquals(false, parser.isValid("foo"));
assertEquals(false, parser.isValid("foo~bar"));
assertEquals(true, parser.isValid("[ProcessCpuLoad>0.2]~profile"));
}

static List<List<String>> emptyCases() {
List<List<String>> l = new ArrayList<>();
l.add(List.of());
Expand Down
Loading