2013-07-16 20:21:33 -04:00
|
|
|
#
|
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
# contributor license agreements. See the NOTICE file distributed with
|
|
|
|
# this work for additional information regarding copyright ownership.
|
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
# (the "License"); you may not use this file except in compliance with
|
|
|
|
# the License. You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
|
2014-08-01 22:38:21 -04:00
|
|
|
import numbers
|
2013-05-06 19:34:30 -04:00
|
|
|
import os
|
2013-07-01 02:20:14 -04:00
|
|
|
import signal
|
2014-08-01 22:38:21 -04:00
|
|
|
import select
|
2013-07-01 02:20:14 -04:00
|
|
|
import socket
|
2013-05-06 19:34:30 -04:00
|
|
|
import sys
|
2013-07-01 02:20:14 -04:00
|
|
|
import traceback
|
2014-08-10 16:00:38 -04:00
|
|
|
import time
|
|
|
|
from errno import EINTR, ECHILD, EAGAIN
|
2013-07-01 02:20:14 -04:00
|
|
|
from socket import AF_INET, SOCK_STREAM, SOMAXCONN
|
|
|
|
from signal import SIGHUP, SIGTERM, SIGCHLD, SIG_DFL, SIG_IGN
|
2013-05-06 19:34:30 -04:00
|
|
|
from pyspark.worker import main as worker_main
|
2014-08-03 18:52:00 -04:00
|
|
|
from pyspark.serializers import read_int, write_int
|
2013-05-06 19:34:30 -04:00
|
|
|
|
|
|
|
|
2013-06-21 12:13:48 -04:00
|
|
|
def compute_real_exit_code(exit_code):
|
2014-07-22 01:30:53 -04:00
|
|
|
# SystemExit's code can be integer or string, but os._exit only accepts integers
|
|
|
|
if isinstance(exit_code, numbers.Integral):
|
|
|
|
return exit_code
|
|
|
|
else:
|
|
|
|
return 1
|
2013-06-21 12:13:48 -04:00
|
|
|
|
|
|
|
|
2014-08-01 22:38:21 -04:00
|
|
|
def worker(sock):
|
|
|
|
"""
|
|
|
|
Called by a worker process after the fork().
|
|
|
|
"""
|
|
|
|
signal.signal(SIGHUP, SIG_DFL)
|
|
|
|
signal.signal(SIGCHLD, SIG_DFL)
|
|
|
|
signal.signal(SIGTERM, SIG_DFL)
|
2013-05-23 14:50:24 -04:00
|
|
|
|
SPARK-1579: Clean up PythonRDD and avoid swallowing IOExceptions
This patch includes several cleanups to PythonRDD, focused around fixing [SPARK-1579](https://issues.apache.org/jira/browse/SPARK-1579) cleanly. Listed in order of approximate importance:
- The Python daemon waits for Spark to close the socket before exiting,
in order to avoid causing spurious IOExceptions in Spark's
`PythonRDD::WriterThread`.
- Removes the Python Monitor Thread, which polled for task cancellations
in order to kill the Python worker. Instead, we do this in the
onCompleteCallback, since this is guaranteed to be called during
cancellation.
- Adds a "completed" variable to TaskContext to avoid the issue noted in
[SPARK-1019](https://issues.apache.org/jira/browse/SPARK-1019), where onCompleteCallbacks may be execution-order dependent.
Along with this, I removed the "context.interrupted = true" flag in
the onCompleteCallback.
- Extracts PythonRDD::WriterThread to its own class.
Since this patch provides an alternative solution to [SPARK-1019](https://issues.apache.org/jira/browse/SPARK-1019), I did test it with
```
sc.textFile("latlon.tsv").take(5)
```
many times without error.
Additionally, in order to test the unswallowed exceptions, I performed
```
sc.textFile("s3n://<big file>").count()
```
and cut my internet during execution. Prior to this patch, we got the "stdin writer exited early" message, which was unhelpful. Now, we get the SocketExceptions propagated through Spark to the user and get proper (though unsuccessful) task retries.
Author: Aaron Davidson <aaron@databricks.com>
Closes #640 from aarondav/pyspark-io and squashes the following commits:
b391ff8 [Aaron Davidson] Detect "clean socket shutdowns" and stop waiting on the socket
c0c49da [Aaron Davidson] SPARK-1579: Clean up PythonRDD and avoid swallowing IOExceptions
2014-05-07 12:48:31 -04:00
|
|
|
# Blocks until the socket is closed by draining the input stream
|
|
|
|
# until it raises an exception or returns EOF.
|
|
|
|
def waitSocketClose(sock):
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
# Empty string is returned upon EOF (and only then).
|
|
|
|
if sock.recv(4096) == '':
|
|
|
|
return
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2014-08-01 22:38:21 -04:00
|
|
|
# Read the socket using fdopen instead of socket.makefile() because the latter
|
|
|
|
# seems to be very slow; note that we need to dup() the file descriptor because
|
|
|
|
# otherwise writes also cause a seek that makes us miss data on the read side.
|
|
|
|
infile = os.fdopen(os.dup(sock.fileno()), "a+", 65536)
|
|
|
|
outfile = os.fdopen(os.dup(sock.fileno()), "a+", 65536)
|
|
|
|
exit_code = 0
|
|
|
|
try:
|
2014-08-03 18:52:00 -04:00
|
|
|
# Acknowledge that the fork was successful
|
|
|
|
write_int(os.getpid(), outfile)
|
2014-08-01 22:38:21 -04:00
|
|
|
outfile.flush()
|
|
|
|
worker_main(infile, outfile)
|
|
|
|
except SystemExit as exc:
|
|
|
|
exit_code = exc.code
|
|
|
|
finally:
|
|
|
|
outfile.flush()
|
|
|
|
# The Scala side will close the socket upon task completion.
|
|
|
|
waitSocketClose(sock)
|
|
|
|
os._exit(compute_real_exit_code(exit_code))
|
2013-05-06 19:34:30 -04:00
|
|
|
|
|
|
|
|
2014-08-10 16:00:38 -04:00
|
|
|
# Cleanup zombie children
|
|
|
|
def cleanup_dead_children():
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
pid, _ = os.waitpid(0, os.WNOHANG)
|
|
|
|
if not pid:
|
|
|
|
break
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2013-05-06 19:34:30 -04:00
|
|
|
def manager():
|
|
|
|
# Create a new process group to corral our children
|
|
|
|
os.setpgid(0, 0)
|
|
|
|
|
|
|
|
# Create a listening socket on the AF_INET loopback interface
|
2013-07-01 02:20:14 -04:00
|
|
|
listen_sock = socket.socket(AF_INET, SOCK_STREAM)
|
2013-05-06 19:34:30 -04:00
|
|
|
listen_sock.bind(('127.0.0.1', 0))
|
2014-08-01 22:38:21 -04:00
|
|
|
listen_sock.listen(max(1024, SOMAXCONN))
|
2013-05-06 19:34:30 -04:00
|
|
|
listen_host, listen_port = listen_sock.getsockname()
|
|
|
|
write_int(listen_port, sys.stdout)
|
2014-09-12 21:42:50 -04:00
|
|
|
sys.stdout.flush()
|
2013-05-06 19:34:30 -04:00
|
|
|
|
2014-08-01 22:38:21 -04:00
|
|
|
def shutdown(code):
|
|
|
|
signal.signal(SIGTERM, SIG_DFL)
|
|
|
|
# Send SIGHUP to notify workers of shutdown
|
|
|
|
os.kill(0, SIGHUP)
|
|
|
|
exit(code)
|
2013-05-06 19:34:30 -04:00
|
|
|
|
2014-08-01 22:38:21 -04:00
|
|
|
def handle_sigterm(*args):
|
|
|
|
shutdown(1)
|
|
|
|
signal.signal(SIGTERM, handle_sigterm) # Gracefully exit on SIGTERM
|
|
|
|
signal.signal(SIGHUP, SIG_IGN) # Don't die on SIGHUP
|
2013-05-06 19:34:30 -04:00
|
|
|
|
|
|
|
# Initialization complete
|
2013-05-10 18:48:48 -04:00
|
|
|
try:
|
2014-08-01 22:38:21 -04:00
|
|
|
while True:
|
2013-05-10 18:48:48 -04:00
|
|
|
try:
|
2014-08-10 16:00:38 -04:00
|
|
|
ready_fds = select.select([0, listen_sock], [], [], 1)[0]
|
2014-08-01 22:38:21 -04:00
|
|
|
except select.error as ex:
|
|
|
|
if ex[0] == EINTR:
|
|
|
|
continue
|
|
|
|
else:
|
2013-05-10 18:48:48 -04:00
|
|
|
raise
|
2014-08-10 16:00:38 -04:00
|
|
|
|
|
|
|
# cleanup in signal handler will cause deadlock
|
|
|
|
cleanup_dead_children()
|
|
|
|
|
2014-08-01 22:38:21 -04:00
|
|
|
if 0 in ready_fds:
|
2014-08-03 18:52:00 -04:00
|
|
|
try:
|
|
|
|
worker_pid = read_int(sys.stdin)
|
|
|
|
except EOFError:
|
|
|
|
# Spark told us to exit by closing stdin
|
|
|
|
shutdown(0)
|
|
|
|
try:
|
|
|
|
os.kill(worker_pid, signal.SIGKILL)
|
|
|
|
except OSError:
|
2014-08-06 15:58:24 -04:00
|
|
|
pass # process already died
|
2014-08-03 18:52:00 -04:00
|
|
|
|
2014-08-01 22:38:21 -04:00
|
|
|
if listen_sock in ready_fds:
|
2014-08-10 16:00:38 -04:00
|
|
|
try:
|
|
|
|
sock, _ = listen_sock.accept()
|
|
|
|
except OSError as e:
|
|
|
|
if e.errno == EINTR:
|
|
|
|
continue
|
|
|
|
raise
|
|
|
|
|
2014-08-01 22:38:21 -04:00
|
|
|
# Launch a worker process
|
|
|
|
try:
|
2014-08-03 18:52:00 -04:00
|
|
|
pid = os.fork()
|
2014-08-10 16:00:38 -04:00
|
|
|
except OSError as e:
|
|
|
|
if e.errno in (EAGAIN, EINTR):
|
|
|
|
time.sleep(1)
|
|
|
|
pid = os.fork() # error here will shutdown daemon
|
2014-08-01 22:38:21 -04:00
|
|
|
else:
|
2014-08-10 16:00:38 -04:00
|
|
|
outfile = sock.makefile('w')
|
|
|
|
write_int(e.errno, outfile) # Signal that the fork failed
|
|
|
|
outfile.flush()
|
|
|
|
outfile.close()
|
2014-08-01 22:38:21 -04:00
|
|
|
sock.close()
|
2014-08-10 16:00:38 -04:00
|
|
|
continue
|
|
|
|
|
|
|
|
if pid == 0:
|
|
|
|
# in child process
|
|
|
|
listen_sock.close()
|
|
|
|
try:
|
|
|
|
worker(sock)
|
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
|
|
|
os._exit(1)
|
|
|
|
else:
|
|
|
|
os._exit(0)
|
|
|
|
else:
|
2014-08-01 22:38:21 -04:00
|
|
|
sock.close()
|
2014-08-10 16:00:38 -04:00
|
|
|
|
2013-05-10 18:48:48 -04:00
|
|
|
finally:
|
2014-08-01 22:38:21 -04:00
|
|
|
shutdown(1)
|
2013-05-06 19:34:30 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
manager()
|