Skip to content

Commit

Permalink
[agroal#235] Wrap pgagroal_prefill()
Browse files Browse the repository at this point in the history
In order to make calls to `pgagroal_prefill()` consistent around the
code and avoid repetitions, there is a new function named
`pgagroal_prefill_if_can()` that wraps several checks and does the
forking and prefilling.
Therefore there is no need to check against the configuration limits
and users before calling `pgagroal_prefill()` because
`pgagroal_prefill_if_can()` will do by itself.

Also `pgagroal_prefill_if_can()` will check for a primary server, and
in the case there is none, will avoid issuing the prefill at all.

Last, this introduces also the `pgagroal_can_prefill()` function that
is used in turn by `pgagroal_prefill_if_can()` and aims at testing the
configuration for the presence of limits and users. This function can
be used as a condition checker before doing clean-up stuff right
before calling `pgagroal_prefill_if_can()`.

Close agroal#223
  • Loading branch information
fluca1978 committed May 19, 2022
1 parent 3eed7c2 commit 4be0d13
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 29 deletions.
11 changes: 11 additions & 0 deletions src/include/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,17 @@ pgagroal_reload_configuration(void);
void
pgagroal_init_pidfile_if_needed(struct configuration* config);

/**
* Checks if the configuration has a min set of values to
* take into account doing a prefill. For example, there must
* be users and limits set, otherwise it does not
* make any sense to attempt a prefill.
* This can be used to wrap the condituion before calling
* other prefill functions, e.g., `pgagroal_prefill()`.
*/
bool
pgagroal_can_prefill(struct configuration* config);

#ifdef __cplusplus
}
#endif
Expand Down
18 changes: 18 additions & 0 deletions src/include/pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@ pgagroal_pool_shutdown(void);
int
pgagroal_pool_status(void);

/**
* This function wraps around the logic to call `pgagroal_prefill()`.
* In order to avoid code repetition, this function can be used safely
* wherever there is the possibility to activate the prefill. The function
* does check if the configuration allows for a prefill, and in such case
* tries to `fork(2)` and executes the prefill.
* Also, the function checks for the presence of a primary with
* `pgagroal_get_primary()` and refuses to do a prefill if there
* is no primary at all.
*
* @param initial true if the prefill has to be done with the INITIAL
* value of the pgagroal_database.conf file, false if it has
* to be done with the MINIMAL value.
*
*/
void
pgagroal_prefill_if_can(bool initial);

#ifdef __cplusplus
}
#endif
Expand Down
13 changes: 13 additions & 0 deletions src/libpgagroal/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -3169,3 +3169,16 @@ pgagroal_init_pidfile_if_needed(struct configuration* config)
pgagroal_log_debug("PID file automatically set to: [%s]", config->pidfile);
}
}

bool
pgagroal_can_prefill(struct configuration* config)
{
if (config->number_of_users > 0 && config->number_of_limits > 0)
{
return true;
}
else
{
return false;
}
}
60 changes: 33 additions & 27 deletions src/libpgagroal/pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <server.h>
#include <tracker.h>
#include <utils.h>
#include <configuration.h>

/* system */
#include <assert.h>
Expand Down Expand Up @@ -252,13 +253,7 @@ pgagroal_get_connection(char* username, char* database, bool reuse, bool transac
pgagroal_tracking_event_slot(TRACKER_BAD_CONNECTION, *slot);
status = pgagroal_kill_connection(*slot, *ssl);

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

if (status == 0)
{
Expand Down Expand Up @@ -577,12 +572,9 @@ pgagroal_idle_timeout(void)
}
}

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

pgagroal_pool_status();
Expand Down Expand Up @@ -662,12 +654,9 @@ pgagroal_validation(void)
}
}

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

pgagroal_pool_status();
Expand Down Expand Up @@ -783,12 +772,9 @@ pgagroal_flush(int mode, char* database)
}
}

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

pgagroal_pool_status();
Expand Down Expand Up @@ -852,12 +838,9 @@ pgagroal_flush_server(signed char server)
}
else
{
if (config->number_of_users > 0 && config->number_of_limits > 0 && server != (unsigned char)primary && primary != -1)
if (server != (unsigned char)primary && primary != -1)
{
if (!fork())
{
pgagroal_prefill(true);
}
pgagroal_prefill_if_can(true);
}
}

Expand Down Expand Up @@ -1359,3 +1342,26 @@ do_prefill(char* username, char* database, int size)

return connections < size && free > 0;
}

void
pgagroal_prefill_if_can(bool initial)
{
int primary;
struct configuration* config;

config = (struct configuration*)shmem;

if (pgagroal_can_prefill(config))
{
if (pgagroal_get_primary(&primary))
{
pgagroal_log_warn("No primary detected, cannot try to prefill!");
return;
}

if (!fork())
{
pgagroal_prefill(initial);
}
}
}
4 changes: 2 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1060,12 +1060,12 @@ main(int argc, char** argv)
pgagroal_log_warn("No users allowed");
}

if (config->number_of_users > 0)
if (pgagroal_can_prefill(config))
{
if (!fork())
{
shutdown_ports();
pgagroal_prefill(true);
pgagroal_prefill_if_can(true);
}
}

Expand Down

0 comments on commit 4be0d13

Please sign in to comment.