1Go: DailyReport tạo ctx bằng WithCancel, tạo errc có buffer len(shards) rồi gọi queryShard cho từng shard. Cả ba goroutine hanoi, danang, hcm được khởi động trước khi chờ, vì các truy vấn độc lập nhau.Go: DailyReport creates ctx with WithCancel, makes errc with a len(shards) buffer and calls queryShard for every shard. All three goroutines, hanoi, danang and hcm, start before any waiting because the queries are independent.
2Mỗi queryShard gọi s.DailyRevenue(ctx, day) qua interface Shard, nhận []Revenue rồi gửi từng dòng vào out chan Revenue của riêng nó. danang trả sớm nhất (~0,6 giây) và không phải đợi hai nhánh kia.Each queryShard calls s.DailyRevenue(ctx, day) through the Shard interface, gets []Revenue and sends each row into its own out chan Revenue. danang answers first (~0.6s) and does not wait for the other two.
3Merge(ctx, outs...) chạy một forwarder cho mỗi channel vào: select đọc value, ok := <-in rồi select gửi out <- v vào channel gộp, cả hai đều kèm nhánh <-ctx.Done().Merge(ctx, outs...) runs one forwarder per input channel: a select reads value, ok := <-in and another select sends out <- v into the merged channel, both alongside a <-ctx.Done() case.
4DailyReport là bên đọc duy nhất: for r := range Merge(...) cộng totals[r.ShopID] += r.Amount. Chỉ một goroutine chạm vào map nên không cần khoá.DailyReport is the only reader: for r := range Merge(...) adds totals[r.ShopID] += r.Amount. Only one goroutine touches the map, so no lock is needed.
5hanoi xong ở ~1,0 giây, hcm ở ~1,1 giây. Mỗi queryShard defer close(out); forwarder thấy ok == false thì return và gọi wg.Done(). Một nguồn đóng chưa có nghĩa là được đóng channel gộp.hanoi finishes at ~1.0s and hcm at ~1.1s. Each queryShard does defer close(out); the forwarder sees ok == false, returns and calls wg.Done(). One closed source does not mean the merged channel may close.
6Goroutine closer của Merge: wg.Wait() trả về rồi close(out). Đóng ở đây an toàn vì không còn forwarder nào gửi được nữa; vòng range trong DailyReport kết thúc.Merge's closer goroutine: wg.Wait() returns, then close(out). Closing here is safe because no forwarder can send anymore; the range loop in DailyReport ends.
7DailyReport kiểm tra ctx.Err() rồi select errc. Nếu một shard lỗi, errc chứa lỗi đã bọc tên shard và hàm trả nil, err; ở đây không có lỗi nên trả totals sau ~1,1 giây thay vì 2,7 giây tuần tự.DailyReport checks ctx.Err() and then selects on errc. If a shard failed, errc holds an error prefixed with the shard name and the function returns nil, err; here there is none, so it returns totals after ~1.1s instead of 2.7s sequentially.
8Java cùng ý tưởng trong ShardReportService.dailyReport: mỗi ShardClient thành một Callable, ioPool.invokeAll(tasks, 5, SECONDS) chạy song song trên ExecutorService, rồi vòng f.get() gộp bằng totals.merge(r.shopId(), r.amount(), Long::sum).Java follows the same idea in ShardReportService.dailyReport: each ShardClient becomes a Callable, ioPool.invokeAll(tasks, 5, SECONDS) runs them in parallel on the ExecutorService, then the f.get() loop merges with totals.merge(r.shopId(), r.amount(), Long::sum).
9Biến thể không chặn AsyncShardReport: CompletableFuture.supplyAsync(() -> s.dailyRevenue(day), ioPool) cho từng shard, rồi allOf(...).thenApply gộp bằng groupingBy(Revenue::shopId, summingLong(Revenue::amount)).The non-blocking variant AsyncShardReport: CompletableFuture.supplyAsync(() -> s.dailyRevenue(day), ioPool) per shard, then allOf(...).thenApply merges with groupingBy(Revenue::shopId, summingLong(Revenue::amount)).