Search in sources :

Example 56 with IndexSet

use of org.graylog2.indexer.IndexSet in project graylog2-server by Graylog2.

the class IndicesTest method testCreateOverwritesIndexTemplate.

@Test
public void testCreateOverwritesIndexTemplate() throws Exception {
    final ObjectMapper mapper = new ObjectMapperProvider().get();
    final String templateName = indexSetConfig.indexTemplateName();
    final IndicesAdminClient client = this.client.admin().indices();
    final ImmutableMap<String, Object> beforeMapping = ImmutableMap.of("_source", ImmutableMap.of("enabled", false), "properties", ImmutableMap.of("message", ImmutableMap.of("type", "string", "index", "not_analyzed")));
    assertThat(client.preparePutTemplate(templateName).setTemplate(indexSet.getIndexWildcard()).addMapping(IndexMapping.TYPE_MESSAGE, beforeMapping).get().isAcknowledged()).isTrue();
    final GetIndexTemplatesResponse responseBefore = client.prepareGetTemplates(templateName).get();
    final List<IndexTemplateMetaData> beforeIndexTemplates = responseBefore.getIndexTemplates();
    assertThat(beforeIndexTemplates).hasSize(1);
    final ImmutableOpenMap<String, CompressedXContent> beforeMappings = beforeIndexTemplates.get(0).getMappings();
    final Map<String, Object> actualMapping = mapper.readValue(beforeMappings.get(IndexMapping.TYPE_MESSAGE).uncompressed(), new TypeReference<Map<String, Object>>() {
    });
    assertThat(actualMapping.get(IndexMapping.TYPE_MESSAGE)).isEqualTo(beforeMapping);
    indices.create("index_template_test", indexSet);
    final GetIndexTemplatesResponse responseAfter = client.prepareGetTemplates(templateName).get();
    assertThat(responseAfter.getIndexTemplates()).hasSize(1);
    final IndexTemplateMetaData templateMetaData = responseAfter.getIndexTemplates().get(0);
    assertThat(templateMetaData.getName()).isEqualTo(templateName);
    assertThat(templateMetaData.getMappings().keysIt()).containsExactly(IndexMapping.TYPE_MESSAGE);
    final Map<String, Object> mapping = mapper.readValue(templateMetaData.getMappings().get(IndexMapping.TYPE_MESSAGE).uncompressed(), new TypeReference<Map<String, Object>>() {
    });
    final Map<String, Object> expectedTemplate = new IndexMapping().messageTemplate(indexSet.getIndexWildcard(), indexSetConfig.indexAnalyzer());
    assertThat(mapping).isEqualTo(expectedTemplate.get("mappings"));
    final DeleteIndexTemplateRequest deleteRequest = client.prepareDeleteTemplate(templateName).request();
    final DeleteIndexTemplateResponse deleteResponse = client.deleteTemplate(deleteRequest).actionGet();
    assertThat(deleteResponse.isAcknowledged()).isTrue();
    indices.delete("index_template_test");
}
Also used : DeleteIndexTemplateResponse(org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse) IndicesAdminClient(org.elasticsearch.client.IndicesAdminClient) DeleteIndexTemplateRequest(org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) ObjectMapperProvider(org.graylog2.shared.bindings.providers.ObjectMapperProvider) IndexMapping(org.graylog2.indexer.IndexMapping) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) IndexTemplateMetaData(org.elasticsearch.cluster.metadata.IndexTemplateMetaData) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 57 with IndexSet

use of org.graylog2.indexer.IndexSet in project graylog2-server by Graylog2.

the class IndexCreatingDatabaseOperation method insert.

@Override
public void insert(InputStream dataScript) {
    waitForGreenStatus();
    final IndicesAdminClient indicesAdminClient = client.admin().indices();
    for (String index : indexes) {
        final IndicesExistsResponse indicesExistsResponse = indicesAdminClient.prepareExists(index).execute().actionGet();
        if (indicesExistsResponse.isExists()) {
            client.admin().indices().prepareDelete(index).execute().actionGet();
        }
        final Messages messages = new Messages(client, new MetricRegistry());
        final Indices indices = new Indices(client, new IndexMapping(), messages, mock(NodeId.class), new NullAuditEventSender());
        if (!indices.create(index, indexSet)) {
            throw new IllegalStateException("Couldn't create index " + index);
        }
    }
    databaseOperation.insert(dataScript);
}
Also used : NullAuditEventSender(org.graylog2.audit.NullAuditEventSender) IndexMapping(org.graylog2.indexer.IndexMapping) Messages(org.graylog2.indexer.messages.Messages) MetricRegistry(com.codahale.metrics.MetricRegistry) IndicesExistsResponse(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse) NodeId(org.graylog2.plugin.system.NodeId) Indices(org.graylog2.indexer.indices.Indices) IndicesAdminClient(org.elasticsearch.client.IndicesAdminClient)

