(七)elasticsearch 源码之元数据CulsterState

1.概述

前面几篇讲述了es的许多重要的逻辑,这些逻辑都涉及到一个非常核心的类ClusterState,本文来看下ClusterState包括什么信息。

2.ClusterState

从注解中我们看到,CusterState表示整个集群的状态,其中的数据都是不可变的(除了RoutingTable),并且只能由master节点来更新。

CusterState 支持diff接口,用来生成增量信息。

主要字段如下:

// CulsterState

    private final long version; // 版本号

    private final String stateUUID; // 状态uuid

    private final RoutingTable routingTable; // 路由表

    private final DiscoveryNodes nodes; // 所有的发现节点

    private final MetaData metaData; // 元数据

    private final ClusterBlocks blocks; // 一些限制,用来阻止非法操作

    private final ImmutableOpenMap<String, Custom> customs; // 自定义

    private final ClusterName clusterName; // 集群名称

    private final boolean wasReadFromDiff;

    private final int minimumMasterNodesOnPublishingMaster;

    // built on demand
    private volatile RoutingNodes routingNodes;  // 路由信息的copy

RoutingTable 即全局的路由表,是索引路由表IndexRoutingTable的迭代器

public class RoutingTable implements Iterable<IndexRoutingTable>, Diffable<RoutingTable> {

    public static final RoutingTable EMPTY_ROUTING_TABLE = builder().build();

    private final long version;

    // index to IndexRoutingTable map
    private final ImmutableOpenMap<String, IndexRoutingTable> indicesRouting;

索引路由表 IndexRoutingTable 是分片路由表 IndexShardRoutingTable的迭代器

public class IndexRoutingTable extends AbstractDiffable<IndexRoutingTable> implements Iterable<IndexShardRoutingTable> {

    private final Index index; // 索引
    private final ShardShuffler shuffler; // 洗牌器

    // note, we assume that when the index routing is created, ShardRoutings are created for all possible number of
    // shards with state set to UNASSIGNED
    private final ImmutableOpenIntMap<IndexShardRoutingTable> shards; // 分片路由信息

    private final List<ShardRouting> allActiveShards; // 所有活跃分片的路由信息

IndexShardRoutingTable 包含了主分片和复制分片的信息

public class IndexShardRoutingTable implements Iterable<ShardRouting> {

    final ShardShuffler shuffler; // 洗牌器
    final ShardId shardId; // shard id is a int

    final ShardRouting primary; // 主分片
    final List<ShardRouting> primaryAsList;
    final List<ShardRouting> replicas; // 复制分片
    final List<ShardRouting> shards; // 分片
    final List<ShardRouting> activeShards; // 活跃分片
    final List<ShardRouting> assignedShards; // 已分配的分片
    final Set<String> allAllocationIds;
    final boolean allShardsStarted;

DiscoveryNodes 表示所有发现节点

public class DiscoveryNodes extends AbstractDiffable<DiscoveryNodes> implements Iterable<DiscoveryNode> {

    public static final DiscoveryNodes EMPTY_NODES = builder().build();

    private final ImmutableOpenMap<String, DiscoveryNode> nodes; // 节点
    private final ImmutableOpenMap<String, DiscoveryNode> dataNodes; // 数据节点
    private final ImmutableOpenMap<String, DiscoveryNode> masterNodes; // master节点
    private final ImmutableOpenMap<String, DiscoveryNode> ingestNodes;

    private final String masterNodeId; // master节点id
    private final String localNodeId; // 本地节点id
    private final Version minNonClientNodeVersion;
    private final Version maxNonClientNodeVersion;
    private final Version maxNodeVersion;
    private final Version minNodeVersion;
public class DiscoveryNode implements Writeable, ToXContentFragment {
    ...
    private final String nodeName; // 节点名称
    private final String nodeId; // 节点id
    private final String ephemeralId; // 临时id
    private final String hostName; // 主机信息
    private final String hostAddress;
    private final TransportAddress address; // transport 地址
    private final Map<String, String> attributes;
    private final Version version;
    private final Set<DiscoveryNodeRole> roles;

MetaData代表元数据,包括一些配置,index mapping

public class MetaData implements Iterable<IndexMetaData>, Diffable<MetaData>, ToXContentFragment {
    ...
    private final String clusterUUID; // cluster uuid
    private final boolean clusterUUIDCommitted;
    private final long version; // 版本号

