You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "dylanwong250 (via GitHub)" <gi...@apache.org> on 2025/08/04 18:11:52 UTC

[PR] [SPARK-53103][SS] Throw an error if state directory is not empty on batch 0 [spark]

dylanwong250 opened a new pull request, #51817:
URL: https://github.com/apache/spark/pull/51817

   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a faster review.
     7. If you want to add a new configuration, please read the guideline first for naming configurations in
        'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
     8. If you want to add or modify an error type or message, please read the guideline first in
        'common/utils/src/main/resources/error/README.md'.
   -->
   
   ### What changes were proposed in this pull request?
   <!--
   Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as new features, bug fixes, or other behavior changes. Documentation-only updates are not considered user-facing changes.
   
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
   -->
   
   
   ### Was this patch authored or co-authored using generative AI tooling?
   <!--
   If generative AI tooling has been used in the process of authoring this patch, please include the
   phrase: 'Generated-by: ' followed by the name of the tool and its version.
   If no, write 'No'.
   Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty on batch 0 [spark]

Posted by "anishshri-db (via GitHub)" <gi...@apache.org>.
anishshri-db commented on code in PR #51817:
URL: https://github.com/apache/spark/pull/51817#discussion_r2277908947


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/ResolveWriteToStream.scala:
##########
@@ -107,6 +109,24 @@ object ResolveWriteToStream extends Rule[LogicalPlan] {
       }
     }
 
