Skip to content

Commit 66f3ddd

Browse files
optimize orphan files cleaner (#135)
1 parent 15ce14b commit 66f3ddd

3 files changed

Lines changed: 58 additions & 28 deletions

File tree

src/paimon/core/operation/metrics/clean_metrics.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class CleanMetrics {
2727
static constexpr char CLEAN_LIST_FILE_STATUS_DURATION[] = "listFileStatusDuration";
2828
static constexpr char CLEAN_LIST_FILE_STATUS_TASKS[] = "listFileStatusTasks";
2929
static constexpr char CLEAN_LIST_USED_FILES_DURATION[] = "listUsedFilesDuration";
30+
static constexpr char CLEAN_SNAPSHOT_FILES[] = "snapshotFiles";
3031
static constexpr char CLEAN_USED_FILES[] = "usedFiles";
3132
static constexpr char CLEAN_SCAN_ORPHAN_FILES_DURATION[] = "scanOrphanFilesDuration";
3233
static constexpr char CLEAN_ORPHAN_FILES[] = "orphanFiles";

src/paimon/core/operation/orphan_files_cleaner_impl.cpp

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -245,39 +245,67 @@ Result<std::set<std::string>> OrphanFilesCleanerImpl::GetUsedFiles() const {
245245
used_files.insert(SnapshotManager::LATEST);
246246
Duration duration;
247247
PAIMON_ASSIGN_OR_RAISE(std::vector<Snapshot> snapshots, snapshot_manager_->GetAllSnapshots());
248-
for (const auto& snapshot : snapshots) {
249-
used_files.insert(SnapshotManager::SNAPSHOT_PREFIX + std::to_string(snapshot.Id()));
250-
used_files.insert(snapshot.BaseManifestList());
251-
used_files.insert(snapshot.DeltaManifestList());
252-
std::vector<ManifestFileMeta> manifests;
253-
PAIMON_RETURN_NOT_OK(manifest_list_->ReadIfFileExist(snapshot.BaseManifestList(),
254-
/*filter=*/nullptr, &manifests));
255-
PAIMON_RETURN_NOT_OK(manifest_list_->ReadIfFileExist(snapshot.DeltaManifestList(),
256-
/*filter=*/nullptr, &manifests));
257-
const std::optional<std::string>& changelog_manifest_list =
258-
snapshot.ChangelogManifestList();
259-
if (changelog_manifest_list) {
260-
used_files.insert(changelog_manifest_list.value());
261-
return Status::NotImplemented("OrphanFilesCleaner do not support clean changelog");
262-
}
263-
const std::optional<std::string>& index_manifest_name = snapshot.IndexManifest();
264-
if (index_manifest_name) {
265-
return Status::NotImplemented("OrphanFilesCleaner do not support clean index manifest");
266-
// TODO(jinli.zjw): support IndexManifestEntry and add tests
267-
// used_files.insert(index_manifest_name.value());
268-
}
269-
for (const auto& manifest : manifests) {
270-
used_files.insert(manifest.FileName());
271-
std::vector<ManifestEntry> manifest_entries;
272-
PAIMON_RETURN_NOT_OK(manifest_file_->ReadIfFileExist(
273-
manifest.FileName(), /*filter=*/nullptr, &manifest_entries));
274-
for (const auto& manifest_entry : manifest_entries) {
275-
used_files.insert(manifest_entry.FileName());
248+
std::vector<std::future<Result<std::set<std::string>>>> used_files_futures;
249+
std::vector<Result<std::set<std::string>>> used_files_results;
250+
{
251+
ScopeGuard guard([&used_files_futures, &used_files_results]() {
252+
used_files_results = CollectAll(used_files_futures);
253+
});
254+
for (const auto& snapshot : snapshots) {
255+
const std::optional<std::string>& changelog_manifest_list =
256+
snapshot.ChangelogManifestList();
257+
if (changelog_manifest_list) {
258+
used_files.insert(changelog_manifest_list.value());
259+
return Status::NotImplemented("OrphanFilesCleaner do not support clean changelog");
276260
}
261+
const std::optional<std::string>& index_manifest_name = snapshot.IndexManifest();
262+
if (index_manifest_name) {
263+
return Status::NotImplemented(
264+
"OrphanFilesCleaner do not support clean index manifest");
265+
// TODO(jinli.zjw): support IndexManifestEntry and add tests
266+
// used_files.insert(index_manifest_name.value());
267+
}
268+
269+
used_files_futures.emplace_back(Via(
270+
executor_.get(), [this, snapshot] { return GetUsedFilesBySnapshot(snapshot); }));
277271
}
278272
}
273+
274+
for (const auto& used_files_result : used_files_results) {
275+
PAIMON_RETURN_NOT_OK(used_files_result);
276+
used_files.insert(used_files_result.value().begin(), used_files_result.value().end());
277+
}
278+
279279
metrics_->SetCounter(CleanMetrics::CLEAN_LIST_USED_FILES_DURATION, duration.Get());
280280
metrics_->SetCounter(CleanMetrics::CLEAN_USED_FILES, static_cast<uint64_t>(used_files.size()));
281+
metrics_->SetCounter(CleanMetrics::CLEAN_SNAPSHOT_FILES,
282+
static_cast<uint64_t>(snapshots.size()));
283+
return used_files;
284+
}
285+
286+
Result<std::set<std::string>> OrphanFilesCleanerImpl::GetUsedFilesBySnapshot(
287+
const Snapshot& snapshot) const {
288+
std::set<std::string> used_files;
289+
290+
used_files.insert(SnapshotManager::SNAPSHOT_PREFIX + std::to_string(snapshot.Id()));
291+
used_files.insert(snapshot.BaseManifestList());
292+
used_files.insert(snapshot.DeltaManifestList());
293+
std::vector<ManifestFileMeta> manifests;
294+
PAIMON_RETURN_NOT_OK(manifest_list_->ReadIfFileExist(snapshot.BaseManifestList(),
295+
/*filter=*/nullptr, &manifests));
296+
PAIMON_RETURN_NOT_OK(manifest_list_->ReadIfFileExist(snapshot.DeltaManifestList(),
297+
/*filter=*/nullptr, &manifests));
298+
299+
for (const auto& manifest : manifests) {
300+
used_files.insert(manifest.FileName());
301+
std::vector<ManifestEntry> manifest_entries;
302+
PAIMON_RETURN_NOT_OK(manifest_file_->ReadIfFileExist(
303+
manifest.FileName(), /*filter=*/nullptr, &manifest_entries));
304+
for (const auto& manifest_entry : manifest_entries) {
305+
used_files.insert(manifest_entry.FileName());
306+
}
307+
}
308+
281309
return used_files;
282310
}
283311

src/paimon/core/operation/orphan_files_cleaner_impl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class OrphanFilesCleanerImpl : public OrphanFilesCleaner {
7474
const std::string& path) const;
7575
std::set<std::string> ListFileDirs(const std::string& path, int32_t max_level) const;
7676
Result<std::set<std::string>> GetUsedFiles() const;
77+
Result<std::set<std::string>> GetUsedFilesBySnapshot(const Snapshot& snapshot) const;
7778
static bool SupportToClean(const std::string& file_name);
7879

7980
private:

0 commit comments

Comments
 (0)