You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ab...@apache.org on 2020/09/04 10:42:41 UTC
[lucene-solr] branch jira/solr-14749 updated: SOLR-14749: Remove
the Scheduler, it's in a separate PR now.
This is an automated email from the ASF dual-hosted git repository.
ab pushed a commit to branch jira/solr-14749
in repository https://gitbox.apache.org/repos/asf/lucene-solr.git
The following commit(s) were added to refs/heads/jira/solr-14749 by this push:
new 154dd6f SOLR-14749: Remove the Scheduler, it's in a separate PR now.
154dd6f is described below
commit 154dd6f47f37ec02d1f20b5190e82c52a898c91c
Author: Andrzej Bialecki <ab...@apache.org>
AuthorDate: Fri Sep 4 12:41:30 2020 +0200
SOLR-14749: Remove the Scheduler, it's in a separate PR now.
---
.../apache/solr/cluster/scheduler/Schedulable.java | 27 -----
.../apache/solr/cluster/scheduler/Schedule.java | 34 ------
.../solr/cluster/scheduler/SolrScheduler.java | 27 -----
.../cluster/scheduler/impl/CompiledSchedule.java | 115 --------------------
.../cluster/scheduler/impl/SolrSchedulerImpl.java | 121 ---------------------
5 files changed, 324 deletions(-)
diff --git a/solr/core/src/java/org/apache/solr/cluster/scheduler/Schedulable.java b/solr/core/src/java/org/apache/solr/cluster/scheduler/Schedulable.java
deleted file mode 100644
index f907245..0000000
--- a/solr/core/src/java/org/apache/solr/cluster/scheduler/Schedulable.java
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.solr.cluster.scheduler;
-
-/**
- *
- */
-public interface Schedulable {
-
- Schedule getSchedule();
-
- void run();
-}
diff --git a/solr/core/src/java/org/apache/solr/cluster/scheduler/Schedule.java b/solr/core/src/java/org/apache/solr/cluster/scheduler/Schedule.java
deleted file mode 100644
index 31210bc..0000000
--- a/solr/core/src/java/org/apache/solr/cluster/scheduler/Schedule.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.solr.cluster.scheduler;
-
-/**
- * Simple schedule for generating periodically occurring events.
- */
-public interface Schedule {
-
- String getName();
-
- // date math expr
- String getStartTime();
-
- // may be null only if getStartTime contained TZ
- String getTimeZone();
-
- // date math expr
- String getInterval();
-}
diff --git a/solr/core/src/java/org/apache/solr/cluster/scheduler/SolrScheduler.java b/solr/core/src/java/org/apache/solr/cluster/scheduler/SolrScheduler.java
deleted file mode 100644
index 7ea76b4..0000000
--- a/solr/core/src/java/org/apache/solr/cluster/scheduler/SolrScheduler.java
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.solr.cluster.scheduler;
-
-/**
- *
- */
-public interface SolrScheduler {
-
- void registerSchedulable(Schedulable schedulable);
-
- void unregisterSchedulable(Schedulable schedulable);
-}
diff --git a/solr/core/src/java/org/apache/solr/cluster/scheduler/impl/CompiledSchedule.java b/solr/core/src/java/org/apache/solr/cluster/scheduler/impl/CompiledSchedule.java
deleted file mode 100644
index fefee45..0000000
--- a/solr/core/src/java/org/apache/solr/cluster/scheduler/impl/CompiledSchedule.java
+++ /dev/null
@@ -1,115 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.solr.cluster.scheduler.impl;
-
-import java.lang.invoke.MethodHandles;
-import java.text.ParseException;
-import java.time.Instant;
-import java.time.format.DateTimeFormatter;
-import java.time.format.DateTimeFormatterBuilder;
-import java.time.temporal.ChronoField;
-import java.util.Date;
-import java.util.Locale;
-import java.util.TimeZone;
-
-import org.apache.solr.cluster.scheduler.Schedule;
-import org.apache.solr.common.SolrException;
-import org.apache.solr.util.DateMathParser;
-import org.apache.solr.util.TimeZoneUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * A version of {@link Schedule} where some of the fields are already resolved.
- */
-class CompiledSchedule {
- private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
-
- final String name;
- final TimeZone timeZone;
- final Instant startTime;
- final String interval;
- final DateMathParser dateMathParser;
-
- Instant lastRunAt;
-
- /**
- * Compile a schedule.
- * @param schedule schedule.
- * @throws Exception if startTime or interval cannot be parsed.
- */
- CompiledSchedule(Schedule schedule) throws Exception {
- this.name = schedule.getName();
- this.timeZone = TimeZoneUtils.getTimeZone(schedule.getTimeZone());
- this.startTime = parseStartTime(new Date(), schedule.getStartTime(), timeZone);
- this.lastRunAt = startTime;
- this.interval = schedule.getInterval();
- this.dateMathParser = new DateMathParser(timeZone);
- // this is just to verify that the interval math is valid
- shouldRun();
- }
-
- private Instant parseStartTime(Date now, String startTimeStr, TimeZone timeZone) throws Exception {
- try {
- // try parsing startTime as an ISO-8601 date time string
- return DateMathParser.parseMath(now, startTimeStr).toInstant();
- } catch (SolrException e) {
- if (e.code() != SolrException.ErrorCode.BAD_REQUEST.code) {
- throw new Exception("startTime: error parsing value '" + startTimeStr + "': " + e.toString());
- }
- }
- DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder()
- .append(DateTimeFormatter.ISO_LOCAL_DATE).appendPattern("['T'[HH[:mm[:ss]]]]")
- .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
- .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
- .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
- .toFormatter(Locale.ROOT).withZone(timeZone.toZoneId());
- try {
- return Instant.from(dateTimeFormatter.parse(startTimeStr));
- } catch (Exception e) {
- throw new Exception("startTime: error parsing startTime '" + startTimeStr + "': " + e.toString());
- }
- }
-
- /**
- * Returns true if the last run + run interval is already in the past.
- */
- boolean shouldRun() {
- dateMathParser.setNow(new Date(lastRunAt.toEpochMilli()));
- Instant nextRunTime;
- try {
- Date next = dateMathParser.parseMath(interval);
- nextRunTime = next.toInstant();
- } catch (ParseException e) {
- log.warn("Invalid math expression, skipping: " + e);
- return false;
- }
- if (Instant.now().isAfter(nextRunTime)) {
- return true;
- } else {
- return false;
- }
- }
-
- /**
- * This setter MUST be invoked after each run.
- * @param lastRunAt time when the schedule was last run.
- */
- void setLastRunAt(Instant lastRunAt) {
- this.lastRunAt = lastRunAt;
- }
-}
diff --git a/solr/core/src/java/org/apache/solr/cluster/scheduler/impl/SolrSchedulerImpl.java b/solr/core/src/java/org/apache/solr/cluster/scheduler/impl/SolrSchedulerImpl.java
deleted file mode 100644
index 058d0fc..0000000
--- a/solr/core/src/java/org/apache/solr/cluster/scheduler/impl/SolrSchedulerImpl.java
+++ /dev/null
@@ -1,121 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.solr.cluster.scheduler.impl;
-
-import java.lang.invoke.MethodHandles;
-import java.time.Instant;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.TimeUnit;
-
-import org.apache.solr.cloud.ClusterSingleton;
-import org.apache.solr.cluster.scheduler.Schedule;
-import org.apache.solr.cluster.scheduler.Schedulable;
-import org.apache.solr.cluster.scheduler.SolrScheduler;
-import org.apache.solr.common.util.SolrNamedThreadFactory;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * <p>Scheduled executions are triggered at most with {@link #SCHEDULE_INTERVAL_SEC} interval.
- * Each registered {@link Schedulable} is processed sequentially and if its next execution time
- * is in the past its {@link Schedulable#run()} method will be invoked.
- * <p>NOTE: If the total time of execution of all registered Schedulable-s exceeds any schedule
- * interval then exact execution times will be silently missed.</p>
- */
-public class SolrSchedulerImpl implements SolrScheduler, ClusterSingleton {
- private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
-
- public static final int SCHEDULE_INTERVAL_SEC = 10;
-
- private final Set<Schedulable> schedulables = ConcurrentHashMap.newKeySet();
- private final Map<String, CompiledSchedule> compiledSchedules = new ConcurrentHashMap<>();
-
- private ScheduledExecutorService scheduler;
- private boolean running = false;
-
- @Override
- public void registerSchedulable(Schedulable schedulable) {
- try {
- CompiledSchedule compiledSchedule = new CompiledSchedule(schedulable.getSchedule());
- if (compiledSchedules.containsKey(compiledSchedule.name)) {
- throw new Exception("Schedule defined by " + schedulable +
- " already exists under name " + compiledSchedule.name);
- }
- compiledSchedules.put(compiledSchedule.name, compiledSchedule);
- schedulables.add(schedulable);
- } catch (Exception e) {
- log.warn("Invalid schedule for {}, skipping: {}", schedulable, e);
- }
- }
-
- @Override
- public void unregisterSchedulable(Schedulable schedulable) {
- compiledSchedules.remove(schedulable.getSchedule().getName());
- schedulables.remove(schedulable);
- }
-
- @Override
- public void start() {
- // create scheduler
- scheduler = Executors.newSingleThreadScheduledExecutor(new SolrNamedThreadFactory("cluster-event-scheduler"));
- scheduler.schedule(() -> maybeFireScheduledEvent(), SCHEDULE_INTERVAL_SEC, TimeUnit.SECONDS);
- running = true;
- }
-
- private void maybeFireScheduledEvent() {
- if (!running) {
- return;
- }
- if (compiledSchedules.isEmpty()) {
- return;
- }
- schedulables.forEach(schedulable -> {
- Schedule schedule = schedulable.getSchedule();
- CompiledSchedule compiledSchedule = compiledSchedules.get(schedule.getName());
- if (compiledSchedule == null) { // ???
- return;
- }
- if (compiledSchedule.shouldRun()) {
- Instant now = Instant.now();
- schedulable.run();
- compiledSchedule.setLastRunAt(now);
- }
- });
- }
-
- @Override
- public boolean isRunning() {
- return running;
- }
-
- @Override
- public void stop() {
- running = false;
- if (scheduler != null) {
- scheduler.shutdownNow();
- try {
- scheduler.awaitTermination(60, TimeUnit.SECONDS);
- } catch (InterruptedException e) {
- log.warn("Interrupted while waiting for the scheduler to shut down, ignoring");
- }
- }
- }
-}