Skip to content

Commit 101b925

Browse files
Merge pull request #42273 from vnvo2409:hadoop-filesystem
PiperOrigin-RevId: 328365342 Change-Id: I773412f6cc745bcf489df19939a3d979b8ec10cf
2 parents 91314fb + 4dd0a04 commit 101b925

4 files changed

Lines changed: 162 additions & 2 deletions

File tree

‎tensorflow/c/experimental/filesystem/plugins/hadoop/BUILD‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Experimental hadoop filesystem plugin.
2-
load("//tensorflow:tensorflow.bzl", "get_win_copts", "tf_cc_shared_object")
2+
load("//tensorflow:tensorflow.bzl", "get_win_copts", "tf_cc_shared_object", "tf_cc_test")
33

44
package(
55
licenses = ["notice"], # Apache 2.0
@@ -33,3 +33,20 @@ cc_library(
3333
"@com_google_absl//absl/synchronization",
3434
],
3535
)
36+
37+
tf_cc_test(
38+
name = "hadoop_filesystem_test",
39+
srcs = [
40+
"hadoop_filesystem_test.cc",
41+
],
42+
tags = [
43+
"manual",
44+
"notap",
45+
],
46+
deps = [
47+
":hadoop_filesystem_impl",
48+
"//tensorflow/core/platform:path",
49+
"//tensorflow/core/platform:stacktrace_handler",
50+
"//tensorflow/core/platform:test",
51+
],
52+
)

‎tensorflow/c/experimental/filesystem/plugins/hadoop/hadoop_filesystem.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ typedef struct HDFSFile {
308308
handle(handle) {}
309309
} HDFSFile;
310310

