소스 검색

Allow bootstrap plugins to appear in _cat/plugins (#66260)

Closes #66107.

Bootstrap plugins are not loaded in the main Elasticsearch process, but
instead take effect only when ES is starting. As such, these plugins are
skipped when ES loads all installed plugins.

As a result, it was impossible for the plugins _cat API to report
whether any bootstrap plugins are installed.

Fix this by adjusting how the loading process skips bootstrap plugins,
and then tweaking the plugins _cat API so that bootstrap plugins can
optionally be included in the response.
Rory Hunter 4 년 전
부모
커밋
4ff612550e

+ 40 - 0
qa/os/src/test/java/org/elasticsearch/packaging/test/QuotaAwareFsTests.java

@@ -31,10 +31,16 @@ import org.junit.BeforeClass;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.util.List;
 import java.util.Locale;
+import java.util.stream.Collectors;
 
+import static org.hamcrest.Matchers.arrayContaining;
 import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.emptyString;
 import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.not;
 import static org.junit.Assume.assumeTrue;
 
 /**
@@ -146,6 +152,40 @@ public class QuotaAwareFsTests extends PackagingTestCase {
         }
     }
 
+    /**
+     * Check that the _cat API can list the plugin correctly.
+     */
+    public void test40CatApiFiltersPlugin() throws Exception {
+        install();
+
+        int total = 20 * 1024 * 1024;
+        int available = 10 * 1024 * 1024;
+
+        installation.executables().pluginTool.run("install --batch \"" + QUOTA_AWARE_FS_PLUGIN.toUri() + "\"");
+
+        final Path quotaPath = getRootTempDir().resolve("quota.properties");
+        Files.writeString(quotaPath, String.format(Locale.ROOT, "total=%d\nremaining=%d\n", total, available));
+
+        sh.getEnv().put("ES_JAVA_OPTS", "-Des.fs.quota.file=" + quotaPath.toUri());
+
+        try {
+            startElasticsearch();
+
+            final String uri = "http://localhost:9200/_cat/plugins?include_bootstrap=true&h=component,type";
+            String response = ServerUtils.makeRequest(Request.Get(uri)).trim();
+            assertThat(response, not(emptyString()));
+
+            List<String> lines = response.lines().collect(Collectors.toList());
+            assertThat(lines, hasSize(1));
+
+            final String[] fields = lines.get(0).split(" ");
+            assertThat(fields, arrayContaining("quota-aware-fs", "bootstrap"));
+        } finally {
+            stopElasticsearch();
+            Files.deleteIfExists(quotaPath);
+        }
+    }
+
     private static class Totals {
         int totalInBytes;
         int availableInBytes;

+ 5 - 0
rest-api-spec/src/main/resources/rest-api-spec/api/cat.plugins.json

@@ -41,6 +41,11 @@
         "description":"Return help information",
         "default":false
       },
+      "include_bootstrap":{
+        "type":"boolean",
+        "description":"Include bootstrap plugins in the response",
+        "default":false
+      },
       "s":{
         "type":"list",
         "description":"Comma-separated list of column names or column aliases to sort by"

+ 5 - 0
rest-api-spec/src/main/resources/rest-api-spec/test/cat.plugins/10_basic.yml

@@ -1,5 +1,9 @@
 ---
 "Help":
+  - skip:
+      version: " - 7.99.99"
+      reason: output format changed in 8.0.0
+
   - do:
       cat.plugins:
         help: true
@@ -11,4 +15,5 @@
                     component   .+   \n
                     version     .+   \n
                     description .+   \n
+                    type        .+   \n
                $/

+ 10 - 12
server/src/main/java/org/elasticsearch/plugins/PluginsService.java

@@ -351,15 +351,11 @@ public class PluginsService implements ReportingService<PluginsAndModules> {
         final Set<Bundle> bundles = new HashSet<>();
         for (final Path plugin : findPluginDirs(directory)) {
             final Bundle bundle = readPluginBundle(plugin, type);
-            if (bundle.plugin.getType() == PluginType.BOOTSTRAP) {
-                logger.trace("--- skipping bootstrap plugin [{}] [{}]", type, plugin.toAbsolutePath());
-            } else {
-                if (bundles.add(bundle) == false) {
-                    throw new IllegalStateException("duplicate " + type + ": " + bundle.plugin);
-                }
-                if (type.equals("module") && bundle.plugin.getName().startsWith("test-") && Build.CURRENT.isSnapshot() == false) {
-                    throw new IllegalStateException("external test module [" + plugin.getFileName() + "] found in non-snapshot build");
-                }
+            if (bundles.add(bundle) == false) {
+                throw new IllegalStateException("duplicate " + type + ": " + bundle.plugin);
+            }
+            if (type.equals("module") && bundle.plugin.getName().startsWith("test-") && Build.CURRENT.isSnapshot() == false) {
+                throw new IllegalStateException("external test module [" + plugin.getFileName() + "] found in non-snapshot build");
             }
         }
 
@@ -443,10 +439,12 @@ public class PluginsService implements ReportingService<PluginsAndModules> {
         Map<String, Set<URL>> transitiveUrls = new HashMap<>();
         List<Bundle> sortedBundles = sortBundles(bundles);
         for (Bundle bundle : sortedBundles) {
-            checkBundleJarHell(JarHell.parseClassPath(), bundle, transitiveUrls);
+            if (bundle.plugin.getType() != PluginType.BOOTSTRAP) {
+                checkBundleJarHell(JarHell.parseClassPath(), bundle, transitiveUrls);
 
-            final Plugin plugin = loadBundle(bundle, loaded);
-            plugins.add(new Tuple<>(bundle.plugin, plugin));
+                final Plugin plugin = loadBundle(bundle, loaded);
+                plugins.add(new Tuple<>(bundle.plugin, plugin));
+            }
         }
 
         loadExtensions(plugins);

+ 13 - 2
server/src/main/java/org/elasticsearch/rest/action/cat/RestPluginsAction.java

@@ -30,12 +30,14 @@ import org.elasticsearch.cluster.node.DiscoveryNode;
 import org.elasticsearch.cluster.node.DiscoveryNodes;
 import org.elasticsearch.common.Table;
 import org.elasticsearch.plugins.PluginInfo;
+import org.elasticsearch.plugins.PluginType;
 import org.elasticsearch.rest.RestRequest;
 import org.elasticsearch.rest.RestResponse;
 import org.elasticsearch.rest.action.RestActionListener;
 import org.elasticsearch.rest.action.RestResponseListener;
 
 import java.util.List;
+import java.util.Locale;
 
 import static org.elasticsearch.rest.RestRequest.Method.GET;
 
@@ -58,6 +60,7 @@ public class RestPluginsAction extends AbstractCatAction {
 
     @Override
     public RestChannelConsumer doCatRequest(final RestRequest request, final NodeClient client) {
+        final boolean includeBootstrapPlugins = request.paramAsBoolean("include_bootstrap", false);
         final ClusterStateRequest clusterStateRequest = new ClusterStateRequest();
         clusterStateRequest.clear().nodes(true);
         clusterStateRequest.local(request.paramAsBoolean("local", clusterStateRequest.local()));
@@ -72,7 +75,10 @@ public class RestPluginsAction extends AbstractCatAction {
                 client.admin().cluster().nodesInfo(nodesInfoRequest, new RestResponseListener<NodesInfoResponse>(channel) {
                     @Override
                     public RestResponse buildResponse(final NodesInfoResponse nodesInfoResponse) throws Exception {
-                        return RestTable.buildResponse(buildTable(request, clusterStateResponse, nodesInfoResponse), channel);
+                        return RestTable.buildResponse(
+                            buildTable(request, clusterStateResponse, nodesInfoResponse, includeBootstrapPlugins),
+                            channel
+                        );
                     }
                 });
             }
@@ -88,11 +94,12 @@ public class RestPluginsAction extends AbstractCatAction {
         table.addCell("component", "alias:c;desc:component");
         table.addCell("version", "alias:v;desc:component version");
         table.addCell("description", "alias:d;default:false;desc:plugin details");
+        table.addCell("type", "alias:t;default:false;desc:plugin type");
         table.endHeaders();
         return table;
     }
 
-    private Table buildTable(RestRequest req, ClusterStateResponse state, NodesInfoResponse nodesInfo) {
+    Table buildTable(RestRequest req, ClusterStateResponse state, NodesInfoResponse nodesInfo, boolean includeBootstrapPlugins) {
         DiscoveryNodes nodes = state.getState().nodes();
         Table table = getTableWithHeader(req);
 
@@ -106,12 +113,16 @@ public class RestPluginsAction extends AbstractCatAction {
                 continue;
             }
             for (PluginInfo pluginInfo : plugins.getPluginInfos()) {
+                if (pluginInfo.getType() == PluginType.BOOTSTRAP && includeBootstrapPlugins == false) {
+                    continue;
+                }
                 table.startRow();
                 table.addCell(node.getId());
                 table.addCell(node.getName());
                 table.addCell(pluginInfo.getName());
                 table.addCell(pluginInfo.getVersion());
                 table.addCell(pluginInfo.getDescription());
+                table.addCell(pluginInfo.getType().toString().toLowerCase(Locale.ROOT));
                 table.endRow();
             }
         }

+ 178 - 0
server/src/test/java/org/elasticsearch/rest/action/cat/RestPluginsActionTests.java

@@ -0,0 +1,178 @@
+/*
+ * Licensed to Elasticsearch under one or more contributor
+ * license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright
+ * ownership. Elasticsearch licenses this file to you under
+ * the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.elasticsearch.rest.action.cat;
+
+import org.elasticsearch.Version;
+import org.elasticsearch.action.admin.cluster.node.info.NodeInfo;
+import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse;
+import org.elasticsearch.action.admin.cluster.node.info.PluginsAndModules;
+import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
+import org.elasticsearch.cluster.ClusterName;
+import org.elasticsearch.cluster.ClusterState;
+import org.elasticsearch.cluster.node.DiscoveryNode;
+import org.elasticsearch.cluster.node.DiscoveryNodes;
+import org.elasticsearch.common.Table;
+import org.elasticsearch.plugins.PluginInfo;
+import org.elasticsearch.plugins.PluginType;
+import org.elasticsearch.rest.RestRequest;
+import org.elasticsearch.test.ESTestCase;
+import org.elasticsearch.test.rest.FakeRestRequest;
+import org.hamcrest.Matcher;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.is;
+
+public class RestPluginsActionTests extends ESTestCase {
+    private final RestPluginsAction action = new RestPluginsAction();
+
+    /**
+     * Check that the plugins cat API handles no plugins being installed
+     */
+    public void testNoPlugins() {
+        final Table table = buildTable(List.of(), false);
+
+        assertThat(table.getRows(), is(empty()));
+    }
+
+    /**
+     * Check that the plugins cat API excludes bootstrap plugins when they are not requested.
+     */
+    public void testIsolatedPluginOnly() {
+        final Table table = buildTable(
+            List.of(
+                plugin("test-plugin", PluginType.ISOLATED),
+                plugin("ignored-plugin", PluginType.BOOTSTRAP)
+            ),
+            false
+        );
+
+        // verify the table headers are correct
+        final List<Object> headers = table.getHeaders().stream().map(h -> h.value).collect(Collectors.toList());
+        assertThat(headers, contains("id", "name", "component", "version", "description", "type"));
+
+        // verify the table rows are correct
+        final List<List<String>> rows = table.getRows()
+            .stream()
+            .map(row -> row.stream().map(c -> String.valueOf(c.value)).collect(Collectors.toList()))
+            .collect(Collectors.toList());
+        assertThat(rows, hasSize(3));
+
+        final List<Matcher<? super List<String>>> matchers = new ArrayList<>();
+
+        for (int i = 0; i < 3; i++) {
+            matchers.add(contains(Integer.toString(i), "node-" + i, "test-plugin", "1.0", "test-plugin description", "isolated"));
+        }
+
+        assertThat(rows, containsInAnyOrder(matchers));
+    }
+
+    /**
+     * Check that the plugins cat API includes bootstrap plugins when they are requested.
+     */
+    public void testIncludeBootstrap() {
+        final Table table = buildTable(
+            List.of(plugin("test-plugin", PluginType.ISOLATED), plugin("bootstrap-plugin", PluginType.BOOTSTRAP)),
+            true
+        );
+
+        // verify the table rows are correct
+        final List<List<String>> rows = table.getRows()
+            .stream()
+            .map(row -> row.stream().map(c -> String.valueOf(c.value)).collect(Collectors.toList()))
+            .collect(Collectors.toList());
+        assertThat(rows, hasSize(6));
+
+        final List<Matcher<? super List<String>>> matchers = new ArrayList<>();
+
+        for (int i = 0; i < 3; i++) {
+            for (String pluginName : List.of("test-plugin", "bootstrap-plugin")) {
+                matchers.add(
+                    contains(
+                        Integer.toString(i),
+                        "node-" + i,
+                        pluginName,
+                        "1.0",
+                        pluginName + " description",
+                        pluginName.contains("bootstrap") ? "bootstrap" : "isolated"
+                    )
+                );
+            }
+        }
+
+        assertThat(rows, containsInAnyOrder(matchers));
+    }
+
+    private Table buildTable(List<PluginInfo> pluginInfo, boolean includeBootstrap) {
+        final RestRequest request = new FakeRestRequest();
+
+        final DiscoveryNodes.Builder builder = DiscoveryNodes.builder();
+        for (int i = 0; i < 3; i++) {
+            builder.add(node(i));
+        }
+
+        final ClusterName clusterName = new ClusterName("test");
+
+        final ClusterState state = ClusterState.builder(clusterName).nodes(builder.build()).build();
+        ClusterStateResponse clusterStateResponse = new ClusterStateResponse(clusterName, state, false);
+
+        final List<NodeInfo> nodeInfos = new ArrayList<>();
+        for (int i = 0; i < 3; i++) {
+            nodeInfos.add(
+                new NodeInfo(
+                    Version.CURRENT,
+                    null,
+                    node(i),
+                    null,
+                    null,
+                    null,
+                    null,
+                    null,
+                    null,
+                    null,
+                    new PluginsAndModules(pluginInfo, List.of()),
+                    null,
+                    null,
+                    null
+                )
+            );
+        }
+
+        NodesInfoResponse nodesInfoResponse = new NodesInfoResponse(clusterName, nodeInfos, List.of());
+
+        return action.buildTable(request, clusterStateResponse, nodesInfoResponse, includeBootstrap);
+    }
+
+    private DiscoveryNode node(final int id) {
+        return new DiscoveryNode("node-" + id, Integer.toString(id), buildNewFakeTransportAddress(), Map.of(), Set.of(), Version.CURRENT);
+    }
+
+    private PluginInfo plugin(String name, PluginType type) {
+        return new PluginInfo(name, name + " description", "1.0", null, null, null, List.of(), false, type, null, false);
+    }
+}