+    // Check to see if we can use this checkpoint location for state.
+    val offsetsCheckpointPath = new Path(checkpointLocation, DIR_NAME_OFFSETS)
+    if (!fileManager.exists(offsetsCheckpointPath)
+      || fileManager.list(offsetsCheckpointPath).isEmpty) {

Review Comment:
   Can we do this check only for the first batch to avoid listing for each batch ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty when query starts [spark]

Posted by "anishshri-db (via GitHub)" <gi...@apache.org>.
anishshri-db commented on code in PR #51817:
URL: https://github.com/apache/spark/pull/51817#discussion_r2289008333


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -2687,6 +2687,15 @@ object SQLConf {
       .intConf
       .createWithDefault(16)
 
+  val STREAMING_VERIFY_CHECKPOINT_DIRECTORY_EMPTY_ON_START =
+    buildConf("spark.sql.streaming.verifyCheckpointDirectoryEmptyOnStart")
+      .doc("When true, verifies that the checkpoint directory (offsets, state, commits) is " +

Review Comment:
   Can we make this `internal` please



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty on batch 0 [spark]

Posted by "dylanwong250 (via GitHub)" <gi...@apache.org>.
dylanwong250 commented on code in PR #51817:
URL: https://github.com/apache/spark/pull/51817#discussion_r2279699302


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/MicroBatchExecution.scala:
##########
@@ -341,6 +344,7 @@ class MicroBatchExecution(
     setLatestExecutionContext(execCtx)
 
     populateStartOffsets(execCtx, sparkSessionForStream)
+

Review Comment:
   fixed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty on batch 0 [spark]

Posted by "dylanwong250 (via GitHub)" <gi...@apache.org>.
dylanwong250 commented on PR #51817:
URL: https://github.com/apache/spark/pull/51817#issuecomment-3193008984

   > @dylanwong250 - could you pls check if the CI failures are related ? Thanks
   
   They are related. Something to do with adding the offset directory checks. Taking a further look.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty on batch 0 [spark]

Posted by "ericm-db (via GitHub)" <gi...@apache.org>.
ericm-db commented on code in PR #51817:
URL: https://github.com/apache/spark/pull/51817#discussion_r2267446792


##########
common/utils/src/main/resources/error/error-conditions.json:
##########
@@ -5149,6 +5149,13 @@
     ],
     "sqlState" : "42802"
   },
+  "STATE_STORE_CHECKPOINT_LOCATION_NOT_EMPTY" : {
+    "message" : [
+      "The offset log does not exist, but the state checkpoint location <checkpointLocation> is not empty.",

Review Comment:
   I guess what you currently have is the most accurate error message we can throw, not sure it needs changing



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty on batch 0 [spark]

Posted by "anishshri-db (via GitHub)" <gi...@apache.org>.
anishshri-db commented on code in PR #51817:
URL: https://github.com/apache/spark/pull/51817#discussion_r2279691893


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/MicroBatchExecution.scala:
##########
@@ -341,6 +344,7 @@ class MicroBatchExecution(
     setLatestExecutionContext(execCtx)
 
     populateStartOffsets(execCtx, sparkSessionForStream)
+

Review Comment:
   nit: intentional ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty on batch 0 [spark]

Posted by "ericm-db (via GitHub)" <gi...@apache.org>.
ericm-db commented on code in PR #51817:
URL: https://github.com/apache/spark/pull/51817#discussion_r2267446792


##########
common/utils/src/main/resources/error/error-conditions.json:
##########
@@ -5149,6 +5149,13 @@
     ],
     "sqlState" : "42802"
   },
+  "STATE_STORE_CHECKPOINT_LOCATION_NOT_EMPTY" : {
+    "message" : [
+      "The offset log does not exist, but the state checkpoint location <checkpointLocation> is not empty.",

Review Comment:
   I guess this is the most accurate error message we can throw, not sure it needs changing



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty on batch 0 [spark]

Posted by "anishshri-db (via GitHub)" <gi...@apache.org>.
anishshri-db commented on code in PR #51817:
URL: https://github.com/apache/spark/pull/51817#discussion_r2277856794


##########
sql/core/src/test/scala/org/apache/spark/sql/streaming/StreamingQuerySuite.scala:
##########
@@ -1475,6 +1476,70 @@ class StreamingQuerySuite extends StreamTest with BeforeAndAfter with Logging wi
     )
   }
 
+  test("SPARK-53103: non empty state checkpoint directory on first batch") {
+    withTempDir { checkpointDir =>
+      val q = MemoryStream[Int].toDS().groupBy().count()

Review Comment:
   Can we add tests for both HDFS and RocksDB provider ?



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/ResolveWriteToStream.scala:
##########
@@ -107,6 +109,25 @@ object ResolveWriteToStream extends Rule[LogicalPlan] {
       }
     }
 
+    // Check to see if we can use this checkpoint location for state.
+    val offsetsCheckpointPath = new Path(checkpointLocation, DIR_NAME_OFFSETS)
+    if (!fileManager.exists(offsetsCheckpointPath)
+      || fileManager.list(offsetsCheckpointPath).isEmpty) {
+
+      // If state or commits has been created, but offsets have not, that means there could be
+      // multiple writers to the state or commits directory of the query.
+      val dirNamesThatShouldNotHaveFiles = Array[String](DIR_NAME_STATE, DIR_NAME_COMMITS)
+
+      dirNamesThatShouldNotHaveFiles.foreach { dirName =>
+        val path = new Path(checkpointLocation, dirName)
+        if (fileManager.exists(path) && !fileManager.list(path).isEmpty) {
+

Review Comment:
   nit: extra newline ?



##########
sql/core/src/test/scala/org/apache/spark/sql/streaming/StreamingQuerySuite.scala:
##########
@@ -1475,6 +1476,70 @@ class StreamingQuerySuite extends StreamTest with BeforeAndAfter with Logging wi
     )
   }
 
+  test("SPARK-53103: non empty state checkpoint directory on first batch") {

Review Comment:
   Do we also have a test for non empty commit dir ?



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/ResolveWriteToStream.scala:
##########
@@ -107,6 +109,20 @@ object ResolveWriteToStream extends Rule[LogicalPlan] {
       }
     }
 
+    // Check to see if we can use this checkpoint location for state.
+    val offsetsCheckpointPath = new Path(checkpointLocation, DIR_NAME_OFFSETS)
+    if (!fileManager.exists(offsetsCheckpointPath)
+      || fileManager.list(offsetsCheckpointPath).isEmpty) {
+      val stateCheckpointPath = new Path(checkpointLocation, DIR_NAME_STATE)
+      if (fileManager.exists(stateCheckpointPath)
+        && !fileManager.list(stateCheckpointPath).isEmpty) {
+        // If state has been created, but offsets have not, that means there could be multiple
+        // writers to the state directory of the query.
+        val loc = stateCheckpointPath.toString
+        throw StateStoreErrors.streamingStateCheckpointLocationNotEmpty(loc)
+      }
+    }

Review Comment:
   No dont do metadata. We only store stuff like the queryId there - `commit` and `state` is fine for now



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty when query starts [spark]

Posted by "anishshri-db (via GitHub)" <gi...@apache.org>.
anishshri-db commented on code in PR #51817:
URL: https://github.com/apache/spark/pull/51817#discussion_r2291573478


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/state/StateStoreErrors.scala:
##########
@@ -187,6 +187,11 @@ object StateStoreErrors {
       numSchemaFiles, schemaFilesThreshold, addedColFamilies, removedColFamilies)
   }
 
+  def streamingStateCheckpointLocationNotEmpty(checkpointLocation: String):
+  StateStoreCheckpointLocationNotEmpty = {

Review Comment:
   nit: indent seems off ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty on batch 0 [spark]

Posted by "dylanwong250 (via GitHub)" <gi...@apache.org>.
dylanwong250 commented on code in PR #51817:
URL: https://github.com/apache/spark/pull/51817#discussion_r2277938163


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/ResolveWriteToStream.scala:
##########
@@ -107,6 +109,24 @@ object ResolveWriteToStream extends Rule[LogicalPlan] {
       }
     }
 
+    // Check to see if we can use this checkpoint location for state.
+    val offsetsCheckpointPath = new Path(checkpointLocation, DIR_NAME_OFFSETS)
+    if (!fileManager.exists(offsetsCheckpointPath)
+      || fileManager.list(offsetsCheckpointPath).isEmpty) {

Review Comment:
   I changed it to not list files in the offsets directory and only check if the directory exists. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty when query starts [spark]

Posted by "dylanwong250 (via GitHub)" <gi...@apache.org>.
dylanwong250 commented on code in PR #51817:
URL: https://github.com/apache/spark/pull/51817#discussion_r2288961593


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/MicroBatchExecution.scala:
##########
@@ -562,12 +565,36 @@ class MicroBatchExecution(
           log"offsets ${MDC(LogKeys.STREAMING_OFFSETS_START, execCtx.startOffsets)} and " +
           log"available offsets ${MDC(LogKeys.STREAMING_OFFSETS_END, execCtx.endOffsets)}")
       case None => // We are starting this stream for the first time.
+        verifyStartCheckpointDirectory()

Review Comment:
   I think this is a good idea. I added STREAMING_VERIFY_CHECKPOINT_DIRECTORY_EMPTY_ON_START.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty on batch 0 [spark]

Posted by "liviazhu (via GitHub)" <gi...@apache.org>.
liviazhu commented on code in PR #51817:
URL: https://github.com/apache/spark/pull/51817#discussion_r2268043816


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/ResolveWriteToStream.scala:
##########
@@ -107,6 +109,20 @@ object ResolveWriteToStream extends Rule[LogicalPlan] {
       }
     }
 
+    // Check to see if we can use this checkpoint location for state.
+    val offsetsCheckpointPath = new Path(checkpointLocation, DIR_NAME_OFFSETS)
+    if (!fileManager.exists(offsetsCheckpointPath)
+      || fileManager.list(offsetsCheckpointPath).isEmpty) {
+      val stateCheckpointPath = new Path(checkpointLocation, DIR_NAME_STATE)
+      if (fileManager.exists(stateCheckpointPath)
+        && !fileManager.list(stateCheckpointPath).isEmpty) {
+        // If state has been created, but offsets have not, that means there could be multiple
+        // writers to the state directory of the query.
+        val loc = stateCheckpointPath.toString
+        throw StateStoreErrors.streamingStateCheckpointLocationNotEmpty(loc)
+      }
+    }

Review Comment:
   Should we also add a quick check that commits dir is empty if offsets dir is empty too? @ericm-db 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty on batch 0 [spark]

Posted by "ericm-db (via GitHub)" <gi...@apache.org>.
ericm-db commented on code in PR #51817:
URL: https://github.com/apache/spark/pull/51817#discussion_r2268084518


##########
common/utils/src/main/resources/error/error-conditions.json:
##########
@@ -5149,6 +5149,13 @@
     ],
     "sqlState" : "42802"
   },
+  "STATE_STORE_CHECKPOINT_LOCATION_NOT_EMPTY" : {
+    "message" : [
+      "The offset log does not exist, but the state checkpoint location <checkpointLocation> is not empty.",
+      "Please either use a new checkpoint location, or delete the existing data in the checkpoint location."
+    ],
+    "sqlState" : "42K06"

Review Comment:
   I don't really think this is an invalid type for options, more that the offset log is missing, or that state directory is incorrectly populated. I think 42K03 might be more appropriate here? What do you think?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty on batch 0 [spark]

Posted by "dylanwong250 (via GitHub)" <gi...@apache.org>.
dylanwong250 commented on code in PR #51817:
URL: https://github.com/apache/spark/pull/51817#discussion_r2277904284


##########
sql/core/src/test/scala/org/apache/spark/sql/streaming/StreamingQuerySuite.scala:
##########
@@ -1475,6 +1476,70 @@ class StreamingQuerySuite extends StreamTest with BeforeAndAfter with Logging wi
     )
   }
 
+  test("SPARK-53103: non empty state checkpoint directory on first batch") {

Review Comment:
   I had a test but forgot to rename the test. Renamed the test to mention commits also.



##########
sql/core/src/test/scala/org/apache/spark/sql/streaming/StreamingQuerySuite.scala:
##########
@@ -1475,6 +1476,70 @@ class StreamingQuerySuite extends StreamTest with BeforeAndAfter with Logging wi
     )
   }
 
