forked from cubefs/cubefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
717 lines (642 loc) · 19.8 KB
/
server.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
// Copyright 2018 The CubeFS Authors.
//
// 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 datanode
import (
"fmt"
"net"
"net/http"
"regexp"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"errors"
"os"
"syscall"
"github.com/cubefs/cubefs/cmd/common"
"github.com/cubefs/cubefs/proto"
"github.com/cubefs/cubefs/raftstore"
"github.com/cubefs/cubefs/repl"
masterSDK "github.com/cubefs/cubefs/sdk/master"
"github.com/cubefs/cubefs/util"
"github.com/cubefs/cubefs/util/config"
"github.com/cubefs/cubefs/util/exporter"
"github.com/cubefs/cubefs/util/log"
"github.com/xtaci/smux"
)
var (
ErrIncorrectStoreType = errors.New("Incorrect store type")
ErrNoSpaceToCreatePartition = errors.New("No disk space to create a data partition")
ErrNewSpaceManagerFailed = errors.New("Creater new space manager failed")
ErrGetMasterDatanodeInfoFailed = errors.New("Failed to get datanode info from master")
LocalIP, serverPort string
gConnPool = util.NewConnectPool()
MasterClient = masterSDK.NewMasterClient(nil, false)
)
const (
DefaultZoneName = proto.DefaultZoneName
DefaultRaftDir = "raft"
DefaultRaftLogsToRetain = 10 // Count of raft logs per data partition
DefaultDiskMaxErr = 1
DefaultDiskRetainMin = 5 * util.GB // GB
)
const (
ModuleName = "dataNode"
)
const (
ConfigKeyLocalIP = "localIP" // string
ConfigKeyPort = "port" // int
ConfigKeyMasterAddr = "masterAddr" // array
ConfigKeyZone = "zoneName" // string
ConfigKeyDisks = "disks" // array
ConfigKeyRaftDir = "raftDir" // string
ConfigKeyRaftHeartbeat = "raftHeartbeat" // string
ConfigKeyRaftReplica = "raftReplica" // string
CfgTickInterval = "tickInterval" // int
CfgRaftRecvBufSize = "raftRecvBufSize" // int
/*
* Metrics Degrade Level
* minus value: turn off metrics collection.
* 0 or 1: full metrics.
* 2: 1/2 of the metrics will be collected.
* 3: 1/3 of the metrics will be collected.
* ...
*/
CfgMetricsDegrade = "metricsDegrade" // int
CfgDiskRdonlySpace = "diskRdonlySpace" // int
// smux Config
ConfigKeyEnableSmuxClient = "enableSmuxConnPool" //bool
ConfigKeySmuxPortShift = "smuxPortShift" //int
ConfigKeySmuxMaxConn = "smuxMaxConn" //int
ConfigKeySmuxStreamPerConn = "smuxStreamPerConn" //int
ConfigKeySmuxMaxBuffer = "smuxMaxBuffer" //int
//rate limit control enable
ConfigDiskQosEnable = "diskQosEnable" //bool
)
// DataNode defines the structure of a data node.
type DataNode struct {
space *SpaceManager
port string
zoneName string
clusterID string
localIP string
localServerAddr string
nodeID uint64
raftDir string
raftHeartbeat string
raftReplica string
raftStore raftstore.RaftStore
tickInterval int
raftRecvBufSize int
startTime int64
tcpListener net.Listener
stopC chan bool
smuxPortShift int
enableSmuxConnPool bool
smuxConnPool *util.SmuxConnectPool
smuxListener net.Listener
smuxServerConfig *smux.Config
smuxConnPoolConfig *util.SmuxConnPoolConfig
getRepairConnFunc func(target string) (net.Conn, error)
putRepairConnFunc func(conn net.Conn, forceClose bool)
metrics *DataNodeMetrics
metricsDegrade int64
metricsCnt uint64
control common.Control
diskQosEnable bool
diskQosEnableFromMaster bool
diskIopsReadLimit uint64
diskIopsWriteLimit uint64
diskFlowReadLimit uint64
diskFlowWriteLimit uint64
}
func NewServer() *DataNode {
return &DataNode{}
}
func (s *DataNode) Start(cfg *config.Config) (err error) {
runtime.GOMAXPROCS(runtime.NumCPU())
return s.control.Start(s, cfg, doStart)
}
// Shutdown shuts down the current data node.
func (s *DataNode) Shutdown() {
s.control.Shutdown(s, doShutdown)
}
// Sync keeps data node in sync.
func (s *DataNode) Sync() {
s.control.Sync()
}
// Workflow of starting up a data node.
func doStart(server common.Server, cfg *config.Config) (err error) {
s, ok := server.(*DataNode)
if !ok {
return errors.New("Invalid node Type!")
}
s.stopC = make(chan bool, 0)
// parse the config file
if err = s.parseConfig(cfg); err != nil {
return
}
exporter.Init(ModuleName, cfg)
s.registerMetrics()
s.register(cfg)
//parse the smux config
if err = s.parseSmuxConfig(cfg); err != nil {
return
}
//connection pool must be created before initSpaceManager
s.initConnPool()
// init limit
initRepairLimit()
// start the raft server
if err = s.startRaftServer(cfg); err != nil {
return
}
// create space manager (disk, partition, etc.)
if err = s.startSpaceManager(cfg); err != nil {
return
}
// check local partition compare with master ,if lack,then not start
if err = s.checkLocalPartitionMatchWithMaster(); err != nil {
log.LogError(err)
exporter.Warning(err.Error())
return
}
// tcp listening & tcp connection pool
if err = s.startTCPService(); err != nil {
return
}
//smux listening & smux connection pool
if err = s.startSmuxService(cfg); err != nil {
return
}
go s.registerHandler()
go s.startUpdateNodeInfo()
return
}
func doShutdown(server common.Server) {
s, ok := server.(*DataNode)
if !ok {
return
}
close(s.stopC)
s.space.Stop()
s.stopUpdateNodeInfo()
s.stopTCPService()
s.stopRaftServer()
s.stopSmuxService()
s.closeSmuxConnPool()
}
func (s *DataNode) parseConfig(cfg *config.Config) (err error) {
var (
port string
regexpPort *regexp.Regexp
)
LocalIP = cfg.GetString(ConfigKeyLocalIP)
port = cfg.GetString(proto.ListenPort)
serverPort = port
if regexpPort, err = regexp.Compile("^(\\d)+$"); err != nil {
return fmt.Errorf("Err:no port")
}
if !regexpPort.MatchString(port) {
return fmt.Errorf("Err:port must string")
}
s.port = port
if len(cfg.GetSlice(proto.MasterAddr)) == 0 {
return fmt.Errorf("Err:masterAddr unavalid")
}
for _, ip := range cfg.GetSlice(proto.MasterAddr) {
MasterClient.AddNode(ip.(string))
}
s.zoneName = cfg.GetString(ConfigKeyZone)
if s.zoneName == "" {
s.zoneName = DefaultZoneName
}
s.metricsDegrade = cfg.GetInt(CfgMetricsDegrade)
log.LogDebugf("action[parseConfig] load masterAddrs(%v).", MasterClient.Nodes())
log.LogDebugf("action[parseConfig] load port(%v).", s.port)
log.LogDebugf("action[parseConfig] load zoneName(%v).", s.zoneName)
return
}
func (s *DataNode) initQosLimit(cfg *config.Config) {
s.space.dataNode.diskQosEnable = cfg.GetBoolWithDefault(ConfigDiskQosEnable, true)
log.LogWarnf("action[initQosLimit] set qos value [%v] ,other param use default value", s.space.dataNode.diskQosEnable)
}
func (s *DataNode) updateQosLimit() {
for _, disk := range s.space.disks {
disk.updateQosLimiter()
}
}
func (s *DataNode) startSpaceManager(cfg *config.Config) (err error) {
s.startTime = time.Now().Unix()
s.space = NewSpaceManager(s)
if len(strings.TrimSpace(s.port)) == 0 {
err = ErrNewSpaceManagerFailed
return
}
s.space.SetRaftStore(s.raftStore)
s.space.SetNodeID(s.nodeID)
s.space.SetClusterID(s.clusterID)
s.initQosLimit(cfg)
diskRdonlySpace := uint64(cfg.GetInt64(CfgDiskRdonlySpace))
if diskRdonlySpace < DefaultDiskRetainMin {
diskRdonlySpace = DefaultDiskRetainMin
}
log.LogInfof("startSpaceManager preReserveSpace %d", diskRdonlySpace)
var wg sync.WaitGroup
for _, d := range cfg.GetSlice(ConfigKeyDisks) {
log.LogDebugf("action[startSpaceManager] load disk raw config(%v).", d)
// format "PATH:RESET_SIZE
arr := strings.Split(d.(string), ":")
if len(arr) != 2 {
return errors.New("Invalid disk configuration. Example: PATH:RESERVE_SIZE")
}
path := arr[0]
fileInfo, err := os.Stat(path)
if err != nil {
return errors.New(fmt.Sprintf("Stat disk path error: %s", err.Error()))
}
if !fileInfo.IsDir() {
return errors.New("Disk path is not dir")
}
reservedSpace, err := strconv.ParseUint(arr[1], 10, 64)
if err != nil {
return errors.New(fmt.Sprintf("Invalid disk reserved space. Error: %s", err.Error()))
}
if reservedSpace < DefaultDiskRetainMin {
reservedSpace = DefaultDiskRetainMin
}
wg.Add(1)
go func(wg *sync.WaitGroup, path string, reservedSpace uint64) {
defer wg.Done()
s.space.LoadDisk(path, reservedSpace, diskRdonlySpace, DefaultDiskMaxErr)
}(&wg, path, reservedSpace)
}
wg.Wait()
return nil
}
// registers the data node on the master to report the information such as IsIPV4 address.
// The startup of a data node will be blocked until the registration succeeds.
func (s *DataNode) register(cfg *config.Config) {
var (
err error
)
timer := time.NewTimer(0)
// get the IsIPV4 address, cluster ID and node ID from the master
for {
select {
case <-timer.C:
var ci *proto.ClusterInfo
if ci, err = MasterClient.AdminAPI().GetClusterInfo(); err != nil {
log.LogErrorf("action[registerToMaster] cannot get ip from master(%v) err(%v).",
MasterClient.Leader(), err)
timer.Reset(2 * time.Second)
continue
}
masterAddr := MasterClient.Leader()
s.clusterID = ci.Cluster
if LocalIP == "" {
LocalIP = string(ci.Ip)
}
s.localServerAddr = fmt.Sprintf("%s:%v", LocalIP, s.port)
if !util.IsIPV4(LocalIP) {
log.LogErrorf("action[registerToMaster] got an invalid local ip(%v) from master(%v).",
LocalIP, masterAddr)
timer.Reset(2 * time.Second)
continue
}
// register this data node on the master
var nodeID uint64
if nodeID, err = MasterClient.NodeAPI().AddDataNode(fmt.Sprintf("%s:%v", LocalIP, s.port), s.zoneName); err != nil {
log.LogErrorf("action[registerToMaster] cannot register this node to master[%v] err(%v).",
masterAddr, err)
timer.Reset(2 * time.Second)
continue
}
exporter.RegistConsul(s.clusterID, ModuleName, cfg)
s.nodeID = nodeID
log.LogDebugf("register: register DataNode: nodeID(%v)", s.nodeID)
return
case <-s.stopC:
timer.Stop()
return
}
}
}
type DataNodeInfo struct {
Addr string
PersistenceDataPartitions []uint64
}
func (s *DataNode) checkLocalPartitionMatchWithMaster() (err error) {
var convert = func(node *proto.DataNodeInfo) *DataNodeInfo {
result := &DataNodeInfo{}
result.Addr = node.Addr
result.PersistenceDataPartitions = node.PersistenceDataPartitions
return result
}
var dataNode *proto.DataNodeInfo
for i := 0; i < 3; i++ {
if dataNode, err = MasterClient.NodeAPI().GetDataNode(s.localServerAddr); err != nil {
log.LogErrorf("checkLocalPartitionMatchWithMaster error %v", err)
continue
}
break
}
if dataNode == nil {
err = ErrGetMasterDatanodeInfoFailed
return
}
dinfo := convert(dataNode)
if len(dinfo.PersistenceDataPartitions) == 0 {
return
}
lackPartitionsNeedCheck := make([]uint64, 0)
for _, partitionID := range dinfo.PersistenceDataPartitions {
dp := s.space.Partition(partitionID)
if dp == nil {
lackPartitionsNeedCheck = append(lackPartitionsNeedCheck, partitionID)
}
}
if len(lackPartitionsNeedCheck) == 0 {
return
}
lackPartitions := make([]uint64, 0)
for _, lackPartitionID := range lackPartitionsNeedCheck {
var dp *proto.DataPartitionInfo
for i := 0; i < 3; i++ {
if dp, err = MasterClient.AdminAPI().GetDataPartitionById(lackPartitionID); err != nil {
log.LogErrorf("checkLocalPartitionMatchWithMaster error %v", err)
continue
}
break
}
if err != nil {
return
}
if dp.ReplicaNum != 1 {
lackPartitions = append(lackPartitions, lackPartitionID)
continue
}
log.LogInfof("action[checkLocalPartitionMatchWithMaster] dp [%v] replicaNum [%v] ignore local error", dp.PartitionID, dp.ReplicaNum)
}
if len(lackPartitions) == 0 {
return
}
err = fmt.Errorf("LackPartitions %v on datanode %v,datanode cannot start", lackPartitions, s.localServerAddr)
log.LogErrorf(err.Error())
return
}
func (s *DataNode) registerHandler() {
http.HandleFunc("/disks", s.getDiskAPI)
http.HandleFunc("/partitions", s.getPartitionsAPI)
http.HandleFunc("/partition", s.getPartitionAPI)
http.HandleFunc("/extent", s.getExtentAPI)
http.HandleFunc("/block", s.getBlockCrcAPI)
http.HandleFunc("/stats", s.getStatAPI)
http.HandleFunc("/raftStatus", s.getRaftStatus)
http.HandleFunc("/setAutoRepairStatus", s.setAutoRepairStatus)
http.HandleFunc("/getTinyDeleted", s.getTinyDeleted)
http.HandleFunc("/getNormalDeleted", s.getNormalDeleted)
http.HandleFunc("/getSmuxPoolStat", s.getSmuxPoolStat())
http.HandleFunc("/setMetricsDegrade", s.setMetricsDegrade)
http.HandleFunc("/getMetricsDegrade", s.getMetricsDegrade)
http.HandleFunc("/qosEnable", s.setQosEnable())
}
func (s *DataNode) startTCPService() (err error) {
log.LogInfo("Start: startTCPService")
addr := fmt.Sprintf(":%v", s.port)
l, err := net.Listen(NetworkProtocol, addr)
log.LogDebugf("action[startTCPService] listen %v address(%v).", NetworkProtocol, addr)
if err != nil {
log.LogError("failed to listen, err:", err)
return
}
s.tcpListener = l
go func(ln net.Listener) {
for {
conn, err := ln.Accept()
if err != nil {
log.LogErrorf("action[startTCPService] failed to accept, err:%s", err.Error())
break
}
log.LogDebugf("action[startTCPService] accept connection from %s.", conn.RemoteAddr().String())
go s.serveConn(conn)
}
}(l)
return
}
func (s *DataNode) stopTCPService() (err error) {
if s.tcpListener != nil {
s.tcpListener.Close()
log.LogDebugf("action[stopTCPService] stop tcp service.")
}
return
}
func (s *DataNode) serveConn(conn net.Conn) {
space := s.space
space.Stats().AddConnection()
c, _ := conn.(*net.TCPConn)
c.SetKeepAlive(true)
c.SetNoDelay(true)
packetProcessor := repl.NewReplProtocol(conn, s.Prepare, s.OperatePacket, s.Post)
packetProcessor.ServerConn()
}
func (s *DataNode) startSmuxService(cfg *config.Config) (err error) {
log.LogInfo("Start: startSmuxService")
addr := fmt.Sprintf(":%v", s.port)
addr = util.ShiftAddrPort(addr, s.smuxPortShift)
log.LogInfof("SmuxListenAddr: (%v)", addr)
// server
l, err := net.Listen(NetworkProtocol, addr)
log.LogDebugf("action[startSmuxService] listen %v address(%v).", NetworkProtocol, addr)
if err != nil {
log.LogError("failed to listen smux addr, err:", err)
return
}
s.smuxListener = l
go func(ln net.Listener) {
for {
conn, err := ln.Accept()
if err != nil {
log.LogErrorf("action[startSmuxService] failed to accept, err:%s", err.Error())
break
}
log.LogDebugf("action[startSmuxService] accept connection from %s.", conn.RemoteAddr().String())
go s.serveSmuxConn(conn)
}
}(l)
return
}
func (s *DataNode) stopSmuxService() (err error) {
if s.smuxListener != nil {
s.smuxListener.Close()
log.LogDebugf("action[stopSmuxService] stop smux service.")
}
return
}
func (s *DataNode) serveSmuxConn(conn net.Conn) {
space := s.space
space.Stats().AddConnection()
c, _ := conn.(*net.TCPConn)
c.SetKeepAlive(true)
c.SetNoDelay(true)
var sess *smux.Session
var err error
sess, err = smux.Server(conn, s.smuxServerConfig)
if err != nil {
log.LogErrorf("action[serveSmuxConn] failed to serve smux connection, addr(%v), err(%v)", c.RemoteAddr(), err)
return
}
defer sess.Close()
for {
stream, err := sess.AcceptStream()
if err != nil {
if util.FilterSmuxAcceptError(err) != nil {
log.LogErrorf("action[startSmuxService] failed to accept, err: %s", err)
} else {
log.LogInfof("action[startSmuxService] accept done, err: %s", err)
}
break
}
go s.serveSmuxStream(stream)
}
return
}
func (s *DataNode) serveSmuxStream(stream *smux.Stream) {
packetProcessor := repl.NewReplProtocol(stream, s.Prepare, s.OperatePacket, s.Post)
packetProcessor.ServerConn()
}
// Increase the disk error count by one.
func (s *DataNode) incDiskErrCnt(partitionID uint64, err error, flag uint8) {
if err == nil {
return
}
dp := s.space.Partition(partitionID)
if dp == nil {
return
}
d := dp.Disk()
if d == nil {
return
}
if !IsDiskErr(err.Error()) {
return
}
if flag == WriteFlag {
d.incWriteErrCnt()
} else if flag == ReadFlag {
d.incReadErrCnt()
}
}
func (s *DataNode) parseSmuxConfig(cfg *config.Config) error {
s.enableSmuxConnPool = cfg.GetBool(ConfigKeyEnableSmuxClient)
s.smuxPortShift = int(cfg.GetInt64(ConfigKeySmuxPortShift))
if s.smuxPortShift == 0 {
s.smuxPortShift = util.DefaultSmuxPortShift
}
// smux server cfg
s.smuxServerConfig = util.DefaultSmuxConfig()
maxBuffer := cfg.GetInt64(ConfigKeySmuxMaxBuffer)
if maxBuffer > 0 {
s.smuxServerConfig.MaxReceiveBuffer = int(maxBuffer)
if s.smuxServerConfig.MaxStreamBuffer > int(maxBuffer) {
s.smuxServerConfig.MaxStreamBuffer = int(maxBuffer)
}
if err := smux.VerifyConfig(s.smuxServerConfig); err != nil {
return err
}
}
//smux conn pool config
if s.enableSmuxConnPool {
s.smuxConnPoolConfig = util.DefaultSmuxConnPoolConfig()
if maxBuffer > 0 {
s.smuxConnPoolConfig.MaxReceiveBuffer = int(maxBuffer)
if s.smuxConnPoolConfig.MaxStreamBuffer > int(maxBuffer) {
s.smuxConnPoolConfig.MaxStreamBuffer = int(maxBuffer)
}
}
maxConn := cfg.GetInt64(ConfigKeySmuxMaxConn)
if maxConn > 0 {
if s.smuxConnPoolConfig.ConnsPerAddr < int(maxConn) {
s.smuxConnPoolConfig.ConnsPerAddr = int(maxConn)
}
}
maxStreamPerConn := cfg.GetInt64(ConfigKeySmuxStreamPerConn)
if maxStreamPerConn > 0 {
s.smuxConnPoolConfig.StreamsPerConn = int(maxStreamPerConn)
}
if err := util.VerifySmuxPoolConfig(s.smuxConnPoolConfig); err != nil {
return err
}
}
log.LogDebugf("[parseSmuxConfig] load smuxPortShift(%v).", s.smuxPortShift)
log.LogDebugf("[parseSmuxConfig] load enableSmuxConnPool(%v).", s.enableSmuxConnPool)
log.LogDebugf("[parseSmuxConfig] load smuxServerConfig(%v).", s.smuxServerConfig)
log.LogDebugf("[parseSmuxConfig] load smuxConnPoolConfig(%v).", s.smuxConnPoolConfig)
return nil
}
func (s *DataNode) initConnPool() {
if s.enableSmuxConnPool {
log.LogInfof("Start: init smux conn pool")
s.smuxConnPool = util.NewSmuxConnectPool(s.smuxConnPoolConfig)
s.getRepairConnFunc = func(target string) (net.Conn, error) {
addr := util.ShiftAddrPort(target, s.smuxPortShift)
log.LogDebugf("[dataNode.getRepairConnFunc] get smux conn, addr(%v)", addr)
return s.smuxConnPool.GetConnect(addr)
}
s.putRepairConnFunc = func(conn net.Conn, forceClose bool) {
log.LogDebugf("[dataNode.putRepairConnFunc] put smux conn, addr(%v), forceClose(%v)", conn.RemoteAddr().String(), forceClose)
s.smuxConnPool.PutConnect(conn.(*smux.Stream), forceClose)
}
} else {
s.getRepairConnFunc = func(target string) (conn net.Conn, err error) {
log.LogDebugf("[dataNode.getRepairConnFunc] get tcp conn, addr(%v)", target)
return gConnPool.GetConnect(target)
}
s.putRepairConnFunc = func(conn net.Conn, forceClose bool) {
log.LogDebugf("[dataNode.putRepairConnFunc] put tcp conn, addr(%v), forceClose(%v)", conn.RemoteAddr().String(), forceClose)
gConnPool.PutConnect(conn.(*net.TCPConn), forceClose)
return
}
}
return
}
func (s *DataNode) closeSmuxConnPool() {
if s.smuxConnPool != nil {
s.smuxConnPool.Close()
log.LogDebugf("action[stopSmuxService] stop smux conn pool")
}
return
}
func (s *DataNode) shallDegrade() bool {
level := atomic.LoadInt64(&s.metricsDegrade)
if level < 0 {
return true
}
if level == 0 {
return false
}
cnt := atomic.LoadUint64(&s.metricsCnt)
if cnt%uint64(level) == 0 {
return false
}
return true
}
func IsDiskErr(errMsg string) bool {
if strings.Contains(errMsg, syscall.EIO.Error()) || strings.Contains(errMsg, syscall.EROFS.Error()) {
return true
}
return false
}