2012-08-10 04:10:02 -04:00
|
|
|
"""
|
|
|
|
Worker that receives input from Piped RDD.
|
|
|
|
"""
|
2013-02-01 03:25:19 -05:00
|
|
|
import os
|
2012-08-10 04:10:02 -04:00
|
|
|
import sys
|
2013-01-31 21:02:28 -05:00
|
|
|
import traceback
|
2012-08-10 04:10:02 -04:00
|
|
|
from base64 import standard_b64decode
|
|
|
|
# CloudPickler needs to be imported so that depicklers are registered using the
|
|
|
|
# copy_reg module.
|
2013-01-20 04:57:44 -05:00
|
|
|
from pyspark.accumulators import _accumulatorRegistry
|
2012-08-25 16:59:01 -04:00
|
|
|
from pyspark.broadcast import Broadcast, _broadcastRegistry
|
2012-08-19 20:12:51 -04:00
|
|
|
from pyspark.cloudpickle import CloudPickler
|
2013-01-21 19:42:24 -05:00
|
|
|
from pyspark.files import SparkFiles
|
2013-01-20 04:57:44 -05:00
|
|
|
from pyspark.serializers import write_with_length, read_with_length, write_int, \
|
2012-12-24 20:20:10 -05:00
|
|
|
read_long, read_int, dump_pickle, load_pickle, read_from_pickle_file
|
2012-08-25 19:46:07 -04:00
|
|
|
|
2012-08-10 04:10:02 -04:00
|
|
|
|
|
|
|
# Redirect stdout to stderr so that users must return values from functions.
|
2013-02-01 03:25:19 -05:00
|
|
|
old_stdout = os.fdopen(os.dup(1), 'w')
|
|
|
|
os.dup2(2, 1)
|
2012-08-10 04:10:02 -04:00
|
|
|
|
|
|
|
|
2012-08-25 19:46:07 -04:00
|
|
|
def load_obj():
|
|
|
|
return load_pickle(standard_b64decode(sys.stdin.readline().strip()))
|
2012-08-10 04:10:02 -04:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2013-01-08 19:04:41 -05:00
|
|
|
split_index = read_int(sys.stdin)
|
2013-01-21 19:42:24 -05:00
|
|
|
spark_files_dir = load_pickle(read_with_length(sys.stdin))
|
|
|
|
SparkFiles._root_directory = spark_files_dir
|
2013-01-23 13:36:18 -05:00
|
|
|
SparkFiles._is_running_on_worker = True
|
2013-01-22 20:54:11 -05:00
|
|
|
sys.path.append(spark_files_dir)
|
2012-10-19 13:24:49 -04:00
|
|
|
num_broadcast_variables = read_int(sys.stdin)
|
2012-08-25 16:59:01 -04:00
|
|
|
for _ in range(num_broadcast_variables):
|
2012-10-19 13:24:49 -04:00
|
|
|
bid = read_long(sys.stdin)
|
2012-08-25 19:46:07 -04:00
|
|
|
value = read_with_length(sys.stdin)
|
2012-10-19 13:24:49 -04:00
|
|
|
_broadcastRegistry[bid] = Broadcast(bid, load_pickle(value))
|
2012-08-25 19:46:07 -04:00
|
|
|
func = load_obj()
|
|
|
|
bypassSerializer = load_obj()
|
|
|
|
if bypassSerializer:
|
|
|
|
dumps = lambda x: x
|
2012-08-10 04:10:02 -04:00
|
|
|
else:
|
2012-08-25 19:46:07 -04:00
|
|
|
dumps = dump_pickle
|
2013-01-08 19:04:41 -05:00
|
|
|
iterator = read_from_pickle_file(sys.stdin)
|
2013-01-31 21:02:28 -05:00
|
|
|
try:
|
|
|
|
for obj in func(split_index, iterator):
|
|
|
|
write_with_length(dumps(obj), old_stdout)
|
|
|
|
except Exception as e:
|
|
|
|
write_int(-2, old_stdout)
|
|
|
|
write_with_length(traceback.format_exc(), old_stdout)
|
|
|
|
sys.exit(-1)
|
2013-01-20 04:57:44 -05:00
|
|
|
# Mark the beginning of the accumulators section of the output
|
|
|
|
write_int(-1, old_stdout)
|
|
|
|
for aid, accum in _accumulatorRegistry.items():
|
|
|
|
write_with_length(dump_pickle((aid, accum._value)), old_stdout)
|
2012-08-10 04:10:02 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|