-
Notifications
You must be signed in to change notification settings - Fork 565
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
Proposal to implement a more efficient webserver shutdown strategy #5876
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
268fbb4
Proposal to implement a more efficient webserver shutdown strategy. F…
spericas 6e09986
Updated copyright year.
spericas 625f068
Removed print statements and fixed logging.
spericas f912825
Fixed checkstyle.
spericas e5c0e48
Avoid using @ServerTest when stopping server manually.
spericas 6285ce6
Dropped SPI for tasks, moved interfaces into webserver package.
spericas d63486d
Fixed checkstyle.
spericas d4cebe4
Make interface package private.
spericas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Misspelled; should be
Interruptible