Skip to content

Commit 62fadfd

Browse files
authored
Merge pull request #16 from scotthart/add_default_policy_options
impl: add policy option defaults
2 parents 1b04d10 + e584a7e commit 62fadfd

File tree

9 files changed

+459
-3
lines changed

9 files changed

+459
-3
lines changed

google/cloud/bigquery_unified/CMakeLists.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,15 @@ set(bigquery_unified_library_files
133133
client.h
134134
connection.cc
135135
connection.h
136+
idempotency_policy.cc
137+
idempotency_policy.h
136138
internal/connection_impl.cc
137139
internal/connection_impl.h
138140
internal/default_options.cc
139141
internal/default_options.h
140-
read_arrow_response.h)
142+
job_options.h
143+
read_arrow_response.h
144+
retry_policy.h)
141145

142146
set(bigquery_unified_deps
143147
# cmake-format: sort
@@ -220,8 +224,9 @@ function (bigquery_unified_client_define_tests)
220224
# googletest itself), because the generic `FindGTest` module does not define
221225
# the GTest::gmock target, and the target names are also weird.
222226
find_package(GTest CONFIG REQUIRED)
223-
set(bigquery_unified_client_unit_tests # cmake-format: sort
224-
client_test.cc)
227+
set(bigquery_unified_client_unit_tests
228+
# cmake-format: sort
229+
client_test.cc internal/default_options_test.cc)
225230

226231
# Export the list of unit tests to a .bzl file so we do not need to maintain
227232
# the list in two places.

google/cloud/bigquery_unified/bigquery_unified_client_unit_tests.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@
1818

1919
bigquery_unified_client_unit_tests = [
2020
"client_test.cc",
21+
"internal/default_options_test.cc",
2122
]

google/cloud/bigquery_unified/google_cloud_cpp_bigquery_bigquery_unified.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@
1919
google_cloud_cpp_bigquery_bigquery_unified_hdrs = [
2020
"client.h",
2121
"connection.h",
22+
"idempotency_policy.h",
2223
"internal/connection_impl.h",
2324
"internal/default_options.h",
25+
"job_options.h",
2426
"read_arrow_response.h",
27+
"retry_policy.h",
2528
]
2629

2730
google_cloud_cpp_bigquery_bigquery_unified_srcs = [
2831
"client.cc",
2932
"connection.cc",
33+
"idempotency_policy.cc",
3034
"internal/connection_impl.cc",
3135
"internal/default_options.cc",
3236
]
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2025 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_unified/idempotency_policy.h"
16+
#include "google/cloud/bigquery_unified/version.h"
17+
18+
namespace google::cloud::bigquery_unified {
19+
GOOGLE_CLOUD_CPP_BIGQUERY_INLINE_NAMESPACE_BEGIN
20+
using ::google::cloud::Idempotency;
21+
22+
/// Create a new copy of this object.
23+
std::unique_ptr<IdempotencyPolicy> IdempotencyPolicy::clone() const {
24+
return std::make_unique<IdempotencyPolicy>(*this);
25+
}
26+
27+
Idempotency IdempotencyPolicy::CancelJob(
28+
google::cloud::bigquery::v2::CancelJobRequest const&, Options) {
29+
return Idempotency::kIdempotent;
30+
}
31+
32+
Idempotency IdempotencyPolicy::CancelJob(
33+
google::cloud::NoAwaitTag,
34+
google::cloud::bigquery::v2::CancelJobRequest const&, Options) {
35+
return Idempotency::kIdempotent;
36+
}
37+
38+
Idempotency IdempotencyPolicy::CancelJob(
39+
google::cloud::bigquery::v2::JobReference const&, Options) {
40+
return Idempotency::kIdempotent;
41+
}
42+
43+
Idempotency IdempotencyPolicy::GetJob(
44+
google::cloud::bigquery::v2::GetJobRequest const&, Options) {
45+
return Idempotency::kIdempotent;
46+
}
47+
48+
Idempotency IdempotencyPolicy::DeleteJob(
49+
google::cloud::bigquery::v2::DeleteJobRequest const&, Options) {
50+
return Idempotency::kIdempotent;
51+
}
52+
53+
Idempotency IdempotencyPolicy::InsertJob(
54+
google::cloud::bigquery::v2::InsertJobRequest const&, Options) {
55+
return Idempotency::kNonIdempotent;
56+
}
57+
58+
Idempotency IdempotencyPolicy::InsertJob(
59+
google::cloud::NoAwaitTag,
60+
google::cloud::bigquery::v2::InsertJobRequest const&, Options) {
61+
return Idempotency::kNonIdempotent;
62+
}
63+
64+
Idempotency IdempotencyPolicy::InsertJob(
65+
google::cloud::bigquery::v2::JobReference const&, Options) {
66+
return Idempotency::kNonIdempotent;
67+
}
68+
69+
Idempotency IdempotencyPolicy::ListJobs(
70+
google::cloud::bigquery::v2::ListJobsRequest, Options) {
71+
return Idempotency::kIdempotent;
72+
}
73+
74+
Idempotency IdempotencyPolicy::ReadArrow(
75+
google::cloud::bigquery::storage::v1::CreateReadSessionRequest const&,
76+
Options) {
77+
return Idempotency::kIdempotent;
78+
}
79+
80+
std::unique_ptr<IdempotencyPolicy> MakeDefaultIdempotencyPolicy() {
81+
return std::make_unique<IdempotencyPolicy>();
82+
}
83+
84+
GOOGLE_CLOUD_CPP_BIGQUERY_INLINE_NAMESPACE_END
85+
} // namespace google::cloud::bigquery_unified
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2025 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_BIGQUERY_GOOGLE_CLOUD_BIGQUERY_UNIFIED_IDEMPOTENCY_POLICY_H
16+
#define GOOGLE_CLOUD_CPP_BIGQUERY_GOOGLE_CLOUD_BIGQUERY_UNIFIED_IDEMPOTENCY_POLICY_H
17+
18+
#include "google/cloud/bigquery/storage/v1/bigquery_read_connection.h"
19+
#include "google/cloud/bigquery_unified/version.h"
20+
#include "google/cloud/bigquerycontrol/v2/job_connection.h"
21+
#include "google/cloud/idempotency.h"
22+
#include "google/cloud/no_await_tag.h"
23+
24+
namespace google::cloud::bigquery_unified {
25+
GOOGLE_CLOUD_CPP_BIGQUERY_INLINE_NAMESPACE_BEGIN
26+
27+
class IdempotencyPolicy {
28+
public:
29+
virtual ~IdempotencyPolicy() = default;
30+
31+
/// Create a new copy of this object.
32+
virtual std::unique_ptr<IdempotencyPolicy> clone() const;
33+
34+
virtual google::cloud::Idempotency CancelJob(
35+
google::cloud::bigquery::v2::CancelJobRequest const& request,
36+
Options opts);
37+
38+
virtual google::cloud::Idempotency CancelJob(
39+
google::cloud::NoAwaitTag no_await_tag,
40+
google::cloud::bigquery::v2::CancelJobRequest const& request,
41+
Options opts);
42+
43+
virtual google::cloud::Idempotency CancelJob(
44+
google::cloud::bigquery::v2::JobReference const& job_reference,
45+
Options opts);
46+
47+
virtual google::cloud::Idempotency DeleteJob(
48+
google::cloud::bigquery::v2::DeleteJobRequest const& request,
49+
Options opts);
50+
51+
virtual google::cloud::Idempotency GetJob(
52+
google::cloud::bigquery::v2::GetJobRequest const& request, Options opts);
53+
54+
virtual google::cloud::Idempotency InsertJob(
55+
google::cloud::bigquery::v2::InsertJobRequest const& request,
56+
Options opts);
57+
58+
virtual google::cloud::Idempotency InsertJob(
59+
google::cloud::NoAwaitTag,
60+
google::cloud::bigquery::v2::InsertJobRequest const& request,
61+
Options opts);
62+
63+
virtual google::cloud::Idempotency InsertJob(
64+
google::cloud::bigquery::v2::JobReference const& job_reference,
65+
Options opts);
66+
67+
virtual google::cloud::Idempotency ListJobs(
68+
google::cloud::bigquery::v2::ListJobsRequest request, Options opts);
69+
70+
virtual google::cloud::Idempotency ReadArrow(
71+
google::cloud::bigquery::storage::v1::CreateReadSessionRequest const&
72+
read_session,
73+
Options opts);
74+
};
75+
76+
std::unique_ptr<IdempotencyPolicy> MakeDefaultIdempotencyPolicy();
77+
78+
GOOGLE_CLOUD_CPP_BIGQUERY_INLINE_NAMESPACE_END
79+
} // namespace google::cloud::bigquery_unified
80+
81+
#endif // GOOGLE_CLOUD_CPP_BIGQUERY_GOOGLE_CLOUD_BIGQUERY_UNIFIED_IDEMPOTENCY_POLICY_H

google/cloud/bigquery_unified/internal/default_options.cc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,47 @@
1313
// limitations under the License.
1414

1515
#include "google/cloud/bigquery_unified/internal/default_options.h"
16+
#include "google/cloud/bigquery_unified/idempotency_policy.h"
17+
#include "google/cloud/bigquery_unified/job_options.h"
18+
#include "google/cloud/bigquery_unified/retry_policy.h"
19+
#include "google/cloud/backoff_policy.h"
20+
#include "google/cloud/polling_policy.h"
1621

1722
namespace google::cloud::bigquery_unified_internal {
1823
GOOGLE_CLOUD_CPP_BIGQUERY_INLINE_NAMESPACE_BEGIN
1924

25+
namespace {
26+
auto constexpr kBackoffScaling = 2.0;
27+
} // namespace
28+
2029
google::cloud::Options DefaultOptions(google::cloud::Options options) {
30+
if (!options.has<bigquery_unified::RetryPolicyOption>()) {
31+
options.set<bigquery_unified::RetryPolicyOption>(
32+
bigquery_unified::LimitedTimeRetryPolicy(std::chrono::minutes(30))
33+
.clone());
34+
}
35+
if (!options.has<bigquery_unified::BackoffPolicyOption>()) {
36+
options.set<bigquery_unified::BackoffPolicyOption>(
37+
ExponentialBackoffPolicy(
38+
std::chrono::seconds(0), std::chrono::seconds(1),
39+
std::chrono::minutes(5), kBackoffScaling, kBackoffScaling)
40+
.clone());
41+
}
42+
if (!options.has<bigquery_unified::PollingPolicyOption>()) {
43+
options.set<bigquery_unified::PollingPolicyOption>(
44+
GenericPollingPolicy<bigquery_unified::RetryPolicyOption::Type,
45+
bigquery_unified::BackoffPolicyOption::Type>(
46+
options.get<bigquery_unified::RetryPolicyOption>()->clone(),
47+
ExponentialBackoffPolicy(std::chrono::seconds(1),
48+
std::chrono::minutes(5), kBackoffScaling)
49+
.clone())
50+
.clone());
51+
}
52+
if (!options.has<bigquery_unified::IdempotencyPolicyOption>()) {
53+
options.set<bigquery_unified::IdempotencyPolicyOption>(
54+
bigquery_unified::MakeDefaultIdempotencyPolicy());
55+
}
56+
2157
return options;
2258
}
2359

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2025 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_unified/internal/default_options.h"
16+
#include "google/cloud/bigquery_unified/job_options.h"
17+
#include "google/cloud/bigquery_unified/version.h"
18+
#include <gmock/gmock.h>
19+
20+
namespace google::cloud::bigquery_unified_internal {
21+
GOOGLE_CLOUD_CPP_BIGQUERY_INLINE_NAMESPACE_BEGIN
22+
namespace {
23+
24+
TEST(BigQueryUnifiedDefaultOptionsTest, AllOptionsDefaulted) {
25+
auto empty_options = google::cloud::Options{};
26+
auto options_result = DefaultOptions(empty_options);
27+
EXPECT_TRUE(options_result.has<bigquery_unified::RetryPolicyOption>());
28+
EXPECT_TRUE(options_result.has<bigquery_unified::BackoffPolicyOption>());
29+
EXPECT_TRUE(options_result.has<bigquery_unified::PollingPolicyOption>());
30+
EXPECT_TRUE(options_result.has<bigquery_unified::IdempotencyPolicyOption>());
31+
}
32+
33+
} // namespace
34+
GOOGLE_CLOUD_CPP_BIGQUERY_INLINE_NAMESPACE_END
35+
} // namespace google::cloud::bigquery_unified_internal
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2025 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_BIGQUERY_GOOGLE_CLOUD_BIGQUERY_UNIFIED_JOB_OPTIONS_H
16+
#define GOOGLE_CLOUD_CPP_BIGQUERY_GOOGLE_CLOUD_BIGQUERY_UNIFIED_JOB_OPTIONS_H
17+
18+
#include "google/cloud/bigquery_unified/idempotency_policy.h"
19+
#include "google/cloud/bigquery_unified/retry_policy.h"
20+
#include "google/cloud/bigquery_unified/version.h"
21+
#include "google/cloud/backoff_policy.h"
22+
#include "google/cloud/options.h"
23+
#include "google/cloud/polling_policy.h"
24+
#include <memory>
25+
26+
namespace google::cloud::bigquery_unified {
27+
GOOGLE_CLOUD_CPP_BIGQUERY_INLINE_NAMESPACE_BEGIN
28+
29+
struct BackoffPolicyOption {
30+
using Type = std::shared_ptr<BackoffPolicy>;
31+
};
32+
33+
struct BillingProjectOption {
34+
using Type = std::string;
35+
};
36+
37+
struct IdempotencyPolicyOption {
38+
using Type = std::shared_ptr<bigquery_unified::IdempotencyPolicy>;
39+
};
40+
41+
struct PollingPolicyOption {
42+
using Type = std::shared_ptr<PollingPolicy>;
43+
};
44+
45+
struct RetryPolicyOption {
46+
using Type = std::shared_ptr<bigquery_unified::RetryPolicy>;
47+
};
48+
49+
GOOGLE_CLOUD_CPP_BIGQUERY_INLINE_NAMESPACE_END
50+
} // namespace google::cloud::bigquery_unified
51+
52+
#endif // GOOGLE_CLOUD_CPP_BIGQUERY_GOOGLE_CLOUD_BIGQUERY_UNIFIED_JOB_OPTIONS_H

0 commit comments

Comments
 (0)