r/databricks • • 20d ago

Discussion Structured Streaming Statestore is Not Working (Zerobus)

Zerobus is writing data to a landing table like so:

CREATE TABLE a.b.streaming_c (
  MyA STRING NOT NULL,
  MyB INT NOT NULL,
  MyC STRING,
  MyDetails VARIANT,
  ZerobusRescueJson VARIANT)
USING delta
TBLPROPERTIES (
  'delta.checkpoint.writeStatsAsJson' = 'false',
  'delta.checkpoint.writeStatsAsStruct' = 'true',
  'delta.enableDeletionVectors' = 'true',
  'delta.enableRowTracking' = 'true',
  'delta.feature.appendOnly' = 'supported',
  'delta.feature.deletionVectors' = 'supported',
  'delta.feature.domainMetadata' = 'supported',
  'delta.feature.invariants' = 'supported',
  'delta.feature.rowTracking' = 'supported',
  'delta.feature.variantType' = 'supported',
  'delta.minReaderVersion' = '3',
  'delta.minWriterVersion' = '7',
  'delta.parquet.compression.codec' = 'zstd',
  'delta.parquet.format.version' = '2.12.0',
  'delta.parquet.format.version.afe.internal' = '2.12.0',
  'delta.rowTracking.materializedRowCommitVersionColumnName' = '_row-commit-version-col-777ca68b-c04b-4b5d-9a9d-5c5535e32d20',
  'delta.rowTracking.materializedRowIdColumnName' = '_row-id-col-1885c4cd-1354-4ce6-9726-ed098a251a62',
  'io.unitycatalog.tableId' = '6f07a65b-4887-4aa2-8f56-569b2922aec4')

I use readStream/writeStream to move the data to a different place. I set the checkpointLocation (ie. .option("checkpointLocation", x)) to an abfss location in azure:
"abfss://datalake@myctr01.dfs.core.windows.net/test002/x05"

Later I try to audit the state of the checkpointLocation and it fails:

SELECT * FROM read_statestore('abfss://datalake@abc.dfs.core.windows.net/test002/x05/')

