From 9bb3167fd5ea25574cc137e62412528f9bc1e475 Mon Sep 17 00:00:00 2001 From: Brian Simpson Date: Mon, 25 Jul 2016 11:10:58 -0700 Subject: [PATCH] CommentTree.add_comments: Add orphaned comments to the tree Adding them to the tree doesn't hurt anything--they still won't be displayed until their parent comment is in the tree, but it does allow for easier cleanup in case comments get processed out of order. Once their parent is added to the tree everything will be consistent and fine. --- r2/r2/models/comment_tree.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/r2/r2/models/comment_tree.py b/r2/r2/models/comment_tree.py index b16b6b2fa..ebd83c621 100644 --- a/r2/r2/models/comment_tree.py +++ b/r2/r2/models/comment_tree.py @@ -129,23 +129,22 @@ class CommentTreePermacache(object): if comment._id not in cids } - # skip adding any comments whose parents are missing from the tree - # because they will never be displayed unless the tree is rebuilt. - # check for the parent in the existing tree and in the current - # batch of comments to be added. + if not comments: + return + + # warn on any comments whose parents are missing from the tree + # because they will never be displayed unless their parent is + # added. this can happen in normal operation if there are multiple + # queue consumers and a child is processed before its parent. parent_ids = set(cids) | {comment._id for comment in comments} - orphan_comments = { + possible_orphan_comments = { comment for comment in comments if (comment.parent_id and comment.parent_id not in parent_ids) } - if orphan_comments: + if possible_orphan_comments: g.log.error("comment_tree_inconsistent: %s %s", link, - orphan_comments) + possible_orphan_comments) g.stats.simple_event('comment_tree_inconsistent') - comments -= orphan_comments - - if not comments: - return for comment in comments: tree.setdefault(comment.parent_id, []).append(comment._id)