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

Don't over-allocate in HeapBufferedAsyncEntityConsumer in order to consume the response #9993

Merged
merged 1 commit into from
Sep 12, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Fixed
- Fix 'org.apache.hc.core5.http.ParseException: Invalid protocol version' under JDK 16+ ([#4827](https://github.com/opensearch-project/OpenSearch/pull/4827))
- Fix compression support for h2c protocol ([#4944](https://github.com/opensearch-project/OpenSearch/pull/4944))
- Don't over-allocate in HeapBufferedAsyncEntityConsumer in order to consume the response ([#9993](https://github.com/opensearch-project/OpenSearch/pull/9993))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,29 @@
return;
}

int len = src.limit();
if (len < 0) {
len = 4096;

Check warning on line 91 in client/rest/src/main/java/org/opensearch/client/nio/HeapBufferedAsyncEntityConsumer.java

View check run for this annotation

Codecov / codecov/patch

client/rest/src/main/java/org/opensearch/client/nio/HeapBufferedAsyncEntityConsumer.java#L91

Added line #L91 was not covered by tests
} else if (len > bufferLimitBytes) {
throw new ContentTooLongException(
"entity content is too long [" + len + "] for the configured buffer limit [" + bufferLimitBytes + "]"
);
}

ByteArrayBuffer buffer = bufferRef.get();
if (buffer == null) {
buffer = new ByteArrayBuffer(bufferLimitBytes);
buffer = new ByteArrayBuffer(len);
if (bufferRef.compareAndSet(null, buffer) == false) {
buffer = bufferRef.get();
}
}

int len = src.limit();
if (buffer.length() + len > bufferLimitBytes) {
throw new ContentTooLongException(
"entity content is too long [" + len + "] for the configured buffer limit [" + bufferLimitBytes + "]"
);
}
reta marked this conversation as resolved.
Show resolved Hide resolved

if (len < 0) {
len = 4096;
}

if (src.hasArray()) {
buffer.append(src.array(), src.arrayOffset() + src.position(), src.remaining());
} else {
Expand Down Expand Up @@ -136,4 +140,12 @@
buffer = null;
}
}

/**
* Gets current byte buffer instance
* @return byte buffer instance
*/
ByteArrayBuffer getBuffer() {
return bufferRef.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.client.nio;

import org.apache.hc.core5.http.ContentTooLongException;
import org.opensearch.client.RestClientTestCase;
import org.junit.After;
import org.junit.Before;

import java.io.IOException;
import java.nio.ByteBuffer;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertThrows;

public class HeapBufferedAsyncEntityConsumerTests extends RestClientTestCase {
private static final int BUFFER_LIMIT = 100 * 1024 * 1024 /* 100Mb */;
private HeapBufferedAsyncEntityConsumer consumer;

@Before
public void setUp() {
consumer = new HeapBufferedAsyncEntityConsumer(BUFFER_LIMIT);
}

@After
public void tearDown() {
consumer.releaseResources();
}

public void testConsumerAllocatesBufferLimit() throws IOException {
consumer.consume(randomByteBufferOfLength(1000).flip());
assertThat(consumer.getBuffer().capacity(), equalTo(1000));
}

public void testConsumerAllocatesEmptyBuffer() throws IOException {
consumer.consume(ByteBuffer.allocate(0).flip());
assertThat(consumer.getBuffer().capacity(), equalTo(0));
}

public void testConsumerExpandsBufferLimits() throws IOException {
consumer.consume(randomByteBufferOfLength(1000).flip());
consumer.consume(randomByteBufferOfLength(2000).flip());
consumer.consume(randomByteBufferOfLength(3000).flip());
assertThat(consumer.getBuffer().capacity(), equalTo(6000));
}

public void testConsumerAllocatesLimit() throws IOException {
consumer.consume(randomByteBufferOfLength(BUFFER_LIMIT).flip());
assertThat(consumer.getBuffer().capacity(), equalTo(BUFFER_LIMIT));
}

public void testConsumerFailsToAllocateOverLimit() throws IOException {
assertThrows(ContentTooLongException.class, () -> consumer.consume(randomByteBufferOfLength(BUFFER_LIMIT + 1).flip()));
}

public void testConsumerFailsToExpandOverLimit() throws IOException {
consumer.consume(randomByteBufferOfLength(BUFFER_LIMIT).flip());
assertThrows(ContentTooLongException.class, () -> consumer.consume(randomByteBufferOfLength(1).flip()));
}

private static ByteBuffer randomByteBufferOfLength(int length) {
return ByteBuffer.allocate(length).put(randomBytesOfLength(length));
}
}
Loading