Should use use_juju_for_storage=True and StoredState instead of direct state-set/state-get calls

Bug #1885714 reported by John A Meinel on 2020-06-30
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
charm-k8s-mattermost
Medium
Paul Collins

Bug Description

For operator framework 0.7 we are landing support for charms to maintain their state in Juju (using state-set and state-get) with our existing StoredState objects.

The actual patch that landed is here: https://github.com/canonical/operator/pull/323
With a bugfix for charms that have metrics.yaml here: https://github.com/canonical/operator/pull/349

0.7.0 with these fixes should be out later today.

That would change charm.py to use:

  main(MattermostK8sCharm, use_juju_for_storage=True)

And would mean you shouldn't need the charmstate.py file.

Likely you would need to keep state-get at least to have an 'upgrade_charm' event handler that would migrate from the existing state that has been set. Something like:

from ops.framework import StoredState

class MatterMostCharm(...):
  ...
  _stored = StoredState()

...

  def __init__(self):
  ...

    self._stored.set_default(
      db_uri=None,
      db_conn_str=None,
      db_ro_uris=None, # this seems to be set but never read?
    )

  def _on_upgrade_charm(self, event):
    if self._stored.db_uri is None:
      old_uri = state_get('db_uri')
      if old_uri is not None:
        self._stored.db_uri = old_uri
        # optionally, not actually implemented by charmstate.py yet
        state_delete('db_uri')

The 'set_default' is necessary to declare them as attributes of _stored, so at any point you can do

if self._stored.db_uri is None:

The upgrade_charm is necessary if you want to migrate from previous state.

Related branches

Paul Collins (pjdc) on 2020-06-30
Changed in charm-k8s-mattermost:
importance: Undecided → Medium
status: New → Triaged
Paul Collins (pjdc) on 2020-08-16
Changed in charm-k8s-mattermost:
status: Triaged → In Progress
assignee: nobody → Paul Collins (pjdc)
Paul Collins (pjdc) on 2020-08-19
Changed in charm-k8s-mattermost:
status: In Progress → Fix Released
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers