-
Notifications
You must be signed in to change notification settings - Fork 925
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up and fix task executor type hierarchy (#4760)
Motivation: There are a few issues in our task executor type hierarchy. - We don't provide the context-aware version of the following task executor types: - `Executor` -> `ContextAwareExecutor` - `ExecutorService` -> `ContextAwareExecutorService` - `BlockingTaskExecutor` -> `ContextAwareBlockingTaskExecutor` - There are no `RequestContext.makeContextAware()` and `makeContextPropagating()` that accept the aforementioned executor types. - `blockingTaskExecutor` properties often use `ScheduledExecutorService` rather than `BlockingTaskExecutor`. Modifications: - Added `ContextAwareExecutor`. - Made `ContextAwareExecutorService` public. - Added `ContxtAwareBlockingTaskExecutor`. - Added the implementation of the aforementioned types. - Added the context-propagating version of the aforementioned types. - Merged `common.RequestContextUtil` to `internal.common.RequestContextUtil` for less confusion. - Added `BlockingTaskExecutor.of(ScheduledExecutorService)`. - Changed the type of `blockingTaskExecutor` from `ScheduledExecutorService`. - Added an overloaded builder method `blockingTaskExecutor(BlockingTaskExecutor)`. - Removed the problematic wrapping of `blockingTaskExecutor` in `DefaultServerConfig.monitorBlockingTaskExecutor()`. - The wrapped executor is only used when a user gets the executor via `ServerConfig.blockingTaskExecutor()`, which is often not the case. A user gets the executor via `ServiceRequestContext.blockingTaskExecutor()` and thus the wrapped executor is never used. Result: - API completeness - You can now create a `BlockingTaskExecutor` by wrapping a `ScheduledExecutorService`. - You can now create context-aware or context-propagating `Executor` and `BlockingTaskExecutor`. - (Breaking change) The type of `blockingTaskExecutor` property has been changed from `ScheduledExecutorService` to `BlockingTaskExecutor`. - Simply recompiling your code should be enough in most cases because `BlockingTaskExecutor` is a `ScheduledExecutorService`. - (Breaking change) The return types of `RequestContext.makeContextAware()` methods have been changed to the context-aware types, e.g. - `makeContextAware(Executor)` returns `ContextAwareExecutor` instead of `Executor`. - `makeContextAware(ScheduledExecutorService)` returns `ContextAwareScheduledExecutorService` instead of `ScheduledExecutorService`.
- Loading branch information
Showing
46 changed files
with
783 additions
and
248 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
83 changes: 83 additions & 0 deletions
83
core/src/main/java/com/linecorp/armeria/common/AbstractContextAwareExecutor.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,83 @@ | ||
/* | ||
* Copyright 2023 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.common; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.concurrent.Executor; | ||
import java.util.function.Supplier; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.linecorp.armeria.common.annotation.Nullable; | ||
|
||
abstract class AbstractContextAwareExecutor<E extends Executor> implements Executor { | ||
enum LogRequestContextWarningOnce implements Supplier<RequestContext> { | ||
INSTANCE; | ||
|
||
@Override | ||
@Nullable | ||
public RequestContext get() { | ||
ClassLoaderHack.loadMe(); | ||
return null; | ||
} | ||
|
||
/** | ||
* This won't be referenced until {@link #get()} is called. If there's only one classloader, the | ||
* initializer will only be called once. | ||
*/ | ||
private static final class ClassLoaderHack { | ||
static void loadMe() {} | ||
|
||
static { | ||
logger.warn( | ||
"Attempted to propagate request context to an executor task, " + | ||
"but no request context available. " + | ||
"If this executor is used for non-request-related tasks then it's safe to ignore this", | ||
new NoRequestContextException()); | ||
} | ||
} | ||
|
||
private static final class NoRequestContextException extends RuntimeException { | ||
private static final long serialVersionUID = 2804189311774982052L; | ||
} | ||
} | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(AbstractContextAwareExecutor.class); | ||
private final E executor; | ||
|
||
AbstractContextAwareExecutor(E executor) { | ||
this.executor = requireNonNull(executor, "executor"); | ||
} | ||
|
||
@Nullable | ||
abstract RequestContext contextOrNull(); | ||
|
||
public final E withoutContext() { | ||
return executor; | ||
} | ||
|
||
final Runnable makeContextAware(Runnable task) { | ||
final RequestContext context = contextOrNull(); | ||
return context == null ? task : context.makeContextAware(task); | ||
} | ||
|
||
@Override | ||
public final void execute(Runnable command) { | ||
executor.execute(makeContextAware(command)); | ||
} | ||
} |
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
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
58 changes: 58 additions & 0 deletions
58
core/src/main/java/com/linecorp/armeria/common/ContextAwareBlockingTaskExecutor.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,58 @@ | ||
/* | ||
* Copyright 2023 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.common; | ||
|
||
import static com.linecorp.armeria.internal.common.RequestContextUtil.ensureSameCtx; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
import com.linecorp.armeria.common.util.BlockingTaskExecutor; | ||
|
||
/** | ||
* A delegating {@link BlockingTaskExecutor} that sets the {@link RequestContext} before executing | ||
* any submitted tasks. | ||
*/ | ||
public interface ContextAwareBlockingTaskExecutor | ||
extends BlockingTaskExecutor, ContextAwareScheduledExecutorService { | ||
|
||
/** | ||
* Returns a new {@link ContextAwareBlockingTaskExecutor} that sets the specified {@link RequestContext} | ||
* before executing any submitted tasks. | ||
*/ | ||
static ContextAwareBlockingTaskExecutor of(RequestContext context, BlockingTaskExecutor executor) { | ||
requireNonNull(context, "context"); | ||
requireNonNull(executor, "executor"); | ||
if (executor instanceof ContextAwareBlockingTaskExecutor) { | ||
ensureSameCtx(context, (ContextAwareBlockingTaskExecutor) executor, | ||
ContextAwareBlockingTaskExecutor.class); | ||
return (ContextAwareBlockingTaskExecutor) executor; | ||
} | ||
return new DefaultContextAwareBlockingTaskExecutor(context, executor); | ||
} | ||
|
||
/** | ||
* Returns the {@link RequestContext} that was specified when creating | ||
* this {@link ContextAwareBlockingTaskExecutor}. | ||
*/ | ||
@Override | ||
RequestContext context(); | ||
|
||
/** | ||
* Returns the {@link BlockingTaskExecutor} that executes the submitted tasks without setting the | ||
* {@link RequestContext}. | ||
*/ | ||
@Override | ||
BlockingTaskExecutor withoutContext(); | ||
} |
Oops, something went wrong.