Skip to content

Commit

Permalink
[SPARK-45582][SS] Ensure that store instance is not used after callin…
Browse files Browse the repository at this point in the history
…g commit within output mode streaming aggregation

### What changes were proposed in this pull request?
Ensure that store instance is not used after calling commit within output mode streaming aggregation

### Why are the changes needed?
Without these changes, we were accessing the store instance to retrieve the iterator even after the commit was called. When commit is called, we release the DB instance lock. So its possible task retries can acquire the instance lock and close the DB instance. So when the original thread tries to access the DB, it might run into a null pointer exception. This change fixes the issue

```
        org.apache.spark.sql.streaming.StreamingQueryException: Job aborted due to stage failure: Task 0 in stage 5.0 failed 1 times, most recent failure: Lost task 0.0 in stage 5.0 (TID 492) (ip-10-110-25-116.us-west-2.compute.internal executor driver): java.lang.NullPointerException
	at org.apache.spark.sql.execution.streaming.state.RocksDB.iterator(RocksDB.scala:337)
	at org.apache.spark.sql.execution.streaming.state.RocksDBStateStoreProvider$RocksDBStateStore.iterator(RocksDBStateStoreProvider.scala:79)
	at org.apache.spark.sql.execution.streaming.state.StreamingAggregationStateManagerImplV1.values(StreamingAggregationStateManager.scala:130)
	at org.apache.spark.sql.execution.streaming.StateStoreSaveExec.$anonfun$doExecute$5(statefulOperators.scala:543)
	at org.apache.spark.sql.execution.streaming.state.package$StateStoreOps.$anonfun$mapPartitionsWithStateStore$1(package.scala:63)
	at org.apache.spark.sql.execution.streaming.state.StateStoreRDD.compute(StateStoreRDD.scala:131)
	at org.apache.spark.rdd.RDD.$anonfun$computeOrReadCheckpoint$1(RDD.scala:407)
	at com.databricks.spark.util.ExecutorFrameProfiler$.record(ExecutorFrameProfiler.scala:110)
	at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:404)
	at org.apache.spark.rdd.RDD.iterator(RDD.scala:371)
```

### Does this PR introduce _any_ user-facing change?
No

### How was this patch tested?
RocksDBStreamingAggregationSuite
```
18:12:00.242 WARN org.apache.spark.sql.streaming.RocksDBStateStoreStreamingAggregationSuite:

===== POSSIBLE THREAD LEAK IN SUITE o.a.s.sql.streaming.RocksDBStateStoreStreamingAggregationSuite, threads: ForkJoinPool.commonPool-worker-6 (daemon=true), ForkJoinPool.commonPool-worker-4 (daemon=true), ForkJoinPool.commonPool-worker-7 (daemon=true), ForkJoinPool.commonPool-worker-5 (daemon=true), ForkJoinPool.commonPool-worker-3 (daemon=true), state-store-maintenance-task (daemon=true), rpc-boss-3-1 (daemon=true), ForkJoinPool.commonPool-worker-8 (daemon=true), shuffle-boss-6-1 (daemon=true), ForkJoinP...
[info] Run completed in 5 minutes, 8 seconds.
[info] Total number of tests run: 80
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 80, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
```

StreamingSessionWindowSuite
```
===== POSSIBLE THREAD LEAK IN SUITE o.a.s.sql.streaming.StreamingSessionWindowSuite, threads: ForkJoinPool.commonPool-worker-6 (daemon=true), state-store-maintenance-thread-0 (daemon=true), ForkJoinPool.commonPool-worker-4 (daemon=true), state-store-maintenance-thread-1 (daemon=true), ForkJoinPool.commonPool-worker-7 (daemon=true), ForkJoinPool.commonPool-worker-5 (daemon=true), ForkJoinPool.commonPool-worker-3 (daemon=true), state-store-maintenance-task (daemon=true), rpc-boss-3-1 (daemon=true), ForkJoin...
[info] Run completed in 3 minutes, 38 seconds.
[info] Total number of tests run: 48
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 48, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
```

### Was this patch authored or co-authored using generative AI tooling?
No

Closes #43413 from anishshri-db/task/SPARK-45582.

Authored-by: Anish Shrigondekar <anish.shrigondekar@databricks.com>
Signed-off-by: Jungtaek Lim <kabhwan.opensource@gmail.com>
  • Loading branch information
anishshri-db authored and HeartSaVioR committed Oct 18, 2023
1 parent 7e82e1b commit b42ac52
Showing 1 changed file with 48 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -515,15 +515,31 @@ case class StateStoreSaveExec(
numUpdatedStateRows += 1
}
}
allRemovalsTimeMs += 0
commitTimeMs += timeTakenMs {
stateManager.commit(store)
}
setStoreMetrics(store)
setOperatorMetrics()
stateManager.values(store).map { valueRow =>
numOutputRows += 1
valueRow

// SPARK-45582 - Ensure that store instance is not used after commit is called
// to invoke the iterator.
val rangeIter = stateManager.values(store)

new NextIterator[InternalRow] {
override protected def getNext(): InternalRow = {
if (rangeIter.hasNext) {
val valueRow = rangeIter.next()
numOutputRows += 1
valueRow
} else {
finished = true
null
}
}

override protected def close(): Unit = {
allRemovalsTimeMs += 0
commitTimeMs += timeTakenMs {
stateManager.commit(store)
}
setStoreMetrics(store)
setOperatorMetrics()
}
}

// Update and output only rows being evicted from the StateStore
Expand Down Expand Up @@ -782,13 +798,29 @@ case class SessionWindowStateStoreSaveExec(
allUpdatesTimeMs += timeTakenMs {
putToStore(iter, store)
}
commitTimeMs += timeTakenMs {
stateManager.commit(store)
}
setStoreMetrics(store)
stateManager.iterator(store).map { row =>
numOutputRows += 1
row

// SPARK-45582 - Ensure that store instance is not used after commit is called
// to invoke the iterator.
val rangeIter = stateManager.iterator(store)

new NextIterator[InternalRow] {
override protected def getNext(): InternalRow = {
if (rangeIter.hasNext) {
val valueRow = rangeIter.next()
numOutputRows += 1
valueRow
} else {
finished = true
null
}
}

override protected def close(): Unit = {
commitTimeMs += timeTakenMs {
stateManager.commit(store)
}
setStoreMetrics(store)
}
}

// Update and output only rows being evicted from the StateStore
Expand Down

0 comments on commit b42ac52

Please sign in to comment.