-
Notifications
You must be signed in to change notification settings - Fork 567
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proposal to implement a more efficient webserver shutdown strategy (#…
…5876) * Proposal to implement a more efficient webserver shutdown strategy. For more information see issue #5717. This approach is based on that described in PR #5748. It includes a simple executor SPI for tasks that are immediately interruptable, such as those waiting to read connection preambles. Some new tests that verify faster webserver shutdown. * Updated copyright year. Signed-off-by: Santiago Pericasgeertsen <[email protected]> * Removed print statements and fixed logging. Signed-off-by: Santiago Pericasgeertsen <[email protected]> * Fixed checkstyle. Signed-off-by: Santiago Pericasgeertsen <[email protected]> * Avoid using @servertest when stopping server manually. Signed-off-by: Santiago Pericasgeertsen <[email protected]> * Dropped SPI for tasks, moved interfaces into webserver package. Signed-off-by: Santiago Pericasgeertsen <[email protected]> * Fixed checkstyle. Signed-off-by: Santiago Pericasgeertsen <[email protected]> * Make interface package private. Signed-off-by: Santiago Pericasgeertsen <[email protected]> --------- Signed-off-by: Santiago Pericasgeertsen <[email protected]>
- Loading branch information
Showing
10 changed files
with
626 additions
and
43 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
50 changes: 50 additions & 0 deletions
50
...bserver/src/test/java/io/helidon/nima/tests/integration/server/WebServerStopIdleTest.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,50 @@ | ||
/* | ||
* 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.server; | ||
|
||
import io.helidon.common.http.Http; | ||
import io.helidon.nima.webclient.http1.Http1Client; | ||
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 WebServerStopIdleTest { | ||
|
||
@Test | ||
void stopWhenIdleExpectTimelyStop() { | ||
WebServer webServer = WebServer.builder() | ||
.routing(router -> router.get("ok", (req, res) -> res.send("ok"))) | ||
.build(); | ||
webServer.start(); | ||
|
||
Http1Client client = Http1Client.builder() | ||
.baseUri("http://localhost:" + webServer.port()) | ||
.build(); | ||
try (var response = client.get("/ok").request()) { | ||
assertThat(response.status(), is(Http.Status.OK_200)); | ||
assertThat(response.entity().as(String.class), is("ok")); | ||
} | ||
|
||
long startMillis = System.currentTimeMillis(); | ||
webServer.stop(); | ||
int stopExecutionTimeInMillis = (int) (System.currentTimeMillis() - startMillis); | ||
assertThat(stopExecutionTimeInMillis, is(lessThan(500))); | ||
} | ||
} |
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
64 changes: 64 additions & 0 deletions
64
nima/webserver/webserver/src/main/java/io/helidon/nima/webserver/HelidonTaskExecutor.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,64 @@ | ||
/* | ||
* 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.webserver; | ||
|
||
import java.io.Closeable; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* A simplified {@link java.util.concurrent.ExecutorService} that can execute | ||
* {@link InterruptableTask}s and can be efficiently terminated. A thread that is | ||
* waiting to read on an open connection cannot be efficiently stopped. This | ||
* 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 { | ||
|
||
/** | ||
* Executes a task. | ||
* | ||
* @param task an interruptable task | ||
* @param <T> type ov value returned by task | ||
* @return a future for a value returned by the task | ||
*/ | ||
<T> Future<T> execute(InterruptableTask<T> task); | ||
|
||
/** | ||
* Verifies if the executor is terminated. | ||
* | ||
* @return outcome of test | ||
*/ | ||
boolean isTerminated(); | ||
|
||
/** | ||
* Terminate executor waiting for any running task to complete for a specified | ||
* timeout period. It will only wait for those {@link InterruptableTask}s that | ||
* are not interruptable. | ||
* | ||
* @param timeout timeout period | ||
* @param unit timeout period unit | ||
* @return outcome of shutdown process | ||
*/ | ||
boolean terminate(long timeout, TimeUnit unit); | ||
|
||
/** | ||
* Force termination by forcefully interrupting all tasks. Shall only be called | ||
* if {@link #terminate} returns {@code false}. | ||
*/ | ||
void forceTerminate(); | ||
} |
50 changes: 50 additions & 0 deletions
50
nima/webserver/webserver/src/main/java/io/helidon/nima/webserver/InterruptableTask.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,50 @@ | ||
/* | ||
* 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.webserver; | ||
|
||
import java.util.concurrent.Callable; | ||
|
||
/** | ||
* An interruptable task that can implements both {@link Runnable} and | ||
* {@link Callable}. | ||
* | ||
* @param <T> type of value returned by task | ||
*/ | ||
public interface InterruptableTask<T> extends Runnable, Callable<T> { | ||
|
||
/** | ||
* Signals if a task can be interrupted at the time this method is called. | ||
* | ||
* @return outcome of interruptable test | ||
*/ | ||
boolean canInterrupt(); | ||
|
||
@Override | ||
default void run() { | ||
try { | ||
call(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
default T call() throws Exception { | ||
run(); | ||
return null; | ||
} | ||
} |
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.