Example 58 with IndexSet

use of org.graylog2.indexer.IndexSet in project graylog2-server by Graylog2.

the class IndexSetValidatorTest method validateWithConflict2.

@Test
public void validateWithConflict2() throws Exception {
    final IndexSetConfig newConfig = mock(IndexSetConfig.class);
    final IndexSet indexSet = mock(IndexSet.class);
    when(indexSetRegistry.iterator()).thenReturn(Collections.singleton(indexSet).iterator());
    // Existing index prefix starts with new index prefix
    when(indexSet.getIndexPrefix()).thenReturn("graylog");
    when(newConfig.indexPrefix()).thenReturn("gray");
    final Optional<IndexSetValidator.Violation> violation = validator.validate(newConfig);
    assertThat(violation).isPresent();
}
Also used : IndexSetConfig(org.graylog2.indexer.indexset.IndexSetConfig) Test(org.junit.Test)

Example 59 with IndexSet

use of org.graylog2.indexer.IndexSet in project graylog2-server by Graylog2.

the class MongoIndexSetRegistryTest method getAllShouldNotBeCachedForCallAfterInvalidate.

@Test
public void getAllShouldNotBeCachedForCallAfterInvalidate() {
    final IndexSetConfig indexSetConfig = mock(IndexSetConfig.class);
    final List<IndexSetConfig> indexSetConfigs = Collections.singletonList(indexSetConfig);
    final MongoIndexSet indexSet = mock(MongoIndexSet.class);
    when(mongoIndexSetFactory.create(indexSetConfig)).thenReturn(indexSet);
    when(indexSetService.findAll()).thenReturn(indexSetConfigs);
    assertThat(this.indexSetRegistry.getAll()).isNotNull().isNotEmpty().hasSize(1).containsExactly(indexSet);
    this.indexSetsCache.invalidate();
    assertThat(this.indexSetRegistry.getAll()).isNotNull().isNotEmpty().hasSize(1).containsExactly(indexSet);
    verify(indexSetService, times(2)).findAll();
}
Also used : IndexSetConfig(org.graylog2.indexer.indexset.IndexSetConfig) Test(org.junit.Test)

Example 60 with IndexSet

use of org.graylog2.indexer.IndexSet in project graylog2-server by Graylog2.

the class AbstractIndexCountBasedRetentionStrategyTest method shouldIgnoreWriteAliasWhenDeterminingRetainedIndices.

@Test
public void shouldIgnoreWriteAliasWhenDeterminingRetainedIndices() {
    final String indexWithWriteIndexAlias = "index1";
    final String writeIndexAlias = "WriteIndexAlias";
    when(indexSet.getWriteIndexAlias()).thenReturn(writeIndexAlias);
    indexMap.put(indexWithWriteIndexAlias, Collections.singleton(writeIndexAlias));
    retentionStrategy.retain(indexSet);
    final ArgumentCaptor<String> retainedIndexName = ArgumentCaptor.forClass(String.class);
    verify(retentionStrategy, times(1)).retain(retainedIndexName.capture(), eq(indexSet));
    assertThat(retainedIndexName.getValue()).isEqualTo("index2");
    verify(activityWriter, times(2)).write(any(Activity.class));
}
Also used : Activity(org.graylog2.shared.system.activities.Activity) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

IndexSet (org.graylog2.indexer.IndexSet)31 Test (org.junit.Test)26 IndexSetConfig (org.graylog2.indexer.indexset.IndexSetConfig)22 Timed (com.codahale.metrics.annotation.Timed)13 ApiOperation (io.swagger.annotations.ApiOperation)13 Path (javax.ws.rs.Path)12 AuditEvent (org.graylog2.audit.jersey.AuditEvent)11 ApiResponses (io.swagger.annotations.ApiResponses)10 Map (java.util.Map)9 POST (javax.ws.rs.POST)9 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)9 BadRequestException (javax.ws.rs.BadRequestException)8 NotFoundException (javax.ws.rs.NotFoundException)8 Produces (javax.ws.rs.Produces)8 DefaultIndexSetConfig (org.graylog2.indexer.indexset.DefaultIndexSetConfig)8 Set (java.util.Set)7 Collectors (java.util.stream.Collectors)7 Inject (javax.inject.Inject)7 IndexSetRegistry (org.graylog2.indexer.IndexSetRegistry)7 Api (io.swagger.annotations.Api)6