OrderedStreamingForkController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * Reads lines of work from an input stream and farms them out to multiple
  4. * child streams. Each child has exactly one piece of work in flight at a given
  5. * moment. Writes the result of child's work to an output stream. If numProcs
  6. * <= zero the work will be performed in process.
  7. *
  8. * This class amends ForkController with the requirement that the output is
  9. * produced in the same exact order as input values were.
  10. *
  11. * Currently used by CirrusSearch extension to implement CLI search script.
  12. *
  13. * @ingroup Maintenance
  14. * @since 1.30
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License along
  27. * with this program; if not, write to the Free Software Foundation, Inc.,
  28. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  29. * http://www.gnu.org/copyleft/gpl.html
  30. */
  31. class OrderedStreamingForkController extends ForkController {
  32. /** @var callable */
  33. protected $workCallback;
  34. /** @var resource */
  35. protected $input;
  36. /** @var resource */
  37. protected $output;
  38. /** @var int */
  39. protected $nextOutputId;
  40. /** @var string[] Int key indicates order, value is data */
  41. protected $delayedOutputData = [];
  42. /**
  43. * @param int $numProcs The number of worker processes to fork
  44. * @param callable $workCallback A callback to call in the child process
  45. * once for each line of work to process.
  46. * @param resource $input A socket to read work lines from
  47. * @param resource $output A socket to write the result of work to.
  48. */
  49. public function __construct( $numProcs, $workCallback, $input, $output ) {
  50. parent::__construct( $numProcs );
  51. $this->workCallback = $workCallback;
  52. $this->input = $input;
  53. $this->output = $output;
  54. }
  55. /**
  56. * @inheritDoc
  57. */
  58. public function start() {
  59. if ( $this->procsToStart > 0 ) {
  60. $status = parent::start();
  61. if ( $status === 'child' ) {
  62. $this->consume();
  63. }
  64. } else {
  65. $status = 'parent';
  66. $this->consumeNoFork();
  67. }
  68. return $status;
  69. }
  70. /**
  71. * @param int $numProcs
  72. * @return string
  73. */
  74. protected function forkWorkers( $numProcs ) {
  75. $this->prepareEnvironment();
  76. $childSockets = [];
  77. // Create the child processes
  78. for ( $i = 0; $i < $numProcs; $i++ ) {
  79. $sockets = stream_socket_pair( STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP );
  80. // Do the fork
  81. $pid = pcntl_fork();
  82. if ( $pid === -1 || $pid === false ) {
  83. echo "Error creating child processes\n";
  84. exit( 1 );
  85. }
  86. if ( !$pid ) {
  87. $this->initChild();
  88. $this->childNumber = $i;
  89. $this->input = $sockets[0];
  90. $this->output = $sockets[0];
  91. fclose( $sockets[1] );
  92. return 'child';
  93. } else {
  94. // This is the parent process
  95. $this->children[$pid] = true;
  96. fclose( $sockets[0] );
  97. $childSockets[] = $sockets[1];
  98. }
  99. }
  100. $this->feedChildren( $childSockets );
  101. foreach ( $childSockets as $socket ) {
  102. // if a child has already shutdown the sockets will be closed,
  103. // closing a second time would raise a warning.
  104. if ( is_resource( $socket ) ) {
  105. fclose( $socket );
  106. }
  107. }
  108. return 'parent';
  109. }
  110. /**
  111. * Child worker process. Reads work from $this->input and writes the
  112. * result of that work to $this->output when completed.
  113. */
  114. protected function consume() {
  115. while ( !feof( $this->input ) ) {
  116. $line = trim( fgets( $this->input ) );
  117. if ( $line ) {
  118. list( $id, $data ) = json_decode( $line );
  119. $result = call_user_func( $this->workCallback, $data );
  120. fwrite( $this->output, json_encode( [ $id, $result ] ) . "\n" );
  121. }
  122. }
  123. }
  124. /**
  125. * Special cased version of self::consume() when no forking occurs
  126. */
  127. protected function consumeNoFork() {
  128. while ( !feof( $this->input ) ) {
  129. $line = trim( fgets( $this->input ) );
  130. if ( $line ) {
  131. $result = call_user_func( $this->workCallback, $line );
  132. fwrite( $this->output, "$result\n" );
  133. }
  134. }
  135. }
  136. /**
  137. * Reads lines of work from $this->input and farms them out to
  138. * the provided socket.
  139. *
  140. * @param resource[] $sockets
  141. */
  142. protected function feedChildren( array $sockets ) {
  143. $used = [];
  144. $id = 0;
  145. $this->nextOutputId = 0;
  146. while ( !feof( $this->input ) ) {
  147. $data = fgets( $this->input );
  148. if ( $used ) {
  149. do {
  150. $this->updateAvailableSockets( $sockets, $used, $sockets ? 0 : 5 );
  151. } while ( !$sockets );
  152. }
  153. $data = trim( $data );
  154. if ( !$data ) {
  155. continue;
  156. }
  157. $socket = array_pop( $sockets );
  158. fwrite( $socket, json_encode( [ $id++, $data ] ) . "\n" );
  159. $used[] = $socket;
  160. }
  161. while ( $used ) {
  162. $this->updateAvailableSockets( $sockets, $used, 5 );
  163. }
  164. }
  165. /**
  166. * Moves sockets from $used to $sockets when they are available
  167. * for more work
  168. *
  169. * @param resource[] &$sockets List of sockets that are waiting for work
  170. * @param resource[] &$used List of sockets currently performing work
  171. * @param int $timeout The number of seconds to block waiting. 0 for
  172. * non-blocking operation.
  173. */
  174. protected function updateAvailableSockets( &$sockets, &$used, $timeout ) {
  175. $read = $used;
  176. $write = $except = [];
  177. stream_select( $read, $write, $except, $timeout );
  178. foreach ( $read as $socket ) {
  179. $line = fgets( $socket );
  180. list( $id, $data ) = json_decode( trim( $line ) );
  181. $this->receive( (int)$id, $data );
  182. $sockets[] = $socket;
  183. $idx = array_search( $socket, $used );
  184. unset( $used[$idx] );
  185. }
  186. }
  187. /**
  188. * @param int $id
  189. * @param string $data
  190. */
  191. protected function receive( $id, $data ) {
  192. if ( $id !== $this->nextOutputId ) {
  193. $this->delayedOutputData[$id] = $data;
  194. return;
  195. }
  196. fwrite( $this->output, $data . "\n" );
  197. $this->nextOutputId = $id + 1;
  198. while ( isset( $this->delayedOutputData[$this->nextOutputId] ) ) {
  199. fwrite( $this->output, $this->delayedOutputData[$this->nextOutputId] . "\n" );
  200. unset( $this->delayedOutputData[$this->nextOutputId] );
  201. $this->nextOutputId++;
  202. }
  203. }
  204. }