You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2020/05/03 13:35:02 UTC
[httpcomponents-core] branch HTTPCORE-631 created (now 31deceb)
This is an automated email from the ASF dual-hosted git repository.
olegk pushed a change to branch HTTPCORE-631
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git.
at 31deceb HTTPCORE-631: Revised i/o reactor shutdown sequence and resource de-allocation
This branch includes the following new commits:
new a5f29ab Improved integration test for out-of-sequence response handling by the HTTP/1.1 async protocol handler
new 31deceb HTTPCORE-631: Revised i/o reactor shutdown sequence and resource de-allocation
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
[httpcomponents-core] 01/02: Improved integration test for
out-of-sequence response handling by the HTTP/1.1 async protocol handler
Posted by ol...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
olegk pushed a commit to branch HTTPCORE-631
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git
commit a5f29ab5af2bb16412b8458b6d35290368a4a970
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Mon Apr 20 19:38:38 2020 +0200
Improved integration test for out-of-sequence response handling by the HTTP/1.1 async protocol handler
---
.../hc/core5/testing/nio/Http1IntegrationTest.java | 21 +++++++++-
...tyProducer.java => MultiBinEntityProducer.java} | 46 +++++++++++++---------
.../core5/testing/nio/MultiLineEntityProducer.java | 10 ++++-
3 files changed, 56 insertions(+), 21 deletions(-)
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1IntegrationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1IntegrationTest.java
index ea990a0..67859d6 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1IntegrationTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1IntegrationTest.java
@@ -1034,9 +1034,26 @@ public class Http1IntegrationTest extends InternalHttp1ServerTestBase {
"localhost", serverEndpoint.getPort(), TIMEOUT);
final ClientSessionEndpoint streamEndpoint = connectFuture.get();
+ for (int i = 0; i < 3; i++) {
+ final HttpRequest request1 = new BasicHttpRequest(Method.POST, createRequestURI(serverEndpoint, "/echo"));
+ final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(
+ new BasicRequestProducer(request1, new MultiLineEntityProducer("0123456789abcdef", 100000)),
+ new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
+ final Message<HttpResponse, String> result1 = future1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
+ Assert.assertNotNull(result1);
+ final HttpResponse response1 = result1.getHead();
+ Assert.assertNotNull(response1);
+ Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response1.getCode());
+ Assert.assertNotNull("You shall not pass", result1.getBody());
+
+ Assert.assertTrue(streamEndpoint.isOpen());
+ }
final HttpRequest request1 = new BasicHttpRequest(Method.POST, createRequestURI(serverEndpoint, "/echo"));
final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(
- new BasicRequestProducer(request1, new MultiLineEntityProducer("0123456789abcdef", 5000)),
+ new BasicRequestProducer(request1, new MultiBinEntityProducer(
+ new byte[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'},
+ 100000,
+ ContentType.TEXT_PLAIN)),
new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
final Message<HttpResponse, String> result1 = future1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
Assert.assertNotNull(result1);
@@ -1044,6 +1061,8 @@ public class Http1IntegrationTest extends InternalHttp1ServerTestBase {
Assert.assertNotNull(response1);
Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response1.getCode());
Assert.assertNotNull("You shall not pass", result1.getBody());
+
+ Assert.assertFalse(streamEndpoint.isOpen());
}
@Test
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/MultiLineEntityProducer.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/MultiBinEntityProducer.java
similarity index 65%
copy from httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/MultiLineEntityProducer.java
copy to httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/MultiBinEntityProducer.java
index 0c0a2fd..9c5188a 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/MultiLineEntityProducer.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/MultiBinEntityProducer.java
@@ -27,25 +27,25 @@
package org.apache.hc.core5.testing.nio;
import java.io.IOException;
-import java.nio.CharBuffer;
+import java.nio.ByteBuffer;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.nio.StreamChannel;
-import org.apache.hc.core5.http.nio.entity.AbstractCharAsyncEntityProducer;
+import org.apache.hc.core5.http.nio.entity.AbstractBinAsyncEntityProducer;
-public class MultiLineEntityProducer extends AbstractCharAsyncEntityProducer {
+public class MultiBinEntityProducer extends AbstractBinAsyncEntityProducer {
- private final String text;
+ private final byte[] bin;
private final int total;
- private final CharBuffer charbuf;
+ private final ByteBuffer bytebuf;
private int count;
- public MultiLineEntityProducer(final String text, final int total) {
- super(1024, -1, ContentType.TEXT_PLAIN);
- this.text = text;
+ public MultiBinEntityProducer(final byte[] bin, final int total, final ContentType contentType) {
+ super(-1, contentType);
+ this.bin = bin;
this.total = total;
- this.charbuf = CharBuffer.allocate(4096);
+ this.bytebuf = ByteBuffer.allocate(4096);
this.count = 0;
}
@@ -60,29 +60,39 @@ public class MultiLineEntityProducer extends AbstractCharAsyncEntityProducer {
}
@Override
- protected void produceData(final StreamChannel<CharBuffer> channel) throws IOException {
- while (charbuf.remaining() > text.length() + 2 && count < total) {
- charbuf.put(text + "\r\n");
+ protected void produceData(final StreamChannel<ByteBuffer> channel) throws IOException {
+ while (bytebuf.remaining() > bin.length + 2 && count < total) {
+ bytebuf.put(bin);
count++;
}
- if (charbuf.position() > 0) {
- charbuf.flip();
- channel.write(charbuf);
- charbuf.compact();
+ if (bytebuf.position() > 0) {
+ bytebuf.flip();
+ channel.write(bytebuf);
+ bytebuf.compact();
}
- if (count >= total && charbuf.position() == 0) {
+ if (count >= total && bytebuf.position() == 0) {
channel.endStream();
}
}
@Override
+ public boolean isChunked() {
+ return false;
+ }
+
+ @Override
+ public long getContentLength() {
+ return bin.length * total;
+ }
+
+ @Override
public void failed(final Exception cause) {
}
@Override
public void releaseResources() {
count = 0;
- charbuf.clear();
+ bytebuf.clear();
super.releaseResources();
}
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/MultiLineEntityProducer.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/MultiLineEntityProducer.java
index 0c0a2fd..c0a24f5 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/MultiLineEntityProducer.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/MultiLineEntityProducer.java
@@ -28,6 +28,8 @@ package org.apache.hc.core5.testing.nio;
import java.io.IOException;
import java.nio.CharBuffer;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.nio.StreamChannel;
@@ -41,14 +43,18 @@ public class MultiLineEntityProducer extends AbstractCharAsyncEntityProducer {
private int count;
- public MultiLineEntityProducer(final String text, final int total) {
- super(1024, -1, ContentType.TEXT_PLAIN);
+ public MultiLineEntityProducer(final String text, final int total, final Charset charset) {
+ super(1024, -1, ContentType.TEXT_PLAIN.withCharset(charset));
this.text = text;
this.total = total;
this.charbuf = CharBuffer.allocate(4096);
this.count = 0;
}
+ public MultiLineEntityProducer(final String text, final int total) {
+ this(text, total, StandardCharsets.US_ASCII);
+ }
+
@Override
public boolean isRepeatable() {
return true;
[httpcomponents-core] 02/02: HTTPCORE-631: Revised i/o reactor
shutdown sequence and resource de-allocation
Posted by ol...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
olegk pushed a commit to branch HTTPCORE-631
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git
commit 31deceb09644cbe369e40a0da4518ea6ea75e83c
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Sun May 3 15:28:46 2020 +0200
HTTPCORE-631: Revised i/o reactor shutdown sequence and resource de-allocation
---
.../core5/reactor/AbstractSingleCoreIOReactor.java | 51 +++++++++++-----------
.../hc/core5/reactor/MultiCoreIOReactor.java | 12 ++---
2 files changed, 33 insertions(+), 30 deletions(-)
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/reactor/AbstractSingleCoreIOReactor.java b/httpcore5/src/main/java/org/apache/hc/core5/reactor/AbstractSingleCoreIOReactor.java
index 86f32cc..af4584b 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/reactor/AbstractSingleCoreIOReactor.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/reactor/AbstractSingleCoreIOReactor.java
@@ -33,6 +33,7 @@ import java.nio.channels.ClosedSelectorException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.hc.core5.function.Callback;
@@ -45,6 +46,7 @@ abstract class AbstractSingleCoreIOReactor implements IOReactor {
private final Callback<Exception> exceptionCallback;
private final AtomicReference<IOReactorStatus> status;
+ private final AtomicBoolean terminated;
private final Object shutdownMutex;
final Selector selector;
@@ -54,6 +56,7 @@ abstract class AbstractSingleCoreIOReactor implements IOReactor {
this.exceptionCallback = exceptionCallback;
this.shutdownMutex = new Object();
this.status = new AtomicReference<>(IOReactorStatus.INACTIVE);
+ this.terminated = new AtomicBoolean();
try {
this.selector = Selector.open();
} catch (final IOException ex) {
@@ -87,27 +90,10 @@ abstract class AbstractSingleCoreIOReactor implements IOReactor {
} finally {
try {
doTerminate();
- final Set<SelectionKey> keys = this.selector.keys();
- for (final SelectionKey key : keys) {
- try {
- Closer.close((Closeable) key.attachment());
- } catch (final IOException ex) {
- logException(ex);
- }
- key.channel().close();
- }
- try {
- this.selector.close();
- } catch (final IOException ex) {
- logException(ex);
- }
} catch (final Exception ex) {
logException(ex);
} finally {
- this.status.set(IOReactorStatus.SHUT_DOWN);
- synchronized (this.shutdownMutex) {
- this.shutdownMutex.notifyAll();
- }
+ close(CloseMode.IMMEDIATE);
}
}
}
@@ -142,6 +128,9 @@ abstract class AbstractSingleCoreIOReactor implements IOReactor {
@Override
public final void close(final CloseMode closeMode) {
+ if (this.status.get() == IOReactorStatus.SHUT_DOWN) {
+ return;
+ }
if (closeMode == CloseMode.GRACEFUL) {
initiateShutdown();
try {
@@ -149,15 +138,27 @@ abstract class AbstractSingleCoreIOReactor implements IOReactor {
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
}
- } else {
- final IOReactorStatus previousStatus = this.status.getAndSet(IOReactorStatus.SHUT_DOWN);
- if (previousStatus == IOReactorStatus.ACTIVE) {
- this.selector.wakeup();
- }
- synchronized (this.shutdownMutex) {
- this.shutdownMutex.notifyAll();
+ }
+ this.status.set(IOReactorStatus.SHUT_DOWN);
+ if (terminated.compareAndSet(false, true)) {
+ try {
+ final Set<SelectionKey> keys = this.selector.keys();
+ for (final SelectionKey key : keys) {
+ try {
+ Closer.close((Closeable) key.attachment());
+ } catch (final IOException ex) {
+ logException(ex);
+ }
+ key.channel().close();
+ }
+ selector.close();
+ } catch (final Exception ex) {
+ logException(ex);
}
}
+ synchronized (this.shutdownMutex) {
+ this.shutdownMutex.notifyAll();
+ }
}
@Override
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/reactor/MultiCoreIOReactor.java b/httpcore5/src/main/java/org/apache/hc/core5/reactor/MultiCoreIOReactor.java
index d41d1f3..c7caf51 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/reactor/MultiCoreIOReactor.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/reactor/MultiCoreIOReactor.java
@@ -28,6 +28,7 @@
package org.apache.hc.core5.reactor;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.hc.core5.io.CloseMode;
@@ -40,12 +41,14 @@ class MultiCoreIOReactor implements IOReactor {
private final IOReactor[] ioReactors;
private final Thread[] threads;
private final AtomicReference<IOReactorStatus> status;
+ private final AtomicBoolean terminated;
MultiCoreIOReactor(final IOReactor[] ioReactors, final Thread[] threads) {
super();
this.ioReactors = ioReactors.clone();
this.threads = threads.clone();
this.status = new AtomicReference<>(IOReactorStatus.INACTIVE);
+ this.terminated = new AtomicBoolean();
}
@Override
@@ -105,8 +108,7 @@ class MultiCoreIOReactor implements IOReactor {
@Override
public final void close(final CloseMode closeMode) {
- final IOReactorStatus currentStatus = this.status.get();
- if (currentStatus == IOReactorStatus.INACTIVE || currentStatus == IOReactorStatus.SHUT_DOWN) {
+ if (this.status.get() == IOReactorStatus.SHUT_DOWN) {
return;
}
if (closeMode == CloseMode.GRACEFUL) {
@@ -116,8 +118,9 @@ class MultiCoreIOReactor implements IOReactor {
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
}
- } else {
- this.status.compareAndSet(IOReactorStatus.ACTIVE, IOReactorStatus.SHUTTING_DOWN);
+ }
+ this.status.set(IOReactorStatus.SHUT_DOWN);
+ if (this.terminated.compareAndSet(false, true)) {
for (int i = 0; i < this.ioReactors.length; i++) {
Closer.close(this.ioReactors[i], CloseMode.IMMEDIATE);
}
@@ -125,7 +128,6 @@ class MultiCoreIOReactor implements IOReactor {
this.threads[i].interrupt();
}
}
- this.status.set(IOReactorStatus.SHUT_DOWN);
}
@Override