cryptomator / cryptofs

Java Filesystem Provider with integrated encryption
GNU Affero General Public License v3.0
94 stars 35 forks source link

Chunk ByteBuffer to soon recycled #158

Closed infeo closed 1 year ago

infeo commented 1 year ago

In PR https://github.com/cryptomator/cryptofs/pull/125, the class BufferPool was introduced to reduce buffer allocations and reuse already allocated buffers for de- and encryption. The affected buffers are part of "Chunk" records, which represent a contigous memory area in a file.

A buffer is recylced, when:

The third point (recycle on eviction) causes a severe bug. Imagine the following scenario:

Low hit rate -> High number of cache evictions -> High recylce rate. But only because a chunk is evicted does not imply, the thread reading on the chunk has finished its operation. If the recycled buffer is selected to be filled again, the (still not finished) thread is reading wrong content.

infeo commented 1 year ago

To recycle the data buffer of Chunks, a synchronization mechanism is needed which ensures:

  1. Once the chunk is evicted from cache, no new accesses to data can be made
  2. the chunk keeps track of accesses
  3. Only if the last access is "closed", the buffer is recycled

This would add another layer of complexity, hence, for now we decided to keep it simple and just synchronize threads on methods in CleartextFileChannel.