Too many dynamic script compilations within, max: [75/5m]问题处理

本文解析了ElasticSearch中因脚本编译过多导致的限流问题,介绍了两种常见解决策略:提高编译频率限制与使用filter加权评分代替动态脚本。深入探讨了ScriptService类中的限流机制,并分享了通过参数化避免重复编译的优化方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.定位问题:

a.字面意思是,ElasticSearch5分钟内执行脚本编译超过75个,编译太多而拒绝编译。编译是非常耗时的,这是ES的自我保护功能。

b.我的操作是动态评分通过script实现去了。

{
    "query": {
        "function_score": {
            "functions": [
                {
                    "script_score": {
                        "script": {
                            "source": "_score * ((doc['scoreWords'].values.contains('大米')) ? 2.0 : 1.0)",
                            "lang": "painless"
                        }
                    }
                }
            ]
        }
    }
}

2.寻求解决办法:一般而言是由两种解决办法,一是调高限制。二是采用filter+权重评分。

a.调高限制,官方操作方法:

PUT _cluster/settings
{
    "transient" : {
        "script.max_compilations_rate" : "100/1m"
    }
}

b.寻找替代方法:

 BoolQueryBuilder bool = QueryBuilders.boolQuery();
                for (String s : lexemeSet) {
                    bool.must(QueryBuilders.termQuery("searchWordsScore", s));
                }
FunctionScoreQueryBuilder.FilterFunctionBuilder mainTitle = new FunctionScoreQueryBuilder
                        .FilterFunctionBuilder(bool, ScoreFunctionBuilders.weightFactorFunction(2.0f));

3.事后查看源码,研究原理:

a.报错位置在ScriptService类里面做的限制

        // If there is enough tokens in the bucket, allow the request and decrease the tokens by 1
        if (scriptsPerMinCounter >= 1) {
            scriptsPerMinCounter -= 1.0;
        } else {
            // Otherwise reject the request
            throw new CircuitBreakingException("[script] Too many dynamic script compilations within one minute, max: [" +
                            totalCompilesPerMinute + "/min]; please use on-disk, indexed, or scripts with parameters instead; " +
                            "this limit can be changed by the [" + SCRIPT_MAX_COMPILATIONS_PER_MINUTE.getKey() + "] setting");
        }

b.从这段代码知道,编译其实是会缓存的,如果语句内容不变是不会重新编译的,继续看看这个key跟什么有关。
在这里插入图片描述
c.key和lang,code还有options,本实例是跟code有关。code的"大米是我动态传的,所以瞬间就超过了限制"。

 CacheKey cacheKey = new CacheKey(lang, idOrCode, options);

在这里插入图片描述

解决办法

将参数写入params,源码source就不需要重复编译。

GET /_search
{
    "query": {
        "function_score": {
            "query": {
                "match": { "message": "elasticsearch" }
            },
            "script_score" : {
                "script" : {
                    "params": {
                        "a": 5,
                        "b": 1.2
                    },
                    "source": "params.a / Math.pow(params.b, doc['likes'].value)"
                }
            }
        }
    }
}
PS D:\housework\housework> npm run serve > [email protected] serve > vue-cli-service serve INFO Starting development server... ERROR Failed to compile with 2 errors 16:39:55 [eslint] Invalid Options: - Unknown options: extensions - 'extensions' has been removed. You may use special comments to disable some warnings. Use // eslint-disable-next-line to ignore the next line. Use /* eslint-disable */ to ignore all warnings in a file. Error: Child compilation failed: [eslint] Invalid Options: - Unknown options: extensions - 'extensions' has been removed. - child-compiler.js:211 [housework]/[html-webpack-plugin]/lib/child-compiler.js:211:18 - Compiler.js:629 finalCallback [housework]/[webpack]/lib/Compiler.js:629:5 - Compiler.js:664 [housework]/[webpack]/lib/Compiler.js:664:11 - Compiler.js:1350 [housework]/[webpack]/lib/Compiler.js:1350:17 - task_queues:95 processTicksAndRejections node:internal/process/task_queues:95:5 - task_queues:64 runNextTicks node:internal/process/task_queues:64:3 - timers:454 process.processImmediate node:internal/timers:454:9 ERROR in [eslint] Invalid Options: - Unknown options: extensions - 'extensions' has been removed. ERROR in Error: Child compilation failed: [eslint] Invalid Options: - Unknown options: extensions - 'extensions' has been removed. - child-compiler.js:211 [housework]/[html-webpack-plugin]/lib/child-compiler.js:211:18 - Compiler.js:629 finalCallback [housework]/[webpack]/lib/Compiler.js:629:5 - Compiler.js:664 [housework]/[webpack]/lib/Compiler.js:664:11 - Compiler.js:1350 [housework]/[webpack]/lib/Compiler.js:1350:17 - task_queues:95 processTicksAndRejections node:internal/process/task_queues:95:5 - task_queues:64 runNextTicks node:internal/process/task_queues:64:3 - timers:454 process.processImmediate node:internal/timers:454:9 1 ERROR in child compilations (Use 'stats.children: true' resp. '--stats-children' for more details) webpack compiled with 3 errors
最新发布
03-08
Failed to compile with 2 errors 20:57:29 [eslint] ESLint is not a constructor You may use special comments to disable some warnings. Use // eslint-disable-next-line to ignore the next line. Use /* eslint-disable */ to ignore all warnings in a file. Error: Child compilation failed: [eslint] ESLint is not a constructor - child-compiler.js:169 [shop]/[html-webpack-plugin]/lib/child-compiler.js:169:18 - Compiler.js:551 finalCallback [shop]/[webpack]/lib/Compiler.js:551:5 - Compiler.js:577 [shop]/[webpack]/lib/Compiler.js:577:11 - Compiler.js:1199 [shop]/[webpack]/lib/Compiler.js:1199:17 - task_queues:95 processTicksAndRejections node:internal/process/task_queues:95:5 - task_queues:64 runNextTicks node:internal/process/task_queues:64:3 - timers:447 process.processImmediate node:internal/timers:447:9 ERROR in [eslint] ESLint is not a constructor ERROR in Error: Child compilation failed: [eslint] ESLint is not a constructor - child-compiler.js:169 [shop]/[html-webpack-plugin]/lib/child-compiler.js:169:18 - Compiler.js:551 finalCallback [shop]/[webpack]/lib/Compiler.js:551:5 - Compiler.js:577 [shop]/[webpack]/lib/Compiler.js:577:11 - Compiler.js:1199 [shop]/[webpack]/lib/Compiler.js:1199:17 - task_queues:95 processTicksAndRejections node:internal/process/task_queues:95:5 - task_queues:64 runNextTicks node:internal/process/task_queues:64:3 - timers:447 process.processImmediate node:internal/timers:447:9 1 ERROR in child compilations (Use 'stats.children: true' resp. '--stats-children' for more details) webpack compiled with 3 errors
06-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值