Skip to main content
src/streaming/job.py · chapter The streaming job, file 2 · 322 lines · open in the repository

What it does

Structured Streaming job: flag abnormal returns and attention spikes as the master table is replayed, and compute each event’s CAR the moment its window closes. Pipeline (one micro-batch at a time): Kafka topic (or a folder of JSON files) one message per ticker per trading day -> parse with the master contract, event time = trading day at 00:00 UTC -> watermark of WATERMARK_DELAY -> join each stock row to SPY’s row for the same day (stream-stream join) for mkt_ret -> per ticker, applyInPandasWithState runs streaming/signals.py: daily alerts from a rolling market model and attention z-score; event windows from the calendar, each with one market model fitted before the window, as in the batch study -> foreachBatch writes data/stream/signals/ and data/stream/event_car/ and prints alerts -> if a trained big-move model exists (src/ml/train.py, decision 0007), it scores each day’s rows and writes data/stream/predictions/: the chance of a big move tomorrow -> with —publish-live, each finished batch also goes to the hosted dashboard’s live feed (decision 0013, src/hosting/live.py) Run locally on files (make them first with src/producers/replay_to_files.py):
Run on the cluster against Kafka while replay_master.py is sending (the course cluster runs Spark 3.5.8 with Scala 2.13 on YARN; spark-submit --version shows yours):
spark-submit starts Spark before this script runs, so the Kafka connector must be on its command line (—packages); the script’s own —kafka-package only applies when the job is started with python3, as on a laptop. On YARN, —out and —checkpoint are HDFS paths (relative ones land in your HDFS home), and —fresh does not clear them: use hdfs dfs -rm -r instead.

What is inside

  • SRC · line 35
  • WATERMARK_DELAY · line 64: How far behind the newest trading day a message may arrive and still be used. replay_master.py’s late test sends messages 3 trading days late; around weekends and holidays that is up to 6 calendar days, so 7 days keeps them all. Spark only promises one direction: anything newer than the watermark is kept, anything older may be dropped (and then counted as “dropped by watermark”). Decision 0004.
  • STATE_SCHEMA · line 66
  • OUTPUT_FIELDS · line 69: applyInPandasWithState has one output schema, so daily signals and CAR rows share it; kind says which is which and foreachBatch splits them.
  • OUTPUT_SCHEMA · line 73
  • EVENTS · line 132: Events come from the shared calendar, not from master’s event_type column, so events on weekends and market holidays still get a window (signals.py moves them to day 0).
  • MODEL · line 138: The big-move model (decision 0007), loaded once. It scores each micro-batch’s daily rows with the same features it was trained on (ml.features), entirely in Spark’s JVM, so the workers need no extra Python code.
  • run_ticker(key, batches, state) · line 188: Called by Spark for one ticker per micro-batch, or when its timeout fires.
  • DropCounter() · line 222: Adds up rows that arrived behind the watermark and were dropped.
  • DropCounter.onQueryStarted(self, event) · line 226
  • DropCounter.onQueryIdle(self, event) · line 227
  • DropCounter.onQueryTerminated(self, event) · line 228
  • DropCounter.onQueryProgress(self, event) · line 230
  • write_batch(batch, batch_id) · line 234: Split one micro-batch into the two outputs. Each batch gets its own folder and overwrites it, so if Spark retries a batch after a crash the result is the same.
  • LIVE · line 276: The hosted dashboard’s live feed (decision 0013), only with —publish-live: finished batches are uploaded in the background. It checks first that this computer can publish (signed in to Hugging Face with write access, writing to a local folder rather than HDFS) and stops with the reason if not.
  • STOP · line 300: Ctrl-C ends a run cleanly. PySpark’s own Ctrl-C handler calls into the JVM from inside the signal, which collides with the call waiting on the query and fails with py4j errors; this one only sets a flag, and the loop below stops the query. SIGTERM too: GitHub Actions sends it when Ctrl-C has not ended a cancelled run quickly enough.
  • LAST_DATA · line 303: when the last micro-batch with rows came (or the start)

Connections

Uses: src/common/contracts.py, src/common/settings.py, src/common/study.py, src/ml/features.py, src/streaming/__init__.py, src/hosting/live.py

Source

src/streaming/job.py