cubefs
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
15package datanode16
17import (18"sync"19"sync/atomic"20"time"21)
22
23// Stats defines various metrics that will be collected during the execution.
24type Stats struct {25inDataSize uint6426outDataSize uint6427inFlow uint6428outFlow uint6429
30Zone string31ConnectionCnt int6432ClusterID string33TCPAddr string34Start time.Time35Total uint6436Used uint6437Available uint64 // available space38TotalPartitionSize uint64 // dataPartitionCnt * dataPartitionSize39RemainingCapacityToCreatePartition uint6440CreatedPartitionCnt uint6441LackPartitionsInMem uint6442LackPartitionsInDisk uint6443
44// the maximum capacity among all the disks that can be used to create partition45MaxCapacityToCreatePartition uint6446
47sync.Mutex48}
49
50// NewStats creates a new Stats.
51func NewStats(zone string) (s *Stats) {52s = new(Stats)53s.Zone = zone54return s55}
56
57// AddConnection adds a connection.
58func (s *Stats) AddConnection() {59atomic.AddInt64(&s.ConnectionCnt, 1)60}
61
62// RemoveConnection removes a connection.
63func (s *Stats) RemoveConnection() {64atomic.AddInt64(&s.ConnectionCnt, -1)65}
66
67// GetConnectionCount gets the connection count.
68func (s *Stats) GetConnectionCount() int64 {69return atomic.LoadInt64(&s.ConnectionCnt)70}
71
72func (s *Stats) updateMetrics(73total, used, available, createdPartitionWeights, remainWeightsForCreatePartition,74maxWeightsForCreatePartition, dataPartitionCnt uint64) {75s.Lock()76defer s.Unlock()77
78s.Total = total79s.Used = used80s.Available = available81s.TotalPartitionSize = createdPartitionWeights82s.RemainingCapacityToCreatePartition = remainWeightsForCreatePartition83s.MaxCapacityToCreatePartition = maxWeightsForCreatePartition84s.CreatedPartitionCnt = dataPartitionCnt85}
86
87func (s *Stats) updateMetricLackPartitionsInMem(lackPartitionsInMem uint64) {88s.Lock()89defer s.Unlock()90
91s.LackPartitionsInMem = lackPartitionsInMem92}
93
94func (s *Stats) updateMetricLackPartitionsInDisk(lackPartitionsInDisk uint64) {95s.Lock()96defer s.Unlock()97
98s.LackPartitionsInDisk = lackPartitionsInDisk99}
100