Skip to content

Specify non-random location for filesystem's temporary cache #266

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

Merged
merged 5 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 10 additions & 1 deletion src/filesystem/api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,16 @@ MakeTemporaryDirectory(const FileSystemType type, std::string* temp_dir)
{
std::shared_ptr<FileSystem> fs;
RETURN_IF_ERROR(fsm_.GetFileSystem(type, fs));
return fs->MakeTemporaryDirectory(temp_dir);
return fs->MakeTemporaryDirectory(kDefaultMountDirectory, temp_dir);
}

Status
MakeTemporaryDirectory(
const FileSystemType type, std::string dir_path, std::string* temp_dir)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const FileSystemType type, std::string dir_path, std::string* temp_dir)
const FileSystemType type, const std::string& dir_path, std::string* temp_dir)

{
std::shared_ptr<FileSystem> fs;
RETURN_IF_ERROR(fsm_.GetFileSystem(type, fs));
return fs->MakeTemporaryDirectory(dir_path, temp_dir);
}

Status
Expand Down
9 changes: 9 additions & 0 deletions src/filesystem/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,15 @@ Status MakeDirectory(const std::string& dir, const bool recursive);
/// \return Error status
Status MakeTemporaryDirectory(const FileSystemType type, std::string* temp_dir);

/// Create a temporary directory of the specified filesystem type
/// in a provided location.
/// \param type The type of the filesystem.
/// \param dir_path The specified path.
/// \param temp_dir Returns the path to the temporary directory.
/// \return Error status
Status MakeTemporaryDirectory(
const FileSystemType type, std::string dir_path, std::string* temp_dir);
Copy link
Contributor

Choose a reason for hiding this comment

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

A small thing, I think the dir_path can be declared as const std::string& dir_path here, and leave the LocalFileSystem::MakeTemporaryDirectory(std::string dir_path, ...) as it is. Then, we can save a copy of dir_path when passing the variable from the caller to MakeTemporaryDirectory() here, and only copy it once when passing dir_path from here to LocalFileSystem::MakeTemporaryDirectory().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll make sure to check and introduce this in a follow-up pr with the bug fix. Thanks for suggestion!

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const FileSystemType type, std::string dir_path, std::string* temp_dir);
const FileSystemType type, const std::string& dir_path, std::string* temp_dir);


/// Delete a path.
/// \param path The path to the directory or file.
/// \return Error status
Expand Down
27 changes: 16 additions & 11 deletions src/filesystem/implementations/as.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ class ASFileSystem : public FileSystem {
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* temp_dir) override;
Status MakeTemporaryDirectory(
std::string dir_path, std::string* temp_dir) override;
Status DeletePath(const std::string& path) override;

private:
Expand Down Expand Up @@ -441,17 +442,20 @@ ASFileSystem::LocalizePath(
"AS file localization not yet implemented " + path);
}

std::string folder_template = "/tmp/folderXXXXXX";
char* tmp_folder = mkdtemp(const_cast<char*>(folder_template.c_str()));
if (tmp_folder == nullptr) {
return Status(
Status::Code::INTERNAL,
"Failed to create local temp folder: " + folder_template +
", errno:" + strerror(errno));
}
// 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(folder_template);
std::string dest(tmp_folder);

std::string container, blob;
RETURN_IF_ERROR(ParsePath(path, &container, &blob));
Expand Down Expand Up @@ -495,7 +499,8 @@ ASFileSystem::MakeDirectory(const std::string& dir, const bool recursive)
}

Status
ASFileSystem::MakeTemporaryDirectory(std::string* temp_dir)
ASFileSystem::MakeTemporaryDirectory(
std::string dir_path, std::string* temp_dir)
{
return Status(
Status::Code::UNSUPPORTED,
Expand Down
20 changes: 19 additions & 1 deletion src/filesystem/implementations/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@

namespace triton { namespace core {

// Default folder for temporary local cache
constexpr char kDefaultMountDirectory[] = "/tmp";

// FileSystem interface that all file system implementation should inherit from.
// To add new file system support, the implementation should be added and made
// visible to FileSystemManager in api.cc
Expand All @@ -91,7 +94,8 @@ class FileSystem {
const size_t content_len) = 0;
virtual Status MakeDirectory(
const std::string& dir, const bool recursive) = 0;
virtual Status MakeTemporaryDirectory(std::string* temp_dir) = 0;
virtual Status MakeTemporaryDirectory(
std::string dir_path, std::string* temp_dir) = 0;
virtual Status DeletePath(const std::string& path) = 0;
};

Expand All @@ -106,4 +110,18 @@ AppendSlash(const std::string& name)
return (name + "/");
}

/// Helper function to get the value of the environment variable,
/// or default value if not set.
///
/// \param variable_name The name of the environment variable.
/// \param default_value The default value.
/// \return The environment variable or the default value if not set.
std::string
GetEnvironmentVariableOrDefault(
const std::string& variable_name, const std::string& default_value)
{
const char* value = getenv(variable_name.c_str());
return value ? value : default_value;
}

}} // namespace triton::core
13 changes: 9 additions & 4 deletions src/filesystem/implementations/gcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ class GCSFileSystem : public FileSystem {
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* temp_dir) override;
Status MakeTemporaryDirectory(
std::string dir_path, std::string* temp_dir) override;
Status DeletePath(const std::string& path) override;

