referencedfilelist.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*##############################################################################
  2. Copyright (C) 2012 HPCC Systems.
  3. All rights reserved. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. #include "referencedfilelist.hpp"
  15. #include "jptree.hpp"
  16. #include "workunit.hpp"
  17. #include "eclhelper.hpp"
  18. #include "dautils.hpp"
  19. #include "dadfs.hpp"
  20. #include "dasess.hpp"
  21. #define WF_LOOKUP_TIMEOUT (1000*15) // 15 seconds
  22. bool getIsOpt(const IPropertyTree &graphNode)
  23. {
  24. if (graphNode.hasProp("att[@name='_isOpt']"))
  25. return graphNode.getPropBool("att[@name='_isOpt']/@value", false);
  26. else
  27. return graphNode.getPropBool("att[@name='_isIndexOpt']/@value", false);
  28. }
  29. const char *skipForeign(const char *name, StringBuffer *ip)
  30. {
  31. if (*name=='~')
  32. name++;
  33. const char *d1 = strstr(name, "::");
  34. if (d1)
  35. {
  36. StringBuffer cmp;
  37. if (strieq("foreign", cmp.append(d1-name, name).trim().str()))
  38. {
  39. // foreign scope - need to strip off the ip and port
  40. d1 += 2; // skip ::
  41. const char *d2 = strstr(d1,"::");
  42. if (d2)
  43. {
  44. if (ip)
  45. ip->append(d2-d1, d1).trim();
  46. d2 += 2;
  47. while (*d2 == ' ')
  48. d2++;
  49. name = d2;
  50. }
  51. }
  52. }
  53. return name;
  54. }
  55. class ReferencedFileList;
  56. class ReferencedFile : public CInterface, implements IReferencedFile
  57. {
  58. public:
  59. IMPLEMENT_IINTERFACE;
  60. ReferencedFile(const char *name, bool isSubFile=false, unsigned _flags=0) : flags(_flags)
  61. {
  62. StringBuffer ip;
  63. StringBuffer lc(skipForeign(name, &ip));
  64. logicalName.set(lc.toLowerCase());
  65. if (ip.length())
  66. foreignNode.setown(createINode(ip.str()));
  67. if (isSubFile)
  68. flags |= RefSubFile;
  69. }
  70. void reset()
  71. {
  72. flags &= RefSubFile;
  73. }
  74. IPropertyTree *getForeignOrRemoteFileTree(IUserDescriptor *user, INode *remote);
  75. void processLocalFileInfo(IDistributedFile *df, const char *cluster, StringArray *subfiles);
  76. void processRemoteFileTree(IPropertyTree *tree, bool foreign, StringArray *subfiles);
  77. void resolveLocal(const char *cluster, IUserDescriptor *user, StringArray *subfiles);
  78. void resolveRemote(IUserDescriptor *user, INode *remote, const char *cluster, bool checkLocalFirst, StringArray *subfiles);
  79. void resolve(const char *cluster, IUserDescriptor *user, INode *remote, bool checkLocalFirst, StringArray *subfiles);
  80. virtual const char *getLogicalName() const {return logicalName.get();}
  81. virtual unsigned getFlags() const {return flags;}
  82. virtual const SocketEndpoint &getForeignIP() const {return foreignNode->endpoint();}
  83. virtual void cloneInfo(IDFUhelper *helper, IUserDescriptor *user, INode *remote, const char *cluster, bool overwrite=false);
  84. void cloneSuperInfo(ReferencedFileList *list, IUserDescriptor *user, INode *remote, bool overwrite);
  85. public:
  86. StringAttr logicalName;
  87. Owned<INode> foreignNode;
  88. unsigned flags;
  89. };
  90. class ReferencedFileList : public CInterface, implements IReferencedFileList
  91. {
  92. public:
  93. IMPLEMENT_IINTERFACE;
  94. ReferencedFileList(const char *username, const char *pw)
  95. {
  96. if (username && pw)
  97. {
  98. user.setown(createUserDescriptor());
  99. user->set(username, pw);
  100. }
  101. }
  102. void ensureFile(const char *ln, unsigned flags);
  103. virtual void addFile(const char *ln);
  104. virtual void addFiles(StringArray &files);
  105. virtual void addFilesFromWorkUnit(IConstWorkUnit *cw);
  106. virtual void addFilesFromQuery(IConstWorkUnit *cw, const IHpccPackageMap *pm, const char *queryid);
  107. virtual IReferencedFileIterator *getFiles();
  108. virtual void cloneFileInfo(IDFUhelper *helper, bool overwrite, bool cloneSuperInfo);
  109. virtual void cloneRelationships();
  110. virtual void cloneAllInfo(IDFUhelper *helper, bool overwrite, bool cloneSuperInfo)
  111. {
  112. cloneFileInfo(helper, overwrite, cloneSuperInfo);
  113. cloneRelationships();
  114. }
  115. virtual void resolveFiles(const char *process, const char *remoteIP, bool checkLocalFirst, bool addSubFiles);
  116. void resolveSubFiles(StringArray &subfiles, bool checkLocalFirst);
  117. public:
  118. Owned<IUserDescriptor> user;
  119. Owned<INode> remote;
  120. MapStringToMyClass<ReferencedFile> map;
  121. StringAttr process;
  122. };
  123. void ReferencedFile::processLocalFileInfo(IDistributedFile *df, const char *cluster, StringArray *subfiles)
  124. {
  125. IDistributedSuperFile *super = df->querySuperFile();
  126. if (super)
  127. {
  128. flags |= RefFileSuper;
  129. if (subfiles)
  130. {
  131. Owned<IDistributedFileIterator> it = super->getSubFileIterator(true); //supersub = true, no need to deal with LOCAL supersubs
  132. ForEach(*it)
  133. {
  134. IDistributedFile &sub = it->query();
  135. StringBuffer name;
  136. sub.getLogicalName(name);
  137. subfiles->append(name.str());
  138. }
  139. }
  140. }
  141. else
  142. {
  143. if (!cluster || !*cluster)
  144. return;
  145. if (df->findCluster(cluster)==NotFound)
  146. flags |= RefFileNotOnCluster;
  147. }
  148. }
  149. void ReferencedFile::processRemoteFileTree(IPropertyTree *tree, bool foreign, StringArray *subfiles)
  150. {
  151. flags |= RefFileRemote;
  152. if (foreign)
  153. flags |= RefFileForeign;
  154. if (streq(tree->queryName(), queryDfsXmlBranchName(DXB_SuperFile)))
  155. {
  156. flags |= RefFileSuper;
  157. if (subfiles)
  158. {
  159. Owned<IPropertyTreeIterator> it = tree->getElements("SubFile");
  160. ForEach(*it)
  161. subfiles->append(it->query().queryProp("@name"));
  162. }
  163. }
  164. }
  165. void ReferencedFile::resolveLocal(const char *cluster, IUserDescriptor *user, StringArray *subfiles)
  166. {
  167. if (flags & RefFileInPackage)
  168. return;
  169. reset();
  170. Owned<IDistributedFile> df = queryDistributedFileDirectory().lookup(logicalName.get(), user);
  171. if(df)
  172. processLocalFileInfo(df, cluster, subfiles);
  173. else
  174. flags |= RefFileNotFound;
  175. }
  176. IPropertyTree *ReferencedFile::getForeignOrRemoteFileTree(IUserDescriptor *user, INode *remote)
  177. {
  178. IDistributedFileDirectory &dir = queryDistributedFileDirectory();
  179. Owned<IPropertyTree> tree;
  180. if (foreignNode)
  181. tree.setown(dir.getFileTree(logicalName.get(), user, foreignNode, WF_LOOKUP_TIMEOUT, false, false));
  182. if (!tree && remote)
  183. tree.setown(dir.getFileTree(logicalName.get(), user, remote, WF_LOOKUP_TIMEOUT, false, false));
  184. return tree.getClear();
  185. }
  186. void ReferencedFile::resolveRemote(IUserDescriptor *user, INode *remote, const char *cluster, bool checkLocalFirst, StringArray *subfiles)
  187. {
  188. if (flags & RefFileInPackage)
  189. return;
  190. reset();
  191. if (checkLocalFirst)
  192. {
  193. Owned<IDistributedFile> df = queryDistributedFileDirectory().lookup(logicalName.get(), user);
  194. if(df)
  195. {
  196. processLocalFileInfo(df, cluster, subfiles);
  197. return;
  198. }
  199. }
  200. Owned<IPropertyTree> tree = getForeignOrRemoteFileTree(user, remote);
  201. if (tree)
  202. processRemoteFileTree(tree, false, subfiles);
  203. else
  204. flags |= RefFileNotFound;
  205. }
  206. void ReferencedFile::resolve(const char *cluster, IUserDescriptor *user, INode *remote, bool checkLocalFirst, StringArray *subfiles)
  207. {
  208. if (foreignNode || remote)
  209. resolveRemote(user, remote, cluster, checkLocalFirst, subfiles);
  210. else
  211. resolveLocal(cluster, user, subfiles);
  212. }
  213. void ReferencedFile::cloneInfo(IDFUhelper *helper, IUserDescriptor *user, INode *remote, const char *cluster, bool overwrite)
  214. {
  215. if ((flags & RefFileCloned) || (flags & RefFileSuper) || (flags & RefFileInPackage))
  216. return;
  217. if (!(flags & (RefFileRemote | RefFileForeign | RefFileNotOnCluster)))
  218. return;
  219. StringBuffer addr;
  220. if (flags & RefFileForeign)
  221. foreignNode->endpoint().getUrlStr(addr);
  222. else if (remote)
  223. remote->endpoint().getUrlStr(addr);
  224. try
  225. {
  226. helper->createSingleFileClone(logicalName.get(), logicalName.get(), cluster,
  227. DFUcpdm_c_replicated_by_d, true, NULL, user, addr.str(), NULL, overwrite, false);
  228. flags |= RefFileCloned;
  229. }
  230. catch (IException *e)
  231. {
  232. flags |= RefFileCopyInfoFailed;
  233. DBGLOG(e);
  234. e->Release();
  235. }
  236. catch (...)
  237. {
  238. flags |= RefFileCopyInfoFailed;
  239. DBGLOG("Unknown error copying file info for %s, from %s", logicalName.sget(), addr.str());
  240. }
  241. }
  242. void ReferencedFile::cloneSuperInfo(ReferencedFileList *list, IUserDescriptor *user, INode *remote, bool overwrite)
  243. {
  244. if ((flags & RefFileCloned) || (flags & RefFileInPackage) || !(flags & RefFileSuper) || !(flags & RefFileRemote))
  245. return;
  246. try
  247. {
  248. Owned<IPropertyTree> tree = getForeignOrRemoteFileTree(user, remote);
  249. if (!tree)
  250. return;
  251. IDistributedFileDirectory &dir = queryDistributedFileDirectory();
  252. Owned<IDistributedFile> df = dir.lookup(logicalName.get(), user);
  253. if(df)
  254. {
  255. if (!overwrite)
  256. return;
  257. df->detach();
  258. df.clear();
  259. }
  260. Owned<IDistributedSuperFile> superfile = dir.createSuperFile(logicalName.get(),user, true, false);
  261. flags |= RefFileCloned;
  262. Owned<IPropertyTreeIterator> subfiles = tree->getElements("SubFile");
  263. ForEach(*subfiles)
  264. {
  265. const char *name = subfiles->query().queryProp("@name");
  266. if (list)
  267. {
  268. //ensure superfile in superfile is cloned, before add
  269. ReferencedFile *subref = list->map.getValue(name);
  270. if (subref)
  271. subref->cloneSuperInfo(list, user, remote, overwrite);
  272. }
  273. if (name && *name)
  274. superfile->addSubFile(name, false, NULL, false);
  275. }
  276. }
  277. catch (IException *e)
  278. {
  279. flags |= RefFileCopyInfoFailed;
  280. DBGLOG(e);
  281. e->Release();
  282. }
  283. catch (...)
  284. {
  285. flags |= RefFileCopyInfoFailed;
  286. DBGLOG("Unknown error copying superfile info for %s", logicalName.get());
  287. }
  288. }
  289. class ReferencedFileIterator : public CInterface, implements IReferencedFileIterator
  290. {
  291. public:
  292. IMPLEMENT_IINTERFACE;
  293. ReferencedFileIterator(ReferencedFileList *_list)
  294. {
  295. list.set(_list);
  296. iter.setown(new HashIterator(list->map));
  297. }
  298. virtual bool first()
  299. {
  300. return iter->first();
  301. }
  302. virtual bool next()
  303. {
  304. return iter->next();
  305. }
  306. virtual bool isValid()
  307. {
  308. return iter->isValid();
  309. }
  310. virtual IReferencedFile & query()
  311. {
  312. return *list->map.mapToValue(&iter->query());
  313. }
  314. virtual ReferencedFile & queryObject()
  315. {
  316. return *(list->map.mapToValue(&iter->query()));
  317. }
  318. public:
  319. Owned<ReferencedFileList> list;
  320. Owned<HashIterator> iter;
  321. };
  322. void ReferencedFileList::ensureFile(const char *ln, unsigned flags)
  323. {
  324. Owned<ReferencedFile> file = new ReferencedFile(ln, false, flags);
  325. if (!file->logicalName.length())
  326. return;
  327. ReferencedFile *existing = map.getValue(file->getLogicalName());
  328. if (existing)
  329. existing->flags |= flags;
  330. else
  331. {
  332. const char *refln = file->getLogicalName();
  333. map.setValue(refln, file.getClear());
  334. }
  335. }
  336. void ReferencedFileList::addFile(const char *ln)
  337. {
  338. ensureFile(ln, 0);
  339. }
  340. void ReferencedFileList::addFiles(StringArray &files)
  341. {
  342. ForEachItemIn(i, files)
  343. addFile(files.item(i));
  344. }
  345. void ReferencedFileList::addFilesFromQuery(IConstWorkUnit *cw, const IHpccPackageMap *pm, const char *queryid)
  346. {
  347. const IHpccPackage *pkg = NULL;
  348. if (pm && queryid && *queryid)
  349. pkg = pm->matchPackage(queryid);
  350. Owned<IConstWUGraphIterator> graphs = &cw->getGraphs(GraphTypeActivities);
  351. ForEach(*graphs)
  352. {
  353. Owned <IPropertyTree> xgmml = graphs->query().getXGMMLTree(false);
  354. Owned<IPropertyTreeIterator> iter = xgmml->getElements("//node[att/@name='_*ileName']");
  355. ForEach(*iter)
  356. {
  357. IPropertyTree &node = iter->query();
  358. const char *logicalName = node.queryProp("att[@name='_fileName']/@value");
  359. if (!logicalName)
  360. logicalName = node.queryProp("att[@name='_indexFileName']/@value");
  361. if (!logicalName)
  362. continue;
  363. ThorActivityKind kind = (ThorActivityKind) node.getPropInt("att[@name='_kind']/@value", TAKnone);
  364. //not likely to be part of roxie queries, but for forward compatibility:
  365. if(kind==TAKdiskwrite || kind==TAKindexwrite || kind==TAKcsvwrite || kind==TAKxmlwrite)
  366. continue;
  367. if (node.getPropBool("att[@name='_isSpill']/@value") ||
  368. node.getPropBool("att[@name='_isTransformSpill']/@value"))
  369. continue;
  370. unsigned flags = 0;
  371. if (pkg && pkg->hasSuperFile(logicalName))
  372. {
  373. flags |= (RefFileSuper | RefFileInPackage);
  374. Owned<ISimpleSuperFileEnquiry> ssfe = pkg->resolveSuperFile(logicalName);
  375. if (ssfe && ssfe->numSubFiles()>0)
  376. {
  377. unsigned count = ssfe->numSubFiles();
  378. while (count--)
  379. {
  380. StringBuffer subfile;
  381. ssfe->getSubFileName(count, subfile);
  382. ensureFile(subfile, RefSubFile | RefFileInPackage);
  383. }
  384. }
  385. }
  386. ensureFile(logicalName, flags);
  387. }
  388. }
  389. }
  390. void ReferencedFileList::addFilesFromWorkUnit(IConstWorkUnit *cw)
  391. {
  392. addFilesFromQuery(cw, NULL, NULL);
  393. }
  394. void ReferencedFileList::resolveSubFiles(StringArray &subfiles, bool checkLocalFirst)
  395. {
  396. StringArray childSubFiles;
  397. ForEachItemIn(i, subfiles)
  398. {
  399. Owned<ReferencedFile> file = new ReferencedFile(subfiles.item(i), true);
  400. if (file->logicalName.length() && !map.getValue(file->getLogicalName()))
  401. {
  402. file->resolve(process.get(), user, remote, checkLocalFirst, &childSubFiles);
  403. const char *ln = file->getLogicalName();
  404. map.setValue(ln, file.getClear());
  405. }
  406. }
  407. if (childSubFiles.length())
  408. resolveSubFiles(childSubFiles, checkLocalFirst);
  409. }
  410. void ReferencedFileList::resolveFiles(const char *_process, const char *remoteIP, bool checkLocalFirst, bool expandSuperFiles)
  411. {
  412. process.set(_process);
  413. remote.setown((remoteIP && *remoteIP) ? createINode(remoteIP, 7070) : NULL);
  414. StringArray subfiles;
  415. {
  416. ReferencedFileIterator files(this);
  417. ForEach(files)
  418. files.queryObject().resolve(process, user, remote, checkLocalFirst, expandSuperFiles ? &subfiles : NULL);
  419. }
  420. if (expandSuperFiles)
  421. resolveSubFiles(subfiles, checkLocalFirst);
  422. }
  423. void ReferencedFileList::cloneFileInfo(IDFUhelper *helper, bool overwrite, bool cloneSuperInfo)
  424. {
  425. ReferencedFileIterator files(this);
  426. ForEach(files)
  427. files.queryObject().cloneInfo(helper, user, remote, process, overwrite);
  428. if (cloneSuperInfo)
  429. ForEach(files)
  430. files.queryObject().cloneSuperInfo(this, user, remote, overwrite);
  431. }
  432. void ReferencedFileList::cloneRelationships()
  433. {
  434. if (!remote || remote->endpoint().isNull())
  435. return;
  436. StringBuffer addr;
  437. remote->endpoint().getUrlStr(addr);
  438. IDistributedFileDirectory &dir = queryDistributedFileDirectory();
  439. ReferencedFileIterator files(this);
  440. ForEach(files)
  441. {
  442. ReferencedFile &file = files.queryObject();
  443. if (!(file.getFlags() & RefFileRemote))
  444. continue;
  445. Owned<IFileRelationshipIterator> iter = dir.lookupFileRelationships(file.getLogicalName(), NULL,
  446. NULL, NULL, NULL, NULL, NULL, addr.str(), WF_LOOKUP_TIMEOUT);
  447. ForEach(*iter)
  448. {
  449. IFileRelationship &r=iter->query();
  450. const char* assoc = r.querySecondaryFilename();
  451. if (!assoc)
  452. continue;
  453. if (*assoc == '~')
  454. assoc++;
  455. IReferencedFile *refAssoc = map.getValue(assoc);
  456. if (refAssoc && !(refAssoc->getFlags() & RefFileCopyInfoFailed))
  457. {
  458. dir.addFileRelationship(file.getLogicalName(), assoc, r.queryPrimaryFields(), r.querySecondaryFields(),
  459. r.queryKind(), r.queryCardinality(), r.isPayload(), user, r.queryDescription());
  460. }
  461. }
  462. }
  463. }
  464. IReferencedFileIterator *ReferencedFileList::getFiles()
  465. {
  466. return new ReferencedFileIterator(this);
  467. }
  468. IReferencedFileList *createReferencedFileList(const char *user, const char *pw)
  469. {
  470. return new ReferencedFileList(user, pw);
  471. }