Search in sources :

Example 1 with TooManyAliasesException

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

the class V20161130141500_DefaultStreamRecalcIndexRanges method upgrade.

@Override
public void upgrade() {
    IndexSet indexSet;
    try {
        indexSet = indexSetRegistry.getDefault();
    } catch (IllegalStateException e) {
        // Try to find the default index set manually if the index set registry cannot find it.
        // This is needed if you run this migration with a 2.2.0-beta.2 state before commit 460cac6af.
        final IndexSetConfig indexSetConfig = indexSetService.findOne(DBQuery.is("default", true)).orElseThrow(() -> new IllegalStateException("No default index set configured! This is a bug!"));
        indexSet = indexSetFactory.create(indexSetConfig);
    }
    final IndexSet defaultIndexSet = indexSet;
    if (!cluster.isConnected()) {
        LOG.info("Cluster not connected yet, delaying migration until it is reachable.");
        while (true) {
            try {
                cluster.waitForConnectedAndDeflectorHealthy();
                break;
            } catch (InterruptedException | TimeoutException e) {
                LOG.warn("Interrupted or timed out waiting for Elasticsearch cluster, checking again.");
            }
        }
    }
    final Set<String> indexRangesWithoutStreams = indexRangeService.findAll().stream().filter(indexRange -> defaultIndexSet.isManagedIndex(indexRange.indexName())).filter(indexRange -> indexRange.streamIds() == null).map(IndexRange::indexName).collect(Collectors.toSet());
    if (indexRangesWithoutStreams.size() == 0) {
        // all ranges have a stream list, even if it is empty, nothing more to do
        return;
    }
    final String currentWriteTarget;
    try {
        currentWriteTarget = defaultIndexSet.getActiveWriteIndex();
    } catch (TooManyAliasesException e) {
        LOG.error("Multiple write targets found for write alias. Cannot continue to assign streams to older indices", e);
        return;
    }
    for (String indexName : defaultIndexSet.getManagedIndices()) {
        if (indexName.equals(currentWriteTarget)) {
            // do not recalculate for current write target
            continue;
        }
        if (!indexRangesWithoutStreams.contains(indexName)) {
            // already computed streams for this index
            continue;
        }
        LOG.info("Recalculating streams in index {}", indexName);
        final CreateNewSingleIndexRangeJob createNewSingleIndexRangeJob = rebuildIndexRangeJobFactory.create(indexSetRegistry.getAll(), indexName);
        createNewSingleIndexRangeJob.execute();
    }
}
Also used : CreateNewSingleIndexRangeJob(org.graylog2.indexer.ranges.CreateNewSingleIndexRangeJob) IndexRangeService(org.graylog2.indexer.ranges.IndexRangeService) Logger(org.slf4j.Logger) Cluster(org.graylog2.indexer.cluster.Cluster) IndexSetConfig(org.graylog2.indexer.indexset.IndexSetConfig) ZonedDateTime(java.time.ZonedDateTime) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) TimeoutException(java.util.concurrent.TimeoutException) DBQuery(org.mongojack.DBQuery) Collectors(java.util.stream.Collectors) IndexRange(org.graylog2.indexer.ranges.IndexRange) Inject(javax.inject.Inject) IndexSetService(org.graylog2.indexer.indexset.IndexSetService) MongoIndexSet(org.graylog2.indexer.MongoIndexSet) IndexSet(org.graylog2.indexer.IndexSet) TooManyAliasesException(org.graylog2.indexer.indices.TooManyAliasesException) IndexSetRegistry(org.graylog2.indexer.IndexSetRegistry) CreateNewSingleIndexRangeJob(org.graylog2.indexer.ranges.CreateNewSingleIndexRangeJob) IndexSetConfig(org.graylog2.indexer.indexset.IndexSetConfig) TooManyAliasesException(org.graylog2.indexer.indices.TooManyAliasesException) MongoIndexSet(org.graylog2.indexer.MongoIndexSet) IndexSet(org.graylog2.indexer.IndexSet) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with TooManyAliasesException

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

the class IndexFieldTypePollerPeriodical method poll.

private void poll(IndexSetConfig indexSetConfig) {
    final String indexSetTitle = indexSetConfig.title();
    final String indexSetId = indexSetConfig.id();
    scheduler.submit(() -> {
        try {
            final MongoIndexSet indexSet = mongoIndexSetFactory.create(indexSetConfig);
            // Only check the active write index on a regular basis, the others don't change anymore
            final String activeWriteIndex = indexSet.getActiveWriteIndex();
            if (activeWriteIndex != null) {
                LOG.debug("Updating index field types for active write index <{}> in index set <{}/{}>", activeWriteIndex, indexSetTitle, indexSetId);
                poller.pollIndex(activeWriteIndex, indexSetId).ifPresent(dbService::upsert);
            } else {
                LOG.warn("Active write index for index set \"{}\" ({}) doesn't exist yet", indexSetTitle, indexSetId);
            }
        } catch (TooManyAliasesException e) {
            LOG.error("Couldn't get active write index", e);
        } catch (Exception e) {
            LOG.error("Couldn't update field types for index set <{}/{}>", indexSetTitle, indexSetId, e);
        } finally {
            lastPoll.put(indexSetId, Instant.now());
        }
    });
}
Also used : MongoIndexSet(org.graylog2.indexer.MongoIndexSet) TooManyAliasesException(org.graylog2.indexer.indices.TooManyAliasesException) TimeoutException(java.util.concurrent.TimeoutException) TooManyAliasesException(org.graylog2.indexer.indices.TooManyAliasesException)

