DDIA·واژه‌هاTERMS

نقشه راهRoadmap/ واژه‌نامهGlossary/ فصل ۱۱Chapter 11

۱۱11پردازش دسته‌ایBatch Processing

۲۱ واژه از فصل ۱۱ — هر کدام با یک مثال روزمره، یک نمودار و تلهٔ رایج.21 terms from chapter 11, each with an everyday example, a diagram and the usual trap.

append-only log

لاگ فقط-افزودنی فصل ۱۱Chapter 11 · دیدن در متنsee in context

چرا هیچ سطری از این دفتر پاک نمی‌شود؟Why does nothing in this ledger ever get erased?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

نانوایی دفترچه دارد و هر فروش را در یک خط تازه می‌نویسد؛ هیچ خطی پاک نمی‌شود. آخر ماه از خط اول می‌خوانی و جمع می‌زنی. اگر نصفه‌کاره بلند شوی، شمارهٔ خطی که رسیده‌ای یادت می‌ماند و فردا از همان‌جا ادامه می‌دهی. لاگ فقط-افزودنی همین دفتر است، با شمارهٔ خط به‌جای انگشتت.The bakery keeps a notebook and writes every sale on a fresh line; no line is ever crossed out. At the end of the month you read from the top and total it up. If you stop halfway, you remember the line number you reached and carry on from there tomorrow. An append-only log is that notebook, with a line number instead of your finger.

تشبیه و تصویر ذهنیThe analogy

چرا کسی خط پاک کند؟ چون نوشتن روی خط قدیمی یعنی اول جایش را پیدا کنی، بعد مطمئن شوی کسی دیگر عوضش نکرده، و اگر برق برود نصف خط نو و نصف خط قدیم بماند. در دفتری که فقط آخرش می‌نویسی، هر نوشتن یک حرکت است و بس. بهایش این است که دفتر مدام قطور می‌شود؛ هر چند وقت یک‌بار باید خلاصه‌ای از خط‌های قدیمی بنویسی و دفتر را کوتاه کنی، وگرنه انبار پر می‌شود. تازه، پیدا کردن یک خط خاص از میان هزاران خط بی‌فایده است: باید از اول بخوانی یا فهرست جدا داشته باشی.Why would anyone erase a line? Because writing over an old line means finding its place first, then making sure nobody else changed it, and if the power dies you can end up with half a new line and half an old one. In a book you only write at the end of, every write is one movement and nothing else. The bill is that the book keeps getting fatter; every so often you have to write a summary of the old lines and shorten it, or the shelf fills up. And finding one particular line among thousands is hopeless: you read from the top, or you keep a separate index.

نوشتن ۱۰۰۱Append 1001فقط انتهای فایلtail onlyخواندن تا ۱۰۰۵Read to 1005نشانگر خودشits own cursorخرابیCrashنشانگر ذخیره شدcursor persistedادامه از ۱۰۰۶Resume 1006بدون خواندن دوبارهno double read
خط زمان یک لاگ: نوشتن فقط در انتها، و هر خواننده نشانگر خودش را نگه می‌دارد.A log over time: writes only at the tail, and every reader keeps its own cursor.

و بعدش، فنیAnd now, technically

لاگ فقط-افزودنی ساده‌ترین ساختار ذخیره است: رکوردها پشت سر هم نوشته می‌شوند، هر رکورد یک شمارهٔ ترتیبی یا offset می‌گیرد و هیچ رکوردی در جای خودش عوض نمی‌شود. چون همیشه فقط انتهای فایل لمس می‌شود، نوشتن ترتیبی است و ارزان؛ و چون رکوردها تغییر نمی‌کنند، چند خواننده می‌توانند هر کدام با نشانگر خودشان جلو بروند و به هم کاری نداشته باشند. همین خاصیت، لاگ را ورودی طبیعی یک job دسته‌ای می‌کند: job یک بازه از offset را می‌خواند، تبدیل می‌کند و خروجی مشتق را می‌نویسد؛ اجرای دوباره از همان بازه، همان خروجی را می‌سازد. هزینه‌ها روشن است. فضا تا ابد رشد می‌کند، پس به سیاست نگهداشت یا فشرده‌سازی نیاز داری. جست‌وجوی نقطه‌ای روی کلید تقریباً وجود ندارد، چون هیچ ایندکسی روی ترتیب نوشتن ساخته نشده. و اگر نشانگر خواننده از پنجرهٔ نگهداشت عقب بماند، رکوردهایش پاک شده‌اند و او بی‌صدا داده از دست می‌دهد.An append-only log is the simplest storage structure there is: records are written one after another, each gets a sequential number or offset, and no record is ever modified in place. Because only the tail is touched, writes are sequential and cheap; and because records never change, several readers can each advance their own cursor without interfering. That property makes a log the natural input to a batch job: the job reads a range of offsets, transforms it, and writes derived output; re-running from the same range reproduces the same output. The costs are plain. Space grows forever, so you need a retention policy or compaction. Point lookups by key are essentially absent, because no index follows the write order. And if a reader's cursor falls behind the retention window, its records are gone and it loses data without an error.

1 appendهر نوشتن فقط یک حرکت در انتهای فایل استevery write is one movement at the tail
offsetشمارهٔ ترتیبی رکورد؛ خواننده با آن جایش را نگه می‌داردa record's sequence number; a reader keeps its place with it
O(1)نوشتن در انتها، بدون گشتن برای جای خالیappending at the tail, no search for a free slot

چرا تغییرناپذیری این‌قدر می‌ارزدWhy immutability pays

وقتی هیچ رکوردی عوض نمی‌شود، خیلی چیزها ساده می‌شود. خواندن نیازی به قفل ندارد، چون چیزی نیست که نصفه به‌روز شود. پشتیبان‌گیری یعنی کپی کردن یک بازه از offset، نه عکس گرفتن از ساختاری که هم‌زمان تغییر می‌کند. دو خواننده می‌توانند با سرعت‌های مختلف جلو بروند و هیچ‌کدام دیگری را کند نکند. و بازپخش ممکن می‌شود: همان ورودی را دوباره از اول بخوان و همان خروجی را بساز. هیچ‌کدام از این‌ها جادو نیست؛ همه از یک تصمیم می‌آید که چیزی را در جا عوض نکن.When no record changes, a lot gets simpler. A read needs no lock, because there is nothing to catch half-updated. A backup is a copy of an offset range, not a snapshot of a structure mutating underneath you. Two readers can move at different speeds and neither slows the other. And replay becomes possible: read the same input from the start again and produce the same output. None of this is magic; it all follows from one decision not to update anything in place.

کِی لاگ جواب نمی‌دهدWhere a log stops paying

اگر کارت با کلید جست‌وجو می‌کند، لاگ بدترین انتخاب است: باید کل فایل را بگردی یا ایندکس جدا بسازی. اگر داده‌ات مدام اصلاح می‌شود و خواننده‌ها باید آخرین نسخه را ببینند، لاگ فقط نسخه‌های پشت سر هم را نگه می‌دارد و کسی باید تصمیم بگیرد کدام تازه‌تر است. و اگر حجم نوشتن از توان یک دیسک بگذرد، یک لاگ روی یک ماشین کافی نیست. این‌ها همان جاهایی است که مردم سراغ درخت B یا پایگاه دادهٔ ستونی می‌روند.If your work is lookups by key, a log is the worst choice: you scan the whole file or build a separate index. If your data is corrected constantly and readers must see the latest version, a log only keeps successive versions, and something has to decide which is newer. And if write volume outgrows one disk, a single log on one machine is not enough. Those are exactly the points where people reach for a B-tree or a column store.

تلهٔ رایجThe usual trap

نگه داشتن نشانگر خواننده بدون نگاه به پنجرهٔ نگهداشت. لاگ کوتاه می‌شود، job یک هفته خواب بوده، و از فردا از وسط داده می‌خواند؛ هیچ خطایی بالا نمی‌آید و فقط اعداد گزارش کم می‌شوند. یا برعکس، چون لاگ تغییرناپذیر است فکر می‌کنی می‌تواند همه چیز را برای همیشه نگه دارد و انبار پر می‌شود.Keeping a reader cursor without watching the retention window. The log is trimmed, the job has slept for a week, and from tomorrow it reads from the middle; nothing errors and only the report totals shrink. Or the mirror mistake: because the log is immutable you assume it can hold everything forever, and the disk fills up.

batch processing

پردازش دسته‌ای فصل ۱۱Chapter 11 · دیدن در متنsee in context

چرا جمع زدن رسیدها را به آخر ماه می‌سپاری؟Why do you leave the receipts until the end of the month?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

رسیدهای تمام ماه را در یک کیف ریخته‌ای. آخر ماه کیف را روی میز خالی می‌کنی، همه را کنار هم می‌چینی، جمع می‌زنی و یک برگهٔ خلاصه می‌نویسی؛ بعد برگهٔ خلاصهٔ ماه قبل را برمی‌داری. پردازش دسته‌ای همین است: ورودی محدود، یک اجرا، و خروجی‌ای که جای قبلی را می‌گیرد.A month of receipts sits in a bag. At month end you empty the bag on the table, spread everything out, add it up, and write a one-page summary; then you take last month's summary off the wall. Batch processing is that: a bounded input, one run, and an output that replaces the previous one.

تشبیه و تصویر ذهنیThe analogy

فرقش با شمردن هر رسید در همان لحظه این است که این‌جا عجله نداری. می‌توانی همه را یک‌جا ببینی، مرتب کنی، دسته‌بندی کنی و چند بار از اول بشماری تا مطمئن شوی. اگر نصفه‌کاره ولش کنی، برگهٔ خلاصهٔ قبلی سر جایش است و خرابی وسط کار چیزی را خراب نمی‌کند. بهایش این است که جواب همیشه عقب است: تا آخر ماه خبری از جمع کل نیست، و اگر رسیدی گم شود فقط وقتی می‌فهمی که جمع با صورت‌حساب بانک نخواند.The difference from adding each receipt as it arrives is that here you are not in a hurry. You can see everything at once, sort it, group it, and count it again from the top until you trust the number. If you walk away halfway, last month's summary is still on the wall, and a failure mid-way breaks nothing. The bill is that the answer is always late: until month end there is no total at all, and a missing receipt only shows up when the total disagrees with the bank statement.

ورودی محدودBounded inputاندازه معلومknown sizejobThe jobخواندن و تبدیلread and transformخروجی موقتTemp outputمسیر جداseparate pathجایگزینی اتمیکAtomic swaprenamerename
مسیر یک job دسته‌ای: ورودی محدود، تبدیل، خروجی در مسیر موقت، و جایگزینی اتمیک.One batch job: bounded input, transformation, a temporary output, and an atomic swap.

و بعدش، فنیAnd now, technically

در پردازش دسته‌ای، ورودی از قبل وجود دارد و اندازه‌اش معلوم است؛ job آن را از اول تا آخر می‌خواند، تبدیل می‌کند و خروجی مشتق را می‌نویسد. چون کسی منتظر جواب در همان لحظه نیست، سیستم می‌تواند برای توان عملیاتی بهینه شود، نه برای تأخیر: خواندن دنباله‌ای، دسته‌های بزرگ، و کارهایی که ساعت‌ها طول می‌کشند. خروجی معمولاً در یک مسیر موقت نوشته می‌شود و بعد با rename اتمیک جای خروجی قبلی را می‌گیرد، تا خواننده هرگز جدول نیمه‌کاره نبیند. خرابی یک task به معنای شکست کل job نیست؛ همان task دوباره اجرا می‌شود، به شرطی که کارش تکرارشدنی باشد. jobهای پیچیده به هم زنجیر می‌شوند و هر مرحله خروجی کامل خودش را روی دیسک می‌نشاند. همین زنجیر کردن هم نقطهٔ قوت است و هم نقطهٔ ضعف: هر مرحله قابل بازرسی است، ولی هر مرحله یک بار کامل خواندن و نوشتن می‌خواهد.In batch processing the input already exists and its size is known; the job reads it end to end, transforms it, and writes derived output. Because nobody is waiting for an answer right now, the system can optimise for throughput rather than latency: sequential reads, large chunks, and jobs that run for hours. The output is usually written to a temporary path and then swapped into place with an atomic rename, so a reader never sees a half-written table. A failed task is not a failed job; that task runs again, provided its work is repeatable. Complex jobs are chained, and every stage lands its full output on disk. That chaining is both the strength and the weakness: each stage is inspectable, and each stage costs a full read and write.

T+1یک اجرا در روز؛ جواب همیشه عقب استone run a day; the answer is always behind
1 passهر اجرا کل ورودی را می‌خواندeach run reads the whole input
boundedاندازهٔ ورودی معلوم است، پس پایان کار هم معلوم استthe input is finite, so the job has an end

خروجی را چطور جایگزین می‌کنیHow you replace the output

بدترین کار این است که مستقیم روی خروجی قبلی بنویسی. اگر وسط کار خرابی بیاید، خواننده‌ها جدولی می‌بینند که نیمی از آن دیروز است و نیمی امروز، و هیچ خطایی هم نگرفته‌اند. راه درست این است که خروجی تازه را در مسیر دیگری بنویسی و فقط وقتی کامل شد، با یک rename اتمیک جایگزین کنی. روی فایل‌سیستم محلی این کار ارزان است. روی انبار شیء که تغییر نام ندارد، همان کار با نوشتن یک شیء فهرست در آخر و خواندن از روی آن انجام می‌شود. در هر دو حالت قاعده یکی است: خواننده فقط نسخهٔ کامل را می‌بیند.The worst move is writing straight over the previous output. If the run dies midway, readers see a table that is half yesterday and half today, and no error was raised. The right move is to write the new output somewhere else and, only when it is complete, swap it in with an atomic rename. On a local filesystem that is cheap. On an object store, which has no rename, the same effect comes from writing a small manifest object last and reading through it. Either way the rule is the same: a reader only ever sees a complete version.

کجا به درد نمی‌خوردWhere it does not fit

اگر جواب باید در چند ثانیه برسد، پردازش دسته‌ای ابزار اشتباهی است. زمان‌بندی دوره‌ای هم یعنی داده‌ات همیشه عقب است؛ برای داشبوردی که مدیر هر دقیقه نگاه می‌کند، همین عقب بودن خودش یک باگ است. تازه، هر اجرا کل ورودی را می‌خواند، حتی اگر فقط یک درصدش عوض شده باشد؛ وقتی این درصد کوچک است و اجرا گران، باید به پردازش افزایشی یا جریان داده فکر کنی. و اگر job اثر جانبی بیرونی داشته باشد، اجرای دوباره‌اش می‌تواند همان ایمیل را دو بار بفرستد.If the answer must arrive in seconds, batch is the wrong tool. A periodic schedule also means the data is always behind; for a dashboard a manager checks every minute, that lag is itself a bug. And each run reads the whole input even if one percent of it changed; when that percentage is small and the run is expensive, incremental processing or a stream is the better shape. Finally, if the job has an external side effect, re-running it can send the same email twice.

تلهٔ رایجThe usual trap

نوشتن مستقیم روی خروجی قبلی، یا گذاشتن اثر جانبی داخل job. اولی جدول نیمه‌کاره به خواننده نشان می‌دهد، دومی با هر retry یک ایمیل یا یک ردیف تکراری می‌سازد. job دسته‌ای باید یا کاملاً تکرارشدنی باشد یا اثرش را جایی ثبت کند که دوبار اجرا نشود.Writing straight over the old output, or hiding a side effect inside the job. The first shows readers a half-built table; the second sends a duplicate email or inserts a duplicate row on every retry. A batch job must either be fully repeatable or record its effects somewhere that refuses a second run.

از کجا می‌فهمی فایل سالم رسیده؟How do you know the file arrived intact?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

سایت ثبت‌نام کد ملی‌ات را می‌گیرد و می‌گوید «این کد معتبر نیست». یک رقم را جابه‌جا تایپ کرده‌ای، ولی سیستم بدون اینکه کسی را بشناسد فهمید. رقم آخر کد ملی چیزی جز یک چک‌سام نیست: از بقیهٔ رقم‌ها حساب می‌شود و همین باعث می‌شود خطای تایپ زود گیر بیفتد.A signup form takes your national ID number and says “this number is not valid”. You transposed one digit, and the system caught it without knowing who you are. The last digit of that number is nothing but a checksum: it is computed from the others, which is why a typing slip is caught early.

تشبیه و تصویر ذهنیThe analogy

کد ملی فقط یک نمونه است؛ نوار مغناطیسی کارت بانکی و رقم‌های زیر بارکد هم چیزی شبیه همین دارند. نکتهٔ مهم این است که این عدد چه کاری می‌کند و چه کاری نمی‌کند. خطای تصادفی — یک رقم جابه‌جا، یک بیت برگشته — را با احتمال بالا می‌گیرد، چون هر تغییری عدد را عوض می‌کند. ولی جلوی کسی که عمداً هم داده را عوض کند و هم عدد را دوباره حساب کند، هیچ کاری نمی‌کند. برای آن حالت به چیز سنگین‌تری نیاز داری، چیزی که بدون کلید نشود دوباره ساخت.The national ID is just one example; the strip on a bank card and the digits under a barcode do something similar. The important part is what such a number does and does not do. It catches accidental damage — a digit swapped, a bit flipped — with high probability, because any change moves the number. It does nothing against someone who changes the data and recomputes the number on purpose. That needs something heavier, something that cannot be recomputed without a key.

و بعدش، فنیAnd now, technically

