| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Maintenance; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class OrderedStreamingForkController extends ForkController { |
| 43 |
|
| 44 |
protected $workCallback; |
| 45 |
|
| 46 |
protected $input; |
| 47 |
|
| 48 |
protected $output; |
| 49 |
|
| 50 |
protected $nextOutputId; |
| 51 |
|
| 52 |
protected $delayedOutputData = []; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function __construct( $numProcs, $workCallback, $input, $output ) { |
| 62 |
parent::__construct( $numProcs ); |
| 63 |
$this->workCallback = $workCallback; |
| 64 |
$this->input = $input; |
| 65 |
$this->output = $output; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function start() { |
| 72 |
if ( $this->procsToStart > 0 ) { |
| 73 |
$status = parent::start(); |
| 74 |
if ( $status === 'child' ) { |
| 75 |
$this->consume(); |
| 76 |
} |
| 77 |
} else { |
| 78 |
$status = 'parent'; |
| 79 |
$this->consumeNoFork(); |
| 80 |
} |
| 81 |
return $status; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
protected function forkWorkers( $numProcs ) { |
| 89 |
$this->prepareEnvironment(); |
| 90 |
|
| 91 |
$childSockets = []; |
| 92 |
|
| 93 |
for ( $i = 0; $i < $numProcs; $i++ ) { |
| 94 |
$sockets = stream_socket_pair( STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP ); |
| 95 |
|
| 96 |
$pid = pcntl_fork(); |
| 97 |
if ( $pid === -1 ) { |
| 98 |
echo "Error creating child processes\n"; |
| 99 |
exit( 1 ); |
| 100 |
} |
| 101 |
|
| 102 |
if ( !$pid ) { |
| 103 |
$this->initChild(); |
| 104 |
$this->childNumber = $i; |
| 105 |
$this->input = $sockets[0]; |
| 106 |
$this->output = $sockets[0]; |
| 107 |
fclose( $sockets[1] ); |
| 108 |
return 'child'; |
| 109 |
} else { |
| 110 |
|
| 111 |
$this->children[$pid] = true; |
| 112 |
fclose( $sockets[0] ); |
| 113 |
$childSockets[] = $sockets[1]; |
| 114 |
} |
| 115 |
} |
| 116 |
$this->feedChildren( $childSockets ); |
| 117 |
foreach ( $childSockets as $socket ) { |
| 118 |
fclose( $socket ); |
| 119 |
} |
| 120 |
return 'parent'; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
protected function consume() { |
| 128 |
while ( !feof( $this->input ) ) { |
| 129 |
$line = trim( fgets( $this->input ) ); |
| 130 |
if ( $line ) { |
| 131 |
[ $id, $data ] = json_decode( $line ); |
| 132 |
$result = call_user_func( $this->workCallback, $data ); |
| 133 |
fwrite( $this->output, json_encode( [ $id, $result ] ) . "\n" ); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
protected function consumeNoFork() { |
| 142 |
while ( !feof( $this->input ) ) { |
| 143 |
$data = fgets( $this->input ); |
| 144 |
if ( substr( $data, -1 ) === "\n" ) { |
| 145 |
|
| 146 |
|
| 147 |
$data = substr( $data, 0, -1 ); |
| 148 |
} |
| 149 |
if ( $data === '' ) { |
| 150 |
continue; |
| 151 |
} |
| 152 |
$result = call_user_func( $this->workCallback, $data ); |
| 153 |
fwrite( $this->output, "$result\n" ); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
protected function feedChildren( array $sockets ) { |
| 164 |
$used = []; |
| 165 |
$id = 0; |
| 166 |
$this->nextOutputId = 0; |
| 167 |
|
| 168 |
while ( !feof( $this->input ) ) { |
| 169 |
$data = fgets( $this->input ); |
| 170 |
if ( $used ) { |
| 171 |
do { |
| 172 |
$this->updateAvailableSockets( $sockets, $used, $sockets ? 0 : 5 ); |
| 173 |
} while ( !$sockets ); |
| 174 |
} |
| 175 |
if ( substr( $data, -1 ) === "\n" ) { |
| 176 |
|
| 177 |
|
| 178 |
$data = substr( $data, 0, -1 ); |
| 179 |
} |
| 180 |
if ( $data === '' ) { |
| 181 |
continue; |
| 182 |
} |
| 183 |
$socket = array_pop( $sockets ); |
| 184 |
fwrite( $socket, json_encode( [ $id++, $data ] ) . "\n" ); |
| 185 |
$used[] = $socket; |
| 186 |
} |
| 187 |
while ( $used ) { |
| 188 |
$this->updateAvailableSockets( $sockets, $used, 5 ); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
protected function updateAvailableSockets( &$sockets, &$used, $timeout ) { |
| 202 |
$read = $used; |
| 203 |
$write = $except = []; |
| 204 |
stream_select( $read, $write, $except, $timeout ); |
| 205 |
foreach ( $read as $socket ) { |
| 206 |
$line = fgets( $socket ); |
| 207 |
[ $id, $data ] = json_decode( trim( $line ) ); |
| 208 |
$this->receive( (int)$id, $data ); |
| 209 |
$sockets[] = $socket; |
| 210 |
$idx = array_search( $socket, $used ); |
| 211 |
unset( $used[$idx] ); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
protected function receive( $id, $data ) { |
| 220 |
if ( $id !== $this->nextOutputId ) { |
| 221 |
$this->delayedOutputData[$id] = $data; |
| 222 |
return; |
| 223 |
} |
| 224 |
fwrite( $this->output, $data . "\n" ); |
| 225 |
$this->nextOutputId = $id + 1; |
| 226 |
while ( isset( $this->delayedOutputData[$this->nextOutputId] ) ) { |
| 227 |
fwrite( $this->output, $this->delayedOutputData[$this->nextOutputId] . "\n" ); |
| 228 |
unset( $this->delayedOutputData[$this->nextOutputId] ); |
| 229 |
$this->nextOutputId++; |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
class_alias( OrderedStreamingForkController::class, 'OrderedStreamingForkController' ); |