-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Share netty event loops between transports #46346
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d112e51
Share netty event loops between transports
Tim-Brooks b37467a
tests
Tim-Brooks 71ff73b
Add security
Tim-Brooks 1de9901
Merge remote-tracking branch 'upstream/master' into shard_event_loops
Tim-Brooks 96f1dc5
Review changes
Tim-Brooks 1e69e78
Merge remote-tracking branch 'upstream/master' into shard_event_loops
Tim-Brooks 9a06615
Review changes
Tim-Brooks 86b8434
Merge remote-tracking branch 'upstream/master' into shard_event_loops
Tim-Brooks 04fe9a6
Changes
Tim-Brooks 4484cac
Changes
Tim-Brooks 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 hidden or 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 hidden or 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
141 changes: 141 additions & 0 deletions
141
modules/transport-netty4/src/main/java/org/elasticsearch/transport/SharedGroupFactory.java
This file contains hidden or 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,141 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch 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 | ||
| * | ||
| * 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 org.elasticsearch.transport; | ||
|
|
||
| import io.netty.channel.EventLoopGroup; | ||
| import io.netty.channel.nio.NioEventLoopGroup; | ||
| import io.netty.util.concurrent.Future; | ||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.common.util.concurrent.AbstractRefCounted; | ||
| import org.elasticsearch.http.HttpServerTransport; | ||
| import org.elasticsearch.http.netty4.Netty4HttpServerTransport; | ||
| import org.elasticsearch.transport.netty4.Netty4Transport; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
|
|
||
| import static org.elasticsearch.common.util.concurrent.EsExecutors.daemonThreadFactory; | ||
|
|
||
| /** | ||
| * Creates and returns {@link io.netty.channel.EventLoopGroup} instances. It will return a shared group for | ||
| * both {@link #getHttpGroup()} and {@link #getTransportGroup()} if | ||
| * {@link org.elasticsearch.http.netty4.Netty4HttpServerTransport#SETTING_HTTP_WORKER_COUNT} is configured to be 0. | ||
| * If that setting is not 0, then it will return a different group in the {@link #getHttpGroup()} call. | ||
| */ | ||
| public final class SharedGroupFactory { | ||
|
|
||
| private static final Logger logger = LogManager.getLogger(SharedGroupFactory.class); | ||
|
|
||
| private final Settings settings; | ||
| private final int workerCount; | ||
| private final int httpWorkerCount; | ||
|
|
||
| private RefCountedGroup genericGroup; | ||
| private SharedGroup dedicatedHttpGroup; | ||
|
|
||
| public SharedGroupFactory(Settings settings) { | ||
| this.settings = settings; | ||
| this.workerCount = Netty4Transport.WORKER_COUNT.get(settings); | ||
| this.httpWorkerCount = Netty4HttpServerTransport.SETTING_HTTP_WORKER_COUNT.get(settings); | ||
| } | ||
|
|
||
| public Settings getSettings() { | ||
| return settings; | ||
| } | ||
|
|
||
| public int getTransportWorkerCount() { | ||
| return workerCount; | ||
| } | ||
|
|
||
| public synchronized SharedGroup getTransportGroup() { | ||
| return getGenericGroup(); | ||
| } | ||
|
|
||
| public synchronized SharedGroup getHttpGroup() { | ||
| if (httpWorkerCount == 0) { | ||
| return getGenericGroup(); | ||
| } else { | ||
| if (dedicatedHttpGroup == null) { | ||
| NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup(httpWorkerCount, | ||
| daemonThreadFactory(settings, HttpServerTransport.HTTP_SERVER_WORKER_THREAD_NAME_PREFIX)); | ||
| dedicatedHttpGroup = new SharedGroup(new RefCountedGroup(eventLoopGroup)); | ||
| } | ||
| return dedicatedHttpGroup; | ||
| } | ||
| } | ||
|
|
||
| private SharedGroup getGenericGroup() { | ||
| if (genericGroup == null) { | ||
| EventLoopGroup eventLoopGroup = new NioEventLoopGroup(workerCount, | ||
| daemonThreadFactory(settings, TcpTransport.TRANSPORT_WORKER_THREAD_NAME_PREFIX)); | ||
| this.genericGroup = new RefCountedGroup(eventLoopGroup); | ||
| } else { | ||
| genericGroup.incRef(); | ||
| } | ||
| return new SharedGroup(genericGroup); | ||
| } | ||
|
|
||
| private static class RefCountedGroup extends AbstractRefCounted { | ||
|
|
||
| public static final String NAME = "ref-counted-event-loop-group"; | ||
| private final EventLoopGroup eventLoopGroup; | ||
|
|
||
| private RefCountedGroup(EventLoopGroup eventLoopGroup) { | ||
| super(NAME); | ||
| this.eventLoopGroup = eventLoopGroup; | ||
| } | ||
|
|
||
| @Override | ||
| protected void closeInternal() { | ||
| Future<?> shutdownFuture = eventLoopGroup.shutdownGracefully(0, 5, TimeUnit.SECONDS); | ||
| shutdownFuture.awaitUninterruptibly(); | ||
| if (shutdownFuture.isSuccess() == false) { | ||
| logger.warn("Error closing netty event loop group", shutdownFuture.cause()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Wraps the {@link RefCountedGroup}. Calls {@link RefCountedGroup#decRef()} on close. After close, | ||
| * this wrapped instance can no longer be used. | ||
| */ | ||
| public static class SharedGroup { | ||
|
|
||
| private final RefCountedGroup refCountedGroup; | ||
|
|
||
| private final AtomicBoolean isOpen = new AtomicBoolean(true); | ||
|
|
||
| private SharedGroup(RefCountedGroup refCountedGroup) { | ||
| this.refCountedGroup = refCountedGroup; | ||
| } | ||
|
|
||
| public EventLoopGroup getLowLevelGroup() { | ||
| return refCountedGroup.eventLoopGroup; | ||
| } | ||
|
|
||
| public void shutdown() { | ||
| if (isOpen.compareAndSet(true, false)) { | ||
| refCountedGroup.decRef(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.