Skip to content

Track global cx limit in overload manager - #28202

Merged
kyessenov merged 30 commits into
envoyproxy:mainfrom
nezdolik:overload-global-cx-limit
Oct 3, 2023
Merged

kyessenov merged 30 commits into
envoyproxy:mainfrom
nezdolik:overload-global-cx-limit

Conversation

@nezdolik

@nezdolik nezdolik commented Jun 30, 2023 •

Copy link
Copy Markdown
Member

Deprecate runtime key overload.global_downstream_max_connections and track global active downstream connections limit in overload manager instead. The runtime key is still usable but using it yields deprecation warning. If both mechanisms are configured (overload resource monitor and runtime key), overload manager config will be preferred.

Commit Message:
Additional Description:
Risk Level: High (change affects request hot path)
Testing: Done
Docs Changes: Done
Release Notes: TBD
Platform Specific Features: NA
Fixes #12419
Deprecated: Added deprecation note for runtime key overload.global_downstream_max_connections

@nezdolik

nezdolik commented Jun 30, 2023 •

Copy link
Copy Markdown
Member Author

@KBaichoo could you validate the suggested approach (for tracking global_cx_limit via overload manager)? If it looks good, i will continue with adding tests and deprecating the runtime flag.

@KBaichoo KBaichoo self-assigned this Jun 30, 2023

@KBaichoo KBaichoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

At a high level the approach makes sense, thanks for working on this

// Check if deprecated runtime flag `overload.global_downstream_max_connections` is configured
// simultaneously with downstream connections monitor in overload manager.
if (runtime_.threadsafeSnapshot()->get(GlobalMaxCxRuntimeKey)) {
ENVOY_LOG_MISC(warn,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Might make sense using ENVOY_LOG_ONCE macro for this (to avoid spamming) on every connection

Signed-off-by: Kateryna Nezdolii <[email protected]>
@nezdolik
nezdolik force-pushed the overload-global-cx-limit branch from 0864861 to 45d8591 Compare July 24, 2023 23:21
Signed-off-by: Kateryna Nezdolii <[email protected]>
@nezdolik
nezdolik force-pushed the overload-global-cx-limit branch from 45d8591 to 5baade6 Compare July 24, 2023 23:23
#include "test/integration/integration.h"
#include "test/test_common/test_runtime.h"

using testing::InvokeWithoutArgs;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

may not be used

@nezdolik

Copy link
Copy Markdown
Member Author

There are still few TODOs and tests left, but implementation and all the main tests are ready. Since proactive resource monitors do not make use of overload actions (and entire overload manager mechanism of "subscribe for updates for this action"), we should either make it clear in user docs that action configuration is not needed for such monitor or introduce separate monitor type in api.

@nezdolik nezdolik changed the title [WIP] Track global cx limit in overload manager Track global cx limit in overload manager Jul 25, 2023
@nezdolik
nezdolik force-pushed the overload-global-cx-limit branch from 59a2d45 to bd33466 Compare July 25, 2023 13:25
Signed-off-by: Kateryna Nezdolii <[email protected]>
Comment thread test/integration/BUILD Outdated

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

disputable

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

probably marked large due to amount of memory the test uses? You could likely pare it down similar to 3c84818#diff-47f3211970111aaa3c26392c795eb8bd7494049106c2b47693cd01bd621fcec8 if you want

@nezdolik
nezdolik force-pushed the overload-global-cx-limit branch from bd33466 to cf266a9 Compare July 25, 2023 13:27
@@ -0,0 +1,213 @@
#include <unordered_map>

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

is this needed?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

looks unused

@nezdolik

nezdolik commented Jul 25, 2023 •

Copy link
Copy Markdown
Member Author

@KBaichoo would this change benefit from measuring thread contention diff (main vs PR)? In old version we as well used atomic variable for tracking global downstream conns but with less synchronization (e.g. no CAS with explicit memory ordering when reading/writing to/from atomic)

ASSERT_TRUE(tcp_clients.back()->connected());
}
};
// todo (nezdolik) this should be logged once per each run (ipv4,ipv6),

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@KBaichoo any ideas what the root cause can be here? Log macro uses static atomic variable for counting number of times logged, maybe it's not being reset across same test runs (but with different test params, e.g. ipv4/ipv6)

