1、查询分配未分配的原因

1.1 问题场景描述

学习 Elasticsearch 最常遇到的一种情况:分片未分配
在这里插入图片描述

索引未分配的原因有很多种,准确的说有一共有 12 种。对于很多人来说,没有多年ES使用经验很难马上判断出索引未分配的原因,只能在网上去搜,然后一种一种方法的去试。线上出问题时间是分秒必争的。这就要求必须在最短的时间内定位问题。

1.2 诊断方式

_cluster/allocation/explain

以下为查看my_index编号为 0 的副本分片的未分配原因

GET _cluster/allocation/explain
{
  "index": "my_index",
  "shard": 0,
  "primary": false
}

以下为查询结果核心代码

{
      "node_id" : "bgLGquyZSeOKbFBTJECjOQ",
      "node_name" : "node1",
      "transport_address" : "127.0.0.1:9302",
      "node_attributes" : {
        "rack" : "rack1",
        "xpack.installed" : "true",
        "transform.node" : "false"
      },
      "node_decision" : "no",
      "deciders" : [
        {
          "decider" : "filter",
          "decision" : "NO",
          "explanation" : """node does not match index setting [index.routing.allocation.require] filters [_name:"node3"]"""
        }
      ]
    }

原因:node does not match index setting [index.routing.allocation.require] filters [_name:“node3”]

由此可见,索引分片未分配原因为分片过滤器阻挡了分片分配,查询到病症,方可对症下药。关闭过滤器即可

1.3 解决方案

不同的问题原因需要不同的解决方案,针对以上问题原因,关闭分片过滤器即可。我将在后续给出常见的分片未分配原因的不同解决方案

PUT my_index/_settings
{
  "index.routing.allocation.require._name": null
}

1.4 所有 12 种分片未分配原因汇总

以下为所有分片未分配的原因的故障码以及描述,共12种

* ALLOCATION_FAILED: 		由于分片分配失败而未分配
* CLUSTER_RECOVERED: 		由于完整群集恢复而未分配.
* DANGLING_INDEX_IMPORTED: 	由于导入悬空索引而未分配.
* EXISTING_INDEX_RESTORED: 	由于还原到闭合索引而未分配.
* INDEX_CREATED: 			由于API创建索引而未分配.
* INDEX_REOPENED: 			由于打开闭合索引而未分配.
* NEW_INDEX_RESTORED: 		由于还原到新索引而未分配.
* NODE_LEFT: 				由于承载它的节点离开集群而取消分配.
* REALLOCATED_REPLICA: 		确定更好的副本位置并取消现有副本分配.
* REINITIALIZED: 			当碎片从“开始”移回“初始化”时.
* REPLICA_ADDED: 			由于显式添加了复制副本而未分配.
* REROUTE_CANCELLED: 		由于显式取消重新路由命令而取消分配.

2、查询集群的健康状况

2.1 健康状态

  • 绿色:所有分片都可用
  • 黄色:至少有一个副本不可用,但是所有主分片都可用
  • 红色:至少有一个主分片不可用,数据不完整

可以通过一些客户端工具查看集群状态信息
在这里插入图片描述

2.2 查看状态信息

GET _cat/health

在这里插入图片描述

GET _cluster/health

输出结构如下

{
  "cluster_name" : "my_cluster",   	集群名称 
  "status" : "yellow",				集群健康值
  "timed_out" : false,				是否超时
  "number_of_nodes" : 3,			索引主分片数量
  "number_of_data_nodes" : 3,		数据节点数量
  "active_primary_shards" : 37,		活跃主分片数量
  "active_shards" : 65,				活跃的分片数量
  "relocating_shards" : 0,			迁移中的分片的数量
  "initializing_shards" : 0,		初始化中的分片数量
  "unassigned_shards" : 3,			未分配的分片数量
  "delayed_unassigned_shards" : 0,	延迟未分配的分片数量
  "number_of_pending_tasks" : 0,	尚未执行的集群级别更改的数量
  "number_of_in_flight_fetch" : 0,							未完成的提取次数
  "task_max_waiting_in_queue_millis" : 0,					自最早启动的任务等待执行以来的时间
  "active_shards_percent_as_number" : 95.58823529411765		集群中活动分片的比率,以百分比表示
}

