You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2022/01/31 20:17:26 UTC

[GitHub] [druid] paul-rogers commented on a change in pull request #12215: Fixing HttpEntity retry mechanism

paul-rogers commented on a change in pull request #12215:
URL: https://github.com/apache/druid/pull/12215#discussion_r796029679



##########
File path: core/src/main/java/org/apache/druid/data/input/impl/HttpEntity.java
##########
@@ -80,23 +80,30 @@ protected String getPath()
     return t -> t instanceof IOException;
   }
 
-  public static InputStream openInputStream(URI object, String userName, PasswordProvider passwordProvider, long offset)
-      throws IOException
-  {
-    final URLConnection urlConnection = object.toURL().openConnection();
+  private static void addAuthHeader(URLConnection urlConnection, String userName, PasswordProvider passwordProvider){
     if (!Strings.isNullOrEmpty(userName) && passwordProvider != null) {
       String userPass = userName + ":" + passwordProvider.getPassword();
       String basicAuthString = "Basic " + Base64.getEncoder().encodeToString(StringUtils.toUtf8(userPass));
       urlConnection.setRequestProperty("Authorization", basicAuthString);
     }
+  }
+
+  public static InputStream openInputStream(URI object, String userName, PasswordProvider passwordProvider, long offset)
+      throws IOException
+  {
+    final URLConnection urlConnection = object.toURL().openConnection();
+    addAuthHeader(urlConnection, userName, passwordProvider);
     final String acceptRanges = urlConnection.getHeaderField(HttpHeaders.ACCEPT_RANGES);
     final boolean withRanges = "bytes".equalsIgnoreCase(acceptRanges);
     if (withRanges && offset > 0) {
       // Set header for range request.
       // Since we need to set only the start offset, the header is "bytes=<range-start>-".
       // See https://tools.ietf.org/html/rfc7233#section-2.1
-      urlConnection.addRequestProperty(HttpHeaders.RANGE, StringUtils.format("bytes=%d-", offset));
-      return urlConnection.getInputStream();
+      urlConnection.getInputStream().close();
+      final URLConnection newUrlConnection = object.toURL().openConnection();
+      addAuthHeader(newUrlConnection, userName, passwordProvider);
+      newUrlConnection.addRequestProperty(HttpHeaders.RANGE, StringUtils.format("bytes=%d-", offset));

Review comment:
       The above line seems to add the offset as an HTTP header. It is more common to add the offset as a query parameter: `http://server/get/offset=20`. Seems that the code already uses headers. Just pointing out that that is an unusual approach.

##########
File path: core/src/main/java/org/apache/druid/data/input/impl/HttpEntity.java
##########
@@ -80,23 +80,30 @@ protected String getPath()
     return t -> t instanceof IOException;
   }
 
-  public static InputStream openInputStream(URI object, String userName, PasswordProvider passwordProvider, long offset)
-      throws IOException
-  {
-    final URLConnection urlConnection = object.toURL().openConnection();
+  private static void addAuthHeader(URLConnection urlConnection, String userName, PasswordProvider passwordProvider){
     if (!Strings.isNullOrEmpty(userName) && passwordProvider != null) {
       String userPass = userName + ":" + passwordProvider.getPassword();
       String basicAuthString = "Basic " + Base64.getEncoder().encodeToString(StringUtils.toUtf8(userPass));
       urlConnection.setRequestProperty("Authorization", basicAuthString);
     }
+  }
+
+  public static InputStream openInputStream(URI object, String userName, PasswordProvider passwordProvider, long offset)
+      throws IOException
+  {
+    final URLConnection urlConnection = object.toURL().openConnection();
+    addAuthHeader(urlConnection, userName, passwordProvider);
     final String acceptRanges = urlConnection.getHeaderField(HttpHeaders.ACCEPT_RANGES);
     final boolean withRanges = "bytes".equalsIgnoreCase(acceptRanges);
     if (withRanges && offset > 0) {
       // Set header for range request.
       // Since we need to set only the start offset, the header is "bytes=<range-start>-".
       // See https://tools.ietf.org/html/rfc7233#section-2.1
-      urlConnection.addRequestProperty(HttpHeaders.RANGE, StringUtils.format("bytes=%d-", offset));
-      return urlConnection.getInputStream();
+      urlConnection.getInputStream().close();

Review comment:
       Doesn't this end up connecting to the destination server, and immediately closing that connection? Is that wanted? This is not "retrying" as the first connection is not fully valid, and we do no error checking. What do we mean by "retry" here?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org