Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cluster stats should not render empty http/transport types #23735

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -25,6 +25,7 @@
import org.elasticsearch.action.admin.cluster.node.info.NodeInfo;
import org.elasticsearch.action.admin.cluster.node.stats.NodeStats;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.network.NetworkModule;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
Expand Down Expand Up @@ -73,7 +74,8 @@ public class ClusterStatsNodes implements ToXContent {
this.plugins.addAll(nodeResponse.nodeInfo().getPlugins().getPluginInfos());

// now do the stats that should be deduped by hardware (implemented by ip deduping)
TransportAddress publishAddress = nodeResponse.nodeInfo().getTransport().address().publishAddress();
TransportAddress publishAddress =
nodeResponse.nodeInfo().getTransport().address().publishAddress();
final InetAddress inetAddress = publishAddress.address().getAddress();
if (!seenAddresses.add(inetAddress)) {
continue;
Expand Down Expand Up @@ -209,7 +211,8 @@ static final class Fields {
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
public XContentBuilder toXContent(XContentBuilder builder, Params params)
throws IOException {
builder.field(Fields.TOTAL, total);
for (Map.Entry<String, Integer> entry : roles.entrySet()) {
builder.field(entry.getKey(), entry.getValue());
Expand Down Expand Up @@ -280,7 +283,8 @@ static final class Fields {
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
public XContentBuilder toXContent(XContentBuilder builder, Params params)
throws IOException {
builder.field(Fields.AVAILABLE_PROCESSORS, availableProcessors);
builder.field(Fields.ALLOCATED_PROCESSORS, allocatedProcessors);
builder.startArray(Fields.NAMES);
Expand Down Expand Up @@ -326,7 +330,8 @@ private ProcessStats(List<NodeStats> nodeStatsList) {
// fd can be -1 if not supported on platform
totalOpenFileDescriptors += fd;
}
// we still do min max calc on -1, so we'll have an indication of it not being supported on one of the nodes.
// we still do min max calc on -1, so we'll have an indication
// of it not being supported on one of the nodes.
minOpenFileDescriptors = Math.min(minOpenFileDescriptors, fd);
maxOpenFileDescriptors = Math.max(maxOpenFileDescriptors, fd);
}
Expand Down Expand Up @@ -375,7 +380,8 @@ static final class Fields {
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
public XContentBuilder toXContent(XContentBuilder builder, Params params)
throws IOException {
builder.startObject(Fields.CPU).field(Fields.PERCENT, cpuPercent).endObject();
if (count > 0) {
builder.startObject(Fields.OPEN_FILE_DESCRIPTORS);
Expand Down Expand Up @@ -479,7 +485,8 @@ static final class Fields {
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
public XContentBuilder toXContent(XContentBuilder builder, Params params)
throws IOException {
builder.timeValueField(Fields.MAX_UPTIME_IN_MILLIS, Fields.MAX_UPTIME, maxUptime);
builder.startArray(Fields.VERSIONS);
for (ObjectIntCursor<JvmVersion> v : versions) {
Expand Down Expand Up @@ -540,17 +547,21 @@ static class NetworkTypes implements ToXContent {
private final Map<String, AtomicInteger> transportTypes;
private final Map<String, AtomicInteger> httpTypes;

private NetworkTypes(final List<NodeInfo> nodeInfos) {
NetworkTypes(final List<NodeInfo> nodeInfos) {
final Map<String, AtomicInteger> transportTypes = new HashMap<>();
final Map<String, AtomicInteger> httpTypes = new HashMap<>();
for (final NodeInfo nodeInfo : nodeInfos) {
final Settings settings = nodeInfo.getSettings();
final String transportType =
settings.get(NetworkModule.TRANSPORT_TYPE_KEY, NetworkModule.TRANSPORT_DEFAULT_TYPE_SETTING.get(settings));
settings.get(NetworkModule.TRANSPORT_TYPE_KEY,
NetworkModule.TRANSPORT_DEFAULT_TYPE_SETTING.get(settings));
final String httpType =
settings.get(NetworkModule.HTTP_TYPE_KEY, NetworkModule.HTTP_DEFAULT_TYPE_SETTING.get(settings));
transportTypes.computeIfAbsent(transportType, k -> new AtomicInteger()).incrementAndGet();
httpTypes.computeIfAbsent(httpType, k -> new AtomicInteger()).incrementAndGet();
settings.get(NetworkModule.HTTP_TYPE_KEY,
NetworkModule.HTTP_DEFAULT_TYPE_SETTING.get(settings));
transportTypes.computeIfAbsent(transportType,
k -> new AtomicInteger()).incrementAndGet();
httpTypes.computeIfAbsent(httpType,
k -> new AtomicInteger()).incrementAndGet();
}
this.transportTypes = Collections.unmodifiableMap(transportTypes);
this.httpTypes = Collections.unmodifiableMap(httpTypes);
Expand All @@ -560,12 +571,16 @@ private NetworkTypes(final List<NodeInfo> nodeInfos) {
public XContentBuilder toXContent(final XContentBuilder builder, final Params params) throws IOException {
builder.startObject("transport_types");
for (final Map.Entry<String, AtomicInteger> entry : transportTypes.entrySet()) {
builder.field(entry.getKey(), entry.getValue().get());
if (Strings.hasText(entry.getKey())) {
Copy link
Contributor

@spinscale spinscale Mar 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the check be done here, or should such an element never get added to the map at the first place?

builder.field(entry.getKey(), entry.getValue().get());
}
}
builder.endObject();
builder.startObject("http_types");
for (final Map.Entry<String, AtomicInteger> entry : httpTypes.entrySet()) {
builder.field(entry.getKey(), entry.getValue().get());
if (Strings.hasText(entry.getKey())) {
builder.field(entry.getKey(), entry.getValue().get());
}
}
builder.endObject();
return builder;
Expand Down
@@ -0,0 +1,76 @@
/*
* 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.action.admin.cluster.stats;

import org.elasticsearch.action.admin.cluster.node.info.NodeInfo;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.network.NetworkModule;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.test.ESTestCase;

import java.util.Arrays;
import java.util.List;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.elasticsearch.common.xcontent.XContentHelper.toXContent;

public class ClusterStatsNodesTests extends ESTestCase {

/**
* Test that empty transport/http types are not printed out as part
* of the cluster stats xcontent output.
*/
public void testNetworkTypesToXContent() throws Exception {
ClusterStatsNodes.NetworkTypes stats = new ClusterStatsNodes.NetworkTypes(emptyList());
assertEquals("{\"transport_types\":{},\"http_types\":{}}",
toXContent(stats, XContentType.JSON, randomBoolean()).utf8ToString());

List<NodeInfo> nodeInfos = singletonList(createNodeInfo("node_0", null, null));
stats = new ClusterStatsNodes.NetworkTypes(nodeInfos);
assertEquals("{\"transport_types\":{},\"http_types\":{}}",
toXContent(stats, XContentType.JSON, randomBoolean()).utf8ToString());

nodeInfos = Arrays.asList(createNodeInfo("node_1", "", ""),
createNodeInfo("node_2", "custom", "custom"),
createNodeInfo("node_3", null, "custom"));
stats = new ClusterStatsNodes.NetworkTypes(nodeInfos);
assertEquals("{"
+ "\"transport_types\":{\"custom\":1},"
+ "\"http_types\":{\"custom\":2}"
+ "}", toXContent(stats, XContentType.JSON, randomBoolean()).utf8ToString());
}

private static NodeInfo createNodeInfo(String nodeId, String transportType, String httpType) {
Settings.Builder settings = Settings.builder();
if (transportType != null) {
settings.put(randomFrom(NetworkModule.TRANSPORT_TYPE_KEY,
NetworkModule.TRANSPORT_TYPE_DEFAULT_KEY), transportType);
}
if (httpType != null) {
settings.put(randomFrom(NetworkModule.HTTP_TYPE_KEY,
NetworkModule.HTTP_TYPE_DEFAULT_KEY), httpType);
}
return new NodeInfo(null, null,
new DiscoveryNode(nodeId, buildNewFakeTransportAddress(), null),
settings.build(), null, null, null, null, null, null, null, null, null);
}
}