Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use singleton per-region for S3Client #384

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fbpcf/io/cloud_util/CloudFileUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ std::unique_ptr<IFileUploader> getCloudFileUploader(
return std::make_unique<S3FileUploader>(
fbpcf::cloudio::S3Client::getInstance(
fbpcf::aws::S3ClientOption{.region = ref.region})
.getS3Client(),
->getS3Client(),
filePath);
} else {
throw fbpcf::PcfException("Not supported yet.");
Expand Down
41 changes: 38 additions & 3 deletions fbpcf/io/cloud_util/S3Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,47 @@

#include "fbpcf/io/cloud_util/S3Client.h"

#include <string>

#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>

#include <folly/container/F14Map.h>
#include <folly/Synchronized.h>

namespace fbpcf::cloudio {
S3Client& S3Client::getInstance(const fbpcf::aws::S3ClientOption& option) {
static S3Client s3Client(option);
return s3Client;
std::shared_ptr<S3Client> S3Client::getInstance(const fbpcf::aws::S3ClientOption& option) {
/* Due to previous problems, we create a Singleton instance of the S3Client,
* but there's a catch: we need a distinct S3Client for each region, or we
* run into other issues. For that reason, we store this map from string to
* S3Client with the assumption that the keys are region names. Since region
* is optional, we also allow for a default empty string region.
* ***************************** NOT THREAD SAFE ****************************
* NOTE: Significant refactoring is required to make this thread safe
* Downstream usage wants a mutable reference, but a folly::Synchronized
* RWLock will return a const ref to a reader, meaning it's hard to refactor.
* Simply trying to use folly::Synchronized around the map isn't sufficient,
* because we'll leak a reference to an object in the map which is unsafe.
* ***************************** NOT THREAD SAFE ****************************
*/
static folly::Synchronized<folly::F14FastMap<std::string, std::shared_ptr<S3Client>>> m;

std::string defaultStr{};
auto region = option.region.value_or(defaultStr);

m.withWLock([&](auto& clientMap) {
if (clientMap.find(region) == clientMap.end()) {
std::shared_ptr<S3Client> ptr{new S3Client{option}};
clientMap.at(region) = ptr;
}
});

/* You may see this and think, "Hey, the NOT THREAD SAFE warning above is
* outdated, it looks like we fixed it!", but you're wrong. This still does
* not fully solve the problem. Because the downstream consumer takes a
* mutable reference, there's no guarantee that this is thread safe. It's
* better than nothing, but you still shouldn't fully trust this code.
*/
return m.wlock()->at(region);
}
} // namespace fbpcf::cloudio
2 changes: 1 addition & 1 deletion fbpcf/io/cloud_util/S3Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class S3Client {
}

public:
static S3Client& getInstance(const fbpcf::aws::S3ClientOption& option);
static std::shared_ptr<S3Client> getInstance(const fbpcf::aws::S3ClientOption& option);

std::shared_ptr<Aws::S3::S3Client> getS3Client() {
return awsS3Client_;
Expand Down