github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/pkg/util/fileutil.go (about) 1 // Copyright 2020 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package util 15 16 import ( 17 "fmt" 18 "io/ioutil" 19 "os" 20 "path/filepath" 21 "syscall" 22 23 "github.com/pingcap/errors" 24 "github.com/pingcap/failpoint" 25 "github.com/pingcap/log" 26 "github.com/pingcap/ticdc/pkg/config" 27 cerror "github.com/pingcap/ticdc/pkg/errors" 28 ) 29 30 const ( 31 gb = 1024 * 1024 * 1024 32 dataDirAvailLowThreshold = 10 // percentage 33 ) 34 35 // IsDirAndWritable checks a given path is directory and writable 36 func IsDirAndWritable(path string) error { 37 st, err := os.Stat(path) 38 if err != nil { 39 return cerror.WrapError(cerror.ErrCheckDirWritable, err) 40 } 41 if !st.IsDir() { 42 return cerror.WrapError(cerror.ErrCheckDirWritable, errors.Errorf("%s is not a directory", path)) 43 } 44 return IsDirWritable(path) 45 } 46 47 // IsDirWritable checks if a dir is writable, return error nil means it is writable 48 func IsDirWritable(dir string) error { 49 f := filepath.Join(dir, ".writable.test") 50 if err := ioutil.WriteFile(f, []byte(""), 0o600); err != nil { 51 return cerror.WrapError(cerror.ErrCheckDirWritable, err) 52 } 53 return cerror.WrapError(cerror.ErrCheckDirWritable, os.Remove(f)) 54 } 55 56 // IsDirReadWritable check if the dir is writable and readable by cdc server 57 func IsDirReadWritable(dir string) error { 58 f := filepath.Join(dir, "file.test") 59 if err := ioutil.WriteFile(f, []byte(""), 0o600); err != nil { 60 return cerror.WrapError(cerror.ErrCheckDirValid, err) 61 } 62 63 if _, err := ioutil.ReadFile(f); err != nil { 64 return cerror.WrapError(cerror.ErrCheckDirValid, err) 65 } 66 67 return cerror.WrapError(cerror.ErrCheckDirValid, os.Remove(f)) 68 } 69 70 // DiskInfo present the disk amount information, in gb 71 type DiskInfo struct { 72 All uint64 73 Used uint64 74 Free uint64 75 Avail uint64 76 AvailPercentage float32 77 } 78 79 func (d *DiskInfo) String() string { 80 return fmt.Sprintf("{All: %+vGB; Used: %+vGB; Free: %+vGB; Available: %+vGB; Available Percentage: %+v%%}", 81 d.All, d.Used, d.Free, d.Avail, d.AvailPercentage) 82 } 83 84 // GetDiskInfo return the disk space information of the given directory 85 // the caller should guarantee that dir exist 86 func GetDiskInfo(dir string) (*DiskInfo, error) { 87 f := filepath.Join(dir, "file.test") 88 if err := ioutil.WriteFile(f, []byte(""), 0o600); err != nil { 89 return nil, cerror.WrapError(cerror.ErrGetDiskInfo, err) 90 } 91 92 fs := syscall.Statfs_t{} 93 if err := syscall.Statfs(dir, &fs); err != nil { 94 return nil, cerror.WrapError(cerror.ErrGetDiskInfo, err) 95 } 96 97 info := &DiskInfo{ 98 All: fs.Blocks * uint64(fs.Bsize) / gb, 99 Avail: fs.Bavail * uint64(fs.Bsize) / gb, 100 Free: fs.Bfree * uint64(fs.Bsize) / gb, 101 } 102 info.Used = info.All - info.Free 103 info.AvailPercentage = float32(info.Avail) / float32(info.All) * 100 104 105 if err := os.Remove(f); err != nil { 106 if !os.IsNotExist(err) { 107 return info, cerror.WrapError(cerror.ErrGetDiskInfo, err) 108 } 109 } 110 111 return info, nil 112 } 113 114 // CheckDataDirSatisfied check if the data-dir meet the requirement during server running 115 // the caller should guarantee that dir exist 116 func CheckDataDirSatisfied() error { 117 conf := config.GetGlobalServerConfig() 118 diskInfo, err := GetDiskInfo(conf.DataDir) 119 if err != nil { 120 return cerror.WrapError(cerror.ErrCheckDataDirSatisfied, err) 121 } 122 if diskInfo.AvailPercentage < dataDirAvailLowThreshold { 123 failpoint.Inject("InjectCheckDataDirSatisfied", func() { 124 log.Info("inject check data dir satisfied error") 125 failpoint.Return(nil) 126 }) 127 return cerror.WrapError(cerror.ErrCheckDataDirSatisfied, errors.Errorf("disk is almost full, TiCDC require that the disk mount data-dir "+ 128 "have 10%% available space, and the total amount has at least 500GB is preferred. disk info: %+v", diskInfo)) 129 } 130 131 return nil 132 }