diff --git a/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/core/ExpirationCallback.java b/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/core/ExpirationCallback.java new file mode 100644 index 00000000000..05fe42869d9 --- /dev/null +++ b/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/core/ExpirationCallback.java @@ -0,0 +1,113 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.license.plugin.core; + +import org.elasticsearch.common.logging.LoggerMessageFormat; +import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.license.core.License; + +public abstract class ExpirationCallback { + + public enum Orientation {PRE, POST} + + public static abstract class Pre extends ExpirationCallback { + + /** + * Callback schedule prior to license expiry + * + * @param min latest relative time to execute before license expiry + * @param max earliest relative time to execute before license expiry + * @param frequency interval between execution + */ + public Pre(TimeValue min, TimeValue max, TimeValue frequency) { + super(Orientation.PRE, min, max, frequency); + } + + @Override + public boolean matches(long expirationDate, long now) { + long expiryDuration = expirationDate - now; + if (expiryDuration > 0L) { + if (expiryDuration <= max.getMillis()) { + return expiryDuration >= min.getMillis(); + } + } + return false; + } + + @Override + public TimeValue delay(long expiryDuration) { + return TimeValue.timeValueMillis(expiryDuration - max.getMillis()); + } + } + + public static abstract class Post extends ExpirationCallback { + + /** + * Callback schedule after license expiry + * + * @param min earliest relative time to execute after license expiry + * @param max latest relative time to execute after license expiry + * @param frequency interval between execution + */ + public Post(TimeValue min, TimeValue max, TimeValue frequency) { + super(Orientation.POST, min, max, frequency); + } + + @Override + public boolean matches(long expirationDate, long now) { + long postExpiryDuration = now - expirationDate; + if (postExpiryDuration > 0L) { + if (postExpiryDuration <= max.getMillis()) { + return postExpiryDuration >= min.getMillis(); + } + } + return false; + } + + @Override + public TimeValue delay(long expiryDuration) { + final long delay; + if (expiryDuration >= 0L) { + delay = expiryDuration + min.getMillis(); + } else { + delay = (-1L * expiryDuration) - min.getMillis(); + } + if (delay > 0L) { + return TimeValue.timeValueMillis(delay); + } else { + return null; + } + } + } + + protected final Orientation orientation; + protected final TimeValue min; + protected final TimeValue max; + private final TimeValue frequency; + + private ExpirationCallback(Orientation orientation, TimeValue min, TimeValue max, TimeValue frequency) { + this.orientation = orientation; + this.min = (min == null) ? TimeValue.timeValueMillis(0) : min; + this.max = (max == null) ? TimeValue.timeValueMillis(Long.MAX_VALUE) : max; + this.frequency = frequency; + } + + public TimeValue frequency() { + return frequency; + } + + public abstract TimeValue delay(long expiryDuration); + + public abstract boolean matches(long expirationDate, long now); + + public abstract void on(License license); + + @Override + public String toString() { + return LoggerMessageFormat.format(null, "ExpirationCallback:(orientation [{}], min [{}], max [{}], freq [{}])", + orientation.name(), min, max, frequency); + } +} diff --git a/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/core/LicensesService.java b/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/core/LicensesService.java index ae15fe9eb29..53349ba8ddc 100644 --- a/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/core/LicensesService.java +++ b/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/core/LicensesService.java @@ -617,109 +617,6 @@ public class LicensesService extends AbstractLifecycleComponent } } - public static abstract class ExpirationCallback { - - public enum Orientation {PRE, POST} - - public static abstract class Pre extends ExpirationCallback { - - /** - * Callback schedule prior to license expiry - * - * @param min latest relative time to execute before license expiry - * @param max earliest relative time to execute before license expiry - * @param frequency interval between execution - */ - public Pre(TimeValue min, TimeValue max, TimeValue frequency) { - super(Orientation.PRE, min, max, frequency); - } - - @Override - public boolean matches(long expirationDate, long now) { - long expiryDuration = expirationDate - now; - if (expiryDuration > 0L) { - if (expiryDuration <= max.getMillis()) { - return expiryDuration >= min.getMillis(); - } - } - return false; - } - - @Override - public TimeValue delay(long expiryDuration) { - return TimeValue.timeValueMillis(expiryDuration - max.getMillis()); - } - } - - public static abstract class Post extends ExpirationCallback { - - /** - * Callback schedule after license expiry - * - * @param min earliest relative time to execute after license expiry - * @param max latest relative time to execute after license expiry - * @param frequency interval between execution - */ - public Post(TimeValue min, TimeValue max, TimeValue frequency) { - super(Orientation.POST, min, max, frequency); - } - - @Override - public boolean matches(long expirationDate, long now) { - long postExpiryDuration = now - expirationDate; - if (postExpiryDuration > 0L) { - if (postExpiryDuration <= max.getMillis()) { - return postExpiryDuration >= min.getMillis(); - } - } - return false; - } - - @Override - public TimeValue delay(long expiryDuration) { - final long delay; - if (expiryDuration >= 0L) { - delay = expiryDuration + min.getMillis(); - } else { - delay = (-1L * expiryDuration) - min.getMillis(); - } - if (delay > 0L) { - return TimeValue.timeValueMillis(delay); - } else { - return null; - } - } - } - - protected final Orientation orientation; - protected final TimeValue min; - protected final TimeValue max; - private final TimeValue frequency; - - private ExpirationCallback(Orientation orientation, TimeValue min, TimeValue max, TimeValue frequency) { - this.orientation = orientation; - this.min = (min == null) ? TimeValue.timeValueMillis(0) : min; - this.max = (max == null) ? TimeValue.timeValueMillis(Long.MAX_VALUE) : max; - this.frequency = frequency; - } - - public TimeValue frequency() { - return frequency; - } - - public abstract TimeValue delay(long expiryDuration); - - public abstract boolean matches(long expirationDate, long now); - - public abstract void on(License license); - - @Override - public String toString() { - return LoggerMessageFormat.format(null, "ExpirationCallback:(orientation [{}], min [{}], max [{}], freq [{}])", - orientation.name(), min, max, frequency); - } - } - @Override public void register(Licensee licensee) { for (final InternalLicensee existingLicensee : registeredLicensees) { diff --git a/elasticsearch/x-pack/license-plugin/src/test/java/org/elasticsearch/license/plugin/core/LicensesExpirationCallbackTests.java b/elasticsearch/x-pack/license-plugin/src/test/java/org/elasticsearch/license/plugin/core/LicensesExpirationCallbackTests.java index 13f9a132ed7..d5af4789ead 100644 --- a/elasticsearch/x-pack/license-plugin/src/test/java/org/elasticsearch/license/plugin/core/LicensesExpirationCallbackTests.java +++ b/elasticsearch/x-pack/license-plugin/src/test/java/org/elasticsearch/license/plugin/core/LicensesExpirationCallbackTests.java @@ -34,7 +34,7 @@ public class LicensesExpirationCallbackTests extends ESSingleNodeTestCase { TimeValue min = TimeValue.timeValueSeconds(postExpirySeconds - randomIntBetween(1, 3)); TimeValue max = TimeValue.timeValueSeconds(postExpirySeconds + randomIntBetween(1, 10)); - final LicensesService.ExpirationCallback.Post post = new LicensesService.ExpirationCallback.Post(min, max, timeValueMillis(10)) { + final ExpirationCallback.Post post = new ExpirationCallback.Post(min, max, timeValueMillis(10)) { @Override public void on(License license) { } @@ -49,7 +49,7 @@ public class LicensesExpirationCallbackTests extends ESSingleNodeTestCase { TimeValue postExpiryDuration = TimeValue.timeValueSeconds(postExpirySeconds); TimeValue min = TimeValue.timeValueSeconds(postExpirySeconds - randomIntBetween(1, 3)); - final LicensesService.ExpirationCallback.Post post = new LicensesService.ExpirationCallback.Post(min, null, timeValueMillis(10)) { + final ExpirationCallback.Post post = new ExpirationCallback.Post(min, null, timeValueMillis(10)) { @Override public void on(License license) { } @@ -63,7 +63,7 @@ public class LicensesExpirationCallbackTests extends ESSingleNodeTestCase { TimeValue expiryDuration = TimeValue.timeValueSeconds(expirySeconds); TimeValue max = TimeValue.timeValueSeconds(expirySeconds + randomIntBetween(1, 10)); - final LicensesService.ExpirationCallback.Pre pre = new LicensesService.ExpirationCallback.Pre(null, max, timeValueMillis(10)) { + final ExpirationCallback.Pre pre = new ExpirationCallback.Pre(null, max, timeValueMillis(10)) { @Override public void on(License license) { } @@ -78,7 +78,7 @@ public class LicensesExpirationCallbackTests extends ESSingleNodeTestCase { TimeValue min = TimeValue.timeValueSeconds(expirySeconds - randomIntBetween(0, 3)); TimeValue max = TimeValue.timeValueSeconds(expirySeconds + randomIntBetween(1, 10)); - final LicensesService.ExpirationCallback.Pre pre = new LicensesService.ExpirationCallback.Pre(min, max, timeValueMillis(10)) { + final ExpirationCallback.Pre pre = new ExpirationCallback.Pre(min, max, timeValueMillis(10)) { @Override public void on(License license) { } @@ -145,9 +145,9 @@ public class LicensesExpirationCallbackTests extends ESSingleNodeTestCase { licensesService.stop(); } - private static LicensesService.ExpirationCallback preCallbackLatch(TimeValue min, TimeValue max, TimeValue frequency, - final AtomicInteger count) { - return new LicensesService.ExpirationCallback.Pre(min, max, frequency) { + private static ExpirationCallback preCallbackLatch(TimeValue min, TimeValue max, TimeValue frequency, + final AtomicInteger count) { + return new ExpirationCallback.Pre(min, max, frequency) { @Override public void on(License license) { count.incrementAndGet(); @@ -155,9 +155,9 @@ public class LicensesExpirationCallbackTests extends ESSingleNodeTestCase { }; } - private static LicensesService.ExpirationCallback postCallbackLatch(TimeValue min, TimeValue max, TimeValue frequency, - final AtomicInteger count) { - return new LicensesService.ExpirationCallback.Post(min, max, frequency) { + private static ExpirationCallback postCallbackLatch(TimeValue min, TimeValue max, TimeValue frequency, + final AtomicInteger count) { + return new ExpirationCallback.Post(min, max, frequency) { @Override public void on(License license) { count.incrementAndGet();