Skip to content

Commit

Permalink
Use MVEL for rule evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
willmostly committed Oct 31, 2024
1 parent 2add137 commit 2c476fc
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 7 deletions.
22 changes: 15 additions & 7 deletions gateway-ha/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,12 @@
<version>${dep.jeasy.version}</version>
</dependency>

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

<dependency>
<groupId>org.weakref</groupId>
<artifactId>jmxutils</artifactId>
Expand All @@ -290,13 +296,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 Expand Up @@ -513,6 +512,15 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>22</source>
<target>22</target>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.google.common.collect.ImmutableMap;
import jakarta.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.mvel2.MVEL.executeExpression;
import static java.util.Collections.sort;

public class MVELRoutingGroupSelector
implements RoutingGroupSelector
{
final List<MVELRule> rules;

public MVELRoutingGroupSelector(List<MVELRule> rules)
{
this.rules = new ArrayList<>(rules);
sort(this.rules);
}

// TODO: add CRUD operations for the rules

@Override
public String findRoutingGroup(HttpServletRequest request)
{
Map<String, String> result = new HashMap<>();
Map<String, Object> variables = ImmutableMap.of("result", result, "request", request);
rules.forEach(rule -> evaluateMVELRule(rule, variables));
return result.getOrDefault("routingGroup", "adhoc");
}

private void evaluateMVELRule(MVELRule rule, Map<String, Object> variables)
{
if ((boolean) executeExpression(rule.condition(), variables)) {
executeExpression(rule.action(), variables);
}
}
}
51 changes: 51 additions & 0 deletions gateway-ha/src/main/java/io/trino/gateway/ha/router/MVELRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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 java.io.Serializable;
import org.mvel2.compiler.ExecutableStatement;

import static java.util.Objects.requireNonNull;
import static java.util.Objects.requireNonNullElse;
import static org.mvel2.MVEL.compileExpression;


public record MVELRule(String name, String description, Serializable condition, Serializable action, Integer priority)
implements Comparable<MVELRule>
{
// String is Serializable - how can we ensure the condition and action are compiled?
public MVELRule(String name, String description, Serializable condition, Serializable action, Integer priority)
{
if (condition instanceof String stringCondition) {
condition = compileExpression(stringCondition);
}
if (action instanceof String stringAction) {
action = compileExpression(stringAction);
}
this.name = requireNonNull(name, "name is null");
this.description = requireNonNullElse(description, "");
this.condition = requireNonNull(condition, "condition is null");
this.action = requireNonNull(action, "action is null");
this.priority = requireNonNull(priority, "priority is null");
}

@Override
public int compareTo(MVELRule o)
{
if (o == null) {
return 1;
}
return priority.compareTo(o.priority);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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;

import com.google.common.collect.ImmutableList;
import io.trino.gateway.ha.router.MVELRoutingGroupSelector;
import io.trino.gateway.ha.router.MVELRule;
import jakarta.servlet.http.HttpServletRequest;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

public class TestMVELRoutingGroupSelector
{

@Test
void testMVELRoutingGroupSelector()
{
String condition = "request != null";
String action = "result.put(\"routingGroup\", \"custom\")";
List<MVELRule> rules = ImmutableList.of(new MVELRule("test", "test rule", condition, action, 0));
MVELRoutingGroupSelector mvelRoutingGroupSelector = new MVELRoutingGroupSelector(rules);

HttpServletRequest mockRequest = mock(HttpServletRequest.class);
assertThat(mvelRoutingGroupSelector.findRoutingGroup(mockRequest)).isEqualTo("custom");
}
}

0 comments on commit 2c476fc

Please sign in to comment.