Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tools] Ksck output sections could be specified #1

Merged
merged 1 commit into from
Sep 19, 2018
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: 15 additions & 4 deletions src/kudu/tools/ksck.cc
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,19 @@ const KsckResults& Ksck::results() const {
return results_;
}

void Ksck::set_print_sections(std::vector<std::string> sections) {
print_sections_ = 0;
for (const auto& s : sections) {
if (s == "MASTER_SUMMARIES") print_sections_ |= (int)PrintSections::MASTER_SUMMARIES;
if (s == "TSERVER_SUMMARIES") print_sections_ |= (int)PrintSections::TSERVER_SUMMARIES;
if (s == "VERSION_SUMMARIES") print_sections_ |= (int)PrintSections::VERSION_SUMMARIES;
if (s == "TABLET_SUMMARIES") print_sections_ |= (int)PrintSections::TABLET_SUMMARIES;
if (s == "TABLE_SUMMARIES") print_sections_ |= (int)PrintSections::TABLE_SUMMARIES;
if (s == "CHECKSUM_RESULTS") print_sections_ |= (int)PrintSections::CHECKSUM_RESULTS;
if (s == "TOTAL_COUNT") print_sections_ |= (int)PrintSections::TOTAL_COUNT;
}
}

Status Ksck::Run() {
PUSH_PREPEND_NOT_OK(CheckMasterHealth(), results_.error_messages,
"error fetching info from masters");
Expand Down Expand Up @@ -461,9 +474,7 @@ Status Ksck::Run() {
[](const Status& s) { return s.IsNotAuthorized(); })) {
return Status::NotAuthorized("re-run ksck with administrator privileges");
}
if (!results_.error_messages.empty()) {
return Status::RuntimeError("ksck discovered errors");
}

return Status::OK();
}

Expand Down Expand Up @@ -529,7 +540,7 @@ Status Ksck::PrintResults() {
return Status::InvalidArgument("unknown ksck format (--ksck_format)",
FLAGS_ksck_format);
}
return results_.PrintTo(mode, *out_);
return results_.PrintTo(mode, print_sections_, *out_);
}

