1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286 |
- <?php
- class HMCDBAccessor {
- private $dbHandle;
- private $dbPath;
- private $logger;
- function __construct($db) {
- $this->dbPath = $db;
- $this->logger = new HMCLogger("HMCDBAccessor");
- $dsn = "sqlite:".$db;
- if (!file_exists($db)) {
- $this->logger->log_error("Sqlite db file does not exist, db=$db");
- throw new Exception("Could not find sqlite db file, db=$db");
- }
- $this->dbHandle = new PDO($dsn);
- if ($this->dbHandle === FALSE) {
- $this->logger->log_error("Could not open sqlite db, db=$db");
- throw new Exception("Could not open sqlite db, db=$db");
- }
- }
- private function getLastDBErrorAsString() {
- $error = "";
- $pdoError = $this->dbHandle->errorInfo();
- if (isset($pdoError) && is_array($pdoError)) {
- if (isset($pdoError[0])) {
- $error = "sqlStateErrorCode=".$pdoError[0];
- if (isset($pdoError[1])) {
- $error .= ", driverErrCode=".$pdoError[1];
- }
- if (isset($pdoError[1])) {
- $error .= ", driverErrMsg=".$pdoError[2];
- }
- }
- }
- else {
- $error = "Unknown DB error";
- }
- return $error;
- }
- /**
- * Create a new cluster
- * @param string $clusterName Cluster Name
- * @param string $version Version
- * @param string $state Current state
- * @return mixed
- * array (
- * "clusterName" => $clusterName,
- * "result" => 0,
- * "error" => "");
- */
- public function createCluster($clusterName, $version, $state) {
- LockAcquire();
- $query = "INSERT INTO Clusters "
- . " ( cluster_name, version, state ) "
- . " VALUES ( "
- . $this->dbHandle->quote($clusterName) . " , "
- . $this->dbHandle->quote($version) . " , "
- . $this->dbHandle->quote($state) . " )";
- $response = array ( "clusterName" => $clusterName,
- "result" => 0, "error" => "");
- $this->logger->log_trace("Running query: $query");
- $ret = $this->dbHandle->exec($query);
- if (FALSE === $ret) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- LockRelease(); return $response;
- }
-
- /**
- * Update cluster state for a given clusterName
- * @param string $clusterName Cluster Name
- * @param string $state New state of cluster
- * @return mixed
- * array ( "state" => $clusterState,
- * "result" => 0,
- * "error" => "");
- */
- public function getClusterState ($clusterName) {
- LockAcquire();
- $query = "SELECT state FROM Clusters WHERE cluster_name = "
- . $this->dbHandle->quote($clusterName);
- $response = array ( "result" => 0, "error" => "");
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- if (isset($result) && is_array($result) && count($result) == 1) {
- $response["state"] = $result[0]["state"];
- LockRelease(); return $response;
- }
- $response["result"] = 1;
- $response["error"] = "Result is not set or not array or count is not 1 ".json_encode($result);
- LockRelease(); return $response;
- }
- /**
- * Update cluster state for a given clusterName
- * @param string $clusterName Cluster Name
- * @param string $state New state of cluster
- * @return mixed
- * array ( "clusterName" => $clusterName,
- * "clusterState" => $clusterState,
- * "oldClusterState" => $oldClusterState,
- * "result" => 0,
- * "error" => "");
- */
- public function setClusterState($clusterName, $state) {
- LockAcquire();
- $response = array ( "clusterName" => $clusterName,
- "result" => 0, "error" => "");
- $ret = $this->dbHandle->beginTransaction();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $response["result"] = 1;
- $response["error"] = "Failed to start DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- $query = "SELECT state FROM Clusters WHERE cluster_name = "
- . $this->dbHandle->quote($clusterName);
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->dbHandle->rollBack();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- if (isset($result) && is_array($result) && count($result) == 1) {
- $response["oldClusterState"] = $result[0]["state"];
- $query = "UPDATE Clusters SET state = " . $this->dbHandle->quote($state)
- . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName);
- $this->logger->log_trace("Running query: $query");
- $ret = $this->dbHandle->exec($query);
- if (FALSE === $ret) {
- $error = $this->getLastDBErrorAsString();
- $this->dbHandle->rollBack();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $ret = $this->dbHandle->commit();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $response["result"] = 1;
- $response["error"] = "Failed to commit DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- $response["clusterState"] = $state;
- LockRelease();
- return $response;
- }
- $this->dbHandle->rollBack();
- $response["result"] = 1;
- $response["error"] = "Could not find cluster name".$clusterName." in DB";
- LockRelease(); return $response;
- }
- /**
- * Get info on all clusters
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "clusters" => array (
- * "$clusterName" => array (
- * "clusterName" => $clusterName,
- * "version" => $version,
- * "state" => $state
- * ),
- * ...
- * )
- * );
- */
- public function getAllClusters() {
- LockAcquire();
- $query = "SELECT cluster_name, version, state FROM Clusters";
- $response = array ( "result" => 0, "error" => "");
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- $response["clusters"] = array();
- foreach ($result as $entry) {
- $cluster = array();
- $cluster["clusterName"] = $entry["cluster_name"];
- $cluster["state"] = $entry["state"];
- $cluster["version"] = $entry["version"];
- $response["clusters"][$cluster["clusterName"]] = $cluster;
- }
- LockRelease(); return $response;
- }
- /////////////////////////////////////////// Services related APIs ////////////////////////////////////////////
- /**
- * Get the list of all possible services supported by the hadoop statck
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "services" => array (
- * "$serviceName" => array (
- * "serviceName" => $serviceName,
- * "displayName" => $displayName,
- * "description" => $desc,
- * "attributes" => array( "isMaster" => boolean, "isClient" => boolean )
- * ),
- * ...
- * )
- * );
- */
- public function getAllServicesList() {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "");
- $query = "SELECT service_name, description, display_name, attributes FROM Services";
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- $response["services"] = array();
- foreach ($result as $entry) {
- $svcInfo = array();
- $svcInfo["serviceName"] = $entry["service_name"];
- $svcInfo["displayName"] = $entry["display_name"];
- $svcInfo["description"] = $entry["description"];
- $svcInfo["attributes"] = json_decode($entry["attributes"], true);
- $response["services"][$svcInfo["serviceName"]] = $svcInfo;
- }
- LockRelease(); return $response;
- }
- /**
- * Get all components for all services that are supported.
- * @return
- * array (
- * "result" => 0,
- * "error" => "",
- * "services" => array (
- * "$serviceName" => array (
- * "serviceName" => $serviceName,
- * "components" => array (
- * "$componentName" => array (
- * "componentName" =>
- * "isMaster" =>
- * "isClient" =>
- * "displayName" =>
- * "description" =>
- * )
- * )
- * )
- * )
- * )
- */
- public function getAllServiceComponentsList() {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "");
- $query = "SELECT service_name, component_name, display_name"
- . ", attributes, description FROM ServiceComponents";
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- $response["services"] = array();
- foreach ($result as $entry) {
- $serviceName = $entry["service_name"];
- if (!isset($response["services"]["$serviceName"])) {
- $response["services"][$serviceName] = array();
- $response["services"][$serviceName]["serviceName"] = $serviceName;
- $response["services"][$serviceName]["components"] = array();
- }
- $comp = array();
- $comp["serviceName"] = $serviceName;
- $comp["componentName"] = $entry["component_name"];
- $comp["displayName"] = $entry["display_name"];
- $comp["attributes"] = json_decode($entry["attributes"], true);
- /* $comp["isMaster"] and $comp["isClient"] are populated only for
- * backward compatibility (with when is_master and is_client were
- * fields in this table) and should be removed once all the code
- * that uses this method is fixed to look at
- * $comp["attributes"]["isMaster"] directly.
- */
- $comp["isMaster"] = FALSE;
- if (isset($comp["attributes"]["isMaster"])) {
- $comp["isMaster"] = $comp["attributes"]["isMaster"];
- }
- $comp["isClient"] = FALSE;
- if (isset($comp["attributes"]["isClient"])) {
- $comp["isClient"] = $comp["attributes"]["isClient"];
- }
- $response["services"][$serviceName]["components"]
- [$comp["componentName"]] = $comp;
- }
- LockRelease(); return $response;
- }
- /**
- * Get the list of all services that are available for the given clusterName
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "clusterName" => $clusterName,
- * "services" => array (
- * "$serviceName" => array (
- * "serviceName" => $serviceName,
- * "state" => $state,
- * "desiredState" => $desiredState,
- * "isEnabled" => $isEnabled
- * ),
- * ...
- * )
- * );
- */
- public function getAllServicesInfo($clusterName) {
- LockAcquire();
- $query = "SELECT ServiceInfo.service_name as service_name"
- . ", ServiceInfo.state as state"
- . ", ServiceInfo.desired_state as desired_state"
- . ", ServiceInfo.is_enabled as is_enabled FROM ServiceInfo "
- . " LEFT JOIN Services ON ServiceInfo.service_name = Services.service_name "
- . " WHERE ServiceInfo.cluster_name = " . $this->dbHandle->quote($clusterName)
- . " ORDER BY Services.id ";
- $response = array ( "result" => 0, "error" => "");
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- $response["clusterName"] = $clusterName;
- $response["services"] = array();
- foreach ($result as $entry) {
- $svcInfo = array();
- $svcInfo["serviceName"] = $entry["service_name"];
- $svcInfo["state"] = $entry["state"];
- $svcInfo["desiredState"] = $entry["desired_state"];
- $svcInfo["isEnabled"] = $entry["is_enabled"];
- $response["services"][$svcInfo["serviceName"]] = $svcInfo;
- }
- LockRelease(); return $response;
- }
- /**
- * Get information related to a service
- * @param string $clusterName
- * @param string $serviceName
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "clusterName" => $clusterName,
- * "serviceName" => $serviceName,
- * "isEnabled" => $isEnabled,
- * "state" => $state,
- * "desiredState" => $desiredState
- * )
- */
- public function getServiceInfo($clusterName, $serviceName) {
- LockAcquire();
- $error = "";
- $query = "SELECT is_enabled, state, desired_state FROM ServiceInfo "
- . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
- . " AND service_name = " . $this->dbHandle->quote($serviceName);
- $response = array ( "result" => 0, "error" => "");
- $response["clusterName"] = $clusterName;
- $response["serviceName"] = $serviceName;
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- if (isset($result) && is_array($result) && count($result) == 1) {
- $response["isEnabled"] = $result[0]["is_enabled"];
- $response["state"] = $result[0]["state"];
- $response["desiredState"] = $result[0]["desired_state"];
- LockRelease(); return $response;
- }
- $response["result"] = 1;
- $response["error"] = "Could not find service in DB";
- LockRelease(); return $response;
- }
- /**
- * Set Service Desired State
- * @param clusterName Cluster Name
- * @param serviceName Service Name
- * @param desiredState New Desired State
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "desiredState" => $desiredState,
- * "oldDesiredState" => $oldDesiredState,
- * "clusterName" =>
- * "serviceName" =>
- * )
- */
- public function setServiceDesiredState($clusterName, $serviceName, $desiredState) {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "",
- "clusterName" => $clusterName, "serviceName" => $serviceName);
- $error = "";
- $ret = $this->dbHandle->beginTransaction();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $response["result"] = 1;
- $response["error"] = "Failed to start DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- $query = "SELECT desired_state FROM ServiceInfo"
- . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
- . " AND service_name = " . $this->dbHandle->quote($serviceName);
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->dbHandle->rollBack();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- if (isset($result) && is_array($result) && count($result) == 1) {
- $response["oldDesiredState"] = $result[0]["desired_state"];
- $query = "UPDATE ServiceInfo SET desired_state = "
- . $this->dbHandle->quote($desiredState)
- . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
- . " AND service_name = " . $this->dbHandle->quote($serviceName);
- $this->logger->log_trace("Running query: $query");
- $ret = $this->dbHandle->exec($query);
- if (FALSE === $ret) {
- $error = $this->getLastDBErrorAsString();
- $this->dbHandle->rollBack();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $ret = $this->dbHandle->commit();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $response["result"] = 1;
- $response["error"] = "Failed to commit DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- $response["desiredState"] = $desiredState;
- LockRelease(); return $response;
- }
- $this->dbHandle->rollBack();
- $response["result"] = 1;
- $response["error"] = "Could not find service in DB";
- LockRelease(); return $response;
- }
- /**
- * Set Service State
- * @param clusterName Cluster Name
- * @param serviceName Service Name
- * @param desiredState New Desired State
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "state" => $state,
- * "oldState" => $oldState,
- * "clusterName" =>
- * "serviceName" =>
- * )
- */
- public function setServiceState($clusterName, $serviceName, $state) {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "",
- "clusterName" => $clusterName, "serviceName" => $serviceName);
- $error = "";
- $ret = $this->dbHandle->beginTransaction();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $response["result"] = 1;
- $response["error"] = "Failed to start DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- $query = "SELECT state FROM ServiceInfo"
- . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
- . " AND service_name = " . $this->dbHandle->quote($serviceName);
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->dbHandle->rollBack();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- if (isset($result) && is_array($result) && count($result) == 1) {
- $response["oldState"] = $result[0]["state"];
- $query = "UPDATE ServiceInfo SET state = "
- . $this->dbHandle->quote($state)
- . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
- . " AND service_name = " . $this->dbHandle->quote($serviceName);
- $this->logger->log_trace("Running query: $query");
- $ret = $this->dbHandle->exec($query);
- if (FALSE === $ret) {
- $error = $this->getLastDBErrorAsString();
- $this->dbHandle->rollBack();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $ret = $this->dbHandle->commit();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $response["result"] = 1;
- $response["error"] = "Failed to commit DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- $response["state"] = $state;
- LockRelease(); return $response;
- }
- $this->dbHandle->rollBack();
- $response["result"] = 1;
- $response["error"] = "Could not find service in DB";
- LockRelease(); return $response;
- }
- /**
- * Add list of services for cluster
- * @param string $clusterName
- * @param mixed $services
- * array (
- * array (
- * "serviceName" => ,
- * "state" =>
- * "desiredState" =>
- * "isEnabled" =>
- * )
- * )
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => ""
- * )
- */
- public function addServicesToCluster($clusterName, $services) {
- $response = array ( "result" => 0, "error" => "",
- "clusterName" => $clusterName);
- $error = "";
- $supportedServices = $this->getAllServicesList();
- if ($supportedServices === FALSE
- || $supportedServices["result"] != 0) {
- return $supportedServices;
- }
- LockAcquire();
- // sanitize input
- $sanitizedSvcs = array();
- foreach ($services as $service) {
- if (!isset($service["serviceName"])
- || $service["serviceName"] == "") {
- $response["error"] = "Invalid arguments";
- $response["result"] = 1;
- LockRelease(); return $response;
- }
- if (!isset($supportedServices["services"][$service["serviceName"]])) {
- $response["error"] = "Invalid service - not supported, svc="
- . $service["serviceName"];
- $response["result"] = 1;
- LockRelease(); return $response;
- }
- if (!isset($service["state"])) {
- $service["state"] = "";
- }
- if (!isset($service["desiredState"])) {
- $service["desiredState"] = "";
- }
- if (!isset($service["isEnabled"])) {
- $service["isEnabled"] = 0;
- }
- else {
- if ($service["isEnabled"]) {
- $service["isEnabled"] = 1;
- }
- else {
- $service["isEnabled"] = 0;
- }
- }
- $sanitizedSvcs[$service["serviceName"]] = $service;
- }
- $ret = $this->dbHandle->beginTransaction();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $response["result"] = 1;
- $response["error"] = "Failed to start DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- foreach ($sanitizedSvcs as $service) {
- $query = "INSERT INTO ServiceInfo "
- . " ( cluster_name, service_name, state, desired_state, is_enabled ) "
- . " VALUES "
- . " ("
- . $this->dbHandle->quote($clusterName) . " , "
- . $this->dbHandle->quote($service["serviceName"]) . " , "
- . $this->dbHandle->quote($service["state"]) . " , "
- . $this->dbHandle->quote($service["desiredState"]) . " , "
- . $service["isEnabled"]
- . " )";
- $this->logger->log_trace("Running query: $query");
- $ret = $this->dbHandle->exec($query);
- if (FALSE === $ret) {
- $error = $this->getLastDBErrorAsString();
- $this->dbHandle->rollBack();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- }
- $ret = $this->dbHandle->commit();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $response["result"] = 1;
- $response["error"] = "Failed to commit DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- LockRelease(); return $response;
- }
- /////////////////////////////////////////// End of services related APIs ////////////////////////////////////////////
- /////////////////////////////////////////// Hosts related APIs ////////////////////////////////////////////
- /**
- * Add new hosts to Hosts table for a given cluster.
- * Replaces existing hosts if needed
- * @param string $clusterName ClusterName
- * @param mixed $hostsInfo Mixed array containing host info
- * array (
- * array (
- * "hostName" =>
- * "ip" =>
- * "totalMem" =>
- * "cpuCount" =>
- * "osArch" =>
- * "osType" =>
- * "os" =>
- * "disksInfo" =>
- * "discoveryStatus" =>
- * "badHealthReason" =>
- * "attributes" => array ( ... )
- * ),
- * )
- * @return mixed
- * array ( "clusterName" => $clusterName,
- * "hosts" => array ($hostName1, $hostName2 ...),
- * "result" => 0,
- * "error" => ""
- * );
- */
- public function addHostsToCluster($clusterName, $hostsInfo) {
- LockAcquire();
- $response = array( "result" => 0, "error" => "");
- if (empty($hostsInfo)) {
- LockRelease(); return $response;
- }
- $hosts = array();
- if (empty($hostsInfo)) {
- return $response;
- }
- foreach ($hostsInfo as $hostInfo) {
- if (!isset($hostInfo["hostName"])
- || !isset($hostInfo["discoveryStatus"])) {
- $response["result"] = 1;
- $response["error"] = "Invalid arguments";
- LockRelease(); return $response;
- }
- // sanitize input
- if (!isset($hostInfo["totalMem"])) {
- $hostInfo["totalMem"] = 0;
- }
- if (!isset($hostInfo["cpuCount"])) {
- $hostInfo["cpuCount"] = 0;
- }
- if (!isset($hostInfo["osArch"])) {
- $hostInfo["os_arch"] = "";
- }
- if (!isset($hostInfo["osType"])) {
- $hostInfo["osType"] = "";
- }
- if (!isset($hostInfo["os"])) {
- $hostInfo["os"] = "";
- }
- if (!isset($hostInfo["disksInfo"])) {
- $hostInfo["disksInfo"] = "";
- }
- if (!isset($hostInfo["badHealthReason"])) {
- $hostInfo["badHealthReason"] = "";
- }
- if (!isset($hostInfo["attributes"])) {
- $hostInfo["attributes"] = array();
- }
- $hostInfo["attributes"] = json_encode($hostInfo["attributes"]);
- array_push($hosts, $hostInfo);
- }
- $response["clusterName"] = $clusterName;
- $ret = $this->dbHandle->beginTransaction();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $response["result"] = 1;
- $response["error"] = "Failed to start DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- foreach ($hosts as $hostInfo) {
- $this->logger->log_debug(json_encode(($hostInfo)));
- $query = " INSERT OR REPLACE INTO Hosts "
- . " ( cluster_name, host_name, ip, total_mem, cpu_count, "
- . " os_arch, os_type, os, disks_info, discovery_status, "
- . " bad_health_reason, attributes ) "
- . " VALUES "
- . " ("
- . $this->dbHandle->quote($clusterName) . " , "
- . $this->dbHandle->quote($hostInfo["hostName"]) . " , "
- . $this->dbHandle->quote($hostInfo["ip"]) . " , "
- . $this->dbHandle->quote($hostInfo["totalMem"]) . " , "
- . $this->dbHandle->quote($hostInfo["cpuCount"]) . " , "
- . $this->dbHandle->quote($hostInfo["osArch"]) . " , "
- . $this->dbHandle->quote($hostInfo["osType"]) . " , "
- . $this->dbHandle->quote($hostInfo["os"]) . " , "
- . $this->dbHandle->quote($hostInfo["disksInfo"]) . " , "
- . $this->dbHandle->quote($hostInfo["discoveryStatus"]) . " , "
- . $this->dbHandle->quote($hostInfo["badHealthReason"]) . " , "
- . $this->dbHandle->quote($hostInfo["attributes"])
- . " ) ";
- $this->logger->log_trace("Running query: $query");
- $ret = $this->dbHandle->exec($query);
- if (FALSE === $ret) {
- $error = $this->getLastDBErrorAsString();
- $this->dbHandle->rollBack();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- }
- $ret = $this->dbHandle->commit();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Failed to commit DB transaction, error=".$error);
- $response["result"] = 1;
- $response["error"] = "Failed to commit DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- $error = "";
- $query = "SELECT host_name FROM Hosts WHERE "
- . " ( cluster_name = " . $this->dbHandle->quote($clusterName) . " ) AND ( ";
- $index = 0;
- foreach ($hostsInfo as $hostInfo) {
- if ($index != 0) {
- $query .= " OR ";
- }
- $index++;
- $query .= " host_name = " . $this->dbHandle->quote($hostInfo["hostName"]);
- }
- $query .= " ) ";
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- $response["hosts"] = array();
- if (isset($result) && is_array($result) && count($result) > 0) {
- foreach ($result as $entry) {
- $host["hostName"] = $entry["host_name"];
- array_push($response["hosts"], $entry["host_name"]);
- }
- LockRelease(); return $response;
- }
- $response["result"] = 1;
- $response["error"] = "Invalid result set returned from DB";
- LockRelease(); return $response;
- }
- /**
- * Add new hosts to Hosts table for a given cluster.
- * Replaces existing hosts if needed
- * @param string $clusterName ClusterName
- * @param mixed $hostsInfo Mixed array containing host info
- * array (
- * "$hostName" => array (
- * "discoveryStatus" =>
- * "badHealthReason" =>
- * ),
- * )
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "sql" => array ( "rowsChanged" => 1 )
- * )
- */
- public function updateHostDiscoveryStatus($clusterName, $hostsInfo) {
- LockAcquire();
- $response = array( "result" => 0, "error" => "");
- $hosts = array();
- foreach ($hostsInfo as $hostName => $hostInfo) {
- if (!isset($hostInfo["discoveryStatus"])) {
- $response["result"] = 1;
- $response["error"] = "Invalid arguments";
- LockRelease(); return $response;
- }
- if (!isset($hostInfo["badHealthReason"])) {
- $hostInfo["badHealthReason"] = "";
- }
- $hosts[$hostName] = $hostInfo;
- }
- $ret = $this->dbHandle->beginTransaction();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $response["result"] = 1;
- $response["error"] = "Failed to start DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- $rowsChanged = 0;
- foreach ($hosts as $hostName => $hostInfo) {
- $this->logger->log_debug(json_encode(($hostInfo)));
- $query = " UPDATE Hosts SET "
- . " discovery_status = "
- . $this->dbHandle->quote($hostInfo["discoveryStatus"])
- . ", bad_health_reason = "
- . $this->dbHandle->quote($hostInfo["badHealthReason"])
- . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
- . " AND host_name = " . $this->dbHandle->quote($hostName);
- $this->logger->log_trace("Running query: $query");
- $ret = $this->dbHandle->exec($query);
- if (FALSE === $ret) {
- $error = $this->getLastDBErrorAsString();
- $this->dbHandle->rollBack();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $rowsChanged += $ret;
- }
- $ret = $this->dbHandle->commit();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Failed to commit DB transaction, error=".$error);
- $response["result"] = 1;
- $response["error"] = "Failed to commit DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- $response["sql"] = array ( "rowsChanged" => $rowsChanged);
- LockRelease(); return $response;
- }
- /**
- * Get information on all hosts
- * @param string $clusterName
- * @param mixed $filter
- * - only supports equal operand
- * - only supports discoveryStatus filter
- * - array (
- * "=" => array ( "discoveryStatus" => "SUCCESS" )
- * )
- * - format
- * "$operand" => array ( "$column1" => "$value1", ... )
- * @param mixed $order order by particular column
- * - only supports hostName, ip, totalMem, cpuCount, osArch, osType
- * array (
- * "sortColumn" => "totalMem",
- * "sortOrder" => "ASC/DESC"
- * )
- * - optionally, an array of [ "sortColumn" => $sortColumn, "sortOrder" => $sortOrder]
- * can be used
- *
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "clusterName" => $clusterName,
- * "hosts" => array (
- * array (
- * "hostName" =>
- * "ip" =>
- * "totalMem" =>
- * "cpuCount" =>
- * "osArch" =>
- * "osType" =>
- * "os" =>
- * "disksInfo" =>
- * "discoveryStatus" =>
- * "badHealthReason" =>
- * "attributes" => array ( ... )
- * ),
- * ...
- * )
- * )
- */
- public function getAllHostsInfo($clusterName, $filter = array(),
- $order = array()) {
- LockAcquire();
- $error = "";
- $query = "SELECT "
- . " host_name, ip, total_mem, "
- . " cpu_count, os_arch, os_type, os, disks_info, "
- . " discovery_status, bad_health_reason, attributes "
- . " FROM Hosts WHERE cluster_name = "
- . $this->dbHandle->quote($clusterName);
- if (is_array($filter) && !empty($filter)) {
- foreach ($filter as $operand => $cols) {
- if ($operand == "=" || $operand == "!=") {
- foreach ($cols as $columnName => $value) {
- if ($columnName == "discoveryStatus") {
- $query .= " AND discovery_status " . $operand . " "
- . $this->dbHandle->quote($value);
- }
- }
- }
- }
- }
- $using_sort = FALSE;
- if (isset($order) && is_array($order)) {
- if (sizeof($order) > 0) {
- $query .= " ORDER BY ";
- $using_sort = TRUE;
- }
- while (sizeof($order) > 0) {
- // is it an array of arrays, for an array of sortColumn and sortOrder?
- if (is_array($order[0])) {
- $this->logger->log_debug('yo');
- $orderItem = array_shift($order);
- } else {
- $orderItem["sortColumn"] = array_shift($order);
- $orderItem["sortOrder"] = array_shift($order);
- }
- if (isset($orderItem["sortColumn"])) {
- if ($orderItem["sortColumn"] == "hostName") {
- $query .= "host_name";
- }
- else if ($orderItem["sortColumn"] == "ip") {
- $query .= "ip";
- }
- else if ($orderItem["sortColumn"] == "totalMem") {
- $query .= "total_mem";
- }
- else if ($orderItem["sortColumn"] == "cpuCount") {
- $query .= "cpu_count";
- }
- else if ($orderItem["sortColumn"] == "osArch") {
- $query .= "os_arch";
- }
- else if ($orderItem["sortColumn"] == "osType") {
- $query .= "os_type";
- }
- if (isset($orderItem["sortOrder"])) {
- $query .= " ".$orderItem["sortOrder"];
- if (sizeof($order) > 0) {
- $query .= ',';
- }
- }
- }
- }
- }
- $response = array ( "result" => 0, "error" => "");
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- $response["hosts"] = array();
- $response["clusterName"] = $clusterName;
- foreach ($result as $entry) {
- $host = array();
- $host["hostName"] = $entry["host_name"];
- $host["ip"] = $entry["ip"];
- $host["totalMem"] = $entry["total_mem"];
- $host["cpuCount"] = $entry["cpu_count"];
- $host["osArch"] = $entry["os_arch"];
- $host["osType"] = $entry["os_type"];
- $host["os"] = $entry["os"];
- $host["disksInfo"] = $entry["disks_info"];
- $host["discoveryStatus"] = $entry["discovery_status"];
- $host["badHealthReason"] = $entry["bad_health_reason"];
- $host["attributes"] = json_decode($entry["attributes"], true);
- array_push($response["hosts"], $host);
- }
- LockRelease(); return $response;
- }
- /**
- * Get information on a specified host
- * @param string $clusterName
- * @param string $hostName
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "clusterName" => $clusterName,
- * "hostName" =>
- * "ip" =>
- * "totalMem" =>
- * "cpuCount" =>
- * "osArch" =>
- * "osType" =>
- * "os" =>
- * "disksInfo" =>
- * "discoveryStatus" =>
- * "badHealthReason" =>
- * "attributes" => array ( ... )
- * )
- */
- public function getHostInfo($clusterName, $hostName) {
- LockAcquire();
- $error = "";
- $query = "SELECT "
- . " host_name, ip, total_mem, "
- . " cpu_count, os_arch, os_type, os, disks_info, "
- . " discovery_status, bad_health_reason, attributes "
- . " FROM Hosts WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
- . " AND host_name = " . $this->dbHandle->quote($hostName);
- $response = array ( "result" => 0, "error" => "");
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- $response["clusterName"] = $clusterName;
- if (isset($result) && is_array($result) && count($result) == 1) {
- $entry = $result[0];
- $response["hostName"] = $entry["host_name"];
- $response["ip"] = $entry["ip"];
- $response["totalMem"] = $entry["total_mem"];
- $response["cpuCount"] = $entry["cpu_count"];
- $response["osArch"] = $entry["os_arch"];
- $response["osType"] = $entry["os_type"];
- $response["os"] = $entry["os"];
- $response["disksInfo"] = $entry["disks_info"];
- $response["discoveryStatus"] = $entry["discovery_status"];
- $response["badHealthReason"] = $entry["bad_health_reason"];
- $response["attributes"] = json_decode($entry["attributes"], true);
- LockRelease(); return $response;
- }
- $response["result"] = 1;
- $response["error"] = "Could not find host in DB";
- LockRelease(); return $response;
- }
- /////////////////////////////////////////// End of hosts related APIs ////////////////////////////////////////////
- /////////////////////////////////////////// Configuration related APIs ////////////////////////////////////////////
- /**
- * Get meta info about all config properties
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "configs" => array(
- * "key" => array (
- * "key" => $key,
- * "displayName" => $displayName,
- * "serviceName" => $serviceName
- * "description" => $description,
- * "displayType" => $displayType,
- * "displayAttributes" => array ( ... )
- * "value" => $default_value
- * )
- * .....
- * );
- */
- public function getConfigPropertiesMetaInfo() {
- LockAcquire();
- $error = "";
- $query = "SELECT "
- . " key, display_name, service_name, description, default_value"
- . ", display_type, display_attributes "
- . " FROM ConfigProperties";
- $response = array ( "result" => 0, "error" => "", "configs" => array());
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- foreach ($result as $entry) {
- $prop = array();
- $prop["key"] = $entry["key"];
- $prop["displayName"] = $entry["display_name"];
- $prop["serviceName"] = $entry["service_name"];
- $prop["description"] = $entry["description"];
- $prop["displayType"] = $entry["display_type"];
- $prop["displayAttributes"] = json_decode($entry["display_attributes"], true);
- $prop["value"] = $entry["default_value"];
- $response["configs"][$prop["key"]] = $prop;
- }
- LockRelease(); return $response;
- }
- /**
- * Get all configuration properties for a given cluster
- * @param string $clusterName
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "clusterName" => $clusterName,
- * "properties" => array (
- * "key1" => "val1",
- * "key2" => "val2",
- * ...
- * )
- * )
- */
- public function getServiceConfig($clusterName) {
- LockAcquire();
- $error = "";
- $query = "SELECT key, value FROM ServiceConfig "
- . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName);
- $response = array ( "result" => 0, "error" => "");
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- $response["properties"] = array();
- $response["clusterName"] = $clusterName;
- foreach ($result as $entry) {
- $response["properties"][$entry["key"]] = $entry["value"];
- }
- LockRelease(); return $response;
- }
- /**
- * Update config properties for a given cluster
- * Updates prop key if it exists or inserts a new entry if not found.
- * @param string $clusterName
- * @param mixed $config
- * array of properties as key-val pairs
- * $config = array ( "prop_key1" => "prop_val1", ... );
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "sql" => array ( "rowsChanged" => 1 )
- * )
- */
- public function updateServiceConfigs($clusterName, $config) {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "");
- if (count($config) == 0) {
- // no-op
- LockRelease(); return $response;
- }
- $error = "";
- $rowsChanged = 0;
- $ret = $this->dbHandle->beginTransaction();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $response["result"] = 1;
- $response["error"] = "Failed to start DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- foreach ($config as $key=>$val) {
- $query = " INSERT OR REPLACE INTO ServiceConfig "
- . " ( cluster_name, key, value ) "
- . " VALUES "
- . " ( " . $this->dbHandle->quote($clusterName) . ", "
- . $this->dbHandle->quote($key) . ", "
- . $this->dbHandle->quote($val) . " ) ";
- $this->logger->log_trace("Running query: $query");
- $ret = $this->dbHandle->exec($query);
- if (FALSE === $ret) {
- $error = $this->getLastDBErrorAsString();
- $ret = $this->dbHandle->rollBack();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $rowsChanged += $ret;
- }
- $ret = $this->dbHandle->commit();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Failed to commit DB transaction, error=".$error);
- $response["result"] = 1;
- $response["error"] = "Failed to commit DB transaction, error=". $error;
- LockRelease(); return $response;
- }
- $response["sql"] = array ( "rowsChanged" => $rowsChanged);
- LockRelease(); return $response;
- }
- /**
- * Update config properties for a given cluster
- * Updates prop key if it exists or inserts a new entry if not found.
- * @param string $clusterName
- * @param mixed $config
- * array of properties as key-val pairs
- * $config =
- * array (
- * "component" => array (
- * "hostname" =>
- * array ( "prop_key1" => "prop_val1", ... )
- * )
- * )
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "sql" => array ( "rowsChanged" => 1 )
- * )
- */
- public function updateHostRoleConfigs($clusterName, $config) {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "");
- if (count($config) == 0) {
- // no-op
- LockRelease(); return $response;
- }
- $error = "";
- $rowsChanged = 0;
- $ret = $this->dbHandle->beginTransaction();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $response["result"] = 1;
- $response["error"] = "Failed to start DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- foreach ($config as $compName => $hostsInfo) {
- foreach ($hostsInfo as $hostName => $configProps) {
- foreach ($configProps as $key => $val) {
- $query = " INSERT OR REPLACE INTO HostRoleConfig "
- . " ( cluster_name, component_name, host_name"
- . ", key, value ) "
- . " VALUES "
- . " ( " . $this->dbHandle->quote($clusterName) . ", "
- . $this->dbHandle->quote($compName) . ", "
- . $this->dbHandle->quote($hostName) . ", "
- . $this->dbHandle->quote($key) . ", "
- . $this->dbHandle->quote($val) . " ) ";
- $this->logger->log_trace("Running query: $query");
- $ret = $this->dbHandle->exec($query);
- if (FALSE === $ret) {
- $error = $this->getLastDBErrorAsString();
- $ret = $this->dbHandle->rollBack();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $rowsChanged += $ret;
- }
- }
- }
- $ret = $this->dbHandle->commit();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Failed to commit DB transaction, error=".$error);
- $response["result"] = 1;
- $response["error"] = "Failed to commit DB transaction, error=". $error;
- LockRelease(); return $response;
- }
- $response["sql"] = array ( "rowsChanged" => $rowsChanged);
- LockRelease(); return $response;
- }
- /**
- * Get host role configs
- * @param string $clusterName
- * @param string $componentName ( optional )
- */
- public function getHostRoleConfigs($clusterName, $componentName = "") {
- LockAcquire();
- $error = "";
- $query = "SELECT component_name, host_name, key, value FROM HostRoleConfig "
- . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName);
- if ($componentName != "") {
- $query .= " AND component_name = "
- . $this->dbHandle->quote($componentName);
- }
- $response = array ( "result" => 0, "error" => "");
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- $response["clusterName"] = $clusterName;
- $response["properties"] = array();
- foreach ($result as $entry) {
- $compName = $entry["component_name"];
- $hostName = $entry["host_name"];
- $key = $entry["key"];
- $val = $entry["value"];
- if (!isset($response["properties"][$compName])) {
- $response["properties"][$compName] = array();
- }
- if (!isset($response["properties"][$compName][$hostName])) {
- $response["properties"][$compName][$hostName] = array();
- }
- $response["properties"][$compName][$hostName][$key] = $val;
- }
- LockRelease(); return $response;
- }
- /////////////////////////////////////////// Configuration related APIs ////////////////////////////////////////////
- /////////////////////////////////////////// Component related APIs ////////////////////////////////////////////
- /**
- * Get all components for a given service.
- * @param string $serviceName
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "serviceName" => $serviceName,
- * "components" => array (
- * "$componentName" => array (
- * "componentName" =>
- * "displayName" =>
- * "isMaster" =>
- * "isClient" =>
- * "description" =>
- * )
- * )
- * )
- */
- public function getAllServiceComponents($serviceName) {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "");
- $query = "SELECT component_name, display_name, attributes, description"
- . " FROM ServiceComponents WHERE service_name = "
- . $this->dbHandle->quote($serviceName);
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- $response["serviceName"] = $serviceName;
- $response["components"] = array();
- foreach ($result as $entry) {
- $comp = array();
- $comp["componentName"] = $entry["component_name"];
- $comp["displayName"] = $entry["display_name"];
- $comp["description"] = $entry["description"];
- $attributes = json_decode($entry["attributes"], true);
- $comp["isMaster"] = FALSE;
- $comp["isClient"] = FALSE;
- if (isset($attributes["isMaster"])) {
- $comp["isMaster"] = $attributes["isMaster"];
- }
- if (isset($attributes["isClient"])) {
- $comp["isClient"] = $attributes["isClient"];
- }
- $response["components"][$comp["componentName"]] = $comp;
- }
- LockRelease(); return $response;
- }
- /**
- * Add host-role mappings for given hostnames to the specified component
- * @param string $clusterName
- * @param string $componentName
- * @param array $hosts Array of hostnames
- * @param string $state
- * @param string $desiredState
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => ""
- * )
- */
- public function addHostsToComponent($clusterName, $componentName,
- $hosts, $state, $desiredState) {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "");
- if (empty($hosts)) {
- LockRelease(); return $response;
- }
- $error = "";
- $ret = $this->dbHandle->beginTransaction();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $response["result"] = 1;
- $response["error"] = "Failed to start DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- foreach ($hosts as $host) {
- $query = " INSERT OR REPLACE INTO HostRoles "
- . " ( cluster_name, component_name"
- . ", host_name, state, desired_state ) "
- . " VALUES "
- . " ( " . $this->dbHandle->quote($clusterName) . ", "
- . $this->dbHandle->quote($componentName) . ", "
- . $this->dbHandle->quote($host) . ", "
- . $this->dbHandle->quote($state) . ", "
- . $this->dbHandle->quote($desiredState) . " ) ";
- $this->logger->log_trace("Running query: $query");
- $ret = $this->dbHandle->exec($query);
- if (FALSE === $ret) {
- $error = $this->getLastDBErrorAsString();
- $ret = $this->dbHandle->rollBack();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- }
- $ret = $this->dbHandle->commit();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Failed to commit DB transaction, error=".$error);
- $response["result"] = 1;
- $response["error"] = "Failed to commit DB transaction, error=". $error;
- LockRelease(); return $response;
- }
- LockRelease(); return $response;
- }
- /**
- * Get all hosts mapped to any component
- * @param string $clusterName
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "clusterName" => $clusterName,
- * "components" => array (
- * "$componentName" = array (
- * "componentName" => $componentName,
- * "hosts" => array (
- * "$hostName" => array (
- * "hostName" =>
- * "state" =>
- * "desiredState" =>
- * )
- * )
- * )
- * )
- * )
- */
- public function getAllHostsByComponent($clusterName) {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "");
- $error = "";
- $query = "SELECT role_id, component_name, host_name, state, desired_state"
- . " FROM HostRoles"
- . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName);
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- $response["clusterName"] = $clusterName;
- $response["components"] = array();
- foreach ($result as $entry) {
- $componentName = $entry["component_name"];
- if (!isset($response["components"][$componentName])) {
- $response["components"][$componentName] = array();
- $response["components"][$componentName]["componentName"] =
- $componentName;
- $response["components"][$componentName]["hosts"] = array();
- }
- $hostInfo = array();
- $hostInfo["hostName"] = $entry["host_name"];
- $hostInfo["roleId"] = $entry["role_id"];
- $hostInfo["state"] = $entry["state"];
- $hostInfo["desiredState"] = $entry["desired_state"];
- $response["components"][$componentName]["hosts"]
- [$hostInfo["hostName"]] = $hostInfo;
- }
- LockRelease(); return $response;
- }
- /**
- * Get all hosts mapped to all the components
- * @param string $clusterName
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "clusterName" => $clusterName,
- * "hosts" => array (
- * "hostName" =>
- * "components" = array (
- * "componentName", .....
- * )
- * )
- * )
- */
- public function getAllHostsToComponentMap($clusterName) {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "");
- $error = "";
- $query = "SELECT component_name, host_name, state, desired_state "
- . " FROM HostRoles"
- . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName);
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- $response["clusterName"] = $clusterName;
- $response["hosts"] = array();
- foreach ($result as $entry) {
- $hostName = $entry["host_name"];
- if (!isset($response["hosts"][$hostName])) {
- $response["hosts"][$hostName] = array();
- $initComponents = array();
- array_push($initComponents, $entry["component_name"]);
- $response["hosts"][$hostName]["components"] =
- $initComponents;
- } else {
- $components = $response["hosts"][$hostName]["components"];
- array_push($components, $entry["component_name"]);
- $response["hosts"][$hostName]["components"] = $components;
- }
- }
- LockRelease(); return $response;
- }
- /**
- * Get hosts mapped to a given component
- * @param string $clusterName
- * @param string $componentName
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "clusterName" => $clusterName,
- * "componentName" => $componentName,
- * "hosts" => array (
- * "$hostName" => array (
- * "hostName" =>
- * "state" =>
- * "desiredState" =>
- * )
- * )
- * )
- */
- public function getHostsForComponent($clusterName, $componentName) {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "");
- $error = "";
- $query = "SELECT host_name, state, desired_state FROM HostRoles"
- . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
- . " AND component_name = " . $this->dbHandle->quote($componentName);
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- $response["hosts"] = array();
- $response["clusterName"] = $clusterName;
- $response["componentName"] = $componentName;
- foreach ($result as $entry) {
- $hostInfo = array();
- $hostInfo["hostName"] = $entry["host_name"];
- $hostInfo["state"] = $entry["state"];
- $hostInfo["desiredState"] = $entry["desired_state"];
- $response["hosts"][$hostInfo["hostName"]] = $hostInfo;
- }
- LockRelease(); return $response;
- }
- /**
- * Set the state for specified hosts mapped to given component/role
- * @param string $clusterName
- * @param string $componentName
- * @param array $hosts
- * @param string $state
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => ""
- * "sql" => array ( "rowsChanged" => 1 )
- * )
- */
- public function setHostsStateForComponent($clusterName, $componentName,
- $hosts, $state) {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "");
- if (count($hosts) == 0) {
- LockRelease(); return $response;
- }
- $query = " UPDATE HostRoles SET state = " . $this->dbHandle->quote($state)
- . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
- . " AND component_name = " . $this->dbHandle->quote($componentName)
- . " AND ( ";
- $index = 0;
- foreach ($hosts as $host) {
- if ($index != 0) {
- $query .= " OR ";
- }
- $index++;
- $query .= " host_name = " . $this->dbHandle->quote($host);
- }
- $query .= " ) ";
- $this->logger->log_trace("Running query: $query");
- $ret = $this->dbHandle->exec($query);
- if (FALSE === $ret) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $rowsChanged = $ret;
- $response["sql"] = array ( "rowsChanged" => $rowsChanged);
- LockRelease(); return $response;
- }
- /**
- * Set the state for specified hosts mapped to given component/role
- * @param string $clusterName
- * @param string $componentName
- * @param array $hosts
- * @param string $desiredState
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => ""
- * "sql" => array ( "rowsChanged" => 1 )
- * )
- */
- public function setHostsDesiredStateForComponent($clusterName, $componentName,
- $hosts, $desiredState) {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "");
- if (count($hosts) == 0) {
- LockRelease(); return $response;
- }
- $query = " UPDATE HostRoles SET desired_state = " . $this->dbHandle->quote($desiredState)
- . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
- . " AND component_name = " . $this->dbHandle->quote($componentName)
- . " AND ( ";
- $index = 0;
- foreach ($hosts as $host) {
- if ($index != 0) {
- $query .= " OR ";
- }
- $index++;
- $query .= " host_name = " . $this->dbHandle->quote($host);
- }
- $query .= " ) ";
- $this->logger->log_trace("Running query: $query");
- $ret = $this->dbHandle->exec($query);
- if (FALSE === $ret) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $rowsChanged = $ret;
- $response["sql"] = array ( "rowsChanged" => $rowsChanged);
- LockRelease(); return $response;
- }
- /**
- * Get services that the given service depends upon
- * @param string $serviceName
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "serviceName" =>
- * "serviceDependencies" = array ( "svc1", "svc2", ... )
- * )
- */
- public function getServiceDependencies($serviceName) {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "");
- $query = "SELECT to_service_name FROM ServiceDependencies WHERE "
- . " from_service_name = " . $this->dbHandle->quote($serviceName);
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- $response["serviceName"] = $serviceName;
- $response["serviceDependencies"] = array();
- foreach ($result as $entry) {
- array_push($response["serviceDependencies"], $entry["to_service_name"]);
- }
- LockRelease(); return $response;
- }
- /**
- * Get services which depend on the given service.
- * @param string $serviceName
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "serviceName" =>
- * "serviceDependents" = array ( "svc1", "svc2", ... )
- * )
- */
- public function getServiceDependents($serviceName) {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "");
- $query = "SELECT from_service_name FROM ServiceDependencies WHERE "
- . " to_service_name = " . $this->dbHandle->quote($serviceName);
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- $response["serviceName"] = $serviceName;
- $response["serviceDependents"] = array();
- foreach ($result as $entry) {
- array_push($response["serviceDependents"], $entry["from_service_name"]);
- }
- LockRelease(); return $response;
- }
- /**
- * Get components that the given component depends upon
- * @param string $componentName
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "componentName" =>
- * "componentDependencies" = array ( "comp1", "comp2", ... )
- * )
- */
- public function getServiceComponentDependencies($componentName) {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "");
- $query = "SELECT to_component_name "
- . " FROM ServiceComponentDependencies WHERE "
- . " from_component_name = "
- . $this->dbHandle->quote($componentName);
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- $response["componentName"] = $componentName;
- $response["componentDependencies"] = array();
- foreach ($result as $entry) {
- array_push($response["componentDependencies"],
- $entry["to_component_name"]);
- }
- LockRelease(); return $response;
- }
- /**
- * Get components that the given component depends upon
- * @param string $componentName
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "serviceName" =>
- * "componentName" =>
- * "componentDependents" = array ( "comp1", "comp2", ... )
- * )
- */
- public function getServiceComponentDependents($componentName) {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "");
- $query = "SELECT from_component_name "
- . " FROM ServiceComponentDependencies WHERE "
- . " to_component_name = "
- . $this->dbHandle->quote($componentName);
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- $response["componentName"] = $componentName;
- $response["componentDependents"] = array();
- foreach ($result as $entry) {
- array_push($response["componentDependents"],
- $entry["from_component_name"]);
- }
- LockRelease(); return $response;
- }
- /**
- * Add Service Components to a given cluster
- * @param string $clusterName
- * @param mixed $components
- * array (
- * "$serviceName" => array (
- * "$componentName" =>
- * "state" =>
- * "desiredState" =>
- * )
- * )
- * )
- */
- public function addServiceComponentsToCluster($clusterName, $components) {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "",
- "clusterName" => $clusterName);
- $error = "";
- $supportedComps = $this->getAllServiceComponentsList();
- if ($supportedComps === FALSE
- || $supportedComps["result"] != 0) {
- LockRelease(); return $supportedComps;
- }
- // sanitize input
- $sanitizedComps = array();
- foreach ($components as $svc=>$comps) {
- if (!isset($comps)
- || !is_array($comps)
- || count($comps) == 0) {
- $this->logger->log_warn("Invalid args to addServiceComponentsToCluster"
- . ", svc=" . $svc
- . ", comps either null or empty");
- continue;
- }
- if (!isset($supportedComps["services"][$svc])
- || !is_array($supportedComps["services"][$svc])
- || !isset($supportedComps["services"][$svc]["components"])
- || !is_array($supportedComps["services"][$svc]["components"])) {
- $response["error"] = "Invalid service - not supported, svc="
- . $svc;
- $response["result"] = 1;
- LockRelease(); return $response;
- }
- foreach ($comps as $compName=>$comp) {
- if (!isset($supportedComps["services"][$svc]["components"][$compName])
- || !is_array($supportedComps["services"]
- [$svc]["components"][$compName])) {
- $response["error"] = "Invalid component - not supported, svc="
- . $svc . ", component=" . $compName;
- $response["result"] = 1;
- LockRelease(); return $response;
- }
- $saneComp = array(); // yes - it really is sane as in not insane
- $saneComp["serviceName"] = $svc;
- $saneComp["componentName"] = $compName;
- $saneComp["state"] = "";
- $saneComp["desiredState"] = "";
- if (isset($comp["state"])) {
- $saneComp["state"] = $comp["state"];
- }
- if (isset($comp["desiredState"])) {
- $saneComp["desiredState"] = $comp["desiredState"];
- }
- array_push($sanitizedComps, $saneComp);
- }
- }
- $ret = $this->dbHandle->beginTransaction();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $response["result"] = 1;
- $response["error"] = "Failed to start DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- foreach ($sanitizedComps as $comp) {
- $query = "INSERT INTO ServiceComponentInfo "
- . " ( cluster_name, service_name, component_name"
- . ", state, desired_state ) "
- . " VALUES "
- . " ("
- . $this->dbHandle->quote($clusterName) . " , "
- . $this->dbHandle->quote($comp["serviceName"]) . " , "
- . $this->dbHandle->quote($comp["componentName"]) . " , "
- . $this->dbHandle->quote($comp["state"]) . " , "
- . $this->dbHandle->quote($comp["desiredState"])
- . " )";
- $this->logger->log_trace("Running query: $query");
- $ret = $this->dbHandle->exec($query);
- if (FALSE === $ret) {
- $error = $this->getLastDBErrorAsString();
- $this->dbHandle->rollBack();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- }
- $ret = $this->dbHandle->commit();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $response["result"] = 1;
- $response["error"] = "Failed to commit DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- LockRelease(); return $response;
- }
- /**
- * Get all components for all services within the cluster
- * @param string $clusterName
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "services" => array (
- * "$serviceName" => array (
- * "serviceName" => $serviceName,
- * "components" => array (
- * "$componentName" => array (
- * "componentName" =>
- * "state" =>
- * "desiredState" =>
- * )
- * )
- * )
- * )
- */
- public function getAllServiceComponentsInfo($clusterName) {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "");
- $query = "SELECT service_name, component_name, state, desired_state "
- . " FROM ServiceComponentInfo WHERE "
- . " cluster_name = " . $this->dbHandle->quote($clusterName);
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- $response["services"] = array();
- foreach ($result as $entry) {
- $serviceName = $entry["service_name"];
- if (!isset($response["services"]["$serviceName"])) {
- $response["services"][$serviceName] = array();
- $response["services"][$serviceName]["serviceName"] = $serviceName;
- $response["services"][$serviceName]["components"] = array();
- }
- $comp = array();
- $comp["serviceName"] = $serviceName;
- $comp["componentName"] = $entry["component_name"];
- $comp["state"] = $entry["state"];
- $comp["desiredState"] = $entry["desired_state"];
- $response["services"][$serviceName]["components"]
- [$comp["componentName"]] = $comp;
- }
- LockRelease(); return $response;
- }
- /*
- public function getServiceComponentInfo($clusterName, $componentName) {
- LockAcquire();
- // TODO
- // use getAllServiceComponentsInfo for now
- }
- */
- private function genericSetServiceComponentState($clusterName, $componentName,
- $state, $updateAllMappedHosts, $updateDesired) {
- LockAcquire();
- $columnName = "state";
- if ($updateDesired) {
- $columnName = "desired_state";
- }
- $response = array ( "result" => 0, "error" => "");
- $ret = $this->dbHandle->beginTransaction();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $response["result"] = 1;
- $response["error"] = "Failed to start DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- $query = "UPDATE ServiceComponentInfo SET " . $columnName . " = "
- . $this->dbHandle->quote($state)
- . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
- . " AND component_name = " . $this->dbHandle->quote($componentName);
- $this->logger->log_trace("Running query: $query");
- $ret = $this->dbHandle->exec($query);
- if (FALSE === $ret) {
- $error = $this->getLastDBErrorAsString();
- $this->dbHandle->rollBack();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- if ($updateAllMappedHosts) {
- $query = "UPDATE HostRoles SET " . $columnName . " = "
- . $this->dbHandle->quote($state)
- . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
- . " AND component_name = " . $this->dbHandle->quote($componentName);
- $this->logger->log_trace("Running query: $query");
- $ret = $this->dbHandle->exec($query);
- if (FALSE === $ret) {
- $error = $this->getLastDBErrorAsString();
- $this->dbHandle->rollBack();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- }
- $ret = $this->dbHandle->commit();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $response["result"] = 1;
- $response["error"] = "Failed to commit DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- LockRelease(); return $response;
- }
- /**
- * Update the state for a specified component. Based on update hosts flag,
- * set the state for all the hosts mapped to that component too.
- * @param string $clusterName
- * @param string $componentName
- * @param string $state
- * @param bool $updateAllMappedHosts
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => ""
- * )
- */
- public function setServiceComponentState($clusterName, $componentName,
- $state, $updateAllMappedHosts) {
- return $this->genericSetServiceComponentState($clusterName, $componentName,
- $state, $updateAllMappedHosts, FALSE);
- }
- /**
- * Update the desired state for a specified component. Based on update hosts
- * flag, set the desired state for all the hosts mapped to that component too.
- * @param string $clusterName
- * @param string $componentName
- * @param string $desiredState
- * @param bool $updateAllMappedHosts
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => ""
- * )
- */
- public function setServiceComponentDesiredState($clusterName, $componentName,
- $desiredState, $updateAllMappedHosts) {
- return $this->genericSetServiceComponentState($clusterName, $componentName,
- $desiredState, $updateAllMappedHosts, TRUE);
- }
- // TODO add apis for config history
- // TODO add apis for txn status table
- /**
- * Create a new transaction
- * @param string $statusInfo Status blob to attach to txn in DB
- * @param string $pidInfo Pid Info to attach to txn
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "txnId" => $txn_id
- * )
- */
- public function createNewTransaction($clusterName, $statusInfo, $pidInfo) {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "");
- $ret = $this->dbHandle->beginTransaction();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $response["result"] = 1;
- $response["error"] = "Failed to start DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- $query = "INSERT INTO TransactionStatus ( cluster_name, create_time"
- . ", status_info, pid_info ) VALUES ( "
- . $this->dbHandle->quote($clusterName) . " , "
- . $this->dbHandle->quote(time()) . " , "
- . $this->dbHandle->quote($statusInfo) . " , "
- . $this->dbHandle->quote($pidInfo) . " ) ";
- $this->logger->log_trace("Running query: $query");
- $ret = $this->dbHandle->exec($query);
- if (FALSE === $ret) {
- $error = $this->getLastDBErrorAsString();
- $this->dbHandle->rollBack();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $lastInsertId = intval($this->dbHandle->lastInsertId());
- if ($lastInsertId == 0) {
- $this->dbHandle->rollBack();
- $error = "Obtained invalid last insert id from DB";
- $this->logger->log_error($error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $ret = $this->dbHandle->commit();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $response["result"] = 1;
- $response["error"] = "Failed to commit DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- $response["txnId"] = $lastInsertId;
- LockRelease(); return $response;
- }
- /**
- * Get transaction Info
- * @param string $clusterName
- * @param int $txnId
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "clusterName" => $clusterName,
- * "txnId" => $txn_id,
- * "statusInfo" => $statusInfo,
- * "pidInfo" => $pidInfo,
- * "createTime" => $createTime
- * )
- */
- public function getTransactionStatusInfo($clusterName, $txnId) {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "");
- $query = "SELECT create_time, status_info, pid_info "
- . " FROM TransactionStatus WHERE "
- . " cluster_name = " . $this->dbHandle->quote($clusterName)
- . " AND txn_id = " . $this->dbHandle->quote($txnId);
- $response = array ( "result" => 0, "error" => "");
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- $response["clusterName"] = $clusterName;
- $response["txnId"] = $txnId;
- if (isset($result) && is_array($result) && count($result) == 1) {
- $entry = $result[0];
- $response["createTime"] = $entry["create_time"];
- $response["statusInfo"] = $entry["status_info"];
- $response["pidInfo"] = $entry["pid_info"];
- LockRelease(); return $response;
- }
- $response["result"] = 1;
- $response["error"] = "Could not find txn in DB";
- LockRelease(); return $response;
- }
- /**
- * Update pid info for a given transaction
- * @param string $clusterName
- * @param string $txnId
- * @param string $pidInfo
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => ""
- * )
- */
- public function updateTransactionPidInfo($clusterName, $txnId, $pidInfo) {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "");
- $query = "UPDATE TransactionStatus SET pid_info = "
- . $this->dbHandle->quote($pidInfo)
- . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
- . " AND txn_id = " . $this->dbHandle->quote($txnId);
- $this->logger->log_trace("Running query: $query");
- $ret = $this->dbHandle->exec($query);
- if (FALSE === $ret) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $rowsChanged = intval($ret);
- $response["sql"] = array ( "rowsChanged" => $rowsChanged);
- LockRelease(); return $response;
- }
- /**
- * Update status info for a given transaction
- * @param string $clusterName
- * @param string $txnId
- * @param string $statusInfo
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => ""
- * )
- */
- public function updateTransactionStatusInfo($clusterName, $txnId, $statusInfo) {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "");
- $query = "UPDATE TransactionStatus SET status_info = "
- . $this->dbHandle->quote($statusInfo)
- . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
- . " AND txn_id = " . $this->dbHandle->quote($txnId);
- $this->logger->log_trace("Running query: $query");
- $ret = $this->dbHandle->exec($query);
- if (FALSE === $ret) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $rowsChanged = intval($ret);
- $response["sql"] = array ( "rowsChanged" => $rowsChanged);
- LockRelease(); return $response;
- }
- /**
- * Add a new sub-txn
- * @param string $clusterName
- * @param int $txnId
- * @param int $subTxnId
- * @param int $parentSubTxnId
- * @param string $state
- * @param string $description
- * @param string $progress
- * @param string $subTxnType
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => ""
- * )
- */
- public function insertOrUpdateSubTransaction($clusterName, $txnId, $subTxnId,
- $parentSubTxnId, $state, $description, $progress, $subTxnType) {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "");
- $query = "SELECT cluster_name, txn_id, sub_txn_id, op_status "
- . " FROM SubTransactionStatus "
- . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
- . " AND txn_id = " . $this->dbHandle->quote($txnId)
- . " AND sub_txn_id = " . $this->dbHandle->quote($subTxnId);
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $opStatus = json_encode(array());
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- if (is_array($result) && count($result) == 1) {
- $opStatus = $result[0]["op_status"];
- }
- $query = "INSERT OR REPLACE INTO SubTransactionStatus "
- . " ( cluster_name, txn_id, sub_txn_id, parent_sub_txn_id"
- . ", state, description, progress, sub_txn_type, op_status ) VALUES ( "
- . $this->dbHandle->quote($clusterName) . " , "
- . $this->dbHandle->quote($txnId) . " , "
- . $this->dbHandle->quote($subTxnId) . " , "
- . $this->dbHandle->quote($parentSubTxnId) . " , "
- . $this->dbHandle->quote($state) . " , "
- . $this->dbHandle->quote($description) . " , "
- . $this->dbHandle->quote($progress) . " , "
- . $this->dbHandle->quote($subTxnType) . " , "
- . $this->dbHandle->quote($opStatus) . " ) ";
- $this->logger->log_trace("Running query: $query");
- $ret = $this->dbHandle->exec($query);
- if (FALSE === $ret) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $rowsChanged = intval($ret);
- $response["sql"] = array ( "rowsChanged" => $rowsChanged);
- LockRelease(); return $response;
- }
- /**
- * Get all sub transactions
- * Enter description here ...
- * @param string $clusterName
- * @param int $txnId
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "clusterName" =>
- * "txnId" =>
- * "subTxns" => array (
- * $subTxnId => array (
- * "subTxnId" =>
- * "parentSubTxnId" =>
- * "state" =>
- * "description" =>
- * "progress" =>
- * "subTxnType" =>
- * "opStatus" =>
- * )
- * )
- * )
- */
- public function getAllSubTransactionsInfo($clusterName, $txnId) {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "");
- $query = "SELECT sub_txn_id, parent_sub_txn_id, state, description"
- . ", progress, sub_txn_type, op_status FROM SubTransactionStatus "
- . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
- . " AND txn_id = " . $this->dbHandle->quote($txnId);
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- $response["clusterName"] = $clusterName;
- $response["txnId"] = $txnId;
- $response["subTxns"] = array();
- foreach ($result as $entry) {
- $subTxn = array();
- $subTxn["subTxnId"] = $entry["sub_txn_id"];
- $subTxn["parentSubTxnId"] = $entry["parent_sub_txn_id"];
- $subTxn["state"] = $entry["state"];
- $subTxn["description"] = $entry["description"];
- $subTxn["progress"] = $entry["progress"];
- $subTxn["subTxnType"] = $entry["sub_txn_type"];
- $subTxn["opStatus"] = $entry["op_status"];
- $response["subTxns"][$entry["sub_txn_id"]] = $subTxn;
- }
- LockRelease(); return $response;
- }
- /**
- * Update op status for sub txn
- * @param string $clusterName
- * @param int $txnId
- * @param int $subTxnId
- * @param string $progress
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => ""
- * )
- */
- public function updateSubTransactionProgress($clusterName, $txnId,
- $subTxnId, $progress) {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "");
- $query = "UPDATE SubTransactionStatus SET progress = "
- . $this->dbHandle->quote($progress)
- . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
- . " AND txn_id = " . $this->dbHandle->quote($txnId)
- . " AND sub_txn_id = " . $this->dbHandle->quote($subTxnId);
- $this->logger->log_trace("Running query: $query");
- $ret = $this->dbHandle->exec($query);
- if (FALSE === $ret) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $rowsChanged = intval($ret);
- $response["sql"] = array ( "rowsChanged" => $rowsChanged);
- LockRelease(); return $response;
- }
- /**
- * Update op status for sub txn
- * @param string $clusterName
- * @param int $txnId
- * @param int $subTxnId
- * @param string $opStatus
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => ""
- * )
- */
- public function updateSubTransactionOpStatus($clusterName, $txnId,
- $subTxnId, $opStatus) {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "");
- $query = "UPDATE SubTransactionStatus SET op_status = "
- . $this->dbHandle->quote($opStatus)
- . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
- . " AND txn_id = " . $this->dbHandle->quote($txnId)
- . " AND sub_txn_id = " . $this->dbHandle->quote($subTxnId);
- $this->logger->log_trace("Running query: $query");
- $ret = $this->dbHandle->exec($query);
- if (FALSE === $ret) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $rowsChanged = intval($ret);
- $response["sql"] = array ( "rowsChanged" => $rowsChanged);
- LockRelease(); return $response;
- }
- /**
- * Get roles which host belongs to
- * @param string $clusterName
- * @param string $hostName
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "clusterName" =>
- * "hosts" => array (
- * "$hostName" => array (
- * "services" => array (
- * "$serviceName" => array (
- * "components" => array (
- * "$compName1" => array (
- * "state" =>
- * "desiredState" =>
- * ),
- * "$compName2" =>
- * ...
- * )
- * )
- * )
- * )
- * )
- * )
- */
- public function getRolesForHosts($clusterName, $hostNames) {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "");
- if (count($hostNames) == 0) {
- LockRelease(); return $response;
- }
- $query = "SELECT HostRoles.host_name as host_name"
- . ", HostRoles.component_name as component_name"
- . ", ServiceComponentInfo.service_name as service_name"
- . ", ServiceComponentInfo.state as component_state"
- . ", ServiceComponentInfo.desired_state as component_desired_state"
- . " FROM HostRoles LEFT JOIN ServiceComponentInfo "
- . " ON HostRoles.cluster_name = ServiceComponentInfo.cluster_name "
- . " AND HostRoles.component_name = ServiceComponentInfo.component_name "
- . " WHERE HostRoles.cluster_name = " . $this->dbHandle->quote($clusterName)
- . " AND ( ";
- $index = 0;
- foreach ($hostNames as $hostName) {
- if ($index != 0) {
- $query .= " OR ";
- }
- ++$index;
- $query .= " HostRoles.host_name = " . $this->dbHandle->quote($hostName);
- }
- $query .= " ) ";
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- $response["clusterName"] = $clusterName;
- $response["hosts"] = array();
- foreach ($result as $entry) {
- if (!isset($response["hosts"][$entry["host_name"]])) {
- $response["hosts"][$entry["host_name"]] = array("services" => array());
- }
- if (!isset($entry["service_name"])
- || $entry["service_name"] == "") {
- $this->logger->log_warn("Invalid entry in host roles table, "
- . print_r($entry, true));
- continue;
- }
- if (!isset($response["hosts"][$entry["host_name"]]["services"][$entry["service_name"]])) {
- $response["hosts"][$entry["host_name"]]["services"][$entry["service_name"]] =
- array( "components" => array());
- }
- $compInfo = array();
- $compInfo["componentName"] = $entry["component_name"];
- $compInfo["state"] = $entry["component_state"];
- $compInfo["desiredState"] = $entry["component_desired_state"];
- $response["hosts"][$entry["host_name"]]["services"][$entry["service_name"]]
- ["components"][$entry["component_name"]] = $compInfo;
- }
- LockRelease(); return $response;
- }
- /**
- * Dump out the contents of the ServiceDependencies table
- * @return mixed
- * array (
- * "result" =>
- * "error" =>
- * "serviceDependencies" => array (
- * "svcName" => array (
- * "svcDep1",
- * ...
- * ),
- * ...
- * )
- * )
- */
- public function getAllServiceDependencies() {
- LockAcquire();
- $response = array ( "result" => 0, "error" => "");
- $query = "SELECT from_service_name, to_service_name "
- . " FROM ServiceDependencies";
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- $svcDeps = array();
- foreach ($result as $entry) {
- if (!isset($svcDeps[$entry["from_service_name"]])) {
- $svcDeps[$entry["from_service_name"]] = array();
- }
- array_push($svcDeps[$entry["from_service_name"]],
- $entry["to_service_name"]);
- }
- $response["serviceDependencies"] = $svcDeps;
- LockRelease(); return $response;
- }
- /**
- * Get recursive dependencies for a given service.
- * @param mixed $serviceDependencies Full Service Dependencies list as
- * obtained from getAllServiceDependencies()
- * @param string $serviceName Service name to get the dependencies for.
- * @return array of service dependencies.
- */
- public function getRecursiveServiceDependency($serviceDependencies,
- $serviceName) {
- if (!isset($serviceDependencies[$serviceName])) {
- return array();
- }
- $svcDeps = array();
- $bfsQ = array ( $serviceName );
- $seen = array();
- while (count($bfsQ) > 0) {
- $svc = array_shift($bfsQ);
- if (isset($seen[$svc])) {
- continue;
- }
- if ($svc != $serviceName) {
- array_push($svcDeps, $svc);
- }
- $seen[$svc] = TRUE;
- if (!isset($serviceDependencies[$svc])) {
- continue;
- }
- $bfsQ = array_merge($bfsQ, $serviceDependencies[$svc]);
- }
- return $svcDeps;
- }
- /**
- * Get recursive dependents for a given service.
- * @param mixed $serviceDependencies Full Service Dependencies list as
- * obtained from getAllServiceDependencies()
- * @param string $serviceName Service name to get the dependents for.
- * @return array of service dependents.
- */
- public function getRecursiveServiceDependents($serviceDependencies,
- $serviceName) {
- $serviceDependents = array();
- foreach ($serviceDependencies as $fromSvc => $toSvcs) {
- foreach ($toSvcs as $toSvc) {
- if (!isset($serviceDependents[$toSvc])) {
- $serviceDependents[$toSvc] = array();
- }
- array_push($serviceDependents[$toSvc], $fromSvc);
- }
- }
- if (!isset($serviceDependents[$serviceName])) {
- return array();
- }
- $svcDeps = array();
- $bfsQ = array ( $serviceName );
- $seen = array();
- while (count($bfsQ) > 0) {
- $svc = array_shift($bfsQ);
- if (isset($seen[$svc])) {
- continue;
- }
- if ($svc != $serviceName) {
- array_push($svcDeps, $svc);
- }
- $seen[$svc] = TRUE;
- if (!isset($serviceDependents[$svc])) {
- continue;
- }
- $bfsQ = array_merge($bfsQ, $serviceDependents[$svc]);
- }
- return $svcDeps;
- }
- /**
- * Set host state
- * @param string $clusterName
- * @param mixed $hostsToUpdate
- * array (
- * "$svc" => array (
- * "$compName" => array (
- * "host1", "host2" ...
- * )
- * )
- * )
- * @param string $state
- * @param boolean $updateDesired
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "sql" => array ( "rowsChanged" => 1 )
- * )
- */
- private function setGenericHostsState($clusterName, $hostsToUpdate,
- $state, $updateDesired) {
- LockAcquire();
- $columnName = "state";
- if ($updateDesired) {
- $columnName = "desired_state";
- }
- $response = array ( "result" => 0, "error" => "");
- $ret = $this->dbHandle->beginTransaction();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $response["result"] = 1;
- $response["error"] = "Failed to start DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- $rowsChanged = 0;
- foreach ($hostsToUpdate as $svcName => $compList) {
- foreach ($compList as $compName => $hostList) {
- if (count($hostList) == 0) {
- continue;
- }
- $query = "UPDATE HostRoles SET " . $columnName . " = "
- . $this->dbHandle->quote($state) . " WHERE "
- . " cluster_name = " . $this->dbHandle->quote($clusterName)
- . " AND component_name = " . $this->dbHandle->quote($compName)
- . " AND ( ";
- $index = 0;
- foreach ($hostList as $host) {
- if ($index != 0) {
- $query .= " OR ";
- }
- ++$index;
- $query .= " host_name = " . $this->dbHandle->quote($host);
- }
- $query .= " ) ";
- $this->logger->log_trace("Running query: $query");
- $ret = $this->dbHandle->exec($query);
- if (FALSE === $ret) {
- $error = $this->getLastDBErrorAsString();
- $this->dbHandle->rollBack();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $rowsChanged += $ret;
- }
- }
- $ret = $this->dbHandle->commit();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $response["result"] = 1;
- $response["error"] = "Failed to commit DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- $response["sql"] = array ( "rowsChanged" => $rowsChanged);
- LockRelease(); return $response;
- }
- /**
- * Set host state
- * @param string $clusterName
- * @param mixed $hostsToUpdate
- * array (
- * "$svc" => array (
- * "$compName" => array (
- * "host1", "host2" ...
- * )
- * )
- * )
- * @param string $state
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "sql" => array ( "rowsChanged" => 1 )
- * )
- */
- public function setHostsState($clusterName, $hostsToUpdate, $state) {
- return $this->setGenericHostsState($clusterName, $hostsToUpdate, $state,
- FALSE);
- }
- /**
- * Set host state
- * @param string $clusterName
- * @param mixed $hostsToUpdate
- * array (
- * "$svc" => array (
- * "$compName" => array (
- * "host1", "host2" ...
- * )
- * )
- * )
- * @param string $state Desired State
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "sql" => array ( "rowsChanged" => 1 )
- * )
- */
- public function setHostsDesiredState($clusterName, $hostsToUpdate, $state) {
- return $this->setGenericHostsState($clusterName, $hostsToUpdate, $state,
- TRUE);
- }
- /**
- * Set host role state to match components to which it belongs to
- * @param mixed $hosts
- * array (
- * "$svc" => array (
- * "$compName" => array (
- * "host1", "host2" ...
- * )
- * )
- * )
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "sql" => array ( "rowsChanged" => 1 )
- * )
- */
- private function genericMatchHostStateToComponent($clusterName, $hosts,
- $updateDesired) {
- $response = array ("result" => 0, "error" => "");
- if (count($hosts) == 0) {
- return $response;
- }
- $columnName = "state";
- if ($updateDesired) {
- $columnName = "desired_state";
- }
- $compList = array();
- foreach ($hosts as $svc => $compInfo) {
- foreach ($compInfo as $compName => $hostList)
- $compList[$compName] = $hostList;
- }
- if (count($compList) == 0) {
- return $response;
- }
- LockAcquire();
- $query = "SELECT component_name, ". $columnName . " FROM "
- . " ServiceComponentInfo WHERE "
- . " cluster_name = " . $this->dbHandle->quote($clusterName)
- . " AND ( " ;
- $index = 0;
- foreach ($compList as $compName => $foo) {
- if ($index != 0) {
- $query .= " OR ";
- }
- ++$index;
- $query .= " component_name = " . $this->dbHandle->quote($compName);
- }
- $query .= " ) ";
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- $compState = array();
- foreach ($result as $entry) {
- $compState[$entry["component_name"]] = $entry[$columnName];
- }
- $rowsChanged = 0;
- $ret = $this->dbHandle->beginTransaction();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $response["result"] = 1;
- $response["error"] = "Failed to start DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- foreach ($compList as $compName => $hostList) {
- if (count($hostList) == 0) {
- continue;
- }
- if (!isset($compState[$compName])) {
- continue;
- }
- $query = " UPDATE HostRoles SET " . $columnName . " = "
- . $this->dbHandle->quote($compState[$compName])
- . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
- . " AND component_name = " . $this->dbHandle->quote($compName)
- . " AND ( ";
- $index = 0;
- foreach ($hostList as $host) {
- if ($index != 0) {
- $query .= " OR ";
- }
- ++$index;
- $query .= " host_name = " . $this->dbHandle->quote($host);
- }
- $query .= " ) ";
- $this->logger->log_trace("Running query: $query");
- $ret = $this->dbHandle->exec($query);
- if (FALSE === $ret) {
- $error = $this->getLastDBErrorAsString();
- $this->dbHandle->rollBack();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $rowsChanged += $ret;
- }
- $ret = $this->dbHandle->commit();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $response["result"] = 1;
- $response["error"] = "Failed to commit DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- $response["sql"] = array ( "rowsChanged" => $rowsChanged);
- LockRelease(); return $response;
- }
- public function matchHostStateToComponent($clusterName, $hosts) {
- return $this->genericMatchHostStateToComponent($clusterName, $hosts, FALSE);
- }
- public function matchHostDesiredStateToComponent($clusterName, $hosts) {
- return $this->genericMatchHostStateToComponent($clusterName, $hosts, TRUE);
- }
- /**
- * Use the current set of configs in ServiceConfig to create a snapshot
- * in the ConfigHistory table
- * @param string $clusterName
- * @param string $changeLogMsg
- * @return mixed
- * array (
- * "result" => 0,
- * "error" => "",
- * "configSnapshotId" => $snapshotId
- * )
- */
- public function createServiceConfigSnapshot($clusterName, $changeLogMsg) {
- $response = array ("result" => 0, "error" => "");
- $result = $this->getServiceConfig($clusterName);
- if ($result["result"] != 0) {
- return $result;
- }
- $configs = $result["properties"];
- $configBlob = json_encode(
- array ( "clusterName" => $clusterName,
- "properties" => $configs ) );
- $curTime = time();
- LockAcquire();
- $ret = $this->dbHandle->beginTransaction();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $response["result"] = 1;
- $response["error"] = "Failed to start DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- $query = "INSERT INTO ConfigHistory ( cluster_name, config, change_log, "
- . " update_time ) VALUES ( " . $this->dbHandle->quote($clusterName)
- . ", " . $this->dbHandle->quote($configBlob)
- . ", " . $this->dbHandle->quote($changeLogMsg)
- . ", " . $this->dbHandle->quote($curTime) . " ) ";
- $this->logger->log_trace("Running query: $query");
- $ret = $this->dbHandle->exec($query);
- if (FALSE === $ret) {
- $error = $this->getLastDBErrorAsString();
- $this->dbHandle->rollBack();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $lastInsertId = intval($this->dbHandle->lastInsertId());
- if ($lastInsertId == 0) {
- $this->dbHandle->rollBack();
- $error = "Obtained invalid last insert id from DB";
- $this->logger->log_error($error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $ret = $this->dbHandle->commit();
- if (!$ret) {
- $error = $this->getLastDBErrorAsString();
- $response["result"] = 1;
- $response["error"] = "Failed to commit DB transaction, error=".$error;
- LockRelease(); return $response;
- }
- $response["configSnapshotId"] = $lastInsertId;
- LockRelease(); return $response;
- }
- public function getConfigSnapshot($clusterName, $snapshotId) {
- LockAcquire();
- $response = array ("result" => 0, "error" => "");
- $query = "SELECT config, change_log, update_time FROM ConfigHistory"
- . " WHERE cluster_name = " . $this->dbHandle->quote($clusterName)
- . " AND version = " . $this->dbHandle->quote($snapshotId);
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt === FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
- $response["clusterName"] = $clusterName;
- $response["configSnapshotId"] = $snapshotId;
- if (isset($result) && is_array($result) && count($result) == 1) {
- $response["changeLog"] = $result[0]["change_log"];
- $response["config"] = json_decode($result[0]["config"], true);
- $response["updateTime"] = $result[0]["update_time"];
- LockRelease(); return $response;
- }
- $response["result"] = 1;
- $response["error"] = "Could not find config snapshot in DB";
- LockRelease(); return $response;
- }
- private function deleteClusterTable ($clusterName, $table) {
- LockAcquire();
- $response = array ("result" => 0, "error" => "");
- $query = "DELETE from " . $table . " WHERE cluster_name = "
- . $this->dbHandle->quote($clusterName);
- $this->logger->log_trace("Running query: $query");
- $pdoStmt = $this->dbHandle->query($query);
- if ($pdoStmt == FALSE) {
- $error = $this->getLastDBErrorAsString();
- $this->logger->log_error("Error when executing query"
- . ", query=".$query
- . ", error=".$error);
- $response["result"] = 1;
- $response["error"] = $error;
- LockRelease(); return $response;
- }
- LockRelease(); return $response;
- }
- public function cleanupServices($clusterName) {
- $this->deleteClusterTable($clusterName, "ServiceConfig");
- $this->deleteClusterTable($clusterName, "ServiceInfo");
- }
- public function cleanupHosts ($clusterName) {
- $this->deleteClusterTable($clusterName, "Hosts");
- $this->deleteClusterTable($clusterName, "HostRoles");
- $this->cleanupServices($clusterName);
- }
- public function cleanupCluster ($clusterName) {
- $this->deleteClusterTable($clusterName, "Clusters");
- $this->deleteClusterTable($clusterName, "ServiceComponentInfo");
- $this->cleanupHosts($clusterName);
- }
- }
- ?>
|