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-6328] [Python] Python API for StreamingListener #9186

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a63ef33
Added skeleton classes for listener.py
djalova Oct 6, 2015
5b3da81
Merge remote-tracking branch 'upstream/master' into SPARK-6328
djalova Oct 12, 2015
f6c91e9
Added skeleton class for Python API streamingListener
djalova Oct 12, 2015
1e6a87e
Added listener.py to __init__.py
djalova Oct 13, 2015
2579f64
Working implementation of StreamingListener for Python API. Need to i…
djalova Oct 13, 2015
afad086
Modified signature for addStreamingListener to match Scala API, added…
djalova Oct 14, 2015
3523e1f
Merge remote-tracking branch 'upstream/master' into SPARK-6328
djalova Oct 14, 2015
7d50848
Refactored StreamingListener methods and added getEventInfo.
djalova Oct 16, 2015
9d66e81
Added test_batch_info_reports() to streaming/tests.py
djalova Oct 20, 2015
aa87c40
Fixed merge conflicts
djalova Oct 20, 2015
0ac3df6
Added Python friendly classes for BatchInfo, OutputOperationInfo, and…
djalova Oct 23, 2015
233104d
Fixed pep8 check. Added delay to test_batch_info_reports
djalova Oct 23, 2015
79d70ab
Added StreamingListenerAdapter for Python.
djalova Oct 26, 2015
47c12ed
Added Python class for StreamInputInfo. Improved function for convert…
djalova Oct 27, 2015
f1c28c6
Merge remote-tracking branch 'upstream/master' into SPARK-6328
djalova Nov 2, 2015
6601913
Merge remote-tracking branch 'upstream/master' into SPARK-6328
djalova Nov 10, 2015
9e4e04a
Simplified Python API by using JavaStreamingListener
djalova Nov 11, 2015
5415389
Fixed syntax error for test_batch_info_reports
djalova Nov 11, 2015
7cdaf37
Added more checks for BatchInfo
djalova Nov 12, 2015
ddad255
Fixed asserts in test_batch_info_reports
djalova Nov 13, 2015
64192d6
Merge remote-tracking branch 'upstream/master' into SPARK-6328
djalova Nov 13, 2015
5349c82
Added more checks to test_batch_info_reports
djalova Nov 13, 2015
c941c3e
Fixed tests in test_batch_info_reports
djalova Nov 14, 2015
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
3 changes: 2 additions & 1 deletion python/pyspark/streaming/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@

from pyspark.streaming.context import StreamingContext
from pyspark.streaming.dstream import DStream
from pyspark.streaming.listener import StreamingListener

__all__ = ['StreamingContext', 'DStream']
__all__ = ['StreamingContext', 'DStream', 'StreamingListener']
9 changes: 9 additions & 0 deletions python/pyspark/streaming/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from pyspark.storagelevel import StorageLevel
from pyspark.streaming.dstream import DStream
from pyspark.streaming.util import TransformFunction, TransformFunctionSerializer
from pyspark.streaming.listener import StreamingListenerAdapter

__all__ = ["StreamingContext"]

Expand Down Expand Up @@ -363,3 +364,11 @@ def union(self, *dstreams):
first = dstreams[0]
jrest = [d._jdstream for d in dstreams[1:]]
return DStream(self._jssc.union(first._jdstream, jrest), self, first._jrdd_deserializer)

def addStreamingListener(self, streamingListener):
"""
Add a [[org.apache.spark.streaming.scheduler.StreamingListener]] object for
receiving system events related to streaming.
"""
streamingListenerAdapter = StreamingListenerAdapter(streamingListener)
self._jssc.addStreamingListener(streamingListenerAdapter)
285 changes: 285 additions & 0 deletions python/pyspark/streaming/listener.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
#
# 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.
#

__all__ = ["StreamingListener"]


class StreamingListenerEvent(object):

def __init__(self):
pass


class StreamingListenerBatchSubmitted(StreamingListenerEvent):

def __init__(self, batchInfo):
super(StreamingListenerEvent, self).__init__()
self.batchInfo = batchInfo


