-
Notifications
You must be signed in to change notification settings - Fork 613
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add observability for pending connections
Summary: # Issue Thrift currently does not have any observability into the number of pending connections which are waiting for acceptance by the IO Worker threads. This observability is especially important during connection storms, which may possibly overload the service with the large number of incoming connections causing OOMs, which was the case for the uCache service (S399591, S400702) # Change Introduce observability into pending connections that are enqueued for the IO workers for processing of heavyweight TLS Handshake and other connection acceptance related operations. **Counter prefix:** `thrift.pending_connections` **Counter types:** - P50 - median, helps to understand the "average" pending connections number under normal conditions. - P99 - captures rare but impactful high-congestion events. - P100 - reports maximum number of pending connections observed. Example values: {F1936343509} Reviewed By: robertroeser Differential Revision: D64355577 fbshipit-source-id: 935281b926add87adae4d045befd81b2f5286ffc
- Loading branch information
1 parent
909e690
commit 1953621
Showing
5 changed files
with
133 additions
and
2 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
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
67 changes: 67 additions & 0 deletions
67
thrift/lib/cpp2/server/metrics/PendingConnectionsMetrics.cpp
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,67 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* 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. | ||
*/ | ||
|
||
#include <thrift/lib/cpp2/server/metrics/PendingConnectionsMetrics.h> | ||
|
||
THRIFT_FLAG_DEFINE_bool(enable_pending_connections_metrics, true); | ||
|
||
namespace { | ||
bool isFeatureDisabled() { | ||
return !THRIFT_FLAG(enable_pending_connections_metrics); | ||
} | ||
} // namespace | ||
|
||
namespace apache::thrift { | ||
PendingConnectionsMetrics::PendingConnectionsMetrics( | ||
std::shared_ptr<server::TServerObserver> observer) | ||
: observer_(std::move(observer)) {} | ||
|
||
void PendingConnectionsMetrics::onConnectionEnqueuedToIoWorker() { | ||
if (isFeatureDisabled()) { | ||
return; | ||
} | ||
pendingConnections_++; | ||
onPendingConnectionsChange(); | ||
} | ||
|
||
void PendingConnectionsMetrics::onConnectionDropped( | ||
const std::string& errorMsg) { | ||
if (isFeatureDisabled()) { | ||
return; | ||
} | ||
// When connection expires in the queue, before it is accepted by an IO | ||
// Worker, AsyncServerSocker reports the connection as dropped. The queue | ||
// counter is decremented in this case for accurate queue size tracking. | ||
if (errorMsg.find("Exceeded deadline for accepting connection") != | ||
std::string::npos) { | ||
pendingConnections_--; | ||
onPendingConnectionsChange(); | ||
} | ||
} | ||
|
||
void PendingConnectionsMetrics::onConnectionDequedByIoWorker() { | ||
if (isFeatureDisabled()) { | ||
return; | ||
} | ||
pendingConnections_--; | ||
onPendingConnectionsChange(); | ||
} | ||
|
||
void PendingConnectionsMetrics::onPendingConnectionsChange() { | ||
observer_->pendingConnections(pendingConnections_.load()); | ||
} | ||
|
||
} // namespace apache::thrift |
42 changes: 42 additions & 0 deletions
42
thrift/lib/cpp2/server/metrics/PendingConnectionsMetrics.h
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,42 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* 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. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <thrift/lib/cpp/server/TServerObserver.h> | ||
#include <thrift/lib/cpp2/Flags.h> | ||
|
||
namespace apache::thrift { | ||
// This class is responsible for recording metrics associated with inflight | ||
// connections. An inflight connection is a connection that has been accepted by | ||
// an Acceptor thread, but has not yet been accepted by an IO Worker thread for | ||
// further processing of heavier weight operations like TLS Handshake. | ||
class PendingConnectionsMetrics { | ||
public: | ||
explicit PendingConnectionsMetrics( | ||
std::shared_ptr<server::TServerObserver> observer); | ||
|
||
void onConnectionEnqueuedToIoWorker(); | ||
void onConnectionDequedByIoWorker(); | ||
void onConnectionDropped(const std::string& errorMsg); | ||
|
||
private: | ||
std::shared_ptr<server::TServerObserver> observer_; | ||
folly::relaxed_atomic<int32_t> pendingConnections_{0}; | ||
|
||
void onPendingConnectionsChange(); | ||
}; | ||
} // namespace apache::thrift |