Skip to content
This repository has been archived by the owner on Apr 26, 2022. It is now read-only.

Commit

Permalink
fix: pass test in replica_health
Browse files Browse the repository at this point in the history
  • Loading branch information
neverchanje committed May 8, 2021
1 parent 12aeb57 commit 47a3fef
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 24 deletions.
46 changes: 24 additions & 22 deletions client/fake_meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,36 @@ func (m *fakeMeta) QueryConfig(tbName string) (*replication.QueryCfgResponse, er
resp := &replication.QueryCfgResponse{}

for _, app := range m.availableApps {
if app.AppName == tbName {
resp.AppID = app.AppID
resp.PartitionCount = app.PartitionCount
resp.Partitions = make([]*replication.PartitionConfiguration, app.PartitionCount)
for i := range resp.Partitions {
resp.Partitions[i] = &replication.PartitionConfiguration{
Pid: &base.Gpid{Appid: app.AppID, PartitionIndex: int32(i)},
Primary: &base.RPCAddress{}, // even the primary is unavailable, it should still not be nil
MaxReplicaCount: 3,
}
if app.AppName != tbName {
continue
}

resp.AppID = app.AppID
resp.PartitionCount = app.PartitionCount
resp.Partitions = make([]*replication.PartitionConfiguration, app.PartitionCount)
for i := range resp.Partitions {
resp.Partitions[i] = &replication.PartitionConfiguration{
Pid: &base.Gpid{Appid: app.AppID, PartitionIndex: int32(i)},
Primary: &base.RPCAddress{}, // even the primary is unavailable, it should still not be nil
MaxReplicaCount: 3,
}
}

for _, n := range fakePegasusCluster.nodes {
for pri := range n.primaries {
if pri.Appid == app.AppID {
pc := resp.Partitions[int(pri.PartitionIndex)]
pc.Primary = n.n.RPCAddress()
}
for _, n := range fakePegasusCluster.nodes {
for pri := range n.primaries {
if pri.Appid == app.AppID {
pc := resp.Partitions[int(pri.PartitionIndex)]
pc.Primary = n.n.RPCAddress()
}
for sec := range n.secondaries {
if sec.Appid == app.AppID {
pc := resp.Partitions[int(sec.PartitionIndex)]
pc.Secondaries = append(pc.Secondaries, n.n.RPCAddress())
}
}
for sec := range n.secondaries {
if sec.Appid == app.AppID {
pc := resp.Partitions[int(sec.PartitionIndex)]
pc.Secondaries = append(pc.Secondaries, n.n.RPCAddress())
}
}
return resp, nil
}
return resp, nil
}

resp.Err = &base.ErrorCode{Errno: base.ERR_OBJECT_NOT_FOUND.String()}
Expand Down
15 changes: 15 additions & 0 deletions client/migrate_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,18 @@ func TestDowngradeNodeHasPrimaries(t *testing.T) {
err := DowngradeNode(fakePegasusCluster.meta, fakePegasusCluster.nodes[0].n)
assert.Error(t, err) // failed
}

func TestDowngrade1Node(t *testing.T) {
fakePegasusCluster = newFakeCluster(4)
createFakeTable("test1", 32)
createFakeTable("test2", 64)
createFakeTable("test3", 128)

effectedReplicas := map[base.Gpid]int{}
replicaServer := fakePegasusCluster.nodes[1]
safelyDowngradeNode(t, replicaServer, &effectedReplicas)

// ensure all replicas are shutdown
assert.Empty(t, fakePegasusCluster.nodes[1].primaries)
assert.Empty(t, fakePegasusCluster.nodes[1].secondaries)
}
9 changes: 7 additions & 2 deletions client/replica_health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package client

import (
"sort"
"testing"

"github.com/XiaoMi/pegasus-go-client/idl/admin"
Expand Down Expand Up @@ -86,7 +87,7 @@ func TestGetTableHealthInfo(t *testing.T) {
}

func TestGetClusterReplicaInfo(t *testing.T) {
fakePegasusCluster = newFakeCluster(4)
fakePegasusCluster = newFakeCluster(9)
createFakeTable("test1", 32)
createFakeTable("test2", 64)
createFakeTable("test3", 128)
Expand All @@ -107,9 +108,13 @@ func TestGetClusterReplicaInfo(t *testing.T) {
safelyDowngradeNode(t, fakePegasusCluster.nodes[0], &effectedReplicas)

c, _ := GetClusterReplicaInfo(meta)
assert.Equal(t, len(c.Nodes), 4)
assert.Equal(t, len(c.Nodes), 9)
assert.Equal(t, len(c.Tables), 3)

// sort nodes by ipport
sort.Slice(c.Nodes, func(i, j int) bool {
return c.Nodes[i].IPPort < c.Nodes[j].IPPort
})
assert.Equal(t, c.Nodes[0].PrimariesNum, 0)
assert.Equal(t, c.Nodes[0].SecondariesNum, 0)
assert.Equal(t, c.Nodes[0].ReplicaCount, 0)
Expand Down

0 comments on commit 47a3fef

Please sign in to comment.