2021-09-29 14:44:53 +00:00
|
|
|
#include <Common/config.h>
|
|
|
|
|
|
|
|
#if USE_AZURE_BLOB_STORAGE
|
|
|
|
|
2021-12-16 22:57:08 +00:00
|
|
|
#include <IO/WriteBufferFromAzureBlobStorage.h>
|
2021-12-01 12:31:25 +00:00
|
|
|
#include <Disks/RemoteDisksCommon.h>
|
2021-12-14 12:34:20 +00:00
|
|
|
#include <Common/getRandomASCIIString.h>
|
2022-04-27 15:05:45 +00:00
|
|
|
#include <Common/logger_useful.h>
|
2021-09-29 14:44:53 +00:00
|
|
|
|
2021-10-01 08:32:28 +00:00
|
|
|
|
2021-09-29 14:44:53 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2021-12-16 22:57:08 +00:00
|
|
|
WriteBufferFromAzureBlobStorage::WriteBufferFromAzureBlobStorage(
|
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-11-17 11:28:30 +00:00
|
|
|
size_t 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
|
|
|
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-12-16 22:57:08 +00:00
|
|
|
WriteBufferFromAzureBlobStorage::~WriteBufferFromAzureBlobStorage()
|
2021-11-30 15:44:28 +00:00
|
|
|
{
|
|
|
|
finalize();
|
|
|
|
}
|
|
|
|
|
2021-12-24 12:40:54 +00:00
|
|
|
void WriteBufferFromAzureBlobStorage::finalizeImpl()
|
|
|
|
{
|
|
|
|
const size_t max_tries = 3;
|
|
|
|
for (size_t i = 0; i < max_tries; ++i)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
next();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
catch (const Azure::Core::RequestFailedException & e)
|
|
|
|
{
|
|
|
|
if (i == max_tries - 1)
|
|
|
|
throw;
|
|
|
|
LOG_INFO(&Poco::Logger::get("WriteBufferFromAzureBlobStorage"),
|
2021-12-25 02:56:15 +00:00
|
|
|
"Exception caught during finalizing azure storage write at attempt {}: {}", i + 1, e.Message);
|
2021-12-24 12:40:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-30 15:44:28 +00:00
|
|
|
|
2021-12-16 22:57:08 +00:00
|
|
|
void WriteBufferFromAzureBlobStorage::nextImpl()
|
2021-10-21 10:14:41 +00:00
|
|
|
{
|
2021-10-01 13:41:10 +00:00
|
|
|
if (!offset())
|
|
|
|
return;
|
|
|
|
|
2021-12-08 13:32:00 +00:00
|
|
|
auto * buffer_begin = working_buffer.begin();
|
2021-10-05 12:00:59 +00:00
|
|
|
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
|
|
|
|
2021-10-21 10:14:41 +00:00
|
|
|
size_t read = 0;
|
2021-12-22 11:21:35 +00:00
|
|
|
std::vector<std::string> block_ids;
|
2021-10-21 10:14:41 +00:00
|
|
|
while (read < len)
|
2021-10-08 14:34:40 +00:00
|
|
|
{
|
2021-10-21 10:14:41 +00:00
|
|
|
auto part_len = std::min(len - read, max_single_part_upload_size);
|
2021-10-08 14:34:40 +00:00
|
|
|
|
2021-12-14 12:34:20 +00:00
|
|
|
auto block_id = getRandomASCIIString(64);
|
2021-10-21 10:14:41 +00:00
|
|
|
block_ids.push_back(block_id);
|
2021-10-08 14:34:40 +00:00
|
|
|
|
2021-12-08 13:32:00 +00:00
|
|
|
Azure::Core::IO::MemoryBodyStream tmp_buffer(reinterpret_cast<uint8_t *>(buffer_begin + read), part_len);
|
2021-11-05 15:32:19 +00:00
|
|
|
block_blob_client.StageBlock(block_id, tmp_buffer);
|
2021-10-01 13:41:10 +00:00
|
|
|
|
2021-10-21 10:14:41 +00:00
|
|
|
read += part_len;
|
|
|
|
}
|
2021-10-08 14:34:40 +00:00
|
|
|
|
2021-12-22 11:21:35 +00:00
|
|
|
block_blob_client.CommitBlockList(block_ids);
|
2021-09-29 14:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|