roxiequerycompiler.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. #pragma warning (disable : 4786)
  15. #include "jlib.hpp"
  16. #include "roxiequerycompiler.hpp"
  17. class CRoxieQueryCompileInfo : public CInterface, implements IRoxieQueryCompileInfo
  18. {
  19. private:
  20. unsigned memoryLimit;
  21. unsigned wuTimeOut;
  22. unsigned timeLimit;
  23. unsigned warnTimeLimit;
  24. bool poolGraphs;
  25. bool highPriority;
  26. StringBuffer repositoryLabel;
  27. StringBuffer moduleName;
  28. StringBuffer defaultStyleName;
  29. StringBuffer wuDebugOptions;
  30. StringBuffer userName;
  31. StringBuffer password;
  32. StringBuffer jobName;
  33. StringBuffer ecl;
  34. StringBuffer appName;
  35. StringBuffer clusterName;
  36. int queryPriority;
  37. public:
  38. IMPLEMENT_IINTERFACE;
  39. CRoxieQueryCompileInfo(const char *_ecl, const char *_jobName, const char *_clusterName, const char *_appName)
  40. :ecl(_ecl), jobName(_jobName), clusterName(_clusterName)
  41. {
  42. memoryLimit = 0;
  43. wuTimeOut = 0;
  44. timeLimit = 0;
  45. warnTimeLimit = 0;
  46. poolGraphs = false;
  47. highPriority = false;
  48. queryPriority = UNKNOWN_PRIORITY;
  49. if (_appName && *_appName)
  50. appName.append(_appName);
  51. else
  52. appName.append("RoxieManager");
  53. }
  54. virtual void setMemoryLimit(unsigned val) { memoryLimit = val; }
  55. virtual unsigned getMemoryLimit() { return memoryLimit; }
  56. virtual void setWuTimeOut(unsigned val) { wuTimeOut = val; }
  57. virtual unsigned getWuTimeOut() { return wuTimeOut; }
  58. virtual void setTimeLimit(unsigned val) { timeLimit = val; }
  59. virtual unsigned getTimeLimit() { return timeLimit; }
  60. virtual void setWarnTimeLimit(unsigned val) { warnTimeLimit = val; }
  61. virtual unsigned getWarnTimeLimit() { return warnTimeLimit; }
  62. virtual void setPoolGraphs(bool val) { poolGraphs = val; }
  63. virtual bool getPoolGraphs() { return poolGraphs; }
  64. virtual void setHighPriority(bool val) { highPriority = val; }
  65. virtual bool getHighPriority() { return highPriority; }
  66. virtual void setRepositoryLabel(const char *val) { repositoryLabel.append(val); }
  67. virtual const char * queryRepositoryLabel() { return repositoryLabel.str(); }
  68. virtual void enableWebServiceInfoRetrieval(const char *_moduleName, const char *_defaultStyleName)
  69. { moduleName.append(_moduleName); defaultStyleName.append(_defaultStyleName);}
  70. virtual void setQueryPriority(int val) { queryPriority = val; }
  71. virtual int getQueryPriority() { return queryPriority; }
  72. virtual void setWuDebugOptions(const char *val) { wuDebugOptions.append(val); }
  73. virtual const char * queryWuDebugOptions() { return wuDebugOptions.str(); }
  74. virtual const char * queryUserId() { return userName.str(); }
  75. virtual const char * queryPassword() { return password.str(); }
  76. virtual const char * queryJobName() { return jobName.str(); }
  77. virtual const char * queryEcl() { return ecl.str(); }
  78. virtual const char * queryAppName() { return appName.str(); }
  79. virtual const char * queryClusterName() { return clusterName.str(); }
  80. virtual const char * queryDefaultStyleName() { return defaultStyleName.str(); }
  81. virtual const char * queryModuleName() { return moduleName.str(); }
  82. };
  83. class CRoxieQueryCompiler : public CInterface, implements IRoxieQueryCompiler
  84. {
  85. private:
  86. SocketEndpoint ep;
  87. void processCompilationErrors(IConstWorkUnit *workunit, SCMStringBuffer &errors)
  88. {
  89. if (workunit->getExceptionCount())
  90. {
  91. Owned<IConstWUExceptionIterator> exceptions = &workunit->getExceptions();
  92. StringArray errorStr;
  93. StringArray warnStr;
  94. ForEach (*exceptions)
  95. {
  96. IConstWUException &exception = exceptions->query();
  97. unsigned exceptCode = exception.getExceptionCode();
  98. SCMStringBuffer name;
  99. exception.getExceptionFileName(name);
  100. unsigned lineNo = exception.getExceptionLineNo();
  101. unsigned column = exception.getExceptionColumn();
  102. SCMStringBuffer source, message;
  103. exception.getExceptionSource(source);
  104. exception.getExceptionMessage(message);
  105. if (exception.getSeverity() == ExceptionSeverityWarning)
  106. {
  107. StringBuffer err;
  108. if ( (lineNo != 0) || (column != 0))
  109. err.appendf("WARNING: %s (%d, %d): %d %s", name.str(), lineNo, column, exceptCode, message.str());
  110. else
  111. err.appendf("WARNING: %s: %d %s", name.str(), exceptCode, message.str());
  112. warnStr.append(err.str());
  113. }
  114. else if (exception.getSeverity() == ExceptionSeverityError)
  115. {
  116. StringBuffer err;
  117. if ( (lineNo != 0) || (column != 0))
  118. err.appendf("ERROR: %s (%d, %d): %d %s", name.str(), lineNo, column, exceptCode, message.str());
  119. else
  120. err.appendf("ERROR: %s: %d %s", name.str(), exceptCode, message.str());
  121. errorStr.append(err.str());
  122. }
  123. }
  124. ForEachItemIn(err_idx, errorStr)
  125. {
  126. const char *err = errorStr.item(err_idx);
  127. errors.s.appendf("%s\n", err);
  128. }
  129. ForEachItemIn(warn_idx, warnStr)
  130. {
  131. const char *err = warnStr.item(warn_idx);
  132. errors.s.appendf("%s\n", err);
  133. }
  134. errors.s.appendf("%d error(s), %d warning(s)\n", errorStr.ordinality(), warnStr.ordinality());
  135. }
  136. }
  137. public:
  138. IMPLEMENT_IINTERFACE;
  139. CRoxieQueryCompiler()
  140. {
  141. }
  142. IConstWorkUnit *createWorkunit(SCMStringBuffer &wuid, const char *userName, const char *queryAppName)
  143. {
  144. Owned<IWorkUnitFactory> factory = getWorkUnitFactory();
  145. Owned<IWorkUnit> workunit = factory->createWorkUnit(NULL, queryAppName, userName);
  146. workunit->getWuid(wuid);
  147. workunit->setState(WUStateUnknown);
  148. workunit->setUser(userName);
  149. return workunit.getClear();
  150. }
  151. IConstWorkUnit *compileEcl(SCMStringBuffer &wuid, const char *userName, const char *password, IRoxieQueryCompileInfo &compileInfo, IRoxieQueryProcessingInfo &processingInfo, SCMStringBuffer &status)
  152. {
  153. Owned<IWorkUnitFactory> factory = getWorkUnitFactory();
  154. {
  155. Owned<IWorkUnit> workunit;
  156. if (wuid.length())
  157. workunit.setown(factory->updateWorkUnit(wuid.str()));
  158. else
  159. workunit.setown(factory->createWorkUnit(NULL, compileInfo.queryAppName(), userName));
  160. workunit->setDebugValue("RoxieConfigStatus", "Compiling...", true);
  161. Owned<IWUQuery> query = workunit->updateQuery();
  162. query->setQueryText(compileInfo.queryEcl());
  163. query->setQueryType(QueryTypeEcl);
  164. const char *queryJobName = compileInfo.queryJobName();
  165. if (queryJobName && *queryJobName)
  166. workunit->setJobName(queryJobName);
  167. workunit->setAction(WUActionCompile);
  168. workunit->setUser(userName);
  169. workunit->getWuid(wuid);
  170. // set generic debug options
  171. StringBuffer options(compileInfo.queryWuDebugOptions());
  172. if (options.length())
  173. {
  174. Owned<IPropertyTree> dbgOptions = createPTreeFromXMLString(options, ipt_caseInsensitive);
  175. Owned<IPropertyTreeIterator> iter = dbgOptions->getElements("Option");
  176. ForEach(*iter)
  177. {
  178. IPropertyTree &item = iter->query();
  179. const char *name = item.queryProp("@name");
  180. const char *value = item.queryProp("@value");
  181. workunit->setDebugValue(name, value, true);
  182. }
  183. }
  184. // set specific debug options
  185. workunit->setDebugValueInt("forceRoxie", 1, true);
  186. workunit->setDebugValueInt("traceRowXml", 1, true);
  187. workunit->setDebugValue("targetClusterType", "roxie", true);
  188. workunit->setClusterName(compileInfo.queryClusterName()); // MORE - eclserver should not require cluster set for roxy compiles (probably)
  189. if (compileInfo.getHighPriority())
  190. workunit->setPriority(PriorityClassHigh);
  191. int queryPriority = compileInfo.getQueryPriority();
  192. if (queryPriority != UNKNOWN_PRIORITY) // MORE - if set use queryPriority to set wu priority
  193. {
  194. workunit->setDebugValueInt("queryPriority", queryPriority, true);
  195. if (queryPriority == LOW_PRIORITY)
  196. workunit->setPriority(PriorityClassLow);
  197. else
  198. workunit->setPriority(PriorityClassHigh);
  199. }
  200. else
  201. {
  202. if (!compileInfo.getHighPriority())
  203. workunit->setDebugValue("queryPriority", "High", true);
  204. }
  205. workunit->setDebugValueInt("memoryLimit", compileInfo.getMemoryLimit(), true);
  206. workunit->setDebugValueInt("timeLimit", compileInfo.getTimeLimit(), true);
  207. workunit->setDebugValueInt("warnTimeLimit", compileInfo.getWarnTimeLimit(), true);
  208. workunit->setDebugValueInt("poolGraphs", compileInfo.getPoolGraphs(), true);
  209. workunit->setDebugValue("comment", processingInfo.queryComment(), true);
  210. workunit->setDebugValue("package", processingInfo.queryPackageName(), true);
  211. const char *label = compileInfo.queryRepositoryLabel();
  212. if (label && *label)
  213. workunit->setSnapshot(label);
  214. }
  215. submitWorkUnit(wuid.str(), userName, password);
  216. if (waitForWorkUnitToCompile(wuid.str(), compileInfo.getWuTimeOut()))
  217. {
  218. IConstWorkUnit *workunit = factory->openWorkUnit(wuid.str(), false);
  219. Owned<IWorkUnit> wu = &workunit->lock();
  220. SCMStringBuffer jn;
  221. wu->getJobName(jn);
  222. if (jn.length() == 0)
  223. {
  224. SCMStringBuffer name;
  225. wu->getDebugValue("name", name);
  226. if (name.length())
  227. wu->setJobName(name.str());
  228. else
  229. wu->setJobName(wuid.str());
  230. }
  231. processCompilationErrors(workunit, status);
  232. return workunit;
  233. }
  234. else
  235. {
  236. Owned<IConstWorkUnit> workunit = factory->openWorkUnit(wuid.str(), false);
  237. WUState wu_state = workunit->getState();
  238. // there was a problem, just report it - either compilation problems, eclserver timeout, etc
  239. processCompilationErrors(workunit, status);
  240. if (!status.length()) // no compilations errors so it must be something else
  241. {
  242. //StringBuffer errStr;
  243. if (wu_state == WUStateCompiling)
  244. status.s.append("Time out trying to compile query - need to increase the wuTimeOut");
  245. else
  246. {
  247. SCMStringBuffer state;
  248. workunit->getStateDesc(state);
  249. status.s.appendf("Unknown error: Could not compile query - make sure eclserver is running - workunit status = %s", state.str());
  250. }
  251. }
  252. return NULL;
  253. }
  254. }
  255. };
  256. IRoxieQueryCompileInfo *createRoxieQueryCompileInfo(const char *_ecl, const char *_jobName, const char *_clusterName, const char *_appName)
  257. {
  258. return new CRoxieQueryCompileInfo(_ecl, _jobName, _clusterName, _appName);
  259. }
  260. IRoxieQueryCompiler *createRoxieQueryCompiler()
  261. {
  262. return new CRoxieQueryCompiler();
  263. }