Skip to content

Commit

Permalink
feat(bigtable): cheap Table creation with different resource name (#8172
Browse files Browse the repository at this point in the history
)

Customers can now produce a copy of a Table with a modified resource name, without needing to make a new connection to the service.
  • Loading branch information
dbolduc authored Jan 31, 2022
1 parent 738fbee commit 7531fa0
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 3 deletions.
22 changes: 22 additions & 0 deletions google/cloud/bigtable/metadata_update_policy_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,28 @@ TEST_F(MetadataUpdatePolicyTest, RunWithEmbeddedServerParamTableName) {
EXPECT_EQ(expected, range.first->second);
}

/// @test A test for setting metadata when table is known.
TEST_F(MetadataUpdatePolicyTest, ModifiedTableName) {
std::string const other_project_id = "other-project";
std::string const other_instance_id = "other-instance";
std::string const other_table_id = "other-table";
auto other_table = table_->WithNewTarget(other_project_id, other_instance_id,
other_table_id);

grpc::string expected =
"table_name=" +
TableName(other_project_id, other_instance_id, other_table_id);
auto reader =
other_table.ReadRows(RowSet("row1"), 1, Filter::PassAllFilter());
// lets make the RPC call to send metadata
reader.begin();
// Get metadata from embedded server
auto client_metadata = bigtable_service_.client_metadata();
auto range = client_metadata.equal_range("x-goog-request-params");
ASSERT_EQ(1, std::distance(range.first, range.second));
EXPECT_EQ(expected, range.first->second);
}

/// @test A cloning test for normal construction of metadata .
TEST_F(MetadataUpdatePolicyTest, SimpleDefault) {
auto const x_google_request_params = "parent=" + std::string(kInstanceName);
Expand Down
41 changes: 38 additions & 3 deletions google/cloud/bigtable/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "google/cloud/bigtable/idempotent_mutation_policy.h"
#include "google/cloud/bigtable/mutations.h"
#include "google/cloud/bigtable/read_modify_write_rule.h"
#include "google/cloud/bigtable/resource_names.h"
#include "google/cloud/bigtable/row_key_sample.h"
#include "google/cloud/bigtable/row_reader.h"
#include "google/cloud/bigtable/row_set.h"
Expand Down Expand Up @@ -212,7 +213,9 @@ class Table {
std::string const& table_id)
: client_(std::move(client)),
app_profile_id_(std::move(app_profile_id)),
table_name_(TableName(client_, table_id)),
project_id_(client_->project_id()),
instance_id_(client_->instance_id()),
table_name_(TableName(project_id_, instance_id_, table_id)),
table_id_(table_id),
rpc_retry_policy_prototype_(
bigtable::DefaultRPCRetryPolicy(internal::kBigtableLimits)),
Expand Down Expand Up @@ -351,10 +354,40 @@ class Table {

std::string const& table_name() const { return table_name_; }
std::string const& app_profile_id() const { return app_profile_id_; }
std::string const& project_id() const { return client_->project_id(); }
std::string const& instance_id() const { return client_->instance_id(); }
std::string const& project_id() const { return project_id_; }
std::string const& instance_id() const { return instance_id_; }
std::string const& table_id() const { return table_id_; }

/**
* Returns a Table that reuses the connection and configuration of this
* Table, but with a different resource name.
*
* @note The app profile id is copied from this Table.
*/
Table WithNewTarget(std::string project_id, std::string instance_id,
std::string table_id) const {
return WithNewTarget(std::move(project_id), std::move(instance_id),
app_profile_id_, std::move(table_id));
}

/**
* Returns a Table that reuses the connection and configuration of this
* Table, but with a different resource name.
*/
Table WithNewTarget(std::string project_id, std::string instance_id,
std::string app_profile_id, std::string table_id) const {
auto table = *this;
table.instance_id_ = std::move(instance_id);
table.project_id_ = std::move(project_id);
table.table_id_ = std::move(table_id);
table.app_profile_id_ = std::move(app_profile_id);
table.table_name_ =
TableName(table.project_id_, table.instance_id_, table.table_id_);
table.metadata_update_policy_ =
MetadataUpdatePolicy(table.table_name_, MetadataParamTypes::TABLE_NAME);
return table;
}

/**
* Attempts to apply the mutation to a row.
*
Expand Down Expand Up @@ -936,6 +969,8 @@ class Table {
friend class MutationBatcher;
std::shared_ptr<DataClient> client_;
std::string app_profile_id_;
std::string project_id_;
std::string instance_id_;
std::string table_name_;
std::string table_id_;
std::shared_ptr<RPCRetryPolicy const> rpc_retry_policy_prototype_;
Expand Down
45 changes: 45 additions & 0 deletions google/cloud/bigtable/table_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,51 @@ TEST_F(TableTest, StandaloneTableName) {

TEST_F(TableTest, TableName) { EXPECT_EQ(kTableName, table_.table_name()); }

TEST_F(TableTest, WithNewTarget) {
Table table(client_, "original-profile", kTableId);
EXPECT_EQ(table.project_id(), kProjectId);
EXPECT_EQ(table.instance_id(), kInstanceId);
EXPECT_EQ(table.table_id(), kTableId);
EXPECT_EQ(table.table_name(), TableName(kProjectId, kInstanceId, kTableId));
EXPECT_EQ(table.app_profile_id(), "original-profile");

std::string const other_project_id = "other-project";
std::string const other_instance_id = "other-instance";
std::string const other_table_id = "other-table";
auto other_table =
table.WithNewTarget(other_project_id, other_instance_id, other_table_id);

EXPECT_EQ(other_table.project_id(), other_project_id);
EXPECT_EQ(other_table.instance_id(), other_instance_id);
EXPECT_EQ(other_table.table_id(), other_table_id);
EXPECT_EQ(other_table.table_name(),
TableName(other_project_id, other_instance_id, other_table_id));
EXPECT_EQ(other_table.app_profile_id(), "original-profile");
}

TEST_F(TableTest, WithNewTargetProfile) {
Table table(client_, "original-profile", kTableId);
EXPECT_EQ(table.project_id(), kProjectId);
EXPECT_EQ(table.instance_id(), kInstanceId);
EXPECT_EQ(table.table_id(), kTableId);
EXPECT_EQ(table.table_name(), TableName(kProjectId, kInstanceId, kTableId));
EXPECT_EQ(table.app_profile_id(), "original-profile");

std::string const other_project_id = "other-project";
std::string const other_instance_id = "other-instance";
std::string const other_table_id = "other-table";
std::string const other_profile_id = "other-profile";
auto other_table = table.WithNewTarget(other_project_id, other_instance_id,
other_profile_id, other_table_id);

EXPECT_EQ(other_table.project_id(), other_project_id);
EXPECT_EQ(other_table.instance_id(), other_instance_id);
EXPECT_EQ(other_table.table_id(), other_table_id);
EXPECT_EQ(other_table.table_name(),
TableName(other_project_id, other_instance_id, other_table_id));
EXPECT_EQ(other_table.app_profile_id(), other_profile_id);
}

TEST_F(TableTest, TableConstructor) {
std::string const other_table_id = "my-table";
std::string const other_table_name = TableName(client_, other_table_id);
Expand Down

0 comments on commit 7531fa0

Please sign in to comment.