Skip to content

Commit f135759

Browse files
Lucas Gameiromarceloneppel
andauthored
[DPE-4416] Update rolling-ops lib to version 0.7 (#478)
* update rolling ops lib and unit test setup * async_replication integration fixes * fix relation_departed condition * revert sync standby assertion removal * Handle upgrade of top of the stack Juju leader Signed-off-by: Marcelo Henrique Neppel <[email protected]> --------- Signed-off-by: Marcelo Henrique Neppel <[email protected]> Co-authored-by: Marcelo Henrique Neppel <[email protected]>
1 parent 4fb533b commit f135759

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

lib/charms/rolling_ops/v0/rollingops.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def _restart(self, event):
4949
5050
To kick off the rolling restart, emit this library's AcquireLock event. The simplest way
5151
to do so would be with an action, though it might make sense to acquire the lock in
52-
response to another event.
52+
response to another event.
5353
5454
```python
5555
def _on_trigger_restart(self, event):
@@ -88,7 +88,7 @@ def _on_trigger_restart(self, event):
8888

8989
# Increment this PATCH version before using `charmcraft publish-lib` or reset
9090
# to 0 if you are raising the major API version
91-
LIBPATCH = 5
91+
LIBPATCH = 7
9292

9393

9494
class LockNoRelationError(Exception):
@@ -182,6 +182,7 @@ def _state(self) -> LockState:
182182
# Active acquire request.
183183
return LockState.ACQUIRE
184184

185+
logger.debug("Lock state: %s %s", unit_state, app_state)
185186
return app_state # Granted or unset/released
186187

187188
@_state.setter
@@ -202,21 +203,27 @@ def _state(self, state: LockState):
202203
if state is LockState.IDLE:
203204
self.relation.data[self.app].update({str(self.unit): state.value})
204205

206+
logger.debug("state: %s", state.value)
207+
205208
def acquire(self):
206209
"""Request that a lock be acquired."""
207210
self._state = LockState.ACQUIRE
211+
logger.debug("Lock acquired.")
208212

209213
def release(self):
210214
"""Request that a lock be released."""
211215
self._state = LockState.RELEASE
216+
logger.debug("Lock released.")
212217

213218
def clear(self):
214219
"""Unset a lock."""
215220
self._state = LockState.IDLE
221+
logger.debug("Lock cleared.")
216222

217223
def grant(self):
218224
"""Grant a lock to a unit."""
219225
self._state = LockState.GRANTED
226+
logger.debug("Lock granted.")
220227

221228
def is_held(self):
222229
"""This unit holds the lock."""
@@ -266,9 +273,11 @@ def __init__(self, handle, callback_override: Optional[str] = None):
266273
self.callback_override = callback_override or ""
267274

268275
def snapshot(self):
276+
"""Snapshot of lock event."""
269277
return {"callback_override": self.callback_override}
270278

271279
def restore(self, snapshot):
280+
"""Restores lock event."""
272281
self.callback_override = snapshot["callback_override"]
273282

274283

@@ -288,7 +297,7 @@ def __init__(self, charm: CharmBase, relation: AnyStr, callback: Callable):
288297
charm: the charm we are attaching this to.
289298
relation: an identifier, by convention based on the name of the relation in the
290299
metadata.yaml, which identifies this instance of RollingOperatorsFactory,
291-
distinct from other instances that may be hanlding other events.
300+
distinct from other instances that may be handling other events.
292301
callback: a closure to run when we have a lock. (It must take a CharmBase object and
293302
EventBase object as args.)
294303
"""
@@ -309,6 +318,7 @@ def __init__(self, charm: CharmBase, relation: AnyStr, callback: Callable):
309318
self.framework.observe(charm.on[self.name].acquire_lock, self._on_acquire_lock)
310319
self.framework.observe(charm.on[self.name].run_with_lock, self._on_run_with_lock)
311320
self.framework.observe(charm.on[self.name].process_locks, self._on_process_locks)
321+
self.framework.observe(charm.on.leader_elected, self._on_process_locks)
312322

313323
def _callback(self: CharmBase, event: EventBase) -> None:
314324
"""Placeholder for the function that actually runs our event.
@@ -381,7 +391,7 @@ def _on_acquire_lock(self: CharmBase, event: ActionEvent):
381391
"""Request a lock."""
382392
try:
383393
Lock(self).acquire() # Updates relation data
384-
# emit relation changed event in the edge case where aquire does not
394+
# emit relation changed event in the edge case where acquire does not
385395
relation = self.model.get_relation(self.name)
386396

387397
# persist callback override for eventual run

src/relations/async_replication.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ def _on_async_relation_changed(self, event: RelationChangedEvent) -> None:
487487
def _on_async_relation_departed(self, event: RelationDepartedEvent) -> None:
488488
"""Set a flag to avoid setting a wrong status message on relation broken event handler."""
489489
# This is needed because of https://bugs.launchpad.net/juju/+bug/1979811.
490-
if event.departing_unit == self.charm.unit:
490+
if event.departing_unit == self.charm.unit and self.charm._peers is not None:
491491
self.charm._peers.data[self.charm.unit].update({"departing": "True"})
492492

493493
def _on_async_relation_joined(self, _) -> None:

tests/unit/test_charm.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def harness():
4444
harness.begin()
4545
harness.add_relation("upgrade", harness.charm.app.name)
4646
harness.add_relation(PEER, harness.charm.app.name)
47+
harness.add_relation("restart", harness.charm.app.name)
4748
yield harness
4849
harness.cleanup()
4950

0 commit comments

Comments
 (0)