licensesService cleanup

Original commit: elastic/x-pack-elasticsearch@a1c136d3bb
This commit is contained in:
Areek Zillur 2014-10-27 21:37:16 -04:00
parent 79f430ebaf
commit a82a0a4e6a
2 changed files with 27 additions and 27 deletions

View File

@ -58,7 +58,7 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
public static final String REGISTER_TRIAL_LICENSE_ACTION_NAME = "internal:cluster/licenses/register_trial_license"; public static final String REGISTER_TRIAL_LICENSE_ACTION_NAME = "internal:cluster/licenses/register_trial_license";
private final ESLicenseManager esLicenseManager; private final ESLicenseManager licenseManager;
private final ClusterService clusterService; private final ClusterService clusterService;
@ -72,16 +72,16 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
private final AtomicReference<ScheduledFuture> notificationScheduler; private final AtomicReference<ScheduledFuture> notificationScheduler;
private final AtomicReference<LicensesMetaData> lastObservedState; private final AtomicReference<LicensesMetaData> lastObservedLicensesState;
@Inject @Inject
public LicensesService(Settings settings, ClusterService clusterService, ThreadPool threadPool, TransportService transportService, ESLicenseManager esLicenseManager) { public LicensesService(Settings settings, ClusterService clusterService, ThreadPool threadPool, TransportService transportService, ESLicenseManager licenseManager) {
super(settings); super(settings);
this.clusterService = clusterService; this.clusterService = clusterService;
this.esLicenseManager = esLicenseManager; this.licenseManager = licenseManager;
this.threadPool = threadPool; this.threadPool = threadPool;
this.transportService = transportService; this.transportService = transportService;
this.lastObservedState = new AtomicReference<>(null); this.lastObservedLicensesState = new AtomicReference<>(null);
this.notificationScheduler = new AtomicReference<>(null); this.notificationScheduler = new AtomicReference<>(null);
transportService.registerHandler(REGISTER_TRIAL_LICENSE_ACTION_NAME, new RegisterTrialLicenseRequestHandler()); transportService.registerHandler(REGISTER_TRIAL_LICENSE_ACTION_NAME, new RegisterTrialLicenseRequestHandler());
} }
@ -118,7 +118,7 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
MetaData.Builder mdBuilder = MetaData.builder(currentState.metaData()); MetaData.Builder mdBuilder = MetaData.builder(currentState.metaData());
LicensesMetaData currentLicenses = metaData.custom(LicensesMetaData.TYPE); LicensesMetaData currentLicenses = metaData.custom(LicensesMetaData.TYPE);
final LicensesWrapper licensesWrapper = LicensesWrapper.wrap(currentLicenses); final LicensesWrapper licensesWrapper = LicensesWrapper.wrap(currentLicenses);
licensesWrapper.addSignedLicenses(esLicenseManager, newLicenses); licensesWrapper.addSignedLicenses(licenseManager, newLicenses);
mdBuilder.putCustom(LicensesMetaData.TYPE, licensesWrapper.get()); mdBuilder.putCustom(LicensesMetaData.TYPE, licensesWrapper.get());
return ClusterState.builder(currentState).metaData(mdBuilder).build(); return ClusterState.builder(currentState).metaData(mdBuilder).build();
} }
@ -142,7 +142,7 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
MetaData.Builder mdBuilder = MetaData.builder(currentState.metaData()); MetaData.Builder mdBuilder = MetaData.builder(currentState.metaData());
LicensesMetaData currentLicenses = metaData.custom(LicensesMetaData.TYPE); LicensesMetaData currentLicenses = metaData.custom(LicensesMetaData.TYPE);
final LicensesWrapper licensesWrapper = LicensesWrapper.wrap(currentLicenses); final LicensesWrapper licensesWrapper = LicensesWrapper.wrap(currentLicenses);
licensesWrapper.removeFeatures(esLicenseManager, request.features()); licensesWrapper.removeFeatures(licenseManager, request.features());
mdBuilder.putCustom(LicensesMetaData.TYPE, licensesWrapper.get()); mdBuilder.putCustom(LicensesMetaData.TYPE, licensesWrapper.get());
return ClusterState.builder(currentState).metaData(mdBuilder).build(); return ClusterState.builder(currentState).metaData(mdBuilder).build();
} }
@ -158,7 +158,7 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
private LicensesStatus checkLicenses(Map<String, ESLicense> licenseMap) { private LicensesStatus checkLicenses(Map<String, ESLicense> licenseMap) {
LicensesStatus status = LicensesStatus.VALID; LicensesStatus status = LicensesStatus.VALID;
try { try {
esLicenseManager.verifyLicenses(licenseMap); licenseManager.verifyLicenses(licenseMap);
} catch (ExpiredLicenseException e) { } catch (ExpiredLicenseException e) {
status = LicensesStatus.EXPIRED; status = LicensesStatus.EXPIRED;
} catch (InvalidLicenseException e) { } catch (InvalidLicenseException e) {
@ -185,7 +185,7 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
LicensesMetaData currentMetaData = clusterService.state().metaData().custom(LicensesMetaData.TYPE); LicensesMetaData currentMetaData = clusterService.state().metaData().custom(LicensesMetaData.TYPE);
if (currentMetaData != null) { if (currentMetaData != null) {
// don't use ESLicenses.reduceAndMap, as it will merge out expired licenses // don't use ESLicenses.reduceAndMap, as it will merge out expired licenses
Set<ESLicense> licenses = Sets.union(esLicenseManager.fromSignatures(currentMetaData.getSignatures()), Set<ESLicense> licenses = Sets.union(licenseManager.fromSignatures(currentMetaData.getSignatures()),
TrialLicenseUtils.fromEncodedTrialLicenses(currentMetaData.getEncodedTrialLicenses())); TrialLicenseUtils.fromEncodedTrialLicenses(currentMetaData.getEncodedTrialLicenses()));
// bucket license for feature with the latest expiry date // bucket license for feature with the latest expiry date
@ -232,7 +232,7 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
LicensesMetaData currentLicensesMetaData = metaData.custom(LicensesMetaData.TYPE); LicensesMetaData currentLicensesMetaData = metaData.custom(LicensesMetaData.TYPE);
final LicensesWrapper licensesWrapper = LicensesWrapper.wrap(currentLicensesMetaData); final LicensesWrapper licensesWrapper = LicensesWrapper.wrap(currentLicensesMetaData);
// do not generate a trial license for a feature that already has a signed/trial license // do not generate a trial license for a feature that already has a signed/trial license
licensesWrapper.addTrialLicenseIfNeeded(esLicenseManager, licensesWrapper.addTrialLicenseIfNeeded(licenseManager,
generateTrialLicense(request.feature, request.duration, request.maxNodes)); generateTrialLicense(request.feature, request.duration, request.maxNodes));
mdBuilder.putCustom(LicensesMetaData.TYPE, licensesWrapper.get()); mdBuilder.putCustom(LicensesMetaData.TYPE, licensesWrapper.get());
return ClusterState.builder(currentState).metaData(mdBuilder).build(); return ClusterState.builder(currentState).metaData(mdBuilder).build();
@ -287,7 +287,7 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
registeredListeners.clear(); registeredListeners.clear();
} }
lastObservedState.set(null); lastObservedLicensesState.set(null);
} }
@Override @Override
@ -319,7 +319,7 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
} }
private void notifyFeaturesAndScheduleNotificationIfNeeded(LicensesMetaData currentLicensesMetaData) { private void notifyFeaturesAndScheduleNotificationIfNeeded(LicensesMetaData currentLicensesMetaData) {
final LicensesMetaData lastNotifiedLicensesMetaData = lastObservedState.get(); final LicensesMetaData lastNotifiedLicensesMetaData = lastObservedLicensesState.get();
if (lastNotifiedLicensesMetaData != null && lastNotifiedLicensesMetaData.equals(currentLicensesMetaData)) { if (lastNotifiedLicensesMetaData != null && lastNotifiedLicensesMetaData.equals(currentLicensesMetaData)) {
logger.info("currentLicensesMetaData has been already notified on"); logger.info("currentLicensesMetaData has been already notified on");
return; return;
@ -433,7 +433,7 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
Map<String, ESLicense> map = new HashMap<>(); Map<String, ESLicense> map = new HashMap<>();
if (metaData != null) { if (metaData != null) {
Set<ESLicense> esLicenses = new HashSet<>(); Set<ESLicense> esLicenses = new HashSet<>();
esLicenses.addAll(esLicenseManager.fromSignatures(metaData.getSignatures())); esLicenses.addAll(licenseManager.fromSignatures(metaData.getSignatures()));
esLicenses.addAll(TrialLicenseUtils.fromEncodedTrialLicenses(metaData.getEncodedTrialLicenses())); esLicenses.addAll(TrialLicenseUtils.fromEncodedTrialLicenses(metaData.getEncodedTrialLicenses()));
return reduceAndMap(esLicenses); return reduceAndMap(esLicenses);
} }
@ -455,11 +455,6 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
} }
} }
//TODO: Shouldn't expose this
public ESLicenseManager getEsLicenseManager() {
return esLicenseManager;
}
public class LicensingClientNotificationJob implements Runnable { public class LicensingClientNotificationJob implements Runnable {
public LicensingClientNotificationJob() { public LicensingClientNotificationJob() {
@ -544,7 +539,7 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
sb.append("]"); sb.append("]");
logger.info(sb.toString()); logger.info(sb.toString());
lastObservedState.set(licensesWrapper.get()); lastObservedLicensesState.set(licensesWrapper.get());
if (nextScheduleFrequency == -1l) { if (nextScheduleFrequency == -1l) {
logger.info("turn off notifications"); logger.info("turn off notifications");
@ -600,17 +595,17 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
} }
private void enableFeatureIfNeeded() { private void enableFeatureIfNeeded() {
logger.info("enabled flag: " + enabled.get()); //logger.info("enabled flag: " + enabled.get());
if (enabled.compareAndSet(false, true)) { if (enabled.compareAndSet(false, true)) {
logger.info("calling onEnabled on listener"); //logger.info("calling onEnabled on listener");
listener.onEnabled(); listener.onEnabled();
} }
} }
private void disableFeatureIfNeeded() { private void disableFeatureIfNeeded() {
logger.info("enabled flag: " + enabled.get()); //logger.info("enabled flag: " + enabled.get());
if (enabled.compareAndSet(true, false)) { if (enabled.compareAndSet(true, false)) {
logger.info("calling onDisabled on listener"); //logger.info("calling onDisabled on listener");
listener.onDisabled(); listener.onDisabled();
} }
} }
@ -641,11 +636,11 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
} }
/** /**
* Check if any trial license for the feature exists, * Check if any license(s) for the feature exists,
* if no trial license for feature exists, add new * if no license(s) for feature exists, add new
* trial license for feature * trial license for feature
* *
* @param trialLicense to add * @param generatedTrialLicense new trial license for feature
*/ */
public void addTrialLicenseIfNeeded(ESLicenseManager licenseManager, ESLicense generatedTrialLicense) { public void addTrialLicenseIfNeeded(ESLicenseManager licenseManager, ESLicense generatedTrialLicense) {
boolean featureTrialLicenseExists = false; boolean featureTrialLicenseExists = false;

View File

@ -146,7 +146,7 @@ public class LicensesServiceTests extends ElasticsearchIntegrationTest {
List<ESLicense> licenses = ESLicenses.fromSource(licenseOutput); List<ESLicense> licenses = ESLicenses.fromSource(licenseOutput);
LicensesManagerService licensesManagerService = masterLicensesManagerService(); LicensesManagerService licensesManagerService = masterLicensesManagerService();
ESLicenseManager esLicenseManager = ((LicensesService) licensesManagerService).getEsLicenseManager(); ESLicenseManager esLicenseManager = masterLicenseManager();
final CountDownLatch latch1 = new CountDownLatch(1); final CountDownLatch latch1 = new CountDownLatch(1);
// todo: fix with awaitBusy // todo: fix with awaitBusy
licensesManagerService.registerLicenses(new LicensesService.PutLicenseRequestHolder(new PutLicenseRequest().licenses(licenses), "test"), new ActionListener<ClusterStateUpdateResponse>() { licensesManagerService.registerLicenses(new LicensesService.PutLicenseRequestHolder(new PutLicenseRequest().licenses(licenses), "test"), new ActionListener<ClusterStateUpdateResponse>() {
@ -344,6 +344,11 @@ public class LicensesServiceTests extends ElasticsearchIntegrationTest {
return clients.getInstance(LicensesManagerService.class, clients.getMasterName()); return clients.getInstance(LicensesManagerService.class, clients.getMasterName());
} }
private ESLicenseManager masterLicenseManager() {
final InternalTestCluster clients = internalCluster();
return clients.getInstance(ESLicenseManager.class, clients.getMasterName());
}
private LicensesManagerService licensesManagerService() { private LicensesManagerService licensesManagerService() {
return internalCluster().getInstance(LicensesManagerService.class, node); return internalCluster().getInstance(LicensesManagerService.class, node);
} }