چک‌سام یک تابع است که رشتهٔ بایت را به یک عدد کوچک و ثابت نگاشت می‌کند، طوری که تغییر یک بیت با احتمال بالا عدد را عوض کند. در ذخیره‌سازی، این عدد کنار خود داده نگه داشته می‌شود. HDFS برای هر بلوک یک چک‌سام حساب می‌کند و آن را جدا ذخیره می‌کند؛ موقع خواندن دوباره حساب می‌شود و اگر نساخت، آن نسخه خراب علامت می‌خورد، از یک replica دیگر خوانده می‌شود و NameNode دستور ساختن نسخهٔ سالم را می‌دهد. همین کار در پس‌زمینه هم دوره‌ای انجام می‌شود تا خرابیِ بی‌صدا پیش از خواندن پیدا شود. نکتهٔ معماری این است که چک‌سام باید از سر تا ته باشد. اگر فقط در یک لینک شبکه بررسی شود، خرابی داخل رم یا روی دیسک دیده نمی‌شود، چون بایت‌های خراب از همان لینک سالم رد می‌شوند. TCP خودش یک چک‌سام ۱۶ بیتی دارد، ولی برای گرفتن خطاهای واقعیِ دیسک کافی نیست و اصلاً برای همین طراحی نشده.A checksum maps a byte string to a small fixed number, arranged so that changing one bit very likely changes the number. In storage the number is kept beside the data. HDFS computes a checksum per block and stores it separately; on read it recomputes it, and if the result does not match, that replica is marked corrupt, the block is read from another replica, and the NameNode orders a fresh copy. A background scanner does the same periodically so that silent damage is found before a reader trips over it. The architectural point is that the checksum has to be end to end. Verify it on one network link only and damage inside RAM or on a disk stays invisible, because corrupted bytes cross that link perfectly well. TCP carries a 16-bit checksum of its own, but it is far too weak for real disk errors and was never designed for them.

CRC-32چک‌سام معمول هر بلوک در HDFS و بسیاری از قالب‌های فایلthe usual per-block checksum in HDFS and many file formats
16 bitچک‌سام TCP؛ برای خطای دیسک کافی نیستTCP's checksum; too weak for disk errors
1 bitعوض شدن یک بیت باید چک‌سام را عوض کندone flipped bit should move the checksum

تلهٔ رایجThe usual trap

نوشتن چک‌سام و بررسی نکردنش موقع خواندن. خیلی از pipelineها موقع نوشتن عدد را حساب می‌کنند و کنار رکورد می‌گذارند، بعد موقع خواندن بی‌خیالش می‌شوند چون «دیسک سالم است». آن وقت فقط چند بایت اضافه ذخیره کرده‌ای و هیچ خرابی‌ای را نگرفته‌ای. چک‌سام ابزارِ خرابی است، نه ابزارِ امنیت.Writing checksums and never verifying them on read. Plenty of pipelines compute the number on write, put it next to the record, and then ignore it on read because “the disk is healthy”. You paid bytes for nothing and caught no damage. A checksum is a tool against damage, not a tool for security.

DAG

گراف جهت‌دار بی‌دور فصل ۱۱Chapter 11 · دیدن در متنsee in context

از کجا می‌دانی کدام کار باید اول انجام شود؟How do you know which piece of work must go first?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

خورش و برنج را با هم می‌پزی. اول پیاز را سرخ می‌کنی، بعد رب را اضافه می‌کنی؛ اما برنج کاری به خورش ندارد و می‌تواند هم‌زمان بپزد. هیچ دستور پختی نمی‌گوید برنج را بعد از خورش بپز تا خورش سرد شود و دوباره گرمش کنی. ترتیب کارها یک گراف جهت‌دار بی‌دور است و همین است که می‌گوید چه چیزی می‌تواند هم‌زمان اجرا شود.You are cooking stew and rice together. First you brown the onions, then add the tomato paste; but the rice has nothing to do with the stew and can cook at the same time. No recipe tells you to cook the rice after the stew so the stew can go cold and be reheated. The order of the work is a directed acyclic graph, and that is what tells you which parts can run side by side.

تشبیه و تصویر ذهنیThe analogy

آشپز این نقشه را روی کاغذ نمی‌نویسد، ولی در سرش دارد. اگر کسی هم‌زمان سالاد درست کند، دو شاخه موازی پیش می‌رود و کسی معطل نمی‌شود. اگر گاز قطع شود و خورش نیمه‌کاره بماند، فقط همان شاخه از اول شروع می‌شود؛ برنجی که تمام شده دست نمی‌خورد. بهایش این است که باید نقشه را نگه داری و به‌روزش کنی: یک پیش‌نیاز از قلم بیفتد، همه چیز به هم می‌ریزد. تازه، نقشه فقط می‌گوید چه چیزی به چه چیزی وابسته است؛ نمی‌گوید هر کار چقدر طول می‌کشد یا چقدر بزرگ است.The cook never draws that map, but carries it in their head. If someone makes the salad at the same time, two branches run in parallel and nobody waits. If the gas is cut and the stew is half done, only that branch starts over; the finished rice is untouched. The bill is that you have to hold the map and keep it current: drop one prerequisite and everything tangles. And the map only says what depends on what; it says nothing about how long a step takes or how big it is.

یک کوئری تحلیلیOne analytical query۱ · خواندن1 · Scan۲ · فیلتر2 · Filter۳ · join3 · Join۴ · تجمیع4 · Aggregateیال‌ها یک‌جهته‌اند و هیچ دوری نیست، پس همیشه می‌شود ترتیبی برای اجرا پیدا کردArrows point one way and no loop exists, so an execution order always exists
گراف وابستگی: هر گره یک عملگر و هر یال یک پیش‌نیاز. جهت‌ها ثابت‌اند و دوری وجود ندارد.A dependency graph: each node an operator, each arrow a prerequisite, with fixed directions and no loops.

و بعدش، فنیAnd now, technically

گراف جهت‌دار بی‌دور، گراف وابستگی کارهاست: هر گره یک عملگر یا یک task است و هر یال می‌گوید خروجی این، ورودی آن است. بی‌دور بودن یعنی می‌توانی ترتیبی خطی از گره‌ها پیدا کنی که هیچ یال آن را نقض نکند؛ همان ترتیب توپولوژیک. زمان‌بند از همین گراف استفاده می‌کند تا بفهمد چه چیزی می‌تواند موازی اجرا شود، کجا باید منتظر بماند، و کدام میانه‌ها باید روی دیسک بنشینند. اگر گراف دور داشته باشد، هیچ ترتیبی وجود ندارد: هر گره منتظر گره بعدی است و کار هرگز شروع نمی‌شود. در پردازش دسته‌ای، برگ‌های گراف خروجی job و ریشه‌هایش ورودی خام‌اند. در MapReduce به همان شکلی که توصیف شد، چنین گرافی برای کل کار وجود ندارد: هر job فقط برش ثابت خودش را می‌شناسد و از job قبل و بعد بی‌خبر است. گراف به خودی خود هزینه‌ای ندارد؛ هزینه جایی است که یک یال، جابه‌جایی یک ترابایت داده بین دو مرحله باشد.A directed acyclic graph is the dependency graph of the work: each node is an operator or a task, and each edge says that this node's output is that node's input. Acyclic means you can find a linear order of the nodes that violates no edge, which is the topological order. The scheduler uses that graph to see what can run in parallel, where it must wait, and which intermediates have to land on disk. If the graph had a cycle there would be no order at all: every node waits for the next and the work never starts. In batch, the leaves are job outputs and the roots are raw input. In MapReduce as originally described there is no such graph for the whole job: each job knows only its own fixed cut and nothing of the job before or after it. The graph itself costs nothing; the cost is an edge that moves a terabyte between two stages.

0 cyclesبدون دور؛ با دور هیچ ترتیبی برای شروع پیدا نمی‌شودno cycles; with a cycle no starting order exists
N nodesهر گره یک عملگر، و یال‌ها وابستگی داده‌اندeach node an operator, each edge a data dependency
1 edgeهر یال یک جابه‌جایی داده که می‌تواند گران‌ترین بخش باشدone edge can be the most expensive part of the job

دور، و چرا ممنوع استCycles, and why they are banned

دور وقتی در گراف می‌افتد که یک مرحله، مستقیم یا غیرمستقیم، به خروجی خودش وابسته شود. یک نمونهٔ واقعی: job اصلاحی که خروجی دیروز را می‌خواند و دوباره روی همان خروجی می‌نویسد. یک بار که جواب بدهد همه راضی‌اند، ولی روزی که ورودی عقب باشد، job منتظر خروجی خودش می‌ماند. زمان‌بند چنین چیزی را نمی‌تواند مرتب کند، چون هیچ نقطه‌ای وجود ندارد که بتوان از آن شروع کرد. راه‌حل معمولاً شکستن دور با یک مرحلهٔ میانی یا یک نسخهٔ تاریخ‌دار است.A cycle appears when a stage depends, directly or indirectly, on its own output. A real example: a fix-up job that reads yesterday's output and writes back over it. It works the first time and everyone is happy, but on the day the input lags, the job waits for its own output. No scheduler can order that, because there is no point from which to start. The usual fix is to break the cycle with an intermediate stage or a dated version.

همهٔ گراف یکسان نیستNot every node is equal

بین گره‌هایی که هر رکورد را جدا پردازش می‌کنند و گره‌هایی که باید همهٔ رکوردهای یک کلید را ببینند تفاوت بزرگی است. نوع اول را می‌شود بدون جابه‌جایی داده و در همان ماشین اجرا کرد؛ نوع دوم یک shuffle کامل می‌خواهد، یعنی هر رکورد از شبکه رد شود و به گره درست برسد. به همین دلیل است که یک گراف کوچک روی کاغذ می‌تواند گران‌ترین بخش کار باشد: پهنای یک یال، نه تعداد گره‌ها، هزینه را تعیین می‌کند.There is a big difference between nodes that process each record on its own and nodes that must see every record for a key. The first kind runs on one machine without moving data; the second needs a full shuffle, meaning every record crosses the network to the right node. That is why a small graph on paper can be the expensive part of the job: the width of one edge, not the number of nodes, sets the price.

تلهٔ رایجThe usual trap

گراف را با زمان‌بندی اشتباه گرفتن. گراف فقط می‌گوید چه چیزی به چه چیزی وابسته است؛ نمی‌گوید روی چند ماشین اجرا شود، کجا نقطهٔ ذخیره بگذاری، یا کدام یال گران است. تیم‌ها یک گراف تمیز می‌کشند و خیال می‌کنند کار تمام است، بعد در تولید می‌بینند یک یال تمام شبکه را پر کرده.Mistaking the graph for the schedule. The graph says what depends on what; it does not say how many machines to use, where to checkpoint, or which edge is expensive. Teams draw a tidy DAG and think the work is done, then find in production that one edge has filled the network.

دیسک سالم است، ولی بایت‌ها عوض شده‌اند. چطور؟The disk is healthy, yet the bytes changed. How?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

یک آلبوم موسیقی را دانلود می‌کنی. حجم فایل همان است که باید باشد، آلبوم هم باز می‌شود، ولی وسط آهنگ هفتم یک صدای سوت می‌آید و بقیه‌اش پخش نمی‌شود. هیچ خطایی ندیدی، هیچ دانلودی قطع نشد؛ فقط چند بایت آنجا که باید، نیستند. این همان خرابی داده است.You download an album. The file size is exactly right, the album opens, but in the middle of track seven there is a burst of noise and the rest will not play. No error appeared, no download was interrupted; a few bytes simply are not what they should be. That is data corruption.

تشبیه و تصویر ذهنیThe analogy

چیزی که این را خطرناک می‌کند سکوتش است. یک دیسک سوخته داد می‌زند؛ یک بایت برگشته هیچ صدایی نمی‌دهد و برنامه هم غالباً متوجه نمی‌شود. در مقیاس یک خوشه، حتی نرخ بسیار کم هم بالاخره به یک فایل خراب می‌انجامد. بدتر، تکثیر کردن داده به‌تنهایی مشکل را حل نمی‌کند. اگر سه نسخه داشته باشی و یکی خراب شود، کدام درست است؟ بدون یک چک‌سام که هر نسخه را جدا تأیید کند، سه نسخه داری و یک حدس. به همین دلیل است که سیستم‌های جدی، چک‌سام را همراه خود داده نگه می‌دارند، نه جدا و نه فقط در لایهٔ شبکه.What makes it dangerous is the silence. A dead disk shouts; a flipped bit says nothing and the application usually does not notice. At the scale of a cluster, even a tiny rate eventually means a broken file every month or so. Worse, replicating data does not solve it by itself. With three copies and one gone bad, which is correct? Without a checksum that vouches for each copy separately you have three copies and a guess. That is why serious systems keep a checksum with the data itself, not apart from it and not only in the network layer.

و بعدش، فنیAnd now, technically

خرابی داده می‌تواند در هر جایی از مسیر رخ دهد: سطح رسانه با گذشت زمان، فریم‌ور دیسک یا کنترلر، رم، گذرگاه، شبکه، یا یک باگ نرم‌افزاری که بایت را در جای اشتباه می‌نویسد. دیسک‌ها خودشان ECC دارند و خطاهای زیادی را تصحیح می‌کنند، ولی ECC کران دارد و خطاهای بزرگ‌تر یا خطاهای بالاتر از لایهٔ دیسک را نمی‌گیرد. نوشتن نیمه‌کاره هم شکلی از خرابی است که با قطع برق رخ می‌دهد و صفحه را ناسازگار می‌گذارد. کشف، همیشه با یک چک‌سام یا هش انجام می‌شود که از منبع حساب شده و در مقصد بررسی می‌شود. یک نکتهٔ ظریف این است که بررسی باید end-to-end باشد: اگر چک‌سام فقط روی هر لینک شبکه بررسی شود، خرابی داخل یک ماشین از دست می‌رود، چون بایت‌های خراب از همان لینک سالم رد می‌شوند. سیستم‌فایل‌هایی مثل ZFS و Btrfs چک‌سام را روی خود داده نگه می‌دارند و می‌توانند نسخهٔ سالم را از replica برگردانند؛ بدون چنین چیزی، خرابی فقط وقتی فهمیده می‌شود که کسی داده را بخواند و بفهمد غلط است — که همیشه دیر است.Corruption can happen anywhere along the path: the medium over time, disk firmware or a controller, RAM, the bus, the network, or a software bug that writes a byte in the wrong place. Disks carry ECC and correct many errors themselves, but ECC has a bound and does not cover larger faults or anything above the disk layer. A torn write is another shape of the same problem: power fails mid-page and leaves the page inconsistent. Detection always comes from a checksum or hash computed at the source and checked at the destination. The subtle part is that the check must be end to end. Verify only per network link and corruption inside a machine disappears, because the bad bytes travel that link unharmed. Filesystems such as ZFS and Btrfs keep checksums with the data and can restore a good copy from a replica. Without that, damage is found only when someone reads the data and notices it is wrong — which is always late.

≈ 10⁻¹⁵نرخ خطای خواندنِ اعلامی سازندگان دیسک، به‌ازای هر بیتthe read error rate disk makers quote, per bit
2 copiesدو نسخهٔ متفاوت بدون چک‌سام یعنی نمی‌دانی کدام درست استtwo differing copies and no checksum means no way to choose
ECCتصحیح خطای داخلی؛ کران دارد و همه‌چیز را نمی‌گیردon-device error correction; bounded, and not everything

تلهٔ رایجThe usual trap

تکیه بر replication به‌جای چک‌سام. سه نسخه داری، خیالت راحت است، تا روزی که یک نسخه خراب شود و نتوانی بگویی کدام. بدتر وقتی است که خرابی به نسخهٔ سالم هم سرایت کند، چون هیچ لایه‌ای آن را ندیده است.Leaning on replication instead of a checksum. Three copies feel safe until one goes bad and you cannot say which. Worse is the day the damage spreads to the healthy copy, because no layer ever looked at it.

چرا جواب را بین مراحل روی دیسک بگذاری؟Why park the answer on disk between every stage?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

کارگاه خیاطی سه نفر دارد: یکی پارچه می‌برد، یکی می‌دوزد، یکی اتو می‌کند. اگر قرار باشد بعد از هر پیراهن، همهٔ پیراهن‌های نیمه‌کاره در انبار گذاشته و بعد دوباره بیرون آورده شوند، کارگاه دو برابر کار می‌کند. در عوض، هر تکه به‌محض آماده شدن دست نفر بعد می‌رود. جریان داده همین است: مراحل بدون توقف روی دیسک، جواب را به هم پاس می‌دهند.A tailoring workshop has three people: one cuts cloth, one sews, one irons. If every shirt had to go into the storeroom between each pair of hands and come back out again, the shop would do twice the work. Instead each piece goes to the next person the moment it is ready. Dataflow is that: stages pass the answer along without parking it on disk.

تشبیه و تصویر ذهنیThe analogy

این کار سرعت را بالا می‌برد، ولی نظم را سخت‌تر می‌کند: اگر نفر وسط غیبت کند، نفر اول نمی‌داند تا کجا کار کند و نفر آخر معطل می‌ماند. برای همین در کارگاه واقعی چند تکهٔ نیمه‌کاره روی میز می‌ماند تا خط بخواب نکند؛ در سیستم هم هر چند وقت یک‌بار باید یک نقطهٔ ذخیره بگذاری. بهای دیگرش این است که اگر یک تکه خراب باشد پیدا کردنش سخت‌تر است، چون هیچ فایل کاملی از میانهٔ کار باقی نمانده که نگاهش کنی.That raises the speed but complicates the order: if the middle person is away, the first does not know how far to work and the last just waits. So a real workshop leaves a few half-finished pieces on the bench to keep the line moving; in a system you place a checkpoint every so often. The other bill is that a bad piece is harder to find, because no complete file from the middle of the work is left to look at.

