XRefNodeManager.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. // XRefNodeManager.cpp: implementation of the CXRefNodeManager class.
  14. //
  15. //////////////////////////////////////////////////////////////////////
  16. #include "jiface.hpp"
  17. #include "jstring.hpp"
  18. #include "jptree.hpp"
  19. #include "jmisc.hpp"
  20. #include "mpcomm.hpp"
  21. #include "platform.h"
  22. #include "jlib.hpp"
  23. #include "mpbase.hpp"
  24. #include "daclient.hpp"
  25. #include "dadiags.hpp"
  26. #include "danqs.hpp"
  27. #include "dadfs.hpp"
  28. #include "dasds.hpp"
  29. #include "dautils.hpp"
  30. #include "daft.hpp"
  31. #include "rmtfile.hpp"
  32. #include "XRefNodeManager.hpp"
  33. //////////////////////////////////////////////////////////////////////
  34. // Construction/Destruction
  35. //////////////////////////////////////////////////////////////////////
  36. IXRefNodeManager * CreateXRefNodeFactory()
  37. {
  38. return new CXRefNodeManager();
  39. }
  40. IXRefNode * CXRefNodeManager::getXRefNode(const char* NodeName)
  41. {
  42. //DBGLOG("Node Name %s:",NodeName);
  43. StringBuffer xpath;
  44. xpath.appendf("Cluster[@name=\"%s\"]", NodeName);
  45. //if not exists, add DFU/XREF/ClusterName branch to SDS
  46. //not linked
  47. Owned<IRemoteConnection> conn = querySDS().connect("/DFU/XREF",myProcessSession(),RTM_CREATE_QUERY|RTM_NONE ,INFINITE);
  48. IPropertyTree* cluster_ptree = conn->queryRoot()->queryPropTree(xpath.str());
  49. conn->commit();
  50. if (cluster_ptree==0)
  51. {
  52. DBGLOG("Cluster[@name=%s] can't be found under /DFU/XREF", NodeName);
  53. return 0 ;
  54. }
  55. return new CXRefNode(NodeName,conn);
  56. }
  57. IXRefNode * CXRefNodeManager::CreateXRefNode(const char* NodeName)
  58. {
  59. Owned<IRemoteConnection> conn = querySDS().connect("/DFU/XREF",myProcessSession(),RTM_CREATE_QUERY|RTM_LOCK_WRITE ,INFINITE);
  60. IPropertyTree* xref_ptree = conn->queryRoot();
  61. IPropertyTree* cluster_ptree = xref_ptree->addPropTree("Cluster", createPTree());
  62. cluster_ptree->setProp("@name",NodeName);
  63. conn->commit();
  64. conn->changeMode(RTM_NONE);
  65. return new CXRefNode(NodeName,conn);
  66. }
  67. CXRefNode::CXRefNode()
  68. {
  69. m_bChanged = false;
  70. }
  71. CXRefNode::~CXRefNode()
  72. {
  73. }
  74. CXRefNode::CXRefNode(const char* NodeName, IRemoteConnection *_conn)
  75. {
  76. //DBGLOG("CXRefNode::CXRefNode(const char* NodeName)");
  77. m_bChanged = false;
  78. m_conn.set(_conn);
  79. StringBuffer xpath;
  80. xpath.appendf("Cluster[@name=\"%s\"]", NodeName);
  81. IPropertyTree* cluster_ptree = m_conn->queryRoot()->queryPropTree(xpath.str());
  82. m_XRefTree.set(cluster_ptree);
  83. m_XRefTree->getProp("@name",m_origName);
  84. //DBGLOG("returning from CXRefNode::CXRefNode(const char* NodeName)");
  85. }
  86. CXRefNode::CXRefNode(IPropertyTree* pTreeRoot)
  87. {
  88. m_bChanged = false;
  89. try
  90. {
  91. m_XRefTree.set(pTreeRoot);
  92. pTreeRoot->getProp("@name",m_origName);
  93. //load up our tree with the data.....if there is data
  94. MemoryBuffer buff;
  95. pTreeRoot->getPropBin("data",buff);
  96. if (buff.length())
  97. {
  98. m_dataStr.append(buff.length(),buff.toByteArray());
  99. }
  100. //lets check to ensure we have the correct children inplace(Orphan,lost,found)
  101. }
  102. catch(...)
  103. {
  104. ERRLOG("Error in creation of XRefNode...");
  105. }
  106. }
  107. bool CXRefNode::useSasha()
  108. {
  109. if (!m_conn)
  110. return false;
  111. return m_conn->queryRoot()->getPropBool("@useSasha");
  112. }
  113. IPropertyTree& CXRefNode::getDataTree()
  114. {
  115. if(m_XRefDataTree.get() == 0)
  116. m_XRefDataTree.setown(createPTreeFromXMLString(m_dataStr.str()));
  117. return *m_XRefDataTree.get();
  118. }
  119. //IConstXRefNode
  120. StringBuffer & CXRefNode::getName(StringBuffer & str)
  121. {
  122. if(m_XRefTree.get())
  123. m_XRefTree->getProp("@name",str);
  124. return str;
  125. }
  126. StringBuffer& CXRefNode::getStatus(StringBuffer & str)
  127. {
  128. if(m_XRefTree.get())
  129. m_XRefTree->getProp("@status",str);
  130. return str;
  131. }
  132. StringBuffer & CXRefNode::getLastModified(StringBuffer & str)
  133. {
  134. if(m_XRefTree.get())
  135. m_XRefTree->getProp("@modified",str);
  136. return str;
  137. }
  138. StringBuffer& CXRefNode::getXRefData(StringBuffer & str)
  139. {
  140. return toXML(&getDataTree(),str);
  141. }
  142. IXRefFilesNode* CXRefNode::getLostFiles()
  143. {
  144. if(!m_lost.get())
  145. {
  146. IPropertyTree* lostBranch = m_XRefTree->queryPropTree("Lost");
  147. if(lostBranch == 0)
  148. {
  149. lostBranch = m_XRefTree->addPropTree("Lost",createPTree());
  150. commit();
  151. }
  152. const char *rootdir = m_XRefTree.get()?m_XRefTree->queryProp("@rootdir"):NULL;
  153. StringBuffer tmpbuf;
  154. m_lost.setown(new CXRefFilesNode(*lostBranch,getName(tmpbuf).str(),rootdir));
  155. }
  156. return m_lost.getLink();
  157. }
  158. IXRefFilesNode* CXRefNode::getFoundFiles()
  159. {
  160. if(!m_found.get())
  161. {
  162. IPropertyTree* foundBranch = m_XRefTree->queryPropTree("Found");
  163. if(foundBranch == 0)
  164. {
  165. foundBranch = m_XRefTree->addPropTree("Found",createPTree());
  166. commit();
  167. }
  168. const char *rootdir = m_XRefTree.get()?m_XRefTree->queryProp("@rootdir"):NULL;
  169. StringBuffer tmpbuf;
  170. m_found.setown(new CXRefFilesNode(*foundBranch,getName(tmpbuf).str(),rootdir));
  171. }
  172. return m_found.getLink();
  173. }
  174. IXRefFilesNode* CXRefNode::getOrphanFiles()
  175. {
  176. if(!m_orphans.get())
  177. {
  178. IPropertyTree* orphanBranch = m_XRefTree->queryPropTree("Orphans");
  179. if(orphanBranch == 0)
  180. {
  181. orphanBranch = m_XRefTree->addPropTree("Orphans",createPTree());
  182. commit();
  183. }
  184. const char *rootdir = m_XRefTree.get()?m_XRefTree->queryProp("@rootdir"):NULL;
  185. StringBuffer tmpbuf;
  186. m_orphans.setown(new CXRefOrphanFilesNode(*orphanBranch,getName(tmpbuf).str(),rootdir));
  187. }
  188. return m_orphans.getLink();
  189. }
  190. StringBuffer &CXRefNode::serializeMessages(StringBuffer &buf)
  191. {
  192. if(!m_messages.get())
  193. {
  194. IPropertyTree* messagesBranch = m_XRefTree->queryPropTree("Messages");
  195. if(messagesBranch == 0)
  196. {
  197. messagesBranch = m_XRefTree->addPropTree("Messages",createPTree());
  198. commit();
  199. }
  200. StringBuffer tmpbuf;
  201. m_messages.set(messagesBranch);
  202. }
  203. buf.clear();
  204. MemoryBuffer data;
  205. m_messages->getPropBin("data",data);
  206. if (data.length())
  207. {
  208. buf.append(data.length(),data.toByteArray());
  209. }
  210. return buf;
  211. }
  212. void CXRefNode::deserializeMessages(IPropertyTree& inTree)
  213. {
  214. if(!m_messages.get())
  215. {
  216. IPropertyTree* messagesBranch = m_XRefTree->queryPropTree("Messages");
  217. if(messagesBranch == 0)
  218. {
  219. messagesBranch = m_XRefTree->addPropTree("Messages",createPTree());
  220. commit();
  221. }
  222. StringBuffer tmpbuf;
  223. m_messages.set(messagesBranch);
  224. }
  225. StringBuffer datastr;
  226. toXML(&inTree,datastr);
  227. m_messages->setPropBin("data",datastr.length(),(void*)datastr.toCharArray());
  228. }
  229. StringBuffer &CXRefNode::serializeDirectories(StringBuffer &buf)
  230. {
  231. if(!m_directories.get())
  232. {
  233. IPropertyTree* directoriesBranch = m_XRefTree->queryPropTree("Directories");
  234. if(directoriesBranch == 0)
  235. {
  236. directoriesBranch = m_XRefTree->addPropTree("Directories",createPTree());
  237. commit();
  238. }
  239. StringBuffer tmpbuf;
  240. m_directories.set(directoriesBranch);
  241. }
  242. buf.clear();
  243. MemoryBuffer data;
  244. m_directories->getPropBin("data",data);
  245. if (data.length())
  246. {
  247. buf.append(data.length(),data.toByteArray());
  248. }
  249. return buf;
  250. }
  251. void CXRefNode::deserializeDirectories(IPropertyTree& inTree)
  252. {
  253. if(!m_directories.get())
  254. {
  255. IPropertyTree* directoriesBranch = m_XRefTree->queryPropTree("Directories");
  256. if(directoriesBranch == 0)
  257. {
  258. directoriesBranch = m_XRefTree->addPropTree("Directories",createPTree());
  259. commit();
  260. }
  261. StringBuffer tmpbuf;
  262. m_directories.set(directoriesBranch);
  263. }
  264. StringBuffer datastr;
  265. toXML(&inTree,datastr);
  266. m_directories->setPropBin("data",datastr.length(),(void*)datastr.toCharArray());
  267. }
  268. static int strptrcmprev(char const ** l, char const ** r) { return -strcmp(*l, *r); }
  269. static bool deleteEmptyDir(IFile *dir)
  270. {
  271. // this is a bit odd - basically we already know no files but there may be empty sub-dirs
  272. Owned<IDirectoryIterator> iter = dir->directoryFiles(NULL,false,true);
  273. IArrayOf<IFile> subdirs;
  274. bool candelete = true;
  275. ForEach(*iter) {
  276. if (iter->isDir())
  277. subdirs.append(iter->get());
  278. else
  279. candelete = false;
  280. }
  281. if (!candelete)
  282. return false;
  283. try {
  284. ForEachItemIn(i,subdirs) {
  285. if (!deleteEmptyDir(&subdirs.item(i)))
  286. candelete = false;
  287. }
  288. }
  289. catch (IException *e) {
  290. EXCLOG(e,"deleteEmptyDir");
  291. candelete = false;
  292. }
  293. if (!candelete)
  294. return false;
  295. static CriticalSection sect;
  296. CriticalBlock block(sect); // don't want to actually remove in parallel
  297. dir->remove();
  298. return !dir->exists();
  299. }
  300. static bool recursiveCheckEmptyScope(IPropertyTree &ct)
  301. {
  302. Owned<IPropertyTreeIterator> iter = ct.getElements("*");
  303. ForEach(*iter) {
  304. IPropertyTree &item = iter->query();
  305. const char *n = item.queryName();
  306. if (!n||(strcmp(n,queryDfsXmlBranchName(DXB_Scope))!=0))
  307. return false;
  308. if (!recursiveCheckEmptyScope(item))
  309. return false;
  310. }
  311. return true;
  312. }
  313. static void emptyScopes()
  314. {
  315. PROGLOG("Removing empty scopes");
  316. Owned<IDFScopeIterator> iter = queryDistributedFileDirectory().getScopeIterator(UNKNOWN_USER,NULL,true,true);//MORE:Pass IUserDescriptor
  317. CDfsLogicalFileName dlfn;
  318. StringBuffer s;
  319. StringArray toremove;
  320. ForEach(*iter) {
  321. CDfsLogicalFileName dlfn;
  322. StringBuffer scope;
  323. scope.append(iter->query());
  324. dlfn.set(scope.str(),"x");
  325. dlfn.makeScopeQuery(s.clear(),true);
  326. Owned<IRemoteConnection> conn = querySDS().connect(s.str(),myProcessSession(),RTM_LOCK_READ, INFINITE);
  327. if (!conn)
  328. DBGLOG("Could not connect to '%s' using %s",iter->query(),s.str());
  329. else {
  330. if (recursiveCheckEmptyScope(*conn->queryRoot())) {
  331. PROGLOG("Empty scope %s",iter->query());
  332. toremove.append(iter->query());
  333. }
  334. }
  335. }
  336. iter.clear();
  337. ForEachItemIn(i,toremove) {
  338. PROGLOG("Removed scope %s",toremove.item(i));
  339. queryDistributedFileDirectory().removeEmptyScope(toremove.item(i));
  340. }
  341. }
  342. bool CXRefNode::removeEmptyDirectories(StringBuffer &errstr)
  343. {
  344. StringBuffer dataStr;
  345. serializeDirectories(dataStr);
  346. if (dataStr.length()==0)
  347. return true;
  348. Owned<IPropertyTree> t = createPTreeFromXMLString(dataStr.str());
  349. Owned<IPropertyTreeIterator> iter = t->getElements("Directory");
  350. const char *clustername = t->queryProp("Cluster");
  351. if (!clustername||!*clustername)
  352. return false;
  353. Owned<IGroup> group = queryNamedGroupStore().lookup(clustername);
  354. if (!group) {
  355. ERRLOG("%s cluster not found",clustername);
  356. errstr.appendf("ERROR: %s cluster not found",clustername);
  357. return false;
  358. }
  359. StringArray dellist;
  360. PointerArray todelete;
  361. ForEach(*iter) {
  362. IPropertyTree &dir = iter->query();
  363. if (dir.getPropInt64("Num")==0) {
  364. const char *dirname = dir.queryProp("Name");
  365. if (dirname&&*dirname) {
  366. dellist.append(dirname);
  367. todelete.append(&dir);
  368. }
  369. }
  370. }
  371. dellist.sort(strptrcmprev);
  372. ForEachItemIn(di,dellist) {
  373. const char *dirname = dellist.item(di);
  374. class casyncfor: public CAsyncFor
  375. {
  376. IGroup *grp;
  377. StringAttr name;
  378. public:
  379. casyncfor(IGroup *_grp,const char *_name)
  380. : name(_name)
  381. {
  382. grp = _grp;
  383. }
  384. void Do(unsigned i)
  385. {
  386. RemoteFilename rfn;
  387. rfn.setPath(grp->queryNode(i).endpoint(),name);
  388. StringBuffer eps;
  389. try
  390. {
  391. Owned<IFile> dir = createIFile(rfn);
  392. if (deleteEmptyDir(dir))
  393. PROGLOG("Removed '%s'",dir->queryFilename());
  394. else
  395. WARNLOG("Could not remove '%s'",dir->queryFilename());
  396. }
  397. catch (IException *e)
  398. {
  399. EXCLOG(e,"Could not remove directory");
  400. e->Release();
  401. }
  402. }
  403. } afor(group,dirname);
  404. afor.For(group->ordinality(),10,false,true);
  405. }
  406. iter.clear();
  407. ForEachItemInRev(i,todelete)
  408. t->removeTree((IPropertyTree *)todelete.item(i)); // probably should check succeeded above but next run will correct
  409. toXML(t,dataStr.clear());
  410. m_directories->setPropBin("data",dataStr.length(),(void*)dataStr.toCharArray());
  411. emptyScopes();
  412. return true;
  413. }
  414. //IXRefNode
  415. void CXRefNode::setName(const char* str)
  416. {
  417. m_XRefTree->setProp("@name",str);
  418. if (m_origName.length() == 0)
  419. m_origName.append(str);
  420. }
  421. void CXRefNode::setStatus(const char* str)
  422. {
  423. m_XRefTree->setProp("@status",str);
  424. }
  425. StringBuffer& CXRefNode::getCluster(StringBuffer& Cluster)
  426. {
  427. Cluster.append(m_ClusterName.str());
  428. return Cluster;
  429. }
  430. void CXRefNode::setCluster(const char* str)
  431. {
  432. m_ClusterName.clear();
  433. m_ClusterName.append(str);
  434. }
  435. void CXRefNode::setLastModified(IJlibDateTime& dt )
  436. {
  437. SCMStringBuffer datestr,timestr;
  438. dt.getDateString(datestr);
  439. dt.getTimeString(timestr);
  440. StringBuffer tmpstr(datestr.str());
  441. tmpstr.append(" ");
  442. tmpstr.append(timestr.str());
  443. m_XRefTree->setProp("@modified",tmpstr.str());
  444. }
  445. void CXRefNode::BuildXRefData(IPropertyTree & pTree,const char* Cluster)
  446. {
  447. // DBGLOG("CXRefNode::BuildXRefData");
  448. if(m_XRefTree.get() == 0)
  449. throw MakeStringException(-1, "No XRef Dali Tree available");
  450. Owned<IXRefFilesNode> lost = getLostFiles();
  451. Owned<IXRefFilesNode> found = getFoundFiles() ;
  452. Owned<IXRefFilesNode> orphan = getOrphanFiles();
  453. IPropertyTree* pSubTree = pTree.queryPropTree("Orphans");
  454. pSubTree->setProp("Cluster",Cluster);
  455. orphan->Deserialize(*pSubTree);
  456. pSubTree = pTree.queryPropTree("Lost");
  457. pSubTree->setProp("Cluster",Cluster);
  458. lost->Deserialize(*pSubTree);
  459. pSubTree = pTree.queryPropTree("Found");
  460. pSubTree->setProp("Cluster",Cluster);
  461. found->Deserialize(*pSubTree);
  462. pSubTree = pTree.queryPropTree("Messages");
  463. pSubTree->setProp("Cluster",Cluster);
  464. deserializeMessages(*pSubTree);
  465. pSubTree = pTree.queryPropTree("Directories");
  466. pSubTree->setProp("Cluster",Cluster);
  467. deserializeDirectories(*pSubTree);
  468. Owned<IJlibDateTime> dt = createDateTimeNow();
  469. setLastModified(*dt);
  470. setStatus("Generated");
  471. commit();
  472. }
  473. bool CXRefNode::IsChanged()
  474. {
  475. if ((m_orphans.get() && m_orphans->IsChanged() == true) ||
  476. (m_lost.get() && m_lost->IsChanged() == true) ||
  477. (m_found.get() && m_found->IsChanged() == true) ||
  478. m_bChanged == true )
  479. return true;
  480. return false;
  481. }
  482. void CXRefNode::SetChanged(bool bChanged)
  483. {
  484. m_bChanged = bChanged;
  485. }
  486. void CXRefNode::commit()
  487. {
  488. CriticalSection(commitMutex);
  489. if(m_conn == 0)
  490. return;
  491. Owned<IXRefFilesNode> lost = getLostFiles();
  492. Owned<IXRefFilesNode> found = getFoundFiles() ;
  493. Owned<IXRefFilesNode> orphan = getOrphanFiles();
  494. lost->Commit();
  495. found->Commit();
  496. orphan->Commit();
  497. m_conn->commit();
  498. }
  499. void CXRefNode::progress(const char *text)
  500. {
  501. DBGLOG("PROGRESS: %s\n",text);
  502. setStatus(text);
  503. commit();
  504. }
  505. void CXRefNode::error(const char *text)
  506. {
  507. DBGLOG("ERROR: %s\n",text);
  508. setStatus(text);
  509. commit();
  510. }