-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathutil.go
161 lines (136 loc) · 3.81 KB
/
util.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package util
import (
"fmt"
"net"
"strings"
"time"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/logutil"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
)
// DefaultIP get a default non local ip, err is not nil, ip return 127.0.0.1
func DefaultIP() (ip string, err error) {
ip = "127.0.0.1"
ifaces, err := net.Interfaces()
if err != nil {
return
}
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
continue
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip.IsUnspecified() || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue
}
return ip.String(), nil
}
}
err = errors.New("no ip found")
return
}
// DefaultListenAddr returns default listen address with appointed port.
func DefaultListenAddr(port int32) string {
defaultIP, err := DefaultIP()
if err != nil {
log.Infof("get default ip err: %v, use: %s", err, defaultIP)
}
return fmt.Sprintf("%s:%d", defaultIP, port)
}
// IsValidateListenHost judge the host is validate listen host or not.
func IsValidateListenHost(host string) bool {
if host == "127.0.0.1" || host == "localhost" || host == "0.0.0.0" {
return false
}
return true
}
// NewStdLogger return an instance of samara.StdLogger
func NewStdLogger(prefix string) *log.Entry {
return log.StandardLogger().WithField("logger", prefix)
}
// InitLogger configures the standard logger, using the same strategy as TiDB's logger
func InitLogger(level string, logFile string, rotateBy string) {
logutil.InitLogger(&logutil.LogConfig{
Level: level,
File: logutil.FileLogConfig{
Filename: logFile,
LogRotate: true,
},
})
// see https://github.com/natefinch/lumberjack/issues/17#issuecomment-185846531 for how to do time-based rotation
if rotateLog, ok := log.StandardLogger().Out.(*lumberjack.Logger); ok {
var interval time.Duration
if rotateBy == "hour" {
interval = time.Hour
} else {
interval = 24 * time.Hour
}
go func() {
for range time.Tick(interval) {
rotateLog.Rotate()
}
}()
}
}
// ToColumnTypeMap return a map index by column id
func ToColumnTypeMap(columns []*model.ColumnInfo) map[int64]*types.FieldType {
colTypeMap := make(map[int64]*types.FieldType)
for _, col := range columns {
colTypeMap[col.ID] = &col.FieldType
}
return colTypeMap
}
// RetryOnError defines a action with retry when fn returns error
func RetryOnError(retryCount int, sleepTime time.Duration, errStr string, fn func() error) error {
var err error
for i := 0; i < retryCount; i++ {
err = fn()
if err == nil {
break
}
log.Errorf("%s: %v", errStr, err)
time.Sleep(sleepTime)
}
return errors.Trace(err)
}
// QueryLatestTsFromPD returns the latest ts
func QueryLatestTsFromPD(tiStore kv.Storage) (int64, error) {
version, err := tiStore.CurrentVersion()
if err != nil {
log.Errorf("get current version error: %v", err)
return 0, errors.Trace(err)
}
return int64(version.Ver), nil
}
// AlreadyExistsf represents an error with an already exists message.
func AlreadyExistsf(format string, args ...interface{}) error {
return errors.Errorf(format+" already exists", args...)
}
// NewNotFound wraps an error with a not found message.
func NewNotFound(err error, msg string) error {
return errors.Annotate(err, msg+" not found")
}
// IsNotFound checks if an error is created via errors.NotFoundf.
func IsNotFound(err error) bool {
return err != nil && strings.Contains(err.Error(), " not found")
}
// IsAlreadyExists checks if an error is created via errors.AlreadyExistsf.
func IsAlreadyExists(err error) bool {
return err != nil && strings.Contains(err.Error(), " already exists")
}