    private final CoordinationMetaData coordinationMetaData; // 节点发现元数据

    private final Settings transientSettings; // 各类设置
    private final Settings persistentSettings;
    private final Settings settings;
    private final DiffableStringMap hashesOfConsistentSettings;
    private final ImmutableOpenMap<String, IndexMetaData> indices; // 索引元数据map
    private final ImmutableOpenMap<String, IndexTemplateMetaData> templates; // 索引模版map
    private final ImmutableOpenMap<String, Custom> customs; // 自定义map

    private final transient int totalNumberOfShards; // Transient ? not serializable anyway? // 分片总数
    private final int totalOpenIndexShards; // open的分片总数

    private final String[] allIndices; // 所有的索引
    private final String[] allOpenIndices; // 所有打开的索引
    private final String[] allClosedIndices; // 所有关闭的索引

    private final SortedMap<String, AliasOrIndex> aliasAndIndexLookup; // name -> alias/index map

ClusterBlock表示在特定状态下对集群的一些禁止操作,比如在集群恢复阶段不可读写

public class ClusterBlocks extends AbstractDiffable<ClusterBlocks> {
    public static final ClusterBlocks EMPTY_CLUSTER_BLOCK = new ClusterBlocks(emptySet(), ImmutableOpenMap.of());

    private final Set<ClusterBlock> global; // 全局的block

    private final ImmutableOpenMap<String, Set<ClusterBlock>> indicesBlocks; // 索引级别的block

    private final EnumMap<ClusterBlockLevel, ImmutableLevelHolder> levelHolders; // 根据level对上面的block进行分类

3.builder模式

ClusterState使用了builder模式,builder模式对于修改复杂的数据对象非常方便。先定义一个内部类,属性和数据对象相同

// ClusterState

public static class Builder {

        private final ClusterName clusterName;
        private long version = 0;
        private String uuid = UNKNOWN_UUID;
        private MetaData metaData = MetaData.EMPTY_META_DATA;
        private RoutingTable routingTable = RoutingTable.EMPTY_ROUTING_TABLE;
        private DiscoveryNodes nodes = DiscoveryNodes.EMPTY_NODES;
        private ClusterBlocks blocks = ClusterBlocks.EMPTY_CLUSTER_BLOCK;
        private final ImmutableOpenMap.Builder<String, Custom> customs;
        private boolean fromDiff;
        private int minimumMasterNodesOnPublishingMaster = -1;

        public Builder(ClusterState state) {
            this.clusterName = state.clusterName;
            this.version = state.version();
            this.uuid = state.stateUUID();
            this.nodes = state.nodes();
            this.routingTable = state.routingTable();
            this.metaData = state.metaData();
            this.blocks = state.blocks();
            this.customs = ImmutableOpenMap.builder(state.customs());
            this.minimumMasterNodesOnPublishingMaster = state.minimumMasterNodesOnPublishingMaster;
            this.fromDiff = false;
        }
    
        ...
        public Builder metaData(MetaData metaData) {
            this.metaData = metaData;
            return this; // 返回当前builder对象
        }
        ...
        public ClusterState build() {
            if (UNKNOWN_UUID.equals(uuid)) {
                uuid = UUIDs.randomBase64UUID();
            }
            return new ClusterState(clusterName, version, uuid, metaData, routingTable, nodes, blocks, customs.build(),
                minimumMasterNodesOnPublishingMaster, fromDiff); // 返回主类对象
        }

初始化和修改对象时进行链式修改,然后build,避免了传统的new, set, set...的写法

ClusterState initialClusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY))
            .metaData(metaData).routingTable(routingTable).nodes
                (nb).build(); // 链式写法

 4.总结

本文简单描述了ClusterState的数据结果,展示了builder模式的运用。

posted @ 2023-02-08 20:50  darcy_yuan  阅读(174)  评论(0编辑  收藏  举报