filesystem.h 4.2 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_FILESYSTEM_H_
  19. #define LIBHDFSPP_LIB_FS_FILESYSTEM_H_
  20. #include "filehandle.h"
  21. #include "common/hdfs_public_api.h"
  22. #include "common/async_stream.h"
  23. #include "hdfspp/hdfspp.h"
  24. #include "fs/bad_datanode_tracker.h"
  25. #include "rpc/rpc_engine.h"
  26. #include "reader/block_reader.h"
  27. #include "reader/fileinfo.h"
  28. #include "ClientNamenodeProtocol.pb.h"
  29. #include "ClientNamenodeProtocol.hrpc.inl"
  30. #include "asio.hpp"
  31. #include <thread>
  32. namespace hdfs {
  33. /**
  34. * NameNodeConnection: abstracts the details of communicating with a NameNode
  35. * and the implementation of the communications protocol.
  36. *
  37. * Will eventually handle retry and failover.
  38. *
  39. * Threading model: thread-safe; all operations can be called concurrently
  40. * Lifetime: owned by a FileSystemImpl
  41. */
  42. class NameNodeOperations {
  43. public:
  44. NameNodeOperations(::asio::io_service *io_service, const Options &options,
  45. const std::string &client_name, const char *protocol_name,
  46. int protocol_version) :
  47. io_service_(io_service),
  48. engine_(io_service, options, client_name, protocol_name, protocol_version),
  49. namenode_(& engine_) {}
  50. void Connect(const std::string &server,
  51. const std::string &service,
  52. std::function<void(const Status &)> &&handler);
  53. void GetBlockLocations(const std::string & path,
  54. std::function<void(const Status &, std::shared_ptr<const struct FileInfo>)> handler);
  55. private:
  56. ::asio::io_service * io_service_;
  57. RpcEngine engine_;
  58. ClientNamenodeProtocol namenode_;
  59. };
  60. /*
  61. * FileSystem: The consumer's main point of interaction with the cluster as
  62. * a whole.
  63. *
  64. * Initially constructed in a disconnected state; call Connect before operating
  65. * on the FileSystem.
  66. *
  67. * All open files must be closed before the FileSystem is destroyed.
  68. *
  69. * Threading model: thread-safe for all operations
  70. * Lifetime: pointer created for consumer who is responsible for deleting it
  71. */
  72. class FileSystemImpl : public FileSystem {
  73. public:
  74. FileSystemImpl(IoService *&io_service, const Options &options);
  75. ~FileSystemImpl() override;
  76. /* attempt to connect to namenode, return bad status on failure */
  77. void Connect(const std::string &server, const std::string &service,
  78. const std::function<void(const Status &, FileSystem *)> &&handler) override;
  79. /* attempt to connect to namenode, return bad status on failure */
  80. Status Connect(const std::string &server, const std::string &service) override;
  81. virtual void Open(const std::string &path,
  82. const std::function<void(const Status &, FileHandle *)>
  83. &handler) override;
  84. Status Open(const std::string &path, FileHandle **handle) override;
  85. /* add a new thread to handle asio requests, return number of threads in pool
  86. */
  87. int AddWorkerThread();
  88. /* how many worker threads are servicing asio requests */
  89. int WorkerThreadCount() { return worker_threads_.size(); }
  90. private:
  91. /**
  92. * The IoService must be the first member variable to ensure that it gets
  93. * destroyed last. This allows other members to dequeue things from the
  94. * service in their own destructors.
  95. **/
  96. std::unique_ptr<IoServiceImpl> io_service_;
  97. NameNodeOperations nn_;
  98. const std::string client_name_;
  99. std::shared_ptr<BadDataNodeTracker> bad_node_tracker_;
  100. struct WorkerDeleter {
  101. void operator()(std::thread *t);
  102. };
  103. typedef std::unique_ptr<std::thread, WorkerDeleter> WorkerPtr;
  104. std::vector<WorkerPtr> worker_threads_;
  105. };
  106. }
  107. #endif