+  test("SPARK-53103: non empty state checkpoint directory on first batch") {
+    withTempDir { checkpointDir =>
+      val q = MemoryStream[Int].toDS().groupBy().count()

Review Comment:
   Done.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty on batch 0 [spark]

Posted by "dylanwong250 (via GitHub)" <gi...@apache.org>.
dylanwong250 commented on code in PR #51817:
URL: https://github.com/apache/spark/pull/51817#discussion_r2267543101


##########
common/utils/src/main/resources/error/error-conditions.json:
##########
@@ -5149,6 +5149,13 @@
     ],
     "sqlState" : "42802"
   },
+  "STATE_STORE_CHECKPOINT_LOCATION_NOT_EMPTY" : {
+    "message" : [
+      "The offset log does not exist, but the state checkpoint location <checkpointLocation> is not empty.",
+      "Please either use a new checkpoint location, or delete the existing data in the checkpoint location."
+    ],
+    "sqlState" : "42K06"

Review Comment:
   This one is ```Invalid type for options```. https://spark.apache.org/docs/3.5.3/sql-error-conditions-sqlstates.html. It seems like the most accurate to me in relation to the existing state store errors.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty on batch 0 [spark]

Posted by "micheal-o (via GitHub)" <gi...@apache.org>.
micheal-o commented on code in PR #51817:
URL: https://github.com/apache/spark/pull/51817#discussion_r2284035445


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/MicroBatchExecution.scala:
##########
@@ -562,12 +565,36 @@ class MicroBatchExecution(
           log"offsets ${MDC(LogKeys.STREAMING_OFFSETS_START, execCtx.startOffsets)} and " +
           log"available offsets ${MDC(LogKeys.STREAMING_OFFSETS_END, execCtx.endOffsets)}")
       case None => // We are starting this stream for the first time.
+        verifyStartCheckpointDirectory()
         logInfo(s"Starting new streaming query.")
         execCtx.batchId = 0
         watermarkTracker = WatermarkTracker(sparkSessionToRunBatches.conf, logicalPlan)
     }
   }
 
