Skip to content

Commit 9efe8aa

Browse files
ti-chi-botrleungx
andauthored
mcs: fix primary parse (#9578) (#10003)
close #9579 Signed-off-by: Ryan Leung <rleungx@gmail.com> Co-authored-by: Ryan Leung <rleungx@gmail.com>
1 parent afc796b commit 9efe8aa

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

pkg/keyspace/tso_keyspace_group.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,13 @@ func (m *GroupManager) GetKeyspaceGroupPrimaryByID(id uint32) (string, error) {
11621162
return "", ErrKeyspaceGroupPrimaryNotFound
11631163
}
11641164
// The format of leader name is address-groupID.
1165-
contents := strings.Split(leader.GetName(), "-")
1166-
return contents[0], err
1165+
return parsePrimaryName(leader.Name), err
1166+
}
1167+
1168+
func parsePrimaryName(name string) string {
1169+
idx := strings.LastIndex(name, "-")
1170+
if idx != -1 {
1171+
return name[:idx]
1172+
}
1173+
return name
11671174
}

pkg/keyspace/tso_keyspace_group_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,3 +562,24 @@ func TestBuildSplitKeyspaces(t *testing.T) {
562562
}
563563
}
564564
}
565+
566+
func TestParsePrimaryName(t *testing.T) {
567+
re := require.New(t)
568+
testCases := []struct {
569+
name string
570+
expected string
571+
}{
572+
{"127.0.0.1:2379-00000", "127.0.0.1:2379"},
573+
{"http://127.0.0.1:2379-10000", "http://127.0.0.1:2379"},
574+
{"https://127.0.0.1:2379-00001", "https://127.0.0.1:2379"},
575+
{"http://[::1]:2379-00002", "http://[::1]:2379"},
576+
{"https://[::1]:2379-00003", "https://[::1]:2379"},
577+
{"https://a-b-c-d-e-f-g:2379-00004", "https://a-b-c-d-e-f-g:2379"},
578+
{"https://pd-tso-server-0.tso-service.tidb-serverless.svc:2379-00002", "https://pd-tso-server-0.tso-service.tidb-serverless.svc:2379"},
579+
{"http://pd-tso-server-0.tso-service.tidb-serverless.svc:2379-00002", "http://pd-tso-server-0.tso-service.tidb-serverless.svc:2379"},
580+
{"pd-tso-server-0.tso-service.tidb-serverless.svc:2379-00000", "pd-tso-server-0.tso-service.tidb-serverless.svc:2379"},
581+
}
582+
for _, tc := range testCases {
583+
re.Equal(tc.expected, parsePrimaryName(tc.name))
584+
}
585+
}

0 commit comments

Comments
 (0)