LicensesService cleanup

Original commit: elastic/x-pack-elasticsearch@348a2a6ca9
This commit is contained in:
Igor Motov 2014-10-30 17:14:43 -04:00
parent 6d6f196af6
commit d518cd86db
1 changed files with 36 additions and 41 deletions

View File

@ -94,7 +94,7 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
this.threadPool = threadPool; this.threadPool = threadPool;
this.transportService = transportService; this.transportService = transportService;
this.lastObservedLicensesState = new AtomicReference<>(null); 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()); transportService.registerHandler(REGISTER_TRIAL_LICENSE_ACTION_NAME, new RegisterTrialLicenseRequestHandler());
} }
} }
@ -107,8 +107,7 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
final PutLicenseRequest request = requestHolder.request; final PutLicenseRequest request = requestHolder.request;
final Set<ESLicense> newLicenses = Sets.newHashSet(request.licenses()); final Set<ESLicense> newLicenses = Sets.newHashSet(request.licenses());
LicensesStatus status = checkLicenses(newLicenses); LicensesStatus status = checkLicenses(newLicenses);
switch (status) { if (status == LicensesStatus.VALID) {
case VALID:
clusterService.submitStateUpdateTask(requestHolder.source, new AckedClusterStateUpdateTask<LicensesUpdateResponse>(request, listener) { clusterService.submitStateUpdateTask(requestHolder.source, new AckedClusterStateUpdateTask<LicensesUpdateResponse>(request, listener) {
@Override @Override
protected LicensesUpdateResponse newResponse(boolean acknowledged) { protected LicensesUpdateResponse newResponse(boolean acknowledged) {
@ -132,11 +131,8 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
return ClusterState.builder(currentState).metaData(mdBuilder).build(); return ClusterState.builder(currentState).metaData(mdBuilder).build();
} }
}); });
break; } else {
case INVALID:
case EXPIRED:
listener.onResponse(new LicensesUpdateResponse(true, status)); listener.onResponse(new LicensesUpdateResponse(true, status));
break;
} }
} }
@ -168,7 +164,6 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
@Override @Override
public ClusterState execute(ClusterState currentState) throws Exception { public ClusterState execute(ClusterState currentState) throws Exception {
MetaData metaData = currentState.metaData(); MetaData metaData = 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);
@ -179,15 +174,16 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
licensesToDelete.add(license); licensesToDelete.add(license);
} }
} }
if (!licensesToDelete.isEmpty()) {
Set<ESLicense> reducedLicenses = Sets.difference(currentSignedLicenses, licensesToDelete); Set<ESLicense> reducedLicenses = Sets.difference(currentSignedLicenses, licensesToDelete);
if (reducedLicenses.size() != currentSignedLicenses.size()) {
Set<String> newSignatures = licenseManager.toSignatures(reducedLicenses); Set<String> newSignatures = licenseManager.toSignatures(reducedLicenses);
LicensesMetaData newLicensesMetaData = new LicensesMetaData(newSignatures, licensesWrapper.encodedTrialLicenses); LicensesMetaData newLicensesMetaData = new LicensesMetaData(newSignatures, licensesWrapper.encodedTrialLicenses);
MetaData.Builder mdBuilder = MetaData.builder(currentState.metaData());
mdBuilder.putCustom(LicensesMetaData.TYPE, newLicensesMetaData); mdBuilder.putCustom(LicensesMetaData.TYPE, newLicensesMetaData);
} else {
mdBuilder.putCustom(LicensesMetaData.TYPE, currentLicenses);
}
return ClusterState.builder(currentState).metaData(mdBuilder).build(); return ClusterState.builder(currentState).metaData(mdBuilder).build();
} else {
return currentState;
}
} }
}); });
} }
@ -334,12 +330,6 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
@Override @Override
protected void doStop() throws ElasticsearchException { 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); clusterService.remove(this);
// cancel all notifications // cancel all notifications
@ -360,6 +350,12 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
lastObservedLicensesState.set(null); 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}: * 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} * - tries to register any {@link #pendingListeners} by calling {@link #registeredListeners}
@ -400,7 +396,6 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
} }
} }
// notify all interested plugins // notify all interested plugins
// Change to debug // Change to debug
if (event.previousState().blocks().hasGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK) || addedNewRegisteredListener) { if (event.previousState().blocks().hasGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK) || addedNewRegisteredListener) {