ورودیInputفایل یا لاگfile or logmap و filterMap and filterدر حافظهin memoryjoinJoinhash یا sort-mergehash or sort-mergeخروجیOutputدادهٔ مشتقderived data
جریان داده عملگرها را پشت هم می‌چیند و داده را در حافظه جلو می‌برد، نه با نوشتن هر مرحله روی دیسک.Dataflow chains operators and moves data forward in memory instead of writing every stage to disk.

و بعدش، فنیAnd now, technically

موتور جریان داده، job را به‌صورت گرافی از عملگرها می‌بیند و خودش تصمیم می‌گیرد هر کدام کجا و به چه ترتیبی اجرا شود. برخلاف MapReduce که شکل ثابت map و shuffle و reduce دارد، این‌جا می‌توانی filter و map را در همان مرحله نگه داری، برای یک طرف کوچک join از broadcast استفاده کنی و برای دو طرف بزرگ از sort-merge. عملگرهای پیوسته می‌توانند هم‌زمان اجرا شوند: تا وقتی یک تکه از خروجی مرحلهٔ اول آماده شد، مرحلهٔ دوم روی همان تکه شروع می‌کند و منتظر کل خروجی نمی‌ماند. ولی هر جا وابستگی پهنا باشد، یعنی هر رکورد به همهٔ رکوردها نگاه کند، باز هم باید منتظر ماند و داده از شبکه رد شود. برای تحمل خرابی هم یا باید نقطهٔ ذخیره گرفت یا از شجره‌نامه فهمید کدام تکه از دست رفته و فقط همان را از نو ساخت.A dataflow engine sees the job as a graph of operators and decides where and in what order each one runs. Unlike MapReduce with its fixed map, shuffle and reduce shape, you can keep filter and map in the same stage, broadcast the small side of a join, and sort-merge the two large sides. Pipelined operators can run at once: as soon as one chunk of the first stage's output is ready, the second stage starts on that chunk instead of waiting for all of it. But wherever the dependency is wide, meaning every record must look at every other, you still have to wait and the data still crosses the network. For fault tolerance you either checkpoint or read the lineage to see which partition was lost and rebuild only that.

pipelinedداده بین عملگرها در حافظه جریان دارد، نه روی دیسکdata flows between operators in memory, not on disk
1 shuffleهر وابستگی پهنا یک عبور کامل از شبکه می‌خواهدevery wide dependency costs a full network pass
checkpointنقطه‌ای که وضعیت روی دیسک می‌نشیند تا بازسازی ارزان شودa point where state lands on disk so recovery is cheap

کجا باید روی دیسک بنشیندWhere it still has to touch disk

هیچ موتوری واقعاً همه چیز را در هوا نگه نمی‌دارد. هر جا مرز shuffle باشد، داده باید دسته‌بندی و مرتب شود و عملاً روی دیسک می‌نشیند؛ هر جا نقطهٔ ذخیره باشد، وضعیت عمداً نوشته می‌شود تا بعد از خرابی از آن‌جا ادامه دهند. انتخاب واقعی این است که کجا این توقف‌ها را بگذاری: توقف کمتر یعنی حافظهٔ بیشتر و بازسازی گران‌تر بعد از خرابی، توقف بیشتر یعنی اجرای کندتر ولی بازیابی ارزان‌تر.No engine truly keeps everything in the air. At every shuffle boundary the data must be partitioned and sorted, which in practice puts it on disk; at every checkpoint the state is written on purpose so a failure can resume from there. The real choice is where to place those stops: fewer stops means more memory and a more expensive rebuild after a failure, more stops means a slower run but cheaper recovery.

چرا این هم رایگان نیستWhy this is not free either

نگه داشتن داده در حافظه یعنی اگر یک ماشین بمیرد، آن تکه دیگر جایی نیست و باید از ورودی بازسازی شود. اگر ورودی هم از بین رفته باشد، بازسازی یعنی دوباره خواندن چند مرحله، و هزینه‌اش با هر مرحله ضرب می‌شود. تازه، وقتی عملگرها پشت سر هم و هم‌زمان اجرا می‌شوند، یک کندی در وسط به عقب فشار می‌آورد و صف‌ها پر می‌شوند؛ این فشار برگشتی نشانهٔ خرابی نیست، نشانهٔ این است که مصرف‌کننده از تولیدکننده کندتر است.Keeping data in memory means that if a machine dies, that partition is nowhere and must be rebuilt from the input. If the input is gone too, the rebuild means re-reading several stages, and the cost multiplies per stage. And when operators run pipelined at once, one slow stage pushes back and queues fill; that backpressure is not a failure, it is the consumer being slower than the producer.

تلهٔ رایجThe usual trap

خیال کردن این که جریان داده یعنی بدون shuffle. هر join یا group by روی دو ورودی بزرگ یک عبور کامل از شبکه است، و اگر روی انبار شیء اجرا شود همان عبور هم گران‌تر می‌شود. تیم‌ها گراف را روی کاغذ قشنگ می‌کنند و بعد می‌بینند یک join کل خوشه را قفل کرده.Assuming dataflow means no shuffle. Any join or group by over two large inputs is a full network pass, and on an object store that pass costs more. Teams draw a beautiful graph, then watch one join lock up the whole cluster.

همان ورودی، همان خروجی — همیشه؟Same input, same output — always?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

دو بار همان دستور پخت را اجرا می‌کنی و کیک‌ها تقریباً یکسان درمی‌آیند. «تقریباً» را گذاشتم چون فر عوض شد، آرد عوض شد، و آشپزخانه متغیرهایی داشت که در دستور نوشته نشده بودند. تابع قطعی همان دستور است، ولی فقط وقتی که هیچ متغیر بیرونی‌ای در کار نباشد.You bake the same recipe twice and get cakes that are almost identical. I said almost because the oven changed, the flour changed, and the kitchen had variables the recipe never mentions. A deterministic function is that recipe, but only when nothing outside it takes part.

تشبیه و تصویر ذهنیThe analogy

چرا این‌قدر مهم است؟ چون در batch و stream، هر وظیفه ممکن است شکست بخورد و دوباره اجرا شود، و فریم‌ورک روی این حساب می‌کند که اجرای دوباره همان جواب را بدهد. اگر تابعی ساعت سیستم را بخواند، یک عدد تصادفی بسازد، یا شناسهٔ تازه بگیرد، دو تلاش از یک وظیفه دو نتیجهٔ متفاوت می‌دهند و هیچ‌کس نمی‌تواند بگوید کدام «همان» است. خطرناک‌تر، متغیرهای پنهان‌اند. یک تابع ممکن است قطعی به نظر برسد ولی ترتیب پیمایش یک هش‌مپ، خواندن یک تنظیمات در زمان اجرا، یا ترتیب جمع کردن اعداد اعشاری در آن اثر بگذارد. قطعی بودن، خاصیت کل مسیر است، نه یک خط کد.Why does it matter so much? In batch and stream processing any task may fail and run again, and the framework counts on the re-run producing the same answer. If a function reads the system clock, draws a random number, or mints a fresh identifier, two attempts of one task give two different results and nobody can say which one is “the” result. More dangerous are the hidden variables. A function can look deterministic while the iteration order of a hash map, a setting read at runtime, or the order in which floating-point numbers are added changes what it returns. Determinism is a property of the whole path, not of one line of code.

و بعدش، فنیAnd now, technically

در پردازش توزیع‌شده، قطعی بودن پایهٔ چند چیز است: تلاش دوباره، اجرای حدسی، بازپخش از لاگ، و بازیابی بعد از خرابی نود. اگر تابع قطعی باشد، فریم‌ورک می‌تواند نتیجهٔ یک تلاش را دور بریزد و تلاش دیگر را نگه دارد، یا یک نسخهٔ کند را با نسخهٔ سریع‌تر عوض کند، بدون این‌که کسی تفاوت را ببیند. منابع ناهمسانی معمولاً همین‌ها هستند: ساعت دیواری، مولد تصادفی بدون seed، UUID، ترتیب پیمایش یک دیکشنری، خواندن وضعیت بیرونی که ممکن است عوض شده باشد، و جمع اعداد اعشاری که جابه‌جایی‌پذیر نیست. راه‌حل معمول این است که این مقادیر را از قبل تثبیت کنی — seed را از شمارهٔ وظیفه بگیری، زمان را از پارامتر بدهی، و برای اعداد اعشاری از جمع صحیح یا کتابخانه‌ای با ترتیب ثابت استفاده کنی. اگر نشد، باید نتیجهٔ تولیدشده را جایی ثبت کنی تا تلاش دوباره همان را بازگرداند، نه یک مقدار تازه.In distributed processing, determinism is the ground under retries, speculative execution, replay from a log, and recovery after a node dies. If the function is deterministic, the framework may discard one attempt and keep another, or replace a slow copy with a fast one, and nobody can tell the difference. The usual sources of divergence are the wall clock, a random generator without a fixed seed, UUIDs, the iteration order of a dictionary, reading external state that may have moved on, and floating-point addition, which is not associative. The usual fix is to pin those values: derive the seed from the task number, pass time in as a parameter, and aggregate decimals as integers or through a library that fixes the order. If you cannot, record what was produced so that a retry returns that instead of a fresh value.

f(x) = f(x)هر بار با همان ورودی، همان خروجیthe same input gives the same output, every time
time · random · UUIDسه منبع رایج ناهمسانی در کدthree common sources of divergence in code
1 seedتصادفی بدون seed ثابت، هر بار جواب دیگری می‌دهدrandomness without a fixed seed answers differently each time

تلهٔ رایجThe usual trap

تست کردن تابع با یک ورودی و قطعی نامیدنش. تابعی که یک بار جواب درست داده ممکن است بار دوم، روی ماشین دیگر و با ترتیب دیگر، جواب دیگری بدهد. نشانه‌اش این است که job دوباره اجرا می‌شود و اعداد کمی فرق می‌کنند؛ همان‌جا باید به منابع پنهان شک کنی.Testing a function once and calling it deterministic. A function that gave the right answer once can give another answer on a different machine with a different order. The symptom is a job that is re-run and comes out slightly different; that is the moment to suspect hidden inputs.

distributed filesystem

فایل‌سیستم توزیع‌شده فصل ۱۱Chapter 11 · دیدن در متنsee in context

فایلی بزرگ‌تر از هر دیسک را کجا می‌گذاری؟Where do you put a file bigger than any single disk?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

عکس‌های خانوادگی را بین سه لپ‌تاپ پخش کرده‌ای: پارسال پیش خواهرت، قبلش پیش پسرعمو، و هر کدام یک کپی از مهم‌ترین‌ها دارند. یک دفتر کوچک داری که نوشته کدام عکس کجاست. اگر یک لپ‌تاپ بسوزد، عکس‌هایش از بین نمی‌روند چون کپی دارند؛ برای پیدا کردن هر عکس هم اول دفتر را نگاه می‌کنی. فایل‌سیستم توزیع‌شده همین دفتر و همین پخش‌کردن است، با بلوک‌های ۱۲۸ مگابایتی به‌جای آلبوم.You have spread the family photos over three laptops: last year's are at your sister's, the year before at a cousin's, and each keeps a copy of the most important ones. A small notebook says which photo is where. If one laptop burns, its photos survive because copies exist; to find any photo you look in the notebook first. A distributed filesystem is that notebook and that spreading, with 128 MB blocks instead of albums.

تشبیه و تصویر ذهنیThe analogy

چرا بلوک بزرگ؟ چون خواندن دنباله‌ای ارزان است و دفتر کوچک می‌ماند: یک فایل ده‌ترابایتی می‌شود چند ده هزار بلوک، و دفتر فقط همین ده‌ها هزار آدرس را نگه می‌دارد، نه آدرس هر بایت را. چرا چند کپی؟ چون یک دیسک خراب می‌شود و کسی نباید بیدار شود. بهایش این است که سه برابر فضا می‌دهی و هر نوشتن باید به هر سه جا برسد؛ و اگر هزاران فایل کوچک بسازی، دفتر از خود داده بزرگ‌تر می‌شود و گلوگاه می‌شود.Why big blocks? Because sequential reading is cheap and the catalogue stays small: a ten-terabyte file becomes tens of thousands of blocks, and the notebook holds those tens of thousands of addresses rather than an address per byte. Why several copies? Because a disk will fail and nobody should have to wake up. The bill is triple the space and a write that must reach all three places; and if you create thousands of tiny files, the notebook grows bigger than the data and becomes the bottleneck.

یک فایل بزرگOne large fileبلوک ۱ · گره ABlock 1 · node Aبلوک ۲ · گره BBlock 2 · node Bبلوک ۳ · گره CBlock 3 · node Cهر بلوک روی چند گره کپی می‌شود و NameNode می‌داند کدام بلوک کجاستEach block is copied onto several nodes, and the NameNode knows where every block lives
یک فایل بزرگ به بلوک‌های ثابت می‌شکند و هر بلوک روی چند گره کپی می‌شود؛ فهرست مرکزی جای همه را می‌داند.One big file becomes fixed blocks, each copied onto several nodes, and a central catalogue knows every location.

و بعدش، فنیAnd now, technically

فایل‌سیستم توزیع‌شده از سه بخش ساخته می‌شود: گره‌های داده که بلوک‌ها را روی دیسک خود نگه می‌دارند، یک گرهٔ فهرست یا NameNode که نام فایل‌ها و نقشهٔ بلوک به گره را در حافظه دارد، و کلاینتی که بلوک به بلوک می‌خواند. اندازهٔ بلوک معمولاً ۱۲۸ مگابایت است و هر بلوک روی سه گره کپی می‌شود. زمان‌بند job را روی همان گره‌ای می‌فرستد که بلوک را دارد، تا خواندن از دیسک محلی باشد نه از شبکه. فایل‌ها فقط-افزودنی‌اند و یک نویسنده دارند؛ تغییر نام فقط یک تغییر در فهرست است و ارزان. نقطهٔ ضعف اصلی سر جای دیگری است: فهرست در حافظه است، پس هر فایل کوچک بخشی از آن حافظه را می‌خورد، و صد میلیون فایل کوچک می‌تواند از خود داده بزرگ‌تر شود. یک NameNode هم گلوگاه است، هرچند نسخهٔ standby دارد.A distributed filesystem has three parts: data nodes holding blocks on their own disks, one catalogue node or NameNode keeping the filename and block-to-node map in memory, and a client that reads block by block. Blocks are usually 128 MB, and each is copied onto three nodes. The scheduler places a job on a node that holds the block, so the read comes from a local disk rather than the network. Files are append-only with a single writer; a rename is just a metadata change and is cheap. The real weakness is elsewhere: the catalogue lives in memory, so every small file consumes some of it, and a hundred million small files can outweigh the data itself. One NameNode is also a bottleneck, even with a standby.

128 MBاندازهٔ معمول یک بلوک، بزرگ تا فهرست کوچک بماندa typical block, large so the catalogue stays small
3 copiesتکثیر معمول هر بلوک روی گره‌های مختلفthe usual replication of each block across nodes
1 NameNodeفهرست مرکزی در حافظه؛ گلوگاه و نقطهٔ حساسone in-memory catalogue: bottleneck and single point of care

چرا محاسبه را نزدیک داده می‌برندWhy the work goes to the data

شبکه در این مقیاس گران‌ترین منبع است. اگر job را روی ماشینی بفرستی که بلوک را ندارد، خواندن یک بلوک ۱۲۸ مگابایتی می‌شود هزاران بسته روی شبکه؛ اگر همان ماشین را انتخاب کنی، همان خواندن از دیسک محلی است. به همین دلیل زمان‌بند اول جاهای داده را نگاه می‌کند و بعد ظرفیت آزاد. وقتی همهٔ گره‌ها پر باشند این بهینه‌سازی دیگر ممکن نیست و کار به شبکه برمی‌گردد.At this scale the network is the most expensive resource. Send the job to a machine that does not hold the block and reading 128 MB becomes thousands of packets; pick the machine that does and the same read is a local disk access. That is why the scheduler looks at data placement before free capacity. When every node is busy, that optimisation is no longer available and the work falls back to the network.

کجا این طرح می‌شکندWhere the design breaks

با فایل‌های کوچک زیاد، فهرست پر می‌شود و نوشتن هر فایل یک رفت‌وبرگشت به NameNode می‌خواهد. با نوشتن تصادفی در میان فایل هم هیچ چیز به نفع تو نیست: فایل فقط-افزودنی است و نمی‌شود وسطش را عوض کرد. با تأخیر بسیار کم هم مناسب نیست، چون هر خواندن از فهرست شروع می‌شود. این سه محدودیت است که خیلی‌ها را به انبار شیء می‌فرستد.With many small files the catalogue fills up and every file creation needs a round trip to the NameNode. Random writes in the middle of a file buy you nothing either: the file is append-only and cannot be edited in place. And it is a poor fit for very low latency, because every read starts at the catalogue. Those three limits are what push many teams to an object store.

تلهٔ رایجThe usual trap

استفاده از فایل‌سیستم توزیع‌شده مثل یک فایل‌سیستم معمولی: ساختن هزاران فایل یک‌کیلوبایتی، و تغییر نام پوشه‌ها به‌جای نوشتن یک خروجی بزرگ. اولی حافظهٔ NameNode را می‌خورد، دومی فقط یک تغییر فهرست است ولی باعث می‌شود job بعدی هزاران فایل کوچک باز کند. هر دو در تولید دیده می‌شوند و هر دو با یک تصمیم ساده در نوشتن خروجی حل می‌شوند.Treating a distributed filesystem like a normal one: thousands of one-kilobyte files, and directory renames instead of one large output. The first eats NameNode memory; the second is only a metadata change, but it makes the next job open thousands of small files. Both show up in production, and both are fixed by one decision about how you write output.

