-
Notifications
You must be signed in to change notification settings - Fork 28.5k
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
Closed
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 5b3da81
Merge remote-tracking branch 'upstream/master' into SPARK-6328
djalova f6c91e9
Added skeleton class for Python API streamingListener
djalova 1e6a87e
Added listener.py to __init__.py
djalova 2579f64
Working implementation of StreamingListener for Python API. Need to i…
djalova afad086
Modified signature for addStreamingListener to match Scala API, added…
djalova 3523e1f
Merge remote-tracking branch 'upstream/master' into SPARK-6328
djalova 7d50848
Refactored StreamingListener methods and added getEventInfo.
djalova 9d66e81
Added test_batch_info_reports() to streaming/tests.py
djalova aa87c40
Fixed merge conflicts
djalova 0ac3df6
Added Python friendly classes for BatchInfo, OutputOperationInfo, and…
djalova 233104d
Fixed pep8 check. Added delay to test_batch_info_reports
djalova 79d70ab
Added StreamingListenerAdapter for Python.
djalova 47c12ed
Added Python class for StreamInputInfo. Improved function for convert…
djalova f1c28c6
Merge remote-tracking branch 'upstream/master' into SPARK-6328
djalova 6601913
Merge remote-tracking branch 'upstream/master' into SPARK-6328
djalova 9e4e04a
Simplified Python API by using JavaStreamingListener
djalova 5415389
Fixed syntax error for test_batch_info_reports
djalova 7cdaf37
Added more checks for BatchInfo
djalova ddad255
Fixed asserts in test_batch_info_reports
djalova 64192d6
Merge remote-tracking branch 'upstream/master' into SPARK-6328
djalova 5349c82
Added more checks to test_batch_info_reports
djalova c941c3e
Fixed tests in test_batch_info_reports
djalova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
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()) | ||
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.
|
||
|
||
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()) | ||
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.
|
||
|
||
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() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: redundant space