Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#224] Prefill when doing a switch-to or primary change. #226

Closed
wants to merge 1 commit into from

Conversation

fluca1978
Copy link
Collaborator

Whenever the primary host is changed, by means of an explicit
switch-to command or by a primary failure, the connection flushing
is activated. If possible, the prefill should be also restored on the
new server.
As suggested in
#225 (comment)
it would be nice to check if the specified new server is the same as
the old one (failure of the primary) or a different one (switch-to),
and in the case they are different the prefill is forced.

@jesperpedersen
Copy link
Collaborator

I think I like this direction a bit more.

Called the variable primary - and set it to -1, since we don't know what server really is. And, do the server != (unsigned char)primary check before the other prefill checks are done.

@jesperpedersen jesperpedersen added the enhancement Improvement to an existing feature label May 4, 2022
@jesperpedersen
Copy link
Collaborator

I would out in an explicit primary != -1 check as well...

fluca1978 added a commit to fluca1978/pgagroal that referenced this pull request May 4, 2022
See <agroal#226 (comment)>.
Do the check of the new primary in the very beginning.

Improves also the logs in debug mode to clearly indicate what is going
to happen to the old and new server.

Close agroal#224
@jesperpedersen
Copy link
Collaborator

You don't need to enter the https://github.com/agroal/pgagroal/blob/master/src/libpgagroal/pool.c#L848 statement if primary isn't valid, so

int primary = -1;

if (pgagroal_get_primary(&primary))
{
    pgagroal_log_debug("No primary defined");
}

if (primary != -1 && server != (unsigned char)primary)
{
  // Line 848 and onwards ...

}

fluca1978 added a commit to fluca1978/pgagroal that referenced this pull request May 4, 2022
@jesperpedersen
Copy link
Collaborator

Can you squash and force push ?

@jesperpedersen
Copy link
Collaborator

Ah, there is still a thing.

We don't need to call prefill(false) for an old flushed primary server... which is the "if (primary != -1 && ..." check.

Otherwise it would try and prefill to MIN_SIZE

@fluca1978
Copy link
Collaborator Author

Just to make it clear, you don't want to prefill at all if the primary is not set or the server has remained the same, right?
That makes me wondering: when it is fine to prefill to the MIN_VALUE?

@jesperpedersen
Copy link
Collaborator

This is only for the flush_server case, and yes - there should be no prefill (as MIN_SIZE could be > 0, and therefore cause connects to an invalid host).

There are a lot of cases where prefill(false) makes sense - f.ex. after validation of a connection fails or after idle timeout. This is why people need to consider MIN_SIZE carefully as it will tie up resources on the database cluster.

@fluca1978 fluca1978 force-pushed the prefill_after_flush branch from ea3ac1d to 0143de9 Compare May 5, 2022 13:38
@fluca1978
Copy link
Collaborator Author

I'm not really comfortable with squashing a set of published commits, please check if it is fine now.

src/libpgagroal/pool.c Outdated Show resolved Hide resolved
@jesperpedersen
Copy link
Collaborator

The prefill(true) will happen if primary != server

@jesperpedersen
Copy link
Collaborator

And, you can just do 2 levels

if (primary != -1 && ...
{
   // Existing code
  if (..

@fluca1978
Copy link
Collaborator Author

Sorry, I'm a little confused: if there is the need to prefill (number of users and limits not zero) we fork and issue a pgagroal_prefill in the child process, that then exits, while the parent process continues to memory destroy and exits too.
So what is the need to fork if not issuing a pgagroal_prefill?

Whenever the primary host is changed, by means of an explicit
`switch-to` command or by a primary failure, the connection flushing
is activated. If possible, the prefill should be also restored on the
new server.
As suggested in
<agroal#225 (comment)>
it would be nice to check if the specified new server is the same as
the old one (failure of the primary) or a different one (`switch-to`),
and in the case they are different the prefill is forced.

The prefill will always be to the `INITIAL` size when needed.
If the primary is not set, there is no need to prefill
with the `MIN_SIZE` value.

Close agroal#224
@fluca1978 fluca1978 force-pushed the prefill_after_flush branch from 0143de9 to a6076a6 Compare May 5, 2022 15:04
@jesperpedersen
Copy link
Collaborator

There is no need. Hence the if (!fork()) should be the inner most one

@jesperpedersen
Copy link
Collaborator

I have to check something with uncrustify - otherwise I'll merge :)

@fluca1978
Copy link
Collaborator Author

Fine.
While reading the code I found a lot of places with code like the following:

if (prefill && config->number_of_users > 0 && config->number_of_limits > 0)
   {
      if (!fork())
      {
         pgagroal_prefill(false);
      }
   }

I was wondering why not to create a function that encapsulates all the above, something like pgagroal_fork_and_prefill_if_needed thcat will be safe to be called without having to check for limits, primary, and so on. Clearly this should be something unrelated to this pull request.

@jesperpedersen
Copy link
Collaborator

Yeah, it could be a static function within pool.c

@jesperpedersen
Copy link
Collaborator

Merged.

Thanks for your contribution !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvement to an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants