Skip to content

Remove inheritence relationship between AsyncDataCache and MemoryAllo…#5503

Closed
tanjialiang wants to merge 1 commit intofacebookincubator:mainfrom
tanjialiang:sep_cache
Closed

Remove inheritence relationship between AsyncDataCache and MemoryAllo…#5503
tanjialiang wants to merge 1 commit intofacebookincubator:mainfrom
tanjialiang:sep_cache

Conversation

@tanjialiang
Copy link
Contributor

@tanjialiang tanjialiang commented Jul 4, 2023

Removing the relationship of AsyncDataCache inheritance from MemoryAllocator. Now they are depending on each other with registration mechanism. Related tests are refactored to be consistent with the new change.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 4, 2023
@netlify
Copy link

netlify bot commented Jul 4, 2023

Deploy Preview for meta-velox canceled.

Name Link
🔨 Latest commit 706e6f3
🔍 Latest deploy log https://app.netlify.com/sites/meta-velox/deploys/64bf021809dfa90008f53bd4

Copy link
Contributor

@xiaoxmeng xiaoxmeng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tanjialiang some comments. Thanks!


protected:
MemoryAllocator() = default;
explicit MemoryAllocator() = default;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop explicit

MemoryAllocator() = default;
explicit MemoryAllocator() = default;

