[ML] Handle usage call when MlMetadata is not yet installed (elastic/x-pack-elasticsearch#890)

Original commit: elastic/x-pack-elasticsearch@495286d4ba
This commit is contained in:
Dimitris Athanasiou 2017-03-30 10:55:36 +01:00 committed by GitHub
parent 72f5e92972
commit 379b800c9f
2 changed files with 32 additions and 1 deletions

View File

@ -108,8 +108,15 @@ public class MachineLearningFeatureSet implements XPackFeatureSet {
public void usage(ActionListener<XPackFeatureSet.Usage> listener) { public void usage(ActionListener<XPackFeatureSet.Usage> listener) {
ClusterState state = clusterService.state(); ClusterState state = clusterService.state();
MlMetadata mlMetadata = state.getMetaData().custom(MlMetadata.TYPE); MlMetadata mlMetadata = state.getMetaData().custom(MlMetadata.TYPE);
// Handle case when usage is called but MlMetadata has not been installed yet
if (mlMetadata == null) {
listener.onResponse(new Usage(available(), enabled,
Collections.emptyMap(), Collections.emptyMap()));
} else {
new Usage.Retriever(client, mlMetadata, available(), enabled()).execute(listener); new Usage.Retriever(client, mlMetadata, available(), enabled()).execute(listener);
} }
}
public static class Usage extends XPackFeatureSet.Usage { public static class Usage extends XPackFeatureSet.Usage {

View File

@ -182,6 +182,30 @@ public class MachineLearningFeatureSetTests extends ESTestCase {
} }
} }
public void testUsageGivenMlMetadataNotInstalled() throws Exception {
when(licenseState.isMachineLearningAllowed()).thenReturn(true);
Settings.Builder settings = Settings.builder();
settings.put("xpack.ml.enabled", true);
when(clusterService.state()).thenReturn(ClusterState.EMPTY_STATE);
MachineLearningFeatureSet featureSet = new MachineLearningFeatureSet(settings.build(),
clusterService, client, licenseState);
PlainActionFuture<Usage> future = new PlainActionFuture<>();
featureSet.usage(future);
XPackFeatureSet.Usage usage = future.get();
assertThat(usage.available(), is(true));
assertThat(usage.enabled(), is(true));
XContentSource source;
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
usage.toXContent(builder, ToXContent.EMPTY_PARAMS);
source = new XContentSource(builder);
assertThat(source.getValue("jobs"), equalTo(Collections.emptyMap()));
assertThat(source.getValue("datafeeds"), equalTo(Collections.emptyMap()));
}
}
private void givenJobs(List<Job> jobs, List<GetJobsStatsAction.Response.JobStats> jobsStats) { private void givenJobs(List<Job> jobs, List<GetJobsStatsAction.Response.JobStats> jobsStats) {
MlMetadata.Builder mlMetadataBuilder = new MlMetadata.Builder(); MlMetadata.Builder mlMetadataBuilder = new MlMetadata.Builder();
for (Job job : jobs) { for (Job job : jobs) {