我的代码可以通过所有测试,但不包括 mit6.824 lab1-mapreduce 中的作业计数测试。
输出如下:
*** Starting job count test.
--- map jobs ran incorrect number of times (10 != 8)
--- job count test: FAIL
我通过注释文件的其余部分来单独运行 test-mr.sh 的作业计数部分,但发现我实际上有 8 个文件而不是 10 个。
jobcount.go中job count插件的map和reduce函数如下:
func Map(filename string, contents string) []mr.KeyValue {
me := os.Getpid()
f := fmt.Sprintf("mr-worker-jobcount-%d-%d", me, count)
count++
err := ioutil.WriteFile(f, []byte("x"), 0666)
if err != nil {
panic(err)
}
time.Sleep(time.Duration(2000+rand.Intn(3000)) * time.Millisecond)
return []mr.KeyValue{mr.KeyValue{"a", "x"}}
}
func Reduce(key string, values []string) string {
files, err := ioutil.ReadDir(".")
if err != nil {
panic(err)
}
invocations := 0
for _, f := range files {
invocations++
}
return strconv.Itoa(invocations)
}
作业计数测试程序如下:
echo '***' Starting job count test.
rm -f mr-*
$TIMEOUT ../mrcoordinator ../pg*txt &
sleep 1
$TIMEOUT ../mrworker ../../mrapps/jobcount.so &
$TIMEOUT ../mrworker ../../mrapps/jobcount.so
$TIMEOUT ../mrworker ../../mrapps/jobcount.so &
$TIMEOUT ../mrworker ../../mrapps/jobcount.so
NT=`cat mr-out* | awk '{print $2}'`
if [ "$NT" -eq "8" ]
then
echo '---' job count test: PASS
else
echo '---' map jobs ran incorrect number of times "($NT != 8)"
echo '---' job count test: FAIL
failed_any=1
fi
wait
我的环境是wsl2中的ubantu。
我在 jobcount.go 中添加了一些代码
for _, f := range files {
if strings.HasPrefix(f.Name(), "mr-worker-jobcount") {
log.Printf("f: %v\n", f)
invocations++
}
}
所以我改变了 jobcount.go 中的reduce函数来删除重复的元素。
func Reduce(key string, values []string) string {
files, err := ioutil.ReadDir(".")
if err != nil {
panic(err)
}
invocations := 0
mp := map[string]struct{}{}
for _, f := range files {
if _, ok := mp[f.Name()]; strings.HasPrefix(f.Name(), "mr-worker-jobcount") && !ok {
invocations++
mp[f.Name()] = struct{}{}
}
}
return strconv.Itoa(invocations)
}
那么我就可以通过所有的测试了
我测试和调试了很多次,但仍然不知道为什么会发生。
我想知道为什么 ioutil.ReadDir 函数的输出是错误的,以及如何在不更改 jobcount.go 的情况下通过测试。
我在wsl2中也有同样的问题,原因可能是你在Windows文件系统中运行测试,我将测试复制到wsl2的文件系统中(例如/home/user/),一切都很好。