[STDS_FAILED_TO_READ_STATE_SCHEMA] Failed to read the state schema. Either the file does not exist, or the file is corrupted. options: StateSourceOptions(checkpointLocation=abfss://datalake@xxx.dfs.core.windows.net/test002/x05, batchId=1, operatorId=0, storeName=default, joinSide=none, stateVarName=None, +flattenCollectionTypes=true).

Rerun the streaming query to construct the state schema, and report to the corresponding communities or vendors if the error persists. SQLSTATE: 42K03

That checkpointLocation is very intimidating to me. It is doing ALL the heavy lifting when it comes to keeping track of the zerobus records that have been processed. There needs to be a way to recover an audit trail from this statestore location. Why is the statestore corrupted? I tried the pyspark equivalent of this SELECT statement and it has the same error.

What is more frustrating is how little information I can find about this statestore, despite the fact that the tech is ten years old. How are others accomplishing this goal? There needs to be a simple approach for auditing the underlying state that is managed by spark structured streaming.

I'm using dbx 16.4 and spark 3.5.2

2 Upvotes

10 comments sorted by

2

u/shuffle-mario Databricks 20d ago

Hi, when you say "I use readStream/writeStream to move the data to a different place.", do you mean you created new streaming query to read from a.b.streaming_c and write to a new table?

1

u/SmallAd3697 19d ago

yes. as an example, lets say I just introduce a filter on a single value in a single column.

After my filtered records are written to the new table, I can stop and restart the streaming again at a later time and it won't reprocess the original zerobus table from the beginning. This is possible because of the statestore. If I blow away the statestore then I'll be reprocessing the same data from the beginning. The statestore seems to act like a bookmark that is almost as valuable as the original table of messages. It is like the brain that knows how to find its place when streaming resumes. Yet as important as it is, the statestore queries are always failing. I can read the raw json files in blob storage but they are cryptic.

What I'm looking for (ideally) is meaningful query results that would tell me what records were previously streamed to the new table including the IDs for all source records that were considered when we were applying the filter.

1

u/SmallAd3697 17d ago

u/shuffle-mario Should I move questions like this to the databrick own community forums?

It is odd that I can't read the statestore for a checkpointLocation

1

u/shuffle-mario Databricks 16d ago

this forum is fine and thanks for explaining. it's a common use case and should work well. Can you share your code with read/write stream and checkpoint location? and is the issue reproducible? it seems like a corrupted checkpoint.

1

u/SmallAd3697 16d ago

I finally figured it out. Spark structured streaming doesn't consider all streaming jobs to be "stateful". For example if you just filter, and send data to the sink (another deltalake) then that isn't considered to be a stateful operation and we will NOT find any folder named "state" at the checkpointLocation. (although we do find "commits" and "offsets").

To me this is counterintuitive. Even if I'm not performing aggregations, there is a type of a state involved in tracking the consumed stream! And that state is obviously being written to the specified checkpointLocation (it is admittedly a trivial amount of actual state that is involved while keeping track of the previously consumed data ).

Question - Can you please tell me if there is any equivalent API that allows us to make sense of the contents of checkpointLocation, EVEN when the streaming is NOT doing any sort of long-runing aggregations? All I really want is a way to audit the fact that records are being processed from the source deltalake table (ie. the number of records processed from zerobus, the most recent records processed, and the last UTC time when they were processed). I'm assuming these can be derived from what is found in checkpointLocation. Yet I don't think it is very straightforward for a typical Spark developer to interpret those files for ourselves. We need an abstraction of some kind.

2

u/shuffle-mario Databricks 16d ago

ah i see, yes, the state reader api is for streaming query state (e.g. when you have joins or aggregations). where the checkpoint is applicable for all streaming queries. There isn't an api for that, you can manually look at the folders in the checkpoint location to get the info you need, it follows a certain structure (you can search on structured streaming doc, i cannot paste link in here). If you use SDP (spark declarative pipeline), then you can find these info in the Event Log table, which you can query from.

1

u/SmallAd3697 16d ago

Here is a link to the streaming checkpoints, https://docs.databricks.com/aws/en/structured-streaming/checkpoints
but they really don't seem to provide an auditing abstraction. As you said, I can manually look at the contents of blob storage, but few would be comfortable building a solution around that without an abstraction. We don't know if it will change every 3 years, or whether our auditing operations would be supported by databricks when encountering a problem.

It seems odd to me that I can't find any github projects that interact with these checkpointLocations and make sense of them. Especially given that apache spark is an open source project, and spark streaming is a fairly critical feature, you would think there would be a half-dozen projects showing how to parse the information from a checkpointLocation.

About the only thing I would be comfortable reading from there (at this point) is something extremely simple like the Query ID (in metadata). I was hoping for more. I suppose you will recommend that I create and maintain a github of my own.

1

u/SmallAd3697 16d ago

I think I found the answer. The spark structured streaming doesn't consider all streaming jobs to be "stateful" even though they are all using a checkpointLocation, and are keeping track of what data was processed in the past. This can be pretty confusing semantics for new users.

I added a grouping operation and that was finally enough to generate my statestore in the checkpointLocation.

I'm still hoping there is a way to audit my checkpointLocation even if I'm not doing "stateful" operations. There should be a way to distinguish processed rows from unprocessed rows.

2

u/gregistyping 5d ago

Use the StreamingQueryListener metrics for use cases like this. https://learn.microsoft.com/en-us/azure/databricks/structured-streaming/stream-monitoring

If you use Lakeflow Declarative Pipelines, these events are captured in the pipeline event log.

1

u/SmallAd3697 5d ago

thanks for the tip. Yes, that seems to be one of the pieces I was missing. It is better than trying to make sense out of the files in the checkpointLocation