forked from opensearch-project/performance-analyzer-rca
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle Reader thread termination gracefully
Signed-off-by: Khushboo Rajput <[email protected]>
- Loading branch information
Showing
7 changed files
with
210 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/org/opensearch/performanceanalyzer/ESLocalhostConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.performanceanalyzer; | ||
|
||
|
||
import io.netty.handler.codec.http.HttpMethod; | ||
import java.io.DataOutputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
public class ESLocalhostConnection { | ||
|
||
private static final Logger LOG = LogManager.getLogger(ESLocalhostConnection.class); | ||
|
||
private static final int TIMEOUT_MILLIS = 30000; | ||
|
||
public static int makeHttpRequest(String path, HttpMethod httpMethod, String requestBody) { | ||
HttpURLConnection connection = null; | ||
try { | ||
connection = createHTTPConnection(path, httpMethod); | ||
DataOutputStream stream = new DataOutputStream(connection.getOutputStream()); | ||
stream.writeBytes(requestBody); | ||
stream.flush(); | ||
stream.close(); | ||
return connection.getResponseCode(); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Request failed: " + e.getMessage(), e); | ||
} finally { | ||
if (connection != null) { | ||
connection.disconnect(); | ||
} | ||
} | ||
} | ||
|
||
public static HttpURLConnection createHTTPConnection(String path, HttpMethod httpMethod) { | ||
try { | ||
String endPoint = "https://localhost:9200" + path; | ||
URL endpointUrl = new URL(endPoint); | ||
HttpURLConnection connection = (HttpURLConnection) endpointUrl.openConnection(); | ||
connection.setRequestMethod(httpMethod.toString()); | ||
|
||
connection.setConnectTimeout(TIMEOUT_MILLIS); | ||
connection.setReadTimeout(TIMEOUT_MILLIS); | ||
connection.setUseCaches(false); | ||
connection.setDoOutput(true); | ||
return connection; | ||
} catch (Exception e) { | ||
throw new RuntimeException( | ||
"Failed to create OpenSearch Connection: " + e.getMessage(), e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
false |