ws_smcService.cpp 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #pragma warning (disable : 4786)
  14. #include "build-config.h"
  15. #ifdef _USE_OPENLDAP
  16. #include "ldapsecurity.ipp"
  17. #endif
  18. #include "ws_smcService.hpp"
  19. #include "wshelpers.hpp"
  20. #include "dalienv.hpp"
  21. #include "WUWrapper.hpp"
  22. #include "wujobq.hpp"
  23. #include "dfuwu.hpp"
  24. #include "exception_util.hpp"
  25. #include "roxiecontrol.hpp"
  26. #include "workunit.hpp"
  27. #define STATUS_SERVER_THOR "ThorMaster"
  28. #define STATUS_SERVER_HTHOR "HThorServer"
  29. #define STATUS_SERVER_ROXIE "RoxieServer"
  30. #define STATUS_SERVER_DFUSERVER "DFUserver"
  31. #define STATUS_SERVER_ECLSERVER "ECLserver"
  32. #define STATUS_SERVER_ECLCCSERVER "ECLCCserver"
  33. #define STATUS_SERVER_ECLAGENT "ECLagent"
  34. #define CLUSTER_TYPE_THOR "Thor"
  35. #define CLUSTER_TYPE_HTHOR "HThor"
  36. #define CLUSTER_TYPE_ROXIE "Roxie"
  37. static const char* FEATURE_URL = "SmcAccess";
  38. const char* THORQUEUE_FEATURE = "ThorQueueAccess";
  39. static const char* ROXIE_CONTROL_URL = "RoxieControlAccess";
  40. static const char* OWN_WU_ACCESS = "OwnWorkunitsAccess";
  41. static const char* OTHERS_WU_ACCESS = "OthersWorkunitsAccess";
  42. const char* PERMISSIONS_FILENAME = "espsmc_permissions.xml";
  43. const unsigned DEFAULTACTIVITYINFOCACHETIMEOUTSECOND = 10;
  44. void AccessSuccess(IEspContext& context, char const * msg,...) __attribute__((format(printf, 2, 3)));
  45. void AccessSuccess(IEspContext& context, char const * msg,...)
  46. {
  47. StringBuffer buf;
  48. buf.appendf("User %s: ",context.queryUserId());
  49. va_list args;
  50. va_start(args, msg);
  51. buf.valist_appendf(msg, args);
  52. va_end(args);
  53. AUDIT(AUDIT_TYPE_ACCESS_SUCCESS,buf.str());
  54. }
  55. void AccessFailure(IEspContext& context, char const * msg,...) __attribute__((format(printf, 2, 3)));
  56. void AccessFailure(IEspContext& context, char const * msg,...)
  57. {
  58. StringBuffer buf;
  59. buf.appendf("User %s: ",context.queryUserId());
  60. va_list args;
  61. va_start(args, msg);
  62. buf.valist_appendf(msg, args);
  63. va_end(args);
  64. AUDIT(AUDIT_TYPE_ACCESS_FAILURE,buf.str());
  65. }
  66. struct QueueWrapper
  67. {
  68. QueueWrapper(const char* targetName, const char* queueExt)
  69. {
  70. StringBuffer name;
  71. name.append(targetName).append('.').append(queueExt);
  72. queue.setown(createJobQueue(name.str()));
  73. }
  74. QueueWrapper(const char* queueName)
  75. {
  76. queue.setown(createJobQueue(queueName));
  77. }
  78. operator IJobQueue*() { return queue.get(); }
  79. IJobQueue* operator->() { return queue.get(); }
  80. Owned<IJobQueue> queue;
  81. };
  82. struct QueueLock
  83. {
  84. QueueLock(IJobQueue* q): queue(q) { queue->lock(); }
  85. ~QueueLock()
  86. {
  87. queue->unlock();
  88. }
  89. Linked<IJobQueue> queue;
  90. };
  91. static int sortTargetClustersByNameDescending(IInterface **L, IInterface **R)
  92. {
  93. IEspTargetCluster *left = (IEspTargetCluster *) *L;
  94. IEspTargetCluster *right = (IEspTargetCluster *) *R;
  95. return strcmp(right->getClusterName(), left->getClusterName());
  96. }
  97. static int sortTargetClustersByNameAscending(IInterface **L, IInterface **R)
  98. {
  99. IEspTargetCluster *left = (IEspTargetCluster *) *L;
  100. IEspTargetCluster *right = (IEspTargetCluster *) *R;
  101. return strcmp(left->getClusterName(), right->getClusterName());
  102. }
  103. static int sortTargetClustersBySizeDescending(IInterface **L, IInterface **R)
  104. {
  105. IEspTargetCluster *left = (IEspTargetCluster *) *L;
  106. IEspTargetCluster *right = (IEspTargetCluster *) *R;
  107. return right->getClusterSize() - left->getClusterSize();
  108. }
  109. static int sortTargetClustersBySizeAscending(IInterface **L, IInterface **R)
  110. {
  111. IEspTargetCluster *left = (IEspTargetCluster *) *L;
  112. IEspTargetCluster *right = (IEspTargetCluster *) *R;
  113. return left->getClusterSize() - right->getClusterSize();
  114. }
  115. void CWsSMCEx::init(IPropertyTree *cfg, const char *process, const char *service)
  116. {
  117. if (!daliClientActive())
  118. {
  119. ERRLOG("No Dali Connection Active.");
  120. throw MakeStringException(-1, "No Dali Connection Active. Please Specify a Dali to connect to in you configuration file");
  121. }
  122. m_BannerAction = 0;
  123. m_EnableChatURL = false;
  124. m_BannerSize = "4";
  125. m_BannerColor = "red";
  126. m_BannerScroll = "2";
  127. StringBuffer xpath;
  128. xpath.appendf("Software/EspProcess[@name='%s']/@portalurl", process);
  129. const char* portalURL = cfg->queryProp(xpath.str());
  130. if (portalURL && *portalURL)
  131. m_PortalURL.append(portalURL);
  132. xpath.setf("Software/EspProcess[@name=\"%s\"]/EspService[@name=\"%s\"]/ActivityInfoCacheSeconds", process, service);
  133. activityInfoCacheSeconds = cfg->getPropInt(xpath.str(), DEFAULTACTIVITYINFOCACHETIMEOUTSECOND);
  134. }
  135. static void countProgress(IPropertyTree *t,unsigned &done,unsigned &total)
  136. {
  137. total = 0;
  138. done = 0;
  139. Owned<IPropertyTreeIterator> it = t->getElements("DFT/progress");
  140. ForEach(*it) {
  141. IPropertyTree &e=it->query();
  142. if (e.getPropInt("@done",0))
  143. done++;
  144. total++;
  145. }
  146. }
  147. struct CActiveWorkunitWrapper: public CActiveWorkunit
  148. {
  149. CActiveWorkunitWrapper(IEspContext &context, const char* wuid,const char* location = NULL,unsigned index=0): CActiveWorkunit("","")
  150. {
  151. CWUWrapper wu(wuid, context);
  152. setActiveWorkunit(wu, wuid, location, index, context.getClientVersion(), false);
  153. }
  154. CActiveWorkunitWrapper(const char* wuid, const char* location = NULL, unsigned index=0): CActiveWorkunit("","")
  155. {
  156. CWUWrapper wu(wuid);
  157. setActiveWorkunit(wu, wuid, location, index, 0.0, true);
  158. }
  159. CActiveWorkunitWrapper(const char* wuid,const char* owner, const char* jobname, const char* state, const char* priority): CActiveWorkunit("","")
  160. {
  161. setWuid(wuid);
  162. setState(state);
  163. setOwner(owner);
  164. setJobname(jobname);
  165. setPriority(priority);
  166. }
  167. void setActiveWorkunit(CWUWrapper& wu, const char* wuid, const char* location, unsigned index, double version, bool notCheckVersion)
  168. {
  169. StringBuffer stateStr;
  170. SCMStringBuffer state, stateEx, owner, jobname;
  171. setWuid(wuid);
  172. wu->getStateDesc(state);
  173. setStateID(wu->getState());
  174. if (wu->getState() == WUStateBlocked)
  175. {
  176. wu->getStateEx(stateEx);
  177. if (notCheckVersion || (version > 1.00))
  178. setExtra(stateEx.str());
  179. }
  180. buildAndSetState(state.str(), stateEx.str(), location, index);
  181. if ((notCheckVersion || (version > 1.09)) && (wu->getState() == WUStateFailed))
  182. setWarning("The job will ultimately not complete. Please check ECLAgent.");
  183. setOwner(wu->getUser(owner).str());
  184. setJobname(wu->getJobName(jobname).str());
  185. setPriorityStr(wu->getPriority());
  186. if ((notCheckVersion || (version > 1.08)) && wu->isPausing())
  187. {
  188. setIsPausing(true);
  189. }
  190. if (notCheckVersion || (version > 1.14))
  191. {
  192. SCMStringBuffer clusterName;
  193. setClusterName(wu->getClusterName(clusterName).str());
  194. }
  195. }
  196. void buildAndSetState(const char* state, const char* stateEx, const char* location, unsigned index)
  197. {
  198. if (!state || !*state)
  199. return;
  200. StringBuffer stateStr;
  201. if(index && location && *location)
  202. stateStr.setf("queued(%d) [%s on %s]", index, state, location);
  203. else if(index)
  204. stateStr.setf("queued(%d) [%s]", index, state);
  205. else if(location && *location)
  206. stateStr.setf("%s [%s]", state, location);
  207. else
  208. stateStr.set(state);
  209. if (stateEx && *stateEx)
  210. stateStr.appendf(" %s", stateEx);
  211. setState(stateStr.str());
  212. }
  213. void setPriorityStr(unsigned priorityType)
  214. {
  215. switch(priorityType)
  216. {
  217. case PriorityClassHigh: setPriority("high"); break;
  218. default:
  219. case PriorityClassNormal: setPriority("normal"); break;
  220. case PriorityClassLow: setPriority("low"); break;
  221. }
  222. return;
  223. }
  224. };
  225. bool CWsSMCEx::onIndex(IEspContext &context, IEspSMCIndexRequest &req, IEspSMCIndexResponse &resp)
  226. {
  227. resp.setRedirectUrl("/");
  228. return true;
  229. }
  230. static int stringcmp(const char **a, const char **b)
  231. {
  232. return strcmp(*a, *b);
  233. }
  234. bool isInWuList(IArrayOf<IEspActiveWorkunit>& aws, const char* wuid)
  235. {
  236. bool bFound = false;
  237. if (wuid && *wuid && (aws.length() > 0))
  238. {
  239. ForEachItemIn(k, aws)
  240. {
  241. IEspActiveWorkunit& wu = aws.item(k);
  242. const char* wuid0 = wu.getWuid();
  243. const char* server0 = wu.getServer();
  244. if (wuid0 && !strcmp(wuid0, wuid) && (!server0 || strcmp(server0, "ECLagent")))
  245. {
  246. bFound = true;
  247. break;
  248. }
  249. }
  250. }
  251. return bFound;
  252. }
  253. void CWsSMCEx::readBannerAndChatRequest(IEspContext& context, IEspActivityRequest &req, IEspActivityResponse& resp)
  254. {
  255. StringBuffer chatURLStr, bannerStr;
  256. const char* chatURL = req.getChatURL();
  257. const char* banner = req.getBannerContent();
  258. //Filter out invalid chars
  259. if (chatURL && *chatURL)
  260. {
  261. const char* pStr = chatURL;
  262. unsigned len = strlen(chatURL);
  263. for (unsigned i = 0; i < len; i++)
  264. {
  265. if (isprint(*pStr))
  266. chatURLStr.append(*pStr);
  267. pStr++;
  268. }
  269. }
  270. if (banner && *banner)
  271. {
  272. const char* pStr = banner;
  273. unsigned len = strlen(banner);
  274. for (unsigned i = 0; i < len; i++)
  275. {
  276. bannerStr.append(isprint(*pStr) ? *pStr : '.');
  277. pStr++;
  278. }
  279. }
  280. chatURLStr.trim();
  281. bannerStr.trim();
  282. if (!req.getBannerAction_isNull() && req.getBannerAction() && (bannerStr.length() < 1))
  283. throw MakeStringException(ECLWATCH_MISSING_BANNER_CONTENT, "If a Banner is enabled, the Banner content must be specified.");
  284. if (!req.getEnableChatURL_isNull() && req.getEnableChatURL() && (chatURLStr.length() < 1))
  285. throw MakeStringException(ECLWATCH_MISSING_CHAT_URL, "If a Chat is enabled, the Chat URL must be specified.");
  286. //Now, store the strings since they are valid.
  287. m_ChatURL = chatURLStr;
  288. m_Banner = bannerStr;
  289. const char* bannerSize = req.getBannerSize();
  290. if (bannerSize && *bannerSize)
  291. m_BannerSize.set(bannerSize);
  292. const char* bannerColor = req.getBannerColor();
  293. if (bannerColor && *bannerColor)
  294. m_BannerColor.set(bannerColor);
  295. const char* bannerScroll = req.getBannerScroll();
  296. if (bannerScroll && *bannerScroll)
  297. m_BannerScroll.set(bannerScroll);
  298. m_BannerAction = req.getBannerAction();
  299. if(!req.getEnableChatURL_isNull())
  300. m_EnableChatURL = req.getEnableChatURL();
  301. }
  302. void CWsSMCEx::setBannerAndChatData(double version, IEspActivityResponse& resp)
  303. {
  304. resp.setShowBanner(m_BannerAction);
  305. resp.setShowChatURL(m_EnableChatURL);
  306. resp.setBannerContent(m_Banner.str());
  307. resp.setBannerSize(m_BannerSize.str());
  308. resp.setBannerColor(m_BannerColor.str());
  309. resp.setChatURL(m_ChatURL.str());
  310. if (version >= 1.08)
  311. resp.setBannerScroll(m_BannerScroll.str());
  312. }
  313. void CWsSMCEx::createActiveWorkUnit(Owned<IEspActiveWorkunit>& ownedWU, IEspContext &context, const char* wuid, const char* location,
  314. unsigned index, const char* serverName, const char* queueName, const char* instanceName, const char* targetClusterName, bool useContext)
  315. {
  316. try
  317. {
  318. if (useContext)
  319. ownedWU.setown(new CActiveWorkunitWrapper(context, wuid, location, index));
  320. else
  321. ownedWU.setown(new CActiveWorkunitWrapper(wuid, location, index));
  322. }
  323. catch (IException *e)
  324. { //if the wu cannot be opened for some reason, the openWorkUnit() inside the CActiveWorkunitWrapper() may throw an exception.
  325. //We do not want the exception stops this process of retrieving/showing all active WUs. And that WU should still be displayed
  326. //with the exception.
  327. StringBuffer msg;
  328. ownedWU.setown(new CActiveWorkunitWrapper(wuid, "", "", e->errorMessage(msg).str(), "normal"));
  329. ownedWU->setStateID(WUStateUnknown);
  330. e->Release();
  331. }
  332. ownedWU->setServer(serverName);
  333. ownedWU->setQueueName(queueName);
  334. if (instanceName && *instanceName)
  335. ownedWU->setInstance(instanceName); // JCSMORE In thor case at least, if queued it is unknown which instance it will run on..
  336. if (targetClusterName && *targetClusterName)
  337. ownedWU->setTargetClusterName(targetClusterName);
  338. }
  339. void CWsSMCEx::readWUsAndStateFromJobQueue(IEspContext& context, CWsSMCTargetCluster& targetCluster,
  340. CWsSMCQueue& jobQueue, const char* queueName, BoolHash& uniqueWUIDs, IArrayOf<IEspActiveWorkunit>& aws)
  341. {
  342. CJobQueueContents contents;
  343. Owned<IJobQueue> queue = createJobQueue(jobQueue.queueName.str());
  344. queue->copyItemsAndState(contents, jobQueue.queueState, jobQueue.queueStateDetails);
  345. Owned<IJobQueueIterator> iter = contents.getIterator();
  346. jobQueue.countQueuedJobs=0;
  347. ForEach(*iter)
  348. {
  349. const char* wuid = iter->query().queryWUID();
  350. if (!wuid || !*wuid || uniqueWUIDs.getValue(wuid))
  351. continue;
  352. uniqueWUIDs.setValue(wuid, true);
  353. const char* queue = NULL;
  354. if (queueName && *queueName)
  355. queue = queueName;
  356. else
  357. queue = targetCluster.clusterName.get();
  358. Owned<IEspActiveWorkunit> wu;
  359. createActiveWorkUnit(wu, context, wuid, jobQueue.queueName.str(), ++jobQueue.countQueuedJobs, targetCluster.statusServerName.str(),
  360. queue, NULL, targetCluster.clusterName.get(), false);
  361. aws.append(*wu.getLink());
  362. }
  363. }
  364. bool CWsSMCEx::findQueueInStatusServer(IEspContext& context, IPropertyTree* serverStatusRoot, const char* serverName, const char* queueName)
  365. {
  366. bool foundServer = false;
  367. VStringBuffer path("Server[@name=\"%s\"]", serverName);
  368. Owned<IPropertyTreeIterator> it(serverStatusRoot->getElements(path.str()));
  369. ForEach(*it)
  370. {
  371. IPropertyTree& serverStatusNode = it->query();
  372. const char* queue = serverStatusNode.queryProp("@queue");
  373. if (!queue || !*queue)
  374. continue;
  375. StringArray qlist;
  376. qlist.appendListUniq(queue, ",");
  377. ForEachItemIn(q, qlist)
  378. {
  379. if (strieq(qlist.item(q), queueName))
  380. {
  381. foundServer = true;
  382. break;
  383. }
  384. }
  385. if (foundServer)
  386. break;
  387. }
  388. return foundServer;
  389. }
  390. void CWsSMCEx::sortTargetClusters(IArrayOf<IEspTargetCluster>& clusters, const char* sortBy, bool descending)
  391. {
  392. if (!sortBy || !*sortBy || strieq(sortBy, "name"))
  393. clusters.sort(descending ? sortTargetClustersByNameDescending : sortTargetClustersByNameAscending);
  394. else
  395. clusters.sort(descending ? sortTargetClustersBySizeDescending : sortTargetClustersBySizeAscending);
  396. }
  397. void CWsSMCEx::setClusterQueueStatus(CWsSMCTargetCluster& targetCluster)
  398. {
  399. CWsSMCQueue& jobQueue = targetCluster.clusterQueue;
  400. if (targetCluster.clusterType != ThorLCRCluster)
  401. jobQueue = targetCluster.agentQueue;
  402. if (!jobQueue.queueName.length())
  403. return;
  404. targetCluster.clusterStatusDetails.appendf("%s: ", jobQueue.queueName.str());
  405. bool queuePausedOrStopped = false;
  406. unsigned countRunningJobs = jobQueue.countRunningJobs;
  407. unsigned countQueuedJobs = jobQueue.countQueuedJobs;
  408. if (targetCluster.clusterType == ThorLCRCluster)
  409. {
  410. countRunningJobs += targetCluster.agentQueue.countRunningJobs;
  411. countQueuedJobs += targetCluster.agentQueue.countQueuedJobs;
  412. }
  413. if (jobQueue.queueState.length())
  414. {
  415. const char* queueState = jobQueue.queueState.str();
  416. const char* queueStateDetails = jobQueue.queueStateDetails.str();
  417. if (queueStateDetails && *queueStateDetails)
  418. targetCluster.clusterStatusDetails.appendf("queue %s; %s;", queueState, queueStateDetails);
  419. else
  420. targetCluster.clusterStatusDetails.appendf("queue %s; ", queueState);
  421. if (strieq(queueState,"stopped") || strieq(queueState,"paused"))
  422. queuePausedOrStopped = true;
  423. }
  424. if (!jobQueue.foundQueueInStatusServer)
  425. {
  426. if (queuePausedOrStopped)
  427. jobQueue.statusType = QueuePausedOrStoppedNotFound;
  428. else
  429. jobQueue.statusType = QueueRunningNotFound;
  430. }
  431. else
  432. {
  433. if (queuePausedOrStopped)
  434. {
  435. if (jobQueue.countRunningJobs > 0)
  436. jobQueue.statusType = QueuePausedOrStoppedWithJobs;
  437. else
  438. jobQueue.statusType = QueuePausedOrStoppedWithNoJob;
  439. }
  440. }
  441. }
  442. void CWsSMCEx::setClusterStatus(IEspContext& context, CWsSMCTargetCluster& targetCluster, IEspTargetCluster* returnCluster)
  443. {
  444. setClusterQueueStatus(targetCluster);
  445. int statusType = (targetCluster.clusterQueue.statusType > targetCluster.agentQueue.statusType) ? targetCluster.clusterQueue.statusType
  446. : targetCluster.agentQueue.statusType;
  447. returnCluster->setClusterStatus(statusType);
  448. //Set 'Warning' which may be displayed beside cluster name
  449. if (statusType == QueueRunningNotFound)
  450. returnCluster->setWarning("Cluster not attached");
  451. else if (statusType == QueuePausedOrStoppedNotFound)
  452. returnCluster->setWarning("Queue paused or stopped - Cluster not attached");
  453. else if (statusType != RunningNormal)
  454. returnCluster->setWarning("Queue paused or stopped");
  455. //Set 'StatusDetails' which may be displayed when a mouse is moved over cluster icon
  456. if (targetCluster.clusterStatusDetails.length())
  457. returnCluster->setStatusDetails(targetCluster.clusterStatusDetails.str());
  458. }
  459. void CWsSMCEx::getWUsNotOnTargetCluster(IEspContext &context, IPropertyTree* serverStatusRoot, IArrayOf<IEspServerJobQueue>& serverJobQueues,
  460. IArrayOf<IEspActiveWorkunit>& aws)
  461. {
  462. BoolHash uniqueServers;
  463. Owned<IPropertyTreeIterator> it(serverStatusRoot->getElements("Server"));
  464. ForEach(*it)
  465. {
  466. IPropertyTree& serverNode = it->query();
  467. const char* serverName = serverNode.queryProp("@name");
  468. const char* instance = serverNode.queryProp("@node");
  469. const char* queueName = serverNode.queryProp("@queue");
  470. unsigned port = serverNode.getPropInt("@mpport", 0);
  471. if (!serverName || !*serverName || !instance || !*instance || strieq(serverName, STATUS_SERVER_DFUSERVER) ||//DFUServer already handled separately
  472. strieq(serverName, "ThorMaster") || strieq(serverName, "RoxieServer") || strieq(serverName, "HThorServer"))//target clusters already handled separately
  473. continue;
  474. VStringBuffer instanceName("%s_on_%s:%d", serverName, instance, port);
  475. Owned<IPropertyTreeIterator> wuids(serverNode.getElements("WorkUnit"));
  476. ForEach(*wuids)
  477. {
  478. const char* wuid=wuids->query().queryProp(NULL);
  479. if (!wuid || !*wuid)
  480. continue;
  481. if (isInWuList(aws, wuid))
  482. continue;
  483. Owned<IEspActiveWorkunit> wu;
  484. createActiveWorkUnit(wu, context, wuid, NULL, 0, serverName, queueName, instanceName.str(), NULL, false);
  485. aws.append(*wu.getLink());
  486. }
  487. if (!uniqueServers.getValue(instanceName))
  488. {
  489. uniqueServers.setValue(instanceName, true);
  490. addServerJobQueue(serverJobQueues, queueName, instanceName, serverName, instance, port);
  491. }
  492. }
  493. return;
  494. }
  495. void CWsSMCEx::readDFUWUs(IEspContext &context, const char* queueName, const char* serverName, IArrayOf<IEspActiveWorkunit>& aws)
  496. {
  497. StringAttrArray wulist;
  498. unsigned running = queuedJobs(queueName, wulist);
  499. ForEachItemIn(i, wulist)
  500. {
  501. StringBuffer jname, uname, state, error;
  502. const char *wuid = wulist.item(i).text.get();
  503. if (i<running)
  504. state.set("running");
  505. else
  506. state.set("queued");
  507. try
  508. {
  509. Owned<IConstDFUWorkUnit> dfuwu = getDFUWorkUnitFactory()->openWorkUnit(wuid, false);
  510. dfuwu->getUser(uname);
  511. dfuwu->getJobName(jname);
  512. }
  513. catch (IException *e)
  514. {
  515. e->errorMessage(error);
  516. state.appendf(" (%s)", error.str());
  517. e->Release();
  518. }
  519. Owned<IEspActiveWorkunit> wu(new CActiveWorkunitWrapper(wuid, uname.str(), jname.str(), state.str(), "normal"));
  520. wu->setServer(STATUS_SERVER_DFUSERVER);
  521. wu->setInstance(serverName);
  522. wu->setQueueName(queueName);
  523. aws.append(*wu.getLink());
  524. }
  525. }
  526. void CWsSMCEx::getDFUServersAndWUs(IEspContext &context, IPropertyTree* envRoot, IArrayOf<IEspServerJobQueue>& serverJobQueues, IArrayOf<IEspActiveWorkunit>& aws)
  527. {
  528. if (!envRoot)
  529. return;
  530. VStringBuffer path("Software/%s", eqDfu);
  531. Owned<IPropertyTreeIterator> services = envRoot->getElements(path);
  532. ForEach(*services)
  533. {
  534. IPropertyTree &serviceTree = services->query();
  535. const char *qname = serviceTree.queryProp("@queue");
  536. const char *serverName = serviceTree.queryProp("@name");
  537. if (!qname || !*qname)
  538. continue;
  539. StringArray queues;
  540. queues.appendListUniq(qname, ",");
  541. ForEachItemIn(q, queues)
  542. {
  543. const char *queueName = queues.item(q);
  544. readDFUWUs(context, queueName, serverName, aws);
  545. addServerJobQueue(serverJobQueues, queueName, serverName, STATUS_SERVER_DFUSERVER, NULL, 0);
  546. }
  547. }
  548. }
  549. void CWsSMCEx::getDFURecoveryJobs(IEspContext &context, const IPropertyTree* dfuRecoveryRoot, IArrayOf<IEspDFUJob>& jobs)
  550. {
  551. if (!dfuRecoveryRoot)
  552. return;
  553. Owned<IPropertyTreeIterator> it(dfuRecoveryRoot->getElements("job"));
  554. ForEach(*it)
  555. {
  556. IPropertyTree &e=it->query();
  557. if (!e.getPropBool("Running",false))
  558. continue;
  559. StringBuffer cmd;
  560. unsigned done, total;
  561. countProgress(&e,done,total);
  562. cmd.append(e.queryProp("@command")).append(" ").append(e.queryProp("@command_parameters"));
  563. Owned<IEspDFUJob> job = new CDFUJob("","");
  564. job->setTimeStarted(e.queryProp("@time_started"));
  565. job->setDone(done);
  566. job->setTotal(total);
  567. job->setCommand(cmd.str());
  568. jobs.append(*job.getLink());
  569. }
  570. }
  571. bool ActivityInfo::isCachedActivityInfoValid(unsigned timeOutSeconds)
  572. {
  573. CDateTime timeNow;
  574. timeNow.setNow();
  575. return timeNow.getSimple() <= timeCached.getSimple() + timeOutSeconds;;
  576. }
  577. void CWsSMCEx::clearActivityInfoCache()
  578. {
  579. CriticalBlock b(getActivityCrit);
  580. activityInfoCache.clear();
  581. }
  582. ActivityInfo* CWsSMCEx::getActivityInfo(IEspContext &context)
  583. {
  584. CriticalBlock b(getActivityCrit);
  585. if (activityInfoCache && activityInfoCache->isCachedActivityInfoValid(activityInfoCacheSeconds))
  586. return activityInfoCache.getLink();
  587. DBGLOG("CWsSMCEx::getActivityInfo - rebuild cached information");
  588. activityInfoCache.setown(createActivityInfo(context));
  589. return activityInfoCache.getLink();
  590. }
  591. ActivityInfo* CWsSMCEx::createActivityInfo(IEspContext &context)
  592. {
  593. Owned<IEnvironmentFactory> factory = getEnvironmentFactory();
  594. Owned<IConstEnvironment> env = factory->openEnvironment();
  595. if (!env)
  596. throw MakeStringException(ECLWATCH_CANNOT_GET_ENV_INFO,"Failed to get environment information.");
  597. CConstWUClusterInfoArray clusters;
  598. Owned<IPropertyTree> envRoot= &env->getPTree();
  599. getEnvironmentClusterInfo(envRoot, clusters);
  600. Owned<IRemoteConnection> connStatusServers = querySDS().connect("/Status/Servers",myProcessSession(),RTM_LOCK_READ,30000);
  601. IPropertyTree* serverStatusRoot = connStatusServers->queryRoot();
  602. if (!serverStatusRoot)
  603. throw MakeStringException(ECLWATCH_CANNOT_GET_STATUS_INFO, "Failed to get status server information.");
  604. IPropertyTree* dfuRecoveryRoot = NULL;
  605. Owned<IRemoteConnection> connDFURecovery = querySDS().connect("DFU/RECOVERY",myProcessSession(), RTM_LOCK_READ, 30000);
  606. if (connDFURecovery)
  607. dfuRecoveryRoot = connDFURecovery->queryRoot();
  608. Owned<ActivityInfo> activityInfo = new ActivityInfo();
  609. readTargetClusterInfo(context, clusters, serverStatusRoot, activityInfo);
  610. readRunningWUsAndQueuedWUs(context, envRoot, serverStatusRoot, dfuRecoveryRoot, activityInfo);
  611. return activityInfo.getClear();
  612. }
  613. // This method reads job information from both /Status/Servers and IJobQueue.
  614. //
  615. // Each server component (a thor cluster, a dfuserver, or an eclagent) is one 'Server' branch under
  616. // /Status/Servers. A 'Server' branch has a @queue which indicates the queue name of the server.
  617. // A 'Server' branch also contains the information about running WUs on that 'Server'. This
  618. // method reads the information. Those WUs are displays under that server (identified by its queue name)
  619. // on Activity page.
  620. //
  621. // For the WUs list inside /Status/Servers/Server[@name=ECLagent] but not list under other 'Server', the
  622. // existing code has to find out WUID and @clusterName of the WU. Then, uses @clusterName to find out the
  623. // queue name in IConstWUClusterInfo. Those WUs list under that server (identified by its queue name) with
  624. // a note 'on ECLagent'. TBD: the logic here will be simpler if the /Status/Servers/Server is named the
  625. // instance and/or cluster.
  626. //
  627. // In order to get information about queued WUs, this method gets queue names from both IConstWUClusterInfo
  628. // and other environment functions. Each of those queue names is linked to one IJobQueues. From the
  629. // IJobQueues, this method reads queued jobs for each server component and list them under the server
  630. // component (identified by its queue name).
  631. bool CWsSMCEx::onActivity(IEspContext &context, IEspActivityRequest &req, IEspActivityResponse& resp)
  632. {
  633. context.validateFeatureAccess(FEATURE_URL, SecAccess_Read, true);
  634. try
  635. {
  636. const char* build_ver = getBuildVersion();
  637. resp.setBuild(build_ver);
  638. double version = context.getClientVersion();
  639. bool isSuperUser = true;
  640. #ifdef _USE_OPENLDAP
  641. CLdapSecManager* secmgr = dynamic_cast<CLdapSecManager*>(context.querySecManager());
  642. if(secmgr && !secmgr->isSuperUser(context.queryUser()))
  643. isSuperUser = false;
  644. #endif
  645. if(isSuperUser && req.getFromSubmitBtn())
  646. readBannerAndChatRequest(context, req, resp);
  647. if (version >= 1.12)
  648. resp.setSuperUser(isSuperUser);
  649. if (version >= 1.06)
  650. setBannerAndChatData(version, resp);
  651. Owned<ActivityInfo> activityInfo = getActivityInfo(context);
  652. setActivityResponse(context, activityInfo, req, resp);
  653. }
  654. catch(IException* e)
  655. {
  656. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  657. }
  658. return true;
  659. }
  660. const char *CWsSMCEx::getStatusServerTypeName(WsSMCStatusServerType type)
  661. {
  662. return (type < WsSMCSSTterm) ? WsSMCStatusServerTypeNames[type] : NULL;
  663. }
  664. void CWsSMCEx::readTargetClusterInfo(IEspContext &context, CConstWUClusterInfoArray& clusters, IPropertyTree* serverStatusRoot,
  665. ActivityInfo* activityInfo)
  666. {
  667. ForEachItemIn(c, clusters)
  668. {
  669. IConstWUClusterInfo &cluster = clusters.item(c);
  670. Owned<CWsSMCTargetCluster> targetCluster = new CWsSMCTargetCluster();
  671. readTargetClusterInfo(context, cluster, serverStatusRoot, targetCluster);
  672. if (cluster.getPlatform() == ThorLCRCluster)
  673. activityInfo->thorTargetClusters.append(*targetCluster.getClear());
  674. else if (cluster.getPlatform() == RoxieCluster)
  675. activityInfo->roxieTargetClusters.append(*targetCluster.getClear());
  676. else
  677. activityInfo->hthorTargetClusters.append(*targetCluster.getClear());
  678. }
  679. }
  680. void CWsSMCEx::readTargetClusterInfo(IEspContext& context, IConstWUClusterInfo& cluster, IPropertyTree* serverStatusRoot, CWsSMCTargetCluster* targetCluster)
  681. {
  682. SCMStringBuffer clusterName;
  683. cluster.getName(clusterName);
  684. targetCluster->clusterName.set(clusterName.str());
  685. targetCluster->clusterType = cluster.getPlatform();
  686. targetCluster->clusterSize = cluster.getSize();
  687. cluster.getServerQueue(targetCluster->serverQueue.queueName);
  688. cluster.getAgentQueue(targetCluster->agentQueue.queueName);
  689. StringBuffer statusServerName;
  690. CWsSMCQueue* jobQueue = NULL;
  691. if (targetCluster->clusterType == ThorLCRCluster)
  692. {
  693. statusServerName.set(getStatusServerTypeName(WsSMCSSTThorLCRCluster));
  694. jobQueue = &targetCluster->clusterQueue;
  695. cluster.getThorQueue(jobQueue->queueName);
  696. }
  697. else if (targetCluster->clusterType == RoxieCluster)
  698. {
  699. statusServerName.set(getStatusServerTypeName(WsSMCSSTRoxieCluster));
  700. jobQueue = &targetCluster->agentQueue;
  701. }
  702. else
  703. {
  704. statusServerName.set(getStatusServerTypeName(WsSMCSSTHThorCluster));
  705. jobQueue = &targetCluster->agentQueue;
  706. }
  707. targetCluster->statusServerName.set(statusServerName.str());
  708. targetCluster->queueName.set(jobQueue->queueName.str());
  709. if (serverStatusRoot)
  710. {
  711. jobQueue->foundQueueInStatusServer = findQueueInStatusServer(context, serverStatusRoot, statusServerName.str(), targetCluster->queueName.get());
  712. if (!jobQueue->foundQueueInStatusServer)
  713. targetCluster->clusterStatusDetails.appendf("Cluster %s not attached; ", clusterName.str());
  714. }
  715. return;
  716. }
  717. void CWsSMCEx::readRunningWUsAndQueuedWUs(IEspContext &context, IPropertyTree* envRoot, IPropertyTree* serverStatusRoot,
  718. IPropertyTree* dfuRecoveryRoot, ActivityInfo* activityInfo)
  719. {
  720. BoolHash uniqueWUIDs;
  721. readRunningWUsOnStatusServer(context, serverStatusRoot, WsSMCSSTThorLCRCluster, activityInfo->thorTargetClusters, activityInfo->roxieTargetClusters, activityInfo->hthorTargetClusters, uniqueWUIDs, activityInfo->aws);
  722. readWUsAndStateFromJobQueue(context, activityInfo->thorTargetClusters, uniqueWUIDs, activityInfo->aws);
  723. readRunningWUsOnStatusServer(context, serverStatusRoot, WsSMCSSTRoxieCluster, activityInfo->roxieTargetClusters, activityInfo->thorTargetClusters, activityInfo->hthorTargetClusters, uniqueWUIDs, activityInfo->aws);
  724. readWUsAndStateFromJobQueue(context, activityInfo->roxieTargetClusters, uniqueWUIDs, activityInfo->aws);
  725. readRunningWUsOnStatusServer(context, serverStatusRoot, WsSMCSSTHThorCluster, activityInfo->hthorTargetClusters, activityInfo->thorTargetClusters, activityInfo->roxieTargetClusters, uniqueWUIDs, activityInfo->aws);
  726. readWUsAndStateFromJobQueue(context, activityInfo->hthorTargetClusters, uniqueWUIDs, activityInfo->aws);
  727. readRunningWUsOnStatusServer(context, serverStatusRoot, WsSMCSSTECLagent, activityInfo->thorTargetClusters, activityInfo->roxieTargetClusters, activityInfo->hthorTargetClusters, uniqueWUIDs, activityInfo->aws);
  728. getWUsNotOnTargetCluster(context, serverStatusRoot, activityInfo->serverJobQueues, activityInfo->aws);
  729. getDFUServersAndWUs(context, envRoot, activityInfo->serverJobQueues, activityInfo->aws);
  730. getDFURecoveryJobs(context, dfuRecoveryRoot, activityInfo->DFURecoveryJobs);
  731. }
  732. void CWsSMCEx::readRunningWUsOnStatusServer(IEspContext& context, IPropertyTree* serverStatusRoot, WsSMCStatusServerType statusServerType,
  733. CIArrayOf<CWsSMCTargetCluster>& targetClusters, CIArrayOf<CWsSMCTargetCluster>& targetClusters1, CIArrayOf<CWsSMCTargetCluster>& targetClusters2,
  734. BoolHash& uniqueWUIDs, IArrayOf<IEspActiveWorkunit>& aws)
  735. {
  736. const char* serverName = getStatusServerTypeName(statusServerType);
  737. if (!serverName || !*serverName)
  738. return;
  739. bool isECLAgent = (statusServerType == WsSMCSSTECLagent);
  740. VStringBuffer path("Server[@name=\"%s\"]", serverName);
  741. Owned<IPropertyTreeIterator> itrStatusServer(serverStatusRoot->getElements(path.str()));
  742. ForEach(*itrStatusServer)
  743. {
  744. IPropertyTree& serverStatusNode = itrStatusServer->query();
  745. StringBuffer instance;
  746. if ((statusServerType == WsSMCSSTThorLCRCluster) || (statusServerType == WsSMCSSTRoxieCluster))
  747. serverStatusNode.getProp("@cluster", instance);
  748. else
  749. instance.appendf("%s on %s", serverName, serverStatusNode.queryProp("@node"));
  750. const char* graph = NULL;
  751. int sgDuration = -1;
  752. int subgraph = -1;
  753. StringBuffer durationStr, subgraphStr;
  754. if (!isECLAgent)
  755. {
  756. sgDuration = serverStatusNode.getPropInt("@sg_duration", -1);
  757. subgraph = serverStatusNode.getPropInt("@subgraph", -1);
  758. graph = serverStatusNode.queryProp("@graph");
  759. durationStr.appendf("%d min", sgDuration);
  760. subgraphStr.appendf("%d", subgraph);
  761. }
  762. Owned<IPropertyTreeIterator> wuids(serverStatusNode.getElements("WorkUnit"));
  763. ForEach(*wuids)
  764. {
  765. const char* wuid=wuids->query().queryProp(NULL);
  766. if (!wuid || !*wuid || (isECLAgent && uniqueWUIDs.getValue(wuid)))
  767. continue;
  768. CWsSMCTargetCluster* targetCluster = findWUClusterInfo(context, wuid, isECLAgent, targetClusters, targetClusters1, targetClusters2);
  769. if (!targetCluster)
  770. continue;
  771. const char* targetClusterName = targetCluster->clusterName.get();
  772. CWsSMCQueue* jobQueue;
  773. if (statusServerType == WsSMCSSTThorLCRCluster)
  774. jobQueue = &targetCluster->clusterQueue;
  775. else
  776. jobQueue = &targetCluster->agentQueue;
  777. Owned<IEspActiveWorkunit> wu;
  778. if (!isECLAgent)
  779. {
  780. uniqueWUIDs.setValue(wuid, true);
  781. const char *cluster = serverStatusNode.queryProp("Cluster");
  782. StringBuffer queueName;
  783. if (cluster) // backward compat check.
  784. getClusterThorQueueName(queueName, cluster);
  785. else
  786. queueName.append(targetCluster->queueName.get());
  787. createActiveWorkUnit(wu, context, wuid, !strieq(targetClusterName, instance.str()) ? instance.str() : NULL, 0, serverName,
  788. queueName, instance.str(), targetClusterName, false);
  789. if (wu->getStateID() == WUStateRunning) //'aborting' may be another possible status
  790. {
  791. if (subgraph > -1 && sgDuration > -1)
  792. {
  793. wu->setGraphName(graph);
  794. wu->setDuration(durationStr.str());
  795. wu->setGID(subgraphStr.str());
  796. }
  797. if (serverStatusNode.getPropInt("@memoryBlocked ", 0) != 0)
  798. wu->setMemoryBlocked(1);
  799. }
  800. }
  801. else
  802. {
  803. createActiveWorkUnit(wu, context, wuid, instance.str(), 0, serverName, serverName, instance.str(), targetClusterName, false);
  804. if (targetCluster->clusterType == ThorLCRCluster)
  805. wu->setClusterType(CLUSTER_TYPE_THOR);
  806. else if (targetCluster->clusterType == RoxieCluster)
  807. wu->setClusterType(CLUSTER_TYPE_ROXIE);
  808. else
  809. wu->setClusterType(CLUSTER_TYPE_HTHOR);
  810. wu->setClusterQueueName(targetCluster->queueName.get());
  811. if (wu->getStateID() != WUStateRunning)
  812. {
  813. const char *extra = wu->getExtra();
  814. if (wu->getStateID() != WUStateBlocked || !extra || !*extra) // Blocked on persist treated as running here
  815. {
  816. aws.append(*wu.getLink());
  817. jobQueue->countQueuedJobs++;
  818. continue;
  819. }
  820. }
  821. if (serverStatusNode.getPropInt("@memoryBlocked ", 0) != 0)
  822. wu->setMemoryBlocked(1);
  823. }
  824. aws.append(*wu.getLink());
  825. jobQueue->countRunningJobs++;
  826. }
  827. }
  828. }
  829. void CWsSMCEx::readWUsAndStateFromJobQueue(IEspContext& context, CIArrayOf<CWsSMCTargetCluster>& targetClusters, BoolHash& uniqueWUIDs, IArrayOf<IEspActiveWorkunit>& aws)
  830. {
  831. ForEachItemIn(i, targetClusters)
  832. readWUsAndStateFromJobQueue(context, targetClusters.item(i), uniqueWUIDs, aws);
  833. }
  834. void CWsSMCEx::readWUsAndStateFromJobQueue(IEspContext& context, CWsSMCTargetCluster& targetCluster, BoolHash& uniqueWUIDs, IArrayOf<IEspActiveWorkunit>& aws)
  835. {
  836. if (targetCluster.clusterType == ThorLCRCluster)
  837. {
  838. readWUsAndStateFromJobQueue(context, targetCluster, targetCluster.clusterQueue, NULL, uniqueWUIDs, aws);
  839. targetCluster.queueStatus.set(targetCluster.clusterQueue.queueState);
  840. }
  841. if (targetCluster.agentQueue.queueName.length())
  842. {
  843. readWUsAndStateFromJobQueue(context, targetCluster, targetCluster.agentQueue, targetCluster.agentQueue.queueName.str(), uniqueWUIDs, aws);
  844. if (targetCluster.clusterType != ThorLCRCluster)
  845. targetCluster.queueStatus.set(targetCluster.agentQueue.queueState);
  846. }
  847. if (targetCluster.serverQueue.queueName.length())
  848. readWUsAndStateFromJobQueue(context, targetCluster, targetCluster.serverQueue, targetCluster.serverQueue.queueName.str(), uniqueWUIDs, aws);
  849. }
  850. CWsSMCTargetCluster* CWsSMCEx::findTargetCluster(const char* clusterName, CIArrayOf<CWsSMCTargetCluster>& targetClusters)
  851. {
  852. ForEachItemIn(i, targetClusters)
  853. {
  854. CWsSMCTargetCluster& targetCluster = targetClusters.item(i);
  855. if (strieq(targetCluster.clusterName.get(), clusterName))
  856. return &targetCluster;
  857. }
  858. return NULL;
  859. }
  860. CWsSMCTargetCluster* CWsSMCEx::findWUClusterInfo(IEspContext& context, const char* wuid, bool isOnECLAgent, CIArrayOf<CWsSMCTargetCluster>& targetClusters,
  861. CIArrayOf<CWsSMCTargetCluster>& targetClusters1, CIArrayOf<CWsSMCTargetCluster>& targetClusters2)
  862. {
  863. SCMStringBuffer clusterName;
  864. try
  865. {
  866. Owned<IWorkUnitFactory> factory = getWorkUnitFactory();
  867. Owned<IConstWorkUnit> cw = factory->openWorkUnit(wuid, false);
  868. if (!cw)
  869. return NULL;
  870. cw->getClusterName(clusterName);
  871. if (!clusterName.length())
  872. return NULL;
  873. }
  874. catch (IException *e)
  875. {//Exception may be thrown when the openWorkUnit() is called inside the CWUWrapper
  876. StringBuffer msg;
  877. WARNLOG("Failed to open workunit %s: %s", wuid, e->errorMessage(msg).str());
  878. e->Release();
  879. return NULL;
  880. }
  881. const char* cluster = clusterName.str();
  882. CWsSMCTargetCluster* targetCluster = findTargetCluster(cluster, targetClusters);
  883. if (targetCluster || !isOnECLAgent)
  884. return targetCluster;
  885. targetCluster = findTargetCluster(cluster, targetClusters1);
  886. if (targetCluster)
  887. return targetCluster;
  888. return findTargetCluster(cluster, targetClusters2);
  889. }
  890. void CWsSMCEx::addWUsToResponse(IEspContext &context, const IArrayOf<IEspActiveWorkunit>& aws, IEspActivityResponse& resp)
  891. {
  892. const char* user = context.queryUserId();
  893. IArrayOf<IEspActiveWorkunit> awsReturned;
  894. ForEachItemIn(i, aws)
  895. {
  896. IEspActiveWorkunit& wu = aws.item(i);
  897. const char* wuid = wu.getWuid();
  898. if (wuid[0] == 'D')//DFU WU
  899. {
  900. awsReturned.append(*LINK(&wu));
  901. continue;
  902. }
  903. try
  904. {
  905. //if no access, throw an exception and go to the 'catch' section.
  906. const char* owner = wu.getOwner();
  907. context.validateFeatureAccess((!owner || !*owner || (user && streq(user, owner))) ? OWN_WU_ACCESS : OTHERS_WU_ACCESS, SecAccess_Read, true);
  908. awsReturned.append(*LINK(&wu));
  909. continue;
  910. }
  911. catch (IException *e)
  912. { //if the wu cannot be opened for some reason, the openWorkUnit() inside the CActiveWorkunitWrapper() may throw an exception.
  913. //We do not want the exception stops this process of retrieving/showing all active WUs. And that WU should still be displayed
  914. //with the exception.
  915. StringBuffer msg;
  916. Owned<IEspActiveWorkunit> cw = new CActiveWorkunitWrapper(wuid, "", "", e->errorMessage(msg).str(), "normal");
  917. cw->setStateID(WUStateUnknown);
  918. cw->setServer(wu.getServer());
  919. cw->setQueueName(wu.getQueueName());
  920. const char* instanceName = wu.getInstance();
  921. const char* targetClusterName = wu.getTargetClusterName();
  922. if (instanceName && *instanceName)
  923. cw->setInstance(instanceName); // JCSMORE In thor case at least, if queued it is unknown which instance it will run on..
  924. if (targetClusterName && *targetClusterName)
  925. cw->setTargetClusterName(targetClusterName);
  926. awsReturned.append(*cw.getLink());
  927. e->Release();
  928. }
  929. }
  930. resp.setRunning(awsReturned);
  931. return;
  932. }
  933. void CWsSMCEx::setActivityResponse(IEspContext &context, ActivityInfo* activityInfo, IEspActivityRequest &req, IEspActivityResponse& resp)
  934. {
  935. double version = context.getClientVersion();
  936. const char* sortBy = req.getSortBy();
  937. bool descending = req.getDescending();
  938. if (version >= 1.16)
  939. {
  940. IArrayOf<IEspTargetCluster> thorClusters;
  941. IArrayOf<IEspTargetCluster> hthorClusters;
  942. IArrayOf<IEspTargetCluster> roxieClusters;
  943. setESPTargetClusters(context, activityInfo->thorTargetClusters, thorClusters);
  944. setESPTargetClusters(context, activityInfo->roxieTargetClusters, roxieClusters);
  945. setESPTargetClusters(context, activityInfo->hthorTargetClusters, hthorClusters);
  946. sortTargetClusters(thorClusters, sortBy, descending);
  947. sortTargetClusters(roxieClusters, sortBy, descending);
  948. SecAccessFlags access;
  949. if (context.authorizeFeature(THORQUEUE_FEATURE, access) && access>=SecAccess_Full)
  950. resp.setAccessRight("Access_Full");
  951. resp.setSortBy(sortBy);
  952. resp.setDescending(descending);
  953. resp.setThorClusterList(thorClusters);
  954. resp.setRoxieClusterList(roxieClusters);
  955. resp.setHThorClusterList(hthorClusters);
  956. resp.setServerJobQueues(activityInfo->serverJobQueues);
  957. }
  958. else
  959. {//for backward compatible
  960. IArrayOf<IEspThorCluster> thorClusters;
  961. ForEachItemIn(i, activityInfo->thorTargetClusters)
  962. {
  963. CWsSMCTargetCluster& targetCluster = activityInfo->thorTargetClusters.item(i);
  964. Owned<IEspThorCluster> respThorCluster = new CThorCluster("", "");
  965. respThorCluster->setClusterName(targetCluster.clusterName.get());
  966. respThorCluster->setQueueStatus(targetCluster.queueStatus.get());
  967. if (version >= 1.03)
  968. respThorCluster->setQueueName(targetCluster.queueName.get());
  969. if (version >= 1.11)
  970. respThorCluster->setClusterSize(targetCluster.clusterSize);
  971. thorClusters.append(*respThorCluster.getClear());
  972. }
  973. resp.setThorClusters(thorClusters);
  974. if (version > 1.06)
  975. {
  976. IArrayOf<IEspRoxieCluster> roxieClusters;
  977. ForEachItemIn(i, activityInfo->roxieTargetClusters)
  978. {
  979. CWsSMCTargetCluster& targetCluster = activityInfo->roxieTargetClusters.item(i);
  980. Owned<IEspRoxieCluster> respRoxieCluster = new CRoxieCluster("", "");
  981. respRoxieCluster->setClusterName(targetCluster.clusterName.get());
  982. respRoxieCluster->setQueueStatus(targetCluster.queueStatus.get());
  983. respRoxieCluster->setQueueName(targetCluster.queueName.get());
  984. if (version >= 1.11)
  985. respRoxieCluster->setClusterSize(targetCluster.clusterSize);
  986. roxieClusters.append(*respRoxieCluster.getClear());
  987. }
  988. resp.setRoxieClusters(roxieClusters);
  989. }
  990. if (version > 1.10)
  991. {
  992. resp.setSortBy(sortBy);
  993. resp.setDescending(req.getDescending());
  994. }
  995. if (version > 1.11)
  996. {
  997. IArrayOf<IEspHThorCluster> hThorClusters;
  998. ForEachItemIn(i, activityInfo->hthorTargetClusters)
  999. {
  1000. CWsSMCTargetCluster& targetCluster = activityInfo->hthorTargetClusters.item(i);
  1001. Owned<IEspHThorCluster> respHThorCluster = new CHThorCluster("", "");
  1002. respHThorCluster->setClusterName(targetCluster.clusterName.get());
  1003. respHThorCluster->setQueueStatus(targetCluster.queueStatus.get());
  1004. respHThorCluster->setQueueName(targetCluster.queueName.get());
  1005. respHThorCluster->setClusterSize(targetCluster.clusterSize);
  1006. hThorClusters.append(*respHThorCluster.getClear());
  1007. }
  1008. resp.setHThorClusters(hThorClusters);
  1009. SecAccessFlags access;
  1010. if (context.authorizeFeature(THORQUEUE_FEATURE, access) && access>=SecAccess_Full)
  1011. resp.setAccessRight("Access_Full");
  1012. }
  1013. if (version > 1.03)
  1014. resp.setServerJobQueues(activityInfo->serverJobQueues);
  1015. }
  1016. resp.setDFUJobs(activityInfo->DFURecoveryJobs);
  1017. addWUsToResponse(context, activityInfo->aws, resp);
  1018. return;
  1019. }
  1020. void CWsSMCEx::setESPTargetClusters(IEspContext& context, CIArrayOf<CWsSMCTargetCluster>& targetClusters, IArrayOf<IEspTargetCluster>& respTargetClusters)
  1021. {
  1022. ForEachItemIn(i, targetClusters)
  1023. {
  1024. Owned<IEspTargetCluster> respTargetCluster = new CTargetCluster("", "");
  1025. setESPTargetCluster(context, targetClusters.item(i), respTargetCluster);
  1026. respTargetClusters.append(*respTargetCluster.getClear());
  1027. }
  1028. }
  1029. void CWsSMCEx::addServerJobQueue(IArrayOf<IEspServerJobQueue>& jobQueues, const char* queueName, const char* serverName,
  1030. const char* serverType, const char* networkAddress, unsigned port)
  1031. {
  1032. if (!queueName || !*queueName || !serverName || !*serverName || !serverType || !*serverType)
  1033. return;
  1034. StringBuffer queueState;
  1035. StringBuffer queueStateDetails;
  1036. Owned<IJobQueue> queue = createJobQueue(queueName);
  1037. if (queue->stopped(queueStateDetails))
  1038. queueState.set("stopped");
  1039. else if (queue->paused(queueStateDetails))
  1040. queueState.set("paused");
  1041. else
  1042. queueState.set("running");
  1043. addServerJobQueue(jobQueues, queueName, serverName, serverType, networkAddress, port, queueState.str(), queueStateDetails.str());
  1044. }
  1045. void CWsSMCEx::addServerJobQueue(IArrayOf<IEspServerJobQueue>& jobQueues, const char* queueName, const char* serverName,
  1046. const char* serverType, const char* networkAddress, unsigned port, const char* queueState, const char* queueStateDetails)
  1047. {
  1048. if (!queueName || !*queueName || !serverName || !*serverName || !serverType || !*serverType)
  1049. return;
  1050. if (!queueState || !*queueState)
  1051. queueState = "running";
  1052. Owned<IEspServerJobQueue> jobQueue = createServerJobQueue("", "");
  1053. jobQueue->setQueueName(queueName);
  1054. jobQueue->setServerName(serverName);
  1055. jobQueue->setServerType(serverType);
  1056. if (networkAddress && *networkAddress)
  1057. {
  1058. jobQueue->setNetworkAddress(networkAddress);
  1059. jobQueue->setPort(port);
  1060. }
  1061. setServerJobQueueStatus(jobQueue, queueState, queueStateDetails);
  1062. jobQueues.append(*jobQueue.getClear());
  1063. }
  1064. void CWsSMCEx::setServerJobQueueStatus(double version, IEspServerJobQueue* jobQueue, const char* status, const char* details)
  1065. {
  1066. if (!status || !*status)
  1067. return;
  1068. jobQueue->setQueueStatus(status);
  1069. if (version >= 1.17)
  1070. setServerJobQueueStatusDetails(jobQueue, status, details);
  1071. }
  1072. void CWsSMCEx::setServerJobQueueStatus(IEspServerJobQueue* jobQueue, const char* status, const char* details)
  1073. {
  1074. if (!status || !*status)
  1075. return;
  1076. jobQueue->setQueueStatus(status);
  1077. setServerJobQueueStatusDetails(jobQueue, status, details);
  1078. }
  1079. void CWsSMCEx::setServerJobQueueStatusDetails(IEspServerJobQueue* jobQueue, const char* status, const char* details)
  1080. {
  1081. StringBuffer queueState;
  1082. if (details && *details)
  1083. queueState.appendf("queue %s; %s;", status, details);
  1084. else
  1085. queueState.appendf("queue %s;", status);
  1086. jobQueue->setStatusDetails(queueState.str());
  1087. }
  1088. void CWsSMCEx::addCapabilities(IPropertyTree* pFeatureNode, const char* access,
  1089. IArrayOf<IEspCapability>& capabilities)
  1090. {
  1091. StringBuffer xpath(access);
  1092. xpath.append("/Capability");
  1093. Owned<IPropertyTreeIterator> it = pFeatureNode->getElements(xpath.str());
  1094. ForEach(*it)
  1095. {
  1096. IPropertyTree* pCapabilityNode = &it->query();
  1097. IEspCapability* pCapability = new CCapability("ws_smc");
  1098. pCapability->setName( pCapabilityNode->queryProp("@name") );
  1099. pCapability->setDescription( pCapabilityNode->queryProp("@description") );
  1100. capabilities.append(*pCapability);
  1101. }
  1102. }
  1103. static void checkAccess(IEspContext &context, const char* feature,int level)
  1104. {
  1105. if (!context.validateFeatureAccess(feature, level, false))
  1106. throw MakeStringException(ECLWATCH_THOR_QUEUE_ACCESS_DENIED, "Failed to access the queue functions. Permission denied.");
  1107. }
  1108. bool CWsSMCEx::onMoveJobDown(IEspContext &context, IEspSMCJobRequest &req, IEspSMCJobResponse &resp)
  1109. {
  1110. try
  1111. {
  1112. checkAccess(context,THORQUEUE_FEATURE,SecAccess_Full);
  1113. Owned<IJobQueue> queue = createJobQueue(req.getQueueName());
  1114. QueueLock lock(queue);
  1115. unsigned index=queue->findRank(req.getWuid());
  1116. if(index<queue->ordinality())
  1117. {
  1118. IJobQueueItem * item0 = queue->getItem(index);
  1119. IJobQueueItem * item = queue->getItem(index+1);
  1120. if(item && item0 && (item0->getPriority() == item->getPriority()))
  1121. queue->moveAfter(req.getWuid(),item->queryWUID());
  1122. }
  1123. AccessSuccess(context, "Changed job priority %s",req.getWuid());
  1124. clearActivityInfoCache();
  1125. resp.setRedirectUrl("/WsSMC/");
  1126. }
  1127. catch(IException* e)
  1128. {
  1129. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1130. }
  1131. return true;
  1132. }
  1133. bool CWsSMCEx::onMoveJobUp(IEspContext &context, IEspSMCJobRequest &req, IEspSMCJobResponse &resp)
  1134. {
  1135. try
  1136. {
  1137. checkAccess(context,THORQUEUE_FEATURE,SecAccess_Full);
  1138. Owned<IJobQueue> queue = createJobQueue(req.getQueueName());
  1139. QueueLock lock(queue);
  1140. unsigned index=queue->findRank(req.getWuid());
  1141. if(index>0 && index<queue->ordinality())
  1142. {
  1143. IJobQueueItem * item0 = queue->getItem(index);
  1144. IJobQueueItem * item = queue->getItem(index-1);
  1145. if(item && item0 && (item0->getPriority() == item->getPriority()))
  1146. queue->moveBefore(req.getWuid(),item->queryWUID());
  1147. }
  1148. AccessSuccess(context, "Changed job priority %s",req.getWuid());
  1149. clearActivityInfoCache();
  1150. resp.setRedirectUrl("/WsSMC/");
  1151. }
  1152. catch(IException* e)
  1153. {
  1154. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1155. }
  1156. return true;
  1157. }
  1158. bool CWsSMCEx::onMoveJobBack(IEspContext &context, IEspSMCJobRequest &req, IEspSMCJobResponse &resp)
  1159. {
  1160. try
  1161. {
  1162. checkAccess(context,THORQUEUE_FEATURE,SecAccess_Full);
  1163. Owned<IJobQueue> queue = createJobQueue(req.getQueueName());
  1164. QueueLock lock(queue);
  1165. unsigned index=queue->findRank(req.getWuid());
  1166. if(index<queue->ordinality())
  1167. {
  1168. int priority0 = queue->getItem(index)->getPriority();
  1169. unsigned biggestIndoxInSamePriority = index;
  1170. unsigned nextIndex = biggestIndoxInSamePriority + 1;
  1171. while (nextIndex<queue->ordinality())
  1172. {
  1173. IJobQueueItem * item = queue->getItem(nextIndex);
  1174. if (priority0 != item->getPriority())
  1175. {
  1176. break;
  1177. }
  1178. biggestIndoxInSamePriority = nextIndex;
  1179. nextIndex++;
  1180. }
  1181. if (biggestIndoxInSamePriority != index)
  1182. {
  1183. IJobQueueItem * item = queue->getItem(biggestIndoxInSamePriority);
  1184. queue->moveAfter(req.getWuid(),item->queryWUID());
  1185. }
  1186. }
  1187. AccessSuccess(context, "Changed job priority %s",req.getWuid());
  1188. clearActivityInfoCache();
  1189. resp.setRedirectUrl("/WsSMC/");
  1190. }
  1191. catch(IException* e)
  1192. {
  1193. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1194. }
  1195. return true;
  1196. }
  1197. bool CWsSMCEx::onMoveJobFront(IEspContext &context, IEspSMCJobRequest &req, IEspSMCJobResponse &resp)
  1198. {
  1199. try
  1200. {
  1201. checkAccess(context,THORQUEUE_FEATURE,SecAccess_Full);
  1202. Owned<IJobQueue> queue = createJobQueue(req.getQueueName());
  1203. QueueLock lock(queue);
  1204. unsigned index=queue->findRank(req.getWuid());
  1205. if(index>0 && index<queue->ordinality())
  1206. {
  1207. int priority0 = queue->getItem(index)->getPriority();
  1208. unsigned smallestIndoxInSamePriority = index;
  1209. int nextIndex = smallestIndoxInSamePriority - 1;
  1210. while (nextIndex >= 0)
  1211. {
  1212. IJobQueueItem * item = queue->getItem(nextIndex);
  1213. if (priority0 != item->getPriority())
  1214. {
  1215. break;
  1216. }
  1217. smallestIndoxInSamePriority = nextIndex;
  1218. nextIndex--;
  1219. }
  1220. if (smallestIndoxInSamePriority != index)
  1221. {
  1222. IJobQueueItem * item = queue->getItem(smallestIndoxInSamePriority);
  1223. queue->moveBefore(req.getWuid(),item->queryWUID());
  1224. }
  1225. }
  1226. AccessSuccess(context, "Changed job priority %s",req.getWuid());
  1227. clearActivityInfoCache();
  1228. resp.setRedirectUrl("/WsSMC/");
  1229. }
  1230. catch(IException* e)
  1231. {
  1232. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1233. }
  1234. return true;
  1235. }
  1236. bool CWsSMCEx::onRemoveJob(IEspContext &context, IEspSMCJobRequest &req, IEspSMCJobResponse &resp)
  1237. {
  1238. try
  1239. {
  1240. checkAccess(context,THORQUEUE_FEATURE,SecAccess_Full);
  1241. secAbortWorkUnit(req.getWuid(), *context.querySecManager(), *context.queryUser());
  1242. Owned<IJobQueue> queue = createJobQueue(req.getQueueName());
  1243. QueueLock lock(queue);
  1244. unsigned index=queue->findRank(req.getWuid());
  1245. if(index<queue->ordinality())
  1246. {
  1247. if(!queue->cancelInitiateConversation(req.getWuid()))
  1248. throw MakeStringException(ECLWATCH_CANNOT_DELETE_WORKUNIT,"Failed to remove the workunit %s",req.getWuid());
  1249. }
  1250. AccessSuccess(context, "Removed job %s",req.getWuid());
  1251. clearActivityInfoCache();
  1252. resp.setRedirectUrl("/WsSMC/");
  1253. }
  1254. catch(IException* e)
  1255. {
  1256. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1257. }
  1258. return true;
  1259. }
  1260. bool CWsSMCEx::onStopQueue(IEspContext &context, IEspSMCQueueRequest &req, IEspSMCQueueResponse &resp)
  1261. {
  1262. try
  1263. {
  1264. checkAccess(context,THORQUEUE_FEATURE,SecAccess_Full);
  1265. StringBuffer info;
  1266. Owned<IJobQueue> queue = createJobQueue(req.getQueueName());
  1267. queue->stop(createQueueActionInfo(context, "stopped", req, info));
  1268. AccessSuccess(context, "Stopped queue %s",req.getCluster());
  1269. clearActivityInfoCache();
  1270. double version = context.getClientVersion();
  1271. if (version >= 1.19)
  1272. getStatusServerInfo(context, req.getServerType(), req.getCluster(), req.getNetworkAddress(), req.getPort(), resp.updateStatusServerInfo());
  1273. resp.setRedirectUrl("/WsSMC/");
  1274. }
  1275. catch(IException* e)
  1276. {
  1277. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1278. }
  1279. return true;
  1280. }
  1281. bool CWsSMCEx::onResumeQueue(IEspContext &context, IEspSMCQueueRequest &req, IEspSMCQueueResponse &resp)
  1282. {
  1283. try
  1284. {
  1285. checkAccess(context,THORQUEUE_FEATURE,SecAccess_Full);
  1286. StringBuffer info;
  1287. Owned<IJobQueue> queue = createJobQueue(req.getQueueName());
  1288. queue->resume(createQueueActionInfo(context, "resumed", req, info));
  1289. AccessSuccess(context, "Resumed queue %s",req.getCluster());
  1290. clearActivityInfoCache();
  1291. double version = context.getClientVersion();
  1292. if (version >= 1.19)
  1293. getStatusServerInfo(context, req.getServerType(), req.getCluster(), req.getNetworkAddress(), req.getPort(), resp.updateStatusServerInfo());
  1294. resp.setRedirectUrl("/WsSMC/");
  1295. }
  1296. catch(IException* e)
  1297. {
  1298. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1299. }
  1300. return true;
  1301. }
  1302. const char* CWsSMCEx::createQueueActionInfo(IEspContext &context, const char* state, IEspSMCQueueRequest &req, StringBuffer& info)
  1303. {
  1304. StringBuffer peer, currentTime;
  1305. context.getPeer(peer);
  1306. const char* userId = context.queryUserId();
  1307. if (!userId || !*userId)
  1308. userId = "Unknown user";
  1309. CDateTime now;
  1310. now.setNow();
  1311. now.getString(currentTime);
  1312. info.appendf("%s by <%s> at <%s> from <%s>", state, userId, currentTime.str(), peer.str());
  1313. const char* comment = req.getComment();
  1314. if (comment && *comment)
  1315. info.append(": ' ").append(comment).append("'");
  1316. return info.str();
  1317. }
  1318. bool CWsSMCEx::onPauseQueue(IEspContext &context, IEspSMCQueueRequest &req, IEspSMCQueueResponse &resp)
  1319. {
  1320. try
  1321. {
  1322. checkAccess(context,THORQUEUE_FEATURE,SecAccess_Full);
  1323. StringBuffer info;
  1324. Owned<IJobQueue> queue = createJobQueue(req.getQueueName());
  1325. queue->pause(createQueueActionInfo(context, "paused", req, info));
  1326. AccessSuccess(context, "Paused queue %s",req.getCluster());
  1327. clearActivityInfoCache();
  1328. double version = context.getClientVersion();
  1329. if (version >= 1.19)
  1330. getStatusServerInfo(context, req.getServerType(), req.getCluster(), req.getNetworkAddress(), req.getPort(), resp.updateStatusServerInfo());
  1331. resp.setRedirectUrl("/WsSMC/");
  1332. }
  1333. catch(IException* e)
  1334. {
  1335. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1336. }
  1337. return true;
  1338. }
  1339. bool CWsSMCEx::onClearQueue(IEspContext &context, IEspSMCQueueRequest &req, IEspSMCQueueResponse &resp)
  1340. {
  1341. try
  1342. {
  1343. checkAccess(context,THORQUEUE_FEATURE,SecAccess_Full);
  1344. Owned<IJobQueue> queue = createJobQueue(req.getQueueName());
  1345. {
  1346. QueueLock lock(queue);
  1347. for(unsigned i=0;i<queue->ordinality();i++)
  1348. secAbortWorkUnit(queue->getItem(i)->queryWUID(), *context.querySecManager(), *context.queryUser());
  1349. queue->clear();
  1350. }
  1351. AccessSuccess(context, "Cleared queue %s",req.getCluster());
  1352. clearActivityInfoCache();
  1353. double version = context.getClientVersion();
  1354. if (version >= 1.19)
  1355. getStatusServerInfo(context, req.getServerType(), req.getCluster(), req.getNetworkAddress(), req.getPort(), resp.updateStatusServerInfo());
  1356. resp.setRedirectUrl("/WsSMC/");
  1357. }
  1358. catch(IException* e)
  1359. {
  1360. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1361. }
  1362. return true;
  1363. }
  1364. void CWsSMCEx::setJobPriority(IWorkUnitFactory* factory, const char* wuid, const char* queueName, WUPriorityClass& priority)
  1365. {
  1366. if (!wuid || !*wuid)
  1367. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Workunit ID not specified.");
  1368. if (!queueName || !*queueName)
  1369. throw MakeStringException(ECLWATCH_INVALID_INPUT, "queue not specified.");
  1370. Owned<IWorkUnit> lw = factory->updateWorkUnit(wuid);
  1371. if (!lw)
  1372. throw MakeStringException(ECLWATCH_CANNOT_UPDATE_WORKUNIT, "Cannot update Workunit %s", wuid);
  1373. lw->setPriority(priority);
  1374. // set job priority to queue
  1375. int priorityValue = lw->getPriorityValue();
  1376. {
  1377. CriticalBlock b(crit);
  1378. Owned<IJobQueue> queue = createJobQueue(queueName);
  1379. QueueLock lock(queue);
  1380. queue->changePriority(wuid,priorityValue);
  1381. }
  1382. return;
  1383. }
  1384. bool CWsSMCEx::onSetJobPriority(IEspContext &context, IEspSMCPriorityRequest &req, IEspSMCPriorityResponse &resp)
  1385. {
  1386. try
  1387. {
  1388. WUPriorityClass priority = PriorityClassNormal;
  1389. if(strieq(req.getPriority(),"high"))
  1390. priority = PriorityClassHigh;
  1391. else if(strieq(req.getPriority(),"low"))
  1392. priority = PriorityClassLow;
  1393. Owned<IWorkUnitFactory> factory = getSecWorkUnitFactory(*context.querySecManager(), *context.queryUser());
  1394. IArrayOf<IConstSMCJob>& jobs = req.getSMCJobs();
  1395. if (!jobs.length())
  1396. setJobPriority(factory, req.getWuid(), req.getQueueName(), priority);
  1397. else
  1398. {
  1399. ForEachItemIn(i, jobs)
  1400. {
  1401. IConstSMCJob &item = jobs.item(i);
  1402. const char *wuid = item.getWuid();
  1403. const char *queueName = item.getQueueName();
  1404. if (wuid && *wuid && queueName && *queueName)
  1405. setJobPriority(factory, wuid, queueName, priority);
  1406. }
  1407. }
  1408. clearActivityInfoCache();
  1409. resp.setRedirectUrl("/WsSMC/");
  1410. }
  1411. catch(IException* e)
  1412. {
  1413. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1414. }
  1415. return true;
  1416. }
  1417. bool CWsSMCEx::onGetThorQueueAvailability(IEspContext &context, IEspGetThorQueueAvailabilityRequest &req, IEspGetThorQueueAvailabilityResponse& resp)
  1418. {
  1419. try
  1420. {
  1421. if (!context.validateFeatureAccess(FEATURE_URL, SecAccess_Read, false))
  1422. throw MakeStringException(ECLWATCH_SMC_ACCESS_DENIED, "Failed to get Thor Queue availability. Permission denied.");
  1423. StringArray thorNames, groupNames, targetNames, queueNames;
  1424. getEnvironmentThorClusterNames(thorNames, groupNames, targetNames, queueNames);
  1425. IArrayOf<IEspThorCluster> ThorClusters;
  1426. ForEachItemIn(x, thorNames)
  1427. {
  1428. const char* targetName = targetNames.item(x);
  1429. const char* queueName = queueNames.item(x);
  1430. IEspThorCluster* returnCluster = new CThorCluster("","");
  1431. returnCluster->setClusterName(targetName);
  1432. returnCluster->setQueueName(queueName);
  1433. StringBuffer info;
  1434. Owned<IJobQueue> queue = createJobQueue(queueName);
  1435. if(queue->stopped(info))
  1436. returnCluster->setQueueStatus("stopped");
  1437. else if (queue->paused(info))
  1438. returnCluster->setQueueStatus("paused");
  1439. else
  1440. returnCluster->setQueueStatus("running");
  1441. unsigned enqueued=0;
  1442. unsigned connected=0;
  1443. unsigned waiting=0;
  1444. queue->getStats(connected,waiting,enqueued);
  1445. returnCluster->setQueueAvailable(waiting);
  1446. returnCluster->setJobsRunning(connected - waiting);
  1447. returnCluster->setJobsInQueue(enqueued);
  1448. ThorClusters.append(*returnCluster);
  1449. }
  1450. resp.setThorClusters(ThorClusters);
  1451. }
  1452. catch(IException* e)
  1453. {
  1454. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1455. }
  1456. return true;
  1457. }
  1458. bool CWsSMCEx::onSetBanner(IEspContext &context, IEspSetBannerRequest &req, IEspSetBannerResponse& resp)
  1459. {
  1460. try
  1461. {
  1462. #ifdef _USE_OPENLDAP
  1463. CLdapSecManager* secmgr = dynamic_cast<CLdapSecManager*>(context.querySecManager());
  1464. if(!secmgr || !secmgr->isSuperUser(context.queryUser()))
  1465. throw MakeStringException(ECLWATCH_SUPER_USER_ACCESS_DENIED, "access denied, administrators only.");
  1466. #endif
  1467. StringBuffer chatURLStr, bannerStr;
  1468. const char* chatURL = req.getChatURL();
  1469. const char* banner = req.getBannerContent();
  1470. //Only display valid strings
  1471. if (chatURL)
  1472. {
  1473. const char* pStr = chatURL;
  1474. for (unsigned i = 0; i < strlen(chatURL); i++)
  1475. {
  1476. if ((pStr[0] > 31) && (pStr[0] < 127))
  1477. chatURLStr.append(pStr[0]);
  1478. pStr++;
  1479. }
  1480. }
  1481. if (banner)
  1482. {
  1483. const char* pStr = banner;
  1484. for (unsigned i = 0; i < strlen(banner); i++)
  1485. {
  1486. if ((pStr[0] > 31) && (pStr[0] < 127))
  1487. bannerStr.append(pStr[0]);
  1488. pStr++;
  1489. }
  1490. }
  1491. chatURLStr.trim();
  1492. bannerStr.trim();
  1493. if (!req.getBannerAction_isNull() && req.getBannerAction() && (bannerStr.length() < 1))
  1494. {
  1495. throw MakeStringException(ECLWATCH_MISSING_BANNER_CONTENT, "If a Banner is enabled, the Banner content must be specified.");
  1496. }
  1497. if (!req.getEnableChatURL_isNull() && req.getEnableChatURL() && (!req.getChatURL() || !*req.getChatURL()))
  1498. {
  1499. throw MakeStringException(ECLWATCH_MISSING_CHAT_URL, "If a Chat is enabled, the Chat URL must be specified.");
  1500. }
  1501. m_ChatURL = chatURLStr;
  1502. m_Banner = bannerStr;
  1503. const char* bannerSize = req.getBannerSize();
  1504. if (bannerSize && *bannerSize)
  1505. m_BannerSize.clear().append(bannerSize);
  1506. const char* bannerColor = req.getBannerColor();
  1507. if (bannerColor && *bannerColor)
  1508. m_BannerColor.clear().append(bannerColor);
  1509. const char* bannerScroll = req.getBannerScroll();
  1510. if (bannerScroll && *bannerScroll)
  1511. m_BannerScroll.clear().append(bannerScroll);
  1512. m_BannerAction = 0;
  1513. if(!req.getBannerAction_isNull())
  1514. m_BannerAction = req.getBannerAction();
  1515. m_EnableChatURL = 0;
  1516. if(!req.getEnableChatURL_isNull())
  1517. m_EnableChatURL = req.getEnableChatURL();
  1518. resp.setRedirectUrl("/WsSMC/Activity");
  1519. }
  1520. catch(IException* e)
  1521. {
  1522. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1523. }
  1524. return true;
  1525. }
  1526. bool CWsSMCEx::onNotInCommunityEdition(IEspContext &context, IEspNotInCommunityEditionRequest &req, IEspNotInCommunityEditionResponse &resp)
  1527. {
  1528. return true;
  1529. }
  1530. bool CWsSMCEx::onBrowseResources(IEspContext &context, IEspBrowseResourcesRequest & req, IEspBrowseResourcesResponse & resp)
  1531. {
  1532. try
  1533. {
  1534. if (!context.validateFeatureAccess(FEATURE_URL, SecAccess_Read, false))
  1535. throw MakeStringException(ECLWATCH_SMC_ACCESS_DENIED, "Failed to Browse Resources. Permission denied.");
  1536. double version = context.getClientVersion();
  1537. Owned<IEnvironmentFactory> factory = getEnvironmentFactory();
  1538. Owned<IConstEnvironment> constEnv = factory->openEnvironment();
  1539. //The resource files will be downloaded from the same box of ESP (not dali)
  1540. StringBuffer ipStr;
  1541. IpAddress ipaddr = queryHostIP();
  1542. ipaddr.getIpText(ipStr);
  1543. if (ipStr.length() > 0)
  1544. {
  1545. resp.setNetAddress(ipStr.str());
  1546. Owned<IConstMachineInfo> machine = constEnv->getMachineByAddress(ipStr.str());
  1547. if (machine)
  1548. {
  1549. int os = machine->getOS();
  1550. resp.setOS(os);
  1551. }
  1552. }
  1553. if (m_PortalURL.length() > 0)
  1554. resp.setPortalURL(m_PortalURL.str());
  1555. #ifndef USE_RESOURCE
  1556. if (version > 1.12)
  1557. resp.setUseResource(false);
  1558. #else
  1559. if (version > 1.12)
  1560. resp.setUseResource(true);
  1561. //Now, get a list of resources stored inside the ESP box
  1562. IArrayOf<IEspHPCCResourceRepository> resourceRepositories;
  1563. Owned<IPropertyTree> pEnvRoot = &constEnv->getPTree();
  1564. const char* ossInstall = pEnvRoot->queryProp("EnvSettings/path");
  1565. if (!ossInstall || !*ossInstall)
  1566. {
  1567. WARNLOG("Failed to get EnvSettings/Path in environment settings.");
  1568. return true;
  1569. }
  1570. StringBuffer path;
  1571. path.appendf("%s/componentfiles/files/downloads", ossInstall);
  1572. Owned<IFile> f = createIFile(path.str());
  1573. if(!f->exists() || !f->isDirectory())
  1574. {
  1575. WARNLOG("Invalid resource folder");
  1576. return true;
  1577. }
  1578. Owned<IDirectoryIterator> di = f->directoryFiles(NULL, false, true);
  1579. if(di.get() == NULL)
  1580. {
  1581. WARNLOG("Resource folder is empty.");
  1582. return true;
  1583. }
  1584. ForEach(*di)
  1585. {
  1586. if (!di->isDir())
  1587. continue;
  1588. StringBuffer folder, path0, tmpBuf;
  1589. di->getName(folder);
  1590. if (folder.length() == 0)
  1591. continue;
  1592. path0.appendf("%s/%s/description.xml", path.str(), folder.str());
  1593. Owned<IFile> f0 = createIFile(path0.str());
  1594. if(!f0->exists())
  1595. {
  1596. WARNLOG("Description file not found for %s", folder.str());
  1597. continue;
  1598. }
  1599. OwnedIFileIO rIO = f0->openShared(IFOread,IFSHfull);
  1600. if(!rIO)
  1601. {
  1602. WARNLOG("Failed to open the description file for %s", folder.str());
  1603. continue;
  1604. }
  1605. offset_t fileSize = f0->size();
  1606. tmpBuf.ensureCapacity((unsigned)fileSize);
  1607. tmpBuf.setLength((unsigned)fileSize);
  1608. size32_t nRead = rIO->read(0, (size32_t) fileSize, (char*)tmpBuf.str());
  1609. if (nRead != fileSize)
  1610. {
  1611. WARNLOG("Failed to read the description file for %s", folder.str());
  1612. continue;
  1613. }
  1614. Owned<IPropertyTree> desc = createPTreeFromXMLString(tmpBuf.str());
  1615. if (!desc)
  1616. {
  1617. WARNLOG("Invalid description file for %s", folder.str());
  1618. continue;
  1619. }
  1620. Owned<IPropertyTreeIterator> fileIterator = desc->getElements("file");
  1621. if (!fileIterator->first())
  1622. {
  1623. WARNLOG("Invalid description file for %s", folder.str());
  1624. continue;
  1625. }
  1626. IArrayOf<IEspHPCCResource> resourcs;
  1627. do {
  1628. IPropertyTree &fileItem = fileIterator->query();
  1629. const char* filename = fileItem.queryProp("filename");
  1630. if (!filename || !*filename)
  1631. continue;
  1632. const char* name0 = fileItem.queryProp("name");
  1633. const char* description0 = fileItem.queryProp("description");
  1634. const char* version0 = fileItem.queryProp("version");
  1635. Owned<IEspHPCCResource> onefile = createHPCCResource();
  1636. onefile->setFileName(filename);
  1637. if (name0 && *name0)
  1638. onefile->setName(name0);
  1639. if (description0 && *description0)
  1640. onefile->setDescription(description0);
  1641. if (version0 && *version0)
  1642. onefile->setVersion(version0);
  1643. resourcs.append(*onefile.getLink());
  1644. } while (fileIterator->next());
  1645. if (resourcs.ordinality())
  1646. {
  1647. StringBuffer path1;
  1648. path1.appendf("%s/%s", path.str(), folder.str());
  1649. Owned<IEspHPCCResourceRepository> oneRepository = createHPCCResourceRepository();
  1650. oneRepository->setName(folder.str());
  1651. oneRepository->setPath(path1.str());
  1652. oneRepository->setHPCCResources(resourcs);
  1653. resourceRepositories.append(*oneRepository.getLink());
  1654. }
  1655. }
  1656. if (resourceRepositories.ordinality())
  1657. resp.setHPCCResourceRepositories(resourceRepositories);
  1658. #endif
  1659. }
  1660. catch(IException* e)
  1661. {
  1662. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1663. }
  1664. return true;
  1665. }
  1666. int CWsSMCSoapBindingEx::onGetForm(IEspContext &context, CHttpRequest* request, CHttpResponse* response, const char *service, const char *method)
  1667. {
  1668. try
  1669. {
  1670. if(stricmp(method,"NotInCommunityEdition")==0)
  1671. {
  1672. StringBuffer page, url, link;
  1673. request->getParameter("EEPortal", url);
  1674. if (url.length() > 0)
  1675. link.appendf("Further information can be found at <a href=\"%s\" target=\"_blank\">%s</a>.", url.str(), url.str());
  1676. page.append(
  1677. "<html>"
  1678. "<head>"
  1679. "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"
  1680. "<link rel=\"stylesheet\" type=\"text/css\" href=\"/esp/files/default.css\"/>"
  1681. "<link rel=\"stylesheet\" type=\"text/css\" href=\"/esp/files/yui/build/fonts/fonts-min.css\" />"
  1682. "<title>Advanced feature in Enterprise Edition</title>"
  1683. "</head>"
  1684. "<body>"
  1685. "<h3 style=\"text-align:centre;\">Advanced feature in the Enterprise Edition</h4>"
  1686. "<p style=\"text-align:centre;\">Support for this feature is coming soon. ");
  1687. if (link.length() > 0)
  1688. page.append(link.str());
  1689. page.append("</p></body>"
  1690. "</html>");
  1691. response->setContent(page.str());
  1692. response->setContentType("text/html");
  1693. response->send();
  1694. return 0;
  1695. }
  1696. else if(stricmp(method,"DisabledInThisVersion")==0)
  1697. {
  1698. StringBuffer page;
  1699. page.append(
  1700. "<html>"
  1701. "<head>"
  1702. "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"
  1703. "<link rel=\"stylesheet\" type=\"text/css\" href=\"/esp/files/default.css\"/>"
  1704. "<link rel=\"stylesheet\" type=\"text/css\" href=\"/esp/files/yui/build/fonts/fonts-min.css\" />"
  1705. "<title>Disabled Feature in This Version</title>"
  1706. "</head>"
  1707. "<body>"
  1708. "<h3 style=\"text-align:centre;\">Disabled Feature in This Version</h4>"
  1709. "<p style=\"text-align:centre;\">This feature is disabled in this version. ");
  1710. page.append("</p></body>"
  1711. "</html>");
  1712. response->setContent(page.str());
  1713. response->setContentType("text/html");
  1714. response->send();
  1715. return 0;
  1716. }
  1717. }
  1718. catch(IException* e)
  1719. {
  1720. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1721. }
  1722. return onGetForm(context, request, response, service, method);
  1723. }
  1724. inline const char *controlCmdMessage(int cmd)
  1725. {
  1726. switch (cmd)
  1727. {
  1728. case CRoxieControlCmd_ATTACH:
  1729. return "<control:unlockDali/>";
  1730. case CRoxieControlCmd_DETACH:
  1731. return "<control:lockDali/>";
  1732. case CRoxieControlCmd_RELOAD:
  1733. return "<control:reload/>";
  1734. case CRoxieControlCmd_STATE:
  1735. return "<control:state/>";
  1736. default:
  1737. throw MakeStringException(ECLWATCH_MISSING_PARAMS, "Unknown Roxie Control Command.");
  1738. }
  1739. return NULL;
  1740. }
  1741. bool CWsSMCEx::onRoxieControlCmd(IEspContext &context, IEspRoxieControlCmdRequest &req, IEspRoxieControlCmdResponse &resp)
  1742. {
  1743. if (!context.validateFeatureAccess(ROXIE_CONTROL_URL, SecAccess_Full, false))
  1744. throw MakeStringException(ECLWATCH_SMC_ACCESS_DENIED, "Cannot Access Roxie Control. Permission denied.");
  1745. const char *process = req.getProcessCluster();
  1746. if (!process || !*process)
  1747. throw MakeStringException(ECLWATCH_MISSING_PARAMS, "Process cluster not specified.");
  1748. const char *controlReq = controlCmdMessage(req.getCommand());
  1749. SocketEndpointArray addrs;
  1750. getRoxieProcessServers(process, addrs);
  1751. if (!addrs.length())
  1752. throw MakeStringException(ECLWATCH_CANNOT_GET_ENV_INFO, "Process cluster not found.");
  1753. Owned<IPropertyTree> controlResp = sendRoxieControlAllNodes(addrs.item(0), controlReq, true, req.getWait());
  1754. if (!controlResp)
  1755. throw MakeStringException(ECLWATCH_INTERNAL_ERROR, "Failed to get control response from roxie.");
  1756. IArrayOf<IEspRoxieControlEndpointInfo> respEndpoints;
  1757. Owned<IPropertyTreeIterator> roxieEndpoints = controlResp->getElements("Endpoint");
  1758. ForEach(*roxieEndpoints)
  1759. {
  1760. IPropertyTree &roxieEndpoint = roxieEndpoints->query();
  1761. Owned<IEspRoxieControlEndpointInfo> respEndpoint = createRoxieControlEndpointInfo();
  1762. respEndpoint->setAddress(roxieEndpoint.queryProp("@ep"));
  1763. respEndpoint->setStatus(roxieEndpoint.queryProp("Status"));
  1764. if (roxieEndpoint.hasProp("Dali/@connected"))
  1765. respEndpoint->setAttached(roxieEndpoint.getPropBool("Dali/@connected"));
  1766. if (roxieEndpoint.hasProp("State/@hash"))
  1767. respEndpoint->setStateHash(roxieEndpoint.queryProp("State/@hash"));
  1768. respEndpoints.append(*respEndpoint.getClear());
  1769. }
  1770. resp.setEndpoints(respEndpoints);
  1771. return true;
  1772. }
  1773. bool CWsSMCEx::onGetStatusServerInfo(IEspContext &context, IEspGetStatusServerInfoRequest &req, IEspGetStatusServerInfoResponse &resp)
  1774. {
  1775. getStatusServerInfo(context, req.getServerType(), req.getServerName(), req.getNetworkAddress(), req.getPort(), resp.updateStatusServerInfo());
  1776. return true;
  1777. }
  1778. void CWsSMCEx::getStatusServerInfo(IEspContext &context, const char *serverType, const char *server, const char *networkAddress, unsigned port,
  1779. IEspStatusServerInfo& statusServerInfo)
  1780. {
  1781. if (!serverType || !*serverType)
  1782. throw MakeStringException(ECLWATCH_MISSING_PARAMS, "Server type not specified.");
  1783. Owned<ActivityInfo> activityInfo = getActivityInfo(context);
  1784. if (!activityInfo)
  1785. throw MakeStringException(ECLWATCH_INTERNAL_ERROR, "Failed to get Activity Info cache.");
  1786. if (strieq(serverType,STATUS_SERVER_THOR))
  1787. {
  1788. setTargetClusterInfo(context, serverType, server, activityInfo->thorTargetClusters, activityInfo->aws, statusServerInfo);
  1789. }
  1790. else if (strieq(serverType,STATUS_SERVER_ROXIE))
  1791. {
  1792. setTargetClusterInfo(context, serverType, server, activityInfo->roxieTargetClusters, activityInfo->aws, statusServerInfo);
  1793. }
  1794. else if (strieq(serverType,STATUS_SERVER_HTHOR))
  1795. {
  1796. setTargetClusterInfo(context, serverType, server, activityInfo->hthorTargetClusters, activityInfo->aws, statusServerInfo);
  1797. }
  1798. else if (strieq(serverType,STATUS_SERVER_DFUSERVER))
  1799. {
  1800. setServerQueueInfo(context, serverType, server, activityInfo->serverJobQueues, activityInfo->aws, statusServerInfo);
  1801. }
  1802. else
  1803. {
  1804. setServerQueueInfo(context, serverType, networkAddress, port, activityInfo->serverJobQueues, activityInfo->aws, statusServerInfo);
  1805. }
  1806. }
  1807. void CWsSMCEx::setTargetClusterInfo(IEspContext &context, const char *serverType, const char *clusterName, const CIArrayOf<CWsSMCTargetCluster>& targetClusters,
  1808. const IArrayOf<IEspActiveWorkunit>& aws, IEspStatusServerInfo& statusServerInfo)
  1809. {
  1810. if (!clusterName || !*clusterName)
  1811. throw MakeStringException(ECLWATCH_MISSING_PARAMS, "Cluster name not specified.");
  1812. IEspTargetCluster& clusterInfo = statusServerInfo.updateTargetClusterInfo();
  1813. ForEachItemIn(i, targetClusters)
  1814. {
  1815. CWsSMCTargetCluster& targetCluster = targetClusters.item(i);
  1816. const char* name = targetCluster.clusterName.get();
  1817. if (name && strieq(name, clusterName))
  1818. {
  1819. setESPTargetCluster(context, targetCluster, &clusterInfo);
  1820. break;
  1821. }
  1822. }
  1823. setActiveWUs(context, serverType, clusterName, clusterInfo.getQueueName(), aws, statusServerInfo);
  1824. }
  1825. void CWsSMCEx::setServerQueueInfo(IEspContext &context, const char *serverType, const char *serverName, const IArrayOf<IEspServerJobQueue>& serverJobQueues,
  1826. const IArrayOf<IEspActiveWorkunit>& aws, IEspStatusServerInfo& statusServerInfo)
  1827. {
  1828. if (!serverName || !*serverName)
  1829. throw MakeStringException(ECLWATCH_MISSING_PARAMS, "Server name not specified.");
  1830. ForEachItemIn(i, serverJobQueues)
  1831. {
  1832. IEspServerJobQueue& serverJobQueue = serverJobQueues.item(i);
  1833. const char* name = serverJobQueue.getServerName();
  1834. if (name && strieq(name, serverName))
  1835. {
  1836. IEspServerJobQueue& serverQueue = statusServerInfo.updateServerInfo();
  1837. serverQueue.copy(serverJobQueue);
  1838. break;
  1839. }
  1840. }
  1841. setActiveWUs(context, serverType, serverName, aws, statusServerInfo);
  1842. }
  1843. void CWsSMCEx::setServerQueueInfo(IEspContext &context, const char *serverType, const char *networkAddress, unsigned port, const IArrayOf<IEspServerJobQueue>& serverJobQueues,
  1844. const IArrayOf<IEspActiveWorkunit>& aws, IEspStatusServerInfo& statusServerInfo)
  1845. {
  1846. if (!networkAddress || !*networkAddress)
  1847. throw MakeStringException(ECLWATCH_MISSING_PARAMS, "Network address not specified.");
  1848. ForEachItemIn(i, serverJobQueues)
  1849. {
  1850. IEspServerJobQueue& serverJobQueue = serverJobQueues.item(i);
  1851. const char* ipAddress = serverJobQueue.getNetworkAddress();
  1852. unsigned thePort = serverJobQueue.getPort();
  1853. if (ipAddress && strieq(ipAddress, networkAddress) && (thePort == port))
  1854. {
  1855. IEspServerJobQueue& serverQueue = statusServerInfo.updateServerInfo();
  1856. serverQueue.copy(serverJobQueue);
  1857. break;
  1858. }
  1859. }
  1860. VStringBuffer instance("%s_on_%s:%d", serverType, networkAddress, port);
  1861. setActiveWUs(context, serverType, instance.str(), aws, statusServerInfo);
  1862. }
  1863. void CWsSMCEx::setESPTargetCluster(IEspContext &context, CWsSMCTargetCluster& targetCluster, IEspTargetCluster* espTargetCluster)
  1864. {
  1865. espTargetCluster->setClusterName(targetCluster.clusterName.get());
  1866. espTargetCluster->setClusterSize(targetCluster.clusterSize);
  1867. espTargetCluster->setClusterType(targetCluster.clusterType);
  1868. espTargetCluster->setQueueName(targetCluster.queueName.get());
  1869. espTargetCluster->setQueueStatus(targetCluster.queueStatus.get());
  1870. setClusterStatus(context, targetCluster, espTargetCluster);
  1871. }
  1872. void CWsSMCEx::setActiveWUs(IEspContext &context, const char *serverType, const char *clusterName, const char *queueName, const IArrayOf<IEspActiveWorkunit>& aws, IEspStatusServerInfo& statusServerInfo)
  1873. {
  1874. const char* clusterType = CLUSTER_TYPE_THOR;
  1875. if (strieq(serverType,STATUS_SERVER_ROXIE))
  1876. clusterType = CLUSTER_TYPE_ROXIE;
  1877. else if (strieq(serverType,STATUS_SERVER_HTHOR))
  1878. clusterType = CLUSTER_TYPE_HTHOR;
  1879. IArrayOf<IEspActiveWorkunit> awsOnThisQueue;
  1880. ForEachItemIn(i, aws)
  1881. {
  1882. IEspActiveWorkunit& wu = aws.item(i);
  1883. const char* wuid = wu.getWuid();
  1884. if (!wuid || !*wuid)
  1885. continue;
  1886. const char* wuServerType = wu.getServer();
  1887. const char* wuClusterName = wu.getTargetClusterName();
  1888. if (!wuServerType || !wuClusterName || !strieq(serverType, wuServerType) || !strieq(clusterName, wuClusterName))
  1889. {
  1890. const char* wuClusterType = wu.getClusterType();
  1891. const char* wuClusterQueueName = wu.getClusterQueueName();
  1892. if (!wuClusterType || !wuClusterQueueName || !strieq(clusterType, wuClusterType) || !strieq(queueName, wuClusterQueueName))
  1893. continue;
  1894. }
  1895. Owned<IEspActiveWorkunit> wuOnThisQueue = new CActiveWorkunitWrapper(wuid, "", "", "", "");
  1896. setActiveWUs(context, wu, wuOnThisQueue);
  1897. awsOnThisQueue.append(*wuOnThisQueue.getLink());
  1898. }
  1899. statusServerInfo.setWorkunits(awsOnThisQueue);
  1900. }
  1901. void CWsSMCEx::setActiveWUs(IEspContext &context, const char *serverType, const char *instance, const IArrayOf<IEspActiveWorkunit>& aws, IEspStatusServerInfo& statusServerInfo)
  1902. {
  1903. IArrayOf<IEspActiveWorkunit> awsOnThisQueue;
  1904. ForEachItemIn(i, aws)
  1905. {
  1906. IEspActiveWorkunit& wu = aws.item(i);
  1907. const char* wuid = wu.getWuid();
  1908. if (!wuid || !*wuid)
  1909. continue;
  1910. const char* wuInstance = wu.getInstance();
  1911. if (!wuInstance || !strieq(wuInstance, instance))
  1912. continue;
  1913. Owned<IEspActiveWorkunit> wuOnThisQueue = new CActiveWorkunitWrapper(wuid, "", "", "", "");
  1914. setActiveWUs(context, wu, wuOnThisQueue);
  1915. awsOnThisQueue.append(*wuOnThisQueue.getLink());
  1916. }
  1917. statusServerInfo.setWorkunits(awsOnThisQueue);
  1918. }
  1919. void CWsSMCEx::setActiveWUs(IEspContext &context, IEspActiveWorkunit& wu, IEspActiveWorkunit* wuToSet)
  1920. {
  1921. try
  1922. {
  1923. const char* user = context.queryUserId();
  1924. const char* owner = wu.getOwner();
  1925. //if no access, throw an exception and go to the 'catch' section.
  1926. context.validateFeatureAccess((!owner || !*owner || (user && streq(user, owner))) ? OWN_WU_ACCESS : OTHERS_WU_ACCESS, SecAccess_Read, true);
  1927. wuToSet->copy(wu);
  1928. }
  1929. catch (IException *e)
  1930. { //if the wu cannot be opened for some reason, the openWorkUnit() inside the CActiveWorkunitWrapper() may throw an exception.
  1931. //We do not want the exception stops this process of retrieving/showing all active WUs. And that WU should still be displayed
  1932. //with the exception.
  1933. wuToSet->setStateID(WUStateUnknown);
  1934. wuToSet->setServer(wu.getServer());
  1935. wuToSet->setQueueName(wu.getQueueName());
  1936. const char* instanceName = wu.getInstance();
  1937. const char* targetClusterName = wu.getTargetClusterName();
  1938. if (instanceName && *instanceName)
  1939. wuToSet->setInstance(instanceName); // JCSMORE In thor case at least, if queued it is unknown which instance it will run on..
  1940. if (targetClusterName && *targetClusterName)
  1941. wuToSet->setTargetClusterName(targetClusterName);
  1942. e->Release();
  1943. }
  1944. }