-
Notifications
You must be signed in to change notification settings - Fork 125
Show and modify routing rules from the UI #433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
| } | ||
| } |
| 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. Defaults to an empty string if not provided, indicating the user intends it to be blank. | ||
| * @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"); | ||
| description = requireNonNullElse(description, ""); | ||
| priority = requireNonNullElse(priority, 0); | ||
| actions = ImmutableList.copyOf(actions); | ||
| requireNonNull(condition, "condition is null"); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Set description to
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we want to set an empty value by default? It worth leaving a code comment.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO the semantics of an empty value are more appropriate than |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| * 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.io.UncheckedIOException; | ||
| import java.nio.channels.FileChannel; | ||
| import java.nio.channels.FileLock; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.List; | ||
|
|
||
| import static java.nio.charset.StandardCharsets.UTF_8; | ||
| import static java.nio.file.StandardOpenOption.READ; | ||
| import static java.nio.file.StandardOpenOption.WRITE; | ||
|
|
||
| public class RoutingRulesManager | ||
prakhar10 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| private final String rulesConfigPath; | ||
|
|
||
| @Inject | ||
| public RoutingRulesManager(HaGatewayConfiguration configuration) | ||
| { | ||
| this.rulesConfigPath = configuration.getRoutingRules().getRulesConfigPath(); | ||
| } | ||
|
|
||
| public List<RoutingRule> getRoutingRules() | ||
| { | ||
| YAMLFactory yamlFactory = new YAMLFactory(); | ||
| ObjectMapper yamlReader = new ObjectMapper(yamlFactory); | ||
| ImmutableList.Builder<RoutingRule> routingRulesBuilder = ImmutableList.builder(); | ||
| try { | ||
| String content = Files.readString(Path.of(rulesConfigPath), UTF_8); | ||
| YAMLParser parser = 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 UncheckedIOException("Failed to read or parse routing rules configuration from path : " + rulesConfigPath, e); | ||
| } | ||
| } | ||
|
|
||
| public synchronized List<RoutingRule> updateRoutingRule(RoutingRule routingRule) | ||
| { | ||
| ImmutableList.Builder<RoutingRule> updatedRoutingRulesBuilder = ImmutableList.builder(); | ||
| List<RoutingRule> currentRoutingRulesList = getRoutingRules(); | ||
| Path path = Path.of(rulesConfigPath); | ||
| try (FileChannel fileChannel = FileChannel.open(path, WRITE, 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, yamlContent.toString(), UTF_8); | ||
| lock.release(); | ||
| } | ||
| catch (IOException e) { | ||
| throw new UncheckedIOException("Failed to parse or update routing rules configuration form path : " + rulesConfigPath, e); | ||
| } | ||
| return updatedRoutingRulesBuilder.build(); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.