-
Notifications
You must be signed in to change notification settings - Fork 28k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-48062][PYTHON][SS][TESTS] Add pyspark test for SimpleDataSourceStreamingReader #46306
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
DataSourceStreamReader, | ||
InputPartition, | ||
DataSourceStreamWriter, | ||
SimpleDataSourceStreamReader, | ||
WriterCommitMessage, | ||
) | ||
from pyspark.sql.types import Row | ||
|
@@ -150,50 +151,90 @@ def check_batch(df, batch_id): | |
q.awaitTermination | ||
self.assertIsNone(q.exception(), "No exception has to be propagated.") | ||
|
||
def test_stream_writer(self): | ||
input_dir = tempfile.TemporaryDirectory(prefix="test_data_stream_write_input") | ||
output_dir = tempfile.TemporaryDirectory(prefix="test_data_stream_write_output") | ||
checkpoint_dir = tempfile.TemporaryDirectory(prefix="test_data_stream_write_checkpoint") | ||
def test_simple_stream_reader(self): | ||
class SimpleStreamReader(SimpleDataSourceStreamReader): | ||
def initialOffset(self): | ||
return {"offset": 0} | ||
|
||
self.spark.range(0, 30).repartition(2).write.format("json").mode("append").save( | ||
input_dir.name | ||
) | ||
self.spark.dataSource.register(self._get_test_data_source()) | ||
df = self.spark.readStream.schema("id int").json(input_dir.name) | ||
q = ( | ||
df.writeStream.format("TestDataSource") | ||
.option("checkpointLocation", checkpoint_dir.name) | ||
.start(output_dir.name) | ||
) | ||
while not q.recentProgress: | ||
time.sleep(0.2) | ||
def read(self, start: dict): | ||
start_idx = start["offset"] | ||
it = iter([(i,) for i in range(start_idx, start_idx + 2)]) | ||
return (it, {"offset": start_idx + 2}) | ||
|
||
def commit(self, end): | ||
pass | ||
|
||
def readBetweenOffsets(self, start: dict, end: dict): | ||
start_idx = start["offset"] | ||
end_idx = end["offset"] | ||
return iter([(i,) for i in range(start_idx, end_idx)]) | ||
|
||
class SimpleDataSource(DataSource): | ||
def schema(self): | ||
return "id INT" | ||
|
||
def simpleStreamReader(self, schema): | ||
return SimpleStreamReader() | ||
|
||
# Test stream writer write and commit. | ||
# The first microbatch contain 30 rows and 2 partitions. | ||
# Number of rows and partitions is writen by StreamWriter.commit(). | ||
assertDataFrameEqual(self.spark.read.json(output_dir.name), [Row(2, 30)]) | ||
self.spark.dataSource.register(SimpleDataSource) | ||
df = self.spark.readStream.format("SimpleDataSource").load() | ||
|
||
self.spark.range(50, 80).repartition(2).write.format("json").mode("append").save( | ||
input_dir.name | ||
) | ||
def check_batch(df, batch_id): | ||
assertDataFrameEqual(df, [Row(batch_id * 2), Row(batch_id * 2 + 1)]) | ||
|
||
# Test StreamWriter write and abort. | ||
# When row id > 50, write tasks throw exception and fail. | ||
# 1.txt is written by StreamWriter.abort() to record the failure. | ||
while q.exception() is None: | ||
q = df.writeStream.foreachBatch(check_batch).start() | ||
while len(q.recentProgress) < 10: | ||
time.sleep(0.2) | ||
assertDataFrameEqual( | ||
self.spark.read.text(os.path.join(output_dir.name, "1.txt")), [Row("failed in batch 1")] | ||
) | ||
q.awaitTermination | ||
q.stop() | ||
q.awaitTermination() | ||
self.assertIsNone(q.exception(), "No exception has to be propagated.") | ||
|
||
def test_stream_writer(self): | ||
input_dir = tempfile.TemporaryDirectory(prefix="test_data_stream_write_input") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's add cleanup input_dir.cleanup()
output_dir.cleanup()
checkpoint_dir.cleanup() those into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
output_dir = tempfile.TemporaryDirectory(prefix="test_data_stream_write_output") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
has to be
because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
checkpoint_dir = tempfile.TemporaryDirectory(prefix="test_data_stream_write_checkpoint") | ||
|
||
input_dir.cleanup() | ||
output_dir.cleanup() | ||
checkpoint_dir.cleanup() | ||
try: | ||
self.spark.range(0, 30).repartition(2).write.format("json").mode("append").save( | ||
input_dir.name | ||
) | ||
self.spark.dataSource.register(self._get_test_data_source()) | ||
df = self.spark.readStream.schema("id int").json(input_dir.name) | ||
q = ( | ||
df.writeStream.format("TestDataSource") | ||
.option("checkpointLocation", checkpoint_dir.name) | ||
.start(output_dir.name) | ||
) | ||
while not q.recentProgress: | ||
time.sleep(0.2) | ||
|
||
# Test stream writer write and commit. | ||
# The first microbatch contain 30 rows and 2 partitions. | ||
# Number of rows and partitions is writen by StreamWriter.commit(). | ||
assertDataFrameEqual(self.spark.read.json(output_dir.name), [Row(2, 30)]) | ||
|
||
self.spark.range(50, 80).repartition(2).write.format("json").mode("append").save( | ||
input_dir.name | ||
) | ||
|
||
# Test StreamWriter write and abort. | ||
# When row id > 50, write tasks throw exception and fail. | ||
# 1.txt is written by StreamWriter.abort() to record the failure. | ||
while q.exception() is None: | ||
time.sleep(0.2) | ||
assertDataFrameEqual( | ||
self.spark.read.text(os.path.join(output_dir.name, "1.txt")), | ||
[Row("failed in batch 1")], | ||
) | ||
q.awaitTermination | ||
finally: | ||
input_dir.cleanup() | ||
output_dir.cleanup() | ||
checkpoint_dir.cleanup() | ||
|
||
|
||
class PythonStreamingDataSourceTests(BasePythonStreamingDataSourceTestsMixin, ReusedSQLTestCase): | ||
... | ||
pass | ||
|
||
|
||
if __name__ == "__main__": | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chaoqin-li1123 can we fix all those instances?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you elaborate? is this test flaky?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
q.awaitTermination()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for reminding!