arcaitan
V2EX  ›  问与答

想问一个 go 的技术问题

  •  
  •   arcaitan · Apr 9, 2020 · 3527 views
    This topic created in 2402 days ago, the information mentioned may be changed or developed.
    在做 6.824 的 lab2A,现在有个问题我搞了几天,一直没找到原因
    下面这个方法,
    执行的时候打印出了 line1 处的 DPrintf("follower rf %v voted for other!\n", rf.me),
    然后就卡住了,永远也打印不出 line2 的("-------------------------------rf %v ----for step %v end!!! \n\n\n", rf.me, i)
    有人有思路么?理论上,如果我已经走到了 line1,下一步不就是跳到 switch 语句外面的 line2 了么?

    go func() {
    i:=0
    for {
    i++
    fmt.Printf("----------------------------------------------------------------rf %v ----for step %v start\n", rf.me, i)
    //...
    switch state {
    case FOLLOWER:
    rf.resetElectionTimer()
    select{
    case <- rf.heartBeatCh:
    DPrintf("follower rf %v heartbeat for other!\n", rf.me)
    rf.stopElectionTimer()

    case <- rf.voteCh:
    rf.stopElectionTimer()
    DPrintf("follower rf %v voted for other!\n", rf.me) //line1

    case <- rf.electionTimer.C:
    DPrintf("follower rf %v timeout change to candidate\n", rf.me)
    rf.changeState(CANDIDATE, term)
    //rf.resetElectionTimer()

    }
    case CANDIDATE:
    //...

    }
    case LEADER:
    //...
    }
    //line2
    fmt.Printf("-------------------------------rf %v ----for step %v end!!! \n\n\n", rf.me, i)
    }
    }()
    Supplement 1  ·  Apr 9, 2020
    搞定了,please ignore
    不是我问题描述里的问题,是因为在 switch 里用了 resettimer,导致了之后卡在 select 里
    8 replies  •  2020-04-09 14:33:06 +08:00
    linauror
    linauror
    Apr 9, 2020
    1
    DPrintf 怎么定义的,是不是打印后就结束程序了?
    arcaitan
    arcaitan
    OP
    Apr 9, 2020
    2
    @linauror 不是,就是个带了开关的 log 打印

    // Debugging
    const Debug = 1

    func DPrintf(format string, a ...interface{}) (n int, err error) {
    if Debug > 0 {
    log.Printf(format, a...)
    }
    return
    }
    zjh31415926
    zjh31415926
    Apr 9, 2020
    3
    哎,这代码也不好好格式化下,谁愿意琢磨啊
    hawken
    hawken
    Apr 9, 2020
    4
    把代码放到 play.golang.org 里吧
    arcaitan
    arcaitan
    OP
    Apr 9, 2020
    5
    @zjh31415926 不是,格式化好的,但是提交到 v2 之后就变成这样了,也没办法 edit
    arcaitan
    arcaitan
    OP
    Apr 9, 2020
    6
    @hawken 真要跑起来依赖很多其他文件的
    Vegetable
    Vegetable
    Apr 9, 2020
    7
    题外话, 用 markdown 语法,或者 github gist 都可以
    arcaitan
    arcaitan
    OP
    Apr 9, 2020
    8
    @Vegetable 下次注意
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4915 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 08:49 · PVG 16:49 · LAX 00:49 · JFK 03:49
    ♥ Do have faith in what you're doing.