Skip to content

Commit 9916b20

Browse files
committed
fix: respect disabled field data budget
Signed-off-by: Shawn Wang <shawn.wang@zilliz.com>
1 parent e751418 commit 9916b20

5 files changed

Lines changed: 81 additions & 18 deletions

File tree

internal/core/src/segcore/MemoryPlannerTest.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
#include <cstdint>
2121
#include <future>
2222
#include <memory>
23+
#include <optional>
2324
#include <string>
2425
#include <vector>
2526

2627
#include "arrow/api.h"
28+
#include "cachinglayer/LoadingOverheadTracker.h"
2729
#include <folly/CancellationToken.h>
2830
#include "folly/ScopeGuard.h"
2931
#include "common/EasyAssert.h"
@@ -209,6 +211,52 @@ TEST(FieldDataLoadBatchSplitTargetBytes, CapsTargetByConfiguredBudget) {
209211
EXPECT_EQ(FieldDataLoadBatchSplitTargetBytes(), 8 * MB);
210212
}
211213

214+
TEST(FieldDataLoadingOverheadUpperBound, UsesUnlimitedWhenBudgetDisabled) {
215+
auto& budget =
216+
milvus::storage::TransientMemoryBudget::GetLoadTransientBudget();
217+
auto old_capacity = budget.CapacityBytes();
218+
auto cleanup = folly::makeGuard(
219+
[&budget, old_capacity]() { budget.SetCapacityBytes(old_capacity); });
220+
221+
budget.SetCapacityBytes(0);
222+
223+
auto upper_bound =
224+
FieldDataLoadingOverheadUpperBound(/*max_memory_overhead=*/128);
225+
226+
EXPECT_EQ(
227+
upper_bound.memory_bytes,
228+
milvus::cachinglayer::LoadingOverheadTracker::kUnlimited.memory_bytes);
229+
EXPECT_EQ(
230+
upper_bound.file_bytes,
231+
milvus::cachinglayer::LoadingOverheadTracker::kUnlimited.file_bytes);
232+
}
233+
234+
TEST(FieldDataLoadingOverheadUpperBound, UsesBudgetWithMaxOverheadFloor) {
235+
constexpr int64_t MB = 1 << 20;
236+
auto& budget =
237+
milvus::storage::TransientMemoryBudget::GetLoadTransientBudget();
238+
auto old_capacity = budget.CapacityBytes();
239+
auto cleanup = folly::makeGuard(
240+
[&budget, old_capacity]() { budget.SetCapacityBytes(old_capacity); });
241+
242+
budget.SetCapacityBytes(8 * MB);
243+
244+
auto memory_only =
245+
FieldDataLoadingOverheadUpperBound(/*max_memory_overhead=*/16 * MB);
246+
EXPECT_EQ(memory_only.memory_bytes, 16 * MB);
247+
EXPECT_EQ(memory_only.file_bytes, 0);
248+
249+
auto mmap_smaller_overhead = FieldDataLoadingOverheadUpperBound(
250+
/*max_memory_overhead=*/4 * MB, std::optional<int64_t>{2 * MB});
251+
EXPECT_EQ(mmap_smaller_overhead.memory_bytes, 8 * MB);
252+
EXPECT_EQ(mmap_smaller_overhead.file_bytes, 8 * MB);
253+
254+
auto mmap_larger_file_overhead = FieldDataLoadingOverheadUpperBound(
255+
/*max_memory_overhead=*/4 * MB, std::optional<int64_t>{32 * MB});
256+
EXPECT_EQ(mmap_larger_file_overhead.memory_bytes, 8 * MB);
257+
EXPECT_EQ(mmap_larger_file_overhead.file_bytes, 32 * MB);
258+
}
259+
212260
// ---- LoadCellBatchAsync tests ----
213261

214262
namespace {

internal/core/src/segcore/memory_planner.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <vector>
2828

2929
#include "arrow/api.h"
30+
#include "cachinglayer/LoadingOverheadTracker.h"
3031
#include "common/Channel.h"
3132
#include "common/Common.h"
3233
#include "common/EasyAssert.h"
@@ -115,6 +116,24 @@ SetFieldDataMaxReadParallelism(int64_t parallelism) {
115116
FIELD_DATA_MAX_READ_PARALLELISM.load());
116117
}
117118

