-
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-23747][Structured Streaming] Add EpochCoordinator unit tests #20983
Closed
Closed
Changes from 3 commits
Commits
Show all changes
5 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
225 changes: 225 additions & 0 deletions
225
...core/src/test/scala/org/apache/spark/sql/streaming/continuous/EpochCoordinatorSuite.scala
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,225 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.apache.spark.sql.streaming.continuous | ||
|
||
import org.mockito.InOrder | ||
import org.mockito.Matchers.{any, eq => eqTo} | ||
import org.mockito.Mockito._ | ||
import org.scalatest.BeforeAndAfterEach | ||
import org.scalatest.mockito.MockitoSugar | ||
|
||
import org.apache.spark._ | ||
import org.apache.spark.rpc.RpcEndpointRef | ||
import org.apache.spark.sql.execution.streaming.continuous._ | ||
import org.apache.spark.sql.sources.v2.reader.streaming.{ContinuousReader, PartitionOffset} | ||
import org.apache.spark.sql.sources.v2.writer.WriterCommitMessage | ||
import org.apache.spark.sql.sources.v2.writer.streaming.StreamWriter | ||
import org.apache.spark.sql.test.SharedSparkSession | ||
|
||
class EpochCoordinatorSuite | ||
extends SparkFunSuite | ||
with SharedSparkSession | ||
with MockitoSugar | ||
with BeforeAndAfterEach { | ||
|
||
private var epochCoordinator: RpcEndpointRef = _ | ||
|
||
private var writer: StreamWriter = _ | ||
private var query: ContinuousExecution = _ | ||
private var orderVerifier: InOrder = _ | ||
|
||
override def beforeEach(): Unit = { | ||
val reader = mock[ContinuousReader] | ||
writer = mock[StreamWriter] | ||
query = mock[ContinuousExecution] | ||
orderVerifier = inOrder(writer, query) | ||
|
||
epochCoordinator | ||
= EpochCoordinatorRef.create(writer, reader, query, "test", 1, spark, SparkEnv.get) | ||
} | ||
|
||
override def afterEach(): Unit = { | ||
SparkEnv.get.rpcEnv.stop(epochCoordinator) | ||
} | ||
|
||
test("single epoch") { | ||
setWriterPartitions(3) | ||
setReaderPartitions(2) | ||
|
||
commitPartitionEpoch(0, 1) | ||
commitPartitionEpoch(1, 1) | ||
commitPartitionEpoch(2, 1) | ||
reportPartitionOffset(0, 1) | ||
reportPartitionOffset(1, 1) | ||
|
||
// Here and in subsequent tests this is called to make a synchronous call to EpochCoordinator | ||
// so that mocks would have been acted upon by the time verification happens | ||
makeSynchronousCall() | ||
|
||
verifyCommit(1) | ||
} | ||
|
||
test("single epoch, all but one writer partition has committed") { | ||
setWriterPartitions(3) | ||
setReaderPartitions(2) | ||
|
||
commitPartitionEpoch(0, 1) | ||
commitPartitionEpoch(1, 1) | ||
reportPartitionOffset(0, 1) | ||
reportPartitionOffset(1, 1) | ||
|
||
makeSynchronousCall() | ||
|
||
verifyCommitHasntHappened(1) | ||
} | ||
|
||
test("single epoch, all but one reader partition has reported an offset") { | ||
setWriterPartitions(3) | ||
setReaderPartitions(2) | ||
|
||
commitPartitionEpoch(0, 1) | ||
commitPartitionEpoch(1, 1) | ||
commitPartitionEpoch(2, 1) | ||
reportPartitionOffset(0, 1) | ||
|
||
makeSynchronousCall() | ||
|
||
verifyCommitHasntHappened(1) | ||
} | ||
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. I'd suggest adding test cases where all but one writer partition has committed, or all but one reader partition has reported an offset. In those cases we should verify that the StreamWriter and query commits haven't happened. |
||
|
||
test("consequent epochs, messages for epoch (k + 1) arrive after messages for epoch k") { | ||
setWriterPartitions(2) | ||
setReaderPartitions(2) | ||
|
||
commitPartitionEpoch(0, 1) | ||
commitPartitionEpoch(1, 1) | ||
reportPartitionOffset(0, 1) | ||
reportPartitionOffset(1, 1) | ||
|
||
commitPartitionEpoch(0, 2) | ||
commitPartitionEpoch(1, 2) | ||
reportPartitionOffset(0, 2) | ||
reportPartitionOffset(1, 2) | ||
|
||
makeSynchronousCall() | ||
|
||
verifyCommitsInOrderOf(List(1, 2)) | ||
} | ||
|
||
ignore("consequent epochs, a message for epoch k arrives after messages for epoch (k + 1)") { | ||
setWriterPartitions(2) | ||
setReaderPartitions(2) | ||
|
||
commitPartitionEpoch(0, 1) | ||
commitPartitionEpoch(1, 1) | ||
reportPartitionOffset(0, 1) | ||
|
||
commitPartitionEpoch(0, 2) | ||
commitPartitionEpoch(1, 2) | ||
reportPartitionOffset(0, 2) | ||
reportPartitionOffset(1, 2) | ||
|
||
// Message that arrives late | ||
reportPartitionOffset(1, 1) | ||
|
||
makeSynchronousCall() | ||
|
||
verifyCommitsInOrderOf(List(1, 2)) | ||
} | ||
|
||
ignore("several epochs, messages arrive in order 1 -> 3 -> 4 -> 2") { | ||
setWriterPartitions(1) | ||
setReaderPartitions(1) | ||
|
||
commitPartitionEpoch(0, 1) | ||
reportPartitionOffset(0, 1) | ||
|
||
commitPartitionEpoch(0, 3) | ||
reportPartitionOffset(0, 3) | ||
|
||
commitPartitionEpoch(0, 4) | ||
reportPartitionOffset(0, 4) | ||
|
||
commitPartitionEpoch(0, 2) | ||
reportPartitionOffset(0, 2) | ||
|
||
makeSynchronousCall() | ||
|
||
verifyCommitsInOrderOf(List(1, 2, 3, 4)) | ||
} | ||
|
||
ignore("several epochs, messages arrive in order 1 -> 3 -> 5 -> 4 -> 2") { | ||
setWriterPartitions(1) | ||
setReaderPartitions(1) | ||
|
||
commitPartitionEpoch(0, 1) | ||
reportPartitionOffset(0, 1) | ||
|
||
commitPartitionEpoch(0, 3) | ||
reportPartitionOffset(0, 3) | ||
|
||
commitPartitionEpoch(0, 5) | ||
reportPartitionOffset(0, 5) | ||
|
||
commitPartitionEpoch(0, 4) | ||
reportPartitionOffset(0, 4) | ||
|
||
commitPartitionEpoch(0, 2) | ||
reportPartitionOffset(0, 2) | ||
|
||
makeSynchronousCall() | ||
|
||
verifyCommitsInOrderOf(List(1, 2, 3, 4, 5)) | ||
} | ||
|
||
private def setWriterPartitions(numPartitions: Int): Unit = { | ||
epochCoordinator.askSync[Unit](SetWriterPartitions(numPartitions)) | ||
} | ||
|
||
private def setReaderPartitions(numPartitions: Int): Unit = { | ||
epochCoordinator.askSync[Unit](SetReaderPartitions(numPartitions)) | ||
} | ||
|
||
private def commitPartitionEpoch(partitionId: Int, epoch: Long): Unit = { | ||
val dummyMessage: WriterCommitMessage = mock[WriterCommitMessage] | ||
epochCoordinator.send(CommitPartitionEpoch(partitionId, epoch, dummyMessage)) | ||
} | ||
|
||
private def reportPartitionOffset(partitionId: Int, epoch: Long): Unit = { | ||
val dummyOffset: PartitionOffset = mock[PartitionOffset] | ||
epochCoordinator.send(ReportPartitionOffset(partitionId, epoch, dummyOffset)) | ||
} | ||
|
||
private def makeSynchronousCall(): Unit = { | ||
epochCoordinator.askSync[Long](GetCurrentEpoch) | ||
} | ||
|
||
private def verifyCommit(epoch: Long): Unit = { | ||
orderVerifier.verify(writer).commit(eqTo(epoch), any()) | ||
orderVerifier.verify(query).commit(epoch) | ||
} | ||
|
||
private def verifyCommitHasntHappened(epoch: Long): Unit = { | ||
verify(writer, never()).commit(eqTo(epoch), any()) | ||
verify(query, never()).commit(epoch) | ||
} | ||
|
||
private def verifyCommitsInOrderOf(epochs: Seq[Long]): Unit = { | ||
epochs.foreach(verifyCommit) | ||
} | ||
} |
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: maybe
verifyNoCommitFor
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.
+1
verifyNoCommitFor
is less verbose. will merge as soon as this is changed.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.
@tdas @jose-torres done