referencedfilelist.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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 "dasds.hpp"
  20. #include "dadfs.hpp"
  21. #include "dasess.hpp"
  22. #define WF_LOOKUP_TIMEOUT (1000*15) // 15 seconds
  23. bool getIsOpt(const IPropertyTree &graphNode)
  24. {
  25. if (graphNode.hasProp("att[@name='_isOpt']"))
  26. return graphNode.getPropBool("att[@name='_isOpt']/@value", false);
  27. else
  28. return graphNode.getPropBool("att[@name='_isIndexOpt']/@value", false);
  29. }
  30. bool checkForeign(const char *lfn)
  31. {
  32. if (*lfn=='~')
  33. lfn++;
  34. static size_t l = strlen("foreign");
  35. if (strnicmp("foreign", lfn, l)==0)
  36. {
  37. lfn+=l;
  38. while (isspace(*lfn))
  39. lfn++;
  40. if (lfn[0]==':' && lfn[1]==':')
  41. return true;
  42. }
  43. return false;
  44. }
  45. const char *skipForeign(const char *name, StringBuffer *ip)
  46. {
  47. if (*name=='~')
  48. name++;
  49. const char *d1 = strstr(name, "::");
  50. if (d1)
  51. {
  52. StringBuffer cmp;
  53. if (strieq("foreign", cmp.append(d1-name, name).trim().str()))
  54. {
  55. // foreign scope - need to strip off the ip and port
  56. d1 += 2; // skip ::
  57. const char *d2 = strstr(d1,"::");
  58. if (d2)
  59. {
  60. if (ip)
  61. ip->append(d2-d1, d1).trim();
  62. d2 += 2;
  63. while (*d2 == ' ')
  64. d2++;
  65. name = d2;
  66. }
  67. }
  68. }
  69. return name;
  70. }
  71. void splitDfsLocation(const char *address, StringBuffer &cluster, StringBuffer &ip, StringBuffer &prefix, const char *defaultCluster)
  72. {
  73. if (!address || !*address)
  74. {
  75. cluster.append(defaultCluster);
  76. return;
  77. }
  78. const char *s=strchr(address, '@');
  79. if (s)
  80. {
  81. cluster.append(s - address, address);
  82. address = s + 1;
  83. }
  84. else if (defaultCluster && *defaultCluster)
  85. cluster.append(defaultCluster);
  86. s=strchr(address, '/');
  87. if (!s)
  88. ip.append(address);
  89. else
  90. {
  91. ip.append(s - address, address);
  92. prefix.append(s+1);
  93. }
  94. }
  95. void splitDerivedDfsLocation(const char *address, StringBuffer &cluster, StringBuffer &ip, StringBuffer &prefix, const char *defaultCluster, const char *baseCluster, const char *baseIP, const char *basePrefix)
  96. {
  97. if (address && *address)
  98. {
  99. splitDfsLocation(address, cluster, ip, prefix, defaultCluster);
  100. return;
  101. }
  102. ip.append(baseIP);
  103. cluster.append(baseCluster);
  104. prefix.append(basePrefix);
  105. }
  106. class ReferencedFileList;
  107. class ReferencedFile : public CInterface, implements IReferencedFile
  108. {
  109. public:
  110. IMPLEMENT_IINTERFACE;
  111. ReferencedFile(const char *lfn, const char *sourceIP, const char *srcCluster, const char *prefix, bool isSubFile, unsigned _flags, const char *_pkgid, bool noDfs) : flags(_flags), pkgid(_pkgid), noDfsResolution(noDfs)
  112. {
  113. logicalName.set(skipForeign(lfn, &daliip)).toLowerCase();
  114. if (daliip.length())
  115. flags |= RefFileForeign;
  116. else
  117. daliip.set(sourceIP);
  118. fileSrcCluster.set(srcCluster);
  119. filePrefix.set(prefix);
  120. if (isSubFile)
  121. flags |= RefSubFile;
  122. }
  123. void reset()
  124. {
  125. flags &= RefSubFile;
  126. }
  127. IPropertyTree *getRemoteFileTree(IUserDescriptor *user, INode *remote, const char *remotePrefix);
  128. IPropertyTree *getSpecifiedOrRemoteFileTree(IUserDescriptor *user, INode *remote, const char *remotePrefix);
  129. void processLocalFileInfo(IDistributedFile *df, const char *dstCluster, const char *srcCluster, StringArray *subfiles);
  130. void processRemoteFileTree(IPropertyTree *tree, const char *srcCluster, StringArray *subfiles);
  131. void resolveLocal(const char *dstCluster, const char *srcCluster, IUserDescriptor *user, StringArray *subfiles);
  132. void resolveRemote(IUserDescriptor *user, INode *remote, const char *remotePrefix, const char *dstCluster, const char *srcCluster, bool checkLocalFirst, StringArray *subfiles, bool resolveForeign=false);
  133. void resolve(const char *dstCluster, const char *srcCluster, IUserDescriptor *user, INode *remote, const char *remotePrefix, bool checkLocalFirst, StringArray *subfiles, bool resolveForeign=false);
  134. virtual const char *getLogicalName() const {return logicalName.str();}
  135. virtual unsigned getFlags() const {return flags;}
  136. virtual const SocketEndpoint &getForeignIP(SocketEndpoint &ep) const
  137. {
  138. if (flags & RefFileForeign && daliip.length())
  139. ep.set(daliip.str());
  140. else
  141. ep.set(NULL);
  142. return ep;
  143. }
  144. virtual void cloneInfo(IDFUhelper *helper, IUserDescriptor *user, const char *dstCluster, const char *srcCluster, bool overwrite=false, bool cloneForeign=false);
  145. void cloneSuperInfo(ReferencedFileList *list, IUserDescriptor *user, INode *remote, bool overwrite);
  146. virtual const char *queryPackageId() const {return pkgid.get();}
  147. public:
  148. StringBuffer logicalName;
  149. StringAttr pkgid;
  150. StringBuffer daliip;
  151. StringBuffer filePrefix;
  152. StringAttr fileSrcCluster;
  153. unsigned flags;
  154. bool noDfsResolution;
  155. };
  156. class ReferencedFileList : public CInterface, implements IReferencedFileList
  157. {
  158. public:
  159. IMPLEMENT_IINTERFACE;
  160. ReferencedFileList(const char *username, const char *pw, bool allowForeignFiles) : allowForeign(allowForeignFiles)
  161. {
  162. if (username && pw)
  163. {
  164. user.setown(createUserDescriptor());
  165. user->set(username, pw);
  166. }
  167. }
  168. ReferencedFileList(IUserDescriptor *userDesc, bool allowForeignFiles) : allowForeign(allowForeignFiles)
  169. {
  170. if (userDesc)
  171. user.set(userDesc);
  172. }
  173. void ensureFile(const char *ln, unsigned flags, const char *pkgid, bool noDfsResolution, const char *daliip=NULL, const char *srcCluster=NULL, const char *remotePrefix=NULL);
  174. virtual void addFile(const char *ln, const char *daliip=NULL, const char *srcCluster=NULL, const char *remotePrefix=NULL);
  175. virtual void addFiles(StringArray &files);
  176. virtual void addFilesFromWorkUnit(IConstWorkUnit *cw);
  177. virtual void addFilesFromQuery(IConstWorkUnit *cw, const IHpccPackageMap *pm, const char *queryid);
  178. virtual void addFilesFromQuery(IConstWorkUnit *cw, const IHpccPackage *pkg);
  179. virtual void addFilesFromPackageMap(IPropertyTree *pm);
  180. void addFileFromSubFile(IPropertyTree &subFile, const char *_daliip, const char *srcCluster, const char *_remotePrefix);
  181. void addFilesFromSuperFile(IPropertyTree &superFile, const char *_daliip, const char *srcCluster, const char *_remotePrefix);
  182. void addFilesFromPackage(IPropertyTree &package, const char *_daliip, const char *srcCluster, const char *_remotePrefix);
  183. virtual IReferencedFileIterator *getFiles();
  184. virtual void cloneFileInfo(IDFUhelper *helper, bool overwrite, bool cloneSuperInfo, bool cloneForeign=false);
  185. virtual void cloneRelationships();
  186. virtual void cloneAllInfo(IDFUhelper *helper, bool overwrite, bool cloneSuperInfo, bool cloneForeign=false)
  187. {
  188. cloneFileInfo(helper, overwrite, cloneSuperInfo, cloneForeign);
  189. cloneRelationships();
  190. }
  191. virtual void resolveFiles(const char *process, const char *remoteIP, const char *_remotePrefix, const char *srcCluster, bool checkLocalFirst, bool addSubFiles, bool resolveForeign=false);
  192. void resolveSubFiles(StringArray &subfiles, bool checkLocalFirst, bool resolveForeign);
  193. public:
  194. Owned<IUserDescriptor> user;
  195. Owned<INode> remote;
  196. MapStringToMyClass<ReferencedFile> map;
  197. StringAttr process;
  198. StringAttr srcCluster;
  199. StringAttr remotePrefix;
  200. bool allowForeign;
  201. };
  202. void ReferencedFile::processLocalFileInfo(IDistributedFile *df, const char *dstCluster, const char *srcCluster, StringArray *subfiles)
  203. {
  204. IDistributedSuperFile *super = df->querySuperFile();
  205. if (super)
  206. {
  207. flags |= RefFileSuper;
  208. if (subfiles)
  209. {
  210. Owned<IDistributedFileIterator> it = super->getSubFileIterator(true); //supersub = true, no need to deal with LOCAL supersubs
  211. ForEach(*it)
  212. {
  213. IDistributedFile &sub = it->query();
  214. StringBuffer name;
  215. sub.getLogicalName(name);
  216. subfiles->append(name.str());
  217. }
  218. }
  219. }
  220. else
  221. {
  222. flags |= RefSubFile;
  223. if (!dstCluster || !*dstCluster)
  224. return;
  225. if (df->findCluster(dstCluster)==NotFound)
  226. flags |= RefFileNotOnCluster;
  227. if (fileSrcCluster.length())
  228. srcCluster=fileSrcCluster;
  229. if (srcCluster && *srcCluster)
  230. if (NotFound == df->findCluster(srcCluster))
  231. flags |= RefFileNotOnSource;
  232. }
  233. }
  234. void ReferencedFile::processRemoteFileTree(IPropertyTree *tree, const char *srcCluster, StringArray *subfiles)
  235. {
  236. flags |= RefFileRemote;
  237. if (fileSrcCluster.length())
  238. srcCluster = fileSrcCluster;
  239. if (streq(tree->queryName(), queryDfsXmlBranchName(DXB_SuperFile)))
  240. {
  241. flags |= RefFileSuper;
  242. if (subfiles)
  243. {
  244. Owned<IPropertyTreeIterator> it = tree->getElements("SubFile");
  245. ForEach(*it)
  246. subfiles->append(it->query().queryProp("@name"));
  247. }
  248. }
  249. else if (srcCluster && *srcCluster)
  250. {
  251. VStringBuffer xpath("Cluster[@name='%s']", srcCluster);
  252. if (!tree->hasProp(xpath))
  253. flags |= RefFileNotOnSource;
  254. }
  255. }
  256. void ReferencedFile::resolveLocal(const char *dstCluster, const char *srcCluster, IUserDescriptor *user, StringArray *subfiles)
  257. {
  258. if (noDfsResolution || (flags & RefFileInPackage))
  259. {
  260. flags |= RefFileNotFound;
  261. return;
  262. }
  263. reset();
  264. Owned<IDistributedFile> df = queryDistributedFileDirectory().lookup(logicalName.str(), user);
  265. if(df)
  266. processLocalFileInfo(df, dstCluster, srcCluster, subfiles);
  267. else
  268. flags |= RefFileNotFound;
  269. }
  270. IPropertyTree *ReferencedFile::getRemoteFileTree(IUserDescriptor *user, INode *remote, const char *remotePrefix)
  271. {
  272. if (!remote)
  273. return NULL;
  274. StringBuffer remoteLFN;
  275. if (remotePrefix && *remotePrefix)
  276. remoteLFN.append(remotePrefix).append("::").append(logicalName);
  277. return queryDistributedFileDirectory().getFileTree(remoteLFN.length() ? remoteLFN : logicalName, user, remote, WF_LOOKUP_TIMEOUT, false, false);
  278. }
  279. IPropertyTree *ReferencedFile::getSpecifiedOrRemoteFileTree(IUserDescriptor *user, INode *remote, const char *remotePrefix)
  280. {
  281. if (daliip.length())
  282. {
  283. Owned<INode> daliNode;
  284. daliNode.setown(createINode(daliip));
  285. return getRemoteFileTree(user, daliNode, filePrefix);
  286. }
  287. if (!remote)
  288. return NULL;
  289. StringBuffer remoteLFN;
  290. Owned<IPropertyTree> fileTree = getRemoteFileTree(user, remote, remotePrefix);
  291. if (!fileTree)
  292. return NULL;
  293. remote->endpoint().getUrlStr(daliip);
  294. filePrefix.set(remotePrefix);
  295. return fileTree.getClear();
  296. }
  297. void ReferencedFile::resolveRemote(IUserDescriptor *user, INode *remote, const char *remotePrefix, const char *dstCluster, const char *srcCluster, bool checkLocalFirst, StringArray *subfiles, bool resolveForeign)
  298. {
  299. if ((flags & RefFileForeign) && !resolveForeign)
  300. return;
  301. if (noDfsResolution || (flags & RefFileInPackage))
  302. {
  303. flags |= RefFileNotFound;
  304. return;
  305. }
  306. reset();
  307. if (checkLocalFirst)
  308. {
  309. Owned<IDistributedFile> df = queryDistributedFileDirectory().lookup(logicalName.str(), user);
  310. if(df)
  311. {
  312. processLocalFileInfo(df, dstCluster, NULL, subfiles);
  313. return;
  314. }
  315. }
  316. Owned<IPropertyTree> tree = getSpecifiedOrRemoteFileTree(user, remote, remotePrefix);
  317. if (tree)
  318. processRemoteFileTree(tree, srcCluster, subfiles);
  319. else
  320. flags |= RefFileNotFound;
  321. }
  322. void ReferencedFile::resolve(const char *dstCluster, const char *srcCluster, IUserDescriptor *user, INode *remote, const char *remotePrefix, bool checkLocalFirst, StringArray *subfiles, bool resolveForeign)
  323. {
  324. if (daliip.length() || remote)
  325. resolveRemote(user, remote, remotePrefix, dstCluster, srcCluster, checkLocalFirst, subfiles, resolveForeign);
  326. else
  327. resolveLocal(dstCluster, srcCluster, user, subfiles);
  328. }
  329. void ReferencedFile::cloneInfo(IDFUhelper *helper, IUserDescriptor *user, const char *dstCluster, const char *srcCluster, bool overwrite, bool cloneForeign)
  330. {
  331. if ((flags & RefFileCloned) || (flags & RefFileSuper) || (flags & RefFileInPackage))
  332. return;
  333. if ((flags & RefFileForeign) && !cloneForeign)
  334. return;
  335. if (!(flags & (RefFileRemote | RefFileForeign | RefFileNotOnCluster)))
  336. return;
  337. if (fileSrcCluster.length())
  338. srcCluster = fileSrcCluster;
  339. try
  340. {
  341. StringBuffer srcLFN;
  342. if (filePrefix.length())
  343. srcLFN.append(filePrefix.str()).append("::");
  344. srcLFN.append(logicalName.str());
  345. helper->createSingleFileClone(srcLFN, srcCluster, logicalName, dstCluster, filePrefix,
  346. DFUcpdm_c_replicated_by_d, true, NULL, user, daliip, NULL, overwrite, false);
  347. flags |= RefFileCloned;
  348. }
  349. catch (IException *e)
  350. {
  351. flags |= RefFileCopyInfoFailed;
  352. DBGLOG(e);
  353. e->Release();
  354. }
  355. catch (...)
  356. {
  357. flags |= RefFileCopyInfoFailed;
  358. DBGLOG("Unknown error copying file info for [%s::] %s, from %s on dfs-dali %s", filePrefix.str(), logicalName.str(), fileSrcCluster.length() ? fileSrcCluster.get() : "*", daliip.str());
  359. }
  360. }
  361. void ReferencedFile::cloneSuperInfo(ReferencedFileList *list, IUserDescriptor *user, INode *remote, bool overwrite)
  362. {
  363. if ((flags & RefFileCloned) || (flags & RefFileInPackage) || !(flags & RefFileSuper) || !(flags & RefFileRemote))
  364. return;
  365. try
  366. {
  367. Owned<IPropertyTree> tree = getSpecifiedOrRemoteFileTree(user, remote, NULL);
  368. if (!tree)
  369. return;
  370. IDistributedFileDirectory &dir = queryDistributedFileDirectory();
  371. Owned<IDistributedFile> df = dir.lookup(logicalName.str(), user);
  372. if(df)
  373. {
  374. if (!overwrite)
  375. return;
  376. df->detach();
  377. df.clear();
  378. }
  379. Owned<IDistributedSuperFile> superfile = dir.createSuperFile(logicalName.str(),user, true, false);
  380. flags |= RefFileCloned;
  381. Owned<IPropertyTreeIterator> subfiles = tree->getElements("SubFile");
  382. ForEach(*subfiles)
  383. {
  384. const char *name = subfiles->query().queryProp("@name");
  385. if (list)
  386. {
  387. //ensure superfile in superfile is cloned, before add
  388. ReferencedFile *subref = list->map.getValue(name);
  389. if (subref)
  390. subref->cloneSuperInfo(list, user, remote, overwrite);
  391. }
  392. if (name && *name)
  393. superfile->addSubFile(name, false, NULL, false);
  394. }
  395. }
  396. catch (IException *e)
  397. {
  398. flags |= RefFileCopyInfoFailed;
  399. DBGLOG(e);
  400. e->Release();
  401. }
  402. catch (...)
  403. {
  404. flags |= RefFileCopyInfoFailed;
  405. DBGLOG("Unknown error copying superfile info for %s", logicalName.str());
  406. }
  407. }
  408. class ReferencedFileIterator : public CInterface, implements IReferencedFileIterator
  409. {
  410. public:
  411. IMPLEMENT_IINTERFACE;
  412. ReferencedFileIterator(ReferencedFileList *_list)
  413. {
  414. list.set(_list);
  415. iter.setown(new HashIterator(list->map));
  416. }
  417. virtual bool first()
  418. {
  419. return iter->first();
  420. }
  421. virtual bool next()
  422. {
  423. return iter->next();
  424. }
  425. virtual bool isValid()
  426. {
  427. return iter->isValid();
  428. }
  429. virtual IReferencedFile & query()
  430. {
  431. return *list->map.mapToValue(&iter->query());
  432. }
  433. virtual ReferencedFile & queryObject()
  434. {
  435. return *(list->map.mapToValue(&iter->query()));
  436. }
  437. public:
  438. Owned<ReferencedFileList> list;
  439. Owned<HashIterator> iter;
  440. };
  441. void ReferencedFileList::ensureFile(const char *ln, unsigned flags, const char *pkgid, bool noDfsResolution, const char *daliip, const char *srcCluster, const char *prefix)
  442. {
  443. if (!allowForeign && checkForeign(ln))
  444. throw MakeStringException(-1, "Foreign file not allowed%s: %s", (flags & RefFileInPackage) ? " (declared in package)" : "", ln);
  445. Owned<ReferencedFile> file = new ReferencedFile(ln, daliip, srcCluster, prefix, false, flags, pkgid, noDfsResolution);
  446. if (!file->logicalName.length())
  447. return;
  448. ReferencedFile *existing = map.getValue(file->getLogicalName());
  449. if (existing)
  450. existing->flags |= flags;
  451. else
  452. {
  453. const char *refln = file->getLogicalName();
  454. map.setValue(refln, file.getClear());
  455. }
  456. }
  457. void ReferencedFileList::addFile(const char *ln, const char *daliip, const char *srcCluster, const char *prefix)
  458. {
  459. ensureFile(ln, 0, NULL, daliip, srcCluster, prefix);
  460. }
  461. void ReferencedFileList::addFiles(StringArray &files)
  462. {
  463. ForEachItemIn(i, files)
  464. addFile(files.item(i));
  465. }
  466. void ReferencedFileList::addFileFromSubFile(IPropertyTree &subFile, const char *ip, const char *cluster, const char *prefix)
  467. {
  468. addFile(subFile.queryProp("@value"), ip, cluster, prefix);
  469. }
  470. void ReferencedFileList::addFilesFromSuperFile(IPropertyTree &superFile, const char *_ip, const char *_cluster, const char *_prefix)
  471. {
  472. StringBuffer ip;
  473. StringBuffer cluster;
  474. StringBuffer prefix;
  475. splitDerivedDfsLocation(superFile.queryProp("@daliip"), cluster, ip, prefix, NULL, _cluster, _ip, _prefix);
  476. if (superFile.hasProp("@sourceCluster"))
  477. cluster.set(superFile.queryProp("@sourceCluster"));
  478. Owned<IPropertyTreeIterator> subFiles = superFile.getElements("SubFile[@value]");
  479. ForEach(*subFiles)
  480. addFileFromSubFile(subFiles->query(), ip, cluster, prefix);
  481. }
  482. void ReferencedFileList::addFilesFromPackage(IPropertyTree &package, const char *_ip, const char *_cluster, const char *_prefix)
  483. {
  484. StringBuffer ip;
  485. StringBuffer cluster;
  486. StringBuffer prefix;
  487. splitDerivedDfsLocation(package.queryProp("@daliip"), cluster, ip, prefix, NULL, _cluster, _ip, _prefix);
  488. if (package.hasProp("@sourceCluster"))
  489. cluster.set(package.queryProp("@sourceCluster"));
  490. Owned<IPropertyTreeIterator> supers = package.getElements("SuperFile");
  491. ForEach(*supers)
  492. addFilesFromSuperFile(supers->query(), ip, cluster, prefix);
  493. }
  494. void ReferencedFileList::addFilesFromPackageMap(IPropertyTree *pm)
  495. {
  496. StringBuffer ip;
  497. StringBuffer cluster;
  498. StringBuffer prefix;
  499. const char *srcCluster = pm->queryProp("@sourceCluster");
  500. splitDerivedDfsLocation(pm->queryProp("@daliip"), cluster, ip, prefix, srcCluster, srcCluster, NULL, NULL);
  501. Owned<IPropertyTreeIterator> packages = pm->getElements("Package");
  502. ForEach(*packages)
  503. addFilesFromPackage(packages->query(), ip, cluster, prefix);
  504. }
  505. void ReferencedFileList::addFilesFromQuery(IConstWorkUnit *cw, const IHpccPackage *pkg)
  506. {
  507. Owned<IConstWUGraphIterator> graphs = &cw->getGraphs(GraphTypeActivities);
  508. ForEach(*graphs)
  509. {
  510. Owned <IPropertyTree> xgmml = graphs->query().getXGMMLTree(false);
  511. Owned<IPropertyTreeIterator> iter = xgmml->getElements("//node[att/@name='_*ileName']");
  512. ForEach(*iter)
  513. {
  514. IPropertyTree &node = iter->query();
  515. const char *logicalName = node.queryProp("att[@name='_fileName']/@value");
  516. if (!logicalName)
  517. logicalName = node.queryProp("att[@name='_indexFileName']/@value");
  518. if (!logicalName)
  519. continue;
  520. ThorActivityKind kind = (ThorActivityKind) node.getPropInt("att[@name='_kind']/@value", TAKnone);
  521. //not likely to be part of roxie queries, but for forward compatibility:
  522. if(kind==TAKdiskwrite || kind==TAKindexwrite || kind==TAKcsvwrite || kind==TAKxmlwrite)
  523. continue;
  524. if (node.getPropBool("att[@name='_isSpill']/@value") ||
  525. node.getPropBool("att[@name='_isTransformSpill']/@value"))
  526. continue;
  527. unsigned flags = 0;
  528. if (pkg)
  529. {
  530. const char *pkgid = pkg->locateSuperFile(logicalName);
  531. if (pkgid)
  532. {
  533. flags |= (RefFileSuper | RefFileInPackage);
  534. Owned<ISimpleSuperFileEnquiry> ssfe = pkg->resolveSuperFile(logicalName);
  535. if (ssfe && ssfe->numSubFiles()>0)
  536. {
  537. unsigned count = ssfe->numSubFiles();
  538. while (count--)
  539. {
  540. StringBuffer subfile;
  541. ssfe->getSubFileName(count, subfile);
  542. ensureFile(subfile, RefSubFile | RefFileInPackage, pkgid, pkg->isCompulsory());
  543. }
  544. }
  545. }
  546. ensureFile(logicalName, flags, pkgid, pkg->isCompulsory());
  547. }
  548. else
  549. ensureFile(logicalName, flags, NULL, false);
  550. }
  551. }
  552. }
  553. void ReferencedFileList::addFilesFromQuery(IConstWorkUnit *cw, const IHpccPackageMap *pm, const char *queryid)
  554. {
  555. const IHpccPackage *pkg = NULL;
  556. if (pm && queryid && *queryid)
  557. pkg = pm->matchPackage(queryid);
  558. addFilesFromQuery(cw, pkg);
  559. }
  560. void ReferencedFileList::addFilesFromWorkUnit(IConstWorkUnit *cw)
  561. {
  562. addFilesFromQuery(cw, NULL, NULL);
  563. }
  564. void ReferencedFileList::resolveSubFiles(StringArray &subfiles, bool checkLocalFirst, bool resolveForeign)
  565. {
  566. StringArray childSubFiles;
  567. ForEachItemIn(i, subfiles)
  568. {
  569. const char *lfn = subfiles.item(i);
  570. if (!allowForeign && checkForeign(lfn))
  571. throw MakeStringException(-1, "Foreign sub file not allowed: %s", lfn);
  572. Owned<ReferencedFile> file = new ReferencedFile(lfn, NULL, NULL, NULL, true, 0, NULL, false);
  573. if (file->logicalName.length() && !map.getValue(file->getLogicalName()))
  574. {
  575. file->resolve(process.get(), srcCluster, user, remote, remotePrefix, checkLocalFirst, &childSubFiles, resolveForeign);
  576. const char *ln = file->getLogicalName();
  577. map.setValue(ln, file.getClear());
  578. }
  579. }
  580. if (childSubFiles.length())
  581. resolveSubFiles(childSubFiles, checkLocalFirst, resolveForeign);
  582. }
  583. void ReferencedFileList::resolveFiles(const char *_process, const char *remoteIP, const char *_remotePrefix, const char *_srcCluster, bool checkLocalFirst, bool expandSuperFiles, bool resolveForeign)
  584. {
  585. process.set(_process);
  586. remote.setown((remoteIP && *remoteIP) ? createINode(remoteIP, 7070) : NULL);
  587. srcCluster.set(_srcCluster);
  588. remotePrefix.set(_remotePrefix);
  589. StringArray subfiles;
  590. {
  591. ReferencedFileIterator files(this);
  592. ForEach(files)
  593. files.queryObject().resolve(process, srcCluster, user, remote, remotePrefix, checkLocalFirst, expandSuperFiles ? &subfiles : NULL, resolveForeign);
  594. }
  595. if (expandSuperFiles)
  596. resolveSubFiles(subfiles, checkLocalFirst, resolveForeign);
  597. }
  598. void ReferencedFileList::cloneFileInfo(IDFUhelper *helper, bool overwrite, bool cloneSuperInfo, bool cloneForeign)
  599. {
  600. ReferencedFileIterator files(this);
  601. ForEach(files)
  602. files.queryObject().cloneInfo(helper, user, process, srcCluster, overwrite, cloneForeign);
  603. if (cloneSuperInfo)
  604. ForEach(files)
  605. files.queryObject().cloneSuperInfo(this, user, remote, overwrite);
  606. }
  607. void ReferencedFileList::cloneRelationships()
  608. {
  609. if (!remote || remote->endpoint().isNull())
  610. return;
  611. StringBuffer addr;
  612. remote->endpoint().getUrlStr(addr);
  613. IDistributedFileDirectory &dir = queryDistributedFileDirectory();
  614. ReferencedFileIterator files(this);
  615. ForEach(files)
  616. {
  617. ReferencedFile &file = files.queryObject();
  618. if (!(file.getFlags() & RefFileRemote))
  619. continue;
  620. Owned<IFileRelationshipIterator> iter = dir.lookupFileRelationships(file.getLogicalName(), NULL,
  621. NULL, NULL, NULL, NULL, NULL, addr.str(), WF_LOOKUP_TIMEOUT);
  622. ForEach(*iter)
  623. {
  624. IFileRelationship &r=iter->query();
  625. const char* assoc = r.querySecondaryFilename();
  626. if (!assoc)
  627. continue;
  628. if (*assoc == '~')
  629. assoc++;
  630. IReferencedFile *refAssoc = map.getValue(assoc);
  631. if (refAssoc && !(refAssoc->getFlags() & RefFileCopyInfoFailed))
  632. {
  633. dir.addFileRelationship(file.getLogicalName(), assoc, r.queryPrimaryFields(), r.querySecondaryFields(),
  634. r.queryKind(), r.queryCardinality(), r.isPayload(), user, r.queryDescription());
  635. }
  636. }
  637. }
  638. }
  639. IReferencedFileIterator *ReferencedFileList::getFiles()
  640. {
  641. return new ReferencedFileIterator(this);
  642. }
  643. IReferencedFileList *createReferencedFileList(const char *user, const char *pw, bool allowForeignFiles)
  644. {
  645. return new ReferencedFileList(user, pw, allowForeignFiles);
  646. }
  647. IReferencedFileList *createReferencedFileList(IUserDescriptor *user, bool allowForeignFiles)
  648. {
  649. return new ReferencedFileList(user, allowForeignFiles);
  650. }