Status Ksck::RunAndPrintResults() {
Expand Down
4 changes: 4 additions & 0 deletions src/kudu/tools/ksck.h
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,8 @@ class Ksck {
tablet_id_filters_ = std::move(tablet_ids);
}

void set_print_sections(std::vector<std::string> sections);

const KsckResults& results() const;

// Check that all masters are healthy.
Expand Down Expand Up @@ -592,6 +594,8 @@ class Ksck {

std::ostream* const out_;

int print_sections_ = (int)PrintSections::DEFAULT_PRINT_SECTIONS;

KsckResults results_;

DISALLOW_COPY_AND_ASSIGN(Ksck);
Expand Down
165 changes: 95 additions & 70 deletions src/kudu/tools/ksck_results.cc
Original file line number Diff line number Diff line change
Expand Up @@ -210,79 +210,93 @@ int ServerHealthScore(KsckServerHealth sh) {
}
}

Status KsckResults::PrintTo(PrintMode mode, ostream& out) {
Status KsckResults::PrintTo(PrintMode mode, int sections, ostream& out) {
if (mode == PrintMode::JSON_PRETTY || mode == PrintMode::JSON_COMPACT) {
return PrintJsonTo(mode, out);
return PrintJsonTo(mode, sections, out);
}

// First, report on the masters and master tablet.
std::sort(master_summaries.begin(), master_summaries.end(), ServerByHealthComparator);
RETURN_NOT_OK(PrintServerHealthSummaries(KsckServerType::MASTER,
master_summaries,
out));
if (mode == PrintMode::PLAIN_FULL || master_consensus_conflict) {
RETURN_NOT_OK(PrintConsensusMatrix(master_uuids,
boost::none,
master_consensus_state_map,
out));
}
out << endl;

RETURN_NOT_OK(PrintFlagTable(KsckServerType::MASTER,
master_summaries.size(),
master_flag_to_servers_map,
master_flag_tags_map,
out));
if (!master_flag_to_servers_map.empty()) {
if (sections & (int)PrintSections::MASTER_SUMMARIES) {
std::sort(master_summaries.begin(), master_summaries.end(), ServerByHealthComparator);
RETURN_NOT_OK(PrintServerHealthSummaries(KsckServerType::MASTER,
master_summaries,
out));
if (mode == PrintMode::PLAIN_FULL || master_consensus_conflict) {
RETURN_NOT_OK(PrintConsensusMatrix(master_uuids,
boost::none,
master_consensus_state_map,
out));
}
out << endl;

RETURN_NOT_OK(PrintFlagTable(KsckServerType::MASTER,
master_summaries.size(),
master_flag_to_servers_map,
master_flag_tags_map,
out));
if (!master_flag_to_servers_map.empty()) {
out << endl;
}
}

// Then, on the health of the tablet servers.
std::sort(tserver_summaries.begin(), tserver_summaries.end(), ServerByHealthComparator);
RETURN_NOT_OK(PrintServerHealthSummaries(KsckServerType::TABLET_SERVER,
tserver_summaries,
out));
if (!tserver_summaries.empty()) {
out << endl;
}
if (sections & (int)PrintSections::TSERVER_SUMMARIES) {
std::sort(tserver_summaries.begin(), tserver_summaries.end(), ServerByHealthComparator);
RETURN_NOT_OK(PrintServerHealthSummaries(KsckServerType::TABLET_SERVER,
tserver_summaries,
out));
if (!tserver_summaries.empty()) {
out << endl;
}

RETURN_NOT_OK(PrintFlagTable(KsckServerType::TABLET_SERVER,
tserver_summaries.size(),
tserver_flag_to_servers_map,
tserver_flag_tags_map,
out));
if (!tserver_flag_to_servers_map.empty()) {
out << endl;
RETURN_NOT_OK(PrintFlagTable(KsckServerType::TABLET_SERVER,
tserver_summaries.size(),
tserver_flag_to_servers_map,
tserver_flag_tags_map,
out));
if (!tserver_flag_to_servers_map.empty()) {
out << endl;
}
}

// Finally, in the "server section", print the version summary.
RETURN_NOT_OK(PrintVersionTable(master_summaries, tserver_summaries, out));
out << endl;
if (sections & (int)PrintSections::VERSION_SUMMARIES) {
RETURN_NOT_OK(PrintVersionTable(master_summaries, tserver_summaries, out));
out << endl;
}

// Then, on each tablet.
RETURN_NOT_OK(PrintTabletSummaries(tablet_summaries, mode, out));
if (sections & (int)PrintSections::TABLET_SUMMARIES) {
RETURN_NOT_OK(PrintTabletSummaries(tablet_summaries, mode, out));
}

// Then, summarize the tablets by table.
// Sort the tables so unhealthy tables are easy to see at the bottom.
std::sort(table_summaries.begin(), table_summaries.end(),
[](const KsckTableSummary& left, const KsckTableSummary& right) {
return std::make_pair(left.TableStatus() != KsckCheckResult::HEALTHY, left.name) <
std::make_pair(right.TableStatus() != KsckCheckResult::HEALTHY, right.name);
});
RETURN_NOT_OK(PrintTableSummaries(table_summaries, out));
if (!table_summaries.empty()) {
out << endl;
if (sections & (int)PrintSections::TABLE_SUMMARIES) {
std::sort(table_summaries.begin(), table_summaries.end(),
[](const KsckTableSummary &left, const KsckTableSummary &right) {
return std::make_pair(left.TableStatus() != KsckCheckResult::HEALTHY, left.name) <
std::make_pair(right.TableStatus() != KsckCheckResult::HEALTHY, right.name);
});
RETURN_NOT_OK(PrintTableSummaries(table_summaries, out));
if (!table_summaries.empty()) {
out << endl;
}
}

// Next, report on checksum scans.
RETURN_NOT_OK(PrintChecksumResults(checksum_results, out));
if (!checksum_results.tables.empty()) {
out << endl;
if (sections & (int)PrintSections::CHECKSUM_RESULTS) {
RETURN_NOT_OK(PrintChecksumResults(checksum_results, out));
if (!checksum_results.tables.empty()) {
out << endl;
}
}

// And, add a summary of all the things we checked.
RETURN_NOT_OK(PrintTotalCounts(*this, out));
out << endl;
if (sections & (int)PrintSections::TOTAL_COUNT) {
RETURN_NOT_OK(PrintTotalCounts(*this, out));
out << endl;
}

// Penultimately, print the warnings.
if (!warning_messages.empty()) {
Expand Down Expand Up @@ -712,47 +726,58 @@ void KsckChecksumResultsToPb(const KsckChecksumResults& results,
}
}

void KsckResults::ToPb(KsckResultsPB* pb) const {
void KsckResults::ToPb(KsckResultsPB* pb, int sections) const {
for (const auto& error : error_messages) {
pb->add_errors(error.ToString());
}

for (const auto& master_summary : master_summaries) {
KsckServerHealthSummaryToPb(master_summary, pb->add_master_summaries());
}
for (const auto& tserver_summary : tserver_summaries) {
KsckServerHealthSummaryToPb(tserver_summary, pb->add_tserver_summaries());
if (sections & (int)PrintSections::MASTER_SUMMARIES) {
for (const auto &master_summary : master_summaries) {
KsckServerHealthSummaryToPb(master_summary, pb->add_master_summaries());
}
for (const auto& master_uuid : master_uuids) {
pb->add_master_uuids(master_uuid);
}
pb->set_master_consensus_conflict(master_consensus_conflict);
for (const auto& entry : master_consensus_state_map) {
KsckConsensusStateToPb(entry.second, pb->add_master_consensus_states());
}
}

for (const auto& master_uuid : master_uuids) {
pb->add_master_uuids(master_uuid);
}
pb->set_master_consensus_conflict(master_consensus_conflict);
for (const auto& entry : master_consensus_state_map) {
KsckConsensusStateToPb(entry.second, pb->add_master_consensus_states());
if (sections & (int)PrintSections::TSERVER_SUMMARIES) {
for (const auto &tserver_summary : tserver_summaries) {
KsckServerHealthSummaryToPb(tserver_summary, pb->add_tserver_summaries());
}
}

for (const auto& tablet : tablet_summaries) {
KsckTabletSummaryToPb(tablet, pb->add_tablet_summaries());
if (sections & (int)PrintSections::TABLET_SUMMARIES) {
for (const auto &tablet : tablet_summaries) {
KsckTabletSummaryToPb(tablet, pb->add_tablet_summaries());
}
}
for (const auto& table : table_summaries) {
KsckTableSummaryToPb(table, pb->add_table_summaries());

if (sections & (int)PrintSections::TABLE_SUMMARIES) {
for (const auto &table : table_summaries) {
KsckTableSummaryToPb(table, pb->add_table_summaries());
}
}

if (!checksum_results.tables.empty()) {
KsckChecksumResultsToPb(checksum_results, pb->mutable_checksum_results());
if (sections & (int)PrintSections::CHECKSUM_RESULTS) {
if (!checksum_results.tables.empty()) {
KsckChecksumResultsToPb(checksum_results, pb->mutable_checksum_results());
}
}
}

Status KsckResults::PrintJsonTo(PrintMode mode, ostream& out) const {
Status KsckResults::PrintJsonTo(PrintMode mode, int sections, ostream& out) const {
CHECK(mode == PrintMode::JSON_PRETTY || mode == PrintMode::JSON_COMPACT);
JsonWriter::Mode jw_mode = JsonWriter::Mode::PRETTY;
if (mode == PrintMode::JSON_COMPACT) {
jw_mode = JsonWriter::Mode::COMPACT;
}

KsckResultsPB pb;
ToPb(&pb);
ToPb(&pb, sections);
out << JsonWriter::ToJson(pb, jw_mode) << endl;
return Status::OK();
}
Expand Down
22 changes: 17 additions & 5 deletions src/kudu/tools/ksck_results.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,18 @@ enum class PrintMode {
PLAIN_FULL,
};

enum class PrintSections {
MASTER_SUMMARIES = 1,
TSERVER_SUMMARIES = 1 << 1,
VERSION_SUMMARIES = 1 << 2,
TABLET_SUMMARIES = 1 << 3,
TABLE_SUMMARIES = 1 << 4,
CHECKSUM_RESULTS = 1 << 5,
TOTAL_COUNT = 1 << 6,

DEFAULT_PRINT_SECTIONS = 0x01111111
};

typedef std::map<std::string, KsckConsensusState> KsckConsensusStateMap;

// A flag and its value.
Expand Down Expand Up @@ -302,14 +314,14 @@ struct KsckResults {
// Collected results of the checksum scan.
KsckChecksumResults checksum_results;

// Print this KsckResults to 'out', according to the PrintMode 'mode'.
Status PrintTo(PrintMode mode, std::ostream& out);
// Print this KsckResults to 'out', according to the PrintMode 'mode' and sections 'sections'.
Status PrintTo(PrintMode mode, int sections, std::ostream& out);

// Print this KsckResults to 'out' in JSON format.
// Print this KsckResults to 'out' in JSON format, and printed sections are limited by 'sections'.
// 'mode' must be PrintMode::JSON_PRETTY or PrintMode::JSON_COMPACT.
Status PrintJsonTo(PrintMode mode, std::ostream& out) const;
Status PrintJsonTo(PrintMode mode, int sections, std::ostream& out) const;

void ToPb(KsckResultsPB* pb) const;
void ToPb(KsckResultsPB* pb, int sections) const;
};

// Print a formatted health summary to 'out', given a list `summaries`
Expand Down
10 changes: 10 additions & 0 deletions src/kudu/tools/tool_action_cluster.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ DEFINE_string(tablets, "",
"Tablets to check (comma-separated list of IDs) "
"If not specified, checks all tablets.");

DEFINE_string(sections, "MASTER_SUMMARIES,TSERVER_SUMMARIES,VERSION_SUMMARIES,"
"TABLET_SUMMARIES,TABLE_SUMMARIES,CHECKSUM_RESULTS,TOTAL_COUNT",
"Sections to print (comma-separated list of sections, "
"available sections are: MASTER_SUMMARIES, TSERVER_SUMMARIES, "
"VERSION_SUMMARIES, TABLET_SUMMARIES, TABLE_SUMMARIES, "
"CHECKSUM_RESULTS, TOTAL_COUNT.) "
"If not specified, print all sections.");

DEFINE_uint32(max_moves_per_server, 5,
"Maximum number of replica moves to perform concurrently on one "
"tablet server: 'move from' and 'move to' are counted "
Expand Down Expand Up @@ -137,6 +145,7 @@ Status RunKsck(const RunnerContext& context) {

ksck->set_table_filters(Split(FLAGS_tables, ",", strings::SkipEmpty()));
ksck->set_tablet_id_filters(Split(FLAGS_tablets, ",", strings::SkipEmpty()));
ksck->set_print_sections(Split(FLAGS_sections, ",", strings::SkipEmpty()));

return ksck->RunAndPrintResults();
}
Expand Down Expand Up @@ -329,6 +338,7 @@ unique_ptr<Mode> BuildClusterMode() {
.AddOptionalParameter("ksck_format")
.AddOptionalParameter("tables")
.AddOptionalParameter("tablets")
.AddOptionalParameter("sections")
.Build();
builder.AddAction(std::move(ksck));
}
Expand Down