forked from line/armeria
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Some annotated service methods are run from blocking task execu… (lin…
…e#2187) Motivation: The annotated services whose return type is neither `HttpResponse` nor `CompeltableFuture`, are run using a `blockingTaskExecutor` by default. We should fix this to use `EventLoop` by default and let a user choose to use `blockingTaskExecutor` if he/she wants. Modifications: - Add `@Blocking` which makes the annotated service run using `blockingTaskExecutor` - Make all annotated services run from `EventLoop` Result: - Fix line#2078 - (Breaking) All annotated services are now run from `EventLoop` by default
- Loading branch information
Showing
5 changed files
with
270 additions
and
42 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
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
34 changes: 34 additions & 0 deletions
34
core/src/main/java/com/linecorp/armeria/server/annotation/Blocking.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,34 @@ | ||
/* | ||
* Copyright 2019 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you 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: | ||
* | ||
* https://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 com.linecorp.armeria.server.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import com.linecorp.armeria.server.ServerConfig; | ||
|
||
/** | ||
* Specifies that the annotated service method must be invoked from the | ||
* {@linkplain ServerConfig#blockingTaskExecutor() blocking task executor} | ||
* instead of an event loop thread. | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
public @interface Blocking { | ||
} |
172 changes: 172 additions & 0 deletions
172
.../test/java/com/linecorp/armeria/internal/annotation/AnnotatedHttpServiceBlockingTest.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,172 @@ | ||
/* | ||
* Copyright 2019 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you 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: | ||
* | ||
* https://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 com.linecorp.armeria.internal.annotation; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionStage; | ||
import java.util.concurrent.LinkedTransferQueue; | ||
import java.util.concurrent.ThreadFactory; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.node.TextNode; | ||
|
||
import com.linecorp.armeria.client.HttpClient; | ||
import com.linecorp.armeria.common.AggregatedHttpResponse; | ||
import com.linecorp.armeria.common.HttpMethod; | ||
import com.linecorp.armeria.common.HttpResponse; | ||
import com.linecorp.armeria.common.HttpStatus; | ||
import com.linecorp.armeria.common.RequestContext; | ||
import com.linecorp.armeria.common.RequestHeaders; | ||
import com.linecorp.armeria.common.util.ThreadFactories; | ||
import com.linecorp.armeria.server.ServerBuilder; | ||
import com.linecorp.armeria.server.annotation.Blocking; | ||
import com.linecorp.armeria.server.annotation.Get; | ||
import com.linecorp.armeria.server.logging.LoggingService; | ||
import com.linecorp.armeria.testing.junit.server.ServerExtension; | ||
|
||
class AnnotatedHttpServiceBlockingTest { | ||
|
||
private static final CountingThreadPoolExecutor executor = new CountingThreadPoolExecutor( | ||
0, 1, 1, TimeUnit.SECONDS, new LinkedTransferQueue<>(), | ||
ThreadFactories.newThreadFactory("blocking-test", true)); | ||
|
||
private static final AtomicInteger blockingCount = new AtomicInteger(); | ||
|
||
@BeforeEach | ||
void clear() { | ||
blockingCount.set(0); | ||
} | ||
|
||
private static class CountingThreadPoolExecutor extends ThreadPoolExecutor { | ||
|
||
CountingThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, | ||
TimeUnit unit, BlockingQueue<Runnable> workQueue, | ||
ThreadFactory threadFactory) { | ||
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory); | ||
} | ||
|
||
@Override | ||
protected void beforeExecute(Thread t, Runnable r) { | ||
blockingCount.incrementAndGet(); | ||
} | ||
} | ||
|
||
@RegisterExtension | ||
static final ServerExtension server = new ServerExtension() { | ||
@Override | ||
protected void configure(ServerBuilder sb) throws Exception { | ||
sb.annotatedService("/myEvenLoop", new MyEventLoopAnnotatedService(), | ||
LoggingService.newDecorator()); | ||
sb.annotatedService("/myBlocking", new MyBlockingAnnotatedService(), | ||
LoggingService.newDecorator()); | ||
sb.blockingTaskExecutor(executor, false); | ||
} | ||
}; | ||
|
||
static class MyEventLoopAnnotatedService { | ||
|
||
@Get("/httpResponse") | ||
public HttpResponse httpResponse(RequestContext ctx) { | ||
return HttpResponse.of(HttpStatus.OK); | ||
} | ||
|
||
@Get("/aggregatedHttpResponse") | ||
public AggregatedHttpResponse aggregatedHttpResponse(RequestContext ctx) { | ||
return AggregatedHttpResponse.of(HttpStatus.OK); | ||
} | ||
|
||
@Get("/jsonNode") | ||
public JsonNode jsonNode(RequestContext ctx) { | ||
return TextNode.valueOf("Armeria"); | ||
} | ||
|
||
@Get("/completionStage") | ||
public CompletionStage<String> completionStage(RequestContext ctx) { | ||
return CompletableFuture.supplyAsync(() -> "Armeria"); | ||
} | ||
} | ||
|
||
static class MyBlockingAnnotatedService { | ||
|
||
@Get("/httpResponse") | ||
@Blocking | ||
public HttpResponse httpResponse(RequestContext ctx) { | ||
return HttpResponse.of(HttpStatus.OK); | ||
} | ||
|
||
@Get("/aggregatedHttpResponse") | ||
@Blocking | ||
public AggregatedHttpResponse aggregatedHttpResponse(RequestContext ctx) { | ||
return AggregatedHttpResponse.of(HttpStatus.OK); | ||
} | ||
|
||
@Get("/jsonNode") | ||
@Blocking | ||
public JsonNode jsonNode(RequestContext ctx) { | ||
return TextNode.valueOf("Armeria"); | ||
} | ||
|
||
@Get("/completionStage") | ||
@Blocking | ||
public CompletionStage<String> completionStage(RequestContext ctx) { | ||
return CompletableFuture.supplyAsync(() -> "Armeria"); | ||
} | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource({ | ||
"/myEvenLoop/httpResponse, 0", | ||
"/myEvenLoop/aggregatedHttpResponse, 0", | ||
"/myEvenLoop/jsonNode, 0", | ||
"/myEvenLoop/completionStage, 0" | ||
}) | ||
void testOnlyEventLoopWithoutBlockingAnnotation(String path, Integer count) throws Exception { | ||
final HttpClient client = HttpClient.of(server.uri("/")); | ||
|
||
final RequestHeaders headers = RequestHeaders.of(HttpMethod.GET, path); | ||
final AggregatedHttpResponse res = client.execute(headers).aggregate().join(); | ||
assertThat(res.status()).isSameAs(HttpStatus.OK); | ||
assertThat(blockingCount).hasValue(count); | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource({ | ||
"/myBlocking/httpResponse, 1", | ||
"/myBlocking/aggregatedHttpResponse, 1", | ||
"/myBlocking/jsonNode, 1", | ||
"/myBlocking/completionStage, 1" | ||
}) | ||
void testOnlyBlockingWithBlockingAnnotation(String path, Integer count) throws Exception { | ||
final HttpClient client = HttpClient.of(server.uri("/")); | ||
|
||
final RequestHeaders headers = RequestHeaders.of(HttpMethod.GET, path); | ||
final AggregatedHttpResponse res = client.execute(headers).aggregate().join(); | ||
assertThat(res.status()).isSameAs(HttpStatus.OK); | ||
assertThat(blockingCount).hasValue(count); | ||
} | ||
} |
Oops, something went wrong.