-
Notifications
You must be signed in to change notification settings - Fork 599
HDDS-15149. Limit Connections Created by DataNode GRPC Server #10184
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
Open
YutaLin
wants to merge
8
commits into
apache:master
Choose a base branch
from
YutaLin:HDDS-15149_limit_connections_on_datanode_grpc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
232c5d7
feat: add GrpcConnectionLimitFilter
YutaLin d2493f8
feat: add max connection in data node configuration
YutaLin e055be5
feat: add connection filter in XceiverServerGrpc
YutaLin 5c58adb
test: data node configuration and connection limit filter
YutaLin 10ff1ee
fix: test
YutaLin 63e6c63
doc: java doc
YutaLin c0e3d89
feat: add so_backlog to prevent burst connections
YutaLin 9d8f62a
test: add so_backlog set test
YutaLin 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
130 changes: 130 additions & 0 deletions
130
.../org/apache/hadoop/ozone/container/common/transport/server/GrpcConnectionLimitFilter.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,130 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF 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.apache.hadoop.ozone.container.common.transport.server; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
| import org.apache.ratis.thirdparty.io.grpc.Attributes; | ||
| import org.apache.ratis.thirdparty.io.grpc.ServerTransportFilter; | ||
| import org.apache.ratis.thirdparty.io.grpc.Status; | ||
| import org.apache.ratis.thirdparty.io.grpc.StatusRuntimeException; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * A gRPC ServerTransportFilter that limits the number of concurrent | ||
| * connections to the Datanode gRPC server. | ||
| * | ||
| * <p>When the connection limit is reached, new connections are rejected | ||
| * with a RESOURCE_EXHAUSTED status, preventing file descriptor exhaustion. | ||
| */ | ||
| public class GrpcConnectionLimitFilter extends ServerTransportFilter { | ||
|
|
||
| private static final Logger LOG = | ||
| LoggerFactory.getLogger(GrpcConnectionLimitFilter.class); | ||
|
|
||
| private final int maxConnections; | ||
| private final AtomicInteger activeConnections = new AtomicInteger(0); | ||
| private final AtomicLong totalAcceptedConnections = new AtomicLong(0); | ||
| private final AtomicLong totalRejectedConnections = new AtomicLong(0); | ||
|
|
||
| /** | ||
| * Creates a new connection limit filter. | ||
| * | ||
| * @param maxConnections the maximum number of concurrent connections allowed. | ||
| * If zero or negative, no limit is enforced. | ||
| */ | ||
| public GrpcConnectionLimitFilter(int maxConnections) { | ||
| this.maxConnections = maxConnections; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public Attributes transportReady(Attributes transportAttrs) { | ||
| if (maxConnections <= 0) { | ||
| return super.transportReady(transportAttrs); | ||
| } | ||
|
|
||
| int current = activeConnections.incrementAndGet(); | ||
| if (current > maxConnections) { | ||
| activeConnections.decrementAndGet(); | ||
| totalRejectedConnections.incrementAndGet(); | ||
| LOG.warn("Connection rejected: limit {} reached, current active: {}", | ||
| maxConnections, current - 1); | ||
| throw new StatusRuntimeException( | ||
| Status.RESOURCE_EXHAUSTED.withDescription( | ||
| "Datanode connection limit exceeded: " + maxConnections)); | ||
| } | ||
|
|
||
| totalAcceptedConnections.incrementAndGet(); | ||
| if (LOG.isDebugEnabled()) { | ||
| LOG.debug("Connection accepted: active connections: {}/{}", | ||
| current, maxConnections); | ||
| } | ||
| return super.transportReady(transportAttrs); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public void transportTerminated(Attributes transportAttrs) { | ||
| if (maxConnections > 0) { | ||
| int current = activeConnections.decrementAndGet(); | ||
| if (LOG.isDebugEnabled()) { | ||
| LOG.debug("Connection terminated: active connections: {}/{}", | ||
| current, maxConnections); | ||
| } | ||
| } | ||
| super.transportTerminated(transportAttrs); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the current number of active connections. | ||
| * @return the number of active connections | ||
| */ | ||
| public int getActiveConnections() { | ||
| return activeConnections.get(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the total number of connections accepted since server start. | ||
| * @return the total accepted connections count | ||
| */ | ||
| public long getTotalAcceptedConnections() { | ||
| return totalAcceptedConnections.get(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the total number of connections rejected since server start. | ||
| * @return the total rejected connections count | ||
| */ | ||
| public long getTotalRejectedConnections() { | ||
| return totalRejectedConnections.get(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the configured maximum number of connections. | ||
| * @return the maximum connections limit | ||
| */ | ||
| public int getMaxConnections() { | ||
| return maxConnections; | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@YutaLin , thanks for working on this.
Could you try use NettyServerBuilder's withChildOption, to set ChannelOption.SO_BACKLOG?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @ChenSammi, thanks for the review,
I've added so_backlog to prevent burst connections, also keep connection limit filter to control sustained high connection.