external sort

مرتب‌سازی بیرونی فصل ۱۱Chapter 11 · دیدن در متنsee in context

وقتی میز جواب نمی‌دهد، ۹۰۰ برگه را چطور مرتب می‌کنی؟The desk is too small. How do you sort 900 papers?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

معلمی ۹۰۰ برگهٔ امتحان دارد و میز فقط ۱۰۰ تا را جا می‌دهد. اول ۱۰۰ تا را روی میز مرتب می‌کند و در یک سینی می‌گذارد؛ همین کار را با بقیه می‌کند تا نه سینی مرتب داشته باشد. بعد از روی هر سینی برگهٔ اول را برمی‌دارد و کوچک‌ترین را می‌گذارد روی دستهٔ نهایی. مرتب‌سازی بیرونی همین است: تکه‌های مرتب، بعد ادغام.A teacher has nine hundred exam papers and the desk holds a hundred. She sorts the first hundred on the desk and puts them in a tray, then repeats until she has nine sorted trays. Then she takes the top paper from each tray and puts the smallest on the final pile. That is external sort: sorted runs first, then a merge.

تشبیه و تصویر ذهنیThe analogy

چرا نه سینی و نه یک سینی بزرگ؟ چون اگر همه را یک‌جا بریزی جا نداری. ادغام همان لحظه جواب می‌دهد چون هر سینی از قبل مرتب است: لازم نیست دنبال کوچک‌ترین در کل ۹۰۰ برگه بگردی، فقط بالای هر سینی را نگاه می‌کنی. بهایش دو چیز است: هر برگه دو بار دست می‌خورد، یک بار موقع ساختن سینی و یک بار موقع ادغام، و اگر تعداد سینی‌ها از حدی بیشتر شود باید چند دور ادغام کنی، چون نمی‌توانی هم‌زمان صد سینی باز نگه داری.Why trays at all, rather than one big pile? Because pouring everything out at once does not fit. The merge works precisely because each tray is already sorted: you do not search all nine hundred papers, you look at the top of each tray. The bill is twofold: every paper is handled twice, once to build a tray and once to merge, and if there are too many trays you need several merge rounds, because you cannot keep a hundred trays open at once.

۱ · تکه‌های در حافظه1 · In-memory chunks۲ · ران‌های مرتب روی دیسک2 · Sorted runs on disk۳ · ادغام k-طرفه3 · k-way merge۴ · خروجی مرتب4 · Sorted output
چهار پلهٔ مرتب‌سازی بیرونی: تکه‌های در حافظه، ران‌های مرتب روی دیسک، ادغام، خروجی نهایی.Four rungs of external sort: in-memory chunks, sorted runs on disk, a k-way merge, and the final output.

و بعدش، فنیAnd now, technically

وقتی داده از حافظه بزرگ‌تر است، مرتب‌سازی در دو مرحله انجام می‌شود. اول داده را تکه‌تکه می‌خوانی، هر تکه را که در حافظه جا می‌شود مرتب می‌کنی و به‌صورت یک ران مرتب روی دیسک می‌نویسی. بعد با یک ادغام k-طرفه، از هر ران یک بافر کوچک نگه می‌داری، کوچک‌ترین سر را برمی‌داری و می‌نویسی. حافظهٔ لازم به k بافر محدود می‌شود، پس k را با رم و تعداد فایل‌های باز تنظیم می‌کنی. اگر k از حد بگذرد، ادغام را در چند دور انجام می‌دهی. هزینهٔ اصلی I/O است: هر بایت حداقل دو بار خوانده و نوشته می‌شود، یک بار برای ساختن ران و یک بار برای ادغام، به‌علاوهٔ خروجی. join هم روی همین پایه ساخته می‌شود: اگر هر دو طرف را بر اساس کلید join مرتب کنی، ادغام آن‌ها یک عبور ترتیبی است و هر طرف فقط یک بار مرتب می‌شود. در MapReduce همین کار به‌صورت توزیع‌شده در shuffle انجام می‌شود.When data outgrows memory, sorting happens in two moves. First you read chunk by chunk, sort each chunk that fits in memory, and write it out as a sorted run. Then a k-way merge holds one small buffer per run, takes the smallest head, and writes it out. Memory is bounded by k buffers, so k is set by RAM and the number of open files; if k has to be larger, the merge takes several rounds. The real cost is I/O: every byte is read and written at least twice, once to build runs and once to merge, plus the output. Joins are built on the same base: sort both sides by the join key and merging them is one sequential pass, with each side sorted once. In MapReduce this happens distributed, inside the shuffle.

2 passesهر بایت حداقل دو بار خوانده و نوشته می‌شودevery byte is read and written at least twice
k-wayتعداد ران‌هایی که هم‌زمان ادغام می‌شوند، محدود به حافظهhow many runs merge at once, bounded by memory
M ≪ Nحافظهٔ یک ماشین در برابر حجم داده‌ای که باید مرتب شودone machine's memory against the data being sorted

ادغام k-طرفه دقیقاً چه می‌کندWhat a k-way merge actually does

یک heap کوچک از سر هر ران نگه می‌داری؛ کوچک‌ترین را بیرون می‌دهی، از همان ران رکورد بعدی را می‌خوانی و دوباره heap را مرتب می‌کنی. این کار وقتی خوب است که هر ران ترتیبی خوانده شود؛ اگر برای هر رکورد دنبال جای تصادفی بروی، همان مزیت ترتیبی از دست می‌رود. حافظه‌ات عملاً به تعداد ران‌ها تقسیم می‌شود، پس ران‌های بزرگ‌تر یعنی k کوچک‌تر و ادغام ساده‌تر. انتخاب اندازهٔ ران، همان معاملهٔ همیشگی بین حافظه و تعداد دورهای ادغام است.You keep a small heap of the head of each run; pop the smallest, read the next record from that run, and restore the heap. This only pays if each run is read sequentially; jump to a random place for every record and the sequential advantage disappears. Memory is effectively divided by the number of runs, so larger runs mean a smaller k and a simpler merge. Choosing the run size is the familiar trade between memory and the number of merge rounds.

sort-merge join و بهایشSort-merge join and its bill

اگر ورودی‌ها را بر اساس کلید join مرتب کنی، هر کلید در دو طرف پشت سر هم می‌آید و ادغام ساده می‌شود. مزیت بزرگش این است که مرتب‌سازی هر طرف یک بار انجام می‌شود و می‌تواند برای کوئری‌های بعدی هم بماند. عیبش این است که اگر داده تازه برسد باید دوباره مرتب شود، و اگر فقط یک کوئری داری، مرتب کردن دو ورودی بزرگ از خود join گران‌تر است.Sort both inputs by the join key and every key appears contiguously on both sides, which makes the merge trivial. The big advantage is that each side is sorted once and that order can serve later queries. The drawback is that fresh data forces a re-sort, and for a single query, sorting two large inputs costs more than the join itself.

تلهٔ رایجThe usual trap

دست‌کم گرفتن فضای موقت. مرتب‌سازی بیرونی به اندازهٔ ورودی فضای اضافی می‌خواهد و اگر دیسک پر شود، job وسط کار می‌میرد. بدتر، k را بزرگ انتخاب می‌کنند تا یک دور ادغام کنند و بعد با محدودیت فایل باز یا بافر کوچک، ادغام به خواندن تصادفی تبدیل می‌شود.Underestimating the temp space. External sort needs room on the order of the input, and a full disk kills the job midway. Worse, people pick a large k to merge in one round, then hit an open-file limit or shrink the buffers until the merge turns into random reads.

دو جدول بزرگ را چطور به هم می‌چسبانی؟How do you join two huge tables?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

برای پیدا کردن نام هر مشتری در فهرست سفارش‌ها، اول یک دفترچهٔ کوچک برمی‌داری و شمارهٔ هر مشتری را کنار نامش می‌نویسی. بعد سفارش‌ها را یکی‌یکی می‌خوانی و با یک نگاه به شماره، نام را پیدا می‌کنی. آن دفترچه همان جدول هش است و این برگرداندن دو فهرست به هم، همان join است.To put a name on every order, you first take a small notebook and write each customer's number next to their name. Then you read the orders one by one and find the name with a glance at the number. That notebook is the hash table, and matching the two lists is the join.

تشبیه و تصویر ذهنیThe analogy

این دفترچه فقط تا وقتی کار می‌کند که فهرست مشتری‌ها در یک دفتر جا شود. اگر مشتری‌ها آن‌قدر زیاد باشند که دفترچه پر شود، راه‌حل این است که هر دو فهرست را به همان ترتیب به چند دفتر تقسیم کنی — مثلاً بر اساس حرف اول — و بعد هر جفت دفتر را جدا به هم بچسبانی. این همان نسخهٔ پارتیشن‌بندی‌شده است که با شافل کار می‌کند. هزینهٔ دیگرش این است که فقط برای تطابق دقیق جواب می‌دهد. اگر سؤال «سفارش‌هایی که مبلغشان بین دو بازه است» باشد، دفترچهٔ نام به کاری نمی‌آید؛ آنجا باید هر دو طرف را مرتب کنی و با یک پیمایش موازی جلو بروی.The notebook works only while the customer list fits in one notebook. If there are too many customers, the answer is to split both lists into several notebooks the same way — by first letter, say — and then match each pair of notebooks separately. That is the partitioned version, and it is paid for with a shuffle. The other cost is that it only answers exact matches. If the question is “orders whose amount falls in this range”, a notebook of names is useless; there you sort both sides and walk them in parallel.

و بعدش، فنیAnd now, technically

در hash join، یک طرف — معمولاً طرف کوچک‌تر — طرف ساخت است: رکوردهایش خوانده می‌شوند و یک جدول هش در حافظه ساخته می‌شود که کلید join را به رکورد نگاشت می‌کند. طرف دیگر طرف کاوش است و یک بار از روی جدول هش عبور می‌کند. هزینهٔ ساخت خطی است، کاوش هم برای هر رکورد تقریباً ثابت، پس کل کار یک عبور از هر دو ورودی است. مشکل وقتی شروع می‌شود که طرف ساخت در حافظه جا نشود. آن وقت به سراغ نسخهٔ پارتیشن‌بندی‌شده می‌روی: هر دو ورودی با هش کلید join به همان تعداد پارتیشن تقسیم می‌شوند، هر پارتیشن جفت خودش را پیدا می‌کند و هر جفت مستقل join می‌شود. این همان شافل است که هزینه‌اش را می‌پردازی تا حافظه کافی شود. نسخهٔ دیگر، broadcast join است: طرف کوچک به همهٔ گره‌ها فرستاده می‌شود و طرف بزرگ اصلاً جابه‌جا نمی‌شود. یک محدودیت همیشگی هم هست: این روش برای شرط تساوی است؛ برای بازه یا نامساوی باید مرتب‌سازی یا تکنیک دیگری را انتخاب کنی.In a hash join one side — usually the smaller — is the build side: its records are read and an in-memory hash table maps each join key to its record. The other side is the probe side and passes over that table once. Building is linear, probing is roughly constant per record, so the whole thing is one pass over each input. The trouble starts when the build side does not fit in memory. Then you go to the partitioned version: both inputs are split by the hash of the join key into the same number of partitions, each partition finds its counterpart, and each pair is joined independently. That is the shuffle you pay for so that memory suffices. The other version is the broadcast join: the small side is shipped to every node and the large side never moves. One limit is permanent: this works for equality. For ranges and inequalities you sort or reach for another technique.

O(1)کاوش هر رکورد در جدول هش، به‌طور میانگینprobing the hash table, on average, per record
build sideطرفی که در حافظه می‌نشیند؛ معمولاً کوچک‌ترthe side that sits in memory; usually the smaller one
= onlyفقط برای تساوی؛ برای بازه جواب نمی‌دهدequality only; it cannot answer a range

تلهٔ رایجThe usual trap

انتخاب طرف ساخت بر اساس تعداد ردیف، نه حجم واقعی. جدولی که ردیف کمتری دارد ممکن است ستون‌های بلند داشته باشد و در حافظه جا نشود؛ آن وقت جدول هش شروع به ریختن روی دیسک می‌کند و مزیت اصلی از دست می‌رود. طرف درست، طرفی است که بعد از فیلترها کوچک‌تر است و در حافظه جا می‌شود.Choosing the build side by row count instead of real size. The table with fewer rows may have long columns and fail to fit; the hash table then starts spilling to disk and the whole advantage is gone. The right side is the one that is smaller after filtering and does fit.

immutable data

دادهٔ تغییرناپذیر فصل ۱۱Chapter 11 · دیدن در متنsee in context

اگر هیچ‌وقت چیزی را عوض نکنی، چه چیزی ساده‌تر می‌شود؟If you never change anything, what gets simpler?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

دفتر یادداشت روزانه‌ات را با خودکار می‌نویسی و هیچ صفحه‌ای را پاره نمی‌کنی. اگر جایی اشتباه بنویسی، خط می‌زنی و زیرش تصحیح را می‌نویسی. ماه بعد می‌توانی بفهمی آن روز چه فکری می‌کردی، نه فقط این‌که حالا چه فکری می‌کنی.You keep a diary in pen and never tear a page out. When you get something wrong you cross it out and write the correction underneath. A month later you can see what you thought that day, not only what you think now.

تشبیه و تصویر ذهنیThe analogy

این عادت سه چیز را آسان می‌کند. اول، می‌شود دفتر را دوباره خواند و دقیقاً همان چیزی را دید که آن روز بود؛ هیچ نوشتهٔ قدیمی با نوشتهٔ تازه عوض نشده. دوم، اگر یک صفحه خراب شود، بقیهٔ دفتر سالم است و از دست نمی‌رود. سوم، می‌شود چند نفر همزمان بخوانند بدون این‌که کسی چیزی را زیر پای دیگری عوض کند. بهایش هم روشن است: دفتر بی‌وقفه ضخیم می‌شود. اگر هر روز فقط اضافه کنی، بعد از چند سال باید تصمیم بگیری چه چیزی را نگه داری، کجا خلاصه کنی، و کِی نسخه‌های قدیمی را کنار بگذاری. تغییرناپذیری مجانی نیست؛ فقط هزینه را از «همه‌چیز را عوض کن» به «همه‌چیز را نگه دار و بعد مرتب کن» منتقل می‌کند.That habit makes three things easy. You can read the diary back and see exactly what was there that day, because no old entry was overwritten. If one page is damaged the rest is intact and nothing is lost. And several people can read at once without anyone pulling the page from under anyone else. The bill is clear too: the diary keeps getting fatter. Add every day and after a few years you have to decide what to keep, where to summarise, and when to set old versions aside. Immutability is not free; it moves the cost from “change everything” to “keep everything and tidy up later”.

و بعدش، فنیAnd now, technically

در batch processing، ورودی معمولاً یک مجموعهٔ فایل تغییرناپذیر است: job مجموعه‌ای از فایل‌های مشخص را می‌خواند و تا آخر کار همان بایت‌ها را می‌بیند. همین خصلت است که اجرای دوباره را معنادار می‌کند؛ اگر ورودی در میانهٔ کار عوض شود، خروجی دیگر قابل بازتولید نیست. HDFS هم همین قرار را می‌گذارد: فایل‌ها فقط اضافه می‌شوند و بلوک‌ها در جا بازنویسی نمی‌شوند. روی این پایه، داده‌های مشتق‌شده می‌توانند دوباره ساخته شوند: اگر خروجی خراب شد، job را روی همان ورودی دوباره اجرا می‌کنی. لاگ فقط-افزودنی، snapshot و time travel هم از همین ایده می‌آیند. هزینه‌اش فضا و فشرده‌سازی است، و مدیریت نسخه: باید بدانی هر job کدام snapshot را خوانده، وگرنه «همان ورودی» دوباره پیدا نمی‌شود.In batch processing the input is usually a fixed, immutable set of files: a job reads a named set of files and sees those bytes until it finishes. That is what makes a re-run meaningful; if the input changed mid-job, the output would no longer be reproducible. HDFS makes the same promise: files are appended to, never rewritten block by block. On that base, derived data can be rebuilt: if the output is wrong, run the job again over the same input. Append-only logs, snapshots and time travel all come from the same idea. The cost is space and compaction, plus version management: you have to know which snapshot each job read, or “the same input” cannot be found again.

append-onlyفایل ورودی در جا بازنویسی نمی‌شودthe input file is never rewritten in place
re-runاجرای دوباره روی همان ورودی، همان نتیجه را می‌دهدa re-run over the same input gives the same result
WORMیک بار نوشتن، چند بار خواندن؛ مدل ذخیره‌سازی خیلی از سیستم‌هاwrite once, read many; how many systems store their data

تلهٔ رایجThe usual trap

خواندن از پوشه‌ای که jobهای دیگر در آن می‌نویسند. آن وقت «دادهٔ تغییرناپذیر» داری که هر شب عوض می‌شود، و آن job که دیروز اجرا شد دیگر قابل بازتولید نیست. تغییرناپذیری فقط وقتی معنا دارد که ورودیِ هر اجرا یک snapshot مشخص و بسته باشد.Reading from a directory other jobs write into. Now you have “immutable” data that changes every night, and yesterday's job can no longer be reproduced. Immutability only means something when each run's input is a specific, closed snapshot.

