logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

20170920032311_fix_reblogs_in_feeds.rb (3053B)


  1. class FixReblogsInFeeds < ActiveRecord::Migration[5.1]
  2. def up
  3. redis = Redis.current
  4. fm = FeedManager.instance
  5. # Old scheme:
  6. # Each user's feed zset had a series of score:value entries,
  7. # where "regular" statuses had the same score and value (their
  8. # ID). Reblogs had a score of the reblogging status' ID, and a
  9. # value of the reblogged status' ID.
  10. # New scheme:
  11. # The feed contains only entries with the same score and value.
  12. # Reblogs result in the reblogging status being added to the
  13. # feed, with an entry in a reblog tracking zset (where the score
  14. # is once again set to the reblogging status' ID, and the value
  15. # is set to the reblogged status' ID). This is safe for Redis'
  16. # float coersion because in this reblog tracking zset, we only
  17. # need the rebloggging status' ID to be able to stop tracking
  18. # entries after they have gotten too far down the feed, which
  19. # does not require an exact value.
  20. # This process reads all feeds and writes 3 times for each reblogs.
  21. # So we use Lua script to avoid overhead between Ruby and Redis.
  22. script = <<-LUA
  23. local timeline_key = KEYS[1]
  24. local reblog_key = KEYS[2]
  25. -- So, first, we iterate over the user's feed to find any reblogs.
  26. local items = redis.call('zrange', timeline_key, 0, -1, 'withscores')
  27. for i = 1, #items, 2 do
  28. local reblogged_id = items[i]
  29. local reblogging_id = items[i + 1]
  30. if (reblogged_id ~= reblogging_id) then
  31. -- The score and value don't match, so this is a reblog.
  32. -- (note that we're transitioning from IDs < 53 bits so we
  33. -- don't have to worry about the loss of precision)
  34. -- Remove the old entry
  35. redis.call('zrem', timeline_key, reblogged_id)
  36. -- Add a new one for the reblogging status
  37. redis.call('zadd', timeline_key, reblogging_id, reblogging_id)
  38. -- Track the fact that this was a reblog
  39. redis.call('zadd', reblog_key, reblogging_id, reblogged_id)
  40. end
  41. end
  42. LUA
  43. script_hash = redis.script(:load, script)
  44. # find_each is batched on the database side.
  45. User.includes(:account).find_each do |user|
  46. account = user.account
  47. timeline_key = fm.key(:home, account.id)
  48. reblog_key = fm.key(:home, account.id, 'reblogs')
  49. redis.evalsha(script_hash, [timeline_key, reblog_key])
  50. end
  51. end
  52. def down
  53. # We *deliberately* do nothing here. This means that reverting
  54. # this and the associated changes to the FeedManager code could
  55. # allow one superfluous reblog of any given status, but in the case
  56. # where things have gone wrong and a revert is necessary, this
  57. # appears preferable to requiring a database hit for every status
  58. # in every users' feed simply to revert.
  59. # Note that this is operating under the assumption that entries
  60. # with >53-bit IDs have already been entered. Otherwise, we could
  61. # just use the data in Redis to reverse this transition.
  62. end
  63. end