Picolab / pico-engine

An implementation of the pico-engine hosted on node.js
http://picolabs.io/
MIT License
43 stars 8 forks source link

Before actually deleting a pico, Wrangler should notify it of pending deletion #591

Open b1conrad opened 2 years ago

b1conrad commented 2 years ago

This would best be done by raising an event, say wrangler:rulesets_need_to_cleanup, and only then doing the actual deletion.

Suggestion (that won't work (see below)): that the existing rule

  rule deleteOneChild {
    select when wrangler child_deletion_request
      eci re#^(.+)$# setting(eci)
    ctx:delPico(eci)
    fired {
      raise wrangler event "child_deleted"
        attributes event:attrs
    }
  }

be replaced by two rules

  rule deleteOneChild {
    select when wrangler child_deletion_request
      eci re#^(.+)$#
    fired {
      raise wrangler event "rulesets_need_to_cleanup"
      raise wrangler event "internal_pico_deletion"
        attributes event:attrs
    }
  }

  rule irrevocablyDeleteOneChild {
    select when wrangler internal_pico_deletion
      eci re#^(.+)$# setting(eci)
    ctx:delPico(eci)
    fired {
      raise wrangler event "child_deleted"
        attributes event:attrs
    }
  }
b1conrad commented 2 years ago

This would actually also address issue #511 because the subscription ruleset already does the needful by selecting on the wrangler:rulesets_need_to_cleanup event!

b1conrad commented 2 years ago

Note that rulesets selecting on the wrangler:rulesets_need_to_cleanup event MUST complete anything needed within the confines of the existing schedule. Oh, which means the suggestion won't work. Sigh.

b1conrad commented 2 years ago

It also was wrong because it was running in the parent pico, not the pico that was about to be deleted!

b1conrad commented 2 years ago

How about this?

  rule deleteOneChild {
    select when wrangler child_deletion_request
      eci re#^(.+)$#
    every {
      event:send({"eci":eci,"domain":"wrangler","type":"rulesets_need_to_cleanup"})
      event:send({"eci":eci,"domain":"wrangler","type":"ready_for_deletion"})
    }
  }

along with modifying the child_initiates_deletion rule to send its parent the wrangler:internal_pico_deletion event, and NOT the wrangler:child_deletion_request else we'd have an infinite loop of events and nothing would ever get deleted!

The idea is, you as a KRL developer do one of two things:

  1. Have the pico to be deleted clean itself up completely and then raise the wrangler:ready_for_deletion event
  2. Send the parent pico the event wrangler:child_deletion_request and select on wrangler:rulesets_need_to_cleanup

Either way, the cleanup is done in the child pico before it is actually deleted.

b1conrad commented 2 years ago

Here is a ruleset taking care of this: https://github.com/Picolab/fully-sharded-database/blob/main/krl/byu.hr.oit.krl#L454-L455

Suggest giving this some thought and then incorporating it into Wrangler.