Skip to content

Commit 6956cc3

Browse files
robot-clickhouseleshikus
authored andcommitted
Backport #127673 to 26.3: Throw an exception instead of aborting when an Azure endpoint omits the ETag header
1 parent 066b687 commit 6956cc3

5 files changed

Lines changed: 121 additions & 3 deletions

File tree

‎src/Disks/DiskObjectStorage/ObjectStorages/AzureBlobStorage/AzureBlobStorageCommon.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ namespace ServerSetting
8888

8989
namespace ErrorCodes
9090
{
91+
extern const int AZURE_BLOB_STORAGE_ERROR;
9192
extern const int BAD_ARGUMENTS;
9293
extern const int LOGICAL_ERROR;
9394
}
@@ -359,6 +360,13 @@ std::unique_ptr<ContainerClient> getContainerClient(const ConnectionParams & par
359360
}
360361
}
361362

363+
String getETag(const Azure::ETag & etag, const String & blob_path)
364+
{
365+
if (!etag.HasValue())
366+
throw Exception(ErrorCodes::AZURE_BLOB_STORAGE_ERROR, "Azure Blob Storage endpoint did not return an ETag for blob {}", blob_path);
367+
return etag.ToString();
368+
}
369+
362370
AuthMethod getAuthMethod(const Poco::Util::AbstractConfiguration & config, const String & config_prefix)
363371
{
364372
if (config.has(config_prefix + ".account_key") && config.has(config_prefix + ".account_name"))

‎src/Disks/DiskObjectStorage/ObjectStorages/AzureBlobStorage/AzureBlobStorageCommon.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ BlobClientOptions getClientOptions(
163163

164164
AuthMethod getAuthMethod(const Poco::Util::AbstractConfiguration & config, const String & config_prefix);
165165

166+
/// `ETag` is an optional response header, and `Azure::ETag::ToString` aborts the process when the tag is absent
167+
/// (also in release builds). Use this function instead: it throws an exception when the endpoint did not send the tag.
168+
String getETag(const Azure::ETag & etag, const String & blob_path);
169+
166170
#endif
167171

168172
Endpoint processEndpoint(const Poco::Util::AbstractConfiguration & config, const String & config_prefix);

‎src/Disks/DiskObjectStorage/ObjectStorages/AzureBlobStorage/AzureObjectStorage.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class AzureIteratorAsync final : public IObjectStorageIteratorAsync
100100
.last_modified = Poco::Timestamp::fromEpochTime(
101101
std::chrono::duration_cast<std::chrono::seconds>(
102102
static_cast<std::chrono::system_clock::time_point>(blob.Details.LastModified).time_since_epoch()).count()),
103-
.etag = blob.Details.ETag.ToString(),
103+
.etag = AzureBlobStorage::getETag(blob.Details.ETag, blob.Name),
104104
.tags = {},
105105
.attributes = {},
106106
}));
@@ -209,7 +209,7 @@ void AzureObjectStorage::listObjects(const std::string & path, RelativePathsWith
209209
.last_modified = Poco::Timestamp::fromEpochTime(
210210
std::chrono::duration_cast<std::chrono::seconds>(
211211
static_cast<std::chrono::system_clock::time_point>(blob.Details.LastModified).time_since_epoch()).count()),
212-
.etag = blob.Details.ETag.ToString(),
212+
.etag = AzureBlobStorage::getETag(blob.Details.ETag, blob.Name),
213213
.tags = {},
214214
.attributes = {},
215215
}));

