Elasticsearch索引的数据存储路径是如何确定的

Elasticsearch中,在node的配置中可以指定path.data用来作为节点数据的存储目录,而且我们可以指定多个值来作为数据存储的路径,那么Elasticsearch是如何判断应该存储到哪个路径下呢?今天我就记录一下这个问题。

Elasticsearch的索引创建过程

  1. 集群master收到创建索引的请求后,经过创建索引的一些步骤,最终会将索引创建完成的请求提交到ClusterState
  2. master将根据ClusterState分发给所有节点
  3. 涉及创建shard的节点会读取本地可用的path.data,然后依据一定的规则获取路径。
  4. 创建基本shard路径,保存基本的shard信息。

如何确定在哪个目录下

源码

主要调用的是ShardPath的selectNewPathForShard方法

   for (NodeEnvironment.NodePath nodePath : env.nodePaths()) {
                totFreeSpace = totFreeSpace.add(BigInteger.valueOf(nodePath.fileStore.getUsableSpace()));
            }

            // TODO: this is a hack!!  We should instead keep track of incoming (relocated) shards since we know
            // how large they will be once they're done copying, instead of a silly guess for such cases:

            // Very rough heuristic of how much disk space we expect the shard will use over its lifetime, the max of current average
            // shard size across the cluster and 5% of the total available free space on this node:
            BigInteger estShardSizeInBytes = BigInteger.valueOf(avgShardSizeInBytes).max(totFreeSpace.divide(BigInteger.valueOf(20)));

            // TODO - do we need something more extensible? Yet, this does the job for now...
            final NodeEnvironment.NodePath[] paths = env.nodePaths();

            // If no better path is chosen, use the one with the most space by default
            NodeEnvironment.NodePath bestPath = getPathWithMostFreeSpace(env);

            if (paths.length != 1) {
                Map<NodeEnvironment.NodePath, Long> pathToShardCount = env.shardCountPerPath(shardId.getIndex());

                // Compute how much space there is on each path
                final Map<NodeEnvironment.NodePath, BigInteger> pathsToSpace = new HashMap<>(paths.length);
                for (NodeEnvironment.NodePath nodePath : paths) {
                    FileStore fileStore = nodePath.fileStore;
                    BigInteger usableBytes = BigInteger.valueOf(fileStore.getUsableSpace());
                    pathsToSpace.put(nodePath, usableBytes);
                }

                bestPath = Arrays.stream(paths)
                        // Filter out paths that have enough space
                        .filter((path) -> pathsToSpace.get(path).subtract(estShardSizeInBytes).compareTo(BigInteger.ZERO) > 0)
                        // Sort by the number of shards for this index
                        .sorted((p1, p2) -> {
                                int cmp = Long.compare(pathToShardCount.getOrDefault(p1, 0L),
                                    pathToShardCount.getOrDefault(p2, 0L));
                                if (cmp == 0) {
                                    // if the number of shards is equal, tie-break with the number of total shards
                                    cmp = Integer.compare(dataPathToShardCount.getOrDefault(p1.path, 0),
                                            dataPathToShardCount.getOrDefault(p2.path, 0));
                                    if (cmp == 0) {
                                        // if the number of shards is equal, tie-break with the usable bytes
                                        cmp = pathsToSpace.get(p2).compareTo(pathsToSpace.get(p1));
                                    }
                                }
                                return cmp;
                            })
                        // Return the first result
                        .findFirst()
                        // Or the existing best path if there aren't any that fit the criteria
                        .orElse(bestPath);
            }

            statePath = bestPath.resolve(shardId);
            dataPath = statePath;
        }

过程分析

  1. 首先判断是否自定义了path.data,没有自定义就在默认路径下创建
  2. 自定义的情况下确保节点下最少有5%的空间可以使用
  3. 获取所有的paths,
  4. 然后设置默认最佳的path是当前拥有最多空间的path
  5. 遍历所有的paths,首先过滤掉没有空间的path,如果最终没有符合的,就返回4步骤的path,否则继续6步骤
  6. 按照规则对paths排序,首先判断每个path下该索引的shard数,优先返回含有本索引的shard数最少的path;
    当条件结果相同,对比每个path中包含有的shard总数(所有索引的),返回包含shard数最少的path;
    当2条件结果相同,对比可用空间,返回可用空间最大的path
  7. 生成相应的路径,创建目录等信息。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Linux上优化Elasticsearch索引文件,可以按照以下步骤进行操作: 1. 进入Elasticsearch的配置文件目录: ``` cd /opt/app/elasticsearch/config/ ``` 2. 打开Elasticsearch的配置文件elasticsearch.yml: ``` vi elasticsearch.yml ``` 3. 根据需要进行以下优化配置: - 修改索引存储路径: 在elasticsearch.yml文件中找到`path.data`配置项,将其指定为一个性能较好的存储路径,例如: ``` path.data: /opt/app/elasticsearch/data ``` - 调整内存分配: 在elasticsearch.yml文件中找到`-Xms`和`-Xmx`配置项,分别表示Elasticsearch的最小和最大堆内存大小。根据服务器的内存情况,可以适当调整这两个值,以提高性能。例如: ``` -Xms2g -Xmx4g ``` - 调整线程池大小: 在elasticsearch.yml文件中找到`thread_pool`配置项,可以根据需要调整各个线程池的大小,以适应并发查询和索引的需求。 4. 保存并关闭elasticsearch.yml文件。 5. 重启Elasticsearch服务,使配置生效。 请注意,以上是一些常见的优化配置,具体的优化策略还需要根据实际情况进行调整。另外,优化Elasticsearch索引文件还可以考虑其他方面,如分片设置、缓存配置等,具体的优化策略可以参考Elasticsearch官方文档或相关资料。 #### 引用[.reference_title] - *1* *2* *3* [Linux部署elasticsearch和迁移数据详细教程](https://blog.csdn.net/qq_39221436/article/details/124397172)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值