Search in sources :

Example 56 with Indices

use of org.graylog2.indexer.indices.Indices 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 57 with Indices

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

the class IndexRangesCleanupPeriodical method doRun.

@Override
public void doRun() {
    if (!cluster.isConnected() || !cluster.isHealthy()) {
        LOG.info("Skipping index range cleanup because the Elasticsearch cluster is unreachable or unhealthy");
        return;
    }
    final Set<String> indexNames = ImmutableSet.copyOf(indexSetRegistry.getManagedIndices());
    final SortedSet<IndexRange> indexRanges = indexRangeService.findAll();
    final Set<String> removedIndices = new HashSet<>();
    for (IndexRange indexRange : indexRanges) {
        if (!indexNames.contains(indexRange.indexName())) {
            removedIndices.add(indexRange.indexName());
        }
    }
    if (!removedIndices.isEmpty()) {
        LOG.info("Removing index range information for unavailable indices: {}", removedIndices);
        eventBus.post(IndicesDeletedEvent.create(removedIndices));
    }
}
Also used : IndexRange(org.graylog2.indexer.ranges.IndexRange) HashSet(java.util.HashSet)

Example 58 with Indices

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

the class IndicesResource method getOpenIndicesInfo.

private OpenIndicesInfo getOpenIndicesInfo(Set<IndexStatistics> indicesStatistics) {
    final Map<String, IndexInfo> indexInfos = new HashMap<>();
    final Set<String> indices = indicesStatistics.stream().map(IndexStatistics::index).collect(Collectors.toSet());
    final Map<String, Boolean> areReopened = this.indices.areReopened(indices);
    for (IndexStatistics indexStatistics : indicesStatistics) {
        final IndexInfo indexInfo = IndexInfo.create(indexStatistics.primaryShards(), indexStatistics.allShards(), fillShardRoutings(indexStatistics.routing()), areReopened.get(indexStatistics.index()));
        indexInfos.put(indexStatistics.index(), indexInfo);
    }
    return OpenIndicesInfo.create(indexInfos);
}
Also used : IndexStatistics(org.graylog2.indexer.indices.stats.IndexStatistics) HashMap(java.util.HashMap) IndexInfo(org.graylog2.rest.models.system.indexer.responses.IndexInfo)

Example 59 with Indices

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

the class IndicesResource method indexSetOpen.

@GET
@Path("/{indexSetId}/open")
@Timed
@ApiOperation(value = "Get information of all open indices managed by Graylog and their shards.")
@RequiresPermissions(RestPermissions.INDICES_READ)
@Produces(MediaType.APPLICATION_JSON)
public OpenIndicesInfo indexSetOpen(@ApiParam(name = "indexSetId") @PathParam("indexSetId") String indexSetId) {
    final IndexSet indexSet = getIndexSet(indexSetRegistry, indexSetId);
    final Set<IndexStatistics> indicesInfos = indices.getIndicesStats(indexSet).stream().filter(indexStats -> isPermitted(RestPermissions.INDICES_READ, indexStats.index())).collect(Collectors.toSet());
    return getOpenIndicesInfo(indicesInfos);
}
Also used : IndexStatistics(org.graylog2.indexer.indices.stats.IndexStatistics) IndicesReadRequest(org.graylog2.rest.models.system.indexer.requests.IndicesReadRequest) PathParam(javax.ws.rs.PathParam) AllIndices(org.graylog2.rest.models.system.indexer.responses.AllIndices) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) ApiParam(io.swagger.annotations.ApiParam) HashMap(java.util.HashMap) ApiResponses(io.swagger.annotations.ApiResponses) ClosedIndices(org.graylog2.rest.models.system.indexer.responses.ClosedIndices) Inject(javax.inject.Inject) Valid(javax.validation.Valid) ApiOperation(io.swagger.annotations.ApiOperation) ShardRouting(org.graylog2.rest.models.system.indexer.responses.ShardRouting) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) MediaType(javax.ws.rs.core.MediaType) Indices(org.graylog2.indexer.indices.Indices) Map(java.util.Map) AuditEvent(org.graylog2.audit.jersey.AuditEvent) IndexStatistics(org.graylog2.indexer.indices.stats.IndexStatistics) Api(io.swagger.annotations.Api) IndexSet(org.graylog2.indexer.IndexSet) IndexSetRegistry(org.graylog2.indexer.IndexSetRegistry) DELETE(javax.ws.rs.DELETE) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) ForbiddenException(javax.ws.rs.ForbiddenException) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) IndexInfo(org.graylog2.rest.models.system.indexer.responses.IndexInfo) Set(java.util.Set) RestResource(org.graylog2.shared.rest.resources.RestResource) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) NotFoundException(javax.ws.rs.NotFoundException) Timed(com.codahale.metrics.annotation.Timed) List(java.util.List) ApiResponse(io.swagger.annotations.ApiResponse) AuditEventTypes(org.graylog2.audit.AuditEventTypes) OpenIndicesInfo(org.graylog2.rest.models.system.indexer.responses.OpenIndicesInfo) RestPermissions(org.graylog2.shared.security.RestPermissions) NodeInfoCache(org.graylog2.indexer.NodeInfoCache) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) TooManyAliasesException(org.graylog2.indexer.indices.TooManyAliasesException) IndexSet(org.graylog2.indexer.IndexSet) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 60 with Indices

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