3、查看集群中所有节点的节点属性

_cat/nodeattrs

3.1 常见使用场景

查看节点属性最常见的即查看节点的自定义属性,自定义属性用于节点标记,常被用于以下几种场景:

  • 冷热集群部署
  • 高可用集群架构部署
  • 分片分配策略部署
    在这里插入图片描述

如上图所示,红框部分为通过自定义属性标记了三个节点处于不同的 rack 上,此类标记常用集群部署高可用策略等

4、查看集群中所有节点的分配信息

_cat/nodes?v

4.1 常见使用场景

此命令长用于获取每个节点的分配情况,包括获取其分配的 IP 地址、资源占用信息、角色等。
在这里插入图片描述

5、查询集群/索引的文档总计数

GET _cat/count

5.1 常见使用场景

此命令用于查看集群或者指定索引中的文档总计数,也可被_count API 替代,比如:

计算集群中的文档总数

GET _cat/count?v

// 返回结果:
epoch      timestamp count
1654141686 03:48:06  13342

计算 product 索引中的文档总数

GET _cat/count/product?v

// 返回结果
epoch      timestamp count
1654141931 03:52:11  13

此命令可被_count API 替代

GET product/_count

// 返回结果
{
  "count" : 13,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  }
}

6、查询集群的分片分配信息

_cat/shards

6.1 常见使用场景

常用于查看分片所处位置,包括所处哪个节点以及所处节点的 IP 地址。

常见的使用场景如:索引的生命周期管理、数据流等

6.2 使用案例

GET _cat/shards?v

返回结果如下

在这里插入图片描述

7、查询集群中索引的分片数、文档数或集群中包含哪些索引

_cat/indices

7.1 常见应用场景

此命令一般用于查看及群众包含哪些索引。可查询信息包括但不仅限于

  • 索引的健康状态
  • 索引的打开关闭状态
  • 索引名称
  • 索引文档数量
  • 被删除的文档数量
  • 主分片数量
  • 副本分片数量

7.2 使用

GET _cat/indices?v

返回结果如下
在这里插入图片描述

8、查询集群的快照存储信息

_cat/snapshots

8.1 常见使用场景

用于获取索引数据的 快照备份信息查询

8.2 使用

GET /_cat/snapshots?v

也可用于指定快照名称进行查询

GET /_cat/snapshots/<repository>

9、查看集群状态信息

_cluster/state

9.1 常见使用场景

用于查询集群状态元数据信息,包括:

  • 集群中的节点集
  • 所有集群级设置
  • 有关集群中索引的信息,包括它们的映射和设置
  • 集群中所有分片的位置。

10、查看集群统计信息

_cluster/stats

10.1 描述

此命令可以查询从集群范围的角度检索统计信息。API 返回基本索引指标(分片数量、存储大小、内存使用情况)和有关构成集群的当前节点的信息(数量、角色、操作系统、jvm 版本、内存使用情况、cpu 和已安装的插件)。

  • 索引到分片的映射
  • 文档技术统计
  • fielddata
  • 缓存信息
  • 段文件信息
  • 索引字段类型统计信息
  • 分词器统计信息
  • 集群节点角色统计信息

