Re: Refactoring postmaster's code to cleanup after child exit
Heikki Linnakangas <hlinnaka@iki.fi>
Attachments
- v4-0001-Add-test-for-connection-limits.patch (text/x-patch) patch v4-0001
- v4-0002-Add-test-for-dead-end-backends.patch (text/x-patch) patch v4-0002
- v4-0003-Use-an-shmem_exit-callback-to-remove-backend-from.patch (text/x-patch) patch v4-0003
- v4-0004-Introduce-a-separate-BackendType-for-dead-end-chi.patch (text/x-patch) patch v4-0004
- v4-0005-Kill-dead-end-children-when-there-s-nothing-else-.patch (text/x-patch) patch v4-0005
- v4-0006-Assign-a-child-slot-to-every-postmaster-child-pro.patch (text/x-patch) patch v4-0006
- v4-0007-Pass-MyPMChildSlot-as-an-explicit-argument-to-chi.patch (text/x-patch) patch v4-0007
On 10/08/2024 00:13, Heikki Linnakangas wrote:
> On 08/08/2024 13:47, Thomas Munro wrote:
>>> * v3-0004-Consolidate-postmaster-code-to-launch-background-.patch
>>
>> Much of the code in process_pm_child_exit() to launch replacement
>> processes when one exits or when progressing to next postmaster
>> state
>> was unnecessary, because the ServerLoop will launch any missing
>> background processes anyway. Remove the redundant code and let
>> ServerLoop handle it.
>
> I'm going to work a little more on the comments on this one before
> committing; I had just moved all the "If we have lost the XXX, try to
> start a new one" comments as is, but they look pretty repetitive now.
Pushed this now, after adjusting the comments a bit. Thanks again for
the review!
Here are the remaining patches, rebased.
> commit a1c43d65907d20a999b203e465db1277ec842a0a
> Author: Heikki Linnakangas <heikki.linnakangas@iki.fi>
> Date: Thu Aug 1 17:24:12 2024 +0300
>
> Introduce a separate BackendType for dead-end children
>
> And replace postmaster.c's own "backend type" codes with BackendType
>
> XXX: While working on this, many times I accidentally did something
> like "foo |= B_SOMETHING" instead of "foo |= 1 << B_SOMETHING", when
> constructing arguments to SignalSomeChildren or CountChildren, and
> things broke in very subtle ways taking a long time to debug. The old
> constants that were already bitmasks avoided that. Maybe we need some
> macro magic or something to make this less error-prone.
While rebasing this today, I spotted another instance of that mistake
mentioned in the XXX comment above. I called "CountChildren(B_BACKEND)"
instead of "CountChildren(1 << B_BACKEND)". Some ideas on how to make
that less error-prone:
1. Add a separate typedef for the bitmasks, and macros/functions to work
with it. Something like:
typedef struct {
uint32 mask;
} BackendTypeMask;
static const BackendTypeMask BTMASK_ALL = { 0xffffffff };
static const BackendTypeMask BTMASK_NONE = { 0 };
static inline BackendTypeMask
BTMASK_ADD(BackendTypeMask mask, BackendType t)
{
mask.mask |= 1 << t;
return mask;
}
static inline BackendTypeMask
BTMASK_DEL(BackendTypeMask mask, BackendType t)
{
mask.mask &= ~(1 << t);
return mask;
}
Now the compiler will complain if you try to pass a BackendType for the
mask. We could do this just for BackendType, or we could put this in
src/include/lib/ with a more generic name, like "bitmask_u32".
2. Another idea is to redefine the BackendType values to be separate
bits, like the current BACKEND_TYPE_* values in postmaster.c:
typedef enum BackendType
{
B_INVALID = 0,
/* Backends and other backend-like processes */
B_BACKEND = 1 << 1,
B_DEAD_END_BACKEND = 1 << 2,
B_AUTOVAC_LAUNCHER = 1 << 3,
B_AUTOVAC_WORKER = 1 << 4,
...
} BackendType;
Then you can use | and & on BackendTypes directly. It makes it less
clear which function arguments are a BackendType and which are a
bitmask, however.
Thoughts, other ideas?
--
Heikki Linnakangas
Neon (https://neon.tech)
Commits
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
Fix test name and username used in failed connection attempts
- 2a943afcff44 18.0 landed
-
tests: Don't fail due to high default timeout in postmaster/003_start_stop
- b48832cddbf5 18.0 landed
-
tests: Fix race condition in postmaster/002_connection_limits
- 71d1ed6fe129 18.0 landed
-
Pass MyPMChildSlot as an explicit argument to child process
- 5b007868577a 18.0 landed
-
Assign a child slot to every postmaster child process
- a78af0427015 18.0 landed
-
Kill dead-end children when there's nothing else left
- bb861414fea3 18.0 landed
-
Replace postmaster.c's own backend type codes with BackendType
- 18d67a8d7d30 18.0 landed
-
Use an shmem_exit callback to remove backend from PMChildFlags on exit
- 2bbc261ddbdf 18.0 landed
-
Add test for dead-end backends
- 85ec945b7880 18.0 landed
-
Add test for connection limits
- 6a1d0d470e84 18.0 landed
-
Fix garbled process name on backend crash
- 56d23855c864 18.0 landed
-
Consolidate postmaster code to launch background processes
- 3354f85284dc 18.0 landed
-
Fix comment on processes being kept over a restart
- a79ed10e6c6b 18.0 landed
-
Refactor code to handle death of a backend or bgworker in postmaster
- 28a520c0b773 18.0 landed
-
Make BackgroundWorkerList doubly-linked
- b43100fa71d7 18.0 landed
-
Minor refactoring of assign_backendlist_entry()
- 63bef4df975c 18.0 landed
-
Fix outdated comment; all running bgworkers are in BackendList
- ef4c35b4166e 18.0 landed
-
Allow building with MSVC and Strawberry perl
- 341f4e002d46 16.0 cited
-
Fix rare failure in LDAP tests.
- f44b9b625bed 14.0 cited
-
Allow notifications to bgworkers without database connections.
- 8a02b3d732cf 9.6.0 cited