com.carrotsearch.hppc.cursors.ObjectObjectCursor Java Examples

The following examples show how to use com.carrotsearch.hppc.cursors.ObjectObjectCursor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RestoreInProgress.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void writeTo(StreamOutput out) throws IOException {
    out.writeVInt(entries.size());
    for (Entry entry : entries) {
        entry.snapshot().writeTo(out);
        out.writeByte(entry.state().value());
        out.writeVInt(entry.indices().size());
        for (String index : entry.indices()) {
            out.writeString(index);
        }
        out.writeVInt(entry.shards().size());
        for (ObjectObjectCursor<ShardId, ShardRestoreStatus> shardEntry : entry.shards()) {
            shardEntry.key.writeTo(out);
            shardEntry.value.writeTo(out);
        }
    }
}
 
Example #2
Source File: AsyncShardFetch.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
/**
 * Fills the shard fetched data with new (data) nodes and a fresh NodeEntry, and removes from
 * it nodes that are no longer part of the state.
 */
private void fillShardCacheWithDataNodes(Map<String, NodeEntry<T>> shardCache, DiscoveryNodes nodes) {
    // verify that all current data nodes are there
    for (ObjectObjectCursor<String, DiscoveryNode> cursor : nodes.dataNodes()) {
        DiscoveryNode node = cursor.value;
        if (shardCache.containsKey(node.getId()) == false) {
            shardCache.put(node.getId(), new NodeEntry<T>(node.getId()));
        }
    }
    // remove nodes that are not longer part of the data nodes set
    for (Iterator<String> it = shardCache.keySet().iterator(); it.hasNext(); ) {
        String nodeId = it.next();
        if (nodes.nodeExists(nodeId) == false) {
            it.remove();
        }
    }
}
 
Example #3
Source File: DiscoveryNodes.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public DiscoveryNodes build() {
    ImmutableOpenMap.Builder<String, DiscoveryNode> dataNodesBuilder = ImmutableOpenMap.builder();
    ImmutableOpenMap.Builder<String, DiscoveryNode> masterNodesBuilder = ImmutableOpenMap.builder();
    Version minNodeVersion = Version.CURRENT;
    Version minNonClientNodeVersion = Version.CURRENT;
    for (ObjectObjectCursor<String, DiscoveryNode> nodeEntry : nodes) {
        if (nodeEntry.value.dataNode()) {
            dataNodesBuilder.put(nodeEntry.key, nodeEntry.value);
            minNonClientNodeVersion = Version.smallest(minNonClientNodeVersion, nodeEntry.value.version());
        }
        if (nodeEntry.value.masterNode()) {
            masterNodesBuilder.put(nodeEntry.key, nodeEntry.value);
            minNonClientNodeVersion = Version.smallest(minNonClientNodeVersion, nodeEntry.value.version());
        }
        minNodeVersion = Version.smallest(minNodeVersion, nodeEntry.value.version());
    }

    return new DiscoveryNodes(nodes.build(), dataNodesBuilder.build(), masterNodesBuilder.build(), deadNodes.build(), masterNodeId, localNodeId, minNodeVersion, minNonClientNodeVersion);
}
 
Example #4
Source File: TransportGetIndexTemplatesAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected void masterOperation(GetIndexTemplatesRequest request, ClusterState state, ActionListener<GetIndexTemplatesResponse> listener) {
    List<IndexTemplateMetaData> results;

    // If we did not ask for a specific name, then we return all templates
    if (request.names().length == 0) {
        results = Arrays.asList(state.metaData().templates().values().toArray(IndexTemplateMetaData.class));
    } else {
        results = new ArrayList<>();
    }

    for (String name : request.names()) {
        if (Regex.isSimpleMatchPattern(name)) {
            for (ObjectObjectCursor<String, IndexTemplateMetaData> entry : state.metaData().templates()) {
                if (Regex.simpleMatch(name, entry.key)) {
                    results.add(entry.value);
                }
            }
        } else if (state.metaData().templates().containsKey(name)) {
            results.add(state.metaData().templates().get(name));
        }
    }

    listener.onResponse(new GetIndexTemplatesResponse(results));
}
 
