Search in sources :

Example 1 with NoTargetIndexException

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

the class MongoIndexSet method cycle.

@Override
public void cycle() {
    if (!getConfig().isWritable()) {
        LOG.debug("Not cycling non-writable index set <{}> ({})", getConfig().id(), getConfig().title());
        return;
    }
    int oldTargetNumber;
    try {
        oldTargetNumber = getNewestIndexNumber();
    } catch (NoTargetIndexException ex) {
        oldTargetNumber = -1;
    }
    final int newTargetNumber = oldTargetNumber + 1;
    final String newTarget = buildIndexName(newTargetNumber);
    final String oldTarget = buildIndexName(oldTargetNumber);
    if (oldTargetNumber == -1) {
        LOG.info("Cycling from <none> to <{}>.", newTarget);
    } else {
        LOG.info("Cycling from <{}> to <{}>.", oldTarget, newTarget);
    }
    // Create new index.
    LOG.info("Creating target index <{}>.", newTarget);
    if (!indices.create(newTarget, this)) {
        throw new RuntimeException("Could not create new target index <" + newTarget + ">.");
    }
    LOG.info("Waiting for allocation of index <{}>.", newTarget);
    final HealthStatus healthStatus = indices.waitForRecovery(newTarget);
    checkIfHealthy(healthStatus, (status) -> new RuntimeException("New target index did not become healthy (target index: <" + newTarget + ">)"));
    LOG.debug("Health status of index <{}>: {}", newTarget, healthStatus);
    addDeflectorIndexRange(newTarget);
    LOG.info("Index <{}> has been successfully allocated.", newTarget);
    // Point deflector to new index.
    final String indexAlias = getWriteIndexAlias();
    LOG.info("Pointing index alias <{}> to new index <{}>.", indexAlias, newTarget);
    final Activity activity = new Activity(IndexSet.class);
    if (oldTargetNumber == -1) {
        // Only pointing, not cycling.
        pointTo(newTarget);
        activity.setMessage("Cycled index alias <" + indexAlias + "> from <none> to <" + newTarget + ">.");
    } else {
        // Re-pointing from existing old index to the new one.
        LOG.debug("Switching over index alias <{}>.", indexAlias);
        pointTo(newTarget, oldTarget);
        setIndexReadOnlyAndCalculateRange(oldTarget);
        activity.setMessage("Cycled index alias <" + indexAlias + "> from <" + oldTarget + "> to <" + newTarget + ">.");
    }
    LOG.info("Successfully pointed index alias <{}> to index <{}>.", indexAlias, newTarget);
    activityWriter.write(activity);
    auditEventSender.success(AuditActor.system(nodeId), ES_WRITE_INDEX_UPDATE, ImmutableMap.of("indexName", newTarget));
}
Also used : HealthStatus(org.graylog2.indexer.indices.HealthStatus) Activity(org.graylog2.shared.system.activities.Activity)

Example 2 with NoTargetIndexException

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

the class IndexRotationThreadTest method testDoNotPerformRotation.

@Test
public void testDoNotPerformRotation() throws NoTargetIndexException {
    final Provider<RotationStrategy> provider = new RotationStrategyProvider();
    final IndexRotationThread rotationThread = new IndexRotationThread(notificationService, indices, indexSetRegistry, cluster, new NullActivityWriter(), nodeId, ImmutableMap.<String, Provider<RotationStrategy>>builder().put("strategy", provider).build());
    when(indexSetConfig.rotationStrategyClass()).thenReturn("strategy");
    rotationThread.checkForRotation(indexSet);
    verify(indexSet, never()).cycle();
}
Also used : NullActivityWriter(org.graylog2.shared.system.activities.NullActivityWriter) RotationStrategy(org.graylog2.plugin.indexer.rotation.RotationStrategy) Test(org.junit.Test)

Example 3 with NoTargetIndexException

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

the class IndexRotationThreadTest method testDoNotPerformRotationIfClusterIsDown.

@Test
public void testDoNotPerformRotationIfClusterIsDown() throws NoTargetIndexException {
    final Provider<RotationStrategy> provider = spy(new RotationStrategyProvider());
    when(cluster.isConnected()).thenReturn(false);
    final IndexRotationThread rotationThread = new IndexRotationThread(notificationService, indices, indexSetRegistry, cluster, new NullActivityWriter(), nodeId, ImmutableMap.<String, Provider<RotationStrategy>>builder().put("strategy", provider).build());
    rotationThread.doRun();
    verify(indexSet, never()).cycle();
    verify(provider, never()).get();
}
Also used : NullActivityWriter(org.graylog2.shared.system.activities.NullActivityWriter) RotationStrategy(org.graylog2.plugin.indexer.rotation.RotationStrategy) Test(org.junit.Test)

Example 4 with NoTargetIndexException

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

the class IndexRotationThreadTest method testPerformRotation.

@Test
public void testPerformRotation() throws NoTargetIndexException {
    final Provider<RotationStrategy> provider = new RotationStrategyProvider() {

        @Override
        public void doRotate(IndexSet indexSet) {
            indexSet.cycle();
        }
    };
    final IndexRotationThread rotationThread = new IndexRotationThread(notificationService, indices, indexSetRegistry, cluster, new NullActivityWriter(), nodeId, ImmutableMap.<String, Provider<RotationStrategy>>builder().put("strategy", provider).build());
    when(indexSetConfig.rotationStrategyClass()).thenReturn("strategy");
    rotationThread.checkForRotation(indexSet);
    verify(indexSet, times(1)).cycle();
}
Also used : NullActivityWriter(org.graylog2.shared.system.activities.NullActivityWriter) RotationStrategy(org.graylog2.plugin.indexer.rotation.RotationStrategy) IndexSet(org.graylog2.indexer.IndexSet) Test(org.junit.Test)

Example 5 with NoTargetIndexException

use of org.graylog2.indexer.NoTargetIndexException 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)

Aggregations

RotationStrategy (org.graylog2.plugin.indexer.rotation.RotationStrategy)3 NullActivityWriter (org.graylog2.shared.system.activities.NullActivityWriter)3 Test (org.junit.Test)3 Activity (org.graylog2.shared.system.activities.Activity)2 IndexSet (org.graylog2.indexer.IndexSet)1 NoTargetIndexException (org.graylog2.indexer.NoTargetIndexException)1 HealthStatus (org.graylog2.indexer.indices.HealthStatus)1 TooManyAliasesException (org.graylog2.indexer.indices.TooManyAliasesException)1 Notification (org.graylog2.notifications.Notification)1