Skip to content

Commit f3eedec

Browse files
authored
impl(bigquery): Project options and policies (#11617)
1 parent efb80e2 commit f3eedec

10 files changed

+374
-0
lines changed

google/cloud/bigquery/bigquery_rest.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ add_library(
7575
v2/minimal/internal/log_wrapper.h
7676
v2/minimal/internal/project.cc
7777
v2/minimal/internal/project.h
78+
v2/minimal/internal/project_idempotency_policy.cc
79+
v2/minimal/internal/project_idempotency_policy.h
80+
v2/minimal/internal/project_options.cc
81+
v2/minimal/internal/project_options.h
7882
v2/minimal/internal/project_request.cc
7983
v2/minimal/internal/project_request.h
8084
v2/minimal/internal/project_response.cc
@@ -83,6 +87,7 @@ add_library(
8387
v2/minimal/internal/project_rest_stub.h
8488
v2/minimal/internal/project_rest_stub_factory.cc
8589
v2/minimal/internal/project_rest_stub_factory.h
90+
v2/minimal/internal/project_retry_policy.h
8691
v2/minimal/internal/rest_stub_utils.cc
8792
v2/minimal/internal/rest_stub_utils.h
8893
v2/minimal/internal/table.cc
@@ -231,6 +236,8 @@ function (bigquery_rest_define_tests)
231236
v2/minimal/internal/job_response_test.cc
232237
v2/minimal/internal/job_rest_stub_test.cc
233238
v2/minimal/internal/job_test.cc
239+
v2/minimal/internal/project_idempotency_policy_test.cc
240+
v2/minimal/internal/project_options_test.cc
234241
v2/minimal/internal/project_request_test.cc
235242
v2/minimal/internal/project_response_test.cc
236243
v2/minimal/internal/project_rest_stub_test.cc

google/cloud/bigquery/bigquery_rest_unit_tests.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ bigquery_rest_unit_tests = [
3939
"v2/minimal/internal/job_response_test.cc",
4040
"v2/minimal/internal/job_rest_stub_test.cc",
4141
"v2/minimal/internal/job_test.cc",
42+
"v2/minimal/internal/project_idempotency_policy_test.cc",
43+
"v2/minimal/internal/project_options_test.cc",
4244
"v2/minimal/internal/project_request_test.cc",
4345
"v2/minimal/internal/project_response_test.cc",
4446
"v2/minimal/internal/project_rest_stub_test.cc",

google/cloud/bigquery/google_cloud_cpp_bigquery_rest.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,13 @@ google_cloud_cpp_bigquery_rest_hdrs = [
4949
"v2/minimal/internal/job_retry_policy.h",
5050
"v2/minimal/internal/log_wrapper.h",
5151
"v2/minimal/internal/project.h",
52+
"v2/minimal/internal/project_idempotency_policy.h",
53+
"v2/minimal/internal/project_options.h",
5254
"v2/minimal/internal/project_request.h",
5355
"v2/minimal/internal/project_response.h",
5456
"v2/minimal/internal/project_rest_stub.h",
5557
"v2/minimal/internal/project_rest_stub_factory.h",
58+
"v2/minimal/internal/project_retry_policy.h",
5659
"v2/minimal/internal/rest_stub_utils.h",
5760
"v2/minimal/internal/table.h",
5861
"v2/minimal/internal/table_client.h",
@@ -101,6 +104,8 @@ google_cloud_cpp_bigquery_rest_srcs = [
101104
"v2/minimal/internal/job_rest_stub.cc",
102105
"v2/minimal/internal/job_rest_stub_factory.cc",
103106
"v2/minimal/internal/project.cc",
107+
"v2/minimal/internal/project_idempotency_policy.cc",
108+
"v2/minimal/internal/project_options.cc",
104109
"v2/minimal/internal/project_request.cc",
105110
"v2/minimal/internal/project_response.cc",
106111
"v2/minimal/internal/project_rest_stub.cc",
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "google/cloud/bigquery/v2/minimal/internal/project_idempotency_policy.h"
16+
#include <memory>
17+
18+
namespace google {
19+
namespace cloud {
20+
namespace bigquery_v2_minimal_internal {
21+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
22+
23+
ProjectIdempotencyPolicy::~ProjectIdempotencyPolicy() = default;
24+
25+
std::unique_ptr<ProjectIdempotencyPolicy> ProjectIdempotencyPolicy::clone()
26+
const {
27+
return std::make_unique<ProjectIdempotencyPolicy>(*this);
28+
}
29+
30+
Idempotency ProjectIdempotencyPolicy::ListProjects(ListProjectsRequest const&) {
31+
return ::google::cloud::Idempotency::kIdempotent;
32+
}
33+
34+
std::unique_ptr<ProjectIdempotencyPolicy>
35+
MakeDefaultProjectIdempotencyPolicy() {
36+
return std::make_unique<ProjectIdempotencyPolicy>();
37+
}
38+
39+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
40+
} // namespace bigquery_v2_minimal_internal
41+
} // namespace cloud
42+
} // namespace google
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGQUERY_V2_MINIMAL_INTERNAL_PROJECT_IDEMPOTENCY_POLICY_H
16+
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGQUERY_V2_MINIMAL_INTERNAL_PROJECT_IDEMPOTENCY_POLICY_H
17+
18+
#include "google/cloud/bigquery/v2/minimal/internal/project_request.h"
19+
#include "google/cloud/idempotency.h"
20+
#include "google/cloud/version.h"
21+
#include <memory>
22+
23+
namespace google {
24+
namespace cloud {
25+
namespace bigquery_v2_minimal_internal {
26+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
27+
28+
class ProjectIdempotencyPolicy {
29+
public:
30+
virtual ~ProjectIdempotencyPolicy();
31+
32+
virtual std::unique_ptr<ProjectIdempotencyPolicy> clone() const;
33+
34+
virtual google::cloud::Idempotency ListProjects(
35+
ListProjectsRequest const& request);
36+
};
37+
38+
std::unique_ptr<ProjectIdempotencyPolicy> MakeDefaultProjectIdempotencyPolicy();
39+
40+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
41+
} // namespace bigquery_v2_minimal_internal
42+
} // namespace cloud
43+
} // namespace google
44+
45+
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGQUERY_V2_MINIMAL_INTERNAL_PROJECT_IDEMPOTENCY_POLICY_H
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "google/cloud/bigquery/v2/minimal/internal/project_idempotency_policy.h"
16+
#include <gmock/gmock.h>
17+
18+
namespace google {
19+
namespace cloud {
20+
namespace bigquery_v2_minimal_internal {
21+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
22+
23+
TEST(ProjectIdempotencyPolicytTest, ListProjects) {
24+
auto actual = MakeDefaultProjectIdempotencyPolicy();
25+
auto expected = Idempotency::kIdempotent;
26+
27+
ListProjectsRequest request;
28+
EXPECT_EQ(actual->ListProjects(request), expected);
29+
}
30+
31+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
32+
} // namespace bigquery_v2_minimal_internal
33+
} // namespace cloud
34+
} // namespace google
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "google/cloud/bigquery/v2/minimal/internal/project_options.h"
16+
#include "google/cloud/bigquery/v2/minimal/internal/common_options.h"
17+
#include "google/cloud/internal/populate_common_options.h"
18+
#include <chrono>
19+
#include <memory>
20+
21+
namespace google {
22+
namespace cloud {
23+
namespace bigquery_v2_minimal_internal {
24+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
25+
26+
Options ProjectDefaultOptions(Options options) {
27+
options = google::cloud::internal::PopulateCommonOptions(
28+
std::move(options), "GOOGLE_CLOUD_CPP_BIGQUERY_V2_PROJECT_ENDPOINT", "",
29+
"GOOGLE_CLOUD_CPP_BIGQUERY_V2_PROJECT_AUTHORITY",
30+
"bigquery.googleapis.com");
31+
32+
if (!options.has<ProjectRetryPolicyOption>()) {
33+
options.set<ProjectRetryPolicyOption>(
34+
ProjectLimitedTimeRetryPolicy(std::chrono::minutes(30)).clone());
35+
}
36+
if (!options.has<ProjectBackoffPolicyOption>()) {
37+
options.set<ProjectBackoffPolicyOption>(
38+
ExponentialBackoffPolicy(std::chrono::seconds(1),
39+
std::chrono::minutes(5), kBackoffScaling)
40+
.clone());
41+
}
42+
if (!options.has<ProjectIdempotencyPolicyOption>()) {
43+
options.set<ProjectIdempotencyPolicyOption>(
44+
MakeDefaultProjectIdempotencyPolicy());
45+
}
46+
if (!options.has<ProjectConnectionPoolSizeOption>()) {
47+
options.set<ProjectConnectionPoolSizeOption>(DefaultConnectionPoolSize());
48+
}
49+
50+
return options;
51+
}
52+
53+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
54+
} // namespace bigquery_v2_minimal_internal
55+
} // namespace cloud
56+
} // namespace google
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGQUERY_V2_MINIMAL_INTERNAL_PROJECT_OPTIONS_H
16+
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGQUERY_V2_MINIMAL_INTERNAL_PROJECT_OPTIONS_H
17+
18+
#include "google/cloud/bigquery/v2/minimal/internal/project_idempotency_policy.h"
19+
#include "google/cloud/bigquery/v2/minimal/internal/project_retry_policy.h"
20+
#include "google/cloud/backoff_policy.h"
21+
#include "google/cloud/options.h"
22+
#include "google/cloud/version.h"
23+
#include <memory>
24+
25+
namespace google {
26+
namespace cloud {
27+
namespace bigquery_v2_minimal_internal {
28+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
29+
30+
/**
31+
* Use with `google::cloud::Options` to configure the retry policy.
32+
*/
33+
struct ProjectRetryPolicyOption {
34+
using Type = std::shared_ptr<ProjectRetryPolicy>;
35+
};
36+
37+
/**
38+
* Use with `google::cloud::Options` to configure the backoff policy.
39+
*/
40+
struct ProjectBackoffPolicyOption {
41+
using Type = std::shared_ptr<BackoffPolicy>;
42+
};
43+
44+
/**
45+
* Use with `google::cloud::Options` to configure which operations are retried.
46+
*/
47+
struct ProjectIdempotencyPolicyOption {
48+
using Type = std::shared_ptr<ProjectIdempotencyPolicy>;
49+
};
50+
51+
/**
52+
* Use with `google::cloud::Options` to configure the connection pool size for
53+
* rest client.
54+
*/
55+
struct ProjectConnectionPoolSizeOption {
56+
using Type = std::size_t;
57+
};
58+
59+
/**
60+
* The options applicable to Project.
61+
*/
62+
using ProjectPolicyOptionList =
63+
OptionList<ProjectRetryPolicyOption, ProjectBackoffPolicyOption,
64+
ProjectIdempotencyPolicyOption, ProjectConnectionPoolSizeOption>;
65+
66+
/**
67+
* Default options for Project.
68+
*/
69+
Options ProjectDefaultOptions(Options options);
70+
71+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
72+
} // namespace bigquery_v2_minimal_internal
73+
} // namespace cloud
74+
} // namespace google
75+
76+
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGQUERY_V2_MINIMAL_INTERNAL_PROJECT_OPTIONS_H
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "google/cloud/bigquery/v2/minimal/internal/project_options.h"
16+
#include "google/cloud/common_options.h"
17+
#include "google/cloud/testing_util/status_matchers.h"
18+
#include <gmock/gmock.h>
19+
20+
namespace google {
21+
namespace cloud {
22+
namespace bigquery_v2_minimal_internal {
23+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
24+
25+
TEST(ProjectOptionsTest, DefaultOptions) {
26+
Options opts;
27+
auto actual = ProjectDefaultOptions(std::move(opts));
28+
auto expected_retry =
29+
ProjectLimitedTimeRetryPolicy(std::chrono::minutes(30)).clone();
30+
auto expected_backoff = ExponentialBackoffPolicy(
31+
std::chrono::seconds(1), std::chrono::minutes(5), 2.0);
32+
auto expected_idempotency = Idempotency::kIdempotent;
33+
auto const* default_endpoint = "bigquery.googleapis.com";
34+
35+
EXPECT_TRUE(actual.has<EndpointOption>());
36+
EXPECT_EQ(actual.get<EndpointOption>(), default_endpoint);
37+
38+
EXPECT_TRUE(actual.has<AuthorityOption>());
39+
EXPECT_EQ(actual.get<AuthorityOption>(), default_endpoint);
40+
41+
ListProjectsRequest request;
42+
EXPECT_TRUE(actual.has<ProjectIdempotencyPolicyOption>());
43+
EXPECT_EQ(actual.get<ProjectIdempotencyPolicyOption>()->ListProjects(request),
44+
expected_idempotency);
45+
46+
EXPECT_TRUE(actual.has<ProjectRetryPolicyOption>());
47+
EXPECT_EQ(actual.get<ProjectRetryPolicyOption>()->IsExhausted(),
48+
expected_retry->IsExhausted());
49+
50+
EXPECT_TRUE(actual.has<ProjectBackoffPolicyOption>());
51+
52+
EXPECT_GT(actual.get<ProjectConnectionPoolSizeOption>(), 0);
53+
}
54+
55+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
56+
} // namespace bigquery_v2_minimal_internal
57+
} // namespace cloud
58+
} // namespace google

0 commit comments

Comments
 (0)