119+
milvus::cachinglayer::ResourceUsage
120+
FieldDataLoadingOverheadUpperBound(int64_t max_memory_overhead,
121+
std::optional<int64_t> max_file_overhead) {
122+
auto budget_capacity = static_cast<int64_t>(
123+
milvus::storage::TransientMemoryBudget::GetLoadTransientBudget()
124+
.CapacityBytes());
125+
if (budget_capacity == 0) {
126+
return milvus::cachinglayer::LoadingOverheadTracker::kUnlimited;
127+
}
128+
129+
auto memory_ub = std::max<int64_t>(budget_capacity, max_memory_overhead);
130+
auto file_ub =
131+
max_file_overhead.has_value()
132+
? std::max<int64_t>(budget_capacity, max_file_overhead.value())
133+
: int64_t{0};
134+
return {memory_ub, file_ub};
135+
}
136+
118137
namespace {
119138

120139
size_t

internal/core/src/segcore/memory_planner.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
#include <functional>
2323
#include <future>
2424
#include <memory>
25+
#include <optional>
2526
#include <string>
2627
#include <vector>
2728

29+
#include "cachinglayer/Utils.h"
2830
#include "common/Channel.h"
2931
#include "common/FieldData.h"
3032
#include "common/GroupChunk.h"
@@ -140,6 +142,11 @@ SetFieldDataReadWindowBytes(int64_t bytes);
140142
void
141143
SetFieldDataMaxReadParallelism(int64_t parallelism);
142144

145+
milvus::cachinglayer::ResourceUsage
146+
FieldDataLoadingOverheadUpperBound(
147+
int64_t max_memory_overhead,
148+
std::optional<int64_t> max_file_overhead = std::nullopt);
149+
143150
// A cell specification: identifies a cell's location within a specific file.
144151
struct CellSpec {
145152
int64_t cid; // cell id

internal/core/src/segcore/storagev2translator/GroupChunkTranslator.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <filesystem>
2424
#include <limits>
2525
#include <memory>
26+
#include <optional>
2627
#include <string>
2728
#include <unordered_map>
2829
#include <vector>
@@ -218,16 +219,10 @@ GroupChunkTranslator::GroupChunkTranslator(
218219
if (!meta_.chunk_memory_size_.empty()) {
219220
int64_t max_cell_sz = *std::max_element(
220221
meta_.chunk_memory_size_.begin(), meta_.chunk_memory_size_.end());
221-
auto budget_capacity = static_cast<int64_t>(
222-
milvus::storage::TransientMemoryBudget::GetLoadTransientBudget()
223-
.CapacityBytes());
224222
auto max_overhead_size = loading_overhead_bytes(max_cell_sz);
225-
auto memory_ub = std::max<int64_t>(budget_capacity, max_overhead_size);
226-
auto file_ub = use_mmap_
227-
? std::max<int64_t>(budget_capacity, max_cell_sz)
228-
: int64_t{0};
229-
auto upper_bound =
230-
milvus::cachinglayer::ResourceUsage{memory_ub, file_ub};
223+
auto upper_bound = milvus::segcore::FieldDataLoadingOverheadUpperBound(
224+
max_overhead_size,
225+
use_mmap_ ? std::optional<int64_t>{max_cell_sz} : std::nullopt);
231226
// Keep MCL reservation aligned with the process-wide transient load
232227
// budget rather than multiplying it by translator type.
233228
auto group = milvus::segcore::kLoadTransientOverheadGroup;

internal/core/src/segcore/storagev2translator/ManifestGroupTranslator.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -217,16 +217,10 @@ ManifestGroupTranslator::ManifestGroupTranslator(
217217
if (!meta_.chunk_memory_size_.empty()) {
218218
int64_t max_cell_sz = *std::max_element(
219219
meta_.chunk_memory_size_.begin(), meta_.chunk_memory_size_.end());
220-
auto budget_capacity = static_cast<int64_t>(
221-
milvus::storage::TransientMemoryBudget::GetLoadTransientBudget()
222-
.CapacityBytes());
223220
auto max_overhead_size = loading_overhead_bytes(max_cell_sz);
224-
auto memory_ub = std::max<int64_t>(budget_capacity, max_overhead_size);
225-
auto file_ub = use_mmap_
226-
? std::max<int64_t>(budget_capacity, max_cell_sz)
227-
: int64_t{0};
228-
auto upper_bound =
229-
milvus::cachinglayer::ResourceUsage{memory_ub, file_ub};
221+
auto upper_bound = milvus::segcore::FieldDataLoadingOverheadUpperBound(
222+
max_overhead_size,
223+
use_mmap_ ? std::optional<int64_t>{max_cell_sz} : std::nullopt);
230224
// Keep MCL reservation aligned with the process-wide transient load
231225
// budget rather than multiplying it by translator type.
232226
auto group = milvus::segcore::kLoadTransientOverheadGroup;

0 commit comments

Comments
 (0)