lineage

شجره‌نامه فصل ۱۱Chapter 11 · دیدن در متنsee in context همچنینalso in فصل ۱۳ch 13

اگر خروجی خراب شد، از کجا می‌فهمی از کدام ورودی آمده؟The output is wrong. Which input did it come from?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

دفترچهٔ دستور پخت مادرت را ورق می‌زنی: کنار هر دستور نوشته «از خالهٔ مادرم، شکر را نصف کردم». یک روز کیک‌ها خوب نمی‌شوند؛ همین یادداشت کوتاه راه را نشانت می‌دهد: شکر نصف شده بود، نه فر آرد. اگر آن خط را نداشت، باید همهٔ دستورها را از اول امتحان می‌کردی. شجره‌نامهٔ داده هم همان یادداشت است، برای جدول‌ها.You leaf through your mother's recipe notebook: beside each recipe she wrote whose it was and what she changed, like half the sugar. One day the cakes stop working, and that short note points the way: it was the sugar, not the flour. Without that line you would have had to test every recipe from scratch. Data lineage is that note, kept for tables.

تشبیه و تصویر ذهنیThe analogy

این دفترچه دو فایده دارد. اول، وقتی چیزی خراب می‌شود، به‌جای گشتن در همه چیز مستقیم سر منبع می‌روی. دوم، وقتی مقدار کمی عوض می‌شود، لازم نیست کل غذا را از نو بپزی؛ فقط همان دستور و چیزهایی که از آن خورده‌اند عوض می‌شوند. بهایش این است که باید یادداشت را به‌روز نگه داری، و یادداشتی که تاریخ و نسخه ندارد در روز خرابی به درد نمی‌خورد: نمی‌دانی با کدام نسخهٔ دستور پخته شده بود.The notebook pays twice. First, when something breaks you go straight to the source instead of searching everywhere. Second, when a small quantity changes you do not re-cook everything; only that recipe and the dishes that used it change. The bill is that the notes must be kept current, and a note with no date and no version is useless on the bad day: you cannot tell which version of the recipe was used.

دادهٔ خامRaw inputمنبعthe sourceتبدیلTransformationنسخهٔ کد ثبت شدcode version pinnedدادهٔ مشتقDerived dataخروجیthe outputبازسازی تکهRebuild a partitionبعد از خرابیafter a failure
شجره‌نامه از ورودی تا خروجی را ثبت می‌کند تا وقتی تکه‌ای گم شد، فقط همان تکه از همان ورودی بازسازی شود.Lineage records the path from input to output, so a lost partition can be rebuilt from its own input.

و بعدش، فنیAnd now, technically

شجره‌نامه فراداده‌ای است که می‌گوید هر دادهٔ مشتق از چه ورودی‌هایی، با چه عملگری و با کدام نسخهٔ کد ساخته شده؛ در موتورهای جریان داده معمولاً در سطح تکه ثبت می‌شود. دو کاربرد اصلی دارد. اول، بازیابی: به‌جای نگه داشتن همهٔ میانه‌ها روی دیسک، موتور می‌تواند تکهٔ ازدست‌رفته را از ورودی و گراف دوباره بسازد. دوم، تحلیل اثر: قبل از عوض کردن یک ستون می‌فهمی کدام گزارش‌ها و مدل‌ها به آن وابسته‌اند. محدودیتش جدی است: اگر تابع غیرقطعی باشد یا نسخهٔ کد را ثبت نکرده باشی، بازسازی عدد دیگری می‌دهد و هیچ‌کس نمی‌فهمد کدام درست است. اگر هم ورودی بیرونی مثل یک جدول مرجع عوض شده باشد و در شجره‌نامه نیامده باشد، خروجی بی‌صدا از ورودی جدا می‌شود.Lineage is metadata saying which inputs, which operator and which code version produced each derived data set; dataflow engines usually record it per partition. It has two main uses. First, recovery: instead of keeping every intermediate on disk, the engine can rebuild a lost partition from the input and the graph. Second, impact analysis: before changing a column you can see which reports and models depend on it. Its limits are serious. If the function is non-deterministic, or the code version was not recorded, a rebuild yields a different number and nobody can tell which is right. And if an external input such as a lookup table changed without appearing in the lineage, the output silently drifts from its source.

1 partitionبازسازی در سطح تکه انجام می‌شود، نه کل جدولrebuilds happen per partition, not per whole table
code + inputهر دو باید ثبت شوند، وگرنه بازسازی قابل اعتماد نیستboth must be recorded or a rebuild cannot be trusted
2 optionsنگه داشتن میانه‌ها یا بازسازی از ورودیkeep the intermediates or recompute from the input

نگه داشتن یا بازسازیKeep it or rebuild it

MapReduce همهٔ میانه‌ها را روی فایل‌سیستم می‌نوشت، پس بازیابی‌اش ساده بود: task را دوباره اجرا کن. موتورهای جریان داده برای صرفه‌جویی در I/O میانه‌ها را نگه نمی‌دارند و به شجره‌نامه تکیه می‌کنند. این یک معامله است: دیسک کمتری می‌خواهی، ولی بعد از خرابی باید CPU بیشتری خرج کنی و ممکن است بازسازی از چند مرحله عقب‌تر شروع شود. اگر خوشه‌ات پایدار است بازسازی ارزان است؛ اگر نه، نگه داشتن میانه‌ها ارزان‌تر تمام می‌شود.MapReduce wrote every intermediate to the filesystem, so recovery was simple: run the task again. Dataflow engines skip that I/O and lean on lineage. That is a trade: less disk, but more CPU after a failure, and the rebuild may have to start several stages back. On a stable cluster recomputation is cheap; on a shaky one, keeping the intermediates is the cheaper answer.

شجره‌نامه‌ای که دروغ می‌گویدLineage that lies

بیشتر خرابی‌های شجره‌نامه از غیرقطعی بودن می‌آید: تابعی که به ساعت یا تصادف تکیه می‌کند، یا join با جدولی که خط‌هایش بین دو اجرا عوض شده. در این حالت بازسازی خروجی متفاوتی می‌دهد و شجره‌نامه فکر می‌کند همان چیز را ساخته. راهش ثبت نسخهٔ کد و شناسهٔ ورودی‌ها و جدا کردن جدول‌های مرجع از جملهٔ منابع است.Most lineage failures come from non-determinism: a function that depends on the clock or on randomness, or a join against a table whose rows changed between runs. The rebuild then produces something different, and the lineage believes it reproduced the original. The answer is to pin the code version, record input identities, and count lookup tables as inputs rather than treating them as constants.

تلهٔ رایجThe usual trap

ثبت شجره‌نامه فقط در سطح کل جدول، بدون نسخهٔ کد و بدون جدول‌های مرجع. موقع خرابی مجبور می‌شوی کل جدول را بازسازی کنی، و بدتر، بازسازی عدد متفاوت می‌دهد چون کد عوض شده بود. آن‌وقت هیچ‌کس نمی‌داند گزارش دیروز درست بود یا امروز.Recording lineage only at whole-table level, with no code version and no lookup tables. A failure forces a full rebuild, and worse, the rebuild gives a different number because the code had changed. Then nobody knows whether yesterday's report or today's is the true one.

اگر هر رکورد را جدا پردازش کنی، چه چیزی از دست می‌رود؟If you process each record alone, what do you lose?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

کسی در خانه نشسته و نامه‌های امروز را باز می‌کند. روی هر نامه یک برچسب می‌زند: «برق، ۲۰۰»، «اجاره، ۱۵۰۰»، «تبلیغ، ۰». نامهٔ بعدی را که برمی‌دارد، هیچ کاری به نامهٔ قبلی ندارد و چیزی از آن را به خاطر نمی‌سپارد. نگاشت‌گر در MapReduce هم همان کارگر است: هر چیز را یکی‌یکی برمی‌دارد، برچسب می‌زند و رد می‌کند.Someone sits at the kitchen table opening today's post. On each letter they stick a label: electricity, 200; rent, 1500; advertising, nothing. The next letter has nothing to do with the previous one, and they remember none of it. A mapper in MapReduce does exactly that: one record in, a few key-value pairs out.

تشبیه و تصویر ذهنیThe analogy

این بی‌حافظگی عمدی است، چون همان چیزی است که اجازه می‌دهد چند نفر هم‌زمان روی جعبه‌های مختلف نامه کار کنند. اگر کارگر مجبور بود نامهٔ قبلی را یادش بماند، نمی‌شد وسط کار نفر تازه‌ای آورد و بخشی را به او داد. بهایش این است که کارگر جمع کل نمی‌داند: هر برچسب فقط یک تکه از حقیقت است. و چون کارش تکرارشدنی است، اگر یک نامه دو بار برچسب بخورد، دو برچسب یکسان تولید می‌شود که بعداً باید جمع شوند.That forgetfulness is deliberate: it is what lets several people work on different boxes of post at once. If the worker had to remember the previous letter, you could not hand half the pile to someone new mid-way. The bill is that the worker never knows the total: each label is one piece of the truth. And because the work is repeatable, a letter labelled twice produces two identical labels that must be combined later.

رکورد ورودیInput recordیکی یکیone at a timemap()map()بی‌حافظهno stateجفت کلید-مقدارKey-value pairsچند جفتseveralدسته‌بندی و مرتبPartition and sortکار چارچوبthe framework
نگاشت‌گر هر رکورد را تنها می‌بیند و جفت کلید-مقدار تولید می‌کند؛ دسته‌بندی و مرتب‌سازی کار چارچوب است.A mapper sees each record alone and emits key-value pairs; partitioning and sorting are the framework's job.

و بعدش، فنیAnd now, technically

نگاشت‌گر یک تابع است با امضای ساده: یک جفت کلید-مقدار می‌گیرد و فهرستی از جفت‌های کلید-مقدار برمی‌گرداند. چارچوب آن را برای هر رکورد ورودی صدا می‌زند، بدون هیچ ترتیب تضمین‌شده‌ای و بدون حالت مشترک بین فراخوانی‌ها. نگاشت‌گر هیچ چیز را مرتب نمی‌کند و هیچ کلیدی را کامل نمی‌بیند؛ فقط تولید می‌کند. چارچوب بعد از آن، هر جفت خروجی را با hash کلید به یکی از R بخش می‌فرستد و هر بخش را مرتب می‌کند، و این‌جا کار نگاشت‌گر تمام می‌شود. چون taskها ممکن است دوباره اجرا شوند یا به‌صورت حدسی تکرار شوند، هر اثر جانبی داخل نگاشت‌گر می‌تواند دو بار اتفاق بیفتد. اگر تجمیع جزئی بخواهی، combiner هست، ولی فقط برای عملیاتی که خاصیت شرکت‌پذیری دارند؛ میانگین گرفتن با combiner بی‌احتیاط، جواب غلط می‌دهد.A mapper is a function with a simple signature: it takes one key-value pair and returns a list of key-value pairs. The framework calls it once per input record, in no guaranteed order and with no state shared between calls. The mapper sorts nothing and never sees all values for a key; it only emits. After that the framework hashes each emitted key to one of R partitions and sorts each partition, and the mapper's job is over. Because tasks may be retried or duplicated speculatively, a side effect inside a mapper can happen twice. If you want partial aggregation there is a combiner, but only for operations that are associative and commutative; averaging carelessly with a combiner gives the wrong answer.

1 recordهر فراخوانی فقط یک رکورد می‌بیندeach call sees exactly one record
hash(k)کلید خروجی تعیین می‌کند به کدام بخش برود، نه خود نگاشت‌گرthe emitted key picks the partition, not the mapper
R partsخروجی بین R بخش پخش می‌شود؛ نگاشت‌گر ترتیبی نمی‌سازدoutput is spread over R partitions; no ordering here

چرا خالص بودن این‌قدر مهم استWhy purity matters here

تابع خالص یعنی خروجی فقط به ورودی بستگی دارد. این ویژگی سه چیز را ممکن می‌کند: اجرای دوبارهٔ task بدون ترس، اجرای حدسی یک task تکراری، و تست کردن منطق بدون راه انداختن کل خوشه. به‌محض این که نگاشت‌گر در یک جدول بیرونی بنویسد یا شمارنده‌ای را زیاد کند، این ویژگی‌ها یکی‌یکی از دست می‌روند و مجبور می‌شوی برای هر retry فکر جداگانه بکنی.A pure function means the output depends only on the input. That buys three things: re-running a task without fear, speculative duplicates, and testing the logic without a cluster. The moment a mapper writes to an external table or bumps a counter, those properties fall away one by one and every retry needs its own plan.

combiner، همان map با حافظهٔ کوچکThe combiner is a small-memory map

combiner روی همان ماشینی اجرا می‌شود که map را اجرا کرد و خروجی map را قبل از رفتن به شبکه جمع می‌کند. برای جمع و بیشینه و شمارش عالی است و حجم shuffle را کم می‌کند. برای میانگین خطرناک است: اگر میانگین‌ها را میانگین بگیری، وزن هر گروه یکسان می‌شود و جواب غلط درمی‌آید؛ باید جمع و تعداد را جدا نگه داری. combiner اختیاری است و چارچوب می‌تواند آن را صفر یا چند بار اجرا کند، پس باید بی‌اثر باشد.A combiner runs on the same machine as the map and folds the map's output before it crosses the network. It is excellent for sums, maxima and counts, and it shrinks the shuffle. It is dangerous for averages: averaging averages weights every group equally and gives a wrong answer; keep a sum and a count instead. The combiner is optional and the framework may run it zero or several times, so it must be free of side effects.

تلهٔ رایجThe usual trap

نوشتن در یک سیستم بیرونی داخل نگاشت‌گر: فرستادن ایمیل، درج در پایگاه داده، صدا زدن یک API. task ممکن است شکست بخورد و دوباره اجرا شود، یا نسخهٔ حدسی هم‌زمان اجرا شود؛ نتیجه دو ایمیل و دو ردیف است. منطق نگاشت‌گر باید خالص بماند و اثر جانبی به مرحلهٔ خروجی برود.Writing to an external system inside the mapper: sending email, inserting rows, calling an API. The task may fail and run again, or a speculative copy may run alongside it, and the result is two emails and two rows. Keep the mapper pure and move the side effect to the output stage.

چطور صدها ماشین را وادار می‌کنی یک کار را با هم بکنند؟How do you make hundreds of machines do one job together?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

عروسی ۳۰۰ مهمان دارد و باید شمرده شود هر میز چند پرس گوشت خورده. سه نفر دم در برای هر مهمان یک کارت می‌نویسند: «میز ۷، گوشت». بعد کارت‌ها را به اتاق‌هایی می‌برند که هر اتاق مخصوص یک میز است؛ کارت‌های میز ۷ همه در یک اتاق جمع می‌شوند. آخرش مسئول هر اتاق کارت‌ها را می‌شمارد و عدد را اعلام می‌کند. مپ‌ردیوس همین سه حرکت است: نوشتن، دسته‌بندی، شمردن.A wedding has three hundred guests and someone must know how many portions of meat each table ordered. Three people at the door write a card per guest: table seven, meat. The cards then go to rooms, one room per table, so every card for table seven lands in the same room. Finally each room's owner counts the cards and calls out a number. MapReduce is those three moves: write, group, count.

تشبیه و تصویر ذهنیThe analogy

نکتهٔ مهم این است که نفر دم در لازم نیست بداند بقیهٔ میزها چه خبر است؛ فقط کارت می‌نویسد. مسئول اتاق هم لازم نیست بداند کارت‌ها از کدام در آمده‌اند؛ فقط می‌شمارد. همین تقسیم کار است که اجازه می‌دهد ۳۰۰ مهمان را با چند نفر جلو ببری. بهایش یک توقف است: تا وقتی آخرین کارت از دم در نرسیده، هیچ اتاقی نمی‌تواند عدد نهایی را بدهد. اگر یک میز شلوغ‌تر باشد، همان اتاق گلوگاه می‌شود.The point is that the person at the door need not know what the other tables are doing; they just write cards. The room owner need not know which door the cards came from; they just count. That split is what lets a few people handle three hundred guests. The bill is one stop: until the last card arrives from the door, no room can give a final number. And if one table is busier, that room becomes the bottleneck.

splitSplitبلوک ورودیan input blockmapMapهر رکوردeach recordshuffle + sortShuffle and sorthash و مرتب‌سازیhash then sortreduceReduceهر کلیدeach keyoutputOutputروی فایل‌سیستمto the filesystem
یک job مپ‌ردیوس: بلوک ورودی، map برای هر رکورد، shuffle با hash و مرتب‌سازی، reduce برای هر کلید، خروجی.One MapReduce job: input split, map per record, shuffle by hash and sort, reduce per key, then output.

و بعدش، فنیAnd now, technically

