Skip to content

Commit

Permalink
cluster/spec: split dir if ',' seped
Browse files Browse the repository at this point in the history
  • Loading branch information
jsvisa committed Oct 29, 2020
1 parent 38134af commit be3084b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
24 changes: 15 additions & 9 deletions pkg/cluster/spec/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,14 @@ func (s *Specification) CountDir(targetHost, dirPrefix string) int {
topoSpec := reflect.ValueOf(s).Elem()
dirPrefix = Abs(s.GlobalOptions.User, dirPrefix)

addHostDir := func(host, deployDir, dir string) {
if !strings.HasPrefix(dir, "/") {
dir = filepath.Join(deployDir, dir)
}
dir = Abs(s.GlobalOptions.User, dir)
dirStats[host+dir]++
}

for i := 0; i < topoSpec.NumField(); i++ {
if isSkipField(topoSpec.Field(i)) {
continue
Expand All @@ -675,6 +683,7 @@ func (s *Specification) CountDir(targetHost, dirPrefix string) int {
compSpecs := topoSpec.Field(i)
for index := 0; index < compSpecs.Len(); index++ {
compSpec := compSpecs.Index(index)
deployDir := compSpec.FieldByName("DeployDir").String()
// Directory conflicts
for _, dirType := range dirTypes {
if j, found := findField(compSpec, dirType); found {
Expand All @@ -683,13 +692,14 @@ func (s *Specification) CountDir(targetHost, dirPrefix string) int {

switch dirType { // the same as in instance.go for (*instance)
case "DataDir":
deployDir := compSpec.FieldByName("DeployDir").String()
// the default data_dir is relative to deploy_dir
if dir != "" && !strings.HasPrefix(dir, "/") {
dir = filepath.Join(deployDir, dir)
if dir != "" {
dataDirs := strings.Split(dir, ",")
for _, dataDir := range dataDirs {
addHostDir(host, deployDir, dataDir)
}
}
case "LogDir":
deployDir := compSpec.FieldByName("DeployDir").String()
field := compSpec.FieldByName("LogDir")
if field.IsValid() {
dir = field.Interface().(string)
Expand All @@ -698,12 +708,8 @@ func (s *Specification) CountDir(targetHost, dirPrefix string) int {
if dir == "" {
dir = "log"
}
if !strings.HasPrefix(dir, "/") {
dir = filepath.Join(deployDir, dir)
}
addHostDir(host, deployDir, dir)
}
dir = Abs(s.GlobalOptions.User, dir)
dirStats[host+dir]++
}
}
}
Expand Down
48 changes: 47 additions & 1 deletion pkg/cluster/spec/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ global:
user: "test1"
ssh_port: 220
deploy_dir: "test-deploy"
data_dir: "test-data"
data_dir: "test-data"
tidb_servers:
- host: 172.16.5.138
port: 1234
Expand Down Expand Up @@ -700,3 +700,49 @@ tikv_servers:
err = CheckTiKVLocationLabels([]string{"zone", "host"}, &topo)
c.Assert(err, IsNil)
}

func (s *metaSuiteTopo) TestCountDirMultiPath(c *C) {
topo := Specification{}

err := yaml.Unmarshal([]byte(`
global:
user: "test1"
ssh_port: 220
deploy_dir: "test-deploy"
tiflash_servers:
- host: 172.19.0.104
data_dir: "/home/tidb/birdstorm/data1,/home/tidb/birdstorm/data3"
`), &topo)
c.Assert(err, IsNil)
cnt := topo.CountDir("172.19.0.104", "/home/tidb/birdstorm/data1")
c.Assert(cnt, Equals, 1)
cnt = topo.CountDir("172.19.0.104", "/home/tidb/birdstorm/data2")
c.Assert(cnt, Equals, 0)
cnt = topo.CountDir("172.19.0.104", "/home/tidb/birdstorm/data3")
c.Assert(cnt, Equals, 1)
cnt = topo.CountDir("172.19.0.104", "/home/tidb/birdstorm")
c.Assert(cnt, Equals, 2)

err = yaml.Unmarshal([]byte(`
global:
user: "test1"
ssh_port: 220
deploy_dir: "test-deploy"
tiflash_servers:
- host: 172.19.0.104
data_dir: "birdstorm/data1,/birdstorm/data3"
`), &topo)
c.Assert(err, IsNil)
cnt = topo.CountDir("172.19.0.104", "/home/test1/test-deploy/birdstorm/data1")
c.Assert(cnt, Equals, 1)
cnt = topo.CountDir("172.19.0.104", "/birdstorm/data3")
c.Assert(cnt, Equals, 1)
cnt = topo.CountDir("172.19.0.104", "/home/tidb/birdstorm/data3")
c.Assert(cnt, Equals, 0)
cnt = topo.CountDir("172.19.0.104", "/home/test1/test-deploy/birdstorm/data3")
c.Assert(cnt, Equals, 0)
cnt = topo.CountDir("172.19.0.104", "/home/tidb/birdstorm")
c.Assert(cnt, Equals, 0)
cnt = topo.CountDir("172.19.0.104", "/birdstorm")
c.Assert(cnt, Equals, 1)
}

0 comments on commit be3084b

Please sign in to comment.