From 7f5a064bd4261df98958bf94c1d992e0aa90518c Mon Sep 17 00:00:00 2001 From: pramy Date: Fri, 8 Jun 2018 18:30:30 +0800 Subject: [PATCH] Fix the memory leak caused by groovyShell --- .../inline/InlineShardingStrategy.java | 3 +- .../shardingsphere/core/util/GroovyUtil.java | 32 +++++++++++++++++++ .../core/util/InlineExpressionParser.java | 3 +- 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 sharding-core/src/main/java/io/shardingsphere/core/util/GroovyUtil.java diff --git a/sharding-core/src/main/java/io/shardingsphere/core/routing/strategy/inline/InlineShardingStrategy.java b/sharding-core/src/main/java/io/shardingsphere/core/routing/strategy/inline/InlineShardingStrategy.java index 41033b1cf0bcd..c33e990225e15 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/routing/strategy/inline/InlineShardingStrategy.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/routing/strategy/inline/InlineShardingStrategy.java @@ -27,6 +27,7 @@ import io.shardingsphere.core.api.algorithm.sharding.ShardingValue; import io.shardingsphere.core.api.config.strategy.InlineShardingStrategyConfiguration; import io.shardingsphere.core.routing.strategy.ShardingStrategy; +import io.shardingsphere.core.util.GroovyUtil; import io.shardingsphere.core.util.InlineExpressionParser; import java.util.ArrayList; @@ -51,7 +52,7 @@ public InlineShardingStrategy(final InlineShardingStrategyConfiguration inlineSh Preconditions.checkNotNull(inlineShardingStrategyConfig.getAlgorithmExpression(), "Sharding algorithm expression cannot be null."); shardingColumn = inlineShardingStrategyConfig.getShardingColumn(); String algorithmExpression = InlineExpressionParser.handlePlaceHolder(inlineShardingStrategyConfig.getAlgorithmExpression().trim()); - closure = (Closure) new GroovyShell().evaluate(Joiner.on("").join("{it -> \"", algorithmExpression, "\"}")); + closure = (Closure) GroovyUtil.getResult(Joiner.on("").join("{it -> \"", algorithmExpression, "\"}")); } @Override diff --git a/sharding-core/src/main/java/io/shardingsphere/core/util/GroovyUtil.java b/sharding-core/src/main/java/io/shardingsphere/core/util/GroovyUtil.java new file mode 100644 index 0000000000000..7aa5365c60a21 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/util/GroovyUtil.java @@ -0,0 +1,32 @@ +package io.shardingsphere.core.util; + +import groovy.lang.GroovyShell; +import groovy.lang.Script; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Script cache + * @author Pramy + */ +public class GroovyUtil { + + + private final static Map scripts = new ConcurrentHashMap<>(); + + private final static GroovyShell shell = new GroovyShell(); + + public static Object getResult(final String expression){ + Script script; + if (scripts.containsKey(expression)) { + script = scripts.get(expression); + } + else { + script = shell.parse(expression); + scripts.put(expression, script); + } + return script.run(); + } +} + diff --git a/sharding-core/src/main/java/io/shardingsphere/core/util/InlineExpressionParser.java b/sharding-core/src/main/java/io/shardingsphere/core/util/InlineExpressionParser.java index 44262691fb8e4..22ee1cfebfdf0 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/util/InlineExpressionParser.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/util/InlineExpressionParser.java @@ -66,7 +66,6 @@ public List evaluate() { private List evaluate(final List inlineExpressions) { List result = new ArrayList<>(inlineExpressions.size()); - GroovyShell shell = new GroovyShell(); for (String each : inlineExpressions) { StringBuilder expression = new StringBuilder(handlePlaceHolder(each)); if (!each.startsWith("\"")) { @@ -75,7 +74,7 @@ private List evaluate(final List inlineExpressions) { if (!each.endsWith("\"")) { expression.append("\""); } - result.add(shell.evaluate(expression.toString())); + result.add(GroovyUtil.getResult(expression.toString())); } return result; }