{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c00-ba729442",
   "metadata": {},
   "source": [
    "# Consumer groups share the work — practical\n",
    "\n",
    "A session feed writes 40 events a second. You start billing workers one by one, watch the group\n",
    "share the six partitions and the lag rise and fall, add one worker too many, and then crash one.\n",
    "\n",
    "**Lab services:** kafka-1, kafka-2, kafka-3 · **Scripts:** `module-03/session_feed.py`,\n",
    "`module-03/billing_worker.py` · **Lesson:** `modules/03-consumers/01-consumer-groups-share-the-work.md`"
   ]
  },
  {
   "cell_type": "code",
   "id": "c01-280104a3",
   "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",
    "\n",
    "lab.require_kafka()\n",
    "lab.print_versions()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "c02-910e2b54",
   "metadata": {},
   "source": [
    "## Step 1: Start the session feed\n",
    "The topic has 6 partitions. The feed script writes session events at 40 a second until it's\n",
    "stopped. The notebook starts it in the background; its output goes to a log file. (You can run\n",
    "the same command in a terminal instead and watch it.)"
   ]
  },
  {
   "cell_type": "code",
   "id": "c03-2d62e117",
   "metadata": {},
   "source": [
    "from confluent_kafka.admin import AdminClient\n",
    "\n",
    "TOPIC, GROUP = \"m03l01-sessions\", \"m03l01-billing\"\n",
    "admin = AdminClient({\"bootstrap.servers\": settings.KAFKA_BOOTSTRAP})\n",
    "lab.recreate_topic(admin, TOPIC, partitions=6)\n",
    "admin.delete_consumer_groups([GROUP])\n",
    "\n",
    "feed = lab.start_script(\"module-03/session_feed.py\", \"--topic\", TOPIC, \"--rate\", \"40\", name=\"m03l01-feed\")\n",
    "time.sleep(3)\n",
    "feed.tail(1)"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "c04-d511d080",
   "metadata": {},
   "source": [
    "You should see the feed's start line: `session feed: 40,000 events -> m03l01-sessions at 40/s ...`.\n",
    "\n",
    "## Step 2: One billing worker\n",
    "The worker handles one event in 50 ms, so it manages about 20 events a second: half the feed's\n",
    "rate. Lag is how far the group is behind: for each partition, the end offset minus the group's\n",
    "committed offset."
   ]
  },
  {
   "cell_type": "code",
   "id": "c05-20ee07f1",
   "metadata": {},
   "source": [
    "from confluent_kafka import ConsumerGroupTopicPartitions, TopicPartition\n",
    "from confluent_kafka.admin import OffsetSpec\n",
    "\n",
    "def lag_now():\n",
    "    \"\"\"Total lag of the group, from its committed offsets and the partitions' end offsets.\"\"\"\n",
    "    tps = [TopicPartition(TOPIC, p) for p in range(6)]\n",
    "    committed = admin.list_consumer_group_offsets([ConsumerGroupTopicPartitions(GROUP, tps)])[GROUP].result()\n",
    "    ends = admin.list_offsets({tp: OffsetSpec.latest() for tp in tps})\n",
    "    return sum(ends[TopicPartition(TOPIC, tp.partition)].result().offset - max(tp.offset, 0)\n",
    "               for tp in committed.topic_partitions)\n",
    "\n",
    "def watch(seconds, every=5):\n",
    "    for _ in range(int(seconds / every)):\n",
    "        time.sleep(every)\n",
    "        print(f\"{time.strftime('%H:%M:%S')}  lag {lag_now():>5}  members {lab.group_members(admin, GROUP)}\")\n",
    "\n",
    "workers = {\"A\": lab.start_script(\"module-03/billing_worker.py\", \"--name\", \"A\", \"--topic\", TOPIC,\n",
    "                                 \"--group\", GROUP, \"--work-ms\", \"50\", name=\"m03l01-A\")}\n",
    "watch(20)"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "c06-f54a7251",
   "metadata": {},
   "source": [
    "You should see worker A own all six partitions and the lag grow by roughly 100 every 5 seconds.\n",
    "\n",
    "## Step 3: Three workers\n",
    "Start B and C with the same group id. The group splits the partitions between the members."
   ]
  },
  {
   "cell_type": "code",
   "id": "c07-de5787b4",
   "metadata": {},
   "source": [
    "for name in [\"B\", \"C\"]:\n",
    "    workers[name] = lab.start_script(\"module-03/billing_worker.py\", \"--name\", name, \"--topic\", TOPIC,\n",
    "                                     \"--group\", GROUP, \"--work-ms\", \"50\", name=f\"m03l01-{name}\")\n",
    "watch(25)"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "c08-c4004077",
   "metadata": {},
   "source": [
    "You should see two partitions per worker and, after a short rise during the rebalance, the lag\n",
    "falling: three workers handle about 60 events a second, more than the feed's 40.\n",
    "\n",
    "## Change it: seven workers, six partitions\n",
    "Predict first: you start D, E, F and G. What does each of the seven workers get?"
   ]
  },
  {
   "cell_type": "code",
   "id": "c09-5f4ef95c",
   "metadata": {},
   "source": [
    "for name in [\"D\", \"E\", \"F\", \"G\"]:\n",
    "    workers[name] = lab.start_script(\"module-03/billing_worker.py\", \"--name\", name, \"--topic\", TOPIC,\n",
    "                                     \"--group\", GROUP, \"--work-ms\", \"50\", name=f\"m03l01-{name}\")\n",
    "watch(10)"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "c10-259eb58e",
   "metadata": {},
   "source": [
    "Six workers get one partition each and **one gets none**. A partition is read by one member\n",
    "of a group at a time, so members beyond the partition count sit idle.\n",
    "\n",
    "## Break it: a worker crashes\n",
    "Stop the extra workers, then kill worker B the hard way (`kill -9`: no clean-up, like a power cut).\n",
    "Predict first: how long do B's partitions go unread?"
   ]
  },
  {
   "cell_type": "code",
   "id": "c11-773913f4",
   "metadata": {},
   "source": [
    "for name in [\"D\", \"E\", \"F\", \"G\"]:\n",
    "    lab.stop_script(workers.pop(name))\n",
    "time.sleep(5)\n",
    "print(\"members before the crash:\", lab.group_members(admin, GROUP))\n",
    "lab.kill_script(workers.pop(\"B\"))\n",
    "crashed = time.monotonic()\n",
    "for _ in range(14):\n",
    "    time.sleep(5)\n",
    "    members = lab.group_members(admin, GROUP)\n",
    "    print(f\"+{time.monotonic() - crashed:4.0f}s  lag {lag_now():>5}  members {members}\")\n",
    "    if \"B\" not in members and sum(len(p) for p in members.values()) == 6:\n",
    "        break                                     # B is gone and its partitions have new owners"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "c12-826a24c9",
   "metadata": {},
   "source": [
    "For about 45 seconds the group still lists B as the owner of its partitions, and the lag grows.\n",
    "Only when B's session times out (`session.timeout.ms`, 45 s by default) does the group give its\n",
    "partitions to A and C.\n",
    "\n",
    "## Clean up\n",
    "Stop the workers and the feed, then delete the topic and the group. If anything gets stuck,\n",
    "`docker compose down -v` in the `code/` folder resets the whole lab."
   ]
  },
  {
   "cell_type": "code",
   "id": "c13-4324daa5",
   "metadata": {},
   "source": [
    "for w in workers.values():\n",
    "    lab.stop_script(w)\n",
    "lab.stop_script(feed)\n",
    "feed.tail(1)\n",
    "lab.delete_topics(admin, [TOPIC])\n",
    "admin.delete_consumer_groups([GROUP])\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
}