class StreamingListenerBatchCompleted(StreamingListenerEvent):

def __init__(self, batchInfo):
super(StreamingListenerEvent, self).__init__()
self.batchInfo = batchInfo


class StreamingListenerBatchStarted(StreamingListenerEvent):

def __init__(self, batchInfo):
super(StreamingListenerEvent, self).__init__()
self.batchInfo = batchInfo


class StreamingListenerOutputOperationStarted(StreamingListenerEvent):

def __init__(self, outputOperationInfo):
super(StreamingListenerEvent, self).__init__()
self.outputOperationInfo = outputOperationInfo


class StreamingListenerOutputOperationCompleted(StreamingListenerEvent):

def __init__(self, outputOperationInfo):
super(StreamingListenerEvent, self).__init__()
self.outputOperationInfo = outputOperationInfo


class StreamingListenerReceieverStarted(StreamingListenerEvent):

def __init__(self, receiverInfo):
super(StreamingListenerEvent, self).__init__()
self.receiverInfo = receiverInfo


class StreamingListenerReceiverError(StreamingListenerEvent):

def __init__(self, receiverInfo):
super(StreamingListenerEvent, self).__init__()
self.receiverInfo = receiverInfo


class StreamingListenerReceiverStopped(StreamingListenerEvent):

def __init__(self, receiverInfo):
super(StreamingListenerEvent, self).__init__()
self.receiverInfo = receiverInfo


class StreamingListener(object):

def __init__(self):
pass

def onReceiverStarted(self, receiverStarted):
"""
Called when a receiver has been started
"""
pass

def onReceiverError(self, receiverError):
"""
Called when a receiver has reported an error
"""
pass

def onReceiverStopped(self, receiverStopped):
"""
Called when a receiver has been stopped
"""
pass

def onBatchSubmitted(self, batchSubmitted):
"""
Called when a batch of jobs has been submitted for processing.
"""
pass

def onBatchStarted(self, batchStarted):
"""
Called when processing of a batch of jobs has started.
"""
pass

def onBatchCompleted(self, batchCompleted):
"""
Called when processing of a batch of jobs has completed.
"""
pass

def onOutputOperationStarted(self, outputOperationStarted):
"""
Called when processing of a job of a batch has started.
"""
pass

def onOutputOperationCompleted(self, outputOperationCompleted):
"""
Called when processing of a job of a batch has completed
"""
pass

class Java:
implements = ["org.apache.spark.streaming.scheduler.StreamingListener"]


class StreamingListenerAdapter(StreamingListener):

def __init__(self, streamingListener):
super(StreamingListener, self).__init__()
self.userStreamingListener = streamingListener

def onReceiverStarted(self, receiverStarted):
receiver_info = ReceiverInfo(receiverStarted.receiverInfo())
receiver_started = StreamingListenerReceieverStarted(receiver_info)
self.userStreamingListener.onReceiverStarted(receiver_started)

def onReceiverError(self, receiverError):
receiver_info = ReceiverInfo(receiverError.receiverInfo())
receiver_error = StreamingListenerReceiverError(receiver_info)
self.userStreamingListener.onReceiverError(receiver_error)

def onReceiverStopped(self, receiverStopped):
receiver_info = ReceiverInfo(receiverStopped.receiverInfo())
receiver_stopped = StreamingListenerReceiverStopped(receiver_info)
self.userStreamingListener.onReceiverStopped(receiver_stopped)

def onBatchSubmitted(self, batchSubmitted):
batch_info = BatchInfo(batchSubmitted.batchInfo())
batch_submitted = StreamingListenerBatchSubmitted(batch_info)
self.userStreamingListener.onBatchSubmitted(batch_submitted)

def onBatchStarted(self, batchStarted):
batch_info = BatchInfo(batchStarted.batchInfo())
batch_started = StreamingListenerBatchStarted(batch_info)
self.userStreamingListener .onBatchStarted(batch_started)
Copy link
Member

Choose a reason for hiding this comment

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

nit: redundant space


