@@ -92,6 +92,10 @@ class TMemoryResource : public TAtomicRefCount<TMemoryResource> {
9292 SpillingCookie->SpillingPercentReached .store (Available () < OverLimit);
9393 }
9494
95+ ui64 GetUsed () const {
96+ return Used;
97+ }
98+
9599 void Release (ui64 value) {
96100 if (Used > value) {
97101 Used -= value;
@@ -103,7 +107,7 @@ class TMemoryResource : public TAtomicRefCount<TMemoryResource> {
103107 }
104108
105109 void SetNewLimit (ui64 baseLimit, double memoryPoolPercent, double overPercent) {
106- if (abs (memoryPoolPercent - MemoryPoolPercent) < MYEPS && baseLimit ! = BaseLimit)
110+ if (abs (memoryPoolPercent - MemoryPoolPercent) < MYEPS && baseLimit = = BaseLimit)
107111 return ;
108112
109113 BaseLimit = baseLimit;
@@ -256,7 +260,7 @@ class TKqpResourceManager : public IKqpResourceManager {
256260 task->TotalMemoryCookie = TotalMemoryResource->GetSpillingCookie ();
257261
258262 if (hasScanQueryMemory && !tx->PoolId .empty () && tx->MemoryPoolPercent > 0 ) {
259- auto [it, success] = MemoryNamedPools.emplace (tx->PoolId , nullptr );
263+ auto [it, success] = MemoryNamedPools.emplace (tx->MakePoolId () , nullptr );
260264
261265 if (success) {
262266 it->second = MakeIntrusive<TMemoryResource>(TotalMemoryResource->GetLimit (), tx->MemoryPoolPercent , SpillingPercent.load ());
@@ -291,9 +295,15 @@ class TKqpResourceManager : public IKqpResourceManager {
291295 Counters->RmNotEnoughMemory ->Inc ();
292296 with_lock (Lock) {
293297 TotalMemoryResource->Release (resources.Memory );
294- auto it = MemoryNamedPools.find (tx->PoolId );
295- if (it != MemoryNamedPools.end ()) {
296- it->second ->Release (resources.Memory );
298+ if (!tx->PoolId .empty ()) {
299+ auto it = MemoryNamedPools.find (tx->MakePoolId ());
300+ if (it != MemoryNamedPools.end ()) {
301+ it->second ->Release (resources.Memory );
302+ }
303+
304+ if (it->second ->GetUsed () == 0 ) {
305+ MemoryNamedPools.erase (it);
306+ }
297307 }
298308 }
299309 }
@@ -356,9 +366,15 @@ class TKqpResourceManager : public IKqpResourceManager {
356366 if (resources.Memory > 0 ) {
357367 with_lock (Lock) {
358368 TotalMemoryResource->Release (resources.Memory );
359- auto it = MemoryNamedPools.find (tx->PoolId );
360- if (it != MemoryNamedPools.end ()) {
361- it->second ->Release (resources.Memory );
369+ if (!tx->PoolId .empty ()) {
370+ auto it = MemoryNamedPools.find (tx->MakePoolId ());
371+ if (it != MemoryNamedPools.end ()) {
372+ it->second ->Release (resources.Memory );
373+
374+ if (it->second ->GetUsed () == 0 ) {
375+ MemoryNamedPools.erase (it);
376+ }
377+ }
362378 }
363379 }
364380 }
@@ -509,7 +525,7 @@ class TKqpResourceManager : public IKqpResourceManager {
509525 std::shared_ptr<TResourceSnapshotState> ResourceSnapshotState;
510526 TActorId ResourceInfoExchanger = TActorId();
511527
512- absl::flat_hash_map<TString, TIntrusivePtr<TMemoryResource>> MemoryNamedPools;
528+ absl::flat_hash_map<std::pair< TString, TString>, TIntrusivePtr<TMemoryResource>, THash<std::pair<TString, TString> >> MemoryNamedPools;
513529};
514530
515531struct TResourceManagers {
0 commit comments