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-35800][SS] Improving GroupState testability by introducing TestGroupState #32938

Conversation

lizhangdatabricks
Copy link
Contributor

@lizhangdatabricks lizhangdatabricks commented Jun 16, 2021

What changes were proposed in this pull request?

Proposed changes in this pull request:

  1. Introducing the TestGroupState interface which is inherited from GroupState so that testing related getters can be exposed in a controlled manner
  2. Changing GroupStateImpl to inherit from TestGroupState interface, instead of directly from GroupState
  3. Implementing TestGroupState object with create() method to forward inputs to the private GroupStateImpl constructor
  4. User input validations have been added into GroupStateImpl's createForStreaming() method to prevent users from creating invalid GroupState objects.
  5. Replacing existing GroupStateImpl usages in sql pkg internal unit tests with the newly added TestGroupState to give user best practice about TestGroupState usage.

With the changes in this PR, the class hierarchy is changed from GroupStateImpl -> GroupState to GroupStateImpl -> TestGroupState -> GroupState (-> means inherits from)

Why are the changes needed?

The internal GroupStateImpl implementation for the GroupState interface has no public constructors accessible outside of the sql pkg. However, the user-provided state transition function for [map|flatMap]GroupsWithState requires a GroupState object as the prevState input.

Currently, users are calling the Structured Streaming engine in their unit tests in order to instantiate such GroupState instances, which makes UTs cumbersome.

The proposed TestGroupState interface is to give users controlled access to the GroupStateImpl internal implementation to largely improve testability of Structured Streaming state transition functions.

Usage Example

import org.apache.spark.sql.streaming.TestGroupState

test(“Structured Streaming state update function”) {
  var prevState = TestGroupState.create[UserStatus](
    optionalState = Optional.empty[UserStatus],
    timeoutConf = EventTimeTimeout,
    batchProcessingTimeMs = 1L,
    eventTimeWatermarkMs = Optional.of(1L),
    hasTimedOut = false)

  val userId: String = ...
  val actions: Iterator[UserAction] = ...

  assert(!prevState.hasUpdated)

  updateState(userId, actions, prevState)

  assert(prevState.hasUpdated)
}

Does this PR introduce any user-facing change?

Yes, the TestGroupState interface and its corresponding create() factory function in its companion object are introduced in this pull request for users to use in unit tests.

How was this patch tested?

  • New unit tests are added
  • Existing GroupState unit tests are updated

@AmplabJenkins
Copy link

Can one of the admins verify this patch?

@HeartSaVioR
Copy link
Contributor

Hello @lizhangdatabricks , welcome to the Apache Spark community!

I'd like to recommend you to take some time to read the section of Contributing Code Changes in contribution page in Apache Spark website - http://spark.apache.org/contributing.html

Shall we check your proposal with the steps and update it after reading?

@lizhangdatabricks
Copy link
Contributor Author

lizhangdatabricks commented Jun 17, 2021 via email

@lizhangdatabricks lizhangdatabricks changed the title Improving GroupState testability by introducing TestGroupState [SPARK-35800][Structured Streaming] Improving GroupState testability by introducing TestGroupState Jun 17, 2021
@HeartSaVioR HeartSaVioR changed the title [SPARK-35800][Structured Streaming] Improving GroupState testability by introducing TestGroupState [SPARK-35800][SS] Improving GroupState testability by introducing TestGroupState Jun 18, 2021
@HeartSaVioR
Copy link
Contributor

Please fill the content of the PR accordingly based on PR template on Apache Spark community's guideline.

https://github.com/apache/spark/blob/master/.github/PULL_REQUEST_TEMPLATE

Let's try not to remote/skip any section in the template. You can seek some approved PRs and see how these PRs fill the content.

*/
trait TestGroupState[S] extends GroupState[S] {
/** Whether the state has been marked for removing */
def hasRemoved: Boolean
Copy link
Contributor

Choose a reason for hiding this comment

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

I think isRemoved and isUpdated are better.

*
* @tparam S User-defined type of the state to be stored for each group. Must be encodable into
* Spark SQL types (see `Encoder` for more details).
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the original GroupState API is still marked as experimental (I know it does make sense, but that's for a different discussion), lets be consistent and add the same annotations to this as well. similarly, also add @since 3.2.0

and would be nice if you could add Java and Scala code snippets of how to use these traits. It's not entirely obvious how to use TestGroupState. See GroupState to find out how to add code snippets. This is how it looks later - https://spark.apache.org/docs/2.4.0/api/scala/index.html#org.apache.spark.sql.streaming.GroupState

* // with desired configs. The create() API would guarantee that
* // the generated instance has the same behavior as the one built by
* // engine with the same configs.
* var prevState = TestGroupState.create[UserStatus](
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: why is this a var

Copy link
Contributor

Choose a reason for hiding this comment

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

why not be consistent with the docs of GroupState by using the same names and types. like the function name over there is mappingFunction and data types are String, Int, and Int?

* // engine with the same configs.
* var prevState = TestGroupState.create[UserStatus](
* optionalState = Optional.empty[UserStatus],
* timeoutConf = EventTimeTimeout,
Copy link
Contributor

@tdas tdas Jun 22, 2021

Choose a reason for hiding this comment

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

lets keep this simple...NoTimeout

*
* // Calls the state transition function with the test previous state
* // with desired configs.
* updateState(userId, actions, prevState)
Copy link
Contributor

@tdas tdas Jun 22, 2021

Choose a reason for hiding this comment

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

if you are sticking to new names, then at least define updateState as the function that is going to be used with map/flatmapGroupsWithState

def isUpdated: Boolean

/**
* Returns the timestamp if setTimeoutTimestamp is called.
Copy link
Contributor

@tdas tdas Jun 22, 2021

Choose a reason for hiding this comment

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

Link to the corresponding functions in GroupState. If thats complicated, at least put the function names and codee snippets in backticks

`setTimeoutTimestamp` `setTimeoutDuration` 
`Optional.empty`

and Or returns batch processing time + the duration...
keeps the verb tense consistent with the previous "Returns the timestamp"

* @return a [[TestGroupState]] instance that's built with the user specified configs.
*/
@throws[IllegalArgumentException]("if 'batchProcessingTimeMs' is not positive")
@throws[IllegalArgumentException]("if 'eventTimeWatermarkMs' present but is not 0 or positive")
Copy link
Contributor

@tdas tdas Jun 22, 2021

Choose a reason for hiding this comment

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

is present but is < 0

not X or Y is a sometimes a little ambiguous to parse in english. is it !x || y or !(x || y)

* // engine with the same configs.
* TestGroupState prevState = new TestGroupState<UserStatus>().create(
* optionalState = Optional.<UserStatus>empty(),
* timeoutConf = EventTimeTimeout,
Copy link
Contributor

Choose a reason for hiding this comment

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

same comments as above.

* will be supported.
* @param batchProcessingTimeMs Processing time of current batch, used to calculate timestamp
* for processing time timeouts.
* @param eventTimeWatermarkMs Optional value of event time watermark in ms. Set as None if
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: None is not correct. its Optional.empty . isnt it?

import org.apache.spark.sql.execution.streaming.GroupStateImpl._

/**
* The extended version of [[GroupState]] interface with extra getters of state machine fields
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you need htis as well
::Experimental::
See GroupState

Copy link
Contributor

@tdas tdas left a comment

Choose a reason for hiding this comment

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

Thank you for the changes. This is going to be very useful!

@asfgit asfgit closed this in dfd7b02 Jun 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants