@@ -22,6 +22,7 @@ limitations under the License.
2222#include < sstream>
2323#include < string>
2424
25+ #include " absl/synchronization/mutex.h"
2526#include " tensorflow/c/env.h"
2627#include " tensorflow/c/experimental/filesystem/filesystem_interface.h"
2728#include " tensorflow/c/tf_status.h"
@@ -162,27 +163,19 @@ class LibHDFS {
162163 void * handle_;
163164};
164165
165- static const LibHDFS* libhdfs (TF_Status* status) {
166- static const LibHDFS* libhdfs = new LibHDFS (status);
167- return libhdfs;
168- }
169-
170166// We rely on HDFS connection caching here. The HDFS client calls
171167// org.apache.hadoop.fs.FileSystem.get(), which caches the connection
172168// internally.
173- hdfsFS Connect (const std::string& path, TF_Status* status) {
174- auto hdfs_file = libhdfs (status);
175- if (TF_GetCode (status) != TF_OK ) return nullptr ;
176-
177- std::string scheme, namenode, nodepath;
178- ParseHadoopPath (path, &scheme, &namenode, &nodepath);
169+ hdfsFS Connect (LibHDFS* libhdfs, const std::string& path, TF_Status* status) {
170+ std::string scheme, namenode, hdfs_path;
171+ ParseHadoopPath (path, &scheme, &namenode, &hdfs_path);
179172
180- hdfsBuilder* builder = hdfs_file ->hdfsNewBuilder ();
173+ hdfsBuilder* builder = libhdfs ->hdfsNewBuilder ();
181174 if (scheme == " file" ) {
182- hdfs_file ->hdfsBuilderSetNameNode (builder, nullptr );
175+ libhdfs ->hdfsBuilderSetNameNode (builder, nullptr );
183176 } else if (scheme == " viewfs" ) {
184177 char * defaultFS = nullptr ;
185- hdfs_file ->hdfsConfGetStr (" fs.defaultFS" , &defaultFS);
178+ libhdfs ->hdfsConfGetStr (" fs.defaultFS" , &defaultFS);
186179 std::string defaultScheme, defaultCluster, defaultPath;
187180 ParseHadoopPath (defaultFS, &defaultScheme, &defaultCluster, &defaultPath);
188181
@@ -195,17 +188,17 @@ hdfsFS Connect(const std::string& path, TF_Status* status) {
195188 // The default NameNode configuration will be used (from the XML
196189 // configuration files). See:
197190 // https://github.com/tensorflow/tensorflow/blob/v1.0.0/third_party/hadoop/hdfs.h#L259
198- hdfs_file ->hdfsBuilderSetNameNode (builder, " default" );
191+ libhdfs ->hdfsBuilderSetNameNode (builder, " default" );
199192 } else if (scheme == " har" ) {
200193 std::string path_har = path;
201194 SplitArchiveNameAndPath (&path_har, &namenode, status);
202195 if (TF_GetCode (status) != TF_OK ) return nullptr ;
203- hdfs_file ->hdfsBuilderSetNameNode (builder, namenode.c_str ());
196+ libhdfs ->hdfsBuilderSetNameNode (builder, namenode.c_str ());
204197 } else {
205- hdfs_file ->hdfsBuilderSetNameNode (
198+ libhdfs ->hdfsBuilderSetNameNode (
206199 builder, namenode.empty () ? " default" : namenode.c_str ());
207200 }
208- auto fs = hdfs_file ->hdfsBuilderConnect (builder);
201+ auto fs = libhdfs ->hdfsBuilderConnect (builder);
209202 if (fs == nullptr )
210203 TF_SetStatusFromIOError (status, TF_NOT_FOUND , strerror (errno));
211204 else
@@ -216,16 +209,178 @@ hdfsFS Connect(const std::string& path, TF_Status* status) {
216209// SECTION 1. Implementation for `TF_RandomAccessFile`
217210// ----------------------------------------------------------------------------
218211namespace tf_random_access_file {
212+ typedef struct HDFSFile {
213+ std::string path;
214+ std::string hdfs_path;
215+ hdfsFS fs;
216+ LibHDFS* libhdfs;
217+ absl::Mutex mu;
218+ hdfsFile handle ABSL_GUARDED_BY (mu);
219+ HDFSFile (std::string path, std::string hdfs_path, hdfsFS fs, LibHDFS* libhdfs,
220+ hdfsFile handle)
221+ : path(std::move(path)),
222+ hdfs_path (std::move(hdfs_path)),
223+ fs(fs),
224+ libhdfs(libhdfs),
225+ mu(),
226+ handle(handle) {}
227+ } HDFSFile;
228+
229+ void Cleanup (TF_RandomAccessFile* file) {
230+ auto hdfs_file = static_cast <HDFSFile*>(file->plugin_file );
231+ {
232+ absl::MutexLock l (&hdfs_file->mu );
233+ if (hdfs_file->handle != nullptr ) {
234+ hdfs_file->libhdfs ->hdfsCloseFile (hdfs_file->fs , hdfs_file->handle );
235+ }
236+ }
237+ delete hdfs_file;
238+ }
219239
220- // TODO(vnvo2409): Implement later
240+ int64_t Read (const TF_RandomAccessFile* file, uint64_t offset, size_t n,
241+ char * buffer, TF_Status* status) {
242+ auto hdfs_file = static_cast <HDFSFile*>(file->plugin_file );
243+ auto libhdfs = hdfs_file->libhdfs ;
244+ auto fs = hdfs_file->fs ;
245+ auto hdfs_path = hdfs_file->hdfs_path .c_str ();
246+ auto path = hdfs_file->path .c_str ();
247+
248+ char * dst = buffer;
249+ bool eof_retried = false ;
250+ int64_t r = 0 ;
251+ while (TF_GetCode (status) == TF_OK && !eof_retried) {
252+ // We lock inside the loop rather than outside so we don't block other
253+ // concurrent readers.
254+ absl::MutexLock l (&hdfs_file->mu );
255+ auto handle = hdfs_file->handle ;
256+ // Max read length is INT_MAX-2, for hdfsPread function take a parameter
257+ // of int32. -2 offset can avoid JVM OutOfMemoryError.
258+ size_t read_n =
259+ (std::min)(n, static_cast <size_t >(std::numeric_limits<int >::max () - 2 ));
260+ r = libhdfs->hdfsPread (fs, handle, static_cast <tOffset>(offset), dst,
261+ static_cast <tSize>(read_n));
262+ if (r > 0 ) {
263+ dst += r;
264+ n -= r;
265+ offset += r;
266+ } else if (!eof_retried && r == 0 ) {
267+ // Always reopen the file upon reaching EOF to see if there's more data.
268+ // If writers are streaming contents while others are concurrently
269+ // reading, HDFS requires that we reopen the file to see updated
270+ // contents.
271+ //
272+ // Fixes #5438
273+ if (handle != nullptr && libhdfs->hdfsCloseFile (fs, handle) != 0 ) {
274+ TF_SetStatusFromIOError (status, errno, path);
275+ return -1 ;
276+ }
277+ handle = libhdfs->hdfsOpenFile (fs, hdfs_path, O_RDONLY , 0 , 0 , 0 );
278+ if (handle == nullptr ) {
279+ TF_SetStatusFromIOError (status, errno, path);
280+ return -1 ;
281+ }
282+ eof_retried = true ;
283+ } else if (eof_retried && r == 0 ) {
284+ TF_SetStatus (status, TF_OUT_OF_RANGE , " Read less bytes than requested" );
285+ } else if (errno == EINTR || errno == EAGAIN ) {
286+ // hdfsPread may return EINTR too. Just retry.
287+ } else {
288+ TF_SetStatusFromIOError (status, errno, path);
289+ }
290+ }
291+ return r;
292+ }
221293
222294} // namespace tf_random_access_file
223295
224296// SECTION 2. Implementation for `TF_WritableFile`
225297// ----------------------------------------------------------------------------
226298namespace tf_writable_file {
299+ typedef struct HDFSFile {
300+ std::string hdfs_path;
301+ hdfsFS fs;
302+ LibHDFS* libhdfs;
303+ hdfsFile handle;
304+ HDFSFile (std::string hdfs_path, hdfsFS fs, LibHDFS* libhdfs, hdfsFile handle)
305+ : hdfs_path(std::move(hdfs_path)),
306+ fs (fs),
307+ libhdfs(libhdfs),
308+ handle(handle) {}
309+ } HDFSFile;
310+
311+ static void Cleanup (TF_WritableFile* file) {
312+ auto hdfs_file = static_cast <HDFSFile*>(file->plugin_file );
313+ hdfs_file->libhdfs ->hdfsCloseFile (hdfs_file->fs , hdfs_file->handle );
314+ hdfs_file->fs = nullptr ;
315+ hdfs_file->handle = nullptr ;
316+ delete hdfs_file;
317+ }
227318
228- // TODO(vnvo2409): Implement later
319+ void Append (const TF_WritableFile* file, const char * buffer, size_t n,
320+ TF_Status* status) {
321+ auto hdfs_file = static_cast <HDFSFile*>(file->plugin_file );
322+ auto libhdfs = hdfs_file->libhdfs ;
323+ auto fs = hdfs_file->fs ;
324+ auto handle = hdfs_file->handle ;
325+
326+ size_t cur_pos = 0 , write_len = 0 ;
327+ bool retry = false ;
328+ // max() - 2 can avoid OutOfMemoryError in JVM .
329+ static const size_t max_len_once =
330+ static_cast <size_t >(std::numeric_limits<tSize>::max () - 2 );
331+ while (cur_pos < n) {
332+ write_len = (std::min)(n - cur_pos, max_len_once);
333+ tSize w = libhdfs->hdfsWrite (fs, handle, buffer + cur_pos,
334+ static_cast <tSize>(write_len));
335+ if (w == -1 ) {
336+ if (!retry && (errno == EINTR || errno == EAGAIN )) {
337+ retry = true ;
338+ } else {
339+ return TF_SetStatusFromIOError (status, errno,
340+ hdfs_file->hdfs_path .c_str ());
341+ }
342+ } else {
343+ cur_pos += w;
344+ }
345+ }
346+ TF_SetStatus (status, TF_OK , " " );
347+ }
348+
349+ int64_t Tell (const TF_WritableFile* file, TF_Status* status) {
350+ auto hdfs_file = static_cast <HDFSFile*>(file->plugin_file );
351+ int64_t position =
352+ hdfs_file->libhdfs ->hdfsTell (hdfs_file->fs , hdfs_file->handle );
353+ if (position == -1 )
354+ TF_SetStatusFromIOError (status, errno, hdfs_file->hdfs_path .c_str ());
355+ else
356+ TF_SetStatus (status, TF_OK , " " );
357+ return position;
358+ }
359+
360+ void Flush (const TF_WritableFile* file, TF_Status* status) {
361+ auto hdfs_file = static_cast <HDFSFile*>(file->plugin_file );
362+ if (hdfs_file->libhdfs ->hdfsHFlush (hdfs_file->fs , hdfs_file->handle ) != 0 )
363+ TF_SetStatusFromIOError (status, errno, hdfs_file->hdfs_path .c_str ());
364+ else
365+ TF_SetStatus (status, TF_OK , " " );
366+ }
367+
368+ void Sync (const TF_WritableFile* file, TF_Status* status) {
369+ auto hdfs_file = static_cast <HDFSFile*>(file->plugin_file );
370+ if (hdfs_file->libhdfs ->hdfsHSync (hdfs_file->fs , hdfs_file->handle ) != 0 )
371+ TF_SetStatusFromIOError (status, errno, hdfs_file->hdfs_path .c_str ());
372+ else
373+ TF_SetStatus (status, TF_OK , " " );
374+ }
375+
376+ void Close (const TF_WritableFile* file, TF_Status* status) {
377+ auto hdfs_file = static_cast <HDFSFile*>(file->plugin_file );
378+ TF_SetStatus (status, TF_OK , " " );
379+ if (hdfs_file->libhdfs ->hdfsCloseFile (hdfs_file->fs , hdfs_file->handle ) != 0 )
380+ TF_SetStatusFromIOError (status, errno, hdfs_file->hdfs_path .c_str ());
381+ hdfs_file->fs = nullptr ;
382+ hdfs_file->handle = nullptr ;
383+ }
229384
230385} // namespace tf_writable_file
231386
@@ -241,6 +396,52 @@ namespace tf_read_only_memory_region {
241396// ----------------------------------------------------------------------------
242397namespace tf_hadoop_filesystem {
243398
399+ void Init (TF_Filesystem* filesystem, TF_Status* status) {
400+ filesystem->plugin_filesystem = new LibHDFS (status);
401+ if (TF_GetCode (status) != TF_OK ) return ;
402+ TF_SetStatus (status, TF_OK , " " );
403+ }
404+
405+ void Cleanup (TF_Filesystem* filesystem) {
406+ auto libhdfs = static_cast <LibHDFS*>(filesystem->plugin_filesystem );
407+ delete libhdfs;
408+ }
409+
410+ void NewRandomAccessFile (const TF_Filesystem* filesystem, const char * path,
411+ TF_RandomAccessFile* file, TF_Status* status) {
412+ auto libhdfs = static_cast <LibHDFS*>(filesystem->plugin_filesystem );
413+ auto fs = Connect (libhdfs, path, status);
414+ if (TF_GetCode (status) != TF_OK ) return ;
415+
416+ std::string scheme, namenode, hdfs_path;
417+ ParseHadoopPath (path, &scheme, &namenode, &hdfs_path);
418+
419+ auto handle = libhdfs->hdfsOpenFile (fs, hdfs_path.c_str (), O_RDONLY , 0 , 0 , 0 );
420+ if (handle == nullptr ) return TF_SetStatusFromIOError (status, errno, path);
421+
422+ file->plugin_file =
423+ new tf_random_access_file::HDFSFile (path, hdfs_path, fs, libhdfs, handle);
424+ TF_SetStatus (status, TF_OK , " " );
425+ }
426+
427+ void NewWritableFile (const TF_Filesystem* filesystem, const char * path,
428+ TF_WritableFile* file, TF_Status* status) {
429+ auto libhdfs = static_cast <LibHDFS*>(filesystem->plugin_filesystem );
430+ auto fs = Connect (libhdfs, path, status);
431+ if (TF_GetCode (status) != TF_OK ) return ;
432+
433+ std::string scheme, namenode, hdfs_path;
434+ ParseHadoopPath (path, &scheme, &namenode, &hdfs_path);
435+
436+ auto handle = libhdfs->hdfsOpenFile (fs, hdfs_path.c_str (),
437+ O_WRONLY | O_APPEND , 0 , 0 , 0 );
438+ if (handle == nullptr ) return TF_SetStatusFromIOError (status, errno, path);
439+
440+ file->plugin_file =
441+ new tf_writable_file::HDFSFile (hdfs_path, fs, libhdfs, handle);
442+ TF_SetStatus (status, TF_OK , " " );
443+ }
444+
244445// TODO(vnvo2409): Implement later
245446
246447} // namespace tf_hadoop_filesystem
0 commit comments