‎src/Disks/IO/ReadBufferFromAzureBlobStorage.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ void ReadBufferFromAzureBlobStorage::setMetadataFromResponse(const Azure::Storag
431431
{
432432
ObjectMetadata new_metadata;
433433
new_metadata.size_bytes = blob_size;
434-
new_metadata.etag = details.ETag.ToString();
434+
new_metadata.etag = AzureBlobStorage::getETag(details.ETag, path);
435435
new_metadata.last_modified = static_cast<std::chrono::system_clock::time_point>(details.LastModified).time_since_epoch().count();
436436
if (!details.Metadata.empty())
437437
{
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#include "config.h"
2+
3+
#if USE_AZURE_BLOB_STORAGE
4+
5+
#include <algorithm>
6+
#include <cstring>
7+
#include <memory>
8+
#include <optional>
9+
#include <string>
10+
#include <vector>
11+
12+
#include <Core/Defines.h>
13+
#include <Disks/DiskObjectStorage/ObjectStorages/AzureBlobStorage/AzureBlobStorageCommon.h>
14+
#include <Disks/IO/ReadBufferFromAzureBlobStorage.h>
15+
#include <IO/ReadHelpers.h>
16+
17+
#include <azure/core/http/raw_response.hpp>
18+
#include <azure/core/http/transport.hpp>
19+
#include <azure/core/io/body_stream.hpp>
20+
#include <azure/storage/blobs/blob_container_client.hpp>
21+
22+
#include <gtest/gtest.h>
23+
24+
namespace DB::ErrorCodes
25+
{
26+
extern const int AZURE_BLOB_STORAGE_ERROR;
27+
}
28+
29+
namespace
30+
{
31+
32+
class OwningBodyStream : public Azure::Core::IO::BodyStream
33+
{
34+
public:
35+
explicit OwningBodyStream(std::vector<uint8_t> data_) : data(std::move(data_)) { }
36+
37+
int64_t Length() const override { return static_cast<int64_t>(data.size()); }
38+
39+
void Rewind() override { position = 0; }
40+
41+
private:
42+
size_t OnRead(uint8_t * buffer, size_t count, const Azure::Core::Context &) override
43+
{
44+
const size_t to_read = std::min(count, data.size() - position);
45+
if (to_read != 0)
46+
memcpy(buffer, data.data() + position, to_read);
47+
position += to_read;
48+
return to_read;
49+
}
50+
51+
std::vector<uint8_t> data;
52+
size_t position = 0;
53+
};
54+
55+
/// A fake Azure endpoint that serves a 10-byte blob in a response without the optional `ETag` header.
56+
class EndpointWithoutETag : public Azure::Core::Http::HttpTransport
57+
{
58+
public:
59+
std::unique_ptr<Azure::Core::Http::RawResponse> Send(Azure::Core::Http::Request &, const Azure::Core::Context &) override
60+
{
61+
constexpr size_t blob_size = 10;
62+
auto response = std::make_unique<Azure::Core::Http::RawResponse>(
63+
1, 1, Azure::Core::Http::HttpStatusCode::PartialContent, "Partial Content");
64+
response->SetHeader("Content-Length", std::to_string(blob_size));
65+
response->SetHeader("Content-Range", "bytes 0-" + std::to_string(blob_size - 1) + "/" + std::to_string(blob_size));
66+
response->SetHeader("Last-Modified", "Wed, 21 Oct 2015 07:28:00 GMT");
67+
response->SetHeader("x-ms-blob-type", "BlockBlob");
68+
response->SetBodyStream(std::make_unique<OwningBodyStream>(std::vector<uint8_t>(blob_size, 'a')));
69+
return response;
70+
}
71+
};
72+
73+
}
74+
75+
/// `Azure::ETag::ToString` aborts the process on an absent tag, also in release builds.
76+
/// A download response without `ETag` must fail the read with an exception instead.
77+
TEST(AzureReadBuffer, MissingETagIsAnException)
78+
{
79+
Azure::Storage::Blobs::BlobClientOptions client_options;
80+
client_options.Retry.MaxRetries = 0;
81+
client_options.Transport.Transport = std::make_shared<EndpointWithoutETag>();
82+
83+
auto container_client = std::make_shared<const DB::AzureBlobStorage::ContainerClient>(
84+
Azure::Storage::Blobs::BlobContainerClient("http://azure.invalid/container", client_options), /* blob_prefix */ "");
85+
86+
DB::ReadBufferFromAzureBlobStorage buffer(
87+
container_client,
88+
"blob",
89+
DB::ReadSettings{},
90+
/* max_single_read_retries */ 1,
91+
/* max_single_download_retries */ 1);
92+
93+
std::optional<int> error_code;
94+
try
95+
{
96+
std::string data;
97+
DB::readStringUntilEOF(data, buffer);
98+
}
99+
catch (const DB::Exception & e)
100+
{
101+
error_code = e.code();
102+
}
103+
ASSERT_EQ(error_code, std::optional<int>(DB::ErrorCodes::AZURE_BLOB_STORAGE_ERROR));
104+
}
105+
106+
#endif

0 commit comments

Comments
 (0)