Skip to content

Commit fc09cb9

Browse files
medjed02meta-codesync[bot]
authored andcommitted
Stop the pool optimizer in stopWorkers() (#518)
Summary: `stopWorkers()` stopped every background worker except the pool optimizer, so the optimizer could still resize pools while `shutDown()` was serializing the cache. Now `stopWorkers()` stops it too. Pull Request resolved: #518 Test Plan: - New `SimplePoolOptimizationTest.StopWorkersStopsPoolOptimizer` waits for the optimizer to run, calls `stopWorkers()` and checks that the strategy is not called again; it fails without the fix. - Full ctest passes. Reviewed By: rlyerly Differential Revision: D131236874 Pulled By: AlnisM fbshipit-source-id: 346ccce2f92fabf522e11bbb3d2654f530dfa898
1 parent af7c1fe commit fc09cb9

3 files changed

Lines changed: 36 additions & 0 deletions

File tree

‎cachelib/allocator/CacheAllocator.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6359,6 +6359,7 @@ folly::IOBufQueue CacheAllocator<CacheTrait>::saveStateToIOBuf() {
63596359
template <typename CacheTrait>
63606360
bool CacheAllocator<CacheTrait>::stopWorkers(std::chrono::seconds timeout) {
63616361
bool success = true;
6362+
success &= stopPoolOptimizer(timeout);
63626363
success &= stopPoolRebalancer(timeout);
63636364
success &= stopPoolResizer(timeout);
63646365
success &= stopMemMonitor(timeout);

‎cachelib/allocator/tests/SimplePoolOptimizationTest.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ TYPED_TEST(SimplePoolOptimizationTest, PoolOptimizerBasic) {
2929
this->testPoolOptimizerBasic();
3030
}
3131

32+
TYPED_TEST(SimplePoolOptimizationTest, StopWorkersStopsPoolOptimizer) {
33+
this->testStopWorkersStopsPoolOptimizer();
34+
}
35+
3236
} // namespace tests
3337
} // namespace cachelib
3438
} // namespace facebook

‎cachelib/allocator/tests/SimplePoolOptimizationTest.h‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
#include <folly/Random.h>
2020
#include <gtest/gtest.h>
2121

22+
#include <atomic>
23+
#include <chrono>
2224
#include <future>
25+
#include <thread>
2326

2427
#include "cachelib/allocator/CCacheAllocator.h"
2528
#include "cachelib/allocator/PoolOptimizer.h"
@@ -85,6 +88,34 @@ struct SimplePoolOptimizeStrategy : public PoolOptimizeStrategy {
8588
template <typename AllocatorT>
8689
class SimplePoolOptimizationTest : public testing::Test {
8790
public:
91+
void testStopWorkersStopsPoolOptimizer() {
92+
struct CountingStrategy : public PoolOptimizeStrategy {
93+
std::atomic<int> calls{0};
94+
95+
protected:
96+
PoolOptimizeContext pickVictimAndReceiverRegularPoolsImpl(
97+
const CacheBase&) override {
98+
++calls;
99+
return kNoOpContext;
100+
}
101+
};
102+
auto strategy = std::make_shared<CountingStrategy>();
103+
typename AllocatorT::Config config;
104+
config.setCacheSize(30 * Slab::kSize);
105+
config.enablePoolOptimizer(strategy, std::chrono::seconds{1},
106+
std::chrono::seconds{1}, 1);
107+
AllocatorT alloc(config);
108+
for (int i = 0; i < 100 && strategy->calls == 0; ++i) {
109+
std::this_thread::sleep_for(std::chrono::milliseconds{100});
110+
}
111+
ASSERT_GT(strategy->calls.load(), 0);
112+
113+
ASSERT_TRUE(alloc.stopWorkers());
114+
const int calls = strategy->calls.load();
115+
std::this_thread::sleep_for(std::chrono::seconds{2});
116+
EXPECT_EQ(calls, strategy->calls.load());
117+
}
118+
88119
void testPoolOptimizerBasic() {
89120
typename AllocatorT::Config config;
90121
config.setCacheSize(30 * Slab::kSize);

0 commit comments

Comments
 (0)