Skip to content

Commit

Permalink
Updated logic evaluating enable/disable index purge
Browse files Browse the repository at this point in the history
  • Loading branch information
georgweiss committed Jan 31, 2024
1 parent 996f455 commit 3e3be18
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 25 deletions.
6 changes: 3 additions & 3 deletions services/alarm-logger/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ Automatic purge of Elasticsearch indices
****************************************

To avoid issues related to a high number of Elasticsearch indices, automatic purge can be enabled in order to delete
indices considered obsolete. This is done by setting the preference ``retention_period_days`` to a number larger
or equal to 100. The default value is 0, i.e. automatic purge is disabled by default.
indices considered obsolete. This is done by setting the preferences ``date_span_units`` and ``retain_indices_count`` such
that they evaluate to a number larger or equal to 100. The default ``retain_indices_count`` is 0, i.e. automatic purge is disabled by default.

The automatic purge is run using a cron expression defined in preference ``purge_cron_expr``, default is
``0 0 0 * * SUN``, i.e. midnight each Sunday. See the SpringDocumentation_ on how to define the cron expression.

An Elasticsearch index is considered eligible for deletion if the last inserted message date is before current time
minus ``retention_period_days``.
minus the number of days computed from ``date_span_units`` and ``retain_indices_count``.

.. _SpringDocumentation: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/support/CronExpression.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import co.elastic.clients.elasticsearch.indices.DeleteIndexResponse;
import org.phoebus.alarm.logging.ElasticClientHelper;
import org.phoebus.alarm.logging.rest.AlarmLogMessage;
import org.phoebus.alarm.logging.rest.AlarmLogSearchUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.scheduling.annotation.Scheduled;
Expand All @@ -46,16 +47,16 @@
import java.util.logging.Logger;

/**
* Utility class purging Elasticsearch from indices considered obsolete based on the retention_period_time
* application property. Any value below 100 (days) suppresses instantiation of this {@link Component}.
* Utility class purging Elasticsearch from indices considered obsolete based on the date_span_units and retain_indices_count
* application properties. If these result in a value below 100 (days), this {@link Component} will not be instantiated.
* To determine last updated date of an index, each Elasticsearch index considered related to alarms is queried for last
* inserted document. The message_time field of that document is compared to the retention period to determine
* if the index should be deleted.
* A cron expression application property is used to define when to run the purging process.
*/
@Component
// Enable only of retention period is >= 100 days
@ConditionalOnExpression("#{T(java.lang.Integer).parseInt('${retention_period_days}') >= 100}")
@ConditionalOnExpression("#{T(org.phoebus.alarm.logging.purge.ElasticIndexPurger.EnableCondition).getRetentionDays('${date_span_units}', '${retain_indices_count}') >= 100}")
public class ElasticIndexPurger {

private static final Logger logger = Logger.getLogger(ElasticIndexPurger.class.getName());
Expand Down Expand Up @@ -113,4 +114,31 @@ public void purgeElasticIndices() {
logger.log(Level.WARNING, "Elastic query failed", e);
}
}

/**
* Helper class used to determine whether this service should be enabled or not
*/
public static class EnableCondition {

/**
*
* @param dateSpanUnits Any of the values Y, M, W, D
* @param retainIndicesCountString String value of the retain_indices_count preference
* @return A number computed from input. In case input arguments are invalid (e.g. non-numerical value
* for retain_indices_coun), then 0 is returned to indicate that this {@link Component} should not be enabled.
*/
@SuppressWarnings("unused")
public static int getRetentionDays(String dateSpanUnits, String retainIndicesCountString) {
int days = AlarmLogSearchUtil.getDateSpanInDays(dateSpanUnits);
if (days == -1) {
return 0;
}
try {
int retainIndicesCount = Integer.parseInt(retainIndicesCountString);
return days * retainIndicesCount;
} catch (NumberFormatException e) {
return 0;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -360,21 +360,7 @@ public static List<String> findIndexNames(String baseIndexName, Instant fromInst
if (fromIndex.equalsIgnoreCase(toIndex)) {
indexList.add(fromIndex);
} else {
int indexDateSpanDayValue = -1;
switch (indexDateSpanUnits) {
case "Y":
indexDateSpanDayValue = 365;
break;
case "M":
indexDateSpanDayValue = 30;
break;
case "W":
indexDateSpanDayValue = 7;
break;
case "D":
indexDateSpanDayValue = 1;
break;
}
int indexDateSpanDayValue = getDateSpanInDays(indexDateSpanUnits);
indexList.add(fromIndex);
while (!fromIndex.equalsIgnoreCase(toIndex)) {
fromInstant = fromInstant.plus(indexDateSpanDayValue, ChronoUnit.DAYS);
Expand All @@ -386,4 +372,25 @@ public static List<String> findIndexNames(String baseIndexName, Instant fromInst

return indexList;
}

/**
*
* @param indexDateSpanUnits A single char string from [Y, M, W, D]
* @return Number of days corresponding to the unit, or -1 if the input does not match
* supported chars.
*/
public static int getDateSpanInDays(String indexDateSpanUnits){
switch (indexDateSpanUnits) {
case "Y":
return 365;
case "M":
return 30;
case "W":
return 7;
case "D":
return 1;
default:
return -1;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ thread_pool_size=4
logging.level.org.springframework.web.filter.CommonsRequestLoggingFilter=INFO

############################## Index purge settings #######################
# Minimum age (in days) of Elasticsearch indices eligible for automatic index purge.
# Any value below 100 will disable automatic purge.
# Non-numeric value will fail service startup.
retention_period_days=0
# How many indices to retain (e.g. if you have selected date_span_units as M and set the retain_indices_count to 6, then indices
# older than 6 months will be deleted.
# Number of days computed form this setting and date_span_units must be greater or equal to 100
# for automatic purge to be enabled.
retain_indices_count=0

# Cron expression used by Spring scheduler running automatic purge, default every Sunday at midnight.
# See https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/support/CronExpression.html
Expand Down

0 comments on commit 3e3be18

Please sign in to comment.