Skip to content

Commit 1b2e1fa

Browse files
authored
cleanup(GCS+gRPC)!: simplify plugin configuration (#8488)
With the new backend for Google Direct Access (aka Direct Path) it is easier to configure gRPC. I also made it possible to configure the plugin using `google::cloud::Options`. Some of these changes are not backwards compatible, but this is an experimental component.
1 parent c6876e2 commit 1b2e1fa

4 files changed

Lines changed: 36 additions & 54 deletions

File tree

google/cloud/storage/grpc_plugin.cc

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,19 @@
1515
#include "google/cloud/storage/grpc_plugin.h"
1616
#include "google/cloud/storage/internal/grpc_client.h"
1717
#include "google/cloud/storage/internal/hybrid_client.h"
18-
#include "google/cloud/internal/getenv.h"
1918

2019
namespace google {
2120
namespace cloud {
2221
namespace storage_experimental {
2322
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
2423

25-
namespace {
26-
absl::optional<std::string> GrpcConfig() {
27-
return google::cloud::internal::GetEnv(
28-
"GOOGLE_CLOUD_CPP_STORAGE_GRPC_CONFIG");
29-
}
30-
} // namespace
31-
3224
google::cloud::storage::Client DefaultGrpcClient(Options opts) {
3325
opts = google::cloud::storage::internal::DefaultOptionsGrpc(std::move(opts));
34-
auto config = GrpcConfig();
35-
if (config.value_or("none") == "none") {
26+
auto config = opts.get<GrpcPluginOption>();
27+
if (config == "none" || config.empty()) {
3628
return google::cloud::storage::Client(std::move(opts));
3729
}
38-
if (config.value_or("") == "metadata") {
30+
if (config == "metadata") {
3931
return storage::internal::ClientImplDetails::CreateClient(
4032
storage::internal::GrpcClient::Create(opts));
4133
}

google/cloud/storage/internal/grpc_client.cc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,26 @@ int DefaultGrpcNumChannels() {
5151
}
5252

5353
Options DefaultOptionsGrpc(Options options) {
54+
using ::google::cloud::internal::GetEnv;
55+
using ::google::cloud::storage_experimental::GrpcPluginOption;
56+
5457
options = DefaultOptionsWithCredentials(std::move(options));
5558
if (!options.has<UnifiedCredentialsOption>() &&
5659
!options.has<GrpcCredentialOption>()) {
5760
options.set<UnifiedCredentialsOption>(
5861
google::cloud::MakeGoogleDefaultCredentials());
5962
}
6063
if (!options.has<EndpointOption>()) {
61-
options.set<EndpointOption>("storage.googleapis.com");
64+
options.set<EndpointOption>(GetEnv("CLOUD_STORAGE_GRPC_ENDPOINT")
65+
.value_or("storage.googleapis.com"));
6266
}
6367
if (!options.has<AuthorityOption>()) {
6468
options.set<AuthorityOption>("storage.googleapis.com");
6569
}
66-
using google::cloud::internal::GetEnv;
67-
auto env = GetEnv("CLOUD_STORAGE_GRPC_ENDPOINT");
68-
if (env.has_value()) options.set<EndpointOption>(*env);
70+
if (!options.has<GrpcPluginOption>()) {
71+
options.set<GrpcPluginOption>(
72+
GetEnv("GOOGLE_CLOUD_CPP_STORAGE_GRPC_CONFIG").value_or("none"));
73+
}
6974
if (GetEnv("CLOUD_STORAGE_EMULATOR_ENDPOINT")) {
7075
// The emulator does not support HTTPS or authentication, use insecure
7176
// (sometimes called "anonymous") credentials, which disable SSL.

google/cloud/storage/internal/grpc_client_test.cc

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

1515
#include "google/cloud/storage/internal/grpc_client.h"
16+
#include "google/cloud/storage/grpc_plugin.h"
1617
#include "google/cloud/storage/testing/mock_storage_stub.h"
1718
#include "google/cloud/credentials.h"
1819
#include "google/cloud/options.h"
20+
#include "google/cloud/testing_util/scoped_environment.h"
1921
#include "google/cloud/testing_util/status_matchers.h"
2022
#include "google/cloud/testing_util/validate_metadata.h"
2123
#include <gmock/gmock.h>
@@ -29,6 +31,8 @@ namespace {
2931

3032
namespace v2 = ::google::storage::v2;
3133
using ::google::cloud::internal::CurrentOptions;
34+
using ::google::cloud::storage_experimental::GrpcPluginOption;
35+
using ::google::cloud::testing_util::ScopedEnvironment;
3236
using ::google::cloud::testing_util::StatusIs;
3337
using ::google::cloud::testing_util::ValidateMetadataFixture;
3438
using ::testing::Pair;
@@ -59,6 +63,24 @@ std::shared_ptr<GrpcClient> CreateTestClient(
5963
Options{}.set<UnifiedCredentialsOption>(MakeInsecureCredentials()));
6064
}
6165

66+
TEST_F(GrpcClientTest, DefaultOptionsGrpcNoEnv) {
67+
auto grpc_config =
68+
ScopedEnvironment("GOOGLE_CLOUD_CPP_STORAGE_GRPC_CONFIG", absl::nullopt);
69+
auto opts = DefaultOptionsGrpc();
70+
EXPECT_EQ(opts.get<GrpcPluginOption>(), "none");
71+
opts = DefaultOptionsGrpc(Options{}.set<GrpcPluginOption>("configured"));
72+
EXPECT_EQ(opts.get<GrpcPluginOption>(), "configured");
73+
}
74+
75+
TEST_F(GrpcClientTest, DefaultOptionsGrpcWithEnv) {
76+
auto grpc_config =
77+
ScopedEnvironment("GOOGLE_CLOUD_CPP_STORAGE_GRPC_CONFIG", "env");
78+
auto opts = DefaultOptionsGrpc();
79+
EXPECT_EQ(opts.get<GrpcPluginOption>(), "env");
80+
opts = DefaultOptionsGrpc(Options{}.set<GrpcPluginOption>("configured"));
81+
EXPECT_EQ(opts.get<GrpcPluginOption>(), "configured");
82+
}
83+
6284
TEST_F(GrpcClientTest, QueryResumableUpload) {
6385
auto mock = std::make_shared<testing::MockStorageStub>();
6486
EXPECT_CALL(*mock, QueryWriteStatus)

google/cloud/storage/internal/storage_stub_factory.cc

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,49 +31,12 @@ namespace storage_internal {
3131
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
3232
namespace {
3333

34-
auto constexpr kDirectPathConfig = R"json({
35-
"loadBalancingConfig": [{
36-
"grpclb": {
37-
"childPolicy": [{
38-
"pick_first": {}
39-
}]
40-
}
41-
}]
42-
})json";
43-
4434
std::shared_ptr<grpc::Channel> CreateGrpcChannel(
4535
google::cloud::internal::GrpcAuthenticationStrategy& auth,
4636
Options const& options, int channel_id) {
4737
grpc::ChannelArguments args;
48-
auto const& config = options.get<storage_experimental::GrpcPluginOption>();
49-
if (config.empty() || config == "default" || config == "none") {
50-
// Just configure for the regular path.
51-
args.SetInt("grpc.channel_id", channel_id);
52-
return auth.CreateChannel(options.get<EndpointOption>(), std::move(args));
53-
}
54-
std::set<absl::string_view> settings = absl::StrSplit(config, ',');
55-
auto const dp = settings.count("dp") != 0 || settings.count("alts") != 0;
56-
if (dp || settings.count("pick-first-lb") != 0) {
57-
args.SetServiceConfigJSON(kDirectPathConfig);
58-
}
59-
if (dp || settings.count("enable-dns-srv-queries") != 0) {
60-
args.SetInt("grpc.dns_enable_srv_queries", 1);
61-
}
62-
if (settings.count("disable-dns-srv-queries") != 0) {
63-
args.SetInt("grpc.dns_enable_srv_queries", 0);
64-
}
65-
if (settings.count("exclusive") != 0) {
66-
args.SetInt("grpc.channel_id", channel_id);
67-
}
68-
if (settings.count("alts") != 0) {
69-
grpc::experimental::AltsCredentialsOptions alts_opts;
70-
return grpc::CreateCustomChannel(
71-
options.get<EndpointOption>(),
72-
grpc::CompositeChannelCredentials(
73-
grpc::experimental::AltsCredentials(alts_opts),
74-
grpc::GoogleComputeEngineCredentials()),
75-
std::move(args));
76-
}
38+
// Just configure for the regular path.
39+
args.SetInt("grpc.channel_id", channel_id);
7740
return auth.CreateChannel(options.get<EndpointOption>(), std::move(args));
7841
}
7942

0 commit comments

Comments
 (0)