This repository was archived by the owner on Apr 26, 2024. It is now read-only.
  
  
  - 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 2.1k
          Fix have_seen_event cache not being invalidated
          #13863
        
      
          
     Merged
      
      
            MadLittleMods
  merged 13 commits into
  develop
from
madlittlemods/13856-fix-have-seen-events-not-being-invalidated
  
      
      
   
  Sep 27, 2022 
      
    
  
     Merged
                    Changes from 6 commits
      Commits
    
    
            Show all changes
          
          
            13 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      a847a35
              
                Fix `have_seen_event` cache not being invalidated
              
              
                MadLittleMods f6393db
              
                Add changelog
              
              
                MadLittleMods f2a5c70
              
                Assert is not None
              
              
                MadLittleMods 1054f91
              
                Merge branch 'develop' into madlittlemods/13856-fix-have-seen-events-…
              
              
                MadLittleMods 2162ab5
              
                Invalidate cache like #13796
              
              
                MadLittleMods 0cdc7bf
              
                Fix `@cachedList` on `_have_seen_events_dict`
              
              
                MadLittleMods 5b9b645
              
                Add test description
              
              
                MadLittleMods 9fb750d
              
                Better changelog
              
              
                MadLittleMods 4fa8f05
              
                Add test to make sure we can actually clear entries just by room_id
              
              
                MadLittleMods b9be6c5
              
                Raise exception so we don't run into this arg mismatch again
              
              
                MadLittleMods f8dc17b
              
                Add test to ensure the safety works
              
              
                MadLittleMods af93b3c
              
                Merge branch 'develop' into madlittlemods/13856-fix-have-seen-events-…
              
              
                MadLittleMods 0d0f54e
              
                Merge branch 'develop' into madlittlemods/13856-fix-have-seen-events-…
              
              
                MadLittleMods File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fix `have_seen_event` cache not being invalidated after we persist an event which causes downstream effects like extra `/state` federation calls. | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -1474,32 +1474,38 @@ async def have_seen_events( | |
| # the batches as big as possible. | ||
|  | ||
| results: Set[str] = set() | ||
| for chunk in batch_iter(event_ids, 500): | ||
| r = await self._have_seen_events_dict( | ||
| [(room_id, event_id) for event_id in chunk] | ||
| for event_ids_chunk in batch_iter(event_ids, 500): | ||
| events_seen_dict = await self._have_seen_events_dict( | ||
| room_id, event_ids_chunk | ||
| ) | ||
| results.update( | ||
| eid for (eid, have_event) in events_seen_dict.items() if have_event | ||
| ) | ||
| results.update(eid for ((_rid, eid), have_event) in r.items() if have_event) | ||
|  | ||
| return results | ||
|  | ||
| @cachedList(cached_method_name="have_seen_event", list_name="keys") | ||
| @cachedList(cached_method_name="have_seen_event", list_name="event_ids") | ||
| async def _have_seen_events_dict( | ||
| self, keys: Collection[Tuple[str, str]] | ||
| ) -> Dict[Tuple[str, str], bool]: | ||
| self, | ||
| room_id: str, | ||
| event_ids: Collection[str], | ||
| ) -> Dict[str, bool]: | ||
| 
      Comment on lines
    
      +1487
     to 
      +1492
    
   There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the fix as described by @erikjohnston in #13865 (comment) The rest of the changes in this file are adapting to this change. | ||
| """Helper for have_seen_events | ||
|  | ||
| Returns: | ||
| a dict {(room_id, event_id)-> bool} | ||
| a dict {event_id -> bool} | ||
| """ | ||
| # if the event cache contains the event, obviously we've seen it. | ||
|  | ||
| cache_results = { | ||
| (rid, eid) | ||
| for (rid, eid) in keys | ||
| if await self._get_event_cache.contains((eid,)) | ||
| event_id | ||
| for event_id in event_ids | ||
| if await self._get_event_cache.contains((event_id,)) | ||
| } | ||
| results = dict.fromkeys(cache_results, True) | ||
| remaining = [k for k in keys if k not in cache_results] | ||
| remaining = [ | ||
| event_id for event_id in event_ids if event_id not in cache_results | ||
| ] | ||
| if not remaining: | ||
| return results | ||
|  | ||
|  | @@ -1511,23 +1517,21 @@ def have_seen_events_txn(txn: LoggingTransaction) -> None: | |
|  | ||
| sql = "SELECT event_id FROM events AS e WHERE " | ||
| clause, args = make_in_list_sql_clause( | ||
| txn.database_engine, "e.event_id", [eid for (_rid, eid) in remaining] | ||
| txn.database_engine, "e.event_id", remaining | ||
| ) | ||
| txn.execute(sql + clause, args) | ||
| found_events = {eid for eid, in txn} | ||
|  | ||
| # ... and then we can update the results for each key | ||
| results.update( | ||
| {(rid, eid): (eid in found_events) for (rid, eid) in remaining} | ||
| ) | ||
| results.update({eid: (eid in found_events) for eid in remaining}) | ||
|  | ||
| await self.db_pool.runInteraction("have_seen_events", have_seen_events_txn) | ||
| return results | ||
|  | ||
| @cached(max_entries=100000, tree=True) | ||
| async def have_seen_event(self, room_id: str, event_id: str) -> bool: | ||
| res = await self._have_seen_events_dict(((room_id, event_id),)) | ||
| return res[(room_id, event_id)] | ||
| res = await self._have_seen_events_dict(room_id, [event_id]) | ||
| return res[event_id] | ||
|  | ||
| def _get_current_state_event_counts_txn( | ||
| self, txn: LoggingTransaction, room_id: str | ||
|  | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.