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

Support for interruption of HTTP/2 connections for efficient shutdowns #6041

Merged
merged 3 commits into from
Feb 2, 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
5 changes: 5 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,11 @@
<artifactId>helidon-common-security</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common-task</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.common.testing</groupId>
<artifactId>helidon-common-testing-junit5</artifactId>
Expand Down
3 changes: 2 additions & 1 deletion common/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2018, 2022 Oracle and/or its affiliates.
Copyright (c) 2018, 2023 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.
Expand Down Expand Up @@ -48,5 +48,6 @@
<module>security</module>
<module>testing</module>
<module>features</module>
<module>task</module>
</modules>
</project>
48 changes: 48 additions & 0 deletions common/task/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2023 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.

-->

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common-project</artifactId>
<version>4.0.0-SNAPSHOT</version>
</parent>
<artifactId>helidon-common-task</artifactId>
<name>Helidon Common Task</name>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package io.helidon.nima.webserver;
package io.helidon.common.task;

import java.io.Closeable;
import java.util.concurrent.Future;
Expand All @@ -27,7 +27,7 @@
* executor will query the thread and interrupt it if possible. It is important
* to efficiently shut down the Nima webserver in certain environments.
*/
interface HelidonTaskExecutor extends Closeable {
public interface HelidonTaskExecutor extends Closeable {

/**
* Executes a task.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package io.helidon.nima.webserver;
package io.helidon.common.task;

import java.util.concurrent.Callable;

Expand Down
20 changes: 20 additions & 0 deletions common/task/src/main/java/io/helidon/common/task/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2023 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.
*/

/**
* Interruptable tasks and related executors.
*/
package io.helidon.common.task;
22 changes: 22 additions & 0 deletions common/task/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2023 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.
*/

/**
* Common task module.
*/
module io.helidon.common.task {
exports io.helidon.common.task;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.helidon.common.http.Http.Header;
import io.helidon.common.http.Http.HeaderValues;
import io.helidon.common.http.HttpPrologue;
import io.helidon.common.task.InterruptableTask;
import io.helidon.nima.http2.FlowControl;
import io.helidon.nima.http2.Http2ConnectionWriter;
import io.helidon.nima.http2.Http2ErrorCode;
Expand Down Expand Up @@ -67,7 +68,7 @@
* A single connection is created between a client and a server.
* A single connection serves multiple streams.
*/
public class Http2Connection implements ServerConnection {
public class Http2Connection implements ServerConnection, InterruptableTask<Void> {
static final String FULL_PROTOCOL = "HTTP/2.0";
static final String PROTOCOL = "HTTP";
static final String PROTOCOL_VERSION = "2.0";
Expand Down Expand Up @@ -189,6 +190,11 @@ public void expectPreface() {
this.expectPreface = true;
}

@Override
public boolean canInterrupt() {
return streams.isEmpty();
}

@Override
public String toString() {
return "[" + ctx.socketId() + " " + ctx.childSocketId() + "]";
Expand Down
1 change: 1 addition & 0 deletions nima/http2/webserver/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
requires transitive io.helidon.common;
requires transitive io.helidon.common.uri;
requires transitive io.helidon.common.socket;
requires transitive io.helidon.common.task;
requires transitive io.helidon.nima.webserver;
requires transitive io.helidon.common.http;
requires transitive io.helidon.nima.http2;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2023 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.nima.tests.integration.http2.webserver;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;

import io.helidon.common.configurable.Resource;
import io.helidon.common.pki.KeyConfig;
import io.helidon.nima.common.tls.Tls;
import io.helidon.nima.webserver.WebServer;

import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.lessThan;

class Http2WebServerStopIdleTest {

private final Tls insecureTls;

Http2WebServerStopIdleTest() {;
this.insecureTls = Tls.builder()
// insecure setup, as we have self-signed certificate
.trustAll(true)
.build();
}

@Test
void stopWhenIdleExpectTimelyStopHttp2() throws IOException, InterruptedException {
KeyConfig privateKeyConfig = KeyConfig.keystoreBuilder()
.keystore(Resource.create("certificate.p12"))
.keystorePassphrase("helidon")
.build();
Tls tls = Tls.builder()
.privateKey(privateKeyConfig.privateKey().get())
.privateKeyCertChain(privateKeyConfig.certChain())
.build();
WebServer webServer = WebServer.builder()
.socket("https", socketBuilder -> socketBuilder.tls(tls))
.routing(router -> router.get("ok", (req, res) -> res.send("ok")))
.build();
webServer.start();

int port = webServer.port("https");
HttpResponse<String> response = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.sslContext(insecureTls.sslContext())
.connectTimeout(Duration.ofSeconds(5))
.build()
.send(HttpRequest.newBuilder()
.timeout(Duration.ofSeconds(5))
.uri(URI.create("https://localhost:" + port + "/ok"))
.GET()
.build(),
HttpResponse.BodyHandlers.ofString());
assertThat(response.statusCode(), is(200));
assertThat(response.body(), is("ok"));

long startMillis = System.currentTimeMillis();
webServer.stop();
int stopExecutionTimeInMillis = (int) (System.currentTimeMillis() - startMillis);
assertThat(stopExecutionTimeInMillis, is(lessThan(500)));
}
}
4 changes: 4 additions & 0 deletions nima/webserver/webserver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common-security</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common-task</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.nima.common</groupId>
<artifactId>helidon-nima-common-tls</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.helidon.common.http.RequestException;
import io.helidon.common.socket.HelidonSocket;
import io.helidon.common.socket.SocketWriter;
import io.helidon.common.task.InterruptableTask;
import io.helidon.nima.webserver.http.DirectHandlers;
import io.helidon.nima.webserver.spi.ServerConnection;
import io.helidon.nima.webserver.spi.ServerConnectionSelector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import io.helidon.common.socket.PlainSocket;
import io.helidon.common.socket.SocketOptions;
import io.helidon.common.socket.TlsSocket;
import io.helidon.common.task.HelidonTaskExecutor;
import io.helidon.nima.common.tls.Tls;
import io.helidon.nima.webserver.http.DirectHandlers;
import io.helidon.nima.webserver.spi.ServerConnectionSelector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import io.helidon.common.task.HelidonTaskExecutor;
import io.helidon.common.task.InterruptableTask;

/**
* An implementation of {@link HelidonTaskExecutor}. Implementation is a simplified
* version of ThreadPerTaskExecutor in the JDK library. Upon termination, this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
import io.helidon.common.http.ServerResponseHeaders;
import io.helidon.common.http.WritableHeaders;
import io.helidon.common.mapper.MapperException;
import io.helidon.common.task.InterruptableTask;
import io.helidon.nima.http.encoding.ContentDecoder;
import io.helidon.nima.http.encoding.ContentEncodingContext;
import io.helidon.nima.webserver.CloseConnectionException;
import io.helidon.nima.webserver.ConnectionContext;
import io.helidon.nima.webserver.InterruptableTask;
import io.helidon.nima.webserver.http.DirectTransportRequest;
import io.helidon.nima.webserver.http.HttpRouting;
import io.helidon.nima.webserver.http1.spi.Http1Upgrader;
Expand Down
1 change: 1 addition & 0 deletions nima/webserver/webserver/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
requires io.helidon.logging.common;
requires io.helidon.builder;
requires io.helidon.pico.builder.config;
requires io.helidon.common.task;

requires java.management;
// only used to keep logging active until shutdown hook finishes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import io.helidon.common.task.HelidonTaskExecutor;
import io.helidon.common.task.InterruptableTask;
import org.junit.jupiter.api.Test;

import static io.helidon.common.testing.junit5.MatcherWithRetry.assertThatWithRetry;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

class ThreadPerTaskExecutorTest {

Expand Down