BitFaster.Caching icon indicating copy to clipboard operation
BitFaster.Caching copied to clipboard

Mitigate LFU struct tearing (new node)

Open bitfaster opened this issue 1 year ago • 1 comments
trafficstars

When updating a large struct, instead of updating in place, we can instead create a new node. Since nodes are not updated, no tearing can occur.

Time based expiry is a special case that is handled as follows:

  • The new INodePolicy.Clone method creates the replacement node to insert into the dictionary. For ExpireAfterPolicy this avoids calling expiry calculator on create, and we set current expiry to match old node. We also set GetNextInTimeOrder == self, this is a hack so that ExpireAfterPolicy.OnWrite knows the node was not created and is currently detached from the TimerWheel.
  • In ExpireAfterPolicy.OnWrite, check if next == self to schedule/reschedule. Schedule will just overwrite next to a valid node.
  • Add note to GetNextInTimeOrder to explain

TODO:

  • Unit test
  • Soak test
  • Verify TLFU logic

bitfaster avatar May 26 '24 01:05 bitfaster