史上最简单的 mit 6.824 分布式系统 Lab 1 MapReduce Part 2/2
上一集的代码有一个问题,如果worker在写入输出文件时crash,输出的结果就会是partially written的。总之就是生成了不一致状态。解决方法可以参考TA的说法:
To ensure that nobody observes partially written files in the presence of crashes, the MapReduce paper mentions the trick of using a temporary file and atomically renaming it once it is completely written. You can use ioutil.TempFile to create a temporary file and os.Rename to atomically rename it.
代码
1 | func WriteToMapperFile(intermediateKeyValue []KeyValue, mapperTaskId, reducerId int) error { |