From 041b6cba612f5640fe490859a54f0ef140e29d33 Mon Sep 17 00:00:00 2001
From: Mark Haines <mark.haines@matrix.org>
Date: Tue, 5 May 2015 16:32:44 +0100
Subject: [PATCH] SYN-369: Add comments to the sequence number logic in the
 cache

---
 synapse/storage/_base.py | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/synapse/storage/_base.py b/synapse/storage/_base.py
index 840a4994bb..579ed56377 100644
--- a/synapse/storage/_base.py
+++ b/synapse/storage/_base.py
@@ -98,6 +98,8 @@ class Cache(object):
     def update(self, sequence, *args):
         self.check_thread()
         if self.sequence == sequence:
+            # Only update the cache if the caches sequence number matches the
+            # number that the cache had before the SELECT was started (SYN-369)
             self.prefill(*args)
 
     def prefill(self, *args):  # because I can't  *keyargs, value
@@ -117,6 +119,8 @@ class Cache(object):
         self.check_thread()
         if len(keyargs) != self.keylen:
             raise ValueError("Expected a key to have %d items", self.keylen)
+        # Increment the sequence number so that any SELECT statements that
+        # raced with the INSERT don't update the cache (SYN-369)
         self.sequence += 1
         self.cache.pop(keyargs, None)
 
@@ -159,6 +163,9 @@ def cached(max_entries=1000, num_args=1, lru=False):
                         raise ValueError("Stale cache entry")
                 defer.returnValue(cached_result)
             except KeyError:
+                # Get the sequence number of the cache before reading from the
+                # database so that we can tell if the cache is invalidated
+                # while the SELECT is executing (SYN-369)
                 sequence = cache.sequence
 
                 ret = yield orig(self, *keyargs)
-- 
GitLab