The built-in rpc method in go language cannot return the result correctly

41 Views Asked by At

I am completing lab 1 of the MIT 6.824 course. When I tried to use RPC to let the client call the method registered on the server, I encountered the problem that the server method could not return parameters correctly. details as follows:

method on the server:

func (c *Coordinator) GetTask(args *string, reply *ReplyStruct) error {
    reply.AS = ReduceFileNames
    reply.filename = ApplyForMapTask()
    reply.MapOrReduce = "map"
    log.Println(reply)
    return nil
}

func ApplyForMapTask() string {
    if fn, ok := <-mapFileNames; ok == true {
        return fn
    } else {
        isMap = false
        sync.OnceFunc(switchStatus)
        return "map task finish"
    }
}

client trying to call GetTask method on the server:

func CallWork() ReplyStruct {
    args := "test"
    replyTask := ReplyStruct{}

    ok := call("Coordinator.GetTask", &args, &replyTask)

    if ok {
        log.Println(replyTask)
    } else {
        fmt.Printf("call failed!\n")
        os.Exit(666)
    }
    return replyTask
}

the definition of ReplyStruct

type ReplyStruct struct {
    filename    string
    MapOrReduce string
    AS          chan string
}

And here is the output of the console:

server:

2024/02/01 19:28:12 &{pg-being_ernest.txt map 0x140000ae600}

client:

2024/02/01 19:28:12 { map <nil>}

I want the reply pointer to always point to the same location, or have the same value. But the pointer is different between client and server, these two pointers point to different memory spaces, which prevents me from getting the correct results.

0

There are 0 best solutions below