Skip to content

Commit 79e2d79

Browse files
committed
add EAGER_DELETE_COMPACT when Trigger compaction when 30% deleted
Signed-off-by: eric-epsilla <[email protected]>
1 parent d552902 commit 79e2d79

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.3.21
1+
0.3.22

engine/config/config.hpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,17 @@ struct Config {
3434

3535
// Compaction configuration (inspired by Qdrant and Milvus best practices)
3636
std::atomic<bool> AutoCompaction{true}; // Enable automatic compaction (default: true)
37-
std::atomic<double> CompactionThreshold{0.2}; // Trigger compaction when 20% deleted (default: 0.2)
37+
std::atomic<double> CompactionThreshold{0.3}; // Trigger compaction when 30% deleted (default: 0.3)
3838
std::atomic<int> CompactionInterval{3600}; // Check interval in seconds (default: 1 hour)
3939
std::atomic<int> MinVectorsForCompaction{1000}; // Minimum vectors to trigger compaction (default: 1000)
4040
std::atomic<int> CompactionMaxDuration{1800}; // Maximum compaction duration in seconds (default: 30 min)
4141

4242
// Memory management configuration
4343
std::atomic<int> InitialTableCapacity{1000}; // Initial table capacity (default: 1000, was 150000)
44+
45+
// Eager compaction configuration: run Table::Compact() immediately after large soft-deletes
46+
std::atomic<bool> EagerCompactionOnDelete{true}; // Default: true
47+
// std::atomic<bool> EagerCompactionOnDelete{false}; --- IGNORE
4448

4549
// Constructor to initialize thread counts based on hardware
4650
Config() {
@@ -127,6 +131,16 @@ struct Config {
127131
}
128132
}
129133

134+
// Eager compaction toggle after delete (optional)
135+
const char* env_eager_delete_compact = std::getenv("EAGER_DELETE_COMPACT");
136+
if (env_eager_delete_compact != nullptr) {
137+
std::string env_value(env_eager_delete_compact);
138+
std::transform(env_value.begin(), env_value.end(), env_value.begin(), ::tolower);
139+
bool eager = (env_value == "true" || env_value == "1" || env_value == "yes");
140+
EagerCompactionOnDelete.store(eager, std::memory_order_release);
141+
printf("[Config] Using EAGER_DELETE_COMPACT=%s from environment\n", eager ? "true" : "false");
142+
}
143+
130144
// Check environment variable for initial table capacity
131145
const char* env_initial_capacity = std::getenv("INITIAL_TABLE_CAPACITY");
132146
if (env_initial_capacity != nullptr) {
@@ -218,6 +232,9 @@ struct Config {
218232
InitialTableCapacity.store(capacity, std::memory_order_release);
219233
}
220234
}
235+
if (json.HasMember("EagerCompactionOnDelete")) {
236+
EagerCompactionOnDelete.store(json.GetBool("EagerCompactionOnDelete"), std::memory_order_release);
237+
}
221238
}
222239

223240
// Setter method for SoftDelete mode
@@ -245,6 +262,7 @@ struct Config {
245262
config.SetInt("MinVectorsForCompaction", MinVectorsForCompaction.load(std::memory_order_acquire));
246263
config.SetInt("CompactionMaxDuration", CompactionMaxDuration.load(std::memory_order_acquire));
247264
config.SetInt("InitialTableCapacity", InitialTableCapacity.load(std::memory_order_acquire));
265+
config.SetBool("EagerCompactionOnDelete", EagerCompactionOnDelete.load(std::memory_order_acquire));
248266
return config;
249267
}
250268
};

engine/db/table.cpp

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -374,18 +374,35 @@ Status Table::Delete(
374374
", deletion_ratio=" + std::to_string(post_deletion_ratio));
375375
logger_.Debug("[Table] TableSegment deletion completed successfully: " + status.message());
376376

377-
// Trigger incremental compaction if enabled
378-
if (compactor_) {
379-
if (post_deletion_ratio > 0.2) { // 20% threshold for incremental compaction
380-
logger_.Debug("[Table] Deletion ratio " + std::to_string(post_deletion_ratio) +
381-
" exceeds 20% threshold, triggering incremental compaction");
382-
compactor_->TriggerCompaction();
383-
} else {
384-
logger_.Debug("[Table] Deletion ratio " + std::to_string(post_deletion_ratio) +
385-
" below 20% threshold, no compaction needed");
377+
// Eager compaction: immediately compact if enabled and threshold exceeded
378+
bool eager = vectordb::globalConfig.EagerCompactionOnDelete.load();
379+
double threshold = vectordb::globalConfig.CompactionThreshold.load();
380+
if (eager && post_deletion_ratio >= threshold) {
381+
logger_.Info("[Table] Eager compaction enabled and deletion ratio " + std::to_string(post_deletion_ratio) +
382+
" >= threshold " + std::to_string(threshold) + ", flushing WAL and compacting now...");
383+
// Ensure WAL is flushed so compaction is crash-safe
384+
auto wal_status = FlushWAL();
385+
if (!wal_status.ok()) {
386+
logger_.Warning("[Table] WAL flush failed before eager compaction: " + wal_status.message());
387+
}
388+
auto compact_status = Compact(threshold);
389+
if (!compact_status.ok()) {
390+
logger_.Warning("[Table] Eager compaction failed: " + compact_status.message());
386391
}
387392
} else {
388-
logger_.Debug("[Table] No compactor available, skipping compaction check");
393+
// Trigger incremental compaction if available (background), using a fixed 20% hint as before
394+
if (compactor_) {
395+
if (post_deletion_ratio > 0.2) {
396+
logger_.Debug("[Table] Deletion ratio " + std::to_string(post_deletion_ratio) +
397+
" exceeds 20% threshold, triggering incremental compaction");
398+
compactor_->TriggerCompaction();
399+
} else {
400+
logger_.Debug("[Table] Deletion ratio " + std::to_string(post_deletion_ratio) +
401+
" below 20% threshold, no compaction needed");
402+
}
403+
} else {
404+
logger_.Debug("[Table] No compactor available, skipping compaction check");
405+
}
389406
}
390407
} else {
391408
logger_.Error("[Table] TableSegment deletion failed: " + status.message());

0 commit comments

Comments
 (0)