{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Session 3 notebook \u2014 capacity, throughput and the rebuild promise\n",
    "\n",
    "This notebook turns the evidence from the RAID and ZFS lab into one table and one artifact, `storage_results.json`.\n",
    "\n",
    "It reads `data/summary.csv` (the seven configurations \u00d7 four fio tests), the mdadm and ZFS status files under `data/status/`, and it prices the archive in block storage against object storage. Everything it needs is bundled; it makes no network request.\n",
    "\n",
    "The big idea it tests: **RAID buys availability, not backup, and a rebuild is a promise you test.**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Setup and seed\n",
    "\n",
    "The notebook uses the Python standard library only, so it runs on the course MacBook, a lab machine and Kaggle with no install cell. The seed is printed so a re-run is traceable; this notebook has no random model training, so the seed only fixes any sampling you add."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "import csv\n",
    "import json\n",
    "import math\n",
    "import os\n",
    "import random\n",
    "import sys\n",
    "from pathlib import Path\n",
    "\n",
    "SEED = 42\n",
    "random.seed(SEED)\n",
    "print(f\"seed={SEED}  python={sys.version.split()[0]}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Find the data\n",
    "\n",
    "The resolver tries the week's `data/` folder first, then a `COURSE_DATA` override, so the same notebook runs from the repository, a copied folder or Kaggle without edits."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "def data_dir() -> Path:\n",
    "    here = Path.cwd()\n",
    "    candidates = []\n",
    "    if os.environ.get(\"COURSE_DATA\"):\n",
    "        candidates.append(Path(os.environ[\"COURSE_DATA\"]))\n",
    "    candidates += [\n",
    "        here / \"data\",\n",
    "        here.parent / \"data\",\n",
    "        Path(\"/kaggle/input/s3-storage/data\"),\n",
    "    ]\n",
    "    for candidate in candidates:\n",
    "        if (candidate / \"summary.csv\").exists():\n",
    "            return candidate\n",
    "    raise FileNotFoundError(\n",
    "        \"summary.csv not found. Copy the lab's results/ into data/ or set COURSE_DATA.\"\n",
    "    )\n",
    "\n",
    "DATA = data_dir()\n",
    "print(\"data directory:\", DATA)\n",
    "assert (DATA / \"summary.csv\").exists(), \"summary.csv is the lab's first evidence item\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Load the measurements\n",
    "\n",
    "`summary.csv` has one row per configuration and test with bandwidth in KiB/s and IOPS. The lab records seven configurations \u2014 local disk, one volume, three mdadm arrays and two ZFS pools \u2014 and four tests each.\n",
    "\n",
    "<details class=\"th\" lang=\"th\"><summary>\u0e2a\u0e23\u0e38\u0e1b\u0e20\u0e32\u0e29\u0e32\u0e44\u0e17\u0e22</summary>\n",
    "\u0e44\u0e1f\u0e25\u0e4c summary.csv \u0e40\u0e01\u0e47\u0e1a\u0e1c\u0e25\u0e27\u0e31\u0e14\u0e02\u0e2d\u0e07\u0e01\u0e32\u0e23\u0e01\u0e33\u0e2b\u0e19\u0e14\u0e04\u0e48\u0e32 7 \u0e41\u0e1a\u0e1a (\u0e14\u0e34\u0e2a\u0e01\u0e4c\u0e43\u0e19\u0e40\u0e04\u0e23\u0e37\u0e48\u0e2d\u0e07, \u0e27\u0e2d\u0e25\u0e38\u0e48\u0e21\u0e40\u0e14\u0e35\u0e22\u0e27, RAID 5, RAID 6, RAID 10, raidz1, raidz2) \u0e41\u0e25\u0e30\u0e01\u0e32\u0e23\u0e17\u0e14\u0e2a\u0e2d\u0e1a 4 \u0e41\u0e1a\u0e1a\u0e15\u0e48\u0e2d\u0e01\u0e32\u0e23\u0e01\u0e33\u0e2b\u0e19\u0e14\u0e04\u0e48\u0e32 \u0e23\u0e27\u0e21 28 \u0e41\u0e16\u0e27\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>\u4e2d\u6587\u603b\u7ed3</summary>\n",
    "\n",
    "summary.csv \u6587\u4ef6\u4fdd\u5b58 7 \u79cd\u914d\u7f6e\uff08\u672c\u5730\u78c1\u76d8\u3001\u5355\u4e2a\u5377\u3001RAID 5\u3001RAID 6\u3001RAID 10\u3001raidz1\u3001raidz2\uff09\u7684\u6d4b\u91cf\u7ed3\u679c\uff0c\u4ee5\u53ca\u6bcf\u79cd\u914d\u7f6e\u4e0b\u7684 4 \u9879\u6d4b\u8bd5\uff0c\u5171 28 \u884c\u3002\n",
    "</details>"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "rows = list(csv.DictReader((DATA / \"summary.csv\").open(encoding=\"utf-8\")))\n",
    "for row in rows:\n",
    "    row[\"read_kib_s\"] = float(row[\"read_kib_s\"])\n",
    "    row[\"read_iops\"] = float(row[\"read_iops\"])\n",
    "    row[\"write_kib_s\"] = float(row[\"write_kib_s\"])\n",
    "    row[\"write_iops\"] = float(row[\"write_iops\"])\n",
    "\n",
    "TESTS = [\"seqread\", \"seqwrite\", \"randread\", \"randwrite\"]\n",
    "CONFIGS = sorted({row[\"config\"] for row in rows})\n",
    "assert len(rows) == 28, f\"expected 28 measurements, found {len(rows)}\"\n",
    "assert len(CONFIGS) == 7, f\"expected 7 configurations, found {CONFIGS}\"\n",
    "for config in CONFIGS:\n",
    "    measured = {row[\"test\"] for row in rows if row[\"config\"] == config}\n",
    "    assert measured == set(TESTS), f\"{config} is missing tests: {set(TESTS) - measured}\"\n",
    "print(f\"{len(rows)} measurements across {len(CONFIGS)} configurations\")\n",
    "print(\"configurations:\", \", \".join(CONFIGS))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Pivot: configuration \u00d7 test\n",
    "\n",
    "Each cell shows the throughput in MiB/s and the IOPS. Read tests populate the read columns and write tests the write columns, so this table is the one-page result of the lab.\n",
    "\n",
    "**Read it as a burst.** The tests run for 10 seconds, and the 256 MiB file can sit in the Droplet's page cache; the write columns are the honest comparison, and the ratios between configurations matter more than the absolute numbers."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "pivot = {}\n",
    "for row in rows:\n",
    "    is_read = row[\"test\"].endswith(\"read\") and not row[\"test\"].endswith(\"write\")\n",
    "    kib = row[\"read_kib_s\"] if is_read else row[\"write_kib_s\"]\n",
    "    iops = row[\"read_iops\"] if is_read else row[\"write_iops\"]\n",
    "    pivot.setdefault(row[\"config\"], {})[row[\"test\"]] = (kib / 1024.0, iops)\n",
    "\n",
    "header = f\"{'configuration':<14}\" + \"\".join(f\"{test:>18}\" for test in TESTS)\n",
    "print(header)\n",
    "print(\"-\" * len(header))\n",
    "for config in CONFIGS:\n",
    "    cells_out = []\n",
    "    for test in TESTS:\n",
    "        mibs, iops = pivot[config][test]\n",
    "        cells_out.append(f\"{mibs:8.1f} MiB/s {iops:6.0f}\")\n",
    "    print(f\"{config:<14}\" + \"\".join(f\"{value:>18}\" for value in cells_out))\n",
    "\n",
    "assert all(value > 0 for config in CONFIGS for value in pivot[config][TESTS[0]]), \\\n",
    "    \"a read measurement is zero; re-run the missing fio command\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Capacity: five layouts, the same 20 GiB\n",
    "\n",
    "Every array is built from the same five 10 GiB volumes. Parity and mirrors spend the raw capacity differently, but all five arrays present about 20 GiB usable \u2014 so capacity is not what chooses between them.\n",
    "\n",
    "<details class=\"th\" lang=\"th\"><summary>\u0e2a\u0e23\u0e38\u0e1b\u0e20\u0e32\u0e29\u0e32\u0e44\u0e17\u0e22</summary>\n",
    "\u0e2d\u0e32\u0e23\u0e4c\u0e40\u0e23\u0e22\u0e4c\u0e17\u0e31\u0e49\u0e07\u0e2b\u0e49\u0e32\u0e41\u0e1a\u0e1a\u0e2a\u0e23\u0e49\u0e32\u0e07\u0e08\u0e32\u0e01\u0e27\u0e2d\u0e25\u0e38\u0e48\u0e21 10 GiB \u0e2b\u0e49\u0e32\u0e25\u0e39\u0e01\u0e40\u0e14\u0e35\u0e22\u0e27\u0e01\u0e31\u0e19 \u0e41\u0e25\u0e30\u0e43\u0e2b\u0e49\u0e1e\u0e37\u0e49\u0e19\u0e17\u0e35\u0e48\u0e43\u0e0a\u0e49\u0e07\u0e32\u0e19\u0e1b\u0e23\u0e30\u0e21\u0e32\u0e13 20 GiB \u0e40\u0e17\u0e48\u0e32\u0e01\u0e31\u0e19 \u0e04\u0e27\u0e32\u0e21\u0e08\u0e38\u0e08\u0e36\u0e07\u0e44\u0e21\u0e48\u0e43\u0e0a\u0e48\u0e15\u0e31\u0e27\u0e15\u0e31\u0e14\u0e2a\u0e34\u0e19\u0e43\u0e08 \u0e40\u0e25\u0e37\u0e2d\u0e01\u0e08\u0e32\u0e01\u0e04\u0e27\u0e32\u0e21\u0e0b\u0e49\u0e33\u0e0b\u0e49\u0e2d\u0e19\u0e41\u0e25\u0e30\u0e23\u0e39\u0e1b\u0e41\u0e1a\u0e1a\u0e01\u0e32\u0e23\u0e40\u0e02\u0e35\u0e22\u0e19\u0e41\u0e17\u0e19\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>\u4e2d\u6587\u603b\u7ed3</summary>\n",
    "\n",
    "\u5168\u90e8\u4e94\u79cd\u9635\u5217\u90fd\u7531\u540c\u6837\u7684\u4e94\u4e2a 10 GiB \u5377\u6784\u5efa\uff0c\u5e76\u63d0\u4f9b\u5927\u81f4\u76f8\u540c\u7684\u7ea6 20 GiB \u53ef\u7528\u7a7a\u95f4\u3002\u56e0\u6b64\u5bb9\u91cf\u4e0d\u662f\u51b3\u5b9a\u56e0\u7d20\uff1b\u5e94\u5f53\u6539\u4ece\u5197\u4f59\u6027\u548c\u5199\u5165\u6a21\u5f0f\u6765\u9009\u62e9\u3002\n",
    "</details>"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "RAW_GIB = 50\n",
    "CAPACITY = {\n",
    "    \"raid5\": {\"usable_gib\": 20, \"spent\": \"one parity disk and one hot spare\"},\n",
    "    \"raid6\": {\"usable_gib\": 20, \"spent\": \"two parity disks and one hot spare\"},\n",
    "    \"raid10\": {\"usable_gib\": 20, \"spent\": \"the mirror copies\"},\n",
    "    \"zfs-raidz1\": {\"usable_gib\": 20, \"spent\": \"distributed parity\"},\n",
    "    \"zfs-raidz2\": {\"usable_gib\": 20, \"spent\": \"two distributed parity blocks\"},\n",
    "}\n",
    "\n",
    "print(f\"{'array':<13}{'raw GiB':>9}{'usable GiB':>12}{'overhead':>10}  spent on\")\n",
    "for name, spec in CAPACITY.items():\n",
    "    overhead = RAW_GIB - spec[\"usable_gib\"]\n",
    "    print(f\"{name:<13}{RAW_GIB:>9}{spec['usable_gib']:>12}{overhead:>10}  {spec['spent']}\")\n",
    "    assert spec[\"usable_gib\"] == 20, f\"{name} should yield 20 GiB usable\"\n",
    "\n",
    "assert sum(spec[\"usable_gib\"] for spec in CAPACITY.values()) == 100\n",
    "print(\"\\nFive arrays, one 50 GiB volume set, 100 GiB of usable capacity in total \u2014\")\n",
    "print(\"each array gives 20 GiB, which is why redundancy and throughput decide.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6. Repairs: did the promise finish?\n",
    "\n",
    "The status files are the evidence the array returned to a clean state. mdadm reports active and total members in square brackets; ZFS reports the pool state and the resilver line. A degraded pool or array that never returned to clean is a failed lab, even if the benchmarks look fine."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "STATUS = DATA / \"status\"\n",
    "\n",
    "\n",
    "def mdstat_members(path: Path):\n",
    "    text = path.read_text(encoding=\"utf-8\")\n",
    "    match = re.search(r\"\\[(\\d+)/(\\d+)\\]\", text)\n",
    "    if not match:\n",
    "        raise ValueError(f\"no [active/total] member count in {path.name}\")\n",
    "    return int(match.group(1)), int(match.group(2))\n",
    "\n",
    "\n",
    "def zpool_state(path: Path):\n",
    "    text = path.read_text(encoding=\"utf-8\")\n",
    "    state = re.search(r\"state:\\s+(\\w+)\", text).group(1)\n",
    "    scan = re.search(r\"scan:\\s+(.+)\", text).group(1).strip()\n",
    "    seconds = None\n",
    "    if \"resilvered\" in scan:\n",
    "        taken = re.search(r\"in (\\d+):(\\d+):(\\d+)\", scan)\n",
    "        if taken:\n",
    "            seconds = int(taken.group(1)) * 3600 + int(taken.group(2)) * 60 + int(taken.group(3))\n",
    "    return state, scan, seconds\n",
    "\n",
    "\n",
    "repairs = {}\n",
    "for array in [\"raid5\", \"raid6\", \"raid10\"]:\n",
    "    active, total = mdstat_members(STATUS / f\"mdstat-{array}-rebuild.txt\")\n",
    "    repairs[array] = {\"active\": active, \"total\": total, \"clean\": active == total}\n",
    "    print(f\"{array:<6} rebuilt to [{active}/{total}]\")\n",
    "\n",
    "for config, pool in [(\"zfs-raidz1\", \"raidz1\"), (\"zfs-raidz2\", \"raidz2\")]:\n",
    "    state, scan, seconds = zpool_state(STATUS / f\"zpool-{pool}-rebuild.txt\")\n",
    "    repairs[config] = {\"state\": state, \"resilvered\": \"resilvered\" in scan,\n",
    "                       \"resilver_seconds\": seconds}\n",
    "    print(f\"{config:<12} {state:<8} {scan.split(',')[0]}\")\n",
    "\n",
    "assert all(item[\"clean\"] for item in repairs.values() if \"clean\" in item), \\\n",
    "    \"an mdadm array did not return to all members active\"\n",
    "assert all(item[\"state\"] == \"ONLINE\" for item in repairs.values() if \"state\" in item), \\\n",
    "    \"a ZFS pool is not ONLINE after the resilver\"\n",
    "assert all(item[\"resilvered\"] for item in repairs.values() if \"resilvered\" in item), \\\n",
    "    \"a ZFS pool did not finish its resilver\"\n",
    "print(\"\\nEvery array and pool returned to a clean, online state.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7. Cost: block storage versus the archive\n",
    "\n",
    "DigitalOcean block storage costs **$0.10 per GiB-month**; Cloudflare R2 standard storage costs **$0.015 per GiB-month** with no egress charge. The scenario archive is 1 TB. The calculation below is the one the research question asks you to argue about: the working set belongs on block storage, but the whole archive does not.\n",
    "\n",
    "<details class=\"th\" lang=\"th\"><summary>\u0e2a\u0e23\u0e38\u0e1b\u0e20\u0e32\u0e29\u0e32\u0e44\u0e17\u0e22</summary>\n",
    "\u0e1a\u0e25\u0e47\u0e2d\u0e01\u0e2a\u0e15\u0e2d\u0e40\u0e23\u0e08\u0e23\u0e32\u0e04\u0e32 $0.10 \u0e15\u0e48\u0e2d GiB-\u0e40\u0e14\u0e37\u0e2d\u0e19 \u0e2a\u0e48\u0e27\u0e19 R2 \u0e23\u0e32\u0e04\u0e32 $0.015 \u0e15\u0e48\u0e2d GiB-\u0e40\u0e14\u0e37\u0e2d\u0e19 \u0e41\u0e25\u0e30\u0e44\u0e21\u0e48\u0e21\u0e35\u0e04\u0e48\u0e32 egress \u0e04\u0e25\u0e31\u0e07\u0e27\u0e34\u0e14\u0e35\u0e42\u0e2d 1 TB \u0e1a\u0e19\u0e1a\u0e25\u0e47\u0e2d\u0e01\u0e2a\u0e15\u0e2d\u0e40\u0e23\u0e08\u0e08\u0e36\u0e07\u0e1b\u0e23\u0e30\u0e21\u0e32\u0e13 $102 \u0e15\u0e48\u0e2d\u0e40\u0e14\u0e37\u0e2d\u0e19 \u0e40\u0e17\u0e35\u0e22\u0e1a\u0e01\u0e31\u0e1a\u0e1b\u0e23\u0e30\u0e21\u0e32\u0e13 $15 \u0e15\u0e48\u0e2d\u0e40\u0e14\u0e37\u0e2d\u0e19\u0e1a\u0e19 R2\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>\u4e2d\u6587\u603b\u7ed3</summary>\n",
    "\n",
    "\u5757\u5b58\u50a8\u7684\u4ef7\u683c\u662f\u6bcf GiB-\u6708 $0.10\uff0c\u800c R2 \u7684\u4ef7\u683c\u662f\u6bcf GiB-\u6708 $0.015\uff0c\u4e14\u6ca1\u6709\u51fa\u7ad9\u6d41\u91cf\u8d39\u7528\u3002\u56e0\u6b64 1 TB \u7684\u89c6\u9891\u6863\u6848\u653e\u5728\u5757\u5b58\u50a8\u4e0a\u5927\u7ea6\u6bcf\u6708 $102\uff0c\u800c\u5728 R2 \u4e0a\u5927\u7ea6\u6bcf\u6708 $15\u3002\n",
    "</details>"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "ARCHIVE_GIB = 1024          # 1 TB expressed in GiB\n",
    "BLOCK_USD_PER_GIB_MONTH = 0.10\n",
    "R2_USD_PER_GIB_MONTH = 0.015\n",
    "VOLUME_GIB = 10\n",
    "VOLUMES_PER_DROPLET = 15\n",
    "\n",
    "block_month = ARCHIVE_GIB * BLOCK_USD_PER_GIB_MONTH\n",
    "r2_month = ARCHIVE_GIB * R2_USD_PER_GIB_MONTH\n",
    "print(f\"block storage : ${block_month:,.2f}/month  ${block_month * 12:,.2f}/year\")\n",
    "print(f\"R2            : ${r2_month:,.2f}/month  ${r2_month * 12:,.2f}/year\")\n",
    "print(f\"ratio         : {block_month / r2_month:.2f}\u00d7\")\n",
    "\n",
    "volumes_needed = math.ceil(ARCHIVE_GIB / VOLUME_GIB)\n",
    "print(f\"\\n1 TB in 10 GiB volumes needs {volumes_needed} volumes; a Droplet accepts {VOLUMES_PER_DROPLET}.\")\n",
    "\n",
    "assert abs(block_month - 102.40) < 0.01, \"block-storage monthly cost changed\"\n",
    "assert abs(r2_month - 15.36) < 0.01, \"R2 monthly cost changed\"\n",
    "assert volumes_needed > VOLUMES_PER_DROPLET, \"the volume layout no longer exceeds the per-Droplet limit\"\n",
    "\n",
    "cost = {\n",
    "    \"archive_gib\": ARCHIVE_GIB,\n",
    "    \"block_usd_per_gib_month\": BLOCK_USD_PER_GIB_MONTH,\n",
    "    \"r2_usd_per_gib_month\": R2_USD_PER_GIB_MONTH,\n",
    "    \"block_month_usd\": round(block_month, 2),\n",
    "    \"r2_month_usd\": round(r2_month, 2),\n",
    "    \"volumes_needed\": volumes_needed,\n",
    "    \"volumes_per_droplet\": VOLUMES_PER_DROPLET,\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8. Write the artifact\n",
    "\n",
    "`storage_results.json` is the notebook half of the evidence bundle. It records the measurements, the capacity table, the repair outcomes and the cost comparison, plus the seed and a timestamp so the run is traceable.\n",
    "\n",
    "<details class=\"th\" lang=\"th\"><summary>\u0e2a\u0e23\u0e38\u0e1b\u0e20\u0e32\u0e29\u0e32\u0e44\u0e17\u0e22</summary>\n",
    "\u0e40\u0e0b\u0e25\u0e25\u0e4c\u0e19\u0e35\u0e49\u0e40\u0e02\u0e35\u0e22\u0e19\u0e44\u0e1f\u0e25\u0e4c storage_results.json \u0e0b\u0e36\u0e48\u0e07\u0e40\u0e1b\u0e47\u0e19\u0e2b\u0e25\u0e31\u0e01\u0e10\u0e32\u0e19\u0e1d\u0e31\u0e48\u0e07\u0e42\u0e19\u0e49\u0e15\u0e1a\u0e38\u0e4a\u0e01 \u0e1b\u0e23\u0e30\u0e01\u0e2d\u0e1a\u0e14\u0e49\u0e27\u0e22\u0e1c\u0e25\u0e27\u0e31\u0e14 \u0e15\u0e32\u0e23\u0e32\u0e07\u0e04\u0e27\u0e32\u0e21\u0e08\u0e38 \u0e1c\u0e25\u0e01\u0e32\u0e23\u0e0b\u0e48\u0e2d\u0e21 \u0e41\u0e25\u0e30\u0e01\u0e32\u0e23\u0e40\u0e1b\u0e23\u0e35\u0e22\u0e1a\u0e40\u0e17\u0e35\u0e22\u0e1a\u0e15\u0e49\u0e19\u0e17\u0e38\u0e19\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>\u4e2d\u6587\u603b\u7ed3</summary>\n",
    "\n",
    "\u8fd9\u4e2a\u5355\u5143\u683c\u5199\u51fa storage_results.json \u6587\u4ef6\uff0c\u5b83\u662f notebook \u4e00\u4fa7\u7684\u8bc1\u636e\uff0c\u5305\u542b\u6d4b\u91cf\u7ed3\u679c\u3001\u5bb9\u91cf\u8868\u3001\u4fee\u590d\u7ed3\u679c\u4ee5\u53ca\u6210\u672c\u6bd4\u8f83\u3002\n",
    "</details>"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "from datetime import datetime, timezone\n",
    "\n",
    "artifact = {\n",
    "    \"session\": \"03-storage-file-systems\",\n",
    "    \"title\": \"Volumes, RAID 5/6/10 and ZFS\",\n",
    "    \"seed\": SEED,\n",
    "    \"generated_utc\": datetime.now(timezone.utc).isoformat(timespec=\"seconds\"),\n",
    "    \"source\": (DATA / \"PROVENANCE.txt\").read_text(encoding=\"utf-8\").splitlines()[0]\n",
    "    if (DATA / \"PROVENANCE.txt\").exists() else \"learner run\",\n",
    "    \"configurations\": {\n",
    "        config: {\n",
    "            test: {\n",
    "                \"mib_s\": round(pivot[config][test][0], 1),\n",
    "                \"iops\": round(pivot[config][test][1]),\n",
    "            }\n",
    "            for test in TESTS\n",
    "        }\n",
    "        for config in CONFIGS\n",
    "    },\n",
    "    \"capacity\": CAPACITY,\n",
    "    \"repairs\": repairs,\n",
    "    \"cost\": cost,\n",
    "    \"notes\": \"Ten-second fio runs measure the burst window; write numbers are the honest comparison.\",\n",
    "}\n",
    "\n",
    "out = Path(\"storage_results.json\")\n",
    "out.write_text(json.dumps(artifact, indent=2, sort_keys=True) + \"\\n\", encoding=\"utf-8\")\n",
    "print(\"wrote\", out.resolve())\n",
    "print(json.dumps(artifact[\"cost\"], indent=2))\n",
    "\n",
    "saved = json.loads(out.read_text(encoding=\"utf-8\"))\n",
    "for key in [\"configurations\", \"capacity\", \"repairs\", \"cost\", \"seed\"]:\n",
    "    assert key in saved, f\"{key} missing from storage_results.json\"\n",
    "assert saved[\"seed\"] == SEED\n",
    "print(\"artifact checks pass\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9. What the numbers say\n",
    "\n",
    "Write the 150\u2013250-word note from these four results:\n",
    "\n",
    "1. **Capacity is equal.** Five arrays from 50 GiB raw all give 20 GiB usable; choose by redundancy and write pattern, not by capacity.\n",
    "2. **Writes are the honest comparison.** RAID 10 spends two disks per write; RAID 5/6 spend parity reads and writes; ZFS spends copy-on-write and checksums. The 10-second bursts flatter reads through the page cache.\n",
    "3. **Repairs finish.** Every mdadm array returned to all members active and every pool returned `ONLINE` after its resilver. That clean state, not the benchmark, is the availability promise.\n",
    "4. **The archive belongs elsewhere.** 1 TB on block storage is about $102/month; on R2 it is about $15/month, so the working set stays on volumes and the tail is tiered to object storage with a tested restore path.\n",
    "\n",
    "**Mac verified:** this notebook runs top to bottom on the Apple Silicon MacBook against the bundled verified run."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
