QueryHelper.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*##############################################################################
  2. Copyright (C) 2011 HPCC Systems.
  3. All rights reserved. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. #include <algorithm>
  15. #include "jlib.hpp"
  16. #include "jmisc.hpp"
  17. #include "QueryHelper.ipp"
  18. #include "ViewHelper.ipp"
  19. QueryHelper::QueryHelper(IProperties * _globals, IFormatType * _format) : globals(_globals), format(_format), wuclient(createWorkunitsClient(_globals))
  20. {
  21. }
  22. QueryHelper::~QueryHelper()
  23. {
  24. }
  25. bool QueryHelper::doit(FILE * fp)
  26. {
  27. Owned<IClientWUCreateRequest> creq = wuclient->createWUCreateRequest();
  28. Owned<IClientWUCreateResponse> cresp = wuclient->WUCreate(creq);
  29. const IMultiException* excep = &cresp->getExceptions();
  30. if(excep != NULL && excep->ordinality() > 0)
  31. {
  32. StringBuffer msg;
  33. excep->errorMessage(msg);
  34. printf("%s\n", msg.str());
  35. return false;
  36. }
  37. IConstECLWorkunit* wu = &cresp->getWorkunit();
  38. if(!wu)
  39. {
  40. printf("can't create workunit\n");
  41. return false;
  42. }
  43. Owned<IClientWUUpdateRequest> ureq = wuclient->createWUUpdateRequest();
  44. ureq->setWuid(wu->getWuid());
  45. // Make a workUnit
  46. if(globals->hasProp("jobname"))
  47. {
  48. ureq->setJobname(globals->queryProp("jobname"));
  49. }
  50. StringBuffer ecl;
  51. if (globals->getProp("ecl", ecl))
  52. {
  53. if (ecl.length() && ecl.charAt(0)=='@')
  54. {
  55. StringBuffer filename(ecl.str()+1);
  56. ecl.clear().loadFile(filename);
  57. }
  58. ureq->setQueryText(ecl.str());
  59. }
  60. else if (globals->hasProp("attr"))
  61. ureq->setQueryText(globals->queryProp("attr"));
  62. if (globals->getPropInt("compileOnly", 0)!=0)
  63. ureq->setAction(WUActionCompile);
  64. IArrayOf<IEspDebugValue> dvals;
  65. IArrayOf<IEspApplicationValue> avals;
  66. StringBuffer xmlParams;
  67. Owned<IPropertyIterator> it = globals->getIterator();
  68. bool xmlSeen = false;
  69. ForEach(*it)
  70. {
  71. const char * key = it->getPropKey();
  72. if (key && strlen(key)>1)
  73. {
  74. if(key[0] == '-')
  75. {
  76. if (key[1] == 'f')
  77. {
  78. Owned<IEspDebugValue> dval = createDebugValue();
  79. dval->setName(&key[2]);
  80. dval->setValue(globals->queryProp(key));
  81. dvals.append(*dval.getLink());
  82. }
  83. //All other options are ignored.
  84. }
  85. else if(key[0] == '_')
  86. {
  87. Owned<IEspApplicationValue> aval = createApplicationValue();
  88. aval->setApplication("eclplus");
  89. aval->setName(&key[1]);
  90. aval->setValue(globals->queryProp(key));
  91. avals.append(*aval.getLink());
  92. }
  93. else if(key[0] == '/')
  94. {
  95. if (xmlSeen)
  96. throw MakeStringException(0, "query option must not be used with stored or /, and cannot appear more than once");
  97. // The / form is expected to be used for scalars, so xmlEncode is appropriate.
  98. // To pass sets or datasets, use the xml= version
  99. xmlParams.appendf("<%s>", &key[1]);
  100. encodeXML(globals->queryProp(key), xmlParams);
  101. xmlParams.appendf("</%s>", &key[1]);
  102. }
  103. else if(stricmp(key, "stored")==0)
  104. {
  105. if (xmlSeen)
  106. throw MakeStringException(0, "query option must not be used with stored or /, and cannot appear more than once");
  107. const char *xml = globals->queryProp(key);
  108. try
  109. {
  110. Owned<IPropertyTree> checkValid = createPTreeFromXMLString(xml, false);
  111. }
  112. catch (IException *E)
  113. {
  114. StringBuffer msg;
  115. E->errorMessage(msg);
  116. E->Release();
  117. throw MakeStringException(0, "Invalid xml: %s", msg.str());
  118. }
  119. xmlParams.append(xml);
  120. }
  121. else if(stricmp(key, "query")==0)
  122. {
  123. if (xmlSeen || xmlParams.length())
  124. throw MakeStringException(0, "query option must not be used with stored or /, and cannot appear more than once");
  125. xmlSeen = true;
  126. StringBuffer xml;
  127. if (!globals->getProp(key, xml))
  128. throw MakeStringException(0, "Invalid value for query= parameter");
  129. if (xml.length() && xml.charAt(0)=='@')
  130. {
  131. StringBuffer filename(xml.str()+1);
  132. xml.clear().loadFile(filename);
  133. }
  134. try
  135. {
  136. Owned<IPropertyTree> checkValid = createPTreeFromXMLString(xml, false);
  137. }
  138. catch (IException *E)
  139. {
  140. StringBuffer msg;
  141. E->errorMessage(msg);
  142. E->Release();
  143. throw MakeStringException(0, "Invalid xml: %s", msg.str());
  144. }
  145. xmlParams.append(xml);
  146. }
  147. }
  148. }
  149. if(dvals.length() > 0)
  150. ureq->setDebugValues(dvals);
  151. if(avals.length() > 0)
  152. ureq->setApplicationValues(avals);
  153. if (xmlParams.length())
  154. {
  155. if (!xmlSeen)
  156. {
  157. xmlParams.insert(0, "<Query>");
  158. xmlParams.append("</Query>");
  159. }
  160. ureq->setXmlParams(xmlParams);
  161. }
  162. Owned<IClientWUUpdateResponse> uresp = wuclient->WUUpdate(ureq);
  163. const IMultiException* uexcep = &uresp->getExceptions();
  164. if(uexcep != NULL && uexcep->ordinality() > 0)
  165. {
  166. StringBuffer msg;
  167. uexcep->errorMessage(msg);
  168. printf("%s\n", msg.str());
  169. return false;
  170. }
  171. // Execute it
  172. return doSubmitWorkUnit(fp, wu->getWuid(), globals->queryProp("cluster"));
  173. }
  174. bool QueryHelper::doSubmitWorkUnit(FILE * fp, const char * wuid, const char* cluster)
  175. {
  176. Owned<IClientWUSubmitRequest> req = wuclient->createWUSubmitRequest();
  177. req->setWuid(wuid);
  178. req->setCluster(cluster);
  179. if(globals->hasProp("snapshot"))
  180. req->setSnapshot(globals->queryProp("snapshot"));
  181. Owned<IClientWUSubmitResponse> resp = wuclient->WUSubmit(req);
  182. if(!resp)
  183. {
  184. printf("error submitting workunit %s\n", wuid);
  185. return false;
  186. }
  187. else
  188. {
  189. const IMultiException* excep = &resp->getExceptions();
  190. if(excep != NULL && excep->ordinality() > 0)
  191. {
  192. StringBuffer msg;
  193. excep->errorMessage(msg);
  194. printf("%s\n", msg.str());
  195. return false;
  196. }
  197. printf("Workunit %s submitted\n", wuid);
  198. }
  199. // Wait for the results (or not)
  200. bool infinite = true;
  201. int timeout = -1;
  202. if(globals->hasProp("timeout"))
  203. {
  204. infinite = false;
  205. timeout = globals->getPropInt("timeout", 0)*1000;
  206. }
  207. bool compileOnly = globals->getPropInt("compileOnly", 0)!=0;
  208. bool returnOnWaitState = globals->getPropInt("returnOnWait", 0)!=0;
  209. if(timeout > 0 || infinite)
  210. {
  211. if (compileOnly)
  212. {
  213. Owned<IClientWUWaitRequest> req = wuclient->createWUWaitCompiledRequest();
  214. req->setWuid(wuid);
  215. req->setWait(timeout);
  216. req->setReturnOnWait(returnOnWaitState);
  217. Owned<IClientWUWaitResponse> resp = wuclient->WUWaitCompiled(req);
  218. WUState s = (WUState)resp->getStateID();
  219. return s == WUStateCompiled;
  220. }
  221. else
  222. {
  223. WUState s;
  224. int initial_wait = 30;
  225. int polling_period = 30;
  226. int waited = 0;
  227. int actual_wait = -1;
  228. while(1)
  229. {
  230. if(actual_wait != 0)
  231. {
  232. // Initially, wait initial_wait
  233. actual_wait = infinite?initial_wait*1000: std::min(timeout, initial_wait*1000);
  234. }
  235. Owned<IClientWUWaitRequest> req = wuclient->createWUWaitCompleteRequest();
  236. req->setWuid(wuid);
  237. req->setWait(actual_wait);
  238. Owned<IClientWUWaitResponse> resp = wuclient->WUWaitComplete(req);
  239. const IMultiException* excep = &resp->getExceptions();
  240. if(excep != NULL && excep->ordinality() > 0)
  241. {
  242. StringBuffer msg;
  243. excep->errorMessage(msg);
  244. printf("%s\n", msg.str());
  245. return false;
  246. }
  247. s = (WUState)resp->getStateID();
  248. if (s != WUStateUnknown)
  249. break;
  250. if(actual_wait != 0)
  251. {
  252. waited += actual_wait;
  253. actual_wait = 0;
  254. if (!infinite && (waited >= timeout))
  255. break;
  256. }
  257. else
  258. {
  259. if (!infinite && (waited >= timeout))
  260. break;
  261. int time_to_wait = infinite?polling_period*1000: std::min(timeout - waited, polling_period*1000);
  262. sleep(time_to_wait/1000);
  263. waited += time_to_wait;
  264. }
  265. }
  266. if (s != WUStateUnknown)
  267. {
  268. globals->setProp("wuid", wuid);
  269. if(format)
  270. LINK(format);
  271. Owned<IEclPlusHelper> viewer = new ViewHelper(LINK(globals), format);
  272. viewer->doit(fp);
  273. }
  274. return ((s == WUStateCompleted) || (returnOnWaitState && (s == WUStateWait)));
  275. }
  276. }
  277. else
  278. {
  279. StringBuffer s;
  280. s.append("Submitted WUID ").append(wuid);
  281. fprintf(fp, "%s", s.str());
  282. return true;
  283. }
  284. }
  285. bool RerunHelper::doit(FILE * fp)
  286. {
  287. if(!globals->hasProp("wuid"))
  288. {
  289. printf("No wuid specified");
  290. return false;
  291. }
  292. if(!globals->hasProp("cluster"))
  293. {
  294. printf("No cluster name specified");
  295. return false;
  296. }
  297. StringBuffer wuid, cluster;
  298. globals->getProp("WUID", wuid);
  299. globals->getProp("cluster", cluster);
  300. // Execute it
  301. return doSubmitWorkUnit(fp, wuid.str(), cluster.str());
  302. }