Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add SuperIDLE config, IDLE node can't change to FOLLOER when… #57

Merged
merged 1 commit into from
Jan 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions src/cli/cmd/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,17 +328,11 @@ func mysqlRebuildMeCommandFn(cmd *cobra.Command, args []string) {

// 16. enable raft
{
// check whether the state is IDLE or not
if conf.Raft.StartAsIDLE {
log.Warning("S16-->enable.raft.skiped.since.StartAsIDLE=true...")
log.Warning("S16-->run.as.IDLE...")
} else {
log.Warning("S16-->enable.raft.begin...")
if _, err := callx.EnableRaftRPC(self); err != nil {
log.Error("enbleRaftRPC.error[%v]", err)
}
log.Warning("S16-->enable.raft.done...")
log.Warning("S16-->enable.raft.begin...")
if _, err := callx.EnableRaftRPC(self); err != nil {
log.Error("enbleRaftRPC.error[%v]", err)
}
log.Warning("S16-->enable.raft.done...")
}

// 17. wait change to master
Expand Down
4 changes: 2 additions & 2 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ type RaftConfig struct {
// purge binlog interval (ms)
PurgeBinlogInterval int `json:"purge-binlog-interval"`

// start as idle
StartAsIDLE bool `json:"start-as-idle"`
// Super IDLE cant' change to FOLLOWER.
SuperIDLE bool `json:"super-idle"`

// MUST: set in init
// the shell command when leader start
Expand Down
3 changes: 2 additions & 1 deletion src/raft/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ func (r *Raft) Start() error {
r.c = make(chan *ev)

// state
if r.conf.StartAsIDLE {
if r.conf.SuperIDLE {
r.setState(IDLE)
r.WARNING("start.as.super.IDLE")
} else {
r.setState(FOLLOWER)
}
Expand Down
4 changes: 2 additions & 2 deletions src/raft/raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -836,12 +836,12 @@ func TestRaftElectionUnderIDLEInMajority(t *testing.T) {
// test run as IDLE with config.StartAsIDLE=true configuration
//
// TEST PROCESSES:
// 1. Start 1 raft with StartAsIDLE=true
// 1. Start 1 raft with SuperIDLE=true
// 2. check the IDLE
func TestRaftStartAsIDLE(t *testing.T) {
log := xlog.NewStdLog(xlog.Level(xlog.PANIC))
conf := config.DefaultRaftConfig()
conf.StartAsIDLE = true
conf.SuperIDLE = true
port := common.RandomPort(8100, 8200)
_, rafts, cleanup := MockRaftsWithConfig(log, conf, port, 1)
defer cleanup()
Expand Down
9 changes: 7 additions & 2 deletions src/raft/rpc_ha.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ func (h *HARPC) HAEnable(req *model.HARPCRequest, rsp *model.HARPCResponse) erro
state := h.raft.getState()
switch state {
case IDLE:
h.raft.setState(FOLLOWER)
h.raft.loopFired()
if h.raft.conf.SuperIDLE {
// Set SuperIDLE to noLeader to fire the 'change master to'.
h.raft.setLeader(noLeader)
} else {
h.raft.setState(FOLLOWER)
h.raft.loopFired()
}
rsp.RetCode = model.OK
return nil
case STOPPED:
Expand Down
78 changes: 78 additions & 0 deletions src/raft/rpc_ha_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,3 +485,81 @@ func TestRaftRPCHATryToLeaderFail_MySQLUnpromotble(t *testing.T) {
assert.Equal(t, whoisleader, GTIDCIDX)
}
}

func TestRaftSuperIDLEEnableHA(t *testing.T) {
var testName = "TestRaftSuperIDLEEnableHA"
var want, got State
var whoisleader int
var leader *Raft
var idler *Raft

log := xlog.NewStdLog(xlog.Level(xlog.PANIC))
port := common.RandomPort(8000, 9000)
names, rafts, cleanup := MockRafts(log, port, 3)
defer cleanup()

// 1. Start 3 rafts.
{
for i, raft := range rafts {
if i == 2 {
raft.conf.SuperIDLE = true
idler = raft
}
raft.Start()
}
}

// 2. wait leader election.
{
MockWaitLeaderEggs(rafts, 1)
whoisleader = 0
got = 0
want = (LEADER + FOLLOWER + IDLE)
for i, raft := range rafts {
got += raft.getState()
if raft.getState() == LEADER {
whoisleader = i
}
}
// [LEADER, FOLLOWER, IDLE]
assert.Equal(t, want, got)
}
idlerLeader1 := idler.getLeader()

// 3. set leader handlers to mock
{
leader = rafts[whoisleader]
log.Warning("%v.leader[%v].set.mock.functions", testName, rafts[whoisleader].getID())
leader.L.setProcessHeartbeatRequestHandler(leader.mockLeaderProcessHeartbeatRequest)
leader.L.setProcessRequestVoteRequestHandler(leader.mockLeaderProcessRequestVoteRequest)
}

// 4. Stop leader hearbeat
{
log.Warning("%v.leader[%v].Stop.heartbeat", testName, leader.getID())
leader.L.setSendHeartbeatHandler(leader.mockLeaderSendHeartbeat)
}

// 5. Wait new leader.
{
MockWaitLeaderEggs(rafts, 2)
}

{
c, cleanup := MockGetClient(t, names[2])
defer cleanup()

method := model.RPCHAEnable
req := model.NewHARPCRequest()
rsp := model.NewHARPCResponse(model.OK)
err := c.Call(method, req, rsp)
assert.Nil(t, err)

want := model.OK
got := rsp.RetCode
assert.Equal(t, want, got)
}

idlerLeader2 := idler.getLeader()
assert.NotEqual(t, idlerLeader1, idlerLeader2)
}