Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Fix failures in ResourceHeatMapGraphTest #321

Merged
merged 2 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ public void tearDown() {
// Unset all SSL settings
if (oldBindHost != null) {
PluginSettings.instance().overrideProperty(PerformanceAnalyzerWebServer.WEBSERVICE_BIND_HOST_NAME, oldBindHost);
} else {
PluginSettings.instance().overrideProperty(PerformanceAnalyzerWebServer.WEBSERVICE_BIND_HOST_NAME, "localhost");
Copy link
Contributor

@vigyasharma vigyasharma Jul 30, 2020

Choose a reason for hiding this comment

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

Are we trying to restore oldBindHost to some default value here? If so, should it be done in setUp() ?
Same for others.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah wait, this is for across tests. Got it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's take oldBindHost as an example. If oldBindHost is null then after this test class ran, we would previously keep the value that PerformanceAnalyzerWebServer changed WEBSERVICE_BIND_HOST_NAME to. This could lead to other tests failing.

The reason this runs as part of a tearDown is to make sure that everything is reset after the test class finishes running.

}
if (oldPort != null) {
PluginSettings.instance().overrideProperty(PerformanceAnalyzerWebServer.WEBSERVICE_PORT_CONF_NAME, oldPort);
} else {
PluginSettings.instance().overrideProperty(PerformanceAnalyzerWebServer.WEBSERVICE_PORT_CONF_NAME, "9600");
}
if (oldCertificateFilePath != null) {
PluginSettings.instance().overrideProperty(CertificateUtils.CERTIFICATE_FILE_PATH, oldCertificateFilePath);
Expand All @@ -82,6 +86,8 @@ public void tearDown() {
}
if (oldTrustedCasFilePath != null) {
PluginSettings.instance().overrideProperty(CertificateUtils.TRUSTED_CAS_FILE_PATH, oldTrustedCasFilePath);
} else {
PluginSettings.instance().overrideProperty(CertificateUtils.TRUSTED_CAS_FILE_PATH, "");
}
if (oldClientCertificateFilePath != null) {
PluginSettings.instance().overrideProperty(CertificateUtils.CLIENT_CERTIFICATE_FILE_PATH, oldClientCertificateFilePath);
Expand All @@ -91,6 +97,8 @@ public void tearDown() {
}
if (oldClientTrustedCasFilePath != null) {
PluginSettings.instance().overrideProperty(CertificateUtils.CLIENT_TRUSTED_CAS_FILE_PATH, oldClientTrustedCasFilePath);
} else {
PluginSettings.instance().overrideProperty(CertificateUtils.CLIENT_TRUSTED_CAS_FILE_PATH, "");
}
PluginSettings.instance().setHttpsEnabled(oldHttpsEnabled);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.AllMetrics;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.net.GRPCConnectionManager;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.rca.RcaTestHelper;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.rca.exceptions.MalformedConfig;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.rca.framework.api.AnalysisGraph;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.rca.framework.api.Metric;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.rca.framework.api.metrics.CPU_Utilization;
Expand Down Expand Up @@ -67,15 +66,11 @@
import com.google.gson.JsonParser;
import com.sun.net.httpserver.HttpServer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
Expand All @@ -88,7 +83,6 @@
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

public class ResourceHeatMapGraphTest {
Expand All @@ -109,25 +103,9 @@ public class ResourceHeatMapGraphTest {
private static AtomicReference<ExecutorService> networkThreadPoolReference;

@BeforeClass
public static void init() {
try {
persistable = PersistenceFactory.create(rcaConf);
} catch (MalformedConfig malformedConfig) {
malformedConfig.printStackTrace();
Assert.fail();
} catch (SQLException e) {
e.printStackTrace();
Assert.fail();
} catch (IOException e) {
e.printStackTrace();
Assert.fail();
}
try {
reader = new SQLiteReader(sqliteFile.toString());
} catch (SQLException e) {
e.printStackTrace();
Assert.fail();
}
public static void init() throws Exception {
persistable = PersistenceFactory.create(rcaConf);
reader = new SQLiteReader(sqliteFile.toString());

AllMetrics.NodeRole nodeRole2 = AllMetrics.NodeRole.ELECTED_MASTER;
AppContext appContext = RcaTestHelper.setMyIp("192.168.0.2", nodeRole2);
Expand All @@ -151,6 +129,12 @@ public static void shutdown() {
clientServers.getHttpServer().stop(0);
clientServers.getNetServer().stop();
clientServers.getNetClient().stop();

try {
Thread.sleep(1000);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this sleep needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It isn't strictly necessary, I'm mimicking similar logic from RcaControllerTest which does this sleep after stopping its clients and servers.

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Maybe it’s a good idea to abstract the shutdown logic with sleep into its own method so that we don’t have to carry the sleep change wherever we use shutdown?

} catch (InterruptedException ie) {
ie.printStackTrace();
}
}

private static class AnalysisGraphX extends ElasticSearchAnalysisGraph {
Expand Down Expand Up @@ -195,7 +179,7 @@ private List<ConnectedComponent> createAndExecuteRcaGraph(AppContext appContext)
return connectedComponents;
}

private String makeRestRequest(final String[] params) {
private String makeRestRequest(final String[] params) throws Exception {
// The params are key/value pairs and therefore there should be even numbers of them.
Assert.assertEquals(0, params.length % 2);
StringBuilder queryString = new StringBuilder();
Expand All @@ -209,63 +193,31 @@ private String makeRestRequest(final String[] params) {
uri.append("?").append(queryString);


URL url = null;
try {
url = new URL(uri.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
Assert.fail();
}
URL url = new URL(uri.toString());

String response = "";
HttpURLConnection connection = null;

try {
connection = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
e.printStackTrace();
Assert.fail();
}

try {
connection.setRequestMethod("GET");
} catch (ProtocolException e) {
e.printStackTrace();
connection.disconnect();
Assert.fail();
}

try {
int status = connection.getResponseCode();
if (status != 200) {
List<String> ret =
new BufferedReader(new InputStreamReader(connection.getErrorStream())).lines().collect(Collectors.toList());
throw new IllegalStateException(ret.toString());
}
} catch (IOException e) {
e.printStackTrace();
connection.disconnect();
Assert.fail();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
int status = connection.getResponseCode();
if (status != 200) {
List<String> ret =
new BufferedReader(new InputStreamReader(connection.getErrorStream())).lines().collect(Collectors.toList());
throw new IllegalStateException(ret.toString());
}

try (BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()))) {
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
response = content.toString();
} catch (IOException e) {
e.printStackTrace();
connection.disconnect();
Assert.fail();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
response = content.toString();
in.close();
return response;
}

@Test
public void clusterTemperatureProfile() {
public void clusterTemperatureProfile() throws Exception {
AppContext appContext = RcaTestHelper.setMyIp("192.168.0.2", AllMetrics.NodeRole.ELECTED_MASTER);

List<ConnectedComponent> connectedComponents = createAndExecuteRcaGraph(appContext);
Expand Down Expand Up @@ -303,7 +255,7 @@ public void clusterTemperatureProfile() {
}

@Test
public void fullNodeTemperatureProfile() {
public void fullNodeTemperatureProfile() throws Exception {
AppContext appContext = RcaTestHelper.setMyIp("192.168.0.3", AllMetrics.NodeRole.DATA);
createAndExecuteRcaGraph(appContext);
verifyFullNodeTemperatureProfile(makeRestRequest(
Expand Down Expand Up @@ -1056,7 +1008,7 @@ public void construct() {
}

@Test
public void testHotShardClusterApiResponse() {
public void testHotShardClusterApiResponse() throws Exception {
AnalysisGraph analysisGraph = new AnalysisGraphHotShard();
List<ConnectedComponent> connectedComponents =
RcaUtil.getAnalysisGraphComponents(analysisGraph);
Expand Down Expand Up @@ -1120,45 +1072,36 @@ public void testHotShardClusterApiResponse() {
rcaSchedulerTaskMaster.run();

URL url = null;
try {
url = new URL("http://localhost:9600" + Util.RCA_QUERY_URL + "?name=" + HotShardClusterRca.RCA_TABLE_NAME);
} catch (MalformedURLException e) {
e.printStackTrace();
Assert.fail();
}
url = new URL("http://localhost:9600" + Util.RCA_QUERY_URL + "?name="
+ HotShardClusterRca.RCA_TABLE_NAME);

try {
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");

int status = con.getResponseCode();
System.out.println("Response status: " + status);
try (BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()))) {
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
final String hotShardClusterRcaName = HotShardClusterRca.RCA_TABLE_NAME;
final String hotClusterSummaryName = HotClusterSummary.HOT_CLUSTER_SUMMARY_TABLE;
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");

JsonParser parser = new JsonParser();
JsonElement jsonElement = parser.parse(content.toString());
JsonObject hotShardClusterRca =
jsonElement.getAsJsonObject().get(hotShardClusterRcaName).getAsJsonArray().get(0).getAsJsonObject();
int status = con.getResponseCode();
System.out.println("Response status: " + status);
try (BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()))) {
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
final String hotShardClusterRcaName = HotShardClusterRca.RCA_TABLE_NAME;
final String hotClusterSummaryName = HotClusterSummary.HOT_CLUSTER_SUMMARY_TABLE;

JsonParser parser = new JsonParser();
JsonElement jsonElement = parser.parse(content.toString());
JsonObject hotShardClusterRca =
jsonElement.getAsJsonObject().get(hotShardClusterRcaName).getAsJsonArray().get(0).getAsJsonObject();

Assert.assertEquals(hotShardClusterRcaName, hotShardClusterRca.get("rca_name").getAsString());
Assert.assertEquals("unhealthy", hotShardClusterRca.get("state").getAsString());
Assert.assertEquals(hotShardClusterRcaName, hotShardClusterRca.get("rca_name").getAsString());
Assert.assertEquals("unhealthy", hotShardClusterRca.get("state").getAsString());

JsonObject hotClusterSummary =
hotShardClusterRca.get(hotClusterSummaryName).getAsJsonArray().get(0).getAsJsonObject();
Assert.assertEquals(1, hotClusterSummary.get("number_of_unhealthy_nodes").getAsInt());
}
con.disconnect();
} catch (IOException e) {
e.printStackTrace();
Assert.fail();
JsonObject hotClusterSummary =
hotShardClusterRca.get(hotClusterSummaryName).getAsJsonArray().get(0).getAsJsonObject();
Assert.assertEquals(1, hotClusterSummary.get("number_of_unhealthy_nodes").getAsInt());
}
con.disconnect();
}
}