Skip to content

Conversation

marceloneppel
Copy link
Member

@marceloneppel marceloneppel commented Jan 24, 2024

Issue

The stanza can only be initialised in the PostgreSQL primary unit if it's the Juju leader unit.

Otherwise, the unit fails with an error, as shown in #370.

Solution

Allow the non-leader unit (when it's the PostgreSQL primary) to initialise the stanza.

For that, all checks for the unit being the Juju leader unit were changed to check whether the unit is the PostgreSQL primary.

Also, some coordination is now made in the relation databags to share the stanza name and the init-pgbackrest flag.

The current flow doesn't change if the PostgreSQL primary unit is also the Juju leader unit.

If they are different units, then:

  • The stanza name and the init-pgbackres flag are set in the primary unit relation databag.
  • The leader unit copies those values to the application relation databag (to be used later for other actions in backups).
  • The pgbackrest check command is executed in the primary unit, which clears the init-pgbackrest flag from the unit relation databag.
  • The leader unit clears the init-pgbackrest flag from the application relation databag.
  • The primary unit clears the stanza name from the unit relation databag.

Both unit and integration tests were added/updated to ensure we keep this flow intact.

Also, a test for #273 was added through this PR, as it was also related to backups/restores.

Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>
Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>
Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>
Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>
Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>
Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>
…tanza-on-primary

Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>
Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>
Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>
Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>
@marceloneppel marceloneppel marked this pull request as ready for review February 6, 2024 11:47
Comment on lines +331 to +334
if self.charm.unit.is_leader():
self.charm.app_peer_data.update(
{"stanza": self.stanza_name, "init-pgbackrest": "True"}
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have to check the unit databags anyway, would it make sense to not set the app data at all? that way we should be able to have a single flow using the unit data, instead of having to check two places.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Let me try to recall if there is any reason for this approach.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remembered the reason I have chosen the approach with both application and unit relation databags: using only the unit relation databag, if we scale down the application and the primary unit is removed, we need to handle the transfer of the stanza name to one of the remaining units in something like the relation departed event or keep the stanza name in all the units, which seems not so good as keeping it in the application relation databag.

Copy link
Contributor

@taurus-forever taurus-forever left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is perfect! Wow!

…tanza-on-primary

Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>
Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>
@marceloneppel marceloneppel merged commit a0a1247 into main Feb 9, 2024
@marceloneppel marceloneppel deleted the dpe-3349-initialise-stanza-on-primary branch February 9, 2024 12:15
BON4 pushed a commit to BON4/postgresql-k8s-operator that referenced this pull request May 20, 2024
* Handle S3 relation in primary non-leader unit

Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>

* Fix relation data removal

Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>

* Update integration test

Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>

* Fix existing unit tests

Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>

* Add extra unit tests

Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>

* Add unit test for peer relation changed event handler

Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>

* Handle pgBackRest service check when the primary was not elected yet

Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>

* Speedup the new check on test

Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>

* Add log call

Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>

* Speed up events

Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>

---------

Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants