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
22 changes: 17 additions & 5 deletions proxy/logging/RolledLogDeleter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ RolledLogDeleter::register_log_type_for_deletion(std::string_view log_type, int

deletingInfoList.push_back(std::move(deletingInfo));
deleting_info.insert(deletingInfoPtr);
candidates_require_sorting = true;
}

bool
Expand All @@ -86,20 +87,31 @@ RolledLogDeleter::consider_for_candidacy(std::string_view log_path, int64_t file
auto &candidates = iter->candidates;
candidates.push_back(std::make_unique<LogDeleteCandidate>(log_path, file_size, modification_time));
++num_candidates;

std::sort(
candidates.begin(), candidates.end(),
[](std::unique_ptr<LogDeleteCandidate> const &a, std::unique_ptr<LogDeleteCandidate> const &b) { return a->mtime > b->mtime; });

candidates_require_sorting = true;
return true;
}

void
RolledLogDeleter::sort_candidates()
{
deleting_info.apply([](LogDeletingInfo &info) {
std::sort(info.candidates.begin(), info.candidates.end(),
[](std::unique_ptr<LogDeleteCandidate> const &a, std::unique_ptr<LogDeleteCandidate> const &b) {
return a->mtime > b->mtime;
});
});
candidates_require_sorting = false;
}

std::unique_ptr<LogDeleteCandidate>
RolledLogDeleter::take_next_candidate_to_delete()
{
if (!has_candidates()) {
return nullptr;
}
if (candidates_require_sorting) {
sort_candidates();
}
// Select the highest priority type (diags.log, traffic.out, etc.) from which
// to select a candidate.
auto target_type =
Expand Down
17 changes: 15 additions & 2 deletions proxy/logging/RolledLogDeleter.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#pragma once

#include <cstdint>
#include <list>
#include <deque>
#include <memory>
#include <string>
#include <string_view>
Expand Down Expand Up @@ -201,14 +201,27 @@ class RolledLogDeleter
*/
void clear_candidates();

private:
/** Sort all the assembled candidates for each LogDeletingInfo.
*
* After any additions to the @a deleting_info, this should be called before
* calling @a take_next_candidate_to_delete because the latter depends upon
* the candidate entries being sorted.
*/
void sort_candidates();

private:
/** The owning references to the set of LogDeletingInfo added to the below
* hash map. */
std::list<std::unique_ptr<LogDeletingInfo>> deletingInfoList;
std::deque<std::unique_ptr<LogDeletingInfo>> deletingInfoList;

/** The set of candidates for deletion keyed by log_type. */
IntrusiveHashMap<LogDeletingInfoDescriptor> deleting_info;

/** The number of tracked candidates. */
size_t num_candidates = 0;

/** Whether the candidates require sorting due to an addition to the
* deleting_info. */
bool candidates_require_sorting = true;
};