Skip to content

Commit

Permalink
Use MVEL for rule evaluation
Browse files Browse the repository at this point in the history
Co-Authored-By: Prakhar Sapre <[email protected]>
  • Loading branch information
willmostly and prakhar10 committed Nov 1, 2024
1 parent 2add137 commit d2d6bfa
Show file tree
Hide file tree
Showing 8 changed files with 291 additions and 151 deletions.
26 changes: 3 additions & 23 deletions gateway-ha/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
<frontend.pnpmRegistryURL>https://registry.npmmirror.com</frontend.pnpmRegistryURL>

<!-- dependency versions -->
<dep.jeasy.version>4.1.0</dep.jeasy.version>
<dep.mockito.version>5.14.2</dep.mockito.version>
<dep.okhttp3.version>4.12.0</dep.okhttp3.version>
<dep.trino.version>462</dep.trino.version>
Expand Down Expand Up @@ -253,21 +252,9 @@
</dependency>

<dependency>
<groupId>org.jeasy</groupId>
<artifactId>easy-rules-core</artifactId>
<version>${dep.jeasy.version}</version>
</dependency>

<dependency>
<groupId>org.jeasy</groupId>
<artifactId>easy-rules-mvel</artifactId>
<version>${dep.jeasy.version}</version>
</dependency>

<dependency>
<groupId>org.jeasy</groupId>
<artifactId>easy-rules-support</artifactId>
<version>${dep.jeasy.version}</version>
<groupId>org.mvel</groupId>
<artifactId>mvel2</artifactId>
<version>2.5.2.Final</version>
</dependency>

<dependency>
Expand All @@ -290,13 +277,6 @@
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.mvel</groupId>
<artifactId>mvel2</artifactId>
<version>2.5.2.Final</version>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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 io.trino.gateway.ha.config.RequestAnalyzerConfig;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;

public class FileReloadingMVELRoutingGroupSelector
extends RulesRoutingGroupSelector
{
Path rulesPath;

FileReloadingMVELRoutingGroupSelector(String rulesPath, RequestAnalyzerConfig requestAnalyzerConfig)
{
super(requestAnalyzerConfig);
this.rulesPath = Paths.get(rulesPath);

List<RoutingRule> ruleList = readRulesFromPath(this.rulesPath, MVELRoutingRule.class);
setRules(ruleList);
}

@Override
void reloadRules(long lastUpdatedTimeMillis)
{
try {
BasicFileAttributes attr = Files.readAttributes(this.rulesPath, BasicFileAttributes.class);
if (attr.lastModifiedTime().toMillis() > lastUpdatedTimeMillis) {
synchronized (this) {
if (attr.lastModifiedTime().toMillis() > lastUpdatedTimeMillis) {
List<RoutingRule> ruleList = readRulesFromPath(this.rulesPath, MVELRoutingRule.class);
setRules(ruleList);
}
}
}
}
catch (IOException e) {
throw new RuntimeException("Could not access rules file", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static org.mvel2.MVEL.compileExpression;
import static org.mvel2.MVEL.executeExpression;

public class MVELRoutingRule
extends RoutingRule
{
@JsonCreator
public MVELRoutingRule(
@JsonProperty("name") String name,
@JsonProperty("description") String description,
@JsonProperty("priority") Integer priority,
@JsonProperty("condition") Serializable condition,
@JsonProperty("actions") List<Serializable> actions)
{
super(
name,
description,
priority,
condition instanceof String stringCondition ? compileExpression(stringCondition) : condition,
actions.stream().map(action -> {
if (action instanceof String stringAction) {
return compileExpression(stringAction);
}
else {
return action;
}
}).collect(toImmutableList()));
}

@Override
public void evaluate(Map<String, Object> variables)
{
if ((boolean) executeExpression(condition, variables)) {
actions.forEach(action -> executeExpression(action, variables));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static RoutingGroupSelector byRoutingGroupHeader()
*/
static RoutingGroupSelector byRoutingRulesEngine(String rulesConfigPath, RequestAnalyzerConfig requestAnalyzerConfig)
{
return new RuleReloadingRoutingGroupSelector(rulesConfigPath, requestAnalyzerConfig);
return new FileReloadingMVELRoutingGroupSelector(rulesConfigPath, requestAnalyzerConfig);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import static java.util.Objects.requireNonNull;
import static java.util.Objects.requireNonNullElse;

public abstract class RoutingRule
implements Comparable<RoutingRule>
{
String name;
String description;
Integer priority;
Serializable condition;
List<Serializable> actions;

@JsonCreator
public RoutingRule(
@JsonProperty("name") String name,
@JsonProperty("description") String description,
@JsonProperty("priority") Integer priority,
@JsonProperty("condition") Serializable condition,
@JsonProperty("actions") List<Serializable> actions)
{
this.name = requireNonNull(name, "name is null");
this.description = requireNonNullElse(description, "");
this.priority = requireNonNullElse(priority, 0);
this.condition = requireNonNull(condition, "condition is null");
this.actions = requireNonNull(actions, "actions is null");
}

public abstract void evaluate(Map<String, Object> variables);

@Override
public int compareTo(RoutingRule o)
{
if (o == null) {
return 1;
}
return priority.compareTo(o.priority);
}
}

This file was deleted.

Loading

0 comments on commit d2d6bfa

Please sign in to comment.