From: jenkins-bot Date: Thu, 7 Sep 2017 18:27:49 +0000 (+0000) Subject: Merge "Move OrderedStreamingForkController class from CirrusSearch to core." X-Git-Tag: 1.31.0-rc.0~2187 X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=commitdiff_plain;h=c66360d167aeacec8ec296ae8ff29fd5d3f1a557;hp=f0bef2cf6994c49031d89425b3b3c56fd0e89a65 Merge "Move OrderedStreamingForkController class from CirrusSearch to core." --- diff --git a/autoload.php b/autoload.php index c7f13d5757..5eba00b16f 100644 --- a/autoload.php +++ b/autoload.php @@ -1043,6 +1043,7 @@ $wgAutoloadLocalClasses = [ 'OldLocalFile' => __DIR__ . '/includes/filerepo/file/OldLocalFile.php', 'OracleInstaller' => __DIR__ . '/includes/installer/OracleInstaller.php', 'OracleUpdater' => __DIR__ . '/includes/installer/OracleUpdater.php', + 'OrderedStreamingForkController' => __DIR__ . '/includes/OrderedStreamingForkController.php', 'OrphanStats' => __DIR__ . '/maintenance/storage/orphanStats.php', 'Orphans' => __DIR__ . '/maintenance/orphans.php', 'OutputPage' => __DIR__ . '/includes/OutputPage.php', diff --git a/includes/OrderedStreamingForkController.php b/includes/OrderedStreamingForkController.php new file mode 100644 index 0000000000..1436c1c960 --- /dev/null +++ b/includes/OrderedStreamingForkController.php @@ -0,0 +1,216 @@ +workCallback = $workCallback; + $this->input = $input; + $this->output = $output; + } + + /** + * {@inheritDoc} + */ + public function start() { + if ( $this->procsToStart > 0 ) { + $status = parent::start(); + if ( $status === 'child' ) { + $this->consume(); + } + } else { + $status = 'parent'; + $this->consumeNoFork(); + } + return $status; + } + + /** + * @param int $numProcs + * @return string + */ + protected function forkWorkers( $numProcs ) { + $this->prepareEnvironment(); + + $childSockets = []; + // Create the child processes + for ( $i = 0; $i < $numProcs; $i++ ) { + $sockets = stream_socket_pair( STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP ); + // Do the fork + $pid = pcntl_fork(); + if ( $pid === -1 || $pid === false ) { + echo "Error creating child processes\n"; + exit( 1 ); + } + + if ( !$pid ) { + $this->initChild(); + $this->childNumber = $i; + $this->input = $sockets[0]; + $this->output = $sockets[0]; + fclose( $sockets[1] ); + return 'child'; + } else { + // This is the parent process + $this->children[$pid] = true; + fclose( $sockets[0] ); + $childSockets[] = $sockets[1]; + } + } + $this->feedChildren( $childSockets ); + foreach ( $childSockets as $socket ) { + // if a child has already shutdown the sockets will be closed, + // closing a second time would raise a warning. + if ( is_resource( $socket ) ) { + fclose( $socket ); + } + } + return 'parent'; + } + + /** + * Child worker process. Reads work from $this->input and writes the + * result of that work to $this->output when completed. + */ + protected function consume() { + while ( !feof( $this->input ) ) { + $line = trim( fgets( $this->input ) ); + if ( $line ) { + list( $id, $data ) = json_decode( $line ); + $result = call_user_func( $this->workCallback, $data ); + fwrite( $this->output, json_encode( [ $id, $result ] ) . "\n" ); + } + } + } + + /** + * Special cased version of self::consume() when no forking occurs + */ + protected function consumeNoFork() { + while ( !feof( $this->input ) ) { + $line = trim( fgets( $this->input ) ); + if ( $line ) { + $result = call_user_func( $this->workCallback, $line ); + fwrite( $this->output, "$result\n" ); + } + } + } + + /** + * Reads lines of work from $this->input and farms them out to + * the provided socket. + * + * @param resource[] $sockets + */ + protected function feedChildren( array $sockets ) { + $used = []; + $id = 0; + $this->nextOutputId = 0; + + while ( !feof( $this->input ) ) { + $data = fgets( $this->input ); + if ( $used ) { + do { + $this->updateAvailableSockets( $sockets, $used, $sockets ? 0 : 5 ); + } while ( !$sockets ); + } + $data = trim( $data ); + if ( !$data ) { + continue; + } + $socket = array_pop( $sockets ); + fwrite( $socket, json_encode( [ $id++, $data ] ) . "\n" ); + $used[] = $socket; + } + while ( $used ) { + $this->updateAvailableSockets( $sockets, $used, 5 ); + } + } + + /** + * Moves sockets from $used to $sockets when they are available + * for more work + * + * @param resource[] &$sockets List of sockets that are waiting for work + * @param resource[] &$used List of sockets currently performing work + * @param int $timeout The number of seconds to block waiting. 0 for + * non-blocking operation. + */ + protected function updateAvailableSockets( &$sockets, &$used, $timeout ) { + $read = $used; + $write = $except = []; + stream_select( $read, $write, $except, $timeout ); + foreach ( $read as $socket ) { + $line = fgets( $socket ); + list( $id, $data ) = json_decode( trim( $line ) ); + $this->receive( (int)$id, $data ); + $sockets[] = $socket; + $idx = array_search( $socket, $used ); + unset( $used[$idx] ); + } + } + + /** + * @param int $id + * @param string $data + */ + protected function receive( $id, $data ) { + if ( $id !== $this->nextOutputId ) { + $this->delayedOutputData[$id] = $data; + return; + } + fwrite( $this->output, $data . "\n" ); + $this->nextOutputId = $id + 1; + while ( isset( $this->delayedOutputData[$this->nextOutputId] ) ) { + fwrite( $this->output, $this->delayedOutputData[$this->nextOutputId] . "\n" ); + unset( $this->delayedOutputData[$this->nextOutputId] ); + $this->nextOutputId++; + } + } +}