Example #5
Source File: ElasticsearchSchema.java    From dk-fitting with Apache License 2.0 6 votes vote down vote up
@Override
protected Map<String, Table> getTableMap() {
    final ImmutableMap.Builder<String, Table> builder = ImmutableMap.builder();

    try {
        GetMappingsResponse response = client.admin().indices().getMappings(
                new GetMappingsRequest().indices(index)).get();
        ImmutableOpenMap<String, MappingMetaData> mapping = response.getMappings().get(index);
        for (ObjectObjectCursor<String, MappingMetaData> c: mapping) {
            builder.put(c.key, new ElasticsearchTable(client, index, c.key));
        }
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
    return builder.build();
}
 
Example #6
Source File: ElasticsearchIntegrationTest.java    From openshift-elasticsearch-plugin with Apache License 2.0 6 votes vote down vote up
protected void dumpIndices() throws Exception {
    ThreadContext threadContext = esNode1.client().threadPool().getThreadContext();
    try (StoredContext cxt = threadContext.stashContext()) {
        threadContext.putHeader(ConfigConstants.SG_CONF_REQUEST_HEADER, "true");
        ClusterStateResponse response = esNode1.client().admin().cluster().prepareState().get();
        Iterator<ObjectObjectCursor<String, IndexMetaData>> iterator = response.getState().getMetaData().indices().iterator();
        while (iterator.hasNext()) {
            ObjectObjectCursor<String, IndexMetaData> c = (ObjectObjectCursor<String, IndexMetaData>) iterator.next();
            IndexMetaData meta = c.value;
            ImmutableOpenMap<String, MappingMetaData> mappings = meta.getMappings();
            Iterator<String> it = mappings.keysIt();
            while (it.hasNext()) {
                String key = it.next();
                System.out.println(String.format("%s %s %s", c.key, key,  mappings.get(key).type()));
            }
        }
    }
}
 
Example #7
Source File: GatewayMetaState.java    From crate with Apache License 2.0 6 votes vote down vote up
private static <Data> boolean applyPluginUpgraders(ImmutableOpenMap<String, Data> existingData,
                                                   UnaryOperator<Map<String, Data>> upgrader,
                                                   Consumer<String> removeData,
                                                   BiConsumer<String, Data> putData) {
    // collect current data
    Map<String, Data> existingMap = new HashMap<>();
    for (ObjectObjectCursor<String, Data> customCursor : existingData) {
        existingMap.put(customCursor.key, customCursor.value);
    }
    // upgrade global custom meta data
    Map<String, Data> upgradedCustoms = upgrader.apply(existingMap);
    if (upgradedCustoms.equals(existingMap) == false) {
        // remove all data first so a plugin can remove custom metadata or templates if needed
        existingMap.keySet().forEach(removeData);
        for (Map.Entry<String, Data> upgradedCustomEntry : upgradedCustoms.entrySet()) {
            putData.accept(upgradedCustomEntry.getKey(), upgradedCustomEntry.getValue());
        }
        return true;
    }
    return false;
}
 
Example #8
Source File: ClusterBlocks.java    From crate with Apache License 2.0 6 votes vote down vote up
private static EnumMap<ClusterBlockLevel, ImmutableLevelHolder> generateLevelHolders(Set<ClusterBlock> global,
        ImmutableOpenMap<String, Set<ClusterBlock>> indicesBlocks) {
    EnumMap<ClusterBlockLevel, ImmutableLevelHolder> levelHolders = new EnumMap<>(ClusterBlockLevel.class);
    for (final ClusterBlockLevel level : ClusterBlockLevel.values()) {
        Predicate<ClusterBlock> containsLevel = block -> block.contains(level);
        Set<ClusterBlock> newGlobal = unmodifiableSet(global.stream()
            .filter(containsLevel)
            .collect(toSet()));

        ImmutableOpenMap.Builder<String, Set<ClusterBlock>> indicesBuilder = ImmutableOpenMap.builder();
        for (ObjectObjectCursor<String, Set<ClusterBlock>> entry : indicesBlocks) {
            indicesBuilder.put(entry.key, unmodifiableSet(entry.value.stream()
                .filter(containsLevel)
                .collect(toSet())));
        }
        levelHolders.put(level, new ImmutableLevelHolder(newGlobal, indicesBuilder.build()));
    }
    return levelHolders;
}
 
Example #9
Source File: SnapshotsInProgress.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public void writeTo(StreamOutput out) throws IOException {
    out.writeVInt(entries.size());
    for (Entry entry : entries) {
        entry.snapshot().writeTo(out);
        out.writeBoolean(entry.includeGlobalState());
        out.writeBoolean(entry.partial());
        out.writeByte(entry.state().value());
        out.writeVInt(entry.indices().size());
        for (IndexId index : entry.indices()) {
            index.writeTo(out);
        }
        out.writeLong(entry.startTime());
        out.writeVInt(entry.shards().size());
        for (ObjectObjectCursor<ShardId, ShardSnapshotStatus> shardEntry : entry.shards()) {
            shardEntry.key.writeTo(out);
            shardEntry.value.writeTo(out);
        }
        out.writeLong(entry.repositoryStateId);
        out.writeOptionalString(entry.failure);
        if (out.getVersion().onOrAfter(SnapshotsService.SHARD_GEN_IN_REPO_DATA_VERSION)) {
            out.writeBoolean(entry.useShardGenerations);
        }
    }
}
 
Example #10
Source File: IndicesShardStoresResponse.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public void writeTo(StreamOutput out) throws IOException {
    super.writeTo(out);
    out.writeVInt(storeStatuses.size());
    for (ObjectObjectCursor<String, ImmutableOpenIntMap<List<StoreStatus>>> indexShards : storeStatuses) {
        out.writeString(indexShards.key);
        out.writeVInt(indexShards.value.size());
        for (IntObjectCursor<List<StoreStatus>> shardStatusesEntry : indexShards.value) {
            out.writeInt(shardStatusesEntry.key);
            out.writeVInt(shardStatusesEntry.value.size());
            for (StoreStatus storeStatus : shardStatusesEntry.value) {
                storeStatus.writeTo(out);
            }
        }
    }
    out.writeVInt(failures.size());
    for (ShardOperationFailedException failure : failures) {
        failure.writeTo(out);
    }
}
 
Example #11
Source File: ClusterChangedEvent.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a set of custom meta data types when any custom metadata for the cluster has changed
 * between the previous cluster state and the new cluster state. custom meta data types are
 * returned iff they have been added, updated or removed between the previous and the current state
 */
public Set<String> changedCustomMetaDataSet() {
    Set<String> result = new HashSet<>();
    ImmutableOpenMap<String, MetaData.Custom> currentCustoms = state.metaData().customs();
    ImmutableOpenMap<String, MetaData.Custom> previousCustoms = previousState.metaData().customs();
    if (currentCustoms.equals(previousCustoms) == false) {
        for (ObjectObjectCursor<String, MetaData.Custom> currentCustomMetaData : currentCustoms) {
            // new custom md added or existing custom md changed
            if (previousCustoms.containsKey(currentCustomMetaData.key) == false
                    || currentCustomMetaData.value.equals(previousCustoms.get(currentCustomMetaData.key)) == false) {
                result.add(currentCustomMetaData.key);
            }
        }
        // existing custom md deleted
        for (ObjectObjectCursor<String, MetaData.Custom> previousCustomMetaData : previousCustoms) {
            if (currentCustoms.containsKey(previousCustomMetaData.key) == false) {
                result.add(previousCustomMetaData.key);
            }
        }
    }
    return result;
}
 
Example #12
Source File: SnapshotsService.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Returns list of indices with missing shards, and list of indices that are closed
 *
 * @param shards list of shard statuses
 * @return list of failed and closed indices
 */
private static Tuple<Set<String>, Set<String>> indicesWithMissingShards(
    ImmutableOpenMap<ShardId, SnapshotsInProgress.ShardSnapshotStatus> shards, MetaData metaData) {
    Set<String> missing = new HashSet<>();
    Set<String> closed = new HashSet<>();
    for (ObjectObjectCursor<ShardId, SnapshotsInProgress.ShardSnapshotStatus> entry : shards) {
        if (entry.value.state() == ShardState.MISSING) {
            if (metaData.hasIndex(entry.key.getIndex().getName()) &&
                metaData.getIndexSafe(entry.key.getIndex()).getState() == IndexMetaData.State.CLOSE) {
                closed.add(entry.key.getIndex().getName());
            } else {
                missing.add(entry.key.getIndex().getName());
            }
        }
    }
    return new Tuple<>(missing, closed);
}
 
Example #13
Source File: RestoreService.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Check if any of the indices to be closed are currently being restored from a snapshot and fail closing if such an index
 * is found as closing an index that is being restored makes the index unusable (it cannot be recovered).
 */
public static void checkIndexClosing(ClusterState currentState, Set<IndexMetaData> indices) {
    RestoreInProgress restore = currentState.custom(RestoreInProgress.TYPE);
    if (restore != null) {
        Set<Index> indicesToFail = null;
        for (RestoreInProgress.Entry entry : restore.entries()) {
            for (ObjectObjectCursor<ShardId, RestoreInProgress.ShardRestoreStatus> shard : entry.shards()) {
                if (!shard.value.state().completed()) {
                    IndexMetaData indexMetaData = currentState.metaData().index(shard.key.getIndex());
                    if (indexMetaData != null && indices.contains(indexMetaData)) {
                        if (indicesToFail == null) {
                            indicesToFail = new HashSet<>();
                        }
                        indicesToFail.add(shard.key.getIndex());
                    }
                }
            }
        }
        if (indicesToFail != null) {
            throw new IllegalArgumentException("Cannot close indices that are being restored: " + indicesToFail);
        }
    }
}
 
Example #14
Source File: TransportGetIndexTemplatesAction.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
protected void masterOperation(GetIndexTemplatesRequest request, ClusterState state, ActionListener<GetIndexTemplatesResponse> listener) {
    List<IndexTemplateMetaData> results;

    // If we did not ask for a specific name, then we return all templates
    if (request.names().length == 0) {
        results = Arrays.asList(state.metaData().templates().values().toArray(IndexTemplateMetaData.class));
    } else {
        results = new ArrayList<>();
    }

    for (String name : request.names()) {
        if (Regex.isSimpleMatchPattern(name)) {
            for (ObjectObjectCursor<String, IndexTemplateMetaData> entry : state.metaData().templates()) {
                if (Regex.simpleMatch(name, entry.key)) {
                    results.add(entry.value);
                }
            }
        } else if (state.metaData().templates().containsKey(name)) {
            results.add(state.metaData().templates().get(name));
        }
    }

    listener.onResponse(new GetIndexTemplatesResponse(results));
}
 
Example #15
Source File: TransportSchemaUpdateAction.java    From crate with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
static ClusterState updateTemplate(NamedXContentRegistry xContentRegistry,
                                   ClusterState currentState,
                                   String templateName,
                                   Map<String, Object> newMapping) throws Exception {
    IndexTemplateMetaData template = currentState.metaData().templates().get(templateName);
    if (template == null) {
        throw new ResourceNotFoundException("Template \"" + templateName + "\" for partitioned table is missing");
    }

    IndexTemplateMetaData.Builder templateBuilder = new IndexTemplateMetaData.Builder(template);
    for (ObjectObjectCursor<String, CompressedXContent> cursor : template.mappings()) {
        Map<String, Object> source = parseMapping(xContentRegistry, cursor.value.toString());
        mergeIntoSource(source, newMapping);
        try (XContentBuilder xContentBuilder = JsonXContent.contentBuilder()) {
            templateBuilder.putMapping(cursor.key, Strings.toString(xContentBuilder.map(source)));
        }
    }
    MetaData.Builder builder = MetaData.builder(currentState.metaData()).put(templateBuilder);
    return ClusterState.builder(currentState).metaData(builder).build();
}
 
Example #16
Source File: Templates.java    From crate with Apache License 2.0 6 votes vote down vote up
public static IndexTemplateMetaData.Builder copyWithNewName(IndexTemplateMetaData source, RelationName newName) {
    String targetTemplateName = PartitionName.templateName(newName.schema(), newName.name());
    String targetAlias = newName.indexNameOrAlias();
    IndexTemplateMetaData.Builder templateBuilder = IndexTemplateMetaData
        .builder(targetTemplateName)
        .patterns(Collections.singletonList(PartitionName.templatePrefix(newName.schema(), newName.name())))
        .settings(source.settings())
        .order(source.order())
        .putAlias(AliasMetaData.builder(targetAlias).build())
        .version(source.version());

    for (ObjectObjectCursor<String, CompressedXContent> mapping : source.mappings()) {
        try {
            templateBuilder.putMapping(mapping.key, mapping.value);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    return templateBuilder;
}
 
Example #17
Source File: PartitionInfos.java    From crate with Apache License 2.0 6 votes vote down vote up
private static PartitionInfo createPartitionInfo(ObjectObjectCursor<String, IndexMetaData> indexMetaDataEntry) {
    PartitionName partitionName = PartitionName.fromIndexOrTemplate(indexMetaDataEntry.key);
    try {
        IndexMetaData indexMetaData = indexMetaDataEntry.value;
        MappingMetaData mappingMetaData = indexMetaData.mapping(Constants.DEFAULT_MAPPING_TYPE);
        Map<String, Object> mappingMap = mappingMetaData.sourceAsMap();
        Map<String, Object> valuesMap = buildValuesMap(partitionName, mappingMetaData);
        Settings settings = indexMetaData.getSettings();
        String numberOfReplicas = NumberOfReplicas.fromSettings(settings);
        return new PartitionInfo(
            partitionName,
            indexMetaData.getNumberOfShards(),
            numberOfReplicas,
            IndexMetaData.SETTING_INDEX_VERSION_CREATED.get(settings),
            settings.getAsVersion(IndexMetaData.SETTING_VERSION_UPGRADED, null),
            DocIndexMetaData.isClosed(indexMetaData, mappingMap, false),
            valuesMap,
            indexMetaData.getSettings());
    } catch (Exception e) {
        LOGGER.trace("error extracting partition infos from index {}", e, indexMetaDataEntry.key);
        return null; // must filter on null
    }
}
 
Example #18
Source File: RestAliasAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private Table buildTable(RestRequest request, GetAliasesResponse response) {
    Table table = getTableWithHeader(request);

    for (ObjectObjectCursor<String, List<AliasMetaData>> cursor : response.getAliases()) {
        String indexName = cursor.key;
        for (AliasMetaData aliasMetaData : cursor.value) {
            table.startRow();
            table.addCell(aliasMetaData.alias());
            table.addCell(indexName);
            table.addCell(aliasMetaData.filteringRequired() ? "*" : "-");
            String indexRouting = Strings.hasLength(aliasMetaData.indexRouting()) ? aliasMetaData.indexRouting() : "-";
            table.addCell(indexRouting);
            String searchRouting = Strings.hasLength(aliasMetaData.searchRouting()) ? aliasMetaData.searchRouting() : "-";
            table.addCell(searchRouting);
            table.endRow();
        }
    }

    return table;
}
 
Example #19
Source File: ElasticsearchSchema.java    From dk-fitting with Apache License 2.0 6 votes vote down vote up
@Override
protected Map<String, Table> getTableMap() {
    final ImmutableMap.Builder<String, Table> builder = ImmutableMap.builder();

    try {
        GetMappingsResponse response = client.admin().indices().getMappings(
                new GetMappingsRequest().indices(index)).get();
        ImmutableOpenMap<String, MappingMetaData> mapping = response.getMappings().get(index);
        for (ObjectObjectCursor<String, MappingMetaData> c: mapping) {
            builder.put(c.key, new ElasticsearchTable(client, index, c.key));
        }
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
    return builder.build();
}
 
Example #20
Source File: AggregatedDfs.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public void writeTo(final StreamOutput out) throws IOException {
    out.writeVInt(termStatistics.size());
    
    for (ObjectObjectCursor<Term, TermStatistics> c : termStatistics()) {
        Term term = (Term) c.key;
        out.writeString(term.field());
        out.writeBytesRef(term.bytes());
        TermStatistics stats = (TermStatistics) c.value;
        out.writeBytesRef(stats.term());
        out.writeVLong(stats.docFreq());
        out.writeVLong(DfsSearchResult.addOne(stats.totalTermFreq()));
    }

    DfsSearchResult.writeFieldStats(out, fieldStatistics);
    out.writeVLong(maxDoc);
}
 
Example #21
Source File: AsyncShardFetch.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Fills the shard fetched data with new (data) nodes and a fresh NodeEntry, and removes from
 * it nodes that are no longer part of the state.
 */
private void fillShardCacheWithDataNodes(Map<String, NodeEntry<T>> shardCache, DiscoveryNodes nodes) {
    // verify that all current data nodes are there
    for (ObjectObjectCursor<String, DiscoveryNode> cursor : nodes.getDataNodes()) {
        DiscoveryNode node = cursor.value;
        if (shardCache.containsKey(node.getId()) == false) {
            shardCache.put(node.getId(), new NodeEntry<T>(node.getId()));
        }
    }
    // remove nodes that are not longer part of the data nodes set
    shardCache.keySet().removeIf(nodeId -> !nodes.nodeExists(nodeId));
}
 
Example #22
Source File: RestGetSettingsAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
    final String[] names = request.paramAsStringArrayOrEmptyIfAll("name");
    GetSettingsRequest getSettingsRequest = new GetSettingsRequest()
            .indices(Strings.splitStringByCommaToArray(request.param("index")))
            .indicesOptions(IndicesOptions.fromRequest(request, IndicesOptions.strictExpandOpen()))
            .humanReadable(request.hasParam("human"))
            .names(names);
    getSettingsRequest.local(request.paramAsBoolean("local", getSettingsRequest.local()));

    client.admin().indices().getSettings(getSettingsRequest, new RestBuilderListener<GetSettingsResponse>(channel) {

        @Override
        public RestResponse buildResponse(GetSettingsResponse getSettingsResponse, XContentBuilder builder) throws Exception {
            builder.startObject();
            for (ObjectObjectCursor<String, Settings> cursor : getSettingsResponse.getIndexToSettings()) {
                // no settings, jump over it to shorten the response data
                if (cursor.value.getAsMap().isEmpty()) {
                    continue;
                }
                builder.startObject(cursor.key, XContentBuilder.FieldCaseConversion.NONE);
                builder.startObject(Fields.SETTINGS);
                cursor.value.toXContent(builder, request);
                builder.endObject();
                builder.endObject();
            }
            builder.endObject();
            return new BytesRestResponse(OK, builder);
        }
    });
}
 
Example #23
Source File: RoutingTable.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    StringBuilder sb = new StringBuilder("routing_table (version ").append(version).append("):\n");
    for (ObjectObjectCursor<String, IndexRoutingTable> entry : indicesRouting) {
        sb.append(entry.value.prettyPrint()).append('\n');
    }
    return sb.toString();
}
 
Example #24
Source File: AlterTableOperation.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private ListenableFuture<Long> updateTemplate(Map<String, Object> newMappings,
                                              Settings newSettings,
                                              TableIdent tableIdent,
                                              AbstractDDLAnalyzedStatement statement) {
    String templateName = PartitionName.templateName(tableIdent.schema(), tableIdent.name());
    IndexTemplateMetaData indexTemplateMetaData =
            clusterService.state().metaData().templates().get(templateName);
    if (indexTemplateMetaData == null) {
        return Futures.immediateFailedFuture(new RuntimeException("Template for partitioned table is missing"));
    }

    // merge mappings
    Map<String, Object> mapping = mergeTemplateMapping(indexTemplateMetaData, newMappings);

    // merge settings
    Settings.Builder settingsBuilder = Settings.builder();
    settingsBuilder.put(indexTemplateMetaData.settings());
    settingsBuilder.put(newSettings);

    PutIndexTemplateRequest request = new PutIndexTemplateRequest(templateName)
            .create(false)
            .mapping(Constants.DEFAULT_MAPPING_TYPE, mapping)
            .order(indexTemplateMetaData.order())
            .settings(settingsBuilder.build())
            .template(indexTemplateMetaData.template());

    request.putHeader(LoginUserContext.USER_INFO_KEY, statement.getParameterContext().getLoginUserContext());
    for (ObjectObjectCursor<String, AliasMetaData> container : indexTemplateMetaData.aliases()) {
        Alias alias = new Alias(container.key);
        request.alias(alias);
    }

    SettableFuture<Long> result = SettableFuture.create();
    transportActionProvider.transportPutIndexTemplateAction().execute(request,
            new SettableFutureToNullActionListener<PutIndexTemplateResponse>(result));

    return result;
}
 
Example #25
Source File: ClusterBlocks.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void writeTo(StreamOutput out) throws IOException {
    writeBlockSet(global, out);
    out.writeVInt(indicesBlocks.size());
    for (ObjectObjectCursor<String, Set<ClusterBlock>> entry : indicesBlocks) {
        out.writeString(entry.key);
        writeBlockSet(entry.value, out);
    }
}
 
Example #26
Source File: TemplateUpgradeService.java    From crate with Apache License 2.0 5 votes vote down vote up
Optional<Tuple<Map<String, BytesReference>, Set<String>>> calculateTemplateChanges(
    ImmutableOpenMap<String, IndexTemplateMetaData> templates) {
    // collect current templates
    Map<String, IndexTemplateMetaData> existingMap = new HashMap<>();
    for (ObjectObjectCursor<String, IndexTemplateMetaData> customCursor : templates) {
        existingMap.put(customCursor.key, customCursor.value);
    }
    // upgrade global custom meta data
    Map<String, IndexTemplateMetaData> upgradedMap = indexTemplateMetaDataUpgraders.apply(existingMap);
    if (upgradedMap.equals(existingMap) == false) {
        Set<String> deletes = new HashSet<>();
        Map<String, BytesReference> changes = new HashMap<>();
        // remove templates if needed
        existingMap.keySet().forEach(s -> {
            if (upgradedMap.containsKey(s) == false) {
                deletes.add(s);
            }
        });
        upgradedMap.forEach((key, value) -> {
            if (value.equals(existingMap.get(key)) == false) {
                changes.put(key, toBytesReference(value));
            }
        });
        return Optional.of(new Tuple<>(changes, deletes));
    }
    return Optional.empty();
}
 
Example #27
Source File: MetaData.java    From crate with Apache License 2.0 5 votes vote down vote up
private static ImmutableOpenMap<String, MappingMetaData> filterFields(ImmutableOpenMap<String, MappingMetaData> mappings,
                                                                      Predicate<String> fieldPredicate) throws IOException {
    if (fieldPredicate == MapperPlugin.NOOP_FIELD_PREDICATE) {
        return mappings;
    }
    ImmutableOpenMap.Builder<String, MappingMetaData> builder = ImmutableOpenMap.builder(mappings.size());
    for (ObjectObjectCursor<String, MappingMetaData> cursor : mappings) {
        builder.put(cursor.key, filterFields(cursor.value, fieldPredicate));
    }
    return builder.build(); // No types specified means return them all
}
 
Example #28
Source File: SnapshotsInProgress.java    From crate with Apache License 2.0 5 votes vote down vote up
public void toXContent(Entry entry, XContentBuilder builder, ToXContent.Params params) throws IOException {
    builder.startObject();
    builder.field(REPOSITORY, entry.snapshot().getRepository());
    builder.field(SNAPSHOT, entry.snapshot().getSnapshotId().getName());
    builder.field(UUID, entry.snapshot().getSnapshotId().getUUID());
    builder.field(INCLUDE_GLOBAL_STATE, entry.includeGlobalState());
    builder.field(PARTIAL, entry.partial());
    builder.field(STATE, entry.state());
    builder.startArray(INDICES);
    {
        for (IndexId index : entry.indices()) {
            index.toXContent(builder, params);
        }
    }
    builder.endArray();
    builder.humanReadableField(START_TIME_MILLIS, START_TIME, new TimeValue(entry.startTime()));
    builder.field(REPOSITORY_STATE_ID, entry.getRepositoryStateId());
    builder.startArray(SHARDS);
    {
        for (ObjectObjectCursor<ShardId, ShardSnapshotStatus> shardEntry : entry.shards) {
            ShardId shardId = shardEntry.key;
            ShardSnapshotStatus status = shardEntry.value;
            builder.startObject();
            {
                builder.field(INDEX, shardId.getIndex());
                builder.field(SHARD, shardId.getId());
                builder.field(STATE, status.state());
                builder.field(NODE, status.nodeId());
            }
            builder.endObject();
        }
    }
    builder.endArray();
    builder.endObject();
}
 
Example #29
Source File: DiscoveryNodes.java    From crate with Apache License 2.0 5 votes vote down vote up
public DiscoveryNodes build() {
    ImmutableOpenMap.Builder<String, DiscoveryNode> dataNodesBuilder = ImmutableOpenMap.builder();
    ImmutableOpenMap.Builder<String, DiscoveryNode> masterNodesBuilder = ImmutableOpenMap.builder();
    Version minNodeVersion = null;
    Version maxNodeVersion = null;
    Version minNonClientNodeVersion = null;
    Version maxNonClientNodeVersion = null;
    for (ObjectObjectCursor<String, DiscoveryNode> nodeEntry : nodes) {
        if (nodeEntry.value.isDataNode()) {
            dataNodesBuilder.put(nodeEntry.key, nodeEntry.value);
        }
        if (nodeEntry.value.isMasterNode()) {
            masterNodesBuilder.put(nodeEntry.key, nodeEntry.value);
        }
        final Version version = nodeEntry.value.getVersion();
        if (nodeEntry.value.isDataNode() || nodeEntry.value.isMasterNode()) {
            if (minNonClientNodeVersion == null) {
                minNonClientNodeVersion = version;
                maxNonClientNodeVersion = version;
            } else {
                minNonClientNodeVersion = Version.min(minNonClientNodeVersion, version);
                maxNonClientNodeVersion = Version.max(maxNonClientNodeVersion, version);
            }
        }
        minNodeVersion = minNodeVersion == null ? version : Version.min(minNodeVersion, version);
        maxNodeVersion = maxNodeVersion == null ? version : Version.max(maxNodeVersion, version);
    }

    return new DiscoveryNodes(
        nodes.build(),
        dataNodesBuilder.build(),
        masterNodesBuilder.build(),
        masterNodeId,
        localNodeId,
        minNonClientNodeVersion == null ? Version.CURRENT : minNonClientNodeVersion,
        maxNonClientNodeVersion == null ? Version.CURRENT : maxNonClientNodeVersion,
        maxNodeVersion == null ? Version.CURRENT : maxNodeVersion,
        minNodeVersion == null ? Version.CURRENT : minNodeVersion
    );
}
 
Example #30
Source File: Elasticsearch5Client.java    From presto-connectors with Apache License 2.0 5 votes vote down vote up
@Override
public ElasticsearchTable getTable(SchemaTableName tableName)
{
    String indexWildcard = tableName.getTableName();
    GetIndexRequest getIndexRequest = createGetIndexRequest(indexWildcard);
    Thread.currentThread().setName("getTable_001"); //----es scher error --
    GetIndexResponse response = client.admin().indices()
            .getIndex(getIndexRequest).actionGet();
    if (response.getIndices() == null || response.getIndices().length == 0) {
        return null;
    }
    //TODO: es中运行index名访问时可以使用*进行匹配,所以可能会返回多个index的mapping, 因此下面需要进行mapping merge  test table = test1"*"
    ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings = response.getMappings();

    List<IndexResolution> resolutions;
    if (mappings.size() > 0) {
        resolutions = new ArrayList<>(mappings.size());
        for (ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>> indexMappings : mappings) {
            resolutions.add(buildGetIndexResult(indexMappings.key, indexMappings.value));
        }
    }
    else {
        resolutions = emptyList();
    }

    IndexResolution indexWithMerged = merge(resolutions, indexWildcard);
    return new ElasticsearchTable(typeManager, tableName.getSchemaName(), tableName.getTableName(), indexWithMerged.get());
}