{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c00-ca2b7f36",
   "metadata": {},
   "source": [
    "# Exactly-once billing with transactions — practical\n",
    "\n",
    "The billing service reads session events and writes one charge per finished session. It\n",
    "crashes halfway, twice: once writing charges and offsets separately, once in transactions. Then\n",
    "two copies of the service run with the same transactional id.\n",
    "\n",
    "**Lab services:** kafka-1, kafka-2, kafka-3 · **Script:** `module-07/billing_service.py`\n",
    "· **Lesson:** `modules/07-stream-processing/01-exactly-once-billing.md`"
   ]
  },
  {
   "cell_type": "code",
   "id": "c01-1a970e94",
   "metadata": {},
   "source": [
    "# Setup: find the lab's shared helpers and check the lab is running\n",
    "import sys, pathlib\n",
    "lab_root = next(p for p in [pathlib.Path.cwd(), *pathlib.Path.cwd().parents] if (p / \"common\").is_dir())\n",
    "sys.path.insert(0, str(lab_root))\n",
    "\n",
    "import time\n",
    "from common import settings, lab\n",
    "from common.scenario import Brightlane, to_json\n",
    "\n",
    "lab.require_kafka()\n",
    "lab.print_versions()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "c02-06d93225",
   "metadata": {},
   "source": [
    "## Step 1: 400 finished sessions\n",
    "800 events: a start and a stop for each session. A correct run writes exactly 400 charges."
   ]
  },
  {
   "cell_type": "code",
   "id": "c03-a6fe9a64",
   "metadata": {},
   "source": [
    "from confluent_kafka import Consumer, Producer, TopicPartition\n",
    "from confluent_kafka.admin import AdminClient\n",
    "\n",
    "SESSIONS, CHARGES = \"m07l01-sessions\", \"m07l01-charges\"\n",
    "admin = AdminClient({\"bootstrap.servers\": settings.KAFKA_BOOTSTRAP})\n",
    "lab.recreate_topic(admin, SESSIONS, partitions=3)\n",
    "producer = Producer(settings.PRODUCER_DEFAULTS)\n",
    "for charger_id, event in Brightlane().stream(sessions=400, readings=False):\n",
    "    producer.produce(SESSIONS, key=charger_id, value=to_json(event))\n",
    "print(\"not yet stored:\", producer.flush(30))"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "c04-8dc816e6",
   "metadata": {},
   "source": [
    "## Step 2: A crash, with offsets committed separately\n",
    "`crash_test()` starts the service with `--crash-after 150`: it dies right after its 150th charge\n",
    "reached the broker, before committing anything. Then it starts the service again and waits until\n",
    "the group has caught up. `audit()` reads the charges topic like an auditor would, with the given\n",
    "isolation level, and counts charges and different sessions."
   ]
  },
  {
   "cell_type": "code",
   "id": "c05-35fc1aff",
   "metadata": {},
   "source": [
    "def crash_test(mode, group):\n",
    "    lab.recreate_topic(admin, CHARGES, partitions=3)\n",
    "    admin.delete_consumer_groups([group])\n",
    "    args = [\"--mode\", mode, \"--sessions\", SESSIONS, \"--charges\", CHARGES, \"--group\", group]\n",
    "    first = lab.start_script(\"module-07/billing_service.py\", *args, \"--crash-after\", \"150\", name=f\"{group}-1\")\n",
    "    lab.wait_until(lambda: not first.running, timeout=60, what=\"the crash\")\n",
    "    print(first.lines(\"CRASH\")[-1])\n",
    "    second = lab.start_script(\"module-07/billing_service.py\", *args, name=f\"{group}-2\")\n",
    "    lab.wait_until(lambda: sum(lag for *_, lag in lab.group_lag(admin, group, SESSIONS, 3).values()) == 0,\n",
    "                   timeout=120, interval=1, what=\"the group to catch up\")\n",
    "    lab.stop_script(second)\n",
    "\n",
    "def audit(isolation):\n",
    "    consumer = Consumer({\"bootstrap.servers\": settings.KAFKA_BOOTSTRAP, \"group.id\": \"m07l01-audit\",\n",
    "                         \"isolation.level\": isolation, \"enable.auto.commit\": False})\n",
    "    consumer.assign([TopicPartition(CHARGES, p, 0) for p in range(3)])\n",
    "    sessions = []\n",
    "    while (msg := consumer.poll(3)) is not None:\n",
    "        if not msg.error():\n",
    "            sessions.append(msg.key())\n",
    "    consumer.close()\n",
    "    return f\"{len(sessions)} charges for {len(set(sessions))} sessions\"\n",
    "\n",
    "crash_test(\"plain\", \"m07l01-plain\")\n",
    "print(\"audit:\", audit(\"read_committed\"))"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "c06-5dce3947",
   "metadata": {},
   "source": [
    "Some sessions were charged twice: the charges of the batch in progress reached the topic, but the\n",
    "offsets weren't committed, so the restarted service handled that batch again.\n",
    "\n",
    "## Step 3: The same crash, with transactions\n",
    "In `txn` mode, each batch's charges **and** the consumer's offsets go into one transaction."
   ]
  },
  {
   "cell_type": "code",
   "id": "c07-3fc33404",
   "metadata": {},
   "source": [
    "crash_test(\"txn\", \"m07l01-txn\")\n",
    "print(\"audit (read_committed):  \", audit(\"read_committed\"))\n",
    "print(\"audit (read_uncommitted):\", audit(\"read_uncommitted\"))"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "c08-c07f310c",
   "metadata": {},
   "source": [
    "A `read_committed` reader sees exactly 400 charges. A `read_uncommitted` reader also sees the\n",
    "charges of the transaction that the crash left open, and that was aborted.\n",
    "\n",
    "## Break it: two copies with one transactional id\n",
    "Someone starts a second copy of the service with the same configuration, so with the same\n",
    "`transactional.id`. Predict first: what happens to the first copy, and to the charges?"
   ]
  },
  {
   "cell_type": "code",
   "id": "c09-f0a83fb8",
   "metadata": {},
   "source": [
    "lab.recreate_topic(admin, CHARGES, partitions=3)\n",
    "admin.delete_consumer_groups([\"m07l01-twins\"])\n",
    "args = [\"--mode\", \"txn\", \"--sessions\", SESSIONS, \"--charges\", CHARGES, \"--group\", \"m07l01-twins\", \"--work-ms\", \"20\"]\n",
    "a = lab.start_script(\"module-07/billing_service.py\", *args, \"--name\", \"A\", name=\"m07l01-A\")\n",
    "time.sleep(5)\n",
    "b = lab.start_script(\"module-07/billing_service.py\", *args, \"--name\", \"B\", name=\"m07l01-B\")\n",
    "lab.wait_until(lambda: sum(lag for *_, lag in lab.group_lag(admin, \"m07l01-twins\", SESSIONS, 3).values()) == 0,\n",
    "               timeout=120, interval=1, what=\"the group to catch up\")\n",
    "for s in (a, b):\n",
    "    lab.stop_script(s)\n",
    "print(\"\\n\".join(line for line in a.lines() if not line.startswith(\"%\")))\n",
    "print(b.lines(\"stopped\")[-1])\n",
    "print(\"audit (read_committed):\", audit(\"read_committed\"))"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "c10-28aa0355",
   "metadata": {},
   "source": [
    "## Clean up\n",
    "Delete the topics and groups. If anything gets stuck, `docker compose down -v` in the `code/`\n",
    "folder resets the whole lab."
   ]
  },
  {
   "cell_type": "code",
   "id": "c11-0e337c33",
   "metadata": {},
   "source": [
    "lab.delete_topics(admin, [SESSIONS, CHARGES])\n",
    "admin.delete_consumer_groups([\"m07l01-plain\", \"m07l01-txn\", \"m07l01-twins\", \"m07l01-audit\"])\n",
    "print(\"cleaned up\")"
   ],
   "execution_count": null,
   "outputs": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
