Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for http response compression #543

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## [Unreleased]
### Added

- Added basic support for HTTP compression when reading from OpenSearch ([#543](https://github.com/opensearch-project/opensearch-hadoop/pull/543))
-
### Changed

### Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
Expand Down Expand Up @@ -152,12 +154,14 @@ public class CommonsHttpTransport implements Transport, StatsAware {
private static class ResponseInputStream extends DelegatingInputStream implements ReusableInputStream {

private final HttpMethod method;
private final boolean compressed;
private final boolean reusable;

public ResponseInputStream(HttpMethod http) throws IOException {
super(http.getResponseBodyAsStream());
super(getStream(http));
this.method = http;
reusable = (delegate() instanceof ByteArrayInputStream);
this.compressed = hasCompressedResponseBody(http);
this.reusable = (http.getResponseBodyAsStream() instanceof ByteArrayInputStream);
}

@Override
Expand All @@ -173,7 +177,7 @@ public boolean equals(Object obj) {
@Override
public InputStream copy() {
try {
return (reusable ? method.getResponseBodyAsStream() : null);
return (reusable ? getStream(method, compressed) : null);
} catch (IOException ex) {
throw new OpenSearchHadoopIllegalStateException(ex);
}
Expand All @@ -190,6 +194,23 @@ public void close() throws IOException {
}
method.releaseConnection();
}

static private InputStream getStream(HttpMethod http) throws IOException {
return getStream(http, hasCompressedResponseBody(http));
}

static private InputStream getStream(HttpMethod http, boolean compressed) throws IOException {
InputStream responseBodyStream = http.getResponseBodyAsStream();
if (compressed) {
responseBodyStream = new GZIPInputStream(responseBodyStream);
}
return responseBodyStream;
}

static private boolean hasCompressedResponseBody(HttpMethod http) {
Header contentEncoding = http.getResponseHeader("Content-Encoding");
return (contentEncoding != null) && Objects.equals(contentEncoding.getValue(), "gzip");
}
}

private class SocketTrackingConnectionManager extends SimpleHttpConnectionManager {
Expand Down Expand Up @@ -692,6 +713,11 @@ public Response execute(Request request) throws IOException {
entityMethod.setContentChunked(false);
}

if (settings.getHttpCompression()) {
log.debug("Requesting compressed response");
http.setRequestHeader("Accept-Encoding", "gzip");
}

headers.applyTo(http);

// We don't want a token added from a proxy user to collide with the
Expand Down
Loading