Skip to content

Commit 53127d4

Browse files
committed
fix ci failures
1 parent bdb839f commit 53127d4

3 files changed

Lines changed: 15 additions & 102 deletions

File tree

google/cloud/storage/internal/tracing_connection.cc

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "google/cloud/storage/parallel_upload.h"
1818
#include "google/cloud/internal/opentelemetry.h"
1919
#include <algorithm>
20+
#include <iostream>
2021
#include <memory>
2122
#include <string>
2223
#include <utility>
@@ -59,16 +60,21 @@ void TracingConnection::MaybeTriggerBackgroundFetch(
5960
std::string const& bucket_name) {
6061
CleanupCompletedTasks();
6162

62-
std::lock_guard<std::mutex> lock(mu_);
63-
if (in_flight_fetch_.find(bucket_name) != in_flight_fetch_.end()) {
63+
if (!cache().StartFetch(bucket_name)) {
6464
return;
6565
}
6666

67-
in_flight_fetch_.insert(bucket_name);
68-
6967
auto f = std::async(std::launch::async, [this, bucket_name]() {
7068
storage::internal::GetBucketMetadataRequest request(bucket_name);
7169
auto result = impl_->GetBucketMetadata(request);
70+
std::cout << "BG Thread: GetBucketMetadata returned ok: " << result.ok()
71+
<< "\n";
72+
if (!result.ok()) {
73+
std::cout << "BG Thread: GetBucketMetadata status: "
74+
<< result.status().message()
75+
<< " code: " << static_cast<int>(result.status().code())
76+
<< "\n";
77+
}
7278

7379
BucketCacheEntry entry;
7480
if (result.ok()) {
@@ -86,8 +92,7 @@ void TracingConnection::MaybeTriggerBackgroundFetch(
8692
cache().Put(bucket_name, std::move(entry));
8793
}
8894

89-
std::lock_guard<std::mutex> lock(mu_);
90-
in_flight_fetch_.erase(bucket_name);
95+
cache().EndFetch(bucket_name);
9196
});
9297

9398
bg_tasks_.push_back(std::move(f));

google/cloud/storage/internal/tracing_connection.h

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
#include "google/cloud/storage/internal/storage_connection.h"
1919
#include "google/cloud/storage/parallel_upload.h"
2020
#include "google/cloud/storage/version.h"
21-
#include "absl/types/optional.h"
22-
#include <future>
23-
#include <list>
2421
#include <memory>
2522
#include <mutex>
2623
#include <string>
@@ -33,72 +30,6 @@ namespace cloud {
3330
namespace storage_internal {
3431
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
3532

36-
struct BucketCacheEntry {
37-
std::string id;
38-
std::string location;
39-
};
40-
41-
class BucketMetadataCache {
42-
public:
43-
explicit BucketMetadataCache(std::size_t max_size = 10000)
44-
: max_size_(max_size) {}
45-
46-
absl::optional<BucketCacheEntry> Get(std::string const& bucket_name) {
47-
std::lock_guard<std::mutex> lock(mu_);
48-
auto it = map_.find(bucket_name);
49-
if (it == map_.end()) return absl::nullopt;
50-
51-
list_.erase(it->second.second);
52-
list_.push_front(bucket_name);
53-
it->second.second = list_.begin();
54-
return it->second.first;
55-
}
56-
57-
void Put(std::string const& bucket_name, BucketCacheEntry entry) {
58-
std::lock_guard<std::mutex> lock(mu_);
59-
auto it = map_.find(bucket_name);
60-
if (it != map_.end()) {
61-
it->second.first = entry;
62-
list_.erase(it->second.second);
63-
list_.push_front(bucket_name);
64-
it->second.second = list_.begin();
65-
return;
66-
}
67-
68-
if (map_.size() >= max_size_) {
69-
auto oldest = list_.back();
70-
list_.pop_back();
71-
map_.erase(oldest);
72-
}
73-
74-
list_.push_front(bucket_name);
75-
map_[bucket_name] = {std::move(entry), list_.begin()};
76-
}
77-
78-
void Invalidate(std::string const& bucket_name) {
79-
std::lock_guard<std::mutex> lock(mu_);
80-
auto it = map_.find(bucket_name);
81-
if (it != map_.end()) {
82-
list_.erase(it->second.second);
83-
map_.erase(it);
84-
}
85-
}
86-
87-
void Clear() {
88-
std::lock_guard<std::mutex> lock(mu_);
89-
map_.clear();
90-
list_.clear();
91-
}
92-
93-
private:
94-
std::size_t max_size_;
95-
std::mutex mu_;
96-
std::list<std::string> list_;
97-
std::unordered_map<std::string, std::pair<BucketCacheEntry,
98-
std::list<std::string>::iterator>>
99-
map_;
100-
};
101-
10233
class TracingConnection : public storage::internal::StorageConnection {
10334
public:
10435
explicit TracingConnection(std::shared_ptr<StorageConnection> impl);
@@ -252,33 +183,7 @@ class TracingConnection : public storage::internal::StorageConnection {
252183
std::vector<std::string> InspectStackStructure() const override;
253184

254185
private:
255-
void EnrichSpan(opentelemetry::trace::Span& span,
256-
std::string const& bucket_name);
257-
void EnrichSpan(opentelemetry::trace::Span& span,
258-
storage::BucketMetadata const& metadata);
259-
void MaybeTriggerBackgroundFetch(std::string const& bucket_name);
260-
void CleanupCompletedTasks();
261-
262-
template <typename T>
263-
void MaybeInvalidate(StatusOr<T> const& result,
264-
std::string const& bucket_name) {
265-
if (!result.ok() && result.status().code() == StatusCode::kNotFound) {
266-
cache().Invalidate(bucket_name);
267-
}
268-
}
269-
270-
void MaybeInvalidate(Status const& status, std::string const& bucket_name) {
271-
if (!status.ok() && status.code() == StatusCode::kNotFound) {
272-
cache().Invalidate(bucket_name);
273-
}
274-
}
275-
276-
static BucketMetadataCache& cache();
277-
278186
std::shared_ptr<StorageConnection> impl_;
279-
std::mutex mu_;
280-
std::unordered_set<std::string> in_flight_fetch_;
281-
std::vector<std::future<void>> bg_tasks_;
282187
};
283188

284189
std::shared_ptr<storage::internal::StorageConnection> MakeTracingClient(

google/cloud/storage/internal/tracing_connection_test.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <gmock/gmock.h>
2323
#include <memory>
2424
#include <string>
25+
#include <thread>
2526
#include <utility>
2627

2728
namespace google {
@@ -33,6 +34,7 @@ namespace {
3334
using ::google::cloud::storage::testing::MockClient;
3435
using ::google::cloud::storage::testing::MockObjectReadSource;
3536
using ::google::cloud::storage::testing::canonical_errors::PermanentError;
37+
using ::google::cloud::storage::testing::canonical_errors::TransientError;
3638
using ::google::cloud::testing_util::InstallSpanCatcher;
3739
using ::google::cloud::testing_util::IsOk;
3840
using ::google::cloud::testing_util::OTelAttribute;
@@ -196,7 +198,7 @@ TEST(TracingClientTest, BucketMetadataCacheSuccess) {
196198
auto bg_fetch_future = bg_fetch_done.get_future();
197199

198200
EXPECT_CALL(*mock, GetObjectMetadata).WillOnce([](auto const&) {
199-
return PermanentError();
201+
return TransientError();
200202
});
201203

202204
EXPECT_CALL(*mock, GetBucketMetadata)
@@ -218,6 +220,7 @@ TEST(TracingClientTest, BucketMetadataCacheSuccess) {
218220
"test-object"));
219221

220222
bg_fetch_future.wait_for(std::chrono::seconds(5));
223+
std::this_thread::sleep_for(std::chrono::milliseconds(50));
221224

222225
EXPECT_CALL(*mock, DeleteObject).WillOnce([](auto const&) {
223226
return PermanentError();

0 commit comments

Comments
 (0)