org.elasticsearch.cluster.ClusterState类的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(18.6k)|赞(0)|评价(0)|浏览(113)

本文整理了Java中org.elasticsearch.cluster.ClusterState类的一些代码示例,展示了ClusterState类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ClusterState类的具体详情如下:
包路径:org.elasticsearch.cluster.ClusterState
类名称:ClusterState

ClusterState介绍

[英]Represents the current state of the cluster. The cluster state object is immutable with an exception of the RoutingNodes structure, which is built on demand from the RoutingTable, and cluster state #status, which is updated during cluster state publishing and applying processing. The cluster state can be updated only on the master node. All updates are performed by on a single thread and controlled by the InternalClusterService. After every update the DiscoveryService#publish method publishes new version of the cluster state to all other nodes in the cluster. The actual publishing mechanism is delegated to the Discovery#publish method and depends on the type of discovery. For example, for local discovery it is implemented by the LocalDiscovery#publishmethod. In the Zen Discovery it is handled in the PublishClusterStateAction#publish method. The publishing mechanism can be overridden by other discovery. The cluster state implements the Diffable interface in order to support publishing of cluster state differences instead of the entire state on each change. The publishing mechanism should only send differences to a node if this node was present in the previous version of the cluster state. If a node is not present was not present in the previous version of the cluster state, such node is unlikely to have the previous cluster state version and should be sent a complete version. In order to make sure that the differences are applied to correct version of the cluster state, each cluster state version update generates #stateUUID that uniquely identifies this version of the state. This uuid is verified by the ClusterStateDiff#apply method to makes sure that the correct diffs are applied. If uuids don’t match, the ClusterStateDiff#apply method throws the IncompatibleClusterStateVersionException, which should cause the publishing mechanism to send a full version of the cluster state to the node on which this exception was thrown.
[中]表示群集的当前状态。cluster state对象是不可变的,但RoutingNodes结构(根据RoutingTable的需要构建)和cluster state#status(在集群状态发布和应用处理期间更新)除外。只能在主节点上更新群集状态。所有更新都由在单个线程上执行,并由InternalClusterService控制。在每次更新之后,DiscoveryService#publish方法将集群状态的新版本发布到集群中的所有其他节点。实际的发布机制委托给发现#发布方法,并取决于发现的类型。例如,对于本地发现,它是通过LocalDiscovery#publishmethod实现的。在Zen Discovery中,它是通过PublishClusterStateAction#publish方法处理的。发布机制可以被其他发现覆盖。集群状态实现了Diffable接口,以便支持发布集群状态差异,而不是每次更改时的整个状态。如果某个节点在群集状态的早期版本中存在,则发布机制应仅向该节点发送差异。如果某个节点不存在且在群集状态的早期版本中不存在,则该节点不太可能具有早期群集状态版本,应发送完整版本。为了确保将差异应用于集群状态的正确版本,每个集群状态版本更新都会生成#stateuid,唯一标识该版本的状态。此uuid由ClusterStateDiff#apply方法验证,以确保应用了正确的差异。如果UUID不匹配,ClusterStateDiff#apply方法将抛出不兼容的ClusterStateVersionException,这将导致发布机制将集群状态的完整版本发送到引发此异常的节点。

代码示例

代码示例来源:origin: richardwilly98/elasticsearch-river-mongodb

logger.trace("dropRecreateMapping index[{}] - type[{}]", index, type);
client.admin().indices().prepareRefresh(index).get();
ImmutableOpenMap<String, MappingMetaData> mappings = client.admin().cluster().prepareState().get().getState().getMetaData()
    .index(index).mappings();
