Skip to content
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

[FLINK-36876] Support external eventLoopGroup for RestClient #25788

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
import org.apache.flink.util.function.CheckedSupplier;

import org.apache.flink.shaded.netty4.io.netty.channel.ConnectTimeoutException;
import org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup;
import org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus;

import org.slf4j.Logger;
Expand Down Expand Up @@ -215,7 +216,16 @@ public RestClusterClient(Configuration config, T clusterId) throws Exception {
public RestClusterClient(
Configuration config, T clusterId, ClientHighAvailabilityServicesFactory factory)
throws Exception {
this(config, null, clusterId, new ExponentialWaitStrategy(10L, 2000L), factory);
this(config, null, clusterId, new ExponentialWaitStrategy(10L, 2000L), factory, null);
}

public RestClusterClient(
Configuration config,
T clusterId,
ClientHighAvailabilityServicesFactory factory,
EventLoopGroup group)
throws Exception {
this(config, null, clusterId, new ExponentialWaitStrategy(10L, 2000L), factory, group);
}

@VisibleForTesting
Expand All @@ -230,15 +240,17 @@ public RestClusterClient(
restClient,
clusterId,
waitStrategy,
DefaultClientHighAvailabilityServicesFactory.INSTANCE);
DefaultClientHighAvailabilityServicesFactory.INSTANCE,
null);
}

private RestClusterClient(
Configuration configuration,
@Nullable RestClient restClient,
T clusterId,
WaitStrategy waitStrategy,
ClientHighAvailabilityServicesFactory clientHAServicesFactory)
ClientHighAvailabilityServicesFactory clientHAServicesFactory,
EventLoopGroup group)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we tag with @nullable?

