Skip leadership check if the etcd instance is active processing heartbeat

Signed-off-by: Benjamin Wang <benjamin.ahrtr@gmail.com>
This commit is contained in:
Benjamin Wang
2024-08-09 11:26:15 +01:00
parent df4e472a2d
commit b8b0cf83d1
3 changed files with 71 additions and 2 deletions

View File

@ -29,6 +29,7 @@ import (
"github.com/coreos/go-semver/semver"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"
@ -1538,3 +1539,45 @@ func TestWaitAppliedIndex(t *testing.T) {
})
}
}
func TestIsActive(t *testing.T) {
cases := []struct {
name string
tickMs uint
durationSinceLastTick time.Duration
expectActive bool
}{
{
name: "1.5*tickMs,active",
tickMs: 100,
durationSinceLastTick: 150 * time.Millisecond,
expectActive: true,
},
{
name: "2*tickMs,active",
tickMs: 200,
durationSinceLastTick: 400 * time.Millisecond,
expectActive: true,
},
{
name: "4*tickMs,not active",
tickMs: 150,
durationSinceLastTick: 600 * time.Millisecond,
expectActive: false,
},
}
for _, tc := range cases {
s := EtcdServer{
Cfg: config.ServerConfig{
TickMs: tc.tickMs,
},
r: raftNode{
tickMu: new(sync.RWMutex),
latestTickTs: time.Now().Add(-tc.durationSinceLastTick),
},
}
require.Equal(t, tc.expectActive, s.isActive())
}
}