+  /**
+   * Verify that the checkpoint directory is in a good state to start a new
+   * streaming query. This checks that the offsets, state, commits directories are
+   * either non-existent or empty.
+   *
+   * If this check fails, an exception is thrown.
+   */
+  private def verifyStartCheckpointDirectory(): Unit = {

Review Comment:
   nit: `verifyNewCheckpointDirectory()`



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/MicroBatchExecution.scala:
##########
@@ -562,12 +565,36 @@ class MicroBatchExecution(
           log"offsets ${MDC(LogKeys.STREAMING_OFFSETS_START, execCtx.startOffsets)} and " +
           log"available offsets ${MDC(LogKeys.STREAMING_OFFSETS_END, execCtx.endOffsets)}")
       case None => // We are starting this stream for the first time.
+        verifyStartCheckpointDirectory()

Review Comment:
   @anishshri-db @dylanwong250 should we add a conf for this, just in case? Conf can be enabled by default.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty on batch 0 [spark]

Posted by "micheal-o (via GitHub)" <gi...@apache.org>.
micheal-o commented on code in PR #51817:
URL: https://github.com/apache/spark/pull/51817#discussion_r2284047739


##########
common/utils/src/main/resources/error/error-conditions.json:
##########
@@ -5149,6 +5149,13 @@
     ],
     "sqlState" : "42802"
   },
+  "STATE_STORE_CHECKPOINT_LOCATION_NOT_EMPTY" : {
+    "message" : [
+      "The offset log does not exist, but the state checkpoint location <checkpointLocation> is not empty.",

Review Comment:
   @ericm-db @dylanwong250 one option is to say, `should be empty for a new streaming query (i.e. batch 0)`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty on batch 0 [spark]

Posted by "anishshri-db (via GitHub)" <gi...@apache.org>.
anishshri-db commented on PR #51817:
URL: https://github.com/apache/spark/pull/51817#issuecomment-3193005265

   @dylanwong250 - could you pls check if the CI failures are related ? Thanks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty on batch 0 [spark]

Posted by "ericm-db (via GitHub)" <gi...@apache.org>.
ericm-db commented on code in PR #51817:
URL: https://github.com/apache/spark/pull/51817#discussion_r2268082538


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/ResolveWriteToStream.scala:
##########
@@ -107,6 +109,20 @@ object ResolveWriteToStream extends Rule[LogicalPlan] {
       }
     }
 
+    // Check to see if we can use this checkpoint location for state.
+    val offsetsCheckpointPath = new Path(checkpointLocation, DIR_NAME_OFFSETS)
+    if (!fileManager.exists(offsetsCheckpointPath)
+      || fileManager.list(offsetsCheckpointPath).isEmpty) {
+      val stateCheckpointPath = new Path(checkpointLocation, DIR_NAME_STATE)
+      if (fileManager.exists(stateCheckpointPath)
+        && !fileManager.list(stateCheckpointPath).isEmpty) {
+        // If state has been created, but offsets have not, that means there could be multiple
+        // writers to the state directory of the query.
+        val loc = stateCheckpointPath.toString
+        throw StateStoreErrors.streamingStateCheckpointLocationNotEmpty(loc)
+      }
+    }

Review Comment:
   Yeah that would be good too.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty on batch 0 [spark]

Posted by "dylanwong250 (via GitHub)" <gi...@apache.org>.
dylanwong250 commented on code in PR #51817:
URL: https://github.com/apache/spark/pull/51817#discussion_r2270508207


##########
common/utils/src/main/resources/error/error-conditions.json:
##########
@@ -5149,6 +5149,13 @@
     ],
     "sqlState" : "42802"
   },
+  "STATE_STORE_CHECKPOINT_LOCATION_NOT_EMPTY" : {
+    "message" : [
+      "The offset log does not exist, but the state checkpoint location <checkpointLocation> is not empty.",
+      "Please either use a new checkpoint location, or delete the existing data in the checkpoint location."
+    ],
+    "sqlState" : "42K06"

Review Comment:
   Changed to 42K03.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty on batch 0 [spark]

Posted by "dylanwong250 (via GitHub)" <gi...@apache.org>.
dylanwong250 commented on code in PR #51817:
URL: https://github.com/apache/spark/pull/51817#discussion_r2279453497


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/ResolveWriteToStream.scala:
##########
@@ -107,6 +109,24 @@ object ResolveWriteToStream extends Rule[LogicalPlan] {
       }
     }
 
+    // Check to see if we can use this checkpoint location for state.
+    val offsetsCheckpointPath = new Path(checkpointLocation, DIR_NAME_OFFSETS)
+    if (!fileManager.exists(offsetsCheckpointPath)
+      || fileManager.list(offsetsCheckpointPath).isEmpty) {

Review Comment:
   Discussed offline. Moved these checks into MicroBatchExecution on query start. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty on batch 0 [spark]

Posted by "dylanwong250 (via GitHub)" <gi...@apache.org>.
dylanwong250 commented on code in PR #51817:
URL: https://github.com/apache/spark/pull/51817#discussion_r2267596324


##########
sql/core/src/test/scala/org/apache/spark/sql/streaming/StreamingQuerySuite.scala:
##########
@@ -1473,6 +1474,45 @@ class StreamingQuerySuite extends StreamTest with BeforeAndAfter with Logging wi
     )
   }
 
