ecl-queries.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include <stdio.h>
  14. #include "jlog.hpp"
  15. #include "jfile.hpp"
  16. #include "jargv.hpp"
  17. #include "build-config.h"
  18. #include "ws_workunits.hpp"
  19. #include "eclcmd.hpp"
  20. #include "eclcmd_common.hpp"
  21. #include "eclcmd_core.hpp"
  22. #define INIFILE "ecl.ini"
  23. #define SYSTEMCONFDIR CONFIG_DIR
  24. #define DEFAULTINIFILE "ecl.ini"
  25. #define SYSTEMCONFFILE ENV_CONF_FILE
  26. //=========================================================================================
  27. class ActiveQueryMap
  28. {
  29. public:
  30. ActiveQueryMap(IConstWUQuerySetDetail &qs) : queryMap(createPTree())
  31. {
  32. IArrayOf<IConstQuerySetAlias> &aliases = qs.getAliases();
  33. ForEachItemIn(i, aliases)
  34. addMappedAlias(aliases.item(i).getId(), aliases.item(i).getName());
  35. }
  36. void addMappedAlias(const char *queryid, const char *alias)
  37. {
  38. if (queryid && *queryid && alias && *alias)
  39. ensureMappedQuery(queryid)->addProp("Alias", alias);
  40. }
  41. IPropertyTree *ensureMappedQuery(const char *queryid)
  42. {
  43. VStringBuffer xpath("Query[@id='%s']", queryid);
  44. IPropertyTree *query = queryMap->getPropTree(xpath.str());
  45. if (!query)
  46. {
  47. query = queryMap->addPropTree("Query", createPTree());
  48. query->setProp("@id", queryid);
  49. }
  50. return query;
  51. }
  52. bool isActive(const char *queryid)
  53. {
  54. VStringBuffer xpath("Query[@id='%s']", queryid);
  55. return queryMap->hasProp(xpath.str());
  56. }
  57. IPropertyTreeIterator *getActiveNames(const char *queryid)
  58. {
  59. VStringBuffer xpath("Query[@id='%s']/Alias", queryid);
  60. return queryMap->getElements(xpath.str());
  61. }
  62. private:
  63. Linked<IPropertyTree> queryMap;
  64. };
  65. #define QUERYLIST_SHOW_UNFLAGGED 0x01
  66. #define QUERYLIST_SHOW_ACTIVE 0x02
  67. #define QUERYLIST_SHOW_SUSPENDED 0x04
  68. #define QUERYLIST_SHOW_CLUSTER_SUSPENDED 0x08
  69. #define QUERYLIST_SHOW_INACTIVE (QUERYLIST_SHOW_UNFLAGGED | QUERYLIST_SHOW_SUSPENDED | QUERYLIST_SHOW_CLUSTER_SUSPENDED)
  70. class EclCmdQueriesList : public EclCmdCommon
  71. {
  72. public:
  73. EclCmdQueriesList() : flags(0), optInactive(false), optCheckAllNodes(false)
  74. {
  75. }
  76. virtual bool parseCommandLineOptions(ArgvIterator &iter)
  77. {
  78. if (iter.done())
  79. return false;
  80. for (; !iter.done(); iter.next())
  81. {
  82. const char *arg = iter.query();
  83. if (*arg!='-')
  84. {
  85. optTargetCluster.set(arg);
  86. continue;
  87. }
  88. if (iter.matchOption(optTargetCluster, ECLOPT_CLUSTER_DEPRECATED)||iter.matchOption(optTargetCluster, ECLOPT_CLUSTER_DEPRECATED_S))
  89. continue;
  90. if (iter.matchOption(optTargetCluster, ECLOPT_TARGET)||iter.matchOption(optTargetCluster, ECLOPT_TARGET_S))
  91. continue;
  92. if (iter.matchFlag(optInactive, ECLOPT_INACTIVE))
  93. continue;
  94. if (iter.matchFlag(optCheckAllNodes, ECLOPT_CHECK_ALL_NODES))
  95. continue;
  96. StringAttr temp;
  97. if (iter.matchOption(temp, ECLOPT_SHOW))
  98. {
  99. for (const char *ch = temp.str(); *ch; ch++)
  100. {
  101. switch (*ch)
  102. {
  103. case 'A':
  104. flags |= QUERYLIST_SHOW_ACTIVE;
  105. break;
  106. case 'S':
  107. flags |= QUERYLIST_SHOW_SUSPENDED;
  108. break;
  109. case 'X':
  110. flags |= QUERYLIST_SHOW_CLUSTER_SUSPENDED;
  111. break;
  112. case 'U':
  113. flags |= QUERYLIST_SHOW_UNFLAGGED;
  114. break;
  115. default:
  116. fprintf(stderr, "Unrecognized --show flag = %c\n", *ch);
  117. return false;
  118. }
  119. }
  120. continue;
  121. }
  122. if (EclCmdCommon::matchCommandLineOption(iter, true)!=EclCmdOptionMatch)
  123. return false;
  124. }
  125. return true;
  126. }
  127. virtual bool finalizeOptions(IProperties *globals)
  128. {
  129. if (optInactive)
  130. {
  131. if (flags)
  132. {
  133. fputs("--show and --inactive should not be used together.\n", stderr);
  134. return false;
  135. }
  136. flags = QUERYLIST_SHOW_INACTIVE;
  137. }
  138. if (!optCheckAllNodes)
  139. extractEclCmdOption(optCheckAllNodes, globals, ECLOPT_CHECK_ALL_NODES_ENV, ECLOPT_CHECK_ALL_NODES_INI, false);
  140. if (!EclCmdCommon::finalizeOptions(globals))
  141. return false;
  142. return true;
  143. }
  144. void outputQuery(IConstQuerySetQuery &query, ActiveQueryMap &queryMap)
  145. {
  146. const char *queryid = query.getId();
  147. bool isActive = queryMap.isActive(queryid);
  148. bool suspendedOnCluster = false;
  149. ForEachItemIn(idx, query.getClusters())
  150. {
  151. IConstClusterQueryState &state = query.getClusters().item(idx);
  152. if (strieq(state.getState(), "Suspended"))
  153. {
  154. suspendedOnCluster = true;
  155. break;
  156. }
  157. }
  158. if (flags)
  159. {
  160. if (isActive && !(flags & QUERYLIST_SHOW_ACTIVE))
  161. return;
  162. if (query.getSuspended() && !(flags & QUERYLIST_SHOW_SUSPENDED))
  163. return;
  164. if (suspendedOnCluster && !(flags & QUERYLIST_SHOW_CLUSTER_SUSPENDED))
  165. return;
  166. if (!isActive && !query.getSuspended() && !(flags & QUERYLIST_SHOW_UNFLAGGED))
  167. return;
  168. }
  169. StringBuffer line(" ");
  170. line.append(suspendedOnCluster ? 'X' : ' ');
  171. line.append(query.getSuspended() ? 'S' : ' ');
  172. line.append(isActive ? 'A' : ' ');
  173. line.append(" ").append(queryid);
  174. if (line.length() < 34)
  175. line.appendN(34 - line.length(), ' ');
  176. line.append(' ').append(query.getWuid());
  177. if (query.getComment())
  178. {
  179. if (line.length() < 51)
  180. line.appendN(51 - line.length(), ' ');
  181. line.append(' ').append(query.getComment());
  182. }
  183. fputs(line.append('\n').str(), stdout);
  184. StringBuffer metaTags;
  185. if (!query.getTimeLimit_isNull())
  186. metaTags.append("timeLimit=").append(query.getTimeLimit());
  187. if (!query.getWarnTimeLimit_isNull())
  188. {
  189. if (metaTags.length())
  190. metaTags.append(", ");
  191. metaTags.append("warnTimeLimit=").append(query.getWarnTimeLimit());
  192. }
  193. if (query.getPriority())
  194. {
  195. if (metaTags.length())
  196. metaTags.append(", ");
  197. metaTags.append("priority=").append(query.getPriority());
  198. }
  199. if (query.getMemoryLimit())
  200. {
  201. if (metaTags.length())
  202. metaTags.append(", ");
  203. metaTags.append("memLimit=").append(query.getMemoryLimit());
  204. }
  205. if (query.getSnapshot())
  206. {
  207. if (metaTags.length())
  208. metaTags.append(", ");
  209. metaTags.append("snapshot=").append(query.getSnapshot());
  210. }
  211. if (metaTags.length())
  212. {
  213. fputs(" [", stdout);
  214. fputs(metaTags.str(), stdout);
  215. fputs("]\n\n", stdout);
  216. }
  217. }
  218. void outputQueryset(IConstWUQuerySetDetail &qs)
  219. {
  220. ActiveQueryMap queryMap(qs);
  221. if (qs.getQuerySetName())
  222. fprintf(stdout, "\nTarget: %s\n", qs.getQuerySetName());
  223. fputs("\n", stdout);
  224. fputs("Flags Query Id WUID Comment\n", stdout);
  225. fputs("----- ---------------------------- ---------------- ------------\n", stdout);
  226. IArrayOf<IConstQuerySetQuery> &queries = qs.getQueries();
  227. ForEachItemIn(id, queries)
  228. outputQuery(queries.item(id), queryMap);
  229. }
  230. virtual int processCMD()
  231. {
  232. Owned<IClientWsWorkunits> client = createCmdClient(WsWorkunits, *this);
  233. Owned<IClientWUMultiQuerySetDetailsRequest> req = client->createWUMultiQuerysetDetailsRequest();
  234. req->setQuerySetName(optTargetCluster.get());
  235. req->setClusterName(optTargetCluster.get());
  236. req->setFilterType("All");
  237. req->setCheckAllNodes(optCheckAllNodes);
  238. Owned<IClientWUMultiQuerySetDetailsResponse> resp = client->WUMultiQuerysetDetails(req);
  239. if (resp->getExceptions().ordinality())
  240. outputMultiExceptions(resp->getExceptions());
  241. else
  242. {
  243. IArrayOf<IConstWUQuerySetDetail> &querysets = resp->getQuerysets();
  244. ForEachItemIn(i, querysets)
  245. outputQueryset(querysets.item(i));
  246. }
  247. return 0;
  248. }
  249. virtual void usage()
  250. {
  251. fputs("\nUsage:\n"
  252. "\n"
  253. "The 'queries list' command displays a list of the queries published to one\n"
  254. "or more target clusters. If a target is provided the querysets associated with\n"
  255. "that cluster will be shown. If no queryset or cluster is specified all targets\n"
  256. "are shown.\n"
  257. "\n"
  258. "ecl queries list [<target>][--show=<flags>]\n\n"
  259. " Options:\n"
  260. " <target> Name of target cluster to get list of queries for\n"
  261. " --show=<flags> Show only queries with matching flags\n"
  262. " --inactive Show only queries that do not have an active alias\n"
  263. " --check-all-nodes Check query status on all nodes in the process cluster\n"
  264. " Flags:\n"
  265. " A Query is active\n"
  266. " S Query is suspended in queryset\n"
  267. //not yet " X Query is suspended on selected cluster\n"
  268. " U Query with no flags set\n"
  269. " Common Options:\n",
  270. stdout);
  271. EclCmdCommon::usage();
  272. }
  273. private:
  274. StringAttr optTargetCluster;
  275. unsigned flags;
  276. bool optInactive;
  277. bool optCheckAllNodes;
  278. };
  279. class EclCmdQueryFiles : public EclCmdCommon
  280. {
  281. public:
  282. EclCmdQueryFiles()
  283. {
  284. }
  285. virtual bool parseCommandLineOptions(ArgvIterator &iter)
  286. {
  287. if (iter.done())
  288. return false;
  289. for (; !iter.done(); iter.next())
  290. {
  291. const char *arg = iter.query();
  292. if (*arg!='-')
  293. {
  294. if (optTarget.isEmpty())
  295. optTarget.set(arg);
  296. else if (optQuery.isEmpty())
  297. optQuery.set(arg);
  298. else
  299. {
  300. fprintf(stderr, "\n%s option not recognized\n", arg);
  301. return false;
  302. }
  303. continue;
  304. }
  305. if (EclCmdCommon::matchCommandLineOption(iter, true)!=EclCmdOptionMatch)
  306. return false;
  307. }
  308. return true;
  309. }
  310. virtual bool finalizeOptions(IProperties *globals)
  311. {
  312. if (optTarget.isEmpty())
  313. {
  314. fputs("Target must be specified.\n", stderr);
  315. return false;
  316. }
  317. if (optTarget.isEmpty())
  318. {
  319. fputs("Query must be specified.\n", stderr);
  320. return false;
  321. }
  322. if (!EclCmdCommon::finalizeOptions(globals))
  323. return false;
  324. return true;
  325. }
  326. virtual int processCMD()
  327. {
  328. Owned<IClientWsWorkunits> client = createCmdClient(WsWorkunits, *this);
  329. Owned<IClientWUQueryFilesRequest> req = client->createWUQueryFilesRequest();
  330. req->setTarget(optTarget.get());
  331. req->setQueryId(optQuery.get());
  332. Owned<IClientWUQueryFilesResponse> resp = client->WUQueryFiles(req);
  333. if (resp->getExceptions().ordinality())
  334. outputMultiExceptions(resp->getExceptions());
  335. else
  336. {
  337. IArrayOf<IConstFileUsedByQuery> &files = resp->getFiles();
  338. if (!files.length())
  339. fputs("No files used.\n", stdout);
  340. else
  341. fputs("Files used:\n", stdout);
  342. ForEachItemIn(i, files)
  343. {
  344. IConstFileUsedByQuery &file = files.item(i);
  345. StringBuffer line(" ");
  346. line.append(file.getFileName()).append(", ");
  347. line.append(file.getFileSize()).append(" bytes, ");
  348. line.append(file.getNumberOfParts()).append(" part(s)\n");
  349. fputs(line, stdout);
  350. }
  351. fputs("\n", stdout);
  352. }
  353. return 0;
  354. }
  355. virtual void usage()
  356. {
  357. fputs("\nUsage:\n"
  358. "\n"
  359. "The 'queries files' command displays a list of the files currently in use by\n"
  360. "the given query.\n"
  361. "\n"
  362. "ecl queries files <target> <query>\n\n"
  363. " Options:\n"
  364. " <target> Name of target cluster the query is published on\n"
  365. " <query> Name of the query to get a list of files in use by\n"
  366. " Common Options:\n",
  367. stdout);
  368. EclCmdCommon::usage();
  369. }
  370. private:
  371. StringAttr optTarget;
  372. StringAttr optQuery;
  373. };
  374. class EclCmdQueriesCopy : public EclCmdCommon
  375. {
  376. public:
  377. EclCmdQueriesCopy() : optActivate(false), optNoReload(false), optMsToWait(10000), optDontCopyFiles(false), optOverwrite(false), optAllowForeign(false),
  378. optUpdateSuperfiles(false), optUpdateCloneFrom(false), optDontAppendCluster(false)
  379. {
  380. optTimeLimit = (unsigned) -1;
  381. optWarnTimeLimit = (unsigned) -1;
  382. }
  383. virtual bool parseCommandLineOptions(ArgvIterator &iter)
  384. {
  385. if (iter.done())
  386. return false;
  387. for (; !iter.done(); iter.next())
  388. {
  389. const char *arg = iter.query();
  390. if (*arg!='-')
  391. {
  392. if (optSourceQueryPath.isEmpty())
  393. optSourceQueryPath.set(arg);
  394. else if (optTargetCluster.isEmpty())
  395. optTargetCluster.set(arg);
  396. else
  397. {
  398. fprintf(stderr, "\nunrecognized argument %s\n", arg);
  399. return false;
  400. }
  401. continue;
  402. }
  403. if (iter.matchOption(optDaliIP, ECLOPT_DALIIP))
  404. continue;
  405. if (iter.matchOption(optSourceProcess, ECLOPT_SOURCE_PROCESS))
  406. continue;
  407. if (iter.matchFlag(optActivate, ECLOPT_ACTIVATE)||iter.matchFlag(optActivate, ECLOPT_ACTIVATE_S))
  408. continue;
  409. if (iter.matchFlag(optNoReload, ECLOPT_NORELOAD))
  410. continue;
  411. if (iter.matchOption(optTargetCluster, ECLOPT_CLUSTER_DEPRECATED)||iter.matchOption(optTargetCluster, ECLOPT_CLUSTER_DEPRECATED_S))
  412. continue;
  413. if (iter.matchOption(optTargetCluster, ECLOPT_TARGET)||iter.matchOption(optTargetCluster, ECLOPT_TARGET_S))
  414. continue;
  415. if (iter.matchFlag(optDontCopyFiles, ECLOPT_DONT_COPY_FILES))
  416. continue;
  417. if (iter.matchFlag(optAllowForeign, ECLOPT_ALLOW_FOREIGN))
  418. continue;
  419. if (iter.matchOption(optMsToWait, ECLOPT_WAIT))
  420. continue;
  421. if (iter.matchOption(optTimeLimit, ECLOPT_TIME_LIMIT))
  422. continue;
  423. if (iter.matchOption(optWarnTimeLimit, ECLOPT_WARN_TIME_LIMIT))
  424. continue;
  425. if (iter.matchOption(optMemoryLimit, ECLOPT_MEMORY_LIMIT))
  426. continue;
  427. if (iter.matchOption(optPriority, ECLOPT_PRIORITY))
  428. continue;
  429. if (iter.matchOption(optComment, ECLOPT_COMMENT))
  430. continue;
  431. if (iter.matchFlag(optOverwrite, ECLOPT_OVERWRITE)||iter.matchFlag(optOverwrite, ECLOPT_OVERWRITE_S))
  432. continue;
  433. if (iter.matchFlag(optUpdateSuperfiles, ECLOPT_UPDATE_SUPER_FILES))
  434. continue;
  435. if (iter.matchFlag(optUpdateCloneFrom, ECLOPT_UPDATE_CLONE_FROM))
  436. continue;
  437. if (iter.matchFlag(optDontAppendCluster, ECLOPT_DONT_APPEND_CLUSTER))
  438. continue;
  439. if (iter.matchFlag(optUpdateSuperfiles, ECLOPT_UPDATE_SUPER_FILES))
  440. continue;
  441. if (iter.matchFlag(optUpdateCloneFrom, ECLOPT_UPDATE_CLONE_FROM))
  442. continue;
  443. if (iter.matchFlag(optDontAppendCluster, ECLOPT_DONT_APPEND_CLUSTER))
  444. continue;
  445. if (iter.matchOption(optName, ECLOPT_NAME)||iter.matchOption(optName, ECLOPT_NAME_S))
  446. continue;
  447. if (EclCmdCommon::matchCommandLineOption(iter, true)!=EclCmdOptionMatch)
  448. return false;
  449. }
  450. return true;
  451. }
  452. virtual bool finalizeOptions(IProperties *globals)
  453. {
  454. if (!EclCmdCommon::finalizeOptions(globals))
  455. return false;
  456. if (optSourceQueryPath.isEmpty() && optTargetCluster.isEmpty())
  457. {
  458. fputs("source and target must both be specified.\n", stderr);
  459. return false;
  460. }
  461. if (optMemoryLimit.length() && !isValidMemoryValue(optMemoryLimit))
  462. {
  463. fprintf(stderr, "invalid --memoryLimit value of %s.\n", optMemoryLimit.get());
  464. return false;
  465. }
  466. if (optPriority.length() && !isValidPriorityValue(optPriority))
  467. {
  468. fprintf(stderr, "invalid --priority value of %s.\n", optPriority.get());
  469. return false;
  470. }
  471. return true;
  472. }
  473. virtual int processCMD()
  474. {
  475. Owned<IClientWsWorkunits> client = createCmdClient(WsWorkunits, *this);
  476. Owned<IClientWUQuerySetCopyQueryRequest> req = client->createWUQuerysetCopyQueryRequest();
  477. req->setSource(optSourceQueryPath.get());
  478. req->setTarget(optTargetCluster.get());
  479. req->setCluster(optTargetCluster.get());
  480. req->setDaliServer(optDaliIP.get());
  481. req->setSourceProcess(optSourceProcess);
  482. req->setActivate(optActivate);
  483. req->setOverwrite(optOverwrite);
  484. req->setUpdateSuperFiles(optUpdateSuperfiles);
  485. req->setUpdateCloneFrom(optUpdateCloneFrom);
  486. req->setAppendCluster(!optDontAppendCluster);
  487. req->setDontCopyFiles(optDontCopyFiles);
  488. req->setWait(optMsToWait);
  489. req->setNoReload(optNoReload);
  490. req->setAllowForeignFiles(optAllowForeign);
  491. if (optTimeLimit != (unsigned) -1)
  492. req->setTimeLimit(optTimeLimit);
  493. if (optWarnTimeLimit != (unsigned) -1)
  494. req->setWarnTimeLimit(optWarnTimeLimit);
  495. if (!optMemoryLimit.isEmpty())
  496. req->setMemoryLimit(optMemoryLimit);
  497. if (!optPriority.isEmpty())
  498. req->setPriority(optPriority);
  499. if (!optName.isEmpty())
  500. req->setDestName(optName);
  501. if (optComment.get()) //allow empty
  502. req->setComment(optComment);
  503. Owned<IClientWUQuerySetCopyQueryResponse> resp = client->WUQuerysetCopyQuery(req);
  504. if (resp->getExceptions().ordinality())
  505. outputMultiExceptions(resp->getExceptions());
  506. if (resp->getQueryId() && *resp->getQueryId())
  507. fprintf(stdout, "%s/%s\n\n", optTargetQuerySet.str(), resp->getQueryId());
  508. return 0;
  509. }
  510. virtual void usage()
  511. {
  512. fputs("\nUsage:\n"
  513. "\n"
  514. "The 'queries copy' command copies a query from one queryset to another.\n"
  515. "\n"
  516. "A query can be copied from one HPCC environment to another by using a path\n"
  517. "which begins with '//' followed by the IP and Port of the source EclWatch\n"
  518. "and then followed by the source queryset and query.\n"
  519. "\n"
  520. "ecl queries copy <source_query_path> <target> [--activate]\n"
  521. "\n"
  522. "ecl queries copy //IP:Port/queryset/query <target> [--activate]\n"
  523. "ecl queries copy queryset/query <target> [--activate]\n"
  524. "\n"
  525. " Options:\n"
  526. " <source_query_path> Path of query to copy\n"
  527. " in the form: //ip:port/queryset/query\n"
  528. " or: queryset/query\n"
  529. " <target> Name of target cluster to copy the query to\n"
  530. " --no-files Do not copy DFS file information for referenced files\n"
  531. " --daliip=<ip> Remote Dali DFS to use for copying file information\n"
  532. " (only required if remote environment version < 3.8)\n"
  533. " --source-process Process cluster to copy files from\n"
  534. " -A, --activate Activate the new query\n"
  535. " --no-reload Do not request a reload of the (roxie) cluster\n"
  536. " -O, --overwrite Completely replace existing DFS file information (dangerous)\n"
  537. " --update-super-files Update local DFS super-files if remote DALI has changed\n"
  538. " --update-clone-from Update local clone from location if remote DALI has changed\n"
  539. " --dont-append-cluster Only use to avoid locking issues due to adding cluster to file\n"
  540. " --allow-foreign Do not fail if foreign files are used in query (roxie)\n"
  541. " --wait=<ms> Max time to wait in milliseconds\n"
  542. " --timeLimit=<sec> Value to set for query timeLimit configuration\n"
  543. " --warnTimeLimit=<sec> Value to set for query warnTimeLimit configuration\n"
  544. " --memoryLimit=<mem> Value to set for query memoryLimit configuration\n"
  545. " format <mem> as 500000B, 550K, 100M, 10G, 1T etc.\n"
  546. " --priority=<val> Set the priority for this query. Value can be LOW,\n"
  547. " HIGH, SLA, NONE. NONE will clear current setting.\n"
  548. " --comment=<string> Set the comment associated with this query\n"
  549. " -n, --name=<val> Destination query name for the copied query\n"
  550. " Common Options:\n",
  551. stdout);
  552. EclCmdCommon::usage();
  553. }
  554. private:
  555. StringAttr optSourceQueryPath;
  556. StringAttr optTargetQuerySet;
  557. StringAttr optTargetCluster;
  558. StringAttr optDaliIP;
  559. StringAttr optSourceProcess;
  560. StringAttr optMemoryLimit;
  561. StringAttr optPriority;
  562. StringAttr optComment;
  563. StringAttr optName;
  564. unsigned optMsToWait;
  565. unsigned optTimeLimit;
  566. unsigned optWarnTimeLimit;
  567. bool optActivate;
  568. bool optNoReload;
  569. bool optOverwrite;
  570. bool optUpdateSuperfiles;
  571. bool optUpdateCloneFrom;
  572. bool optDontAppendCluster; //Undesirable but here temporarily because DALI may have locking issues
  573. bool optDontCopyFiles;
  574. bool optAllowForeign;
  575. };
  576. class EclCmdQueriesCopyQueryset : public EclCmdCommon
  577. {
  578. public:
  579. EclCmdQueriesCopyQueryset() : optCloneActiveState(false), optAllQueries(false), optDontCopyFiles(false), optOverwrite(false), optAllowForeign(false),
  580. optUpdateSuperfiles(false), optUpdateCloneFrom(false), optDontAppendCluster(false)
  581. {
  582. }
  583. virtual bool parseCommandLineOptions(ArgvIterator &iter)
  584. {
  585. if (iter.done())
  586. return false;
  587. for (; !iter.done(); iter.next())
  588. {
  589. const char *arg = iter.query();
  590. if (*arg!='-')
  591. {
  592. if (optSourceQuerySet.isEmpty())
  593. optSourceQuerySet.set(arg);
  594. else if (optDestQuerySet.isEmpty())
  595. optDestQuerySet.set(arg);
  596. else
  597. {
  598. fprintf(stderr, "\nunrecognized argument %s\n", arg);
  599. return false;
  600. }
  601. continue;
  602. }
  603. if (iter.matchOption(optDaliIP, ECLOPT_DALIIP))
  604. continue;
  605. if (iter.matchOption(optSourceProcess, ECLOPT_SOURCE_PROCESS))
  606. continue;
  607. if (iter.matchFlag(optCloneActiveState, ECLOPT_CLONE_ACTIVE_STATE))
  608. continue;
  609. if (iter.matchFlag(optDontCopyFiles, ECLOPT_DONT_COPY_FILES))
  610. continue;
  611. if (iter.matchFlag(optAllQueries, ECLOPT_ALL))
  612. continue;
  613. if (iter.matchFlag(optAllowForeign, ECLOPT_ALLOW_FOREIGN))
  614. continue;
  615. if (iter.matchFlag(optOverwrite, ECLOPT_OVERWRITE)||iter.matchFlag(optOverwrite, ECLOPT_OVERWRITE_S))
  616. continue;
  617. if (iter.matchFlag(optUpdateSuperfiles, ECLOPT_UPDATE_SUPER_FILES))
  618. continue;
  619. if (iter.matchFlag(optUpdateCloneFrom, ECLOPT_UPDATE_CLONE_FROM))
  620. continue;
  621. if (iter.matchFlag(optDontAppendCluster, ECLOPT_DONT_APPEND_CLUSTER))
  622. continue;
  623. if (EclCmdCommon::matchCommandLineOption(iter, true)!=EclCmdOptionMatch)
  624. return false;
  625. }
  626. return true;
  627. }
  628. virtual bool finalizeOptions(IProperties *globals)
  629. {
  630. if (!EclCmdCommon::finalizeOptions(globals))
  631. return false;
  632. if (optSourceQuerySet.isEmpty() || optDestQuerySet.isEmpty())
  633. {
  634. fputs("source and destination querysets must both be specified.\n", stderr);
  635. return false;
  636. }
  637. return true;
  638. }
  639. virtual int processCMD()
  640. {
  641. Owned<IClientWsWorkunits> client = createCmdClient(WsWorkunits, *this);
  642. Owned<IClientWUCopyQuerySetRequest> req = client->createWUCopyQuerySetRequest();
  643. req->setActiveOnly(!optAllQueries);
  644. req->setSource(optSourceQuerySet.get());
  645. req->setTarget(optDestQuerySet.get());
  646. req->setDfsServer(optDaliIP.get());
  647. req->setSourceProcess(optSourceProcess);
  648. req->setCloneActiveState(optCloneActiveState);
  649. req->setOverwriteDfs(optOverwrite);
  650. req->setUpdateSuperFiles(optUpdateSuperfiles);
  651. req->setUpdateCloneFrom(optUpdateCloneFrom);
  652. req->setAppendCluster(!optDontAppendCluster);
  653. req->setCopyFiles(!optDontCopyFiles);
  654. req->setAllowForeignFiles(optAllowForeign);
  655. Owned<IClientWUCopyQuerySetResponse> resp = client->WUCopyQuerySet(req);
  656. if (resp->getExceptions().ordinality())
  657. outputMultiExceptions(resp->getExceptions());
  658. StringArray &copied = resp->getCopiedQueries();
  659. fputs("Queries copied:\n", stdout);
  660. if (!copied.length())
  661. fputs(" none\n\n", stdout);
  662. else
  663. {
  664. ForEachItemIn(i, copied)
  665. fprintf(stdout, " %s\n", copied.item(i));
  666. fputs("\n", stdout);
  667. }
  668. StringArray &existing = resp->getExistingQueries();
  669. fputs("Queries already on destination target:\n", stdout);
  670. if (!existing.length())
  671. fputs(" none\n\n", stdout);
  672. else
  673. {
  674. ForEachItemIn(i, existing)
  675. fprintf(stdout, " %s\n", existing.item(i));
  676. fputs("\n", stdout);
  677. }
  678. return 0;
  679. }
  680. virtual void usage()
  681. {
  682. fputs("\nUsage:\n"
  683. "\n"
  684. "The 'queries copy-set' command copies a set of queries from one target to another.\n"
  685. "\n"
  686. "By default only active queries will be copied. Use --all to copy all queries.\n"
  687. "\n"
  688. "ecl queries copy-set <source_target> <destination_target> [--clone-active-state]\n"
  689. "ecl queries copy-set roxie1 roxie2\n"
  690. "ecl queries copy-set //ip:port/roxie1 roxie2 --clone-active-state\n"
  691. "\n"
  692. " Options:\n"
  693. " <source_target> Name of local (or path to remote) target cluster to"
  694. " copy queries from\n"
  695. " <destination_target> Target cluster to copy queries to\n"
  696. " --all Copy both active and inactive queries\n"
  697. " --no-files Do not copy DFS file information for referenced files\n"
  698. " --daliip=<ip> Remote Dali DFS to use for copying file information\n"
  699. " --source-process Process cluster to copy files from\n"
  700. " --clone-active-state Make copied queries active if active on source\n"
  701. " -O, --overwrite Completely replace existing DFS file information (dangerous)\n"
  702. " --update-super-files Update local DFS super-files if remote DALI has changed\n"
  703. " --update-clone-from Update local clone from location if remote DALI has changed\n"
  704. " --dont-append-cluster Only use to avoid locking issues due to adding cluster to file\n"
  705. " --allow-foreign Do not fail if foreign files are used in query (roxie)\n"
  706. " Common Options:\n",
  707. stdout);
  708. EclCmdCommon::usage();
  709. }
  710. private:
  711. StringAttr optSourceQuerySet;
  712. StringAttr optDestQuerySet;
  713. StringAttr optDaliIP;
  714. StringAttr optSourceProcess;
  715. bool optCloneActiveState;
  716. bool optOverwrite;
  717. bool optUpdateSuperfiles;
  718. bool optUpdateCloneFrom;
  719. bool optDontAppendCluster; //Undesirable but here temporarily because DALI may have locking issues
  720. bool optDontCopyFiles;
  721. bool optAllowForeign;
  722. bool optAllQueries;
  723. };
  724. class EclCmdQueriesConfig : public EclCmdCommon
  725. {
  726. public:
  727. EclCmdQueriesConfig() : optNoReload(false), optMsToWait(10000)
  728. {
  729. optTimeLimit = (unsigned) -1;
  730. optWarnTimeLimit = (unsigned) -1;
  731. }
  732. virtual bool parseCommandLineOptions(ArgvIterator &iter)
  733. {
  734. if (iter.done())
  735. return false;
  736. for (; !iter.done(); iter.next())
  737. {
  738. const char *arg = iter.query();
  739. if (*arg!='-')
  740. {
  741. if (optTargetCluster.isEmpty())
  742. optTargetCluster.set(arg);
  743. else if (optQueryId.isEmpty())
  744. optQueryId.set(arg);
  745. else
  746. {
  747. fprintf(stderr, "\nunrecognized argument %s\n", arg);
  748. return false;
  749. }
  750. continue;
  751. }
  752. if (iter.matchFlag(optNoReload, ECLOPT_NORELOAD))
  753. continue;
  754. if (iter.matchOption(optMsToWait, ECLOPT_WAIT))
  755. continue;
  756. if (iter.matchOption(optTimeLimit, ECLOPT_TIME_LIMIT))
  757. continue;
  758. if (iter.matchOption(optWarnTimeLimit, ECLOPT_WARN_TIME_LIMIT))
  759. continue;
  760. if (iter.matchOption(optMemoryLimit, ECLOPT_MEMORY_LIMIT))
  761. continue;
  762. if (iter.matchOption(optPriority, ECLOPT_PRIORITY))
  763. continue;
  764. if (iter.matchOption(optComment, ECLOPT_COMMENT))
  765. continue;
  766. if (EclCmdCommon::matchCommandLineOption(iter, true)!=EclCmdOptionMatch)
  767. return false;
  768. }
  769. return true;
  770. }
  771. virtual bool finalizeOptions(IProperties *globals)
  772. {
  773. if (!EclCmdCommon::finalizeOptions(globals))
  774. return false;
  775. if (optTargetCluster.isEmpty() || optQueryId.isEmpty())
  776. {
  777. fputs("Target and QueryId must both be specified.\n", stderr);
  778. return false;
  779. }
  780. if (optMemoryLimit.length() && !isValidMemoryValue(optMemoryLimit))
  781. {
  782. fprintf(stderr, "invalid --memoryLimit value of %s.\n", optMemoryLimit.get());
  783. return false;
  784. }
  785. if (optPriority.length() && !isValidPriorityValue(optPriority))
  786. {
  787. fprintf(stderr, "invalid --priority value of %s.\n", optPriority.get());
  788. return false;
  789. }
  790. return true;
  791. }
  792. virtual int processCMD()
  793. {
  794. Owned<IClientWsWorkunits> client = createCmdClient(WsWorkunits, *this);
  795. Owned<IClientWUQueryConfigRequest> req = client->createWUQueryConfigRequest();
  796. req->setTarget(optTargetCluster.get());
  797. req->setQueryId(optQueryId.get());
  798. req->setWait(optMsToWait);
  799. req->setNoReload(optNoReload);
  800. if (optTimeLimit != (unsigned) -1)
  801. req->setTimeLimit(optTimeLimit);
  802. if (optWarnTimeLimit != (unsigned) -1)
  803. req->setWarnTimeLimit(optWarnTimeLimit);
  804. if (!optMemoryLimit.isEmpty())
  805. req->setMemoryLimit(optMemoryLimit);
  806. if (!optPriority.isEmpty())
  807. req->setPriority(optPriority);
  808. if (optComment.get()) //allow empty
  809. req->setComment(optComment);
  810. Owned<IClientWUQueryConfigResponse> resp = client->WUQueryConfig(req);
  811. if (resp->getExceptions().ordinality())
  812. outputMultiExceptions(resp->getExceptions());
  813. IArrayOf<IConstWUQueryConfigResult> &results = resp->getResults();
  814. if (results.length())
  815. {
  816. fputs("configured:\n", stdout);
  817. ForEachItemIn(i, results)
  818. fprintf(stdout, " %s\n", results.item(i).getQueryId());
  819. }
  820. return 0;
  821. }
  822. virtual void usage()
  823. {
  824. fputs("\nUsage:\n"
  825. "\n"
  826. "The 'queries config' command updates query configuration values.\n"
  827. "\n"
  828. "ecl queries config <target> <queryid> [options]\n"
  829. "\n"
  830. " Options:\n"
  831. " <target> Name of target queryset containing query\n"
  832. " <queryid> Id of the query to configure\n"
  833. " --no-reload Do not request a reload of the (roxie) cluster\n"
  834. " --wait=<ms> Max time to wait in milliseconds\n"
  835. " --timeLimit=<sec> Value to set for query timeLimit configuration\n"
  836. " --warnTimeLimit=<sec> Value to set for query warnTimeLimit configuration\n"
  837. " --memoryLimit=<mem> Value to set for query memoryLimit configuration\n"
  838. " format <mem> as 500000B, 550K, 100M, 10G, 1T etc.\n"
  839. " --priority=<val> Set the priority for this query. Value can be LOW,\n"
  840. " HIGH, SLA, NONE. NONE will clear current setting.\n"
  841. " --comment=<string> Set the comment associated with this query\n"
  842. " Common Options:\n",
  843. stdout);
  844. EclCmdCommon::usage();
  845. }
  846. private:
  847. StringAttr optTargetCluster;
  848. StringAttr optQueryId;
  849. StringAttr optMemoryLimit;
  850. StringAttr optPriority;
  851. StringAttr optComment;
  852. unsigned optMsToWait;
  853. unsigned optTimeLimit;
  854. unsigned optWarnTimeLimit;
  855. bool optNoReload;
  856. };
  857. IEclCommand *createEclQueriesCommand(const char *cmdname)
  858. {
  859. if (!cmdname || !*cmdname)
  860. return NULL;
  861. if (strieq(cmdname, "list"))
  862. return new EclCmdQueriesList();
  863. if (strieq(cmdname, "files"))
  864. return new EclCmdQueryFiles();
  865. if (strieq(cmdname, "config"))
  866. return new EclCmdQueriesConfig();
  867. if (strieq(cmdname, "copy"))
  868. return new EclCmdQueriesCopy();
  869. if (strieq(cmdname, "copy-set"))
  870. return new EclCmdQueriesCopyQueryset();
  871. return NULL;
  872. }
  873. //=========================================================================================
  874. class EclQueriesCMDShell : public EclCMDShell
  875. {
  876. public:
  877. EclQueriesCMDShell(int argc, const char *argv[], EclCommandFactory _factory, const char *_version)
  878. : EclCMDShell(argc, argv, _factory, _version)
  879. {
  880. }
  881. virtual void usage()
  882. {
  883. fprintf(stdout,"\nUsage:\n\n"
  884. "ecl queries <command> [command options]\n\n"
  885. " Queries Commands:\n"
  886. " list list queries on target cluster(s)\n"
  887. " files list the files currently used by a query\n"
  888. " config update query settings\n"
  889. " copy copy a query from one target cluster to another\n"
  890. " copy-set copy queries from one target cluster to another\n"
  891. );
  892. }
  893. };
  894. static int doMain(int argc, const char *argv[])
  895. {
  896. EclQueriesCMDShell processor(argc, argv, createEclQueriesCommand, BUILD_TAG);
  897. return processor.run();
  898. }
  899. int main(int argc, const char *argv[])
  900. {
  901. InitModuleObjects();
  902. queryStderrLogMsgHandler()->setMessageFields(0);
  903. unsigned exitCode = doMain(argc, argv);
  904. releaseAtoms();
  905. exit(exitCode);
  906. }