Skip to content

Commit 85287b9

Browse files
committed
Add api documentation and changes related to PR comments
1 parent 79eed9b commit 85287b9

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

docs/gateway-api.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,16 @@ Will return a JSON array of active Trino cluster backends:
9191
curl -X POST http://localhost:8080/gateway/backend/activate/trino-2
9292
```
9393

94+
## Update Routing Rules
95+
96+
This API can be used to programmatically update the Routing Rules.
97+
Rule will be updated based on the rule name.
98+
```shell
99+
curl -X POST http://localhost:8080/webapp/updateRoutingRules \
100+
-d '{ "name": "trino-rule",
101+
"description": "updated rule description",
102+
"priority": 0,
103+
"actions": ["updated action"],
104+
"condition": "updated condition"
105+
}'
106+
```

gateway-ha/src/main/java/io/trino/gateway/ha/domain/RoutingRules.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
*/
1414
package io.trino.gateway.ha.domain;
1515

16-
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
1716
import com.fasterxml.jackson.annotation.JsonProperty;
1817

1918
import java.util.List;
@@ -27,7 +26,6 @@
2726
* @param actions actions of the routing rule
2827
* @param condition condition of the routing rule
2928
*/
30-
@JsonIgnoreProperties(ignoreUnknown = true)
3129
public record RoutingRules(
3230
@JsonProperty("name") String name,
3331
@JsonProperty("description") String description,

gateway-ha/src/main/java/io/trino/gateway/ha/resource/GatewayWebAppResource.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
*/
1414
package io.trino.gateway.ha.resource;
1515

16+
import com.fasterxml.jackson.core.type.TypeReference;
1617
import com.fasterxml.jackson.databind.ObjectMapper;
1718
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
1819
import com.fasterxml.jackson.dataformat.yaml.YAMLParser;
1920
import com.google.common.base.Strings;
2021
import com.google.inject.Inject;
22+
import com.google.inject.Singleton;
2123
import io.trino.gateway.ha.clustermonitor.ClusterStats;
2224
import io.trino.gateway.ha.config.HaGatewayConfiguration;
2325
import io.trino.gateway.ha.config.ProxyBackendConfiguration;
@@ -69,6 +71,7 @@
6971
import static java.util.Objects.requireNonNullElse;
7072

7173
@Path("/webapp")
74+
@Singleton
7275
public class GatewayWebAppResource
7376
{
7477
private static final LocalDateTime START_TIME = LocalDateTime.now();
@@ -443,17 +446,14 @@ public Response readExactMatchSourceSelector()
443446
@Path("/getRoutingRules")
444447
public Response getRoutingRules()
445448
{
446-
String content = null;
447449
try {
448450
String rulesConfigPath = configuration.getRoutingRules().getRulesConfigPath();
449-
content = new String(Files.readAllBytes(Paths.get(rulesConfigPath)));
450-
ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());
451-
YAMLParser parser = new YAMLFactory().createParser(content);
452-
List<RoutingRules> routingRulesList = new ArrayList<>();
453-
while (parser.nextToken() != null) {
454-
RoutingRules routingRules = yamlReader.readValue(parser, RoutingRules.class);
455-
routingRulesList.add(routingRules);
456-
}
451+
YAMLFactory yamlFactory = new YAMLFactory();
452+
ObjectMapper yamlReader = new ObjectMapper(yamlFactory);
453+
YAMLParser yamlParser = yamlFactory.createParser(new String(Files.readAllBytes(Paths.get(rulesConfigPath))));
454+
List<RoutingRules> routingRulesList = yamlReader
455+
.readValues(yamlParser, new TypeReference<RoutingRules>() {})
456+
.readAll();
457457
return Response.ok(Result.ok(routingRulesList)).build();
458458
}
459459
catch (IOException e) {
@@ -466,19 +466,17 @@ public Response getRoutingRules()
466466
@Consumes(MediaType.APPLICATION_JSON)
467467
@Produces(MediaType.APPLICATION_JSON)
468468
@Path("/updateRoutingRules")
469-
public Response updateRoutingRules(RoutingRules routingRules)
469+
public synchronized Response updateRoutingRules(RoutingRules routingRules)
470470
{
471471
String rulesConfigPath = configuration.getRoutingRules().getRulesConfigPath();
472472
ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());
473473
List<RoutingRules> routingRulesList = new ArrayList<>();
474-
474+
YAMLFactory yamlFactory = new YAMLFactory();
475475
try {
476-
String content = new String(Files.readAllBytes(Paths.get(rulesConfigPath)));
477-
YAMLParser parser = new YAMLFactory().createParser(content);
478-
while (parser.nextToken() != null) {
479-
RoutingRules routingRule = yamlReader.readValue(parser, RoutingRules.class);
480-
routingRulesList.add(routingRule);
481-
}
476+
YAMLParser yamlParser = yamlFactory.createParser(new String(Files.readAllBytes(Paths.get(rulesConfigPath))));
477+
routingRulesList = yamlReader
478+
.readValues(yamlParser, new TypeReference<RoutingRules>() {})
479+
.readAll();
482480

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

0 commit comments

Comments
 (0)