Skip to content

Commit

Permalink
ksck: colorize and clean up output
Browse files Browse the repository at this point in the history
Dan and I were looking at some ksck output earlier and found it somewhat
hard to read. This colorizes the output and also cleans up the format to
be slightly less messy. The output now goes to stdout instead of stderr
as well, so it's easier to pipe to a file or 'less', which is a common
use case.

Example output:

Tablet 0e4e79de2c2547c091779b13735d2b73 of table 'my_table' is under-replicated: 1 replica(s) not RUNNING
  63c4785c3b6f47f5a1c23ffbf6e52b71 (ts-023.foo.com:7050): RUNNING
  cd20c68cd23c4cf986eda0936a5691cc (ts-004.foo.com:7050): RUNNING [LEADER]
  5edf82f0516b4897b3a7991a7e67d71c (ts-028.foo.com:7050): bad state
    State:       NOT_STARTED
    Data state:  TABLET_DATA_COPYING
    Last status: TabletCopy: Downloading block 4611685629730158289 (3491/9569)

Change-Id: Icc5196d63cbcbcbb2a9aba1ff17377b678c104bd
Reviewed-on: http://gerrit.cloudera.org:8080/4129
Tested-by: Kudu Jenkins
Reviewed-by: Adar Dembo <[email protected]>
  • Loading branch information
toddlipcon authored and adembo committed Aug 26, 2016
1 parent 362743c commit cf0eb4d
Show file tree
Hide file tree
Showing 7 changed files with 299 additions and 147 deletions.
2 changes: 2 additions & 0 deletions src/kudu/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ set(LINK_LIBS
)

