最新要闻

广告

手机

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

家电

(七)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 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, Diffable {    public static final RoutingTable EMPTY_ROUTING_TABLE = builder().build();    private final long version;    // index to IndexRoutingTable map    private final ImmutableOpenMap indicesRouting;

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

public class IndexRoutingTable extends AbstractDiffable implements Iterable {    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 shards; // 分片路由信息    private final List allActiveShards; // 所有活跃分片的路由信息

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

public class IndexShardRoutingTable implements Iterable {    final ShardShuffler shuffler; // 洗牌器    final ShardId shardId; // shard id is a int    final ShardRouting primary; // 主分片    final List primaryAsList;    final List replicas; // 复制分片    final List shards; // 分片    final List activeShards; // 活跃分片    final List assignedShards; // 已分配的分片    final Set allAllocationIds;    final boolean allShardsStarted;

DiscoveryNodes 表示所有发现节点

public class DiscoveryNodes extends AbstractDiffable implements Iterable {    public static final DiscoveryNodes EMPTY_NODES = builder().build();    private final ImmutableOpenMap nodes; // 节点    private final ImmutableOpenMap dataNodes; // 数据节点    private final ImmutableOpenMap masterNodes; // master节点    private final ImmutableOpenMap 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 attributes;    private final Version version;    private final Set roles;

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

public class MetaData implements Iterable, Diffable, 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 indices; // 索引元数据map    private final ImmutableOpenMap templates; // 索引模版map    private final ImmutableOpenMap 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 aliasAndIndexLookup; // name -> alias/index map

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

public class ClusterBlocks extends AbstractDiffable {    public static final ClusterBlocks EMPTY_CLUSTER_BLOCK = new ClusterBlocks(emptySet(), ImmutableOpenMap.of());    private final Set global; // 全局的block    private final ImmutableOpenMap> indicesBlocks; // 索引级别的block    private final EnumMap levelHolders; // 根据level对上面的block进行分类

3.builder模式

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

// ClusterStatepublic 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 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模式的运用。

关键词: 数据对象 禁止操作 非法操作