Skip to content

Commit

Permalink
This change adds a warning header when a license is about to expire
Browse files Browse the repository at this point in the history
Resolves elastic#60562
  • Loading branch information
BigPandaToo committed Nov 11, 2020
1 parent b48ecda commit f85a624
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@ public License getLicense() {
return license == LicensesMetadata.LICENSE_TOMBSTONE ? null : license;
}

public Lifecycle.State getLifecycleState() {
return clusterService.lifecycleState();
}

private LicensesMetadata getLicensesMetadata() {
return this.clusterService.state().metadata().custom(LicensesMetadata.TYPE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.elasticsearch.Version;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.component.Lifecycle;
import org.elasticsearch.common.logging.HeaderWarning;
import org.elasticsearch.common.logging.LoggerMessageFormat;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.License.OperationMode;
Expand All @@ -30,6 +32,11 @@
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static org.elasticsearch.cluster.service.ClusterApplierService.CLUSTER_UPDATE_THREAD_NAME;
import static org.elasticsearch.index.mapper.RangeFieldMapper.Defaults.DATE_FORMATTER;
import static org.elasticsearch.license.LicenseService.GRACE_PERIOD_DURATION;
import static org.elasticsearch.xpack.core.XPackPlugin.getSharedLicenseService;

/**
* A holder for the current state of the license for all xpack features.
*/
Expand Down Expand Up @@ -506,11 +513,23 @@ public boolean isActive() {
* Checks whether the given feature is allowed, tracking the last usage time.
*/
public boolean checkFeature(Feature feature) {
License license;
boolean allowed = isAllowed(feature);
LongAccumulator maxEpochAccumulator = lastUsed.get(feature);
long now = System.currentTimeMillis();
if (maxEpochAccumulator != null) {
maxEpochAccumulator.accumulate(epochMillisProvider.getAsLong());
}

if(getSharedLicenseService() != null && getSharedLicenseService().getLifecycleState() == Lifecycle.State.STARTED
&& (Thread.currentThread().getName().contains(CLUSTER_UPDATE_THREAD_NAME) == false)
&& (license = getSharedLicenseService().getLicense()) != null
&& feature.minimumOperationMode.compareTo(OperationMode.BASIC) > 0
&& now > license.expiryDate() - GRACE_PERIOD_DURATION.getMillis()) {
HeaderWarning.addWarning("The license [{}] is going to expire on [{}]", license.uid(),
DATE_FORMATTER.formatMillis(license.expiryDate()));
}

return allowed;
}

Expand Down

0 comments on commit f85a624

Please sign in to comment.