-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show and modify routing rules from the UI
- Loading branch information
Showing
19 changed files
with
905 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
gateway-ha/src/main/java/io/trino/gateway/ha/config/UIConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* 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.trino.gateway.ha.config; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import java.util.List; | ||
|
||
public class UIConfiguration | ||
{ | ||
private List<String> disablePages; | ||
|
||
@JsonProperty | ||
public List<String> getDisablePages() | ||
{ | ||
return disablePages; | ||
} | ||
|
||
public void setDisablePages(List<String> disablePages) | ||
{ | ||
this.disablePages = disablePages; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
gateway-ha/src/main/java/io/trino/gateway/ha/domain/RoutingRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* 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.trino.gateway.ha.domain; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
|
||
import java.util.List; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static java.util.Objects.requireNonNullElse; | ||
|
||
/** | ||
* RoutingRules | ||
* | ||
* @param name name of the routing rule | ||
* @param description description of the routing rule | ||
* @param priority priority of the routing rule. Higher number represents higher priority. If two rules have same priority then order of execution is not guaranteed. | ||
* @param actions actions of the routing rule | ||
* @param condition condition of the routing rule | ||
*/ | ||
public record RoutingRule( | ||
String name, | ||
String description, | ||
Integer priority, | ||
List<String> actions, | ||
String condition) | ||
{ | ||
public RoutingRule { | ||
requireNonNull(name, "name is null"); | ||
requireNonNullElse(description, ""); | ||
requireNonNullElse(priority, 0); | ||
actions = ImmutableList.copyOf(actions); | ||
requireNonNull(condition, "condition is null"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
gateway-ha/src/main/java/io/trino/gateway/ha/router/RoutingRulesManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* 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.trino.gateway.ha.router; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLParser; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.inject.Inject; | ||
import io.trino.gateway.ha.config.HaGatewayConfiguration; | ||
import io.trino.gateway.ha.domain.RoutingRule; | ||
|
||
import java.io.IOException; | ||
import java.nio.channels.FileChannel; | ||
import java.nio.channels.FileLock; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardOpenOption; | ||
import java.util.List; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
public class RoutingRulesManager | ||
{ | ||
private final String rulesConfigPath; | ||
|
||
@Inject | ||
public RoutingRulesManager(HaGatewayConfiguration configuration) | ||
{ | ||
this.rulesConfigPath = configuration.getRoutingRules().getRulesConfigPath(); | ||
} | ||
|
||
public List<RoutingRule> getRoutingRules() | ||
throws IOException | ||
{ | ||
ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory()); | ||
ImmutableList.Builder<RoutingRule> routingRulesBuilder = ImmutableList.builder(); | ||
try { | ||
String content = Files.readString(Path.of(rulesConfigPath), UTF_8); | ||
YAMLParser parser = new YAMLFactory().createParser(content); | ||
while (parser.nextToken() != null) { | ||
RoutingRule routingRule = yamlReader.readValue(parser, RoutingRule.class); | ||
routingRulesBuilder.add(routingRule); | ||
} | ||
return routingRulesBuilder.build(); | ||
} | ||
catch (IOException e) { | ||
throw new IOException("Failed to read or parse routing rules configuration from path : " + rulesConfigPath, e); | ||
} | ||
} | ||
|
||
public synchronized List<RoutingRule> updateRoutingRule(RoutingRule routingRule) | ||
throws IOException | ||
{ | ||
ImmutableList.Builder<RoutingRule> updatedRoutingRulesBuilder = ImmutableList.builder(); | ||
List<RoutingRule> currentRoutingRulesList = getRoutingRules(); | ||
try (FileChannel fileChannel = FileChannel.open(Path.of(rulesConfigPath), StandardOpenOption.WRITE, StandardOpenOption.READ); | ||
FileLock lock = fileChannel.lock()) { | ||
ObjectMapper yamlWriter = new ObjectMapper(new YAMLFactory()); | ||
StringBuilder yamlContent = new StringBuilder(); | ||
for (RoutingRule rule : currentRoutingRulesList) { | ||
if (rule.name().equals(routingRule.name())) { | ||
yamlContent.append(yamlWriter.writeValueAsString(routingRule)); | ||
updatedRoutingRulesBuilder.add(routingRule); | ||
} | ||
else { | ||
yamlContent.append(yamlWriter.writeValueAsString(rule)); | ||
updatedRoutingRulesBuilder.add(rule); | ||
} | ||
} | ||
Files.writeString(Path.of(rulesConfigPath), yamlContent.toString(), UTF_8); | ||
lock.release(); | ||
} | ||
catch (IOException e) { | ||
throw new IOException("Failed to parse or update routing rules configuration form path : " + rulesConfigPath, e); | ||
} | ||
return updatedRoutingRulesBuilder.build(); | ||
} | ||
} |
Oops, something went wrong.