Skip to content

[8.0] Simplify LocalExporter cleaner function to fix failing tests (#83812) #83992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -598,64 +598,64 @@ private boolean canUseWatcher() {

@Override
public void onCleanUpIndices(TimeValue retention) {
if (state.get() != State.RUNNING) {
ClusterState clusterState = clusterService.state();
if (clusterService.localNode() == null
|| clusterState == null
|| clusterState.blocks().hasGlobalBlockWithLevel(ClusterBlockLevel.METADATA_WRITE)) {
logger.debug("exporter not ready");
return;
}

if (clusterService.state().nodes().isLocalNodeElectedMaster()) {
if (clusterState.nodes().isLocalNodeElectedMaster()) {
// Reference date time will be compared to index.creation_date settings,
// that's why it must be in UTC
ZonedDateTime expiration = ZonedDateTime.now(ZoneOffset.UTC).minus(retention.millis(), ChronoUnit.MILLIS);
logger.debug("cleaning indices [expiration={}, retention={}]", expiration, retention);

ClusterState clusterState = clusterService.state();
if (clusterState != null) {
final long expirationTimeMillis = expiration.toInstant().toEpochMilli();
final long currentTimeMillis = System.currentTimeMillis();
final long expirationTimeMillis = expiration.toInstant().toEpochMilli();
final long currentTimeMillis = System.currentTimeMillis();

// list of index patterns that we clean up
final String[] indexPatterns = new String[] { ".monitoring-*" };
// list of index patterns that we clean up
final String[] indexPatterns = new String[] { ".monitoring-*" };

// Get the names of the current monitoring indices
final Set<String> currents = MonitoredSystem.allSystems()
.map(s -> MonitoringTemplateUtils.indexName(dateTimeFormatter, s, currentTimeMillis))
.collect(Collectors.toSet());
// Get the names of the current monitoring indices
final Set<String> currents = MonitoredSystem.allSystems()
.map(s -> MonitoringTemplateUtils.indexName(dateTimeFormatter, s, currentTimeMillis))
.collect(Collectors.toSet());

// avoid deleting the current alerts index, but feel free to delete older ones
currents.add(MonitoringTemplateRegistry.ALERTS_INDEX_TEMPLATE_NAME);
// avoid deleting the current alerts index, but feel free to delete older ones
currents.add(MonitoringTemplateRegistry.ALERTS_INDEX_TEMPLATE_NAME);

Set<String> indices = new HashSet<>();
for (ObjectObjectCursor<String, IndexMetadata> index : clusterState.getMetadata().indices()) {
String indexName = index.key;
Set<String> indices = new HashSet<>();
for (ObjectObjectCursor<String, IndexMetadata> index : clusterState.getMetadata().indices()) {
String indexName = index.key;

if (Regex.simpleMatch(indexPatterns, indexName)) {
// Never delete any "current" index (e.g., today's index or the most recent version no timestamp, like alerts)
if (currents.contains(indexName)) {
continue;
}
if (Regex.simpleMatch(indexPatterns, indexName)) {
// Never delete any "current" index (e.g., today's index or the most recent version no timestamp, like alerts)
if (currents.contains(indexName)) {
continue;
}

long creationDate = index.value.getCreationDate();
if (creationDate <= expirationTimeMillis) {
if (logger.isDebugEnabled()) {
logger.debug(
"detected expired index [name={}, created={}, expired={}]",
indexName,
Instant.ofEpochMilli(creationDate).atZone(ZoneOffset.UTC),
expiration
);
}
indices.add(indexName);
long creationDate = index.value.getCreationDate();
if (creationDate <= expirationTimeMillis) {
if (logger.isDebugEnabled()) {
logger.debug(
"detected expired index [name={}, created={}, expired={}]",
indexName,
Instant.ofEpochMilli(creationDate).atZone(ZoneOffset.UTC),
expiration
);
}
indices.add(indexName);
}
}
}

if (indices.isEmpty() == false) {
logger.info("cleaning up [{}] old indices", indices.size());
deleteIndices(indices);
} else {
logger.debug("no old indices found for clean up");
}
if (indices.isEmpty() == false) {
logger.info("cleaning up [{}] old indices", indices.size());
deleteIndices(indices);
} else {
logger.debug("no old indices found for clean up");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils;
import org.elasticsearch.xpack.monitoring.exporter.Exporter;
import org.elasticsearch.xpack.monitoring.exporter.Exporters;
import org.elasticsearch.xpack.monitoring.exporter.local.LocalExporter;
import org.elasticsearch.xpack.monitoring.test.MonitoringIntegTestCase;
import org.junit.Before;

Expand All @@ -23,7 +22,6 @@
import java.util.Locale;

import static org.elasticsearch.test.ESIntegTestCase.Scope.TEST;
import static org.hamcrest.Matchers.is;

@ClusterScope(scope = TEST, numDataNodes = 0, numClientNodes = 0)
public abstract class AbstractIndicesCleanerTestCase extends MonitoringIntegTestCase {
Expand All @@ -40,7 +38,6 @@ public void setup() {
cleanerService.setGlobalRetention(TimeValue.MAX_VALUE);
}

@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/78737")
public void testNothingToDelete() throws Exception {
CleanerService.Listener listener = getListener();
listener.onCleanUpIndices(days(0));
Expand Down Expand Up @@ -107,7 +104,6 @@ public void testIgnoreCurrentTimestampedIndex() throws Exception {
assertIndicesCount(1);
}

@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/78862")
public void testDeleteIndices() throws Exception {
CleanerService.Listener listener = getListener();

Expand Down Expand Up @@ -167,10 +163,6 @@ protected CleanerService.Listener getListener() throws Exception {
Exporters exporters = internalCluster().getInstance(Exporters.class, internalCluster().getMasterName());
for (Exporter exporter : exporters.getEnabledExporters()) {
if (exporter instanceof CleanerService.Listener) {
// Ensure that the exporter is initialized.
if (exporter instanceof LocalExporter) {
assertBusy(() -> assertThat(((LocalExporter) exporter).isExporterReady(), is(true)));
}
return (CleanerService.Listener) exporter;
}
}
Expand Down