[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:
parent
72f5e92972
commit
379b800c9f
|
@ -108,7 +108,14 @@ 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);
|
||||||
new Usage.Retriever(client, mlMetadata, available(), enabled()).execute(listener);
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Usage extends XPackFeatureSet.Usage {
|
public static class Usage extends XPackFeatureSet.Usage {
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
Loading…
Reference in New Issue