def onBatchCompleted(self, batchCompleted):
batch_info = BatchInfo(batchCompleted.batchInfo())
batch_completed = StreamingListenerBatchCompleted(batch_info)
self.userStreamingListener.onBatchCompleted(batch_completed)

def onOutputOperationStarted(self, outputOperationStarted):
output_op_info = OutputOperationInfo(outputOperationStarted.outputOperationInfo())
output_operation_started = StreamingListenerOutputOperationStarted(output_op_info)
self.userStreamingListener.onOutputOperationStarted(output_operation_started)

def onOutputOperationCompleted(self, outputOperationCompleted):
output_op_info = OutputOperationInfo(outputOperationCompleted.outputOperationInfo())
output_operation_completed = StreamingListenerOutputOperationCompleted(output_op_info)
self.userStreamingListener.onOutputOperationCompleted(output_operation_completed)


class BatchInfo(object):

def __init__(self, javaBatchInfo):

self.processingStartTime = None
self.processingEndTime = None

self.batchTime = javaBatchInfo.batchTime()
self.streamIdToInputInfo = self._map2dict(javaBatchInfo.streamIdToInputInfo())
Copy link
Member

Choose a reason for hiding this comment

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

StreamInputInfo has a Scala Map field.


self.submissionTime = javaBatchInfo.submissionTime()
if javaBatchInfo.processingStartTime().isEmpty() is False:
self.processingStartTime = javaBatchInfo.processingStartTime().get()
if javaBatchInfo.processingEndTime().isEmpty() is False:
self.processingEndTime = javaBatchInfo.processingEndTime().get()

self.outputOperationInfos = self._map2dict(javaBatchInfo.outputOperationInfos())
Copy link
Member

Choose a reason for hiding this comment

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

OutputOperationInfo has Scala Option fields.


def schedulingDelay(self):
"""
Time taken for the first job of this batch to start processing from the time this batch
was submitted to the streaming scheduler.
"""
if self.processingStartTime is None:
return None
else:
return self.processingStartTime - self.submissionTime

def processingDelay(self):
"""
Time taken for the all jobs of this batch to finish processing from the time they started
processing.
"""
if self.processingEndTime is None or self.processingStartTime is None:
return None
else:
return self.processingEndTime - self.processingStartTime

def totalDelay(self):
"""
Time taken for all the jobs of this batch to finish processing from the time they
were submitted
"""
if self.processingEndTime is None or self.processingStartTime is None:
return None
else:
return self.processingDelay() + self.schedulingDelay()

def numRecords(self):
"""
The number of recorders received by the receivers in this batch.
"""
return len(self.streamIdToInputInfo)

def _map2dict(self, javaMap):
"""
Converts a scala.collection.immutable.Map to a Python dict
"""
mapping = dict()
map_iterator = javaMap.iterator()
while map_iterator.hasNext():
entry = map_iterator.next()
mapping[entry._1()] = entry._2()
return mapping


class OutputOperationInfo(object):

def __init__(self, outputOperationInfo):
self.batchTime = outputOperationInfo.batchTime()
self.id = outputOperationInfo.id()
self.name = outputOperationInfo.name()
self.startTime = None
if outputOperationInfo.startTime().isEmpty() is False:
self.startTime = outputOperationInfo.startTime().get()
self.endTime = None
if outputOperationInfo.endTime().isEmpty() is False:
self.endTime = outputOperationInfo.endTime().get()
self.failureReason = None
if outputOperationInfo.failureReason().isEmpty() is False:
self.failureReason = outputOperationInfo.failureReason().get()

def duration(self):
if self.endTime is None or self.startTime is None:
return None
else:
return self.endTime - self.startTime


class ReceiverInfo(object):

def __init__(self, receiverInfo):
self.streamId = receiverInfo.streamId()
self.name = receiverInfo.name()
self.active = receiverInfo.active()
self.location = receiverInfo.location()
self.lastErrorMessage = receiverInfo.lastErrorMessage()
self.lastError = receiverInfo.lastError()
self.lastErrorTime = receiverInfo.lastErrorTime()
Loading