-
Notifications
You must be signed in to change notification settings - Fork 857
Spark Serving v2 #530
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
Merged
Merged
Spark Serving v2 #530
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,106 @@ | ||
| // Copyright (C) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. See LICENSE in project root for information. | ||
|
|
||
| package org.apache.spark.sql.execution.streaming.continuous | ||
|
|
||
| import com.microsoft.ml.spark._ | ||
| import org.apache.spark.TaskContext | ||
| import org.apache.spark.internal.Logging | ||
| import org.apache.spark.sql._ | ||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.sources.DataSourceRegister | ||
| import org.apache.spark.sql.sources.v2._ | ||
| import org.apache.spark.sql.sources.v2.writer.streaming.StreamWriter | ||
| import org.apache.spark.sql.sources.v2.writer.{DataWriter, DataWriterFactory, WriterCommitMessage} | ||
| import org.apache.spark.sql.streaming.OutputMode | ||
| import org.apache.spark.sql.types._ | ||
|
|
||
| import scala.collection.mutable | ||
|
|
||
| class HTTPSinkProviderV2 extends DataSourceV2 | ||
| with StreamWriteSupport | ||
| with DataSourceRegister { | ||
|
|
||
| override def createStreamWriter(queryId: String, | ||
| schema: StructType, | ||
| mode: OutputMode, | ||
| options: DataSourceOptions): StreamWriter = { | ||
| new HTTPWriter(schema, options) | ||
| } | ||
|
|
||
| def shortName(): String = "HTTPv2" | ||
| } | ||
|
|
||
| /** Common methods used to create writes for the the console sink */ | ||
| class HTTPWriter(schema: StructType, options: DataSourceOptions) | ||
| extends StreamWriter with Logging { | ||
|
|
||
| protected val idCol: String = options.get("idCol").orElse("id") | ||
| protected val replyCol: String = options.get("replyCol").orElse("reply") | ||
| protected val name: String = options.get("name").get | ||
|
|
||
| val idColIndex: Int = schema.fieldIndex(idCol) | ||
| val replyColIndex: Int = schema.fieldIndex(replyCol) | ||
|
|
||
| assert(SparkSession.getActiveSession.isDefined) | ||
|
|
||
| def createWriterFactory(): DataWriterFactory[InternalRow] = | ||
| HTTPWriterFactory(idColIndex, replyColIndex, name) | ||
|
|
||
| override def commit(epochId: Long, messages: Array[WriterCommitMessage]): Unit = {} | ||
|
|
||
| def abort(epochId: Long, messages: Array[WriterCommitMessage]): Unit = { | ||
| HTTPSourceStateHolder.cleanUp(name) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private[streaming] case class HTTPWriterFactory(idColIndex: Int, | ||
| replyColIndex: Int, | ||
| name: String) | ||
| extends DataWriterFactory[InternalRow] { | ||
| def createDataWriter(partitionId: Int, taskId: Long, epochId: Long): DataWriter[InternalRow] = { | ||
| new HTTPDataWriter(partitionId, idColIndex, replyColIndex, name, epochId) | ||
| } | ||
| } | ||
|
|
||
| private[streaming] class HTTPDataWriter(partitionId: Int, | ||
| val idColIndex: Int, | ||
| val replyColIndex: Int, | ||
| val name: String, | ||
| epochId: Long) | ||
| extends DataWriter[InternalRow] with Logging { | ||
| logInfo(s"Creating writer on PID:$partitionId") | ||
| HTTPSourceStateHolder.getServer(name).commit(epochId - 1, partitionId) | ||
|
|
||
| private val ids: mutable.ListBuffer[(String, Int)] = new mutable.ListBuffer[(String, Int)]() | ||
|
|
||
| private val fromRow = HTTPResponseData.makeFromInternalRowConverter | ||
|
|
||
| override def write(row: InternalRow): Unit = { | ||
| val id = row.getStruct(idColIndex, 2) | ||
| val mid = id.getString(0) | ||
| val rid = id.getString(1) | ||
| val pid = id.getInt(2) | ||
| val reply = fromRow(row.getStruct(replyColIndex, 4)) | ||
| HTTPSourceStateHolder.getServer(name).replyTo(mid, rid, reply) | ||
| ids.append((rid, pid)) | ||
| } | ||
|
|
||
| override def commit(): HTTPCommitMessage = { | ||
| val msg = HTTPCommitMessage(ids.toArray) | ||
| ids.foreach { case (rid, pid) => | ||
| HTTPSourceStateHolder.getServer(name).commit(rid) | ||
| } | ||
| ids.clear() | ||
| msg | ||
| } | ||
|
|
||
| override def abort(): Unit = { | ||
| if (TaskContext.get().getKillReason().contains("Stage cancelled")) { | ||
| HTTPSourceStateHolder.cleanUp(name) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private[streaming] case class HTTPCommitMessage(ids: Array[(String, Int)]) extends WriterCommitMessage |
This file contains hidden or 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
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.
UTF8String.fromString(id.toString) looks expensive. what type is id?
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.
its a long, for some reason Spark's internal row construction needs this UTF8 string instead of a regular string. The HTTP Source is mainly good for debugging though and will be deprecated for v2 stuff soon though