8
8
import org .elasticsearch .Version ;
9
9
import org .elasticsearch .common .Nullable ;
10
10
import org .elasticsearch .common .Strings ;
11
- import org .elasticsearch .common .component .Lifecycle ;
12
11
import org .elasticsearch .common .logging .HeaderWarning ;
13
12
import org .elasticsearch .common .logging .LoggerMessageFormat ;
14
13
import org .elasticsearch .common .settings .Settings ;
25
24
import java .util .Objects ;
26
25
import java .util .Set ;
27
26
import java .util .concurrent .CopyOnWriteArrayList ;
27
+ import java .util .concurrent .TimeUnit ;
28
28
import java .util .concurrent .atomic .LongAccumulator ;
29
29
import java .util .function .BiFunction ;
30
30
import java .util .function .Function ;
31
31
import java .util .function .LongSupplier ;
32
32
import java .util .function .Predicate ;
33
33
import java .util .stream .Collectors ;
34
34
35
- import static org .elasticsearch .cluster .service .ClusterApplierService .CLUSTER_UPDATE_THREAD_NAME ;
36
- import static org .elasticsearch .index .mapper .RangeFieldMapper .Defaults .DATE_FORMATTER ;
37
35
import static org .elasticsearch .license .LicenseService .GRACE_PERIOD_DURATION ;
38
- import static org .elasticsearch .xpack .core .XPackPlugin .getSharedLicenseService ;
39
36
40
37
/**
41
38
* A holder for the current state of the license for all xpack features.
@@ -407,9 +404,13 @@ private static class Status {
407
404
/** True if the license is active, or false if it is expired. */
408
405
final boolean active ;
409
406
410
- Status (OperationMode mode , boolean active ) {
407
+ /** The current expiration date of the license; Long.MAX_VALUE if not avILble yet. */
408
+ final long licenseExpiryDate ;
409
+
410
+ Status (OperationMode mode , boolean active , long licenseExpiryDate ) {
411
411
this .mode = mode ;
412
412
this .active = active ;
413
+ this .licenseExpiryDate = licenseExpiryDate ;
413
414
}
414
415
}
415
416
@@ -423,7 +424,7 @@ private static class Status {
423
424
// XPackLicenseState. However, if status is read multiple times in a method, it can change in between
424
425
// reads. Methods should use `executeAgainstStatus` and `checkAgainstStatus` to ensure that the status
425
426
// is only read once.
426
- private volatile Status status = new Status (OperationMode .TRIAL , true );
427
+ private volatile Status status = new Status (OperationMode .TRIAL , true , Long . MAX_VALUE );
427
428
428
429
public XPackLicenseState (Settings settings , LongSupplier epochMillisProvider ) {
429
430
this .listeners = new CopyOnWriteArrayList <>();
@@ -471,12 +472,13 @@ private boolean checkAgainstStatus(Predicate<Status> statusPredicate) {
471
472
*
472
473
* @param mode The mode (type) of the current license.
473
474
* @param active True if the current license exists and is within its allowed usage period; false if it is expired or missing.
475
+ * @param expirationDate Expiration date of the current license.
474
476
* @param mostRecentTrialVersion If this cluster has, at some point commenced a trial, the most recent version on which they did that.
475
477
* May be {@code null} if they have never generated a trial license on this cluster, or the most recent
476
478
* trial was prior to this metadata being tracked (6.1)
477
479
*/
478
- void update (OperationMode mode , boolean active , @ Nullable Version mostRecentTrialVersion ) {
479
- status = new Status (mode , active );
480
+ void update (OperationMode mode , boolean active , long expirationDate , @ Nullable Version mostRecentTrialVersion ) {
481
+ status = new Status (mode , active , expirationDate );
480
482
listeners .forEach (LicenseStateListener ::licenseStateChanged );
481
483
}
482
484
@@ -513,21 +515,18 @@ public boolean isActive() {
513
515
* Checks whether the given feature is allowed, tracking the last usage time.
514
516
*/
515
517
public boolean checkFeature (Feature feature ) {
516
- License license ;
517
518
boolean allowed = isAllowed (feature );
518
519
LongAccumulator maxEpochAccumulator = lastUsed .get (feature );
519
520
long now = System .currentTimeMillis ();
520
521
if (maxEpochAccumulator != null ) {
521
522
maxEpochAccumulator .accumulate (epochMillisProvider .getAsLong ());
522
523
}
523
524
524
- if (getSharedLicenseService () != null && getSharedLicenseService ().getLifecycleState () == Lifecycle .State .STARTED
525
- && (Thread .currentThread ().getName ().contains (CLUSTER_UPDATE_THREAD_NAME ) == false )
526
- && (license = getSharedLicenseService ().getLicense ()) != null
527
- && feature .minimumOperationMode .compareTo (OperationMode .BASIC ) > 0
528
- && now > license .expiryDate () - GRACE_PERIOD_DURATION .getMillis ()) {
529
- HeaderWarning .addWarning ("The license [{}] is going to expire on [{}]" , license .uid (),
530
- DATE_FORMATTER .formatMillis (license .expiryDate ()));
525
+ if (feature .minimumOperationMode .compareTo (OperationMode .BASIC ) > 0
526
+ && now > status .licenseExpiryDate - GRACE_PERIOD_DURATION .getMillis ()) {
527
+ HeaderWarning .addWarning ("Your license will expire in [{}] days. " +
528
+ "Contact your administrator or update your license for continued use of features" ,
529
+ TimeUnit .MILLISECONDS .toDays (status .licenseExpiryDate - now ));
531
530
}
532
531
533
532
return allowed ;
0 commit comments