10.2 使用

 GET _cluster/stats

 // 返回结果如下
 {
  "_nodes" : {
    "total" : 3,
    "successful" : 3,
    "failed" : 0
  },
  "cluster_name" : "msb_cluster",
  "cluster_uuid" : "KhkbJ4V-TZemUBJ2vcWe9w",
  "timestamp" : 1654144248125,
  "status" : "yellow",
  "indices" : {
    "count" : 30,
    "shards" : {
      "total" : 65,
      "primaries" : 37,
      "replication" : 0.7567567567567568,
      "index" : {
        "shards" : {
          "min" : 2,
          "max" : 6,
          "avg" : 2.1666666666666665
        },
        "primaries" : {
          "min" : 1,
          "max" : 6,
          "avg" : 1.2333333333333334
        },
        "replication" : {
          "min" : 0.0,
          "max" : 1.0,
          "avg" : 0.9333333333333333
        }
      }
    },
    "docs" : {
      "count" : 13452,
      "deleted" : 2044
    },
    "store" : {
      "size_in_bytes" : 17546432,
      "total_data_set_size_in_bytes" : 17546432,
      "reserved_in_bytes" : 0
    },
    "fielddata" : {
      "memory_size_in_bytes" : 0,
      "evictions" : 0
    },
    "query_cache" : {
      "memory_size_in_bytes" : 0,
      "total_count" : 0,
      "hit_count" : 0,
      "miss_count" : 0,
      "cache_size" : 0,
      "cache_count" : 0,
      "evictions" : 0
    },
    "completion" : {
      "size_in_bytes" : 0
    },
    "segments" : {
      "count" : 126,
      "memory_in_bytes" : 370536,
      "terms_memory_in_bytes" : 242816,
      "stored_fields_memory_in_bytes" : 61872,
      "term_vectors_memory_in_bytes" : 0,
      "norms_memory_in_bytes" : 13824,
      "points_memory_in_bytes" : 0,
      "doc_values_memory_in_bytes" : 52024,
      "index_writer_memory_in_bytes" : 765980,
      "version_map_memory_in_bytes" : 0,
      "fixed_bit_set_memory_in_bytes" : 2816,
      "max_unsafe_auto_id_timestamp" : 1648427283621,
      "file_sizes" : { }
    },
    "mappings" : {
      "field_types" : [
        {
          "name" : "boolean",
          "count" : 5,
          "index_count" : 4,
          "script_count" : 0
        },
        {
          "name" : "date",
          "count" : 43,
          "index_count" : 12,
          "script_count" : 0
        },
        {
          "name" : "float",
          "count" : 14,
          "index_count" : 5,
          "script_count" : 0
        },
        {
          "name" : "geo_point",
          "count" : 3,
          "index_count" : 2,
          "script_count" : 0
        },
        {
          "name" : "geo_shape",
          "count" : 2,
          "index_count" : 2,
          "script_count" : 0
        },
        {
          "name" : "integer",
          "count" : 7,
          "index_count" : 2,
          "script_count" : 0
        },
        {
          "name" : "keyword",
          "count" : 199,
          "index_count" : 21,
          "script_count" : 0
        },
        {
          "name" : "long",
          "count" : 22,
          "index_count" : 12,
          "script_count" : 0
        },
        {
          "name" : "murmur3",
          "count" : 2,
          "index_count" : 2,
          "script_count" : 0
        },
        {
          "name" : "nested",
          "count" : 3,
          "index_count" : 3,
          "script_count" : 0
        },
        {
          "name" : "object",
          "count" : 31,
          "index_count" : 9,
          "script_count" : 0
        },
        {
          "name" : "text",
          "count" : 56,
          "index_count" : 20,
          "script_count" : 0
        }
      ],
      "runtime_field_types" : [ ]
    },
    "analysis" : {
      "char_filter_types" : [ ],
      "tokenizer_types" : [ ],
      "filter_types" : [ ],
      "analyzer_types" : [ ],
      "built_in_char_filters" : [ ],
      "built_in_tokenizers" : [ ],
      "built_in_filters" : [ ],
      "built_in_analyzers" : [
        {
          "name" : "ik_max_word",
          "count" : 2,
          "index_count" : 1
        }
      ]
    },
    "versions" : [
      {
        "version" : "7.13.0",
        "index_count" : 30,
        "primary_shard_count" : 37,
        "total_primary_bytes" : 8764317
      }
    ]
  },
  "nodes" : {
    "count" : {
      "total" : 3,
      "coordinating_only" : 0,
      "data" : 0,
      "data_cold" : 0,
      "data_content" : 3,
      "data_frozen" : 0,
      "data_hot" : 3,
      "data_warm" : 0,
      "ingest" : 3,
      "master" : 3,
      "ml" : 0,
      "remote_cluster_client" : 0,
      "transform" : 0,
      "voting_only" : 0
    },
    "versions" : [
      "7.13.0"
    ],
    "os" : {
      "available_processors" : 24,
      "allocated_processors" : 24,
      "names" : [
        {
          "name" : "Windows 10",
          "count" : 3
        }
      ],
      "pretty_names" : [
        {
          "pretty_name" : "Windows 10",
          "count" : 3
        }
      ],
      "architectures" : [
        {
          "arch" : "amd64",
          "count" : 3
        }
      ],
      "mem" : {
        "total_in_bytes" : 51175010304,
        "free_in_bytes" : 24862060544,
        "used_in_bytes" : 26312949760,
        "free_percent" : 49,
        "used_percent" : 51
      }
    },
    "process" : {
      "cpu" : {
        "percent" : 0
      },
      "open_file_descriptors" : {
        "min" : -1,
        "max" : -1,
        "avg" : 0
      }
    },
    "jvm" : {
      "max_uptime_in_millis" : 6379434,
      "versions" : [
        {
          "version" : "1.8.0_301",
          "vm_name" : "Java HotSpot(TM) 64-Bit Server VM",
          "vm_version" : "25.301-b09",
          "vm_vendor" : "Oracle Corporation",
          "bundled_jdk" : true,
          "using_bundled_jdk" : false,
          "count" : 3
        }
      ],
      "mem" : {
        "heap_used_in_bytes" : 873520008,
        "heap_max_in_bytes" : 3113877504
      },
      "threads" : 215
    },
    "fs" : {
      "total_in_bytes" : 148684926976,
      "free_in_bytes" : 31089479680,
      "available_in_bytes" : 31089479680
    },
    "plugins" : [
      {
        "name" : "analysis-ik",
        "version" : "7.13.0",
        "elasticsearch_version" : "7.13.0",
        "java_version" : "1.8",
        "description" : "IK Analyzer for Elasticsearch",
        "classname" : "org.elasticsearch.plugin.analysis.ik.AnalysisIkPlugin",
        "extended_plugins" : [ ],
        "has_native_controller" : false,
        "licensed" : false,
        "type" : "isolated"
      },
      {
        "name" : "mapper-murmur3",
        "version" : "7.13.0",
        "elasticsearch_version" : "7.13.0",
        "java_version" : "1.8",
        "description" : "The Mapper Murmur3 plugin allows to compute hashes of a field's values at index-time and to store them in the index.",
        "classname" : "org.elasticsearch.plugin.mapper.MapperMurmur3Plugin",
        "extended_plugins" : [ ],
        "has_native_controller" : false,
        "licensed" : false,
        "type" : "isolated"
      }
    ],
    "network_types" : {
      "transport_types" : {
        "security4" : 3
      },
      "http_types" : {
        "security4" : 3
      }
    },
    "discovery_types" : {
      "zen" : 3
    },
    "packaging_types" : [
      {
        "flavor" : "default",
        "type" : "zip",
        "count" : 3
      }
    ],
    "ingest" : {
      "number_of_pipelines" : 4,
      "processor_stats" : {
        "circle" : {
          "count" : 0,
          "failed" : 0,
          "current" : 0,
          "time_in_millis" : 0
        },
        "foreach" : {
          "count" : 0,
          "failed" : 0,
          "current" : 0,
          "time_in_millis" : 0
        },
        "gsub" : {
          "count" : 0,
          "failed" : 0,
          "current" : 0,
          "time_in_millis" : 0
        },
        "script" : {
          "count" : 0,
          "failed" : 0,
          "current" : 0,
          "time_in_millis" : 0
        }
      }
    }
  }
}
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