@@ -245,15 +245,12 @@ 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;
249+ std::vector<Result<std::set<std::string>>> used_files_results;
250+ ScopeGuard guard ([&used_files_futures, &used_files_results]() {
251+ used_files_results = CollectAll (used_files_futures);
252+ });
248253 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));
257254 const std::optional<std::string>& changelog_manifest_list =
258255 snapshot.ChangelogManifestList ();
259256 if (changelog_manifest_list) {
@@ -266,18 +263,46 @@ Result<std::set<std::string>> OrphanFilesCleanerImpl::GetUsedFiles() const {
266263 // TODO(jinli.zjw): support IndexManifestEntry and add tests
267264 // used_files.insert(index_manifest_name.value());
268265 }
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- }
266+
267+ used_files_futures.emplace_back (
268+ Via (executor_.get (), [this , snapshot] { return GetUsedFilesBySnapshot (snapshot); }));
269+ }
270+
271+ for (const auto & used_files_result : used_files_results) {
272+ PAIMON_RETURN_NOT_OK (used_files_result);
273+ used_files.insert (used_files_result.value ().begin (), used_files_result.value ().end ());
278274 }
275+
279276 metrics_->SetCounter (CleanMetrics::CLEAN_LIST_USED_FILES_DURATION , duration.Get ());
280277 metrics_->SetCounter (CleanMetrics::CLEAN_USED_FILES , static_cast <uint64_t >(used_files.size ()));
278+ metrics_->SetCounter (CleanMetrics::CLEAN_SNAPSHOT_FILES ,
279+ static_cast <uint64_t >(snapshots.size ()));
280+ return used_files;
281+ }
282+
283+ Result<std::set<std::string>> OrphanFilesCleanerImpl::GetUsedFilesBySnapshot (
284+ const Snapshot& snapshot) const {
285+ std::set<std::string> used_files;
286+
287+ used_files.insert (SnapshotManager::SNAPSHOT_PREFIX + std::to_string (snapshot.Id ()));
288+ used_files.insert (snapshot.BaseManifestList ());
289+ used_files.insert (snapshot.DeltaManifestList ());
290+ std::vector<ManifestFileMeta> manifests;
291+ PAIMON_RETURN_NOT_OK (manifest_list_->ReadIfFileExist (snapshot.BaseManifestList (),
292+ /* filter=*/ nullptr , &manifests));
293+ PAIMON_RETURN_NOT_OK (manifest_list_->ReadIfFileExist (snapshot.DeltaManifestList (),
294+ /* filter=*/ nullptr , &manifests));
295+
296+ for (const auto & manifest : manifests) {
297+ used_files.insert (manifest.FileName ());
298+ std::vector<ManifestEntry> manifest_entries;
299+ PAIMON_RETURN_NOT_OK (manifest_file_->ReadIfFileExist (
300+ manifest.FileName (), /* filter=*/ nullptr , &manifest_entries));
301+ for (const auto & manifest_entry : manifest_entries) {
302+ used_files.insert (manifest_entry.FileName ());
303+ }
304+ }
305+
281306 return used_files;
282307}
283308
0 commit comments