+  test("SPARK-53103: non empty state checkpoint directory on first batch") {
+    withTempDir { checkpointDir =>
+      val q = MemoryStream[Int].toDS().groupBy().count()
+        .writeStream
+        .format("memory")
+        .outputMode("complete")
+        .queryName(s"name${RandomStringUtils.secure.nextAlphabetic(10)}")

Review Comment:
   I was following a similar pattern to the other exception tests but I tested without and get the error ```org.apache.spark.sql.AnalysisException: queryName must be specified for memory sink.```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty when query starts [spark]

Posted by "anishshri-db (via GitHub)" <gi...@apache.org>.
anishshri-db closed pull request #51817: [SPARK-53103][SS] Throw an error if state directory is not empty when query starts
URL: https://github.com/apache/spark/pull/51817


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty on batch 0 [spark]

Posted by "dylanwong250 (via GitHub)" <gi...@apache.org>.
dylanwong250 commented on code in PR #51817:
URL: https://github.com/apache/spark/pull/51817#discussion_r2270511438


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/ResolveWriteToStream.scala:
##########
@@ -107,6 +109,20 @@ object ResolveWriteToStream extends Rule[LogicalPlan] {
       }
     }
 
+    // Check to see if we can use this checkpoint location for state.
+    val offsetsCheckpointPath = new Path(checkpointLocation, DIR_NAME_OFFSETS)
+    if (!fileManager.exists(offsetsCheckpointPath)
+      || fileManager.list(offsetsCheckpointPath).isEmpty) {
+      val stateCheckpointPath = new Path(checkpointLocation, DIR_NAME_STATE)
+      if (fileManager.exists(stateCheckpointPath)
+        && !fileManager.list(stateCheckpointPath).isEmpty) {
+        // If state has been created, but offsets have not, that means there could be multiple
+        // writers to the state directory of the query.
+        val loc = stateCheckpointPath.toString
+        throw StateStoreErrors.streamingStateCheckpointLocationNotEmpty(loc)
+      }
+    }

Review Comment:
   Added a check and unit test for the commits directory. Should we also do metadata? I am not too familiar with the metadata directory.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty on batch 0 [spark]

Posted by "ericm-db (via GitHub)" <gi...@apache.org>.
ericm-db commented on code in PR #51817:
URL: https://github.com/apache/spark/pull/51817#discussion_r2267417446


##########
common/utils/src/main/resources/error/error-conditions.json:
##########
@@ -5149,6 +5149,13 @@
     ],
     "sqlState" : "42802"
   },
+  "STATE_STORE_CHECKPOINT_LOCATION_NOT_EMPTY" : {
+    "message" : [
+      "The offset log does not exist, but the state checkpoint location <checkpointLocation> is not empty.",
+      "Please either use a new checkpoint location, or delete the existing data in the checkpoint location."
+    ],
+    "sqlState" : "42K06"

Review Comment:
   What is this SQL state? Just want to confirm that this is the best one.



##########
common/utils/src/main/resources/error/error-conditions.json:
##########
@@ -5149,6 +5149,13 @@
     ],
     "sqlState" : "42802"
   },
+  "STATE_STORE_CHECKPOINT_LOCATION_NOT_EMPTY" : {
+    "message" : [
+      "The offset log does not exist, but the state checkpoint location <checkpointLocation> is not empty.",

Review Comment:
   I think we can modify this exception message,
   `"This is a new query with a non-empty checkpoint location <checkpointLocation>"`
   I'm not sure whether we want/need to tell the user about the offset log.
   What do you think @micheal-o @anishshri-db?



##########
sql/core/src/test/scala/org/apache/spark/sql/streaming/StreamingQuerySuite.scala:
##########
@@ -1473,6 +1474,45 @@ class StreamingQuerySuite extends StreamTest with BeforeAndAfter with Logging wi
     )
   }
 
+  test("SPARK-53103: non empty state checkpoint directory on first batch") {
+    withTempDir { checkpointDir =>
+      val q = MemoryStream[Int].toDS().groupBy().count()
+        .writeStream
+        .format("memory")
+        .outputMode("complete")
+        .queryName(s"name${RandomStringUtils.secure.nextAlphabetic(10)}")

Review Comment:
   Not saying you have to remove it or anything, but just curious if we actually need this for the test to work? 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty on batch 0 [spark]

Posted by "dylanwong250 (via GitHub)" <gi...@apache.org>.
dylanwong250 commented on code in PR #51817:
URL: https://github.com/apache/spark/pull/51817#discussion_r2267547355


##########
common/utils/src/main/resources/error/error-conditions.json:
##########
@@ -5149,6 +5149,13 @@
     ],
     "sqlState" : "42802"
   },
