Skip to content

Commit

Permalink
Merge pull request #16 from scotthart/add_default_policy_options
Browse files Browse the repository at this point in the history
impl: add policy option defaults
  • Loading branch information
scotthart authored Jan 14, 2025
2 parents 1b04d10 + e584a7e commit 62fadfd
Show file tree
Hide file tree
Showing 9 changed files with 459 additions and 3 deletions.
11 changes: 8 additions & 3 deletions google/cloud/bigquery_unified/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,15 @@ set(bigquery_unified_library_files
client.h
connection.cc
connection.h
idempotency_policy.cc
idempotency_policy.h
internal/connection_impl.cc
internal/connection_impl.h
internal/default_options.cc
internal/default_options.h
read_arrow_response.h)
job_options.h
read_arrow_response.h
retry_policy.h)

set(bigquery_unified_deps
# cmake-format: sort
Expand Down Expand Up @@ -220,8 +224,9 @@ function (bigquery_unified_client_define_tests)
# googletest itself), because the generic `FindGTest` module does not define
# the GTest::gmock target, and the target names are also weird.
find_package(GTest CONFIG REQUIRED)
set(bigquery_unified_client_unit_tests # cmake-format: sort
client_test.cc)
set(bigquery_unified_client_unit_tests
# cmake-format: sort
client_test.cc internal/default_options_test.cc)

# Export the list of unit tests to a .bzl file so we do not need to maintain
# the list in two places.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@

bigquery_unified_client_unit_tests = [
"client_test.cc",
"internal/default_options_test.cc",
]
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@
google_cloud_cpp_bigquery_bigquery_unified_hdrs = [
"client.h",
"connection.h",
"idempotency_policy.h",
"internal/connection_impl.h",
"internal/default_options.h",
"job_options.h",
"read_arrow_response.h",
"retry_policy.h",
]

google_cloud_cpp_bigquery_bigquery_unified_srcs = [
"client.cc",
"connection.cc",
"idempotency_policy.cc",
"internal/connection_impl.cc",
"internal/default_options.cc",
]
85 changes: 85 additions & 0 deletions google/cloud/bigquery_unified/idempotency_policy.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2025 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_unified/idempotency_policy.h"
#include "google/cloud/bigquery_unified/version.h"

namespace google::cloud::bigquery_unified {
GOOGLE_CLOUD_CPP_BIGQUERY_INLINE_NAMESPACE_BEGIN
using ::google::cloud::Idempotency;

/// Create a new copy of this object.
std::unique_ptr<IdempotencyPolicy> IdempotencyPolicy::clone() const {
return std::make_unique<IdempotencyPolicy>(*this);
}

Idempotency IdempotencyPolicy::CancelJob(
google::cloud::bigquery::v2::CancelJobRequest const&, Options) {
return Idempotency::kIdempotent;
}

Idempotency IdempotencyPolicy::CancelJob(
google::cloud::NoAwaitTag,
google::cloud::bigquery::v2::CancelJobRequest const&, Options) {
return Idempotency::kIdempotent;
}

Idempotency IdempotencyPolicy::CancelJob(
google::cloud::bigquery::v2::JobReference const&, Options) {
return Idempotency::kIdempotent;
}

Idempotency IdempotencyPolicy::GetJob(
google::cloud::bigquery::v2::GetJobRequest const&, Options) {
return Idempotency::kIdempotent;
}

Idempotency IdempotencyPolicy::DeleteJob(
google::cloud::bigquery::v2::DeleteJobRequest const&, Options) {
return Idempotency::kIdempotent;
}

Idempotency IdempotencyPolicy::InsertJob(
google::cloud::bigquery::v2::InsertJobRequest const&, Options) {
return Idempotency::kNonIdempotent;
}

Idempotency IdempotencyPolicy::InsertJob(
google::cloud::NoAwaitTag,
google::cloud::bigquery::v2::InsertJobRequest const&, Options) {
return Idempotency::kNonIdempotent;
}

Idempotency IdempotencyPolicy::InsertJob(
google::cloud::bigquery::v2::JobReference const&, Options) {
return Idempotency::kNonIdempotent;
}

Idempotency IdempotencyPolicy::ListJobs(
google::cloud::bigquery::v2::ListJobsRequest, Options) {
return Idempotency::kIdempotent;
}

Idempotency IdempotencyPolicy::ReadArrow(
google::cloud::bigquery::storage::v1::CreateReadSessionRequest const&,
Options) {
return Idempotency::kIdempotent;
}

std::unique_ptr<IdempotencyPolicy> MakeDefaultIdempotencyPolicy() {
return std::make_unique<IdempotencyPolicy>();
}

GOOGLE_CLOUD_CPP_BIGQUERY_INLINE_NAMESPACE_END
} // namespace google::cloud::bigquery_unified
81 changes: 81 additions & 0 deletions google/cloud/bigquery_unified/idempotency_policy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2025 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_BIGQUERY_GOOGLE_CLOUD_BIGQUERY_UNIFIED_IDEMPOTENCY_POLICY_H
#define GOOGLE_CLOUD_CPP_BIGQUERY_GOOGLE_CLOUD_BIGQUERY_UNIFIED_IDEMPOTENCY_POLICY_H

#include "google/cloud/bigquery/storage/v1/bigquery_read_connection.h"
#include "google/cloud/bigquery_unified/version.h"
#include "google/cloud/bigquerycontrol/v2/job_connection.h"
#include "google/cloud/idempotency.h"
#include "google/cloud/no_await_tag.h"

namespace google::cloud::bigquery_unified {
GOOGLE_CLOUD_CPP_BIGQUERY_INLINE_NAMESPACE_BEGIN

class IdempotencyPolicy {
public:
virtual ~IdempotencyPolicy() = default;

/// Create a new copy of this object.
virtual std::unique_ptr<IdempotencyPolicy> clone() const;

virtual google::cloud::Idempotency CancelJob(
google::cloud::bigquery::v2::CancelJobRequest const& request,
Options opts);

virtual google::cloud::Idempotency CancelJob(
google::cloud::NoAwaitTag no_await_tag,
google::cloud::bigquery::v2::CancelJobRequest const& request,
Options opts);

virtual google::cloud::Idempotency CancelJob(
google::cloud::bigquery::v2::JobReference const& job_reference,
Options opts);

virtual google::cloud::Idempotency DeleteJob(
google::cloud::bigquery::v2::DeleteJobRequest const& request,
Options opts);

virtual google::cloud::Idempotency GetJob(
google::cloud::bigquery::v2::GetJobRequest const& request, Options opts);

virtual google::cloud::Idempotency InsertJob(
google::cloud::bigquery::v2::InsertJobRequest const& request,
Options opts);

virtual google::cloud::Idempotency InsertJob(
google::cloud::NoAwaitTag,
google::cloud::bigquery::v2::InsertJobRequest const& request,
Options opts);

virtual google::cloud::Idempotency InsertJob(
google::cloud::bigquery::v2::JobReference const& job_reference,
Options opts);

virtual google::cloud::Idempotency ListJobs(
google::cloud::bigquery::v2::ListJobsRequest request, Options opts);

virtual google::cloud::Idempotency ReadArrow(
google::cloud::bigquery::storage::v1::CreateReadSessionRequest const&
read_session,
Options opts);
};

std::unique_ptr<IdempotencyPolicy> MakeDefaultIdempotencyPolicy();

GOOGLE_CLOUD_CPP_BIGQUERY_INLINE_NAMESPACE_END
} // namespace google::cloud::bigquery_unified

#endif // GOOGLE_CLOUD_CPP_BIGQUERY_GOOGLE_CLOUD_BIGQUERY_UNIFIED_IDEMPOTENCY_POLICY_H
36 changes: 36 additions & 0 deletions google/cloud/bigquery_unified/internal/default_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,47 @@
// limitations under the License.

#include "google/cloud/bigquery_unified/internal/default_options.h"
#include "google/cloud/bigquery_unified/idempotency_policy.h"
#include "google/cloud/bigquery_unified/job_options.h"
#include "google/cloud/bigquery_unified/retry_policy.h"
#include "google/cloud/backoff_policy.h"
#include "google/cloud/polling_policy.h"

namespace google::cloud::bigquery_unified_internal {
GOOGLE_CLOUD_CPP_BIGQUERY_INLINE_NAMESPACE_BEGIN

namespace {
auto constexpr kBackoffScaling = 2.0;
} // namespace

google::cloud::Options DefaultOptions(google::cloud::Options options) {
if (!options.has<bigquery_unified::RetryPolicyOption>()) {
options.set<bigquery_unified::RetryPolicyOption>(
bigquery_unified::LimitedTimeRetryPolicy(std::chrono::minutes(30))
.clone());
}
if (!options.has<bigquery_unified::BackoffPolicyOption>()) {
options.set<bigquery_unified::BackoffPolicyOption>(
ExponentialBackoffPolicy(
std::chrono::seconds(0), std::chrono::seconds(1),
std::chrono::minutes(5), kBackoffScaling, kBackoffScaling)
.clone());
}
if (!options.has<bigquery_unified::PollingPolicyOption>()) {
options.set<bigquery_unified::PollingPolicyOption>(
GenericPollingPolicy<bigquery_unified::RetryPolicyOption::Type,
bigquery_unified::BackoffPolicyOption::Type>(
options.get<bigquery_unified::RetryPolicyOption>()->clone(),
ExponentialBackoffPolicy(std::chrono::seconds(1),
std::chrono::minutes(5), kBackoffScaling)
.clone())
.clone());
}
if (!options.has<bigquery_unified::IdempotencyPolicyOption>()) {
options.set<bigquery_unified::IdempotencyPolicyOption>(
bigquery_unified::MakeDefaultIdempotencyPolicy());
}

return options;
}

Expand Down
35 changes: 35 additions & 0 deletions google/cloud/bigquery_unified/internal/default_options_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2025 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_unified/internal/default_options.h"
#include "google/cloud/bigquery_unified/job_options.h"
#include "google/cloud/bigquery_unified/version.h"
#include <gmock/gmock.h>

namespace google::cloud::bigquery_unified_internal {
GOOGLE_CLOUD_CPP_BIGQUERY_INLINE_NAMESPACE_BEGIN
namespace {

TEST(BigQueryUnifiedDefaultOptionsTest, AllOptionsDefaulted) {
auto empty_options = google::cloud::Options{};
auto options_result = DefaultOptions(empty_options);
EXPECT_TRUE(options_result.has<bigquery_unified::RetryPolicyOption>());
EXPECT_TRUE(options_result.has<bigquery_unified::BackoffPolicyOption>());
EXPECT_TRUE(options_result.has<bigquery_unified::PollingPolicyOption>());
EXPECT_TRUE(options_result.has<bigquery_unified::IdempotencyPolicyOption>());
}

} // namespace
GOOGLE_CLOUD_CPP_BIGQUERY_INLINE_NAMESPACE_END
} // namespace google::cloud::bigquery_unified_internal
52 changes: 52 additions & 0 deletions google/cloud/bigquery_unified/job_options.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2025 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_BIGQUERY_GOOGLE_CLOUD_BIGQUERY_UNIFIED_JOB_OPTIONS_H
#define GOOGLE_CLOUD_CPP_BIGQUERY_GOOGLE_CLOUD_BIGQUERY_UNIFIED_JOB_OPTIONS_H

#include "google/cloud/bigquery_unified/idempotency_policy.h"
#include "google/cloud/bigquery_unified/retry_policy.h"
#include "google/cloud/bigquery_unified/version.h"
#include "google/cloud/backoff_policy.h"
#include "google/cloud/options.h"
#include "google/cloud/polling_policy.h"
#include <memory>

namespace google::cloud::bigquery_unified {
GOOGLE_CLOUD_CPP_BIGQUERY_INLINE_NAMESPACE_BEGIN

struct BackoffPolicyOption {
using Type = std::shared_ptr<BackoffPolicy>;
};

struct BillingProjectOption {
using Type = std::string;
};

struct IdempotencyPolicyOption {
using Type = std::shared_ptr<bigquery_unified::IdempotencyPolicy>;
};

struct PollingPolicyOption {
using Type = std::shared_ptr<PollingPolicy>;
};

struct RetryPolicyOption {
using Type = std::shared_ptr<bigquery_unified::RetryPolicy>;
};

GOOGLE_CLOUD_CPP_BIGQUERY_INLINE_NAMESPACE_END
} // namespace google::cloud::bigquery_unified

#endif // GOOGLE_CLOUD_CPP_BIGQUERY_GOOGLE_CLOUD_BIGQUERY_UNIFIED_JOB_OPTIONS_H
Loading

0 comments on commit 62fadfd

Please sign in to comment.