Consider handling IOException in a manner consistent with other exceptions in CloudConfigFactory.fetchProxyMetadata()
Description
Environment
None
Pull Requests
None
Activity
Show:
Bret McGuire
July 21, 2022 at 8:34 PM
Note that IOException is also thrown by other methods called within fetchProxyMetadata() (Iām looking at you URL.openConnection() and HttpsURLConnection.setRequestMethod()). So something very explicitly targeted at the getInputStream() op probably is the right call here:
@NonNull
protected BufferedReader fetchProxyMetadata(
@NonNull URL metadataServiceUrl, @NonNull SSLContext sslContext) throws IOException {
try {
HttpsURLConnection connection = (HttpsURLConnection) metadataServiceUrl.openConnection();
connection.setSSLSocketFactory(sslContext.getSocketFactory());
connection.setRequestMethod("GET");
connection.setRequestProperty("host", "localhost");
try {
return new BufferedReader(
new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
}
catch (IOException ioe) {
throw new IllegalStateException(
"Unable to read data from cloud metadata service. Please make sure your cluster is not parked or terminated",
ioe);
}
} catch (ConnectException e) {
throw new IllegalStateException(
"Unable to connect to cloud metadata service. Please make sure your cluster is not parked or terminated",
e);
} catch (UnknownHostException e) {
throw new IllegalStateException(
"Unable to resolve host for cloud metadata service. Please make sure your cluster is not terminated",
e);
}
}
Details
Details
Assignee
Unassigned
UnassignedReporter
Bret McGuire
Bret McGuirePriority
Created July 21, 2022 at 8:30 PM
Updated November 30, 2023 at 5:14 AM
The following exception was reported for this method:
Caused by: java.io.IOException: Server returned HTTP response code: 401 for URL: https://fd059d2c-eeb8-4471-a2fb-70725ce450ee-europe-west1.db.astra.datastax.com:29080/metadata at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1924) at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1520) at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:250) at com.datastax.oss.driver.internal.core.config.cloud.CloudConfigFactory.fetchProxyMetadata(CloudConfigFactory.java:232) at com.datastax.oss.driver.internal.core.config.cloud.CloudConfigFactory.createCloudConfig(CloudConfigFactory.java:133) at com.datastax.oss.driver.api.core.session.SessionBuilder.buildDefaultSessionAsync(SessionBuilder.java:876) at com.datastax.oss.driver.api.core.session.SessionBuilder.buildAsync(SessionBuilder.java:817)
Code in question can be found here. Note specifically that HttpsURLConnection.getInputStream() can thrown an IOException āif an I/O error occurs while creating the input stream.ā In this case the Astra database in question was hibernated⦠thatās apparently what led to the 401 response, which in turn led to the IOException.
Note that IOException is declared to be thrown from fetchProxyMetadata() while other exceptions which are obviously interpreted as (likely) caused by hibernated databases are converted into IllegalStateExceptions with appropriate messaging. Perhaps it makes sense to take a similar path with IOExceptions, at least around the getInputStream() call specifically.