the class IndexRangesResource method rebuildIndex.

@POST
@Timed
@Path("/{index: [a-z_0-9-]+}/rebuild")
@ApiOperation(value = "Rebuild/sync index range information.", notes = "This triggers a system job that scans an index and stores meta information " + "about what indices contain messages in what time ranges. It atomically overwrites " + "already existing meta information.")
@ApiResponses(value = { @ApiResponse(code = 202, message = "Rebuild/sync system job triggered.") })
@Produces(MediaType.APPLICATION_JSON)
@AuditEvent(type = AuditEventTypes.ES_INDEX_RANGE_UPDATE_JOB)
public Response rebuildIndex(@ApiParam(name = "index", value = "The name of the Graylog-managed Elasticsearch index", required = true) @PathParam("index") @NotEmpty String index) {
    if (!indexSetRegistry.isManagedIndex(index)) {
        throw new BadRequestException(index + " is not a Graylog-managed Elasticsearch index.");
    }
    checkPermission(RestPermissions.INDEXRANGES_REBUILD, index);
    final SystemJob rebuildJob = singleIndexRangeJobFactory.create(indexSetRegistry.getAll(), index);
    try {
        this.systemJobManager.submit(rebuildJob);
    } catch (SystemJobConcurrencyException e) {
        final String msg = "Concurrency level of this job reached: " + e.getMessage();
        LOG.error(msg);
        throw new ForbiddenException(msg, e);
    }
    return Response.accepted().build();
}
Also used : SystemJob(org.graylog2.system.jobs.SystemJob) ForbiddenException(javax.ws.rs.ForbiddenException) SystemJobConcurrencyException(org.graylog2.system.jobs.SystemJobConcurrencyException) BadRequestException(javax.ws.rs.BadRequestException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

IndexSet (org.graylog2.indexer.IndexSet)20 IndexRange (org.graylog2.indexer.ranges.IndexRange)19 Set (java.util.Set)18 Test (org.junit.Test)18 DateTime (org.joda.time.DateTime)17 Map (java.util.Map)16 Collectors (java.util.stream.Collectors)15 List (java.util.List)14 TimeRange (org.graylog2.plugin.indexer.searches.timeranges.TimeRange)14 ZonedDateTime (java.time.ZonedDateTime)13 Inject (javax.inject.Inject)13 JsonNode (com.fasterxml.jackson.databind.JsonNode)11 ImmutableMap (com.google.common.collect.ImmutableMap)11 HashMap (java.util.HashMap)11 Indices (org.graylog2.indexer.indices.Indices)11 MongoIndexRange (org.graylog2.indexer.ranges.MongoIndexRange)11 Logger (org.slf4j.Logger)11 LoggerFactory (org.slf4j.LoggerFactory)11 Optional (java.util.Optional)10 ImmutableSet (com.google.common.collect.ImmutableSet)9