ccdstate.cpp 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886
  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. #include <platform.h>
  14. #include <jlib.hpp>
  15. #include "build-config.h"
  16. #include "jisem.hpp"
  17. #include "jsort.hpp"
  18. #include "jregexp.hpp"
  19. #include "ccd.hpp"
  20. #include "ccdquery.hpp"
  21. #include "ccdstate.hpp"
  22. #include "ccdqueue.ipp"
  23. #include "ccdlistener.hpp"
  24. #include "ccdfile.hpp"
  25. #include "ccdsnmp.hpp"
  26. #include "hqlplugins.hpp"
  27. #include "thorplugin.hpp"
  28. #include "eclrtl.hpp"
  29. #include "dafdesc.hpp"
  30. #include "dautils.hpp"
  31. #include "pkgimpl.hpp"
  32. //-------------------------------------------------------------------------------------------
  33. // class CRoxiePluginCtx - provide the environments for plugins loaded by roxie.
  34. // Base class handles making sure memory allocation comes from the right heap.
  35. // implement get/set properties to allow plugin configuration information to be retrieved from Roxie topology file
  36. //-------------------------------------------------------------------------------------------
  37. class CRoxiePluginCtx : public SimplePluginCtx
  38. {
  39. public:
  40. virtual int ctxGetPropInt(const char *propName, int defaultValue) const
  41. {
  42. return topology->getPropInt(propName, defaultValue);
  43. }
  44. virtual const char *ctxQueryProp(const char *propName) const
  45. {
  46. return topology->queryProp(propName);
  47. }
  48. } PluginCtx;
  49. SafePluginMap *plugins;
  50. //================================================================================================
  51. // In legacy state files, the original file names passed in _fileName or _indexFileName may have been translated into _superFileName or _superKeyName,
  52. // and then 0 or more (max 1 for subfiles, no limit for subkeys) _fileName or _indexFileName will have been added. This translation will not take place
  53. // if the files resolve to single file/key, or if we are using new embedded wu system
  54. // Basic mode of operation therefore is to get the original name, see if it can be resolved by package into a list of subfiles, and if not, use
  55. // iterator on the xgmml node to get the list.
  56. // These two helper functions will return the original filenames placed in the XGMML by the codegen, regardless of how/if roxieconfig resolved them
  57. const char *queryNodeFileName(const IPropertyTree &graphNode)
  58. {
  59. const char *id = graphNode.queryProp("att[@name='_fileName']/@value");
  60. return id;
  61. }
  62. const char *queryNodeIndexName(const IPropertyTree &graphNode)
  63. {
  64. const char * id = graphNode.queryProp("att[@name='_indexFileName']/@value");
  65. if (!id && !graphNode.hasProp("att[@name='_indexFileName_dynamic']")) // can remove soon
  66. id = graphNode.queryProp("att[@name='_fileName']/@value");
  67. return id;
  68. }
  69. class CSimpleSuperFileArray : public CInterface, implements ISimpleSuperFileEnquiry
  70. {
  71. IArrayOf<IPropertyTree> subFiles;
  72. public:
  73. IMPLEMENT_IINTERFACE;
  74. CSimpleSuperFileArray(IPropertyTreeIterator &_subs)
  75. {
  76. ForEach(_subs)
  77. {
  78. IPropertyTree &sub = _subs.query();
  79. sub.Link();
  80. subFiles.append(sub);
  81. }
  82. }
  83. virtual unsigned numSubFiles() const
  84. {
  85. return subFiles.length();
  86. }
  87. virtual bool getSubFileName(unsigned num, StringBuffer &name) const
  88. {
  89. if (subFiles.isItem(num))
  90. {
  91. name.append(subFiles.item(num).queryProp("@value"));
  92. return true;
  93. }
  94. else
  95. return false;
  96. }
  97. virtual unsigned findSubName(const char *subname) const
  98. {
  99. ForEachItemIn(idx, subFiles)
  100. {
  101. if (stricmp(subFiles.item(idx).queryProp("@value"), subname))
  102. return idx;
  103. }
  104. return NotFound;
  105. }
  106. virtual unsigned getContents(StringArray &contents) const
  107. {
  108. ForEachItemIn(idx, subFiles)
  109. {
  110. contents.append(subFiles.item(idx).queryProp("@value"));
  111. }
  112. return subFiles.length();
  113. }
  114. };
  115. //-------------------------------------------------------------------------------------------
  116. // class CRoxiePackage - provide the environment in which file names and query options are interpreted
  117. // by a roxie query.
  118. // File names are resolved into IResolvedFile objects. A cache is used to ensure that the IResolvedFile is
  119. // shared wherever possible.
  120. // Effective environment is precomputed in mergedEnvironment for efficient recall by queries
  121. // Packages are described using XML files - see documentation for details.
  122. //-------------------------------------------------------------------------------------------
  123. /**
  124. * Packages are hierarchical - they are searched recursively to get the info you want
  125. * A PackageMap defines the entire environment - potentially each query that uses that PackageMap will pick a different package within it
  126. * A particular instantiation of a roxie query (i.e. a IQueryFactory) will have a pointer to the specific IRoxiePackage within the active PackageMap
  127. * that is providing its environment.
  128. *
  129. * A PackageMap can also indicate the name of the QuerySet it applies to. If not specified, at will apply to all QuerySets on the Roxie.
  130. *
  131. * A PackageSet is a list of PackageMap id's, and is used to tell Roxie what PackageMaps to load.
  132. * A Roxie can have multiple PackageMap's active. When updating the data, you might:
  133. * - create a new PackageMap to refer to the new data
  134. * - once it has loaded, mark it active, and mark the previous one as inactive
  135. * - Once sure no queries in flight, unload the previous one
  136. *
  137. * Each Roxie will load all PackageMaps that are in any PackageSet whose @process attribute matches the cluster name.
  138. *
  139. * All package information is stored in Dali (and cached locally)
  140. *
  141. * <PackageSets>
  142. * <PackageSet id = 'ps1' process='*'> # use this packageset for all roxies (same as omitting process)
  143. * <PackageMap id='pm1b' querySet='qs1' active='true'/> # Use the PackageMap pm1b for QuerySet qs1 and make it active
  144. * <PackageMap id='pm1a' querySet='qs1' active='false'/> # Use the PackageMap pm1a for QuerySet qs1 but don't make it active
  145. * <PackageMap id='pm2' querySet='dev*' active='true'/> # Use the PackageMap pm1a for all QuerySets with names starting dev and make it active
  146. * </PackageMapSet>
  147. * </PackageSets>
  148. *
  149. * <PackageMaps>
  150. * <PackageMap id='pm1a'>
  151. * <Package id='package1'>
  152. * ...
  153. * </Package>
  154. * <Package id='package2'>
  155. * </Package>
  156. * </PackageMap>
  157. * <PackageMap id='pm2'>
  158. * </PackageMap>
  159. * <PackageMap id='pm3'>
  160. * </PackageMap>
  161. * </PackageMaps>
  162. */
  163. class CResolvedFileCache : implements IResolvedFileCache
  164. {
  165. CriticalSection cacheLock;
  166. CopyMapStringToMyClass<IResolvedFile> files;
  167. public:
  168. // Retrieve number of files in cache
  169. inline unsigned count() const
  170. {
  171. return files.count();
  172. }
  173. // Add a filename and the corresponding IResolvedFile to the cache
  174. virtual void addCache(const char *filename, const IResolvedFile *file)
  175. {
  176. CriticalBlock b(cacheLock);
  177. IResolvedFile *add = const_cast<IResolvedFile *>(file);
  178. add->setCache(this);
  179. files.setValue(filename, add);
  180. }
  181. // Remove an IResolvedFile from the cache
  182. virtual void removeCache(const IResolvedFile *file)
  183. {
  184. CriticalBlock b(cacheLock);
  185. if (traceLevel > 9)
  186. DBGLOG("removeCache %s", file->queryFileName());
  187. // NOTE: it's theoretically possible for the final release to happen after a replacement has been inserted into hash table.
  188. // So only remove from hash table if what we find there matches the item that is being deleted.
  189. IResolvedFile *goer = files.getValue(file->queryFileName());
  190. if (goer == file)
  191. files.remove(file->queryFileName());
  192. // You might want to remove files from the daliServer cache too, but it's not safe to do so here as there may be multiple package caches
  193. }
  194. // Lookup a filename in the cache
  195. virtual IResolvedFile *lookupCache(const char *filename)
  196. {
  197. CriticalBlock b(cacheLock);
  198. IResolvedFile *cache = files.getValue(filename);
  199. if (cache)
  200. {
  201. LINK(cache);
  202. if (cache->isAlive())
  203. return cache;
  204. if (traceLevel)
  205. DBGLOG("Not returning %s from cache as isAlive() returned false", filename);
  206. }
  207. return NULL;
  208. }
  209. };
  210. // Note - we use a separate cache for the misses rather than any clever attempts to overload
  211. // the one cache with a "special" value, since (among other reasons) the misses are cleared
  212. // prior to a package reload, but the hits need not be (as the file will be locked as long as it
  213. // is in the cache)
  214. static CriticalSection daliMissesCrit;
  215. static Owned<KeptLowerCaseAtomTable> daliMisses;
  216. static void noteDaliMiss(const char *filename)
  217. {
  218. CriticalBlock b(daliMissesCrit);
  219. if (traceLevel > 9)
  220. DBGLOG("noteDaliMiss %s", filename);
  221. daliMisses->addAtom(filename);
  222. }
  223. static bool checkCachedDaliMiss(const char *filename)
  224. {
  225. CriticalBlock b(daliMissesCrit);
  226. bool ret = daliMisses->find(filename) != NULL;
  227. if (traceLevel > 9)
  228. DBGLOG("checkCachedDaliMiss %s returns %d", filename, ret);
  229. return ret;
  230. }
  231. static void clearDaliMisses()
  232. {
  233. CriticalBlock b(daliMissesCrit);
  234. if (traceLevel)
  235. DBGLOG("Clearing dali misses cache");
  236. daliMisses.setown(new KeptLowerCaseAtomTable);
  237. }
  238. class CRoxiePackageNode : extends CPackageNode, implements IRoxiePackage
  239. {
  240. protected:
  241. static CResolvedFileCache daliFiles;
  242. mutable CResolvedFileCache fileCache;
  243. virtual aindex_t getBaseCount() const = 0;
  244. virtual const CRoxiePackageNode *getBaseNode(aindex_t pos) const = 0;
  245. virtual bool getSysFieldTranslationEnabled() const {return fieldTranslationEnabled;} //roxie configured value
  246. // Use local package file only to resolve subfile into physical file info
  247. IResolvedFile *resolveLFNusingPackage(const char *fileName) const
  248. {
  249. if (node)
  250. {
  251. StringBuffer xpath;
  252. IPropertyTree *fileInfo = node->queryPropTree(xpath.appendf("File[@id='%s']", fileName).str());
  253. if (fileInfo)
  254. {
  255. Owned <IResolvedFileCreator> result = createResolvedFile(fileName, NULL, false);
  256. result->addSubFile(createFileDescriptorFromRoxieXML(fileInfo), NULL);
  257. return result.getClear();
  258. }
  259. }
  260. return NULL;
  261. }
  262. // Use dali to resolve subfile into physical file info
  263. static IResolvedFile *resolveLFNusingDaliOrLocal(const char *fileName, bool useCache, bool cacheResult, bool writeAccess, bool alwaysCreate)
  264. {
  265. // MORE - look at alwaysCreate... This may be useful to implement earlier locking semantics.
  266. if (traceLevel > 9)
  267. DBGLOG("resolveLFNusingDaliOrLocal %s %d %d %d %d", fileName, useCache, cacheResult, writeAccess, alwaysCreate);
  268. IResolvedFile* result = NULL;
  269. if (useCache)
  270. {
  271. result = daliFiles.lookupCache(fileName);
  272. if (result)
  273. {
  274. if (traceLevel > 9)
  275. DBGLOG("resolveLFNusingDaliOrLocal %s - cache hit", fileName);
  276. return result;
  277. }
  278. }
  279. if (alwaysCreate || !useCache || !checkCachedDaliMiss(fileName))
  280. {
  281. Owned<IRoxieDaliHelper> daliHelper = connectToDali();
  282. if (daliHelper)
  283. {
  284. if (daliHelper->connected())
  285. {
  286. Owned<IDistributedFile> dFile = daliHelper->resolveLFN(fileName, cacheResult, writeAccess);
  287. if (dFile)
  288. result = createResolvedFile(fileName, NULL, dFile.getClear(), daliHelper, cacheResult, writeAccess);
  289. }
  290. else if (!writeAccess) // If we need write access and expect a dali, but don't have one, we should probably fail
  291. {
  292. // we have no dali, we can't lock..
  293. Owned<IFileDescriptor> fd = daliHelper->resolveCachedLFN(fileName);
  294. if (fd)
  295. {
  296. Owned <IResolvedFileCreator> creator = createResolvedFile(fileName, NULL, false);
  297. Owned<IFileDescriptor> remoteFDesc = daliHelper->checkClonedFromRemote(fileName, fd, cacheResult);
  298. creator->addSubFile(fd.getClear(), remoteFDesc.getClear());
  299. result = creator.getClear();
  300. }
  301. }
  302. }
  303. if (!result)
  304. {
  305. StringBuffer useName;
  306. if (strstr(fileName,"::"))
  307. {
  308. bool wasDFS;
  309. makeSinglePhysicalPartName(fileName, useName, true, wasDFS);
  310. }
  311. else
  312. useName.append(fileName);
  313. bool exists = checkFileExists(useName);
  314. if (exists || alwaysCreate)
  315. {
  316. Owned <IResolvedFileCreator> creator = createResolvedFile(fileName, useName, false);
  317. if (exists)
  318. creator->addSubFile(useName);
  319. result = creator.getClear();
  320. }
  321. }
  322. }
  323. if (cacheResult)
  324. {
  325. if (traceLevel > 9)
  326. DBGLOG("resolveLFNusingDaliOrLocal %s - cache add %d", fileName, result != NULL);
  327. if (result)
  328. daliFiles.addCache(fileName, result);
  329. else
  330. noteDaliMiss(fileName);
  331. }
  332. return result;
  333. }
  334. // Use local package and its bases to resolve existing file into physical file info via all supported resolvers
  335. IResolvedFile *lookupExpandedFileName(const char *fileName, bool useCache, bool cacheResult, bool writeAccess, bool alwaysCreate, bool checkCompulsory) const
  336. {
  337. IResolvedFile *result = lookupFile(fileName, useCache, cacheResult, writeAccess, alwaysCreate);
  338. if (!result && (!checkCompulsory || !isCompulsory()))
  339. result = resolveLFNusingDaliOrLocal(fileName, useCache, cacheResult, writeAccess, alwaysCreate);
  340. return result;
  341. }
  342. IResolvedFile *lookupFile(const char *fileName, bool useCache, bool cacheResult, bool writeAccess, bool alwaysCreate) const
  343. {
  344. // Order of resolution:
  345. // 1. Files named in package
  346. // 2. Files named in bases
  347. IResolvedFile* result = useCache ? fileCache.lookupCache(fileName) : NULL;
  348. if (result)
  349. return result;
  350. Owned<const ISimpleSuperFileEnquiry> subFileInfo = resolveSuperFile(fileName);
  351. if (subFileInfo)
  352. {
  353. unsigned numSubFiles = subFileInfo->numSubFiles();
  354. // Note: do not try to optimize the common case of a single subfile
  355. // as we still want to report the superfile info from the resolvedFile
  356. Owned<IResolvedFileCreator> super;
  357. for (unsigned idx = 0; idx < numSubFiles; idx++)
  358. {
  359. StringBuffer subFileName;
  360. subFileInfo->getSubFileName(idx, subFileName);
  361. if (subFileName.length()) // Empty subfile names can come from package file - just ignore
  362. {
  363. if (subFileName.charAt(0)=='~')
  364. {
  365. // implies that a package file had ~ in subfile names - shouldn't really, but we allow it (and just strip the ~)
  366. subFileName.remove(0,1);
  367. }
  368. if (traceLevel > 9)
  369. DBGLOG("Looking up subfile %s", subFileName.str());
  370. Owned<const IResolvedFile> subFileInfo = lookupExpandedFileName(subFileName, useCache, cacheResult, false, false, false); // NOTE - overwriting a superfile does NOT require write access to subfiles
  371. if (subFileInfo)
  372. {
  373. if (!super)
  374. super.setown(createResolvedFile(fileName, NULL, true));
  375. super->addSubFile(subFileInfo);
  376. }
  377. }
  378. }
  379. if (super && cacheResult)
  380. fileCache.addCache(fileName, super);
  381. return super.getClear();
  382. }
  383. result = resolveLFNusingPackage(fileName);
  384. if (result)
  385. {
  386. if (cacheResult)
  387. fileCache.addCache(fileName, result);
  388. return result;
  389. }
  390. aindex_t count = getBaseCount();
  391. for (aindex_t i = 0; i < count; i++)
  392. {
  393. const CRoxiePackageNode *basePackage = getBaseNode(i);
  394. if (!basePackage)
  395. continue;
  396. IResolvedFile *result = basePackage->lookupFile(fileName, useCache, cacheResult, writeAccess, alwaysCreate);
  397. if (result)
  398. return result;
  399. }
  400. return NULL;
  401. }
  402. // default constructor for derived class use
  403. CRoxiePackageNode()
  404. {
  405. }
  406. public:
  407. IMPLEMENT_IINTERFACE;
  408. CRoxiePackageNode(IPropertyTree *p) : CPackageNode(p)
  409. {
  410. }
  411. ~CRoxiePackageNode()
  412. {
  413. assertex(fileCache.count()==0);
  414. // If it's possible for cached objects to outlive the cache I think there is a problem...
  415. // we could set the cache field to null here for any objects still in cache but there would be a race condition
  416. }
  417. virtual void setHash(hash64_t newhash)
  418. {
  419. hash = newhash;
  420. }
  421. virtual IPropertyTreeIterator *getInMemoryIndexInfo(const IPropertyTree &graphNode) const
  422. {
  423. StringBuffer xpath;
  424. xpath.append("SuperFile[@id='").append(queryNodeFileName(graphNode)).append("']");
  425. return lookupElements(xpath.str(), "MemIndex");
  426. }
  427. virtual const IResolvedFile *lookupFileName(const char *_fileName, bool opt, bool useCache, bool cacheResult, IConstWorkUnit *wu) const
  428. {
  429. StringBuffer fileName;
  430. expandLogicalFilename(fileName, _fileName, wu, false);
  431. if (traceLevel > 5)
  432. DBGLOG("lookupFileName %s", fileName.str());
  433. const IResolvedFile *result = lookupExpandedFileName(fileName, useCache, cacheResult, false, false, true);
  434. if (!result)
  435. {
  436. StringBuffer compulsoryMsg;
  437. if (isCompulsory())
  438. compulsoryMsg.append(" (Package is compulsory)");
  439. if (!opt && !pretendAllOpt)
  440. throw MakeStringException(ROXIE_FILE_ERROR, "Could not resolve filename %s%s", fileName.str(), compulsoryMsg.str());
  441. if (traceLevel > 4)
  442. DBGLOG("Could not resolve OPT filename %s%s", fileName.str(), compulsoryMsg.str());
  443. }
  444. return result;
  445. }
  446. virtual IRoxieWriteHandler *createFileName(const char *_fileName, bool overwrite, bool extend, const StringArray &clusters, IConstWorkUnit *wu) const
  447. {
  448. StringBuffer fileName;
  449. expandLogicalFilename(fileName, _fileName, wu, false);
  450. Owned<IResolvedFile> resolved = lookupFile(fileName, false, false, true, true);
  451. if (!resolved)
  452. resolved.setown(resolveLFNusingDaliOrLocal(fileName, false, false, true, true));
  453. if (resolved)
  454. {
  455. if (resolved->exists())
  456. {
  457. if (!overwrite)
  458. throw MakeStringException(99, "Cannot write %s, file already exists (missing OVERWRITE attribute?)", resolved->queryFileName());
  459. if (extend)
  460. UNIMPLEMENTED; // How does extend fit in with the clusterwritemanager stuff? They can't specify cluster and extend together...
  461. resolved->setCache(NULL);
  462. resolved->remove();
  463. }
  464. if (resolved->queryPhysicalName())
  465. fileName.clear().append(resolved->queryPhysicalName());
  466. resolved.clear();
  467. }
  468. Owned<IRoxieDaliHelper> daliHelper = connectToDali();
  469. bool disconnected = !daliHelper->connected();
  470. // MORE - not sure this is really the right test. If there SHOULD be a dali but is's unavailable, we should fail.
  471. Owned<ILocalOrDistributedFile> ldFile = createLocalOrDistributedFile(fileName, NULL, disconnected, !disconnected, true);
  472. if (!ldFile)
  473. throw MakeStringException(ROXIE_FILE_ERROR, "Cannot write %s", fileName.str());
  474. return createRoxieWriteHandler(daliHelper, ldFile.getClear(), clusters);
  475. }
  476. //map ambiguous IHpccPackage
  477. virtual ISimpleSuperFileEnquiry *resolveSuperFile(const char *superFileName) const
  478. {
  479. return CPackageNode::resolveSuperFile(superFileName);
  480. }
  481. virtual const char *queryEnv(const char *varname) const
  482. {
  483. return CPackageNode::queryEnv(varname);
  484. }
  485. virtual bool getEnableFieldTranslation() const
  486. {
  487. return CPackageNode::getEnableFieldTranslation();
  488. }
  489. virtual const IPropertyTree *queryTree() const
  490. {
  491. return CPackageNode::queryTree();
  492. }
  493. virtual hash64_t queryHash() const
  494. {
  495. return CPackageNode::queryHash();
  496. }
  497. virtual const char *queryId() const
  498. {
  499. return CPackageNode::queryId();
  500. }
  501. };
  502. CResolvedFileCache CRoxiePackageNode::daliFiles;
  503. typedef CResolvedPackage<CRoxiePackageNode> CRoxiePackage;
  504. IRoxiePackage *createRoxiePackage(IPropertyTree *p, IRoxiePackageMap *packages)
  505. {
  506. Owned<CRoxiePackage> pkg = new CRoxiePackage(p);
  507. if (packages)
  508. pkg->resolveBases(packages);
  509. return pkg.getClear();
  510. }
  511. //================================================================================================
  512. // CPackageMap - an implementation of IPackageMap using a string map
  513. //================================================================================================
  514. class CRoxiePackageMap : public CPackageMapOf<CRoxiePackageNode, IRoxiePackage>, implements IRoxiePackageMap
  515. {
  516. public:
  517. IMPLEMENT_IINTERFACE;
  518. typedef CPackageMapOf<CRoxiePackageNode, IRoxiePackage> BASE;
  519. CRoxiePackageMap(const char *_packageId, const char *_querySet, bool _active)
  520. : BASE(_packageId, _querySet, _active)
  521. {
  522. }
  523. //map ambiguous IHpccPackageMap interface
  524. virtual const IHpccPackage *queryPackage(const char *name) const
  525. {
  526. return BASE::queryPackage(name);
  527. }
  528. virtual const IHpccPackage *matchPackage(const char *name) const
  529. {
  530. return BASE::matchPackage(name);
  531. }
  532. virtual const char *queryPackageId() const
  533. {
  534. return BASE::queryPackageId();
  535. }
  536. virtual bool isActive() const
  537. {
  538. return BASE::isActive();
  539. }
  540. virtual bool validate(const char *queryid, StringArray &wrn, StringArray &err, StringArray &unmatchedQueries, StringArray &unusedPackages, StringArray &unmatchedFiles) const
  541. {
  542. return BASE::validate(queryid, wrn, err, unmatchedQueries, unusedPackages, unmatchedFiles);
  543. }
  544. virtual void gatherFileMappingForQuery(const char *queryname, IPropertyTree *fileInfo) const
  545. {
  546. BASE::gatherFileMappingForQuery(queryname, fileInfo);
  547. }
  548. virtual const IRoxiePackage *queryRoxiePackage(const char *name) const
  549. {
  550. return queryResolvedPackage(name);
  551. }
  552. virtual const IRoxiePackage *matchRoxiePackage(const char *name) const
  553. {
  554. return matchResolvedPackage(name);
  555. }
  556. };
  557. static CRoxiePackageMap *emptyPackageMap;
  558. static CRoxiePackage *rootPackage;
  559. static SpinLock emptyPackageMapCrit;
  560. static IRoxieDebugSessionManager *debugSessionManager;
  561. extern const IRoxiePackage &queryRootRoxiePackage()
  562. {
  563. SpinBlock b(emptyPackageMapCrit);
  564. if (!rootPackage)
  565. {
  566. // Set up the root package. This contains global settings from topology file
  567. rootPackage = new CRoxiePackage(topology); // attributes become control: environment settings. Rest of topology ignored.
  568. rootPackage->setHash(0); // we don't include the topology in the package hashes...
  569. rootPackage->resolveBases(NULL);
  570. }
  571. return *rootPackage;
  572. }
  573. extern const IRoxiePackageMap &queryEmptyRoxiePackageMap()
  574. {
  575. SpinBlock b(emptyPackageMapCrit);
  576. if (!emptyPackageMap)
  577. emptyPackageMap = new CRoxiePackageMap("<none>", NULL, true);
  578. return *emptyPackageMap;
  579. }
  580. MODULE_INIT(INIT_PRIORITY_STANDARD)
  581. {
  582. emptyPackageMap = NULL;
  583. debugSessionManager = NULL;
  584. return true;
  585. }
  586. MODULE_EXIT()
  587. {
  588. ::Release(emptyPackageMap); // You can't use static Owned to release anything that may own a IPropertyTree
  589. ::Release(rootPackage);
  590. ::Release(debugSessionManager);
  591. }
  592. // IRoxieQuerySetManager
  593. // - CRoxieQuerySetManager -
  594. // - CRoxieServerQuerySetManager
  595. // - CRoxieSlaveQuerySetManager
  596. //
  597. // Manages a set of instantiated queries and allows us to look them up by queryname or alias
  598. //
  599. // IRoxieQuerySetManagerSet
  600. // - CRoxieSlaveQuerySetManagerSet
  601. //
  602. // Manages the IRoxieQuerySetManager for multiple channels
  603. //
  604. // CRoxieQueryPackageManager
  605. // - CRoxieDaliQueryPackageManager
  606. // - CStandaloneQueryPackageManager
  607. //
  608. // Groups a server resource manager and a set of slave resource managers (one per channel) together.
  609. // There is one per PackageMap
  610. //
  611. // CQueryPackageSetManager at outer level
  612. // There will be exactly one of these. It will reload the CQueryPackageManager's if dali Package info changes
  613. //================================================================================================
  614. // CRoxieQuerySetManager - shared base class for slave and server query set manager classes
  615. // Manages a set of instantiated queries and allows us to look them up by queryname or alias,
  616. // as well as controlling their lifespan
  617. //================================================================================================
  618. class CRoxieQuerySetManager : public CInterface, implements IRoxieQuerySetManager
  619. {
  620. protected:
  621. MapStringToMyClass<IQueryFactory> queries;
  622. MapStringToMyClass<IQueryFactory> aliases; // Do we gain anything by having two tables?
  623. unsigned channelNo;
  624. bool active;
  625. StringAttr querySetName;
  626. void addQuery(const char *id, IQueryFactory *n, hash64_t &hash)
  627. {
  628. hash = rtlHash64Data(sizeof(hash), &hash, n->queryHash());
  629. queries.setValue(id, n);
  630. n->Release(); // setValue links
  631. }
  632. void addAlias(const char *alias, const char *original, hash64_t &hash)
  633. {
  634. if (original && alias)
  635. {
  636. IQueryFactory *orig = queries.getValue(original);
  637. if (orig)
  638. {
  639. hash = rtlHash64VStr(alias, hash);
  640. hash = rtlHash64Data(sizeof(hash), &hash, orig->queryHash());
  641. aliases.setValue(alias, orig);
  642. }
  643. else
  644. throw MakeStringException(ROXIE_UNKNOWN_QUERY, "Unknown query %s", original);
  645. }
  646. else
  647. throw MakeStringException(ROXIE_INTERNAL_ERROR, "Invalid parameters to addAlias");
  648. }
  649. virtual IQueryFactory *loadQueryFromDll(const char *id, const IQueryDll *dll, const IHpccPackage &package, const IPropertyTree *stateInfo) = 0;
  650. public:
  651. IMPLEMENT_IINTERFACE;
  652. CRoxieQuerySetManager(unsigned _channelNo, const char *_querySetName)
  653. : queries(true), aliases(true), active(false), querySetName(_querySetName)
  654. {
  655. channelNo = _channelNo;
  656. }
  657. virtual const char *queryId() const
  658. {
  659. return querySetName;
  660. }
  661. virtual bool isActive() const
  662. {
  663. return active;
  664. }
  665. virtual void load(const IPropertyTree *querySet, const IRoxiePackageMap &packages, hash64_t &hash)
  666. {
  667. Owned<IPropertyTreeIterator> queryNames = querySet->getElements("Query");
  668. ForEach (*queryNames)
  669. {
  670. const IPropertyTree &query = queryNames->query();
  671. const char *id = query.queryProp("@id");
  672. const char *dllName = query.queryProp("@dll");
  673. try
  674. {
  675. if (!id || !*id || !dllName || !*dllName)
  676. throw MakeStringException(ROXIE_QUERY_MODIFICATION, "dll and id must be specified");
  677. Owned<const IQueryDll> queryDll = createQueryDll(dllName);
  678. const IHpccPackage *package = NULL;
  679. const char *packageName = query.queryProp("@package");
  680. if (packageName && *packageName)
  681. {
  682. package = packages.queryPackage(packageName); // if a package is specified, require exact match
  683. if (!package)
  684. throw MakeStringException(ROXIE_QUERY_MODIFICATION, "Package %s specified by query %s not found", packageName, id);
  685. }
  686. else
  687. {
  688. package = packages.queryPackage(id); // Look for an exact match, then a fuzzy match, using query name as the package id
  689. if(!package) package = packages.matchPackage(id);
  690. if (!package) package = &queryRootRoxiePackage();
  691. }
  692. assertex(package);
  693. addQuery(id, loadQueryFromDll(id, queryDll.getClear(), *package, &query), hash);
  694. }
  695. catch (IException *E)
  696. {
  697. // we don't want a single bad query in the set to stop us loading all the others
  698. StringBuffer msg;
  699. msg.appendf("Failed to load query %s from %s", id ? id : "(null)", dllName ? dllName : "(null)");
  700. EXCLOG(E, msg.str());
  701. if (id)
  702. {
  703. StringBuffer emsg;
  704. E->errorMessage(emsg);
  705. Owned<IQueryFactory> dummyQuery = loadQueryFromDll(id, NULL, queryRootRoxiePackage(), NULL);
  706. dummyQuery->suspend(true, emsg.str(), "roxie", true);
  707. addQuery(id, dummyQuery.getClear(), hash);
  708. }
  709. E->Release();
  710. }
  711. }
  712. Owned<IPropertyTreeIterator> a = querySet->getElements("Alias");
  713. ForEach(*a)
  714. {
  715. IPropertyTree &item = a->query();
  716. const char *alias = item.queryProp("@name");
  717. const char *original = item.queryProp("@id");
  718. try
  719. {
  720. addAlias(alias, original, hash);
  721. }
  722. catch (IException *E)
  723. {
  724. // we don't want a single bad alias in the set to stop us loading all the others
  725. VStringBuffer msg("Failed to set alias %s on %s", alias, original);
  726. EXCLOG(E, msg.str());
  727. E->Release();
  728. }
  729. }
  730. active = packages.isActive();
  731. if (active)
  732. hash = rtlHash64VStr("active", hash);
  733. }
  734. virtual void getStats(const char *queryName, const char *graphName, StringBuffer &reply, const IRoxieContextLogger &logctx) const
  735. {
  736. Owned<IQueryFactory> f = getQuery(queryName, logctx);
  737. if (f)
  738. {
  739. reply.appendf("<Query id='%s'>\n", queryName);
  740. f->getStats(reply, graphName);
  741. reply.append("</Query>\n");
  742. }
  743. else
  744. throw MakeStringException(ROXIE_UNKNOWN_QUERY, "Unknown query %s", queryName);
  745. }
  746. virtual void resetQueryTimings(const char *queryName, const IRoxieContextLogger &logctx)
  747. {
  748. Owned<IQueryFactory> f = getQuery(queryName, logctx);
  749. if (f)
  750. f->resetQueryTimings();
  751. else
  752. throw MakeStringException(ROXIE_UNKNOWN_QUERY, "Unknown query %s", queryName);
  753. }
  754. virtual void resetAllQueryTimings()
  755. {
  756. HashIterator elems(queries);
  757. for (elems.first(); elems.isValid(); elems.next())
  758. {
  759. IMapping &cur = elems.query();
  760. queries.mapToValue(&cur)->resetQueryTimings();
  761. }
  762. }
  763. virtual void getActivityMetrics(StringBuffer &reply) const
  764. {
  765. HashIterator elems(queries);
  766. for (elems.first(); elems.isValid(); elems.next())
  767. {
  768. IMapping &cur = elems.query();
  769. queries.mapToValue(&cur)->getActivityMetrics(reply);
  770. }
  771. }
  772. virtual void getAllQueryInfo(StringBuffer &reply, bool full, const IRoxieQuerySetManagerSet *slaves, const IRoxieContextLogger &logctx) const
  773. {
  774. HashIterator elems(queries);
  775. for (elems.first(); elems.isValid(); elems.next())
  776. {
  777. IMapping &cur = elems.query();
  778. IQueryFactory *query = queries.mapToValue(&cur);
  779. IArrayOf<IQueryFactory> slaveQueries;
  780. slaves->getQueries(query->queryQueryName(), slaveQueries, logctx);
  781. query->getQueryInfo(reply, full, &slaveQueries, logctx);
  782. }
  783. HashIterator aliasIterator(aliases);
  784. for (aliasIterator.first(); aliasIterator.isValid(); aliasIterator.next())
  785. {
  786. IMapping &cur = aliasIterator.query();
  787. reply.appendf(" <Alias id='%s' query='%s'/>\n", (const char *) cur.getKey(), aliases.mapToValue(&cur)->queryQueryName());
  788. }
  789. }
  790. virtual IQueryFactory *getQuery(const char *id, const IRoxieContextLogger &logctx) const
  791. {
  792. IQueryFactory *ret;
  793. ret = aliases.getValue(id);
  794. if (ret && logctx.queryTraceLevel() > 5)
  795. logctx.CTXLOG("Found query alias %s => %s", id, ret->queryQueryName());
  796. if (!ret)
  797. ret = queries.getValue(id);
  798. return LINK(ret);
  799. }
  800. };
  801. //===============================================================================================================
  802. class CRoxieServerQuerySetManager : public CRoxieQuerySetManager
  803. {
  804. public:
  805. IMPLEMENT_IINTERFACE;
  806. CRoxieServerQuerySetManager(const char *_querySetName)
  807. : CRoxieQuerySetManager(0, _querySetName)
  808. {
  809. }
  810. virtual IQueryFactory * loadQueryFromDll(const char *id, const IQueryDll *dll, const IHpccPackage &package, const IPropertyTree *stateInfo)
  811. {
  812. return createServerQueryFactory(id, dll, package, stateInfo);
  813. }
  814. };
  815. extern IRoxieQuerySetManager *createServerManager(const char *querySet)
  816. {
  817. return new CRoxieServerQuerySetManager(querySet);
  818. }
  819. //===============================================================================================================
  820. class CRoxieSlaveQuerySetManager : public CRoxieQuerySetManager
  821. {
  822. public:
  823. IMPLEMENT_IINTERFACE;
  824. CRoxieSlaveQuerySetManager(unsigned _channelNo, const char *_querySetName)
  825. : CRoxieQuerySetManager(_channelNo, _querySetName)
  826. {
  827. channelNo = _channelNo;
  828. }
  829. virtual IQueryFactory *loadQueryFromDll(const char *id, const IQueryDll *dll, const IHpccPackage &package, const IPropertyTree *stateInfo)
  830. {
  831. return createSlaveQueryFactory(id, dll, package, channelNo, stateInfo);
  832. }
  833. };
  834. class CRoxieSlaveQuerySetManagerSet : public CInterface, implements IRoxieQuerySetManagerSet
  835. {
  836. public:
  837. IMPLEMENT_IINTERFACE;
  838. CRoxieSlaveQuerySetManagerSet(unsigned _numChannels, const char *querySetName)
  839. : numChannels(_numChannels)
  840. {
  841. CriticalBlock b(ccdChannelsCrit);
  842. managers = new CRoxieSlaveQuerySetManager *[numChannels];
  843. memset(managers, 0, sizeof(CRoxieSlaveQuerySetManager *) * numChannels);
  844. Owned<IPropertyTreeIterator> it = ccdChannels->getElements("RoxieSlaveProcess");
  845. ForEach(*it)
  846. {
  847. unsigned channelNo = it->query().getPropInt("@channel", 0);
  848. assertex(channelNo>0 && channelNo<=numChannels);
  849. if (managers[channelNo-1] == NULL)
  850. managers[channelNo-1] = new CRoxieSlaveQuerySetManager(channelNo, querySetName);
  851. else
  852. throw MakeStringException(ROXIE_INVALID_TOPOLOGY, "Invalid topology file - channel %d repeated for this slave", channelNo);
  853. }
  854. }
  855. ~CRoxieSlaveQuerySetManagerSet()
  856. {
  857. for (unsigned channel = 0; channel < numChannels; channel++)
  858. ::Release(managers[channel]);
  859. delete [] managers;
  860. }
  861. inline CRoxieSlaveQuerySetManager *item(int idx)
  862. {
  863. return managers[idx];
  864. }
  865. virtual void load(const IPropertyTree *querySets, const IRoxiePackageMap &packages, hash64_t &hash)
  866. {
  867. for (unsigned channel = 0; channel < numChannels; channel++)
  868. if (managers[channel])
  869. managers[channel]->load(querySets, packages, hash); // MORE - this means the hash depends on the number of channels. Is that desirable?
  870. }
  871. virtual void getQueries(const char *id, IArrayOf<IQueryFactory> &queries, const IRoxieContextLogger &logctx) const
  872. {
  873. for (unsigned channel = 0; channel < numChannels; channel++)
  874. if (managers[channel])
  875. {
  876. IQueryFactory *query = managers[channel]->getQuery(id, logctx);
  877. if (query)
  878. queries.append(*query);
  879. }
  880. }
  881. private:
  882. unsigned numChannels;
  883. CRoxieSlaveQuerySetManager **managers;
  884. };
  885. //===============================================================================================================
  886. class CRoxieDebugSessionManager : public CInterface, implements IRoxieDebugSessionManager
  887. {
  888. protected:
  889. ReadWriteLock debugLock;
  890. MapStringToMyClass<IDebuggerContext> debuggerContexts;
  891. public:
  892. IMPLEMENT_IINTERFACE;
  893. void getActiveQueries(StringBuffer &reply)
  894. {
  895. HashIterator q(debuggerContexts);
  896. for (q.first(); q.isValid(); q.next())
  897. {
  898. IDebuggerContext *ctx = debuggerContexts.mapToValue(&q.query());
  899. reply.appendf(" <Query id='%s' uid='%s' debug='1'/>\n", ctx->queryQueryName(), ctx->queryDebugId());
  900. }
  901. }
  902. virtual void registerDebugId(const char *id, IDebuggerContext *ctx)
  903. {
  904. WriteLockBlock block(debugLock);
  905. debuggerContexts.setValue(id, ctx);
  906. }
  907. virtual void deregisterDebugId(const char *id)
  908. {
  909. WriteLockBlock block(debugLock);
  910. debuggerContexts.remove(id);
  911. }
  912. virtual IDebuggerContext *lookupDebuggerContext(const char *id)
  913. {
  914. ReadLockBlock block(debugLock);
  915. IDebuggerContext *ctx = debuggerContexts.getValue(id);
  916. if (ctx)
  917. return LINK(ctx);
  918. else
  919. {
  920. #ifdef _DEBUG
  921. // In a debug environment, it is convenient to be able to use '*' to mean 'the only active debug session'...
  922. if (strcmp(id, "*")==0 && debuggerContexts.count()==1)
  923. {
  924. HashIterator q(debuggerContexts);
  925. for (q.first(); q.isValid(); q.next())
  926. {
  927. IDebuggerContext *ctx = debuggerContexts.mapToValue(&q.query());
  928. return LINK(ctx);
  929. }
  930. }
  931. #endif
  932. throw MakeStringException(ROXIE_INTERNAL_ERROR, "Debug context %s not found", id);
  933. }
  934. }
  935. };
  936. //===============================================================================================
  937. /*----------------------------------------------------------------------------------------------
  938. * A CRoxieQueryPackageManager object manages all the queries that are currently runnable via XML.
  939. * There may be more than one in existence, but only one will be active and therefore used to
  940. * look up queries that are received - this corresponds to the currently active package.
  941. *-----------------------------------------------------------------------------------------------*/
  942. class CRoxieQueryPackageManager : public CInterface
  943. {
  944. public:
  945. IMPLEMENT_IINTERFACE;
  946. CRoxieQueryPackageManager(unsigned _numChannels, const char *_querySet, const IRoxiePackageMap *_packages)
  947. : numChannels(_numChannels), packages(_packages), querySet(_querySet)
  948. {
  949. queryHash = 0;
  950. }
  951. ~CRoxieQueryPackageManager()
  952. {
  953. }
  954. inline const char *queryPackageId() const
  955. {
  956. return packages->queryPackageId();
  957. }
  958. virtual void reload()
  959. {
  960. // Default is to do nothing...
  961. }
  962. virtual void load() = 0;
  963. virtual hash64_t getHash()
  964. {
  965. CriticalBlock b2(updateCrit);
  966. return queryHash;
  967. }
  968. IRoxieQuerySetManager* getRoxieServerManager()
  969. {
  970. CriticalBlock b2(updateCrit);
  971. return serverManager.getLink();
  972. }
  973. IRoxieQuerySetManagerSet* getRoxieSlaveManagers()
  974. {
  975. CriticalBlock b2(updateCrit);
  976. return slaveManagers.getLink();
  977. }
  978. void getInfo(StringBuffer &reply, const IRoxieContextLogger &logctx) const
  979. {
  980. reply.appendf(" <PackageSet id=\"%s\" querySet=\"%s\"/>\n", queryPackageId(), querySet.get());
  981. }
  982. void resetStats(const char *queryId, const IRoxieContextLogger &logctx)
  983. {
  984. CriticalBlock b(updateCrit);
  985. if (queryId)
  986. {
  987. Owned<IQueryFactory> query = serverManager->getQuery(queryId, logctx);
  988. if (query)
  989. {
  990. const char *id = query->queryQueryName();
  991. serverManager->resetQueryTimings(id, logctx);
  992. for (unsigned channel = 0; channel < numChannels; channel++)
  993. if (slaveManagers->item(channel))
  994. {
  995. slaveManagers->item(channel)->resetQueryTimings(id, logctx);
  996. }
  997. }
  998. }
  999. else
  1000. {
  1001. serverManager->resetAllQueryTimings();
  1002. for (unsigned channel = 0; channel < numChannels; channel++)
  1003. if (slaveManagers->item(channel))
  1004. slaveManagers->item(channel)->resetAllQueryTimings();
  1005. }
  1006. }
  1007. void getStats(const char *queryId, const char *action, const char *graphName, StringBuffer &reply, const IRoxieContextLogger &logctx) const
  1008. {
  1009. CriticalBlock b2(updateCrit);
  1010. Owned<IQueryFactory> query = serverManager->getQuery(queryId, logctx);
  1011. if (query)
  1012. {
  1013. StringBuffer freply;
  1014. serverManager->getStats(queryId, graphName, freply, logctx);
  1015. Owned<IPropertyTree> stats = createPTreeFromXMLString(freply.str());
  1016. for (unsigned channel = 0; channel < numChannels; channel++)
  1017. if (slaveManagers->item(channel))
  1018. {
  1019. StringBuffer sreply;
  1020. slaveManagers->item(channel)->getStats(queryId, graphName, sreply, logctx);
  1021. Owned<IPropertyTree> cstats = createPTreeFromXMLString(sreply.str());
  1022. mergeStats(stats, cstats, 1);
  1023. }
  1024. toXML(stats, reply);
  1025. }
  1026. }
  1027. void getActivityMetrics(StringBuffer &reply) const
  1028. {
  1029. CriticalBlock b2(updateCrit);
  1030. serverManager->getActivityMetrics(reply);
  1031. for (unsigned channel = 0; channel < numChannels; channel++)
  1032. {
  1033. if (slaveManagers->item(channel))
  1034. {
  1035. slaveManagers->item(channel)->getActivityMetrics(reply);
  1036. }
  1037. }
  1038. }
  1039. void getAllQueryInfo(StringBuffer &reply, bool full, const IRoxieContextLogger &logctx) const
  1040. {
  1041. CriticalBlock b2(updateCrit);
  1042. serverManager->getAllQueryInfo(reply, full, slaveManagers, logctx);
  1043. }
  1044. protected:
  1045. void reloadQueryManagers(CRoxieSlaveQuerySetManagerSet *newSlaveManagers, IRoxieQuerySetManager *newServerManager, hash64_t newHash)
  1046. {
  1047. Owned<CRoxieSlaveQuerySetManagerSet> oldSlaveManagers;
  1048. Owned<IRoxieQuerySetManager> oldServerManager;
  1049. {
  1050. // Atomically, replace the existing query managers with the new ones
  1051. CriticalBlock b2(updateCrit);
  1052. oldSlaveManagers.setown(slaveManagers.getClear()); // so that the release happens outside the critblock
  1053. oldServerManager.setown(serverManager.getClear()); // so that the release happens outside the critblock
  1054. slaveManagers.setown(newSlaveManagers);
  1055. serverManager.setown(newServerManager);
  1056. queryHash = newHash;
  1057. }
  1058. }
  1059. mutable CriticalSection updateCrit; // protects updates of slaveManagers and serverManager
  1060. Owned<CRoxieSlaveQuerySetManagerSet> slaveManagers;
  1061. Owned<IRoxieQuerySetManager> serverManager;
  1062. Owned<const IRoxiePackageMap> packages;
  1063. unsigned numChannels;
  1064. hash64_t queryHash;
  1065. StringAttr querySet;
  1066. };
  1067. /**
  1068. * class CRoxieDaliQueryPackageManager - manages queries specified in QuerySets, for a given package set.
  1069. *
  1070. * If the QuerySet is modified, it will be reloaded.
  1071. * There is one CRoxieDaliQueryPackageManager for every PackageSet - only one will be active for query lookup
  1072. * at a given time (the one associated with the active PackageSet).
  1073. *
  1074. * To deploy new data, typically we will load a new PackageSet, make it active, then release the old one
  1075. * A packageSet is not modified while loaded, to avoid timing issues between slaves and server.
  1076. *
  1077. * We need to be able to spot a change (in dali) to the active package indicator (and switch the active CRoxieDaliQueryPackageManager)
  1078. * We need to be able to spot a change (in dali) that adds a new PackageSet
  1079. * We need to decide what to do about a change (in dali) to an existing PackageSet. Maybe we allow it (leave it up to the gui to
  1080. * encourage changing in the right sequence). In which case a change to the package info in dali means reload all global package
  1081. * managers (and then discard the old ones). Hash-based queries means everything should work ok.
  1082. * -> If the active ptr changes, just change what is active
  1083. * If any change to any package set, reload all globalResourceManagers and discard prior
  1084. * The query caching code should ensure that it is quick enough to do so
  1085. *
  1086. **/
  1087. class CRoxieDaliQueryPackageManager : public CRoxieQueryPackageManager, implements ISDSSubscription
  1088. {
  1089. Owned<IRoxieDaliHelper> daliHelper;
  1090. Owned<IDaliPackageWatcher> notifier;
  1091. public:
  1092. IMPLEMENT_IINTERFACE;
  1093. CRoxieDaliQueryPackageManager(unsigned _numChannels, const IRoxiePackageMap *_packages, const char *_querySet)
  1094. : CRoxieQueryPackageManager(_numChannels, _querySet, _packages)
  1095. {
  1096. daliHelper.setown(connectToDali());
  1097. }
  1098. ~CRoxieDaliQueryPackageManager()
  1099. {
  1100. if (notifier)
  1101. daliHelper->releaseSubscription(notifier);
  1102. }
  1103. virtual void notify(SubscriptionId id, const char *xpath, SDSNotifyFlags flags, unsigned valueLen, const void *valueData)
  1104. {
  1105. reload();
  1106. daliHelper->commitCache();
  1107. }
  1108. virtual void load()
  1109. {
  1110. notifier.setown(daliHelper->getQuerySetSubscription(querySet, this));
  1111. reload();
  1112. }
  1113. virtual void reload()
  1114. {
  1115. hash64_t newHash = numChannels;
  1116. Owned<IPropertyTree> newQuerySet = daliHelper->getQuerySet(querySet);
  1117. Owned<CRoxieSlaveQuerySetManagerSet> newSlaveManagers = new CRoxieSlaveQuerySetManagerSet(numChannels, querySet);
  1118. Owned<IRoxieQuerySetManager> newServerManager = createServerManager(querySet);
  1119. newServerManager->load(newQuerySet, *packages, newHash);
  1120. newSlaveManagers->load(newQuerySet, *packages, newHash);
  1121. reloadQueryManagers(newSlaveManagers.getClear(), newServerManager.getClear(), newHash);
  1122. clearKeyStoreCache(false); // Allows us to fully release files we no longer need because of unloaded queries
  1123. }
  1124. };
  1125. class CStandaloneQueryPackageManager : public CRoxieQueryPackageManager
  1126. {
  1127. Owned<IPropertyTree> standaloneDll;
  1128. public:
  1129. IMPLEMENT_IINTERFACE;
  1130. CStandaloneQueryPackageManager(unsigned _numChannels, const char *_querySet, const IRoxiePackageMap *_packages, IPropertyTree *_standaloneDll)
  1131. : CRoxieQueryPackageManager(_numChannels, _querySet, _packages), standaloneDll(_standaloneDll)
  1132. {
  1133. assertex(standaloneDll);
  1134. }
  1135. ~CStandaloneQueryPackageManager()
  1136. {
  1137. }
  1138. virtual void load()
  1139. {
  1140. hash64_t newHash = numChannels;
  1141. Owned<IPropertyTree> newQuerySet = createPTree("QuerySet");
  1142. newQuerySet->setProp("@name", "_standalone");
  1143. newQuerySet->addPropTree("Query", standaloneDll.getLink());
  1144. Owned<CRoxieSlaveQuerySetManagerSet> newSlaveManagers = new CRoxieSlaveQuerySetManagerSet(numChannels, querySet);
  1145. Owned<IRoxieQuerySetManager> newServerManager = createServerManager(querySet);
  1146. newServerManager->load(newQuerySet, *packages, newHash);
  1147. newSlaveManagers->load(newQuerySet, *packages, newHash);
  1148. reloadQueryManagers(newSlaveManagers.getClear(), newServerManager.getClear(), newHash);
  1149. }
  1150. };
  1151. static SpinLock roxieDebugSessionManagerLock;
  1152. extern IRoxieDebugSessionManager &queryRoxieDebugSessionManager()
  1153. {
  1154. SpinBlock b(roxieDebugSessionManagerLock);
  1155. if (!debugSessionManager)
  1156. debugSessionManager = new CRoxieDebugSessionManager();
  1157. return *debugSessionManager;
  1158. }
  1159. class CRoxiePackageSetWatcher : public CInterface, implements ISDSSubscription
  1160. {
  1161. public:
  1162. IMPLEMENT_IINTERFACE;
  1163. CRoxiePackageSetWatcher(IRoxieDaliHelper *_daliHelper, ISDSSubscription *_owner, unsigned numChannels)
  1164. : stateHash(0), daliHelper(_daliHelper), owner(_owner)
  1165. {
  1166. ForEachItemIn(idx, allQuerySetNames)
  1167. {
  1168. createQueryPackageManagers(numChannels, allQuerySetNames.item(idx));
  1169. }
  1170. }
  1171. CRoxiePackageSetWatcher(IRoxieDaliHelper *_daliHelper, ISDSSubscription *_owner, const IQueryDll *standAloneDll, unsigned numChannels, const char *querySet)
  1172. : stateHash(0), daliHelper(_daliHelper), owner(_owner)
  1173. {
  1174. Owned<IPropertyTree> standAloneDllTree;
  1175. standAloneDllTree.setown(createPTree("Query"));
  1176. standAloneDllTree->setProp("@id", "roxie");
  1177. standAloneDllTree->setProp("@dll", standAloneDll->queryDll()->queryName());
  1178. Owned<CRoxieQueryPackageManager> qpm = new CStandaloneQueryPackageManager(numChannels, querySet, LINK(&queryEmptyRoxiePackageMap()), standAloneDllTree.getClear());
  1179. qpm->load();
  1180. stateHash = rtlHash64Data(sizeof(stateHash), &stateHash, qpm->getHash());
  1181. allQueryPackages.append(*qpm.getClear());
  1182. }
  1183. ~CRoxiePackageSetWatcher()
  1184. {
  1185. unsubscribe();
  1186. }
  1187. virtual void notify(SubscriptionId id, const char *xpath, SDSNotifyFlags flags, unsigned valueLen, const void *valueData)
  1188. {
  1189. owner->notify(id, xpath, flags, valueLen, valueData);
  1190. }
  1191. IQueryFactory *lookupLibrary(const char *libraryName, unsigned expectedInterfaceHash, const IRoxieContextLogger &logctx) const
  1192. {
  1193. ForEachItemIn(idx, allQueryPackages)
  1194. {
  1195. Owned<IRoxieQuerySetManager> sm = allQueryPackages.item(idx).getRoxieServerManager();
  1196. if (sm->isActive())
  1197. {
  1198. Owned<IQueryFactory> library = sm->getQuery(libraryName, logctx);
  1199. if (library)
  1200. {
  1201. if (library->isQueryLibrary())
  1202. {
  1203. unsigned foundInterfaceHash = library->getQueryLibraryInterfaceHash();
  1204. if (!foundInterfaceHash || (foundInterfaceHash == expectedInterfaceHash))
  1205. return library.getClear();
  1206. else
  1207. throw MakeStringException(ROXIE_LIBRARY_ERROR, "The library interface found in %s is not compatible (found %d, expected %d)", libraryName, foundInterfaceHash, expectedInterfaceHash);
  1208. }
  1209. else
  1210. throw MakeStringException(ROXIE_LIBRARY_ERROR, "The query resolved by %s is not a library", libraryName);
  1211. }
  1212. }
  1213. }
  1214. throw MakeStringException(ROXIE_LIBRARY_ERROR, "No library available for %s", libraryName);
  1215. }
  1216. IQueryFactory *getQuery(const char *id, IArrayOf<IQueryFactory> *slaveQueries, const IRoxieContextLogger &logctx) const
  1217. {
  1218. ForEachItemIn(idx, allQueryPackages)
  1219. {
  1220. Owned<IRoxieQuerySetManager> sm = allQueryPackages.item(idx).getRoxieServerManager();
  1221. if (sm->isActive())
  1222. {
  1223. IQueryFactory *query = sm->getQuery(id, logctx);
  1224. if (query)
  1225. {
  1226. if (slaveQueries)
  1227. {
  1228. Owned<IRoxieQuerySetManagerSet> slaveManagers = allQueryPackages.item(idx).getRoxieSlaveManagers();
  1229. slaveManagers->getQueries(id, *slaveQueries, logctx);
  1230. }
  1231. return query;
  1232. }
  1233. }
  1234. }
  1235. return NULL;
  1236. }
  1237. int getActivePackageCount() const
  1238. {
  1239. int count = 0;
  1240. ForEachItemIn(idx, allQueryPackages)
  1241. {
  1242. Owned<IRoxieQuerySetManager> sm = allQueryPackages.item(idx).getRoxieServerManager();
  1243. if (sm->isActive())
  1244. count++;
  1245. }
  1246. return count;
  1247. }
  1248. inline hash64_t queryHash() const
  1249. {
  1250. return stateHash;
  1251. }
  1252. void getAllQueryInfo(StringBuffer &reply, bool full, const IRoxieContextLogger &logctx) const
  1253. {
  1254. ForEachItemIn(idx, allQueryPackages)
  1255. {
  1256. Owned<IRoxieQuerySetManager> serverManager = allQueryPackages.item(idx).getRoxieServerManager();
  1257. Owned<IRoxieQuerySetManagerSet> slaveManagers = allQueryPackages.item(idx).getRoxieSlaveManagers();
  1258. serverManager->getAllQueryInfo(reply, full, slaveManagers, logctx);
  1259. }
  1260. }
  1261. void getActivityMetrics(StringBuffer &reply) const
  1262. {
  1263. ForEachItemIn(idx, allQueryPackages)
  1264. {
  1265. CRoxieQueryPackageManager &qpm = allQueryPackages.item(idx);
  1266. qpm.getActivityMetrics(reply);
  1267. }
  1268. }
  1269. void getInfo(StringBuffer &reply, const IRoxieContextLogger &logctx) const
  1270. {
  1271. reply.append("<PackageSets>\n");
  1272. ForEachItemIn(idx, allQueryPackages)
  1273. {
  1274. allQueryPackages.item(idx).getInfo(reply, logctx);
  1275. }
  1276. reply.append("</PackageSets>\n");
  1277. }
  1278. void getStats(StringBuffer &reply, const char *id, const char *action, const char *graphName, const IRoxieContextLogger &logctx) const
  1279. {
  1280. ForEachItemIn(idx, allQueryPackages)
  1281. {
  1282. allQueryPackages.item(idx).getStats(id, action, graphName, reply, logctx);
  1283. }
  1284. }
  1285. void resetStats(const char *id, const IRoxieContextLogger &logctx) const
  1286. {
  1287. ForEachItemIn(idx, allQueryPackages)
  1288. {
  1289. allQueryPackages.item(idx).resetStats(id, logctx);
  1290. }
  1291. }
  1292. private:
  1293. ISDSSubscription *owner;
  1294. CIArrayOf<CRoxieQueryPackageManager> allQueryPackages;
  1295. IArrayOf<IDaliPackageWatcher> notifiers;
  1296. Linked<IRoxieDaliHelper> daliHelper;
  1297. hash64_t stateHash;
  1298. void createQueryPackageManager(unsigned numChannels, const IRoxiePackageMap *packageMap, const char *querySet)
  1299. {
  1300. Owned<CRoxieQueryPackageManager> qpm = new CRoxieDaliQueryPackageManager(numChannels, packageMap, querySet);
  1301. qpm->load();
  1302. stateHash = rtlHash64Data(sizeof(stateHash), &stateHash, qpm->getHash());
  1303. allQueryPackages.append(*qpm.getClear());
  1304. }
  1305. void createQueryPackageManagers(unsigned numChannels, const char *querySet)
  1306. {
  1307. int loadedPackages = 0;
  1308. int activePackages = 0;
  1309. Owned<IPropertyTree> packageTree = daliHelper->getPackageSets();
  1310. Owned<IPropertyTreeIterator> packageSets = packageTree->getElements("PackageSet");
  1311. ForEach(*packageSets)
  1312. {
  1313. IPropertyTree &ps = packageSets->query();
  1314. const char *packageSetSpec = ps.queryProp("@process");
  1315. if (!packageSetSpec || WildMatch(roxieName, packageSetSpec, false))
  1316. {
  1317. if (traceLevel)
  1318. {
  1319. DBGLOG("Loading package set %s, process spec %s", ps.queryProp("@id") ? ps.queryProp("@id") : "<no-id>",
  1320. packageSetSpec ? packageSetSpec : "<*>");
  1321. }
  1322. Owned<IPropertyTreeIterator> packageMaps = ps.getElements("PackageMap");
  1323. ForEach(*packageMaps)
  1324. {
  1325. IPropertyTree &pm = packageMaps->query();
  1326. const char *packageMapId = pm.queryProp("@id");
  1327. const char *packageMapFilter = pm.queryProp("@querySet");
  1328. if (packageMapId && *packageMapId && (!packageMapFilter || WildMatch(querySet, packageMapFilter, false)))
  1329. {
  1330. bool isActive = pm.getPropBool("@active", true);
  1331. if (traceLevel)
  1332. DBGLOG("Loading package map %s, active %s", packageMapId, isActive ? "true" : "false");
  1333. try
  1334. {
  1335. Owned<CRoxiePackageMap> packageMap = new CRoxiePackageMap(packageMapId, packageMapFilter, isActive);
  1336. Owned<IPropertyTree> xml = daliHelper->getPackageMap(packageMapId);
  1337. packageMap->load(xml);
  1338. createQueryPackageManager(numChannels, packageMap.getLink(), querySet);
  1339. loadedPackages++;
  1340. if (isActive)
  1341. activePackages++;
  1342. notifiers.append(*daliHelper->getPackageMapSubscription(packageMapId, this));
  1343. }
  1344. catch (IException *E)
  1345. {
  1346. StringBuffer msg;
  1347. msg.appendf("Failed to load package map %s", packageMapId);
  1348. EXCLOG(E, msg.str());
  1349. E->Release();
  1350. }
  1351. }
  1352. }
  1353. }
  1354. }
  1355. if (!loadedPackages)
  1356. {
  1357. if (traceLevel)
  1358. DBGLOG("Loading empty package for QuerySet %s", querySet);
  1359. createQueryPackageManager(numChannels, LINK(&queryEmptyRoxiePackageMap()), querySet);
  1360. }
  1361. else if (traceLevel)
  1362. DBGLOG("Loaded %d packages (%d active)", loadedPackages, activePackages);
  1363. }
  1364. void unsubscribe()
  1365. {
  1366. ForEachItemIn(idx, notifiers)
  1367. {
  1368. daliHelper->releaseSubscription(&notifiers.item(idx));
  1369. }
  1370. notifiers.kill();
  1371. }
  1372. };
  1373. class CRoxiePackageSetManager : public CInterface, implements IRoxieQueryPackageManagerSet, implements ISDSSubscription
  1374. {
  1375. Owned<IDaliPackageWatcher> notifier;
  1376. public:
  1377. IMPLEMENT_IINTERFACE;
  1378. CRoxiePackageSetManager(const IQueryDll *_standAloneDll) :
  1379. standAloneDll(_standAloneDll)
  1380. {
  1381. daliHelper.setown(connectToDali(ROXIE_DALI_CONNECT_TIMEOUT));
  1382. notifier.setown(daliHelper->getPackageSetsSubscription(this));
  1383. }
  1384. ~CRoxiePackageSetManager()
  1385. {
  1386. }
  1387. virtual void load()
  1388. {
  1389. try
  1390. {
  1391. reload();
  1392. daliHelper->commitCache();
  1393. controlSem.signal();
  1394. }
  1395. catch(IException *E)
  1396. {
  1397. EXCLOG(E, "No configuration could be loaded");
  1398. controlSem.interrupt();
  1399. throw; // Roxie will refuse to start up if configuration invalid
  1400. }
  1401. }
  1402. virtual void doControlMessage(IPropertyTree *xml, StringBuffer &reply, const IRoxieContextLogger &logctx)
  1403. {
  1404. if (!controlSem.wait(20000))
  1405. throw MakeStringException(ROXIE_TIMEOUT, "Timed out waiting for current control query to complete");
  1406. try
  1407. {
  1408. _doControlMessage(xml, reply, logctx);
  1409. reply.append(" <Status>ok</Status>\n");
  1410. }
  1411. catch(IException *E)
  1412. {
  1413. controlSem.signal();
  1414. EXCLOG(E);
  1415. throw;
  1416. }
  1417. catch(...)
  1418. {
  1419. controlSem.signal();
  1420. throw;
  1421. }
  1422. controlSem.signal();
  1423. }
  1424. virtual IQueryFactory *lookupLibrary(const char *libraryName, unsigned expectedInterfaceHash, const IRoxieContextLogger &logctx) const
  1425. {
  1426. ReadLockBlock b(packageCrit);
  1427. return allQueryPackages->lookupLibrary(libraryName, expectedInterfaceHash, logctx);
  1428. }
  1429. virtual IQueryFactory *getQuery(const char *id, IArrayOf<IQueryFactory> *slaveQueries, const IRoxieContextLogger &logctx) const
  1430. {
  1431. ReadLockBlock b(packageCrit);
  1432. return allQueryPackages->getQuery(id, slaveQueries, logctx);
  1433. }
  1434. virtual int getActivePackageCount() const
  1435. {
  1436. ReadLockBlock b(packageCrit);
  1437. return allQueryPackages->getActivePackageCount();
  1438. }
  1439. virtual void notify(SubscriptionId id, const char *xpath, SDSNotifyFlags flags, unsigned valueLen, const void *valueData)
  1440. {
  1441. reload();
  1442. daliHelper->commitCache();
  1443. }
  1444. private:
  1445. Owned<const IQueryDll> standAloneDll;
  1446. Owned<CRoxieDebugSessionManager> debugSessionManager;
  1447. Owned<IRoxieDaliHelper> daliHelper;
  1448. mutable ReadWriteLock packageCrit;
  1449. InterruptableSemaphore controlSem;
  1450. Owned<CRoxiePackageSetWatcher> allQueryPackages;
  1451. void reload()
  1452. {
  1453. clearDaliMisses();
  1454. // We want to kill the old packages, but not until we have created the new ones
  1455. // So that the query/dll caching will work for anything that is not affected by the changes
  1456. Owned<CRoxiePackageSetWatcher> newPackages;
  1457. if (standAloneDll)
  1458. newPackages.setown(new CRoxiePackageSetWatcher(daliHelper, this, standAloneDll, numChannels, "roxie"));
  1459. else
  1460. newPackages.setown(new CRoxiePackageSetWatcher(daliHelper, this, numChannels));
  1461. // Hold the lock for as little time as we can
  1462. // Note that we must NOT hold the lock during the delete of the old object - or we deadlock.
  1463. // Hence the slightly convoluted code below
  1464. Owned<CRoxiePackageSetWatcher> oldPackages; // NB Destroyed outside the WriteLockBlock
  1465. {
  1466. WriteLockBlock b(packageCrit);
  1467. oldPackages.setown(allQueryPackages.getLink()); // To ensure that the setown just below does not delete it
  1468. allQueryPackages.setown(newPackages.getClear());
  1469. }
  1470. }
  1471. // Common code used by control:queries and control:getQueryXrefInfo
  1472. void getQueryInfo(IPropertyTree *control, StringBuffer &reply, bool full, const IRoxieContextLogger &logctx) const
  1473. {
  1474. Owned<IPropertyTreeIterator> ids = control->getElements("Query");
  1475. reply.append("<Queries>\n");
  1476. if (ids->first())
  1477. {
  1478. ForEach(*ids)
  1479. {
  1480. const char *id = ids->query().queryProp("@id");
  1481. if (id)
  1482. {
  1483. IArrayOf<IQueryFactory> slaveQueries;
  1484. Owned<IQueryFactory> query = getQuery(id, &slaveQueries, logctx);
  1485. if (query)
  1486. query->getQueryInfo(reply, full, &slaveQueries, logctx);
  1487. else
  1488. reply.appendf(" <Query id=\"%s\" error=\"Query not found\"/>\n", id);
  1489. }
  1490. }
  1491. }
  1492. else
  1493. {
  1494. ReadLockBlock readBlock(packageCrit);
  1495. allQueryPackages->getAllQueryInfo(reply, full, logctx);
  1496. }
  1497. reply.append("</Queries>\n");
  1498. }
  1499. void _doControlMessage(IPropertyTree *control, StringBuffer &reply, const IRoxieContextLogger &logctx)
  1500. {
  1501. const char *queryName = control->queryName();
  1502. logctx.CTXLOG("doControlMessage - %s", queryName);
  1503. assertex(memicmp(queryName, "control:", 8) == 0);
  1504. bool unknown = false;
  1505. switch (_toupper(queryName[8]))
  1506. {
  1507. case 'A':
  1508. if (stricmp(queryName, "control:aclupdate") == 0)
  1509. {
  1510. // MORE - do nothing for now - possibly needed in the future - leave this so no exception is thrown
  1511. }
  1512. else if (stricmp(queryName, "control:activeQueries")==0)
  1513. {
  1514. if (debugSessionManager)
  1515. debugSessionManager->getActiveQueries(reply);
  1516. }
  1517. else if (stricmp(queryName, "control:activitymetrics")==0)
  1518. {
  1519. ReadLockBlock readBlock(packageCrit);
  1520. allQueryPackages->getActivityMetrics(reply);
  1521. }
  1522. else if (stricmp(queryName, "control:alive")==0)
  1523. {
  1524. reply.appendf("<Alive restarts='%d'/>", restarts);
  1525. }
  1526. else
  1527. unknown = true;
  1528. break;
  1529. case 'B':
  1530. if (stricmp(queryName, "control:blobCacheMem")==0)
  1531. {
  1532. blobCacheMB = control->getPropInt("@val", 0);
  1533. topology->setPropInt("@blobCacheMem", blobCacheMB);
  1534. setBlobCacheMem(blobCacheMB * 0x100000);
  1535. }
  1536. else
  1537. unknown = true;
  1538. break;
  1539. case 'C':
  1540. if (stricmp(queryName, "control:checkCompleted")==0)
  1541. {
  1542. checkCompleted = control->getPropBool("@val", true);
  1543. topology->setPropBool("@checkCompleted", checkCompleted );
  1544. }
  1545. else if (stricmp(queryName, "control:checkingHeap")==0)
  1546. {
  1547. defaultCheckingHeap = control->getPropBool("@val", true);
  1548. topology->setPropInt("@checkingHeap", defaultCheckingHeap);
  1549. }
  1550. else if (stricmp(queryName, "control:clearIndexCache")==0)
  1551. {
  1552. bool clearAll = control->getPropBool("@clearAll", true);
  1553. clearKeyStoreCache(clearAll);
  1554. }
  1555. else if (stricmp(queryName, "control:closedown")==0)
  1556. {
  1557. closedown();
  1558. }
  1559. else if (stricmp(queryName, "control:closeExpired")==0)
  1560. {
  1561. queryFileCache().closeExpired(false);
  1562. queryFileCache().closeExpired(true);
  1563. }
  1564. else if (stricmp(queryName, "control:closeLocalExpired")==0)
  1565. {
  1566. queryFileCache().closeExpired(false);
  1567. }
  1568. else if (stricmp(queryName, "control:closeRemoteExpired")==0)
  1569. {
  1570. queryFileCache().closeExpired(true);
  1571. }
  1572. else
  1573. unknown = true;
  1574. break;
  1575. case 'D':
  1576. if (stricmp(queryName, "control:dafilesrvLookupTimeout")==0)
  1577. {
  1578. dafilesrvLookupTimeout = control->getPropInt("@val", 10000);
  1579. topology->setPropInt("@dafilesrvLookupTimeout", dafilesrvLookupTimeout);
  1580. }
  1581. else if (stricmp(queryName, "control:defaultConcatPreload")==0)
  1582. {
  1583. defaultConcatPreload = control->getPropInt("@val", 0);
  1584. topology->setPropInt("@defaultConcatPreload", defaultConcatPreload);
  1585. }
  1586. else if (stricmp(queryName, "control:defaultFetchPreload")==0)
  1587. {
  1588. defaultFetchPreload = control->getPropInt("@val", 0);
  1589. topology->setPropInt("@defaultFetchPreload", defaultFetchPreload);
  1590. }
  1591. else if (stricmp(queryName, "control:defaultFullKeyedJoinPreload")==0)
  1592. {
  1593. defaultFullKeyedJoinPreload = control->getPropInt("@val", 0);
  1594. topology->setPropInt("@defaultFullKeyedJoinPreload", defaultFullKeyedJoinPreload);
  1595. }
  1596. else if (stricmp(queryName, "control:defaultHighPriorityTimeLimit")==0)
  1597. {
  1598. defaultTimeLimit[1] = control->getPropInt("@limit", 0);
  1599. topology->setPropInt("@defaultHighPriorityTimeLimit", defaultTimeLimit[1]);
  1600. }
  1601. else if (stricmp(queryName, "control:defaultHighPriorityTimeWarning")==0)
  1602. {
  1603. defaultWarnTimeLimit[1] = control->getPropInt("@limit", 0);
  1604. topology->setPropInt("@defaultHighPriorityTimeWarning", defaultWarnTimeLimit[1]);
  1605. }
  1606. else if (stricmp(queryName, "control:defaultKeyedJoinPreload")==0)
  1607. {
  1608. defaultKeyedJoinPreload = control->getPropInt("@val", 0);
  1609. topology->setPropInt("@defaultKeyedJoinPreload", defaultKeyedJoinPreload);
  1610. }
  1611. else if (stricmp(queryName, "control:defaultLowPriorityTimeLimit")==0)
  1612. {
  1613. defaultTimeLimit[0] = control->getPropInt("@limit", 0);
  1614. topology->setPropInt("@defaultLowPriorityTimeLimit", defaultTimeLimit[0]);
  1615. }
  1616. else if (stricmp(queryName, "control:defaultLowPriorityTimeWarning")==0)
  1617. {
  1618. defaultWarnTimeLimit[0] = control->getPropInt("@limit", 0);
  1619. topology->setPropInt("@defaultLowPriorityTimeWarning", defaultWarnTimeLimit[0]);
  1620. }
  1621. else if (stricmp(queryName, "control:defaultParallelJoinPreload")==0)
  1622. {
  1623. defaultParallelJoinPreload = control->getPropInt("@val", 0);
  1624. topology->setPropInt("@defaultParallelJoinPreload", defaultParallelJoinPreload);
  1625. }
  1626. else if (stricmp(queryName, "control:defaultSLAPriorityTimeLimit")==0)
  1627. {
  1628. defaultTimeLimit[2] = control->getPropInt("@limit", 0);
  1629. topology->setPropInt("@defaultSLAPriorityTimeLimit", defaultTimeLimit[2]);
  1630. }
  1631. else if (stricmp(queryName, "control:defaultSLAPriorityTimeWarning")==0)
  1632. {
  1633. defaultWarnTimeLimit[2] = control->getPropInt("@limit", 0);
  1634. topology->setPropInt("@defaultSLAPriorityTimeWarning", defaultWarnTimeLimit[2]);
  1635. }
  1636. else if (stricmp(queryName, "control:deleteUnneededPhysicalFiles")==0)
  1637. {
  1638. UNIMPLEMENTED;
  1639. }
  1640. else if (stricmp(queryName, "control:deleteUnneededQueryCacheFiles")==0)
  1641. {
  1642. UNIMPLEMENTED;
  1643. }
  1644. else if (stricmp(queryName, "control:doIbytiDelay")==0)
  1645. { // WARNING: use with extra care only during inactivity on system
  1646. doIbytiDelay = control->getPropBool("@val", true);
  1647. topology->setPropBool("@doIbytiDelay", doIbytiDelay);
  1648. }
  1649. else
  1650. unknown = true;
  1651. break;
  1652. case 'E':
  1653. if (stricmp(queryName, "control:enableKeyDiff")==0)
  1654. {
  1655. enableKeyDiff = control->getPropBool("@val", true);
  1656. topology->setPropBool("@enableKeyDiff", enableKeyDiff);
  1657. }
  1658. else
  1659. unknown = true;
  1660. break;
  1661. case 'F':
  1662. if (stricmp(queryName, "control:fieldTranslationEnabled")==0)
  1663. {
  1664. fieldTranslationEnabled = control->getPropBool("@val", true);
  1665. topology->setPropInt("@fieldTranslationEnabled", fieldTranslationEnabled);
  1666. }
  1667. else if (stricmp(queryName, "control:flushJHtreeCacheOnOOM")==0)
  1668. {
  1669. flushJHtreeCacheOnOOM = control->getPropBool("@val", true);
  1670. topology->setPropInt("@flushJHtreeCacheOnOOM", flushJHtreeCacheOnOOM);
  1671. }
  1672. else
  1673. unknown = true;
  1674. break;
  1675. case 'G':
  1676. if (stricmp(queryName, "control:getACLinfo") == 0)
  1677. {
  1678. // MORE - do nothing for now - possibly needed in the future - leave this so no exception is thrown
  1679. }
  1680. else if (stricmp(queryName, "control:getClusterName")==0)
  1681. {
  1682. reply.appendf("<clusterName id='%s'/>", roxieName.str());
  1683. }
  1684. else if (stricmp(queryName, "control:getKeyInfo")==0)
  1685. {
  1686. reportInMemoryIndexStatistics(reply, control->queryProp("@id"), control->getPropInt("@count", 10));
  1687. }
  1688. else if (stricmp(queryName, "control:getQueryXrefInfo")==0)
  1689. {
  1690. getQueryInfo(control, reply, true, logctx);
  1691. }
  1692. else if (stricmp(queryName, "control:getQuery")==0)
  1693. {
  1694. const char* id = control->queryProp("@id");
  1695. if (!id)
  1696. throw MakeStringException(ROXIE_MISSING_PARAMS, "No query name specified");
  1697. Owned<IQueryFactory> q = getQuery(id, NULL, logctx);
  1698. if (q)
  1699. {
  1700. Owned<IPropertyTree> tempTree = q->cloneQueryXGMML();
  1701. tempTree->setProp("@roxieName", roxieName.str());
  1702. toXML(tempTree, reply);
  1703. }
  1704. else
  1705. throw MakeStringException(ROXIE_UNKNOWN_QUERY, "Unknown query %s", id);
  1706. }
  1707. else if (stricmp(queryName, "control:getQueryWarningTime")==0)
  1708. {
  1709. const char *id = control->queryProp("Query/@id");
  1710. if (!id)
  1711. badFormat();
  1712. Owned<IQueryFactory> f = getQuery(id, NULL, logctx);
  1713. if (f)
  1714. {
  1715. unsigned warnLimit = f->getWarnTimeLimit();
  1716. reply.appendf("<QueryTimeWarning val='%d'/>", warnLimit);
  1717. }
  1718. }
  1719. else if (stricmp(queryName, "control:getBuildVersion")==0)
  1720. {
  1721. reply.appendf("<version id='%s'/>", BUILD_TAG);
  1722. }
  1723. else
  1724. unknown = true;
  1725. break;
  1726. case 'I':
  1727. if (stricmp(queryName, "control:indexmetrics")==0)
  1728. {
  1729. getIndexMetrics(reply);
  1730. }
  1731. else if (stricmp(queryName, "control:inMemoryKeysEnabled")==0)
  1732. {
  1733. inMemoryKeysEnabled = control->getPropBool("@val", true);
  1734. topology->setPropBool("@inMemoryKeysEnabled", inMemoryKeysEnabled);
  1735. }
  1736. else
  1737. unknown = true;
  1738. break;
  1739. case 'L':
  1740. if (stricmp(queryName, "control:leafCacheMem")==0)
  1741. {
  1742. leafCacheMB = control->getPropInt("@val", 50);
  1743. topology->setPropInt("@leafCacheMem", leafCacheMB);
  1744. setLeafCacheMem(leafCacheMB * 0x100000);
  1745. }
  1746. else if (stricmp(queryName, "control:listFileOpenErrors")==0)
  1747. {
  1748. // this just creates a delta state file to remove references to Keys / Files we now longer have interest in
  1749. StringAttrMapping *mapping = queryFileCache().queryFileErrorList();
  1750. HashIterator iter(*mapping);
  1751. StringBuffer err;
  1752. for (iter.first(); iter.isValid(); iter.next())
  1753. {
  1754. IMapping &cur = iter.query();
  1755. StringAttr *item = mapping->mapToValue(&cur);
  1756. const char *filename = (const char*)cur.getKey();
  1757. const char *filetype = item->get();
  1758. reply.appendf("<file><name>%s</name><type>%s</type></file>", filename, filetype);
  1759. }
  1760. }
  1761. else if (stricmp(queryName, "control:listUnusedFiles")==0)
  1762. {
  1763. UNIMPLEMENTED;
  1764. }
  1765. else if (stricmp(queryName, "control:lockDali")==0)
  1766. {
  1767. topology->setPropBool("@lockDali", true);
  1768. if (daliHelper)
  1769. daliHelper->disconnect();
  1770. saveTopology();
  1771. }
  1772. else if (stricmp(queryName, "control:logfullqueries")==0)
  1773. {
  1774. logFullQueries = control->getPropBool("@val", true);
  1775. topology->setPropBool("@logFullQueries", logFullQueries);
  1776. }
  1777. else
  1778. unknown = true;
  1779. break;
  1780. case 'M':
  1781. if (stricmp(queryName, "control:memoryStatsInterval")==0)
  1782. {
  1783. memoryStatsInterval = (unsigned) control->getPropInt64("@val", 0);
  1784. roxiemem::setMemoryStatsInterval(memoryStatsInterval);
  1785. topology->setPropInt64("@memoryStatsInterval", memoryStatsInterval);
  1786. }
  1787. else if (stricmp(queryName, "control:memtrace")==0)
  1788. {
  1789. roxiemem::memTraceLevel = control->getPropInt("@level", 0);
  1790. topology->setPropInt("@memTraceLevel", roxiemem::memTraceLevel);
  1791. }
  1792. else if (stricmp(queryName, "control:memtracesizelimit")==0)
  1793. {
  1794. roxiemem::memTraceSizeLimit = (memsize_t) control->getPropInt64("@val", control->getPropInt64("@value", 0)); // used to accept @value so coded like this for backward compatibility
  1795. topology->setPropInt64("@memTraceSizeLimit", roxiemem::memTraceSizeLimit);
  1796. }
  1797. else if (stricmp(queryName, "control:metrics")==0)
  1798. {
  1799. roxieMetrics->getMetrics(reply);
  1800. }
  1801. else if (stricmp(queryName, "control:minFreeDiskSpace")==0)
  1802. {
  1803. minFreeDiskSpace = (unsigned) control->getPropInt64("@val", 1048576);
  1804. topology->setPropInt64("@minFreeDiskSpace", minFreeDiskSpace);
  1805. }
  1806. else if (stricmp(queryName, "control:misctrace")==0)
  1807. {
  1808. miscDebugTraceLevel = control->getPropInt("@level", 0);
  1809. topology->setPropInt("@miscDebugTraceLevel", miscDebugTraceLevel);
  1810. }
  1811. else
  1812. unknown = true;
  1813. break;
  1814. case 'N':
  1815. if (stricmp(queryName, "control:nodeCachePreload")==0)
  1816. {
  1817. nodeCachePreload = control->getPropBool("@val", true);
  1818. topology->setPropBool("@nodeCachePreload", nodeCachePreload);
  1819. setNodeCachePreload(nodeCachePreload);
  1820. }
  1821. else if (stricmp(queryName, "control:nodeCacheMem")==0)
  1822. {
  1823. nodeCacheMB = control->getPropInt("@val", 100);
  1824. topology->setPropInt("@nodeCacheMem", nodeCacheMB);
  1825. setNodeCacheMem(nodeCacheMB * 0x100000);
  1826. }
  1827. else if (stricmp(queryName, "control:numFilesToProcess")==0)
  1828. {
  1829. int numFiles = queryFileCache().numFilesToCopy();
  1830. reply.appendf("<FilesToProcess value='%d'/>", numFiles);
  1831. }
  1832. else
  1833. unknown = true;
  1834. break;
  1835. case 'P':
  1836. if (stricmp(queryName, "control:parallelAggregate")==0)
  1837. {
  1838. parallelAggregate = control->getPropInt("@val", 0);
  1839. if (!parallelAggregate)
  1840. parallelAggregate = hdwInfo.numCPUs;
  1841. if (!parallelAggregate)
  1842. parallelAggregate = 1;
  1843. topology->setPropInt("@parallelAggregate", parallelAggregate);
  1844. }
  1845. else if (stricmp(queryName, "control:pingInterval")==0)
  1846. {
  1847. unsigned newInterval = (unsigned) control->getPropInt64("@val", 0);
  1848. if (newInterval && !pingInterval)
  1849. {
  1850. pingInterval = newInterval; // best to set before the start...
  1851. startPingTimer();
  1852. }
  1853. else if (pingInterval && !newInterval)
  1854. stopPingTimer(); // but after the stop
  1855. pingInterval = newInterval;
  1856. topology->setPropInt64("@pingInterval", pingInterval);
  1857. }
  1858. else if (stricmp(queryName, "control:preabortIndexReadsThreshold")==0)
  1859. {
  1860. preabortIndexReadsThreshold = control->getPropInt("@val", 100);
  1861. topology->setPropInt("@preabortIndexReadsThreshold", preabortIndexReadsThreshold);
  1862. }
  1863. else if (stricmp(queryName, "control:preabortKeyedJoinsThreshold")==0)
  1864. {
  1865. preabortKeyedJoinsThreshold = control->getPropInt("@val", 100);
  1866. topology->setPropInt("@preabortKeyedJoinsThreshold", preabortKeyedJoinsThreshold);
  1867. }
  1868. else if (stricmp(queryName, "control:probeAllRows")==0)
  1869. {
  1870. probeAllRows = control->getPropBool("@val", true);
  1871. }
  1872. else
  1873. unknown = true;
  1874. break;
  1875. case 'Q':
  1876. if (stricmp(queryName, "control:queries")==0)
  1877. {
  1878. getQueryInfo(control, reply, false, logctx);
  1879. }
  1880. else if (stricmp(queryName, "control:queryAggregates")==0)
  1881. {
  1882. time_t from;
  1883. const char *fromTime = control->queryProp("@from");
  1884. if (fromTime)
  1885. {
  1886. CDateTime f;
  1887. f.setString(fromTime, NULL, true);
  1888. from = f.getSimple();
  1889. }
  1890. else
  1891. from = startupTime;
  1892. time_t to;
  1893. const char *toTime = control->queryProp("@to");
  1894. if (toTime)
  1895. {
  1896. CDateTime t;
  1897. t.setString(toTime, NULL, true);
  1898. to = t.getSimple();
  1899. }
  1900. else
  1901. time(&to);
  1902. const char *id = control->queryProp("Query/@id");
  1903. if (id)
  1904. {
  1905. Owned<IQueryFactory> f = getQuery(id, NULL, logctx); // MORE - slave too?
  1906. if (f)
  1907. {
  1908. Owned<const IPropertyTree> stats = f->getQueryStats(from, to);
  1909. toXML(stats, reply);
  1910. }
  1911. else
  1912. throw MakeStringException(ROXIE_CONTROL_MSG_ERROR, "Unknown query %s", id);
  1913. }
  1914. else
  1915. {
  1916. bool includeAllQueries = control->getPropBool("@all", true);
  1917. Owned<const IPropertyTree> stats = getAllQueryStats(includeAllQueries, from, to);
  1918. toXML(stats, reply);
  1919. }
  1920. }
  1921. else if (stricmp(queryName, "control:queryPackageInfo")==0)
  1922. {
  1923. ReadLockBlock readBlock(packageCrit);
  1924. allQueryPackages->getInfo(reply, logctx);
  1925. }
  1926. else if (stricmp(queryName, "control:queryStats")==0)
  1927. {
  1928. const char *id = control->queryProp("Query/@id");
  1929. if (!id)
  1930. badFormat();
  1931. const char *action = control->queryProp("Query/@action");
  1932. const char *graphName = 0;
  1933. if (action)
  1934. {
  1935. if (stricmp(action, "listGraphNames") == 0)
  1936. {
  1937. Owned<IQueryFactory> query = getQuery(id, NULL, logctx); // MORE - slave too?
  1938. if (query)
  1939. {
  1940. reply.appendf("<Query id='%s'>\n", id);
  1941. StringArray graphNames;
  1942. query->getGraphNames(graphNames);
  1943. ForEachItemIn(idx, graphNames)
  1944. {
  1945. const char *graphName = graphNames.item(idx);
  1946. reply.appendf("<Graph id='%s'/>", graphName);
  1947. }
  1948. reply.appendf("</Query>\n");
  1949. }
  1950. return; // done
  1951. }
  1952. else if (stricmp(action, "selectGraph") == 0)
  1953. graphName = control->queryProp("Query/@name");
  1954. else if (stricmp(action, "allGraphs") != 0) // if we get here and its NOT allgraphs - then error
  1955. throw MakeStringException(ROXIE_CONTROL_MSG_ERROR, "invalid action in control:queryStats %s", action);
  1956. }
  1957. ReadLockBlock readBlock(packageCrit);
  1958. allQueryPackages->getStats(reply, id, action, graphName, logctx);
  1959. }
  1960. else if (stricmp(queryName, "control:queryWuid")==0)
  1961. {
  1962. UNIMPLEMENTED;
  1963. }
  1964. else
  1965. unknown = true;
  1966. break;
  1967. case 'R':
  1968. if (stricmp(queryName, "control:reload")==0)
  1969. {
  1970. reload();
  1971. if (daliHelper && daliHelper->connected())
  1972. reply.appendf("<Dali connected='1'/>");
  1973. else
  1974. reply.appendf("<Dali connected='0'/>");
  1975. unsigned __int64 thash = getTopologyHash();
  1976. unsigned __int64 shash;
  1977. {
  1978. ReadLockBlock readBlock(packageCrit);
  1979. shash = allQueryPackages->queryHash();
  1980. }
  1981. reply.appendf("<State hash='%"I64F"u' topologyHash='%"I64F"u'/>", shash, thash);
  1982. }
  1983. else if (stricmp(queryName, "control:resetindexmetrics")==0)
  1984. {
  1985. resetIndexMetrics();
  1986. }
  1987. else if (stricmp(queryName, "control:resetmetrics")==0)
  1988. {
  1989. roxieMetrics->resetMetrics();
  1990. }
  1991. else if (stricmp(queryName, "control:resetquerystats")==0)
  1992. {
  1993. ReadLockBlock readBlock(packageCrit);
  1994. Owned<IPropertyTreeIterator> queries = control->getElements("Query");
  1995. if (queries->first())
  1996. {
  1997. while (queries->isValid())
  1998. {
  1999. IPropertyTree &query = queries->query();
  2000. const char *id = query.queryProp("@id");
  2001. if (!id)
  2002. badFormat();
  2003. allQueryPackages->resetStats(id, logctx);
  2004. queries->next();
  2005. }
  2006. }
  2007. else
  2008. allQueryPackages->resetStats(NULL, logctx);
  2009. }
  2010. else if (stricmp(queryName, "control:resetremotedalicache")==0)
  2011. {
  2012. queryNamedGroupStore().resetCache();
  2013. }
  2014. else if (stricmp(queryName, "control:restart")==0)
  2015. {
  2016. FatalError("Roxie process restarted by operator request");
  2017. }
  2018. else if (stricmp(queryName, "control:retrieveActivityDetails")==0)
  2019. {
  2020. UNIMPLEMENTED;
  2021. }
  2022. else if (stricmp(queryName, "control:retrieveFileInfo")==0)
  2023. {
  2024. UNIMPLEMENTED;
  2025. }
  2026. else if (stricmp(queryName, "control:roxiememstats") == 0)
  2027. {
  2028. StringBuffer memStats;
  2029. queryMemoryPoolStats(memStats);
  2030. reply.append("<MemoryStats>").append(memStats.str()).append("</MemoryStats>\n");
  2031. }
  2032. else
  2033. unknown = true;
  2034. break;
  2035. case 'S':
  2036. if (stricmp(queryName, "control:setCopyResources")==0)
  2037. {
  2038. copyResources = control->getPropBool("@val", true);
  2039. topology->setPropBool("@copyResources", copyResources);
  2040. }
  2041. else if (stricmp(queryName, "control:simpleLocalKeyedJoins")==0)
  2042. {
  2043. simpleLocalKeyedJoins = control->getPropBool("@val", true);
  2044. }
  2045. else if (stricmp(queryName, "control:soapInfo")==0)
  2046. {
  2047. UNIMPLEMENTED;
  2048. }
  2049. else if (stricmp(queryName, "control:soapTrace")==0)
  2050. {
  2051. soapTraceLevel = control->getPropInt("@level", 0);
  2052. topology->setPropInt("@soapTraceLevel", soapTraceLevel);
  2053. }
  2054. else if (stricmp(queryName, "control:socketCheckInterval")==0)
  2055. {
  2056. socketCheckInterval = (unsigned) control->getPropInt64("@val", 0);
  2057. topology->setPropInt64("@socketCheckInterval", socketCheckInterval);
  2058. }
  2059. else if (stricmp(queryName, "control:state")==0)
  2060. {
  2061. if (daliHelper && daliHelper->connected())
  2062. reply.appendf("<Dali connected='1'/>");
  2063. else
  2064. reply.appendf("<Dali connected='0'/>");
  2065. unsigned __int64 thash = getTopologyHash();
  2066. unsigned __int64 shash;
  2067. {
  2068. ReadLockBlock readBlock(packageCrit);
  2069. shash = allQueryPackages->queryHash();
  2070. }
  2071. reply.appendf("<State hash='%"I64F"u' topologyHash='%"I64F"u'/>", shash, thash);
  2072. }
  2073. else if (stricmp(queryName, "control:steppingEnabled")==0)
  2074. {
  2075. steppingEnabled = control->getPropBool("@val", true);
  2076. }
  2077. else if (stricmp(queryName, "control:suspend")==0)
  2078. {
  2079. StringBuffer id(control->queryProp("Query/@id"));
  2080. if (!id.length())
  2081. badFormat();
  2082. {
  2083. Owned<IQueryFactory> f = getQuery(id, NULL, logctx); // MORE - slave too?
  2084. if (f)
  2085. id.clear().append(f->queryQueryName()); // use the spelling of the query stored with the query factory
  2086. }
  2087. UNIMPLEMENTED;
  2088. }
  2089. else if (stricmp(queryName, "control:suspendChannel")==0)
  2090. {
  2091. if (control->hasProp("@channel") && control->hasProp("@suspend"))
  2092. {
  2093. unsigned channel = control->getPropInt("@channel", 0);
  2094. bool suspend = control->getPropBool("@suspend", true);
  2095. CriticalBlock b(ccdChannelsCrit);
  2096. if (channel)
  2097. {
  2098. StringBuffer xpath;
  2099. IPropertyTree *slaveNode = ccdChannels->queryPropTree(xpath.appendf("RoxieSlaveProcess[@channel='%u']", channel).str());
  2100. if (slaveNode)
  2101. {
  2102. ROQ->suspendChannel(channel, suspend, logctx);
  2103. slaveNode->setPropBool("@suspended", suspend);
  2104. }
  2105. else
  2106. throw MakeStringException(ROXIE_INVALID_INPUT, "Unknown channel %u", channel);
  2107. }
  2108. else
  2109. {
  2110. Owned<IPropertyTreeIterator> slaves = ccdChannels->getElements("RoxieSlaveProcess");
  2111. ForEach(*slaves)
  2112. {
  2113. IPropertyTree &slaveNode = slaves->query();
  2114. channel = slaveNode.getPropInt("@channel", 0);
  2115. ROQ->suspendChannel(channel, suspend, logctx);
  2116. slaveNode.setPropBool("@suspended", suspend);
  2117. }
  2118. }
  2119. toXML(ccdChannels, reply);
  2120. }
  2121. else
  2122. badFormat();
  2123. }
  2124. else if (stricmp(queryName, "control:suspendServer")==0)
  2125. {
  2126. if (control->hasProp("@port") && control->hasProp("@suspend"))
  2127. {
  2128. unsigned port = control->getPropInt("@port", 0);
  2129. bool suspend = control->getPropBool("@suspend", true);
  2130. CriticalBlock b(ccdChannelsCrit);
  2131. if (port)
  2132. {
  2133. StringBuffer xpath;
  2134. IPropertyTree *serverNode = ccdChannels->queryPropTree(xpath.appendf("RoxieServerProcess[@port='%u']", port).str());
  2135. if (serverNode)
  2136. {
  2137. suspendRoxieListener(port, suspend);
  2138. serverNode->setPropBool("@suspended", suspend);
  2139. }
  2140. else
  2141. throw MakeStringException(ROXIE_INVALID_INPUT, "Unknown Roxie server port %u", port);
  2142. }
  2143. else
  2144. {
  2145. Owned<IPropertyTreeIterator> servers = ccdChannels->getElements("RoxieServerProcess");
  2146. ForEach(*servers)
  2147. {
  2148. IPropertyTree &serverNode = servers->query();
  2149. port = serverNode.getPropInt("@port", 0);
  2150. suspendRoxieListener(port, suspend);
  2151. serverNode.setPropBool("@suspended", suspend);
  2152. }
  2153. }
  2154. toXML(ccdChannels, reply);
  2155. }
  2156. else
  2157. badFormat();
  2158. }
  2159. else if (stricmp(queryName, "control:systemMonitor")==0)
  2160. {
  2161. unsigned interval = control->getPropInt("@interval", 60000);
  2162. bool enable = control->getPropBool("@enable", true);
  2163. if (enable)
  2164. startPerformanceMonitor(interval);
  2165. else
  2166. stopPerformanceMonitor();
  2167. }
  2168. else
  2169. unknown = true;
  2170. break;
  2171. case 'T':
  2172. if (stricmp(queryName, "control:testSlaveFailure")==0)
  2173. {
  2174. testSlaveFailure = control->getPropInt("@val", 20);
  2175. }
  2176. else if (stricmp(queryName, "control:timeActivities")==0)
  2177. {
  2178. timeActivities = control->getPropBool("@val", true);
  2179. topology->setPropInt("@timeActivities", timeActivities);
  2180. }
  2181. else if (stricmp(queryName, "control:timings")==0)
  2182. {
  2183. reply.append("<Timings>");
  2184. timer->getTimings(reply);
  2185. reply.append("</Timings>");
  2186. if (control->getPropBool("@reset", false))
  2187. {
  2188. timer->reset();
  2189. }
  2190. }
  2191. else if (stricmp(queryName, "control:topology")==0)
  2192. {
  2193. toXML(topology, reply);
  2194. }
  2195. else if (stricmp(queryName, "control:trace")==0)
  2196. {
  2197. traceLevel = control->getPropInt("@level", 0);
  2198. if (traceLevel > MAXTRACELEVEL)
  2199. traceLevel = MAXTRACELEVEL;
  2200. topology->setPropInt("@traceLevel", traceLevel);
  2201. }
  2202. else if (stricmp(queryName, "control:traceServerSideCache")==0)
  2203. {
  2204. traceServerSideCache = control->getPropBool("@val", true);
  2205. topology->setPropInt("@traceServerSideCache", traceServerSideCache);
  2206. }
  2207. else if (stricmp(queryName, "control:traceJHtreeAllocations")==0)
  2208. {
  2209. traceJHtreeAllocations = control->getPropBool("@val", true);
  2210. topology->setPropInt("@traceJHtreeAllocations", traceJHtreeAllocations);
  2211. }
  2212. else if (stricmp(queryName, "control:traceSmartStepping")==0)
  2213. {
  2214. traceSmartStepping = control->getPropBool("@val", true);
  2215. topology->setPropInt("@traceSmartStepping", traceSmartStepping);
  2216. }
  2217. else if (stricmp(queryName, "control:traceStartStop")==0)
  2218. {
  2219. traceStartStop = control->getPropBool("@val", true);
  2220. topology->setPropInt("@traceStartStop", traceStartStop);
  2221. }
  2222. else
  2223. unknown = true;
  2224. break;
  2225. case 'U':
  2226. if (stricmp(queryName, "control:udptrace")==0)
  2227. {
  2228. udpTraceLevel = control->getPropInt("@level", 0);
  2229. topology->setPropInt("@udpTraceLevel", udpTraceLevel);
  2230. }
  2231. else if (stricmp(queryName, "control:unlockDali")==0)
  2232. {
  2233. topology->setPropBool("@lockDali", false);
  2234. // Dali will reattach via the timer that checks every so often if can reattach...
  2235. saveTopology();
  2236. }
  2237. else if (stricmp(queryName, "control:unsuspend")==0)
  2238. {
  2239. UNIMPLEMENTED;
  2240. }
  2241. else if (stricmp(queryName, "control:userMetric")==0)
  2242. {
  2243. const char *name = control->queryProp("@name");
  2244. const char *regex= control->queryProp("@regex");
  2245. if (name && regex)
  2246. {
  2247. roxieMetrics->addUserMetric(name, regex);
  2248. // MORE - we could add to topology, we could check for dups, and we could support removing them.
  2249. }
  2250. else
  2251. throw MakeStringException(ROXIE_MISSING_PARAMS, "Metric name or regex missing");
  2252. }
  2253. else if (stricmp(queryName, "control:useTreeCopy")==0)
  2254. {
  2255. useTreeCopy = control->getPropBool("@val", true);
  2256. topology->setPropInt("@useTreeCopy", useTreeCopy);
  2257. }
  2258. else
  2259. unknown = true;
  2260. break;
  2261. case 'W':
  2262. if (stricmp(queryName, "control:watchActivityId")==0)
  2263. {
  2264. watchActivityId = control->getPropInt("@id", true);
  2265. }
  2266. else
  2267. unknown = true;
  2268. break;
  2269. default:
  2270. unknown = true;
  2271. break;
  2272. }
  2273. if (unknown)
  2274. throw MakeStringException(ROXIE_UNKNOWN_QUERY, "Unknown query %s", queryName);
  2275. }
  2276. void badFormat()
  2277. {
  2278. throw MakeStringException(ROXIE_INVALID_INPUT, "Badly formated control query");
  2279. }
  2280. hash64_t getTopologyHash()
  2281. {
  2282. StringBuffer xml;
  2283. toXML(topology, xml, 0, XML_SortTags);
  2284. return rtlHash64Data(xml.length(), xml.str(), 707018);
  2285. }
  2286. };
  2287. extern IRoxieQueryPackageManagerSet *createRoxiePackageSetManager(const IQueryDll *standAloneDll)
  2288. {
  2289. return new CRoxiePackageSetManager(standAloneDll);
  2290. }
  2291. IRoxieQueryPackageManagerSet *globalPackageSetManager = NULL;
  2292. extern void loadPlugins()
  2293. {
  2294. if (pluginDirectory.length() && isDirectory(pluginDirectory.str()))
  2295. {
  2296. plugins = new SafePluginMap(&PluginCtx, traceLevel >= 1);
  2297. plugins->loadFromDirectory(pluginDirectory);
  2298. }
  2299. }
  2300. extern void cleanupPlugins()
  2301. {
  2302. delete plugins;
  2303. plugins = NULL;
  2304. }
  2305. /*=======================================================================================================
  2306. * mergeStats and associated code is used to combine the graph stats from multiple nodes in a cluster into
  2307. * a single aggregate structure
  2308. * It should be moved into ccdquery.cpp really
  2309. *========================================================================================================*/
  2310. typedef void (*mergefunc)(IPropertyTree *s1, IPropertyTree *s2, unsigned level);
  2311. struct MergeInfo
  2312. {
  2313. const char *element;
  2314. const char *attribute;
  2315. mergefunc f;
  2316. };
  2317. void mergeSubGraphs(IPropertyTree *s1, IPropertyTree *s2, unsigned);
  2318. void mergeNodes(IPropertyTree *s1, IPropertyTree *s2)
  2319. {
  2320. Owned<IPropertyTreeIterator> elems = s1->getElements("att");
  2321. ForEach(*elems)
  2322. {
  2323. IPropertyTree &e1 = elems->query();
  2324. unsigned __int64 v1 = e1.getPropInt64("@value", 0);
  2325. const char *name = e1.queryProp("@name");
  2326. if (stricmp(name, "_kind")==0 && v1 == TAKsubgraph)
  2327. {
  2328. IPropertyTree *s1child = s1->queryPropTree("att/graph");
  2329. IPropertyTree *s2child = s2->queryPropTree("att[@name='_kind']/graph");
  2330. if (s1child && s2child)
  2331. {
  2332. mergeSubGraphs(s1child, s2child, 0);
  2333. s2->removeProp("att[@name='_kind']");
  2334. }
  2335. }
  2336. else
  2337. {
  2338. StringBuffer xpath;
  2339. xpath.appendf("att[@name='%s']", name);
  2340. const char *type = e1.queryProp("@type");
  2341. if (type)
  2342. {
  2343. IPropertyTree *e2 = s2->queryPropTree(xpath.str());
  2344. if (e2)
  2345. {
  2346. unsigned __int64 v2 = e2->getPropInt64("@value", 0);
  2347. if (strcmp(name, "max")==0)
  2348. {
  2349. if (v2 > v1)
  2350. e1.setPropInt64("@value", v2);
  2351. }
  2352. else if (strcmp(type, "min")==0)
  2353. {
  2354. if (v2 < v1)
  2355. e1.setPropInt64("@value", v2);
  2356. }
  2357. else if (strcmp(type, "sum")==0)
  2358. e1.setPropInt64("@value", v1+v2);
  2359. else
  2360. throw MakeStringException(ROXIE_UNKNOWN_QUERY, "Unknown type %s in graph statistics", type);
  2361. s2->removeTree(e2);
  2362. }
  2363. }
  2364. else
  2365. {
  2366. // remove from s2 any complete dups
  2367. const char *s1val = e1.queryProp("@value");
  2368. Owned<IPropertyTreeIterator> s2elems = s2->getElements(xpath.str());
  2369. IArrayOf<IPropertyTree> goers;
  2370. ForEach(*s2elems)
  2371. {
  2372. IPropertyTree &e2 = s2elems->query();
  2373. const char *s2val = e2.queryProp("@value");
  2374. if ((!s1val && !s2val) || (s1val && s2val && strcmp(s1val, s2val)==0))
  2375. goers.append(*LINK(&e2));
  2376. }
  2377. ForEachItemIn(idx, goers)
  2378. {
  2379. s2->removeTree(&goers.item(idx));
  2380. }
  2381. }
  2382. }
  2383. }
  2384. elems.setown(s2->getElements("*"));
  2385. ForEach(*elems)
  2386. {
  2387. IPropertyTree &e2 = elems->query();
  2388. s1->addPropTree(e2.queryName(), LINK(&e2));
  2389. }
  2390. }
  2391. void mergeSubGraphs(IPropertyTree *s1, IPropertyTree *s2, unsigned)
  2392. {
  2393. Owned<IPropertyTreeIterator> elems = s1->getElements("*");
  2394. ForEach(*elems)
  2395. {
  2396. IPropertyTree &e1 = elems->query();
  2397. const char *elemName = e1.queryName();
  2398. StringBuffer xpath;
  2399. if (strcmp(elemName, "att")==0)
  2400. {
  2401. xpath.appendf("att[@name='%s']", e1.queryProp("@name"));
  2402. IPropertyTree *e2 = s2->queryPropTree(xpath.str());
  2403. if (e2)
  2404. s2->removeTree(e2);
  2405. }
  2406. else
  2407. {
  2408. xpath.appendf("%s[@id='%s']", elemName, e1.queryProp("@id"));
  2409. IPropertyTree *e2 = s2->queryPropTree(xpath.str());
  2410. if (e2)
  2411. {
  2412. mergeNodes(&e1, e2);
  2413. s2->removeTree(e2);
  2414. }
  2415. }
  2416. }
  2417. elems.setown(s2->getElements("*"));
  2418. ForEach(*elems)
  2419. {
  2420. IPropertyTree &e2 = elems->query();
  2421. s1->addPropTree(e2.queryName(), LINK(&e2));
  2422. }
  2423. }
  2424. void mergeNode(IPropertyTree *s1, IPropertyTree *s2, unsigned level);
  2425. MergeInfo mergeTable[] =
  2426. {
  2427. {"Query", "@id", mergeStats},
  2428. {"Graph", "@id", mergeStats},
  2429. {"xgmml", NULL, mergeStats},
  2430. {"graph", NULL, mergeStats},
  2431. {"node", "@id", mergeNode},
  2432. {"att", NULL, mergeStats},
  2433. {"graph", NULL, mergeSubGraphs},
  2434. };
  2435. void mergeNode(IPropertyTree *s1, IPropertyTree *s2, unsigned level)
  2436. {
  2437. if (s1->hasProp("att/@name"))
  2438. mergeNodes(s1, s2);
  2439. else
  2440. mergeStats(s1, s2, level);
  2441. }
  2442. void mergeStats(IPropertyTree *s1, IPropertyTree *s2, unsigned level)
  2443. {
  2444. MergeInfo & mi = mergeTable[level];
  2445. Owned<IPropertyTreeIterator> elems = s1->getElements(mi.element);
  2446. ForEach(*elems)
  2447. {
  2448. IPropertyTree &e1 = elems->query();
  2449. StringBuffer xpath;
  2450. if (mi.attribute)
  2451. xpath.appendf("%s[%s='%s']", mi.element, mi.attribute, e1.queryProp(mi.attribute));
  2452. else
  2453. xpath.append(mi.element);
  2454. IPropertyTree *e2 = s2->queryPropTree(xpath.str());
  2455. if (e2)
  2456. {
  2457. mi.f(&e1, e2, level+1);
  2458. s2->removeTree(e2);
  2459. }
  2460. }
  2461. elems.setown(s2->getElements(mi.element));
  2462. ForEach(*elems)
  2463. {
  2464. IPropertyTree &e2 = elems->query();
  2465. s1->addPropTree(mi.element, LINK(&e2));
  2466. }
  2467. }
  2468. void mergeStats(IPropertyTree *s1, IPropertyTree *s2)
  2469. {
  2470. Owned<IPropertyTreeIterator> elems = s2->getElements("Exception");
  2471. ForEach(*elems)
  2472. {
  2473. s1->addPropTree("Exception", LINK(&elems->query()));
  2474. }
  2475. mergeStats(s1, s2, 0);
  2476. }
  2477. #ifdef _USE_CPPUNIT
  2478. #include <cppunit/extensions/HelperMacros.h>
  2479. static const char *g1 =
  2480. "<Stats>"
  2481. "<Query id='stats'>"
  2482. "<Graph id='graph1'>"
  2483. "<xgmml>"
  2484. "<graph>"
  2485. "<node id='1'>"
  2486. "<att>"
  2487. "<graph>"
  2488. "<node id='2' label='Temp Table'>"
  2489. "<att name='name' value='d'/>"
  2490. "<att name='_kind' value='25'/>"
  2491. "<att name='helper' value='f2'/>"
  2492. "</node>"
  2493. "<node id='2a'>"
  2494. " <att name='_kind' value='1'>" // TAKsubgraph
  2495. " <graph>"
  2496. " <node id='7696' label='Nested'>"
  2497. " <att name='seeks' value='15' type='sum'/>"
  2498. " </node>"
  2499. " </graph>"
  2500. " </att>"
  2501. "</node>"
  2502. "<node id='3' label='Filter'>"
  2503. "<att name='name' value='ds'/>"
  2504. "<att name='_kind' value='5'/>"
  2505. "<att name='helper' value='f3'/>"
  2506. "</node>"
  2507. "<att name='rootGraph' value='1'/>"
  2508. "<edge id='2_0' source='2' target='3'>"
  2509. "<att name='count' value='15' type='sum'/>"
  2510. "<att name='started' value='1'/>"
  2511. "<att name='stopped' value='1'/>"
  2512. "</edge>"
  2513. "<edge id='3_0' source='3' target='5'>"
  2514. "<att name='count' value='15' type='sum'/>"
  2515. "<att name='started' value='1'/>"
  2516. "<att name='stopped' value='1'/>"
  2517. "</edge>"
  2518. "<edge id='5_0' source='5' target='6'>"
  2519. "<att name='count' value='3' type='sum'/>"
  2520. "<att name='started' value='1'/>"
  2521. "<att name='stopped' value='1'/>"
  2522. "</edge>"
  2523. "<edge id='5_1' source='5' target='7'>"
  2524. "<att name='_sourceIndex' value='1'/>"
  2525. "<att name='count' value='15' type='sum'/>"
  2526. "<att name='started' value='1'/>"
  2527. "<att name='stopped' value='1'/>"
  2528. "</edge>"
  2529. "</graph>"
  2530. "</att>"
  2531. "</node>"
  2532. "</graph>"
  2533. "</xgmml>"
  2534. "</Graph>"
  2535. "</Query>"
  2536. "</Stats>";
  2537. static const char *g2 =
  2538. "<Stats>"
  2539. "<Query id='stats'>"
  2540. "<Graph id='graph1'>"
  2541. "<xgmml>"
  2542. "<graph>"
  2543. "<node id='1'>"
  2544. "<att>"
  2545. "<graph>"
  2546. "<node id='2' label='Temp Table'>"
  2547. "<att name='name' value='d'/>"
  2548. "<att name='_kind' value='25'/>"
  2549. "<att name='helper' value='f2'/>"
  2550. "</node>"
  2551. "<node id='2a'>"
  2552. " <att name='_kind' value='1'>" // TAKsubgraph
  2553. " <graph>"
  2554. " <node id='7696' label='Nested'>"
  2555. " <att name='seeks' value='25' type='sum'/>"
  2556. " </node>"
  2557. " </graph>"
  2558. " </att>"
  2559. "</node>"
  2560. "<node id='4' label='Filter2'>"
  2561. "<att name='name' value='ds2'/>"
  2562. "<att name='_kind' value='53'/>"
  2563. "<att name='helper' value='f23'/>"
  2564. "</node>"
  2565. "<att name='rootGraph' value='1'/>"
  2566. "<edge id='2_0' source='2' target='3'>"
  2567. "<att name='count' value='15' type='sum'/>"
  2568. "<att name='started' value='1'/>"
  2569. "<att name='stopped' value='1'/>"
  2570. "</edge>"
  2571. "<edge id='3_0' source='3' target='5'>"
  2572. "<att name='count' value='15' type='sum'/>"
  2573. "<att name='started' value='1'/>"
  2574. "<att name='stopped' value='1'/>"
  2575. "</edge>"
  2576. "<edge id='5_0' source='5' target='6'>"
  2577. "<att name='count' value='3' type='sum'/>"
  2578. "<att name='started' value='1'/>"
  2579. "<att name='stopped' value='1'/>"
  2580. "</edge>"
  2581. "</graph>"
  2582. "</att>"
  2583. "</node>"
  2584. "</graph>"
  2585. "</xgmml>"
  2586. "</Graph>"
  2587. "</Query>"
  2588. "</Stats>";
  2589. static const char *expected =
  2590. "<Stats>"
  2591. "<Query id='stats'>"
  2592. "<Graph id='graph1'>"
  2593. "<xgmml>"
  2594. "<graph>"
  2595. "<node id='1'>"
  2596. "<att>"
  2597. "<graph>"
  2598. "<node id='2' label='Temp Table'>"
  2599. "<att name='name' value='d'/>"
  2600. "<att name='_kind' value='25'/>"
  2601. "<att name='helper' value='f2'/>"
  2602. "</node>"
  2603. "<node id='2a'>"
  2604. " <att name='_kind' value='1'>" // TAKsubgraph
  2605. " <graph>"
  2606. " <node id='7696' label='Nested'>"
  2607. " <att name='seeks' type='sum' value='40'/>"
  2608. " </node>"
  2609. " </graph>"
  2610. " </att>"
  2611. "</node>"
  2612. "<node id='3' label='Filter'>"
  2613. "<att name='name' value='ds'/>"
  2614. "<att name='_kind' value='5'/>"
  2615. "<att name='helper' value='f3'/>"
  2616. "</node>"
  2617. "<node id='4' label='Filter2'>"
  2618. "<att name='name' value='ds2'/>"
  2619. "<att name='_kind' value='53'/>"
  2620. "<att name='helper' value='f23'/>"
  2621. "</node>"
  2622. "<att name='rootGraph' value='1'/>"
  2623. "<edge id='2_0' source='2' target='3'>"
  2624. "<att name='count' value='30' type='sum'/>"
  2625. "<att name='started' value='1'/>"
  2626. "<att name='stopped' value='1'/>"
  2627. "</edge>"
  2628. "<edge id='3_0' source='3' target='5'>"
  2629. "<att name='count' value='30' type='sum'/>"
  2630. "<att name='started' value='1'/>"
  2631. "<att name='stopped' value='1'/>"
  2632. "</edge>"
  2633. "<edge id='5_0' source='5' target='6'>"
  2634. "<att name='count' value='6' type='sum'/>"
  2635. "<att name='started' value='1'/>"
  2636. "<att name='stopped' value='1'/>"
  2637. "</edge>"
  2638. "<edge id='5_1' source='5' target='7'>"
  2639. "<att name='_sourceIndex' value='1'/>"
  2640. "<att name='count' value='15' type='sum'/>"
  2641. "<att name='started' value='1'/>"
  2642. "<att name='stopped' value='1'/>"
  2643. "</edge>"
  2644. "</graph>"
  2645. "</att>"
  2646. "</node>"
  2647. "</graph>"
  2648. "</xgmml>"
  2649. "</Graph>"
  2650. "</Query>"
  2651. "</Stats>"
  2652. ;
  2653. class MergeStatsTest : public CppUnit::TestFixture
  2654. {
  2655. CPPUNIT_TEST_SUITE( MergeStatsTest );
  2656. CPPUNIT_TEST(test1);
  2657. CPPUNIT_TEST_SUITE_END();
  2658. protected:
  2659. void test1()
  2660. {
  2661. Owned<IPropertyTree> p1 = createPTreeFromXMLString(g1);
  2662. Owned<IPropertyTree> p2 = createPTreeFromXMLString(g2);
  2663. Owned<IPropertyTree> e = createPTreeFromXMLString(expected);
  2664. mergeStats(p1, p2);
  2665. StringBuffer s1, s2;
  2666. toXML(p1, s1);
  2667. toXML(e, s2);
  2668. CPPUNIT_ASSERT(strcmp(s1, s2)==0);
  2669. }
  2670. };
  2671. CPPUNIT_TEST_SUITE_REGISTRATION( MergeStatsTest );
  2672. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MergeStatsTest, "MergeStatsTest" );
  2673. #endif