Skip to content

Commit

Permalink
Merge pull request elastic#15423 from brwe/fix-bwc-nodeinfo
Browse files Browse the repository at this point in the history
fix bwc for NodeInfo, plugins and modules streaming was changed only …
  • Loading branch information
brwe committed Dec 15, 2015
2 parents d7e595b + 119d3a4 commit a1e6b4b
Show file tree
Hide file tree
Showing 4 changed files with 253 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.action.admin.cluster.node.info;

import org.elasticsearch.Version;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
Expand Down Expand Up @@ -57,7 +58,7 @@ public int compare(PluginInfo p1, PluginInfo p2) {
});
return plugins;
}

/**
* Returns an ordered list based on modules name
*/
Expand All @@ -75,7 +76,7 @@ public int compare(PluginInfo p1, PluginInfo p2) {
public void addPlugin(PluginInfo info) {
plugins.add(info);
}

public void addModule(PluginInfo info) {
modules.add(info);
}
Expand All @@ -89,21 +90,34 @@ public void readFrom(StreamInput in) throws IOException {
for (int i = 0; i < plugins_size; i++) {
plugins.add(PluginInfo.readFromStream(in));
}
int modules_size = in.readInt();
for (int i = 0; i < modules_size; i++) {
modules.add(PluginInfo.readFromStream(in));
if (in.getVersion().onOrAfter(Version.V_2_2_0)) {
int modules_size = in.readInt();
for (int i = 0; i < modules_size; i++) {
modules.add(PluginInfo.readFromStream(in));
}
}
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeInt(plugins.size());
for (PluginInfo plugin : getPluginInfos()) {
plugin.writeTo(out);
if (out.getVersion().before(Version.V_2_2_0)) {
out.writeInt(plugins.size() + modules.size());
for (PluginInfo plugin : getPluginInfos()) {
plugin.writeTo(out);
}
for (PluginInfo module : getModuleInfos()) {
module.writeTo(out);
}
}
out.writeInt(modules.size());
for (PluginInfo module : getModuleInfos()) {
module.writeTo(out);
else {
out.writeInt(plugins.size());
for (PluginInfo plugin : getPluginInfos()) {
plugin.writeTo(out);
}
out.writeInt(modules.size());
for (PluginInfo module : getModuleInfos()) {
module.writeTo(out);
}
}
}

Expand Down
34 changes: 34 additions & 0 deletions core/src/main/java/org/elasticsearch/monitor/os/DummyOsInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.monitor.os;

public class DummyOsInfo extends OsInfo {

DummyOsInfo() {
refreshInterval = 0;
availableProcessors = 0;
allocatedProcessors = 0;
name = "dummy_name";
arch = "dummy_arch";
version = "dummy_version";
}

public static final DummyOsInfo INSTANCE = new DummyOsInfo();
}
28 changes: 28 additions & 0 deletions core/src/main/java/org/elasticsearch/plugins/DummyPluginInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.plugins;

public class DummyPluginInfo extends PluginInfo {

private DummyPluginInfo(String name, String description, boolean site, String version, boolean jvm, String classname, boolean isolated) {
super(name, description, site, version, jvm, classname, isolated);
}

public static final DummyPluginInfo INSTANCE = new DummyPluginInfo("dummy_plugin_name", "dummy plugin description", true, "dummy_plugin_version", true, "DummyPluginName", true);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* 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.nodesinfo;

import com.google.common.collect.ImmutableMap;
import org.elasticsearch.Build;
import org.elasticsearch.Version;
import org.elasticsearch.action.admin.cluster.node.info.NodeInfo;
import org.elasticsearch.action.admin.cluster.node.info.PluginsAndModules;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.BoundTransportAddress;
import org.elasticsearch.common.transport.DummyTransportAddress;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.http.HttpInfo;
import org.elasticsearch.monitor.jvm.JvmInfo;
import org.elasticsearch.monitor.os.DummyOsInfo;
import org.elasticsearch.monitor.os.OsInfo;
import org.elasticsearch.monitor.process.ProcessInfo;
import org.elasticsearch.plugins.DummyPluginInfo;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.VersionUtils;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.threadpool.ThreadPoolInfo;
import org.elasticsearch.transport.TransportInfo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.hamcrest.core.IsEqual.equalTo;

/**
*
*/
public class NodeInfoStreamingTests extends ESTestCase {

public void testNodeInfoStreaming() throws IOException {
NodeInfo nodeInfo = createNodeInfo();

// test version on or after V_2_2_0
Version version = VersionUtils.randomVersionBetween(random(), Version.V_2_2_0, Version.CURRENT);
BytesStreamOutput out = new BytesStreamOutput();
out.setVersion(version);
nodeInfo.writeTo(out);
out.close();
StreamInput in = StreamInput.wrap(out.bytes());
in.setVersion(version);
NodeInfo readNodeInfo = NodeInfo.readNodeInfo(in);
assertExpectedUnchanged(nodeInfo, readNodeInfo);

comparePluginsAndModulesOnOrAfter2_2_0(nodeInfo, readNodeInfo);

// test version before V_2_2_0
version = VersionUtils.randomVersionBetween(random(), Version.V_2_0_0, Version.V_2_1_1);
out = new BytesStreamOutput();
out.setVersion(version);
nodeInfo.writeTo(out);
out.close();
in = StreamInput.wrap(out.bytes());
in.setVersion(version);
readNodeInfo = NodeInfo.readNodeInfo(in);
assertExpectedUnchanged(nodeInfo, readNodeInfo);

comparePluginsAndModulesBefore2_2_0(nodeInfo, readNodeInfo);
}

// checks all properties that are expected to be unchanged. Once we start changing them between versions this method has to be changed as well
private void assertExpectedUnchanged(NodeInfo nodeInfo, NodeInfo readNodeInfo) throws IOException {
assertThat(nodeInfo.getBuild().toString(), equalTo(readNodeInfo.getBuild().toString()));
assertThat(nodeInfo.getHostname(), equalTo(readNodeInfo.getHostname()));
assertThat(nodeInfo.getVersion(), equalTo(readNodeInfo.getVersion()));
assertThat(nodeInfo.getServiceAttributes().size(), equalTo(readNodeInfo.getServiceAttributes().size()));
for (Map.Entry<String, String> entry : nodeInfo.getServiceAttributes().entrySet()) {
assertNotNull(readNodeInfo.getServiceAttributes().get(entry.getKey()));
assertThat(readNodeInfo.getServiceAttributes().get(entry.getKey()), equalTo(entry.getValue()));
}
compareJsonOutput(nodeInfo.getHttp(), readNodeInfo.getHttp());
compareJsonOutput(nodeInfo.getJvm(), readNodeInfo.getJvm());
// see issue https://github.com/elastic/elasticsearch/issues/15422
// compareJsonOutput(nodeInfo.getOs(), readNodeInfo.getOs());
compareJsonOutput(nodeInfo.getProcess(), readNodeInfo.getProcess());
compareJsonOutput(nodeInfo.getSettings(), readNodeInfo.getSettings());
compareJsonOutput(nodeInfo.getThreadPool(), readNodeInfo.getThreadPool());
compareJsonOutput(nodeInfo.getTransport(), readNodeInfo.getTransport());
compareJsonOutput(nodeInfo.getNode(), readNodeInfo.getNode());
}

private void comparePluginsAndModulesBefore2_2_0(NodeInfo nodeInfo, NodeInfo readNodeInfo) throws IOException {
assertThat(readNodeInfo.getPlugins().getPluginInfos().size(), equalTo(nodeInfo.getPlugins().getModuleInfos().size() + nodeInfo.getPlugins().getPluginInfos().size()));
for (int i = 0; i < nodeInfo.getPlugins().getPluginInfos().size(); i++) {
compareJsonOutput(readNodeInfo.getPlugins().getPluginInfos().get(i), nodeInfo.getPlugins().getPluginInfos().get(i));
}
for (int i = 0; i < nodeInfo.getPlugins().getPluginInfos().size(); i++) {
compareJsonOutput(readNodeInfo.getPlugins().getPluginInfos().get(i + nodeInfo.getPlugins().getPluginInfos().size()), nodeInfo.getPlugins().getModuleInfos().get(i));
}
}

private void comparePluginsAndModulesOnOrAfter2_2_0(NodeInfo nodeInfo, NodeInfo readNodeInfo) throws IOException {
ToXContent.Params params = ToXContent.EMPTY_PARAMS;
XContentBuilder pluginsAndModules = jsonBuilder();
pluginsAndModules.startObject();
nodeInfo.getPlugins().toXContent(pluginsAndModules, params);
pluginsAndModules.endObject();
XContentBuilder readPluginsAndModules = jsonBuilder();
readPluginsAndModules.startObject();
readNodeInfo.getPlugins().toXContent(readPluginsAndModules, params);
readPluginsAndModules.endObject();
assertThat(pluginsAndModules.string(), equalTo(readPluginsAndModules.string()));
}

private void compareJsonOutput(ToXContent param1, ToXContent param2) throws IOException {
ToXContent.Params params = ToXContent.EMPTY_PARAMS;
XContentBuilder param1Builder = jsonBuilder();
XContentBuilder param2Builder = jsonBuilder();
param1.toXContent(param1Builder, params);
param2.toXContent(param2Builder, params);
assertThat(param1Builder.string(), equalTo(param2Builder.string()));
}

private NodeInfo createNodeInfo() {
Build build = Build.CURRENT;
DiscoveryNode node = new DiscoveryNode("test_node", DummyTransportAddress.INSTANCE, VersionUtils.randomVersion(random()));
ImmutableMap<String, String> serviceAttributes = ImmutableMap.of("test", "attribute");
Settings settings = Settings.builder().put("test", "setting").build();
OsInfo osInfo = DummyOsInfo.INSTANCE;
ProcessInfo process = new ProcessInfo(randomInt(), randomBoolean());
JvmInfo jvm = JvmInfo.jvmInfo();
List<ThreadPool.Info> threadPoolInfos = new ArrayList<>();
threadPoolInfos.add(new ThreadPool.Info("test_threadpool", ThreadPool.ThreadPoolType.FIXED, 5));
ThreadPoolInfo threadPoolInfo = new ThreadPoolInfo(threadPoolInfos);
Map<String, BoundTransportAddress> profileAddresses = new HashMap<>();
BoundTransportAddress dummyBoundTransportAddress = new BoundTransportAddress(new TransportAddress[]{DummyTransportAddress.INSTANCE}, DummyTransportAddress.INSTANCE);
profileAddresses.put("test_address", dummyBoundTransportAddress);
TransportInfo transport = new TransportInfo(dummyBoundTransportAddress, profileAddresses);
HttpInfo htttpInfo = new HttpInfo(dummyBoundTransportAddress, randomLong());
PluginsAndModules plugins = new PluginsAndModules();
plugins.addModule(DummyPluginInfo.INSTANCE);
plugins.addPlugin(DummyPluginInfo.INSTANCE);
return new NodeInfo(VersionUtils.randomVersion(random()), build, node, serviceAttributes, settings, osInfo, process, jvm, threadPoolInfo, transport, htttpInfo, plugins);
}
}

0 comments on commit a1e6b4b

Please sign in to comment.