Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions include/libjungle/jungle.h
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,25 @@ class DB {
Status mergeLevel(const CompactOptions& options,
size_t level);

/**
* Drop all data in the table section, by replacing all tables
* with new empty ones.
*
* Records in the log section (i.e., not flushed yet) are NOT affected,
* and they will be flushed to the new tables later.
*
* Ongoing iterators and snapshots are not blocked, and they keep
* reading the old tables until they are closed. Ongoing compactions
* will be cancelled.
*
* If it fails to store the manifest file, the purge is still effective
* in memory, but may be rolled back if the process crashes before
* the manifest file is stored successfully.
*
* @return OK on success.
*/
Status purgeTables();

/**
* Get the current statistics of the Jungle instance.
*
Expand Down
7 changes: 7 additions & 0 deletions include/libjungle/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ struct DebugParams {
, syncCb(nullptr)
, afterMemTableFlushCb(nullptr)
, adjustL0Cb(nullptr)
, inPlaceCompactionBeforeLockCb(nullptr)
, forceMerge(false)
{}

Expand Down Expand Up @@ -333,6 +334,12 @@ struct DebugParams {
*/
std::function< void(const GenericCbParams&) > adjustL0Cb;

/**
* Callback function that will be invoked during in-place compaction,
* after picking a victim table and before locking it.
*/
std::function< void(const GenericCbParams&) > inPlaceCompactionBeforeLockCb;

/**
* If true, merge will proceed the task even with the small number
* of tables in the level.
Expand Down
7 changes: 7 additions & 0 deletions src/jungle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,13 @@ Status DB::mergeLevel(const CompactOptions& options,
return s;
}

Status DB::purgeTables() {
Status s;
EP( p->checkHandleValidity(DBInternal::OPTYPE_WRITE) );
if (sn) return Status::INVALID_HANDLE_USAGE;
return p->tableMgr->purgeTables();
}


// NOTE:
// MemTable --> LogFile --> LogMgr --(memcpy)--+--> DB ---> User
Expand Down
127 changes: 113 additions & 14 deletions src/table_compaction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ Status TableMgr::compactLevelItr(const CompactOptions& options,

{ // WARNING: See the comment at `mani->removeTableFile` in `compactLevelItr`.
std::lock_guard<std::mutex> l(mani->getLock());
// WARNING:
// If the victim has been purged by `purgeTables()`, the tables
// we get here are newly created ones. Writing records of the
// victim to them will resurrect the purged data.
if (local_victim->isRemoved()) {
throw Status(Status::COMPACTION_CANCELLED);
}
mani->getTablesRange(level+1, min_key_victim, max_key_victim, tables);
}
_log_info( myLog, "victim table's min key %s max key %s, "
Expand All @@ -104,6 +111,14 @@ Status TableMgr::compactLevelItr(const CompactOptions& options,
TableLockHolder tl_dst_holder( this, table_list_to_number(tables) );
if (!tl_dst_holder.ownsLock()) throw Status(Status::OPERATION_IN_PROGRESS);

// WARNING:
// Destination tables may have been migrated to the next level
// (and then purged) before we lock them. In such a case, the
// back pointer of their files does not point to them.
if (isAnyTableRemoved(tables) || isAnyTableRemoved({local_victim})) {
throw Status(Status::COMPACTION_CANCELLED);
}

TableStats victim_stats;
local_victim->file->getStats(victim_stats);

Expand Down Expand Up @@ -492,6 +507,16 @@ Status TableMgr::compactLevelItr(const CompactOptions& options,

{ // Grab lock, add first, and then remove next.
std::lock_guard<std::mutex> l(mani->getLock());
if (local_victim->isRemoved()) {
// Purged by `purgeTables()` in the middle, discard the results.
// NOTE: keys will be freed below.
for (LsmFlushResult& rr: results) {
rr.tFile->destroySelf();
delete rr.tFile;
}
throw Status(Status::COMPACTION_CANCELLED);
}

// WARNING:
// We should add in descending order of min key.
// Otherwise, if there is a point query in the middle,
Expand Down Expand Up @@ -617,6 +642,11 @@ Status TableMgr::migrateLevel(const CompactOptions& options,
{ // Grab lock, add first, and then remove next.
std::lock_guard<std::mutex> l(mani->getLock());

// Purged by `purgeTables()` in the middle, should not migrate.
for (TableInfo* ti: tables) {
if (ti->isRemoved()) throw Status(Status::COMPACTION_CANCELLED);
}

// WARNING:
// We should add in descending order of min key.
// Otherwise, if there is a point query in the middle,
Expand All @@ -625,8 +655,14 @@ Status TableMgr::migrateLevel(const CompactOptions& options,
auto eer = tables.rbegin();
while (eer != tables.rend()) {
TableInfo* ti = *eer;
ti->setMigration();
TC( mani->addTableFile( level + 1, 0, ti->minKey, ti->file ) );
// The file will be shared by the current table and
// the new table in the next level.
ti->file->grabOwnership();
s = mani->addTableFile( level + 1, 0, ti->minKey, ti->file );
if (!s) {
ti->file->releaseOwnership();
throw s;
}
eer++;
}

Expand Down Expand Up @@ -694,10 +730,28 @@ Status TableMgr::compactInPlace(const CompactOptions& options,
try {
if (level == 1) numL1Compactions.fetch_add(1);

DBMgr* db_mgr = DBMgr::getWithoutInit();
if ( db_mgr &&
db_mgr->isDebugCallbackEffective() ) {
DebugParams d_params = db_mgr->getDebugParams();
if (d_params.inPlaceCompactionBeforeLockCb) {
d_params.inPlaceCompactionBeforeLockCb(DebugParams::GenericCbParams());
}
}

// Lock victim table.
TableLockHolder tl_holder(this, {local_victim->number});
if (!tl_holder.ownsLock()) throw Status(Status::OPERATION_IN_PROGRESS);

// WARNING:
// The victim may have been migrated to the next level (and then
// purged) before we lock it. In such a case, the back pointer of the
// victim's file does not point to the victim, and compacting it
// will be meaningless anyway.
if (isAnyTableRemoved({local_victim})) {
throw Status(Status::COMPACTION_CANCELLED);
}

uint64_t dst_table_num = 0;
std::string dst_filename;
EP( mani->issueTableNumber(dst_table_num) );
Expand Down Expand Up @@ -730,21 +784,36 @@ Status TableMgr::compactInPlace(const CompactOptions& options,
delete newly_compacted_file;
throw s;
}
mani->addTableFile(level, 0, local_victim->minKey, newly_compacted_file);

// Add special checkpoint 0, for base snapshot.
//
// WARNING:
// Should be done before adding the new file to manifest. Once it is
// added, `purgeTables()` can remove and free it at any time,
// as we don't hold a reference to it.
uint64_t new_file_last_seqnum = 0;
newly_compacted_file->getLatestSnapMarker(new_file_last_seqnum);
newly_compacted_file->addCheckpoint(0, new_file_last_seqnum);

// Close the destination handle.
local_victim->file->releaseDstHandle(dst_handle);

{ // Remove source table file.
{ // Grab lock, add first, and then remove next.
//
// NOTE:
// Both should be done in the same critical section, so that
// others holding the lock (e.g., `purgeTables()`) never see
// the source and the new file having the same min key together.
std::lock_guard<std::mutex> l(mani->getLock());
if (local_victim->isRemoved()) {
// Purged by `purgeTables()` in the middle, discard the result.
newly_compacted_file->destroySelf();
delete newly_compacted_file;
throw Status(Status::COMPACTION_CANCELLED);
Comment thread
Copilot marked this conversation as resolved.
Resolved
}
mani->addTableFile(level, 0, local_victim->minKey, newly_compacted_file);
mani->removeTableFile(level, local_victim);
}
Comment thread
Copilot marked this conversation as resolved.
Resolved

// Close the destination handle.
local_victim->file->releaseDstHandle(dst_handle);

// WARNING:
// Release it ONLY WHEN this table is not given by caller.
// If not, caller is responsible to release the table.
Expand Down Expand Up @@ -861,8 +930,19 @@ Status TableMgr::compactL0(const CompactOptions& options,
}
}
createNewTableFile(0, new_normal_file, t_opt);
EP( mani->addTableFile(0, hash_num, SizedBuf(), new_normal_file) );
new_normal_table_num = new_normal_file->getNumber();
{ std::lock_guard<std::mutex> l(mani->getLock());
if (origin_table->isRemoved()) {
// Purged by `purgeTables()` after we got the tables.
new_normal_file->destroySelf();
delete new_normal_file;
throw Status(Status::COMPACTION_CANCELLED);
}
TC( mani->addTableFile(0, hash_num, SizedBuf(), new_normal_file) );
// WARNING:
// Should be done while holding the lock. Once the lock
// is released, `purgeTables()` can free the new file.
new_normal_table_num = new_normal_file->getNumber();
}
}

} else {
Expand Down Expand Up @@ -974,16 +1054,35 @@ Status TableMgr::compactL0(const CompactOptions& options,
TableFile* newly_compacted_file = new TableFile(this);
newly_compacted_file->setLogger(myLog);
newly_compacted_file->load(0, dst_table_num, dst_filename, opt.fOps, t_opt);
mani->addTableFile(0, hash_num, SizedBuf(), newly_compacted_file);

// Add special checkpoint 0, for base snapshot.
//
// WARNING:
// Should be done before adding the new file to manifest.
// See the comment in `compactInPlace()`.
uint64_t new_file_last_seqnum = 0;
newly_compacted_file->getLatestSnapMarker(new_file_last_seqnum);
newly_compacted_file->addCheckpoint(0, new_file_last_seqnum);

// Remove old file from manifest, and delete it.
mani->removeTableFile(0, origin_table);
if (merge_table) mani->removeTableFile(0, merge_table);
{ // Grab lock, add first, and then remove next.
//
// NOTE:
// Removal should also be protected by the lock, as
// `purgeTables()` may remove the same tables concurrently.
std::lock_guard<std::mutex> l(mani->getLock());
if ( origin_table->isRemoved() ||
(merge_table && merge_table->isRemoved()) ) {
Comment on lines +1073 to +1074
// Purged by `purgeTables()` in the middle, discard the result.
newly_compacted_file->destroySelf();
delete newly_compacted_file;
throw Status(Status::COMPACTION_CANCELLED);
}
mani->addTableFile(0, hash_num, SizedBuf(), newly_compacted_file);

// Remove old file from manifest, and delete it.
mani->removeTableFile(0, origin_table);
if (merge_table) mani->removeTableFile(0, merge_table);
}

for (TableInfo*& table: tables) table->done();

Expand Down
34 changes: 22 additions & 12 deletions src/table_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ TableFile::TableFile(const TableMgr* table_mgr)
, fOps(nullptr)
, tableMgr(table_mgr)
, tableInfo(nullptr)
, numOwners(1)
, keepOnDisk(false)
, writer(nullptr)
, bfByKey(nullptr)
, tlbByKey(nullptr)
Expand Down Expand Up @@ -617,9 +619,10 @@ Status TableFile::closeSnapshot(DB* snap_handle) {
}

void TableFile::addCheckpoint(uint64_t chk, uint64_t commit_seqnum) {
if (tableInfo) {
TableInfo* cur_info = tableInfo.load();
if (cur_info) {
_log_info(myLog, "file lv %zu num %zu hash %zu checkpoint %zu %zu",
tableInfo->level, tableInfo->number, tableInfo->hashNum,
cur_info->level, cur_info->number, cur_info->hashNum,
chk, commit_seqnum);
}

Expand Down Expand Up @@ -896,9 +899,10 @@ Status TableFile::setSingle(uint32_t key_hash_val,
doc.flags = FDB_CUSTOM_SEQNUM;

bool deletion_executed = false;
TableInfo* cur_info = tableInfo.load();
if ( ( db_config->purgeDeletedDocImmediately &&
tableInfo &&
tableInfo->level &&
cur_info &&
cur_info->level &&
is_last_level ) ||
force_delete ) {
// Immediate purging option,
Expand Down Expand Up @@ -1013,11 +1017,12 @@ Status TableFile::setBatch(std::list<Record*>& batch,
}

SimpleLogger::Levels ll = SimpleLogger::INFO;
if (tableInfo) {
if (tableInfo->level) {
TableInfo* cur_info = tableInfo.load();
if (cur_info) {
if (cur_info->level) {
_log_( ll, myLog,
"L%zu: file %zu_%zu, set %zu del %zu, %zu us, %zu us, %zu us",
tableInfo->level,
cur_info->level,
tableMgr->getTableMgrOptions()->prefixNum,
myNumber,
set_count, del_count, tt.getUs(), bf_elapsed,
Expand All @@ -1026,8 +1031,8 @@ Status TableFile::setBatch(std::list<Record*>& batch,
_log_( ll, myLog,
"L%zu: hash %zu, file %zu_%zu, set %zu del %zu, %zu us, %zu us, "
"%zu us",
tableInfo->level,
tableInfo->hashNum,
cur_info->level,
cur_info->hashNum,
tableMgr->getTableMgrOptions()->prefixNum,
myNumber,
set_count, del_count, tt.getUs(), bf_elapsed,
Expand Down Expand Up @@ -1055,12 +1060,12 @@ Status TableFile::setBatch(std::list<Record*>& batch,
while (entry != chkMap.end()) {
if ( entry->first < oldest_seq ||
entry->second < oldest_seq ) {
if (tableInfo) {
if (cur_info) {
_log_debug( myLog,
"file lv %zu num %zu hash %zu removed "
"checkpoint %zu %zu",
tableInfo->level, tableInfo->number,
tableInfo->hashNum,
cur_info->level, cur_info->number,
cur_info->hashNum,
entry->first, entry->second );
}
entry = chkMap.erase(entry);
Expand Down Expand Up @@ -1632,6 +1637,11 @@ Status TableFile::getCheckpointSeqnum(uint64_t chk, uint64_t& seqnum_out) {
}

Status TableFile::destroySelf() {
if (keepOnDisk) {
_log_warn(myLog, "keep table file %s on disk", filename.c_str());
return Status();
}

if (fOps->exist(filename.c_str())) {
// Instead removing it immediately,
// put it into remove list.
Expand Down
Loading
Loading