Skip to content

Commit

Permalink
feat: add rule transition with case insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
earayu committed Dec 28, 2023
1 parent 464a913 commit 45a88ac
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
14 changes: 9 additions & 5 deletions go/vt/vttablet/tabletserver/role/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"os"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -50,16 +51,19 @@ const (
)

func transitionRoleType(role string) topodatapb.TabletType {
// Convert the role to lower case for case-insensitive comparison
role = strings.ToLower(role)

switch role {
case LEADER, PRIMARY, MASTER:
case strings.ToLower(LEADER), strings.ToLower(PRIMARY), strings.ToLower(MASTER):
return topodatapb.TabletType_PRIMARY
case FOLLOWER, SECONDARY, SLAVE:
case strings.ToLower(FOLLOWER), strings.ToLower(SECONDARY), strings.ToLower(SLAVE):
return topodatapb.TabletType_REPLICA
case CANDIDATE:
case strings.ToLower(CANDIDATE):
return topodatapb.TabletType_REPLICA
case LEARNER:
case strings.ToLower(LEARNER):
return topodatapb.TabletType_RDONLY
case LOGGER:
case strings.ToLower(LOGGER):
return topodatapb.TabletType_SPARE
default:
return topodatapb.TabletType_UNKNOWN
Expand Down
56 changes: 56 additions & 0 deletions go/vt/vttablet/tabletserver/role/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package role
import (
"os"
"testing"
"vitess.io/vitess/go/vt/proto/topodata"
)

func Test_setUpMysqlProbeServicePort(t *testing.T) {
Expand Down Expand Up @@ -54,3 +55,58 @@ func Test_setUpMysqlProbeServicePort(t *testing.T) {
})
}
}

func Test_transitionRoleType(t *testing.T) {
tests := []struct {
role string
want topodata.TabletType
}{
{
role: "primary",
want: topodata.TabletType_PRIMARY,
},
{
role: "PriMARY",
want: topodata.TabletType_PRIMARY,
},
{
role: "secondary",
want: topodata.TabletType_REPLICA,
},
{
role: "Secondary",
want: topodata.TabletType_REPLICA,
},
{
role: "SecondarY",
want: topodata.TabletType_REPLICA,
},
{
role: "follower",
want: topodata.TabletType_REPLICA,
},
{
role: "slave",
want: topodata.TabletType_REPLICA,
},
{
role: "Slave",
want: topodata.TabletType_REPLICA,
},
{
role: "SLAVE",
want: topodata.TabletType_REPLICA,
},
{
role: "SLAVE2",
want: topodata.TabletType_UNKNOWN,
},
}
for _, tt := range tests {
t.Run(tt.role, func(t *testing.T) {
if got := transitionRoleType(tt.role); got != tt.want {
t.Errorf("transitionRoleType() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 45a88ac

Please sign in to comment.