Skip to content

Commit 1e68b78

Browse files
authored
conn pool: state variable cleanup and more ENVOY_BUGs (envoyproxy#39381)
Rename `state_` to a more descriptive name and make it private. Convert some ASSERTs to ENVOY_BUGs for better detection of accounting bugs. Signed-off-by: Greg Greenway <[email protected]>
1 parent a8a9b9e commit 1e68b78

File tree

2 files changed

+29
-31
lines changed

2 files changed

+29
-31
lines changed

source/common/conn_pool/conn_pool_base.cc

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ void ConnPoolImplBase::assertCapacityCountsAreCorrect() {
4040
SLOW_ASSERT(currentUnusedCapacity(busy_clients_) <= 0, dumpState());
4141

4242
if (ready_clients_.empty()) {
43-
ASSERT((connecting_and_connected_stream_capacity_ - connecting_stream_capacity_) <= 0,
44-
dumpState());
43+
ENVOY_BUG((connecting_and_connected_stream_capacity_ - connecting_stream_capacity_) <= 0,
44+
dumpState());
4545
} else {
46-
ASSERT((connecting_and_connected_stream_capacity_ - connecting_stream_capacity_) > 0,
47-
dumpState());
46+
ENVOY_BUG((connecting_and_connected_stream_capacity_ - connecting_stream_capacity_) > 0,
47+
dumpState());
4848
}
4949
}
5050

@@ -53,21 +53,21 @@ ConnPoolImplBase::ConnPoolImplBase(
5353
Event::Dispatcher& dispatcher, const Network::ConnectionSocket::OptionsSharedPtr& options,
5454
const Network::TransportSocketOptionsConstSharedPtr& transport_socket_options,
5555
Upstream::ClusterConnectivityState& state)
56-
: state_(state), host_(host), priority_(priority), dispatcher_(dispatcher),
57-
socket_options_(options), transport_socket_options_(transport_socket_options),
56+
: host_(host), priority_(priority), dispatcher_(dispatcher), socket_options_(options),
57+
transport_socket_options_(transport_socket_options), cluster_connectivity_state_(state),
5858
upstream_ready_cb_(dispatcher_.createSchedulableCallback([this]() { onUpstreamReady(); })) {}
5959

6060
ConnPoolImplBase::~ConnPoolImplBase() {
61-
ASSERT(isIdleImpl(), dumpState());
62-
ASSERT(connecting_stream_capacity_ == 0, dumpState());
63-
ASSERT(connecting_and_connected_stream_capacity_ == 0, dumpState());
61+
ENVOY_BUG(isIdleImpl(), dumpState());
62+
ENVOY_BUG(connecting_stream_capacity_ == 0, dumpState());
63+
ENVOY_BUG(connecting_and_connected_stream_capacity_ == 0, dumpState());
6464
}
6565

6666
void ConnPoolImplBase::deleteIsPendingImpl() {
6767
deferred_deleting_ = true;
68-
ASSERT(isIdleImpl(), dumpState());
69-
ASSERT(connecting_stream_capacity_ == 0, dumpState());
70-
ASSERT(connecting_and_connected_stream_capacity_ == 0, dumpState());
68+
ENVOY_BUG(isIdleImpl(), dumpState());
69+
ENVOY_BUG(connecting_stream_capacity_ == 0, dumpState());
70+
ENVOY_BUG(connecting_and_connected_stream_capacity_ == 0, dumpState());
7171
}
7272

7373
void ConnPoolImplBase::destructAllConnections() {
@@ -197,9 +197,9 @@ ConnPoolImplBase::tryCreateNewConnection(float global_preconnect_ratio) {
197197
return ConnectionResult::FailedToCreateConnection;
198198
}
199199
ASSERT(client->state() == ActiveClient::State::Connecting, dumpState());
200-
ASSERT(std::numeric_limits<uint64_t>::max() - connecting_stream_capacity_ >=
201-
static_cast<uint64_t>(client->currentUnusedCapacity()),
202-
dumpState());
200+
ENVOY_BUG(std::numeric_limits<uint64_t>::max() - connecting_stream_capacity_ >=
201+
static_cast<uint64_t>(client->currentUnusedCapacity()),
202+
dumpState());
203203
ASSERT(client->real_host_description_);
204204
// Increase the connecting capacity to reflect the streams this connection can serve.
205205
incrConnectingAndConnectedStreamCapacity(client->currentUnusedCapacity(), *client);
@@ -249,7 +249,7 @@ void ConnPoolImplBase::attachStreamToClient(Envoy::ConnectionPool::ActiveClient&
249249
decrConnectingAndConnectedStreamCapacity(1, client);
250250
}
251251
// Track the new active stream.
252-
state_.incrActiveStreams(1);
252+
cluster_connectivity_state_.incrActiveStreams(1);
253253
num_active_streams_++;
254254
host_->stats().rq_total_.inc();
255255
host_->stats().rq_active_.inc();
@@ -266,7 +266,7 @@ void ConnPoolImplBase::onStreamClosed(Envoy::ConnectionPool::ActiveClient& clien
266266
debug, "destroying stream: {} active remaining, readyForStream {}, currentUnusedCapacity {}",
267267
client, client.numActiveStreams(), client.readyForStream(), client.currentUnusedCapacity());
268268
ASSERT(num_active_streams_ > 0, dumpState());
269-
state_.decrActiveStreams(1);
269+
cluster_connectivity_state_.decrActiveStreams(1);
270270
num_active_streams_--;
271271
host_->stats().rq_active_.dec();
272272
host_->cluster().trafficStats()->upstream_rq_active_.dec();
@@ -378,7 +378,7 @@ void ConnPoolImplBase::onUpstreamReady() {
378378
ENVOY_CONN_LOG(debug, "attaching to next stream", *client);
379379
// Pending streams are pushed onto the front, so pull from the back.
380380
attachStreamToClient(*client, pending_streams_.back()->context());
381-
state_.decrPendingStreams(1);
381+
cluster_connectivity_state_.decrPendingStreams(1);
382382
pending_streams_.pop_back();
383383
}
384384
if (!pending_streams_.empty()) {
@@ -622,7 +622,7 @@ void ConnPoolImplBase::onConnectionEvent(ActiveClient& client, absl::string_view
622622
client.connect_timer_->disableTimer();
623623
client.connect_timer_.reset();
624624

625-
ASSERT(connecting_stream_capacity_ >= client.currentUnusedCapacity(), dumpState());
625+
ENVOY_BUG(connecting_stream_capacity_ >= client.currentUnusedCapacity(), dumpState());
626626
connecting_stream_capacity_ -= client.currentUnusedCapacity();
627627
client.has_handshake_completed_ = true;
628628
client.conn_connect_ms_->complete();
@@ -691,7 +691,7 @@ void ConnPoolImplBase::purgePendingStreams(
691691
absl::string_view failure_reason, ConnectionPool::PoolFailureReason reason) {
692692
// NOTE: We move the existing pending streams to a temporary list. This is done so that
693693
// if retry logic submits a new stream to the pool, we don't fail it inline.
694-
state_.decrPendingStreams(pending_streams_.size());
694+
cluster_connectivity_state_.decrPendingStreams(pending_streams_.size());
695695
pending_streams_to_purge_ = std::move(pending_streams_);
696696
while (!pending_streams_to_purge_.empty()) {
697697
PendingStreamPtr stream =
@@ -703,7 +703,7 @@ void ConnPoolImplBase::purgePendingStreams(
703703

704704
bool ConnPoolImplBase::connectingConnectionIsExcess(const ActiveClient& client) const {
705705
ASSERT(!client.hasHandshakeCompleted());
706-
ASSERT(connecting_stream_capacity_ >= client.currentUnusedCapacity(), dumpState());
706+
ENVOY_BUG(connecting_stream_capacity_ >= client.currentUnusedCapacity(), dumpState());
707707
// If perUpstreamPreconnectRatio is one, this simplifies to checking if there would still be
708708
// sufficient connecting stream capacity to serve all pending streams if the most recent client
709709
// were removed from the picture.
@@ -725,7 +725,7 @@ void ConnPoolImplBase::onPendingStreamCancel(PendingStream& stream,
725725
// and there is no need to call its onPoolFailure callback.
726726
stream.removeFromList(pending_streams_to_purge_);
727727
} else {
728-
state_.decrPendingStreams(1);
728+
cluster_connectivity_state_.decrPendingStreams(1);
729729
stream.removeFromList(pending_streams_);
730730
}
731731
if (policy == Envoy::ConnectionPool::CancelPolicy::CloseExcess) {
@@ -760,7 +760,7 @@ void ConnPoolImplBase::decrConnectingAndConnectedStreamCapacity(uint32_t delta,
760760
if (!client.hasHandshakeCompleted()) {
761761
// If still doing handshake, it is contributing to the local connecting stream capacity. Update
762762
// the capacity as well.
763-
ASSERT(connecting_stream_capacity_ >= delta, dumpState());
763+
ENVOY_BUG(connecting_stream_capacity_ >= delta, dumpState());
764764
connecting_stream_capacity_ -= delta;
765765
}
766766
}
@@ -796,7 +796,7 @@ void ConnPoolImplBase::onUpstreamReadyForEarlyData(ActiveClient& client) {
796796
if (stream.can_send_early_data_) {
797797
ENVOY_CONN_LOG(debug, "creating stream for early data.", client);
798798
attachStreamToClient(client, stream.context());
799-
state_.decrPendingStreams(1);
799+
cluster_connectivity_state_.decrPendingStreams(1);
800800
stream.removeFromList(pending_streams_);
801801
}
802802
if (stop_iteration) {

source/common/conn_pool/conn_pool_base.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,11 @@ class ConnPoolImplBase : protected Logger::Loggable<Logger::Id::pool> {
279279
bool hasPendingStreams() const { return !pending_streams_.empty(); }
280280

281281
void decrClusterStreamCapacity(uint32_t delta) {
282-
state_.decrConnectingAndConnectedStreamCapacity(delta);
282+
cluster_connectivity_state_.decrConnectingAndConnectedStreamCapacity(delta);
283283
connecting_and_connected_stream_capacity_ -= delta;
284284
}
285285
void incrClusterStreamCapacity(uint32_t delta) {
286-
state_.incrConnectingAndConnectedStreamCapacity(delta);
286+
cluster_connectivity_state_.incrConnectingAndConnectedStreamCapacity(delta);
287287
connecting_and_connected_stream_capacity_ += delta;
288288
}
289289
void dumpState(std::ostream& os, int indent_level = 0) const {
@@ -304,8 +304,6 @@ class ConnPoolImplBase : protected Logger::Loggable<Logger::Id::pool> {
304304
// Helper for use as the 2nd argument to ASSERT.
305305
std::string dumpState() const;
306306

307-
Upstream::ClusterConnectivityState& state() { return state_; }
308-
309307
void decrConnectingAndConnectedStreamCapacity(uint32_t delta, ActiveClient& client);
310308
void incrConnectingAndConnectedStreamCapacity(uint32_t delta, ActiveClient& client);
311309

@@ -349,14 +347,12 @@ class ConnPoolImplBase : protected Logger::Loggable<Logger::Id::pool> {
349347
ConnectionPool::Cancellable*
350348
addPendingStream(Envoy::ConnectionPool::PendingStreamPtr&& pending_stream) {
351349
LinkedList::moveIntoList(std::move(pending_stream), pending_streams_);
352-
state_.incrPendingStreams(1);
350+
cluster_connectivity_state_.incrPendingStreams(1);
353351
return pending_streams_.front().get();
354352
}
355353

356354
bool hasActiveStreams() const { return num_active_streams_ > 0; }
357355

358-
Upstream::ClusterConnectivityState& state_;
359-
360356
const Upstream::HostConstSharedPtr host_;
361357
const Upstream::ResourcePriority priority_;
362358

@@ -399,6 +395,8 @@ class ConnPoolImplBase : protected Logger::Loggable<Logger::Id::pool> {
399395

400396
void assertCapacityCountsAreCorrect();
401397

398+
Upstream::ClusterConnectivityState& cluster_connectivity_state_;
399+
402400
std::list<PendingStreamPtr> pending_streams_;
403401

404402
// The number of streams that can be immediately dispatched from the current

0 commit comments

Comments
 (0)