fetchTxnProgress.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. /*
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. *
  21. */
  22. include_once '../util/Logger.php';
  23. include_once "../conf/Config.inc";
  24. include_once "../orchestrator/HMC.php";
  25. include_once '../util/clusterState.php';
  26. include_once "uninstallCleanup.php";
  27. include_once "deployPostProcess.php";
  28. include_once "uninstallCleanup.php";
  29. include_once "restoreDeployedStatePostProcess.php";
  30. $dbPath = $GLOBALS["DB_PATH"];
  31. $clusterName = $_GET['clusterName'];
  32. $txnId = $_GET['txnId'];
  33. $deployUser = $_GET['deployUser'];
  34. $logger = new HMCLogger("TxnProgress");
  35. $map = array(
  36. "HMC::deployHDP" => array (
  37. "deployPostProcess"
  38. ),
  39. "HMC::deployNodes" => array (
  40. "restoreDeployedStatePostProcess"
  41. ),
  42. "HMC::uninstallHDP" => array (
  43. "deBootStrap"
  44. ),
  45. "HMC::startAllServices" => array (
  46. "restoreDeployedStatePostProcess"
  47. ),
  48. "HMC::stopAllServices" => array (
  49. "restoreDeployedStatePostProcess"
  50. ),
  51. "HMC::startServices" => array (
  52. "restoreDeployedStatePostProcess"
  53. ),
  54. "HMC::stopServices" => array (
  55. "restoreDeployedStatePostProcess"
  56. ),
  57. "HMC::reconfigureServices" => array (
  58. "restoreDeployedStatePostProcess"
  59. )
  60. );
  61. $dbAccessor = new HMCDBAccessor($GLOBALS["DB_PATH"]);
  62. function fetchTxnProgress( $txnId )
  63. {
  64. global $dbPath;
  65. global $clusterName;
  66. $hmc = new HMC($dbPath, $clusterName);
  67. $progress = $hmc->getProgress($txnId);
  68. return $progress;
  69. }
  70. function sortProgressStatesByRank( $first, $second )
  71. {
  72. if( $first['rank'] == $second['rank'] )
  73. {
  74. return 0;
  75. }
  76. return ($first['rank'] < $second['rank']) ? -1 : 1;
  77. }
  78. $progress = fetchTxnProgress($txnId);
  79. // TODO XXX Check for $progress['result'] and $progress['error'] here, before proceeding.
  80. /* Tack on some additional state to make life on the frontend easier. */
  81. $progress['encounteredError'] = false;
  82. /* Marker to keep track of whether at least one subTxn has been kicked off. */
  83. $atLeastOneSubTxnInProgress = false;
  84. /* Sort the subTxns array inside $progress by rank, and then remove all notion
  85. * of rank from the sorted array we're going to return.
  86. */
  87. usort( $progress['subTxns'], 'sortProgressStatesByRank' );
  88. foreach( $progress['subTxns'] as &$progressSubTxn )
  89. {
  90. unset( $progressSubTxn['rank'] );
  91. /* Any one subTxn failing means we want the frontend to bail. */
  92. if( $progressSubTxn['progress'] == 'FAILED' )
  93. {
  94. $progress['encounteredError'] = true;
  95. }
  96. /* We need to make sure at least one subTxn is not pending before
  97. * sending a progress report back to the frontend - if not, the
  98. * progress states aren't yet finalized and will change across
  99. * invocations to this webservice, so we prefer to wait before
  100. * showing anything.
  101. */
  102. if( $progressSubTxn['progress'] != 'PENDING' )
  103. {
  104. $atLeastOneSubTxnInProgress = true;
  105. }
  106. }
  107. $lastTxnIndex = -1;
  108. if ($atLeastOneSubTxnInProgress) {
  109. $lastTxnIndex = count($progress['subTxns']) - 1;
  110. }
  111. /* If at least one subTxn isn't in progress, signal to the frontend that
  112. * there's nothing worthy for it to process yet.
  113. */
  114. if (!$atLeastOneSubTxnInProgress) {
  115. $progress['subTxns'] = null;
  116. }
  117. LockAcquire(HMC_CLUSTER_STATE_LOCK_FILE_SUFFIX);
  118. $doPostProcess = TRUE;
  119. $clusterStateResponse = $dbAccessor->getClusterState($clusterName);
  120. $logger->log_debug("Got cluster state: ".json_encode($clusterStateResponse));
  121. if ($clusterStateResponse['result'] != 0) {
  122. print json_encode($clusterStateResponse);
  123. LockRelease(HMC_CLUSTER_STATE_LOCK_FILE_SUFFIX); return;
  124. }
  125. // if state is not set, should not proceed to post process.
  126. // setting post process to false allows returning appropriate txn data
  127. // without post processing.
  128. $clusterState = null;
  129. if (!isset($clusterStateResponse['state'])) {
  130. $doPostProcess = FALSE;
  131. // create an empty cluster state
  132. $clusterState = array();
  133. } else {
  134. $clusterState = json_decode($clusterStateResponse['state'], true);
  135. $logger->log_debug("Current cluster state, " . print_r($clusterState, true));
  136. }
  137. /* check for matching txn id. if present check if it is same
  138. * if not return with all data for txn id requested for and
  139. * do nothing for post process
  140. * setting post process to false allows returning appropriate txn data
  141. * without post processing.
  142. */
  143. if (array_key_exists('context', $clusterState)) {
  144. $clusterContext = $clusterState['context'];
  145. if (!array_key_exists('txnId', $clusterContext) || !isset($clusterContext['txnId']) || ($clusterContext["txnId"] != $txnId)) {
  146. $logger->log_debug("TxnId does not exist ".
  147. array_key_exists("txnId", $clusterContext) .
  148. " or not set " .!isset($clusterContext["txnId"]) ." or does not match " .
  149. ($clusterContext["txnId"] != $txnId));
  150. $doPostProcess = FALSE;
  151. }
  152. }
  153. if ($progress['processRunning'] == FALSE) {
  154. $logger->log_trace("Checking cluster state for post process state");
  155. $context = $clusterState['context'];
  156. if (isset($context['isInPostProcess'])) {
  157. $doPostProcess = FALSE;
  158. $logger->log_trace("Post process already done before in another call");
  159. if ($context['isInPostProcess'] == TRUE) {
  160. $logger->log_trace("Post process still in progress in another call");
  161. $progress['processRunning'] = TRUE;
  162. } else {
  163. $logger->log_trace("Post process completed in another call");
  164. $progress['processRunning'] = FALSE;
  165. if (!isset($context['postProcessSuccessful'])
  166. || $context['postProcessSuccessful'] == FALSE) {
  167. $progress['encounteredError'] = TRUE;
  168. if ($lastTxnIndex >= 0) {
  169. $progress['subTxns'][$lastTxnIndex]["progress"] = "FAILED";
  170. }
  171. }
  172. }
  173. }
  174. }
  175. if ((($progress['processRunning'] == FALSE) || ($progress['encounteredError'] == TRUE))
  176. && $doPostProcess) {
  177. // get the transaction status info from db
  178. $retval = $dbAccessor->getTransactionStatusInfo($clusterName, $txnId);
  179. if ($retval["result"] != 0) {
  180. $progress['encounteredError'] = TRUE;
  181. if ($lastTxnIndex >= 0) {
  182. $progress['subTxns'][$lastTxnIndex]["progress"] = "FAILED";
  183. }
  184. } else {
  185. if (isset($retval['statusInfo'])) {
  186. $statusInfo = json_decode($retval['statusInfo'], true);
  187. $logger->log_debug("Status info function ".$statusInfo['function']);
  188. $logger->log_debug("Running post process functions");
  189. // run the next script from the map
  190. // supports multiple post process functions
  191. foreach ($map[$statusInfo['function']] as $postProcessFunc) {
  192. $logger->log_debug("Post process function is ".$postProcessFunc);
  193. // setting cluster state to denote in post process
  194. $clusterState['context']['isInPostProcess'] = TRUE;
  195. $logger->log_trace("Starting post process function");
  196. updateClusterState($clusterName, $clusterState['state'],
  197. $clusterState['displayName'], $clusterState['context']);
  198. LockRelease(HMC_CLUSTER_STATE_LOCK_FILE_SUFFIX);
  199. $retval = $postProcessFunc($clusterName, $deployUser, $txnId, $progress);
  200. LockAcquire(HMC_CLUSTER_STATE_LOCK_FILE_SUFFIX);
  201. // setting cluster state to denote post process completed
  202. $logger->log_trace("Finished post process function");
  203. $clusterStateResponse = $dbAccessor->getClusterState($clusterName);
  204. if ($clusterStateResponse['result'] != 0) {
  205. print json_encode($clusterStateResponse);
  206. LockRelease(HMC_CLUSTER_STATE_LOCK_FILE_SUFFIX); return;
  207. }
  208. $clusterState = json_decode($clusterStateResponse['state'], true);
  209. $clusterState['context']['isInPostProcess'] = FALSE;
  210. $clusterState['context']['postProcessSuccessful'] = ($retval["result"] == 0);
  211. updateClusterState($clusterName, $clusterState['state'],
  212. $clusterState['displayName'], $clusterState['context']);
  213. $clusterStateResponse = $dbAccessor->getClusterState($clusterName);
  214. $logger->log_trace("STATE AFTER UPDATE: ".json_encode($clusterStateResponse));
  215. if ($retval["result"] != 0) {
  216. $progress['encounteredError'] = TRUE;
  217. if ($lastTxnIndex >= 0) {
  218. $progress['subTxns'][$lastTxnIndex]["progress"] = "FAILED";
  219. }
  220. // if the post process failed stop from calling
  221. // further post process functions.
  222. break;
  223. }
  224. }
  225. }
  226. }
  227. }
  228. LockRelease(HMC_CLUSTER_STATE_LOCK_FILE_SUFFIX);
  229. /* Clean up some more remnants that we don't need on the frontend. */
  230. unset( $progress['result'] );
  231. unset( $progress['error'] );
  232. /* Create the output data... */
  233. $jsonOutput = array(
  234. 'clusterName' => $clusterName,
  235. 'txnId' => $txnId,
  236. 'progress' => $progress );
  237. if ($deployUser != null) {
  238. $jsonOutput['deployUser'] = $deployUser;
  239. }
  240. /* ...and spit it out. */
  241. header("Content-type: application/json");
  242. print (json_encode($jsonOutput));
  243. ?>