filesystem.cc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #include "filesystem.h"
  19. #include "common/util.h"
  20. #include <asio/ip/tcp.hpp>
  21. #include <limits>
  22. namespace hdfs {
  23. static const char kNamenodeProtocol[] = "org.apache.hadoop.hdfs.protocol.ClientProtocol";
  24. static const int kNamenodeProtocolVersion = 1;
  25. using ::asio::ip::tcp;
  26. FileSystem::~FileSystem()
  27. {}
  28. Status FileSystem::New(IoService *io_service, const char *server,
  29. unsigned short port, FileSystem **fsptr) {
  30. std::unique_ptr<FileSystemImpl> impl(new FileSystemImpl(io_service));
  31. Status stat = impl->Connect(server, port);
  32. if (stat.ok()) {
  33. *fsptr = impl.release();
  34. }
  35. return stat;
  36. }
  37. FileSystemImpl::FileSystemImpl(IoService *io_service)
  38. : io_service_(static_cast<IoServiceImpl*>(io_service))
  39. , engine_(&io_service_->io_service(), RpcEngine::GetRandomClientName(),
  40. kNamenodeProtocol, kNamenodeProtocolVersion)
  41. , namenode_(&engine_)
  42. {}
  43. Status FileSystemImpl::Connect(const char *server, unsigned short port) {
  44. asio::error_code ec;
  45. tcp::resolver resolver(io_service_->io_service());
  46. tcp::resolver::query query(tcp::v4(), server, std::to_string(port));
  47. tcp::resolver::iterator iterator = resolver.resolve(query, ec);
  48. if (ec) {
  49. return ToStatus(ec);
  50. }
  51. std::vector<tcp::endpoint> servers(iterator, tcp::resolver::iterator());
  52. Status stat = engine_.Connect(servers);
  53. if (!stat.ok()) {
  54. return stat;
  55. }
  56. engine_.Start();
  57. return stat;
  58. }
  59. Status FileSystemImpl::Open(const char *path, InputStream **isptr) {
  60. using ::hadoop::hdfs::GetBlockLocationsRequestProto;
  61. using ::hadoop::hdfs::GetBlockLocationsResponseProto;
  62. GetBlockLocationsRequestProto req;
  63. auto resp = std::make_shared<GetBlockLocationsResponseProto>();
  64. req.set_src(path);
  65. req.set_offset(0);
  66. req.set_length(std::numeric_limits<long long>::max());
  67. Status stat = namenode_.GetBlockLocations(&req, resp);
  68. if (!stat.ok()) {
  69. return stat;
  70. }
  71. *isptr = new InputStreamImpl(this, &resp->locations());
  72. return Status::OK();
  73. }
  74. }