add_library(kudu_tools_util
color.cc
data_gen_util.cc)
target_link_libraries(kudu_tools_util
${LINK_LIBS})
Expand Down Expand Up @@ -75,6 +76,7 @@ add_library(ksck
)
target_link_libraries(ksck
consensus
kudu_tools_util
master_proto
server_base_proto
tserver_proto
Expand Down
80 changes: 80 additions & 0 deletions src/kudu/tools/color.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#include "kudu/tools/color.h"

#include <gflags/gflags.h>
#include <glog/logging.h>
#include <unistd.h>

#include "kudu/gutil/strings/substitute.h"
#include "kudu/util/flag_tags.h"

DEFINE_string(color, "auto",
"Specifies whether output should be colorized. The default value "
"'auto' colorizes output if the output is a terminal. The other "
"valid values are 'always' or 'never'.");
TAG_FLAG(color, stable);

static bool ValidateColorFlag(const char* flagname, const string& value) {
if (value == "always" ||
value == "auto" ||
value == "never") {
return true;
}
LOG(ERROR) << "option 'color' expects \"always\", \"auto\", or \"never\"";
return false;
}
static bool dummy = google::RegisterFlagValidator(
&FLAGS_color, &ValidateColorFlag);


namespace kudu {
namespace tools {

namespace {
bool UseColor() {
if (FLAGS_color == "never") return false;
if (FLAGS_color == "always") return true;
return isatty(STDOUT_FILENO);
}

const char* StringForCode(AnsiCode color) {
if (!UseColor()) return "";

// Codes from: https://en.wikipedia.org/wiki/ANSI_escape_code
switch (color) {
case AnsiCode::RED: return "\x1b[31m";
case AnsiCode::GREEN: return "\x1b[32m";
case AnsiCode::YELLOW: return "\x1b[33m";
case AnsiCode::BLUE: return "\x1b[34m";
case AnsiCode::RESET: return "\x1b[m";
}
LOG(FATAL);
return "";
}
} // anonymous namespace

string Color(AnsiCode color, StringPiece s) {
return strings::Substitute("$0$1$2",
StringForCode(color),
s,
StringForCode(AnsiCode::RESET));
}

} // namespace tools
} // namespace kudu
37 changes: 37 additions & 0 deletions src/kudu/tools/color.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#pragma once

#include <string>

#include "kudu/gutil/strings/stringpiece.h"

namespace kudu {
namespace tools {

enum class AnsiCode {
RED,
YELLOW,
GREEN,
BLUE,
RESET
};

std::string Color(AnsiCode color, StringPiece s);

} // namespace tools
} // namespace kudu
70 changes: 37 additions & 33 deletions src/kudu/tools/ksck-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.

#include <gflags/gflags.h>
#include <gtest/gtest.h>
#include <memory>
#include <unordered_map>
Expand All @@ -25,6 +26,8 @@
#include "kudu/util/scoped_cleanup.h"
#include "kudu/util/test_util.h"

DECLARE_string(color);

namespace kudu {
namespace tools {

Expand Down Expand Up @@ -112,6 +115,7 @@ class KsckTest : public KuduTest {
: master_(new MockKsckMaster()),
cluster_(new KsckCluster(static_pointer_cast<KsckMaster>(master_))),
ksck_(new Ksck(cluster_)) {
FLAGS_color = "never";
unordered_map<string, shared_ptr<KsckTabletServer>> tablet_servers;
for (int i = 0; i < 3; i++) {
string name = Substitute("ts-id-$0", i);
Expand Down Expand Up @@ -289,25 +293,22 @@ TEST_F(KsckTest, TestBadTabletServer) {
"ts-id-1 (<mock>): Network error: Network failure");
ASSERT_STR_CONTAINS(
err_stream_.str(),
"WARNING: Detected problems with Tablet tablet-id-0 of table 'test'\n"
"------------------------------------------------------------\n"
"WARNING: Should have a replica on TS ts-id-1 (<mock>), but TS is unavailable\n"
"INFO: OK state on TS ts-id-0 (<mock>): RUNNING\n"
"INFO: OK state on TS ts-id-2 (<mock>): RUNNING\n");
"Tablet tablet-id-0 of table 'test' is under-replicated: 1 replica(s) not RUNNING\n"
" ts-id-0 (<mock>): RUNNING [LEADER]\n"
" ts-id-1 (<mock>): TS unavailable\n"
" ts-id-2 (<mock>): RUNNING\n");
ASSERT_STR_CONTAINS(
err_stream_.str(),
"WARNING: Detected problems with Tablet tablet-id-1 of table 'test'\n"
"------------------------------------------------------------\n"
"WARNING: Should have a replica on TS ts-id-1 (<mock>), but TS is unavailable\n"
"INFO: OK state on TS ts-id-0 (<mock>): RUNNING\n"
"INFO: OK state on TS ts-id-2 (<mock>): RUNNING\n");
"Tablet tablet-id-1 of table 'test' is under-replicated: 1 replica(s) not RUNNING\n"
" ts-id-0 (<mock>): RUNNING [LEADER]\n"
" ts-id-1 (<mock>): TS unavailable\n"
" ts-id-2 (<mock>): RUNNING\n");
ASSERT_STR_CONTAINS(
err_stream_.str(),
"WARNING: Detected problems with Tablet tablet-id-2 of table 'test'\n"
"------------------------------------------------------------\n"
"WARNING: Should have a replica on TS ts-id-1 (<mock>), but TS is unavailable\n"
"INFO: OK state on TS ts-id-0 (<mock>): RUNNING\n"
"INFO: OK state on TS ts-id-2 (<mock>): RUNNING\n");
"Tablet tablet-id-2 of table 'test' is under-replicated: 1 replica(s) not RUNNING\n"
" ts-id-0 (<mock>): RUNNING [LEADER]\n"
" ts-id-1 (<mock>): TS unavailable\n"
" ts-id-2 (<mock>): RUNNING\n");
}

TEST_F(KsckTest, TestZeroTabletReplicasCheck) {
Expand Down Expand Up @@ -340,7 +341,7 @@ TEST_F(KsckTest, TestOneSmallReplicatedTable) {
Status s = ksck_->ChecksumData(ChecksumOptions());
EXPECT_EQ("Not found: No table found. Filter: table_filters=xyz", s.ToString());
ASSERT_STR_CONTAINS(err_stream_.str(),
"INFO: The cluster doesn't have any matching tables");
"The cluster doesn't have any matching tables");

// Test filtering with a matching table pattern.
err_stream_.str("");
Expand All @@ -365,7 +366,8 @@ TEST_F(KsckTest, TestOneOneTabletBrokenTable) {
Status s = RunKsck();
EXPECT_EQ("Corruption: 1 table(s) are bad", s.ToString());
ASSERT_STR_CONTAINS(err_stream_.str(),
"Tablet tablet-id-1 of table 'test' has 2 instead of 3 replicas");
"Tablet tablet-id-1 of table 'test' is under-replicated: "
"configuration has 2 replicas vs desired 3");
}

TEST_F(KsckTest, TestMismatchedAssignments) {
Expand All @@ -377,9 +379,11 @@ TEST_F(KsckTest, TestMismatchedAssignments) {
Status s = RunKsck();
EXPECT_EQ("Corruption: 1 table(s) are bad", s.ToString());
ASSERT_STR_CONTAINS(err_stream_.str(),
"WARNING: Detected problems with Tablet tablet-id-2 of table 'test'\n"
"------------------------------------------------------------\n"
"WARNING: Missing a tablet replica on tablet server ts-id-0 (<mock>)\n");
"Tablet tablet-id-2 of table 'test' is under-replicated: "
"1 replica(s) not RUNNING\n"
" ts-id-0 (<mock>): missing [LEADER]\n"
" ts-id-1 (<mock>): RUNNING\n"
" ts-id-2 (<mock>): RUNNING\n");
}

TEST_F(KsckTest, TestTabletNotRunning) {
Expand All @@ -389,19 +393,19 @@ TEST_F(KsckTest, TestTabletNotRunning) {
EXPECT_EQ("Corruption: 1 table(s) are bad", s.ToString());
ASSERT_STR_CONTAINS(
err_stream_.str(),
"WARNING: Detected problems with Tablet tablet-id-0 of table 'test'\n"
"------------------------------------------------------------\n"
"WARNING: Bad state on TS ts-id-0 (<mock>): FAILED\n"
" Last status: \n"
" Data state: TABLET_DATA_UNKNOWN\n"
"WARNING: Bad state on TS ts-id-1 (<mock>): FAILED\n"
" Last status: \n"
" Data state: TABLET_DATA_UNKNOWN\n"
"WARNING: Bad state on TS ts-id-2 (<mock>): FAILED\n"
" Last status: \n"
" Data state: TABLET_DATA_UNKNOWN\n"
"ERROR: Tablet tablet-id-0 of table 'test' does not have a majority of "
"replicas in RUNNING state\n");
"Tablet tablet-id-0 of table 'test' is unavailable: 3 replica(s) not RUNNING\n"
" ts-id-0 (<mock>): bad state [LEADER]\n"
" State: FAILED\n"
" Data state: TABLET_DATA_UNKNOWN\n"
" Last status: \n"
" ts-id-1 (<mock>): bad state\n"
" State: FAILED\n"
" Data state: TABLET_DATA_UNKNOWN\n"
" Last status: \n"
" ts-id-2 (<mock>): bad state\n"
" State: FAILED\n"
" Data state: TABLET_DATA_UNKNOWN\n"
" Last status: \n");
}

} // namespace tools
Expand Down
Loading

0 comments on commit cf0eb4d

Please sign in to comment.