/
tempDir.go
127 lines (115 loc) · 3.31 KB
/
tempDir.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package disk
import (
"os"
"path/filepath"
"github.com/danjacques/gofslock/fslock"
"github.com/pingcap/errors"
"github.com/pingcap/log"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/parser/terror"
"go.uber.org/zap"
"golang.org/x/sync/singleflight"
)
var (
tempDirLock fslock.Handle
sf singleflight.Group
)
const (
lockFile = "_dir.lock"
recordDir = "record"
)
// CheckAndInitTempDir check whether the temp directory is existed.
// If not, initializes the temp directory.
func CheckAndInitTempDir() (err error) {
_, err, _ = sf.Do("tempDir", func() (value interface{}, err error) {
if !checkTempDirExist() {
log.Info("Tmp-storage-path not found. Try to initialize TempDir.")
err = InitializeTempDir()
}
return
})
return
}
func checkTempDirExist() bool {
tempDir := config.GetGlobalConfig().TempStoragePath
_, err := os.Stat(tempDir)
if err != nil && !os.IsExist(err) {
return false
}
return true
}
// InitializeTempDir initializes the temp directory.
func InitializeTempDir() error {
tempDir := config.GetGlobalConfig().TempStoragePath
_, err := os.Stat(tempDir)
if err != nil && !os.IsExist(err) {
err = os.MkdirAll(tempDir, 0750)
if err != nil {
return err
}
}
tempDirLock, err = fslock.Lock(filepath.Join(tempDir, lockFile))
if err != nil {
switch err {
case fslock.ErrLockHeld:
log.Error("The current temporary storage dir has been occupied by another instance, "+
"check tmp-storage-path config and make sure they are different.", zap.String("TempStoragePath", tempDir), zap.Error(err))
default:
log.Error("Failed to acquire exclusive lock on the temporary storage dir.", zap.String("TempStoragePath", tempDir), zap.Error(err))
}
return err
}
subDirs, err := os.ReadDir(tempDir)
if err != nil {
return err
}
// If it exists others files except lock file, creates another goroutine to clean them.
if len(subDirs) > 2 {
go func() {
for _, subDir := range subDirs {
// Do not remove the lock file.
switch subDir.Name() {
case lockFile, recordDir:
continue
}
err := os.RemoveAll(filepath.Join(tempDir, subDir.Name()))
if err != nil {
log.Warn("Remove temporary file error",
zap.String("tempStorageSubDir", filepath.Join(tempDir, subDir.Name())), zap.Error(err))
}
}
}()
}
return nil
}
// CleanUp releases the directory lock when exiting TiDB.
func CleanUp() {
if tempDirLock != nil {
err := tempDirLock.Unlock()
terror.Log(errors.Trace(err))
}
}
// CheckAndCreateDir check whether the directory is existed. If not, then create it.
func CheckAndCreateDir(path string) error {
_, err := os.Stat(path)
if err != nil && !os.IsExist(err) {
err = os.MkdirAll(path, 0750)
if err != nil {
return err
}
}
return nil
}