ssbc / ssb-meta-feeds

10 stars 0 forks source link

ensure that findOrCreate / findAndTombstone is never concurrent #92

Closed staltz closed 1 year ago

staltz commented 1 year ago

Context

I'm working on updating ssb-replication-scheduler to use the new API in ssb-meta-feeds, and in the tests there are some cases of index feeds that are created at the same time.

Problem

If two findOrCreate calls happen concurrently ("at the same time"), then both calls may create a "v1" feed under the root, which leads to a situation like

graph TB;
  root --> v1a[v1] & v1b[v1]

This can also happen with shard feeds and leaf feeds.

Also, this concurrency problem can happen with findAndTombstone: there should be only one metafeed/tombstone message for a given subfeed.

Solution

Use mutexify in findOrCreate and findAndTombstone to enforce that only one call can happen at a time. I was working on a more complex solution before, which would allow concurrency if you're creating unrelated feeds like root/v1/7/chess and root/v1/7/tictactoe, but I realized that subfeed creation doesn't have to be efficient (we're not going to be creating feeds at a high throughput), so I went with the nuclear (and simpler) solution of allowing only one findOrCreate at a time, no matter what the arguments are.

1st :x: 2nd :heavy_check_mark:

staltz commented 1 year ago

Yup, let's not create problems for ourselves to debug in the future. :sweat_smile: