From aa877161ff8cafefc9129a89681bf19d0fe134eb Mon Sep 17 00:00:00 2001 From: Zachary Tong Date: Thu, 8 Mar 2018 08:06:42 -0800 Subject: [PATCH] [Rollup] Register FeatureSetUsage with xpack, add tests (elastic/x-pack-elasticsearch#4040) We had a Usage class before, but weren't registering it with XPack. Would be nice to add more usage info in the future (like the running jobs on each node), but unclear the best way to do it since we'd need to filter through the list of allocated tasks. Original commit: elastic/x-pack-elasticsearch@5207d2758b8f7bca2d5b30405347e40109ab0b37 --- .../xpack/core/XPackClientPlugin.java | 2 + .../core/rollup/RollupFeatureSetUsage.java | 23 +++++++ .../xpack/rollup/RollupFeatureSet.java | 17 +---- .../xpack/rollup/RollupFeatureSetTests.java | 67 +++++++++++++++++++ 4 files changed, 95 insertions(+), 14 deletions(-) create mode 100644 plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/RollupFeatureSetUsage.java create mode 100644 plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/RollupFeatureSetTests.java diff --git a/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java b/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java index 0cfada98667..f49eba31a32 100644 --- a/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java +++ b/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java @@ -92,6 +92,7 @@ import org.elasticsearch.persistent.PersistentTasksNodeService; import org.elasticsearch.persistent.RemovePersistentTaskAction; import org.elasticsearch.persistent.StartPersistentTaskAction; import org.elasticsearch.persistent.UpdatePersistentTaskStatusAction; +import org.elasticsearch.xpack.core.rollup.RollupFeatureSetUsage; import org.elasticsearch.xpack.core.rollup.RollupField; import org.elasticsearch.xpack.core.rollup.action.DeleteRollupJobAction; import org.elasticsearch.xpack.core.rollup.action.GetRollupCapsAction; @@ -344,6 +345,7 @@ public class XPackClientPlugin extends Plugin implements ActionPlugin, NetworkPl new NamedWriteableRegistry.Entry(MetaData.Custom.class, LicensesMetaData.TYPE, LicensesMetaData::new), new NamedWriteableRegistry.Entry(NamedDiff.class, LicensesMetaData.TYPE, LicensesMetaData::readDiffFrom), //rollup + new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.ROLLUP, RollupFeatureSetUsage::new), new NamedWriteableRegistry.Entry(PersistentTaskParams.class, RollupJob.NAME, RollupJob::new), new NamedWriteableRegistry.Entry(Task.Status.class, RollupJobStatus.NAME, RollupJobStatus::new) ); diff --git a/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/RollupFeatureSetUsage.java b/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/RollupFeatureSetUsage.java new file mode 100644 index 00000000000..b0377fbcc67 --- /dev/null +++ b/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/RollupFeatureSetUsage.java @@ -0,0 +1,23 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.xpack.core.rollup; + +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.xpack.core.XPackFeatureSet; +import org.elasticsearch.xpack.core.XPackField; + +import java.io.IOException; + +public class RollupFeatureSetUsage extends XPackFeatureSet.Usage { + + public RollupFeatureSetUsage(StreamInput input) throws IOException { + super(input); + } + + public RollupFeatureSetUsage(boolean available, boolean enabled) { + super(XPackField.ROLLUP, available, enabled); + } +} \ No newline at end of file diff --git a/plugin/rollup/src/main/java/org/elasticsearch/xpack/rollup/RollupFeatureSet.java b/plugin/rollup/src/main/java/org/elasticsearch/xpack/rollup/RollupFeatureSet.java index 3bd27671278..780b5fa6ffa 100644 --- a/plugin/rollup/src/main/java/org/elasticsearch/xpack/rollup/RollupFeatureSet.java +++ b/plugin/rollup/src/main/java/org/elasticsearch/xpack/rollup/RollupFeatureSet.java @@ -8,14 +8,13 @@ package org.elasticsearch.xpack.rollup; import org.elasticsearch.action.ActionListener; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.xpack.core.XPackFeatureSet; import org.elasticsearch.xpack.core.XPackField; import org.elasticsearch.xpack.core.XPackSettings; +import org.elasticsearch.xpack.core.rollup.RollupFeatureSetUsage; -import java.io.IOException; import java.util.Map; public class RollupFeatureSet implements XPackFeatureSet { @@ -56,17 +55,7 @@ public class RollupFeatureSet implements XPackFeatureSet { @Override public void usage(ActionListener listener) { - listener.onResponse(new RollupFeatureSet.Usage(available(), enabled())); - } - - public static class Usage extends XPackFeatureSet.Usage { - - public Usage(StreamInput input) throws IOException { - super(input); - } - - public Usage(boolean available, boolean enabled) { - super(XPackField.ROLLUP, available, enabled); - } + // TODO expose the currently running rollup tasks on this node? Unclear the best way to do that + listener.onResponse(new RollupFeatureSetUsage(available(), enabled())); } } diff --git a/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/RollupFeatureSetTests.java b/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/RollupFeatureSetTests.java new file mode 100644 index 00000000000..092fea16f28 --- /dev/null +++ b/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/RollupFeatureSetTests.java @@ -0,0 +1,67 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.xpack.rollup; + +import org.elasticsearch.action.support.PlainActionFuture; +import org.elasticsearch.common.io.stream.BytesStreamOutput; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.license.XPackLicenseState; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xpack.core.XPackFeatureSet; +import org.elasticsearch.xpack.core.rollup.RollupFeatureSetUsage; +import org.junit.Before; + +import java.io.IOException; +import java.util.Arrays; +import java.util.concurrent.ExecutionException; + +import static org.hamcrest.core.Is.is; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class RollupFeatureSetTests extends ESTestCase { + private XPackLicenseState licenseState; + + @Before + public void init() { + licenseState = mock(XPackLicenseState.class); + } + + public void testAvailable() { + RollupFeatureSet featureSet = new RollupFeatureSet(Settings.EMPTY, licenseState); + boolean available = randomBoolean(); + when(licenseState.isRollupAllowed()).thenReturn(available); + assertThat(featureSet.available(), is(available)); + } + + public void testEnabledSetting() { + boolean enabled = randomBoolean(); + Settings.Builder settings = Settings.builder(); + settings.put("xpack.rollup.enabled", enabled); + RollupFeatureSet featureSet = new RollupFeatureSet(settings.build(), licenseState); + assertThat(featureSet.enabled(), is(enabled)); + } + + public void testEnabledDefault() { + RollupFeatureSet featureSet = new RollupFeatureSet(Settings.EMPTY, licenseState); + assertThat(featureSet.enabled(), is(true)); + } + + public void testUsage() throws ExecutionException, InterruptedException, IOException { + RollupFeatureSet featureSet = new RollupFeatureSet(Settings.EMPTY, licenseState); + PlainActionFuture future = new PlainActionFuture<>(); + featureSet.usage(future); + XPackFeatureSet.Usage rollupUsage = future.get(); + BytesStreamOutput out = new BytesStreamOutput(); + rollupUsage.writeTo(out); + XPackFeatureSet.Usage serializedUsage = new RollupFeatureSetUsage(out.bytes().streamInput()); + for (XPackFeatureSet.Usage usage : Arrays.asList(rollupUsage, serializedUsage)) { + assertThat(usage.name(), is(featureSet.name())); + assertThat(usage.enabled(), is(featureSet.enabled())); + } + } + +}