filehandle.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 "reader/fileinfo.h"
  25. #include "reader/readergroup.h"
  26. #include "asio.hpp"
  27. #include "bad_datanode_tracker.h"
  28. #include "ClientNamenodeProtocol.pb.h"
  29. #include <mutex>
  30. #include <iostream>
  31. namespace hdfs {
  32. class BlockReader;
  33. class BlockReaderOptions;
  34. class DataNodeConnection;
  35. /*
  36. * FileHandle: coordinates operations on a particular file in HDFS
  37. *
  38. * Threading model: not thread-safe; consumers and io_service should not call
  39. * concurrently. PositionRead is the exceptions; they can be
  40. * called concurrently and repeatedly.
  41. * Lifetime: pointer returned to consumer by FileSystem::Open. Consumer is
  42. * resonsible for freeing the object.
  43. */
  44. class FileHandleImpl : public FileHandle {
  45. public:
  46. FileHandleImpl(const std::string & cluster_name,
  47. const std::string & path,
  48. ::asio::io_service *io_service, const std::string &client_name,
  49. const std::shared_ptr<const struct FileInfo> file_info,
  50. std::shared_ptr<BadDataNodeTracker> bad_data_nodes,
  51. std::shared_ptr<LibhdfsEvents> event_handlers);
  52. /*
  53. * [Some day reliably] Reads a particular offset into the data file.
  54. * On error, bytes_read returns the number of bytes successfully read; on
  55. * success, bytes_read will equal nbyte
  56. */
  57. void PositionRead(
  58. void *buf,
  59. size_t nbyte,
  60. uint64_t offset,
  61. const std::function<void(const Status &status, size_t bytes_read)> &handler
  62. ) override;
  63. /**
  64. * Note: The nbyte argument for Read and Pread as well as the
  65. * offset argument for Seek are in/out parameters.
  66. *
  67. * For Read and Pread the value referenced by nbyte should
  68. * be set to the number of bytes to read. Before returning
  69. * the value referenced will be set by the callee to the number
  70. * of bytes that was successfully read.
  71. *
  72. * For Seek the value referenced by offset should be the number
  73. * of bytes to shift from the specified whence position. The
  74. * referenced value will be set to the new offset before returning.
  75. **/
  76. Status PositionRead(void *buf, size_t *bytes_read, off_t offset) override;
  77. Status Read(void *buf, size_t *nbyte) override;
  78. Status Seek(off_t *offset, std::ios_base::seekdir whence) override;
  79. /*
  80. * Reads some amount of data into the buffer. Will attempt to find the best
  81. * datanode and read data from it.
  82. *
  83. * If an error occurs during connection or transfer, the callback will be
  84. * called with bytes_read equal to the number of bytes successfully transferred.
  85. * If no data nodes can be found, status will be Status::ResourceUnavailable.
  86. *
  87. */
  88. void AsyncPreadSome(size_t offset, const MutableBuffers &buffers,
  89. std::shared_ptr<NodeExclusionRule> excluded_nodes,
  90. const std::function<void(const Status &status,
  91. const std::string &dn_id, size_t bytes_read)> handler);
  92. /**
  93. * Cancels all operations instantiated from this FileHandle.
  94. * Will set a flag to abort continuation pipelines when they try to move to the next step.
  95. * Closes TCP connections to Datanode in order to abort pipelines waiting on slow IO.
  96. **/
  97. virtual void CancelOperations(void) override;
  98. virtual void SetFileEventCallback(file_event_callback callback) override;
  99. /**
  100. * Ephemeral objects created by the filehandle will need to get the event
  101. * handler registry owned by the FileSystem.
  102. **/
  103. std::shared_ptr<LibhdfsEvents> get_event_handlers();
  104. protected:
  105. virtual std::shared_ptr<BlockReader> CreateBlockReader(const BlockReaderOptions &options,
  106. std::shared_ptr<DataNodeConnection> dn);
  107. virtual std::shared_ptr<DataNodeConnection> CreateDataNodeConnection(
  108. ::asio::io_service *io_service,
  109. const ::hadoop::hdfs::DatanodeInfoProto & dn,
  110. const hadoop::common::TokenProto * token);
  111. private:
  112. const std::string cluster_name_;
  113. const std::string path_;
  114. ::asio::io_service * const io_service_;
  115. const std::string client_name_;
  116. const std::shared_ptr<const struct FileInfo> file_info_;
  117. std::shared_ptr<BadDataNodeTracker> bad_node_tracker_;
  118. bool CheckSeekBounds(ssize_t desired_position);
  119. off_t offset_;
  120. CancelHandle cancel_state_;
  121. ReaderGroup readers_;
  122. std::shared_ptr<LibhdfsEvents> event_handlers_;
  123. };
  124. }
  125. #endif