+  "STATE_STORE_CHECKPOINT_LOCATION_NOT_EMPTY" : {
+    "message" : [
+      "The offset log does not exist, but the state checkpoint location <checkpointLocation> is not empty.",

Review Comment:
   Other error messages seem to also reference the offset log but I can change this message.
   



##########
common/utils/src/main/resources/error/error-conditions.json:
##########
@@ -5149,6 +5149,13 @@
     ],
     "sqlState" : "42802"
   },
+  "STATE_STORE_CHECKPOINT_LOCATION_NOT_EMPTY" : {
+    "message" : [
+      "The offset log does not exist, but the state checkpoint location <checkpointLocation> is not empty.",

Review Comment:
   Other error messages also reference the offset log but I can change this message.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty on batch 0 [spark]

Posted by "ericm-db (via GitHub)" <gi...@apache.org>.
ericm-db commented on code in PR #51817:
URL: https://github.com/apache/spark/pull/51817#discussion_r2267422471


##########
common/utils/src/main/resources/error/error-conditions.json:
##########
@@ -5149,6 +5149,13 @@
     ],
     "sqlState" : "42802"
   },
+  "STATE_STORE_CHECKPOINT_LOCATION_NOT_EMPTY" : {
+    "message" : [
+      "The offset log does not exist, but the state checkpoint location <checkpointLocation> is not empty.",

Review Comment:
   I think we can modify this exception message,
   `"The checkpoint location <checkpointLocation> should be empty on batch 0"`
   I'm not sure whether we want/need to tell the user about the offset log.
   What do you think @micheal-o @anishshri-db?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty on batch 0 [spark]

Posted by "dylanwong250 (via GitHub)" <gi...@apache.org>.
dylanwong250 commented on code in PR #51817:
URL: https://github.com/apache/spark/pull/51817#discussion_r2277903175


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/ResolveWriteToStream.scala:
##########
@@ -107,6 +109,25 @@ object ResolveWriteToStream extends Rule[LogicalPlan] {
       }
     }
 
+    // Check to see if we can use this checkpoint location for state.
+    val offsetsCheckpointPath = new Path(checkpointLocation, DIR_NAME_OFFSETS)
+    if (!fileManager.exists(offsetsCheckpointPath)
+      || fileManager.list(offsetsCheckpointPath).isEmpty) {
+
+      // If state or commits has been created, but offsets have not, that means there could be
+      // multiple writers to the state or commits directory of the query.
+      val dirNamesThatShouldNotHaveFiles = Array[String](DIR_NAME_STATE, DIR_NAME_COMMITS)
+
+      dirNamesThatShouldNotHaveFiles.foreach { dirName =>
+        val path = new Path(checkpointLocation, dirName)
+        if (fileManager.exists(path) && !fileManager.list(path).isEmpty) {
+

Review Comment:
   Removed the newline



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53103][SS] Throw an error if state directory is not empty when query starts [spark]

Posted by "dylanwong250 (via GitHub)" <gi...@apache.org>.
dylanwong250 commented on code in PR #51817:
URL: https://github.com/apache/spark/pull/51817#discussion_r2288962098


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/MicroBatchExecution.scala:
##########
@@ -562,12 +565,36 @@ class MicroBatchExecution(
           log"offsets ${MDC(LogKeys.STREAMING_OFFSETS_START, execCtx.startOffsets)} and " +
           log"available offsets ${MDC(LogKeys.STREAMING_OFFSETS_END, execCtx.endOffsets)}")
       case None => // We are starting this stream for the first time.
+        verifyStartCheckpointDirectory()
         logInfo(s"Starting new streaming query.")
         execCtx.batchId = 0
         watermarkTracker = WatermarkTracker(sparkSessionToRunBatches.conf, logicalPlan)
     }
   }
 
+  /**
+   * Verify that the checkpoint directory is in a good state to start a new
+   * streaming query. This checks that the offsets, state, commits directories are
+   * either non-existent or empty.
+   *
+   * If this check fails, an exception is thrown.
+   */
+  private def verifyStartCheckpointDirectory(): Unit = {

Review Comment:
   Changed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org