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

Implement and test S3 Sink, with usage in runtime. #278

Merged
merged 8 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

- Support for writing to S3 buckets.
shlomnissan marked this conversation as resolved.
Show resolved Hide resolved

### Changed

- Calling `acquire_get_configuration` with a Zarr storage device now returns a URI of the storage device, with file://
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ add_library(${tgt} MODULE
writers/sink.creator.cpp
writers/file.sink.hh
writers/file.sink.cpp
writers/s3.sink.hh
writers/s3.sink.cpp
writers/writer.hh
writers/writer.cpp
writers/zarrv2.writer.hh
Expand Down
85 changes: 81 additions & 4 deletions src/common/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,16 @@ common::split_uri(const std::string& uri)
auto end = uri.find_first_of(delim);

std::vector<std::string> out;
while (end != std::string::npos) {
while (end <= std::string::npos) {
shlomnissan marked this conversation as resolved.
Show resolved Hide resolved
if (end > begin) {
out.emplace_back(uri.substr(begin, end - begin));
std::string part = uri.substr(begin, end - begin);
if (!part.empty()) {
out.emplace_back(part);
}
}

if (end == std::string::npos) {
break;
}

begin = end + 1;
Expand Down Expand Up @@ -480,6 +487,76 @@ extern "C"

return retval;
}
}

#endif
acquire_export int unit_test__split_uri()
{
try {
auto parts = common::split_uri("s3://bucket/key");
CHECK(parts.size() == 3);
CHECK(parts[0] == "s3:");
CHECK(parts[1] == "bucket");
CHECK(parts[2] == "key");

parts = common::split_uri("s3://bucket/key/");
CHECK(parts.size() == 3);
CHECK(parts[0] == "s3:");
CHECK(parts[1] == "bucket");
CHECK(parts[2] == "key");

parts = common::split_uri("s3://bucket/key/with/slashes");
CHECK(parts.size() == 5);
CHECK(parts[0] == "s3:");
CHECK(parts[1] == "bucket");
CHECK(parts[2] == "key");
CHECK(parts[3] == "with");
CHECK(parts[4] == "slashes");

parts = common::split_uri("s3://bucket");
CHECK(parts.size() == 2);
CHECK(parts[0] == "s3:");
CHECK(parts[1] == "bucket");

parts = common::split_uri("s3://");
CHECK(parts.size() == 1);
CHECK(parts[0] == "s3:");

parts = common::split_uri("s3:///");
CHECK(parts.size() == 1);
CHECK(parts[0] == "s3:");

parts = common::split_uri("s3://bucket/");
CHECK(parts.size() == 2);
CHECK(parts[0] == "s3:");
CHECK(parts[1] == "bucket");

parts = common::split_uri("s3://bucket/");
CHECK(parts.size() == 2);
CHECK(parts[0] == "s3:");
CHECK(parts[1] == "bucket");

parts = common::split_uri("s3://bucket/key/with/slashes/");
CHECK(parts.size() == 5);
CHECK(parts[0] == "s3:");
CHECK(parts[1] == "bucket");
CHECK(parts[2] == "key");
CHECK(parts[3] == "with");
CHECK(parts[4] == "slashes");

parts = common::split_uri("s3://bucket/key/with/slashes//");
CHECK(parts.size() == 5);
CHECK(parts[0] == "s3:");
CHECK(parts[1] == "bucket");
CHECK(parts[2] == "key");
CHECK(parts[3] == "with");
CHECK(parts[4] == "slashes");
return 1;
} catch (const std::exception& exc) {
LOGE("Exception: %s\n", exc.what());
} catch (...) {
LOGE("Exception: (unknown)");
}

return 0;
}
}
#endif
7 changes: 6 additions & 1 deletion src/common/utilities.hh
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ namespace acquire::sink::zarr {

struct Zarr;

namespace common {
enum class ZarrVersion
{
V2 = 2,
shlomnissan marked this conversation as resolved.
Show resolved Hide resolved
V3
};

namespace common {
/// @brief Get the number of chunks along a dimension.
/// @param dimension A dimension.
/// @return The number of, possibly ragged, chunks along the dimension, given
Expand Down
5 changes: 2 additions & 3 deletions src/writers/file.sink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ zarr::FileSink::FileSink(const std::string& uri)
bool
zarr::FileSink::write(size_t offset, const uint8_t* buf, size_t bytes_of_buf)
{
if (!file_) {
return false;
}
CHECK(buf);
shlomnissan marked this conversation as resolved.
Show resolved Hide resolved
CHECK(bytes_of_buf);

return file_write(file_.get(), offset, buf, buf + bytes_of_buf);
}
160 changes: 160 additions & 0 deletions src/writers/s3.sink.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#include "s3.sink.hh"

#include "common/utilities.hh"
#include "logger.h"

#include <miniocpp/client.h>

namespace zarr = acquire::sink::zarr;

zarr::S3Sink::S3Sink(const std::string& bucket_name,
const std::string& object_key,
shlomnissan marked this conversation as resolved.
Show resolved Hide resolved
std::shared_ptr<common::S3ConnectionPool> connection_pool)
: bucket_name_{ bucket_name }
, object_key_{ object_key }
, connection_pool_{ connection_pool }
, buf_(5 << 20, 0) // 5 MiB is the minimum multipart upload size
{
CHECK(!bucket_name_.empty());
CHECK(!object_key_.empty());
CHECK(connection_pool_);
}

zarr::S3Sink::~S3Sink()
{
try {
// upload_id_ is only populated after successfully uploading a part
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding a computed property to determine if the upload was successful, defined in terms of the upload ID, would be good. It will also allow us to remove this comment.

auto is_upload_successful() const { return !upload_id_.empty(); } 

if (upload_id_.empty() && buf_size_ > 0) {
CHECK(put_object_());
} else if (!upload_id_.empty()) {
if (buf_size_ > 0) {
CHECK(flush_part_());
}
CHECK(finalize_multipart_upload_());
}
shlomnissan marked this conversation as resolved.
Show resolved Hide resolved
} catch (const std::exception& exc) {
LOGE("Error: %s", exc.what());
} catch (...) {
LOGE("Error: (unknown)");
}
shlomnissan marked this conversation as resolved.
Show resolved Hide resolved
}

bool
zarr::S3Sink::write(size_t offset, const uint8_t* buf, size_t bytes_of_buf)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's an opportunity to improve this function. Something along the lines of:

auto zarr::S3Sink::write(std::span buffer) -> bool {
    auto bytes_to_write = buffer.size();

    while (bytes_to_write > 0) {
        const auto chunk_size = std::min(bytes_to_write, buf_.size() - bytes_read_);

        if (chunk_size > 0) {
            std::copy_n(buffer.data(), chunk_size, buf_.begin() + bytes_read_);
            bytes_read_ += chunk_size;
            bytes_to_write -= chunk_size;
        }

        if (buf_.size() == data_size_) {
            if (!flush_part_()) return false;
        }
    }
    return true;
}
  • The names buf, buf_, bytes_of_buf, and buf_size_ are very confusing.
  • I don’t think the offset parameter is being used.
  • Use std::copy_n instead of std::copy.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

offset is not being used because this is an override and it is needed for FileSink::write().

{
CHECK(buf);
CHECK(bytes_of_buf);

while (bytes_of_buf > 0) {
const auto n = std::min(bytes_of_buf, buf_.size() - buf_size_);
if (n) {
std::copy(buf, buf + n, buf_.begin() + buf_size_);
buf_size_ += n;
buf += n;
bytes_of_buf -= n;
}

if (buf_size_ == buf_.size()) {
CHECK(flush_part_());
}
}

return true;
shlomnissan marked this conversation as resolved.
Show resolved Hide resolved
}

bool
zarr::S3Sink::put_object_() noexcept
{
if (buf_size_ == 0) {
return false;
}

auto connection = connection_pool_->get_connection();

bool retval = false;
try {
std::span<uint8_t> data(buf_.data(), buf_size_);
std::string etag =
connection->put_object(bucket_name_, object_key_, data);
shlomnissan marked this conversation as resolved.
Show resolved Hide resolved
EXPECT(
!etag.empty(), "Failed to upload object: %s", object_key_.c_str());

retval = true;
} catch (const std::exception& exc) {
LOGE("Error: %s", exc.what());
} catch (...) {
LOGE("Error: (unknown)");
}

// cleanup
connection_pool_->return_connection(std::move(connection));
buf_size_ = 0;

return retval;
}

bool
zarr::S3Sink::flush_part_() noexcept
{
if (buf_size_ == 0) {
return false;
}

auto connection = connection_pool_->get_connection();

bool retval = false;
try {
std::string upload_id = upload_id_;
if (upload_id.empty()) {
upload_id =
connection->create_multipart_object(bucket_name_, object_key_);
EXPECT(!upload_id.empty(),
"Failed to create multipart object: %s",
object_key_.c_str());
}

minio::s3::Part part;
part.number = static_cast<unsigned int>(parts_.size()) + 1;

std::span<uint8_t> data(buf_.data(), buf_size_);
part.etag = connection->upload_multipart_object_part(
bucket_name_, object_key_, upload_id, data, part.number);
EXPECT(!part.etag.empty(),
"Failed to upload part %u of object %s",
part.number,
object_key_.c_str());

// set these only when the part is successfully uploaded
parts_.push_back(part);
upload_id_ = upload_id;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same upload ID applies to all parts of the object, right? So if we generate an upload ID and a part fails, why would we generate another upload ID (or only assign it if it succeeded)? I'm not sure it's meaningful because what if the second part fails -- the upload ID will not change anyway.

We can probably create a computed property:

auto get_upload_id() -> std::string {
    if (upload_id_.empty()) {
        upload_id_ = connection->create_multipart_object(bucket_name_, object_key_);
    }
    return upload_id_;
}

Have create_multipart_object throw an exception. This function already catches and logs, so we're good, and then:

part.etag = connection->upload_multipart_object_part(
          bucket_name_, object_key_, get_upload_id(), data, part.number);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it.


retval = true;
} catch (const std::exception& exc) {
LOGE("Error: %s", exc.what());
} catch (...) {
LOGE("Error: (unknown)");
}

// cleanup
connection_pool_->return_connection(std::move(connection));
buf_size_ = 0;

return retval;
}

bool
zarr::S3Sink::finalize_multipart_upload_() noexcept
{
if (upload_id_.empty()) {
return false;
}

auto connection = connection_pool_->get_connection();

bool retval = connection->complete_multipart_object(
bucket_name_, object_key_, upload_id_, parts_);

connection_pool_->return_connection(std::move(connection));

return retval;
}
49 changes: 49 additions & 0 deletions src/writers/s3.sink.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#pragma once

#include "sink.hh"
#include "platform.h"
#include "common/s3.connection.hh"

#include <miniocpp/types.h>

#include <future>
shlomnissan marked this conversation as resolved.
Show resolved Hide resolved
#include <string>

namespace acquire::sink::zarr {
struct S3Sink final : public Sink
shlomnissan marked this conversation as resolved.
Show resolved Hide resolved
{
S3Sink() = delete;
shlomnissan marked this conversation as resolved.
Show resolved Hide resolved
S3Sink(const std::string& bucket_name,
shlomnissan marked this conversation as resolved.
Show resolved Hide resolved
const std::string& object_key,
std::shared_ptr<common::S3ConnectionPool> connection_pool);
~S3Sink() override;

bool write(size_t offset, const uint8_t* buf, size_t bytes_of_buf) override;
shlomnissan marked this conversation as resolved.
Show resolved Hide resolved

private:
std::string bucket_name_;
std::string object_key_;

std::shared_ptr<common::S3ConnectionPool> connection_pool_;

// multipart upload
std::vector<uint8_t> buf_; // temporary 5MiB buffer for multipart upload
shlomnissan marked this conversation as resolved.
Show resolved Hide resolved
size_t buf_size_ = 0;

std::string upload_id_;
std::list<minio::s3::Part> parts_;
shlomnissan marked this conversation as resolved.
Show resolved Hide resolved

// single-part upload
/// @brief Upload the object to S3.
/// @returns True if the object was successfully uploaded, otherwise false.
[[nodiscard]] bool put_object_() noexcept;
shlomnissan marked this conversation as resolved.
Show resolved Hide resolved

// multipart upload
/// @brief Flush the current part to S3.
/// @returns True if the part was successfully flushed, otherwise false.
[[nodiscard]] bool flush_part_() noexcept;
/// @brief Finalize the multipart upload.
/// @returns True if a multipart upload was successfully finalized, otherwise false.
[[nodiscard]] bool finalize_multipart_upload_() noexcept;
};
} // namespace acquire::sink::zarr
Loading
Loading