throws Exception {
this.configuration = checkNotNull(configuration);

Expand All @@ -258,7 +270,8 @@ private RestClusterClient(
if (restClient != null) {
this.restClient = restClient;
} else {
this.restClient = RestClient.forUrl(configuration, executorService, jobmanagerUrl);
this.restClient =
RestClient.forUrl(configuration, executorService, jobmanagerUrl, group);
}

this.waitStrategy = checkNotNull(waitStrategy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.apache.flink.shaded.netty4.io.netty.channel.ChannelInitializer;
import org.apache.flink.shaded.netty4.io.netty.channel.ChannelOption;
import org.apache.flink.shaded.netty4.io.netty.channel.DefaultSelectStrategyFactory;
import org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup;
import org.apache.flink.shaded.netty4.io.netty.channel.SelectStrategyFactory;
import org.apache.flink.shaded.netty4.io.netty.channel.SimpleChannelInboundHandler;
import org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup;
Expand Down Expand Up @@ -143,30 +144,42 @@ public class RestClient implements AutoCloseableAsync {
ConcurrentHashMap.newKeySet();

private final List<OutboundChannelHandlerFactory> outboundChannelHandlerFactories;
private final Boolean useInternalEventLoopGroup;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: why not boolean, I assume we do not want this ever to be null


/**
* Creates a new RestClient for the provided root URL. If the protocol of the URL is "https",
* then SSL is automatically enabled for the REST client.
*/
public static RestClient forUrl(Configuration configuration, Executor executor, URL rootUrl)
throws ConfigurationException {
return forUrl(configuration, executor, rootUrl, null);
}

public static RestClient forUrl(
Configuration configuration, Executor executor, URL rootUrl, EventLoopGroup group)
throws ConfigurationException {
Preconditions.checkNotNull(configuration);
Preconditions.checkNotNull(rootUrl);
if ("https".equals(rootUrl.getProtocol())) {
configuration = configuration.clone();
configuration.set(SSL_REST_ENABLED, true);
}
return new RestClient(configuration, executor, rootUrl.getHost(), rootUrl.getPort());
return new RestClient(configuration, executor, rootUrl.getHost(), rootUrl.getPort(), group);
}

public RestClient(Configuration configuration, Executor executor)
throws ConfigurationException {
this(configuration, executor, null, -1);
this(configuration, executor, null, -1, null);
}

public RestClient(Configuration configuration, Executor executor, String host, int port)
public RestClient(
Configuration configuration,
Executor executor,
String host,
int port,
EventLoopGroup group)
throws ConfigurationException {
this(configuration, executor, host, port, DefaultSelectStrategyFactory.INSTANCE);
this(configuration, executor, host, port, DefaultSelectStrategyFactory.INSTANCE, group);
}

@VisibleForTesting
Expand All @@ -175,15 +188,16 @@ public RestClient(Configuration configuration, Executor executor, String host, i
Executor executor,
SelectStrategyFactory selectStrategyFactory)
throws ConfigurationException {
this(configuration, executor, null, -1, selectStrategyFactory);
this(configuration, executor, null, -1, selectStrategyFactory, null);
}

private RestClient(
Configuration configuration,
Executor executor,
String host,
int port,
SelectStrategyFactory selectStrategyFactory)
SelectStrategyFactory selectStrategyFactory,
EventLoopGroup group)
throws ConfigurationException {
Preconditions.checkNotNull(configuration);
this.executor = Preconditions.checkNotNull(executor);
Expand Down Expand Up @@ -264,15 +278,21 @@ protected void initChannel(SocketChannel socketChannel) {
}
};

// No NioEventLoopGroup constructor available that allows passing nThreads, threadFactory,
// and selectStrategyFactory without also passing a SelectorProvider, so mimicking its
// default value seen in other constructors
NioEventLoopGroup group =
new NioEventLoopGroup(
1,
new ExecutorThreadFactory("flink-rest-client-netty"),
SelectorProvider.provider(),
selectStrategyFactory);
if (group == null) {
// No NioEventLoopGroup constructor available that allows passing nThreads,
// threadFactory,
// and selectStrategyFactory without also passing a SelectorProvider, so mimicking its
// default value seen in other constructors
group =
new NioEventLoopGroup(
1,
new ExecutorThreadFactory("flink-rest-client-netty"),
SelectorProvider.provider(),
selectStrategyFactory);
useInternalEventLoopGroup = true;
} else {
useInternalEventLoopGroup = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we check that the supplied group is not shutting down or shutdown

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the channel is NioSocketChannel.class is this always the case for external groups?

Copy link
Contributor Author

@chenyuzhi459 chenyuzhi459 Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the extenal service doesn't care the type of SocketChannel. As the jira says, it just pass a shared event group to avoid heap memory leak.

Maybe it's a better choice to specify the contruct param group as NioEventLoopGroup, which could avoid the type error building Bootstrap instance ?

}

bootstrap = new Bootstrap();
bootstrap
Expand Down Expand Up @@ -317,7 +337,7 @@ private CompletableFuture<Void> shutdownInternally(Duration timeout) {
LOG.debug("Shutting down rest endpoint.");

if (bootstrap != null) {
if (bootstrap.config().group() != null) {
if (bootstrap.config().group() != null && useInternalEventLoopGroup) {
bootstrap
.config()
.group()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@
import org.apache.flink.testutils.TestingUtils;
import org.apache.flink.testutils.executor.TestExecutorExtension;
import org.apache.flink.util.NetUtils;
import org.apache.flink.util.concurrent.ExecutorThreadFactory;
import org.apache.flink.util.concurrent.Executors;
import org.apache.flink.util.function.CheckedSupplier;

import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.flink.shaded.netty4.io.netty.channel.Channel;
import org.apache.flink.shaded.netty4.io.netty.channel.ConnectTimeoutException;
import org.apache.flink.shaded.netty4.io.netty.channel.DefaultSelectStrategyFactory;
import org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup;
import org.apache.flink.shaded.netty4.io.netty.channel.SelectStrategy;
import org.apache.flink.shaded.netty4.io.netty.channel.SelectStrategyFactory;
import org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup;
import org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus;

import org.assertj.core.api.InstanceOfAssertFactories;
Expand Down Expand Up @@ -115,6 +118,20 @@ void testConnectionTimeout() throws Exception {
}
}

@Test
void testExternalEventGroup() throws Exception {
EventLoopGroup externalGroup =
new NioEventLoopGroup(
1, new ExecutorThreadFactory("flink-rest-client-netty-external"));

final RestClient restClient =
new RestClient(
new Configuration(), Executors.directExecutor(), null, -1, externalGroup);
restClient.closeAsync();

assertThat(externalGroup.isShuttingDown() || externalGroup.isShutdown()).isFalse();
}

@Test
void testInvalidVersionRejection() throws Exception {
try (final RestClient restClient =
Expand Down