From d518cd86dbbb12421850b4b4d3ba23f90afcff35 Mon Sep 17 00:00:00 2001 From: Igor Motov Date: Thu, 30 Oct 2014 17:14:43 -0400 Subject: [PATCH] LicensesService cleanup Original commit: elastic/x-pack-elasticsearch@348a2a6ca957cf286e1503958d1a9bc3316310f4 --- .../license/plugin/core/LicensesService.java | 77 +++++++++---------- 1 file changed, 36 insertions(+), 41 deletions(-) diff --git a/src/main/java/org/elasticsearch/license/plugin/core/LicensesService.java b/src/main/java/org/elasticsearch/license/plugin/core/LicensesService.java index 733693ba357..c72bf894457 100644 --- a/src/main/java/org/elasticsearch/license/plugin/core/LicensesService.java +++ b/src/main/java/org/elasticsearch/license/plugin/core/LicensesService.java @@ -94,7 +94,7 @@ public class LicensesService extends AbstractLifecycleComponent this.threadPool = threadPool; this.transportService = transportService; this.lastObservedLicensesState = new AtomicReference<>(null); - if (DiscoveryNode.dataNode(settings) || DiscoveryNode.masterNode(settings)) { + if (DiscoveryNode.masterNode(settings)) { transportService.registerHandler(REGISTER_TRIAL_LICENSE_ACTION_NAME, new RegisterTrialLicenseRequestHandler()); } } @@ -107,36 +107,32 @@ public class LicensesService extends AbstractLifecycleComponent final PutLicenseRequest request = requestHolder.request; final Set newLicenses = Sets.newHashSet(request.licenses()); LicensesStatus status = checkLicenses(newLicenses); - switch (status) { - case VALID: - clusterService.submitStateUpdateTask(requestHolder.source, new AckedClusterStateUpdateTask(request, listener) { - @Override - protected LicensesUpdateResponse newResponse(boolean acknowledged) { - return new LicensesUpdateResponse(acknowledged, LicensesStatus.VALID); - } + if (status == LicensesStatus.VALID) { + clusterService.submitStateUpdateTask(requestHolder.source, new AckedClusterStateUpdateTask(request, listener) { + @Override + protected LicensesUpdateResponse newResponse(boolean acknowledged) { + return new LicensesUpdateResponse(acknowledged, LicensesStatus.VALID); + } - @Override - public ClusterState execute(ClusterState currentState) throws Exception { - MetaData metaData = currentState.metaData(); - MetaData.Builder mdBuilder = MetaData.builder(currentState.metaData()); - LicensesMetaData currentLicenses = metaData.custom(LicensesMetaData.TYPE); - final LicensesWrapper licensesWrapper = LicensesWrapper.wrap(currentLicenses); - Set newSignatures = licenseManager.toSignatures(newLicenses); - Set newLicenseSignatures = Sets.union(licensesWrapper.signatures, newSignatures); - if (newLicenseSignatures.size() != licensesWrapper.signatures.size()) { - LicensesMetaData newLicensesMetaData = new LicensesMetaData(newLicenseSignatures, licensesWrapper.encodedTrialLicenses); - mdBuilder.putCustom(LicensesMetaData.TYPE, newLicensesMetaData); - } else { - mdBuilder.putCustom(LicensesMetaData.TYPE, currentLicenses); - } - return ClusterState.builder(currentState).metaData(mdBuilder).build(); + @Override + public ClusterState execute(ClusterState currentState) throws Exception { + MetaData metaData = currentState.metaData(); + MetaData.Builder mdBuilder = MetaData.builder(currentState.metaData()); + LicensesMetaData currentLicenses = metaData.custom(LicensesMetaData.TYPE); + final LicensesWrapper licensesWrapper = LicensesWrapper.wrap(currentLicenses); + Set newSignatures = licenseManager.toSignatures(newLicenses); + Set newLicenseSignatures = Sets.union(licensesWrapper.signatures, newSignatures); + if (newLicenseSignatures.size() != licensesWrapper.signatures.size()) { + LicensesMetaData newLicensesMetaData = new LicensesMetaData(newLicenseSignatures, licensesWrapper.encodedTrialLicenses); + mdBuilder.putCustom(LicensesMetaData.TYPE, newLicensesMetaData); + } else { + mdBuilder.putCustom(LicensesMetaData.TYPE, currentLicenses); } - }); - break; - case INVALID: - case EXPIRED: - listener.onResponse(new LicensesUpdateResponse(true, status)); - break; + return ClusterState.builder(currentState).metaData(mdBuilder).build(); + } + }); + } else { + listener.onResponse(new LicensesUpdateResponse(true, status)); } } @@ -168,7 +164,6 @@ public class LicensesService extends AbstractLifecycleComponent @Override public ClusterState execute(ClusterState currentState) throws Exception { MetaData metaData = currentState.metaData(); - MetaData.Builder mdBuilder = MetaData.builder(currentState.metaData()); LicensesMetaData currentLicenses = metaData.custom(LicensesMetaData.TYPE); final LicensesWrapper licensesWrapper = LicensesWrapper.wrap(currentLicenses); @@ -179,15 +174,16 @@ public class LicensesService extends AbstractLifecycleComponent licensesToDelete.add(license); } } - Set reducedLicenses = Sets.difference(currentSignedLicenses, licensesToDelete); - if (reducedLicenses.size() != currentSignedLicenses.size()) { + if (!licensesToDelete.isEmpty()) { + Set reducedLicenses = Sets.difference(currentSignedLicenses, licensesToDelete); Set newSignatures = licenseManager.toSignatures(reducedLicenses); LicensesMetaData newLicensesMetaData = new LicensesMetaData(newSignatures, licensesWrapper.encodedTrialLicenses); + MetaData.Builder mdBuilder = MetaData.builder(currentState.metaData()); mdBuilder.putCustom(LicensesMetaData.TYPE, newLicensesMetaData); + return ClusterState.builder(currentState).metaData(mdBuilder).build(); } else { - mdBuilder.putCustom(LicensesMetaData.TYPE, currentLicenses); + return currentState; } - return ClusterState.builder(currentState).metaData(mdBuilder).build(); } }); } @@ -334,12 +330,6 @@ public class LicensesService extends AbstractLifecycleComponent @Override protected void doStop() throws ElasticsearchException { - // Should scheduledNotifications be cancelled on stop as well? - } - - @Override - protected void doClose() throws ElasticsearchException { - logger.info("Closing LicensesService"); clusterService.remove(this); // cancel all notifications @@ -360,6 +350,12 @@ public class LicensesService extends AbstractLifecycleComponent lastObservedLicensesState.set(null); } + @Override + protected void doClose() throws ElasticsearchException { + logger.info("Closing LicensesService"); + transportService.removeHandler(REGISTER_TRIAL_LICENSE_ACTION_NAME); + } + /** * When there is no global block on {@link org.elasticsearch.gateway.GatewayService#STATE_NOT_RECOVERED_BLOCK}: * - tries to register any {@link #pendingListeners} by calling {@link #registeredListeners} @@ -400,7 +396,6 @@ public class LicensesService extends AbstractLifecycleComponent } } - // notify all interested plugins // Change to debug if (event.previousState().blocks().hasGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK) || addedNewRegisteredListener) {