311-
static void Cleanup(TF_WritableFile* file) {
311+
void Cleanup(TF_WritableFile* file) {
312312
auto hdfs_file = static_cast<HDFSFile*>(file->plugin_file);
313313
hdfs_file->libhdfs->hdfsCloseFile(hdfs_file->fs, hdfs_file->handle);
314314
hdfs_file->fs = nullptr;

‎tensorflow/c/experimental/filesystem/plugins/hadoop/hadoop_filesystem.h‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,58 @@ limitations under the License.
1515
#ifndef TENSORFLOW_C_EXPERIMENTAL_FILESYSTEM_PLUGINS_HADOOP_HADOOP_FILESYSTEM_H_
1616
#define TENSORFLOW_C_EXPERIMENTAL_FILESYSTEM_PLUGINS_HADOOP_HADOOP_FILESYSTEM_H_
1717

18+
#include <string>
19+
1820
#include "tensorflow/c/experimental/filesystem/filesystem_interface.h"
1921
#include "tensorflow/c/tf_status.h"
2022

23+
void ParseHadoopPath(const std::string& fname, std::string* scheme,
24+
std::string* namenode, std::string* path);
25+
class LibHDFS;
26+
27+
namespace tf_random_access_file {
28+
void Cleanup(TF_RandomAccessFile* file);
29+
int64_t Read(const TF_RandomAccessFile* file, uint64_t offset, size_t n,
30+
char* buffer, TF_Status* status);
31+
} // namespace tf_random_access_file
32+
33+
namespace tf_writable_file {
34+
void Cleanup(TF_WritableFile* file);
35+
void Append(const TF_WritableFile* file, const char* buffer, size_t n,
36+
TF_Status* status);
37+
int64_t Tell(const TF_WritableFile* file, TF_Status* status);
38+
void Sync(const TF_WritableFile* file, TF_Status* status);
39+
void Flush(const TF_WritableFile* file, TF_Status* status);
40+
void Close(const TF_WritableFile* file, TF_Status* status);
41+
} // namespace tf_writable_file
42+
43+
namespace tf_hadoop_filesystem {
44+
void Init(TF_Filesystem* filesystem, TF_Status* status);
45+
void Cleanup(TF_Filesystem* filesystem);
46+
void NewRandomAccessFile(const TF_Filesystem* filesystem, const char* path,
47+
TF_RandomAccessFile* file, TF_Status* status);
48+
void NewWritableFile(const TF_Filesystem* filesystem, const char* path,
49+
TF_WritableFile* file, TF_Status* status);
50+
void NewReadOnlyMemoryRegionFromFile(const TF_Filesystem* filesystem,
51+
const char* path,
52+
TF_ReadOnlyMemoryRegion* region,
53+
TF_Status* status);
54+
void PathExists(const TF_Filesystem* filesystem, const char* path,
55+
TF_Status* status);
56+
void Stat(const TF_Filesystem* filesystem, const char* path,
57+
TF_FileStatistics* stats, TF_Status* status);
58+
int64_t GetFileSize(const TF_Filesystem* filesystem, const char* path,
59+
TF_Status* status);
60+
void DeleteFile(const TF_Filesystem* filesystem, const char* path,
61+
TF_Status* status);
62+
void CreateDir(const TF_Filesystem* filesystem, const char* path,
63+
TF_Status* status);
64+
void DeleteDir(const TF_Filesystem* filesystem, const char* path,
65+
TF_Status* status);
66+
void RenameFile(const TF_Filesystem* filesystem, const char* src,
67+
const char* dst, TF_Status* status);
68+
int GetChildren(const TF_Filesystem* filesystem, const char* path,
69+
char*** entries, TF_Status* status);
70+
} // namespace tf_hadoop_filesystem
71+
2172
#endif // TENSORFLOW_C_EXPERIMENTAL_FILESYSTEM_PLUGINS_HADOOP_HADOOP_FILESYSTEM_H_
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
==============================================================================*/
15+
#include "tensorflow/c/experimental/filesystem/plugins/hadoop/hadoop_filesystem.h"
16+
17+
#include "tensorflow/core/platform/path.h"
18+
#include "tensorflow/core/platform/stacktrace_handler.h"
19+
#include "tensorflow/core/platform/test.h"
20+
21+
#define ASSERT_TF_OK(x) ASSERT_EQ(TF_OK, TF_GetCode(x)) << TF_Message(x)
22+
#define EXPECT_TF_OK(x) EXPECT_EQ(TF_OK, TF_GetCode(x)) << TF_Message(x)
23+
24+
namespace tensorflow {
25+
namespace {
26+
27+
class HadoopFileSystemTest : public ::testing::Test {
28+
public:
29+
void SetUp() override {
30+
status_ = TF_NewStatus();
31+
filesystem_ = new TF_Filesystem;
32+
tf_hadoop_filesystem::Init(filesystem_, status_);
33+
ASSERT_TF_OK(status_) << "Could not initialize filesystem. "
34+
<< TF_Message(status_);
35+
}
36+
void TearDown() override {
37+
TF_DeleteStatus(status_);
38+
tf_hadoop_filesystem::Cleanup(filesystem_);
39+
delete filesystem_;
40+
}
41+
42+
std::string TmpDir(const std::string& path) {
43+
char* test_dir = getenv("HADOOP_TEST_TMPDIR");
44+
if (test_dir != nullptr) {
45+
return io::JoinPath(std::string(test_dir), path);
46+
} else {
47+
return "file://" + io::JoinPath(testing::TmpDir(), path);
48+
}
49+
}
50+
51+
std::unique_ptr<TF_WritableFile, void (*)(TF_WritableFile* file)>
52+
GetWriter() {
53+
std::unique_ptr<TF_WritableFile, void (*)(TF_WritableFile * file)> writer(
54+
new TF_WritableFile, [](TF_WritableFile* file) {
55+
if (file != nullptr) {
56+
if (file->plugin_file != nullptr) tf_writable_file::Cleanup(file);
57+
delete file;
58+
}
59+
});
60+
writer->plugin_file = nullptr;
61+
return writer;
62+
}
63+
64+
std::unique_ptr<TF_RandomAccessFile, void (*)(TF_RandomAccessFile* file)>
65+
GetReader() {
66+
std::unique_ptr<TF_RandomAccessFile, void (*)(TF_RandomAccessFile * file)>
67+
reader(new TF_RandomAccessFile, [](TF_RandomAccessFile* file) {
68+
if (file != nullptr) {
69+
if (file->plugin_file != nullptr)
70+
tf_random_access_file::Cleanup(file);
71+
delete file;
72+
}
73+
});
74+
reader->plugin_file = nullptr;
75+
return reader;
76+
}
77+
78+
protected:
79+
TF_Filesystem* filesystem_;
80+
TF_Status* status_;
81+
};
82+
83+
TEST_F(HadoopFileSystemTest, Init) { ASSERT_TF_OK(status_); }
84+
85+
} // namespace
86+
} // namespace tensorflow
87+
88+
GTEST_API_ int main(int argc, char** argv) {
89+
tensorflow::testing::InstallStacktraceHandler();
90+
::testing::InitGoogleTest(&argc, argv);
91+
return RUN_ALL_TESTS();
92+
}

0 commit comments

Comments
 (0)