هر job مپ‌ردیوس از یک فاز map و یک فاز reduce ساخته می‌شود. ورودی به بلوک‌هایی تقسیم می‌شود و برای هر بلوک یک task map اجرا می‌شود، معمولاً روی همان ماشینی که بلوک را دارد. نگاشت‌گر برای هر رکورد چند جفت کلید-مقدار تولید می‌کند و کارش تمام است؛ هیچ ترتیبی در خروجی‌اش تضمین نمی‌شود. بعد چارچوب هر جفت را با hash کلید به یکی از R بخش می‌فرستد، هر بخش را مرتب می‌کند و روی دیسک می‌نویسد؛ این همان shuffle است. هر reducer بخش خودش را از همهٔ نگاشت‌گرها می‌گیرد، ران‌های مرتب را ادغام می‌کند و برای هر کلید یک بار reduce را با تکرارگر مقادیر صدا می‌زند. خروجی به فایل‌سیستم توزیع‌شده نوشته می‌شود. یک سد طبیعی این‌جاست: reducer تا وقتی همهٔ نگاشت‌گرها تمام نکنند نمی‌تواند کارش را کامل کند. و چون هر job فقط یک map و یک reduce دارد، کارهای پیچیده به زنجیره‌ای از jobها شکسته می‌شوند و هر job خروجی کامل خودش را روی دیسک می‌نشاند؛ خود چارچوب هیچ خبری از job قبلی یا بعدی ندارد.A MapReduce job has one map phase and one reduce phase. The input is split into blocks, and one map task runs per block, usually on the machine that holds it. The mapper emits key-value pairs for each record and that is the end of its work; no order is guaranteed in its output. The framework then hashes each pair to one of R partitions, sorts each partition, and writes it to disk; that is the shuffle. Each reducer fetches its partition from every mapper, merges the sorted runs, and calls reduce once per key with an iterator of values. The output goes to the distributed filesystem. There is a natural barrier here: a reducer cannot finish until every mapper has finished. And since each job has only one map and one reduce, complex work becomes a chain of jobs, each landing its full output on disk; the framework itself knows nothing about the job before or after it.

R reducersتعداد بخش‌ها را خودت انتخاب می‌کنی و موازی‌سازی را تعیین می‌کندyou choose the partition count, and it sets parallelism
hash(k) % Rقاعدهٔ پیش‌فرض برای فرستادن هر کلید به یک reducerthe default rule that sends each key to one reducer
1 barrierیک سد: reduce تا پایان همهٔ mapها کامل نمی‌شودone barrier: reduce cannot finish before every map does

shuffle دقیقاً چه می‌کندWhat the shuffle actually does

shuffle ارزان نیست. خروجی هر نگاشت‌گر اول در بافر حافظه می‌نشیند، وقتی پر شد روی دیسک ریخته می‌شود، و بعد هر reducer باید بخش مربوط به خودش را از همهٔ نگاشت‌گرها روی شبکه بکشد. یعنی داده یک بار کامل جابه‌جا می‌شود، و اگر تعداد نگاشت‌گرها و reducerها زیاد باشد این جابه‌جایی همه‌به‌همه است. مرتب‌سازی هم بخشی از همین مرحله است: reducer کلیدها را به ترتیب می‌بیند، و همین ترتیب است که گروه‌بندی را ممکن می‌کند و اجازه می‌دهد برای هر کلید یک بار صدا زده شود. به همین دلیل است که خیلی از بهینه‌سازی‌ها فقط یک هدف دارند: کم کردن حجم shuffle.The shuffle is not cheap. Each mapper's output first sits in a memory buffer, spills to disk when full, and then every reducer must pull its own partition from every mapper over the network. The data crosses once in full, and with many mappers and reducers that crossing is all-to-all. Sorting is part of the same stage: the reducer sees keys in order, and that order is what makes grouping possible and allows one call per key. That is why so many optimisations aim at a single number: the volume of the shuffle.

کجا دست‌وپاگیر می‌شودWhere it gets in the way

برای کارهای تکراری مثل الگوریتم‌های گراف که هر دور روی خروجی دور قبل کار می‌کنند، زنجیرهٔ jobها یعنی نوشتن و خواندن کل داده در هر دور. چارچوب هم نمی‌تواند مرحله‌ها را با هم بهینه کند، چون هر job را جدا می‌بیند و از job بعدی خبر ندارد. این‌جا همان جایی است که موتورهای DAG جلو می‌زنند: گراف کامل را می‌بینند، میانه‌ها را در حافظه نگه می‌دارند و عملگرها را با هم اجرا می‌کنند.For iterative work such as graph algorithms, where each round reads the previous round's output, a chain of jobs means writing and reading all the data every round. The framework also cannot optimise across stages, because it sees each job alone and knows nothing about the next one. This is exactly where DAG engines pull ahead: they see the whole graph, keep intermediates in memory, and run operators together.

تلهٔ رایجThe usual trap

فکر کردن این که خروجی به‌طور سراسری مرتب است. هر reducer خروجی خودش را مرتب می‌نویسد، ولی بین R reducer هیچ ترتیب سراسری وجود ندارد؛ کسی که برای مرتب بودن یک reducer بگذارد، موازی‌سازی را کشته و باز هم فقط یک ترتیب محلی دارد. اگر ترتیب سراسری می‌خواهی باید partitioning را درست انتخاب کنی، نه R را یک کنی.Assuming the output is globally sorted. Each reducer writes its own output in order, but across R reducers there is no global order; setting one reducer to get order kills parallelism and still gives only a local order. If you need global order, choose the partitioning properly instead of collapsing R to one.

چرا در انبار شیء نمی‌شود یک فایل را تغییر نام داد؟Why can you not rename a file in an object store?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

کمد امانات استخر: یک کمد شماره‌دار می‌گیری و کیف ورزشی‌ات را کامل می‌گذاری داخل؛ بعداً با همان شماره برش می‌داری. نمی‌توانی داخل کمد قفسه بزنی یا اسم کیف را عوض کنی؛ اگر اسم عوض شود، کیف را بیرون می‌آوری و یک کیف تازه با اسم نو می‌گذاری. انبار شیء هم همین است: کلید، شیء کامل، و بیرون و داخل کردن.The pool's locker room: you rent a numbered locker and put your whole gym bag inside, then later take it out by number. You cannot put shelves inside it or rename the bag; to rename it you take the bag out and put a fresh one in with a new name. An object store works the same way: a key, a whole object, and in or out.

تشبیه و تصویر ذهنیThe analogy

این سادگی دو فایده دارد: هر شیء را می‌شود از هر جایی روی شبکه خواند، و لازم نیست بدانی روی کدام دیسک است. برای job دسته‌ای عالی است، چون هزار ماشین می‌توانند هم‌زمان بخش‌های مختلف یک ورودی بزرگ را بخوانند. بهایش این است که انبار دفتری برای گشتن ندارد: فهرست کردن کلیدها کند است و برای پیدا کردن یک شیء باید کلیدش را بدانی. تازه، چون تغییر نام وجود ندارد، هر جابه‌جایی می‌شود کپی کردن و بعد حذف کردن؛ روی دادهٔ بزرگ این کار گران است.The simplicity pays twice: any object can be read from anywhere on the network, and you never need to know which disk it sits on. That is ideal for a batch job, because a thousand machines can read different parts of one large input at once. The bill is that the store has no shelf to browse: listing keys is slow, and to fetch an object you must know its key. And since there is no rename, every move becomes a copy followed by a delete; on large data that is expensive.

HDFS · دیسک محلیHDFS · local disksبلوک ۱۲۸ مگ، سه کپی128 MB blocks, three copiesتغییر نام فقط فهرستrename is metadata onlyمحاسبه کنار دادهcompute beside the dataانبار شیء · S3Object store · S3هر شیء کامل، با یک کلیدwhole objects under a keyتغییر نام یعنی کپی و حذفrename means copy and deleteمحاسبه جدا از ذخیرهcompute apart from storage
دو جای متفاوت برای دادهٔ خام: یکی فهرست مرکزی روی دیسک محلی، یکی شیء کامل روی شبکه.Two places for raw data: a central catalogue over local disks, or whole objects over the network.

و بعدش، فنیAnd now, technically

انبار شیء مثل S3 یا GCS فضای نام تخت دارد: هر شیء یک کلید و یک بدنه است، و رابط کار با آن PUT و GET است؛ می‌توانی بازه‌ای را بخوانی، ولی نمی‌توانی بازه‌ای را در جا بنویسی. نه افزودن به انتهای شیء داری، نه تغییر در جا، نه تغییر نام: تغییر نام در عمل کپی کردن شیء و حذف کردن اصلی است. فهرست کردن هم یک عملیات فراداده جداست که صفحه‌بندی می‌شود و روی حجم زیاد کند است. دوام داخل سرویس با تکثیر یا erasure coding تأمین می‌شود و تو با آن کاری نداری. مزیت اصلی‌اش جدا بودن ذخیره از محاسبه است: هر تعداد ماشین می‌تواند روی همان داده کار کند و دیسک محلی هیچ ماشینی گلوگاه نیست. برای همین، انبار شیء جای HDFS را در بسیاری از jobهای تحلیلی گرفته. اما همین جدایی یعنی هر خواندن از شبکه می‌آید، و برای داده‌های کوچک و پرشمار، سربار هزینهٔ هر درخواست از خود داده بیشتر می‌شود.An object store such as S3 or GCS has a flat namespace: each object is a key and a body, and the interface is PUT and GET; you can read a range, but you cannot write one in place. There is no append, no edit in place, and no rename: a rename is a copy of the object followed by a delete of the original. Listing is a separate metadata operation, paginated, and slow at volume. Durability inside the service comes from replication or erasure coding and is not your problem. The real advantage is that storage is separate from compute: any number of machines can work on the same data and no machine's local disk is a bottleneck. That is why object stores have taken the place of HDFS for many analytical jobs. But that separation also means every read comes over the network, and for many small objects the per-request overhead outweighs the data.

1 objectهر نوشتن یک شیء کامل است، نه یک رکوردevery write is a whole object, not a record
copy+deleteتغییر نام در عمل کپی و حذف استa rename is really a copy and a delete
GET/PUTرابط فقط خواندن و نوشتن کل شیء استthe interface reads and writes whole objects

پس خروجی job را چطور می‌بندیSo how do you commit a job's output

روی فایل‌سیستم، خروجی را در مسیر موقت می‌نویسی و با rename اتمیک جایگزین می‌کنی. روی انبار شیء rename نداری، پس الگو عوض می‌شود: داده را می‌نویسی، و در آخر یک شیء کوچک فهرست می‌نویسی که می‌گوید کدام نسخه معتبر است؛ خواننده‌ها همیشه اول فهرست را می‌خوانند. اگر job وسط کار بمیرد، فهرست هرگز نوشته نشده و کسی سراغ دادهٔ نیمه‌کاره نمی‌رود. این الگو همان تضمین را می‌دهد، به شرطی که هیچ خواننده‌ای مسیر داده را حدس نزند و از فهرست رد نشود.On a filesystem you write to a temporary path and swap with an atomic rename. An object store has no rename, so the pattern changes: write the data, then write one small manifest object naming the valid version, and have readers always read the manifest first. If the job dies midway the manifest was never written, and nobody goes looking for the partial data. That gives the same guarantee, provided no reader guesses the data path and skips the manifest.

کجا HDFS هنوز بهتر استWhere HDFS is still better

اگر کارت به تغییر نام زیاد، پیمایش پوشه‌ای، یا دسترسی کم‌تأخیر به بخشی از فایل نیاز دارد، دیسک محلی با فهرست مرکزی بهتر است. shuffle هم روی انبار شیء گران است، چون هر تکه باید با یک درخواست جدا نوشته و خوانده شود. تیم‌ها معمولاً دادهٔ خام را در انبار شیء می‌گذارند و محاسبه را روی دیسک محلی همان خوشه انجام می‌دهند.If the work needs many renames, directory walks, or low-latency access to part of a file, local disks with a central catalogue are better. The shuffle is also expensive on an object store, because each partition must be written and read with a separate request. Teams commonly keep raw data in the object store and run the computation on the cluster's local disks.

تلهٔ رایجThe usual trap

استفاده از انبار شیء مثل فایل‌سیستم: نوشتن هزاران فایل کوچک و بعد تغییر نام پوشه برای انتشار. آن تغییر نام در واقع کپی هر شیء است، و اگر وسطش خطا بدهد هر دو نسخه می‌مانند. بعد هم job بعدی با فهرست کردن پوشه دنبال ورودی می‌گردد و ساعت‌هایش را صرف فراداده می‌کند.Using an object store as a filesystem: writing thousands of small files and then renaming the folder to publish. That rename actually copies every object, and a failure halfway leaves both versions behind. Then the next job hunts for its input by listing the folder and burns hours on metadata.

وقتی ماشین را از زیر پایت می‌کشند، کارت چه می‌شود؟When the machine is pulled away, what happens to the work?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

در آشپزخانهٔ مشترک خوابگاه قابلمه‌ات روی گاز است. سرایدار می‌آید و می‌گوید سفارش شام ساختمان مانده؛ قابلمه را برمی‌داری و می‌گذاری کنار. نیم‌پخته دور ریخته نمی‌شود، ولی باید بعداً از همان مرحله شروع کنی. پیش‌دستی همین است: منابع از تو گرفته می‌شود و کارت بعداً ادامه پیدا می‌کند.Your pot is on the stove in the shared dorm kitchen. The caretaker comes in and says the building's dinner order is waiting; you lift the pot off and set it aside. The half-cooked food is not thrown away, but you will have to start from that stage later. Preemption is that: the resource is taken from you and your work resumes afterwards.

تشبیه و تصویر ذهنیThe analogy

این کار فقط وقتی جواب می‌دهد که کارت تکه‌تکه باشد و هر تکه از نو قابل اجرا. اگر کل شام یک تکهٔ به‌هم‌پیوسته بود که فقط با همان قابلمه پخته می‌شد، پس گرفتنش فاجعه بود. برای همین کارهای دسته‌ای با پیش‌دستی کنار می‌آیند و کارهای لحظه‌ای نه: یک گزارش شبانه می‌تواند نصفه بماند و از اول اجرا شود، ولی یک تراکنش پرداخت را وسط کار نمی‌کشند. بهایش این است که خروجی هر تکه را طوری بنویسی که اجرای دوباره‌اش ضرری نداشته باشد.This only works when the work comes in pieces and each piece can run again. If the whole dinner were one continuous step that only that pot could cook, taking it away would be a disaster. That is why batch work tolerates preemption and moment-to-moment work does not: a nightly report can stop halfway and start over, while a payment transaction is not killed mid-flight. The bill is that each piece's output must be written so a second run costs nothing.

شروع taskTask startsمنبع گرفته شدresource heldتصمیم زمان‌بندScheduler decidesکار مهم‌تر رسیدpriority arriveskill شدTask killedخروجی نیمه‌کاره دورpartial output droppedاجرای دوبارهRuns againاز همان ورودیsame input
یک task از شروع تا پس گرفته شدن و اجرای دوباره: کار نیمه‌کاره دور ریخته می‌شود، خود task نه.One task from start to reclaim and rerun: the partial work is discarded, the task is not.

و بعدش، فنیAnd now, technically

زمان‌بند خوشه مثل YARN یا Kubernetes کارها را در صف‌هایی با اولویت و سهمیه می‌پذیرد. وقتی کار مهم‌تری منابع بخواهد و ظرفیت آزاد نباشد، زمان‌بند می‌تواند ظرف یا CPU کارهای کم‌اهمیت‌تر را پس بگیرد؛ آن task کشته می‌شود، خروجی نیمه‌کاره‌اش دور ریخته می‌شود و بعداً دوباره اجرا می‌شود. این کار فقط به این دلیل قابل تحمل است که taskهای دسته‌ای مستقل و تکرارشدنی‌اند و خروجی هر task به‌صورت اتمیک منتشر می‌شود. پیش‌دستی یک تصمیم زمان‌بندی است، نه یک خرابی: هیچ گره‌ای نمرده، فقط اولویت عوض شده. همین منطق در ماشین‌های spot هم هست، با این تفاوت که آن‌جا کل ماشین پس گرفته می‌شود. نکتهٔ عملی این است که کشتن یک نگاشتگر در اواخر job گران است، چون خروجی مرتب‌شده‌اش هم از دست می‌رود و باید از نو ساخته شود.A cluster scheduler such as YARN or Kubernetes admits work into queues with priorities and quotas. When more important work needs resources and there is no free capacity, the scheduler can reclaim containers or CPU from lower-priority tasks; that task is killed, its partial output dropped, and it runs again later. This is only tolerable because batch tasks are independent and repeatable and each task's output is published atomically. Preemption is a scheduling decision, not a failure: no node died, only the priority changed. The same logic applies to spot machines, except there the whole machine is reclaimed. The practical note is that killing a mapper late in a job is expensive, because its sorted output is lost too and must be rebuilt.

1 taskواحد پس گرفته‌شده یک task است، نه کل jobthe unit reclaimed is one task, not the whole job
secondsپس گرفتن معمولاً در چند ثانیه انجام می‌شود، نه دقیقهa reclaim usually lands in seconds, not minutes

پیش‌دستی با اجرای حدسی یکی نیستPreemption is not speculative execution

هر دو کار اضافی تولید می‌کنند، ولی در دو جهت مخالف. اجرای حدسی وقتی است که یک task از بقیه کندتر شده؛ زمان‌بند یک نسخهٔ دوم را هم‌زمان اجرا می‌کند و هر کدام اول تمام شد برنده است، دیگری کشته می‌شود. پیش‌دستی وقتی است که منابع کم است؛ نسخهٔ اول کشته می‌شود تا کار مهم‌تری اجرا شود. در اولی کار تکراری است و در دومی کار ناتمام دور ریخته می‌شود. کتاب هر دو را زیر کار اضافی قابل قبول می‌آورد، ولی مکانیکشان را نباید قاطی کرد.Both waste work, but in opposite directions. Speculative execution is for a task that has fallen behind: the scheduler runs a second copy alongside it and whichever finishes first wins while the other is killed. Preemption is for a shortage of resources: the first copy is killed so more important work can run. In one the work is duplicated, in the other unfinished work is thrown away. The book treats both as acceptable waste, but their mechanics should not be mixed up.

job باید چه چیزی را تحمل کندWhat the job must tolerate

