filehandle.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef LIBHDFSPP_LIB_FS_FILEHANDLE_H_
  19. #define LIBHDFSPP_LIB_FS_FILEHANDLE_H_
  20. #include "common/hdfs_public_api.h"
  21. #include "common/async_stream.h"
  22. #include "common/cancel_tracker.h"
  23. #include "common/libhdfs_events_impl.h"
  24. #include "common/new_delete.h"
  25. #include "reader/fileinfo.h"
  26. #include "reader/readergroup.h"
  27. #include "asio.hpp"
  28. #include "bad_datanode_tracker.h"
  29. #include "ClientNamenodeProtocol.pb.h"
  30. #include <mutex>
  31. #include <iostream>
  32. namespace hdfs {
  33. class BlockReader;
  34. class BlockReaderOptions;
  35. class DataNodeConnection;
  36. /*
  37. * FileHandle: coordinates operations on a particular file in HDFS
  38. *
  39. * Threading model: not thread-safe; consumers and io_service should not call
  40. * concurrently. PositionRead is the exceptions; they can be
  41. * called concurrently and repeatedly.
  42. * Lifetime: pointer returned to consumer by FileSystem::Open. Consumer is
  43. * resonsible for freeing the object.
  44. */
  45. class FileHandleImpl : public FileHandle {
  46. public:
  47. MEMCHECKED_CLASS(FileHandleImpl)
  48. FileHandleImpl(const std::string & cluster_name,
  49. const std::string & path,
  50. ::asio::io_service *io_service, const std::string &client_name,
  51. const std::shared_ptr<const struct FileInfo> file_info,
  52. std::shared_ptr<BadDataNodeTracker> bad_data_nodes,
  53. std::shared_ptr<LibhdfsEvents> event_handlers);
  54. /*
  55. * [Some day reliably] Reads a particular offset into the data file.
  56. * On error, bytes_read returns the number of bytes successfully read; on
  57. * success, bytes_read will equal nbyte
  58. */
  59. void PositionRead(
  60. void *buf,
  61. size_t nbyte,
  62. uint64_t offset,
  63. const std::function<void(const Status &status, size_t bytes_read)> &handler
  64. ) override;
  65. /**
  66. * Note: The nbyte argument for Read and Pread as well as the
  67. * offset argument for Seek are in/out parameters.
  68. *
  69. * For Read and Pread the value referenced by nbyte should
  70. * be set to the number of bytes to read. Before returning
  71. * the value referenced will be set by the callee to the number
  72. * of bytes that was successfully read.
  73. *
  74. * For Seek the value referenced by offset should be the number
  75. * of bytes to shift from the specified whence position. The
  76. * referenced value will be set to the new offset before returning.
  77. **/
  78. Status PositionRead(void *buf, size_t *bytes_read, off_t offset) override;
  79. Status Read(void *buf, size_t *nbyte) override;
  80. Status Seek(off_t *offset, std::ios_base::seekdir whence) override;
  81. /*
  82. * Reads some amount of data into the buffer. Will attempt to find the best
  83. * datanode and read data from it.
  84. *
  85. * If an error occurs during connection or transfer, the callback will be
  86. * called with bytes_read equal to the number of bytes successfully transferred.
  87. * If no data nodes can be found, status will be Status::ResourceUnavailable.
  88. *
  89. */
  90. void AsyncPreadSome(size_t offset, const MutableBuffers &buffers,
  91. std::shared_ptr<NodeExclusionRule> excluded_nodes,
  92. const std::function<void(const Status &status,
  93. const std::string &dn_id, size_t bytes_read)> handler);
  94. /**
  95. * Cancels all operations instantiated from this FileHandle.
  96. * Will set a flag to abort continuation pipelines when they try to move to the next step.
  97. * Closes TCP connections to Datanode in order to abort pipelines waiting on slow IO.
  98. **/
  99. virtual void CancelOperations(void) override;
  100. virtual void SetFileEventCallback(file_event_callback callback) override;
  101. /**
  102. * Ephemeral objects created by the filehandle will need to get the event
  103. * handler registry owned by the FileSystem.
  104. **/
  105. std::shared_ptr<LibhdfsEvents> get_event_handlers();
  106. protected:
  107. virtual std::shared_ptr<BlockReader> CreateBlockReader(const BlockReaderOptions &options,
  108. std::shared_ptr<DataNodeConnection> dn);
  109. virtual std::shared_ptr<DataNodeConnection> CreateDataNodeConnection(
  110. ::asio::io_service *io_service,
  111. const ::hadoop::hdfs::DatanodeInfoProto & dn,
  112. const hadoop::common::TokenProto * token);
  113. private:
  114. const std::string cluster_name_;
  115. const std::string path_;
  116. ::asio::io_service * const io_service_;
  117. const std::string client_name_;
  118. const std::shared_ptr<const struct FileInfo> file_info_;
  119. std::shared_ptr<BadDataNodeTracker> bad_node_tracker_;
  120. bool CheckSeekBounds(ssize_t desired_position);
  121. off_t offset_;
  122. CancelHandle cancel_state_;
  123. ReaderGroup readers_;
  124. std::shared_ptr<LibhdfsEvents> event_handlers_;
  125. };
  126. }
  127. #endif