ccdcontext.cpp 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2013 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 "nbcd.hpp"
  16. #include "rtlread_imp.hpp"
  17. #include "thorplugin.hpp"
  18. #include "thorxmlread.hpp"
  19. #include "roxiemem.hpp"
  20. #include "ccd.hpp"
  21. #include "ccdcontext.hpp"
  22. #include "ccddebug.hpp"
  23. #include "ccddali.hpp"
  24. #include "ccdquery.hpp"
  25. #include "ccdqueue.ipp"
  26. #include "ccdsnmp.hpp"
  27. #include "ccdstate.hpp"
  28. using roxiemem::IRowManager;
  29. //=======================================================================================================================
  30. #define DEBUGEE_TIMEOUT 10000
  31. class CSlaveDebugContext : public CBaseDebugContext
  32. {
  33. /*
  34. Some thoughts on slave debugging
  35. 1. Something like a ping can be used to get data from slave when needed
  36. 2. Should disable IBYTI processing (always use primary) - DONE
  37. and server-side caching - DONE
  38. 3. Roxie server can know what slave transactions are pending by intercepting the sends - no need for slave to call back just to indicate start of slave subgraph
  39. 4. There is a problem when a slave hits a breakpoint in that the breakpoint cound have been deleted by the time it gets a chance to tell the Roxie server - can't
  40. happen in local case because of the critical block at the head of checkBreakpoint but the local copy of BPs out on slave CAN get out of date. Should we care?
  41. Should there be a "Sorry, your breakpoints are out of date, here's the new set" response?
  42. Actually what we do is recheck the BP on the server, and ensure that breakpoint indexes are persistant. DONE
  43. 5. We need to serialize over our graph info if changed since last time.
  44. 6. I think we need to change implementation of debugGraph to support children. Then we have a place to put a proxy for a remote one.
  45. - id's should probably be structured so we can use a hash table at each level
  46. */
  47. const RoxiePacketHeader &header;
  48. memsize_t parentActivity;
  49. unsigned channel;
  50. int debugSequence;
  51. CriticalSection crit;
  52. const IRoxieContextLogger &logctx; // hides base class definition with more derived class pointer
  53. public:
  54. CSlaveDebugContext(IRoxieSlaveContext *_ctx, const IRoxieContextLogger &_logctx, RoxiePacketHeader &_header)
  55. : CBaseDebugContext(_logctx), header(_header), logctx(_logctx)
  56. {
  57. channel = header.channel;
  58. debugSequence = 0;
  59. parentActivity = 0;
  60. }
  61. void init(const IRoxieQueryPacket *_packet)
  62. {
  63. unsigned traceLength = _packet->getTraceLength();
  64. assertex(traceLength);
  65. const byte *traceInfo = _packet->queryTraceInfo();
  66. assertex((*traceInfo & LOGGING_DEBUGGERACTIVE) != 0);
  67. unsigned debugLen = *(unsigned short *) (traceInfo + 1);
  68. MemoryBuffer b;
  69. b.setBuffer(debugLen, (char *) (traceInfo + 1 + sizeof(unsigned short)), false);
  70. deserialize(b);
  71. __uint64 tmp; // can't serialize memsize_t
  72. b.read(tmp); // note - this is written by the RemoteAdaptor not by the serialize....
  73. parentActivity = (memsize_t)tmp;
  74. }
  75. virtual unsigned queryChannel() const
  76. {
  77. return channel;
  78. }
  79. virtual BreakpointActionMode checkBreakpoint(DebugState state, IActivityDebugContext *probe, const void *extra)
  80. {
  81. return CBaseDebugContext::checkBreakpoint(state, probe, extra);
  82. }
  83. virtual void waitForDebugger(DebugState state, IActivityDebugContext *probe)
  84. {
  85. StringBuffer debugIdString;
  86. CriticalBlock b(crit); // Make sure send sequentially - don't know if this is strictly needed...
  87. debugSequence++;
  88. debugIdString.appendf(".debug.%x", debugSequence);
  89. IPendingCallback *callback = ROQ->notePendingCallback(header, debugIdString.str()); // note that we register before the send to avoid a race.
  90. try
  91. {
  92. RoxiePacketHeader newHeader(header, ROXIE_DEBUGCALLBACK);
  93. loop // retry indefinately, as more than likely Roxie server is waiting for user input ...
  94. {
  95. Owned<IMessagePacker> output = ROQ->createOutputStream(newHeader, true, logctx);
  96. // These are deserialized in onDebugCallback..
  97. MemoryBuffer debugInfo;
  98. debugInfo.append(debugSequence);
  99. debugInfo.append((char) state);
  100. if (state==DebugStateGraphFinished)
  101. {
  102. debugInfo.append(globalCounts.count());
  103. HashIterator edges(globalCounts);
  104. ForEach(edges)
  105. {
  106. IGlobalEdgeRecord *edge = globalCounts.mapToValue(&edges.query());
  107. debugInfo.append((const char *) edges.query().getKey());
  108. debugInfo.append(edge->queryCount());
  109. }
  110. }
  111. debugInfo.append(currentBreakpointUID);
  112. debugInfo.append((__uint64)parentActivity); // can't serialize memsize_t
  113. debugInfo.append(channel);
  114. assertex (currentGraph); // otherwise what am I remote debugging?
  115. currentGraph->serializeProxyGraphs(debugInfo);
  116. debugInfo.append(probe ? probe->queryEdgeId() : "");
  117. char *buf = (char *) output->getBuffer(debugInfo.length(), true);
  118. memcpy(buf, debugInfo.toByteArray(), debugInfo.length());
  119. output->putBuffer(buf, debugInfo.length(), true);
  120. output->flush(true);
  121. output.clear();
  122. if (callback->wait(5000))
  123. break;
  124. }
  125. if (traceLevel > 6)
  126. { StringBuffer s; DBGLOG("Processing information from Roxie server in response to %s", newHeader.toString(s).str()); }
  127. MemoryBuffer &serverData = callback->queryData();
  128. deserialize(serverData);
  129. }
  130. catch (...)
  131. {
  132. ROQ->removePendingCallback(callback);
  133. throw;
  134. }
  135. ROQ->removePendingCallback(callback);
  136. }
  137. virtual IRoxieQueryPacket *onDebugCallback(const RoxiePacketHeader &header, size32_t len, char *data)
  138. {
  139. // MORE - Implies a server -> slave child -> slave grandchild type situation - need to pass call on to Roxie server (rather as I do for file callback)
  140. UNIMPLEMENTED;
  141. }
  142. virtual bool onDebuggerTimeout()
  143. {
  144. throwUnexpected();
  145. }
  146. virtual void debugCounts(IXmlWriter *output, unsigned sinceSequence, bool reset)
  147. {
  148. // This gives info for the global view - accumulated counts for all instances, plus the graph as fetched from the workunit
  149. HashIterator edges(globalCounts);
  150. ForEach(edges)
  151. {
  152. IGlobalEdgeRecord *edge = globalCounts.mapToValue(&edges.query());
  153. if (edge->queryLastSequence() && (!sinceSequence || edge->queryLastSequence() > sinceSequence))
  154. {
  155. output->outputBeginNested("edge", true);
  156. output->outputCString((const char *) edges.query().getKey(), "@edgeId");
  157. output->outputUInt(edge->queryCount(), "@count");
  158. output->outputEndNested("edge");
  159. }
  160. if (reset)
  161. edge->reset();
  162. }
  163. }
  164. };
  165. //=======================================================================================================================
  166. class CRoxieWorkflowMachine : public WorkflowMachine
  167. {
  168. public:
  169. CRoxieWorkflowMachine(IPropertyTree *_workflowInfo, bool _doOnce, const IRoxieContextLogger &_logctx) : WorkflowMachine(_logctx)
  170. {
  171. workflowInfo = _workflowInfo;
  172. doOnce = _doOnce;
  173. }
  174. protected:
  175. virtual void begin()
  176. {
  177. // MORE - should pre-do more of this work
  178. unsigned count = 0;
  179. Owned<IConstWorkflowItemIterator> iter = createWorkflowItemIterator(workflowInfo);
  180. for(iter->first(); iter->isValid(); iter->next())
  181. count++;
  182. workflow.setown(createWorkflowItemArray(count));
  183. for(iter->first(); iter->isValid(); iter->next())
  184. {
  185. IConstWorkflowItem *item = iter->query();
  186. bool isOnce = (item->queryMode() == WFModeOnce);
  187. workflow->addClone(item);
  188. if (isOnce != doOnce)
  189. workflow->queryWfid(item->queryWfid()).setState(WFStateDone);
  190. }
  191. }
  192. virtual void end()
  193. {
  194. workflow.clear();
  195. }
  196. virtual void schedulingStart() { throw MakeStringException(ROXIE_UNIMPLEMENTED_ERROR, "Scheduling not supported in roxie"); }
  197. virtual bool schedulingPull() { throw MakeStringException(ROXIE_UNIMPLEMENTED_ERROR, "Scheduling not supported in roxie"); }
  198. virtual bool schedulingPullStop() { throw MakeStringException(ROXIE_UNIMPLEMENTED_ERROR, "Scheduling not supported in roxie"); }
  199. virtual void reportContingencyFailure(char const * type, IException * e) {}
  200. virtual void checkForAbort(unsigned wfid, IException * handling) {}
  201. virtual void doExecutePersistItem(IRuntimeWorkflowItem & item) { throw MakeStringException(ROXIE_UNIMPLEMENTED_ERROR, "Persists not supported in roxie"); }
  202. private:
  203. IPropertyTree *workflowInfo;
  204. bool doOnce;
  205. };
  206. WorkflowMachine *createRoxieWorkflowMachine(IPropertyTree *_workflowInfo, bool _doOnce, const IRoxieContextLogger &_logctx)
  207. {
  208. return new CRoxieWorkflowMachine(_workflowInfo, _doOnce, _logctx);
  209. }
  210. //=======================================================================================================================
  211. typedef byte *row_t;
  212. typedef row_t * rowset_t;
  213. class DeserializedDataReader : public CInterface, implements IWorkUnitRowReader
  214. {
  215. const rowset_t data;
  216. size32_t count;
  217. unsigned idx;
  218. public:
  219. IMPLEMENT_IINTERFACE;
  220. DeserializedDataReader(size32_t _count, rowset_t _data)
  221. : data(_data), count(_count)
  222. {
  223. idx = 0;
  224. }
  225. virtual const void * nextInGroup()
  226. {
  227. if (idx < count)
  228. {
  229. const void *row = data[idx];
  230. if (row)
  231. LinkRoxieRow(row);
  232. idx++;
  233. return row;
  234. }
  235. return NULL;
  236. }
  237. virtual void getResultRowset(size32_t & tcount, byte * * & tgt)
  238. {
  239. tcount = count;
  240. if (data)
  241. rtlLinkRowset(data);
  242. tgt = data;
  243. }
  244. };
  245. class CDeserializedResultStore : public CInterface, implements IDeserializedResultStore
  246. {
  247. PointerArrayOf<row_t> stored;
  248. UnsignedArray counts;
  249. PointerIArrayOf<IOutputMetaData> metas;
  250. mutable SpinLock lock;
  251. public:
  252. IMPLEMENT_IINTERFACE;
  253. ~CDeserializedResultStore()
  254. {
  255. ForEachItemIn(idx, stored)
  256. {
  257. rowset_t rows = stored.item(idx);
  258. if (rows)
  259. {
  260. rtlReleaseRowset(counts.item(idx), (byte**) rows);
  261. }
  262. }
  263. }
  264. virtual int addResult(size32_t count, rowset_t data, IOutputMetaData *meta)
  265. {
  266. SpinBlock b(lock);
  267. stored.append(data);
  268. counts.append(count);
  269. metas.append(meta);
  270. return stored.ordinality()-1;
  271. }
  272. virtual void queryResult(int id, size32_t &count, rowset_t &data) const
  273. {
  274. count = counts.item(id);
  275. data = stored.item(id);
  276. }
  277. virtual IWorkUnitRowReader *createDeserializedReader(int id) const
  278. {
  279. return new DeserializedDataReader(counts.item(id), stored.item(id));
  280. }
  281. virtual void serialize(unsigned & tlen, void * & tgt, int id, ICodeContext *codectx) const
  282. {
  283. IOutputMetaData *meta = metas.item(id);
  284. rowset_t data = stored.item(id);
  285. size32_t count = counts.item(id);
  286. MemoryBuffer result;
  287. Owned<IOutputRowSerializer> rowSerializer = meta->createDiskSerializer(codectx, 0); // NOTE - we don't have a meaningful activity id. Only used for error reporting.
  288. bool grouped = meta->isGrouped();
  289. for (size32_t idx = 0; idx<count; idx++)
  290. {
  291. const byte *row = data[idx];
  292. if (grouped && idx)
  293. result.append(row == NULL);
  294. if (row)
  295. {
  296. CThorDemoRowSerializer serializerTarget(result);
  297. rowSerializer->serialize(serializerTarget, row);
  298. }
  299. }
  300. tlen = result.length();
  301. tgt= result.detach();
  302. }
  303. };
  304. extern IDeserializedResultStore *createDeserializedResultStore()
  305. {
  306. return new CDeserializedResultStore;
  307. }
  308. class WorkUnitRowReaderBase : public CInterface, implements IWorkUnitRowReader
  309. {
  310. protected:
  311. Linked<IEngineRowAllocator> rowAllocator;
  312. bool isGrouped;
  313. public:
  314. IMPLEMENT_IINTERFACE;
  315. WorkUnitRowReaderBase(IEngineRowAllocator *_rowAllocator, bool _isGrouped)
  316. : rowAllocator(_rowAllocator), isGrouped(_isGrouped)
  317. {
  318. }
  319. virtual void getResultRowset(size32_t & tcount, byte * * & tgt)
  320. {
  321. bool atEOG = true;
  322. RtlLinkedDatasetBuilder builder(rowAllocator);
  323. loop
  324. {
  325. const void *ret = nextInGroup();
  326. if (!ret)
  327. {
  328. if (atEOG || !isGrouped)
  329. break;
  330. atEOG = true;
  331. }
  332. else
  333. atEOG = false;
  334. builder.appendOwn(ret);
  335. }
  336. tcount = builder.getcount();
  337. tgt = builder.linkrows();
  338. }
  339. };
  340. class RawDataReader : public WorkUnitRowReaderBase
  341. {
  342. protected:
  343. const IRoxieContextLogger &logctx;
  344. byte *bufferBase;
  345. MemoryBuffer blockBuffer;
  346. Owned<ISerialStream> bufferStream;
  347. CThorStreamDeserializerSource rowSource;
  348. bool eof;
  349. bool eogPending;
  350. Owned<IOutputRowDeserializer> rowDeserializer;
  351. virtual bool nextBlock(unsigned & tlen, void * & tgt, void * & base) = 0;
  352. bool reload()
  353. {
  354. free(bufferBase);
  355. size32_t lenData;
  356. bufferBase = NULL;
  357. void *tempData, *base;
  358. eof = !nextBlock(lenData, tempData, base);
  359. bufferBase = (byte *) base;
  360. blockBuffer.setBuffer(lenData, tempData, false);
  361. return !eof;
  362. }
  363. public:
  364. RawDataReader(ICodeContext *codeContext, IEngineRowAllocator *_rowAllocator, bool _isGrouped, const IRoxieContextLogger &_logctx)
  365. : WorkUnitRowReaderBase(_rowAllocator, _isGrouped), logctx(_logctx)
  366. {
  367. eof = false;
  368. eogPending = false;
  369. bufferBase = NULL;
  370. rowDeserializer.setown(rowAllocator->createDiskDeserializer(codeContext));
  371. bufferStream.setown(createMemoryBufferSerialStream(blockBuffer));
  372. rowSource.setStream(bufferStream);
  373. }
  374. ~RawDataReader()
  375. {
  376. if (bufferBase)
  377. free(bufferBase);
  378. }
  379. virtual const void *nextInGroup()
  380. {
  381. if (eof)
  382. return NULL;
  383. if (rowSource.eos() && !reload())
  384. return NULL;
  385. if (eogPending)
  386. {
  387. eogPending = false;
  388. return NULL;
  389. }
  390. #if 0
  391. // MORE - think a bit about what happens on incomplete rows - I think deserializer will throw an exception?
  392. unsigned thisSize = meta.getRecordSize(data+cursor);
  393. if (thisSize > lenData-cursor)
  394. {
  395. CTXLOG("invalid stored dataset - incomplete row at end");
  396. throw MakeStringException(ROXIE_DATA_ERROR, "invalid stored dataset - incomplete row at end");
  397. }
  398. #endif
  399. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  400. size32_t size = rowDeserializer->deserialize(rowBuilder, rowSource);
  401. if (isGrouped)
  402. rowSource.read(sizeof(bool), &eogPending);
  403. atomic_inc(&rowsIn);
  404. return rowBuilder.finalizeRowClear(size);
  405. }
  406. };
  407. class InlineRawDataReader : public RawDataReader
  408. {
  409. Linked<IPropertyTree> xml;
  410. public:
  411. InlineRawDataReader(ICodeContext *codeContext, IEngineRowAllocator *_rowAllocator, bool _isGrouped, const IRoxieContextLogger &_logctx, IPropertyTree *_xml)
  412. : RawDataReader(codeContext, _rowAllocator, _isGrouped, _logctx), xml(_xml)
  413. {
  414. }
  415. virtual bool nextBlock(unsigned & tlen, void * & tgt, void * & base)
  416. {
  417. base = tgt = NULL;
  418. if (xml)
  419. {
  420. MemoryBuffer result;
  421. xml->getPropBin(NULL, result);
  422. tlen = result.length();
  423. base = tgt = result.detach();
  424. xml.clear();
  425. return tlen != 0;
  426. }
  427. else
  428. {
  429. tlen = 0;
  430. return false;
  431. }
  432. }
  433. };
  434. class StreamedRawDataReader : public RawDataReader
  435. {
  436. SafeSocket &client;
  437. StringAttr id;
  438. offset_t offset;
  439. public:
  440. StreamedRawDataReader(ICodeContext *codeContext, IEngineRowAllocator *_rowAllocator, bool _isGrouped, const IRoxieContextLogger &_logctx, SafeSocket &_client, const char *_id)
  441. : RawDataReader(codeContext, _rowAllocator, _isGrouped, _logctx), client(_client), id(_id)
  442. {
  443. offset = 0;
  444. }
  445. virtual bool nextBlock(unsigned & tlen, void * & tgt, void * & base)
  446. {
  447. try
  448. {
  449. #ifdef FAKE_EXCEPTIONS
  450. if (offset > 0x10000)
  451. throw MakeStringException(ROXIE_INTERNAL_ERROR, "TEST EXCEPTION");
  452. #endif
  453. // Go request from the socket
  454. MemoryBuffer request;
  455. request.reserve(sizeof(size32_t));
  456. request.append('D');
  457. offset_t loffset = offset;
  458. _WINREV(loffset);
  459. request.append(sizeof(loffset), &loffset);
  460. request.append(strlen(id)+1, id);
  461. size32_t len = request.length() - sizeof(size32_t);
  462. len |= 0x80000000;
  463. _WINREV(len);
  464. *(size32_t *) request.toByteArray() = len;
  465. client.write(request.toByteArray(), request.length());
  466. // Note: I am the only thread reading (we only support a single input dataset in roxiepipe mode)
  467. MemoryBuffer reply;
  468. client.readBlock(reply, readTimeout);
  469. tlen = reply.length();
  470. // MORE - not very robust!
  471. // skip past block header
  472. if (tlen > 0)
  473. {
  474. tgt = base = reply.detach();
  475. tgt = ((char *)base) + 9;
  476. tgt = strchr((char *)tgt, '\0') + 1;
  477. tlen -= ((char *)tgt - (char *)base);
  478. offset += tlen;
  479. }
  480. else
  481. tgt = base = NULL;
  482. return tlen != 0;
  483. }
  484. catch (IException *E)
  485. {
  486. StringBuffer text;
  487. E->errorMessage(text);
  488. int errCode = E->errorCode();
  489. E->Release();
  490. IException *ee = MakeStringException(MSGAUD_internal, errCode, "%s", text.str());
  491. logctx.logOperatorException(ee, __FILE__, __LINE__, "Exception caught in RawDataReader::nextBlock");
  492. throw ee;
  493. }
  494. catch (...)
  495. {
  496. logctx.logOperatorException(NULL, __FILE__, __LINE__, "Unknown exception caught in RawDataReader::nextBlock");
  497. throw;
  498. }
  499. }
  500. };
  501. class InlineXmlDataReader : public WorkUnitRowReaderBase
  502. {
  503. Linked<IPropertyTree> xml;
  504. Owned <XmlColumnProvider> columns;
  505. Owned<IPropertyTreeIterator> rows;
  506. IXmlToRowTransformer &rowTransformer;
  507. public:
  508. IMPLEMENT_IINTERFACE;
  509. InlineXmlDataReader(IXmlToRowTransformer &_rowTransformer, IPropertyTree *_xml, IEngineRowAllocator *_rowAllocator, bool _isGrouped)
  510. : WorkUnitRowReaderBase(_rowAllocator, _isGrouped), xml(_xml), rowTransformer(_rowTransformer)
  511. {
  512. columns.setown(new XmlDatasetColumnProvider);
  513. rows.setown(xml->getElements("Row")); // NOTE - the 'hack for Gordon' as found in thorxmlread is not implemented here. Does it need to be ?
  514. rows->first();
  515. }
  516. virtual const void *nextInGroup()
  517. {
  518. if (rows->isValid())
  519. {
  520. columns->setRow(&rows->query());
  521. rows->next();
  522. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  523. NullDiskCallback callback;
  524. size_t outSize = rowTransformer.transform(rowBuilder, columns, &callback);
  525. return rowBuilder.finalizeRowClear(outSize);
  526. }
  527. return NULL;
  528. }
  529. };
  530. //---------------------------------------------------------------------------------------
  531. class CSlaveContext : public CInterface, implements IRoxieSlaveContext, implements ICodeContext, implements roxiemem::ITimeLimiter, implements IRowAllocatorMetaActIdCacheCallback
  532. {
  533. protected:
  534. mutable Owned<IRowAllocatorMetaActIdCache> allocatorMetaCache;
  535. Owned<IRowManager> rowManager; // NOTE: the order of destruction here is significant. For leak check to work destroy this BEFORE allAllocators, but after most other things
  536. Owned <IDebuggableContext> debugContext;
  537. const IQueryFactory *factory;
  538. Owned<IProbeManager> probeManager; // must be destroyed after childGraphs
  539. MapXToMyClass<unsigned, unsigned, IActivityGraph> childGraphs;
  540. Owned<IActivityGraph> graph;
  541. unsigned priority;
  542. StringBuffer authToken;
  543. Owned<IPropertyTree> probeQuery;
  544. RoxiePacketHeader *header;
  545. unsigned lastWuAbortCheck;
  546. unsigned startTime;
  547. unsigned timeLimit;
  548. unsigned totSlavesReplyLen;
  549. unsigned ctxParallelJoinPreload;
  550. unsigned ctxFullKeyedJoinPreload;
  551. unsigned ctxKeyedJoinPreload;
  552. unsigned ctxConcatPreload;
  553. unsigned ctxFetchPreload;
  554. unsigned ctxPrefetchProjectPreload;
  555. bool traceActivityTimes;
  556. Owned<IConstWorkUnit> workUnit;
  557. Owned<IRoxieDaliHelper> daliHelperLink;
  558. CriticalSection statsCrit;
  559. const IRoxieContextLogger &logctx;
  560. protected:
  561. CriticalSection resultsCrit;
  562. PointerIArrayOf<FlushingStringBuffer> resultMap;
  563. bool exceptionLogged;
  564. bool aborted;
  565. CriticalSection abortLock; // NOTE: we don't bother to get lock when just reading to see whether to abort
  566. Owned<IException> exception;
  567. static void _toXML(IPropertyTree *tree, StringBuffer &xgmml, unsigned indent)
  568. {
  569. if (tree->getPropInt("att[@name='_roxieStarted']/@value", 1) == 0)
  570. return;
  571. if (0 && tree->getPropInt("att[@name='_kind']/@value", 0) == 496)
  572. {
  573. Owned<IPropertyTreeIterator> sub = tree->getElements(".//att[@name='_roxieStarted']");
  574. bool foundOne = false;
  575. ForEach(*sub)
  576. {
  577. if (sub->query().getPropInt("@value", 1)==0)
  578. {
  579. foundOne = true;
  580. break;
  581. }
  582. }
  583. if (!foundOne)
  584. return;
  585. }
  586. const char *name = tree->queryName();
  587. xgmml.pad(indent).append('<').append(name);
  588. Owned<IAttributeIterator> it = tree->getAttributes(true);
  589. if (it->first())
  590. {
  591. do
  592. {
  593. const char *key = it->queryName();
  594. xgmml.append(' ').append(key+1).append("=\"");
  595. encodeXML(it->queryValue(), xgmml, ENCODE_NEWLINES);
  596. xgmml.append("\"");
  597. }
  598. while (it->next());
  599. }
  600. Owned<IPropertyTreeIterator> sub = tree->getElements("*", iptiter_sort);
  601. if (!sub->first())
  602. {
  603. xgmml.append("/>\n");
  604. }
  605. else
  606. {
  607. xgmml.append(">\n");
  608. for(; sub->isValid(); sub->next())
  609. _toXML(&sub->query(), xgmml, indent+1);
  610. xgmml.pad(indent).append("</").append(name).append(">\n");
  611. }
  612. }
  613. public:
  614. IMPLEMENT_IINTERFACE;
  615. CSlaveContext(const IQueryFactory *_factory, const IRoxieContextLogger &_logctx, unsigned _timeLimit, memsize_t _memoryLimit, IRoxieQueryPacket *_packet, bool _traceActivityTimes, bool _debuggerActive)
  616. : factory(_factory), logctx(_logctx)
  617. {
  618. if (_packet)
  619. header = &_packet->queryHeader();
  620. else
  621. header = NULL;
  622. timeLimit = _timeLimit;
  623. startTime = lastWuAbortCheck = msTick();
  624. ctxParallelJoinPreload = 0;
  625. ctxFullKeyedJoinPreload = 0;
  626. ctxKeyedJoinPreload = 0;
  627. ctxConcatPreload = 0;
  628. ctxFetchPreload = 0;
  629. ctxPrefetchProjectPreload = 0;
  630. traceActivityTimes = _traceActivityTimes;
  631. temporaries = NULL;
  632. deserializedResultStore = NULL;
  633. rereadResults = NULL;
  634. xmlStoredDatasetReadFlags = ptr_none;
  635. if (_debuggerActive)
  636. {
  637. CSlaveDebugContext *slaveDebugContext = new CSlaveDebugContext(this, logctx, *header);
  638. slaveDebugContext->init(_packet);
  639. debugContext.setown(slaveDebugContext);
  640. probeManager.setown(createDebugManager(debugContext, "slaveDebugger"));
  641. }
  642. aborted = false;
  643. allocatorMetaCache.setown(createRowAllocatorCache(this));
  644. rowManager.setown(roxiemem::createRowManager(_memoryLimit, this, logctx, allocatorMetaCache, false));
  645. //MORE: If checking heap required then should have
  646. //rowManager.setown(createCheckingHeap(rowManager)) or something similar.
  647. }
  648. ~CSlaveContext()
  649. {
  650. ::Release(rereadResults);
  651. ::Release(temporaries);
  652. ::Release(deserializedResultStore);
  653. }
  654. // interface IRoxieServerContext
  655. virtual void noteStatistic(unsigned statCode, unsigned __int64 value, unsigned count) const
  656. {
  657. logctx.noteStatistic(statCode, value, count);
  658. }
  659. virtual void CTXLOG(const char *format, ...) const
  660. {
  661. va_list args;
  662. va_start(args, format);
  663. logctx.CTXLOGva(format, args);
  664. va_end(args);
  665. }
  666. virtual void CTXLOGva(const char *format, va_list args) const
  667. {
  668. logctx.CTXLOGva(format, args);
  669. }
  670. virtual void CTXLOGa(TracingCategory category, const char *prefix, const char *text) const
  671. {
  672. logctx.CTXLOGa(category, prefix, text);
  673. }
  674. virtual void logOperatorException(IException *E, const char *file, unsigned line, const char *format, ...) const
  675. {
  676. va_list args;
  677. va_start(args, format);
  678. logctx.logOperatorExceptionVA(E, file, line, format, args);
  679. va_end(args);
  680. }
  681. virtual void logOperatorExceptionVA(IException *E, const char *file, unsigned line, const char *format, va_list args) const
  682. {
  683. logctx.logOperatorExceptionVA(E, file, line, format, args);
  684. }
  685. virtual void CTXLOGae(IException *E, const char *file, unsigned line, const char *prefix, const char *format, ...) const
  686. {
  687. va_list args;
  688. va_start(args, format);
  689. logctx.CTXLOGaeva(E, file, line, prefix, format, args);
  690. va_end(args);
  691. }
  692. virtual void CTXLOGaeva(IException *E, const char *file, unsigned line, const char *prefix, const char *format, va_list args) const
  693. {
  694. logctx.CTXLOGaeva(E, file, line, prefix, format, args);
  695. }
  696. virtual void CTXLOGl(LogItem *log) const
  697. {
  698. if (log->isStatistics())
  699. {
  700. logctx.noteStatistic(log->getStatCode(), log->getStatValue(), (unsigned)log->getStatCount());
  701. log->Release();
  702. }
  703. else
  704. logctx.CTXLOGl(log);
  705. }
  706. virtual unsigned logString(const char *text) const
  707. {
  708. if (text && *text)
  709. {
  710. CTXLOG("USER: %s", text);
  711. return strlen(text);
  712. }
  713. else
  714. return 0;
  715. }
  716. virtual const IContextLogger &queryContextLogger() const
  717. {
  718. return logctx;
  719. }
  720. virtual StringBuffer &getLogPrefix(StringBuffer &ret) const
  721. {
  722. return logctx.getLogPrefix(ret);
  723. }
  724. virtual bool isIntercepted() const
  725. {
  726. return logctx.isIntercepted();
  727. }
  728. virtual bool isBlind() const
  729. {
  730. return logctx.isBlind();
  731. }
  732. virtual unsigned queryTraceLevel() const
  733. {
  734. return logctx.queryTraceLevel();
  735. }
  736. virtual void noteProcessed(const IRoxieContextLogger &activityContext, const IRoxieServerActivity *activity, unsigned _idx, unsigned _processed, unsigned __int64 _totalCycles, unsigned __int64 _localCycles) const
  737. {
  738. if (traceActivityTimes)
  739. {
  740. StringBuffer text, prefix;
  741. text.appendf("%s outputIdx %d processed %d total %d us local %d us",
  742. getActivityText(activity->getKind()), _idx, _processed, (unsigned) (cycle_to_nanosec(_totalCycles)/1000), (unsigned)(cycle_to_nanosec(_localCycles)/1000));
  743. activityContext.getLogPrefix(prefix);
  744. CTXLOGa(LOG_TIMING, prefix.str(), text.str());
  745. }
  746. if (graphProgress)
  747. {
  748. IPropertyTree& nodeProgress = graphProgress->updateNode(activity->querySubgraphId(), activity->queryId());
  749. nodeProgress.setPropInt64("@totalTime", (unsigned) (cycle_to_nanosec(_totalCycles)/1000));
  750. nodeProgress.setPropInt64("@localTime", (unsigned) (cycle_to_nanosec(_localCycles)/1000));
  751. if (_processed)
  752. {
  753. StringBuffer edgeId;
  754. edgeId.append(activity->queryId()).append('_').append(_idx);
  755. IPropertyTree& edgeProgress = graphProgress->updateEdge(activity->querySubgraphId(), edgeId.str());
  756. edgeProgress.setPropInt64("@count", _processed);
  757. }
  758. }
  759. }
  760. virtual bool queryTimeActivities() const
  761. {
  762. return traceActivityTimes;
  763. }
  764. virtual void checkAbort()
  765. {
  766. // MORE - really should try to apply limits at slave end too
  767. #ifdef __linux__
  768. if (linuxYield)
  769. sched_yield();
  770. #endif
  771. #ifdef _DEBUG
  772. if (shuttingDown)
  773. throw MakeStringException(ROXIE_FORCE_SHUTDOWN, "Roxie is shutting down");
  774. #endif
  775. if (aborted) // NOTE - don't bother getting lock before reading this (for speed) - a false read is very unlikely and not a problem
  776. {
  777. CriticalBlock b(abortLock);
  778. if (!exception)
  779. exception.setown(MakeStringException(ROXIE_INTERNAL_ERROR, "Query was aborted"));
  780. throw exception.getLink();
  781. }
  782. if (graph)
  783. graph->checkAbort();
  784. if (timeLimit && (msTick() - startTime > timeLimit))
  785. {
  786. unsigned oldLimit = timeLimit;
  787. //timeLimit = 0; // to prevent exceptions in cleanup - this means only one arm gets stopped!
  788. CriticalBlock b(abortLock);
  789. IException *E = MakeStringException(ROXIE_TIMEOUT, "Query %s exceeded time limit (%d ms) - terminated", factory->queryQueryName(), oldLimit);
  790. if (!exceptionLogged)
  791. {
  792. logOperatorException(E, NULL, 0, NULL);
  793. exceptionLogged = true;
  794. }
  795. throw E;
  796. }
  797. if (workUnit && (msTick() - lastWuAbortCheck > 5000))
  798. {
  799. CriticalBlock b(abortLock);
  800. if (workUnit->aborting())
  801. {
  802. if (!exception)
  803. exception.setown(MakeStringException(ROXIE_INTERNAL_ERROR, "Query was aborted"));
  804. throw exception.getLink();
  805. }
  806. lastWuAbortCheck = msTick();
  807. }
  808. }
  809. virtual void notifyAbort(IException *E)
  810. {
  811. CriticalBlock b(abortLock);
  812. if (!aborted && QUERYINTERFACE(E, InterruptedSemaphoreException) == NULL)
  813. {
  814. aborted = true;
  815. exception.set(E);
  816. setWUState(WUStateAborting);
  817. }
  818. }
  819. virtual void setWUState(WUState state)
  820. {
  821. if (workUnit)
  822. {
  823. WorkunitUpdate w(&workUnit->lock());
  824. w->setState(state);
  825. }
  826. }
  827. virtual bool checkWuAborted()
  828. {
  829. return workUnit && workUnit->aborting();
  830. }
  831. virtual unsigned parallelJoinPreload()
  832. {
  833. return ctxParallelJoinPreload;
  834. }
  835. virtual unsigned concatPreload()
  836. {
  837. return ctxConcatPreload;
  838. }
  839. virtual unsigned fetchPreload()
  840. {
  841. return ctxFetchPreload;
  842. }
  843. virtual unsigned fullKeyedJoinPreload()
  844. {
  845. return ctxFullKeyedJoinPreload;
  846. }
  847. virtual unsigned keyedJoinPreload()
  848. {
  849. return ctxKeyedJoinPreload;
  850. }
  851. virtual unsigned prefetchProjectPreload()
  852. {
  853. return ctxPrefetchProjectPreload;
  854. }
  855. const char *queryAuthToken()
  856. {
  857. return authToken.str();
  858. }
  859. virtual void noteChildGraph(unsigned id, IActivityGraph *childGraph)
  860. {
  861. if (queryTraceLevel() > 10)
  862. CTXLOG("CSlaveContext %p noteChildGraph %d=%p", this, id, childGraph);
  863. childGraphs.setValue(id, childGraph);
  864. }
  865. virtual IActivityGraph *getLibraryGraph(const LibraryCallFactoryExtra &extra, IRoxieServerActivity *parentActivity)
  866. {
  867. if (extra.embedded)
  868. {
  869. return factory->lookupGraph(extra.libraryName, probeManager, *this, parentActivity);
  870. }
  871. else
  872. {
  873. Owned<IQueryFactory> libraryQuery = factory->lookupLibrary(extra.libraryName, extra.interfaceHash, *this);
  874. assertex(libraryQuery);
  875. return libraryQuery->lookupGraph("graph1", probeManager, *this, parentActivity);
  876. }
  877. }
  878. void beginGraph(const char *graphName)
  879. {
  880. if (debugContext)
  881. {
  882. probeManager.clear(); // Hack!
  883. probeManager.setown(createDebugManager(debugContext, graphName));
  884. debugContext->checkBreakpoint(DebugStateGraphCreate, NULL, graphName);
  885. }
  886. else if (probeAllRows || probeQuery != NULL)
  887. probeManager.setown(createProbeManager());
  888. graph.setown(factory->lookupGraph(graphName, probeManager, *this, NULL));
  889. graph->onCreate(this, NULL); // MORE - is that right
  890. if (debugContext)
  891. debugContext->checkBreakpoint(DebugStateGraphStart, NULL, graphName);
  892. }
  893. Owned<IWUGraphProgress> graphProgress; // could make local to endGraph and pass to reset - might be cleaner
  894. void endGraph(bool aborting)
  895. {
  896. if (graph)
  897. {
  898. if (debugContext)
  899. debugContext->checkBreakpoint(aborting ? DebugStateGraphAbort : DebugStateGraphEnd, NULL, graph->queryName());
  900. if (aborting)
  901. graph->abort();
  902. WorkunitUpdate progressWorkUnit(NULL);
  903. Owned<IConstWUGraphProgress> progress;
  904. if (workUnit)
  905. {
  906. progressWorkUnit.setown(&workUnit->lock());
  907. progress.setown(progressWorkUnit->getGraphProgress(graph->queryName()));
  908. graphProgress.setown(progress->update());
  909. }
  910. graph->reset();
  911. if (graphProgress)
  912. {
  913. graphProgress.clear();
  914. progress.clear();
  915. }
  916. graph.clear();
  917. childGraphs.kill();
  918. }
  919. }
  920. void runGraph()
  921. {
  922. try
  923. {
  924. graph->execute();
  925. if (probeQuery)
  926. graph->getProbeResponse(probeQuery);
  927. }
  928. catch(...)
  929. {
  930. if (probeQuery)
  931. graph->getProbeResponse(probeQuery);
  932. throw;
  933. }
  934. }
  935. virtual void executeGraph(const char * name, bool realThor, size32_t parentExtractSize, const void * parentExtract)
  936. {
  937. assertex(parentExtractSize == 0);
  938. if (queryTraceLevel() > 8)
  939. CTXLOG("Executing graph %s", name);
  940. assertex(!realThor);
  941. bool created = false;
  942. try
  943. {
  944. beginGraph(name);
  945. created = true;
  946. runGraph();
  947. }
  948. catch (IException *e)
  949. {
  950. if (e->errorAudience() == MSGAUD_operator)
  951. EXCLOG(e, "Exception thrown in query - cleaning up"); // if an IException is throw let EXCLOG determine if a trap should be generated
  952. else
  953. {
  954. StringBuffer s;
  955. CTXLOG("Exception thrown in query - cleaning up: %d: %s", e->errorCode(), e->errorMessage(s).str());
  956. }
  957. if (created)
  958. endGraph(true);
  959. CTXLOG("Done cleaning up");
  960. throw;
  961. }
  962. catch (...)
  963. {
  964. CTXLOG("Exception thrown in query - cleaning up");
  965. if (created)
  966. endGraph(true);
  967. CTXLOG("Done cleaning up");
  968. throw;
  969. }
  970. endGraph(false);
  971. }
  972. virtual IActivityGraph * queryChildGraph(unsigned id)
  973. {
  974. if (queryTraceLevel() > 10)
  975. CTXLOG("CSlaveContext %p resolveChildGraph %d", this, id);
  976. IActivityGraph *childGraph = childGraphs.getValue(id);
  977. assertex(childGraph);
  978. return childGraph;
  979. }
  980. virtual IThorChildGraph * resolveChildQuery(__int64 activityId, IHThorArg * colocal)
  981. {
  982. // NOTE - part of ICodeContext interface
  983. return LINK(queryChildGraph((unsigned) activityId)->queryChildGraph());
  984. }
  985. virtual IEclGraphResults * resolveLocalQuery(__int64 id)
  986. {
  987. return queryChildGraph((unsigned) id)->queryLocalGraph();
  988. }
  989. virtual IRowManager &queryRowManager()
  990. {
  991. return *rowManager;
  992. }
  993. virtual void addSlavesReplyLen(unsigned len)
  994. {
  995. CriticalBlock b(statsCrit); // MORE: change to atomic_add, or may not need it at all?
  996. totSlavesReplyLen += len;
  997. }
  998. virtual const char *loadResource(unsigned id)
  999. {
  1000. ILoadedDllEntry *dll = factory->queryDll();
  1001. return (const char *) dll->getResource(id);
  1002. }
  1003. virtual const IResolvedFile *resolveLFN(const char *filename, bool isOpt)
  1004. {
  1005. CDateTime cacheDate; // Note - this is empty meaning we don't know...
  1006. return querySlaveDynamicFileCache()->lookupDynamicFile(*this, filename, cacheDate, 0, header, isOpt, false);
  1007. }
  1008. virtual IRoxieWriteHandler *createLFN(const char *filename, bool overwrite, bool extend, const StringArray &clusters)
  1009. {
  1010. throwUnexpected(); // only support writing on the server
  1011. }
  1012. virtual void onFileCallback(const RoxiePacketHeader &header, const char *lfn, bool isOpt, bool isLocal)
  1013. {
  1014. // On a slave, we need to request info using our own header (not the one passed in) and need to get global rather than just local info
  1015. // (possibly we could get just local if the channel matches but not sure there is any point)
  1016. Owned<const IResolvedFile> dFile = resolveLFN(lfn, isOpt);
  1017. if (dFile)
  1018. {
  1019. MemoryBuffer mb;
  1020. mb.append(sizeof(RoxiePacketHeader), &header);
  1021. mb.append(lfn);
  1022. dFile->serializePartial(mb, header.channel, isLocal);
  1023. ((RoxiePacketHeader *) mb.toByteArray())->activityId = ROXIE_FILECALLBACK;
  1024. Owned<IRoxieQueryPacket> reply = createRoxiePacket(mb);
  1025. reply->queryHeader().retries = 0;
  1026. ROQ->sendPacket(reply, *this); // MORE - the caller's log context might be better? Should we unicast? Note that this does not release the packet
  1027. return;
  1028. }
  1029. ROQ->sendAbortCallback(header, lfn, *this);
  1030. throwUnexpected();
  1031. }
  1032. virtual ICodeContext *queryCodeContext()
  1033. {
  1034. return this;
  1035. }
  1036. virtual IRoxieServerContext *queryServerContext()
  1037. {
  1038. return NULL;
  1039. }
  1040. virtual IProbeManager *queryProbeManager() const
  1041. {
  1042. return probeManager;
  1043. }
  1044. virtual IDebuggableContext *queryDebugContext() const
  1045. {
  1046. return debugContext;
  1047. }
  1048. virtual char *getOS()
  1049. {
  1050. #ifdef _WIN32
  1051. return strdup("windows");
  1052. #else
  1053. return strdup("linux");
  1054. #endif
  1055. }
  1056. virtual const void * fromXml(IEngineRowAllocator * rowAllocator, size32_t len, const char * utf8, IXmlToRowTransformer * xmlTransformer, bool stripWhitespace)
  1057. {
  1058. return createRowFromXml(rowAllocator, len, utf8, xmlTransformer, stripWhitespace);
  1059. }
  1060. virtual IEngineContext *queryEngineContext() { return NULL; }
  1061. virtual char *getDaliServers() { throwUnexpected(); }
  1062. // The following from ICodeContext should never be executed in slave activity. If we are on Roxie server (or in child query on slave), they will be implemented by more derived CRoxieServerContext class
  1063. virtual void setResultBool(const char *name, unsigned sequence, bool value) { throwUnexpected(); }
  1064. virtual void setResultData(const char *name, unsigned sequence, int len, const void * data) { throwUnexpected(); }
  1065. virtual void setResultDecimal(const char * stepname, unsigned sequence, int len, int precision, bool isSigned, const void *val) { throwUnexpected(); }
  1066. virtual void setResultInt(const char *name, unsigned sequence, __int64 value) { throwUnexpected(); }
  1067. virtual void setResultRaw(const char *name, unsigned sequence, int len, const void * data) { throwUnexpected(); }
  1068. virtual void setResultReal(const char * stepname, unsigned sequence, double value) { throwUnexpected(); }
  1069. virtual void setResultSet(const char *name, unsigned sequence, bool isAll, size32_t len, const void * data, ISetToXmlTransformer * transformer) { throwUnexpected(); }
  1070. virtual void setResultString(const char *name, unsigned sequence, int len, const char * str) { throwUnexpected(); }
  1071. virtual void setResultUInt(const char *name, unsigned sequence, unsigned __int64 value) { throwUnexpected(); }
  1072. virtual void setResultUnicode(const char *name, unsigned sequence, int len, UChar const * str) { throwUnexpected(); }
  1073. virtual void setResultVarString(const char * name, unsigned sequence, const char * value) { throwUnexpected(); }
  1074. virtual void setResultVarUnicode(const char * name, unsigned sequence, UChar const * value) { throwUnexpected(); }
  1075. virtual unsigned getResultHash(const char * name, unsigned sequence) { throwUnexpected(); }
  1076. virtual void printResults(IXmlWriter *output, const char *name, unsigned sequence) { throwUnexpected(); }
  1077. virtual char *getWuid() { throwUnexpected(); }
  1078. virtual void getExternalResultRaw(unsigned & tlen, void * & tgt, const char * wuid, const char * stepname, unsigned sequence, IXmlToRowTransformer * xmlTransformer, ICsvToRowTransformer * csvTransformer) { throwUnexpected(); }
  1079. virtual char * getExpandLogicalName(const char * logicalName) { throwUnexpected(); }
  1080. virtual void addWuException(const char * text, unsigned code, unsigned severity) { throwUnexpected(); }
  1081. virtual void addWuAssertFailure(unsigned code, const char * text, const char * filename, unsigned lineno, unsigned column, bool isAbort) { throwUnexpected(); }
  1082. virtual IUserDescriptor *queryUserDescriptor() { throwUnexpected(); }
  1083. virtual unsigned __int64 getDatasetHash(const char * name, unsigned __int64 hash) { throwUnexpected(); }
  1084. virtual unsigned getNodes() { throwUnexpected(); }
  1085. virtual unsigned getNodeNum() { throwUnexpected(); }
  1086. virtual char *getFilePart(const char *logicalPart, bool create=false) { throwUnexpected(); }
  1087. virtual unsigned __int64 getFileOffset(const char *logicalPart) { throwUnexpected(); }
  1088. virtual IDistributedFileTransaction *querySuperFileTransaction() { throwUnexpected(); }
  1089. virtual char *getJobName() { throwUnexpected(); }
  1090. virtual char *getJobOwner() { throwUnexpected(); }
  1091. virtual char *getClusterName() { throwUnexpected(); }
  1092. virtual char *getGroupName() { throwUnexpected(); }
  1093. virtual char * queryIndexMetaData(char const * lfn, char const * xpath) { throwUnexpected(); }
  1094. virtual unsigned getPriority() const { throwUnexpected(); }
  1095. virtual char *getPlatform() { throwUnexpected(); }
  1096. virtual char *getEnv(const char *name, const char *defaultValue) const { throwUnexpected(); }
  1097. virtual IEngineRowAllocator *getRowAllocator(IOutputMetaData * meta, unsigned activityId) const
  1098. {
  1099. return allocatorMetaCache->ensure(meta, activityId);
  1100. }
  1101. virtual const char *cloneVString(const char *str) const
  1102. {
  1103. return rowManager->cloneVString(str);
  1104. }
  1105. virtual const char *cloneVString(size32_t len, const char *str) const
  1106. {
  1107. return rowManager->cloneVString(len, str);
  1108. }
  1109. virtual void getRowXML(size32_t & lenResult, char * & result, IOutputMetaData & info, const void * row, unsigned flags)
  1110. {
  1111. convertRowToXML(lenResult, result, info, row, flags);
  1112. }
  1113. virtual IWorkUnit *updateWorkUnit() const
  1114. {
  1115. if (workUnit)
  1116. return &workUnit->lock();
  1117. else
  1118. return NULL;
  1119. }
  1120. virtual IConstWorkUnit *queryWorkUnit() const
  1121. {
  1122. return workUnit;
  1123. }
  1124. // roxiemem::IRowAllocatorMetaActIdCacheCallback
  1125. virtual IEngineRowAllocator *createAllocator(IOutputMetaData *meta, unsigned activityId, unsigned id) const
  1126. {
  1127. return createRoxieRowAllocator(*rowManager, meta, activityId, id, roxiemem::RHFnone);
  1128. }
  1129. virtual void getResultRowset(size32_t & tcount, byte * * & tgt, const char * stepname, unsigned sequence, IEngineRowAllocator * _rowAllocator, bool isGrouped, IXmlToRowTransformer * xmlTransformer, ICsvToRowTransformer * csvTransformer)
  1130. {
  1131. try
  1132. {
  1133. Owned<IWorkUnitRowReader> wuReader = getWorkunitRowReader(stepname, sequence, xmlTransformer, _rowAllocator, isGrouped);
  1134. wuReader->getResultRowset(tcount, tgt);
  1135. }
  1136. catch (IException * e)
  1137. {
  1138. StringBuffer text;
  1139. e->errorMessage(text);
  1140. e->Release();
  1141. throw MakeStringException(ROXIE_DATA_ERROR, "Failed to retrieve data value \"%s\". [%s]", stepname, text.str());
  1142. }
  1143. catch (...)
  1144. {
  1145. throw MakeStringException(ROXIE_DATA_ERROR, "Failed to retrieve data value \"%s\"", stepname);
  1146. }
  1147. }
  1148. virtual void getResultDictionary(size32_t & tcount, byte * * & tgt, IEngineRowAllocator * _rowAllocator, const char * stepname, unsigned sequence, IXmlToRowTransformer * xmlTransformer, ICsvToRowTransformer * csvTransformer, IHThorHashLookupInfo * hasher)
  1149. {
  1150. try
  1151. {
  1152. Owned<IWorkUnitRowReader> wuReader = getWorkunitRowReader(stepname, sequence, xmlTransformer, _rowAllocator, false);
  1153. wuReader->getResultRowset(tcount, tgt);
  1154. }
  1155. catch (IException * e)
  1156. {
  1157. StringBuffer text;
  1158. e->errorMessage(text);
  1159. e->Release();
  1160. throw MakeStringException(ROXIE_DATA_ERROR, "Failed to retrieve data value \"%s\". [%s]", stepname, text.str());
  1161. }
  1162. catch (...)
  1163. {
  1164. throw MakeStringException(ROXIE_DATA_ERROR, "Failed to retrieve data value \"%s\"", stepname);
  1165. }
  1166. }
  1167. virtual bool getResultBool(const char * name, unsigned sequence)
  1168. {
  1169. CriticalBlock b(contextCrit);
  1170. return useContext(sequence).getPropBool(name);
  1171. }
  1172. static unsigned hex2digit(char c)
  1173. {
  1174. // MORE - what about error cases?
  1175. if (c >= 'a')
  1176. return (c - 'a' + 10);
  1177. else if (c >= 'A')
  1178. return (c - 'A' + 10);
  1179. return (c - '0');
  1180. }
  1181. virtual void getResultData(unsigned & tlen, void * & tgt, const char * name, unsigned sequence)
  1182. {
  1183. MemoryBuffer result;
  1184. CriticalBlock b(contextCrit);
  1185. const char *val = useContext(sequence).queryProp(name);
  1186. if (val)
  1187. {
  1188. loop
  1189. {
  1190. char c0 = *val++;
  1191. if (!c0)
  1192. break;
  1193. char c1 = *val++;
  1194. if (!c1)
  1195. break; // Shouldn't really happen - we expect even length
  1196. unsigned c2 = (hex2digit(c0) << 4) | hex2digit(c1);
  1197. result.append((unsigned char) c2);
  1198. }
  1199. }
  1200. tlen = result.length();
  1201. tgt = result.detach();
  1202. }
  1203. virtual void getResultDecimal(unsigned tlen, int precision, bool isSigned, void * tgt, const char * stepname, unsigned sequence)
  1204. {
  1205. if (isSpecialResultSequence(sequence))
  1206. {
  1207. MemoryBuffer m;
  1208. CriticalBlock b(contextCrit);
  1209. useContext(sequence).getPropBin(stepname, m);
  1210. if (m.length())
  1211. {
  1212. assertex(m.length() == tlen);
  1213. m.read(tlen, tgt);
  1214. }
  1215. else
  1216. memset(tgt, 0, tlen);
  1217. }
  1218. else
  1219. {
  1220. StringBuffer x;
  1221. {
  1222. CriticalBlock b(contextCrit);
  1223. useContext(sequence).getProp(stepname, x);
  1224. }
  1225. Decimal d;
  1226. d.setString(x.length(), x.str());
  1227. if (isSigned)
  1228. d.getDecimal(tlen, precision, tgt);
  1229. else
  1230. d.getUDecimal(tlen, precision, tgt);
  1231. }
  1232. }
  1233. virtual __int64 getResultInt(const char * name, unsigned sequence)
  1234. {
  1235. CriticalBlock b(contextCrit);
  1236. const char *val = useContext(sequence).queryProp(name);
  1237. if (val)
  1238. {
  1239. // NOTE - we use this rather than getPropInt64 since it handles uint64 values up to MAX_UINT better (for our purposes)
  1240. return rtlStrToInt8(strlen(val), val);
  1241. }
  1242. else
  1243. return 0;
  1244. }
  1245. virtual double getResultReal(const char * name, unsigned sequence)
  1246. {
  1247. CriticalBlock b(contextCrit);
  1248. IPropertyTree &ctx = useContext(sequence);
  1249. double ret = 0;
  1250. if (ctx.hasProp(name))
  1251. {
  1252. if (ctx.isBinary(name))
  1253. {
  1254. MemoryBuffer buf;
  1255. ctx.getPropBin(name, buf);
  1256. buf.read(ret);
  1257. }
  1258. else
  1259. {
  1260. const char *val = ctx.queryProp(name);
  1261. if (val)
  1262. ret = atof(val);
  1263. }
  1264. }
  1265. return ret;
  1266. }
  1267. virtual void getResultSet(bool & tisAll, unsigned & tlen, void * & tgt, const char *stepname, unsigned sequence, IXmlToRowTransformer * xmlTransformer, ICsvToRowTransformer * csvTransformer)
  1268. {
  1269. try
  1270. {
  1271. CriticalBlock b(contextCrit);
  1272. IPropertyTree &ctx = useContext(sequence);
  1273. IPropertyTree *val = ctx.queryPropTree(stepname);
  1274. doExtractRawResultX(tlen, tgt, val, sequence, xmlTransformer, csvTransformer, true);
  1275. tisAll = val ? val->getPropBool("@isAll", false) : false;
  1276. }
  1277. catch (IException * e)
  1278. {
  1279. StringBuffer text;
  1280. e->errorMessage(text);
  1281. e->Release();
  1282. throw MakeStringException(ROXIE_DATA_ERROR, "Failed to retrieve set \"%s\". [%s]", stepname, text.str());
  1283. }
  1284. catch (...)
  1285. {
  1286. throw MakeStringException(ROXIE_DATA_ERROR, "Failed to retrieve set \"%s\"", stepname);
  1287. }
  1288. }
  1289. virtual void getResultRaw(unsigned & tlen, void * & tgt, const char *stepname, unsigned sequence, IXmlToRowTransformer * xmlTransformer, ICsvToRowTransformer * csvTransformer)
  1290. {
  1291. try
  1292. {
  1293. CriticalBlock b(contextCrit);
  1294. IPropertyTree &ctx = useContext(sequence);
  1295. IPropertyTree *val = ctx.queryPropTree(stepname);
  1296. doExtractRawResultX(tlen, tgt, val, sequence, xmlTransformer, csvTransformer, false);
  1297. }
  1298. catch (IException * e)
  1299. {
  1300. StringBuffer text;
  1301. e->errorMessage(text);
  1302. e->Release();
  1303. throw MakeStringException(ROXIE_DATA_ERROR, "Failed to retrieve data value \"%s\". [%s]", stepname, text.str());
  1304. }
  1305. catch (...)
  1306. {
  1307. throw MakeStringException(ROXIE_DATA_ERROR, "Failed to retrieve data value \"%s\"", stepname);
  1308. }
  1309. }
  1310. virtual void getResultString(unsigned & tlen, char * & tgt, const char * name, unsigned sequence)
  1311. {
  1312. MemoryBuffer x;
  1313. bool isBinary;
  1314. {
  1315. CriticalBlock b(contextCrit);
  1316. IPropertyTree &ctx = useContext(sequence);
  1317. isBinary = ctx.isBinary(name);
  1318. ctx.getPropBin(name, x);
  1319. }
  1320. if (isBinary) // No utf8 translation if previously set via setResultString
  1321. {
  1322. tlen = x.length();
  1323. tgt = (char *) x.detach();
  1324. }
  1325. else
  1326. rtlUtf8ToStrX(tlen, tgt, rtlUtf8Length(x.length(), x.toByteArray()), x.toByteArray());
  1327. }
  1328. virtual void getResultStringF(unsigned tlen, char * tgt, const char * name, unsigned sequence)
  1329. {
  1330. MemoryBuffer x;
  1331. bool isBinary;
  1332. {
  1333. CriticalBlock b(contextCrit);
  1334. IPropertyTree &ctx = useContext(sequence);
  1335. isBinary = ctx.isBinary(name);
  1336. ctx.getPropBin(name, x);
  1337. }
  1338. if (isBinary)
  1339. rtlStrToStr(tlen, tgt, x.length(), x.toByteArray());
  1340. else
  1341. rtlUtf8ToStr(tlen, tgt, rtlUtf8Length(x.length(), x.toByteArray()), x.toByteArray());
  1342. }
  1343. virtual void getResultUnicode(unsigned & tlen, UChar * & tgt, const char * name, unsigned sequence)
  1344. {
  1345. StringBuffer x;
  1346. {
  1347. CriticalBlock b(contextCrit);
  1348. useContext(sequence).getProp(name, x);
  1349. }
  1350. tgt = rtlCodepageToVUnicodeX(x.length(), x.str(), "utf-8");
  1351. tlen = rtlUnicodeStrlen(tgt);
  1352. }
  1353. virtual char *getResultVarString(const char * name, unsigned sequence)
  1354. {
  1355. CriticalBlock b(contextCrit);
  1356. IPropertyTree &ctx = useContext(sequence);
  1357. bool isBinary = ctx.isBinary(name);
  1358. if (isBinary)
  1359. {
  1360. StringBuffer s;
  1361. ctx.getProp(name, s);
  1362. return s.detach();
  1363. }
  1364. else
  1365. {
  1366. MemoryBuffer x;
  1367. ctx.getPropBin(name, x);
  1368. return rtlUtf8ToVStr(rtlUtf8Length(x.length(), x.toByteArray()), x.toByteArray());
  1369. }
  1370. }
  1371. virtual UChar *getResultVarUnicode(const char * name, unsigned sequence)
  1372. {
  1373. StringBuffer x;
  1374. CriticalBlock b(contextCrit);
  1375. useContext(sequence).getProp(name, x);
  1376. return rtlVCodepageToVUnicodeX(x.str(), "utf-8");
  1377. }
  1378. protected:
  1379. mutable CriticalSection contextCrit;
  1380. Owned<IPropertyTree> context;
  1381. IPropertyTree *temporaries;
  1382. IPropertyTree *rereadResults;
  1383. PTreeReaderOptions xmlStoredDatasetReadFlags;
  1384. CDeserializedResultStore *deserializedResultStore;
  1385. IPropertyTree &useContext(unsigned sequence)
  1386. {
  1387. checkAbort();
  1388. switch (sequence)
  1389. {
  1390. case ResultSequenceStored:
  1391. if (context)
  1392. return *context;
  1393. else
  1394. throw MakeStringException(ROXIE_CODEGEN_ERROR, "Code generation error - attempting to access stored variable on slave");
  1395. case ResultSequencePersist:
  1396. throwUnexpected(); // Do not expect to see in Roxie
  1397. case ResultSequenceInternal:
  1398. {
  1399. CriticalBlock b(contextCrit);
  1400. if (!temporaries)
  1401. temporaries = createPTree();
  1402. return *temporaries;
  1403. }
  1404. case ResultSequenceOnce:
  1405. {
  1406. return factory->queryOnceContext(logctx);
  1407. }
  1408. default:
  1409. {
  1410. CriticalBlock b(contextCrit);
  1411. if (!rereadResults)
  1412. rereadResults = createPTree();
  1413. return *rereadResults;
  1414. }
  1415. }
  1416. }
  1417. IDeserializedResultStore &useResultStore(unsigned sequence)
  1418. {
  1419. checkAbort();
  1420. switch (sequence)
  1421. {
  1422. case ResultSequenceOnce:
  1423. return factory->queryOnceResultStore();
  1424. default:
  1425. // No need to have separate stores for other temporaries...
  1426. CriticalBlock b(contextCrit);
  1427. if (!deserializedResultStore)
  1428. deserializedResultStore = new CDeserializedResultStore;
  1429. return *deserializedResultStore;
  1430. }
  1431. }
  1432. void doExtractRawResultX(unsigned & tlen, void * & tgt, IPropertyTree *val, unsigned sequence, IXmlToRowTransformer * xmlTransformer, ICsvToRowTransformer * csvTransformer, bool isSet)
  1433. {
  1434. tgt = NULL;
  1435. tlen = 0;
  1436. if (val)
  1437. {
  1438. if (val->isBinary())
  1439. {
  1440. MemoryBuffer m;
  1441. val->getPropBin(NULL, m);
  1442. tlen = m.length();
  1443. tgt= m.detach();
  1444. }
  1445. else
  1446. {
  1447. const char *format = val->queryProp("@format");
  1448. if (!format || strcmp(format, "xml")==0)
  1449. {
  1450. assertex(xmlTransformer);
  1451. Variable2IDataVal result(&tlen, &tgt);
  1452. CXmlToRawTransformer rawTransformer(*xmlTransformer, xmlStoredDatasetReadFlags);
  1453. rawTransformer.transformTree(result, *val, !isSet);
  1454. }
  1455. else if (strcmp(format, "deserialized")==0)
  1456. {
  1457. IDeserializedResultStore &resultStore = useResultStore(sequence);
  1458. resultStore.serialize(tlen, tgt, val->getPropInt("@id", -1), queryCodeContext());
  1459. }
  1460. else if (strcmp(format, "csv")==0)
  1461. {
  1462. // MORE - never tested this code.....
  1463. assertex(csvTransformer);
  1464. Variable2IDataVal result(&tlen, &tgt);
  1465. MemoryBuffer m;
  1466. val->getPropBin(NULL, m);
  1467. CCsvToRawTransformer rawCsvTransformer(*csvTransformer);
  1468. rawCsvTransformer.transform(result, m.length(), m.toByteArray(), !isSet);
  1469. }
  1470. else
  1471. throw MakeStringException(ROXIE_UNIMPLEMENTED_ERROR, "no transform function available");
  1472. }
  1473. }
  1474. }
  1475. virtual IWorkUnitRowReader *createStreamedRawRowReader(IEngineRowAllocator *rowAllocator, bool isGrouped, const char *id)
  1476. {
  1477. throwUnexpected(); // Should only see on server
  1478. }
  1479. virtual IWorkUnitRowReader *getWorkunitRowReader(const char *stepname, unsigned sequence, IXmlToRowTransformer * xmlTransformer, IEngineRowAllocator *rowAllocator, bool isGrouped)
  1480. {
  1481. try
  1482. {
  1483. CriticalBlock b(contextCrit);
  1484. IPropertyTree &ctx = useContext(sequence);
  1485. IPropertyTree *val = ctx.queryPropTree(stepname);
  1486. if (val)
  1487. {
  1488. const char *id = val->queryProp("@id");
  1489. const char *format = val->queryProp("@format");
  1490. if (id)
  1491. {
  1492. if (!format || strcmp(format, "raw") == 0)
  1493. {
  1494. return createStreamedRawRowReader(rowAllocator, isGrouped, id);
  1495. }
  1496. else if (strcmp(format, "deserialized") == 0)
  1497. {
  1498. IDeserializedResultStore &resultStore = useResultStore(sequence);
  1499. return resultStore.createDeserializedReader(atoi(id));
  1500. }
  1501. else
  1502. throwUnexpected();
  1503. }
  1504. else
  1505. {
  1506. if (!format || strcmp(format, "xml") == 0)
  1507. {
  1508. if (xmlTransformer)
  1509. return new InlineXmlDataReader(*xmlTransformer, val, rowAllocator, isGrouped);
  1510. }
  1511. else if (strcmp(format, "raw") == 0)
  1512. {
  1513. return new InlineRawDataReader(queryCodeContext(), rowAllocator, isGrouped, logctx, val);
  1514. }
  1515. else
  1516. throwUnexpected();
  1517. }
  1518. }
  1519. }
  1520. catch (IException * e)
  1521. {
  1522. StringBuffer text;
  1523. e->errorMessage(text);
  1524. e->Release();
  1525. throw MakeStringException(ROXIE_DATA_ERROR, "Failed to retrieve data value %s. [%s]", stepname, text.str());
  1526. }
  1527. catch (...)
  1528. {
  1529. throw MakeStringException(ROXIE_DATA_ERROR, "Failed to retrieve data value %s", stepname);
  1530. }
  1531. throw MakeStringException(ROXIE_DATA_ERROR, "Failed to retrieve data value %s", stepname);
  1532. }
  1533. };
  1534. IRoxieSlaveContext *createSlaveContext(const IQueryFactory *_factory, const SlaveContextLogger &_logctx, unsigned _timeLimit, memsize_t _memoryLimit, IRoxieQueryPacket *packet)
  1535. {
  1536. return new CSlaveContext(_factory, _logctx, _timeLimit, _memoryLimit, packet, _logctx.queryTraceActivityTimes(), _logctx.queryDebuggerActive());
  1537. }
  1538. class CRoxieServerDebugContext : extends CBaseServerDebugContext
  1539. {
  1540. // Some questions:
  1541. // 1. Do we let all threads go even when say step? Probably... (may allow a thread to be suspended at some point)
  1542. // 2. Doesn't that then make a bit of a mockery of step (when there are multiple threads active)... I _think_ it actually means we DON'T try to wait for all
  1543. // threads to hit a stop, but allow any that hit stop while we are paused to be queued up to be returned by step.... perhaps actually stop them in critsec rather than
  1544. // semaphore and it all becomes easier to code... Anything calling checkBreakPoint while program state is "in debugger" will block on that critSec.
  1545. // 3. I think we need to recheck breakpoints on Roxie server but just check not deleted
  1546. public:
  1547. IRoxieSlaveContext *ctx;
  1548. CRoxieServerDebugContext(IRoxieSlaveContext *_ctx, const IContextLogger &_logctx, IPropertyTree *_queryXGMML, SafeSocket &_client)
  1549. : CBaseServerDebugContext(_logctx, _queryXGMML, _client), ctx(_ctx)
  1550. {
  1551. }
  1552. void debugCounts(IXmlWriter *output, unsigned sinceSequence, bool reset)
  1553. {
  1554. CriticalBlock b(debugCrit);
  1555. if (running)
  1556. throw MakeStringException(ROXIE_DEBUG_ERROR, "Command not available while query is running");
  1557. if (currentGraph)
  1558. currentGraph->mergeRemoteCounts(this);
  1559. CBaseServerDebugContext::debugCounts(output, sinceSequence, reset);
  1560. }
  1561. virtual void waitForDebugger(DebugState state, IActivityDebugContext *probe)
  1562. {
  1563. ctx->setWUState(WUStateDebugPaused);
  1564. CBaseServerDebugContext::waitForDebugger(state, probe);
  1565. ctx->setWUState(WUStateDebugRunning);
  1566. }
  1567. virtual bool onDebuggerTimeout()
  1568. {
  1569. return ctx->checkWuAborted();
  1570. }
  1571. virtual void debugInitialize(const char *id, const char *_queryName, bool _breakAtStart)
  1572. {
  1573. CBaseServerDebugContext::debugInitialize(id, _queryName, _breakAtStart);
  1574. queryRoxieDebugSessionManager().registerDebugId(id, this);
  1575. }
  1576. virtual void debugTerminate()
  1577. {
  1578. CriticalBlock b(debugCrit);
  1579. assertex(running);
  1580. currentState = DebugStateUnloaded;
  1581. running = false;
  1582. queryRoxieDebugSessionManager().deregisterDebugId(debugId);
  1583. if (debuggerActive)
  1584. {
  1585. debuggerSem.signal(debuggerActive);
  1586. debuggerActive = 0;
  1587. }
  1588. }
  1589. virtual IRoxieQueryPacket *onDebugCallback(const RoxiePacketHeader &header, size32_t len, char *data)
  1590. {
  1591. MemoryBuffer slaveInfo;
  1592. slaveInfo.setBuffer(len, data, false);
  1593. unsigned debugSequence;
  1594. slaveInfo.read(debugSequence);
  1595. {
  1596. CriticalBlock b(breakCrit); // we want to wait until it's our turn before updating the graph info or the counts get ahead of the current row and life is confusing
  1597. char slaveStateChar;
  1598. slaveInfo.read(slaveStateChar);
  1599. DebugState slaveState = (DebugState) slaveStateChar;
  1600. if (slaveState==DebugStateGraphFinished)
  1601. {
  1602. unsigned numCounts;
  1603. slaveInfo.read(numCounts);
  1604. while (numCounts)
  1605. {
  1606. StringAttr edgeId;
  1607. unsigned edgeCount;
  1608. slaveInfo.read(edgeId);
  1609. slaveInfo.read(edgeCount);
  1610. Owned<IGlobalEdgeRecord> thisEdge = getEdgeRecord(edgeId);
  1611. thisEdge->incrementCount(edgeCount, sequence);
  1612. numCounts--;
  1613. }
  1614. }
  1615. slaveInfo.read(currentBreakpointUID);
  1616. memsize_t slaveActivity;
  1617. unsigned channel;
  1618. __uint64 tmp;
  1619. slaveInfo.read(tmp);
  1620. slaveActivity = (memsize_t)tmp;
  1621. slaveInfo.read(channel);
  1622. assertex(currentGraph);
  1623. currentGraph->deserializeProxyGraphs(slaveState, slaveInfo, (IActivityBase *) slaveActivity, channel);
  1624. if (slaveState != DebugStateGraphFinished) // MORE - this is debatable - may (at least sometimes) want a child graph finished to be a notified event...
  1625. {
  1626. StringBuffer slaveActivityId;
  1627. slaveInfo.read(slaveActivityId);
  1628. IActivityDebugContext *slaveActivityCtx = slaveActivityId.length() ? currentGraph->lookupActivityByEdgeId(slaveActivityId.str()) : NULL;
  1629. BreakpointActionMode action = checkBreakpoint(slaveState, slaveActivityCtx , NULL);
  1630. }
  1631. }
  1632. MemoryBuffer mb;
  1633. mb.append(sizeof(RoxiePacketHeader), &header);
  1634. StringBuffer debugIdString;
  1635. debugIdString.appendf(".debug.%x", debugSequence);
  1636. mb.append(debugIdString.str());
  1637. serialize(mb);
  1638. Owned<IRoxieQueryPacket> reply = createRoxiePacket(mb);
  1639. reply->queryHeader().activityId = ROXIE_DEBUGCALLBACK;
  1640. reply->queryHeader().retries = 0;
  1641. return reply.getClear();
  1642. }
  1643. virtual void debugPrintVariable(IXmlWriter *output, const char *name, const char *type) const
  1644. {
  1645. CriticalBlock b(debugCrit);
  1646. if (running)
  1647. throw MakeStringException(ROXIE_DEBUG_ERROR, "Command not available while query is running");
  1648. output->outputBeginNested("Variables", true);
  1649. if (!type || stricmp(type, "temporary"))
  1650. {
  1651. output->outputBeginNested("Temporary", true);
  1652. ctx->printResults(output, name, (unsigned) ResultSequenceInternal);
  1653. output->outputEndNested("Temporary");
  1654. }
  1655. if (!type || stricmp(type, "global"))
  1656. {
  1657. output->outputBeginNested("Global", true);
  1658. ctx->printResults(output, name, (unsigned) ResultSequenceStored);
  1659. output->outputEndNested("Global");
  1660. }
  1661. output->outputEndNested("Variables");
  1662. }
  1663. };
  1664. class CRoxieServerContext : public CSlaveContext, implements IRoxieServerContext, implements IGlobalCodeContext
  1665. {
  1666. const IQueryFactory *serverQueryFactory;
  1667. CriticalSection daliUpdateCrit;
  1668. TextMarkupFormat mlFmt;
  1669. bool isRaw;
  1670. bool sendHeartBeats;
  1671. unsigned warnTimeLimit;
  1672. unsigned lastSocketCheckTime;
  1673. unsigned lastHeartBeat;
  1674. protected:
  1675. Owned<WorkflowMachine> workflow;
  1676. SafeSocket *client;
  1677. bool isBlocked;
  1678. bool isHttp;
  1679. bool trim;
  1680. void doPostProcess()
  1681. {
  1682. CriticalBlock b(resultsCrit); // Probably not needed
  1683. if (!isRaw && !isBlocked)
  1684. {
  1685. ForEachItemIn(seq, resultMap)
  1686. {
  1687. FlushingStringBuffer *result = resultMap.item(seq);
  1688. if (result)
  1689. result->flush(true);
  1690. }
  1691. }
  1692. if (probeQuery)
  1693. {
  1694. FlushingStringBuffer response(client, isBlocked, MarkupFmt_XML, false, isHttp, *this);
  1695. // create output stream
  1696. response.startDataset("_Probe", NULL, (unsigned) -1); // initialize it
  1697. // loop through all of the graphs and create a _Probe to output each xgmml
  1698. Owned<IPropertyTreeIterator> graphs = probeQuery->getElements("Graph");
  1699. ForEach(*graphs)
  1700. {
  1701. IPropertyTree &graph = graphs->query();
  1702. StringBuffer xgmml;
  1703. _toXML(&graph, xgmml, 0);
  1704. response.append("\n");
  1705. response.append(xgmml.str());
  1706. }
  1707. }
  1708. }
  1709. void addWuException(IException *E)
  1710. {
  1711. if (workUnit)
  1712. ::addWuException(workUnit, E);
  1713. }
  1714. void init()
  1715. {
  1716. client = NULL;
  1717. totSlavesReplyLen = 0;
  1718. mlFmt = MarkupFmt_XML;
  1719. isRaw = false;
  1720. isBlocked = false;
  1721. isHttp = false;
  1722. trim = false;
  1723. priority = 0;
  1724. sendHeartBeats = false;
  1725. timeLimit = serverQueryFactory->getTimeLimit();
  1726. if (!timeLimit)
  1727. timeLimit = defaultTimeLimit[priority];
  1728. warnTimeLimit = serverQueryFactory->getWarnTimeLimit();
  1729. if (!warnTimeLimit)
  1730. warnTimeLimit = defaultWarnTimeLimit[priority];
  1731. lastSocketCheckTime = startTime;
  1732. lastHeartBeat = startTime;
  1733. ctxParallelJoinPreload = defaultParallelJoinPreload;
  1734. ctxFullKeyedJoinPreload = defaultFullKeyedJoinPreload;
  1735. ctxKeyedJoinPreload = defaultKeyedJoinPreload;
  1736. ctxConcatPreload = defaultConcatPreload;
  1737. ctxFetchPreload = defaultFetchPreload;
  1738. ctxPrefetchProjectPreload = defaultPrefetchProjectPreload;
  1739. traceActivityTimes = false;
  1740. }
  1741. void startWorkUnit()
  1742. {
  1743. WorkunitUpdate wu(&workUnit->lock());
  1744. if (!context->getPropBool("@outputToSocket", false))
  1745. client = NULL;
  1746. SCMStringBuffer wuParams;
  1747. if (workUnit->getXmlParams(wuParams).length())
  1748. {
  1749. // Merge in params from WU. Ones on command line take precedence though...
  1750. Owned<IPropertyTree> wuParamTree = createPTreeFromXMLString(wuParams.str(), ipt_caseInsensitive);
  1751. Owned<IPropertyTreeIterator> params = wuParamTree ->getElements("*");
  1752. ForEach(*params)
  1753. {
  1754. IPropertyTree &param = params->query();
  1755. if (!context->hasProp(param.queryName()))
  1756. context->addPropTree(param.queryName(), LINK(&param));
  1757. }
  1758. }
  1759. if (workUnit->getDebugValueBool("Debug", false))
  1760. {
  1761. bool breakAtStart = workUnit->getDebugValueBool("BreakAtStart", true);
  1762. wu->setState(WUStateDebugRunning);
  1763. SCMStringBuffer wuid;
  1764. initDebugMode(breakAtStart, workUnit->getWuid(wuid).str());
  1765. }
  1766. else
  1767. wu->setState(WUStateRunning);
  1768. }
  1769. void initDebugMode(bool breakAtStart, const char *debugUID)
  1770. {
  1771. if (!debugPermitted || !ownEP.port)
  1772. throw MakeStringException(ROXIE_ACCESS_ERROR, "Debug queries are not permitted on this system");
  1773. debugContext.setown(new CRoxieServerDebugContext(this, logctx, factory->cloneQueryXGMML(), *client));
  1774. debugContext->debugInitialize(debugUID, factory->queryQueryName(), breakAtStart);
  1775. if (workUnit)
  1776. {
  1777. WorkunitUpdate wu(&workUnit->lock());
  1778. wu->setDebugAgentListenerPort(ownEP.port); //tells debugger what port to write commands to
  1779. StringBuffer sb;
  1780. ownEP.getIpText(sb);
  1781. wu->setDebugAgentListenerIP(sb); //tells debugger what IP to write commands to
  1782. }
  1783. timeLimit = 0;
  1784. warnTimeLimit = 0;
  1785. }
  1786. public:
  1787. IMPLEMENT_IINTERFACE;
  1788. CRoxieServerContext(const IQueryFactory *_factory, const IRoxieContextLogger &_logctx)
  1789. : CSlaveContext(_factory, _logctx, 0, 0, NULL, false, false), serverQueryFactory(_factory)
  1790. {
  1791. init();
  1792. rowManager->setMemoryLimit(serverQueryFactory->getMemoryLimit());
  1793. workflow.setown(_factory->createWorkflowMachine(true, logctx));
  1794. context.setown(createPTree(ipt_caseInsensitive));
  1795. }
  1796. CRoxieServerContext(IConstWorkUnit *_workUnit, const IQueryFactory *_factory, const IRoxieContextLogger &_logctx)
  1797. : CSlaveContext(_factory, _logctx, 0, 0, NULL, false, false), serverQueryFactory(_factory)
  1798. {
  1799. init();
  1800. workUnit.set(_workUnit);
  1801. rowManager->setMemoryLimit(serverQueryFactory->getMemoryLimit());
  1802. workflow.setown(_factory->createWorkflowMachine(false, logctx));
  1803. context.setown(createPTree(ipt_caseInsensitive));
  1804. startWorkUnit();
  1805. }
  1806. CRoxieServerContext(IPropertyTree *_context, const IQueryFactory *_factory, SafeSocket &_client, TextMarkupFormat _mlFmt, bool _isRaw, bool _isBlocked, HttpHelper &httpHelper, bool _trim, unsigned _priority, const IRoxieContextLogger &_logctx, PTreeReaderOptions _xmlReadFlags)
  1807. : CSlaveContext(_factory, _logctx, 0, 0, NULL, false, false), serverQueryFactory(_factory)
  1808. {
  1809. init();
  1810. context.set(_context);
  1811. client = &_client;
  1812. mlFmt = _mlFmt;
  1813. isRaw = _isRaw;
  1814. isBlocked = _isBlocked;
  1815. isHttp = httpHelper.isHttp();
  1816. trim = _trim;
  1817. priority = _priority;
  1818. xmlStoredDatasetReadFlags = _xmlReadFlags;
  1819. sendHeartBeats = enableHeartBeat && isRaw && isBlocked && priority==0;
  1820. timeLimit = context->getPropInt("_TimeLimit", timeLimit);
  1821. warnTimeLimit = context->getPropInt("_warnTimeLimit", warnTimeLimit);
  1822. const char *wuid = context->queryProp("@wuid");
  1823. if (wuid)
  1824. {
  1825. IRoxieDaliHelper *daliHelper = checkDaliConnection();
  1826. assertex(daliHelper );
  1827. workUnit.setown(daliHelper->attachWorkunit(wuid, _factory->queryDll()));
  1828. if (!workUnit)
  1829. throw MakeStringException(ROXIE_DALI_ERROR, "Failed to open workunit %s", wuid);
  1830. startWorkUnit();
  1831. }
  1832. else if (context->getPropBool("@debug", false))
  1833. {
  1834. bool breakAtStart = context->getPropBool("@break", true);
  1835. const char *debugUID = context->queryProp("@uid");
  1836. if (debugUID && *debugUID)
  1837. initDebugMode(breakAtStart, debugUID);
  1838. }
  1839. else if (context->getPropBool("_Probe", false))
  1840. probeQuery.setown(_factory->cloneQueryXGMML());
  1841. // MORE some of these might be appropriate in wu case too?
  1842. rowManager->setActivityTracking(context->getPropBool("_TraceMemory", false));
  1843. rowManager->setMemoryLimit((memsize_t) context->getPropInt64("_MemoryLimit", _factory->getMemoryLimit()));
  1844. authToken.append(httpHelper.queryAuthToken());
  1845. workflow.setown(_factory->createWorkflowMachine(false, logctx));
  1846. ctxParallelJoinPreload = context->getPropInt("_ParallelJoinPreload", defaultParallelJoinPreload);
  1847. ctxFullKeyedJoinPreload = context->getPropInt("_FullKeyedJoinPreload", defaultFullKeyedJoinPreload);
  1848. ctxKeyedJoinPreload = context->getPropInt("_KeyedJoinPreload", defaultKeyedJoinPreload);
  1849. ctxConcatPreload = context->getPropInt("_ConcatPreload", defaultConcatPreload);
  1850. ctxFetchPreload = context->getPropInt("_FetchPreload", defaultFetchPreload);
  1851. ctxPrefetchProjectPreload = context->getPropInt("_PrefetchProjectPreload", defaultPrefetchProjectPreload);
  1852. traceActivityTimes = context->getPropBool("_TraceActivityTimes", false) || context->getPropBool("@timing", false);
  1853. }
  1854. virtual roxiemem::IRowManager &queryRowManager()
  1855. {
  1856. return *rowManager;
  1857. }
  1858. virtual IRoxieDaliHelper *checkDaliConnection()
  1859. {
  1860. CriticalBlock b(daliUpdateCrit);
  1861. if (!daliHelperLink)
  1862. daliHelperLink.setown(::connectToDali());
  1863. return daliHelperLink;
  1864. }
  1865. virtual void checkAbort()
  1866. {
  1867. CSlaveContext::checkAbort();
  1868. unsigned ticksNow = msTick();
  1869. if (warnTimeLimit)
  1870. {
  1871. unsigned elapsed = ticksNow - startTime;
  1872. if (elapsed > warnTimeLimit)
  1873. {
  1874. CriticalBlock b(abortLock);
  1875. if (elapsed > warnTimeLimit) // we don't want critsec on the first check (for efficiency) but check again inside the critsec
  1876. {
  1877. logOperatorException(NULL, NULL, 0, "SLOW (%d ms): %s", elapsed, factory->queryQueryName());
  1878. warnTimeLimit = elapsed + warnTimeLimit;
  1879. }
  1880. }
  1881. }
  1882. if (client)
  1883. {
  1884. if (socketCheckInterval)
  1885. {
  1886. if (ticksNow - lastSocketCheckTime > socketCheckInterval)
  1887. {
  1888. CriticalBlock b(abortLock);
  1889. if (!client->checkConnection())
  1890. throw MakeStringException(ROXIE_CLIENT_CLOSED, "Client socket closed");
  1891. lastSocketCheckTime = ticksNow;
  1892. }
  1893. }
  1894. if (sendHeartBeats)
  1895. {
  1896. unsigned hb = ticksNow - lastHeartBeat;
  1897. if (hb > 30000)
  1898. {
  1899. lastHeartBeat = msTick();
  1900. client->sendHeartBeat(*this);
  1901. }
  1902. }
  1903. }
  1904. }
  1905. virtual unsigned getXmlFlags() const
  1906. {
  1907. return trim ? XWFtrim|XWFopt : XWFexpandempty;
  1908. }
  1909. virtual unsigned getMemoryUsage()
  1910. {
  1911. return rowManager->getMemoryUsage();
  1912. }
  1913. virtual unsigned getSlavesReplyLen()
  1914. {
  1915. return totSlavesReplyLen;
  1916. }
  1917. virtual void process()
  1918. {
  1919. EclProcessFactory pf = (EclProcessFactory) factory->queryDll()->getEntry("createProcess");
  1920. Owned<IEclProcess> p = pf();
  1921. try
  1922. {
  1923. if (debugContext)
  1924. debugContext->checkBreakpoint(DebugStateReady, NULL, NULL);
  1925. if (workflow)
  1926. workflow->perform(this, p);
  1927. else
  1928. p->perform(this, 0);
  1929. }
  1930. catch(WorkflowException *E)
  1931. {
  1932. if (debugContext)
  1933. debugContext->checkBreakpoint(DebugStateException, NULL, static_cast<IException *>(E));
  1934. addWuException(E);
  1935. doPostProcess();
  1936. throw;
  1937. }
  1938. catch(IException *E)
  1939. {
  1940. if (debugContext)
  1941. debugContext->checkBreakpoint(DebugStateException, NULL, E);
  1942. addWuException(E);
  1943. doPostProcess();
  1944. throw;
  1945. }
  1946. catch(...)
  1947. {
  1948. if (debugContext)
  1949. debugContext->checkBreakpoint(DebugStateFailed, NULL, NULL);
  1950. if (workUnit)
  1951. {
  1952. Owned<IException> E = MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception caught in CRoxieServerContext::process");
  1953. addWuException(E);
  1954. }
  1955. doPostProcess();
  1956. throw;
  1957. }
  1958. if (debugContext)
  1959. debugContext->checkBreakpoint(DebugStateFinished, NULL, NULL);
  1960. doPostProcess();
  1961. }
  1962. virtual void done(bool failed)
  1963. {
  1964. if (debugContext)
  1965. debugContext->debugTerminate();
  1966. setWUState(aborted ? WUStateAborted : (failed ? WUStateFailed : WUStateCompleted));
  1967. }
  1968. virtual ICodeContext *queryCodeContext()
  1969. {
  1970. return this;
  1971. }
  1972. virtual IRoxieServerContext *queryServerContext()
  1973. {
  1974. return this;
  1975. }
  1976. virtual IGlobalCodeContext *queryGlobalCodeContext()
  1977. {
  1978. return this;
  1979. }
  1980. // interface ICodeContext
  1981. virtual FlushingStringBuffer *queryResult(unsigned sequence)
  1982. {
  1983. if (!client && workUnit)
  1984. return NULL; // when outputting to workunit only, don't output anything to stdout
  1985. CriticalBlock procedure(resultsCrit);
  1986. while (!resultMap.isItem(sequence))
  1987. resultMap.append(NULL);
  1988. FlushingStringBuffer *result = resultMap.item(sequence);
  1989. if (!result)
  1990. {
  1991. result = new FlushingStringBuffer(client, isBlocked, mlFmt, isRaw, isHttp, *this);
  1992. result->isSoap = isHttp;
  1993. result->trim = trim;
  1994. result->queryName.set(context->queryName());
  1995. resultMap.replace(result, sequence);
  1996. }
  1997. return result;
  1998. }
  1999. virtual char *getDaliServers()
  2000. {
  2001. //MORE: Should this now be implemented using IRoxieDaliHelper?
  2002. throwUnexpected();
  2003. }
  2004. virtual void setResultBool(const char *name, unsigned sequence, bool value)
  2005. {
  2006. if (isSpecialResultSequence(sequence))
  2007. {
  2008. CriticalBlock b(contextCrit);
  2009. useContext(sequence).setPropBool(name, value);
  2010. }
  2011. else
  2012. {
  2013. FlushingStringBuffer *r = queryResult(sequence);
  2014. if (r)
  2015. {
  2016. r->startScalar(name, sequence);
  2017. if (isRaw)
  2018. r->append(sizeof(value), (char *)&value);
  2019. else
  2020. r->append(value ? "true" : "false");
  2021. }
  2022. }
  2023. if (workUnit)
  2024. {
  2025. try
  2026. {
  2027. WorkunitUpdate wu(&workUnit->lock());
  2028. CriticalBlock b(daliUpdateCrit); // MORE - do we really need these locks?
  2029. wu->setResultBool(name, sequence, value);
  2030. }
  2031. catch(IException *e)
  2032. {
  2033. StringBuffer text;
  2034. e->errorMessage(text);
  2035. CTXLOG("Error trying to update dali: %s", text.str());
  2036. e->Release();
  2037. }
  2038. catch(...)
  2039. {
  2040. CTXLOG("Unknown exception trying to update dali");
  2041. }
  2042. }
  2043. }
  2044. virtual void setResultData(const char *name, unsigned sequence, int len, const void * data)
  2045. {
  2046. static char hexchar[] = "0123456789ABCDEF";
  2047. if (isSpecialResultSequence(sequence))
  2048. {
  2049. StringBuffer s;
  2050. const byte *field = (const byte *) data;
  2051. for (int i = 0; i < len; i++)
  2052. s.append(hexchar[field[i] >> 4]).append(hexchar[field[i] & 0x0f]);
  2053. CriticalBlock b(contextCrit);
  2054. IPropertyTree &ctx = useContext(sequence);
  2055. ctx.setProp(name, s.str());
  2056. }
  2057. else
  2058. {
  2059. FlushingStringBuffer *r = queryResult(sequence);
  2060. if (r)
  2061. {
  2062. r->startScalar(name, sequence);
  2063. if (isRaw)
  2064. r->append(len, (const char *) data);
  2065. else
  2066. {
  2067. const byte *field = (const byte *) data;
  2068. for (int i = 0; i < len; i++)
  2069. {
  2070. r->append(hexchar[field[i] >> 4]);
  2071. r->append(hexchar[field[i] & 0x0f]);
  2072. }
  2073. }
  2074. }
  2075. }
  2076. if (workUnit)
  2077. {
  2078. try
  2079. {
  2080. WorkunitUpdate wu(&workUnit->lock());
  2081. CriticalBlock b(daliUpdateCrit);
  2082. wu->setResultData(name, sequence, len, data);
  2083. }
  2084. catch(IException *e)
  2085. {
  2086. StringBuffer text;
  2087. e->errorMessage(text);
  2088. CTXLOG("Error trying to update dali: %s", text.str());
  2089. e->Release();
  2090. }
  2091. catch(...)
  2092. {
  2093. CTXLOG("Unknown exception trying to update dali");
  2094. }
  2095. }
  2096. }
  2097. virtual void appendResultDeserialized(const char *name, unsigned sequence, size32_t count, rowset_t data, bool extend, IOutputMetaData *meta)
  2098. {
  2099. CriticalBlock b(contextCrit);
  2100. IPropertyTree &ctx = useContext(sequence);
  2101. IDeserializedResultStore &resultStore = useResultStore(sequence);
  2102. IPropertyTree *val = ctx.queryPropTree(name);
  2103. if (extend && val)
  2104. {
  2105. int oldId = val->getPropInt("@id", -1);
  2106. const char * oldFormat = val->queryProp("@format");
  2107. assertex(oldId != -1);
  2108. assertex(oldFormat && strcmp(oldFormat, "deserialized")==0);
  2109. size32_t oldCount;
  2110. rowset_t oldData;
  2111. resultStore.queryResult(oldId, oldCount, oldData);
  2112. Owned<IEngineRowAllocator> allocator = createRoxieRowAllocator(*rowManager, meta, 0, 0, roxiemem::RHFnone);
  2113. RtlLinkedDatasetBuilder builder(allocator);
  2114. builder.appendRows(oldCount, oldData);
  2115. builder.appendRows(count, data);
  2116. rtlReleaseRowset(count, data);
  2117. val->setPropInt("@id", resultStore.addResult(builder.getcount(), builder.linkrows(), meta));
  2118. }
  2119. else
  2120. {
  2121. if (!val)
  2122. val = ctx.addPropTree(name, createPTree());
  2123. val->setProp("@format", "deserialized");
  2124. val->setPropInt("@id", resultStore.addResult(count, data, meta));
  2125. }
  2126. }
  2127. virtual void appendResultRawContext(const char *name, unsigned sequence, int len, const void * data, int numRows, bool extend, bool saveInContext)
  2128. {
  2129. if (saveInContext)
  2130. {
  2131. CriticalBlock b(contextCrit);
  2132. IPropertyTree &ctx = useContext(sequence);
  2133. ctx.appendPropBin(name, len, data);
  2134. ctx.queryPropTree(name)->setProp("@format", "raw");
  2135. }
  2136. if (workUnit)
  2137. {
  2138. try
  2139. {
  2140. WorkunitUpdate wu(&workUnit->lock());
  2141. CriticalBlock b(daliUpdateCrit);
  2142. wu->setResultDataset(name, sequence, len, data, numRows, extend);
  2143. }
  2144. catch(IException *e)
  2145. {
  2146. StringBuffer text;
  2147. e->errorMessage(text);
  2148. CTXLOG("Error trying to update dali: %s", text.str());
  2149. e->Release();
  2150. }
  2151. catch(...)
  2152. {
  2153. CTXLOG("Unknown exception trying to update dali");
  2154. }
  2155. }
  2156. }
  2157. virtual void setResultRaw(const char *name, unsigned sequence, int len, const void * data)
  2158. {
  2159. if (isSpecialResultSequence(sequence))
  2160. {
  2161. CriticalBlock b(contextCrit);
  2162. IPropertyTree &ctx = useContext(sequence);
  2163. ctx.setPropBin(name, len, data);
  2164. ctx.queryPropTree(name)->setProp("@format", "raw");
  2165. }
  2166. else
  2167. {
  2168. FlushingStringBuffer *r = queryResult(sequence);
  2169. if (r)
  2170. {
  2171. r->startScalar(name, sequence);
  2172. if (isRaw)
  2173. r->append(len, (const char *) data);
  2174. else
  2175. UNIMPLEMENTED;
  2176. }
  2177. }
  2178. if (workUnit)
  2179. {
  2180. try
  2181. {
  2182. WorkunitUpdate wu(&workUnit->lock());
  2183. CriticalBlock b(daliUpdateCrit);
  2184. wu->setResultRaw(name, sequence, len, data);
  2185. }
  2186. catch(IException *e)
  2187. {
  2188. StringBuffer text;
  2189. e->errorMessage(text);
  2190. CTXLOG("Error trying to update dali: %s", text.str());
  2191. e->Release();
  2192. }
  2193. catch(...)
  2194. {
  2195. CTXLOG("Unknown exception trying to update dali");
  2196. }
  2197. }
  2198. }
  2199. virtual void setResultSet(const char *name, unsigned sequence, bool isAll, size32_t len, const void * data, ISetToXmlTransformer * transformer)
  2200. {
  2201. if (isSpecialResultSequence(sequence))
  2202. {
  2203. CriticalBlock b(contextCrit);
  2204. IPropertyTree &ctx = useContext(sequence);
  2205. ctx.setPropBin(name, len, data);
  2206. ctx.queryPropTree(name)->setProp("@format", "raw");
  2207. ctx.queryPropTree(name)->setPropBool("@isAll", isAll);
  2208. }
  2209. else
  2210. {
  2211. FlushingStringBuffer *r = queryResult(sequence);
  2212. if (r)
  2213. {
  2214. r->startScalar(name, sequence);
  2215. if (isRaw)
  2216. r->append(len, (char *)data);
  2217. else if (mlFmt==MarkupFmt_XML)
  2218. {
  2219. assertex(transformer);
  2220. CommonXmlWriter writer(getXmlFlags()|XWFnoindent, 0);
  2221. transformer->toXML(isAll, len, (byte *)data, writer);
  2222. r->append(writer.str());
  2223. }
  2224. else if (mlFmt==MarkupFmt_JSON)
  2225. {
  2226. assertex(transformer);
  2227. CommonJsonWriter writer(getXmlFlags()|XWFnoindent, 0);
  2228. transformer->toXML(isAll, len, (byte *)data, writer);
  2229. r->append(writer.str());
  2230. }
  2231. else
  2232. {
  2233. assertex(transformer);
  2234. r->append('[');
  2235. if (isAll)
  2236. r->appendf("*]");
  2237. else
  2238. {
  2239. SimpleOutputWriter x;
  2240. transformer->toXML(isAll, len, (const byte *) data, x);
  2241. r->appendf("%s]", x.str());
  2242. }
  2243. }
  2244. }
  2245. }
  2246. if (workUnit)
  2247. {
  2248. try
  2249. {
  2250. WorkunitUpdate wu(&workUnit->lock());
  2251. CriticalBlock b(daliUpdateCrit);
  2252. wu->setResultSet(name, sequence, isAll, len, data, transformer);
  2253. }
  2254. catch(IException *e)
  2255. {
  2256. StringBuffer text;
  2257. e->errorMessage(text);
  2258. CTXLOG("Error trying to update dali: %s", text.str());
  2259. e->Release();
  2260. }
  2261. catch(...)
  2262. {
  2263. CTXLOG("Unknown exception trying to update dali");
  2264. }
  2265. }
  2266. }
  2267. virtual void setResultXml(const char *name, unsigned sequence, const char *xml)
  2268. {
  2269. CriticalBlock b(contextCrit);
  2270. useContext(sequence).setPropTree(name, createPTreeFromXMLString(xml, ipt_caseInsensitive));
  2271. }
  2272. virtual void setResultDecimal(const char *name, unsigned sequence, int len, int precision, bool isSigned, const void *val)
  2273. {
  2274. if (isSpecialResultSequence(sequence))
  2275. {
  2276. MemoryBuffer m;
  2277. serializeFixedData(len, val, m);
  2278. CriticalBlock b(contextCrit);
  2279. useContext(sequence).setPropBin(name, m.length(), m.toByteArray());
  2280. }
  2281. else
  2282. {
  2283. FlushingStringBuffer *r = queryResult(sequence);
  2284. if (r)
  2285. {
  2286. r->startScalar(name, sequence);
  2287. if (isRaw)
  2288. r->append(len, (char *)val);
  2289. else
  2290. {
  2291. StringBuffer s;
  2292. if (isSigned)
  2293. outputXmlDecimal(val, len, precision, NULL, s);
  2294. else
  2295. outputXmlUDecimal(val, len, precision, NULL, s);
  2296. r->append(s);
  2297. }
  2298. }
  2299. }
  2300. if (workUnit)
  2301. {
  2302. try
  2303. {
  2304. WorkunitUpdate wu(&workUnit->lock());
  2305. CriticalBlock b(daliUpdateCrit);
  2306. wu->setResultDecimal(name, sequence, len, precision, isSigned, val);
  2307. }
  2308. catch(IException *e)
  2309. {
  2310. StringBuffer text;
  2311. e->errorMessage(text);
  2312. CTXLOG("Error trying to update dali: %s", text.str());
  2313. e->Release();
  2314. }
  2315. catch(...)
  2316. {
  2317. CTXLOG("Unknown exception trying to update dali");
  2318. }
  2319. }
  2320. }
  2321. virtual void setResultInt(const char *name, unsigned sequence, __int64 value)
  2322. {
  2323. if (isSpecialResultSequence(sequence))
  2324. {
  2325. CriticalBlock b(contextCrit);
  2326. useContext(sequence).setPropInt64(name, value);
  2327. }
  2328. else
  2329. {
  2330. FlushingStringBuffer *r = queryResult(sequence);
  2331. if (r)
  2332. {
  2333. r->startScalar(name, sequence);
  2334. if (isRaw)
  2335. r->append(sizeof(value), (char *)&value);
  2336. else
  2337. r->appendf("%"I64F"d", value);
  2338. }
  2339. }
  2340. if (workUnit)
  2341. {
  2342. try
  2343. {
  2344. WorkunitUpdate wu(&workUnit->lock());
  2345. CriticalBlock b(daliUpdateCrit);
  2346. wu->setResultInt(name, sequence, value);
  2347. }
  2348. catch(IException *e)
  2349. {
  2350. StringBuffer text;
  2351. e->errorMessage(text);
  2352. CTXLOG("Error trying to update dali: %s", text.str());
  2353. e->Release();
  2354. }
  2355. catch(...)
  2356. {
  2357. CTXLOG("Unknown exception trying to update dali");
  2358. }
  2359. }
  2360. }
  2361. virtual void setResultUInt(const char *name, unsigned sequence, unsigned __int64 value)
  2362. {
  2363. if (isSpecialResultSequence(sequence))
  2364. {
  2365. CriticalBlock b(contextCrit);
  2366. useContext(sequence).setPropInt64(name, value);
  2367. }
  2368. else
  2369. {
  2370. FlushingStringBuffer *r = queryResult(sequence);
  2371. if (r)
  2372. {
  2373. r->startScalar(name, sequence);
  2374. if (isRaw)
  2375. r->append(sizeof(value), (char *)&value);
  2376. else
  2377. r->appendf("%"I64F"u", value);
  2378. }
  2379. }
  2380. if (workUnit)
  2381. {
  2382. try
  2383. {
  2384. WorkunitUpdate wu(&workUnit->lock());
  2385. CriticalBlock b(daliUpdateCrit);
  2386. wu->setResultUInt(name, sequence, value);
  2387. }
  2388. catch(IException *e)
  2389. {
  2390. StringBuffer text;
  2391. e->errorMessage(text);
  2392. CTXLOG("Error trying to update dali: %s", text.str());
  2393. e->Release();
  2394. }
  2395. catch(...)
  2396. {
  2397. CTXLOG("Unknown exception trying to update dali");
  2398. }
  2399. }
  2400. }
  2401. virtual void setResultReal(const char *name, unsigned sequence, double value)
  2402. {
  2403. if (isSpecialResultSequence(sequence))
  2404. {
  2405. CriticalBlock b(contextCrit);
  2406. useContext(sequence).setPropBin(name, sizeof(value), &value);
  2407. }
  2408. else
  2409. {
  2410. StringBuffer v;
  2411. v.append(value);
  2412. FlushingStringBuffer *r = queryResult(sequence);
  2413. if (r)
  2414. {
  2415. r->startScalar(name, sequence);
  2416. if (r->isRaw)
  2417. r->append(sizeof(value), (char *)&value);
  2418. else
  2419. r->appendf("%s", v.str());
  2420. }
  2421. }
  2422. if (workUnit)
  2423. {
  2424. try
  2425. {
  2426. WorkunitUpdate wu(&workUnit->lock());
  2427. CriticalBlock b(daliUpdateCrit);
  2428. wu->setResultReal(name, sequence, value);
  2429. }
  2430. catch(IException *e)
  2431. {
  2432. StringBuffer text;
  2433. e->errorMessage(text);
  2434. CTXLOG("Error trying to update dali: %s", text.str());
  2435. e->Release();
  2436. }
  2437. catch(...)
  2438. {
  2439. CTXLOG("Unknown exception trying to update dali");
  2440. }
  2441. }
  2442. }
  2443. virtual void setResultString(const char *name, unsigned sequence, int len, const char * str)
  2444. {
  2445. if (isSpecialResultSequence(sequence))
  2446. {
  2447. CriticalBlock b(contextCrit);
  2448. useContext(sequence).setPropBin(name, len, str);
  2449. }
  2450. else
  2451. {
  2452. FlushingStringBuffer *r = queryResult(sequence);
  2453. if (r)
  2454. {
  2455. r->startScalar(name, sequence);
  2456. if (r->isRaw)
  2457. {
  2458. r->append(len, str);
  2459. }
  2460. else
  2461. {
  2462. r->encodeXML(str, 0, len);
  2463. }
  2464. }
  2465. }
  2466. if (workUnit)
  2467. {
  2468. try
  2469. {
  2470. WorkunitUpdate wu(&workUnit->lock());
  2471. CriticalBlock b(daliUpdateCrit);
  2472. wu->setResultString(name, sequence, len, str);
  2473. }
  2474. catch(IException *e)
  2475. {
  2476. StringBuffer text;
  2477. e->errorMessage(text);
  2478. CTXLOG("Error trying to update dali: %s", text.str());
  2479. e->Release();
  2480. }
  2481. catch(...)
  2482. {
  2483. CTXLOG("Unknown exception trying to update dali");
  2484. }
  2485. }
  2486. }
  2487. virtual void setResultUnicode(const char *name, unsigned sequence, int len, UChar const * str)
  2488. {
  2489. if (isSpecialResultSequence(sequence))
  2490. {
  2491. rtlDataAttr buff;
  2492. unsigned bufflen = 0;
  2493. rtlUnicodeToCodepageX(bufflen, buff.refstr(), len, str, "utf-8");
  2494. CriticalBlock b(contextCrit);
  2495. useContext(sequence).setPropBin(name, bufflen, buff.getstr());
  2496. }
  2497. else
  2498. {
  2499. FlushingStringBuffer *r = queryResult(sequence);
  2500. if (r)
  2501. {
  2502. r->startScalar(name, sequence);
  2503. if (r->isRaw)
  2504. {
  2505. r->append(len*2, (const char *) str);
  2506. }
  2507. else
  2508. {
  2509. rtlDataAttr buff;
  2510. unsigned bufflen = 0;
  2511. rtlUnicodeToCodepageX(bufflen, buff.refstr(), len, str, "utf-8");
  2512. r->encodeXML(buff.getstr(), 0, bufflen, true); // output as UTF-8
  2513. }
  2514. }
  2515. }
  2516. if (workUnit)
  2517. {
  2518. try
  2519. {
  2520. WorkunitUpdate wu(&workUnit->lock());
  2521. CriticalBlock b(daliUpdateCrit);
  2522. wu->setResultUnicode(name, sequence, len, str);
  2523. }
  2524. catch(IException *e)
  2525. {
  2526. StringBuffer text;
  2527. e->errorMessage(text);
  2528. CTXLOG("Error trying to update dali: %s", text.str());
  2529. e->Release();
  2530. }
  2531. catch(...)
  2532. {
  2533. CTXLOG("Unknown exception trying to update dali");
  2534. }
  2535. }
  2536. }
  2537. virtual void setResultVarString(const char * name, unsigned sequence, const char * value)
  2538. {
  2539. setResultString(name, sequence, strlen(value), value);
  2540. }
  2541. virtual void setResultVarUnicode(const char * name, unsigned sequence, UChar const * value)
  2542. {
  2543. setResultUnicode(name, sequence, rtlUnicodeStrlen(value), value);
  2544. }
  2545. virtual void setResultDataset(const char * name, unsigned sequence, size32_t len, const void *data, unsigned numRows, bool extend)
  2546. {
  2547. appendResultRawContext(name, sequence, len, data, numRows, extend, true);
  2548. }
  2549. virtual IWorkUnitRowReader *createStreamedRawRowReader(IEngineRowAllocator *rowAllocator, bool isGrouped, const char *id)
  2550. {
  2551. return new StreamedRawDataReader(this, rowAllocator, isGrouped, logctx, *client, id);
  2552. }
  2553. virtual void printResults(IXmlWriter *output, const char *name, unsigned sequence)
  2554. {
  2555. CriticalBlock b(contextCrit);
  2556. IPropertyTree &tree = useContext(sequence);
  2557. if (name)
  2558. {
  2559. const char *val = tree.queryProp(name);
  2560. if (val)
  2561. output->outputCString(val, name);
  2562. }
  2563. else
  2564. {
  2565. StringBuffer hack;
  2566. toXML(&tree, hack);
  2567. output->outputString(0, NULL, NULL); // Hack upon hack...
  2568. output->outputQuoted(hack.str());
  2569. }
  2570. }
  2571. virtual const IResolvedFile *resolveLFN(const char *filename, bool isOpt)
  2572. {
  2573. return factory->queryPackage().lookupFileName(filename, isOpt, true, workUnit);
  2574. }
  2575. virtual IRoxieWriteHandler *createLFN(const char *filename, bool overwrite, bool extend, const StringArray &clusters)
  2576. {
  2577. return factory->queryPackage().createFileName(filename, overwrite, extend, clusters, workUnit);
  2578. }
  2579. virtual void onFileCallback(const RoxiePacketHeader &header, const char *lfn, bool isOpt, bool isLocal)
  2580. {
  2581. Owned<const IResolvedFile> dFile = resolveLFN(lfn, isOpt);
  2582. if (dFile)
  2583. {
  2584. MemoryBuffer mb;
  2585. mb.append(sizeof(RoxiePacketHeader), &header);
  2586. mb.append(lfn);
  2587. dFile->serializePartial(mb, header.channel, isLocal);
  2588. ((RoxiePacketHeader *) mb.toByteArray())->activityId = ROXIE_FILECALLBACK;
  2589. Owned<IRoxieQueryPacket> reply = createRoxiePacket(mb);
  2590. reply->queryHeader().retries = 0;
  2591. ROQ->sendPacket(reply, *this); // MORE - the caller's log context might be better? Should we unicast? Note that this does not release the packet
  2592. return;
  2593. }
  2594. ROQ->sendAbortCallback(header, lfn, *this);
  2595. throwUnexpected();
  2596. }
  2597. virtual void getExternalResultRaw(unsigned & tlen, void * & tgt, const char * wuid, const char * stepname, unsigned sequence, IXmlToRowTransformer * xmlTransformer, ICsvToRowTransformer * csvTransformer)
  2598. {
  2599. UNIMPLEMENTED;
  2600. }
  2601. virtual void addWuException(const char * text, unsigned code, unsigned _severity)
  2602. {
  2603. WUExceptionSeverity severity = (WUExceptionSeverity) _severity;
  2604. CTXLOG("%s", text);
  2605. if (severity > ExceptionSeverityInformation)
  2606. OERRLOG("%d - %s", code, text);
  2607. if (workUnit)
  2608. {
  2609. WorkunitUpdate wu(&workUnit->lock());
  2610. addExceptionToWorkunit(wu, severity, "user", code, text, NULL, 0 ,0);
  2611. }
  2612. }
  2613. virtual void addWuAssertFailure(unsigned code, const char * text, const char * filename, unsigned lineno, unsigned column, bool isAbort)
  2614. {
  2615. CTXLOG("%s", text);
  2616. OERRLOG("%d - %s", code, text);
  2617. if (workUnit)
  2618. {
  2619. WorkunitUpdate wu(&workUnit->lock());
  2620. addExceptionToWorkunit(wu, ExceptionSeverityError, "user", code, text, filename, lineno, column);
  2621. }
  2622. if (isAbort)
  2623. rtlFailOnAssert(); // minimal implementation
  2624. }
  2625. IUserDescriptor *queryUserDescriptor()
  2626. {
  2627. return NULL; // TBD - Richard, where do user credentials for a roxie query come from
  2628. }
  2629. virtual bool isResult(const char * name, unsigned sequence)
  2630. {
  2631. CriticalBlock b(contextCrit);
  2632. return useContext(sequence).hasProp(name);
  2633. }
  2634. virtual char *getClusterName() { throwUnexpected(); }
  2635. virtual char *getGroupName() { throwUnexpected(); }
  2636. virtual char * queryIndexMetaData(char const * lfn, char const * xpath) { throwUnexpected(); }
  2637. virtual char *getEnv(const char *name, const char *defaultValue) const
  2638. {
  2639. return serverQueryFactory->getEnv(name, defaultValue);
  2640. }
  2641. virtual char *getJobName()
  2642. {
  2643. return strdup(factory->queryQueryName());
  2644. }
  2645. virtual char *getJobOwner() { throwUnexpected(); }
  2646. virtual char *getPlatform()
  2647. {
  2648. return strdup("roxie");
  2649. }
  2650. virtual char *getWuid()
  2651. {
  2652. if (workUnit)
  2653. {
  2654. SCMStringBuffer wuid;
  2655. workUnit->getWuid(wuid);
  2656. return strdup(wuid.str());
  2657. }
  2658. else
  2659. {
  2660. throw MakeStringException(ROXIE_INVALID_ACTION, "No workunit associated with this context");
  2661. }
  2662. }
  2663. // persist-related code - usage of persist should have been caught and rejected at codegen time
  2664. virtual char * getExpandLogicalName(const char * logicalName) { throwUnexpected(); }
  2665. virtual void startPersist(const char * name) { throwUnexpected(); }
  2666. virtual void finishPersist() { throwUnexpected(); }
  2667. virtual void clearPersist(const char * logicalName) { throwUnexpected(); }
  2668. virtual void updatePersist(const char * logicalName, unsigned eclCRC, unsigned __int64 allCRC) { throwUnexpected(); }
  2669. virtual void checkPersistMatches(const char * logicalName, unsigned eclCRC) { throwUnexpected(); }
  2670. virtual void setWorkflowCondition(bool value) { if(workflow) workflow->setCondition(value); }
  2671. virtual void returnPersistVersion(const char * logicalName, unsigned eclCRC, unsigned __int64 allCRC, bool isFile) { throwUnexpected(); }
  2672. virtual void fail(int code, const char *text)
  2673. {
  2674. addWuException(text, code, 2);
  2675. }
  2676. virtual unsigned getWorkflowId() { return workflow->queryCurrentWfid(); }
  2677. virtual void doNotify(char const * code, char const * extra) { UNIMPLEMENTED; }
  2678. virtual void doNotify(char const * code, char const * extra, const char * target) { UNIMPLEMENTED; }
  2679. virtual void doWait(unsigned code, char const * extra) { UNIMPLEMENTED; }
  2680. virtual void doWaitCond(unsigned code, char const * extra, int sequence, char const * alias, unsigned wfid) { UNIMPLEMENTED; }
  2681. virtual unsigned __int64 getDatasetHash(const char * name, unsigned __int64 hash) { throwUnexpected(); }
  2682. virtual int queryLastFailCode() { UNIMPLEMENTED; }
  2683. virtual void getLastFailMessage(size32_t & outLen, char * &outStr, const char * tag) { UNIMPLEMENTED; }
  2684. virtual void getEventName(size32_t & outLen, char * & outStr) { UNIMPLEMENTED; }
  2685. virtual void getEventExtra(size32_t & outLen, char * & outStr, const char * tag) { UNIMPLEMENTED; }
  2686. virtual bool fileExists(const char * filename) { throwUnexpected(); }
  2687. virtual void deleteFile(const char * logicalName) { throwUnexpected(); }
  2688. virtual unsigned getNodes() { return numChannels; }
  2689. virtual unsigned getNodeNum() { return 1; }
  2690. virtual char *getFilePart(const char *logicalPart, bool create=false) { UNIMPLEMENTED; }
  2691. virtual unsigned __int64 getFileOffset(const char *logicalPart) { throwUnexpected(); }
  2692. virtual IDistributedFileTransaction *querySuperFileTransaction() { UNIMPLEMENTED; }
  2693. virtual void flush(unsigned seqNo) { throwUnexpected(); }
  2694. virtual unsigned getPriority() const { return priority; }
  2695. virtual bool outputResultsToWorkUnit() const { return workUnit != NULL; }
  2696. virtual bool outputResultsToSocket() const { return client != NULL; }
  2697. virtual void selectCluster(const char * cluster) { throwUnexpected(); }
  2698. virtual void restoreCluster() { throwUnexpected(); }
  2699. };
  2700. //================================================================================================
  2701. class CSoapRoxieServerContext : public CRoxieServerContext
  2702. {
  2703. private:
  2704. StringAttr queryName;
  2705. public:
  2706. CSoapRoxieServerContext(IPropertyTree *_context, const IQueryFactory *_factory, SafeSocket &_client, HttpHelper &httpHelper, unsigned _priority, const IRoxieContextLogger &_logctx, PTreeReaderOptions xmlReadFlags)
  2707. : CRoxieServerContext(_context, _factory, _client, MarkupFmt_XML, false, false, httpHelper, true, _priority, _logctx, xmlReadFlags)
  2708. {
  2709. queryName.set(_context->queryName());
  2710. }
  2711. virtual void process()
  2712. {
  2713. EclProcessFactory pf = (EclProcessFactory) factory->queryDll()->getEntry("createProcess");
  2714. Owned<IEclProcess> p = pf();
  2715. if (workflow)
  2716. workflow->perform(this, p);
  2717. else
  2718. p->perform(this, 0);
  2719. }
  2720. virtual void flush(unsigned seqNo)
  2721. {
  2722. CriticalBlock b(resultsCrit);
  2723. CriticalBlock b1(client->queryCrit());
  2724. StringBuffer responseHead, responseTail;
  2725. responseHead.append("<").append(queryName).append("Response");
  2726. responseHead.append(" sequence=\"").append(seqNo).append("\"");
  2727. responseHead.append(" xmlns=\"urn:hpccsystems:ecl:").appendLower(queryName.length(), queryName.sget()).append("\">");
  2728. responseHead.append("<Results><Result>");
  2729. unsigned len = responseHead.length();
  2730. client->write(responseHead.detach(), len, true);
  2731. ForEachItemIn(seq, resultMap)
  2732. {
  2733. FlushingStringBuffer *result = resultMap.item(seq);
  2734. if (result)
  2735. {
  2736. result->flush(true);
  2737. for(;;)
  2738. {
  2739. size32_t length;
  2740. void *payload = result->getPayload(length);
  2741. if (!length)
  2742. break;
  2743. client->write(payload, length, true);
  2744. }
  2745. }
  2746. }
  2747. responseTail.append("</Result></Results>");
  2748. responseTail.append("</").append(queryName).append("Response>");
  2749. len = responseTail.length();
  2750. client->write(responseTail.detach(), len, true);
  2751. }
  2752. };
  2753. class CJsonRoxieServerContext : public CRoxieServerContext
  2754. {
  2755. private:
  2756. StringAttr queryName;
  2757. public:
  2758. CJsonRoxieServerContext(IPropertyTree *_context, const IQueryFactory *_factory, SafeSocket &_client, HttpHelper &httpHelper, unsigned _priority, const IRoxieContextLogger &_logctx, PTreeReaderOptions xmlReadFlags)
  2759. : CRoxieServerContext(_context, _factory, _client, MarkupFmt_JSON, false, false, httpHelper, true, _priority, _logctx, xmlReadFlags)
  2760. {
  2761. queryName.set(_context->queryName());
  2762. }
  2763. virtual void process()
  2764. {
  2765. EclProcessFactory pf = (EclProcessFactory) factory->queryDll()->getEntry("createProcess");
  2766. Owned<IEclProcess> p = pf();
  2767. if (workflow)
  2768. workflow->perform(this, p);
  2769. else
  2770. p->perform(this, 0);
  2771. }
  2772. virtual void flush(unsigned seqNo)
  2773. {
  2774. CriticalBlock b(resultsCrit);
  2775. CriticalBlock b1(client->queryCrit());
  2776. StringBuffer responseHead, responseTail;
  2777. appendfJSONName(responseHead, "%sResponse", queryName.get()).append(" {");
  2778. appendJSONValue(responseHead, "sequence", seqNo);
  2779. appendJSONName(responseHead, "Results").append(" {\n ");
  2780. unsigned len = responseHead.length();
  2781. client->write(responseHead.detach(), len, true);
  2782. bool needDelimiter = false;
  2783. ForEachItemIn(seq, resultMap)
  2784. {
  2785. FlushingStringBuffer *result = resultMap.item(seq);
  2786. if (result)
  2787. {
  2788. result->flush(true);
  2789. for(;;)
  2790. {
  2791. size32_t length;
  2792. void *payload = result->getPayload(length);
  2793. if (!length)
  2794. break;
  2795. if (needDelimiter)
  2796. {
  2797. StringAttr s(",\n "); //write() will take ownership of buffer
  2798. size32_t len = s.length();
  2799. client->write((void *)s.detach(), len, true);
  2800. needDelimiter=false;
  2801. }
  2802. client->write(payload, length, true);
  2803. }
  2804. needDelimiter=true;
  2805. }
  2806. }
  2807. responseTail.append("}}");
  2808. len = responseTail.length();
  2809. client->write(responseTail.detach(), len, true);
  2810. }
  2811. virtual FlushingStringBuffer *queryResult(unsigned sequence)
  2812. {
  2813. if (!client && workUnit)
  2814. return NULL; // when outputting to workunit only, don't output anything to stdout
  2815. CriticalBlock procedure(resultsCrit);
  2816. while (!resultMap.isItem(sequence))
  2817. resultMap.append(NULL);
  2818. FlushingStringBuffer *result = resultMap.item(sequence);
  2819. if (!result)
  2820. {
  2821. result = new FlushingJsonBuffer(client, isBlocked, isHttp, *this);
  2822. result->trim = trim;
  2823. result->queryName.set(context->queryName());
  2824. resultMap.replace(result, sequence);
  2825. }
  2826. return result;
  2827. }
  2828. };
  2829. IRoxieServerContext *createRoxieServerContext(IPropertyTree *context, const IQueryFactory *factory, SafeSocket &client, bool isXml, bool isRaw, bool isBlocked, HttpHelper &httpHelper, bool trim, unsigned priority, const IRoxieContextLogger &_logctx, PTreeReaderOptions readFlags)
  2830. {
  2831. if (httpHelper.isHttp())
  2832. {
  2833. if (httpHelper.queryContentFormat()==MarkupFmt_JSON)
  2834. return new CJsonRoxieServerContext(context, factory, client, httpHelper, priority, _logctx, readFlags);
  2835. return new CSoapRoxieServerContext(context, factory, client, httpHelper, priority, _logctx, readFlags);
  2836. }
  2837. else
  2838. return new CRoxieServerContext(context, factory, client, isXml ? MarkupFmt_XML : MarkupFmt_Unknown, isRaw, isBlocked, httpHelper, trim, priority, _logctx, readFlags);
  2839. }
  2840. IRoxieServerContext *createOnceServerContext(const IQueryFactory *factory, const IRoxieContextLogger &_logctx)
  2841. {
  2842. return new CRoxieServerContext(factory, _logctx);
  2843. }
  2844. IRoxieServerContext *createWorkUnitServerContext(IConstWorkUnit *wu, const IQueryFactory *factory, const IRoxieContextLogger &_logctx)
  2845. {
  2846. return new CRoxieServerContext(wu, factory, _logctx);
  2847. }