cubefs

Форк
0
/
unit.go 
242 строки · 5.2 Кб
1
// Copyright 2018 The CubeFS Authors.
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
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12
// implied. See the License for the specific language governing
13
// permissions and limitations under the License.
14

15
package util
16

17
import (
18
	"crypto/md5"
19
	"encoding/hex"
20
	"fmt"
21
	"net"
22
	"regexp"
23
	"strings"
24

25
	"github.com/cubefs/cubefs/depends/tiglabs/raft/util"
26
	"github.com/cubefs/cubefs/util/log"
27
)
28

29
const (
30
	_  = iota
31
	KB = 1 << (10 * iota)
32
	MB
33
	GB
34
	TB
35
	PB
36
	DefaultDataPartitionSize = 120 * GB
37
	TaskWorkerInterval       = 1
38
)
39

40
const (
41
	BlockCount          = 1024
42
	BlockSize           = 65536 * 2
43
	ReadBlockSize       = BlockSize
44
	PerBlockCrcSize     = 4
45
	ExtentSize          = BlockCount * BlockSize
46
	PacketHeaderSize    = 57
47
	BlockHeaderSize     = 4096
48
	SyscallTryMaxTimes  = 3
49
	PacketHeaderVerSize = 65
50
)
51

52
const (
53
	PageSize          = 4 * util.KB
54
	FallocFLKeepSize  = 1
55
	FallocFLPunchHole = 2
56
)
57

58
const (
59
	AclListIP  = 0
60
	AclAddIP   = 1
61
	AclDelIP   = 2
62
	AclCheckIP = 3
63
)
64

65
const (
66
	UidLimitList = 0
67
	UidAddLimit  = 1
68
	UidDel       = 2
69
	UidGetLimit  = 3
70
)
71

72
const (
73
	DefaultTinySizeLimit = 1 * MB // TODO explain tiny extent?
74
)
75

76
type MultiVersionSeq uint64
77

78
func Min(a, b int) int {
79
	if a > b {
80
		return b
81
	}
82
	return a
83
}
84

85
func Max(a, b int) int {
86
	if a > b {
87
		return a
88
	}
89
	return b
90
}
91

92
// IsIPV4 returns if it is IPV4 address.
93
func IsIPV4(val interface{}) bool {
94
	ip4Pattern := `((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)`
95
	ip4 := regexpCompile(ip4Pattern)
96
	return isMatch(ip4, val)
97
}
98

99
func GetIp(addr string) (ip string) {
100
	var arr []string
101
	if arr = strings.Split(addr, ":"); len(arr) < 2 {
102
		return
103
	}
104
	ip = strings.Trim(arr[0], " ")
105
	return ip
106
}
107

108
func getIpAndPort(ipAddr string) (ip string, port string, success bool) {
109
	success = false
110
	arr := strings.Split(ipAddr, ":")
111
	if len(arr) != 2 {
112
		log.LogWarnf("action[GetIpAndPort] ipAddr[%v] invalid", ipAddr)
113
		return
114
	}
115
	ip = strings.Trim(arr[0], " ")
116
	port = strings.Trim(arr[1], " ")
117
	success = true
118
	return
119
}
120

121
func getDomainAndPort(domainAddr string) (domain string, port string, success bool) {
122
	success = false
123
	arr := strings.Split(domainAddr, ":")
124
	if len(arr) != 2 {
125
		log.LogWarnf("action[GetDomainAndPort] domainAddr[%v] invalid", domainAddr)
126
		return
127
	}
128
	domain = strings.Trim(arr[0], " ")
129
	port = strings.Trim(arr[1], " ")
130
	success = true
131
	return
132
}
133

134
func IsIPV4Addr(ipAddr string) bool {
135
	ip, _, ok := getIpAndPort(ipAddr)
136
	if !ok {
137
		return false
138
	}
139
	return IsIPV4(ip)
140
}
141

142
func ParseIpAddrToDomainAddr(ipAddr string) (domainAddr string) {
143
	ip, port, ok := getIpAndPort(ipAddr)
144
	if !ok {
145
		return
146
	}
147
	domains, err := net.LookupAddr(ip)
148
	if err != nil {
149
		log.LogWarnf("action[ParseIpAddrToDomainAddr] failed, ipAddr[%v], ip[%v], err[%v]", ipAddr, ip, err)
150
		return
151
	}
152
	for _, v := range domains {
153
		domain := v
154
		if domain[len(domain)-1] == '.' {
155
			domain = domain[0 : len(domain)-1]
156
		}
157
		if len(domainAddr) != 0 {
158
			domainAddr += ","
159
		}
160
		domainAddr += fmt.Sprintf("%s:%v", domain, port)
161
	}
162
	return
163
}
164

165
func ParseAddrToIpAddr(addr string) (ipAddr string, success bool) {
166
	success = true
167
	if IsIPV4Addr(addr) {
168
		ipAddr = addr
169
		return
170
	}
171
	if parsedAddr, ok := ParseDomainAddrToIpAddr(addr); ok {
172
		ipAddr = parsedAddr
173
		return
174
	}
175
	success = false
176
	return
177
}
178

179
func ParseDomainAddrToIpAddr(domainAddr string) (ipAddr string, success bool) {
180
	success = false
181
	domain, port, ok := getDomainAndPort(domainAddr)
182
	if !ok {
183
		return
184
	}
185
	ips, err := net.LookupHost(domain)
186
	if err != nil {
187
		log.LogWarnf("action[ParseDomainAddrToIpAddr] failed, domainAddr[%v], domain[%v], err[%v]",
188
			domainAddr, domain, err)
189
		return
190
	}
191
	if len(ips) == 0 {
192
		log.LogWarnf("action[ParseDomainAddrToIpAddr] ips is null, domainAddr[%v], domain[%v]",
193
			domainAddr, domain)
194
		return
195
	}
196
	for i := 0; i < len(ips); i += 1 {
197
		if ips[i] != ips[0] {
198
			log.LogWarnf("action[ParseDomainAddrToIpAddr] the number of ips is not one,"+
199
				"domainAddr[%v], domain[%v], ips[%v], err[%v]", domainAddr, domain, ips, err)
200
			return
201
		}
202
	}
203
	ipAddr = fmt.Sprintf("%s:%v", ips[0], port)
204
	success = true
205
	return
206
}
207

208
func regexpCompile(str string) *regexp.Regexp {
209
	return regexp.MustCompile("^" + str + "$")
210
}
211

212
func isMatch(exp *regexp.Regexp, val interface{}) bool {
213
	switch v := val.(type) {
214
	case []rune:
215
		return exp.MatchString(string(v))
216
	case []byte:
217
		return exp.Match(v)
218
	case string:
219
		return exp.MatchString(v)
220
	default:
221
		return false
222
	}
223
}
224

225
func GenerateKey(volName string, ino uint64, offset uint64) string {
226
	return fmt.Sprintf("%v_%v_%016x", volName, ino, offset)
227
}
228

229
func GenerateRepVolKey(volName string, ino uint64, dpId uint64, extentId uint64, offset uint64) string {
230
	return fmt.Sprintf("%v_%v_%v_%v_%016x", volName, ino, dpId, extentId, offset)
231
}
232

233
func OneDaySec() int64 {
234
	return 60 * 60 * 24
235
}
236

237
func CalcAuthKey(key string) (authKey string) {
238
	h := md5.New()
239
	_, _ = h.Write([]byte(key))
240
	cipherStr := h.Sum(nil)
241
	return strings.ToLower(hex.EncodeToString(cipherStr))
242
}
243

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.