Kateryna Nezdolii added 2 commits July 25, 2023 20:48

@KBaichoo KBaichoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for working on this, here's a pass

@@ -0,0 +1,213 @@
#include <unordered_map>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

looks unused

Comment thread test/integration/BUILD Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

probably marked large due to amount of memory the test uses? You could likely pare it down similar to 3c84818#diff-47f3211970111aaa3c26392c795eb8bd7494049106c2b47693cd01bd621fcec8 if you want

Comment thread source/server/overload_manager_impl.cc Outdated
int64_t currentResourceUsage(OverloadProactiveResourceName resource_name) override {
const auto proactive_resource = proactive_resources_->find(resource_name);
if (proactive_resource == proactive_resources_->end()) {
ENVOY_LOG_MISC(warn, " {Failed to get resource usage for unknown proactive resource }");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Given this is an enum class the resource name is likely not corrupted; this is more likely if the resource monitor isn't enabled?

Comment thread source/server/overload_manager_impl.cc Outdated
const auto proactive_resource = proactive_resources_->find(resource_name);
if (proactive_resource == proactive_resources_->end()) {
ENVOY_LOG_MISC(warn, " {Failed to get resource usage for unknown proactive resource }");
return false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

yikes old c implicit conversion from bool -> int. Can we avoid somehow?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hah, I think that was a copy paste from another existing method and compilation silently passed so went unnoticed by me 🙂

* Returns current resource usage (most recent read) tracked by monitor.
* @param name of resource monitor to check.
*/
virtual int64_t currentResourceUsage(OverloadProactiveResourceName resource_name) PURE;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this only going to be used for tests, as currently done in your integration test?

What do you think about const ProactiveResourceMonitor& getProactiveResourceMonitorForTest() or using a test peer freidn or similar ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yes, good point

initialize();
}

std::string counterPrefix() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

const method

num_downstream_conns_reached.Notify();
}
// TODO(mattklein123): Do not use a real sleep here. Switch to events with waitFor().
timeSystem().realSleepDoNotUseWithoutScrutiny(std::chrono::milliseconds(50));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we will be sleeping on main thread (very bad)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

this review comment is not yet resolved

// TODO(mattklein123): Do not use a real sleep here. Switch to events with waitFor().
timeSystem().realSleepDoNotUseWithoutScrutiny(std::chrono::milliseconds(50));
}
if (overload_state.currentResourceUsage(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

don't understand why this is repeated twice in the same loop iteration

if (overload_state.currentResourceUsage(
Server::OverloadProactiveResourceName::GlobalDownstreamMaxConnections) ==
expected_connections) {
num_downstream_conns_reached.Notify();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

calling Notify() more that once on same notification is undefined behavior -- we should break out of here it we satisfied the condition

};
// TODO(nezdolik) this should be logged once per each run (ipv4,ipv6),
// but second run does not contain the log line even though logging code is hit. Some macro+test
// setup weirdness.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yea I think it is as you pointed out due to the local static in the macro that this will just be logged once for the test.

I think we could run the test suite with just a IP version as it shouldn't be depedent on the IP version? (as one workaround).

@KBaichoo

Copy link
Copy Markdown
Contributor

/wait

@nezdolik

Copy link
Copy Markdown
Member Author

Thanks for review @KBaichoo, I will get back to it in 10 days after vacation.

Signed-off-by: Kateryna Nezdolii <[email protected]>

@KBaichoo KBaichoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Windows CI still broken:
test/extensions/listener_managers/listener_manager/listener_manager_impl_test.cc(6106): error C2661: 'Envoy::Network::AcceptedSocketImpl::AcceptedSocketImpl': no overloaded function takes 3 arguments

/wait

Signed-off-by: Kateryna Nezdolii <[email protected]>
@nezdolik

Copy link
Copy Markdown
Member Author

Apologies for delay, had to deal with a high priority task. Will wait if there are any other review comments left and then add the changelog.

@KBaichoo

Copy link
Copy Markdown
Contributor

ping @RyanTheOptimist

I think this is waiting for your input.

overload_state_->tryDeallocateResource(
Server::OverloadProactiveResourceName::GlobalDownstreamMaxConnections, 1);
} else {
ASSERT(global_accepted_socket_count_.load() > 0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hm. I'm slightly concerned about this going away. I've hit this ASSERT() in the past by buggingly changing some socket accounting. Do we need to remove this assert, even if we're using the overload manager generally?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@RyanTheOptimist we could add similar assert in overload manager if branch above as extra layer of protection.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think that would be great.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

while this protection would be useful for programmatic errors, it will not cover all race cases, since check for current.load() > 0 reads atomic value in one place and then later tries to decrement it, there is still a chance of race condition (e.g. another worker threads have consumed all resource usage between assert and actual decrement). Overload manager logic has protection against this case: https://github.com/envoyproxy/envoy/blob/main/source/extensions/resource_monitors/downstream_connections/downstream_connections_monitor.cc#L41

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@RyanTheOptimist do you think assertion is still needed given that overload manager method would fail to deallocate resource if current value is <=0 ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, I see. Ok, fair enough.

ASSERT(global_accepted_socket_count_.load() > 0);
--global_accepted_socket_count_;
if (track_global_cx_limit_in_overload_manager_) {
overload_state_->tryDeallocateResource(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would have expected to see a pair of calls, one in the constructor to allocate, and one in the destructor to deallocate. But clearly that's not the model we're using here. Can you say more?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@RyanTheOptimist the explanation is in comments here: https://github.com/envoyproxy/envoy/pull/28202/files/1bfae38e6f7c34c04836fe235ba37b97f359f1f3#diff-27e11486c4836b7d7f730f65c6591cc6794ffb35a4273c148fd3cb54c9ba77cfR40
Instead of checking if it is possible to allocate resource/connection and then later in the code actually allocating the resource (like the old way does), we do a single call to overload manager in tcp listener which both checks for possibility of allocation and tries to allocate resource to reduce the possibility of race conditions.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah! That makes sense. Mind adding a brief comment here explaining that it was allocated in TcpListenerImpl::rejectCxOverGlobalLimit()?

@RyanTheOptimist

Copy link
Copy Markdown
Contributor

ping @RyanTheOptimist

I think this is waiting for your input.

Oh, gosh, sorry. I thought it was waiting on you. Sorry about that! Added my comments.

@RyanTheOptimist

Copy link
Copy Markdown
Contributor

/wait

Signed-off-by: Kateryna Nezdolii <[email protected]>
@nezdolik

Copy link
Copy Markdown
Member Author

@RyanTheOptimist addressed review comments+added changelog, did not add assert statement yet, waiting for input.

@RyanTheOptimist

Copy link
Copy Markdown
Contributor

Looks like this needs a main merge.
/wait

Kateryna Nezdolii added 2 commits September 29, 2023 19:57
Signed-off-by: Kateryna Nezdolii <[email protected]>

@RyanTheOptimist RyanTheOptimist left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, assuming CI passes

@nezdolik

nezdolik commented Oct 2, 2023

Copy link
Copy Markdown
Member Author

/retest

@nezdolik

nezdolik commented Oct 2, 2023

Copy link
Copy Markdown
Member Author

@RyanTheOptimist looks like all the checks are passing now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

overload manager: overload signals based on number of downstream connections and active requests

4 participants