Search in sources :

Example 1 with BuiltIndex

use of org.sonar.server.es.newindex.BuiltIndex in project sonarqube by SonarSource.

the class EsTester method createIndices.

private static List<BuiltIndex> createIndices(IndexDefinition... definitions) {
    IndexDefinitionContext context = new IndexDefinitionContext();
    Stream.of(definitions).forEach(d -> d.define(context));
    List<BuiltIndex> result = new ArrayList<>();
    for (NewIndex newIndex : context.getIndices().values()) {
        BuiltIndex index = newIndex.build();
        String indexName = index.getMainType().getIndex().getName();
        deleteIndexIfExists(indexName);
        // create index
        Settings.Builder settings = Settings.builder();
        settings.put(index.getSettings());
        CreateIndexResponse indexResponse = createIndex(indexName, settings);
        if (!indexResponse.isAcknowledged()) {
            throw new IllegalStateException("Failed to create index " + indexName);
        }
        waitForClusterYellowStatus(indexName);
        // create types
        String typeName = index.getMainType().getType();
        putIndexMapping(index, indexName, typeName);
        waitForClusterYellowStatus(indexName);
        result.add(index);
    }
    return result;
}
Also used : BuiltIndex(org.sonar.server.es.newindex.BuiltIndex) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) NewIndex(org.sonar.server.es.newindex.NewIndex) CreateIndexResponse(org.elasticsearch.client.indices.CreateIndexResponse) IndexDefinitionContext(org.sonar.server.es.IndexDefinition.IndexDefinitionContext) RecoverySettings(org.elasticsearch.indices.recovery.RecoverySettings) Settings(org.elasticsearch.common.settings.Settings) HttpTransportSettings(org.elasticsearch.http.HttpTransportSettings) DiskThresholdSettings(org.elasticsearch.cluster.routing.allocation.DiskThresholdSettings)

Example 2 with BuiltIndex

use of org.sonar.server.es.newindex.BuiltIndex in project sonarqube by SonarSource.

the class IndexCreator method createIndex.

private void createIndex(BuiltIndex<?> builtIndex, boolean useMetadata) {
    Index index = builtIndex.getMainType().getIndex();
    LOGGER.info(String.format("Create index [%s]", index.getName()));
    Settings.Builder settings = Settings.builder();
    settings.put(builtIndex.getSettings());
    if (useMetadata) {
        metadataIndex.setHash(index, IndexDefinitionHash.of(builtIndex));
        metadataIndex.setInitialized(builtIndex.getMainType(), false);
        builtIndex.getRelationTypes().forEach(relationType -> metadataIndex.setInitialized(relationType, false));
    }
    CreateIndexResponse indexResponse = client.create(new CreateIndexRequest(index.getName()).settings((settings)));
    if (!indexResponse.isAcknowledged()) {
        throw new IllegalStateException("Failed to create index [" + index.getName() + "]");
    }
    client.waitForStatus(ClusterHealthStatus.YELLOW);
    // create types
    LOGGER.info("Create type {}", builtIndex.getMainType().format());
    AcknowledgedResponse mappingResponse = client.putMapping(new PutMappingRequest(builtIndex.getMainType().getIndex().getName()).type(builtIndex.getMainType().getType()).source(builtIndex.getAttributes()));
    if (!mappingResponse.isAcknowledged()) {
        throw new IllegalStateException("Failed to create type " + builtIndex.getMainType().getType());
    }
    client.waitForStatus(ClusterHealthStatus.YELLOW);
}
Also used : PutMappingRequest(org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) BuiltIndex(org.sonar.server.es.newindex.BuiltIndex) MetadataIndex(org.sonar.server.es.metadata.MetadataIndex) NewIndex(org.sonar.server.es.newindex.NewIndex) CreateIndexResponse(org.elasticsearch.client.indices.CreateIndexResponse) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest) Settings(org.elasticsearch.common.settings.Settings)

Aggregations

CreateIndexResponse (org.elasticsearch.client.indices.CreateIndexResponse)2 Settings (org.elasticsearch.common.settings.Settings)2 BuiltIndex (org.sonar.server.es.newindex.BuiltIndex)2 NewIndex (org.sonar.server.es.newindex.NewIndex)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 ArrayList (java.util.ArrayList)1 PutMappingRequest (org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest)1 AcknowledgedResponse (org.elasticsearch.action.support.master.AcknowledgedResponse)1 CreateIndexRequest (org.elasticsearch.client.indices.CreateIndexRequest)1 DiskThresholdSettings (org.elasticsearch.cluster.routing.allocation.DiskThresholdSettings)1 HttpTransportSettings (org.elasticsearch.http.HttpTransportSettings)1 RecoverySettings (org.elasticsearch.indices.recovery.RecoverySettings)1 IndexDefinitionContext (org.sonar.server.es.IndexDefinition.IndexDefinitionContext)1 MetadataIndex (org.sonar.server.es.metadata.MetadataIndex)1