Skip to content

Commit

Permalink
impl(bigquery): Project options and policies (#11617)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsrinnn authored May 15, 2023
1 parent efb80e2 commit f3eedec
Show file tree
Hide file tree
Showing 10 changed files with 374 additions and 0 deletions.
7 changes: 7 additions & 0 deletions google/cloud/bigquery/bigquery_rest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ add_library(
v2/minimal/internal/log_wrapper.h
v2/minimal/internal/project.cc
v2/minimal/internal/project.h
v2/minimal/internal/project_idempotency_policy.cc
v2/minimal/internal/project_idempotency_policy.h
v2/minimal/internal/project_options.cc
v2/minimal/internal/project_options.h
v2/minimal/internal/project_request.cc
v2/minimal/internal/project_request.h
v2/minimal/internal/project_response.cc
Expand All @@ -83,6 +87,7 @@ add_library(
v2/minimal/internal/project_rest_stub.h
v2/minimal/internal/project_rest_stub_factory.cc
v2/minimal/internal/project_rest_stub_factory.h
v2/minimal/internal/project_retry_policy.h
v2/minimal/internal/rest_stub_utils.cc
v2/minimal/internal/rest_stub_utils.h
v2/minimal/internal/table.cc
Expand Down Expand Up @@ -231,6 +236,8 @@ function (bigquery_rest_define_tests)
v2/minimal/internal/job_response_test.cc
v2/minimal/internal/job_rest_stub_test.cc
v2/minimal/internal/job_test.cc
v2/minimal/internal/project_idempotency_policy_test.cc
v2/minimal/internal/project_options_test.cc
v2/minimal/internal/project_request_test.cc
v2/minimal/internal/project_response_test.cc
v2/minimal/internal/project_rest_stub_test.cc
Expand Down
2 changes: 2 additions & 0 deletions google/cloud/bigquery/bigquery_rest_unit_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ bigquery_rest_unit_tests = [
"v2/minimal/internal/job_response_test.cc",
"v2/minimal/internal/job_rest_stub_test.cc",
"v2/minimal/internal/job_test.cc",
"v2/minimal/internal/project_idempotency_policy_test.cc",
"v2/minimal/internal/project_options_test.cc",
"v2/minimal/internal/project_request_test.cc",
"v2/minimal/internal/project_response_test.cc",
"v2/minimal/internal/project_rest_stub_test.cc",
Expand Down
5 changes: 5 additions & 0 deletions google/cloud/bigquery/google_cloud_cpp_bigquery_rest.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@ google_cloud_cpp_bigquery_rest_hdrs = [
"v2/minimal/internal/job_retry_policy.h",
"v2/minimal/internal/log_wrapper.h",
"v2/minimal/internal/project.h",
"v2/minimal/internal/project_idempotency_policy.h",
"v2/minimal/internal/project_options.h",
"v2/minimal/internal/project_request.h",
"v2/minimal/internal/project_response.h",
"v2/minimal/internal/project_rest_stub.h",
"v2/minimal/internal/project_rest_stub_factory.h",
"v2/minimal/internal/project_retry_policy.h",
"v2/minimal/internal/rest_stub_utils.h",
"v2/minimal/internal/table.h",
"v2/minimal/internal/table_client.h",
Expand Down Expand Up @@ -101,6 +104,8 @@ google_cloud_cpp_bigquery_rest_srcs = [
"v2/minimal/internal/job_rest_stub.cc",
"v2/minimal/internal/job_rest_stub_factory.cc",
"v2/minimal/internal/project.cc",
"v2/minimal/internal/project_idempotency_policy.cc",
"v2/minimal/internal/project_options.cc",
"v2/minimal/internal/project_request.cc",
"v2/minimal/internal/project_response.cc",
"v2/minimal/internal/project_rest_stub.cc",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2023 Google LLC
//
// Licensed 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
//
// https://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 "google/cloud/bigquery/v2/minimal/internal/project_idempotency_policy.h"
#include <memory>

namespace google {
namespace cloud {
namespace bigquery_v2_minimal_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN

ProjectIdempotencyPolicy::~ProjectIdempotencyPolicy() = default;

std::unique_ptr<ProjectIdempotencyPolicy> ProjectIdempotencyPolicy::clone()
const {
return std::make_unique<ProjectIdempotencyPolicy>(*this);
}

Idempotency ProjectIdempotencyPolicy::ListProjects(ListProjectsRequest const&) {
return ::google::cloud::Idempotency::kIdempotent;
}

std::unique_ptr<ProjectIdempotencyPolicy>
MakeDefaultProjectIdempotencyPolicy() {
return std::make_unique<ProjectIdempotencyPolicy>();
}

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace bigquery_v2_minimal_internal
} // namespace cloud
} // namespace google
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2023 Google LLC
//
// Licensed 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
//
// https://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.

#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGQUERY_V2_MINIMAL_INTERNAL_PROJECT_IDEMPOTENCY_POLICY_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGQUERY_V2_MINIMAL_INTERNAL_PROJECT_IDEMPOTENCY_POLICY_H

#include "google/cloud/bigquery/v2/minimal/internal/project_request.h"
#include "google/cloud/idempotency.h"
#include "google/cloud/version.h"
#include <memory>

namespace google {
namespace cloud {
namespace bigquery_v2_minimal_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN

class ProjectIdempotencyPolicy {
public:
virtual ~ProjectIdempotencyPolicy();

virtual std::unique_ptr<ProjectIdempotencyPolicy> clone() const;

virtual google::cloud::Idempotency ListProjects(
ListProjectsRequest const& request);
};

std::unique_ptr<ProjectIdempotencyPolicy> MakeDefaultProjectIdempotencyPolicy();

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace bigquery_v2_minimal_internal
} // namespace cloud
} // namespace google

#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGQUERY_V2_MINIMAL_INTERNAL_PROJECT_IDEMPOTENCY_POLICY_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2023 Google LLC
//
// Licensed 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
//
// https://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 "google/cloud/bigquery/v2/minimal/internal/project_idempotency_policy.h"
#include <gmock/gmock.h>

namespace google {
namespace cloud {
namespace bigquery_v2_minimal_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN

TEST(ProjectIdempotencyPolicytTest, ListProjects) {
auto actual = MakeDefaultProjectIdempotencyPolicy();
auto expected = Idempotency::kIdempotent;

ListProjectsRequest request;
EXPECT_EQ(actual->ListProjects(request), expected);
}

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace bigquery_v2_minimal_internal
} // namespace cloud
} // namespace google
56 changes: 56 additions & 0 deletions google/cloud/bigquery/v2/minimal/internal/project_options.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2023 Google LLC
//
// Licensed 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
//
// https://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 "google/cloud/bigquery/v2/minimal/internal/project_options.h"
#include "google/cloud/bigquery/v2/minimal/internal/common_options.h"
#include "google/cloud/internal/populate_common_options.h"
#include <chrono>
#include <memory>

namespace google {
namespace cloud {
namespace bigquery_v2_minimal_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN

Options ProjectDefaultOptions(Options options) {
options = google::cloud::internal::PopulateCommonOptions(
std::move(options), "GOOGLE_CLOUD_CPP_BIGQUERY_V2_PROJECT_ENDPOINT", "",
"GOOGLE_CLOUD_CPP_BIGQUERY_V2_PROJECT_AUTHORITY",
"bigquery.googleapis.com");

if (!options.has<ProjectRetryPolicyOption>()) {
options.set<ProjectRetryPolicyOption>(
ProjectLimitedTimeRetryPolicy(std::chrono::minutes(30)).clone());
}
if (!options.has<ProjectBackoffPolicyOption>()) {
options.set<ProjectBackoffPolicyOption>(
ExponentialBackoffPolicy(std::chrono::seconds(1),
std::chrono::minutes(5), kBackoffScaling)
.clone());
}
if (!options.has<ProjectIdempotencyPolicyOption>()) {
options.set<ProjectIdempotencyPolicyOption>(
MakeDefaultProjectIdempotencyPolicy());
}
if (!options.has<ProjectConnectionPoolSizeOption>()) {
options.set<ProjectConnectionPoolSizeOption>(DefaultConnectionPoolSize());
}

return options;
}

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace bigquery_v2_minimal_internal
} // namespace cloud
} // namespace google
76 changes: 76 additions & 0 deletions google/cloud/bigquery/v2/minimal/internal/project_options.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2023 Google LLC
//
// Licensed 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
//
// https://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.

#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGQUERY_V2_MINIMAL_INTERNAL_PROJECT_OPTIONS_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGQUERY_V2_MINIMAL_INTERNAL_PROJECT_OPTIONS_H

#include "google/cloud/bigquery/v2/minimal/internal/project_idempotency_policy.h"
#include "google/cloud/bigquery/v2/minimal/internal/project_retry_policy.h"
#include "google/cloud/backoff_policy.h"
#include "google/cloud/options.h"
#include "google/cloud/version.h"
#include <memory>

namespace google {
namespace cloud {
namespace bigquery_v2_minimal_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN

/**
* Use with `google::cloud::Options` to configure the retry policy.
*/
struct ProjectRetryPolicyOption {
using Type = std::shared_ptr<ProjectRetryPolicy>;
};

/**
* Use with `google::cloud::Options` to configure the backoff policy.
*/
struct ProjectBackoffPolicyOption {
using Type = std::shared_ptr<BackoffPolicy>;
};

/**
* Use with `google::cloud::Options` to configure which operations are retried.
*/
struct ProjectIdempotencyPolicyOption {
using Type = std::shared_ptr<ProjectIdempotencyPolicy>;
};

/**
* Use with `google::cloud::Options` to configure the connection pool size for
* rest client.
*/
struct ProjectConnectionPoolSizeOption {
using Type = std::size_t;
};

/**
* The options applicable to Project.
*/
using ProjectPolicyOptionList =
OptionList<ProjectRetryPolicyOption, ProjectBackoffPolicyOption,
ProjectIdempotencyPolicyOption, ProjectConnectionPoolSizeOption>;

/**
* Default options for Project.
*/
Options ProjectDefaultOptions(Options options);

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace bigquery_v2_minimal_internal
} // namespace cloud
} // namespace google

#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGQUERY_V2_MINIMAL_INTERNAL_PROJECT_OPTIONS_H
58 changes: 58 additions & 0 deletions google/cloud/bigquery/v2/minimal/internal/project_options_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2023 Google LLC
//
// Licensed 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
//
// https://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 "google/cloud/bigquery/v2/minimal/internal/project_options.h"
#include "google/cloud/common_options.h"
#include "google/cloud/testing_util/status_matchers.h"
#include <gmock/gmock.h>

namespace google {
namespace cloud {
namespace bigquery_v2_minimal_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN

TEST(ProjectOptionsTest, DefaultOptions) {
Options opts;
auto actual = ProjectDefaultOptions(std::move(opts));
auto expected_retry =
ProjectLimitedTimeRetryPolicy(std::chrono::minutes(30)).clone();
auto expected_backoff = ExponentialBackoffPolicy(
std::chrono::seconds(1), std::chrono::minutes(5), 2.0);
auto expected_idempotency = Idempotency::kIdempotent;
auto const* default_endpoint = "bigquery.googleapis.com";

EXPECT_TRUE(actual.has<EndpointOption>());
EXPECT_EQ(actual.get<EndpointOption>(), default_endpoint);

EXPECT_TRUE(actual.has<AuthorityOption>());
EXPECT_EQ(actual.get<AuthorityOption>(), default_endpoint);

ListProjectsRequest request;
EXPECT_TRUE(actual.has<ProjectIdempotencyPolicyOption>());
EXPECT_EQ(actual.get<ProjectIdempotencyPolicyOption>()->ListProjects(request),
expected_idempotency);

EXPECT_TRUE(actual.has<ProjectRetryPolicyOption>());
EXPECT_EQ(actual.get<ProjectRetryPolicyOption>()->IsExhausted(),
expected_retry->IsExhausted());

EXPECT_TRUE(actual.has<ProjectBackoffPolicyOption>());

EXPECT_GT(actual.get<ProjectConnectionPoolSizeOption>(), 0);
}

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace bigquery_v2_minimal_internal
} // namespace cloud
} // namespace google
Loading

0 comments on commit f3eedec

Please sign in to comment.