Skip to content

Commit 917e6a6

Browse files
optimize orphan files cleaner
1 parent 37ba043 commit 917e6a6

3 files changed

Lines changed: 49 additions & 29 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: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -245,39 +245,57 @@ 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+
std::vector<std::future<Result<std::set<std::string>>>> used_files_futures;
248249
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());
276-
}
277-
}
250+
used_files_futures.emplace_back(
251+
Via(executor_.get(), [this, snapshot] { return GetUsedFilesBySnapshot(snapshot); }));
278252
}
253+
254+
for (const auto& used_files_future : CollectAll(used_files_futures)) {
255+
PAIMON_RETURN_NOT_OK(used_files_future);
256+
used_files.insert(used_files_future.value().begin(), used_files_future.value().end());
257+
}
258+
279259
metrics_->SetCounter(CleanMetrics::CLEAN_LIST_USED_FILES_DURATION, duration.Get());
280260
metrics_->SetCounter(CleanMetrics::CLEAN_USED_FILES, static_cast<uint64_t>(used_files.size()));
261+
metrics_->SetCounter(CleanMetrics::CLEAN_SNAPSHOT_FILES,
262+
static_cast<uint64_t>(snapshots.size()));
263+
return used_files;
264+
}
265+
266+
Result<std::set<std::string>> OrphanFilesCleanerImpl::GetUsedFilesBySnapshot(
267+
const Snapshot& snapshot) const {
268+
std::set<std::string> used_files;
269+
270+
used_files.insert(SnapshotManager::SNAPSHOT_PREFIX + std::to_string(snapshot.Id()));
271+
used_files.insert(snapshot.BaseManifestList());
272+
used_files.insert(snapshot.DeltaManifestList());
273+
std::vector<ManifestFileMeta> manifests;
274+
PAIMON_RETURN_NOT_OK(manifest_list_->ReadIfFileExist(snapshot.BaseManifestList(),
275+
/*filter=*/nullptr, &manifests));
276+
PAIMON_RETURN_NOT_OK(manifest_list_->ReadIfFileExist(snapshot.DeltaManifestList(),
277+
/*filter=*/nullptr, &manifests));
278+
const std::optional<std::string>& changelog_manifest_list = snapshot.ChangelogManifestList();
279+
if (changelog_manifest_list) {
280+
used_files.insert(changelog_manifest_list.value());
281+
return Status::NotImplemented("OrphanFilesCleaner do not support clean changelog");
282+
}
283+
const std::optional<std::string>& index_manifest_name = snapshot.IndexManifest();
284+
if (index_manifest_name) {
285+
return Status::NotImplemented("OrphanFilesCleaner do not support clean index manifest");
286+
// TODO(jinli.zjw): support IndexManifestEntry and add tests
287+
// used_files.insert(index_manifest_name.value());
288+
}
289+
for (const auto& manifest : manifests) {
290+
used_files.insert(manifest.FileName());
291+
std::vector<ManifestEntry> manifest_entries;
292+
PAIMON_RETURN_NOT_OK(manifest_file_->ReadIfFileExist(
293+
manifest.FileName(), /*filter=*/nullptr, &manifest_entries));
294+
for (const auto& manifest_entry : manifest_entries) {
295+
used_files.insert(manifest_entry.FileName());
296+
}
297+
}
298+
281299
return used_files;
282300
}
283301

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)