6.0 的 Settings 变更共 11 条:配置文件只支持
elasticsearch.yml(.yaml/.json存在即拒绝启动);配置文件里的重复键报错;布尔设置只认true/false(6.0 之前建的索引上的索引级设置例外,节点级设置不豁免);内部设置cluster.routing.allocation.snapshot.relocation_enabled移除;存储限速设置移除;index.store.type的default值移除;阻塞式 TCP/HTTP 与几个 netty 设置移除;basesimilarity 被忽略;脚本安全设置换成script.allowed_types/script.allowed_contexts;discovery.type不再接受gce/azure/ec2;search.remote.*改名cluster.remote.*。
说明
实测环境:本地起两个单节点实例——Elasticsearch 5.6.16 与 6.8.23(官方 tar 包直接跑,堆内存 512MB),5.6 那次把配置写进它自己的 config/elasticsearch.yml 并在跑完后还原,6.8 那次用 ES_PATH_CONF 指向临时目录,两次都用独立 path.data 与端口(9406 / 9407)。图 4 上中下三段出自这两个实例。
来源和边界先交代:本文的依据出自 v6.8.23 标签下的官方文件(settings.asciidoc、InternalSettingsPreparer.java、Settings.java),文中引用的原文均来自这些文件。示例取自官方示例;哪些是文档写明、哪些是我的推断,正文里逐处标注。
以下均出自 6.0 迁移文档(migrate_6_0/settings.asciidoc @ v6.8.23),共 113 行 11 条,下面逐条给出行号。
图 1:官方文件 settings.asciidoc(v6.8.23 标签)。来源:GitHub elastic/elasticsearch v6.8.23 settings.asciidoc
配置文件只支持 elasticsearch.yml
The configuration file found in the Elasticsearch config directory could previously have a
.yml,.yamlor.jsonextension. Onlyelasticsearch.ymlis now supported.
以前 elasticsearch.yml、elasticsearch.yaml、elasticsearch.json 三种都读,6.0 起只认 .yml。
实际行为是报错,不是静默忽略
InternalSettingsPreparer.prepareEnvironment 先检查 config 目录里有没有 elasticsearch.yaml 和 elasticsearch.json,有就直接抛 SettingsException:
elasticsearch.yaml was deprecated in 5.5.0 and must be renamed to elasticsearch.ymlelasticsearch.json was deprecated in 5.5.0 and must be converted to elasticsearch.yml
见 InternalSettingsPreparer.java @ v6.8.23 第 88–94 行。也就是说这两个文件留着,节点是起不来的,日志里也写得很清楚——它不是那种需要核对才能发现的失效。
真正会静默失效的是另一种情况:config 目录里没有 elasticsearch.yml。代码只在文件存在时才 loadFromPath(同文件第 97–104 行),不存在就按默认值 + 环境变量 + 启动参数走,没有任何提示。配置挂载到别的路径、或者用配置管理生成了 es.yml 这类名字,就落进这种情况:节点正常起来,集群名、路径、角色全用默认值。
所以迁移动作是两件:把 .yaml/.json 转成 elasticsearch.yml 并删掉原文件(否则拒绝启动),再确认 elasticsearch.yml 确实在 config 目录下(否则静默按默认启动)。
图 2:官方文件 InternalSettingsPreparer.java(v6.8.23 标签)。来源:GitHub elastic/elasticsearch v6.8.23 InternalSettingsPreparer.java
配置文件里的重复键
In previous versions of Elasticsearch, the configuration file was allowed to contain duplicate keys.
以前允许这样写:
node:
name: my-node
node:
attr:
rack: my-rack
6.0 起必须合并成一个 key:
node:
name: my-node
attr:
rack: my-rack
报错来自 Settings 加载时的检查,错误文本里带键名、行列号和前后两个值(Settings.java 第 722 行):duplicate settings key [...] found at line number [...], column number [...], previous value [...], current value [...]。
方向跟 REST 层「重复键报错」一致。YAML 里重复键在编辑配置时很容易出现——同一个 node: 块在文件里写了两处,缩进看着都对。以前哪个生效取决于解析实现,现在直接拒绝,排查成本反而低。
排查方法见后面「升级前扫一遍配置文件」那一节。
图 3:官方文件 Settings.java(v6.8.23 标签)。来源:GitHub elastic/elasticsearch v6.8.23 Settings.java
布尔设置严格化
Previously, Elasticsearch recognized the strings
true,false,on,off,yes,no,0,1as booleans. Elasticsearch 6.0 recognizes onlytrueandfalseas boolean and will throw an error otherwise.
兼容范围有明确限定:
For backwards compatibility purposes, during the 6.x series index settings on pre-6.0 indices will continue to work. Note that this does not apply to node-level settings that are stored in
elasticsearch.yml.
两层意思:
- 6.0 之前创建的索引上的索引级布尔设置,6.x 期间仍按旧规则工作;
elasticsearch.yml里的节点级设置不豁免——立刻改成true/false。
5.x 与 6.x 的实现差异
5.6 的 Setting.parseBoolean 走的是「宽松解析 + 打废弃日志」:先 Booleans.parseBooleanExact(value),再判断 Booleans.isStrictlyBoolean(value),不是严格布尔值时打一条 Expected a boolean [true/false] for setting [{}] but got [{}](v5.6.16 的 Setting.java 第 862–870 行)。也就是说 5.x 阶段 node.data: 0 能生效,只是日志里有提示。
6.8 的实现换成了硬错误:Setting.parseBoolean 把解析交给 Booleans.parseBoolean(Setting.java 第 1152–1162 行),而后者只认 true/false,其余一律抛 Failed to parse value [...] as only [true] or [false] are allowed.(Booleans.java 第 66–74 行)。
这条是 11 条里唯一会让节点起不来的:elasticsearch.yml 里出现 node.data: 0、node.master: off 这类写法,启动阶段就失败。排查时先扫布尔值,再管其他。
快照相关的内部设置移除
The internal setting
cluster.routing.allocation.snapshot.relocation_enabledthat allowed shards with running snapshots to be reallocated to different nodes has been removed. Enabling this setting could cause allocation issues if a shard got allocated off a node and then reallocated back to this node while a snapshot was running.
这个设置允许「正在做快照的分片被重新分配到别的节点」。移除理由是分配问题:分片从某节点调走、快照运行期间又被调回来,快照逻辑会失去对数据的引用。
5.6 里它还定义在 SnapshotInProgressAllocationDecider.java 第 43 行,默认 false;6.8 的源码里已经搜不到。它是内部设置,默认关闭,配置里出现就删掉——留着会因为未知设置启动失败。
同一节里另外七条
存储限速设置移除
Store throttling has been removed. As a consequence, the
indices.store.throttle.typeandindices.store.throttle.max_bytes_per_seccluster settings and theindex.store.throttle.typeandindex.store.throttle.max_bytes_per_secindex settings are not recognized anymore.
四个设置全部不再是已知设置,配置里留着会启动失败。限速能力本身在 6.0 之前就已经失效,这次是把配置项清掉。
index.store.type 的 default 值移除
The
defaultindex.store.typehas been removed. If you were using it, we advise that you simply remove it from your index settings and Elasticsearch will use the beststoreimplementation for your operating system.
注意是「值」被移除,不是设置本身:index.store.type 这个设置在 6.8 里还在(IndexModule.java 第 95–96 行),但写 default 不再被识别。索引设置里出现它,直接删掉这一项,让 ES 按操作系统选实现。
阻塞式传输与几个 netty 设置移除
The blocking TCP client, blocking TCP server, and blocking HTTP server have been removed. As a consequence, the
network.tcp.blocking_server,network.tcp.blocking_client,network.tcp.blocking,transport.tcp.blocking_client,transport.tcp.blocking_server, andhttp.tcp.blocking_serversettings are not recognized anymore.The previously unused settings
transport.netty.max_cumulation_buffer_capacity,transport.netty.max_composite_buffer_componentsandhttp.netty.max_cumulation_buffer_capacityhave been removed.
九个设置名,逐个删:六个阻塞式相关(network.tcp.blocking_server、network.tcp.blocking_client、network.tcp.blocking、transport.tcp.blocking_client、transport.tcp.blocking_server、http.tcp.blocking_server),三个从未生效的 netty 缓冲设置(transport.netty.max_cumulation_buffer_capacity、transport.netty.max_composite_buffer_components、http.netty.max_cumulation_buffer_capacity)。
base similarity 被忽略
The
basesimilarity is now ignored as coords and query normalization have been removed. If provided, this setting will be ignored and issue a deprecation warning.
协调因子(coords)与查询归一化在 6.0 移除,base similarity 失去了存在意义。写了它不报错,只忽略并打废弃日志——不启动失败,但打分行为按新的默认走。这条不阻断升级,要留在升级后的观察项里。
脚本安全设置换成两个白名单
All of the existing scripting security settings have been removed. Instead they are replaced with
script.allowed_typesandscript.allowed_contexts.
两个新设置都定义在 ScriptService.java 第 111–113 行,是 listSetting、NodeScope,默认空列表。老的脚本安全设置要整体替换,不是加两个新项。
discovery.type 不再接受云厂商值
The
discovery.typesettings no longer supports the valuesgce,azureandec2. Integration with these platforms should be done by setting thediscovery.zen.hosts_providersetting to one of those values.
discovery.type: ec2 这类写法不再被接受,改成 discovery.zen.hosts_provider: ec2(gce、azure 同理)。
search.remote.* 改名 cluster.remote.*
The cross-cluster search remote cluster connection infrastructure is also used in cross-cluster replication. This means that the setting names
search.remote.*used for configuring cross-cluster search belie the fact that they also apply to other situations where a connection to a remote cluster as used. Therefore, these settings have been renamed fromsearch.remote.*tocluster.remote.*. For backwards compatibility purposes, we will fallback tosearch.remote.*ifcluster.remote.*is not set. For any such settings stored in the cluster state, or set on dynamic settings updates, we will automatically upgrade the setting fromsearch.remote.*tocluster.remote.*. The fallback settings will be removed in 8.0.0.
改名的原因是这套远程连接设施已经不只给跨集群搜索用(也用于跨集群复制),search.remote 这个名字对不上了。兼容做法有三层:没配 cluster.remote.* 时回退读 search.remote.*;集群状态里存的和动态更新的设置会自动升级成新名;回退在 8.0.0 移除——源码里也留了这条注释(RemoteClusterService.java 第 77 行)。
所以 6.x 期间老配置还能用,但要在这期间迁完,8.0 之后 search.remote.* 就是未知设置。
升级前扫一遍配置文件
# 1) 配置文件名:.yaml / .json 存在会拒绝启动
ls /etc/elasticsearch/*.yml /etc/elasticsearch/*.yaml /etc/elasticsearch/*.json 2>/dev/null
# 2) 布尔宽松写法(只认 true/false,命中即启动失败)
grep -nE ":\s*(yes|no|on|off|1|0)\s*(#.*)?$" /etc/elasticsearch/elasticsearch.yml
# 3) 已移除的设置
grep -nE "snapshot\.relocation_enabled|store\.throttle|blocking|netty\.max_(cumulation_buffer_capacity|composite_buffer_components)|^script\.(inline|stored|file)|search\.remote" \
/etc/elasticsearch/elasticsearch.yml
第 2 步会误伤:on/off/1/0 也可能是某个字符串的合法值,要结合键名判断,命中后逐个确认。
重复键用解析器查,肉眼看几百行的文件不可靠:
python3 - <<'EOF'
import collections
keys = collections.defaultdict(list)
with open('/etc/elasticsearch/elasticsearch.yml') as f:
for i, line in enumerate(f, 1):
s = line.strip()
if s and not s.startswith('#') and ':' in s and not s.startswith('-'):
keys[s.split(':', 1)[0].strip()].append(i)
dup = {k: v for k, v in keys.items() if len(v) > 1}
if not dup:
print('没有重复的一级键')
for k, v in dup.items():
print('重复键:', k, '行号', v)
EOF
这个脚本只查一级键(YAML 里重复键最常出现在这一层),嵌套层的重复它查不出来;要完整覆盖可以用 YAML 解析器加载后检测,但配置里常有 ${} 占位符,解析前要替换掉。
核对实际生效值
改完不看配置文件,看接口:
curl -s 'localhost:9200/_cluster/settings?include_defaults=true' | python3 -m json.tool
curl -s 'localhost:9200/_nodes/settings?flat_settings=true' | python3 -m json.tool
curl -s 'localhost:9200/_nodes/jvm' | python3 -c "
import sys, json
d = json.load(sys.stdin)
for n in d['nodes'].values():
print(n['name'], n['jvm']['mem']['heap_max_in_bytes'] / 1024**3, 'GB')
"
重点核堆大小、集群名、节点角色、数据目录四项。这一步要写进部署流程,不是靠人记得:容器化部署里配置靠挂载传入,最容易出的是「挂到别的路径」或「文件名不对」,两种情况服务都能起来。
升级 6.0 的配置检查清单
- 转成
elasticsearch.yml并删掉.yaml/.json,确认文件在 config 目录下; - 合并重复键,尤其
node:、path:、cluster:这几个常见块; elasticsearch.yml里的布尔值全部改成true/false,优先级最高——这条会让节点起不来;- 删掉
cluster.routing.allocation.snapshot.relocation_enabled; - 删掉四个
*.store.throttle.*设置,索引设置里的index.store.type: default也删掉; - 删掉六个阻塞式设置与三个未使用的 netty 缓冲设置;
- 脚本安全设置换成
script.allowed_types/script.allowed_contexts; discovery.type: gce|azure|ec2改成discovery.zen.hosts_provider;search.remote.*迁到cluster.remote.*(回退在 8.0 移除);basesimilarity 写进升级后的观察项;- 启动后用
_cluster/settings?include_defaults与_nodes/settings核对实际生效值。
在 5.6.16 和 6.8.23 上各跑一遍
迁移文档对这条的原话是「以前的版本允许配置里有重复键」,实测下来这句要分两种重复来看,两种版本的结论不一样。
图 4:上:把官方示例那份 node: 写两遍的配置准备好(node.name 一处、node.attr.rack 一处)。中:5.6.16 用这份配置起得来,GET / 返回的节点名是 my-node,说明两个 node: 块被合并了。下:6.8.23 用同一份配置直接拒绝,SettingsException ... nested: JsonParseException[Duplicate field 'node'。本地实测输出。
# 同一份配置,放在 5.6 的 config 目录下
node:
name: my-node
node:
attr:
rack: my-rack
# 5.6.16:起来之后
curl -s 'http://localhost:9406' | jq -r '.name'
# my-node
# 6.8.23:直接抛异常,节点起不来
# SettingsException[Failed to load settings from [elasticsearch.yml]];
# nested: JsonParseException[Duplicate field 'node'
同一个键写两遍(比如 cluster.name 出现两次)在 5.6.16 上也是报错的,错误文本是 ElasticsearchParseException[duplicate settings key [cluster.name] found at line number [2], column number [15], previous value [dup-a], current value [dup-b]]。所以准确的划分是:
- 完全相同的键重复:5.x 就拒,检查在 ES 自己的设置加载里做,报错带键名和行列号;
- 同一个 map 块写两次、子键不同:5.x 合并(图 4 中),6.0 起在 YAML 解析层就拒(图 4 下),报的是 Jackson 的
Duplicate field。
结论是 6.0 收紧的是第二种——也就是官方示例那种「同一个 node: 块在文件里出现两次,缩进看着都对」的写法。这类写法在 5.x 从来没出过错,所以升级时没人会主动去改,它是靠启动失败被动暴露的;好在暴露得很早,节点根本起不来。
我的建议是升级前先用 YAML 解析器扫一遍配置,别靠 ES 启动来验:python3 -c "import yaml,sys; yaml.safe_load(open(sys.argv[1]))" 这类基础解析不会报重复键,得用带重复键检测的解析方式(yaml.compose 后自己查重,或用 ruamel.yaml 的 allow_duplicate_keys=False)。等启动报错也行,但要留出「改配置 → 重启 → 再遇到下一个」的轮次。
边界:
- 图 4 上半出自 5.6.16(tar 包,配置直接写到它自己的
config/elasticsearch.yml,跑完还原),下半出自 6.8.23(ES_PATH_CONF指向临时目录); elasticsearch.yaml/elasticsearch.json存在即拒绝启动这条没有单独跑,它要往配置目录里放一个空文件,和本文这次「重复键」的实验会互相干扰;- 其余八条(
index.store.type、netty 设置、script.allowed_types等)分别需要旧索引、阻塞式传输实现或脚本沙箱配置,单节点验证不了。
写在最后
- 布尔值那条会让节点起不来,我的建议是把它放在扫配置的顺序第一位;
.yaml/.json留着是拒绝启动(有明确报错),真正的静默失效是 config 目录里没有elasticsearch.yml;- 重复键报错带行号和前后值,按报错改即可,升级前先扫一遍省事;
basesimilarity 不报错只忽略,不会挡住升级,但要留在观察项里;search.remote.*的回退到 8.0 就结束,6.x 期间要迁完。
11 条里,第 1、2、3、4 条与存储限速、阻塞式、脚本安全这几条都会在启动时因为「未知设置」或「值不合法」直接失败,属于升级前必须清完的部分;base similarity 与 search.remote.* 不阻断启动,属于升级后要跟进的部分。分这两类处理,顺序不会乱。
参考
本文对照 Elasticsearch
v6.8.23与v5.6.16标签的官方文档与源码核对,引文逐字取自原文。图 4 是本地 5.6.16 与 6.8.23 实例对「同一个 map 块写两次」的实测输出;其中 5.6 上「完全相同的键重复」也报错这一点是实测补充,其余各条未实测。
