Skip to content

Commit

Permalink
TiKV: support clearing extra data dirs automatically when destroying …
Browse files Browse the repository at this point in the history
…cluster.

Signed-off-by: lucasliang <[email protected]>
  • Loading branch information
LykxSassinator committed Dec 2, 2024
1 parent f7d5cb8 commit 7cc079f
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
47 changes: 47 additions & 0 deletions pkg/cluster/spec/parse_topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"fmt"
"os"
"path/filepath"
"sort"
"testing"

"github.com/pingcap/check"
Expand Down Expand Up @@ -277,6 +278,52 @@ tikv_servers:
})
}

func (s *topoSuite) TestTiKVServerConfigs(c *check.C) {
// test tikv server config section
withTempFile(`
global:
user: tidb
deploy_dir: my-global-deploy
data_dir: my-global-data
log_dir: my-global-log
server_configs:
tikv:
storage.reserve-space: 1K
storage.data-dir: /data1/tikv
raft-engine.dir: /data1/data/raft
raft-engine.spill-dir: /data1/data/spill
rocksdb.wal-dir: /data2/data/wal
rocksdb.titan.dirname: /data2/data/titan
tikv_servers:
- host: 172.16.5.140
port: 20161
status_port: 20181
`, func(file string) {
topo := Specification{}
err := ParseTopologyYaml(file, &topo)
c.Assert(err, check.IsNil)
ExpandRelativeDir(&topo)

c.Assert(topo.TiKVServers[0].DeployDir, check.Equals, "/home/tidb/my-global-deploy/tikv-20161")
c.Assert(topo.TiKVServers[0].DataDir, check.Equals, "/home/tidb/my-global-deploy/tikv-20161/my-global-data")
c.Assert(topo.TiKVServers[0].LogDir, check.Equals, "/home/tidb/my-global-deploy/tikv-20161/my-global-log")

c.Assert(topo.ServerConfigs.TiKV["storage.data-dir"], check.Equals, "/data1/tikv")
c.Assert(topo.ServerConfigs.TiKV["raft-engine.dir"], check.Equals, "/data1/data/raft")
c.Assert(topo.ServerConfigs.TiKV["raft-engine.spill-dir"], check.Equals, "/data1/data/spill")
c.Assert(topo.ServerConfigs.TiKV["rocksdb.wal-dir"], check.Equals, "/data2/data/wal")
c.Assert(topo.ServerConfigs.TiKV["rocksdb.titan.dirname"], check.Equals, "/data2/data/titan")

extraDirs := topo.TiKVServers[0].getExtraDeployDirs(topo.ServerConfigs.TiKV)
sort.Strings(extraDirs)
targetDirs := []string{"/data1/tikv", "/data1/data/raft", "/data1/data/spill", "/data2/data/wal", "/data2/data/titan"}
sort.Strings(targetDirs)
c.Assert(extraDirs, check.DeepEquals, targetDirs)
})
}

func (s *topoSuite) TestTiFlashStorage(c *check.C) {
// test tiflash storage section, 'storage.main.dir' should not be defined in server_configs
withTempFile(`
Expand Down
49 changes: 45 additions & 4 deletions pkg/cluster/spec/tikv.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,48 @@ func (s *TiKVSpec) Labels() (map[string]string, error) {
return lbs, nil
}

// getExtraDeployDirs returns the extra directories that needs to be checked.
func (s *TiKVSpec) getExtraDeployDirs(globalConf map[string]any) []string {
var extractDir = func(dir any, rootPath string) string {
realDir := strings.TrimSpace(dir.(string))
// Skip the relative path under the root path.
if realDir == "" || (strings.HasPrefix(realDir, "./") || strings.HasPrefix(realDir, rootPath)) {
return ""
}
elemStr := strings.TrimSuffix(realDir, "/")
return elemStr
}

extraDirs := []string{}
rootPath := s.DataDir
candidates := []string{
"storage.data-dir",
"rocksdb.wal-dir",
"rocksdb.titan.dirname",
"raft-engine.dir",
"raft-engine.spill-dir",
}
for _, candidate := range candidates {
// Check the global configuration first.
globalConfDir, ok := globalConf[candidate]
if ok {
elemStr := extractDir(globalConfDir, rootPath)
if elemStr != "" {
extraDirs = append(extraDirs, elemStr)
}
}
// Check the instance configuration.
dir, ok := s.Config[candidate]
if ok {
elemStr := extractDir(dir, rootPath)
if elemStr != "" {
extraDirs = append(extraDirs, elemStr)
}
}
}
return extraDirs
}

// TiKVComponent represents TiKV component.
type TiKVComponent struct{ Topology *Specification }

Expand Down Expand Up @@ -205,6 +247,8 @@ func (c *TiKVComponent) Instances() []Instance {
ins := make([]Instance, 0, len(c.Topology.TiKVServers))
for _, s := range c.Topology.TiKVServers {
s := s
dirs := []string{s.DeployDir, s.DataDir}
dirs = append(dirs, s.getExtraDeployDirs(c.Topology.ServerConfigs.TiKV)...)
ins = append(ins, &TiKVInstance{BaseInstance{
InstanceSpec: s,
Name: c.Name(),
Expand All @@ -221,10 +265,7 @@ func (c *TiKVComponent) Instances() []Instance {
s.Port,
s.StatusPort,
},
Dirs: []string{
s.DeployDir,
s.DataDir,
},
Dirs: dirs,
StatusFn: s.Status,
UptimeFn: func(_ context.Context, timeout time.Duration, tlsCfg *tls.Config) time.Duration {
return UptimeByHost(s.GetManageHost(), s.StatusPort, timeout, tlsCfg)
Expand Down

0 comments on commit 7cc079f

Please sign in to comment.