Skip to content

Commit 13ab195

Browse files
committed
more refactoring
1 parent cb3b154 commit 13ab195

4 files changed

Lines changed: 69 additions & 49 deletions

File tree

google/cloud/storage/internal/bucket_metadata_cache.cc

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

1515
#include "google/cloud/storage/internal/bucket_metadata_cache.h"
16+
#include "google/cloud/storage/bucket_metadata.h"
1617
#include <mutex>
1718
#include <utility>
1819

@@ -21,6 +22,18 @@ namespace cloud {
2122
namespace storage_internal {
2223
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
2324

25+
BucketCacheEntry BucketCacheEntry::FromMetadata(
26+
storage::BucketMetadata const& m) {
27+
std::string loc = m.location();
28+
if (m.location_type() == "multi-region" ||
29+
m.location_type() == "dual-region") {
30+
loc = "global";
31+
}
32+
return {
33+
"projects/" + std::to_string(m.project_number()) + "/buckets/" + m.name(),
34+
std::move(loc)};
35+
}
36+
2437
void BucketMetadataCache::MoveToFront(std::list<std::string>::iterator it) {
2538
list_.splice(list_.begin(), list_, it);
2639
}

google/cloud/storage/internal/bucket_metadata_cache.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,19 @@
2727

2828
namespace google {
2929
namespace cloud {
30+
namespace storage {
31+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
32+
class BucketMetadata;
33+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
34+
} // namespace storage
3035
namespace storage_internal {
3136
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
3237

3338
struct BucketCacheEntry {
3439
std::string id;
3540
std::string location;
41+
42+
static BucketCacheEntry FromMetadata(storage::BucketMetadata const& m);
3643
};
3744

3845
class BucketMetadataCache {

google/cloud/storage/internal/tracing_connection.cc

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ void TracingConnection::CleanupCompletedTasks() {
5656
bg_tasks_.end());
5757
}
5858

59+
void TracingConnection::EnrichSpan(opentelemetry::trace::Span& span,
60+
BucketCacheEntry const& entry) {
61+
span.SetAttribute("gcp.resource.destination.id", entry.id);
62+
span.SetAttribute("gcp.resource.destination.location", entry.location);
63+
}
64+
5965
void TracingConnection::MaybeTriggerBackgroundFetch(
6066
std::string const& bucket_name) {
6167
CleanupCompletedTasks();
@@ -65,30 +71,20 @@ void TracingConnection::MaybeTriggerBackgroundFetch(
6571
}
6672

6773
auto current_options = google::cloud::internal::SaveCurrentOptions();
68-
auto f =
69-
std::async(std::launch::async, [this, bucket_name, current_options]() {
70-
google::cloud::internal::OptionsSpan span(current_options);
71-
storage::internal::GetBucketMetadataRequest request(bucket_name);
72-
auto result = impl_->GetBucketMetadata(request);
73-
74-
BucketCacheEntry entry;
75-
if (result.ok()) {
76-
entry.id = "projects/" + std::to_string(result->project_number()) +
77-
"/buckets/" + result->name();
78-
entry.location = result->location();
79-
if (result->location_type() == "multi-region" ||
80-
result->location_type() == "dual-region") {
81-
entry.location = "global";
82-
}
83-
cache().Put(bucket_name, std::move(entry));
84-
} else if (result.status().code() == StatusCode::kPermissionDenied) {
85-
entry.id = "projects/_/buckets/" + bucket_name;
86-
entry.location = "global";
87-
cache().Put(bucket_name, std::move(entry));
88-
}
89-
90-
cache().EndFetch(bucket_name);
91-
});
74+
auto f = std::async(std::launch::async, [this, bucket_name,
75+
current_options]() {
76+
google::cloud::internal::OptionsSpan span(current_options);
77+
storage::internal::GetBucketMetadataRequest request(bucket_name);
78+
auto result = impl_->GetBucketMetadata(request);
79+
80+
if (result.ok()) {
81+
cache().Put(bucket_name, BucketCacheEntry::FromMetadata(*result));
82+
} else if (result.status().code() == StatusCode::kPermissionDenied) {
83+
cache().Put(bucket_name, {"projects/_/buckets/" + bucket_name, "global"});
84+
}
85+
86+
cache().EndFetch(bucket_name);
87+
});
9288

9389
bg_tasks_.push_back(std::move(f));
9490
}
@@ -98,27 +94,17 @@ void TracingConnection::EnrichSpan(opentelemetry::trace::Span& span,
9894
if (bucket_name.empty()) return;
9995
auto entry = cache().Get(bucket_name);
10096
if (entry.has_value()) {
101-
span.SetAttribute("gcp.resource.destination.id", entry->id);
102-
span.SetAttribute("gcp.resource.destination.location", entry->location);
97+
EnrichSpan(span, *entry);
10398
} else {
10499
MaybeTriggerBackgroundFetch(bucket_name);
105100
}
106101
}
107102

108103
void TracingConnection::EnrichSpan(opentelemetry::trace::Span& span,
109104
storage::BucketMetadata const& metadata) {
110-
std::string id = "projects/" + std::to_string(metadata.project_number()) +
111-
"/buckets/" + metadata.name();
112-
std::string location = metadata.location();
113-
if (metadata.location_type() == "multi-region" ||
114-
metadata.location_type() == "dual-region") {
115-
location = "global";
116-
}
117-
span.SetAttribute("gcp.resource.destination.id", id);
118-
span.SetAttribute("gcp.resource.destination.location", location);
119-
120-
// Populate cache since we have metadata!
121-
cache().Put(metadata.name(), {id, location});
105+
auto entry = BucketCacheEntry::FromMetadata(metadata);
106+
EnrichSpan(span, entry);
107+
cache().Put(metadata.name(), std::move(entry));
122108
}
123109

124110
StatusOr<storage::internal::ListBucketsResponse> TracingConnection::ListBuckets(
@@ -143,7 +129,11 @@ StatusOr<storage::BucketMetadata> TracingConnection::GetBucketMetadata(
143129
auto span = internal::MakeSpan("storage::Client::GetBucketMetadata");
144130
auto scope = opentelemetry::trace::Scope(span);
145131
auto result = impl_->GetBucketMetadata(request);
146-
if (result.ok()) EnrichSpan(*span, *result);
132+
if (result.ok()) {
133+
EnrichSpan(*span, *result);
134+
} else {
135+
MaybeInvalidate(result, request.bucket_name());
136+
}
147137
return internal::EndSpan(*span, std::move(result));
148138
}
149139

@@ -153,7 +143,9 @@ StatusOr<storage::internal::EmptyResponse> TracingConnection::DeleteBucket(
153143
auto scope = opentelemetry::trace::Scope(span);
154144
EnrichSpan(*span, request.bucket_name());
155145
auto result = impl_->DeleteBucket(request);
156-
MaybeInvalidate(result, request.bucket_name());
146+
if (result.ok() || result.status().code() == StatusCode::kNotFound) {
147+
cache().Invalidate(request.bucket_name());
148+
}
157149
return internal::EndSpan(*span, std::move(result));
158150
}
159151

@@ -162,7 +154,11 @@ StatusOr<storage::BucketMetadata> TracingConnection::UpdateBucket(
162154
auto span = internal::MakeSpan("storage::Client::UpdateBucket");
163155
auto scope = opentelemetry::trace::Scope(span);
164156
auto result = impl_->UpdateBucket(request);
165-
if (result.ok()) EnrichSpan(*span, *result);
157+
if (result.ok()) {
158+
EnrichSpan(*span, *result);
159+
} else {
160+
MaybeInvalidate(result, request.metadata().name());
161+
}
166162
return internal::EndSpan(*span, std::move(result));
167163
}
168164

@@ -171,7 +167,11 @@ StatusOr<storage::BucketMetadata> TracingConnection::PatchBucket(
171167
auto span = internal::MakeSpan("storage::Client::PatchBucket");
172168
auto scope = opentelemetry::trace::Scope(span);
173169
auto result = impl_->PatchBucket(request);
174-
if (result.ok()) EnrichSpan(*span, *result);
170+
if (result.ok()) {
171+
EnrichSpan(*span, *result);
172+
} else {
173+
MaybeInvalidate(result, request.bucket());
174+
}
175175
return internal::EndSpan(*span, std::move(result));
176176
}
177177

google/cloud/storage/internal/tracing_connection.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,22 +187,22 @@ class TracingConnection : public storage::internal::StorageConnection {
187187
std::string const& bucket_name);
188188
static void EnrichSpan(opentelemetry::trace::Span& span,
189189
storage::BucketMetadata const& metadata);
190+
static void EnrichSpan(opentelemetry::trace::Span& span,
191+
BucketCacheEntry const& entry);
190192
void MaybeTriggerBackgroundFetch(std::string const& bucket_name);
191193
void CleanupCompletedTasks();
192194

193-
template <typename T>
194-
static void MaybeInvalidate(StatusOr<T> const& result,
195+
static void MaybeInvalidate(Status const& status,
195196
std::string const& bucket_name) {
196-
if (!result.ok() && result.status().code() == StatusCode::kNotFound) {
197+
if (!status.ok() && status.code() == StatusCode::kNotFound) {
197198
cache().Invalidate(bucket_name);
198199
}
199200
}
200201

201-
static void MaybeInvalidate(Status const& status,
202+
template <typename T>
203+
static void MaybeInvalidate(StatusOr<T> const& result,
202204
std::string const& bucket_name) {
203-
if (!status.ok() && status.code() == StatusCode::kNotFound) {
204-
cache().Invalidate(bucket_name);
205-
}
205+
MaybeInvalidate(result.status(), bucket_name);
206206
}
207207

208208
static BucketMetadataCache& cache();

0 commit comments

Comments
 (0)