Skip to content

Proactive checks in overload manager - #15707

Closed
nezdolik wants to merge 25 commits into
envoyproxy:mainfrom
nezdolik:reactive-checks-overload
Closed

nezdolik wants to merge 25 commits into
envoyproxy:mainfrom
nezdolik:reactive-checks-overload

Conversation

@nezdolik

@nezdolik nezdolik commented Mar 26, 2021 •

Copy link
Copy Markdown
Member

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

For an explanation of how to fill out the fields, please see the relevant section
in PULL_REQUESTS.md

Commit Message:
Additional Description: This PR contains addition of proactive checks into overload manager/thread local state, new proactive resource monitor for active downstream connections and plugging of proactive checks into tcp listener (for rejecting connections).
Risk Level:
Testing:
Docs Changes:
Release Notes:
Platform Specific Features:
[Optional Runtime guard:]
[Optional Fixes #Issue]
[Optional Deprecated:]
[Optional API Considerations:]

@nezdolik

Copy link
Copy Markdown
Member Author

There is bunch of duplicated code. Will refactor it after ensuring that whole thing works in tests.

@antoniovicente antoniovicente 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 taking on this improvement to overload manager to allow for proactive checks when accepting new connections.

Comment thread source/server/overload_manager_impl.h Outdated
void onSuccess(const ResourceUsage& usage) override;
void onFailure(const EnvoyException& error) override;

void update(uint64_t increment);

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.

See #12419 (comment)

I would suggest changing the signature of this method to "bool tryAllocateResource()" and have it return true iff the attempt to allocate succeeded given current resource limits.

virtual const OverloadActionState& getState(const std::string& action) PURE;
// On-demand recalculation of resource pressure and update of overload action state.
virtual void updateReactiveResource(const std::string& reactive_resource_name,
uint64_t increment) 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.

I think that the idea behind the existing getState API is that most users will call getState once and save the reference for use from there on. updateReactiveResource seems to be called with the string argument a lot more often; consider passing in an enum with to select the resource name instead of a string for efficiency, or try to use a pattern similar to the getState one which returns a reference to the reactive resource which can then be updated directly.

Comment thread include/envoy/server/resource_monitor.h Outdated
ActiveConnectionsResourceMonitor(uint64_t max_active_conns)
: ReactiveResourceMonitor(max_active_conns, 0){};

void updateResourceUsage(uint64_t increment, Callbacks& callbacks) {

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.

nit: There's a disagreement between the name of the first argument to this function in the base interface definition and the implementation here: It is curr_value in ReactiveResourceMonitor but increment here. The name of the first argument should be consistent between API definition and implementation.

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.

fixed

Comment thread include/envoy/server/resource_monitor.h Outdated
: ReactiveResourceMonitor(max_active_conns, 0){};

void updateResourceUsage(uint64_t increment, Callbacks& callbacks) {
// Calling code will need to reset its current value after this.

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.

how will this value be decremented or reset?

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.

as per your suggestion in the issue, i agree that we should introduce separate method like tryDeallocateResource that client code (for example TCP listener) will be calling when closing the socket.

Comment thread include/envoy/server/resource_monitor.h Outdated
callbacks.onSuccess(usage);
}

void updateResourceUsage(Callbacks&) { NOT_IMPLEMENTED_GCOVR_EXCL_LINE; }

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 this use of NOT_IMPLEMENTED_GCOVR_EXCL_LINE suggests that ReactiveResourceMonitor is not a ResourceMonitor. Consider removing ResourceMonitor as a base class.

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.

fixed

Comment thread include/envoy/server/resource_monitor.h Outdated
ReactiveResourceMonitor(uint64_t max, uint64_t current) : max_(max), current_(current){};
virtual ~ReactiveResourceMonitor() = default;
using ResourceMonitor::updateResourceUsage;
virtual void updateResourceUsage(uint64_t curr_value, Callbacks& callbacks) 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.

Would be good to have comments for this new class and its API methods

Comment thread include/envoy/server/resource_monitor.h Outdated

protected:
uint64_t max_;
std::atomic<uint64_t> current_;

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'm not finding examples of interface classes in the include/envoy directory that have data members. Could you move these data members to the implementation class and make currentResourceUsage/maxResourceUsage PURE ?

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.

yeap, good point

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.

fixed

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

Copy link
Copy Markdown
Member Author

pushing intermediate state to keep PR alive, i still need to apply bunch of review comments and add tests

Kateryna Nezdolii added 2 commits April 19, 2021 22:08
Signed-off-by: Kateryna Nezdolii <[email protected]>
Signed-off-by: Kateryna Nezdolii <[email protected]>
Signed-off-by: Kateryna Nezdolii <[email protected]>
Kateryna Nezdolii added 2 commits April 23, 2021 19:57
Signed-off-by: Kateryna Nezdolii <[email protected]>
Signed-off-by: Kateryna Nezdolii <[email protected]>
@nezdolik

nezdolik commented Apr 23, 2021 •

Copy link
Copy Markdown
Member Author

@antoniovicente cleaned up code a lot and reduced proactive checks to:

bool tryAllocateResource(OverloadReactiveResourceName resource_name /* enum val*/, uint64_t increment, ReactiveResourceUpdateCallbacks& cb);
bool tryDeallocateResource(OverloadReactiveResourceName resource_name /* enum val*/, uint64_t decrement,
                             ReactiveResourceUpdateCallbacks& cb);

Proactive checks now represent very different mechanism in OM and don't fit within OM model of registering for action, periodic resource pressure/action trigger recalculation and flushing updates etc.
This methods will either be called from thread local object in worker threads or plugged into Dispatcher api.
Calling code would provide callback which will be invoked in OM upon updating the resource on next main dispatcher loop run.
I would like to know if this is an acceptable approach.

@nezdolik nezdolik changed the title Introduce reactive checks in overload manager Proactive reactive checks in overload manager Apr 23, 2021
Signed-off-by: Kateryna Nezdolii <[email protected]>
@antoniovicente antoniovicente changed the title Proactive reactive checks in overload manager Proactive checks in overload manager Apr 26, 2021

@antoniovicente antoniovicente 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.

@antoniovicente cleaned up code a lot and reduced proactive checks to:

bool tryAllocateResource(OverloadReactiveResourceName resource_name /* enum val*/, uint64_t increment, ReactiveResourceUpdateCallbacks& cb);
bool tryDeallocateResource(OverloadReactiveResourceName resource_name /* enum val*/, uint64_t decrement,
                             ReactiveResourceUpdateCallbacks& cb);

Proactive checks now represent very different mechanism in OM and don't fit within OM model of registering for action, periodic resource pressure/action trigger recalculation and flushing updates etc.
This methods will either be called from thread local object in worker threads or plugged into Dispatcher api.
Calling code would provide callback which will be invoked in OM upon updating the resource on next main dispatcher loop run.
I would like to know if this is an acceptable approach.

I'm having trouble understanding the proposed APIs, specifically OverloadManagerImpl::tryAllocateResource

Comment thread include/envoy/server/resource_monitor.h Outdated
// uint64_t overflow, cannot allocate resource.
return false;
} else {
current_ += increment;

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 it is possible for multiple threads to hit this increment concurrently after narrowly passing the checks above to verify that resource is available. Say that current_ is max_ - 1 as N threads attempt to increment by 1, all N of those threads may execute the current_.load() before the first thread gets to the increment, so at the end of these tryAllocateResource calls the value of current_ could be any value in [max_, max_ + N - 1].

The question how strict we want to be about this limit. Possible way to be more strict:

uint64_t new_val = (current_ += increment);
if (new_val >= max_) {
  current_ -= increment;
  return false;
}
return true;

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.

Double guard would also protect against potential race, though this version has more code:

    if (current_.load() + inrement > max) {
      return false;
    } else {
      //Guard against raice condition.
      if (current_.load() + inrement <= max) {
        current_ += increment;
        return true;
      }
      return false;
    }

Comment thread include/envoy/server/resource_monitor.h Outdated

bool tryAllocateResource(uint64_t increment) {
auto curr_capacity = current_.load();
if (std::numeric_limits<uint64_t>::max() - increment < curr_capacity) {

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 you meant to compare against max_ which contains the configured max number of connections, not std::numeric_limits<uint64_t>::max()

Comment thread include/envoy/server/resource_monitor.h Outdated
auto curr_capacity = current_.load();
if (decrement > curr_capacity) {
// There are not enough resources to deallocate.
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.

It seems very unlikely for deallocate to fail unless the calling code is buggy. Consider making this an ASSERT instead.

Comment thread source/server/overload_manager_impl.cc Outdated

bool OverloadManagerImpl::tryAllocateResource(OverloadReactiveResourceName resource_name,
uint64_t increment,
ReactiveResourceUpdateCallbacks& cb) {

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'm having some trouble understanding how listeners would call this method. Some comments for the class method and interface method could help a lot.

I assume that tryAllocateResource can be called from any thread and would most likely be called from the thread associated with cb.dispatcher(). Having to go through a post for every accept would make it difficult to implement other desirable limits like accepting at most N connections per iteration of the event loop since all the accepts would happen from a possibly infinite chain of post calls.

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.

@antoniovicente this is most likely due to my knowledge gap, the only way i came up with is by posting on thread dispatcher from main thread.

Comment thread source/server/overload_manager_impl.cc Outdated
return true;
}

bool OverloadManagerImpl::tryAllocateResource(OverloadReactiveResourceName resource_name,

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.

Can you document the return value of this method? At first glance it seemed to me that the return value should be true when allocation succeeds, but it seems to instead return true if the requested resource exists and this method was able to schedule the provided callback.

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.

@antoniovicente at first i was going to proceed with version that returns true if allocation succeeds:

bool tryAllocateResource(OverloadReactiveResourceName resource_name, uint64_t increment);

But then I could not come up with a way how to call this method from worker thread. I may be wrong, but in my understanding worker thread will not be able to call this method which runs on main thread without either blocking for result or doing it asynchronously (which is done by passing thread dispatcher and a callback).

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.

As discussed over slack, this method should be callable from workers. We just need to figure out how to make it available to workers.

Comment thread source/server/overload_manager_impl.cc Outdated
}
} else {
ENVOY_LOG(warn, " {Failed to allocate unknown reactive 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.

Should this branch call cb.dispatcher().post([&cb]() { cb.onFailure(); });

if so, I think the return value of this method could be removed.

@nezdolik

Copy link
Copy Markdown
Member Author

@antoniovicente apologies for delays, am travelling this week. I will get back on this next Monday (may have little bit of time this weekend).

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

Copy link
Copy Markdown
Member Author

@antoniovicente applied review comments. We should agree on how tryAllocateResource\tryDeallocateResource will be called from worker threads before writing tests for new functionality. I've switched back to original version (with no callback): tryAllocateResource(OverloadReactiveResourceName resource_name, uint64_t increment) and posted a question in thread of a review comment.

@antoniovicente antoniovicente 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.

Removal of callbacks from the API seems like a big improvement. I took a quick look at the most recent commit, see comments below

Comment thread include/envoy/server/resource_monitor.h Outdated
if (std::numeric_limits<uint64_t>::max() - increment < curr_capacity) {
// uint64_t overflow, cannot allocate resource.
uint64_t new_val = (current_ += increment);
if (new_val >= max_) {

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 this should be:

if (new_val > max_) {

Comment thread include/envoy/server/resource_monitor.h Outdated
return false;
} else {
ASSERT(decrement <= current_.load());
// Guard against race condition.

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.

You still have a race between the current_.load() and the decrement.

I would consider making current_ atomic<int64_t> and RELEASE_ASSERT that the decrement doesn't go negative.

std::string category() const override { return "envoy.resource_monitors"; }
};

class ReactiveResourceMonitorFactory : public Config::TypedFactory {

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.

Reactive -> Proactive

Comment thread source/server/overload_manager_impl.cc Outdated
return true;
}

bool OverloadManagerImpl::tryAllocateResource(OverloadReactiveResourceName resource_name,

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.

As discussed over slack, this method should be callable from workers. We just need to figure out how to make it available to workers.

Comment thread include/envoy/server/resource_monitor.h Outdated
return true;
}

bool tryDeallocateResource(uint64_t decrement) {

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'm not sure what caller code can/should if deallocation fails. If deallocation ever fails we have a serious logic error.

I guess an alternate API could involve a movable resource tracking object that releases the resource on destruction. That approach could reduce the risk of mismatched calls to allocate/deallocate the resource. That said, the resource tracking object approach may be overkill given that we expect this API to be used in a few places, one example being allocate just before creation of a server connection and de-allocation on server connection destruction.

@antoniovicente

Copy link
Copy Markdown
Contributor

@antoniovicente applied review comments. We should agree on how tryAllocateResource\tryDeallocateResource will be called from worker threads before writing tests for new functionality. I've switched back to original version (with no callback): tryAllocateResource(OverloadReactiveResourceName resource_name, uint64_t increment) and posted a question in thread of a review comment.

As discussed over slack:
tryAllocateResource does operations on an atomic variable. The calls could happen in any thread including the workers.

Waiting for a round trip to the main thread from the workers will cause problems. The issue is that the main thread handles some very expensive operations like config reload and admin interface stats. These operations can take seconds to complete.

Kateryna Nezdolii added 2 commits May 21, 2021 15:13
Signed-off-by: Kateryna Nezdolii <[email protected]>
Kateryna Nezdolii added 2 commits June 15, 2021 18:18
Signed-off-by: Kateryna Nezdolii <[email protected]>
@repokitteh-read-only

Copy link
Copy Markdown

CC @envoyproxy/api-shepherds: Your approval is needed for changes made to api/envoy/.
envoyproxy/api-shepherds assignee is @adisuissa
CC @envoyproxy/api-watchers: FYI only for changes made to api/envoy/.

🐱

Caused by: #15707 was synchronize by nezdolik.

see: more, trace.

Kateryna Nezdolii added 2 commits July 5, 2021 16:18
Signed-off-by: Kateryna Nezdolii <[email protected]>
@nezdolik
nezdolik marked this pull request as ready for review July 5, 2021 16:46
@nezdolik

nezdolik commented Jul 5, 2021

Copy link
Copy Markdown
Member Author

@antoniovicente @alyssawilk i may need to split this huge PR into 2-3 parts for easier review:

  • add proactive checks into overload manager
  • add proactive downstream connections monitor (extension)
  • deprecate runtime key for max downstream connections used by tcp listener and switch to overload manager proactive monitor.

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

@antoniovicente antoniovicente 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.

@antoniovicente @alyssawilk i may need to split this huge PR into 2-3 parts for easier review:

  • add proactive checks into overload manager
  • add proactive downstream connections monitor (extension)
  • deprecate runtime key for max downstream connections used by tcp listener and switch to overload manager proactive monitor.

Yes, it would be best to split this into smaller PRs if possible.

Comment thread CODEOWNERS
/*/extensions/resource_monitors/injected_resource @eziskind @htuch
/*/extensions/resource_monitors/common @eziskind @htuch
/*/extensions/resource_monitors/fixed_heap @eziskind @htuch
/*/extensions/resource_monitors/downstream_connections @antoniovicente @mattklein123

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 suggest also adding yourself to this list.

#include "envoy/server/resource_monitor.h"
#include "envoy/server/resource_monitor_config.h"

#include "source/extensions/resource_monitors/downstream_connections/downstream_connections_monitor.h"

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.

It would be best to move tests that depend on this extension to a new test/extensions file so it is possible to build envoy core without pulling in this extension.

Comment thread test/integration/BUILD
":http_protocol_integration_lib",
"//envoy/network:filter_interface",
"//source/extensions/filters/network/tcp_proxy:config",
"//source/extensions/resource_monitors/downstream_connections:downstream_connections_monitor",

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.

It is not legal to depend on source/extensions from tests that are not associated with the extension in question.

@adisuissa adisuissa 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.

IIUC this is going to be split into multiple PRs, so feel free to make the API change in one of these PRs.


// TODO(nezdolik) Add description
message DownstreamConnectionsConfig {
uint64 max_active_downstream_connections = 1 [(validate.rules).uint64 = {gt: 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.

Please also add comments to the field, and specify default value.

@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in 7 days if no further activity occurs. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions!

@github-actions github-actions Bot added the stale stalebot believes this issue/PR has not been touched recently label Aug 18, 2021
@github-actions

Copy link
Copy Markdown

This pull request has been automatically closed because it has not had activity in the last 37 days. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions!

@github-actions github-actions Bot closed this Aug 26, 2021
@nezdolik

Copy link
Copy Markdown
Member Author

heaps up, am now actively working on splitting this PR into multiple ones, this is not forgotten

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

Labels

api stale stalebot believes this issue/PR has not been touched recently waiting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants