org.graylog2.indexer.rotation.strategies.AbstractRotationStrategy Maven / Gradle / Ivy
/**
* This file is part of Graylog.
*
* Graylog is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graylog is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graylog. If not, see .
*/
package org.graylog2.indexer.rotation.strategies;
import org.graylog2.indexer.Deflector;
import org.graylog2.indexer.NoTargetIndexException;
import org.graylog2.plugin.indexer.rotation.RotationStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nullable;
public abstract class AbstractRotationStrategy implements RotationStrategy {
private static final Logger LOG = LoggerFactory.getLogger(AbstractRotationStrategy.class);
public interface Result {
String getDescription();
boolean shouldRotate();
}
private final Deflector deflector;
public AbstractRotationStrategy(Deflector deflector) {
this.deflector = deflector;
}
@Nullable
protected abstract Result shouldRotate(String indexName);
@Override
public void rotate() {
final String indexName;
try {
indexName = deflector.getNewestTargetName();
} catch (NoTargetIndexException e) {
LOG.error("Could not find current deflector target. Aborting.", e);
return;
}
final Result rotate = shouldRotate(indexName);
if (rotate == null) {
LOG.error("Cannot perform rotation at this moment.");
return;
}
LOG.debug("Rotation strategy result: {}", rotate.getDescription());
if (rotate.shouldRotate()) {
LOG.info("Deflector index <{}> should be rotated, Pointing deflector to new index now!", indexName);
deflector.cycle();
} else {
LOG.debug("Deflector index <{}> should not be rotated. Not doing anything.", indexName);
}
}
}