Skip to content

Commit

Permalink
Add api documentation and changes related to PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
prakhar10 committed Aug 30, 2024
1 parent 79eed9b commit 85287b9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
13 changes: 13 additions & 0 deletions docs/gateway-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}'
```
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,6 +71,7 @@
import static java.util.Objects.requireNonNullElse;

@Path("/webapp")
@Singleton
public class GatewayWebAppResource
{
private static final LocalDateTime START_TIME = LocalDateTime.now();
Expand Down Expand Up @@ -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<RoutingRules> 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<RoutingRules> routingRulesList = yamlReader
.readValues(yamlParser, new TypeReference<RoutingRules>() {})
.readAll();
return Response.ok(Result.ok(routingRulesList)).build();
}
catch (IOException e) {
Expand All @@ -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<RoutingRules> 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<RoutingRules>() {})
.readAll();

for (int i = 0; i < routingRulesList.size(); i++) {
if (routingRulesList.get(i).name().equals(routingRules.name())) {
Expand Down

0 comments on commit 85287b9

Please sign in to comment.