use of org.graylog2.indexer.indices.Indices in project graylog2-server by Graylog2.
the class RebuildIndexRangesJob method execute.
@Override
public void execute() {
info("Recalculating index ranges.");
// for each index set we know about
final ListMultimap<IndexSet, String> indexSets = MultimapBuilder.hashKeys().arrayListValues().build();
for (IndexSet indexSet : this.indexSets) {
final String[] managedIndicesNames = indexSet.getManagedIndices();
for (String name : managedIndicesNames) {
indexSets.put(indexSet, name);
}
}
if (indexSets.size() == 0) {
info("No indices, nothing to calculate.");
return;
}
indicesToCalculate = indexSets.values().size();
Stopwatch sw = Stopwatch.createStarted();
for (IndexSet indexSet : indexSets.keySet()) {
LOG.info("Recalculating index ranges for index set {} ({}): {} indices affected.", indexSet.getConfig().title(), indexSet.getIndexWildcard(), indexSets.get(indexSet).size());
for (String index : indexSets.get(indexSet)) {
try {
if (index.equals(indexSet.getActiveWriteIndex())) {
LOG.debug("{} is current write target, do not calculate index range for it", index);
final IndexRange emptyRange = indexRangeService.createUnknownRange(index);
try {
final IndexRange indexRange = indexRangeService.get(index);
if (indexRange.begin().getMillis() != 0 || indexRange.end().getMillis() != 0) {
LOG.info("Invalid date ranges for write index {}, resetting it.", index);
indexRangeService.save(emptyRange);
}
} catch (NotFoundException e) {
LOG.info("No index range found for write index {}, recreating it.", index);
indexRangeService.save(emptyRange);
}
indicesCalculated.incrementAndGet();
continue;
}
} catch (TooManyAliasesException e) {
LOG.error("Multiple write alias targets found, this is a bug.");
indicesCalculated.incrementAndGet();
continue;
}
if (cancelRequested) {
info("Stop requested. Not calculating next index range, not updating ranges.");
sw.stop();
return;
}
try {
final IndexRange indexRange = indexRangeService.calculateRange(index);
indexRangeService.save(indexRange);
LOG.info("Created ranges for index {}: {}", index, indexRange);
} catch (Exception e) {
LOG.info("Could not calculate range of index [" + index + "]. Skipping.", e);
} finally {
indicesCalculated.incrementAndGet();
}
}
}
info("Done calculating index ranges for " + indicesToCalculate + " indices. Took " + sw.stop().elapsed(TimeUnit.MILLISECONDS) + "ms.");
}
use of org.graylog2.indexer.indices.Indices in project graylog2-server by Graylog2.
the class Indices method getAllMessageFieldsForIndices.
public Map<String, Set<String>> getAllMessageFieldsForIndices(final String[] writeIndexWildcards) {
final Map<String, Set<String>> fields = new HashMap<>();
final ClusterStateRequest csr = new ClusterStateRequest().blocks(true).nodes(true).indices(writeIndexWildcards);
final ClusterState cs = c.admin().cluster().state(csr).actionGet().getState();
for (ObjectObjectCursor<String, IndexMetaData> m : cs.getMetaData().indices()) {
try {
MappingMetaData mmd = m.value.mapping(IndexMapping.TYPE_MESSAGE);
if (mmd == null) {
// There is no mapping if there are no messages in the index.
continue;
}
@SuppressWarnings("unchecked") final Map<String, Object> mapping = (Map<String, Object>) mmd.getSourceAsMap().get("properties");
if (mapping != null) {
fields.put(m.key, mapping.keySet());
}
} catch (Exception e) {
LOG.error("Error while trying to get fields of <" + m.index + ">", e);
}
}
return fields;
}
use of org.graylog2.indexer.indices.Indices in project graylog2-server by Graylog2.
the class Indices method ensureIndexTemplate.
private void ensureIndexTemplate(IndexSet indexSet) {
final Map<String, Object> template = indexMapping.messageTemplate(indexSet.getIndexWildcard(), indexSet.getConfig().indexAnalyzer());
final PutIndexTemplateRequest itr = c.admin().indices().preparePutTemplate(indexSet.getConfig().indexTemplateName()).setOrder(// Make sure templates with "order: 0" and higher are applied after our template!
-1).setSource(template).request();
try {
final boolean acknowledged = c.admin().indices().putTemplate(itr).actionGet().isAcknowledged();
if (acknowledged) {
LOG.info("Created Graylog index template \"{}\" in Elasticsearch.", indexSet.getConfig().indexTemplateName());
}
} catch (Exception e) {
LOG.error("Unable to create the Graylog index template: " + indexSet.getConfig().indexTemplateName(), e);
}
}
use of org.graylog2.indexer.indices.Indices in project graylog2-server by Graylog2.
the class IndexSetCleanupJob method execute.
@Override
public void execute() {
final IndexSetConfig config = indexSet.getConfig();
final String[] managedIndices = indexSet.getManagedIndices();
this.total = managedIndices.length;
try {
LOG.info("Deleting index template <{}> from Elasticsearch", config.indexTemplateName());
indices.deleteIndexTemplate(indexSet);
} catch (IndexTemplateMissingException ignored) {
LOG.debug("Unable to delete index template <{}> because it does not exist.", config.indexTemplateName());
} catch (Exception e) {
LOG.error("Unable to delete index template <{}>", config.indexTemplateName(), e);
}
for (String indexName : managedIndices) {
if (cancel) {
LOG.info("Cancel requested. Deleted <{}> of <{}> indices.", deleted, total);
break;
}
try {
LOG.info("Removing index range information for index: {}", indexName);
indexRangeService.remove(indexName);
LOG.info("Deleting index <{}> in index set <{}> ({})", indexName, config.id(), config.title());
indices.delete(indexName);
deleted.incrementAndGet();
} catch (Exception e) {
LOG.error("Unable to delete index <{}>", indexName, e);
}
}
}
use of org.graylog2.indexer.indices.Indices in project graylog2-server by Graylog2.
the class SearchesTest method determineAffectedIndicesDoesNotIncludesDeflectorTargetIfMissing.
@Test
public void determineAffectedIndicesDoesNotIncludesDeflectorTargetIfMissing() throws Exception {
final DateTime now = DateTime.now(DateTimeZone.UTC);
final MongoIndexRange indexRange0 = MongoIndexRange.create("graylog_0", now, now.plusDays(1), now, 0);
final MongoIndexRange indexRange1 = MongoIndexRange.create("graylog_1", now.plusDays(1), now.plusDays(2), now, 0);
final SortedSet<IndexRange> indices = ImmutableSortedSet.orderedBy(IndexRange.COMPARATOR).add(indexRange0).add(indexRange1).build();
when(indexRangeService.find(any(DateTime.class), any(DateTime.class))).thenReturn(indices);
final TimeRange absoluteRange = AbsoluteRange.create(now.minusDays(1), now.plusDays(1));
final TimeRange keywordRange = KeywordRange.create("1 day ago");
final TimeRange relativeRange = RelativeRange.create(3600);
assertThat(searches.determineAffectedIndices(absoluteRange, null)).containsOnly(indexRange0.indexName(), indexRange1.indexName());
assertThat(searches.determineAffectedIndices(keywordRange, null)).containsOnly(indexRange0.indexName(), indexRange1.indexName());
assertThat(searches.determineAffectedIndices(relativeRange, null)).containsOnly(indexRange0.indexName(), indexRange1.indexName());
}
Aggregations