logger.trace("mappings contains type {}: {}", type, mappings.containsKey(type));
if (mappings.containsKey(type)) {
  MappingMetaData mapping = mappings.get(type);
  if (client.admin().indices().prepareDeleteMapping(index).setType(type).get().isAcknowledged()) {
    PutMappingResponse pmr = client.admin().indices().preparePutMapping(index).setType(type)

代码示例来源:origin: floragunncom/search-guard

final IndexMetaData indexMetaData = clusterService.state().metaData().getIndices().get(requestAliasOrIndex);
  log.debug("{} does not exist in cluster metadata", requestAliasOrIndex);
  continue;
final ImmutableOpenMap<String, AliasMetaData> aliases = indexMetaData.getAliases();
if(aliases != null && aliases.size() > 0) {
  if(log.isDebugEnabled()) {
    log.debug("Aliases for {}: {}", requestAliasOrIndex, aliases);
  final Iterator<String> it = aliases.keysIt();
  while(it.hasNext()) {
    final String alias = it.next();

代码示例来源:origin: floragunncom/search-guard

@Override
public void clusterChanged(ClusterChangedEvent event) {
  if(has5xNodes == null || event.nodesChanged()) {
    has5xNodes = Boolean.valueOf(clusterHas5xNodes(event.state()));
    if(log.isTraceEnabled()) {
      log.trace("has5xNodes: {}", has5xNodes);
    }
  }
  
  final List<String> indicesCreated = event.indicesCreated();
  final List<Index> indicesDeleted = event.indicesDeleted();
  if(has5xIndices == null || !indicesCreated.isEmpty() || !indicesDeleted.isEmpty()) {
    has5xIndices = Boolean.valueOf(clusterHas5xIndices(event.state()));
    if(log.isTraceEnabled()) {
      log.trace("has5xIndices: {}", has5xIndices);
    }
  }
  
  if(nodes == null || event.nodesChanged()) {
    nodes = event.state().nodes();
    if(log.isDebugEnabled()) {
      log.debug("Cluster Info Holder now initialized for 'nodes'");
    }
  }
  
  isLocalNodeElectedMaster = event.localNodeMaster()?Boolean.TRUE:Boolean.FALSE;
}

代码示例来源:origin: org.elasticsearch/elasticsearch

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.fromDiff = false;
}

代码示例来源:origin: floragunncom/search-guard

private Resolved resolveIndexPatterns(final String... requestedPatterns) {
  if(log.isTraceEnabled()) {
    log.trace("resolve requestedPatterns: "+Arrays.toString(requestedPatterns));
  final SortedMap<String, AliasOrIndex> lookup = state.metaData().getAliasAndIndexLookup();
  final Set<String> aliases = lookup.entrySet().stream().filter(e->e.getValue().isAlias()).map(e->e.getKey()).collect(Collectors.toSet());
    try {
      _indices = new ArrayList<>(Arrays.asList(resolver.concreteIndexNames(state, IndicesOptions.fromOptions(false, true, true, false), requestedPatterns)));
      if (log.isDebugEnabled()) {
        log.debug("Resolved pattern {} to {}", requestedPatterns, _indices);
          Set<String> doubleIndices = lookup.get(al).getIndices().stream().map(a->a.getIndex().getName()).collect(Collectors.toSet());
          _indices.removeAll(doubleIndices);

代码示例来源:origin: org.elasticsearch/elasticsearch

@Override
public void onMaster() {
  this.isMaster = true;
  if (logger.isTraceEnabled()) {
    logger.trace("I have been elected master, scheduling a ClusterInfoUpdateJob");
  }
  try {
    // Submit a job that will start after DEFAULT_STARTING_INTERVAL, and reschedule itself after running
    threadPool.schedule(updateFrequency, executorName(), new SubmitReschedulingClusterInfoUpdatedJob());
    if (clusterService.state().getNodes().getDataNodes().size() > 1) {
      // Submit an info update job to be run immediately
      threadPool.executor(executorName()).execute(() -> maybeRefresh());
    }
  } catch (EsRejectedExecutionException ex) {
    if (logger.isDebugEnabled()) {
      logger.debug("Couldn't schedule cluster info update task - node might be shutting down", ex);
    }
  }
}

代码示例来源:origin: org.elasticsearch/elasticsearch

DiscoveryNodes nodes = response.getState().nodes();
    Iterable<DiscoveryNode> nodesIter = nodes.getNodes()::valuesIt;
    for (DiscoveryNode n : nodesIter) {
      DiscoveryNode node = maybeAddProxyAddress(proxyAddress, n);
          logger.debug(() -> new ParameterizedMessage("failed to connect to node {}", node), ex);
logger.warn(() -> new ParameterizedMessage("fetching nodes from external cluster {} failed", clusterAlias), ex);
collectRemoteNodes(seedNodes, transportService, connectionManager, listener);

代码示例来源:origin: org.elasticsearch/elasticsearch

private void createIndices(final ClusterState state) {
  RoutingNode localRoutingNode = state.getRoutingNodes().node(state.nodes().getLocalNodeId());
  if (localRoutingNode == null) {
    return;
    if (failedShardsCache.containsKey(shardRouting.shardId()) == false) {
      final Index index = shardRouting.index();
      if (indicesService.indexService(index) == null) {
        indicesToCreate.computeIfAbsent(index, k -> new ArrayList<>()).add(shardRouting);
    final IndexMetaData indexMetaData = state.metaData().index(index);
    logger.debug("[{}] creating index", index);
      indexService = indicesService.createIndex(indexMetaData, buildInIndexListener);
      if (indexService.updateMapping(null, indexMetaData) && sendRefreshMapping) {
        nodeMappingRefreshAction.nodeMappingRefresh(state.nodes().getMasterNode(),
          new NodeMappingRefreshAction.NodeMappingRefreshRequest(indexMetaData.getIndex().getName(),
            indexMetaData.getIndexUUID(), state.nodes().getLocalNodeId())
        );

代码示例来源:origin: floragunncom/search-guard

final SortedMap<String, AliasOrIndex> lookup = clusterService.state().metaData().getAliasAndIndexLookup();
  for(final String indexOrAlias: lookup.keySet()) {
    final String tenant = tenantNameForIndex(indexOrAlias);
log.error(e1.toString(),e1);

代码示例来源:origin: org.elasticsearch/elasticsearch

@Override
  public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
    logger.info("recovered [{}] indices into cluster_state", newState.metaData().indices().size());
  }
});

代码示例来源:origin: floragunncom/search-guard

threadContext.putHeader(ConfigConstants.SG_CONF_REQUEST_HEADER, "true");
boolean searchGuardIndexExists = clusterService.state().metaData().hasConcreteIndex(this.searchguardIndex);
  if(clusterService.state().metaData().index(this.searchguardIndex).mapping("config") != null) {
    LOGGER.debug("sg index exists and was created before ES 6 (legacy layout)");
    retVal.putAll(validate(legacycl.loadLegacy(configTypes.toArray(new String[0]), 5, TimeUnit.SECONDS), configTypes.size()));
  } else {
    LOGGER.debug("sg index exists and was created with ES 6 (new layout)");
    retVal.putAll(validate(cl.load(configTypes.toArray(new String[0]), 5, TimeUnit.SECONDS), configTypes.size()));
  LOGGER.debug("sg index not exists (yet)");
  retVal.putAll(validate(cl.load(configTypes.toArray(new String[0]), 30, TimeUnit.SECONDS), configTypes.size()));

代码示例来源:origin: org.elasticsearch/elasticsearch

@Override
public ClusterState execute(ClusterState currentState) throws Exception {
  if (request.create && currentState.metaData().templates().containsKey(request.name)) {
    throw new IllegalArgumentException("index_template [" + request.name + "] already exists");
  }
  validateAndAddTemplate(request, templateBuilder, indicesService, xContentRegistry);
  for (Alias alias : request.aliases) {
    AliasMetaData aliasMetaData = AliasMetaData.builder(alias.name()).filter(alias.filter())
      .indexRouting(alias.indexRouting()).searchRouting(alias.searchRouting()).build();
    templateBuilder.putAlias(aliasMetaData);
  }
  IndexTemplateMetaData template = templateBuilder.build();
  MetaData.Builder builder = MetaData.builder(currentState.metaData()).put(template);
  logger.info("adding template [{}] for index patterns {}", request.name, request.indexPatterns);
  return ClusterState.builder(currentState).metaData(builder).build();
}

代码示例来源:origin: org.elasticsearch/elasticsearch

private void handleMasterGone(final DiscoveryNode masterNode, final Throwable cause, final String reason) {
  if (lifecycleState() != Lifecycle.State.STARTED) {
    // not started, ignore a master failure
    return;
  }
  if (localNodeMaster()) {
    // we might get this on both a master telling us shutting down, and then the disconnect failure
    return;
  }
  logger.info(() -> new ParameterizedMessage("master_left [{}], reason [{}]", masterNode, reason), cause);
  synchronized (stateMutex) {
    if (localNodeMaster() == false && masterNode.equals(committedState.get().nodes().getMasterNode())) {
      // flush any pending cluster states from old master, so it will not be set as master again
      pendingStatesQueue.failAllStatesAndClear(new ElasticsearchException("master left [{}]", reason));
      rejoin("master left (reason = " + reason + ")");
    }
  }
}

代码示例来源:origin: org.elasticsearch/elasticsearch

private ClusterState.Builder becomeMasterAndTrimConflictingNodes(ClusterState currentState, List<DiscoveryNode> joiningNodes) {
  assert currentState.nodes().getMasterNodeId() == null : currentState;
  DiscoveryNodes currentNodes = currentState.nodes();
  DiscoveryNodes.Builder nodesBuilder = DiscoveryNodes.builder(currentNodes);
  nodesBuilder.masterNodeId(currentState.nodes().getLocalNodeId());
  for (final DiscoveryNode joiningNode : joiningNodes) {
    final DiscoveryNode nodeWithSameId = nodesBuilder.get(joiningNode.getId());
    if (nodeWithSameId != null && nodeWithSameId.equals(joiningNode) == false) {
      logger.debug("removing existing node [{}], which conflicts with incoming join from [{}]", nodeWithSameId, joiningNode);
      nodesBuilder.remove(nodeWithSameId.getId());
    }
    final DiscoveryNode nodeWithSameAddress = currentNodes.findByAddress(joiningNode.getAddress());
    if (nodeWithSameAddress != null && nodeWithSameAddress.equals(joiningNode) == false) {
      logger.debug("removing existing node [{}], which conflicts with incoming join from [{}]", nodeWithSameAddress,
        joiningNode);
      nodesBuilder.remove(nodeWithSameAddress.getId());
    }
  }
  // now trim any left over dead nodes - either left there when the previous master stepped down
  // or removed by us above
  ClusterState tmpState = ClusterState.builder(currentState).nodes(nodesBuilder).blocks(ClusterBlocks.builder()
    .blocks(currentState.blocks())
    .removeGlobalBlock(DiscoverySettings.NO_MASTER_BLOCK_ID)).build();
  return ClusterState.builder(allocationService.deassociateDeadNodes(tmpState, false,
    "removed dead nodes on election"));
}

代码示例来源:origin: org.elasticsearch/elasticsearch

final String localNodeId = state.nodes().getLocalNodeId();
assert localNodeId != null;
RoutingNode localRoutingNode = state.getRoutingNodes().node(localNodeId);
if (localRoutingNode != null) { // null e.g. if we are not a data node
  for (ShardRouting shardRouting : localRoutingNode) {
    indicesWithShards.add(shardRouting.index());
    final IndexMetaData indexMetaData = state.metaData().index(index);
    assert indexMetaData != null || event.isNewCluster() :
      "index " + index + " does not exist in the cluster state, it should either " +
        "have been deleted or the cluster must be new";
    final AllocatedIndices.IndexRemovalReason reason =
      indexMetaData != null && indexMetaData.getState() == IndexMetaData.State.CLOSE ? CLOSED : NO_LONGER_ASSIGNED;
    logger.debug("{} removing index, [{}]", index, reason);
    indicesService.removeIndex(index, reason, "removing index (no shards allocated)");

代码示例来源:origin: org.elasticsearch/elasticsearch

nodesFD.stop();
DiscoveryNodes nodes = clusterState().nodes();
if (sendLeaveRequest) {
  if (nodes.getMasterNode() == null) {
  } else if (!nodes.isLocalNodeElectedMaster()) {
    try {
      membership.sendLeaveRequestBlocking(nodes.getMasterNode(), nodes.getLocalNode(), TimeValue.timeValueSeconds(1));
    } catch (Exception e) {
      logger.debug(() -> new ParameterizedMessage("failed to send leave request to master [{}]", nodes.getMasterNode()), e);
    DiscoveryNode[] possibleMasters = electMaster.nextPossibleMasters(nodes.getNodes().values(), 5);
    for (DiscoveryNode possibleMaster : possibleMasters) {
      if (nodes.getLocalNode().equals(possibleMaster)) {
        continue;
        membership.sendLeaveRequest(nodes.getLocalNode(), possibleMaster);
      } catch (Exception e) {
        logger.debug(() -> new ParameterizedMessage("failed to send leave request from master [{}] to possible master [{}]",
          nodes.getMasterNode(), possibleMaster), e);

代码示例来源:origin: org.elasticsearch/elasticsearch

/**
 * Deletes an index that is not assigned to this node. This method cleans up all disk folders relating to the index
 * but does not deal with in-memory structures. For those call {@link #removeIndex(Index, IndexRemovalReason, String)}
 */
@Override
public void deleteUnassignedIndex(String reason, IndexMetaData metaData, ClusterState clusterState) {
  if (nodeEnv.hasNodeFile()) {
    String indexName = metaData.getIndex().getName();
    try {
      if (clusterState.metaData().hasIndex(indexName)) {
        final IndexMetaData index = clusterState.metaData().index(indexName);
        throw new IllegalStateException("Can't delete unassigned index store for [" + indexName + "] - it's still part of " +
                        "the cluster state [" + index.getIndexUUID() + "] [" + metaData.getIndexUUID() + "]");
      }
      deleteIndexStore(reason, metaData, clusterState);
    } catch (Exception e) {
      logger.warn(() -> new ParameterizedMessage("[{}] failed to delete unassigned index (reason [{}])",
        metaData.getIndex(), reason), e);
    }
  }
}

代码示例来源:origin: org.elasticsearch/elasticsearch

public void verify(String repository, boolean readOnly, String verificationToken, final ActionListener<VerifyResponse> listener) {
  final DiscoveryNodes discoNodes = clusterService.state().nodes();
  final DiscoveryNode localNode = discoNodes.getLocalNode();
  final ObjectContainer<DiscoveryNode> masterAndDataNodes = discoNodes.getMasterAndDataNodes().values();
  final List<DiscoveryNode> nodes = new ArrayList<>();
  for (ObjectCursor<DiscoveryNode> cursor : masterAndDataNodes) {
    DiscoveryNode node = cursor.value;
    if (readOnly && node.getVersion().before(Version.V_6_6_0)) {
      continue;
  final AtomicInteger counter = new AtomicInteger(nodes.size());
  for (final DiscoveryNode node : nodes) {
    if (node.equals(localNode)) {
      try {
        doVerify(repository, verificationToken, localNode);
      } catch (Exception e) {
        logger.warn(() -> new ParameterizedMessage("[{}] failed to verify repository", repository), e);
        errors.add(new VerificationFailure(node.getId(), e));

代码示例来源:origin: org.elasticsearch/elasticsearch

public IndexMetaData verifyIndexIsDeleted(final Index index, final ClusterState clusterState) {
  if (clusterState.metaData().index(index) != null) {
    throw new IllegalStateException("Cannot delete index [" + index + "], it is still part of the cluster state.");
      metaData = metaStateService.loadIndexState(index);
    } catch (Exception e) {
      logger.warn(() -> new ParameterizedMessage("[{}] failed to load state file from a stale deleted index, " +
        "folders will be left on disk", index), e);
      return null;
      logger.warn(() -> new ParameterizedMessage("[{}] failed to delete index on disk", metaData.getIndex()), e);

代码示例来源:origin: org.elasticsearch/elasticsearch

private void handleAnotherMaster(ClusterState localClusterState, final DiscoveryNode otherMaster, long otherClusterStateVersion,
                 String reason) {
  assert localClusterState.nodes().isLocalNodeElectedMaster() : "handleAnotherMaster called but current node is not a master";
  assert Thread.holdsLock(stateMutex);
  if (otherClusterStateVersion > localClusterState.version()) {
    rejoin("zen-disco-discovered another master with a new cluster_state [" + otherMaster + "][" + reason + "]");
  } else {
    // TODO: do this outside mutex
    logger.warn("discovered [{}] which is also master but with an older cluster_state, telling [{}] to rejoin the cluster ([{}])",
      otherMaster, otherMaster, reason);
    try {
      // make sure we're connected to this node (connect to node does nothing if we're already connected)
      // since the network connections are asymmetric, it may be that we received a state but have disconnected from the node
      // in the past (after a master failure, for example)
      transportService.connectToNode(otherMaster);
      transportService.sendRequest(otherMaster, DISCOVERY_REJOIN_ACTION_NAME,
        new RejoinClusterRequest(localClusterState.nodes().getLocalNodeId()),
        new EmptyTransportResponseHandler(ThreadPool.Names.SAME) {
        @Override
        public void handleException(TransportException exp) {
          logger.warn(() -> new ParameterizedMessage("failed to send rejoin request to [{}]", otherMaster), exp);
        }
      });
    } catch (Exception e) {
      logger.warn(() -> new ParameterizedMessage("failed to send rejoin request to [{}]", otherMaster), e);
    }
  }
}

相关文章