سه چیز. اول، خروجی هر task باید اتمیک منتشر شود تا خواننده نسخهٔ نیمه‌کاره نبیند. دوم، اثر جانبی بیرونی باید یا نباشد یا به مرحلهٔ commit برود، وگرنه هر retry یک ایمیل یا یک ردیف تکراری می‌سازد. سوم، اگر job نقطهٔ ذخیره ندارد، کشته شدن یک task یعنی بازسازی از ورودی؛ پس ورودی باید هنوز در دسترس باشد. jobهایی که این سه را رعایت می‌کنند می‌توانند روی ظرفیت ارزان و قابل پس گرفتن اجرا شوند.Three things. First, each task's output must be published atomically so no reader sees a partial version. Second, external side effects must be absent or deferred to a commit step, or every retry sends another email or inserts another row. Third, without checkpoints a killed task means a rebuild from the input, so the input must still be there. Jobs that satisfy all three can run on cheap, reclaimable capacity.

تلهٔ رایجThe usual trap

تنظیم نکردن اندازهٔ task و نقطهٔ ذخیره. اگر taskها خیلی بزرگ باشند، هر پیش‌دستی چند ساعت کار را دور می‌ریزد؛ اگر خیلی کوچک باشند، سربار زمان‌بندی از خود کار بیشتر می‌شود. و اگر اثر جانبی داخل task باشد، پیش‌دستی و retry آن را دو بار اجرا می‌کنند.Never tuning task size and checkpoints. If tasks are too large, every reclaim throws away hours; if they are too small, scheduling overhead exceeds the work. And if a task carries a side effect, preemption and retry will run it twice.

همهٔ مقدارهای یک کلید را داری. حالا چه؟You have every value for one key. Now what?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

بعد از سفر، کیسهٔ رسیدها را روی میز خالی می‌کنی. اول رسیدها را بر اساس اسم آدم‌ها به چند دسته تقسیم می‌کنی، بعد هر دسته را جدا جمع می‌زنی: علی ۳۲۰، مریم ۲۱۰. آن جمعِ آخر برای هر نفر همان reducer است؛ تفکیک دسته‌ها کار shuffle است.After a trip you empty the bag of receipts onto the table. First you split them into piles by name, then you add each pile up on its own: Ali 320, Maryam 210. That final sum per person is the reducer; the splitting into piles is the shuffle.

تشبیه و تصویر ذهنیThe analogy

حالا همان میز را با جزئیاتش ببین. جمع زدن برای هر نفر مستقل است، پس می‌توانی سه دسته را همزمان به سه دوست بدهی و هر کدام فقط جمع خودش را بزنند؛ این استقلال همان چیزی است که reducer را قابل موازی‌سازی می‌کند. ولی هیچ‌کدام از آن‌ها نمی‌تواند بگوید کل خرج سفر چقدر شد؛ برای آن عدد، باید یک نفر دوباره جمعِ جمع‌ها را بزند. دومین هزینه این است که reducer فقط سهم خودش را می‌بیند. اگر یک نفر در سفر همه‌چیز را خریده باشد، دستهٔ او کوهی از رسید می‌شود و دو دستهٔ دیگر در چند دقیقه تمام می‌شوند، ولی تسویهٔ سفر تا تمام شدن آن کوه طول می‌کشد. سوم، ترتیب رسیدها در دسته اهمیتی ندارد؛ اگر جایی به «آخرین رسید» تکیه کنی، جوابت به شانس بند است.Look at that table more closely. Adding up one person's receipts is independent of the others, so you can hand three piles to three friends and each of them only totals their own; that independence is exactly what makes a reducer parallel. But none of them can tell you what the whole trip cost; for that number someone has to add the subtotals again. The second cost is that a reducer only sees its own share. If one person bought everything on the trip, their pile becomes a mountain of receipts while the other two finish in minutes, yet the settlement waits for that mountain. Third, the order of receipts inside a pile does not matter; if you rely on “the last receipt”, your answer depends on luck.

رکوردهای ورودیInput recordsفایل روی HDFSfiles on HDFSmapmapکلید و مقدارkey and valueگروه‌بندی بر اساس کلیدGroup by keyپس از شافلafter the shufflereducerreducerیک بار برای هر کلیدonce per keyخروجیOutputیک نتیجه برای کلیدone result per key
مسیر یک کلید در MapReduce: هر رکورد به جفت کلید-مقدار تبدیل می‌شود، کلیدهای یکسان کنار هم می‌آیند و reducer یک نتیجه می‌سازد.One key's path through MapReduce: each record becomes a key–value pair, equal keys meet, and the reducer emits one result.

و بعدش، فنیAnd now, technically

در MapReduce، فریم‌ورک reducer را یک بار برای هر کلید صدا می‌زند و همهٔ مقدارهای آن کلید را به شکل یک تکرارگر به آن می‌دهد؛ خودت هیچ‌وقت روی «ردیف بعدی» حلقه نمی‌زنی، فقط می‌گویی با این کلید و این مقدارها چه کنم. مقدارها پیش از رسیدن مرتب و گروه‌بندی شده‌اند، چون همان مرتب‌سازی است که کلیدهای یکسان را به یک reducer و پشت سر هم می‌آورد. خروجی reducer به فایل‌های خروجی همان پارتیشن نوشته می‌شود و در MapReduce این فایل‌ها روی HDFS می‌نشینند، پس تعداد reducerها هم تعداد فایل خروجی را تعیین می‌کند و هم درجهٔ موازی‌سازی مرحلهٔ بعد را. یک بهینه‌سازی مهم کنار reducer هست: combiner. همان تابع کاهش است که روی خروجی map و روی همان ماشین اجرا می‌شود تا حجم دادهٔ عبوری از شبکه کم شود. مجاز بودنش شرط دارد: تابع باید جابه‌جایی‌پذیر و شرکت‌پذیر باشد، مثل جمع و بیشینه، چون فریم‌ورک تضمین نمی‌کند combiner صفر بار اجرا شود یا یک بار. اگر reducer را طوری بنویسی که به ترتیب مقدارها یا به تعداد فراخوانی وابسته باشد، همین آزادی عمل فریم‌ورک جواب را عوض می‌کند.In MapReduce the framework calls the reducer once per key and hands it every value for that key as an iterator; you never loop over “the next row” yourself, you only say what to do with this key and these values. The values arrive already sorted and grouped, because that sort is what brings equal keys to the same reducer, next to each other. The reducer's output goes to the output files of its partition, and in MapReduce those files live on HDFS, so the number of reducers sets both the number of output files and the parallelism of the next stage. One important optimisation sits beside the reducer: the combiner. It is the same reduction function, run on the map output on the same machine, to shrink what crosses the network. It is only allowed if the function is commutative and associative, like sum or max, because the framework does not promise the combiner runs zero times or once. If you write a reducer that depends on the order of values or on how many times it is called, that freedom is exactly what changes your answer.

1 keyهر فراخوانی reducer فقط یک کلید و مقدارهایش را می‌بیندone call sees exactly one key and its values
0..ncombiner ممکن است هیچ، یک یا چند بار اجرا شودthe combiner may run zero, one, or many times
R filesتعداد reducerها، تعداد فایل‌های خروجی را تعیین می‌کندthe number of reducers sets the number of output files

چرا reducer فقط یک کلید را می‌بیند؟Why a reducer sees only one key

اگر reducer می‌توانست همهٔ کلیدها را با هم ببیند، دیگر نمی‌شد آن را تکه‌تکه اجرا کرد؛ هر تکه باید کل داده را می‌دید و موازی‌سازی بی‌معنی می‌شد. قرارداد MapReduce این محدودیت را آگاهانه انتخاب می‌کند: فریم‌ورک تضمین می‌کند که همهٔ مقدارهای یک کلید، و فقط آن‌ها، به یک فراخوانی می‌رسند، و در عوض اجازه می‌دهد هزار فراخوانی مستقل همزمان اجرا شوند. هر چیزی که مقایسهٔ بین کلیدها می‌خواهد — مثل «پنج فروشندهٔ برتر» یا «سهم از کل» — با یک مرحلهٔ reduce دوم، یا با یک پارتیشن‌بندی آگاهانه، یا با جمع کردن خروجی کوچک در یک گره به دست می‌آید. اگر انتظار داشته باشی reducer خودش مقایسهٔ بین کلیدها را انجام دهد، در واقع چیزی را می‌خواهی که قراردادش هرگز نداده است.If a reducer could see all keys together, it could not be split across machines: every piece would need the whole data set and parallelism would be meaningless. MapReduce chooses that limit on purpose. The framework guarantees that all values for one key, and only those, reach one call, and in exchange allows a thousand independent calls to run at once. Anything that needs a comparison across keys — “top five salespeople”, “share of the total” — comes from a second reduce stage, a deliberate partitioner, or collecting the small output on one node. Expecting the reducer itself to compare keys is asking for something the contract never gave.

کجی: وقتی یک کلید کوهی از مقدارها داردSkew: when one key owns a mountain of values

توزیع کلیدها تقریباً هیچ‌وقت یکنواخت نیست. یک کاربر پرطرفدار، یک محصول پرفروش، یا یک مقدار خالی که همهٔ رکوردهای بی‌مقدار را جمع می‌کند، می‌تواند میلیون‌ها مقدار را روی یک کلید بریزد. آن کلید به یک reducer می‌رود، بقیه در چند ثانیه کارشان تمام می‌شود و کل job منتظر همان یکی می‌ماند. دو راه متداول وجود دارد: نمک‌پاشی (salting)، یعنی کلید داغ را مصنوعاً به چند کلید می‌شکنی و نتیجه را در یک مرحلهٔ بعد جمع می‌کنی؛ و پیش‌تجمیع، یعنی بخشی از کار را در مرحلهٔ map انجام می‌دهی تا حجم ورودی reducer کم شود. هر دو راه هزینهٔ اضافه دارند و باید فقط وقتی سراغشان بروی که نمونه‌گیری نشان داده یک کلید واقعاً داغ است.Key distributions are almost never even. A popular user, a best-selling product, or a single empty value that collects every useless record can put millions of values on one key. That key goes to one reducer; the others finish in seconds and the whole job waits for that one. Two common answers: salting, where you artificially split the hot key into several keys and combine the results in a later stage; and pre-aggregation, where part of the work happens in the map stage so the reducer's input shrinks. Both cost extra work and are worth doing only when sampling shows a key is genuinely hot.

combiner در برابر reducercombiner versus reducer

combinercombinerreducerreducer
کجا اجرا می‌شودWhere it runsروی همان ماشین mapon the map machineروی ماشین جدا، بعد از شافلon another machine, after the shuffle
چند بارHow oftenصفر تا چند بارzero to many timesیک بار برای هر کلیدonce per key
شرط استفادهRequirementباید شرکت‌پذیر و جابه‌جایی‌پذیر باشدmust be associative and commutativeهر تابعی، به شرط قطعی بودنany function, as long as it is deterministic

تلهٔ رایجThe usual trap

تکیه کردن به ترتیب مقدارها. در ذهنت نوشته‌ای «آخرین مقدار برنده است» یا «اولی را بردار»، در حالی که قرارداد هیچ ترتیبی برای مقدارهای یک کلید تعریف نکرده و پیاده‌سازی‌ها هم عوض می‌شوند. اگر واقعاً ترتیب می‌خواهی، باید آن را در کلید بگذاری و مرتب‌سازی ثانویه تعریف کنی، نه اینکه به شانس تکیه کنی.Relying on the order of values. You have “last value wins” or “take the first” in your head, while the contract defines no order for the values of a key and implementations change. If you really need an order, put it in the key and define a secondary sort; do not bet on luck.

چه کسی تصمیم می‌گیرد این کار کجا اجرا شود؟Who decides where each job actually runs?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

در یک ساختمان اداری، مدیر ساختمان تعیین می‌کند کدام شرکت کدام طبقه را بگیرد و اتاق جلسه برای چه ساعتی رزرو شود. هیچ شرکتی نمی‌آید یک طبقه را قرق کند؛ اول از مدیر می‌پرسد. در خوشه هم کاری که این نقش را دارد، resource manager است.In an office building the manager decides which company gets which floor and who has the meeting room at four. No tenant walks in and takes over a floor; they ask the manager first. In a cluster, the thing playing that role is the resource manager.

تشبیه و تصویر ذهنیThe analogy

این مدیر چند تصمیم سخت دارد. باید منصف باشد، وگرنه یک تیم همهٔ ظرفیت را می‌بلعد. باید سهمیه‌ها و صف‌ها را رعایت کند، چون تیم مالی و تیم آزمایشی حق یکسانی ندارند. باید وقتی یک کار فوری می‌رسد، کار کم‌اهمیت‌تر را کنار بگذارد و ظرفیتش را آزاد کند؛ این همان preemption است. و باید با نودی که می‌میرد کنار بیاید: ظرفیتش از دست رفته و کارهایی که رویش بودند باید جای دیگری اجرا شوند. در عوض، او کد تو را اجرا نمی‌کند و ترتیب مراحل job را هم نمی‌چیند. کارش این است که ظرف بدهد و بگوید کجا؛ این‌که داخل آن ظرف چه اتفاقی می‌افتد، کار scheduler خودِ job است.That manager has several hard calls. It has to be fair, or one team swallows the whole cluster. It has to respect quotas and queues, because the finance team and the experimental team do not have the same claim. When urgent work arrives it has to push lower-priority work aside and free its capacity; that is preemption. And it has to cope with a node that dies: the capacity is gone and the work that was on it must run somewhere else. It does not run your code and it does not order the stages of your job. Its job is to hand out containers and say where; what happens inside a container is the job's own scheduler.

و بعدش، فنیAnd now, technically

در YARN، یک ResourceManager مرکزی روی کل خوشه است و روی هر نود یک NodeManager وضعیت منابع را گزارش می‌کند. اپلیکیشن با یک ApplicationMaster می‌آید و از ResourceManager ظرف می‌خواهد: ترکیبی از حافظه و واحد پردازنده روی یک نود مشخص. ResourceManager با سیاست‌هایی مثل fair و capacity بین صف‌ها تقسیم می‌کند، و برای کارهای کم‌اهمیت‌تر preemption دارد. در Kubernetes همین نقش را scheduler بازی می‌کند، با درخواست‌ها و سقف منابع در هر pod، و با بسته‌بندی روی نودها. برای batch، دو چیز بیشتر از همه مهم است: نزدیکی به داده، یعنی اجرا روی نودی که بلوک را دارد تا شبکه کمتر بخورد؛ و gang scheduling، یعنی یا همهٔ وظیفه‌ها با هم شروع شوند یا هیچ‌کدام. خودِ ResourceManager معمولاً تک‌نقطهٔ هماهنگی است و بدون standby، خرابی‌اش یعنی کل خوشه توان گرفتن کار ندارد.In YARN a central ResourceManager covers the whole cluster while a NodeManager on each node reports its resources. An application arrives with an ApplicationMaster and asks the ResourceManager for containers: a combination of memory and CPU share on a particular node. The ResourceManager divides capacity between queues with policies such as fair and capacity, and preempts lower-priority work when it must. Kubernetes plays the same role with its scheduler, using per-pod requests and limits and packing pods onto nodes. For batch, two things matter most: locality — run where the data block is, so the network is not the bottleneck — and gang scheduling, where either all tasks start together or none do. The ResourceManager itself is usually a single coordination point, and without a standby its failure means the cluster cannot accept work at all.

1 containerواحد تخصیص: حافظه و پردازنده روی یک نودthe unit of allocation: memory and CPU on one node
fair · capacityدو سیاست رایج صف‌بندی در YARNtwo common queue policies in YARN
localityنودی که داده را دارد، در اولویت استthe node that already holds the data comes first

تلهٔ رایجThe usual trap

فکر کردن این‌که مدیر منابع جلوی پر شدن نود را می‌گیرد. او فقط ظرف می‌دهد؛ اگر وظیفه‌ای سه برابر چیزی که خواسته مصرف کند، همان نود زیر فشار می‌افتد و کرنل یا مدیر خوشه وظیفه را می‌کشد. درخواست منابع یک قرارداد است، نه یک سقف تضمین‌شده.Assuming the resource manager stops a node from being overloaded. It only hands out containers; if a task uses three times what it asked for, that node comes under pressure and the kernel or the cluster manager kills the task. A resource request is a contract, not an enforced ceiling.

چرا یک group-by ساده این‌قدر گران است؟Why is one simple group-by so expensive?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

معلم هزار برگهٔ امتحان را بین چهار همکار تقسیم می‌کند. اول برگه‌ها را روی میز بزرگ بر اساس شمارهٔ کلاس به چهار دسته می‌ریزد، بعد هر معلم دستهٔ خودش را می‌برد و تصحیح می‌کند. آن ریختن و جابه‌جا کردن برگه‌ها پیش از تصحیح، همان shuffle است.A teacher splits a thousand exam papers among four colleagues. First the papers are piled into four stacks by class number on a big table; then each teacher carries their stack away and marks it. That piling and passing of papers, before any marking starts, is the shuffle.

تشبیه و تصویر ذهنیThe analogy

