-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix increased latency for gRPC after adding `ListenableAsyncCloseable…
….onClosing()` (#2473) Motivation: #2430 adds `ListenableAsyncCloseable.onClosing()` method. Its implementations require allocation of a `CompletableProcessor`. Because we wrap channel's `EventLoop` with `IoExecutor` on every new connection and every new HTTP/2 stream, it caused significant increase of allocations and pressure for GC. As the result, latency increased for all HTTP/2 and gRPC use-cases. Modifications: - Add `ExecutionContextUtils` that adapt `EventLoop` to `IoExecutor` API for `ExecutionContext` and cache its value in `FastThreadLocal` to reduce allocations; - Make sure that HTTP/1.x and parent HTTP/2 connections use `ExecutionContextUtils`; - For each new HTTP/2 stream (child channel) take execution context from the parent channel because child channel uses the same `EventLoop` as its parent channel; - Add a new `DefaultNettyConnection.initChannel` overload that takes already constructed `ExecutionContext`; - Adjust existing tests for new API; Result: Significantly reduced allocation rate and less GC pauses for HTTP/2 and gRPC.
- Loading branch information
1 parent
f9afee4
commit 5e3e4ce
Showing
11 changed files
with
178 additions
and
48 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
servicetalk-http-netty/src/main/java/io/servicetalk/http/netty/ExecutionContextUtils.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,74 @@ | ||
/* | ||
* Copyright © 2022 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* 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.servicetalk.http.netty; | ||
|
||
import io.servicetalk.concurrent.api.ListenableAsyncCloseable; | ||
import io.servicetalk.http.api.DelegatingHttpExecutionContext; | ||
import io.servicetalk.http.api.HttpExecutionContext; | ||
import io.servicetalk.transport.api.IoExecutor; | ||
|
||
import io.netty.channel.Channel; | ||
import io.netty.channel.EventLoop; | ||
import io.netty.util.concurrent.FastThreadLocal; | ||
|
||
import static io.servicetalk.transport.netty.internal.NettyIoExecutors.fromNettyEventLoop; | ||
|
||
final class ExecutionContextUtils { | ||
|
||
private static final FastThreadLocal<IoExecutor> CHANNEL_IO_EXECUTOR = new FastThreadLocal<>(); | ||
|
||
private ExecutionContextUtils() { | ||
// No instances | ||
} | ||
|
||
/** | ||
* Utility that maps {@link Channel#eventLoop()} into {@link IoExecutor} and caches the result for future mappings | ||
* to reduce allocations. Because {@link IoExecutor} implements {@link ListenableAsyncCloseable} interface, its | ||
* allocation cost is relatively high. | ||
* | ||
* @param channel {@link Channel} registered for a single {@link EventLoop} thread | ||
* @param builderExecutionContext {@link HttpExecutionContext} pre-computed by the builder for new connections | ||
* @return {@link HttpExecutionContext} which has {@link IoExecutor} backed by a single {@link EventLoop} thread | ||
* associated with the passed {@link Channel}. | ||
*/ | ||
static HttpExecutionContext channelExecutionContext(final Channel channel, | ||
final HttpExecutionContext builderExecutionContext) { | ||
final IoExecutor channelIoExecutor = fromChannel(channel, | ||
builderExecutionContext.ioExecutor().isIoThreadSupported()); | ||
return new DelegatingHttpExecutionContext(builderExecutionContext) { | ||
@Override | ||
public IoExecutor ioExecutor() { | ||
return channelIoExecutor; | ||
} | ||
}; | ||
} | ||
|
||
private static IoExecutor fromChannel(final Channel channel, boolean isIoThreadSupported) { | ||
assert channel.eventLoop().inEventLoop(); | ||
IoExecutor ioExecutor = CHANNEL_IO_EXECUTOR.getIfExists(); | ||
if (ioExecutor != null) { | ||
return ioExecutor; | ||
} | ||
ioExecutor = fromNettyEventLoop(channel.eventLoop(), isIoThreadSupported); | ||
CHANNEL_IO_EXECUTOR.set(ioExecutor); | ||
return ioExecutor; | ||
} | ||
|
||
// for tests only | ||
static void clearThreadLocal() { | ||
CHANNEL_IO_EXECUTOR.remove(); | ||
} | ||
} |
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
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
Oops, something went wrong.