|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <gtest/gtest.h> |
| 18 | +#include "dwio/nimble/common/Buffer.h" |
| 19 | +#include "dwio/nimble/common/Exceptions.h" |
| 20 | +#include "folly/executors/CPUThreadPoolExecutor.h" |
| 21 | +#include "velox/common/memory/Memory.h" |
| 22 | +#include "velox/dwio/common/ExecutorBarrier.h" |
| 23 | + |
| 24 | +namespace facebook::nimble::test { |
| 25 | +using MemoryPool = velox::memory::MemoryPool; |
| 26 | +using ExecutorBarrier = velox::dwio::common::ExecutorBarrier; |
| 27 | + |
| 28 | +class BufferPoolTest : public ::testing::Test { |
| 29 | + protected: |
| 30 | + static void SetUpTestCase() {} |
| 31 | + |
| 32 | + void SetUp() override { |
| 33 | + memPool_ = velox::memory::deprecatedAddDefaultLeafMemoryPool(); |
| 34 | + executor_ = std::make_unique<folly::CPUThreadPoolExecutor>(1); |
| 35 | + } |
| 36 | + |
| 37 | + std::shared_ptr<velox::memory::MemoryPool> memPool_; |
| 38 | + std::unique_ptr<folly::CPUThreadPoolExecutor> executor_; |
| 39 | +}; |
| 40 | + |
| 41 | +TEST_F(BufferPoolTest, CreateBufferPool) { |
| 42 | + auto bufferPool = BufferPool{*memPool_, std::move(executor_)}; |
| 43 | + EXPECT_NO_THROW(bufferPool); |
| 44 | +} |
| 45 | + |
| 46 | +TEST_F(BufferPoolTest, CreateBufferPoolWithInitialSize) { |
| 47 | + auto bufferPool = BufferPool{ |
| 48 | + *memPool_, std::move(executor_), std::chrono::milliseconds{10}, 10, 10}; |
| 49 | + EXPECT_EQ(bufferPool.size(), 10); |
| 50 | +} |
| 51 | + |
| 52 | +TEST_F(BufferPoolTest, CreateBufferPoolBadTimeout) { |
| 53 | + auto throwLambda = [&]() { |
| 54 | + auto bufferPool = BufferPool{ |
| 55 | + *memPool_, std::move(executor_), std::chrono::milliseconds{0}}; |
| 56 | + }; |
| 57 | + EXPECT_THROW(throwLambda(), NimbleUserError); |
| 58 | + |
| 59 | + try { |
| 60 | + throwLambda(); |
| 61 | + } catch (const NimbleUserError& e) { |
| 62 | + EXPECT_EQ(e.errorMessage(), "timeout must be > 0"); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +TEST_F(BufferPoolTest, CreateBufferPoolBadMaxPool) { |
| 67 | + auto throwLambda = [&]() { |
| 68 | + auto bufferPool = BufferPool{ |
| 69 | + *memPool_, std::move(executor_), std::chrono::milliseconds{1}, 0}; |
| 70 | + }; |
| 71 | + EXPECT_THROW(throwLambda(), NimbleUserError); |
| 72 | + |
| 73 | + try { |
| 74 | + throwLambda(); |
| 75 | + } catch (const NimbleUserError& e) { |
| 76 | + EXPECT_EQ(e.errorMessage(), "max pool size must be > 0"); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +TEST_F(BufferPoolTest, CreateBufferPoolBadInitialSize) { |
| 81 | + auto throwLambda = [&]() { |
| 82 | + auto bufferPool = BufferPool{ |
| 83 | + *memPool_, std::move(executor_), std::chrono::milliseconds{1}, 10, 0}; |
| 84 | + }; |
| 85 | + EXPECT_THROW(throwLambda(), NimbleUserError); |
| 86 | + try { |
| 87 | + throwLambda(); |
| 88 | + } catch (const NimbleUserError& e) { |
| 89 | + EXPECT_EQ(e.errorMessage(), "initial pool size must be > 0"); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +TEST_F(BufferPoolTest, CreateBufferPoolBadInitialSizeMaxSize) { |
| 94 | + auto throwLambda = [&]() { |
| 95 | + auto bufferPool = BufferPool{ |
| 96 | + *memPool_, std::move(executor_), std::chrono::milliseconds{1}, 1, 2}; |
| 97 | + }; |
| 98 | + EXPECT_THROW(throwLambda(), NimbleUserError); |
| 99 | + |
| 100 | + try { |
| 101 | + throwLambda(); |
| 102 | + } catch (const NimbleUserError& e) { |
| 103 | + EXPECT_EQ(e.errorMessage(), "initial pool size must be <= max pool size"); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +TEST_F(BufferPoolTest, ReserveBuffer) { |
| 108 | + auto bufferPool = BufferPool{*memPool_, std::move(executor_)}; |
| 109 | + auto buffer = bufferPool.reserveBuffer(); |
| 110 | + EXPECT_NE(buffer, nullptr); |
| 111 | +} |
| 112 | + |
| 113 | +TEST_F(BufferPoolTest, AddBuffer) { |
| 114 | + auto bufferPool = BufferPool{*memPool_, std::move(executor_)}; |
| 115 | + auto buffer = bufferPool.reserveBuffer(); |
| 116 | + EXPECT_NE(buffer, nullptr); |
| 117 | + EXPECT_EQ(bufferPool.size(), 0); |
| 118 | + bufferPool.addBuffer(std::move(buffer)); |
| 119 | + EXPECT_EQ(bufferPool.size(), 1); |
| 120 | +} |
| 121 | + |
| 122 | +TEST_F(BufferPoolTest, DestroyBufferPool) { |
| 123 | + auto bufferPoolPtr = |
| 124 | + std::make_unique<BufferPool>(*memPool_, std::move(executor_)); |
| 125 | + bufferPoolPtr.reset(); |
| 126 | + EXPECT_NO_THROW(); |
| 127 | +} |
| 128 | + |
| 129 | +TEST_F(BufferPoolTest, emptyBufferPool) { |
| 130 | + auto bufferPool = BufferPool{ |
| 131 | + *memPool_, std::move(executor_), std::chrono::milliseconds{10}, 1}; |
| 132 | + EXPECT_EQ(bufferPool.size(), 1); |
| 133 | + |
| 134 | + auto buffer1 = bufferPool.reserveBuffer(); |
| 135 | + EXPECT_EQ(bufferPool.size(), 0); |
| 136 | +} |
| 137 | + |
| 138 | +TEST_F(BufferPoolTest, OverfillBufferPool) { |
| 139 | + // not guarenteed to fail at size of 2 due to DMPMCQueue |
| 140 | + auto bufferPool = BufferPool{ |
| 141 | + *memPool_, std::move(executor_), std::chrono::milliseconds{10}, 1}; |
| 142 | + auto throwLambda = [&]() { |
| 143 | + for (auto i = 0; i < 5; i++) { |
| 144 | + auto buffer = std::make_unique<Buffer>(*memPool_); |
| 145 | + bufferPool.addBuffer(std::move(buffer)); |
| 146 | + } |
| 147 | + }; |
| 148 | + |
| 149 | + EXPECT_THROW(throwLambda(), NimbleInternalError); |
| 150 | +} |
| 151 | + |
| 152 | +TEST_F(BufferPoolTest, FillEmptyFillBufferPool) { |
| 153 | + size_t iterations = 10; |
| 154 | + std::vector<BufferPtr> buffers; |
| 155 | + |
| 156 | + auto bufferPool = BufferPool{ |
| 157 | + *memPool_, |
| 158 | + std::move(executor_), |
| 159 | + std::chrono::milliseconds{10}, |
| 160 | + iterations}; |
| 161 | + for (auto i = 0; i < iterations; i++) { |
| 162 | + auto buffer = bufferPool.reserveBuffer(); |
| 163 | + buffers.push_back(std::move(buffer)); |
| 164 | + } |
| 165 | + EXPECT_EQ(bufferPool.size(), 0); |
| 166 | + EXPECT_EQ(buffers.size(), iterations); |
| 167 | + for (auto& buffer : buffers) { |
| 168 | + bufferPool.addBuffer(std::move(buffer)); |
| 169 | + } |
| 170 | + EXPECT_EQ(bufferPool.size(), iterations); |
| 171 | + buffers.clear(); |
| 172 | + |
| 173 | + for (auto i = 0; i < iterations; i++) { |
| 174 | + auto buffer = bufferPool.reserveBuffer(); |
| 175 | + buffers.push_back(std::move(buffer)); |
| 176 | + } |
| 177 | + EXPECT_EQ(bufferPool.size(), 0); |
| 178 | + EXPECT_EQ(buffers.size(), iterations); |
| 179 | +} |
| 180 | + |
| 181 | +TEST_F(BufferPoolTest, ParallelFillPool) { |
| 182 | + folly::CPUThreadPoolExecutor executor{10}; |
| 183 | + ExecutorBarrier barrier{executor}; |
| 184 | + auto bufferPool = BufferPool{ |
| 185 | + *memPool_, |
| 186 | + std::move(executor_), |
| 187 | + std::chrono::milliseconds{1000 * 10}, |
| 188 | + 100}; |
| 189 | + auto fillPool = [&]() { |
| 190 | + for (auto i = 0; i < 10; i++) { |
| 191 | + EXPECT_NO_THROW( |
| 192 | + auto buffer = bufferPool.reserveBuffer(); |
| 193 | + std::this_thread::sleep_for(std::chrono::milliseconds{1000}); |
| 194 | + bufferPool.addBuffer(std::move(buffer));); |
| 195 | + } |
| 196 | + }; |
| 197 | + |
| 198 | + for (auto i = 0; i < 10; i++) { |
| 199 | + barrier.add(fillPool); |
| 200 | + } |
| 201 | + |
| 202 | + barrier.waitAll(); |
| 203 | +} |
| 204 | + |
| 205 | +} // namespace facebook::nimble::test |
0 commit comments