2021-09-29 14:44:53 +00:00
|
|
|
#if !defined(ARCADIA_BUILD)
|
|
|
|
#include <Common/config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if USE_AZURE_BLOB_STORAGE
|
|
|
|
|
|
|
|
#include <IO/WriteBufferFromBlobStorage.h>
|
|
|
|
|
2021-10-01 08:32:28 +00:00
|
|
|
|
2021-09-29 14:44:53 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2021-10-08 14:34:40 +00:00
|
|
|
// TODO: abstract this function from DiskS3.cpp, from where it was copy-pasted
|
|
|
|
String getRandomName(char first = 'a', char last = 'z', size_t len = 64)
|
|
|
|
{
|
|
|
|
std::uniform_int_distribution<int> distribution(first, last);
|
|
|
|
String res(len, ' ');
|
|
|
|
for (auto & c : res)
|
|
|
|
c = distribution(thread_local_rng);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-01 08:32:28 +00:00
|
|
|
WriteBufferFromBlobStorage::WriteBufferFromBlobStorage(
|
2021-10-19 09:30:15 +00:00
|
|
|
std::shared_ptr<Azure::Storage::Blobs::BlobContainerClient> blob_container_client_,
|
2021-10-01 08:32:28 +00:00
|
|
|
const String & blob_path_,
|
2021-10-08 14:34:40 +00:00
|
|
|
UInt64 /* min_upload_part_size_ */,
|
|
|
|
UInt64 max_single_part_upload_size_,
|
2021-10-01 08:32:28 +00:00
|
|
|
size_t buf_size_) :
|
2021-10-01 13:41:10 +00:00
|
|
|
BufferWithOwnMemory<WriteBuffer>(buf_size_, nullptr, 0),
|
2021-10-01 08:32:28 +00:00
|
|
|
blob_container_client(blob_container_client_),
|
2021-10-08 14:34:40 +00:00
|
|
|
// min_upload_part_size(min_upload_part_size_),
|
|
|
|
max_single_part_upload_size(max_single_part_upload_size_),
|
2021-10-06 15:12:24 +00:00
|
|
|
blob_path(blob_path_) {}
|
2021-10-01 13:41:10 +00:00
|
|
|
|
2021-10-01 08:32:28 +00:00
|
|
|
|
2021-09-29 14:44:53 +00:00
|
|
|
void WriteBufferFromBlobStorage::nextImpl() {
|
2021-10-01 13:41:10 +00:00
|
|
|
|
|
|
|
if (!offset())
|
|
|
|
return;
|
|
|
|
|
2021-10-05 12:00:59 +00:00
|
|
|
auto pos = working_buffer.begin();
|
|
|
|
auto len = offset();
|
|
|
|
|
2021-10-19 09:30:15 +00:00
|
|
|
auto block_blob_client = blob_container_client->GetBlockBlobClient(blob_path);
|
2021-10-08 14:34:40 +00:00
|
|
|
|
|
|
|
if (len <= max_single_part_upload_size)
|
|
|
|
{
|
|
|
|
Azure::Core::IO::MemoryBodyStream tmp_buffer(reinterpret_cast<uint8_t *>(pos), len);
|
|
|
|
|
2021-10-19 09:30:15 +00:00
|
|
|
blob_container_client->UploadBlob(blob_path, tmp_buffer);
|
2021-10-08 14:34:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t read = 0;
|
|
|
|
|
|
|
|
while (read < len)
|
|
|
|
{
|
|
|
|
auto part_len = std::min(len - read, max_single_part_upload_size);
|
|
|
|
|
|
|
|
auto block_id = getRandomName();
|
|
|
|
block_ids.push_back(block_id);
|
2021-10-01 13:41:10 +00:00
|
|
|
|
2021-10-08 14:34:40 +00:00
|
|
|
Azure::Core::IO::MemoryBodyStream tmp_buffer(reinterpret_cast<uint8_t *>(pos + read), part_len);
|
|
|
|
block_blob_client.StageBlock(block_ids.back(), tmp_buffer);
|
|
|
|
|
|
|
|
read += part_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
block_blob_client.CommitBlockList(block_ids);
|
|
|
|
}
|
2021-09-29 14:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|