Skip to content
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

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
111 changes: 76 additions & 35 deletions python/pyspark/sql/tests/test_python_streaming_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
DataSourceStreamReader,
InputPartition,
DataSourceStreamWriter,
SimpleDataSourceStreamReader,
WriterCommitMessage,
)
from pyspark.sql.types import Row
Expand Down Expand Up @@ -150,50 +151,90 @@ def check_batch(df, batch_id):
q.awaitTermination
Copy link
Member

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?

Copy link
Contributor Author

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

q.awaitTermination()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reminding!

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")
Copy link
Member

Choose a reason for hiding this comment

The 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 finally. Feel free to do it in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

output_dir = tempfile.TemporaryDirectory(prefix="test_data_stream_write_output")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class PythonStreamingDataSourceTests(BasePythonStreamingDataSourceTestsMixin, ReusedSQLTestCase):
    ...

has to be

class PythonStreamingDataSourceTests(BasePythonStreamingDataSourceTestsMixin, ReusedSQLTestCase):
    pass

because ... basically means to omit (and to be defined/implemented laster).

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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__":
Expand Down