-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathas.h
More file actions
577 lines (527 loc) · 20 KB
/
as.h
File metadata and controls
577 lines (527 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
// Copyright 2019-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of NVIDIA CORPORATION nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once
#include <azure/identity/default_azure_credential.hpp>
#include <azure/identity/managed_identity_credential.hpp>
#include <azure/storage/blobs.hpp>
#include <azure/storage/common/storage_credential.hpp>
#include "common.h"
// [WIP] below needed?
#undef LOG_INFO
#undef LOG_WARNING
namespace triton { namespace core {
namespace as = Azure::Storage;
namespace asb = Azure::Storage::Blobs;
const std::string AS_URL_PATTERN = "as://([^/]+)/([^/?]+)(?:/([^?]*))?(\\?.*)?";
/// Supported authentication modes for Azure Storage access.
/// - "key": Shared Key (account key) — default, backwards-compatible.
/// - "managed_identity": Azure Managed Identity (system- or user-assigned).
/// - "default": Azure DefaultAzureCredential chain
/// (environment → managed identity → CLI → etc.).
struct ASCredential {
std::string account_str_;
/// Authentication type: "key" (default), "managed_identity", or "default".
std::string auth_type_;
std::string account_key_;
/// Optional client ID for user-assigned Managed Identity.
std::string client_id_;
ASCredential(); // from env var
ASCredential(triton::common::TritonJson::Value& cred_json);
};
ASCredential::ASCredential()
{
const auto to_str = [](const char* s) -> std::string {
return (s != nullptr ? std::string(s) : "");
};
const char* account_str = std::getenv("AZURE_STORAGE_ACCOUNT");
const char* auth_type = std::getenv("AZURE_STORAGE_AUTH_TYPE");
const char* account_key = std::getenv("AZURE_STORAGE_KEY");
const char* client_id = std::getenv("AZURE_STORAGE_CLIENT_ID");
account_str_ = to_str(account_str);
auth_type_ = to_str(auth_type);
account_key_ = to_str(account_key);
client_id_ = to_str(client_id);
// When no explicit auth type is set, infer from available credentials:
// if an account key is present use "key", otherwise remain empty (which
// the filesystem constructor treats as "key" for backwards compatibility).
if (auth_type_.empty() && !account_key_.empty()) {
auth_type_ = "key";
}
}
ASCredential::ASCredential(triton::common::TritonJson::Value& cred_json)
{
triton::common::TritonJson::Value account_str_json, account_key_json,
auth_type_json, client_id_json;
if (cred_json.Find("account_str", &account_str_json))
account_str_json.AsString(&account_str_);
if (cred_json.Find("auth_type", &auth_type_json))
auth_type_json.AsString(&auth_type_);
if (cred_json.Find("account_key", &account_key_json))
account_key_json.AsString(&account_key_);
if (cred_json.Find("client_id", &client_id_json))
client_id_json.AsString(&client_id_);
}
class ASFileSystem : public FileSystem {
public:
ASFileSystem(const std::string& path, const ASCredential& as_cred);
Status CheckClient();
// unify with S3 interface
Status CheckClient(const std::string& path) { return CheckClient(); }
Status FileExists(const std::string& path, bool* exists) override;
Status IsDirectory(const std::string& path, bool* is_dir) override;
Status FileModificationTime(
const std::string& path, int64_t* mtime_ns) override;
Status GetDirectoryContents(
const std::string& path, std::set<std::string>* contents) override;
Status GetDirectorySubdirs(
const std::string& path, std::set<std::string>* subdirs) override;
Status GetDirectoryFiles(
const std::string& path, std::set<std::string>* files) override;
Status ReadTextFile(const std::string& path, std::string* contents) override;
Status LocalizePath(
const std::string& path,
std::shared_ptr<LocalizedPath>* localized) override;
Status WriteTextFile(
const std::string& path, const std::string& contents) override;
Status WriteBinaryFile(
const std::string& path, const char* contents,
const size_t content_len) override;
Status MakeDirectory(const std::string& dir, const bool recursive) override;
Status MakeTemporaryDirectory(
std::string dir_path, std::string* temp_dir) override;
Status DeletePath(const std::string& path) override;
private:
Status ParsePath(
const std::string& path, std::string* container, std::string* blob);
// 'callback' will be invoked when directory content is received, it may
// be invoked multiple times within the same ListDirectory() call if the
// result is paged.
Status ListDirectory(
const std::string& path, const std::string& dir_path,
std::function<Status(
const std::vector<asb::Models::BlobItem>& blobs,
const std::vector<std::string>& blob_prefixes)>
callback);
Status DownloadFolder(
const std::string& container, const std::string& path,
const std::string& dest);
std::shared_ptr<asb::BlobServiceClient> client_;
re2::RE2 as_regex_;
};
Status
ASFileSystem::ParsePath(
const std::string& path, std::string* container, std::string* blob)
{
std::string host_name, query;
if (!RE2::FullMatch(path, as_regex_, &host_name, container, blob, &query)) {
return Status(
Status::Code::INTERNAL, "Invalid azure storage path: " + path);
}
return Status::Success;
}
ASFileSystem::ASFileSystem(const std::string& path, const ASCredential& as_cred)
: as_regex_(AS_URL_PATTERN)
{
std::string host_name, container, blob_path, query;
if (RE2::FullMatch(
path, as_regex_, &host_name, &container, &blob_path, &query)) {
size_t pos = host_name.rfind(".blob.core.windows.net");
std::string account_name;
if (as_cred.account_str_.empty()) {
if (pos != std::string::npos) {
account_name = host_name.substr(0, pos);
} else {
account_name = host_name;
}
} else {
account_name = as_cred.account_str_;
}
std::string service_url(
"https://" + account_name + ".blob.core.windows.net");
if (as_cred.auth_type_ == "managed_identity") {
// Azure Managed Identity authentication (system- or user-assigned).
// Token caching and refresh are handled by the Azure Identity SDK.
LOG_VERBOSE(1) << "Using Azure Managed Identity authentication for "
<< account_name;
std::shared_ptr<Azure::Core::Credentials::TokenCredential> token_cred;
if (!as_cred.client_id_.empty()) {
// User-assigned Managed Identity: specify the client ID.
Azure::Identity::ManagedIdentityCredentialOptions mi_opts;
mi_opts.ClientId = as_cred.client_id_;
token_cred =
std::make_shared<Azure::Identity::ManagedIdentityCredential>(
mi_opts);
LOG_VERBOSE(1) << "Using user-assigned Managed Identity with client ID "
<< as_cred.client_id_;
} else {
// System-assigned Managed Identity.
token_cred =
std::make_shared<Azure::Identity::ManagedIdentityCredential>();
LOG_VERBOSE(1) << "Using system-assigned Managed Identity";
}
client_ =
std::make_shared<asb::BlobServiceClient>(service_url, token_cred);
} else if (as_cred.auth_type_ == "default") {
// DefaultAzureCredential chains multiple credential sources:
// environment variables → managed identity → Azure CLI → etc.
LOG_VERBOSE(1) << "Using Azure DefaultAzureCredential for "
<< account_name;
auto token_cred =
std::make_shared<Azure::Identity::DefaultAzureCredential>();
client_ =
std::make_shared<asb::BlobServiceClient>(service_url, token_cred);
} else if (!as_cred.account_key_.empty()) {
// Shared Key authentication (backwards-compatible default).
auto cred = std::make_shared<as::StorageSharedKeyCredential>(
account_name, as_cred.account_key_);
client_ = std::make_shared<asb::BlobServiceClient>(service_url, cred);
} else {
// Anonymous access (no credential provided).
client_ = std::make_shared<asb::BlobServiceClient>(service_url);
}
}
}
Status
ASFileSystem::CheckClient()
{
if (client_ == nullptr) {
return Status(
Status::Code::INTERNAL,
"Unable to create Azure filesystem client. Check account credentials.");
}
return Status::Success;
}
Status
ASFileSystem::FileModificationTime(const std::string& path, int64_t* mtime_ns)
{
std::string container, blob;
RETURN_IF_ERROR(ParsePath(path, &container, &blob));
auto bc = client_->GetBlobContainerClient(container).GetBlobClient(blob);
try {
auto blobProperty = bc.GetProperties().Value;
*mtime_ns = std::chrono::time_point_cast<std::chrono::nanoseconds>(
blobProperty.LastModified)
.time_since_epoch()
.count();
}
catch (as::StorageException& ex) {
return Status(
Status::Code::INTERNAL,
"Unable to get blob property for file at " + path + ":" + ex.what());
}
return Status::Success;
};
Status
ASFileSystem::ListDirectory(
const std::string& container, const std::string& dir_path,
std::function<Status(
const std::vector<asb::Models::BlobItem>& blobs,
const std::vector<std::string>& blob_prefixes)>
callback)
{
auto container_client = client_->GetBlobContainerClient(container);
auto options = asb::ListBlobsOptions();
// Append a slash to make it easier to list contents
std::string full_dir = AppendSlash(dir_path);
options.Prefix = full_dir;
try {
for (auto blobPage = container_client.ListBlobsByHierarchy("/", options);
blobPage.HasPage(); blobPage.MoveToNextPage()) {
// per-page per-blob
RETURN_IF_ERROR(callback(blobPage.Blobs, blobPage.BlobPrefixes));
}
}
catch (as::StorageException& ex) {
return Status(
Status::Code::INTERNAL,
"Failed to get contents of directory " + dir_path + ":" + ex.what());
}
return Status::Success;
}
Status
ASFileSystem::GetDirectoryContents(
const std::string& path, std::set<std::string>* contents)
{
auto func = [&](const std::vector<asb::Models::BlobItem>& blobs,
const std::vector<std::string>& blob_prefixes) {
for (const auto& blob_item : blobs) {
// Fail-safe check to ensure the item name is not empty
if (blob_item.Name.empty()) {
return Status(
Status::Code::INTERNAL,
"Cannot handle item with empty name at " + path);
}
contents->insert(BaseName(blob_item.Name));
}
for (const auto& directory_item : blob_prefixes) {
// Fail-safe check to ensure the item name is not empty
if (directory_item.empty()) {
return Status(
Status::Code::INTERNAL,
"Cannot handle item with empty name at " + path);
}
contents->insert(BaseName(directory_item));
}
return Status::Success;
};
std::string container, dir_path;
RETURN_IF_ERROR(ParsePath(path, &container, &dir_path));
return ListDirectory(container, dir_path, func);
}
Status
ASFileSystem::GetDirectorySubdirs(
const std::string& path, std::set<std::string>* subdirs)
{
auto func = [&](const std::vector<asb::Models::BlobItem>& blobs,
const std::vector<std::string>& blob_prefixes) {
for (const auto& directory_item : blob_prefixes) {
// Fail-safe check to ensure the item name is not empty
if (directory_item.empty()) {
return Status(
Status::Code::INTERNAL,
"Cannot handle item with empty name at " + path);
}
subdirs->insert(BaseName(directory_item));
}
return Status::Success;
};
std::string container, dir_path;
RETURN_IF_ERROR(ParsePath(path, &container, &dir_path));
return ListDirectory(container, dir_path, func);
}
Status
ASFileSystem::GetDirectoryFiles(
const std::string& path, std::set<std::string>* files)
{
auto func = [&](const std::vector<asb::Models::BlobItem>& blobs,
const std::vector<std::string>& blob_prefixes) {
for (const auto& blob_item : blobs) {
// Fail-safe check to ensure the item name is not empty
if (blob_item.Name.empty()) {
return Status(
Status::Code::INTERNAL,
"Cannot handle item with empty name at " + path);
}
files->insert(BaseName(blob_item.Name));
}
return Status::Success;
};
std::string container, dir_path;
RETURN_IF_ERROR(ParsePath(path, &container, &dir_path));
return ListDirectory(container, dir_path, func);
}
Status
ASFileSystem::IsDirectory(const std::string& path, bool* is_dir)
{
*is_dir = false;
std::string container, blob_path;
RETURN_IF_ERROR(ParsePath(path, &container, &blob_path));
auto container_client = client_->GetBlobContainerClient(container);
auto options = asb::ListBlobsOptions();
// Append a slash to make it easier to list contents
std::string full_dir = AppendSlash(blob_path);
options.Prefix = full_dir;
try {
for (auto blobPage = container_client.ListBlobsByHierarchy("/", options);
blobPage.HasPage(); blobPage.MoveToNextPage()) {
if ((blobPage.Blobs.size() == 1) &&
(blobPage.Blobs[0].Name == blob_path)) {
// It's a file
return Status::Success;
}
*is_dir =
((blobPage.Blobs.size() > 0) || (blobPage.BlobPrefixes.size() > 0));
break;
}
}
catch (as::StorageException& ex) {
return Status(
Status::Code::INTERNAL,
"Failed to check if directory at " + path + ":" + ex.what());
}
return Status::Success;
};
Status
ASFileSystem::ReadTextFile(const std::string& path, std::string* contents)
{
std::string container, blob_path;
RETURN_IF_ERROR(ParsePath(path, &container, &blob_path));
try {
auto res = client_->GetBlobContainerClient(container)
.GetBlobClient(blob_path)
.Download();
*contents = std::string(
(const char*)res.Value.BodyStream->ReadToEnd().data(),
res.Value.BlobSize);
}
catch (as::StorageException& ex) {
return Status(
Status::Code::INTERNAL,
"Failed to read text file at " + path + ":" + ex.what());
}
return Status::Success;
}
Status
ASFileSystem::FileExists(const std::string& path, bool* exists)
{
*exists = false;
std::string container, blob;
RETURN_IF_ERROR(ParsePath(path, &container, &blob));
auto container_client = client_->GetBlobContainerClient(container);
auto options = asb::ListBlobsOptions();
options.Prefix = blob;
try {
for (auto blobPage = container_client.ListBlobsByHierarchy("/", options);
blobPage.HasPage(); blobPage.MoveToNextPage()) {
// If any entries are returned from ListBlobs, the file / directory exists
*exists =
((blobPage.Blobs.size() > 0) || (blobPage.BlobPrefixes.size() > 0));
break;
}
}
catch (as::StorageException& ex) {
return Status(
Status::Code::INTERNAL,
"Failed to check if file exists at " + path + ":" + ex.what());
}
return Status::Success;
}
Status
ASFileSystem::DownloadFolder(
const std::string& container, const std::string& path,
const std::string& dest)
{
auto container_client = client_->GetBlobContainerClient(container);
auto func = [&](const std::vector<asb::Models::BlobItem>& blobs,
const std::vector<std::string>& blob_prefixes) {
for (const auto& blob_item : blobs) {
const auto& local_path = JoinPath({dest, BaseName(blob_item.Name)});
try {
container_client.GetBlobClient(blob_item.Name).DownloadTo(local_path);
}
catch (as::StorageException& ex) {
return Status(
Status::Code::INTERNAL,
"Failed to download file at " + blob_item.Name + ":" + ex.what());
}
}
for (const auto& directory_item : blob_prefixes) {
const auto& local_path = JoinPath({dest, BaseName(directory_item)});
int status = mkdir(
const_cast<char*>(local_path.c_str()), S_IRUSR | S_IWUSR | S_IXUSR);
if (status == -1) {
return Status(
Status::Code::INTERNAL,
"Failed to create local folder: " + local_path +
", errno:" + strerror(errno));
}
RETURN_IF_ERROR(DownloadFolder(container, directory_item, local_path));
}
return Status::Success;
};
return ListDirectory(container, path, func);
}
Status
ASFileSystem::LocalizePath(
const std::string& path, std::shared_ptr<LocalizedPath>* localized)
{
bool exists;
RETURN_IF_ERROR(FileExists(path, &exists));
if (!exists) {
return Status(
Status::Code::INTERNAL, "directory or file does not exist at " + path);
}
bool is_dir;
RETURN_IF_ERROR(IsDirectory(path, &is_dir));
if (!is_dir) {
return Status(
Status::Code::UNSUPPORTED,
"AS file localization not yet implemented " + path);
}
// Create a local directory for azure model store.
// If ENV variable are not set, creates a temporary directory
// under `/tmp` with the format: "folderXXXXXX".
// Otherwise, will create a folder under specified directory with the same
// format.
std::string env_mount_dir = GetEnvironmentVariableOrDefault(
"TRITON_AZURE_MOUNT_DIRECTORY", kDefaultMountDirectory);
std::string tmp_folder;
RETURN_IF_ERROR(triton::core::MakeTemporaryDirectory(
FileSystemType::LOCAL, env_mount_dir, &tmp_folder));
localized->reset(new LocalizedPath(path, tmp_folder));
std::string dest(tmp_folder);
std::string container, blob;
RETURN_IF_ERROR(ParsePath(path, &container, &blob));
return DownloadFolder(container, blob, dest);
}
Status
ASFileSystem::WriteTextFile(
const std::string& path, const std::string& contents)
{
std::string container, blob;
RETURN_IF_ERROR(ParsePath(path, &container, &blob));
try {
client_->GetBlobContainerClient(container)
.GetBlockBlobClient(blob)
.UploadFrom((const uint8_t*)contents.data(), contents.size());
}
catch (as::StorageException& ex) {
return Status(
Status::Code::INTERNAL,
"Failed to write file to " + path + ":" + ex.what());
}
return Status::Success;
}
Status
ASFileSystem::WriteBinaryFile(
const std::string& path, const char* contents, const size_t content_len)
{
return Status(
Status::Code::UNSUPPORTED,
"Write text file operation not yet implemented " + path);
}
Status
ASFileSystem::MakeDirectory(const std::string& dir, const bool recursive)
{
return Status(
Status::Code::UNSUPPORTED,
"Make directory operation not yet implemented");
}
Status
ASFileSystem::MakeTemporaryDirectory(
std::string dir_path, std::string* temp_dir)
{
return Status(
Status::Code::UNSUPPORTED,
"Make temporary directory operation not yet implemented");
}
Status
ASFileSystem::DeletePath(const std::string& path)
{
return Status(
Status::Code::UNSUPPORTED, "Delete path operation not yet implemented");
}
}} // namespace triton::core