HMCDBAccessor.php 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286
  1. <?php
  2. class HMCDBAccessor {
  3. private $dbHandle;
  4. private $dbPath;
  5. private $logger;
  6. function __construct($db) {
  7. $this->dbPath = $db;
  8. $this->logger = new HMCLogger("HMCDBAccessor");
  9. $dsn = "sqlite:".$db;
  10. if (!file_exists($db)) {
  11. $this->logger->log_error("Sqlite db file does not exist, db=$db");
  12. throw new Exception("Could not find sqlite db file, db=$db");
  13. }
  14. $this->dbHandle = new PDO($dsn);
  15. if ($this->dbHandle === FALSE) {
  16. $this->logger->log_error("Could not open sqlite db, db=$db");
  17. throw new Exception("Could not open sqlite db, db=$db");
  18. }
  19. }
  20. private function getLastDBErrorAsString() {
  21. $error = "";
  22. $pdoError = $this->dbHandle->errorInfo();
  23. if (isset($pdoError) && is_array($pdoError)) {
  24. if (isset($pdoError[0])) {
  25. $error = "sqlStateErrorCode=".$pdoError[0];
  26. if (isset($pdoError[1])) {
  27. $error .= ", driverErrCode=".$pdoError[1];
  28. }
  29. if (isset($pdoError[1])) {
  30. $error .= ", driverErrMsg=".$pdoError[2];
  31. }
  32. }
  33. }
  34. else {
  35. $error = "Unknown DB error";
  36. }
  37. return $error;
  38. }
  39. /**
  40. * Create a new cluster
  41. * @param string $clusterName Cluster Name
  42. * @param string $version Version
  43. * @param string $state Current state
  44. * @return mixed
  45. * array (
  46. * "clusterName" => $clusterName,
  47. * "result" => 0,
  48. * "error" => "");
  49. */
  50. public function createCluster($clusterName, $version, $state) {
  51. LockAcquire();
  52. $query = "INSERT INTO Clusters "
  53. . " ( cluster_name, version, state ) "
  54. . " VALUES ( "
  55. . $this->dbHandle->quote($clusterName) . " , "
  56. . $this->dbHandle->quote($version) . " , "
  57. . $this->dbHandle->quote($state) . " )";
  58. $response = array ( "clusterName" => $clusterName,
  59. "result" => 0, "error" => "");
  60. $this->logger->log_trace("Running query: $query");
  61. $ret = $this->dbHandle->exec($query);
  62. if (FALSE === $ret) {
  63. $error = $this->getLastDBErrorAsString();
  64. $this->logger->log_error("Error when executing query"
  65. . ", query=".$query
  66. . ", error=".$error);
  67. $response["result"] = 1;
  68. $response["error"] = $error;
  69. LockRelease(); return $response;
  70. }
  71. LockRelease(); return $response;
  72. }
  73. /**
  74. * Update cluster state for a given clusterName
  75. * @param string $clusterName Cluster Name
  76. * @param string $state New state of cluster
  77. * @return mixed
  78. * array ( "state" => $clusterState,
  79. * "result" => 0,
  80. * "error" => "");
  81. */
  82. public function getClusterState ($clusterName) {
  83. LockAcquire();
  84. $query = "SELECT state FROM Clusters WHERE cluster_name = "
  85. . $this->dbHandle->quote($clusterName);
  86. $response = array ( "result" => 0, "error" => "");
  87. $this->logger->log_trace("Running query: $query");
  88. $pdoStmt = $this->dbHandle->query($query);
  89. if ($pdoStmt === FALSE) {
  90. $error = $this->getLastDBErrorAsString();
  91. $this->logger->log_error("Error when executing query"
  92. . ", query=".$query
  93. . ", error=".$error);
  94. $response["result"] = 1;
  95. $response["error"] = $error;
  96. LockRelease(); return $response;
  97. }
  98. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  99. if (isset($result) && is_array($result) && count($result) == 1) {
  100. $response["state"] = $result[0]["state"];
  101. LockRelease(); return $response;
  102. }
  103. $response["result"] = 1;
  104. $response["error"] = "Result is not set or not array or count is not 1 ".json_encode($result);
  105. LockRelease(); return $response;
  106. }
  107. /**
  108. * Update cluster state for a given clusterName
  109. * @param string $clusterName Cluster Name
  110. * @param string $state New state of cluster
  111. * @return mixed
  112. * array ( "clusterName" => $clusterName,
  113. * "clusterState" => $clusterState,
  114. * "oldClusterState" => $oldClusterState,
  115. * "result" => 0,
  116. * "error" => "");
  117. */
  118. public function setClusterState($clusterName, $state) {
  119. LockAcquire();
  120. $response = array ( "clusterName" => $clusterName,
  121. "result" => 0, "error" => "");
  122. $ret = $this->dbHandle->beginTransaction();
  123. if (!$ret) {
  124. $error = $this->getLastDBErrorAsString();
  125. $response["result"] = 1;
  126. $response["error"] = "Failed to start DB transaction, error=".$error;
  127. LockRelease(); return $response;
  128. }
  129. $query = "SELECT state FROM Clusters WHERE cluster_name = "
  130. . $this->dbHandle->quote($clusterName);
  131. $this->logger->log_trace("Running query: $query");
  132. $pdoStmt = $this->dbHandle->query($query);
  133. if ($pdoStmt === FALSE) {
  134. $error = $this->getLastDBErrorAsString();
  135. $this->dbHandle->rollBack();
  136. $this->logger->log_error("Error when executing query"
  137. . ", query=".$query
  138. . ", error=".$error);
  139. $response["result"] = 1;
  140. $response["error"] = $error;
  141. LockRelease(); return $response;
  142. }
  143. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  144. if (isset($result) && is_array($result) && count($result) == 1) {
  145. $response["oldClusterState"] = $result[0]["state"];
  146. $query = "UPDATE Clusters SET state = " . $this->dbHandle->quote($state)
  147. . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName);
  148. $this->logger->log_trace("Running query: $query");
  149. $ret = $this->dbHandle->exec($query);
  150. if (FALSE === $ret) {
  151. $error = $this->getLastDBErrorAsString();
  152. $this->dbHandle->rollBack();
  153. $this->logger->log_error("Error when executing query"
  154. . ", query=".$query
  155. . ", error=".$error);
  156. $response["result"] = 1;
  157. $response["error"] = $error;
  158. LockRelease(); return $response;
  159. }
  160. $ret = $this->dbHandle->commit();
  161. if (!$ret) {
  162. $error = $this->getLastDBErrorAsString();
  163. $response["result"] = 1;
  164. $response["error"] = "Failed to commit DB transaction, error=".$error;
  165. LockRelease(); return $response;
  166. }
  167. $response["clusterState"] = $state;
  168. LockRelease();
  169. return $response;
  170. }
  171. $this->dbHandle->rollBack();
  172. $response["result"] = 1;
  173. $response["error"] = "Could not find cluster name".$clusterName." in DB";
  174. LockRelease(); return $response;
  175. }
  176. /**
  177. * Get info on all clusters
  178. * @return mixed
  179. * array (
  180. * "result" => 0,
  181. * "error" => "",
  182. * "clusters" => array (
  183. * "$clusterName" => array (
  184. * "clusterName" => $clusterName,
  185. * "version" => $version,
  186. * "state" => $state
  187. * ),
  188. * ...
  189. * )
  190. * );
  191. */
  192. public function getAllClusters() {
  193. LockAcquire();
  194. $query = "SELECT cluster_name, version, state FROM Clusters";
  195. $response = array ( "result" => 0, "error" => "");
  196. $this->logger->log_trace("Running query: $query");
  197. $pdoStmt = $this->dbHandle->query($query);
  198. if ($pdoStmt === FALSE) {
  199. $error = $this->getLastDBErrorAsString();
  200. $this->logger->log_error("Error when executing query"
  201. . ", query=".$query
  202. . ", error=".$error);
  203. $response["result"] = 1;
  204. $response["error"] = $error;
  205. LockRelease(); return $response;
  206. }
  207. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  208. $response["clusters"] = array();
  209. foreach ($result as $entry) {
  210. $cluster = array();
  211. $cluster["clusterName"] = $entry["cluster_name"];
  212. $cluster["state"] = $entry["state"];
  213. $cluster["version"] = $entry["version"];
  214. $response["clusters"][$cluster["clusterName"]] = $cluster;
  215. }
  216. LockRelease(); return $response;
  217. }
  218. /////////////////////////////////////////// Services related APIs ////////////////////////////////////////////
  219. /**
  220. * Get the list of all possible services supported by the hadoop statck
  221. * @return mixed
  222. * array (
  223. * "result" => 0,
  224. * "error" => "",
  225. * "services" => array (
  226. * "$serviceName" => array (
  227. * "serviceName" => $serviceName,
  228. * "displayName" => $displayName,
  229. * "description" => $desc,
  230. * "attributes" => array( "isMaster" => boolean, "isClient" => boolean )
  231. * ),
  232. * ...
  233. * )
  234. * );
  235. */
  236. public function getAllServicesList() {
  237. LockAcquire();
  238. $response = array ( "result" => 0, "error" => "");
  239. $query = "SELECT service_name, description, display_name, attributes FROM Services";
  240. $this->logger->log_trace("Running query: $query");
  241. $pdoStmt = $this->dbHandle->query($query);
  242. if ($pdoStmt === FALSE) {
  243. $error = $this->getLastDBErrorAsString();
  244. $this->logger->log_error("Error when executing query"
  245. . ", query=".$query
  246. . ", error=".$error);
  247. $response["result"] = 1;
  248. $response["error"] = $error;
  249. LockRelease(); return $response;
  250. }
  251. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  252. $response["services"] = array();
  253. foreach ($result as $entry) {
  254. $svcInfo = array();
  255. $svcInfo["serviceName"] = $entry["service_name"];
  256. $svcInfo["displayName"] = $entry["display_name"];
  257. $svcInfo["description"] = $entry["description"];
  258. $svcInfo["attributes"] = json_decode($entry["attributes"], true);
  259. $response["services"][$svcInfo["serviceName"]] = $svcInfo;
  260. }
  261. LockRelease(); return $response;
  262. }
  263. /**
  264. * Get all components for all services that are supported.
  265. * @return
  266. * array (
  267. * "result" => 0,
  268. * "error" => "",
  269. * "services" => array (
  270. * "$serviceName" => array (
  271. * "serviceName" => $serviceName,
  272. * "components" => array (
  273. * "$componentName" => array (
  274. * "componentName" =>
  275. * "isMaster" =>
  276. * "isClient" =>
  277. * "displayName" =>
  278. * "description" =>
  279. * )
  280. * )
  281. * )
  282. * )
  283. * )
  284. */
  285. public function getAllServiceComponentsList() {
  286. LockAcquire();
  287. $response = array ( "result" => 0, "error" => "");
  288. $query = "SELECT service_name, component_name, display_name"
  289. . ", attributes, description FROM ServiceComponents";
  290. $this->logger->log_trace("Running query: $query");
  291. $pdoStmt = $this->dbHandle->query($query);
  292. if ($pdoStmt === FALSE) {
  293. $error = $this->getLastDBErrorAsString();
  294. $this->logger->log_error("Error when executing query"
  295. . ", query=".$query
  296. . ", error=".$error);
  297. $response["result"] = 1;
  298. $response["error"] = $error;
  299. LockRelease(); return $response;
  300. }
  301. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  302. $response["services"] = array();
  303. foreach ($result as $entry) {
  304. $serviceName = $entry["service_name"];
  305. if (!isset($response["services"]["$serviceName"])) {
  306. $response["services"][$serviceName] = array();
  307. $response["services"][$serviceName]["serviceName"] = $serviceName;
  308. $response["services"][$serviceName]["components"] = array();
  309. }
  310. $comp = array();
  311. $comp["serviceName"] = $serviceName;
  312. $comp["componentName"] = $entry["component_name"];
  313. $comp["displayName"] = $entry["display_name"];
  314. $comp["attributes"] = json_decode($entry["attributes"], true);
  315. /* $comp["isMaster"] and $comp["isClient"] are populated only for
  316. * backward compatibility (with when is_master and is_client were
  317. * fields in this table) and should be removed once all the code
  318. * that uses this method is fixed to look at
  319. * $comp["attributes"]["isMaster"] directly.
  320. */
  321. $comp["isMaster"] = FALSE;
  322. if (isset($comp["attributes"]["isMaster"])) {
  323. $comp["isMaster"] = $comp["attributes"]["isMaster"];
  324. }
  325. $comp["isClient"] = FALSE;
  326. if (isset($comp["attributes"]["isClient"])) {
  327. $comp["isClient"] = $comp["attributes"]["isClient"];
  328. }
  329. $response["services"][$serviceName]["components"]
  330. [$comp["componentName"]] = $comp;
  331. }
  332. LockRelease(); return $response;
  333. }
  334. /**
  335. * Get the list of all services that are available for the given clusterName
  336. * @return mixed
  337. * array (
  338. * "result" => 0,
  339. * "error" => "",
  340. * "clusterName" => $clusterName,
  341. * "services" => array (
  342. * "$serviceName" => array (
  343. * "serviceName" => $serviceName,
  344. * "state" => $state,
  345. * "desiredState" => $desiredState,
  346. * "isEnabled" => $isEnabled
  347. * ),
  348. * ...
  349. * )
  350. * );
  351. */
  352. public function getAllServicesInfo($clusterName) {
  353. LockAcquire();
  354. $query = "SELECT ServiceInfo.service_name as service_name"
  355. . ", ServiceInfo.state as state"
  356. . ", ServiceInfo.desired_state as desired_state"
  357. . ", ServiceInfo.is_enabled as is_enabled FROM ServiceInfo "
  358. . " LEFT JOIN Services ON ServiceInfo.service_name = Services.service_name "
  359. . " WHERE ServiceInfo.cluster_name = " . $this->dbHandle->quote($clusterName)
  360. . " ORDER BY Services.id ";
  361. $response = array ( "result" => 0, "error" => "");
  362. $this->logger->log_trace("Running query: $query");
  363. $pdoStmt = $this->dbHandle->query($query);
  364. if ($pdoStmt === FALSE) {
  365. $error = $this->getLastDBErrorAsString();
  366. $this->logger->log_error("Error when executing query"
  367. . ", query=".$query
  368. . ", error=".$error);
  369. $response["result"] = 1;
  370. $response["error"] = $error;
  371. LockRelease(); return $response;
  372. }
  373. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  374. $response["clusterName"] = $clusterName;
  375. $response["services"] = array();
  376. foreach ($result as $entry) {
  377. $svcInfo = array();
  378. $svcInfo["serviceName"] = $entry["service_name"];
  379. $svcInfo["state"] = $entry["state"];
  380. $svcInfo["desiredState"] = $entry["desired_state"];
  381. $svcInfo["isEnabled"] = $entry["is_enabled"];
  382. $response["services"][$svcInfo["serviceName"]] = $svcInfo;
  383. }
  384. LockRelease(); return $response;
  385. }
  386. /**
  387. * Get information related to a service
  388. * @param string $clusterName
  389. * @param string $serviceName
  390. * @return mixed
  391. * array (
  392. * "result" => 0,
  393. * "error" => "",
  394. * "clusterName" => $clusterName,
  395. * "serviceName" => $serviceName,
  396. * "isEnabled" => $isEnabled,
  397. * "state" => $state,
  398. * "desiredState" => $desiredState
  399. * )
  400. */
  401. public function getServiceInfo($clusterName, $serviceName) {
  402. LockAcquire();
  403. $error = "";
  404. $query = "SELECT is_enabled, state, desired_state FROM ServiceInfo "
  405. . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
  406. . " AND service_name = " . $this->dbHandle->quote($serviceName);
  407. $response = array ( "result" => 0, "error" => "");
  408. $response["clusterName"] = $clusterName;
  409. $response["serviceName"] = $serviceName;
  410. $this->logger->log_trace("Running query: $query");
  411. $pdoStmt = $this->dbHandle->query($query);
  412. if ($pdoStmt === FALSE) {
  413. $error = $this->getLastDBErrorAsString();
  414. $this->logger->log_error("Error when executing query"
  415. . ", query=".$query
  416. . ", error=".$error);
  417. $response["result"] = 1;
  418. $response["error"] = $error;
  419. LockRelease(); return $response;
  420. }
  421. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  422. if (isset($result) && is_array($result) && count($result) == 1) {
  423. $response["isEnabled"] = $result[0]["is_enabled"];
  424. $response["state"] = $result[0]["state"];
  425. $response["desiredState"] = $result[0]["desired_state"];
  426. LockRelease(); return $response;
  427. }
  428. $response["result"] = 1;
  429. $response["error"] = "Could not find service in DB";
  430. LockRelease(); return $response;
  431. }
  432. /**
  433. * Set Service Desired State
  434. * @param clusterName Cluster Name
  435. * @param serviceName Service Name
  436. * @param desiredState New Desired State
  437. * @return mixed
  438. * array (
  439. * "result" => 0,
  440. * "error" => "",
  441. * "desiredState" => $desiredState,
  442. * "oldDesiredState" => $oldDesiredState,
  443. * "clusterName" =>
  444. * "serviceName" =>
  445. * )
  446. */
  447. public function setServiceDesiredState($clusterName, $serviceName, $desiredState) {
  448. LockAcquire();
  449. $response = array ( "result" => 0, "error" => "",
  450. "clusterName" => $clusterName, "serviceName" => $serviceName);
  451. $error = "";
  452. $ret = $this->dbHandle->beginTransaction();
  453. if (!$ret) {
  454. $error = $this->getLastDBErrorAsString();
  455. $response["result"] = 1;
  456. $response["error"] = "Failed to start DB transaction, error=".$error;
  457. LockRelease(); return $response;
  458. }
  459. $query = "SELECT desired_state FROM ServiceInfo"
  460. . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
  461. . " AND service_name = " . $this->dbHandle->quote($serviceName);
  462. $this->logger->log_trace("Running query: $query");
  463. $pdoStmt = $this->dbHandle->query($query);
  464. if ($pdoStmt === FALSE) {
  465. $error = $this->getLastDBErrorAsString();
  466. $this->dbHandle->rollBack();
  467. $this->logger->log_error("Error when executing query"
  468. . ", query=".$query
  469. . ", error=".$error);
  470. $response["result"] = 1;
  471. $response["error"] = $error;
  472. LockRelease(); return $response;
  473. }
  474. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  475. if (isset($result) && is_array($result) && count($result) == 1) {
  476. $response["oldDesiredState"] = $result[0]["desired_state"];
  477. $query = "UPDATE ServiceInfo SET desired_state = "
  478. . $this->dbHandle->quote($desiredState)
  479. . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
  480. . " AND service_name = " . $this->dbHandle->quote($serviceName);
  481. $this->logger->log_trace("Running query: $query");
  482. $ret = $this->dbHandle->exec($query);
  483. if (FALSE === $ret) {
  484. $error = $this->getLastDBErrorAsString();
  485. $this->dbHandle->rollBack();
  486. $this->logger->log_error("Error when executing query"
  487. . ", query=".$query
  488. . ", error=".$error);
  489. $response["result"] = 1;
  490. $response["error"] = $error;
  491. LockRelease(); return $response;
  492. }
  493. $ret = $this->dbHandle->commit();
  494. if (!$ret) {
  495. $error = $this->getLastDBErrorAsString();
  496. $response["result"] = 1;
  497. $response["error"] = "Failed to commit DB transaction, error=".$error;
  498. LockRelease(); return $response;
  499. }
  500. $response["desiredState"] = $desiredState;
  501. LockRelease(); return $response;
  502. }
  503. $this->dbHandle->rollBack();
  504. $response["result"] = 1;
  505. $response["error"] = "Could not find service in DB";
  506. LockRelease(); return $response;
  507. }
  508. /**
  509. * Set Service State
  510. * @param clusterName Cluster Name
  511. * @param serviceName Service Name
  512. * @param desiredState New Desired State
  513. * @return mixed
  514. * array (
  515. * "result" => 0,
  516. * "error" => "",
  517. * "state" => $state,
  518. * "oldState" => $oldState,
  519. * "clusterName" =>
  520. * "serviceName" =>
  521. * )
  522. */
  523. public function setServiceState($clusterName, $serviceName, $state) {
  524. LockAcquire();
  525. $response = array ( "result" => 0, "error" => "",
  526. "clusterName" => $clusterName, "serviceName" => $serviceName);
  527. $error = "";
  528. $ret = $this->dbHandle->beginTransaction();
  529. if (!$ret) {
  530. $error = $this->getLastDBErrorAsString();
  531. $response["result"] = 1;
  532. $response["error"] = "Failed to start DB transaction, error=".$error;
  533. LockRelease(); return $response;
  534. }
  535. $query = "SELECT state FROM ServiceInfo"
  536. . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
  537. . " AND service_name = " . $this->dbHandle->quote($serviceName);
  538. $this->logger->log_trace("Running query: $query");
  539. $pdoStmt = $this->dbHandle->query($query);
  540. if ($pdoStmt === FALSE) {
  541. $error = $this->getLastDBErrorAsString();
  542. $this->dbHandle->rollBack();
  543. $this->logger->log_error("Error when executing query"
  544. . ", query=".$query
  545. . ", error=".$error);
  546. $response["result"] = 1;
  547. $response["error"] = $error;
  548. LockRelease(); return $response;
  549. }
  550. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  551. if (isset($result) && is_array($result) && count($result) == 1) {
  552. $response["oldState"] = $result[0]["state"];
  553. $query = "UPDATE ServiceInfo SET state = "
  554. . $this->dbHandle->quote($state)
  555. . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
  556. . " AND service_name = " . $this->dbHandle->quote($serviceName);
  557. $this->logger->log_trace("Running query: $query");
  558. $ret = $this->dbHandle->exec($query);
  559. if (FALSE === $ret) {
  560. $error = $this->getLastDBErrorAsString();
  561. $this->dbHandle->rollBack();
  562. $this->logger->log_error("Error when executing query"
  563. . ", query=".$query
  564. . ", error=".$error);
  565. $response["result"] = 1;
  566. $response["error"] = $error;
  567. LockRelease(); return $response;
  568. }
  569. $ret = $this->dbHandle->commit();
  570. if (!$ret) {
  571. $error = $this->getLastDBErrorAsString();
  572. $response["result"] = 1;
  573. $response["error"] = "Failed to commit DB transaction, error=".$error;
  574. LockRelease(); return $response;
  575. }
  576. $response["state"] = $state;
  577. LockRelease(); return $response;
  578. }
  579. $this->dbHandle->rollBack();
  580. $response["result"] = 1;
  581. $response["error"] = "Could not find service in DB";
  582. LockRelease(); return $response;
  583. }
  584. /**
  585. * Add list of services for cluster
  586. * @param string $clusterName
  587. * @param mixed $services
  588. * array (
  589. * array (
  590. * "serviceName" => ,
  591. * "state" =>
  592. * "desiredState" =>
  593. * "isEnabled" =>
  594. * )
  595. * )
  596. * @return mixed
  597. * array (
  598. * "result" => 0,
  599. * "error" => ""
  600. * )
  601. */
  602. public function addServicesToCluster($clusterName, $services) {
  603. $response = array ( "result" => 0, "error" => "",
  604. "clusterName" => $clusterName);
  605. $error = "";
  606. $supportedServices = $this->getAllServicesList();
  607. if ($supportedServices === FALSE
  608. || $supportedServices["result"] != 0) {
  609. return $supportedServices;
  610. }
  611. LockAcquire();
  612. // sanitize input
  613. $sanitizedSvcs = array();
  614. foreach ($services as $service) {
  615. if (!isset($service["serviceName"])
  616. || $service["serviceName"] == "") {
  617. $response["error"] = "Invalid arguments";
  618. $response["result"] = 1;
  619. LockRelease(); return $response;
  620. }
  621. if (!isset($supportedServices["services"][$service["serviceName"]])) {
  622. $response["error"] = "Invalid service - not supported, svc="
  623. . $service["serviceName"];
  624. $response["result"] = 1;
  625. LockRelease(); return $response;
  626. }
  627. if (!isset($service["state"])) {
  628. $service["state"] = "";
  629. }
  630. if (!isset($service["desiredState"])) {
  631. $service["desiredState"] = "";
  632. }
  633. if (!isset($service["isEnabled"])) {
  634. $service["isEnabled"] = 0;
  635. }
  636. else {
  637. if ($service["isEnabled"]) {
  638. $service["isEnabled"] = 1;
  639. }
  640. else {
  641. $service["isEnabled"] = 0;
  642. }
  643. }
  644. $sanitizedSvcs[$service["serviceName"]] = $service;
  645. }
  646. $ret = $this->dbHandle->beginTransaction();
  647. if (!$ret) {
  648. $error = $this->getLastDBErrorAsString();
  649. $response["result"] = 1;
  650. $response["error"] = "Failed to start DB transaction, error=".$error;
  651. LockRelease(); return $response;
  652. }
  653. foreach ($sanitizedSvcs as $service) {
  654. $query = "INSERT INTO ServiceInfo "
  655. . " ( cluster_name, service_name, state, desired_state, is_enabled ) "
  656. . " VALUES "
  657. . " ("
  658. . $this->dbHandle->quote($clusterName) . " , "
  659. . $this->dbHandle->quote($service["serviceName"]) . " , "
  660. . $this->dbHandle->quote($service["state"]) . " , "
  661. . $this->dbHandle->quote($service["desiredState"]) . " , "
  662. . $service["isEnabled"]
  663. . " )";
  664. $this->logger->log_trace("Running query: $query");
  665. $ret = $this->dbHandle->exec($query);
  666. if (FALSE === $ret) {
  667. $error = $this->getLastDBErrorAsString();
  668. $this->dbHandle->rollBack();
  669. $this->logger->log_error("Error when executing query"
  670. . ", query=".$query
  671. . ", error=".$error);
  672. $response["result"] = 1;
  673. $response["error"] = $error;
  674. LockRelease(); return $response;
  675. }
  676. }
  677. $ret = $this->dbHandle->commit();
  678. if (!$ret) {
  679. $error = $this->getLastDBErrorAsString();
  680. $response["result"] = 1;
  681. $response["error"] = "Failed to commit DB transaction, error=".$error;
  682. LockRelease(); return $response;
  683. }
  684. LockRelease(); return $response;
  685. }
  686. /////////////////////////////////////////// End of services related APIs ////////////////////////////////////////////
  687. /////////////////////////////////////////// Hosts related APIs ////////////////////////////////////////////
  688. /**
  689. * Add new hosts to Hosts table for a given cluster.
  690. * Replaces existing hosts if needed
  691. * @param string $clusterName ClusterName
  692. * @param mixed $hostsInfo Mixed array containing host info
  693. * array (
  694. * array (
  695. * "hostName" =>
  696. * "ip" =>
  697. * "totalMem" =>
  698. * "cpuCount" =>
  699. * "osArch" =>
  700. * "osType" =>
  701. * "os" =>
  702. * "disksInfo" =>
  703. * "discoveryStatus" =>
  704. * "badHealthReason" =>
  705. * "attributes" => array ( ... )
  706. * ),
  707. * )
  708. * @return mixed
  709. * array ( "clusterName" => $clusterName,
  710. * "hosts" => array ($hostName1, $hostName2 ...),
  711. * "result" => 0,
  712. * "error" => ""
  713. * );
  714. */
  715. public function addHostsToCluster($clusterName, $hostsInfo) {
  716. LockAcquire();
  717. $response = array( "result" => 0, "error" => "");
  718. if (empty($hostsInfo)) {
  719. LockRelease(); return $response;
  720. }
  721. $hosts = array();
  722. if (empty($hostsInfo)) {
  723. return $response;
  724. }
  725. foreach ($hostsInfo as $hostInfo) {
  726. if (!isset($hostInfo["hostName"])
  727. || !isset($hostInfo["discoveryStatus"])) {
  728. $response["result"] = 1;
  729. $response["error"] = "Invalid arguments";
  730. LockRelease(); return $response;
  731. }
  732. // sanitize input
  733. if (!isset($hostInfo["totalMem"])) {
  734. $hostInfo["totalMem"] = 0;
  735. }
  736. if (!isset($hostInfo["cpuCount"])) {
  737. $hostInfo["cpuCount"] = 0;
  738. }
  739. if (!isset($hostInfo["osArch"])) {
  740. $hostInfo["os_arch"] = "";
  741. }
  742. if (!isset($hostInfo["osType"])) {
  743. $hostInfo["osType"] = "";
  744. }
  745. if (!isset($hostInfo["os"])) {
  746. $hostInfo["os"] = "";
  747. }
  748. if (!isset($hostInfo["disksInfo"])) {
  749. $hostInfo["disksInfo"] = "";
  750. }
  751. if (!isset($hostInfo["badHealthReason"])) {
  752. $hostInfo["badHealthReason"] = "";
  753. }
  754. if (!isset($hostInfo["attributes"])) {
  755. $hostInfo["attributes"] = array();
  756. }
  757. $hostInfo["attributes"] = json_encode($hostInfo["attributes"]);
  758. array_push($hosts, $hostInfo);
  759. }
  760. $response["clusterName"] = $clusterName;
  761. $ret = $this->dbHandle->beginTransaction();
  762. if (!$ret) {
  763. $error = $this->getLastDBErrorAsString();
  764. $response["result"] = 1;
  765. $response["error"] = "Failed to start DB transaction, error=".$error;
  766. LockRelease(); return $response;
  767. }
  768. foreach ($hosts as $hostInfo) {
  769. $this->logger->log_debug(json_encode(($hostInfo)));
  770. $query = " INSERT OR REPLACE INTO Hosts "
  771. . " ( cluster_name, host_name, ip, total_mem, cpu_count, "
  772. . " os_arch, os_type, os, disks_info, discovery_status, "
  773. . " bad_health_reason, attributes ) "
  774. . " VALUES "
  775. . " ("
  776. . $this->dbHandle->quote($clusterName) . " , "
  777. . $this->dbHandle->quote($hostInfo["hostName"]) . " , "
  778. . $this->dbHandle->quote($hostInfo["ip"]) . " , "
  779. . $this->dbHandle->quote($hostInfo["totalMem"]) . " , "
  780. . $this->dbHandle->quote($hostInfo["cpuCount"]) . " , "
  781. . $this->dbHandle->quote($hostInfo["osArch"]) . " , "
  782. . $this->dbHandle->quote($hostInfo["osType"]) . " , "
  783. . $this->dbHandle->quote($hostInfo["os"]) . " , "
  784. . $this->dbHandle->quote($hostInfo["disksInfo"]) . " , "
  785. . $this->dbHandle->quote($hostInfo["discoveryStatus"]) . " , "
  786. . $this->dbHandle->quote($hostInfo["badHealthReason"]) . " , "
  787. . $this->dbHandle->quote($hostInfo["attributes"])
  788. . " ) ";
  789. $this->logger->log_trace("Running query: $query");
  790. $ret = $this->dbHandle->exec($query);
  791. if (FALSE === $ret) {
  792. $error = $this->getLastDBErrorAsString();
  793. $this->dbHandle->rollBack();
  794. $this->logger->log_error("Error when executing query"
  795. . ", query=".$query
  796. . ", error=".$error);
  797. $response["result"] = 1;
  798. $response["error"] = $error;
  799. LockRelease(); return $response;
  800. }
  801. }
  802. $ret = $this->dbHandle->commit();
  803. if (!$ret) {
  804. $error = $this->getLastDBErrorAsString();
  805. $this->logger->log_error("Failed to commit DB transaction, error=".$error);
  806. $response["result"] = 1;
  807. $response["error"] = "Failed to commit DB transaction, error=".$error;
  808. LockRelease(); return $response;
  809. }
  810. $error = "";
  811. $query = "SELECT host_name FROM Hosts WHERE "
  812. . " ( cluster_name = " . $this->dbHandle->quote($clusterName) . " ) AND ( ";
  813. $index = 0;
  814. foreach ($hostsInfo as $hostInfo) {
  815. if ($index != 0) {
  816. $query .= " OR ";
  817. }
  818. $index++;
  819. $query .= " host_name = " . $this->dbHandle->quote($hostInfo["hostName"]);
  820. }
  821. $query .= " ) ";
  822. $this->logger->log_trace("Running query: $query");
  823. $pdoStmt = $this->dbHandle->query($query);
  824. if ($pdoStmt === FALSE) {
  825. $error = $this->getLastDBErrorAsString();
  826. $this->logger->log_error("Error when executing query"
  827. . ", query=".$query
  828. . ", error=".$error);
  829. $response["result"] = 1;
  830. $response["error"] = $error;
  831. LockRelease(); return $response;
  832. }
  833. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  834. $response["hosts"] = array();
  835. if (isset($result) && is_array($result) && count($result) > 0) {
  836. foreach ($result as $entry) {
  837. $host["hostName"] = $entry["host_name"];
  838. array_push($response["hosts"], $entry["host_name"]);
  839. }
  840. LockRelease(); return $response;
  841. }
  842. $response["result"] = 1;
  843. $response["error"] = "Invalid result set returned from DB";
  844. LockRelease(); return $response;
  845. }
  846. /**
  847. * Add new hosts to Hosts table for a given cluster.
  848. * Replaces existing hosts if needed
  849. * @param string $clusterName ClusterName
  850. * @param mixed $hostsInfo Mixed array containing host info
  851. * array (
  852. * "$hostName" => array (
  853. * "discoveryStatus" =>
  854. * "badHealthReason" =>
  855. * ),
  856. * )
  857. * @return mixed
  858. * array (
  859. * "result" => 0,
  860. * "error" => "",
  861. * "sql" => array ( "rowsChanged" => 1 )
  862. * )
  863. */
  864. public function updateHostDiscoveryStatus($clusterName, $hostsInfo) {
  865. LockAcquire();
  866. $response = array( "result" => 0, "error" => "");
  867. $hosts = array();
  868. foreach ($hostsInfo as $hostName => $hostInfo) {
  869. if (!isset($hostInfo["discoveryStatus"])) {
  870. $response["result"] = 1;
  871. $response["error"] = "Invalid arguments";
  872. LockRelease(); return $response;
  873. }
  874. if (!isset($hostInfo["badHealthReason"])) {
  875. $hostInfo["badHealthReason"] = "";
  876. }
  877. $hosts[$hostName] = $hostInfo;
  878. }
  879. $ret = $this->dbHandle->beginTransaction();
  880. if (!$ret) {
  881. $error = $this->getLastDBErrorAsString();
  882. $response["result"] = 1;
  883. $response["error"] = "Failed to start DB transaction, error=".$error;
  884. LockRelease(); return $response;
  885. }
  886. $rowsChanged = 0;
  887. foreach ($hosts as $hostName => $hostInfo) {
  888. $this->logger->log_debug(json_encode(($hostInfo)));
  889. $query = " UPDATE Hosts SET "
  890. . " discovery_status = "
  891. . $this->dbHandle->quote($hostInfo["discoveryStatus"])
  892. . ", bad_health_reason = "
  893. . $this->dbHandle->quote($hostInfo["badHealthReason"])
  894. . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
  895. . " AND host_name = " . $this->dbHandle->quote($hostName);
  896. $this->logger->log_trace("Running query: $query");
  897. $ret = $this->dbHandle->exec($query);
  898. if (FALSE === $ret) {
  899. $error = $this->getLastDBErrorAsString();
  900. $this->dbHandle->rollBack();
  901. $this->logger->log_error("Error when executing query"
  902. . ", query=".$query
  903. . ", error=".$error);
  904. $response["result"] = 1;
  905. $response["error"] = $error;
  906. LockRelease(); return $response;
  907. }
  908. $rowsChanged += $ret;
  909. }
  910. $ret = $this->dbHandle->commit();
  911. if (!$ret) {
  912. $error = $this->getLastDBErrorAsString();
  913. $this->logger->log_error("Failed to commit DB transaction, error=".$error);
  914. $response["result"] = 1;
  915. $response["error"] = "Failed to commit DB transaction, error=".$error;
  916. LockRelease(); return $response;
  917. }
  918. $response["sql"] = array ( "rowsChanged" => $rowsChanged);
  919. LockRelease(); return $response;
  920. }
  921. /**
  922. * Get information on all hosts
  923. * @param string $clusterName
  924. * @param mixed $filter
  925. * - only supports equal operand
  926. * - only supports discoveryStatus filter
  927. * - array (
  928. * "=" => array ( "discoveryStatus" => "SUCCESS" )
  929. * )
  930. * - format
  931. * "$operand" => array ( "$column1" => "$value1", ... )
  932. * @param mixed $order order by particular column
  933. * - only supports hostName, ip, totalMem, cpuCount, osArch, osType
  934. * array (
  935. * "sortColumn" => "totalMem",
  936. * "sortOrder" => "ASC/DESC"
  937. * )
  938. * - optionally, an array of [ "sortColumn" => $sortColumn, "sortOrder" => $sortOrder]
  939. * can be used
  940. *
  941. * @return mixed
  942. * array (
  943. * "result" => 0,
  944. * "error" => "",
  945. * "clusterName" => $clusterName,
  946. * "hosts" => array (
  947. * array (
  948. * "hostName" =>
  949. * "ip" =>
  950. * "totalMem" =>
  951. * "cpuCount" =>
  952. * "osArch" =>
  953. * "osType" =>
  954. * "os" =>
  955. * "disksInfo" =>
  956. * "discoveryStatus" =>
  957. * "badHealthReason" =>
  958. * "attributes" => array ( ... )
  959. * ),
  960. * ...
  961. * )
  962. * )
  963. */
  964. public function getAllHostsInfo($clusterName, $filter = array(),
  965. $order = array()) {
  966. LockAcquire();
  967. $error = "";
  968. $query = "SELECT "
  969. . " host_name, ip, total_mem, "
  970. . " cpu_count, os_arch, os_type, os, disks_info, "
  971. . " discovery_status, bad_health_reason, attributes "
  972. . " FROM Hosts WHERE cluster_name = "
  973. . $this->dbHandle->quote($clusterName);
  974. if (is_array($filter) && !empty($filter)) {
  975. foreach ($filter as $operand => $cols) {
  976. if ($operand == "=" || $operand == "!=") {
  977. foreach ($cols as $columnName => $value) {
  978. if ($columnName == "discoveryStatus") {
  979. $query .= " AND discovery_status " . $operand . " "
  980. . $this->dbHandle->quote($value);
  981. }
  982. }
  983. }
  984. }
  985. }
  986. $using_sort = FALSE;
  987. if (isset($order) && is_array($order)) {
  988. if (sizeof($order) > 0) {
  989. $query .= " ORDER BY ";
  990. $using_sort = TRUE;
  991. }
  992. while (sizeof($order) > 0) {
  993. // is it an array of arrays, for an array of sortColumn and sortOrder?
  994. if (is_array($order[0])) {
  995. $this->logger->log_debug('yo');
  996. $orderItem = array_shift($order);
  997. } else {
  998. $orderItem["sortColumn"] = array_shift($order);
  999. $orderItem["sortOrder"] = array_shift($order);
  1000. }
  1001. if (isset($orderItem["sortColumn"])) {
  1002. if ($orderItem["sortColumn"] == "hostName") {
  1003. $query .= "host_name";
  1004. }
  1005. else if ($orderItem["sortColumn"] == "ip") {
  1006. $query .= "ip";
  1007. }
  1008. else if ($orderItem["sortColumn"] == "totalMem") {
  1009. $query .= "total_mem";
  1010. }
  1011. else if ($orderItem["sortColumn"] == "cpuCount") {
  1012. $query .= "cpu_count";
  1013. }
  1014. else if ($orderItem["sortColumn"] == "osArch") {
  1015. $query .= "os_arch";
  1016. }
  1017. else if ($orderItem["sortColumn"] == "osType") {
  1018. $query .= "os_type";
  1019. }
  1020. if (isset($orderItem["sortOrder"])) {
  1021. $query .= " ".$orderItem["sortOrder"];
  1022. if (sizeof($order) > 0) {
  1023. $query .= ',';
  1024. }
  1025. }
  1026. }
  1027. }
  1028. }
  1029. $response = array ( "result" => 0, "error" => "");
  1030. $this->logger->log_trace("Running query: $query");
  1031. $pdoStmt = $this->dbHandle->query($query);
  1032. if ($pdoStmt === FALSE) {
  1033. $error = $this->getLastDBErrorAsString();
  1034. $this->logger->log_error("Error when executing query"
  1035. . ", query=".$query
  1036. . ", error=".$error);
  1037. $response["result"] = 1;
  1038. $response["error"] = $error;
  1039. LockRelease(); return $response;
  1040. }
  1041. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  1042. $response["hosts"] = array();
  1043. $response["clusterName"] = $clusterName;
  1044. foreach ($result as $entry) {
  1045. $host = array();
  1046. $host["hostName"] = $entry["host_name"];
  1047. $host["ip"] = $entry["ip"];
  1048. $host["totalMem"] = $entry["total_mem"];
  1049. $host["cpuCount"] = $entry["cpu_count"];
  1050. $host["osArch"] = $entry["os_arch"];
  1051. $host["osType"] = $entry["os_type"];
  1052. $host["os"] = $entry["os"];
  1053. $host["disksInfo"] = $entry["disks_info"];
  1054. $host["discoveryStatus"] = $entry["discovery_status"];
  1055. $host["badHealthReason"] = $entry["bad_health_reason"];
  1056. $host["attributes"] = json_decode($entry["attributes"], true);
  1057. array_push($response["hosts"], $host);
  1058. }
  1059. LockRelease(); return $response;
  1060. }
  1061. /**
  1062. * Get information on a specified host
  1063. * @param string $clusterName
  1064. * @param string $hostName
  1065. * @return mixed
  1066. * array (
  1067. * "result" => 0,
  1068. * "error" => "",
  1069. * "clusterName" => $clusterName,
  1070. * "hostName" =>
  1071. * "ip" =>
  1072. * "totalMem" =>
  1073. * "cpuCount" =>
  1074. * "osArch" =>
  1075. * "osType" =>
  1076. * "os" =>
  1077. * "disksInfo" =>
  1078. * "discoveryStatus" =>
  1079. * "badHealthReason" =>
  1080. * "attributes" => array ( ... )
  1081. * )
  1082. */
  1083. public function getHostInfo($clusterName, $hostName) {
  1084. LockAcquire();
  1085. $error = "";
  1086. $query = "SELECT "
  1087. . " host_name, ip, total_mem, "
  1088. . " cpu_count, os_arch, os_type, os, disks_info, "
  1089. . " discovery_status, bad_health_reason, attributes "
  1090. . " FROM Hosts WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
  1091. . " AND host_name = " . $this->dbHandle->quote($hostName);
  1092. $response = array ( "result" => 0, "error" => "");
  1093. $this->logger->log_trace("Running query: $query");
  1094. $pdoStmt = $this->dbHandle->query($query);
  1095. if ($pdoStmt === FALSE) {
  1096. $error = $this->getLastDBErrorAsString();
  1097. $this->logger->log_error("Error when executing query"
  1098. . ", query=".$query
  1099. . ", error=".$error);
  1100. $response["result"] = 1;
  1101. $response["error"] = $error;
  1102. LockRelease(); return $response;
  1103. }
  1104. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  1105. $response["clusterName"] = $clusterName;
  1106. if (isset($result) && is_array($result) && count($result) == 1) {
  1107. $entry = $result[0];
  1108. $response["hostName"] = $entry["host_name"];
  1109. $response["ip"] = $entry["ip"];
  1110. $response["totalMem"] = $entry["total_mem"];
  1111. $response["cpuCount"] = $entry["cpu_count"];
  1112. $response["osArch"] = $entry["os_arch"];
  1113. $response["osType"] = $entry["os_type"];
  1114. $response["os"] = $entry["os"];
  1115. $response["disksInfo"] = $entry["disks_info"];
  1116. $response["discoveryStatus"] = $entry["discovery_status"];
  1117. $response["badHealthReason"] = $entry["bad_health_reason"];
  1118. $response["attributes"] = json_decode($entry["attributes"], true);
  1119. LockRelease(); return $response;
  1120. }
  1121. $response["result"] = 1;
  1122. $response["error"] = "Could not find host in DB";
  1123. LockRelease(); return $response;
  1124. }
  1125. /////////////////////////////////////////// End of hosts related APIs ////////////////////////////////////////////
  1126. /////////////////////////////////////////// Configuration related APIs ////////////////////////////////////////////
  1127. /**
  1128. * Get meta info about all config properties
  1129. * @return mixed
  1130. * array (
  1131. * "result" => 0,
  1132. * "error" => "",
  1133. * "configs" => array(
  1134. * "key" => array (
  1135. * "key" => $key,
  1136. * "displayName" => $displayName,
  1137. * "serviceName" => $serviceName
  1138. * "description" => $description,
  1139. * "displayType" => $displayType,
  1140. * "displayAttributes" => array ( ... )
  1141. * "value" => $default_value
  1142. * )
  1143. * .....
  1144. * );
  1145. */
  1146. public function getConfigPropertiesMetaInfo() {
  1147. LockAcquire();
  1148. $error = "";
  1149. $query = "SELECT "
  1150. . " key, display_name, service_name, description, default_value"
  1151. . ", display_type, display_attributes "
  1152. . " FROM ConfigProperties";
  1153. $response = array ( "result" => 0, "error" => "", "configs" => array());
  1154. $this->logger->log_trace("Running query: $query");
  1155. $pdoStmt = $this->dbHandle->query($query);
  1156. if ($pdoStmt === FALSE) {
  1157. $error = $this->getLastDBErrorAsString();
  1158. $this->logger->log_error("Error when executing query"
  1159. . ", query=".$query
  1160. . ", error=".$error);
  1161. $response["result"] = 1;
  1162. $response["error"] = $error;
  1163. LockRelease(); return $response;
  1164. }
  1165. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  1166. foreach ($result as $entry) {
  1167. $prop = array();
  1168. $prop["key"] = $entry["key"];
  1169. $prop["displayName"] = $entry["display_name"];
  1170. $prop["serviceName"] = $entry["service_name"];
  1171. $prop["description"] = $entry["description"];
  1172. $prop["displayType"] = $entry["display_type"];
  1173. $prop["displayAttributes"] = json_decode($entry["display_attributes"], true);
  1174. $prop["value"] = $entry["default_value"];
  1175. $response["configs"][$prop["key"]] = $prop;
  1176. }
  1177. LockRelease(); return $response;
  1178. }
  1179. /**
  1180. * Get all configuration properties for a given cluster
  1181. * @param string $clusterName
  1182. * @return mixed
  1183. * array (
  1184. * "result" => 0,
  1185. * "error" => "",
  1186. * "clusterName" => $clusterName,
  1187. * "properties" => array (
  1188. * "key1" => "val1",
  1189. * "key2" => "val2",
  1190. * ...
  1191. * )
  1192. * )
  1193. */
  1194. public function getServiceConfig($clusterName) {
  1195. LockAcquire();
  1196. $error = "";
  1197. $query = "SELECT key, value FROM ServiceConfig "
  1198. . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName);
  1199. $response = array ( "result" => 0, "error" => "");
  1200. $this->logger->log_trace("Running query: $query");
  1201. $pdoStmt = $this->dbHandle->query($query);
  1202. if ($pdoStmt === FALSE) {
  1203. $error = $this->getLastDBErrorAsString();
  1204. $this->logger->log_error("Error when executing query"
  1205. . ", query=".$query
  1206. . ", error=".$error);
  1207. $response["result"] = 1;
  1208. $response["error"] = $error;
  1209. LockRelease(); return $response;
  1210. }
  1211. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  1212. $response["properties"] = array();
  1213. $response["clusterName"] = $clusterName;
  1214. foreach ($result as $entry) {
  1215. $response["properties"][$entry["key"]] = $entry["value"];
  1216. }
  1217. LockRelease(); return $response;
  1218. }
  1219. /**
  1220. * Update config properties for a given cluster
  1221. * Updates prop key if it exists or inserts a new entry if not found.
  1222. * @param string $clusterName
  1223. * @param mixed $config
  1224. * array of properties as key-val pairs
  1225. * $config = array ( "prop_key1" => "prop_val1", ... );
  1226. * @return mixed
  1227. * array (
  1228. * "result" => 0,
  1229. * "error" => "",
  1230. * "sql" => array ( "rowsChanged" => 1 )
  1231. * )
  1232. */
  1233. public function updateServiceConfigs($clusterName, $config) {
  1234. LockAcquire();
  1235. $response = array ( "result" => 0, "error" => "");
  1236. if (count($config) == 0) {
  1237. // no-op
  1238. LockRelease(); return $response;
  1239. }
  1240. $error = "";
  1241. $rowsChanged = 0;
  1242. $ret = $this->dbHandle->beginTransaction();
  1243. if (!$ret) {
  1244. $error = $this->getLastDBErrorAsString();
  1245. $response["result"] = 1;
  1246. $response["error"] = "Failed to start DB transaction, error=".$error;
  1247. LockRelease(); return $response;
  1248. }
  1249. foreach ($config as $key=>$val) {
  1250. $query = " INSERT OR REPLACE INTO ServiceConfig "
  1251. . " ( cluster_name, key, value ) "
  1252. . " VALUES "
  1253. . " ( " . $this->dbHandle->quote($clusterName) . ", "
  1254. . $this->dbHandle->quote($key) . ", "
  1255. . $this->dbHandle->quote($val) . " ) ";
  1256. $this->logger->log_trace("Running query: $query");
  1257. $ret = $this->dbHandle->exec($query);
  1258. if (FALSE === $ret) {
  1259. $error = $this->getLastDBErrorAsString();
  1260. $ret = $this->dbHandle->rollBack();
  1261. $this->logger->log_error("Error when executing query"
  1262. . ", query=".$query
  1263. . ", error=".$error);
  1264. $response["result"] = 1;
  1265. $response["error"] = $error;
  1266. LockRelease(); return $response;
  1267. }
  1268. $rowsChanged += $ret;
  1269. }
  1270. $ret = $this->dbHandle->commit();
  1271. if (!$ret) {
  1272. $error = $this->getLastDBErrorAsString();
  1273. $this->logger->log_error("Failed to commit DB transaction, error=".$error);
  1274. $response["result"] = 1;
  1275. $response["error"] = "Failed to commit DB transaction, error=". $error;
  1276. LockRelease(); return $response;
  1277. }
  1278. $response["sql"] = array ( "rowsChanged" => $rowsChanged);
  1279. LockRelease(); return $response;
  1280. }
  1281. /**
  1282. * Update config properties for a given cluster
  1283. * Updates prop key if it exists or inserts a new entry if not found.
  1284. * @param string $clusterName
  1285. * @param mixed $config
  1286. * array of properties as key-val pairs
  1287. * $config =
  1288. * array (
  1289. * "component" => array (
  1290. * "hostname" =>
  1291. * array ( "prop_key1" => "prop_val1", ... )
  1292. * )
  1293. * )
  1294. * @return mixed
  1295. * array (
  1296. * "result" => 0,
  1297. * "error" => "",
  1298. * "sql" => array ( "rowsChanged" => 1 )
  1299. * )
  1300. */
  1301. public function updateHostRoleConfigs($clusterName, $config) {
  1302. LockAcquire();
  1303. $response = array ( "result" => 0, "error" => "");
  1304. if (count($config) == 0) {
  1305. // no-op
  1306. LockRelease(); return $response;
  1307. }
  1308. $error = "";
  1309. $rowsChanged = 0;
  1310. $ret = $this->dbHandle->beginTransaction();
  1311. if (!$ret) {
  1312. $error = $this->getLastDBErrorAsString();
  1313. $response["result"] = 1;
  1314. $response["error"] = "Failed to start DB transaction, error=".$error;
  1315. LockRelease(); return $response;
  1316. }
  1317. foreach ($config as $compName => $hostsInfo) {
  1318. foreach ($hostsInfo as $hostName => $configProps) {
  1319. foreach ($configProps as $key => $val) {
  1320. $query = " INSERT OR REPLACE INTO HostRoleConfig "
  1321. . " ( cluster_name, component_name, host_name"
  1322. . ", key, value ) "
  1323. . " VALUES "
  1324. . " ( " . $this->dbHandle->quote($clusterName) . ", "
  1325. . $this->dbHandle->quote($compName) . ", "
  1326. . $this->dbHandle->quote($hostName) . ", "
  1327. . $this->dbHandle->quote($key) . ", "
  1328. . $this->dbHandle->quote($val) . " ) ";
  1329. $this->logger->log_trace("Running query: $query");
  1330. $ret = $this->dbHandle->exec($query);
  1331. if (FALSE === $ret) {
  1332. $error = $this->getLastDBErrorAsString();
  1333. $ret = $this->dbHandle->rollBack();
  1334. $this->logger->log_error("Error when executing query"
  1335. . ", query=".$query
  1336. . ", error=".$error);
  1337. $response["result"] = 1;
  1338. $response["error"] = $error;
  1339. LockRelease(); return $response;
  1340. }
  1341. $rowsChanged += $ret;
  1342. }
  1343. }
  1344. }
  1345. $ret = $this->dbHandle->commit();
  1346. if (!$ret) {
  1347. $error = $this->getLastDBErrorAsString();
  1348. $this->logger->log_error("Failed to commit DB transaction, error=".$error);
  1349. $response["result"] = 1;
  1350. $response["error"] = "Failed to commit DB transaction, error=". $error;
  1351. LockRelease(); return $response;
  1352. }
  1353. $response["sql"] = array ( "rowsChanged" => $rowsChanged);
  1354. LockRelease(); return $response;
  1355. }
  1356. /**
  1357. * Get host role configs
  1358. * @param string $clusterName
  1359. * @param string $componentName ( optional )
  1360. */
  1361. public function getHostRoleConfigs($clusterName, $componentName = "") {
  1362. LockAcquire();
  1363. $error = "";
  1364. $query = "SELECT component_name, host_name, key, value FROM HostRoleConfig "
  1365. . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName);
  1366. if ($componentName != "") {
  1367. $query .= " AND component_name = "
  1368. . $this->dbHandle->quote($componentName);
  1369. }
  1370. $response = array ( "result" => 0, "error" => "");
  1371. $this->logger->log_trace("Running query: $query");
  1372. $pdoStmt = $this->dbHandle->query($query);
  1373. if ($pdoStmt === FALSE) {
  1374. $error = $this->getLastDBErrorAsString();
  1375. $this->logger->log_error("Error when executing query"
  1376. . ", query=".$query
  1377. . ", error=".$error);
  1378. $response["result"] = 1;
  1379. $response["error"] = $error;
  1380. LockRelease(); return $response;
  1381. }
  1382. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  1383. $response["clusterName"] = $clusterName;
  1384. $response["properties"] = array();
  1385. foreach ($result as $entry) {
  1386. $compName = $entry["component_name"];
  1387. $hostName = $entry["host_name"];
  1388. $key = $entry["key"];
  1389. $val = $entry["value"];
  1390. if (!isset($response["properties"][$compName])) {
  1391. $response["properties"][$compName] = array();
  1392. }
  1393. if (!isset($response["properties"][$compName][$hostName])) {
  1394. $response["properties"][$compName][$hostName] = array();
  1395. }
  1396. $response["properties"][$compName][$hostName][$key] = $val;
  1397. }
  1398. LockRelease(); return $response;
  1399. }
  1400. /////////////////////////////////////////// Configuration related APIs ////////////////////////////////////////////
  1401. /////////////////////////////////////////// Component related APIs ////////////////////////////////////////////
  1402. /**
  1403. * Get all components for a given service.
  1404. * @param string $serviceName
  1405. * @return mixed
  1406. * array (
  1407. * "result" => 0,
  1408. * "error" => "",
  1409. * "serviceName" => $serviceName,
  1410. * "components" => array (
  1411. * "$componentName" => array (
  1412. * "componentName" =>
  1413. * "displayName" =>
  1414. * "isMaster" =>
  1415. * "isClient" =>
  1416. * "description" =>
  1417. * )
  1418. * )
  1419. * )
  1420. */
  1421. public function getAllServiceComponents($serviceName) {
  1422. LockAcquire();
  1423. $response = array ( "result" => 0, "error" => "");
  1424. $query = "SELECT component_name, display_name, attributes, description"
  1425. . " FROM ServiceComponents WHERE service_name = "
  1426. . $this->dbHandle->quote($serviceName);
  1427. $this->logger->log_trace("Running query: $query");
  1428. $pdoStmt = $this->dbHandle->query($query);
  1429. if ($pdoStmt === FALSE) {
  1430. $error = $this->getLastDBErrorAsString();
  1431. $this->logger->log_error("Error when executing query"
  1432. . ", query=".$query
  1433. . ", error=".$error);
  1434. $response["result"] = 1;
  1435. $response["error"] = $error;
  1436. LockRelease(); return $response;
  1437. }
  1438. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  1439. $response["serviceName"] = $serviceName;
  1440. $response["components"] = array();
  1441. foreach ($result as $entry) {
  1442. $comp = array();
  1443. $comp["componentName"] = $entry["component_name"];
  1444. $comp["displayName"] = $entry["display_name"];
  1445. $comp["description"] = $entry["description"];
  1446. $attributes = json_decode($entry["attributes"], true);
  1447. $comp["isMaster"] = FALSE;
  1448. $comp["isClient"] = FALSE;
  1449. if (isset($attributes["isMaster"])) {
  1450. $comp["isMaster"] = $attributes["isMaster"];
  1451. }
  1452. if (isset($attributes["isClient"])) {
  1453. $comp["isClient"] = $attributes["isClient"];
  1454. }
  1455. $response["components"][$comp["componentName"]] = $comp;
  1456. }
  1457. LockRelease(); return $response;
  1458. }
  1459. /**
  1460. * Add host-role mappings for given hostnames to the specified component
  1461. * @param string $clusterName
  1462. * @param string $componentName
  1463. * @param array $hosts Array of hostnames
  1464. * @param string $state
  1465. * @param string $desiredState
  1466. * @return mixed
  1467. * array (
  1468. * "result" => 0,
  1469. * "error" => ""
  1470. * )
  1471. */
  1472. public function addHostsToComponent($clusterName, $componentName,
  1473. $hosts, $state, $desiredState) {
  1474. LockAcquire();
  1475. $response = array ( "result" => 0, "error" => "");
  1476. if (empty($hosts)) {
  1477. LockRelease(); return $response;
  1478. }
  1479. $error = "";
  1480. $ret = $this->dbHandle->beginTransaction();
  1481. if (!$ret) {
  1482. $error = $this->getLastDBErrorAsString();
  1483. $response["result"] = 1;
  1484. $response["error"] = "Failed to start DB transaction, error=".$error;
  1485. LockRelease(); return $response;
  1486. }
  1487. foreach ($hosts as $host) {
  1488. $query = " INSERT OR REPLACE INTO HostRoles "
  1489. . " ( cluster_name, component_name"
  1490. . ", host_name, state, desired_state ) "
  1491. . " VALUES "
  1492. . " ( " . $this->dbHandle->quote($clusterName) . ", "
  1493. . $this->dbHandle->quote($componentName) . ", "
  1494. . $this->dbHandle->quote($host) . ", "
  1495. . $this->dbHandle->quote($state) . ", "
  1496. . $this->dbHandle->quote($desiredState) . " ) ";
  1497. $this->logger->log_trace("Running query: $query");
  1498. $ret = $this->dbHandle->exec($query);
  1499. if (FALSE === $ret) {
  1500. $error = $this->getLastDBErrorAsString();
  1501. $ret = $this->dbHandle->rollBack();
  1502. $this->logger->log_error("Error when executing query"
  1503. . ", query=".$query
  1504. . ", error=".$error);
  1505. $response["result"] = 1;
  1506. $response["error"] = $error;
  1507. LockRelease(); return $response;
  1508. }
  1509. }
  1510. $ret = $this->dbHandle->commit();
  1511. if (!$ret) {
  1512. $error = $this->getLastDBErrorAsString();
  1513. $this->logger->log_error("Failed to commit DB transaction, error=".$error);
  1514. $response["result"] = 1;
  1515. $response["error"] = "Failed to commit DB transaction, error=". $error;
  1516. LockRelease(); return $response;
  1517. }
  1518. LockRelease(); return $response;
  1519. }
  1520. /**
  1521. * Get all hosts mapped to any component
  1522. * @param string $clusterName
  1523. * @return mixed
  1524. * array (
  1525. * "result" => 0,
  1526. * "error" => "",
  1527. * "clusterName" => $clusterName,
  1528. * "components" => array (
  1529. * "$componentName" = array (
  1530. * "componentName" => $componentName,
  1531. * "hosts" => array (
  1532. * "$hostName" => array (
  1533. * "hostName" =>
  1534. * "state" =>
  1535. * "desiredState" =>
  1536. * )
  1537. * )
  1538. * )
  1539. * )
  1540. * )
  1541. */
  1542. public function getAllHostsByComponent($clusterName) {
  1543. LockAcquire();
  1544. $response = array ( "result" => 0, "error" => "");
  1545. $error = "";
  1546. $query = "SELECT role_id, component_name, host_name, state, desired_state"
  1547. . " FROM HostRoles"
  1548. . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName);
  1549. $this->logger->log_trace("Running query: $query");
  1550. $pdoStmt = $this->dbHandle->query($query);
  1551. if ($pdoStmt === FALSE) {
  1552. $error = $this->getLastDBErrorAsString();
  1553. $this->logger->log_error("Error when executing query"
  1554. . ", query=".$query
  1555. . ", error=".$error);
  1556. $response["result"] = 1;
  1557. $response["error"] = $error;
  1558. LockRelease(); return $response;
  1559. }
  1560. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  1561. $response["clusterName"] = $clusterName;
  1562. $response["components"] = array();
  1563. foreach ($result as $entry) {
  1564. $componentName = $entry["component_name"];
  1565. if (!isset($response["components"][$componentName])) {
  1566. $response["components"][$componentName] = array();
  1567. $response["components"][$componentName]["componentName"] =
  1568. $componentName;
  1569. $response["components"][$componentName]["hosts"] = array();
  1570. }
  1571. $hostInfo = array();
  1572. $hostInfo["hostName"] = $entry["host_name"];
  1573. $hostInfo["roleId"] = $entry["role_id"];
  1574. $hostInfo["state"] = $entry["state"];
  1575. $hostInfo["desiredState"] = $entry["desired_state"];
  1576. $response["components"][$componentName]["hosts"]
  1577. [$hostInfo["hostName"]] = $hostInfo;
  1578. }
  1579. LockRelease(); return $response;
  1580. }
  1581. /**
  1582. * Get all hosts mapped to all the components
  1583. * @param string $clusterName
  1584. * @return mixed
  1585. * array (
  1586. * "result" => 0,
  1587. * "error" => "",
  1588. * "clusterName" => $clusterName,
  1589. * "hosts" => array (
  1590. * "hostName" =>
  1591. * "components" = array (
  1592. * "componentName", .....
  1593. * )
  1594. * )
  1595. * )
  1596. */
  1597. public function getAllHostsToComponentMap($clusterName) {
  1598. LockAcquire();
  1599. $response = array ( "result" => 0, "error" => "");
  1600. $error = "";
  1601. $query = "SELECT component_name, host_name, state, desired_state "
  1602. . " FROM HostRoles"
  1603. . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName);
  1604. $this->logger->log_trace("Running query: $query");
  1605. $pdoStmt = $this->dbHandle->query($query);
  1606. if ($pdoStmt === FALSE) {
  1607. $error = $this->getLastDBErrorAsString();
  1608. $this->logger->log_error("Error when executing query"
  1609. . ", query=".$query
  1610. . ", error=".$error);
  1611. $response["result"] = 1;
  1612. $response["error"] = $error;
  1613. LockRelease(); return $response;
  1614. }
  1615. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  1616. $response["clusterName"] = $clusterName;
  1617. $response["hosts"] = array();
  1618. foreach ($result as $entry) {
  1619. $hostName = $entry["host_name"];
  1620. if (!isset($response["hosts"][$hostName])) {
  1621. $response["hosts"][$hostName] = array();
  1622. $initComponents = array();
  1623. array_push($initComponents, $entry["component_name"]);
  1624. $response["hosts"][$hostName]["components"] =
  1625. $initComponents;
  1626. } else {
  1627. $components = $response["hosts"][$hostName]["components"];
  1628. array_push($components, $entry["component_name"]);
  1629. $response["hosts"][$hostName]["components"] = $components;
  1630. }
  1631. }
  1632. LockRelease(); return $response;
  1633. }
  1634. /**
  1635. * Get hosts mapped to a given component
  1636. * @param string $clusterName
  1637. * @param string $componentName
  1638. * @return mixed
  1639. * array (
  1640. * "result" => 0,
  1641. * "error" => "",
  1642. * "clusterName" => $clusterName,
  1643. * "componentName" => $componentName,
  1644. * "hosts" => array (
  1645. * "$hostName" => array (
  1646. * "hostName" =>
  1647. * "state" =>
  1648. * "desiredState" =>
  1649. * )
  1650. * )
  1651. * )
  1652. */
  1653. public function getHostsForComponent($clusterName, $componentName) {
  1654. LockAcquire();
  1655. $response = array ( "result" => 0, "error" => "");
  1656. $error = "";
  1657. $query = "SELECT host_name, state, desired_state FROM HostRoles"
  1658. . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
  1659. . " AND component_name = " . $this->dbHandle->quote($componentName);
  1660. $this->logger->log_trace("Running query: $query");
  1661. $pdoStmt = $this->dbHandle->query($query);
  1662. if ($pdoStmt === FALSE) {
  1663. $error = $this->getLastDBErrorAsString();
  1664. $this->logger->log_error("Error when executing query"
  1665. . ", query=".$query
  1666. . ", error=".$error);
  1667. $response["result"] = 1;
  1668. $response["error"] = $error;
  1669. LockRelease(); return $response;
  1670. }
  1671. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  1672. $response["hosts"] = array();
  1673. $response["clusterName"] = $clusterName;
  1674. $response["componentName"] = $componentName;
  1675. foreach ($result as $entry) {
  1676. $hostInfo = array();
  1677. $hostInfo["hostName"] = $entry["host_name"];
  1678. $hostInfo["state"] = $entry["state"];
  1679. $hostInfo["desiredState"] = $entry["desired_state"];
  1680. $response["hosts"][$hostInfo["hostName"]] = $hostInfo;
  1681. }
  1682. LockRelease(); return $response;
  1683. }
  1684. /**
  1685. * Set the state for specified hosts mapped to given component/role
  1686. * @param string $clusterName
  1687. * @param string $componentName
  1688. * @param array $hosts
  1689. * @param string $state
  1690. * @return mixed
  1691. * array (
  1692. * "result" => 0,
  1693. * "error" => ""
  1694. * "sql" => array ( "rowsChanged" => 1 )
  1695. * )
  1696. */
  1697. public function setHostsStateForComponent($clusterName, $componentName,
  1698. $hosts, $state) {
  1699. LockAcquire();
  1700. $response = array ( "result" => 0, "error" => "");
  1701. if (count($hosts) == 0) {
  1702. LockRelease(); return $response;
  1703. }
  1704. $query = " UPDATE HostRoles SET state = " . $this->dbHandle->quote($state)
  1705. . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
  1706. . " AND component_name = " . $this->dbHandle->quote($componentName)
  1707. . " AND ( ";
  1708. $index = 0;
  1709. foreach ($hosts as $host) {
  1710. if ($index != 0) {
  1711. $query .= " OR ";
  1712. }
  1713. $index++;
  1714. $query .= " host_name = " . $this->dbHandle->quote($host);
  1715. }
  1716. $query .= " ) ";
  1717. $this->logger->log_trace("Running query: $query");
  1718. $ret = $this->dbHandle->exec($query);
  1719. if (FALSE === $ret) {
  1720. $error = $this->getLastDBErrorAsString();
  1721. $this->logger->log_error("Error when executing query"
  1722. . ", query=".$query
  1723. . ", error=".$error);
  1724. $response["result"] = 1;
  1725. $response["error"] = $error;
  1726. LockRelease(); return $response;
  1727. }
  1728. $rowsChanged = $ret;
  1729. $response["sql"] = array ( "rowsChanged" => $rowsChanged);
  1730. LockRelease(); return $response;
  1731. }
  1732. /**
  1733. * Set the state for specified hosts mapped to given component/role
  1734. * @param string $clusterName
  1735. * @param string $componentName
  1736. * @param array $hosts
  1737. * @param string $desiredState
  1738. * @return mixed
  1739. * array (
  1740. * "result" => 0,
  1741. * "error" => ""
  1742. * "sql" => array ( "rowsChanged" => 1 )
  1743. * )
  1744. */
  1745. public function setHostsDesiredStateForComponent($clusterName, $componentName,
  1746. $hosts, $desiredState) {
  1747. LockAcquire();
  1748. $response = array ( "result" => 0, "error" => "");
  1749. if (count($hosts) == 0) {
  1750. LockRelease(); return $response;
  1751. }
  1752. $query = " UPDATE HostRoles SET desired_state = " . $this->dbHandle->quote($desiredState)
  1753. . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
  1754. . " AND component_name = " . $this->dbHandle->quote($componentName)
  1755. . " AND ( ";
  1756. $index = 0;
  1757. foreach ($hosts as $host) {
  1758. if ($index != 0) {
  1759. $query .= " OR ";
  1760. }
  1761. $index++;
  1762. $query .= " host_name = " . $this->dbHandle->quote($host);
  1763. }
  1764. $query .= " ) ";
  1765. $this->logger->log_trace("Running query: $query");
  1766. $ret = $this->dbHandle->exec($query);
  1767. if (FALSE === $ret) {
  1768. $error = $this->getLastDBErrorAsString();
  1769. $this->logger->log_error("Error when executing query"
  1770. . ", query=".$query
  1771. . ", error=".$error);
  1772. $response["result"] = 1;
  1773. $response["error"] = $error;
  1774. LockRelease(); return $response;
  1775. }
  1776. $rowsChanged = $ret;
  1777. $response["sql"] = array ( "rowsChanged" => $rowsChanged);
  1778. LockRelease(); return $response;
  1779. }
  1780. /**
  1781. * Get services that the given service depends upon
  1782. * @param string $serviceName
  1783. * @return mixed
  1784. * array (
  1785. * "result" => 0,
  1786. * "error" => "",
  1787. * "serviceName" =>
  1788. * "serviceDependencies" = array ( "svc1", "svc2", ... )
  1789. * )
  1790. */
  1791. public function getServiceDependencies($serviceName) {
  1792. LockAcquire();
  1793. $response = array ( "result" => 0, "error" => "");
  1794. $query = "SELECT to_service_name FROM ServiceDependencies WHERE "
  1795. . " from_service_name = " . $this->dbHandle->quote($serviceName);
  1796. $this->logger->log_trace("Running query: $query");
  1797. $pdoStmt = $this->dbHandle->query($query);
  1798. if ($pdoStmt === FALSE) {
  1799. $error = $this->getLastDBErrorAsString();
  1800. $this->logger->log_error("Error when executing query"
  1801. . ", query=".$query
  1802. . ", error=".$error);
  1803. $response["result"] = 1;
  1804. $response["error"] = $error;
  1805. LockRelease(); return $response;
  1806. }
  1807. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  1808. $response["serviceName"] = $serviceName;
  1809. $response["serviceDependencies"] = array();
  1810. foreach ($result as $entry) {
  1811. array_push($response["serviceDependencies"], $entry["to_service_name"]);
  1812. }
  1813. LockRelease(); return $response;
  1814. }
  1815. /**
  1816. * Get services which depend on the given service.
  1817. * @param string $serviceName
  1818. * @return mixed
  1819. * array (
  1820. * "result" => 0,
  1821. * "error" => "",
  1822. * "serviceName" =>
  1823. * "serviceDependents" = array ( "svc1", "svc2", ... )
  1824. * )
  1825. */
  1826. public function getServiceDependents($serviceName) {
  1827. LockAcquire();
  1828. $response = array ( "result" => 0, "error" => "");
  1829. $query = "SELECT from_service_name FROM ServiceDependencies WHERE "
  1830. . " to_service_name = " . $this->dbHandle->quote($serviceName);
  1831. $this->logger->log_trace("Running query: $query");
  1832. $pdoStmt = $this->dbHandle->query($query);
  1833. if ($pdoStmt === FALSE) {
  1834. $error = $this->getLastDBErrorAsString();
  1835. $this->logger->log_error("Error when executing query"
  1836. . ", query=".$query
  1837. . ", error=".$error);
  1838. $response["result"] = 1;
  1839. $response["error"] = $error;
  1840. LockRelease(); return $response;
  1841. }
  1842. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  1843. $response["serviceName"] = $serviceName;
  1844. $response["serviceDependents"] = array();
  1845. foreach ($result as $entry) {
  1846. array_push($response["serviceDependents"], $entry["from_service_name"]);
  1847. }
  1848. LockRelease(); return $response;
  1849. }
  1850. /**
  1851. * Get components that the given component depends upon
  1852. * @param string $componentName
  1853. * @return mixed
  1854. * array (
  1855. * "result" => 0,
  1856. * "error" => "",
  1857. * "componentName" =>
  1858. * "componentDependencies" = array ( "comp1", "comp2", ... )
  1859. * )
  1860. */
  1861. public function getServiceComponentDependencies($componentName) {
  1862. LockAcquire();
  1863. $response = array ( "result" => 0, "error" => "");
  1864. $query = "SELECT to_component_name "
  1865. . " FROM ServiceComponentDependencies WHERE "
  1866. . " from_component_name = "
  1867. . $this->dbHandle->quote($componentName);
  1868. $this->logger->log_trace("Running query: $query");
  1869. $pdoStmt = $this->dbHandle->query($query);
  1870. if ($pdoStmt === FALSE) {
  1871. $error = $this->getLastDBErrorAsString();
  1872. $this->logger->log_error("Error when executing query"
  1873. . ", query=".$query
  1874. . ", error=".$error);
  1875. $response["result"] = 1;
  1876. $response["error"] = $error;
  1877. LockRelease(); return $response;
  1878. }
  1879. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  1880. $response["componentName"] = $componentName;
  1881. $response["componentDependencies"] = array();
  1882. foreach ($result as $entry) {
  1883. array_push($response["componentDependencies"],
  1884. $entry["to_component_name"]);
  1885. }
  1886. LockRelease(); return $response;
  1887. }
  1888. /**
  1889. * Get components that the given component depends upon
  1890. * @param string $componentName
  1891. * @return mixed
  1892. * array (
  1893. * "result" => 0,
  1894. * "error" => "",
  1895. * "serviceName" =>
  1896. * "componentName" =>
  1897. * "componentDependents" = array ( "comp1", "comp2", ... )
  1898. * )
  1899. */
  1900. public function getServiceComponentDependents($componentName) {
  1901. LockAcquire();
  1902. $response = array ( "result" => 0, "error" => "");
  1903. $query = "SELECT from_component_name "
  1904. . " FROM ServiceComponentDependencies WHERE "
  1905. . " to_component_name = "
  1906. . $this->dbHandle->quote($componentName);
  1907. $this->logger->log_trace("Running query: $query");
  1908. $pdoStmt = $this->dbHandle->query($query);
  1909. if ($pdoStmt === FALSE) {
  1910. $error = $this->getLastDBErrorAsString();
  1911. $this->logger->log_error("Error when executing query"
  1912. . ", query=".$query
  1913. . ", error=".$error);
  1914. $response["result"] = 1;
  1915. $response["error"] = $error;
  1916. LockRelease(); return $response;
  1917. }
  1918. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  1919. $response["componentName"] = $componentName;
  1920. $response["componentDependents"] = array();
  1921. foreach ($result as $entry) {
  1922. array_push($response["componentDependents"],
  1923. $entry["from_component_name"]);
  1924. }
  1925. LockRelease(); return $response;
  1926. }
  1927. /**
  1928. * Add Service Components to a given cluster
  1929. * @param string $clusterName
  1930. * @param mixed $components
  1931. * array (
  1932. * "$serviceName" => array (
  1933. * "$componentName" =>
  1934. * "state" =>
  1935. * "desiredState" =>
  1936. * )
  1937. * )
  1938. * )
  1939. */
  1940. public function addServiceComponentsToCluster($clusterName, $components) {
  1941. LockAcquire();
  1942. $response = array ( "result" => 0, "error" => "",
  1943. "clusterName" => $clusterName);
  1944. $error = "";
  1945. $supportedComps = $this->getAllServiceComponentsList();
  1946. if ($supportedComps === FALSE
  1947. || $supportedComps["result"] != 0) {
  1948. LockRelease(); return $supportedComps;
  1949. }
  1950. // sanitize input
  1951. $sanitizedComps = array();
  1952. foreach ($components as $svc=>$comps) {
  1953. if (!isset($comps)
  1954. || !is_array($comps)
  1955. || count($comps) == 0) {
  1956. $this->logger->log_warn("Invalid args to addServiceComponentsToCluster"
  1957. . ", svc=" . $svc
  1958. . ", comps either null or empty");
  1959. continue;
  1960. }
  1961. if (!isset($supportedComps["services"][$svc])
  1962. || !is_array($supportedComps["services"][$svc])
  1963. || !isset($supportedComps["services"][$svc]["components"])
  1964. || !is_array($supportedComps["services"][$svc]["components"])) {
  1965. $response["error"] = "Invalid service - not supported, svc="
  1966. . $svc;
  1967. $response["result"] = 1;
  1968. LockRelease(); return $response;
  1969. }
  1970. foreach ($comps as $compName=>$comp) {
  1971. if (!isset($supportedComps["services"][$svc]["components"][$compName])
  1972. || !is_array($supportedComps["services"]
  1973. [$svc]["components"][$compName])) {
  1974. $response["error"] = "Invalid component - not supported, svc="
  1975. . $svc . ", component=" . $compName;
  1976. $response["result"] = 1;
  1977. LockRelease(); return $response;
  1978. }
  1979. $saneComp = array(); // yes - it really is sane as in not insane
  1980. $saneComp["serviceName"] = $svc;
  1981. $saneComp["componentName"] = $compName;
  1982. $saneComp["state"] = "";
  1983. $saneComp["desiredState"] = "";
  1984. if (isset($comp["state"])) {
  1985. $saneComp["state"] = $comp["state"];
  1986. }
  1987. if (isset($comp["desiredState"])) {
  1988. $saneComp["desiredState"] = $comp["desiredState"];
  1989. }
  1990. array_push($sanitizedComps, $saneComp);
  1991. }
  1992. }
  1993. $ret = $this->dbHandle->beginTransaction();
  1994. if (!$ret) {
  1995. $error = $this->getLastDBErrorAsString();
  1996. $response["result"] = 1;
  1997. $response["error"] = "Failed to start DB transaction, error=".$error;
  1998. LockRelease(); return $response;
  1999. }
  2000. foreach ($sanitizedComps as $comp) {
  2001. $query = "INSERT INTO ServiceComponentInfo "
  2002. . " ( cluster_name, service_name, component_name"
  2003. . ", state, desired_state ) "
  2004. . " VALUES "
  2005. . " ("
  2006. . $this->dbHandle->quote($clusterName) . " , "
  2007. . $this->dbHandle->quote($comp["serviceName"]) . " , "
  2008. . $this->dbHandle->quote($comp["componentName"]) . " , "
  2009. . $this->dbHandle->quote($comp["state"]) . " , "
  2010. . $this->dbHandle->quote($comp["desiredState"])
  2011. . " )";
  2012. $this->logger->log_trace("Running query: $query");
  2013. $ret = $this->dbHandle->exec($query);
  2014. if (FALSE === $ret) {
  2015. $error = $this->getLastDBErrorAsString();
  2016. $this->dbHandle->rollBack();
  2017. $this->logger->log_error("Error when executing query"
  2018. . ", query=".$query
  2019. . ", error=".$error);
  2020. $response["result"] = 1;
  2021. $response["error"] = $error;
  2022. LockRelease(); return $response;
  2023. }
  2024. }
  2025. $ret = $this->dbHandle->commit();
  2026. if (!$ret) {
  2027. $error = $this->getLastDBErrorAsString();
  2028. $response["result"] = 1;
  2029. $response["error"] = "Failed to commit DB transaction, error=".$error;
  2030. LockRelease(); return $response;
  2031. }
  2032. LockRelease(); return $response;
  2033. }
  2034. /**
  2035. * Get all components for all services within the cluster
  2036. * @param string $clusterName
  2037. * @return mixed
  2038. * array (
  2039. * "result" => 0,
  2040. * "error" => "",
  2041. * "services" => array (
  2042. * "$serviceName" => array (
  2043. * "serviceName" => $serviceName,
  2044. * "components" => array (
  2045. * "$componentName" => array (
  2046. * "componentName" =>
  2047. * "state" =>
  2048. * "desiredState" =>
  2049. * )
  2050. * )
  2051. * )
  2052. * )
  2053. */
  2054. public function getAllServiceComponentsInfo($clusterName) {
  2055. LockAcquire();
  2056. $response = array ( "result" => 0, "error" => "");
  2057. $query = "SELECT service_name, component_name, state, desired_state "
  2058. . " FROM ServiceComponentInfo WHERE "
  2059. . " cluster_name = " . $this->dbHandle->quote($clusterName);
  2060. $this->logger->log_trace("Running query: $query");
  2061. $pdoStmt = $this->dbHandle->query($query);
  2062. if ($pdoStmt === FALSE) {
  2063. $error = $this->getLastDBErrorAsString();
  2064. $this->logger->log_error("Error when executing query"
  2065. . ", query=".$query
  2066. . ", error=".$error);
  2067. $response["result"] = 1;
  2068. $response["error"] = $error;
  2069. LockRelease(); return $response;
  2070. }
  2071. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  2072. $response["services"] = array();
  2073. foreach ($result as $entry) {
  2074. $serviceName = $entry["service_name"];
  2075. if (!isset($response["services"]["$serviceName"])) {
  2076. $response["services"][$serviceName] = array();
  2077. $response["services"][$serviceName]["serviceName"] = $serviceName;
  2078. $response["services"][$serviceName]["components"] = array();
  2079. }
  2080. $comp = array();
  2081. $comp["serviceName"] = $serviceName;
  2082. $comp["componentName"] = $entry["component_name"];
  2083. $comp["state"] = $entry["state"];
  2084. $comp["desiredState"] = $entry["desired_state"];
  2085. $response["services"][$serviceName]["components"]
  2086. [$comp["componentName"]] = $comp;
  2087. }
  2088. LockRelease(); return $response;
  2089. }
  2090. /*
  2091. public function getServiceComponentInfo($clusterName, $componentName) {
  2092. LockAcquire();
  2093. // TODO
  2094. // use getAllServiceComponentsInfo for now
  2095. }
  2096. */
  2097. private function genericSetServiceComponentState($clusterName, $componentName,
  2098. $state, $updateAllMappedHosts, $updateDesired) {
  2099. LockAcquire();
  2100. $columnName = "state";
  2101. if ($updateDesired) {
  2102. $columnName = "desired_state";
  2103. }
  2104. $response = array ( "result" => 0, "error" => "");
  2105. $ret = $this->dbHandle->beginTransaction();
  2106. if (!$ret) {
  2107. $error = $this->getLastDBErrorAsString();
  2108. $response["result"] = 1;
  2109. $response["error"] = "Failed to start DB transaction, error=".$error;
  2110. LockRelease(); return $response;
  2111. }
  2112. $query = "UPDATE ServiceComponentInfo SET " . $columnName . " = "
  2113. . $this->dbHandle->quote($state)
  2114. . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
  2115. . " AND component_name = " . $this->dbHandle->quote($componentName);
  2116. $this->logger->log_trace("Running query: $query");
  2117. $ret = $this->dbHandle->exec($query);
  2118. if (FALSE === $ret) {
  2119. $error = $this->getLastDBErrorAsString();
  2120. $this->dbHandle->rollBack();
  2121. $this->logger->log_error("Error when executing query"
  2122. . ", query=".$query
  2123. . ", error=".$error);
  2124. $response["result"] = 1;
  2125. $response["error"] = $error;
  2126. LockRelease(); return $response;
  2127. }
  2128. if ($updateAllMappedHosts) {
  2129. $query = "UPDATE HostRoles SET " . $columnName . " = "
  2130. . $this->dbHandle->quote($state)
  2131. . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
  2132. . " AND component_name = " . $this->dbHandle->quote($componentName);
  2133. $this->logger->log_trace("Running query: $query");
  2134. $ret = $this->dbHandle->exec($query);
  2135. if (FALSE === $ret) {
  2136. $error = $this->getLastDBErrorAsString();
  2137. $this->dbHandle->rollBack();
  2138. $this->logger->log_error("Error when executing query"
  2139. . ", query=".$query
  2140. . ", error=".$error);
  2141. $response["result"] = 1;
  2142. $response["error"] = $error;
  2143. LockRelease(); return $response;
  2144. }
  2145. }
  2146. $ret = $this->dbHandle->commit();
  2147. if (!$ret) {
  2148. $error = $this->getLastDBErrorAsString();
  2149. $response["result"] = 1;
  2150. $response["error"] = "Failed to commit DB transaction, error=".$error;
  2151. LockRelease(); return $response;
  2152. }
  2153. LockRelease(); return $response;
  2154. }
  2155. /**
  2156. * Update the state for a specified component. Based on update hosts flag,
  2157. * set the state for all the hosts mapped to that component too.
  2158. * @param string $clusterName
  2159. * @param string $componentName
  2160. * @param string $state
  2161. * @param bool $updateAllMappedHosts
  2162. * @return mixed
  2163. * array (
  2164. * "result" => 0,
  2165. * "error" => ""
  2166. * )
  2167. */
  2168. public function setServiceComponentState($clusterName, $componentName,
  2169. $state, $updateAllMappedHosts) {
  2170. return $this->genericSetServiceComponentState($clusterName, $componentName,
  2171. $state, $updateAllMappedHosts, FALSE);
  2172. }
  2173. /**
  2174. * Update the desired state for a specified component. Based on update hosts
  2175. * flag, set the desired state for all the hosts mapped to that component too.
  2176. * @param string $clusterName
  2177. * @param string $componentName
  2178. * @param string $desiredState
  2179. * @param bool $updateAllMappedHosts
  2180. * @return mixed
  2181. * array (
  2182. * "result" => 0,
  2183. * "error" => ""
  2184. * )
  2185. */
  2186. public function setServiceComponentDesiredState($clusterName, $componentName,
  2187. $desiredState, $updateAllMappedHosts) {
  2188. return $this->genericSetServiceComponentState($clusterName, $componentName,
  2189. $desiredState, $updateAllMappedHosts, TRUE);
  2190. }
  2191. // TODO add apis for config history
  2192. // TODO add apis for txn status table
  2193. /**
  2194. * Create a new transaction
  2195. * @param string $statusInfo Status blob to attach to txn in DB
  2196. * @param string $pidInfo Pid Info to attach to txn
  2197. * @return mixed
  2198. * array (
  2199. * "result" => 0,
  2200. * "error" => "",
  2201. * "txnId" => $txn_id
  2202. * )
  2203. */
  2204. public function createNewTransaction($clusterName, $statusInfo, $pidInfo) {
  2205. LockAcquire();
  2206. $response = array ( "result" => 0, "error" => "");
  2207. $ret = $this->dbHandle->beginTransaction();
  2208. if (!$ret) {
  2209. $error = $this->getLastDBErrorAsString();
  2210. $response["result"] = 1;
  2211. $response["error"] = "Failed to start DB transaction, error=".$error;
  2212. LockRelease(); return $response;
  2213. }
  2214. $query = "INSERT INTO TransactionStatus ( cluster_name, create_time"
  2215. . ", status_info, pid_info ) VALUES ( "
  2216. . $this->dbHandle->quote($clusterName) . " , "
  2217. . $this->dbHandle->quote(time()) . " , "
  2218. . $this->dbHandle->quote($statusInfo) . " , "
  2219. . $this->dbHandle->quote($pidInfo) . " ) ";
  2220. $this->logger->log_trace("Running query: $query");
  2221. $ret = $this->dbHandle->exec($query);
  2222. if (FALSE === $ret) {
  2223. $error = $this->getLastDBErrorAsString();
  2224. $this->dbHandle->rollBack();
  2225. $this->logger->log_error("Error when executing query"
  2226. . ", query=".$query
  2227. . ", error=".$error);
  2228. $response["result"] = 1;
  2229. $response["error"] = $error;
  2230. LockRelease(); return $response;
  2231. }
  2232. $lastInsertId = intval($this->dbHandle->lastInsertId());
  2233. if ($lastInsertId == 0) {
  2234. $this->dbHandle->rollBack();
  2235. $error = "Obtained invalid last insert id from DB";
  2236. $this->logger->log_error($error);
  2237. $response["result"] = 1;
  2238. $response["error"] = $error;
  2239. LockRelease(); return $response;
  2240. }
  2241. $ret = $this->dbHandle->commit();
  2242. if (!$ret) {
  2243. $error = $this->getLastDBErrorAsString();
  2244. $response["result"] = 1;
  2245. $response["error"] = "Failed to commit DB transaction, error=".$error;
  2246. LockRelease(); return $response;
  2247. }
  2248. $response["txnId"] = $lastInsertId;
  2249. LockRelease(); return $response;
  2250. }
  2251. /**
  2252. * Get transaction Info
  2253. * @param string $clusterName
  2254. * @param int $txnId
  2255. * @return mixed
  2256. * array (
  2257. * "result" => 0,
  2258. * "error" => "",
  2259. * "clusterName" => $clusterName,
  2260. * "txnId" => $txn_id,
  2261. * "statusInfo" => $statusInfo,
  2262. * "pidInfo" => $pidInfo,
  2263. * "createTime" => $createTime
  2264. * )
  2265. */
  2266. public function getTransactionStatusInfo($clusterName, $txnId) {
  2267. LockAcquire();
  2268. $response = array ( "result" => 0, "error" => "");
  2269. $query = "SELECT create_time, status_info, pid_info "
  2270. . " FROM TransactionStatus WHERE "
  2271. . " cluster_name = " . $this->dbHandle->quote($clusterName)
  2272. . " AND txn_id = " . $this->dbHandle->quote($txnId);
  2273. $response = array ( "result" => 0, "error" => "");
  2274. $this->logger->log_trace("Running query: $query");
  2275. $pdoStmt = $this->dbHandle->query($query);
  2276. if ($pdoStmt === FALSE) {
  2277. $error = $this->getLastDBErrorAsString();
  2278. $this->logger->log_error("Error when executing query"
  2279. . ", query=".$query
  2280. . ", error=".$error);
  2281. $response["result"] = 1;
  2282. $response["error"] = $error;
  2283. LockRelease(); return $response;
  2284. }
  2285. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  2286. $response["clusterName"] = $clusterName;
  2287. $response["txnId"] = $txnId;
  2288. if (isset($result) && is_array($result) && count($result) == 1) {
  2289. $entry = $result[0];
  2290. $response["createTime"] = $entry["create_time"];
  2291. $response["statusInfo"] = $entry["status_info"];
  2292. $response["pidInfo"] = $entry["pid_info"];
  2293. LockRelease(); return $response;
  2294. }
  2295. $response["result"] = 1;
  2296. $response["error"] = "Could not find txn in DB";
  2297. LockRelease(); return $response;
  2298. }
  2299. /**
  2300. * Update pid info for a given transaction
  2301. * @param string $clusterName
  2302. * @param string $txnId
  2303. * @param string $pidInfo
  2304. * @return mixed
  2305. * array (
  2306. * "result" => 0,
  2307. * "error" => ""
  2308. * )
  2309. */
  2310. public function updateTransactionPidInfo($clusterName, $txnId, $pidInfo) {
  2311. LockAcquire();
  2312. $response = array ( "result" => 0, "error" => "");
  2313. $query = "UPDATE TransactionStatus SET pid_info = "
  2314. . $this->dbHandle->quote($pidInfo)
  2315. . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
  2316. . " AND txn_id = " . $this->dbHandle->quote($txnId);
  2317. $this->logger->log_trace("Running query: $query");
  2318. $ret = $this->dbHandle->exec($query);
  2319. if (FALSE === $ret) {
  2320. $error = $this->getLastDBErrorAsString();
  2321. $this->logger->log_error("Error when executing query"
  2322. . ", query=".$query
  2323. . ", error=".$error);
  2324. $response["result"] = 1;
  2325. $response["error"] = $error;
  2326. LockRelease(); return $response;
  2327. }
  2328. $rowsChanged = intval($ret);
  2329. $response["sql"] = array ( "rowsChanged" => $rowsChanged);
  2330. LockRelease(); return $response;
  2331. }
  2332. /**
  2333. * Update status info for a given transaction
  2334. * @param string $clusterName
  2335. * @param string $txnId
  2336. * @param string $statusInfo
  2337. * @return mixed
  2338. * array (
  2339. * "result" => 0,
  2340. * "error" => ""
  2341. * )
  2342. */
  2343. public function updateTransactionStatusInfo($clusterName, $txnId, $statusInfo) {
  2344. LockAcquire();
  2345. $response = array ( "result" => 0, "error" => "");
  2346. $query = "UPDATE TransactionStatus SET status_info = "
  2347. . $this->dbHandle->quote($statusInfo)
  2348. . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
  2349. . " AND txn_id = " . $this->dbHandle->quote($txnId);
  2350. $this->logger->log_trace("Running query: $query");
  2351. $ret = $this->dbHandle->exec($query);
  2352. if (FALSE === $ret) {
  2353. $error = $this->getLastDBErrorAsString();
  2354. $this->logger->log_error("Error when executing query"
  2355. . ", query=".$query
  2356. . ", error=".$error);
  2357. $response["result"] = 1;
  2358. $response["error"] = $error;
  2359. LockRelease(); return $response;
  2360. }
  2361. $rowsChanged = intval($ret);
  2362. $response["sql"] = array ( "rowsChanged" => $rowsChanged);
  2363. LockRelease(); return $response;
  2364. }
  2365. /**
  2366. * Add a new sub-txn
  2367. * @param string $clusterName
  2368. * @param int $txnId
  2369. * @param int $subTxnId
  2370. * @param int $parentSubTxnId
  2371. * @param string $state
  2372. * @param string $description
  2373. * @param string $progress
  2374. * @param string $subTxnType
  2375. * @return mixed
  2376. * array (
  2377. * "result" => 0,
  2378. * "error" => ""
  2379. * )
  2380. */
  2381. public function insertOrUpdateSubTransaction($clusterName, $txnId, $subTxnId,
  2382. $parentSubTxnId, $state, $description, $progress, $subTxnType) {
  2383. LockAcquire();
  2384. $response = array ( "result" => 0, "error" => "");
  2385. $query = "SELECT cluster_name, txn_id, sub_txn_id, op_status "
  2386. . " FROM SubTransactionStatus "
  2387. . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
  2388. . " AND txn_id = " . $this->dbHandle->quote($txnId)
  2389. . " AND sub_txn_id = " . $this->dbHandle->quote($subTxnId);
  2390. $this->logger->log_trace("Running query: $query");
  2391. $pdoStmt = $this->dbHandle->query($query);
  2392. if ($pdoStmt === FALSE) {
  2393. $error = $this->getLastDBErrorAsString();
  2394. $this->logger->log_error("Error when executing query"
  2395. . ", query=".$query
  2396. . ", error=".$error);
  2397. $response["result"] = 1;
  2398. $response["error"] = $error;
  2399. LockRelease(); return $response;
  2400. }
  2401. $opStatus = json_encode(array());
  2402. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  2403. if (is_array($result) && count($result) == 1) {
  2404. $opStatus = $result[0]["op_status"];
  2405. }
  2406. $query = "INSERT OR REPLACE INTO SubTransactionStatus "
  2407. . " ( cluster_name, txn_id, sub_txn_id, parent_sub_txn_id"
  2408. . ", state, description, progress, sub_txn_type, op_status ) VALUES ( "
  2409. . $this->dbHandle->quote($clusterName) . " , "
  2410. . $this->dbHandle->quote($txnId) . " , "
  2411. . $this->dbHandle->quote($subTxnId) . " , "
  2412. . $this->dbHandle->quote($parentSubTxnId) . " , "
  2413. . $this->dbHandle->quote($state) . " , "
  2414. . $this->dbHandle->quote($description) . " , "
  2415. . $this->dbHandle->quote($progress) . " , "
  2416. . $this->dbHandle->quote($subTxnType) . " , "
  2417. . $this->dbHandle->quote($opStatus) . " ) ";
  2418. $this->logger->log_trace("Running query: $query");
  2419. $ret = $this->dbHandle->exec($query);
  2420. if (FALSE === $ret) {
  2421. $error = $this->getLastDBErrorAsString();
  2422. $this->logger->log_error("Error when executing query"
  2423. . ", query=".$query
  2424. . ", error=".$error);
  2425. $response["result"] = 1;
  2426. $response["error"] = $error;
  2427. LockRelease(); return $response;
  2428. }
  2429. $rowsChanged = intval($ret);
  2430. $response["sql"] = array ( "rowsChanged" => $rowsChanged);
  2431. LockRelease(); return $response;
  2432. }
  2433. /**
  2434. * Get all sub transactions
  2435. * Enter description here ...
  2436. * @param string $clusterName
  2437. * @param int $txnId
  2438. * @return mixed
  2439. * array (
  2440. * "result" => 0,
  2441. * "error" => "",
  2442. * "clusterName" =>
  2443. * "txnId" =>
  2444. * "subTxns" => array (
  2445. * $subTxnId => array (
  2446. * "subTxnId" =>
  2447. * "parentSubTxnId" =>
  2448. * "state" =>
  2449. * "description" =>
  2450. * "progress" =>
  2451. * "subTxnType" =>
  2452. * "opStatus" =>
  2453. * )
  2454. * )
  2455. * )
  2456. */
  2457. public function getAllSubTransactionsInfo($clusterName, $txnId) {
  2458. LockAcquire();
  2459. $response = array ( "result" => 0, "error" => "");
  2460. $query = "SELECT sub_txn_id, parent_sub_txn_id, state, description"
  2461. . ", progress, sub_txn_type, op_status FROM SubTransactionStatus "
  2462. . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
  2463. . " AND txn_id = " . $this->dbHandle->quote($txnId);
  2464. $this->logger->log_trace("Running query: $query");
  2465. $pdoStmt = $this->dbHandle->query($query);
  2466. if ($pdoStmt === FALSE) {
  2467. $error = $this->getLastDBErrorAsString();
  2468. $this->logger->log_error("Error when executing query"
  2469. . ", query=".$query
  2470. . ", error=".$error);
  2471. $response["result"] = 1;
  2472. $response["error"] = $error;
  2473. LockRelease(); return $response;
  2474. }
  2475. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  2476. $response["clusterName"] = $clusterName;
  2477. $response["txnId"] = $txnId;
  2478. $response["subTxns"] = array();
  2479. foreach ($result as $entry) {
  2480. $subTxn = array();
  2481. $subTxn["subTxnId"] = $entry["sub_txn_id"];
  2482. $subTxn["parentSubTxnId"] = $entry["parent_sub_txn_id"];
  2483. $subTxn["state"] = $entry["state"];
  2484. $subTxn["description"] = $entry["description"];
  2485. $subTxn["progress"] = $entry["progress"];
  2486. $subTxn["subTxnType"] = $entry["sub_txn_type"];
  2487. $subTxn["opStatus"] = $entry["op_status"];
  2488. $response["subTxns"][$entry["sub_txn_id"]] = $subTxn;
  2489. }
  2490. LockRelease(); return $response;
  2491. }
  2492. /**
  2493. * Update op status for sub txn
  2494. * @param string $clusterName
  2495. * @param int $txnId
  2496. * @param int $subTxnId
  2497. * @param string $progress
  2498. * @return mixed
  2499. * array (
  2500. * "result" => 0,
  2501. * "error" => ""
  2502. * )
  2503. */
  2504. public function updateSubTransactionProgress($clusterName, $txnId,
  2505. $subTxnId, $progress) {
  2506. LockAcquire();
  2507. $response = array ( "result" => 0, "error" => "");
  2508. $query = "UPDATE SubTransactionStatus SET progress = "
  2509. . $this->dbHandle->quote($progress)
  2510. . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
  2511. . " AND txn_id = " . $this->dbHandle->quote($txnId)
  2512. . " AND sub_txn_id = " . $this->dbHandle->quote($subTxnId);
  2513. $this->logger->log_trace("Running query: $query");
  2514. $ret = $this->dbHandle->exec($query);
  2515. if (FALSE === $ret) {
  2516. $error = $this->getLastDBErrorAsString();
  2517. $this->logger->log_error("Error when executing query"
  2518. . ", query=".$query
  2519. . ", error=".$error);
  2520. $response["result"] = 1;
  2521. $response["error"] = $error;
  2522. LockRelease(); return $response;
  2523. }
  2524. $rowsChanged = intval($ret);
  2525. $response["sql"] = array ( "rowsChanged" => $rowsChanged);
  2526. LockRelease(); return $response;
  2527. }
  2528. /**
  2529. * Update op status for sub txn
  2530. * @param string $clusterName
  2531. * @param int $txnId
  2532. * @param int $subTxnId
  2533. * @param string $opStatus
  2534. * @return mixed
  2535. * array (
  2536. * "result" => 0,
  2537. * "error" => ""
  2538. * )
  2539. */
  2540. public function updateSubTransactionOpStatus($clusterName, $txnId,
  2541. $subTxnId, $opStatus) {
  2542. LockAcquire();
  2543. $response = array ( "result" => 0, "error" => "");
  2544. $query = "UPDATE SubTransactionStatus SET op_status = "
  2545. . $this->dbHandle->quote($opStatus)
  2546. . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
  2547. . " AND txn_id = " . $this->dbHandle->quote($txnId)
  2548. . " AND sub_txn_id = " . $this->dbHandle->quote($subTxnId);
  2549. $this->logger->log_trace("Running query: $query");
  2550. $ret = $this->dbHandle->exec($query);
  2551. if (FALSE === $ret) {
  2552. $error = $this->getLastDBErrorAsString();
  2553. $this->logger->log_error("Error when executing query"
  2554. . ", query=".$query
  2555. . ", error=".$error);
  2556. $response["result"] = 1;
  2557. $response["error"] = $error;
  2558. LockRelease(); return $response;
  2559. }
  2560. $rowsChanged = intval($ret);
  2561. $response["sql"] = array ( "rowsChanged" => $rowsChanged);
  2562. LockRelease(); return $response;
  2563. }
  2564. /**
  2565. * Get roles which host belongs to
  2566. * @param string $clusterName
  2567. * @param string $hostName
  2568. * @return mixed
  2569. * array (
  2570. * "result" => 0,
  2571. * "error" => "",
  2572. * "clusterName" =>
  2573. * "hosts" => array (
  2574. * "$hostName" => array (
  2575. * "services" => array (
  2576. * "$serviceName" => array (
  2577. * "components" => array (
  2578. * "$compName1" => array (
  2579. * "state" =>
  2580. * "desiredState" =>
  2581. * ),
  2582. * "$compName2" =>
  2583. * ...
  2584. * )
  2585. * )
  2586. * )
  2587. * )
  2588. * )
  2589. * )
  2590. */
  2591. public function getRolesForHosts($clusterName, $hostNames) {
  2592. LockAcquire();
  2593. $response = array ( "result" => 0, "error" => "");
  2594. if (count($hostNames) == 0) {
  2595. LockRelease(); return $response;
  2596. }
  2597. $query = "SELECT HostRoles.host_name as host_name"
  2598. . ", HostRoles.component_name as component_name"
  2599. . ", ServiceComponentInfo.service_name as service_name"
  2600. . ", ServiceComponentInfo.state as component_state"
  2601. . ", ServiceComponentInfo.desired_state as component_desired_state"
  2602. . " FROM HostRoles LEFT JOIN ServiceComponentInfo "
  2603. . " ON HostRoles.cluster_name = ServiceComponentInfo.cluster_name "
  2604. . " AND HostRoles.component_name = ServiceComponentInfo.component_name "
  2605. . " WHERE HostRoles.cluster_name = " . $this->dbHandle->quote($clusterName)
  2606. . " AND ( ";
  2607. $index = 0;
  2608. foreach ($hostNames as $hostName) {
  2609. if ($index != 0) {
  2610. $query .= " OR ";
  2611. }
  2612. ++$index;
  2613. $query .= " HostRoles.host_name = " . $this->dbHandle->quote($hostName);
  2614. }
  2615. $query .= " ) ";
  2616. $this->logger->log_trace("Running query: $query");
  2617. $pdoStmt = $this->dbHandle->query($query);
  2618. if ($pdoStmt === FALSE) {
  2619. $error = $this->getLastDBErrorAsString();
  2620. $this->logger->log_error("Error when executing query"
  2621. . ", query=".$query
  2622. . ", error=".$error);
  2623. $response["result"] = 1;
  2624. $response["error"] = $error;
  2625. LockRelease(); return $response;
  2626. }
  2627. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  2628. $response["clusterName"] = $clusterName;
  2629. $response["hosts"] = array();
  2630. foreach ($result as $entry) {
  2631. if (!isset($response["hosts"][$entry["host_name"]])) {
  2632. $response["hosts"][$entry["host_name"]] = array("services" => array());
  2633. }
  2634. if (!isset($entry["service_name"])
  2635. || $entry["service_name"] == "") {
  2636. $this->logger->log_warn("Invalid entry in host roles table, "
  2637. . print_r($entry, true));
  2638. continue;
  2639. }
  2640. if (!isset($response["hosts"][$entry["host_name"]]["services"][$entry["service_name"]])) {
  2641. $response["hosts"][$entry["host_name"]]["services"][$entry["service_name"]] =
  2642. array( "components" => array());
  2643. }
  2644. $compInfo = array();
  2645. $compInfo["componentName"] = $entry["component_name"];
  2646. $compInfo["state"] = $entry["component_state"];
  2647. $compInfo["desiredState"] = $entry["component_desired_state"];
  2648. $response["hosts"][$entry["host_name"]]["services"][$entry["service_name"]]
  2649. ["components"][$entry["component_name"]] = $compInfo;
  2650. }
  2651. LockRelease(); return $response;
  2652. }
  2653. /**
  2654. * Dump out the contents of the ServiceDependencies table
  2655. * @return mixed
  2656. * array (
  2657. * "result" =>
  2658. * "error" =>
  2659. * "serviceDependencies" => array (
  2660. * "svcName" => array (
  2661. * "svcDep1",
  2662. * ...
  2663. * ),
  2664. * ...
  2665. * )
  2666. * )
  2667. */
  2668. public function getAllServiceDependencies() {
  2669. LockAcquire();
  2670. $response = array ( "result" => 0, "error" => "");
  2671. $query = "SELECT from_service_name, to_service_name "
  2672. . " FROM ServiceDependencies";
  2673. $this->logger->log_trace("Running query: $query");
  2674. $pdoStmt = $this->dbHandle->query($query);
  2675. if ($pdoStmt === FALSE) {
  2676. $error = $this->getLastDBErrorAsString();
  2677. $this->logger->log_error("Error when executing query"
  2678. . ", query=".$query
  2679. . ", error=".$error);
  2680. $response["result"] = 1;
  2681. $response["error"] = $error;
  2682. LockRelease(); return $response;
  2683. }
  2684. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  2685. $svcDeps = array();
  2686. foreach ($result as $entry) {
  2687. if (!isset($svcDeps[$entry["from_service_name"]])) {
  2688. $svcDeps[$entry["from_service_name"]] = array();
  2689. }
  2690. array_push($svcDeps[$entry["from_service_name"]],
  2691. $entry["to_service_name"]);
  2692. }
  2693. $response["serviceDependencies"] = $svcDeps;
  2694. LockRelease(); return $response;
  2695. }
  2696. /**
  2697. * Get recursive dependencies for a given service.
  2698. * @param mixed $serviceDependencies Full Service Dependencies list as
  2699. * obtained from getAllServiceDependencies()
  2700. * @param string $serviceName Service name to get the dependencies for.
  2701. * @return array of service dependencies.
  2702. */
  2703. public function getRecursiveServiceDependency($serviceDependencies,
  2704. $serviceName) {
  2705. if (!isset($serviceDependencies[$serviceName])) {
  2706. return array();
  2707. }
  2708. $svcDeps = array();
  2709. $bfsQ = array ( $serviceName );
  2710. $seen = array();
  2711. while (count($bfsQ) > 0) {
  2712. $svc = array_shift($bfsQ);
  2713. if (isset($seen[$svc])) {
  2714. continue;
  2715. }
  2716. if ($svc != $serviceName) {
  2717. array_push($svcDeps, $svc);
  2718. }
  2719. $seen[$svc] = TRUE;
  2720. if (!isset($serviceDependencies[$svc])) {
  2721. continue;
  2722. }
  2723. $bfsQ = array_merge($bfsQ, $serviceDependencies[$svc]);
  2724. }
  2725. return $svcDeps;
  2726. }
  2727. /**
  2728. * Get recursive dependents for a given service.
  2729. * @param mixed $serviceDependencies Full Service Dependencies list as
  2730. * obtained from getAllServiceDependencies()
  2731. * @param string $serviceName Service name to get the dependents for.
  2732. * @return array of service dependents.
  2733. */
  2734. public function getRecursiveServiceDependents($serviceDependencies,
  2735. $serviceName) {
  2736. $serviceDependents = array();
  2737. foreach ($serviceDependencies as $fromSvc => $toSvcs) {
  2738. foreach ($toSvcs as $toSvc) {
  2739. if (!isset($serviceDependents[$toSvc])) {
  2740. $serviceDependents[$toSvc] = array();
  2741. }
  2742. array_push($serviceDependents[$toSvc], $fromSvc);
  2743. }
  2744. }
  2745. if (!isset($serviceDependents[$serviceName])) {
  2746. return array();
  2747. }
  2748. $svcDeps = array();
  2749. $bfsQ = array ( $serviceName );
  2750. $seen = array();
  2751. while (count($bfsQ) > 0) {
  2752. $svc = array_shift($bfsQ);
  2753. if (isset($seen[$svc])) {
  2754. continue;
  2755. }
  2756. if ($svc != $serviceName) {
  2757. array_push($svcDeps, $svc);
  2758. }
  2759. $seen[$svc] = TRUE;
  2760. if (!isset($serviceDependents[$svc])) {
  2761. continue;
  2762. }
  2763. $bfsQ = array_merge($bfsQ, $serviceDependents[$svc]);
  2764. }
  2765. return $svcDeps;
  2766. }
  2767. /**
  2768. * Set host state
  2769. * @param string $clusterName
  2770. * @param mixed $hostsToUpdate
  2771. * array (
  2772. * "$svc" => array (
  2773. * "$compName" => array (
  2774. * "host1", "host2" ...
  2775. * )
  2776. * )
  2777. * )
  2778. * @param string $state
  2779. * @param boolean $updateDesired
  2780. * @return mixed
  2781. * array (
  2782. * "result" => 0,
  2783. * "error" => "",
  2784. * "sql" => array ( "rowsChanged" => 1 )
  2785. * )
  2786. */
  2787. private function setGenericHostsState($clusterName, $hostsToUpdate,
  2788. $state, $updateDesired) {
  2789. LockAcquire();
  2790. $columnName = "state";
  2791. if ($updateDesired) {
  2792. $columnName = "desired_state";
  2793. }
  2794. $response = array ( "result" => 0, "error" => "");
  2795. $ret = $this->dbHandle->beginTransaction();
  2796. if (!$ret) {
  2797. $error = $this->getLastDBErrorAsString();
  2798. $response["result"] = 1;
  2799. $response["error"] = "Failed to start DB transaction, error=".$error;
  2800. LockRelease(); return $response;
  2801. }
  2802. $rowsChanged = 0;
  2803. foreach ($hostsToUpdate as $svcName => $compList) {
  2804. foreach ($compList as $compName => $hostList) {
  2805. if (count($hostList) == 0) {
  2806. continue;
  2807. }
  2808. $query = "UPDATE HostRoles SET " . $columnName . " = "
  2809. . $this->dbHandle->quote($state) . " WHERE "
  2810. . " cluster_name = " . $this->dbHandle->quote($clusterName)
  2811. . " AND component_name = " . $this->dbHandle->quote($compName)
  2812. . " AND ( ";
  2813. $index = 0;
  2814. foreach ($hostList as $host) {
  2815. if ($index != 0) {
  2816. $query .= " OR ";
  2817. }
  2818. ++$index;
  2819. $query .= " host_name = " . $this->dbHandle->quote($host);
  2820. }
  2821. $query .= " ) ";
  2822. $this->logger->log_trace("Running query: $query");
  2823. $ret = $this->dbHandle->exec($query);
  2824. if (FALSE === $ret) {
  2825. $error = $this->getLastDBErrorAsString();
  2826. $this->dbHandle->rollBack();
  2827. $this->logger->log_error("Error when executing query"
  2828. . ", query=".$query
  2829. . ", error=".$error);
  2830. $response["result"] = 1;
  2831. $response["error"] = $error;
  2832. LockRelease(); return $response;
  2833. }
  2834. $rowsChanged += $ret;
  2835. }
  2836. }
  2837. $ret = $this->dbHandle->commit();
  2838. if (!$ret) {
  2839. $error = $this->getLastDBErrorAsString();
  2840. $response["result"] = 1;
  2841. $response["error"] = "Failed to commit DB transaction, error=".$error;
  2842. LockRelease(); return $response;
  2843. }
  2844. $response["sql"] = array ( "rowsChanged" => $rowsChanged);
  2845. LockRelease(); return $response;
  2846. }
  2847. /**
  2848. * Set host state
  2849. * @param string $clusterName
  2850. * @param mixed $hostsToUpdate
  2851. * array (
  2852. * "$svc" => array (
  2853. * "$compName" => array (
  2854. * "host1", "host2" ...
  2855. * )
  2856. * )
  2857. * )
  2858. * @param string $state
  2859. * @return mixed
  2860. * array (
  2861. * "result" => 0,
  2862. * "error" => "",
  2863. * "sql" => array ( "rowsChanged" => 1 )
  2864. * )
  2865. */
  2866. public function setHostsState($clusterName, $hostsToUpdate, $state) {
  2867. return $this->setGenericHostsState($clusterName, $hostsToUpdate, $state,
  2868. FALSE);
  2869. }
  2870. /**
  2871. * Set host state
  2872. * @param string $clusterName
  2873. * @param mixed $hostsToUpdate
  2874. * array (
  2875. * "$svc" => array (
  2876. * "$compName" => array (
  2877. * "host1", "host2" ...
  2878. * )
  2879. * )
  2880. * )
  2881. * @param string $state Desired State
  2882. * @return mixed
  2883. * array (
  2884. * "result" => 0,
  2885. * "error" => "",
  2886. * "sql" => array ( "rowsChanged" => 1 )
  2887. * )
  2888. */
  2889. public function setHostsDesiredState($clusterName, $hostsToUpdate, $state) {
  2890. return $this->setGenericHostsState($clusterName, $hostsToUpdate, $state,
  2891. TRUE);
  2892. }
  2893. /**
  2894. * Set host role state to match components to which it belongs to
  2895. * @param mixed $hosts
  2896. * array (
  2897. * "$svc" => array (
  2898. * "$compName" => array (
  2899. * "host1", "host2" ...
  2900. * )
  2901. * )
  2902. * )
  2903. * @return mixed
  2904. * array (
  2905. * "result" => 0,
  2906. * "error" => "",
  2907. * "sql" => array ( "rowsChanged" => 1 )
  2908. * )
  2909. */
  2910. private function genericMatchHostStateToComponent($clusterName, $hosts,
  2911. $updateDesired) {
  2912. $response = array ("result" => 0, "error" => "");
  2913. if (count($hosts) == 0) {
  2914. return $response;
  2915. }
  2916. $columnName = "state";
  2917. if ($updateDesired) {
  2918. $columnName = "desired_state";
  2919. }
  2920. $compList = array();
  2921. foreach ($hosts as $svc => $compInfo) {
  2922. foreach ($compInfo as $compName => $hostList)
  2923. $compList[$compName] = $hostList;
  2924. }
  2925. if (count($compList) == 0) {
  2926. return $response;
  2927. }
  2928. LockAcquire();
  2929. $query = "SELECT component_name, ". $columnName . " FROM "
  2930. . " ServiceComponentInfo WHERE "
  2931. . " cluster_name = " . $this->dbHandle->quote($clusterName)
  2932. . " AND ( " ;
  2933. $index = 0;
  2934. foreach ($compList as $compName => $foo) {
  2935. if ($index != 0) {
  2936. $query .= " OR ";
  2937. }
  2938. ++$index;
  2939. $query .= " component_name = " . $this->dbHandle->quote($compName);
  2940. }
  2941. $query .= " ) ";
  2942. $this->logger->log_trace("Running query: $query");
  2943. $pdoStmt = $this->dbHandle->query($query);
  2944. if ($pdoStmt === FALSE) {
  2945. $error = $this->getLastDBErrorAsString();
  2946. $this->logger->log_error("Error when executing query"
  2947. . ", query=".$query
  2948. . ", error=".$error);
  2949. $response["result"] = 1;
  2950. $response["error"] = $error;
  2951. LockRelease(); return $response;
  2952. }
  2953. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  2954. $compState = array();
  2955. foreach ($result as $entry) {
  2956. $compState[$entry["component_name"]] = $entry[$columnName];
  2957. }
  2958. $rowsChanged = 0;
  2959. $ret = $this->dbHandle->beginTransaction();
  2960. if (!$ret) {
  2961. $error = $this->getLastDBErrorAsString();
  2962. $response["result"] = 1;
  2963. $response["error"] = "Failed to start DB transaction, error=".$error;
  2964. LockRelease(); return $response;
  2965. }
  2966. foreach ($compList as $compName => $hostList) {
  2967. if (count($hostList) == 0) {
  2968. continue;
  2969. }
  2970. if (!isset($compState[$compName])) {
  2971. continue;
  2972. }
  2973. $query = " UPDATE HostRoles SET " . $columnName . " = "
  2974. . $this->dbHandle->quote($compState[$compName])
  2975. . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
  2976. . " AND component_name = " . $this->dbHandle->quote($compName)
  2977. . " AND ( ";
  2978. $index = 0;
  2979. foreach ($hostList as $host) {
  2980. if ($index != 0) {
  2981. $query .= " OR ";
  2982. }
  2983. ++$index;
  2984. $query .= " host_name = " . $this->dbHandle->quote($host);
  2985. }
  2986. $query .= " ) ";
  2987. $this->logger->log_trace("Running query: $query");
  2988. $ret = $this->dbHandle->exec($query);
  2989. if (FALSE === $ret) {
  2990. $error = $this->getLastDBErrorAsString();
  2991. $this->dbHandle->rollBack();
  2992. $this->logger->log_error("Error when executing query"
  2993. . ", query=".$query
  2994. . ", error=".$error);
  2995. $response["result"] = 1;
  2996. $response["error"] = $error;
  2997. LockRelease(); return $response;
  2998. }
  2999. $rowsChanged += $ret;
  3000. }
  3001. $ret = $this->dbHandle->commit();
  3002. if (!$ret) {
  3003. $error = $this->getLastDBErrorAsString();
  3004. $response["result"] = 1;
  3005. $response["error"] = "Failed to commit DB transaction, error=".$error;
  3006. LockRelease(); return $response;
  3007. }
  3008. $response["sql"] = array ( "rowsChanged" => $rowsChanged);
  3009. LockRelease(); return $response;
  3010. }
  3011. public function matchHostStateToComponent($clusterName, $hosts) {
  3012. return $this->genericMatchHostStateToComponent($clusterName, $hosts, FALSE);
  3013. }
  3014. public function matchHostDesiredStateToComponent($clusterName, $hosts) {
  3015. return $this->genericMatchHostStateToComponent($clusterName, $hosts, TRUE);
  3016. }
  3017. /**
  3018. * Use the current set of configs in ServiceConfig to create a snapshot
  3019. * in the ConfigHistory table
  3020. * @param string $clusterName
  3021. * @param string $changeLogMsg
  3022. * @return mixed
  3023. * array (
  3024. * "result" => 0,
  3025. * "error" => "",
  3026. * "configSnapshotId" => $snapshotId
  3027. * )
  3028. */
  3029. public function createServiceConfigSnapshot($clusterName, $changeLogMsg) {
  3030. $response = array ("result" => 0, "error" => "");
  3031. $result = $this->getServiceConfig($clusterName);
  3032. if ($result["result"] != 0) {
  3033. return $result;
  3034. }
  3035. $configs = $result["properties"];
  3036. $configBlob = json_encode(
  3037. array ( "clusterName" => $clusterName,
  3038. "properties" => $configs ) );
  3039. $curTime = time();
  3040. LockAcquire();
  3041. $ret = $this->dbHandle->beginTransaction();
  3042. if (!$ret) {
  3043. $error = $this->getLastDBErrorAsString();
  3044. $response["result"] = 1;
  3045. $response["error"] = "Failed to start DB transaction, error=".$error;
  3046. LockRelease(); return $response;
  3047. }
  3048. $query = "INSERT INTO ConfigHistory ( cluster_name, config, change_log, "
  3049. . " update_time ) VALUES ( " . $this->dbHandle->quote($clusterName)
  3050. . ", " . $this->dbHandle->quote($configBlob)
  3051. . ", " . $this->dbHandle->quote($changeLogMsg)
  3052. . ", " . $this->dbHandle->quote($curTime) . " ) ";
  3053. $this->logger->log_trace("Running query: $query");
  3054. $ret = $this->dbHandle->exec($query);
  3055. if (FALSE === $ret) {
  3056. $error = $this->getLastDBErrorAsString();
  3057. $this->dbHandle->rollBack();
  3058. $this->logger->log_error("Error when executing query"
  3059. . ", query=".$query
  3060. . ", error=".$error);
  3061. $response["result"] = 1;
  3062. $response["error"] = $error;
  3063. LockRelease(); return $response;
  3064. }
  3065. $lastInsertId = intval($this->dbHandle->lastInsertId());
  3066. if ($lastInsertId == 0) {
  3067. $this->dbHandle->rollBack();
  3068. $error = "Obtained invalid last insert id from DB";
  3069. $this->logger->log_error($error);
  3070. $response["result"] = 1;
  3071. $response["error"] = $error;
  3072. LockRelease(); return $response;
  3073. }
  3074. $ret = $this->dbHandle->commit();
  3075. if (!$ret) {
  3076. $error = $this->getLastDBErrorAsString();
  3077. $response["result"] = 1;
  3078. $response["error"] = "Failed to commit DB transaction, error=".$error;
  3079. LockRelease(); return $response;
  3080. }
  3081. $response["configSnapshotId"] = $lastInsertId;
  3082. LockRelease(); return $response;
  3083. }
  3084. public function getConfigSnapshot($clusterName, $snapshotId) {
  3085. LockAcquire();
  3086. $response = array ("result" => 0, "error" => "");
  3087. $query = "SELECT config, change_log, update_time FROM ConfigHistory"
  3088. . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
  3089. . " AND version = " . $this->dbHandle->quote($snapshotId);
  3090. $this->logger->log_trace("Running query: $query");
  3091. $pdoStmt = $this->dbHandle->query($query);
  3092. if ($pdoStmt === FALSE) {
  3093. $error = $this->getLastDBErrorAsString();
  3094. $this->logger->log_error("Error when executing query"
  3095. . ", query=".$query
  3096. . ", error=".$error);
  3097. $response["result"] = 1;
  3098. $response["error"] = $error;
  3099. LockRelease(); return $response;
  3100. }
  3101. $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
  3102. $response["clusterName"] = $clusterName;
  3103. $response["configSnapshotId"] = $snapshotId;
  3104. if (isset($result) && is_array($result) && count($result) == 1) {
  3105. $response["changeLog"] = $result[0]["change_log"];
  3106. $response["config"] = json_decode($result[0]["config"], true);
  3107. $response["updateTime"] = $result[0]["update_time"];
  3108. LockRelease(); return $response;
  3109. }
  3110. $response["result"] = 1;
  3111. $response["error"] = "Could not find config snapshot in DB";
  3112. LockRelease(); return $response;
  3113. }
  3114. private function deleteClusterTable ($clusterName, $table) {
  3115. LockAcquire();
  3116. $response = array ("result" => 0, "error" => "");
  3117. $query = "DELETE from " . $table . " WHERE cluster_name = "
  3118. . $this->dbHandle->quote($clusterName);
  3119. $this->logger->log_trace("Running query: $query");
  3120. $pdoStmt = $this->dbHandle->query($query);
  3121. if ($pdoStmt == FALSE) {
  3122. $error = $this->getLastDBErrorAsString();
  3123. $this->logger->log_error("Error when executing query"
  3124. . ", query=".$query
  3125. . ", error=".$error);
  3126. $response["result"] = 1;
  3127. $response["error"] = $error;
  3128. LockRelease(); return $response;
  3129. }
  3130. LockRelease(); return $response;
  3131. }
  3132. public function cleanupServices($clusterName) {
  3133. $this->deleteClusterTable($clusterName, "ServiceConfig");
  3134. $this->deleteClusterTable($clusterName, "ServiceInfo");
  3135. }
  3136. public function cleanupHosts ($clusterName) {
  3137. $this->deleteClusterTable($clusterName, "Hosts");
  3138. $this->deleteClusterTable($clusterName, "HostRoles");
  3139. $this->cleanupServices($clusterName);
  3140. }
  3141. public function cleanupCluster ($clusterName) {
  3142. $this->deleteClusterTable($clusterName, "Clusters");
  3143. $this->deleteClusterTable($clusterName, "ServiceComponentInfo");
  3144. $this->cleanupHosts($clusterName);
  3145. }
  3146. }
  3147. ?>