Apache Lucene open-source search software
SOLR-11066: Added examples of startTime in ref guide. Added null check for processor and code comments to explain skipped events and lastRunAt initialization. Moved default preferredOp value to ComputePlanAction. Extracted a constant.
Shalin Shekhar Mangar9cec2221a66e
diff --git a/solr/core/src/java/org/apache/solr/cloud/autoscaling/ComputePlanAction.java b/solr/core/src/java/org/apache/solr/cloud/autoscaling/ComputePlanAction.javaindex 9eb6382a6bb..aed821a7a78 100644--- a/solr/core/src/java/org/apache/solr/cloud/autoscaling/ComputePlanAction.java+++ b/solr/core/src/java/org/apache/solr/cloud/autoscaling/ComputePlanAction.java@@ -209,7 +209,7 @@ public class ComputePlanAction extends TriggerActionBase {}break;case SCHEDULED:- String preferredOp = (String) event.getProperty(AutoScalingParams.PREFERRED_OP);+ String preferredOp = (String) event.getProperty(AutoScalingParams.PREFERRED_OP, CollectionParams.CollectionAction.MOVEREPLICA.toLower());CollectionParams.CollectionAction action = CollectionParams.CollectionAction.get(preferredOp);suggester = session.getSuggester(action);break;diff --git a/solr/core/src/java/org/apache/solr/cloud/autoscaling/ScheduledTrigger.java b/solr/core/src/java/org/apache/solr/cloud/autoscaling/ScheduledTrigger.javaindex 9c8ee6dea0b..1aa7a0a44b2 100644--- a/solr/core/src/java/org/apache/solr/cloud/autoscaling/ScheduledTrigger.java+++ b/solr/core/src/java/org/apache/solr/cloud/autoscaling/ScheduledTrigger.java@@ -33,7 +33,6 @@ import java.util.TimeZone;import org.apache.solr.client.solrj.cloud.autoscaling.SolrCloudManager;import org.apache.solr.client.solrj.cloud.autoscaling.TriggerEventType;import org.apache.solr.common.SolrException;-import org.apache.solr.common.params.CollectionParams;import org.apache.solr.core.SolrResourceLoader;import org.apache.solr.util.DateMathParser;import org.apache.solr.util.TimeZoneUtils;@@ -49,6 +48,7 @@ public class ScheduledTrigger extends TriggerBase {private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());private static final String DEFAULT_GRACE_DURATION = "+15MINUTES";+ private static final String LAST_RUN_AT = "lastRunAt";static final String ACTUAL_EVENT_TIME = "actualEventTime";private final String everyStr;@@ -72,13 +72,18 @@ public class ScheduledTrigger extends TriggerBase {this.everyStr = (String) properties.get("every");this.graceDurationStr = (String) properties.getOrDefault("graceDuration", DEFAULT_GRACE_DURATION);- preferredOp = (String) properties.getOrDefault(PREFERRED_OP, CollectionParams.CollectionAction.MOVEREPLICA.toLower());+ preferredOp = (String) properties.get(PREFERRED_OP);// attempt parsing to validate date math stringsInstant startTime = parseStartTime(startTimeStr, timeZoneStr);DateMathParser.parseMath(null, startTime + everyStr, timeZone);DateMathParser.parseMath(null, startTime + graceDurationStr, timeZone);+ // We set lastRunAt to be the startTime (which could be a date math expression such as 'NOW')+ // Ordinarily, NOW will always be evaluated in this constructor so it may seem that+ // the trigger will always fire the first time.+ // However, the lastRunAt is overwritten with the value from ZK+ // during restoreState() operation (which is performed before run()) so the trigger works correctlythis.lastRunAt = startTime;}@@ -107,13 +112,13 @@ public class ScheduledTrigger extends TriggerBase {@Overrideprotected Map<String, Object> getState() {- return Collections.singletonMap("lastRunAt", lastRunAt.toEpochMilli());+ return Collections.singletonMap(LAST_RUN_AT, lastRunAt.toEpochMilli());}@Overrideprotected void setState(Map<String, Object> state) {- if (state.containsKey("lastRunAt")) {- this.lastRunAt = Instant.ofEpochMilli((Long) state.get("lastRunAt"));+ if (state.containsKey(LAST_RUN_AT)) {+ this.lastRunAt = Instant.ofEpochMilli((Long) state.get(LAST_RUN_AT));}}@@ -163,8 +168,10 @@ public class ScheduledTrigger extends TriggerBase {log.warn("ScheduledTrigger was not able to run event at scheduled time: {}. Now: {}",nextRunTime, now);}- if (processor.process(new ScheduledEvent(getEventType(), getName(), nextRunTime.toEpochMilli(),- preferredOp, now.toEpochMilli(), true))) {+ // Even though we are skipping the event, we need to notify any listeners of the IGNORED stage+ // so we create a dummy event with the ignored=true flag and ScheduledTriggers will do the rest+ if (processor != null && processor.process(new ScheduledEvent(getEventType(), getName(), nextRunTime.toEpochMilli(),+ preferredOp, now.toEpochMilli(), true))) {lastRunAt = nextRunTime;return;}@@ -191,7 +198,9 @@ public class ScheduledTrigger extends TriggerBase {public ScheduledEvent(TriggerEventType eventType, String source, long eventTime, String preferredOp, long actualEventTime, boolean ignored) {super(eventType, source, eventTime, null, ignored);- properties.put(PREFERRED_OP, preferredOp);+ if (preferredOp != null) {+ properties.put(PREFERRED_OP, preferredOp);+ }properties.put(ACTUAL_EVENT_TIME, actualEventTime);}}diff --git a/solr/solr-ref-guide/src/solrcloud-autoscaling-triggers.adoc b/solr/solr-ref-guide/src/solrcloud-autoscaling-triggers.adocindex ec09a79b95c..08e00852f30 100644--- a/solr/solr-ref-guide/src/solrcloud-autoscaling-triggers.adoc+++ b/solr/solr-ref-guide/src/solrcloud-autoscaling-triggers.adoc@@ -159,7 +159,10 @@ The Scheduled trigger generates events according to a fixed rate schedule.The trigger supports the following configuration:-* `startTime` - (string, required) the start date/time of the schedule. This should either be an ISO-8601 date time string (the same standard used during search and indexing in Solr, thus defaulting to UTC) or be specified with the `timeZone` parameter.+* `startTime` - (string, required) the start date/time of the schedule. This should either be a DateMath string e.g. 'NOW' or be an ISO-8601 date time string (the same standard used during search and indexing in Solr, thus defaulting to UTC) or be specified without the trailing 'Z' accompanied with the `timeZone` parameter. For example, each of the following values are acceptable:+ ** `2018-01-31T15:30:00Z` - ISO-8601 date time string. The trailing `Z` signals that the time is in UTC+ ** `NOW+5MINUTES` - Solr's date math string+ ** `2018-01-31T15:30:00` - No trailing 'Z' signals that the `timeZone` parameter must be specified to avoid ambiguity* `every` - (string, required) a positive Solr date math string which is added to the `startTime` or the last run time to arrive at the next scheduled time* `graceTime` - (string, optional) a positive Solr date math string. This is the additional grace time over the scheduled time within which the trigger is allowed to generate an event.* `timeZone` - (string, optional) a time zone string which is used for calculating the scheduled times