namenode_operations.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_NAMENODEOPERATIONS_H_
  19. #define LIBHDFSPP_LIB_FS_NAMENODEOPERATIONS_H_
  20. #include "rpc/rpc_engine.h"
  21. #include "hdfspp/statinfo.h"
  22. #include "hdfspp/fsinfo.h"
  23. #include "hdfspp/content_summary.h"
  24. #include "common/namenode_info.h"
  25. #include "ClientNamenodeProtocol.pb.h"
  26. #include "ClientNamenodeProtocol.hrpc.inl"
  27. #include <memory>
  28. #include <string>
  29. namespace hdfs {
  30. /**
  31. * NameNodeConnection: abstracts the details of communicating with a NameNode
  32. * and the implementation of the communications protocol.
  33. *
  34. * Will eventually handle retry and failover.
  35. *
  36. * Threading model: thread-safe; all operations can be called concurrently
  37. * Lifetime: owned by a FileSystemImpl
  38. */
  39. class NameNodeOperations {
  40. public:
  41. MEMCHECKED_CLASS(NameNodeOperations)
  42. NameNodeOperations(std::shared_ptr<IoService> io_service, const Options &options,
  43. const std::shared_ptr<std::string> &client_name, const std::string &user_name,
  44. const char *protocol_name, int protocol_version) :
  45. io_service_(io_service),
  46. engine_(std::make_shared<RpcEngine>(io_service, options, client_name, user_name, protocol_name, protocol_version)),
  47. namenode_(engine_), options_(options) {}
  48. void Connect(const std::string &cluster_name,
  49. const std::vector<ResolvedNamenodeInfo> &servers,
  50. std::function<void(const Status &)> &&handler);
  51. bool CancelPendingConnect();
  52. void GetBlockLocations(const std::string & path, uint64_t offset, uint64_t length,
  53. std::function<void(const Status &, std::shared_ptr<const struct FileInfo>)> handler);
  54. void GetPreferredBlockSize(const std::string & path,
  55. std::function<void(const Status &, const uint64_t)> handler);
  56. void SetReplication(const std::string & path, int16_t replication,
  57. std::function<void(const Status &)> handler);
  58. void SetTimes(const std::string & path, uint64_t mtime, uint64_t atime,
  59. std::function<void(const Status &)> handler);
  60. void GetFileInfo(const std::string & path,
  61. std::function<void(const Status &, const StatInfo &)> handler);
  62. void GetContentSummary(const std::string & path,
  63. std::function<void(const Status &, const ContentSummary &)> handler);
  64. void GetFsStats(std::function<void(const Status &, const FsInfo &)> handler);
  65. // start_after="" for initial call
  66. void GetListing(const std::string & path,
  67. std::function<void(const Status &, const std::vector<StatInfo> &, bool)> handler,
  68. const std::string & start_after = "");
  69. void Mkdirs(const std::string & path, uint16_t permissions, bool createparent,
  70. std::function<void(const Status &)> handler);
  71. void Delete(const std::string & path, bool recursive,
  72. std::function<void(const Status &)> handler);
  73. void Rename(const std::string & oldPath, const std::string & newPath,
  74. std::function<void(const Status &)> handler);
  75. void SetPermission(const std::string & path, uint16_t permissions,
  76. std::function<void(const Status &)> handler);
  77. void SetOwner(const std::string & path, const std::string & username,
  78. const std::string & groupname, std::function<void(const Status &)> handler);
  79. void CreateSnapshot(const std::string & path, const std::string & name,
  80. std::function<void(const Status &)> handler);
  81. void DeleteSnapshot(const std::string & path, const std::string & name,
  82. std::function<void(const Status &)> handler);
  83. void RenameSnapshot(const std::string & path, const std::string & old_name, const std::string & new_name,
  84. std::function<void(const Status &)> handler);
  85. void AllowSnapshot(const std::string & path,
  86. std::function<void(const Status &)> handler);
  87. void DisallowSnapshot(const std::string & path,
  88. std::function<void(const Status &)> handler);
  89. void SetFsEventCallback(fs_event_callback callback);
  90. private:
  91. static void HdfsFileStatusProtoToStatInfo(hdfs::StatInfo & si, const ::hadoop::hdfs::HdfsFileStatusProto & fs);
  92. static void ContentSummaryProtoToContentSummary(hdfs::ContentSummary & content_summary, const ::hadoop::hdfs::ContentSummaryProto & csp);
  93. static void DirectoryListingProtoToStatInfo(std::shared_ptr<std::vector<StatInfo>> stat_infos, const ::hadoop::hdfs::DirectoryListingProto & dl);
  94. static void GetFsStatsResponseProtoToFsInfo(hdfs::FsInfo & fs_info, const std::shared_ptr<::hadoop::hdfs::GetFsStatsResponseProto> & fs);
  95. std::shared_ptr<IoService> io_service_;
  96. // This is the only permanent owner of the RpcEngine, however the RPC layer
  97. // needs to reference count it prevent races during FileSystem destruction.
  98. // In order to do this they hold weak_ptrs and promote them to shared_ptr
  99. // when calling non-blocking RpcEngine methods e.g. get_client_id().
  100. std::shared_ptr<RpcEngine> engine_;
  101. // Automatically generated methods for RPC calls. See protoc_gen_hrpc.cc
  102. ClientNamenodeProtocol namenode_;
  103. const Options options_;
  104. };
  105. }
  106. #endif