Skip to content

Commit

Permalink
Fix cache race condition when reading time series (fixes DEV-22)
Browse files Browse the repository at this point in the history
  • Loading branch information
aptiko committed Nov 11, 2024
1 parent 1df3c1c commit 7e68757
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
13 changes: 11 additions & 2 deletions enhydris/models/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,17 @@ def _get_data_from_cache(self, start_date, end_date):
raise DataNotInCache()
if self.start_date is None:
return data # Data should be empty in that case; just return it
start_date = max(start_date, self.start_date).astimezone(data.index.tzinfo)
end_date = min(end_date, self.end_date).astimezone(data.index.tzinfo)
try:
start_date = max(start_date, self.start_date).astimezone(data.index.tzinfo)
end_date = min(end_date, self.end_date).astimezone(data.index.tzinfo)
except TypeError:
# A TypeError will occur if self.start_date or self.end_date above is none.
# This should normally not happen, because self.start_date had already
# been checked immediately before, and self.end_date is not none whenever
# self.start_date is not none. However there's a race condition where
# another thread or process could have updated the time series (and
# invalidated the cache) in between these statements.
raise DataNotInCache()
if data.index.min() > start_date or data.index.max() < end_date:
raise DataNotInCache()
return data.loc[start_date:end_date]
Expand Down
19 changes: 19 additions & 0 deletions enhydris/tests/test_models/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,25 @@ def test_cached_empty_dataframe(self):
data = self.timeseries.get_data(start_date=self.timeseries.end_date)
self.assertEqual(len(data.data), 1)

@mock.patch("enhydris.models.timeseries.Timeseries._invalidate_cached_data")
def test_race_condition(self, m):
# Populate cache
self._get_data_and_check_num_queries(1, start_date=None, end_date=None)

# Pretend there's a race condition. We delete the time series data, but
# because we've mocked _invalidate_cached_data, the cache will not be deleted.
# However, we manually delete the cached end date. This simulates the case
# where another thread or process deletes the data (and invalidates the
# cache) between examining the start date and end date.
self.timeseries.set_data(HTimeseries())
cache.delete(f"timeseries_end_date_{self.timeseries.id}")

# Try getting the data again and see that it works. There are two queries now;
# one is for the end date.
with self.assertNumQueries(2):
data = self.timeseries.get_data(start_date=None, end_date=None)
self.assertEqual(len(data.data), 0)


class TimeseriesSetDataTestCase(TestTimeseriesMixin, TestCase):
def setUp(self):
Expand Down

0 comments on commit 7e68757

Please sign in to comment.