Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Simplify approach just for parallelize xrange
  • Loading branch information
xuanyuanking committed Jan 7, 2019
commit ab451e5b4e152450e3fda7ef677deef52bf359a1
4 changes: 4 additions & 0 deletions python/pyspark/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,10 @@ def getStart(split):
return start0 + int((split * size / numSlices)) * step

def f(split, iterator):
# it's an empty iterator here but we need this line for triggering the logic of
# checking END_OF_DATA_SECTION during load iterator in runtime, thus make sure
# worker reuse takes effect. See more details in SPARK-26549.
assert len(list(iterator)) == 0
return xrange(getStart(split), getStart(split + 1), step)

return self.parallelize([], numSlices).mapPartitionsWithIndex(f)
Expand Down
4 changes: 2 additions & 2 deletions python/pyspark/tests/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ def test_with_different_versions_of_python(self):
finally:
self.sc.pythonVer = version

def test_reuse_worker(self):
def test_reuse_worker_of_parallelize_xrange(self):
def get_worker_pid(input_rdd):
return input_rdd.map(lambda x: os.getpid()).collect()
rdd = self.sc.parallelize(range(20), 20)
rdd = self.sc.parallelize(xrange(20), 20)
worker_pids = get_worker_pid(rdd)
pids = get_worker_pid(rdd)
for pid in pids:
Expand Down
7 changes: 1 addition & 6 deletions python/pyspark/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,12 +446,7 @@ def process():
pickleSer._write_with_length((aid, accum._value), outfile)

# check end of stream
res = read_int(infile)
if sys.version >= '3' and res == SpecialLengths.END_OF_DATA_SECTION:
# skip the END_OF_DATA_SECTION for Python3, otherwise the worker reuse will take
# no effect, see SPARK-26549 for more details.
res = read_int(infile)
if res == SpecialLengths.END_OF_STREAM:
if read_int(infile) == SpecialLengths.END_OF_STREAM:
write_int(SpecialLengths.END_OF_STREAM, outfile)
else:
# write a different value to tell JVM to not reuse this worker
Expand Down