filehandle.h 4.7 KB

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