diff --git a/docs/gateway-api.md b/docs/gateway-api.md index 87f30575a..6f8d0afd1 100644 --- a/docs/gateway-api.md +++ b/docs/gateway-api.md @@ -91,3 +91,16 @@ Will return a JSON array of active Trino cluster backends: curl -X POST http://localhost:8080/gateway/backend/activate/trino-2 ``` +## Update Routing Rules + +This API can be used to programmatically update the Routing Rules. +Rule will be updated based on the rule name. +```shell +curl -X POST http://localhost:8080/webapp/updateRoutingRules \ + -d '{ "name": "trino-rule", + "description": "updated rule description", + "priority": 0, + "actions": ["updated action"], + "condition": "updated condition" + }' +``` diff --git a/gateway-ha/src/main/java/io/trino/gateway/ha/domain/RoutingRules.java b/gateway-ha/src/main/java/io/trino/gateway/ha/domain/RoutingRules.java index a70c03e51..8fef950d2 100644 --- a/gateway-ha/src/main/java/io/trino/gateway/ha/domain/RoutingRules.java +++ b/gateway-ha/src/main/java/io/trino/gateway/ha/domain/RoutingRules.java @@ -13,7 +13,6 @@ */ package io.trino.gateway.ha.domain; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import java.util.List; @@ -27,7 +26,6 @@ * @param actions actions of the routing rule * @param condition condition of the routing rule */ -@JsonIgnoreProperties(ignoreUnknown = true) public record RoutingRules( @JsonProperty("name") String name, @JsonProperty("description") String description, diff --git a/gateway-ha/src/main/java/io/trino/gateway/ha/resource/GatewayWebAppResource.java b/gateway-ha/src/main/java/io/trino/gateway/ha/resource/GatewayWebAppResource.java index 62f3e8413..b8056fafd 100644 --- a/gateway-ha/src/main/java/io/trino/gateway/ha/resource/GatewayWebAppResource.java +++ b/gateway-ha/src/main/java/io/trino/gateway/ha/resource/GatewayWebAppResource.java @@ -13,11 +13,13 @@ */ package io.trino.gateway.ha.resource; +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import com.fasterxml.jackson.dataformat.yaml.YAMLParser; import com.google.common.base.Strings; import com.google.inject.Inject; +import com.google.inject.Singleton; import io.trino.gateway.ha.clustermonitor.ClusterStats; import io.trino.gateway.ha.config.HaGatewayConfiguration; import io.trino.gateway.ha.config.ProxyBackendConfiguration; @@ -69,6 +71,7 @@ import static java.util.Objects.requireNonNullElse; @Path("/webapp") +@Singleton public class GatewayWebAppResource { private static final LocalDateTime START_TIME = LocalDateTime.now(); @@ -443,17 +446,14 @@ public Response readExactMatchSourceSelector() @Path("/getRoutingRules") public Response getRoutingRules() { - String content = null; try { String rulesConfigPath = configuration.getRoutingRules().getRulesConfigPath(); - content = new String(Files.readAllBytes(Paths.get(rulesConfigPath))); - ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory()); - YAMLParser parser = new YAMLFactory().createParser(content); - List routingRulesList = new ArrayList<>(); - while (parser.nextToken() != null) { - RoutingRules routingRules = yamlReader.readValue(parser, RoutingRules.class); - routingRulesList.add(routingRules); - } + YAMLFactory yamlFactory = new YAMLFactory(); + ObjectMapper yamlReader = new ObjectMapper(yamlFactory); + YAMLParser yamlParser = yamlFactory.createParser(new String(Files.readAllBytes(Paths.get(rulesConfigPath)))); + List routingRulesList = yamlReader + .readValues(yamlParser, new TypeReference() {}) + .readAll(); return Response.ok(Result.ok(routingRulesList)).build(); } catch (IOException e) { @@ -466,19 +466,17 @@ public Response getRoutingRules() @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Path("/updateRoutingRules") - public Response updateRoutingRules(RoutingRules routingRules) + public synchronized Response updateRoutingRules(RoutingRules routingRules) { String rulesConfigPath = configuration.getRoutingRules().getRulesConfigPath(); ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory()); List routingRulesList = new ArrayList<>(); - + YAMLFactory yamlFactory = new YAMLFactory(); try { - String content = new String(Files.readAllBytes(Paths.get(rulesConfigPath))); - YAMLParser parser = new YAMLFactory().createParser(content); - while (parser.nextToken() != null) { - RoutingRules routingRule = yamlReader.readValue(parser, RoutingRules.class); - routingRulesList.add(routingRule); - } + YAMLParser yamlParser = yamlFactory.createParser(new String(Files.readAllBytes(Paths.get(rulesConfigPath)))); + routingRulesList = yamlReader + .readValues(yamlParser, new TypeReference() {}) + .readAll(); for (int i = 0; i < routingRulesList.size(); i++) { if (routingRulesList.get(i).name().equals(routingRules.name())) {