Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Added unified cache memory storage for data caches with an integration of the general-purpose memory allocator.
Features
BlockCache-- the pool of the similar data caches. The pool has separate memory storage and eviction mechanism.RebalanceStrategy, which is a specific algorithm to extend/reduce the sizes of the cache pools. TheRebalanceStrategyis the point of configuration of the cache memory management.BlockCacheis allocated through mmap.BlockCacheis allocated through specialBuddyAllocatorwhich is a custom allocator with buddy schema. The size of the memory arena for the allocator is fixed. This rebalance strategy can take memory blocks from one cache pool and add them to another.Performance
The ClickBench results are shown in the figure below.
Our unified cache storage has a similar performance to the baseline both in cold and hot runs. The same performance in the cold run without populated caches means that there is no significant overhead to the writing to the cache storage, and the same performance in the hot run with populated caches means that there is no significant overhead to the reading from the cache storage. The performance difference over all queries is not greater than 3%.
Design
Overview
BlockCachewhich is a cache of static memory blocks. This class is based on the existingArrayCache.BlockCaches.RebalanceStrategyBlockCachedesignBlockCachestores underlying memory in the form of chunks.Chunkis an abstraction of the contiguous memory block that is given to theBlockCachefrom an external source. TheBlockCacheslices a chunk’s memory into parts which are assigned to regions.Regionis part of a chunk that is used as a memory storage for cache items. The metadata of regions is stored in the intrusive list that implements the LRU cache eviction policy. This policy is chosen as a baseline, because it is easy to implement, and it works well enough in most scenarios.BlockCacheprovides the memory of regions for cache items, but it must not evict regions which are currently in use to serve as an underlying memory for cache items.Holderwhich is an access container for regions.Holderprovides access to raw memory, but the current data caches do not work directly with raw memory.UnifiedCacheAdapteris implemented to connect current data caches with raw memory storage provided byHolder.BlockCachemust have dynamically changed underlying memory storage. TheChunkrepresents the underlying memory storage, so theBlockCachemust have support for adding chunks to it and taking chunks from it.BlockCache, we need to add it to the Free list. So we can use chunks from the Free list if we do not have enough free space in chunks in the Acquired list.BlockCache, we can directly do it from the Free list. This guarantees that we do not take a chunk from theBlockCachethat is currently used for some cache items.Dynamic
BuddyAllocatordesignWe want to allow the general-purpose allocator to take memory from the cache storage, so this requires modification to the general-purpose allocator. As we do not want to modify the external library jemalloc, we implement our allocator schema that can serve as a general-purpose allocator.
We chose the buddy schema among other allocation algorithms because:
BlockCachedesign.BuddyAllocatoronly for the cache items, so we need to add memory shrinkage and growth support in BuddyAllocator, so we can reuse memory from the cache storage for general-purpose allocations and dynamically change the size of the storage.BuddyAllocator.BuddyAllocatorallocates the absolute maximum of virtual memory without a population of the mapping to the physical memory.BuddyAllocatorinitializes metadata for the whole memory arena.Cache sharding
BlockCachethat serves as cache storage for a group of related data caches.initializeof the rebalance strategy is called by the cache pool during its initialization. Each cache pool has a unique name, so the rebalance strategy can distinguish cache pools between different calls to its methods. This method allows the rebalance strategy to assign starter memory chunks to the cache pool.finalizehas similar semantics, but it is needed so that the cache pool can return memory Chunks on its destruction.shouldRebalanceis that the cache pool can notify the rebalance strategy about the shortage of memory, so the rebalance strategy can add more chunks or allow evicting some cache items.shouldRebalancemethod is integrated into the pipeline of a newcache entry allocation.
is a space for further research.
BuddyAllocatoras the source of chunks for cache pools.BuddyAllocator, and thenBuddyAllocatorcan also shrink itself to return memory to the operating system, orBuddyAllocatorcan serve allocations directly as a general-purpose allocator.shouldRebalancealgorithm:Future work
BlockCache.Additional information
This work was part of my bachelor's thesis.
You can also check the slides from the project presentation.