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

[Tool] Show the information of a tablet #48

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/kudu/client/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,9 @@ Status KuduClient::GetTablet(const string& tablet_id, KuduTablet** tablet) {
}

unique_ptr<KuduTablet> client_tablet(new KuduTablet);
client_tablet->data_ = new KuduTablet::Data(tablet_id, std::move(replicas));
client_tablet->data_ = new KuduTablet::Data(tablet_id, std::move(replicas),
t.table_id(),
t.table_name());
replicas.clear();

*tablet = client_tablet.release();
Expand Down Expand Up @@ -2353,6 +2355,14 @@ const string& KuduTablet::id() const {
return data_->id_;
}

const string& KuduTablet::table_id() const {
return data_->table_id_;
}

const string& KuduTablet::table_name() const {
return data_->table_name_;
}

const vector<const KuduReplica*>& KuduTablet::replicas() const {
return data_->replicas_;
}
Expand Down
3 changes: 3 additions & 0 deletions src/kudu/client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,9 @@ class KUDU_EXPORT KuduTablet {
/// for the lifetime of the tablet.
const std::string& id() const;

const std::string& table_id() const;
const std::string& table_name() const;

/// @return The replicas of this tablet. The KuduTablet retains ownership
/// over the replicas.
///
Expand Down
8 changes: 8 additions & 0 deletions src/kudu/client/tablet-internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ KuduTablet::Data::Data(string id, vector<const KuduReplica*> replicas)
replicas_(std::move(replicas)) {
}

KuduTablet::Data::Data(string id, vector<const KuduReplica*> replicas,
string table_id, string table_name)
: id_(std::move(id)),
replicas_(std::move(replicas)),
table_id_(std::move(table_id)),
table_name_(std::move(table_name)) {
}

KuduTablet::Data::~Data() {
STLDeleteElements(&replicas_);
}
Expand Down
4 changes: 4 additions & 0 deletions src/kudu/client/tablet-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ namespace client {
class KuduTablet::Data {
public:
Data(std::string id, std::vector<const KuduReplica*> replicas);
Data(std::string id, std::vector<const KuduReplica*> replicas,
std::string table_id, std::string table_name);
~Data();

const std::string id_;
const std::string table_id_;
const std::string table_name_;
std::vector<const KuduReplica*> replicas_;

DISALLOW_COPY_AND_ASSIGN(Data);
Expand Down
2 changes: 2 additions & 0 deletions src/kudu/master/catalog_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6224,6 +6224,8 @@ Status CatalogManager::BuildLocationsForTablet(
// No longer used; always set to false.
locs_pb->set_deprecated_stale(false);

locs_pb->set_table_id(tablet->table()->id());
locs_pb->set_table_name(tablet->table()->table_name());
return Status::OK();
}

Expand Down
3 changes: 3 additions & 0 deletions src/kudu/master/master.proto
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,9 @@ message TabletLocationsPB {

optional PartitionPB partition = 6;

optional bytes table_id = 8;
optional string table_name = 9;

// DEPRECATED.
// Used only if interned replicas are not supported by client.
repeated DEPRECATED_ReplicaPB DEPRECATED_replicas = 4;
Expand Down
47 changes: 47 additions & 0 deletions src/kudu/tools/kudu-tool-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1525,11 +1525,13 @@ TEST_F(ToolTest, TestModeHelp) {
"change_config.*Change.*Raft configuration",
"leader_step_down.*Change.*tablet's leader",
"unsafe_replace_tablet.*Replace a tablet with an empty one",
"show_info.*Show the table and location information of a tablet"
};
NO_FATALS(RunTestHelp(kCmd, kTabletModeRegexes));
NO_FATALS(RunTestHelpRpcFlags(kCmd,
{ "leader_step_down",
"unsafe_replace_tablet",
"show_info"
}));
}
{
Expand Down Expand Up @@ -3928,6 +3930,51 @@ TEST_F(ToolTest, TestPerfTabletScan) {
}
}

TEST_F(ToolTest, TestTabletShowInfo) {
ExternalMiniClusterOptions opts;
opts.num_tablet_servers = 3;
const int kNumTservers = 3;
const int kNumTablets = 3;
const string& kTableName = "test_table";
MonoDelta kTimeout = MonoDelta::FromSeconds(30);
NO_FATALS(StartExternalMiniCluster(std::move(opts)));

TestWorkload workload(cluster_.get());
workload.set_num_tablets(kNumTservers);
workload.set_num_replicas(kNumTablets);
workload.set_table_name(kTableName);
workload.Setup();

vector<ListTabletsResponsePB::StatusAndSchemaPB> tablets;
TServerDetails* ts = ts_map_[cluster_->tablet_server(0)->uuid()];
ASSERT_OK(WaitForNumTabletsOnTS(ts, kNumTablets, kTimeout, &tablets));

vector<string> master_addrs;
for (const auto& hp : cluster_->master_rpc_addrs()) {
master_addrs.emplace_back(hp.ToString());
}
const string& master_addrs_str = JoinStrings(master_addrs, ",");
string stdout;
NO_FATALS(RunActionStdoutString(Substitute("tablet show_info $0 $1",
master_addrs_str, tablets[0].tablet_status().tablet_id()), &stdout));

// Stdout contains table name.
ASSERT_STR_CONTAINS(stdout, kTableName);
client::sp::shared_ptr<KuduTable> workload_table;
ASSERT_OK(workload.client()->OpenTable(workload.table_name(), &workload_table));
// Stdout contains table id.
ASSERT_STR_CONTAINS(stdout, workload_table->id());
for (int i = 0; i < kNumTservers; i++) {
// Stdout contains tserver's host.
ASSERT_STR_CONTAINS(stdout, cluster_->tablet_server(i)->bound_rpc_hostport().host());
// Stdout contains tserver's port.
ASSERT_STR_CONTAINS(stdout, Substitute("$0",
cluster_->tablet_server(i)->bound_rpc_hostport().port()));
// Stdout contains tserver's uuid.
ASSERT_STR_CONTAINS(stdout, cluster_->tablet_server(i)->uuid());
}
}

// Test 'kudu remote_replica copy' tool when the destination tablet server is online.
// 1. Test the copy tool when the destination replica is healthy
// 2. Test the copy tool when the destination replica is tombstoned
Expand Down
32 changes: 32 additions & 0 deletions src/kudu/tools/tool_action_tablet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ DEFINE_int64(move_leader_timeout_sec, 30,
"Number of seconds to wait for a leader when relocating a leader tablet");

using kudu::client::KuduClient;
using kudu::client::KuduTablet;
using kudu::consensus::ADD_PEER;
using kudu::consensus::ChangeConfigType;
using kudu::consensus::LeaderStepDownMode;
Expand All @@ -81,6 +82,7 @@ using std::endl;
using std::make_optional;
using std::nullopt;
using std::optional;
using std::ostream;
using std::string;
using std::unique_ptr;
using std::vector;
Expand Down Expand Up @@ -276,6 +278,29 @@ Status ReplaceTablet(const RunnerContext& context) {
return Status::OK();
}

Status ShowInfo(const RunnerContext& context) {
vector<string> master_addresses;
RETURN_NOT_OK(ParseMasterAddresses(context, &master_addresses));
const string& tablet_id = FindOrDie(context.required_args, kTabletIdArg);
client::sp::shared_ptr<KuduClient> client;
RETURN_NOT_OK(CreateKuduClient(master_addresses, &client));

KuduTablet* tablet_raw = nullptr;
RETURN_NOT_OK(client->GetTablet(tablet_id, &tablet_raw));
unique_ptr<KuduTablet> tablet(tablet_raw);

cout << Substitute("Table id: $0, Table name: $1",
tablet->table_id(), tablet->table_name()) << endl;
DataTable cmatrix({ "UUID", "Host", "Port", "Is Leader"});
for (const auto& replica : tablet->replicas()) {
cmatrix.AddRow({replica->ts().uuid(), replica->ts().hostname(),
Substitute("$0", replica->ts().port()),
Substitute("$0", replica->is_leader())});
}
ostream out(std::cout.rdbuf());
return cmatrix.PrintTo(out);
}

} // anonymous namespace

unique_ptr<Mode> BuildTabletMode() {
Expand Down Expand Up @@ -359,11 +384,18 @@ unique_ptr<Mode> BuildTabletMode() {
.AddRequiredParameter({ kTabletIdArg, kTabletIdArgDesc })
.Build();

unique_ptr<Action> show_info =
ClusterActionBuilder("show_info", &ShowInfo)
.Description("Show the table and location information of a tablet.")
.AddRequiredParameter({ kTabletIdArg, kTabletIdArgDesc })
.Build();

return ModeBuilder("tablet")
.Description("Operate on remote Kudu tablets")
.AddMode(std::move(change_config))
.AddAction(std::move(leader_step_down))
.AddAction(std::move(replace_tablet))
.AddAction(std::move(show_info))
.Build();
}

Expand Down