Skip to content

Commit

Permalink
Fix update bug in ets_lru
Browse files Browse the repository at this point in the history
Previously, when an existing cache entry was updated, we didn't update the
atime. Subsequently, when the entry was removed, we never cleaned the orphan
atime entry. During trimming, we traverse the atime entries, and since the
orphaned one didn't have any matching objects or ctime entries, the ets_lru
cache would crash, possibly taking down the primary couch supervisor.
  • Loading branch information
nickva committed Nov 9, 2023
1 parent 484847d commit 5a21bb2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/ets_lru/src/ets_lru.erl
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,9 @@ handle_call({insert, Key, Val}, _From, St) ->
Pattern = #entry{key = Key, atime = '$1', _ = '_'},
case ets:match(St#st.objects, Pattern) of
[[ATime]] ->
Update = {#entry.val, Val},
true = ets:update_element(St#st.objects, Key, Update),
true = ets:update_element(St#st.objects, Key, [
{#entry.val, Val}, {#entry.atime, NewATime}
]),
true = ets:delete(St#st.atimes, ATime),
true = ets:insert(St#st.atimes, {NewATime, Key});
[] ->
Expand Down
28 changes: 28 additions & 0 deletions src/ets_lru/test/ets_lru_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,34 @@ basic_behavior_test_() ->
?_assertEqual(not_found, ets_lru:lookup(LRU, foo))
}
]
end,
fun({ok, LRU}) ->
[
{
"Insert the value twice",
?_assertEqual(ok, ets_lru:insert(LRU, foo, bar))
},
{
"Objects table size should be 1",
?_assertEqual(1, ets:info(test_lru_objects, size))
},
{
"ATimes table size should be 1",
?_assertEqual(1, ets:info(test_lru_atimes, size))
},
{
"CTimes table size should be 1",
?_assertEqual(1, ets:info(test_lru_ctimes, size))
},
{
"Clear LRU after duplicate insert",
?_assertEqual(ok, ets_lru:clear(LRU))
},
{
"Lookup returned not_found after a clear after a duplicate insert",
?_assertEqual(not_found, ets_lru:lookup(LRU, foo))
}
]
end
]}
}.
Expand Down

0 comments on commit 5a21bb2

Please sign in to comment.