Example 3 with TooManyAliasesException

use of org.graylog2.indexer.indices.TooManyAliasesException 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.");
}
Also used : Stopwatch(com.google.common.base.Stopwatch) NotFoundException(org.graylog2.database.NotFoundException) TooManyAliasesException(org.graylog2.indexer.indices.TooManyAliasesException) IndexSet(org.graylog2.indexer.IndexSet) TooManyAliasesException(org.graylog2.indexer.indices.TooManyAliasesException) NotFoundException(org.graylog2.database.NotFoundException)

Example 4 with TooManyAliasesException

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

the class IndexRotationThread method checkAndRepair.

protected void checkAndRepair(IndexSet indexSet) {
    if (!indexSet.isUp()) {
        if (indices.exists(indexSet.getWriteIndexAlias())) {
            // Publish a notification if there is an *index* called graylog2_deflector
            Notification notification = notificationService.buildNow().addType(Notification.Type.DEFLECTOR_EXISTS_AS_INDEX).addSeverity(Notification.Severity.URGENT);
            final boolean published = notificationService.publishIfFirst(notification);
            if (published) {
                LOG.warn("There is an index called [" + indexSet.getWriteIndexAlias() + "]. Cannot fix this automatically and published a notification.");
            }
        } else {
            indexSet.setUp();
        }
    } else {
        try {
            String currentTarget;
            try {
                currentTarget = indexSet.getActiveWriteIndex();
            } catch (TooManyAliasesException e) {
                // If we get this exception, there are multiple indices which have the deflector alias set.
                // We try to cleanup the alias and try again. This should not happen, but might under certain
                // circumstances.
                indexSet.cleanupAliases(e.getIndices());
                try {
                    currentTarget = indexSet.getActiveWriteIndex();
                } catch (TooManyAliasesException e1) {
                    throw new IllegalStateException(e1);
                }
            }
            String shouldBeTarget = indexSet.getNewestIndex();
            if (!shouldBeTarget.equals(currentTarget)) {
                String msg = "Deflector is pointing to [" + currentTarget + "], not the newest one: [" + shouldBeTarget + "]. Re-pointing.";
                LOG.warn(msg);
                activityWriter.write(new Activity(msg, IndexRotationThread.class));
                if (indices.waitForRecovery(shouldBeTarget) == HealthStatus.Red) {
                    LOG.error("New target index for deflector didn't get healthy within timeout. Skipping deflector update.");
                } else {
                    indexSet.pointTo(shouldBeTarget, currentTarget);
                }
            }
        } catch (NoTargetIndexException e) {
            LOG.warn("Deflector is not up. Not trying to point to another index.");
        }
    }
}
Also used : NoTargetIndexException(org.graylog2.indexer.NoTargetIndexException) Activity(org.graylog2.shared.system.activities.Activity) TooManyAliasesException(org.graylog2.indexer.indices.TooManyAliasesException) Notification(org.graylog2.notifications.Notification)

Example 5 with TooManyAliasesException

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

the class IndexerOverviewResource method getIndexerOverview.

private IndexerOverview getIndexerOverview(IndexSet indexSet) throws TooManyAliasesException {
    final String indexSetId = indexSet.getConfig().id();
    final DeflectorSummary deflectorSummary = deflectorResource.deflector(indexSetId);
    final List<IndexRangeSummary> indexRanges = indexRangesResource.list().ranges();
    final JsonNode indexStats = indices.getIndexStats(indexSet);
    final List<String> indexNames = new ArrayList<>();
    indexStats.fieldNames().forEachRemaining(indexNames::add);
    final Map<String, Boolean> areReopened = indices.areReopened(indexNames);
    final Map<String, IndexSummary> indicesSummaries = buildIndexSummaries(deflectorSummary, indexSet, indexRanges, indexStats, areReopened);
    return IndexerOverview.create(deflectorSummary, IndexerClusterOverview.create(indexerClusterResource.clusterHealth(), indexerClusterResource.clusterName().name()), MessageCountResponse.create(counts.total(indexSet)), indicesSummaries);
}
Also used : DeflectorSummary(org.graylog2.rest.models.system.deflector.responses.DeflectorSummary) IndexRangeSummary(org.graylog2.rest.models.system.indexer.responses.IndexRangeSummary) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) IndexSummary(org.graylog2.rest.models.system.indexer.responses.IndexSummary)

Aggregations

TooManyAliasesException (org.graylog2.indexer.indices.TooManyAliasesException)4 TimeoutException (java.util.concurrent.TimeoutException)2 IndexSet (org.graylog2.indexer.IndexSet)2 MongoIndexSet (org.graylog2.indexer.MongoIndexSet)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Stopwatch (com.google.common.base.Stopwatch)1 ZonedDateTime (java.time.ZonedDateTime)1 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Inject (javax.inject.Inject)1 NotFoundException (org.graylog2.database.NotFoundException)1 IndexSetRegistry (org.graylog2.indexer.IndexSetRegistry)1 NoTargetIndexException (org.graylog2.indexer.NoTargetIndexException)1 Cluster (org.graylog2.indexer.cluster.Cluster)1 IndexSetConfig (org.graylog2.indexer.indexset.IndexSetConfig)1 IndexSetService (org.graylog2.indexer.indexset.IndexSetService)1 CreateNewSingleIndexRangeJob (org.graylog2.indexer.ranges.CreateNewSingleIndexRangeJob)1 IndexRange (org.graylog2.indexer.ranges.IndexRange)1 IndexRangeService (org.graylog2.indexer.ranges.IndexRangeService)1