این تصویر سه هزینه را نشان می‌دهد که در کد دیده نمی‌شوند. اول، هر برگه باید از یک جای میز به جای دیگر برود؛ در خوشه، این جابه‌جایی از شبکه رد می‌شود و شبکه کندترین چیزی است که داری. دوم، تا وقتی دسته‌بندی تمام نشده، هیچ معلمی نمی‌تواند شروع کند: شافل یک سد است، و اگر یک دسته دیر برسد، همه منتظر می‌مانند. سوم، دسته‌ها نامتوازن‌اند. کلاسی با سیصد دانش‌آموز یک کوه کاغذ است و کلاسی با دوازده نفر یک بستهٔ نازک؛ معلمِ کوه تا شب کار می‌کند و بقیه بیکار می‌نشینند. در عوض، همین جدا کردن است که تصحیح را مستقل می‌کند: هر معلم می‌تواند بدون نگاه کردن به برگهٔ دیگری کار کند، و می‌توانی چهار معلم را همزمان بگذاری. اگر شافل نکنی و بخواهی همه با هم روی یک میز کار کنید، هم به هم برمی‌خورید و هم هیچ‌کس نمی‌تواند کارش را تا آخر ببرد.The picture shows three costs that code hides. First, every paper has to move across the room; in a cluster that move crosses the network, the slowest thing you own. Second, until the sorting is done no teacher can start: the shuffle is a barrier, and one late stack holds everyone up. Third, the stacks are uneven. A class with three hundred students is a mountain of paper, a class with twelve is a thin bundle; the teacher with the mountain works until night while the others idle. In exchange, that separation is what makes marking independent: each teacher works without looking at anyone else's papers, and you can put four teachers to work at once. Skip the shuffle, put everyone around one table, and you collide and nobody finishes.

خروجی mapMap outputدیسک محلیlocal diskپارتیشن با هشPartition by hashhash(key) % Rhash(key) % Rانتقال روی شبکهFetch over networkهر reducer سهم خودشpulls its shareادغام ران‌های مرتبMerge sorted runsکلیدها کنار همkeys side by sideورودی reducerReducer inputگروه‌بندی نهاییfinal grouping
خروجی map روی دیسک محلی پارتیشن می‌شود، از شبکه رد می‌شود و مرتب در reducer می‌نشیند.Map output is partitioned onto local disk, crosses the network, and lands sorted at the reducer.

و بعدش، فنیAnd now, technically

در MapReduce، خروجی هر map اول روی دیسک محلی همان ماشین نوشته می‌شود، نه روی HDFS؛ چون این داده موقتی است و اگر job شکست بخورد، از ورودی دوباره ساخته می‌شود. برای هر رکورد، پارتیشن با هش کلید حساب می‌شود و رکورد در بافر پارتیشن مربوطه می‌نشیند. وقتی بافر پر شد، روی دیسک ریخته می‌شود و چون کلیدها در هر ران مرتب شده‌اند، reducer می‌تواند چند رانِ مرتب را با یک ادغام ترکیب کند. مرحلهٔ بعد all-to-all است: هر reducer سهم خودش را با درخواست به هر ماشینی که map اجرا کرده می‌گیرد. این یعنی با N وظیفهٔ map و R وظیفهٔ reduce، تا N×R ارتباط ممکن است. هزینه‌ها در سه جا جمع می‌شوند: سریالایز کردن، نوشتن و خواندن روی دیسک محلی، و انتقال شبکه. به همین دلیل فشرده کردن خروجی map معمولاً می‌ارزد: CPU بیشتری می‌گیرد ولی شبکه کمتری مصرف می‌شود. در Spark همین کار با یک shuffle manager انجام می‌شود که فایل‌های map را با ایندکس می‌نویسد و reducerها بازه‌های لازم را واکشی می‌کنند.In MapReduce the output of each map task is first written to the local disk of that machine, not to HDFS, because it is temporary and can be rebuilt from the input if the job fails. For every record a partition is computed from the hash of the key and the record lands in that partition's buffer. When the buffer fills it spills to disk, and because keys are sorted within each run, a reducer can merge several sorted runs. The next stage is all-to-all: each reducer pulls its share with requests to every machine that ran a map. With N map tasks and R reduce tasks that is up to N×R connections. The costs gather in three places: serialisation, writing and reading local disk, and network transfer. That is why compressing map output usually pays: it spends CPU to save network. Spark does the same job with its shuffle manager, writing map files with an index that reducers use to fetch only the ranges they need.

hash % Rهر کلید به یک پارتیشن و در نتیجه یک reducer می‌رودeach key goes to one partition, and so to one reducer
N × Rدر بدترین حالت، هر وظیفهٔ map به هر reducer داده می‌دهدin the worst case every map task feeds every reducer
≈ 100 MBاندازهٔ معمول یک پارتیشن برای یک وظیفهٔ reducea typical partition size for one reduce task

شافل گران است چون داده سه بار جابه‌جا می‌شودA shuffle is expensive because data moves three times

هر رکورد در مسیر شافل چند بار دست به دست می‌شود: یک بار موقع نوشتن در بافر، یک بار موقع ریختن روی دیسک، یک بار موقع خواندن از دیسک، و یک بار موقع رفتن روی شبکه. این نوشتن‌های میانی روی HDFS نیستند، روی دیسک محلی همان ماشین‌اند، و همین تفاوت مهم است: اگر آن نود از دست برود، دادهٔ میانی هم از دست می‌رود و باید از ورودی بازسازی شود. به همین دلیل در Hadoop، از دست دادن یک نود در میانهٔ job می‌تواند کل مرحلهٔ map را دوباره اجرا کند. صرفه‌جویی‌های عملی هم از همین‌جا می‌آیند: کم کردن حجم خروجی map با combiner، فشرده کردن آن، و درست انتخاب کردن تعداد reducer.Every record changes hands several times: into the buffer, out to disk, back from disk, and across the network. That intermediate data is not on HDFS but on local disk, and that difference matters: lose the node and you lose the intermediate data, so it is rebuilt from the input. In Hadoop, losing a node in the middle of a job can therefore re-run the entire map stage. The practical savings come from the same place: shrink map output with a combiner, compress it, and pick the number of reducers on purpose.

راه‌های فرار از شافلWays to avoid a shuffle

هر شافل یک تصمیم است، نه یک اجبار. اگر یکی از دو طرف join کوچک باشد، می‌شود آن را به همهٔ ماشین‌ها فرستاد و طرف بزرگ را بدون جابه‌جایی خواند؛ این همان broadcast join است. اگر داده از قبل با همان کلید پارتیشن شده باشد — مثلاً هر دو جدول با یک bucketing یکسان — می‌شود partition-wise join کرد و شافل را حذف کرد. و اگر تجمیع مورد نظر شرکت‌پذیر باشد، combiner بخش زیادی از حجم را قبل از شبکه کم می‌کند. هر سه راه یک چیز را می‌خرند: وقت، به قیمت حافظه یا محدودیت در شکل کوئری. به همین دلیل است که در کار واقعی، اول شکل کوئری را نگاه می‌کنند و بعد سراغ تنظیم تعداد پارتیشن‌ها می‌روند.A shuffle is a decision, not a law. If one side of a join is small, ship it to every machine and read the big side without moving it; that is a broadcast join. If both sides are already partitioned on the join key — both bucketed the same way, say — you can join partition by partition and skip the shuffle. And if the aggregation is associative, a combiner removes much of the volume before the network. All three buy time with memory or with a constraint on the shape of the query. That is why in real work people look at the shape of the query first and tune the partition count later.

تلهٔ رایجThe usual trap

فکر کردن این‌که شافل فقط ترافیک شبکه است. بخش بزرگ‌تری از هزینه، نوشتن روی دیسک محلی و خواندن دوبارهٔ آن است؛ همان دیسکی که با لاگ و کش مشترک است. وقتی دیسک محلی پر شود، وظیفه‌ها با خطای نامفهوم می‌میرند، و اگر نود از دست برود، خروجی map هم با آن می‌رود و باید دوباره ساخته شود.Believing the shuffle is only network traffic. The bigger share of the cost is writing local disk and reading it back, on the same disk that holds logs and caches. When local disk fills, tasks die with an unrelated-looking error, and when a node is lost the map output goes with it and has to be rebuilt.

کار دوباره اجرا شد. ایمیل دوباره رفت؟The task ran again. Did the email go out again?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

دکمهٔ پرداخت را می‌زنی، اینترنت قطع می‌شود، دوباره می‌زنی. پول دو بار کم شد ولی سفارش یکی است. آن پرداخت، چیزی بود بیرون از دفتر سفارش‌ها که کسی تکرارش را نشمرده بود.You tap pay, the connection drops, you tap again. The money left twice but there is one order. That payment was something outside the order book, and nobody was counting its repeats.

تشبیه و تصویر ذهنیThe analogy

در batch و stream، تکرار یک ویژگی است، نه یک حادثه: وظیفه‌ای که شکست بخورد دوباره اجرا می‌شود، و یک وظیفهٔ کند ممکن است همزمان دو نسخه داشته باشد تا زودتر تمام شود. هر چیزی که بیرون از داده تغییر می‌کند — ایمیل، پیامک، تراکنش بانکی، شمارنده — با هر تلاش دوباره تکرار می‌شود، چون فریم‌ورک فقط از خروجی خودش خبر دارد. راه‌های ایمن کردنش سه‌تاست: اثر را با یک کلید یکتا idempotent کنی تا تکرارش بی‌اثر شود؛ اثر را به همان کامیتِ خروجی گره بزنی تا یا هر دو با هم اتفاق بیفتند یا هیچ‌کدام؛ یا اثر را جایی صف کنی و بعد از موفقیت job، یک بار و با حذف تکراری‌ها بفرستی. هر سه یک چیز می‌خرند: ایمنی در برابر تکرار، به قیمت پیچیدگی.In batch and stream processing, repetition is a feature, not an accident: a failed task runs again, and a slow task may run as two copies at once so one finishes sooner. Anything that changes the world outside the data — an email, an SMS, a bank transaction, a counter — repeats with every attempt, because the framework only knows about its own output. There are three ways to make it safe: make the effect idempotent under a unique key so a repeat does nothing; tie the effect to the same commit as the output so both happen or neither does; or queue the effect and send it once, after the job succeeds, with duplicates removed. All three buy safety against repeats at the price of complexity.

و بعدش، فنیAnd now, technically

اثر جانبی هر تعاملی است با دنیای بیرون که در مدل دادهٔ job ثبت نمی‌شود. نوشتن در همان سیستم ذخیره‌سازی خروجی معمولاً اثر جانبی حساب نمی‌شود، چون بخشی از خروجی است و در کامیت اتمی می‌آید. اما فرستادن ایمیل، صدا زدن API یک شرکت دیگر، یا افزایش یک شمارنده در سیستم دیگر، بیرون از آن کامیت است و فریم‌ورک هیچ راهی ندارد بفهمد تکرار شده. در MapReduce، وظیفهٔ map یا reduce ممکن است چند بار اجرا شود، و یک نسخهٔ حدسی هم ممکن است همزمان با نسخهٔ اصلی تمام شود. اگر در همان تابع ایمیل بفرستی، دو ایمیل می‌فرستی. در Spark، نوشتن مستقیم در یک پایگاه داده از داخل foreachPartition همین مشکل را دارد: تلاش دوباره یعنی نوشتن دوباره. راه‌حل درست، اثر را داخل خود کامیت آوردن است — مثلاً جدول موقت و بعد جابه‌جایی — یا idempotent کردن نوشتن با کلید طبیعی، یا استفاده از الگوی outbox. عبارت exactly-once همیشه دربارهٔ اثر جانبی است، نه دربارهٔ محاسبه.A side effect is any interaction with the outside world that is not recorded in the job's data model. Writing to the output store is usually not a side effect, because it is part of the output and arrives in the atomic commit. Sending an email, calling another company's API, or bumping a counter in another system sits outside that commit, and the framework has no way to know it happened twice. In MapReduce a map or reduce task may run several times, and a speculative copy may finish alongside the original. Send the email inside that function and you send two. In Spark, writing straight to a database from foreachPartition has the same problem: a retry means a second write. The clean answers are to bring the effect inside the commit (a staging table, then a swap), to make the write idempotent on a natural key, or to use the outbox pattern. The phrase exactly-once is always about the side effect, never about the computation.

at-least-onceبا هر تلاش دوباره، اثر هم دوباره تکرار می‌شودevery retry repeats the effect as well
1 keyکلید یکتا، تکرار اثر را بی‌خطر می‌کندa unique key makes a repeated effect harmless
commitاثر را به همان کامیت گره بزن تا با هم اتفاق بیفتندtie the effect to the commit so both happen together

تلهٔ رایجThe usual trap

گذاشتن اثر جانبی داخل تابعی که فریم‌ورک دوباره اجرا می‌کند. یک خط send_email در map کافی است تا بعد از یک تلاش دوباره، مشتری دو نسخه از یک خبر بگیرد. نشانه‌اش این است که تعداد ایمیل‌ها از تعداد رکوردها بیشتر می‌شود و هیچ خطایی هم دیده نمی‌شود.Putting a side effect inside a function the framework re-runs. One send_email line in a map is enough for a customer to get the same news twice after a retry. The symptom is more emails than records, with no error anywhere.

حافظه پر شد. کار وسط راه می‌ماند؟Memory filled up. Does the work just stop?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

می‌خواهی کتابخانهٔ خانه را مرتب کنی و میز کوچک است. کتاب‌ها را دسته‌دسته روی زمین کنار دیوار می‌چینی، هر دسته را مرتب می‌کنی، و آخر سر دسته‌ها را یکی‌یکی برمی‌داری و در قفسه می‌گذاری. آن چیدن موقت روی زمین، همان spill است.You want to sort the books at home and the table is small. You stack them in piles on the floor along the wall, each pile in order, and at the end you pick the piles up one by one and shelve them. That temporary stacking on the floor is the spill.

تشبیه و تصویر ذهنیThe analogy

این راه‌حل، کار را نجات می‌دهد ولی کند می‌کند. هر کتاب دو بار جابه‌جا می‌شود: یک بار روی زمین، یک بار از زمین به قفسه. اگر زمین هم پر شود، باید بساط را جمع کنی و از اول شروع کنی؛ در کامپیوتر همین اتفاق وقتی می‌افتد که چند وظیفه همزمان روی یک دیسک محلی spill کنند و جا تمام شود. نکتهٔ دوم این است که ترتیب حفظ می‌شود: چون هر دسته هنگام چیدن مرتب شده، آخر کار می‌توانی دسته‌ها را با هم ادغام کنی و یک ترتیب نهایی بگیری. این همان ترفند اصلی external sort است؛ بدون مرتب کردن هر دسته، ادغام بی‌فایده می‌شود.The trick saves the job but slows it down. Every book moves twice: once to the floor, once from the floor to the shelf. If the floor fills too, you have to clear it and start over; on a computer that happens when several tasks spill onto the same local disk and it runs out of room. The second point is that order survives: because each pile was sorted as it was built, at the end you can merge the piles into one final order. That is the core of external sort; without sorting each pile, merging them would be pointless.

و بعدش، فنیAnd now, technically

spill وقتی رخ می‌دهد که حجم کاری یک عملگر از حافظهٔ در اختیارش بیشتر شود. در مرتب‌سازی، الگوریتم به external merge sort تبدیل می‌شود: بافر را پر می‌کنی، مرتب می‌کنی، به‌عنوان یک ران روی دیسک می‌نویسی، و در پایان چند رانِ مرتب را با یک ادغام چندمسیره به یک جریان مرتب تبدیل می‌کنی. در تجمیع و join هشی، وقتی جدول هش از سقف رد شد، بخشی از کلیدها به پارتیشن‌های روی دیسک ریخته می‌شوند و بعد هر پارتیشن جدا پردازش می‌شود. در Spark، بافر شافل هم spill می‌شود: ابتدا فایل‌های موقت روی دیسک محلی، سپس ادغام موقع خواندن. هزینه‌اش دو بخش دارد: نوشتن و خواندن اضافی روی دیسک، و سریالایز کردن. در عمل یک job که به spill می‌افتد می‌تواند چند برابر کندتر شود، بدون این‌که هیچ خطایی بدهد. بدتر، spill به دیسک محلی است؛ اگر آن نود از دست برود، دادهٔ میانی هم می‌رود.A spill happens when an operator's working set outgrows the memory it was given. In a sort, the algorithm becomes an external merge sort: fill the buffer, sort it, write it out as one run, and at the end merge the sorted runs into a single ordered stream. In hash aggregation and hash joins, once the table passes its limit part of the keys are written to partitions on disk and each partition is processed on its own. In Spark the shuffle buffer spills the same way: temporary files on local disk first, merged when read. The cost has two parts: the extra write and read on disk, and serialisation. In practice a job that starts spilling can run several times slower with no error at all. Worse, the spill lives on local disk, so if that node is lost the intermediate data goes with it.

k-way mergeچند رانِ مرتب با هم ادغام می‌شوندseveral sorted runs are merged at once
local diskمحل ریختن: دیسک محلی، نه HDFSwhere it lands: local disk, not HDFS
2–3×کندی معمول مرحله‌ای که به spill می‌افتدthe usual slowdown of a stage that starts spilling

تلهٔ رایجThe usual trap

فکر کردن این‌که spill فقط کندی است. اول، دیسک محلی سقف دارد و چند وظیفهٔ همزمان می‌توانند آن را پر کنند؛ آن وقت وظیفه‌ها با خطای نامربوط می‌میرند. دوم، دادهٔ ریخته‌شده روی همان نودی است که ممکن است از دست برود. اگر در داشبورد فقط زمان اجرا را ببینی، spill را نمی‌بینی تا روزی که job از یک ساعت به شش ساعت برسد.Treating a spill as nothing but slowness. First, local disk has a ceiling and several concurrent tasks can fill it, and then tasks die with an error that looks unrelated. Second, the spilled data sits on a node that may disappear. If your dashboard only shows run time, you will not see the spill until a job goes from one hour to six.