private:
Expand Down Expand Up @@ -380,9 +381,12 @@ GCSFileSystem::LocalizePath(
"GCS file localization not yet implemented " + path);
}

// Create a local directory for GCS model store.
std::string env_mount_dir = GetEnvironmentVariableOrDefault(
"TRITON_GCS_MOUNT_DIRECTORY", kDefaultMountDirectory);
std::string tmp_folder;
RETURN_IF_ERROR(
triton::core::MakeTemporaryDirectory(FileSystemType::LOCAL, &tmp_folder));
RETURN_IF_ERROR(triton::core::MakeTemporaryDirectory(
FileSystemType::LOCAL, env_mount_dir, &tmp_folder));

localized->reset(new LocalizedPath(path, tmp_folder));

Expand Down Expand Up @@ -480,7 +484,8 @@ GCSFileSystem::MakeDirectory(const std::string& dir, const bool recursive)
}

Status
GCSFileSystem::MakeTemporaryDirectory(std::string* temp_dir)
GCSFileSystem::MakeTemporaryDirectory(
std::string dir_path, std::string* temp_dir)
{
return Status(
Status::Code::UNSUPPORTED,
Expand Down
11 changes: 8 additions & 3 deletions src/filesystem/implementations/local.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ class LocalFileSystem : public FileSystem {
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* temp_dir) override;
Status MakeTemporaryDirectory(
std::string dir_path, std::string* temp_dir) override;
Status DeletePath(const std::string& path) override;
};

Expand Down Expand Up @@ -280,7 +281,8 @@ LocalFileSystem::MakeDirectory(const std::string& dir, const bool recursive)
}

Status
LocalFileSystem::MakeTemporaryDirectory(std::string* temp_dir)
LocalFileSystem::MakeTemporaryDirectory(
std::string dir_path, std::string* temp_dir)
{
#ifdef _WIN32
char temp_path[MAX_PATH + 1];
Expand Down Expand Up @@ -314,7 +316,10 @@ LocalFileSystem::MakeTemporaryDirectory(std::string* temp_dir)
"Failed to create local temp folder: " + *temp_dir);
}
#else
std::string folder_template = "/tmp/folderXXXXXX";
if (dir_path.empty()) {
dir_path = kDefaultMountDirectory;
}
std::string folder_template = JoinPath({dir_path, "folderXXXXXX"});
char* res = mkdtemp(const_cast<char*>(folder_template.c_str()));
if (res == nullptr) {
return Status(
Expand Down
18 changes: 13 additions & 5 deletions src/filesystem/implementations/s3.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ class S3FileSystem : public FileSystem {
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* temp_dir) override;
Status MakeTemporaryDirectory(
std::string dir_path, std::string* temp_dir) override;
Status DeletePath(const std::string& path) override;

private:
Expand Down Expand Up @@ -652,10 +653,16 @@ S3FileSystem::LocalizePath(
effective_path = path;
}

// Create temporary directory
// Create a local directory for AWS 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_AWS_MOUNT_DIRECTORY", kDefaultMountDirectory);
std::string tmp_folder;
RETURN_IF_ERROR(
triton::core::MakeTemporaryDirectory(FileSystemType::LOCAL, &tmp_folder));
RETURN_IF_ERROR(triton::core::MakeTemporaryDirectory(
FileSystemType::LOCAL, env_mount_dir, &tmp_folder));

// Specify contents to be downloaded
std::set<std::string> contents;
Expand Down Expand Up @@ -774,7 +781,8 @@ S3FileSystem::MakeDirectory(const std::string& dir, const bool recursive)
}

Status
S3FileSystem::MakeTemporaryDirectory(std::string* temp_dir)
S3FileSystem::MakeTemporaryDirectory(
std::string dir_path, std::string* temp_dir)
{
return Status(
Status::Code::UNSUPPORTED,
Expand Down