Skip to content

Commit fb033b9

Browse files
authored
Move SchedulerEngine and TimeValueSchedule to server/common/scheduler (#93862)
We built quite a bit of infrastructure to have one polling job running via the `SchedulerEngine` and `ActiveSchedule`. This moves this infrastructure outside x-pack to server so elasticsearch/modules can use it and avoid re-implementing it using `threadPool.schedule`.
1 parent ee3f3e1 commit fb033b9

File tree

26 files changed

+57
-36
lines changed

26 files changed

+57
-36
lines changed
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/*
22
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
33
* or more contributor license agreements. Licensed under the Elastic License
4-
* 2.0; you may not use this file except in compliance with the Elastic License
5-
* 2.0.
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
67
*/
78

8-
package org.elasticsearch.xpack.core.scheduler;
9+
package org.elasticsearch.common.scheduler;
910

1011
import org.apache.logging.log4j.Logger;
1112
import org.apache.logging.log4j.util.Supplier;

server/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@
206206
exports org.elasticsearch.common.path;
207207
exports org.elasticsearch.common.recycler;
208208
exports org.elasticsearch.common.regex;
209+
exports org.elasticsearch.common.scheduler;
209210
exports org.elasticsearch.common.settings;
210211
exports org.elasticsearch.common.text;
211212
exports org.elasticsearch.common.time;
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/*
22
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
33
* or more contributor license agreements. Licensed under the Elastic License
4-
* 2.0; you may not use this file except in compliance with the Elastic License
5-
* 2.0.
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
67
*/
78

8-
package org.elasticsearch.xpack.core.scheduler;
9+
package org.elasticsearch.common.scheduler;
910

1011
import org.apache.logging.log4j.LogManager;
1112
import org.apache.logging.log4j.Logger;
@@ -30,6 +31,12 @@
3031
import java.util.concurrent.ScheduledFuture;
3132
import java.util.concurrent.TimeUnit;
3233

34+
/**
35+
* Thread-safe scheduling implementation that'll cancel an already scheduled job
36+
* before rescheduling.
37+
* The scheduler engine offers support for registering listeners that'll get notified
38+
* when a job triggers (according to the job's configured schedule)
39+
*/
3340
public class SchedulerEngine {
3441

3542
public static class Job {

x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/TimeValueSchedule.java renamed to server/src/main/java/org/elasticsearch/common/scheduler/TimeValueSchedule.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
/*
22
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
33
* or more contributor license agreements. Licensed under the Elastic License
4-
* 2.0; you may not use this file except in compliance with the Elastic License
5-
* 2.0.
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
67
*/
78

8-
package org.elasticsearch.xpack.ilm;
9+
package org.elasticsearch.common.scheduler;
910

11+
import org.elasticsearch.common.scheduler.SchedulerEngine.Schedule;
1012
import org.elasticsearch.core.TimeValue;
11-
import org.elasticsearch.xpack.core.scheduler.SchedulerEngine.Schedule;
1213

1314
import java.util.Objects;
1415

16+
/**
17+
* {@link Schedule} implementation wrapping a {@link TimeValue} interval that'll compute the
18+
* next scheduled execution time according to the configured interval.
19+
*/
1520
public class TimeValueSchedule implements Schedule {
1621

1722
private TimeValue interval;
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
/*
22
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
33
* or more contributor license agreements. Licensed under the Elastic License
4-
* 2.0; you may not use this file except in compliance with the Elastic License
5-
* 2.0.
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
67
*/
78

8-
package org.elasticsearch.xpack.core.scheduler;
9+
package org.elasticsearch.common.scheduler;
910

1011
import org.apache.logging.log4j.Logger;
1112
import org.apache.logging.log4j.util.Supplier;
13+
import org.elasticsearch.common.scheduler.SchedulerEngine.ActiveSchedule;
14+
import org.elasticsearch.common.scheduler.SchedulerEngine.Job;
1215
import org.elasticsearch.common.settings.Settings;
1316
import org.elasticsearch.core.Tuple;
1417
import org.elasticsearch.test.ESTestCase;
15-
import org.elasticsearch.xpack.core.scheduler.SchedulerEngine.ActiveSchedule;
16-
import org.elasticsearch.xpack.core.scheduler.SchedulerEngine.Job;
1718
import org.mockito.ArgumentCaptor;
1819

1920
import java.time.Clock;

x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/TimeValueScheduleTests.java renamed to server/src/test/java/org/elasticsearch/common/scheduler/TimeValueScheduleTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/*
22
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
33
* or more contributor license agreements. Licensed under the Elastic License
4-
* 2.0; you may not use this file except in compliance with the Elastic License
5-
* 2.0.
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
67
*/
78

8-
package org.elasticsearch.xpack.ilm;
9+
package org.elasticsearch.common.scheduler;
910

1011
import org.elasticsearch.core.TimeValue;
1112
import org.elasticsearch.test.ESTestCase;

x-pack/plugin/core/src/main/java/org/elasticsearch/license/ExpirationCallback.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package org.elasticsearch.license;
88

99
import org.elasticsearch.common.logging.LoggerMessageFormat;
10+
import org.elasticsearch.common.scheduler.SchedulerEngine;
1011
import org.elasticsearch.core.TimeValue;
1112

1213
import java.util.UUID;
@@ -138,7 +139,7 @@ final TimeValue delay(long expirationDate, long now) {
138139
}
139140

140141
/**
141-
* {@link org.elasticsearch.xpack.core.scheduler.SchedulerEngine.Schedule#nextScheduledTimeAfter(long, long)}
142+
* {@link SchedulerEngine.Schedule#nextScheduledTimeAfter(long, long)}
142143
* with respect to license expiry date
143144
*/
144145
public final long nextScheduledTimeForExpiry(long expiryDate, long startTime, long time) {

x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicenseService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.elasticsearch.common.component.Lifecycle;
2525
import org.elasticsearch.common.hash.MessageDigests;
2626
import org.elasticsearch.common.logging.LoggerMessageFormat;
27+
import org.elasticsearch.common.scheduler.SchedulerEngine;
2728
import org.elasticsearch.common.settings.Setting;
2829
import org.elasticsearch.common.settings.Settings;
2930
import org.elasticsearch.common.time.DateFormatter;
@@ -40,7 +41,6 @@
4041
import org.elasticsearch.watcher.ResourceWatcherService;
4142
import org.elasticsearch.xpack.core.XPackPlugin;
4243
import org.elasticsearch.xpack.core.XPackSettings;
43-
import org.elasticsearch.xpack.core.scheduler.SchedulerEngine;
4444

4545
import java.nio.charset.StandardCharsets;
4646
import java.time.Clock;

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/scheduler/CronSchedule.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*/
77
package org.elasticsearch.xpack.core.scheduler;
88

9+
import org.elasticsearch.common.scheduler.SchedulerEngine;
10+
911
public class CronSchedule implements SchedulerEngine.Schedule {
1012
private final Cron cron;
1113

x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseScheduleTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
*/
77
package org.elasticsearch.license;
88

9+
import org.elasticsearch.common.scheduler.SchedulerEngine;
910
import org.elasticsearch.core.TimeValue;
1011
import org.elasticsearch.test.ESTestCase;
11-
import org.elasticsearch.xpack.core.scheduler.SchedulerEngine;
1212
import org.junit.Before;
1313

1414
import java.time.Instant;

0 commit comments

Comments
 (0)