-
Notifications
You must be signed in to change notification settings - Fork 566
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Watermarked response backpressure #3136
Signed-off-by: Daniel Kec <daniel.kec@oracle.com>
- Loading branch information
Showing
19 changed files
with
745 additions
and
222 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
104 changes: 104 additions & 0 deletions
104
...oprofile/server/src/test/java/io/helidon/microprofile/server/StreamingOutputLeakTest.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,104 @@ | ||
/* | ||
* Copyright (c) 2022 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.helidon.microprofile.server; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.QueryParam; | ||
import jakarta.ws.rs.client.WebTarget; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.core.StreamingOutput; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Random; | ||
|
||
import io.helidon.microprofile.tests.junit5.AddBean; | ||
import io.helidon.microprofile.tests.junit5.AddConfig; | ||
import io.helidon.microprofile.tests.junit5.AddExtension; | ||
import io.helidon.microprofile.tests.junit5.DisableDiscovery; | ||
import io.helidon.microprofile.tests.junit5.HelidonTest; | ||
|
||
import org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.CoreMatchers.is; | ||
|
||
@HelidonTest | ||
@DisableDiscovery | ||
@AddBean(StreamingOutputLeakTest.DownloadResource.class) | ||
@AddExtension(ServerCdiExtension.class) | ||
@AddExtension(JaxRsCdiExtension.class) | ||
@AddExtension(CdiComponentProvider.class) | ||
@AddConfig(key = "server.backpressure-buffer-size", value = "20971520")//20Mb | ||
class StreamingOutputLeakTest { | ||
|
||
private static final int SIZE10MB = 10 * 1024 * 1024; | ||
private static final int SIZE = SIZE10MB; | ||
private static final long NUMBER_OF_BUFS = 20; | ||
private static final byte[] DATA_10MB = new byte[SIZE]; | ||
|
||
static { | ||
Random r = new Random(); | ||
r.nextBytes(DATA_10MB); | ||
} | ||
|
||
/** | ||
* Reproducer for issue #4643 | ||
*/ | ||
@Test | ||
void streamingOutput(WebTarget target) throws IOException { | ||
|
||
InputStream is = target.path("/download") | ||
.request() | ||
.get(InputStream.class); | ||
long size = 0; | ||
while (is.read() != -1) { | ||
size++; | ||
} | ||
is.close(); | ||
|
||
// Make sure all data has been read | ||
assertThat(size, is(NUMBER_OF_BUFS * SIZE)); | ||
} | ||
|
||
@Path("/download") | ||
public static class DownloadResource { | ||
|
||
@GET | ||
@Produces(MediaType.MULTIPART_FORM_DATA) | ||
public Response getPayload( | ||
@NotNull @QueryParam("fileName") String fileName) { | ||
StreamingOutput fileStream = output -> { | ||
|
||
// 2gb | ||
for (int i = 0; i < NUMBER_OF_BUFS; i++) { | ||
output.write(DATA_10MB); | ||
output.flush(); | ||
} | ||
|
||
}; | ||
return Response | ||
.ok(fileStream, MediaType.MULTIPART_FORM_DATA) | ||
.build(); | ||
} | ||
} | ||
} |
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
67 changes: 67 additions & 0 deletions
67
...webserver/webserver/src/main/java/io/helidon/reactive/webserver/BackpressureStrategy.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,67 @@ | ||
/* | ||
* Copyright (c) 2022 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.helidon.reactive.webserver; | ||
|
||
import java.util.concurrent.Flow; | ||
|
||
import io.helidon.reactive.webserver.ServerResponseSubscription.Unbounded; | ||
import io.helidon.reactive.webserver.ServerResponseSubscription.WatermarkAutoFlush; | ||
import io.helidon.reactive.webserver.ServerResponseSubscription.WatermarkLinear; | ||
import io.helidon.reactive.webserver.ServerResponseSubscription.WatermarkPrefetch; | ||
|
||
/** | ||
* Strategy for applying backpressure to the reactive stream of response data. | ||
*/ | ||
public enum BackpressureStrategy { | ||
/** | ||
* Data chunks are requested one-by-one after previous data chunk has been given to Netty for writing. | ||
* When backpressure-buffer-size watermark is reached new chunks are not requested until buffer size | ||
* decrease under the watermark value. | ||
*/ | ||
LINEAR(1), | ||
/** | ||
* Data are requested one-by-one, in case buffer reaches watermark, | ||
* no other data is requested and extra flush is initiated. | ||
*/ | ||
AUTO_FLUSH(2), | ||
/** | ||
* After first data chunk arrives, expected number of chunks needed | ||
* to fill the buffer up to watermark is calculated and requested. | ||
*/ | ||
PREFETCH(3), | ||
/** | ||
* No backpressure is applied, Long.MAX_VALUE(unbounded) is requested from upstream. | ||
*/ | ||
UNBOUNDED(4); | ||
|
||
private final int type; | ||
|
||
BackpressureStrategy(int type) { | ||
this.type = type; | ||
} | ||
|
||
ServerResponseSubscription createSubscription(Flow.Subscription subscription, | ||
long backpressureBufferSize) { | ||
switch (type) { | ||
case 1: return new WatermarkLinear(subscription, backpressureBufferSize); | ||
case 2: return new WatermarkAutoFlush(subscription, backpressureBufferSize); | ||
case 3: return new WatermarkPrefetch(subscription, backpressureBufferSize); | ||
case 4: return new Unbounded(subscription); | ||
default: throw new IllegalStateException("Unknown backpressure strategy."); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.