Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
0683d51
[SPARK-46913] Timer related implementation for transformWithState ope…
anishshri-db Jan 30, 2024
5494f23
Misc updates
anishshri-db Jan 30, 2024
709013a
Merge branch 'master' into task/SPARK-46913
anishshri-db Feb 1, 2024
6a81f49
Test changes
anishshri-db Feb 1, 2024
04896ca
Merge branch 'master' into task/SPARK-46913
anishshri-db Feb 6, 2024
427cdba
Merge branch 'master' into task/SPARK-46913
anishshri-db Feb 8, 2024
a72a64d
Misc updates
anishshri-db Feb 9, 2024
7799319
[SPARK-46979] Add support for specifying key and value encoder separa…
anishshri-db Feb 6, 2024
5b356fd
Misc fix
anishshri-db Feb 6, 2024
89e1e15
Change to address Bhuwan's comments
anishshri-db Feb 6, 2024
e2833ed
Misc fixes
anishshri-db Feb 9, 2024
eae2dc2
Misc updates
anishshri-db Feb 9, 2024
f2b7d17
Added tests
anishshri-db Feb 10, 2024
2f06795
Merge branch 'master' into task/SPARK-46913
anishshri-db Feb 14, 2024
ade22ae
Merge branch 'master' into task/SPARK-46913
anishshri-db Feb 21, 2024
d55d40c
Misc changes
anishshri-db Feb 21, 2024
9f3e809
Misc updates
anishshri-db Feb 22, 2024
ede1db1
Misc updates
anishshri-db Feb 22, 2024
fd0db3a
Misc updates
anishshri-db Feb 22, 2024
29e4427
Misc fix
anishshri-db Feb 22, 2024
6b83aaf
Fix nerf error
anishshri-db Feb 23, 2024
fcc7a33
Misc test addition
anishshri-db Feb 23, 2024
e3da877
Change to address Bhuwan's comments
anishshri-db Feb 27, 2024
d18d2b8
Change to address Bhuwan's comments
anishshri-db Feb 28, 2024
6018c73
Fix test case
anishshri-db Feb 28, 2024
7c6d6d5
Merge branch 'master' into task/SPARK-46913
anishshri-db Mar 11, 2024
7b26daf
Change to address Jungtaeks' comments
anishshri-db Mar 11, 2024
145e041
Address Jungtaek's comments
anishshri-db Mar 11, 2024
88e20f4
Misc updates
anishshri-db Mar 12, 2024
ea356a0
Fix misc issues
anishshri-db Mar 12, 2024
5efa1f7
Merge branch 'master' into task/SPARK-46913
anishshri-db Mar 12, 2024
65e58f5
Misc updates
anishshri-db Mar 12, 2024
436181f
Add unit test
anishshri-db Mar 12, 2024
b2032fd
Add unit test
anishshri-db Mar 12, 2024
d83fdb0
Merge branch 'master' into task/SPARK-46913
anishshri-db Mar 12, 2024
f46063a
Remove redundant error class
anishshri-db Mar 12, 2024
a139c5f
Fix misc issues
anishshri-db Mar 13, 2024
b311787
Misc fix
anishshri-db Mar 13, 2024
4649b8d
Add note around usage of listTimers
anishshri-db Mar 13, 2024
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
18 changes: 18 additions & 0 deletions common/utils/src/main/resources/error/error-classes.json
Original file line number Diff line number Diff line change
Expand Up @@ -3488,6 +3488,24 @@
],
"sqlState" : "0A000"
},
"STATEFUL_PROCESSOR_CANNOT_PERFORM_OPERATION_WITH_INVALID_HANDLE_STATE" : {
"message" : [
"Failed to perform stateful processor operation=<operationType> with invalid handle state=<handleState>."
],
"sqlState" : "42802"
},
"STATEFUL_PROCESSOR_CANNOT_PERFORM_OPERATION_WITH_INVALID_TIMEOUT_MODE" : {
"message" : [
"Failed to perform stateful processor operation=<operationType> with invalid timeoutMode=<timeoutMode>"
],
"sqlState" : "42802"
},
"STATE_STORE_CANNOT_CREATE_COLUMN_FAMILY_WITH_RESERVED_CHARS" : {
"message" : [
"Failed to create column family with unsupported starting character and name=<colFamilyName>."
],
"sqlState" : "42802"
},
"STATE_STORE_CANNOT_USE_COLUMN_FAMILY_WITH_INVALID_NAME" : {
"message" : [
"Failed to perform column family operation=<operationName> with invalid name=<colFamilyName>. Column family name cannot be empty or include leading/trailing spaces or use the reserved keyword=default"
Expand Down
18 changes: 18 additions & 0 deletions docs/sql-error-conditions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2145,6 +2145,24 @@ The SQL config `<sqlConf>` cannot be found. Please verify that the config exists

Star (*) is not allowed in a select list when GROUP BY an ordinal position is used.

### STATEFUL_PROCESSOR_CANNOT_PERFORM_OPERATION_WITH_INVALID_HANDLE_STATE

[SQLSTATE: 42802](sql-error-conditions-sqlstates.html#class-42-syntax-error-or-access-rule-violation)

Failed to perform stateful processor operation=`<operationType>` with invalid handle state=`<handleState>`.

### STATEFUL_PROCESSOR_CANNOT_PERFORM_OPERATION_WITH_INVALID_TIMEOUT_MODE

[SQLSTATE: 42802](sql-error-conditions-sqlstates.html#class-42-syntax-error-or-access-rule-violation)

Failed to perform stateful processor operation=`<operationType>` with invalid timeoutMode=`<timeoutMode>`

### STATE_STORE_CANNOT_CREATE_COLUMN_FAMILY_WITH_RESERVED_CHARS

[SQLSTATE: 42802](sql-error-conditions-sqlstates.html#class-42-syntax-error-or-access-rule-violation)

Failed to create column family with unsupported starting character and name=`<colFamilyName>`.

### STATE_STORE_CANNOT_USE_COLUMN_FAMILY_WITH_INVALID_NAME

[SQLSTATE: 42802](sql-error-conditions-sqlstates.html#class-42-syntax-error-or-access-rule-violation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,18 @@ public class TimeoutMode {
public static final TimeoutMode NoTimeouts() {
return NoTimeouts$.MODULE$;
}

/**
* Stateful processor that only registers processing time based timers
*/
public static final TimeoutMode ProcessingTime() {
return ProcessingTime$.MODULE$;
}

/**
* Stateful processor that only registers event time based timers
*/
public static final TimeoutMode EventTime() {
return EventTime$.MODULE$;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ package org.apache.spark.sql.catalyst.plans.logical

import org.apache.spark.sql.streaming.TimeoutMode

/** Types of timeouts used in tranformWithState operator */
/** Types of timeouts used in transformWithState operator */
case object NoTimeouts extends TimeoutMode
case object ProcessingTime extends TimeoutMode
case object EventTime extends TimeoutMode
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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

import java.io.Serializable

import org.apache.spark.annotation.{Evolving, Experimental}

/**
* Class used to provide access to expired timer's expiry time. These values
* are only relevant if the ExpiredTimerInfo is valid.
*/
@Experimental
@Evolving
private[sql] trait ExpiredTimerInfo extends Serializable {
/**
* Check if provided ExpiredTimerInfo is valid.
*/
def isValid(): Boolean

/**
* Get the expired timer's expiry time as milliseconds in epoch time.
*/
def getExpiryTimeInMs(): Long
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ private[sql] trait StatefulProcessor[K, I, O] extends Serializable {
* Function that will be invoked as the first method that allows for users to
* initialize all their state variables and perform other init actions before handling data.
* @param outputMode - output mode for the stateful processor
* @param timeoutMode - timeout mode for the stateful processor
*/
def init(outputMode: OutputMode): Unit
def init(
outputMode: OutputMode,
timeoutMode: TimeoutMode): Unit

/**
* Function that will allow users to interact with input data rows along with the grouping key
Expand All @@ -50,12 +53,15 @@ private[sql] trait StatefulProcessor[K, I, O] extends Serializable {
* @param inputRows - iterator of input rows associated with grouping key
* @param timerValues - instance of TimerValues that provides access to current processing/event
* time if available
* @param expiredTimerInfo - instance of ExpiredTimerInfo that provides access to expired timer
* if applicable
* @return - Zero or more output rows
*/
def handleInputRows(
key: K,
inputRows: Iterator[I],
timerValues: TimerValues): Iterator[O]
timerValues: TimerValues,
expiredTimerInfo: ExpiredTimerInfo): Iterator[O]
Comment thread
anishshri-db marked this conversation as resolved.

/**
* Function called as the last method that allows for users to perform
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,29 @@ private[sql] trait StatefulProcessorHandle extends Serializable {
/** Function to return queryInfo for currently running task */
def getQueryInfo(): QueryInfo

/**
* Function to register a processing/event time based timer for given implicit grouping key
* and provided timestamp
* @param expiryTimestampMs - timer expiry timestamp in milliseconds
*/
def registerTimer(expiryTimestampMs: Long): Unit

/**
* Function to delete a processing/event time based timer for given implicit grouping key
* and provided timestamp
* @param expiryTimestampMs - timer expiry timestamp in milliseconds
*/
def deleteTimer(expiryTimestampMs: Long): Unit

/**
* Function to list all the timers registered for given implicit grouping key
* Note: calling listTimers() within the `handleInputRows` method of the StatefulProcessor
* will return all the unprocessed registered timers, including the one being fired within the
* invocation of `handleInputRows`.
* @return - list of all the registered timers for given implicit grouping key
*/
def listTimers(): Iterator[Long]

/**
* Function to delete and purge state variable if defined previously
* @param stateName - name of the state variable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.execution.streaming

import org.apache.spark.sql.streaming.{ExpiredTimerInfo, TimeoutMode}

/**
* Class that provides a concrete implementation that can be used to provide access to expired
* timer's expiry time. These values are only relevant if the ExpiredTimerInfo
* is valid.
* @param isValid - boolean to check if the provided ExpiredTimerInfo is valid
* @param expiryTimeInMsOpt - option to expired timer's expiry time as milliseconds in epoch time
*/
class ExpiredTimerInfoImpl(
isValid: Boolean,
expiryTimeInMsOpt: Option[Long] = None,
timeoutMode: TimeoutMode = TimeoutMode.NoTimeouts()) extends ExpiredTimerInfo {

override def isValid(): Boolean = isValid

override def getExpiryTimeInMs(): Long = expiryTimeInMsOpt.getOrElse(-1L)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import org.apache.spark.TaskContext
import org.apache.spark.internal.Logging
import org.apache.spark.sql.Encoder
import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder
import org.apache.spark.sql.execution.streaming.state.StateStore
import org.apache.spark.sql.streaming.{ListState, MapState, QueryInfo, StatefulProcessorHandle, ValueState}
import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.execution.streaming.state._
import org.apache.spark.sql.streaming.{ListState, MapState, QueryInfo, StatefulProcessorHandle, TimeoutMode, ValueState}
import org.apache.spark.util.Utils

/**
Expand All @@ -45,7 +46,7 @@ object ImplicitGroupingKeyTracker {
*/
object StatefulProcessorHandleState extends Enumeration {
type StatefulProcessorHandleState = Value
val CREATED, INITIALIZED, DATA_PROCESSED, CLOSED = Value
val CREATED, INITIALIZED, DATA_PROCESSED, TIMER_PROCESSED, CLOSED = Value
}

class QueryInfoImpl(
Expand Down Expand Up @@ -76,6 +77,7 @@ class StatefulProcessorHandleImpl(
store: StateStore,
runId: UUID,
keyEncoder: ExpressionEncoder[Any],
timeoutMode: TimeoutMode,
isStreaming: Boolean = true)
extends StatefulProcessorHandle with Logging {
import StatefulProcessorHandleState._
Expand Down Expand Up @@ -114,28 +116,85 @@ class StatefulProcessorHandleImpl(
def getHandleState: StatefulProcessorHandleState = currState

override def getValueState[T](stateName: String, valEncoder: Encoder[T]): ValueState[T] = {
verify(currState == CREATED, s"Cannot create state variable with name=$stateName after " +
"initialization is complete")
verifyStateVarOperations("get_value_state")
val resultState = new ValueStateImpl[T](store, stateName, keyEncoder, valEncoder)
resultState
}

override def getQueryInfo(): QueryInfo = currQueryInfo

private lazy val timerState = new TimerStateImpl(store, timeoutMode, keyEncoder)

private def verifyStateVarOperations(operationType: String): Unit = {
if (currState != CREATED) {
throw StateStoreErrors.cannotPerformOperationWithInvalidHandleState(operationType,
currState.toString)
}
}

private def verifyTimerOperations(operationType: String): Unit = {
if (timeoutMode == NoTimeouts) {
throw StateStoreErrors.cannotPerformOperationWithInvalidTimeoutMode(operationType,
timeoutMode.toString)
}

if (currState < INITIALIZED || currState >= TIMER_PROCESSED) {
throw StateStoreErrors.cannotPerformOperationWithInvalidHandleState(operationType,
currState.toString)
}
}

/**
* Function to register a timer for the given expiryTimestampMs
* @param expiryTimestampMs - timestamp in milliseconds for the timer to expire
*/
override def registerTimer(expiryTimestampMs: Long): Unit = {
verifyTimerOperations("register_timer")
timerState.registerTimer(expiryTimestampMs)
}

/**
* Function to delete a timer for the given expiryTimestampMs
* @param expiryTimestampMs - timestamp in milliseconds for the timer to delete
*/
override def deleteTimer(expiryTimestampMs: Long): Unit = {
verifyTimerOperations("delete_timer")
timerState.deleteTimer(expiryTimestampMs)
}

/**
* Function to retrieve all registered timers for all grouping keys
* @return - iterator of registered timers for all grouping keys
*/
def getExpiredTimers(): Iterator[(Any, Long)] = {
verifyTimerOperations("get_expired_timers")
timerState.getExpiredTimers()
}

/**
* Function to list all the registered timers for given implicit key
* Note: calling listTimers() within the `handleInputRows` method of the StatefulProcessor
* will return all the unprocessed registered timers, including the one being fired within the
* invocation of `handleInputRows`.
* @return - iterator of all the registered timers for given implicit key
*/
def listTimers(): Iterator[Long] = {
verifyTimerOperations("list_timers")
timerState.listTimers()
}

/**
* Function to delete and purge state variable if defined previously
*
* @param stateName - name of the state variable
*/
override def deleteIfExists(stateName: String): Unit = {
verify(currState == CREATED, s"Cannot delete state variable with name=$stateName after " +
"initialization is complete")
verifyStateVarOperations("delete_if_exists")
store.removeColFamilyIfExists(stateName)
}

override def getListState[T](stateName: String, valEncoder: Encoder[T]): ListState[T] = {
verify(currState == CREATED, s"Cannot create state variable with name=$stateName after " +
"initialization is complete")
verifyStateVarOperations("get_list_state")
val resultState = new ListStateImpl[T](store, stateName, keyEncoder, valEncoder)
resultState
}
Expand All @@ -144,8 +203,7 @@ class StatefulProcessorHandleImpl(
stateName: String,
userKeyEnc: Encoder[K],
valEncoder: Encoder[V]): MapState[K, V] = {
verify(currState == CREATED, s"Cannot create state variable with name=$stateName after " +
"initialization is complete")
verifyStateVarOperations("get_map_state")
val resultState = new MapStateImpl[K, V](store, stateName, keyEncoder, userKeyEnc, valEncoder)
resultState
}
Expand Down
Loading