filesystem.cc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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/namenode_info.h"
  20. #include <functional>
  21. #include <limits>
  22. #include <future>
  23. #include <tuple>
  24. #include <iostream>
  25. #include <pwd.h>
  26. #define FMT_THIS_ADDR "this=" << (void*)this
  27. namespace hdfs {
  28. static const char kNamenodeProtocol[] =
  29. "org.apache.hadoop.hdfs.protocol.ClientProtocol";
  30. static const int kNamenodeProtocolVersion = 1;
  31. using ::asio::ip::tcp;
  32. static constexpr uint16_t kDefaultPort = 8020;
  33. /*****************************************************************************
  34. * FILESYSTEM BASE CLASS
  35. ****************************************************************************/
  36. FileSystem * FileSystem::New(
  37. IoService *&io_service, const std::string &user_name, const Options &options) {
  38. return new FileSystemImpl(io_service, user_name, options);
  39. }
  40. /*****************************************************************************
  41. * FILESYSTEM IMPLEMENTATION
  42. ****************************************************************************/
  43. const std::string get_effective_user_name(const std::string &user_name) {
  44. if (!user_name.empty())
  45. return user_name;
  46. // If no user name was provided, try the HADOOP_USER_NAME and USER environment
  47. // variables
  48. const char * env = getenv("HADOOP_USER_NAME");
  49. if (env) {
  50. return env;
  51. }
  52. env = getenv("USER");
  53. if (env) {
  54. return env;
  55. }
  56. // If running on POSIX, use the currently logged in user
  57. #if defined(_POSIX_VERSION)
  58. uid_t uid = geteuid();
  59. struct passwd *pw = getpwuid(uid);
  60. if (pw && pw->pw_name)
  61. {
  62. return pw->pw_name;
  63. }
  64. #endif
  65. return "unknown_user";
  66. }
  67. FileSystemImpl::FileSystemImpl(IoService *&io_service, const std::string &user_name, const Options &options) :
  68. options_(options), client_name_(GetRandomClientName()), io_service_(
  69. static_cast<IoServiceImpl *>(io_service)),
  70. nn_(
  71. &io_service_->io_service(), options, client_name_,
  72. get_effective_user_name(user_name), kNamenodeProtocol,
  73. kNamenodeProtocolVersion
  74. ), bad_node_tracker_(std::make_shared<BadDataNodeTracker>()),
  75. event_handlers_(std::make_shared<LibhdfsEvents>()) {
  76. LOG_TRACE(kFileSystem, << "FileSystemImpl::FileSystemImpl("
  77. << FMT_THIS_ADDR << ") called");
  78. // Poor man's move
  79. io_service = nullptr;
  80. /* spawn background threads for asio delegation */
  81. unsigned int threads = 1 /* options.io_threads_, pending HDFS-9117 */;
  82. for (unsigned int i = 0; i < threads; i++) {
  83. AddWorkerThread();
  84. }
  85. }
  86. FileSystemImpl::~FileSystemImpl() {
  87. LOG_TRACE(kFileSystem, << "FileSystemImpl::~FileSystemImpl("
  88. << FMT_THIS_ADDR << ") called");
  89. /**
  90. * Note: IoService must be stopped before getting rid of worker threads.
  91. * Once worker threads are joined and deleted the service can be deleted.
  92. **/
  93. io_service_->Stop();
  94. worker_threads_.clear();
  95. }
  96. void FileSystemImpl::Connect(const std::string &server,
  97. const std::string &service,
  98. const std::function<void(const Status &, FileSystem * fs)> &handler) {
  99. LOG_INFO(kFileSystem, << "FileSystemImpl::Connect(" << FMT_THIS_ADDR
  100. << ", server=" << server << ", service="
  101. << service << ") called");
  102. /* IoService::New can return nullptr */
  103. if (!io_service_) {
  104. handler (Status::Error("Null IoService"), this);
  105. }
  106. // DNS lookup here for namenode(s)
  107. std::vector<ResolvedNamenodeInfo> resolved_namenodes;
  108. auto name_service = options_.services.find(server);
  109. if(name_service != options_.services.end()) {
  110. cluster_name_ = name_service->first;
  111. resolved_namenodes = BulkResolve(&io_service_->io_service(), name_service->second);
  112. } else {
  113. cluster_name_ = server + ":" + service;
  114. // tmp namenode info just to get this in the right format for BulkResolve
  115. NamenodeInfo tmp_info;
  116. optional<URI> uri = URI::parse_from_string("hdfs://" + cluster_name_);
  117. if(!uri) {
  118. LOG_ERROR(kFileSystem, << "Unable to use URI for cluster " << cluster_name_);
  119. handler(Status::Error(("Invalid namenode " + cluster_name_ + " in config").c_str()), this);
  120. }
  121. tmp_info.uri = uri.value();
  122. resolved_namenodes = BulkResolve(&io_service_->io_service(), {tmp_info});
  123. }
  124. for(unsigned int i=0;i<resolved_namenodes.size();i++) {
  125. LOG_DEBUG(kFileSystem, << "Resolved Namenode");
  126. LOG_DEBUG(kFileSystem, << resolved_namenodes[i].str());
  127. }
  128. nn_.Connect(cluster_name_, /*server, service*/ resolved_namenodes, [this, handler](const Status & s) {
  129. handler(s, this);
  130. });
  131. }
  132. Status FileSystemImpl::Connect(const std::string &server, const std::string &service) {
  133. LOG_INFO(kFileSystem, << "FileSystemImpl::[sync]Connect(" << FMT_THIS_ADDR
  134. << ", server=" << server << ", service=" << service << ") called");
  135. /* synchronized */
  136. auto stat = std::make_shared<std::promise<Status>>();
  137. std::future<Status> future = stat->get_future();
  138. auto callback = [stat](const Status &s, FileSystem *fs) {
  139. (void)fs;
  140. stat->set_value(s);
  141. };
  142. Connect(server, service, callback);
  143. /* block until promise is set */
  144. auto s = future.get();
  145. return s;
  146. }
  147. void FileSystemImpl::ConnectToDefaultFs(const std::function<void(const Status &, FileSystem *)> &handler) {
  148. std::string scheme = options_.defaultFS.get_scheme();
  149. if (strcasecmp(scheme.c_str(), "hdfs") != 0) {
  150. std::string error_message;
  151. error_message += "defaultFS of [" + options_.defaultFS.str() + "] is not supported";
  152. handler(Status::InvalidArgument(error_message.c_str()), nullptr);
  153. return;
  154. }
  155. std::string host = options_.defaultFS.get_host();
  156. if (host.empty()) {
  157. handler(Status::InvalidArgument("defaultFS must specify a hostname"), nullptr);
  158. return;
  159. }
  160. optional<uint16_t> port = options_.defaultFS.get_port();
  161. if (!port) {
  162. port = kDefaultPort;
  163. }
  164. std::string port_as_string = std::to_string(*port);
  165. Connect(host, port_as_string, handler);
  166. }
  167. Status FileSystemImpl::ConnectToDefaultFs() {
  168. auto stat = std::make_shared<std::promise<Status>>();
  169. std::future<Status> future = stat->get_future();
  170. auto callback = [stat](const Status &s, FileSystem *fs) {
  171. (void)fs;
  172. stat->set_value(s);
  173. };
  174. ConnectToDefaultFs(callback);
  175. /* block until promise is set */
  176. auto s = future.get();
  177. return s;
  178. }
  179. int FileSystemImpl::AddWorkerThread() {
  180. LOG_DEBUG(kFileSystem, << "FileSystemImpl::AddWorkerThread("
  181. << FMT_THIS_ADDR << ") called."
  182. << " Existing thread count = " << worker_threads_.size());
  183. auto service_task = [](IoService *service) { service->Run(); };
  184. worker_threads_.push_back(
  185. WorkerPtr(new std::thread(service_task, io_service_.get())));
  186. return worker_threads_.size();
  187. }
  188. void FileSystemImpl::Open(
  189. const std::string &path,
  190. const std::function<void(const Status &, FileHandle *)> &handler) {
  191. LOG_INFO(kFileSystem, << "FileSystemImpl::Open("
  192. << FMT_THIS_ADDR << ", path="
  193. << path << ") called");
  194. nn_.GetBlockLocations(path, [this, path, handler](const Status &stat, std::shared_ptr<const struct FileInfo> file_info) {
  195. if(!stat.ok()) {
  196. LOG_INFO(kFileSystem, << "FileSystemImpl::Open failed to get block locations. status=" << stat.ToString());
  197. if(stat.get_server_exception_type() == Status::kStandbyException) {
  198. LOG_INFO(kFileSystem, << "Operation not allowed on standby datanode");
  199. }
  200. }
  201. handler(stat, stat.ok() ? new FileHandleImpl(cluster_name_, path, &io_service_->io_service(), client_name_, file_info, bad_node_tracker_, event_handlers_)
  202. : nullptr);
  203. });
  204. }
  205. Status FileSystemImpl::Open(const std::string &path,
  206. FileHandle **handle) {
  207. LOG_INFO(kFileSystem, << "FileSystemImpl::[sync]Open("
  208. << FMT_THIS_ADDR << ", path="
  209. << path << ") called");
  210. auto callstate = std::make_shared<std::promise<std::tuple<Status, FileHandle*>>>();
  211. std::future<std::tuple<Status, FileHandle*>> future(callstate->get_future());
  212. /* wrap async FileSystem::Open with promise to make it a blocking call */
  213. auto h = [callstate](const Status &s, FileHandle *is) {
  214. callstate->set_value(std::make_tuple(s, is));
  215. };
  216. Open(path, h);
  217. /* block until promise is set */
  218. auto returnstate = future.get();
  219. Status stat = std::get<0>(returnstate);
  220. FileHandle *file_handle = std::get<1>(returnstate);
  221. if (!stat.ok()) {
  222. delete file_handle;
  223. return stat;
  224. }
  225. if (!file_handle) {
  226. return stat;
  227. }
  228. *handle = file_handle;
  229. return stat;
  230. }
  231. BlockLocation LocatedBlockToBlockLocation(const hadoop::hdfs::LocatedBlockProto & locatedBlock)
  232. {
  233. BlockLocation result;
  234. result.setCorrupt(locatedBlock.corrupt());
  235. result.setOffset(locatedBlock.offset());
  236. std::vector<DNInfo> dn_info;
  237. dn_info.reserve(locatedBlock.locs_size());
  238. for (const hadoop::hdfs::DatanodeInfoProto & datanode_info: locatedBlock.locs()) {
  239. const hadoop::hdfs::DatanodeIDProto &id = datanode_info.id();
  240. DNInfo newInfo;
  241. if (id.has_ipaddr())
  242. newInfo.setIPAddr(id.ipaddr());
  243. if (id.has_hostname())
  244. newInfo.setHostname(id.hostname());
  245. if (id.has_xferport())
  246. newInfo.setXferPort(id.xferport());
  247. if (id.has_infoport())
  248. newInfo.setInfoPort(id.infoport());
  249. if (id.has_ipcport())
  250. newInfo.setIPCPort(id.ipcport());
  251. if (id.has_infosecureport())
  252. newInfo.setInfoSecurePort(id.infosecureport());
  253. dn_info.push_back(newInfo);
  254. }
  255. result.setDataNodes(dn_info);
  256. if (locatedBlock.has_b()) {
  257. const hadoop::hdfs::ExtendedBlockProto & b=locatedBlock.b();
  258. result.setLength(b.numbytes());
  259. }
  260. return result;
  261. }
  262. void FileSystemImpl::GetBlockLocations(const std::string & path,
  263. const std::function<void(const Status &, std::shared_ptr<FileBlockLocation> locations)> handler)
  264. {
  265. LOG_DEBUG(kFileSystem, << "FileSystemImpl::GetBlockLocations("
  266. << FMT_THIS_ADDR << ", path="
  267. << path << ") called");
  268. auto conversion = [handler](const Status & status, std::shared_ptr<const struct FileInfo> fileInfo) {
  269. if (status.ok()) {
  270. auto result = std::make_shared<FileBlockLocation>();
  271. result->setFileLength(fileInfo->file_length_);
  272. result->setLastBlockComplete(fileInfo->last_block_complete_);
  273. result->setUnderConstruction(fileInfo->under_construction_);
  274. std::vector<BlockLocation> blocks;
  275. for (const hadoop::hdfs::LocatedBlockProto & locatedBlock: fileInfo->blocks_) {
  276. auto newLocation = LocatedBlockToBlockLocation(locatedBlock);
  277. blocks.push_back(newLocation);
  278. }
  279. result->setBlockLocations(blocks);
  280. handler(status, result);
  281. } else {
  282. handler(status, std::shared_ptr<FileBlockLocation>());
  283. }
  284. };
  285. nn_.GetBlockLocations(path, conversion);
  286. }
  287. Status FileSystemImpl::GetBlockLocations(const std::string & path,
  288. std::shared_ptr<FileBlockLocation> * fileBlockLocations)
  289. {
  290. LOG_DEBUG(kFileSystem, << "FileSystemImpl::[sync]GetBlockLocations("
  291. << FMT_THIS_ADDR << ", path="
  292. << path << ") called");
  293. if (!fileBlockLocations)
  294. return Status::InvalidArgument("Null pointer passed to GetBlockLocations");
  295. auto callstate = std::make_shared<std::promise<std::tuple<Status, std::shared_ptr<FileBlockLocation>>>>();
  296. std::future<std::tuple<Status, std::shared_ptr<FileBlockLocation>>> future(callstate->get_future());
  297. /* wrap async call with promise/future to make it blocking */
  298. auto callback = [callstate](const Status &s, std::shared_ptr<FileBlockLocation> blockInfo) {
  299. callstate->set_value(std::make_tuple(s,blockInfo));
  300. };
  301. GetBlockLocations(path, callback);
  302. /* wait for async to finish */
  303. auto returnstate = future.get();
  304. auto stat = std::get<0>(returnstate);
  305. if (!stat.ok()) {
  306. return stat;
  307. }
  308. *fileBlockLocations = std::get<1>(returnstate);
  309. return stat;
  310. }
  311. void FileSystemImpl::GetFileInfo(
  312. const std::string &path,
  313. const std::function<void(const Status &, const StatInfo &)> &handler) {
  314. LOG_DEBUG(kFileSystem, << "FileSystemImpl::GetFileInfo("
  315. << FMT_THIS_ADDR << ", path="
  316. << path << ") called");
  317. nn_.GetFileInfo(path, handler);
  318. }
  319. Status FileSystemImpl::GetFileInfo(const std::string &path,
  320. StatInfo & stat_info) {
  321. LOG_DEBUG(kFileSystem, << "FileSystemImpl::[sync]GetFileInfo("
  322. << FMT_THIS_ADDR << ", path="
  323. << path << ") called");
  324. auto callstate = std::make_shared<std::promise<std::tuple<Status, StatInfo>>>();
  325. std::future<std::tuple<Status, StatInfo>> future(callstate->get_future());
  326. /* wrap async FileSystem::GetFileInfo with promise to make it a blocking call */
  327. auto h = [callstate](const Status &s, const StatInfo &si) {
  328. callstate->set_value(std::make_tuple(s, si));
  329. };
  330. GetFileInfo(path, h);
  331. /* block until promise is set */
  332. auto returnstate = future.get();
  333. Status stat = std::get<0>(returnstate);
  334. StatInfo info = std::get<1>(returnstate);
  335. if (!stat.ok()) {
  336. return stat;
  337. }
  338. stat_info = info;
  339. return stat;
  340. }
  341. void FileSystemImpl::GetFsStats(
  342. const std::function<void(const Status &, const FsInfo &)> &handler) {
  343. LOG_DEBUG(kFileSystem,
  344. << "FileSystemImpl::GetFsStats(" << FMT_THIS_ADDR << ") called");
  345. nn_.GetFsStats(handler);
  346. }
  347. Status FileSystemImpl::GetFsStats(FsInfo & fs_info) {
  348. LOG_DEBUG(kFileSystem,
  349. << "FileSystemImpl::[sync]GetFsStats(" << FMT_THIS_ADDR << ") called");
  350. auto callstate = std::make_shared<std::promise<std::tuple<Status, FsInfo>>>();
  351. std::future<std::tuple<Status, FsInfo>> future(callstate->get_future());
  352. /* wrap async FileSystem::GetFsStats with promise to make it a blocking call */
  353. auto h = [callstate](const Status &s, const FsInfo &si) {
  354. callstate->set_value(std::make_tuple(s, si));
  355. };
  356. GetFsStats(h);
  357. /* block until promise is set */
  358. auto returnstate = future.get();
  359. Status stat = std::get<0>(returnstate);
  360. FsInfo info = std::get<1>(returnstate);
  361. if (!stat.ok()) {
  362. return stat;
  363. }
  364. fs_info = info;
  365. return stat;
  366. }
  367. /**
  368. * Helper function for recursive GetListing calls.
  369. *
  370. * Some compilers don't like recursive lambdas, so we make the lambda call a
  371. * method, which in turn creates a lambda calling itself.
  372. */
  373. void FileSystemImpl::GetListingShim(const Status &stat, std::shared_ptr<std::vector<StatInfo>> &stat_infos, bool has_more,
  374. std::string path,
  375. const std::function<bool(const Status &, std::shared_ptr<std::vector<StatInfo>>&, bool)> &handler) {
  376. bool has_next = stat_infos && stat_infos->size() > 0;
  377. bool get_more = handler(stat, stat_infos, has_more && has_next);
  378. if (get_more && has_more && has_next ) {
  379. auto callback = [this, path, handler](const Status &stat, std::shared_ptr<std::vector<StatInfo>> &stat_infos, bool has_more) {
  380. GetListingShim(stat, stat_infos, has_more, path, handler);
  381. };
  382. std::string last = stat_infos->back().path;
  383. nn_.GetListing(path, callback, last);
  384. }
  385. }
  386. void FileSystemImpl::GetListing(
  387. const std::string &path,
  388. const std::function<bool(const Status &, std::shared_ptr<std::vector<StatInfo>>&, bool)> &handler) {
  389. LOG_INFO(kFileSystem, << "FileSystemImpl::GetListing("
  390. << FMT_THIS_ADDR << ", path="
  391. << path << ") called");
  392. // Caputure the state and push it into the shim
  393. auto callback = [this, path, handler](const Status &stat, std::shared_ptr<std::vector<StatInfo>> &stat_infos, bool has_more) {
  394. GetListingShim(stat, stat_infos, has_more, path, handler);
  395. };
  396. nn_.GetListing(path, callback);
  397. }
  398. Status FileSystemImpl::GetListing(const std::string &path, std::shared_ptr<std::vector<StatInfo>> &stat_infos) {
  399. LOG_INFO(kFileSystem, << "FileSystemImpl::[sync]GetListing("
  400. << FMT_THIS_ADDR << ", path="
  401. << path << ") called");
  402. // In this case, we're going to allocate the result on the heap and have the
  403. // async code populate it.
  404. auto results = std::make_shared<std::vector<StatInfo>>();
  405. auto callstate = std::make_shared<std::promise<Status>>();
  406. std::future<Status> future(callstate->get_future());
  407. /* wrap async FileSystem::GetListing with promise to make it a blocking call.
  408. *
  409. Keep requesting more until we get the entire listing, and don't set the promise
  410. * until we have the entire listing.
  411. */
  412. auto h = [callstate, results](const Status &s, std::shared_ptr<std::vector<StatInfo>> si, bool has_more) -> bool {
  413. if (si) {
  414. results->insert(results->end(), si->begin(), si->end());
  415. }
  416. bool done = !s.ok() || !has_more;
  417. if (done) {
  418. callstate->set_value(s);
  419. return false;
  420. }
  421. return true;
  422. };
  423. GetListing(path, h);
  424. /* block until promise is set */
  425. Status stat = future.get();
  426. if (!stat.ok()) {
  427. return stat;
  428. }
  429. stat_infos = results;
  430. return stat;
  431. }
  432. void FileSystemImpl::Mkdirs(const std::string & path, long permissions, bool createparent,
  433. std::function<void(const Status &)> handler) {
  434. LOG_DEBUG(kFileSystem,
  435. << "FileSystemImpl::Mkdirs(" << FMT_THIS_ADDR << ", path=" << path <<
  436. ", permissions=" << permissions << ", createparent=" << createparent << ") called");
  437. if (path.empty()) {
  438. handler(Status::InvalidArgument("Mkdirs: argument 'path' cannot be empty"));
  439. return;
  440. }
  441. nn_.Mkdirs(path, permissions, createparent, handler);
  442. }
  443. Status FileSystemImpl::Mkdirs(const std::string & path, long permissions, bool createparent) {
  444. LOG_DEBUG(kFileSystem,
  445. << "FileSystemImpl::[sync]Mkdirs(" << FMT_THIS_ADDR << ", path=" << path <<
  446. ", permissions=" << permissions << ", createparent=" << createparent << ") called");
  447. auto callstate = std::make_shared<std::promise<Status>>();
  448. std::future<Status> future(callstate->get_future());
  449. /* wrap async FileSystem::Mkdirs with promise to make it a blocking call */
  450. auto h = [callstate](const Status &s) {
  451. callstate->set_value(s);
  452. };
  453. Mkdirs(path, permissions, createparent, h);
  454. /* block until promise is set */
  455. auto returnstate = future.get();
  456. Status stat = returnstate;
  457. return stat;
  458. }
  459. void FileSystemImpl::Delete(const std::string &path, bool recursive,
  460. const std::function<void(const Status &)> &handler) {
  461. LOG_DEBUG(kFileSystem,
  462. << "FileSystemImpl::Delete(" << FMT_THIS_ADDR << ", path=" << path << ", recursive=" << recursive << ") called");
  463. if (path.empty()) {
  464. handler(Status::InvalidArgument("Delete: argument 'path' cannot be empty"));
  465. return;
  466. }
  467. nn_.Delete(path, recursive, handler);
  468. }
  469. Status FileSystemImpl::Delete(const std::string &path, bool recursive) {
  470. LOG_DEBUG(kFileSystem,
  471. << "FileSystemImpl::[sync]Delete(" << FMT_THIS_ADDR << ", path=" << path << ", recursive=" << recursive << ") called");
  472. auto callstate = std::make_shared<std::promise<Status>>();
  473. std::future<Status> future(callstate->get_future());
  474. /* wrap async FileSystem::Delete with promise to make it a blocking call */
  475. auto h = [callstate](const Status &s) {
  476. callstate->set_value(s);
  477. };
  478. Delete(path, recursive, h);
  479. /* block until promise is set */
  480. auto returnstate = future.get();
  481. Status stat = returnstate;
  482. return stat;
  483. }
  484. void FileSystemImpl::Rename(const std::string &oldPath, const std::string &newPath,
  485. const std::function<void(const Status &)> &handler) {
  486. LOG_DEBUG(kFileSystem,
  487. << "FileSystemImpl::Rename(" << FMT_THIS_ADDR << ", oldPath=" << oldPath << ", newPath=" << newPath << ") called");
  488. if (oldPath.empty()) {
  489. handler(Status::InvalidArgument("Rename: argument 'oldPath' cannot be empty"));
  490. return;
  491. }
  492. if (newPath.empty()) {
  493. handler(Status::InvalidArgument("Rename: argument 'newPath' cannot be empty"));
  494. return;
  495. }
  496. nn_.Rename(oldPath, newPath, handler);
  497. }
  498. Status FileSystemImpl::Rename(const std::string &oldPath, const std::string &newPath) {
  499. LOG_DEBUG(kFileSystem,
  500. << "FileSystemImpl::[sync]Rename(" << FMT_THIS_ADDR << ", oldPath=" << oldPath << ", newPath=" << newPath << ") called");
  501. auto callstate = std::make_shared<std::promise<Status>>();
  502. std::future<Status> future(callstate->get_future());
  503. /* wrap async FileSystem::Rename with promise to make it a blocking call */
  504. auto h = [callstate](const Status &s) {
  505. callstate->set_value(s);
  506. };
  507. Rename(oldPath, newPath, h);
  508. /* block until promise is set */
  509. auto returnstate = future.get();
  510. Status stat = returnstate;
  511. return stat;
  512. }
  513. void FileSystemImpl::SetPermission(const std::string & path,
  514. short permissions, const std::function<void(const Status &)> &handler) {
  515. LOG_DEBUG(kFileSystem,
  516. << "FileSystemImpl::SetPermission(" << FMT_THIS_ADDR << ", path=" << path << ", permissions=" << permissions << ") called");
  517. if (path.empty()) {
  518. handler(Status::InvalidArgument("SetPermission: argument 'path' cannot be empty"));
  519. return;
  520. }
  521. Status permStatus = NameNodeOperations::CheckValidPermissionMask(permissions);
  522. if (!permStatus.ok()) {
  523. handler(permStatus);
  524. return;
  525. }
  526. nn_.SetPermission(path, permissions, handler);
  527. }
  528. Status FileSystemImpl::SetPermission(const std::string & path, short permissions) {
  529. LOG_DEBUG(kFileSystem,
  530. << "FileSystemImpl::[sync]SetPermission(" << FMT_THIS_ADDR << ", path=" << path << ", permissions=" << permissions << ") called");
  531. auto callstate = std::make_shared<std::promise<Status>>();
  532. std::future<Status> future(callstate->get_future());
  533. /* wrap async FileSystem::SetPermission with promise to make it a blocking call */
  534. auto h = [callstate](const Status &s) {
  535. callstate->set_value(s);
  536. };
  537. SetPermission(path, permissions, h);
  538. /* block until promise is set */
  539. Status stat = future.get();
  540. return stat;
  541. }
  542. void FileSystemImpl::SetOwner(const std::string & path, const std::string & username,
  543. const std::string & groupname, const std::function<void(const Status &)> &handler) {
  544. LOG_DEBUG(kFileSystem,
  545. << "FileSystemImpl::SetOwner(" << FMT_THIS_ADDR << ", path=" << path << ", username=" << username << ", groupname=" << groupname << ") called");
  546. if (path.empty()) {
  547. handler(Status::InvalidArgument("SetOwner: argument 'path' cannot be empty"));
  548. return;
  549. }
  550. nn_.SetOwner(path, username, groupname, handler);
  551. }
  552. Status FileSystemImpl::SetOwner(const std::string & path,
  553. const std::string & username, const std::string & groupname) {
  554. LOG_DEBUG(kFileSystem,
  555. << "FileSystemImpl::[sync]SetOwner(" << FMT_THIS_ADDR << ", path=" << path << ", username=" << username << ", groupname=" << groupname << ") called");
  556. auto callstate = std::make_shared<std::promise<Status>>();
  557. std::future<Status> future(callstate->get_future());
  558. /* wrap async FileSystem::SetOwner with promise to make it a blocking call */
  559. auto h = [callstate](const Status &s) {
  560. callstate->set_value(s);
  561. };
  562. SetOwner(path, username, groupname, h);
  563. /* block until promise is set */
  564. Status stat = future.get();
  565. return stat;
  566. }
  567. void FileSystemImpl::CreateSnapshot(const std::string &path,
  568. const std::string &name,
  569. const std::function<void(const Status &)> &handler) {
  570. LOG_DEBUG(kFileSystem,
  571. << "FileSystemImpl::CreateSnapshot(" << FMT_THIS_ADDR << ", path=" << path << ", name=" << name << ") called");
  572. if (path.empty()) {
  573. handler(Status::InvalidArgument("CreateSnapshot: argument 'path' cannot be empty"));
  574. return;
  575. }
  576. nn_.CreateSnapshot(path, name, handler);
  577. }
  578. Status FileSystemImpl::CreateSnapshot(const std::string &path,
  579. const std::string &name) {
  580. LOG_DEBUG(kFileSystem,
  581. << "FileSystemImpl::[sync]CreateSnapshot(" << FMT_THIS_ADDR << ", path=" << path << ", name=" << name << ") called");
  582. auto callstate = std::make_shared<std::promise<Status>>();
  583. std::future<Status> future(callstate->get_future());
  584. /* wrap async FileSystem::CreateSnapshot with promise to make it a blocking call */
  585. auto h = [callstate](const Status &s) {
  586. callstate->set_value(s);
  587. };
  588. CreateSnapshot(path, name, h);
  589. /* block until promise is set */
  590. auto returnstate = future.get();
  591. Status stat = returnstate;
  592. return stat;
  593. }
  594. void FileSystemImpl::DeleteSnapshot(const std::string &path,
  595. const std::string &name,
  596. const std::function<void(const Status &)> &handler) {
  597. LOG_DEBUG(kFileSystem,
  598. << "FileSystemImpl::DeleteSnapshot(" << FMT_THIS_ADDR << ", path=" << path << ", name=" << name << ") called");
  599. if (path.empty()) {
  600. handler(Status::InvalidArgument("DeleteSnapshot: argument 'path' cannot be empty"));
  601. return;
  602. }
  603. if (name.empty()) {
  604. handler(Status::InvalidArgument("DeleteSnapshot: argument 'name' cannot be empty"));
  605. return;
  606. }
  607. nn_.DeleteSnapshot(path, name, handler);
  608. }
  609. Status FileSystemImpl::DeleteSnapshot(const std::string &path,
  610. const std::string &name) {
  611. LOG_DEBUG(kFileSystem,
  612. << "FileSystemImpl::[sync]DeleteSnapshot(" << FMT_THIS_ADDR << ", path=" << path << ", name=" << name << ") called");
  613. auto callstate = std::make_shared<std::promise<Status>>();
  614. std::future<Status> future(callstate->get_future());
  615. /* wrap async FileSystem::DeleteSnapshot with promise to make it a blocking call */
  616. auto h = [callstate](const Status &s) {
  617. callstate->set_value(s);
  618. };
  619. DeleteSnapshot(path, name, h);
  620. /* block until promise is set */
  621. auto returnstate = future.get();
  622. Status stat = returnstate;
  623. return stat;
  624. }
  625. void FileSystemImpl::AllowSnapshot(const std::string &path,
  626. const std::function<void(const Status &)> &handler) {
  627. LOG_DEBUG(kFileSystem,
  628. << "FileSystemImpl::AllowSnapshot(" << FMT_THIS_ADDR << ", path=" << path << ") called");
  629. if (path.empty()) {
  630. handler(Status::InvalidArgument("AllowSnapshot: argument 'path' cannot be empty"));
  631. return;
  632. }
  633. nn_.AllowSnapshot(path, handler);
  634. }
  635. Status FileSystemImpl::AllowSnapshot(const std::string &path) {
  636. LOG_DEBUG(kFileSystem,
  637. << "FileSystemImpl::[sync]AllowSnapshot(" << FMT_THIS_ADDR << ", path=" << path << ") called");
  638. auto callstate = std::make_shared<std::promise<Status>>();
  639. std::future<Status> future(callstate->get_future());
  640. /* wrap async FileSystem::AllowSnapshot with promise to make it a blocking call */
  641. auto h = [callstate](const Status &s) {
  642. callstate->set_value(s);
  643. };
  644. AllowSnapshot(path, h);
  645. /* block until promise is set */
  646. auto returnstate = future.get();
  647. Status stat = returnstate;
  648. return stat;
  649. }
  650. void FileSystemImpl::DisallowSnapshot(const std::string &path,
  651. const std::function<void(const Status &)> &handler) {
  652. LOG_DEBUG(kFileSystem,
  653. << "FileSystemImpl::DisallowSnapshot(" << FMT_THIS_ADDR << ", path=" << path << ") called");
  654. if (path.empty()) {
  655. handler(Status::InvalidArgument("DisallowSnapshot: argument 'path' cannot be empty"));
  656. return;
  657. }
  658. nn_.DisallowSnapshot(path, handler);
  659. }
  660. Status FileSystemImpl::DisallowSnapshot(const std::string &path) {
  661. LOG_DEBUG(kFileSystem,
  662. << "FileSystemImpl::[sync]DisallowSnapshot(" << FMT_THIS_ADDR << ", path=" << path << ") called");
  663. auto callstate = std::make_shared<std::promise<Status>>();
  664. std::future<Status> future(callstate->get_future());
  665. /* wrap async FileSystem::DisallowSnapshot with promise to make it a blocking call */
  666. auto h = [callstate](const Status &s) {
  667. callstate->set_value(s);
  668. };
  669. DisallowSnapshot(path, h);
  670. /* block until promise is set */
  671. auto returnstate = future.get();
  672. Status stat = returnstate;
  673. return stat;
  674. }
  675. void FileSystemImpl::WorkerDeleter::operator()(std::thread *t) {
  676. // It is far too easy to destroy the filesystem (and thus the threadpool)
  677. // from within one of the worker threads, leading to a deadlock. Let's
  678. // provide some explicit protection.
  679. if(t->get_id() == std::this_thread::get_id()) {
  680. LOG_ERROR(kFileSystem, << "FileSystemImpl::WorkerDeleter::operator(treadptr="
  681. << t << ") : FATAL: Attempted to destroy a thread pool"
  682. "from within a callback of the thread pool!");
  683. }
  684. t->join();
  685. delete t;
  686. }
  687. void FileSystemImpl::SetFsEventCallback(fs_event_callback callback) {
  688. if (event_handlers_) {
  689. event_handlers_->set_fs_callback(callback);
  690. nn_.SetFsEventCallback(callback);
  691. }
  692. }
  693. std::shared_ptr<LibhdfsEvents> FileSystemImpl::get_event_handlers() {
  694. return event_handlers_;
  695. }
  696. }