Repository navigation
Add ConnectionMaxLifetime Feature for V4 - #2068
FlorianRainer wants to merge 2 commits into
Conversation
fe46200 to
1f156c8
Compare
|
push for 4.0.4! |
| @@ -599,15 +599,24 @@ void PruneIdleConnectors(object _) | |||
| { | |||
| var now = DateTime.UtcNow; | |||
There was a problem hiding this comment.
@roji Just out of curiosity, have we ever considered using NodaTime internally?
There was a problem hiding this comment.
None in particular, just finished writing some code with NodaTime before looking at this, so it was fresh on my mind.
There was a problem hiding this comment.
Feel free to change it to what ever the Project standard is, for me it was only copy paste from:
npgsql/src/Npgsql/ConnectorPool.cs
Line 530 in a91fe0e
There was a problem hiding this comment.
The only reason not to use NodaTime internally is the added dependency - we want to force as little as possible upon users... Especially since there doesn't seem to be a lot of value in doing so here.
| continue; | ||
|
|
||
| var idleLifetimeExceeded = (now - connector.ReleaseTimestamp).TotalSeconds >= idleLifetime; | ||
| var maxLifetimeExceeded = maxLifetime > 0 && (now - connector.CreationTimestamp).TotalSeconds >= maxLifetime; |
There was a problem hiding this comment.
Some thoughts on this...
Enforcing max lifetime this way has the disadvantage of getting rid of connections only when the pruning event fires, and only if the connection happens to be idle at that point. For example, a connection could cycle in and out of the pool, and it may happen to always be out of the pool (busy) just when the pruning event fires. This could allow it to stay alive for far longer than the configured max lifetime. An alternative would be to check the max lifetime when allocating a new connection (or when releasing, or even both) - this would always work.
However, this would add an additional branch when allocating a new connection, which will degrade performance (we try to be super efficient in the pool).
This feature really is more of helper for people with some sort of leak - a normal, well-functioning program should never need to use this feature. Because of this, I'm leaning towards keeping the current proposed implementation, which is imperfect in the way described above but which has zero impact on perf.
Thoughts (@austindrenski @YohDeadfall)?
There was a problem hiding this comment.
@roji you're absolutly right, this change can't ensure the real maximum connection lifetime as a kind of "hard limit" it's more like a soft limit, and in certain cases it could cause to not close a "long running connection"
I agree with not adding any check to connection open (allocate from pool) for performance reasons, but maybe when the connection will be released. Normaly the connection would be given back to the pool with idle state, if we change this to not add it to idle list again but use a separate state or list for "expired connections" then the performance should not even change in this case.
later the pruning event will release this connection, and it would be not possible for any connection.Open to allocate this connection from the pool.
The main problem (for me) described here: #1810 (comment) is not realy related to bad programming, or a leak, its more related to how postgres server works and how data / exec. plans and many more stuff is cached on realy large databases with a lot of tables and a lot of different quereies.
the answer from this pos: https://www.postgresql.org/message-id/CAD9xk1_Oy9v23dA51iLF910OkmSBy947O9Qrn9iQExBEhuF9PA%40mail.gmail.com is a clear guide to use "shorter lived connections" if there are problems with "postmaster process memory usage". My personal tests (on production) are even connections with 1 day uptime are still not problematic, but with 2 days they already grown memory usage exponencialy, and i think to "refresh" connections each day or each 12 hours is not bad in general.
There was a problem hiding this comment.
I agree with not adding any check to connection open (allocate from pool) for performance reasons, but maybe when the connection will be released.
Assuming starvation of the connection pool (which is the perf scenario we're usually interested in when discussing pool perf), there's no difference between adding a check on open or on release... Both occur as part of the application cycle and slowing either down slows the entire cycle in the same way.
Normaly the connection would be given back to the pool with idle state, if we change this to not add it to idle list again but use a separate state or list for "expired connections" then the performance should not even change in this case.
I'm not following this one... In order to know whether to add a connection to the idle list or to the new "expired connections" list, you would need to perform the check (i.e. the branch), which is exactly what we're trying to avoid here. The point is not just to remove the closing of connections from the main path (either open or release), but to avoid even adding the condition.
The main problem (for me) described here: #1810 (comment) is not realy related to bad programming, or a leak, its more related to how postgres server works and how data / exec. plans and many more stuff is cached on realy large databases with a lot of tables and a lot of different quereies.
As discussed #1810, you're basically saying that PostgreSQL leaks resources in some way, which is hard for me to believe... I don't know who the guy from your message is (Jony Cohen), but it's not one of the main maintainers; I've never heard of a recommendation to close connections to avoid increasing memory use. There should definitely be a way to do what you need, and I'd really encourage investigating that rather than using max connection lifetime to work around it. Please consider posting a simple, minimal repro to the PostgreSQL hackers list; if the big boys tell you to use short-lived connections I'll definitely shut up, but until then I'm assuming a better alternative exists. So for now I really consider max connection lifetime only as an option for cases where there aren't enough developer resources to properly understand the source of the leak, and a max connection lifetime solution is good enough.
There was a problem hiding this comment.
FWIW, another use-case is a situation where you need to fairly distribute the available connection slots to an unknown set of worker nodes. In this scenario, being able to limit the lifetime of each individual connection could be a viable strategy, assuming that each worker uses a greedy strategy for filling a connection pool.
There was a problem hiding this comment.
@malthe a specific pool is (at least currently) for connections to a specific IP address, not to several available worker nodes.
I'm guessing you have in mind a scenario where behind that IP is some sort of dispatcher that forwards the connection to a set of workers, and where a connection remains "bound" to whatever worker it got at (physical) connection time (do you have a specific dispatcher component in mind here?). I'm still not super convinced about this: the dispatcher (whatever it is) should be the one ensuring a good distribution over the workers, rather than a solution where we periodically close and open connections just so they can get redispatched.
But just to be clear, I don't really have anything against this feature... It's simply a bit lower on the priority than other things (because it should not in principle be necessary etc.).
There was a problem hiding this comment.
The scenario I have in mind is something like a serverless setup (e.g. Azure Functions) where the function runs on an arbitrary number of "function hosts" (isolated processes). You're not necessarily able to manage how many of these hosts are activate at any given time, and this can create a situation of connection slot starvation with the different function host processes fighting each other to allocate enough connections.
That is, it's a fundamental mismatch between the computing model and the database connectivity, but it's also rather difficult to solve in practice without something like pgbouncer.
There was a problem hiding this comment.
The scenario I have in mind is something like a serverless setup (e.g. Azure Functions) where the function runs on an arbitrary number of "function hosts" (isolated processes).
Npgsql's pool is an in-process pool, meaning that it only manages physical connections in the process it's in (unlike, say, pgbouncer which is out-of-process). This means that if you have the same application running in different processes (with serverless or not), this option would only take effect for each process separately. So I don't really see this as relevant for fair distribution.
| [DisplayName("Connection Max Lifetime")] | ||
| [NpgsqlConnectionStringProperty] | ||
| [DefaultValue(0)] | ||
| public int ConnectionMaxLifetime |
There was a problem hiding this comment.
If we want to align with SQL Client, we should consider calling this Connection Lifetime (see this doc page). This parameter actually existed before 3.1, but actually meant what Connection Idle Lifetime means (it was renamed, see the release notes). 4.1 seems like the right time to add the "max lifetime" feature with the SQL Client name, for consistency.
|
Strictly speaking this is a feature so not a good candidate for a 4.0 patch version... |
|
@FlorianRainer sorry that this hasn't made it in up to now. Since this PR is stale and the pool has changed since you submitted this, and since the perf impact of a branch is actually quite negligible, I've submitted #3133 which introduces the lifetime check when an idle connector is returned from the pool, to make sure this (finally) makes it into 5.0. |
|
@roji No Problem, good to see this functionality comming in 5.0! Nice Work! |
Redone pull request from #1856
i needed to do it this way because i messed up my old repo.
the description from #1856 is still valid (well tested)
@roji any chance to get this feature for 4.0.3?