public static int failedShards(ImmutableOpenMap<ShardId, RestoreInProgress.ShardRestoreStatus> shards) { int failedShards = 0; for (ObjectCursor<RestoreInProgress.ShardRestoreStatus> status : shards.values()) { if (status.value.state() == RestoreInProgress.State.FAILURE) { failedShards++; } } return failedShards; }
public Iterator<Mapper> iterator() { return StreamSupport.stream(mappers.values().spliterator(), false).map((p) -> (Mapper)p.value).iterator(); }
public static boolean completed(ImmutableOpenMap<ShardId, RestoreInProgress.ShardRestoreStatus> shards) { for (ObjectCursor<RestoreInProgress.ShardRestoreStatus> status : shards.values()) { if (!status.value.state().completed()) { return false; } } return true; }
/** * Finds index templates whose index pattern matched with the given index name. * The result is sorted by {@link IndexTemplateMetaData#order} descending. */ public static List<IndexTemplateMetaData> findTemplates(MetaData metaData, String indexName) { final List<IndexTemplateMetaData> matchedTemplates = new ArrayList<>(); for (ObjectCursor<IndexTemplateMetaData> cursor : metaData.templates().values()) { final IndexTemplateMetaData template = cursor.value; final boolean matched = template.patterns().stream().anyMatch(pattern -> Regex.simpleMatch(pattern, indexName)); if (matched) { matchedTemplates.add(template); } } CollectionUtil.timSort(matchedTemplates, Comparator.comparingInt(IndexTemplateMetaData::order).reversed()); return matchedTemplates; }
/** * Get a node by its address * * @param address {@link TransportAddress} of the wanted node * @return node identified by the given address or <code>null</code> if no such node exists */ public DiscoveryNode findByAddress(TransportAddress address) { for (ObjectCursor<DiscoveryNode> cursor : nodes.values()) { DiscoveryNode node = cursor.value; if (node.getAddress().equals(address)) { return node; } } return null; }
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { if (!mappers.isEmpty()) { // sort the mappers so we get consistent serialization format Mapper[] sortedMappers = mappers.values().toArray(Mapper.class); Arrays.sort(sortedMappers, new Comparator<Mapper>() { @Override public int compare(Mapper o1, Mapper o2) { return o1.name().compareTo(o2.name()); } }); builder.startObject("fields"); for (Mapper mapper : sortedMappers) { mapper.toXContent(builder, params); } builder.endObject(); } return builder; } }
/** * {@inheritDoc} */ @Override public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { builder.startArray("snapshots"); for (ObjectCursor<Entry> entry : entries.values()) { toXContent(entry.value, builder, params); } builder.endArray(); return builder; }
private static String[] concreteAliases(AliasActions action, MetaData metaData, String concreteIndex) { if (action.expandAliasesWildcards()) { //for DELETE we expand the aliases String[] indexAsArray = {concreteIndex}; ImmutableOpenMap<String, List<AliasMetaData>> aliasMetaData = metaData.findAliases(action.aliases(), indexAsArray); List<String> finalAliases = new ArrayList<>(); for (ObjectCursor<List<AliasMetaData>> curAliases : aliasMetaData.values()) { for (AliasMetaData aliasMeta: curAliases.value) { finalAliases.add(aliasMeta.alias()); } } return finalAliases.toArray(new String[finalAliases.size()]); } else { //for ADD and REMOVE_INDEX we just return the current aliases return action.aliases(); } } }
public static RestoreInProgress.State overallState(RestoreInProgress.State nonCompletedState, ImmutableOpenMap<ShardId, RestoreInProgress.ShardRestoreStatus> shards) { boolean hasFailed = false; for (ObjectCursor<RestoreInProgress.ShardRestoreStatus> status : shards.values()) { if (!status.value.state().completed()) { return nonCompletedState; } if (status.value.state() == RestoreInProgress.State.FAILURE) { hasFailed = true; } } if (hasFailed) { return RestoreInProgress.State.FAILURE; } else { return RestoreInProgress.State.SUCCESS; } }
@Override public void applyClusterState(ClusterChangedEvent event) { ingestNodes = event.state().getNodes().getIngestNodes().values().toArray(DiscoveryNode.class); } }
@Override public void writeTo(StreamOutput out) throws IOException { out.writeLong(version); out.writeVInt(indicesRouting.size()); for (ObjectCursor<IndexRoutingTable> index : indicesRouting.values()) { index.value.writeTo(out); } }
public static boolean isGlobalStateEquals(MetaData metaData1, MetaData metaData2) { if (!metaData1.persistentSettings.equals(metaData2.persistentSettings)) { return false; } if (!metaData1.templates.equals(metaData2.templates())) { return false; } // Check if any persistent metadata needs to be saved int customCount1 = 0; for (ObjectObjectCursor<String, Custom> cursor : metaData1.customs) { if (cursor.value.context().contains(XContentContext.GATEWAY)) { if (!cursor.value.equals(metaData2.custom(cursor.key))) return false; customCount1++; } } int customCount2 = 0; for (ObjectCursor<Custom> cursor : metaData2.customs.values()) { if (cursor.value.context().contains(XContentContext.GATEWAY)) { customCount2++; } } if (customCount1 != customCount2) return false; return true; }
private List<Index> indicesDeletedFromClusterState() { // If the new cluster state has a new cluster UUID, the likely scenario is that a node was elected // master that has had its data directory wiped out, in which case we don't want to delete the indices and lose data; // rather we want to import them as dangling indices instead. So we check here if the cluster UUID differs from the previous // cluster UUID, in which case, we don't want to delete indices that the master erroneously believes shouldn't exist. // See test DiscoveryWithServiceDisruptionsIT.testIndicesDeleted() // See discussion on https://github.com/elastic/elasticsearch/pull/9952 and // https://github.com/elastic/elasticsearch/issues/11665 if (metaDataChanged() == false || isNewCluster()) { return Collections.emptyList(); } List<Index> deleted = null; for (ObjectCursor<IndexMetaData> cursor : previousState.metaData().indices().values()) { IndexMetaData index = cursor.value; IndexMetaData current = state.metaData().index(index.getIndex()); if (current == null) { if (deleted == null) { deleted = new ArrayList<>(); } deleted.add(index.getIndex()); } } return deleted == null ? Collections.<Index>emptyList() : deleted; }
private synchronized Map<String, DocumentMapper> internalMerge(IndexMetaData indexMetaData, MergeReason reason, boolean updateAllTypes, boolean onlyUpdateIfNeeded) { Map<String, CompressedXContent> map = new LinkedHashMap<>(); for (ObjectCursor<MappingMetaData> cursor : indexMetaData.getMappings().values()) { MappingMetaData mappingMetaData = cursor.value; if (onlyUpdateIfNeeded) { DocumentMapper existingMapper = documentMapper(mappingMetaData.type()); if (existingMapper == null || mappingMetaData.source().equals(existingMapper.mappingSource()) == false) { map.put(mappingMetaData.type(), mappingMetaData.source()); } } else { map.put(mappingMetaData.type(), mappingMetaData.source()); } } return internalMerge(map, reason, updateAllTypes); }
public boolean equalsAliases(MetaData other) { for (ObjectCursor<IndexMetaData> cursor : other.indices().values()) { IndexMetaData otherIndex = cursor.value; IndexMetaData thisIndex = index(otherIndex.getIndex()); if (thisIndex == null) { return false; } if (otherIndex.getAliases().equals(thisIndex.getAliases()) == false) { return false; } } return true; }
public MultiFields updateFieldType(Map<String, MappedFieldType> fullNameToFieldType) { ImmutableOpenMap.Builder<String, FieldMapper> newMappersBuilder = null; for (ObjectCursor<FieldMapper> cursor : mappers.values()) { FieldMapper updated = cursor.value.updateFieldType(fullNameToFieldType); if (updated != cursor.value) { if (newMappersBuilder == null) { newMappersBuilder = ImmutableOpenMap.builder(mappers); } newMappersBuilder.put(updated.simpleName(), updated); } } if (newMappersBuilder == null) { return this; } ImmutableOpenMap<String, FieldMapper> mappers = newMappersBuilder.build(); return new MultiFields(mappers); }
public void parse(FieldMapper mainField, ParseContext context) throws IOException { // TODO: multi fields are really just copy fields, we just need to expose "sub fields" or something that can be part // of the mappings if (mappers.isEmpty()) { return; } context = context.createMultiFieldContext(); context.path().add(mainField.simpleName()); for (ObjectCursor<FieldMapper> cursor : mappers.values()) { cursor.value.parse(context); } context.path().remove(); }
public MultiFields merge(MultiFields mergeWith) { ImmutableOpenMap.Builder<String, FieldMapper> newMappersBuilder = ImmutableOpenMap.builder(mappers); for (ObjectCursor<FieldMapper> cursor : mergeWith.mappers.values()) { FieldMapper mergeWithMapper = cursor.value; FieldMapper mergeIntoMapper = mappers.get(mergeWithMapper.simpleName()); if (mergeIntoMapper == null) { newMappersBuilder.put(mergeWithMapper.simpleName(), mergeWithMapper); } else { FieldMapper merged = mergeIntoMapper.merge(mergeWithMapper, false); newMappersBuilder.put(merged.simpleName(), merged); // override previous definition } } ImmutableOpenMap<String, FieldMapper> mappers = newMappersBuilder.build(); return new MultiFields(mappers); }
private boolean removedNodesCleanupNeeded(ClusterChangedEvent event) { SnapshotsInProgress snapshotsInProgress = event.state().custom(SnapshotsInProgress.TYPE); if (snapshotsInProgress == null) { return false; } // Check if we just became the master boolean newMaster = !event.previousState().nodes().isLocalNodeElectedMaster(); for (SnapshotsInProgress.Entry snapshot : snapshotsInProgress.entries()) { if (newMaster && (snapshot.state() == State.SUCCESS || snapshot.state() == State.INIT)) { // We just replaced old master and snapshots in intermediate states needs to be cleaned return true; } for (DiscoveryNode node : event.nodesDelta().removedNodes()) { for (ObjectCursor<ShardSnapshotStatus> shardStatus : snapshot.shards().values()) { if (!shardStatus.value.state().completed() && node.getId().equals(shardStatus.value.nodeId())) { // At least one shard was running on the removed node - we need to fail it return true; } } } } return false; }
/** * Returns a {@link DiskUsage} for the {@link RoutingNode} using the * average usage of other nodes in the disk usage map. * @param node Node to return an averaged DiskUsage object for * @param usages Map of nodeId to DiskUsage for all known nodes * @return DiskUsage representing given node using the average disk usage */ DiskUsage averageUsage(RoutingNode node, ImmutableOpenMap<String, DiskUsage> usages) { if (usages.size() == 0) { return new DiskUsage(node.nodeId(), node.node().getName(), "_na_", 0, 0); } long totalBytes = 0; long freeBytes = 0; for (ObjectCursor<DiskUsage> du : usages.values()) { totalBytes += du.value.getTotalBytes(); freeBytes += du.value.getFreeBytes(); } return new DiskUsage(node.nodeId(), node.node().getName(), "_na_", totalBytes / usages.size(), freeBytes / usages.size()); }