Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make sure that deleteFeed is idempotent and performant #344

Merged
merged 1 commit into from
May 20, 2022

Conversation

staltz
Copy link
Member

@staltz staltz commented May 19, 2022

Context: I have an idea for deleting blocked feeds in an automatic way: use ssb-friends to scan all the blocked feeds and call deleteFeed on them. In case those feeds were already deleted, we need to make sure that deleteFeed is going to be performant and not cause any bugs.

Solution: when clearing the base index, this PR now checks first if an entry exists for that feedId. If it exists, then we can avoid calling flush and level.del in vain.

An important thing to keep in mind is that deleteFeed is not atomic. The app may crash in the middle of a deleteFeed, meaning that some messages owned by that feed are deleted, but not all of them. So in a sense it's desired to call deleteFeed every time the app is opened, to make sure all messages are deleted.

Check more comments I put in the implementation of deleteFeed just to explain my points better:

self.query(
  where(author(feedId)),
  asOffsets(),
  toCallback((err, offsets) => {
    if (err) return cb(clarify(err, 'deleteFeed() failed to query jitdb'))
    //
    // if `offsets` is empty, then the push-stream pipe below 
    // skips directly to the collect
    //
    push(
      push.values(offsets),
      push.asyncMap(log.del),
      push.collect((err) => {
        if (err) cb(clarify(err, 'deleteFeed() failed for feed ' + feedId))
        else {
          //
          // this `delete` should be fast
          //
          delete state[feedId]
          //
          // this removeFeedFromLatest should be fast
          //
          indexes.base.removeFeedFromLatest(feedId, cb)
        }
      })
    )
  })
)

@staltz staltz requested a review from arj03 May 19, 2022 16:18
const pValueAuthor = bipf.seekKey2(buf, pValue, BIPF_AUTHOR, 0)
const pValueSequence = bipf.seekKey2(buf, pValue, BIPF_SEQUENCE, 0)
const author = bipf.decode(buf, pValueAuthor)
const sequence = bipf.decode(buf, pValueSequence)
Copy link
Member Author

Choose a reason for hiding this comment

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

These changes are unrelated to the PR, but when I ran prettier, the decode ended up taking many lines, so I refactored to create the pValueAuthor and keep each of these a one-liner.

@staltz
Copy link
Member Author

staltz commented May 19, 2022

Mysterious, why isn't the CI completing within 10minutes...

Whaaat, the npm install is taking more than 10minutes and getting stuck..

@arj03
Copy link
Member

arj03 commented May 19, 2022

Maybe CI acting up. Node 16 ran fine.

Copy link
Member

@arj03 arj03 left a comment

Choose a reason for hiding this comment

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

Looks good

@staltz
Copy link
Member Author

staltz commented May 20, 2022

I investigated this locally by running npm install --verbose with node.js 14 and 16, and it really just takes a long long time to install. I don't know why, but it seems like when it reaches ssb-bfe-spec and ssb-bfe (and other repos, but not all, repos that we moved from ssb-ngi-pointer to ssbc), it gets stuck on those particular repos for a long while. I don't understand why the GitHub transferring would have an effect, because npm packages are not on GitHub at all. Maybe we can just blame this on npm.com itself and move on. It might affect all kinds of PRs, and would be good to improve the install duration eventually.

@staltz
Copy link
Member Author

staltz commented May 20, 2022

Tests pass locally with node.js 12 and 14 and 16 so I'll merge.

@staltz staltz merged commit 96a8db6 into master May 20, 2022
@staltz staltz deleted the idempotent-delete-feed branch May 20, 2022 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants