Skip to content

Commit 563a225

Browse files
committed
CommentTreeStorageV1: Don't trigger a rebuild on miss
Instead just assume that the comment tree is empty. One downside of this approach is that until a comment tree is created (when a new comment is added), each attempt to retrieve the comment tree will result in a read hitting Cassandra (permacache). Doing a rebuild was bad because it caused lock contention if there were simultaneous attempts to: 1) View the link's comments page 2) Add a new comment 3) Vote on a comment (update_comment_votes retrieves the tree for qa sort)
1 parent 47aad74 commit 563a225

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

r2/r2/lib/comment_tree.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,7 @@ def get_comment_tree(link, _update=False, timer=None):
281281
if not _update:
282282
cache = CommentTree.by_link(link)
283283
timer.intermediate('load')
284-
285-
if cache:
286-
return cache
284+
return cache
287285

288286
with CommentTree.mutation_context(link, timeout=180):
289287
timer.intermediate('lock')

r2/r2/models/comment_tree.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,11 @@ def by_link(cls, link):
413413

414414
r = g.permacache.get(key)
415415
if not r:
416-
return None
416+
# this link has not had an empty tree written for it. make an empty
417+
# tree here and return it. the downside is that until a comment is
418+
# added every time the storage for this link is requested it will
419+
# have a cache miss and fall through to cassandra
420+
return dict(cids=[], tree={}, depth={}, parents={})
417421

418422
try:
419423
cids, cid_tree, depth = r
@@ -479,10 +483,7 @@ def mutation_context(cls, link, timeout=None):
479483
def by_link(cls, link):
480484
impl = cls.IMPLEMENTATIONS[link.comment_tree_version]
481485
data = impl.by_link(link)
482-
if data is None:
483-
return None
484-
else:
485-
return cls(link, **data)
486+
return cls(link, **data)
486487

487488
def add_comments(self, comments):
488489
impl = self.IMPLEMENTATIONS[self.link.comment_tree_version]

0 commit comments

Comments
 (0)