Skip to content

Commit

Permalink
[Eng-3495] #343 Implement Select and CreateDB commands.
Browse files Browse the repository at this point in the history
Summary:
Server side only.
Added test(s). Changes to client/Jedis to follow in a separate diff.

Depends on https://phabricator.dev.yugabyte.com/D5217

Test Plan: Added test. Wait for jenkins.

Reviewers: karthik, kannan, hector, sergei, robert, venkatesh

Reviewed By: venkatesh

Subscribers: bogdan, bharat, yql

Differential Revision: https://phabricator.dev.yugabyte.com/D5172
  • Loading branch information
amitanandaiyer committed Aug 6, 2018
1 parent e72395e commit 8758c9f
Show file tree
Hide file tree
Showing 12 changed files with 570 additions and 161 deletions.
14 changes: 9 additions & 5 deletions src/yb/client/client-internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -596,14 +596,16 @@ Status YBClient::Data::WaitForDeleteTableToFinish(YBClient* client,
std::bind(&YBClient::Data::IsDeleteTableInProgress, this, client, deleted_table_id, _1, _2));
}

Status YBClient::Data::TruncateTable(YBClient* client,
const string& table_id,
Status YBClient::Data::TruncateTables(YBClient* client,
const vector<string>& table_ids,
const MonoTime& deadline,
bool wait) {
TruncateTableRequestPB req;
TruncateTableResponsePB resp;

req.set_table_id(table_id);
for (const auto& table_id : table_ids) {
req.add_table_ids(table_id);
}
RETURN_NOT_OK((SyncLeaderMasterRpc<TruncateTableRequestPB, TruncateTableResponsePB>(
deadline, client, req, &resp, nullptr /* num_attempts */, "TruncateTable",
&MasterServiceProxy::TruncateTable)));
Expand All @@ -613,10 +615,12 @@ Status YBClient::Data::TruncateTable(YBClient* client,

// Spin until the table is fully truncated, if requested.
if (wait) {
RETURN_NOT_OK(WaitForTruncateTableToFinish(client, table_id, deadline));
for (const auto& table_id : table_ids) {
RETURN_NOT_OK(WaitForTruncateTableToFinish(client, table_id, deadline));
}
}

LOG(INFO) << "Truncated table " << table_id;
LOG(INFO) << "Truncated table(s) " << JoinStrings(table_ids, ",");
return Status::OK();
}

Expand Down
8 changes: 4 additions & 4 deletions src/yb/client/client-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ class YBClient::Data {
const std::string& deleted_table_id,
const MonoTime& deadline);

CHECKED_STATUS TruncateTable(YBClient* client,
const std::string& table_id,
const MonoTime& deadline,
bool wait = true);
CHECKED_STATUS TruncateTables(YBClient* client,
const std::vector<std::string>& table_ids,
const MonoTime& deadline,
bool wait = true);

CHECKED_STATUS IsTruncateTableInProgress(YBClient* client,
const std::string& table_id,
Expand Down
43 changes: 16 additions & 27 deletions src/yb/client/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,8 @@ using std::string;
using std::vector;
using google::protobuf::RepeatedPtrField;

using namespace yb::size_literals;
using namespace yb::size_literals; // NOLINT.

DEFINE_int32(redis_password_caching_duration_ms, 5000,
"The duration for which we will cache the redis passwords. 0 to disable.");
DEFINE_test_flag(int32, yb_num_total_tablets, 0,
"The total number of tablets per table when a table is created.");
DECLARE_int32(yb_num_shards_per_tserver);
Expand Down Expand Up @@ -406,8 +404,12 @@ Status YBClient::IsCreateTableInProgress(const YBTableName& table_name,
}

Status YBClient::TruncateTable(const string& table_id, bool wait) {
return TruncateTables({table_id}, wait);
}

Status YBClient::TruncateTables(const vector<string>& table_ids, bool wait) {
MonoTime deadline = MonoTime::Now() + default_admin_operation_timeout();
return data_->TruncateTable(this, table_id, deadline, wait);
return data_->TruncateTables(this, table_ids, deadline, wait);
}

Status YBClient::DeleteTable(const YBTableName& table_name, bool wait) {
Expand Down Expand Up @@ -628,6 +630,16 @@ CHECKED_STATUS YBClient::SetRedisPasswords(const std::vector<string>& passwords)
return SetRedisConfig(kRequirePass, passwords);
}

CHECKED_STATUS YBClient::GetRedisPasswords(vector<string>* passwords) {
Status s = GetRedisConfig(kRequirePass, passwords);
if (s.IsNotFound()) {
// If the redis config has no kRequirePass key.
passwords->clear();
s = Status::OK();
}
return s;
}

CHECKED_STATUS YBClient::SetRedisConfig(const string& key, const vector<string>& values) {
// Setting up request.
RedisConfigSetRequestPB req;
Expand All @@ -652,29 +664,6 @@ CHECKED_STATUS YBClient::GetRedisConfig(const string& key, vector<string>* value
return Status::OK();
}

CHECKED_STATUS YBClient::GetRedisPasswords(vector<string>* passwords) {
MonoTime now = MonoTime::Now();
{
std::lock_guard<std::mutex> lock(redis_password_mutex_);
if (redis_cached_password_validity_expiry_.Initialized() &&
now < redis_cached_password_validity_expiry_) {
*passwords = redis_cached_passwords_;
return Status::OK();
}

redis_cached_password_validity_expiry_ =
now + MonoDelta::FromMilliseconds(FLAGS_redis_password_caching_duration_ms);
Status s = GetRedisConfig(kRequirePass, &redis_cached_passwords_);
if (s.IsNotFound()) {
// If the redis config has no kRequirePass key.
redis_cached_passwords_.clear();
s = Status::OK();
}
*passwords = redis_cached_passwords_;
return s;
}
}

CHECKED_STATUS YBClient::GrantRole(const std::string& granted_role_name,
const std::string& recipient_role_name) {
// Setting up request.
Expand Down
5 changes: 1 addition & 4 deletions src/yb/client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ class YBClient : public std::enable_shared_from_this<YBClient> {
// Truncate the specified table.
// Set 'wait' to true if the call must wait for the table to be fully truncated before returning.
CHECKED_STATUS TruncateTable(const std::string& table_id, bool wait = true);
CHECKED_STATUS TruncateTables(const std::vector<std::string>& table_ids, bool wait = true);

// Delete the specified table.
// Set 'wait' to true if the call must wait for the table to be fully deleted before returning.
Expand Down Expand Up @@ -544,10 +545,6 @@ class YBClient : public std::enable_shared_from_this<YBClient> {
// same client to the same data.
const std::string client_id_;

std::mutex redis_password_mutex_;
MonoTime redis_cached_password_validity_expiry_;
vector<string> redis_cached_passwords_;

DISALLOW_COPY_AND_ASSIGN(YBClient);
};

Expand Down
5 changes: 3 additions & 2 deletions src/yb/client/yb_table_name.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ class YBTableName {
}

bool is_redis_table() const {
return ((has_namespace() && resolved_namespace_name() == common::kRedisKeyspaceName) &&
table_name_ == common::kRedisTableName);
return (
(has_namespace() && resolved_namespace_name() == common::kRedisKeyspaceName) &&
table_name_.find(common::kRedisTableName) == 0);
}

std::string ToString() const {
Expand Down
39 changes: 21 additions & 18 deletions src/yb/master/catalog_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2099,27 +2099,30 @@ Status CatalogManager::TruncateTable(const TruncateTableRequestPB* req,

RETURN_NOT_OK(CheckOnline());

// Lookup the table and verify if it exists.
TRACE("Looking up table");
scoped_refptr<TableInfo> table = FindPtrOrNull(table_ids_map_, req->table_id());
if (table == nullptr) {
Status s = STATUS(NotFound, "The table does not exist");
return SetupError(resp->mutable_error(), MasterErrorPB::TABLE_NOT_FOUND, s);
}
for (int i = 0; i < req->table_ids_size(); i++) {
const auto& table_id = req->table_ids(i);
// Lookup the table and verify if it exists.
TRACE("Looking up table");
scoped_refptr<TableInfo> table = FindPtrOrNull(table_ids_map_, table_id);
if (table == nullptr) {
Status s = STATUS(NotFound, Substitute("The table for table_id $0 does not exist", table_id));
return SetupError(resp->mutable_error(), MasterErrorPB::TABLE_NOT_FOUND, s);
}

TRACE("Locking table");
auto l = table->LockForRead();
if (l->data().started_deleting() || l->data().is_deleted()) {
Status s = STATUS(NotFound, "The table does not exist");
return SetupError(resp->mutable_error(), MasterErrorPB::TABLE_NOT_FOUND, s);
}
TRACE("Locking table");
auto l = table->LockForRead();
if (l->data().started_deleting() || l->data().is_deleted()) {
Status s = STATUS(NotFound, Substitute("The table $0 does not exist", table->ToString()));
return SetupError(resp->mutable_error(), MasterErrorPB::TABLE_NOT_FOUND, s);
}

// Send a Truncate() request to each tablet in the table.
SendTruncateTableRequest(table);
// Send a Truncate() request to each tablet in the table.
SendTruncateTableRequest(table);

LOG(INFO) << "Successfully initiated TRUNCATE for " << table->ToString()
<< " per request from " << RequestorString(rpc);
background_tasks_->Wake();
LOG(INFO) << "Successfully initiated TRUNCATE for " << table->ToString() << " per request from "
<< RequestorString(rpc);
background_tasks_->Wake();
}
return Status::OK();
}

Expand Down
2 changes: 1 addition & 1 deletion src/yb/master/master.proto
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ message IsCreateTableDoneResponsePB {
}

message TruncateTableRequestPB {
optional bytes table_id = 1;
repeated bytes table_ids = 1;
}

message TruncateTableResponsePB {
Expand Down
Loading

0 comments on commit 8758c9f

Please sign in to comment.