use clock interface for license service

Original commit: elastic/x-pack-elasticsearch@f7d4795926
This commit is contained in:
Areek Zillur 2016-05-31 18:01:46 -04:00
parent 9a398b9413
commit 95631d955e
1 changed files with 9 additions and 5 deletions

View File

@ -42,6 +42,7 @@ import org.elasticsearch.transport.TransportRequest;
import org.elasticsearch.transport.TransportRequestHandler;
import org.elasticsearch.transport.TransportResponse;
import org.elasticsearch.transport.TransportService;
import org.elasticsearch.xpack.support.clock.Clock;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@ -110,6 +111,8 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
*/
private final AtomicReference<License> currentLicense = new AtomicReference<>();
private final Clock clock;
/**
* Callbacks to notify relative to license expiry
*/
@ -136,7 +139,7 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
"please read the following messages and update the license again, this time with the \"acknowledge=true\" parameter:";
@Inject
public LicensesService(Settings settings, ClusterService clusterService, ThreadPool threadPool, TransportService transportService) {
public LicensesService(Settings settings, ClusterService clusterService, ThreadPool threadPool, TransportService transportService, Clock clock) {
super(settings);
this.clusterService = clusterService;
this.threadPool = threadPool;
@ -146,6 +149,7 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
ThreadPool.Names.SAME, new RegisterTrialLicenseRequestHandler());
}
populateExpirationCallbacks();
this.clock = clock;
}
private void populateExpirationCallbacks() {
@ -253,7 +257,7 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
*/
public void registerLicense(final PutLicenseRequest request, final ActionListener<PutLicenseResponse> listener) {
final License newLicense = request.license();
final long now = System.currentTimeMillis();
final long now = clock.millis();
if (!verifyLicense(newLicense) || newLicense.issueDate() > now) {
listener.onResponse(new PutLicenseResponse(true, LicensesStatus.INVALID));
} else if (newLicense.expiryDate() < now) {
@ -382,7 +386,7 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
MetaData.Builder mdBuilder = MetaData.builder(currentState.metaData());
// do not generate a trial license if any license is present
if (currentLicensesMetaData == null) {
long issueDate = System.currentTimeMillis();
long issueDate = clock.millis();
License.Builder specBuilder = License.builder()
.uid(UUID.randomUUID().toString())
.issuedTo(clusterService.state().getClusterName().value())
@ -492,7 +496,7 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
// in this case, it is a no-op
if (license != null) {
logger.debug("notifying [{}] listeners", registeredLicensees.size());
long now = System.currentTimeMillis();
long now = clock.millis();
if (license.issueDate() > now) {
logger.warn("license [{}] - invalid", license.uid());
return;
@ -587,7 +591,7 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
LicensesMetaData currentLicensesMetaData = clusterService.state().metaData().custom(LicensesMetaData.TYPE);
License license = getLicense(currentLicensesMetaData);
if (license != null) {
long now = System.currentTimeMillis();
long now = clock.millis();
if (expirationCallback.matches(license.expiryDate(), now)) {
expirationCallback.on(license);
if (expirationCallback.frequency() != null) {