virtual bool allocateContiguousImpl(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: not sure if allocateContiguousWithoutRetry is better?

/// allocation fails. 'reservationCB' is used in the same way as allocate
/// does. It may throw and the end state will be consistent, with no new
/// allocation and 'allocation' and 'collateral' cleared.
/// allocation and 'allocation' and 'collateral' cleared. The allocator will
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function might retry allocation failure by making space from 'cache_' if set.

dittos

/// A structure that uses 'allocator_' to consume memory, and is also able to
/// free up memory upon request by shrinking itself, to give memory back to
/// 'allocator_'.
class Shrinkable {
Copy link
Contributor

@xiaoxmeng xiaoxmeng Jul 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class MemoryAllocator::Cache {
   virtual bool makeSpace(memory::MachinePageCount numPages, std::function<bool()> allocate) = 0;

/// the kind of the delegated memory allocator underneath.
virtual Kind kind() const = 0;

void setShrinkable(const std::shared_ptr<Shrinkable>& shrinkable) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/setShrinkable/setCache/ and add to check we only allow to set it once from null to non-null? thanks!

@tanjialiang tanjialiang force-pushed the sep_cache branch 7 times, most recently from 171fea8 to bd736d8 Compare July 12, 2023 21:17
@tanjialiang tanjialiang marked this pull request as ready for review July 12, 2023 21:20
Copy link
Contributor

@xiaoxmeng xiaoxmeng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tanjialiang Looks great. Thanks for interating!

public:
/// This method should be implemented so that it tries to accommodate the
/// passed in 'allocate' by freeing up space from 'this' if needed. 'numPages'
/// is the number of pages 'allocate' tries to allocate. The default
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function should return true if 'allocate' succeeds, and false otherwise.

Remove dummy part.

/// Unregisters the 'Cache' that was previously registered. It is the caller's
/// responsibility to unregister the cache upon destruction of the 'Cache'.
///
/// NOTE: Caller should make sure that unregistration of 'Cache' shall happen
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: the caller should ensures that unregistration of 'Cache' shall happen before its destruction and all the allocated cached memory has been freed.

It might not be safe to unregister the cache as the memory allocator can access the cache at any point so there is a race condition there. I don't think we really need unregister cache operation. Let's hold a shared reference on the cache from the allocator. And make sure there is no memory usage from cache when memory allocator destroys. We could add the allocated memory in cache interface.

Allocation& out,
ReservationCallback reservationCB,
MachinePageCount minSizeClass) {
if (!cache_) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: if (cache_ == nullptr)

ContiguousAllocation& allocation,
ReservationCallback reservationCB);

void freeContiguous(ContiguousAllocation& allocation) override;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a public method?

}

AsyncDataCache::~AsyncDataCache() {
allocator_->unregisterCache(this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have a check that all memory have been freed instead.

@tanjialiang tanjialiang force-pushed the sep_cache branch 3 times, most recently from 7884ac8 to 4df1851 Compare July 14, 2023 04:10
Copy link
Contributor

@xiaoxmeng xiaoxmeng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tanjialiang thanks for the update and iterations!

allocator_ = std::make_shared<cache::AsyncDataCache>(
allocator, memoryBytes, std::move(ssdCache));
allocator_ = std::make_shared<memory::MmapAllocator>(options);
cache_ = cache::AsyncDataCache::createAsyncDataCache(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/cache::AsyncDataCache::createAsyncDataCache/cache::AsyncDataCache::create/


AsyncDataCache::AsyncDataCache(
const std::shared_ptr<MemoryAllocator>& allocator,
memory::MemoryAllocator* const allocator,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop pointer const qualifier as we pass the pointer by value.


AsyncDataCache::AsyncDataCache(
const std::shared_ptr<MemoryAllocator>& allocator,
memory::MemoryAllocator* const allocator,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

public:
// TODO(jtan6): Remove this constructor after Presto Native switches to below
// constructor
AsyncDataCache(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this ctor now?


// static
std::shared_ptr<AsyncDataCache> AsyncDataCache::createAsyncDataCache(
memory::MemoryAllocator* const allocator,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

AsyncDataCache::create

}

// static
AsyncDataCache*& AsyncDataCache::getInstanceInternal() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AsyncDataCache** AsyncDataCache::getInstancePtr() {
  return &cache_;
}

AsyncDataCache* AsyncDataCache::getInstancePtr() {
  return *getInstanceInternal();
}

std::unique_ptr<ManagedMmapArenas> managedArenas_;

std::shared_ptr<Cache> cache_;
Stats stats_;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still stats_ here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOPE

@tanjialiang tanjialiang force-pushed the sep_cache branch 2 times, most recently from ae2e0d3 to 7a7f9ad Compare July 17, 2023 01:17
@facebook-github-bot
Copy link
Contributor

@tanjialiang has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@tanjialiang has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@tanjialiang tanjialiang force-pushed the sep_cache branch 5 times, most recently from aa7316f to db4a5e4 Compare July 20, 2023 22:36
@facebook-github-bot
Copy link
Contributor

@tanjialiang has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

tanjialiang added a commit to tanjialiang/velox-1 that referenced this pull request Jul 21, 2023
facebookincubator#5503)

Summary:
Removing the relationship of AsyncDataCache inheritance from MemoryAllocator. Now they are depending on each other with registration mechanism. Related tests are refactored to be consistent with the new change.

Pull Request resolved: facebookincubator#5503

Reviewed By: xiaoxmeng

Differential Revision: D47536273

Pulled By: tanjialiang

fbshipit-source-id: ce91a4eaf46297a5146ea90743b0e8f0833ab13d
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D47536273

tanjialiang added a commit to tanjialiang/velox-1 that referenced this pull request Jul 21, 2023
facebookincubator#5503)

Summary:
Removing the relationship of AsyncDataCache inheritance from MemoryAllocator. Now they are depending on each other with registration mechanism. Related tests are refactored to be consistent with the new change.

Pull Request resolved: facebookincubator#5503

Reviewed By: xiaoxmeng

Differential Revision: D47536273

Pulled By: tanjialiang

fbshipit-source-id: e1edc0d967b211fc092adfeac02aefc3276a7a9c
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D47536273

facebookincubator#5503)

Summary:
X-link: prestodb/presto#20372

Removing the relationship of AsyncDataCache inheritance from MemoryAllocator. Now they are depending on each other with registration mechanism. Related tests are refactored to be consistent with the new change.

Pull Request resolved: facebookincubator#5503

Reviewed By: xiaoxmeng

Differential Revision: D47536273

Pulled By: tanjialiang

fbshipit-source-id: 9a02e8dc5e1f05a00d4cd7e2828fdd5e8648669e
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D47536273

}
}

void CacheShard::prepareShutdown() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just name it as shutdown()


static void setInstance(AsyncDataCache* asyncDataCache);

/// Release any resources that consume memory from 'allocator_' for a graceful
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/// Release any memory allocated from 'allocator_' for a graceful shutdown.
///
/// NOTE: user can not access cache after this call as the cache is no longer valid.

@facebook-github-bot
Copy link
Contributor

@tanjialiang merged this pull request in ad9ffa1.

tanjialiang added a commit to tanjialiang/presto that referenced this pull request Jul 25, 2023
prestodb#20372)

Summary:
Pull Request resolved: prestodb#20372

Removing the relationship of AsyncDataCache inheritance from MemoryAllocator. Now they are depending on each other with registration mechanism. Related tests are refactored to be consistent with the new change.

X-link: facebookincubator/velox#5503

Reviewed By: xiaoxmeng

Differential Revision: D47536273

Pulled By: tanjialiang

fbshipit-source-id: 7749b787bf2a1027b33e02690917c59bb497026c
mbasmanova pushed a commit to prestodb/presto that referenced this pull request Jul 26, 2023
#20372)

Summary:
Pull Request resolved: #20372

Removing the relationship of AsyncDataCache inheritance from MemoryAllocator. Now they are depending on each other with registration mechanism. Related tests are refactored to be consistent with the new change.

X-link: facebookincubator/velox#5503

Reviewed By: xiaoxmeng

Differential Revision: D47536273

Pulled By: tanjialiang

fbshipit-source-id: 7749b787bf2a1027b33e02690917c59bb497026c
wypb pushed a commit to wypb/presto that referenced this pull request Dec 22, 2023
prestodb#20372)

Summary:
Pull Request resolved: prestodb#20372

Removing the relationship of AsyncDataCache inheritance from MemoryAllocator. Now they are depending on each other with registration mechanism. Related tests are refactored to be consistent with the new change.

X-link: facebookincubator/velox#5503

Reviewed By: xiaoxmeng

Differential Revision: D47536273

Pulled By: tanjialiang

fbshipit-source-id: 7749b787bf2a1027b33e02690917c59bb497026c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants