Skip to content

add cache storage metrics #1195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 26 additions & 22 deletions common/monitoring/metricsMonitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package monitoring
import (
"database/sql"
"fmt"
"github.com/zoobc/lib/address"
"github.com/zoobc/zoobc-core/common/constant"
"math"
"net/http"
"reflect"
"sync"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/zoobc/lib/address"
"github.com/zoobc/zoobc-core/common/chaintype"
"github.com/zoobc/zoobc-core/common/constant"
"github.com/zoobc/zoobc-core/common/model"
)

Expand Down Expand Up @@ -45,9 +44,8 @@ var (
apiRunningGaugeVector *prometheus.GaugeVec
snapshotDownloadRequestCounter *prometheus.CounterVec
dbStatGaugeVector *prometheus.GaugeVec
cacheStorageGaugeVector *prometheus.GaugeVec

badgerMetrics map[string]prometheus.Gauge
badgerMetricsLock sync.Mutex
cliMonitoringInstance CLIMonitoringInteface
)

Expand Down Expand Up @@ -270,6 +268,12 @@ func SetMonitoringActive(isActive bool) {
}, []string{"status"})
prometheus.MustRegister(dbStatGaugeVector)

// Cache Storage
cacheStorageGaugeVector = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "zoobc_cache_storage",
Help: "Cache storage usage in bytes",
}, []string{"cache_type"})
prometheus.MustRegister(cacheStorageGaugeVector)
}

func SetCLIMonitoring(cliMonitoring CLIMonitoringInteface) {
Expand Down Expand Up @@ -543,22 +547,22 @@ func SetDatabaseStats(dbStat sql.DBStats) {
dbStatGaugeVector.WithLabelValues("ConnectionsWaitCount").Set(float64(dbStat.WaitCount))
}

func SetBadgerMetrics(metrics map[string]float64) {
if badgerMetrics == nil {
badgerMetrics = make(map[string]prometheus.Gauge)
}
type (
// CacheStorageType type of cache storage that needed for inc or dec the value
CacheStorageType string
)

for key, val := range metrics {
if _, ok := badgerMetrics[key]; !ok {
badgerMetricsLock.Lock()
if _, ok := badgerMetrics[key]; !ok {
badgerMetrics[key] = prometheus.NewGauge(prometheus.GaugeOpts{
Name: key,
})
prometheus.MustRegister(badgerMetrics[key])
}
badgerMetricsLock.Unlock()
}
badgerMetrics[key].Set(val)
}
// Cache Storage environments
// Please add new one when add new cache storage instance
var (
TypeMempoolCacheStorage CacheStorageType = "mempools"
TypeBatchReceiptCacheStorage CacheStorageType = "batch_receipts"
TypeScrambleNodeCacheStorage CacheStorageType = "scramble_nodes"
TypeMempoolBackupCacheStorage CacheStorageType = "backup_mempools"
TypeNodeShardCacheStorage CacheStorageType = "node_shards"
TypeNodeAddressInfoCacheStorage CacheStorageType = "node_address_infos"
)

func SetCacheStorageMetrics(cacheType CacheStorageType, size float64) {
cacheStorageGaugeVector.WithLabelValues(string(cacheType)).Set(size)
}
17 changes: 12 additions & 5 deletions common/storage/batchReceipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/zoobc/zoobc-core/common/blocker"
"github.com/zoobc/zoobc-core/common/model"
"github.com/zoobc/zoobc-core/common/monitoring"
)

type (
Expand Down Expand Up @@ -37,6 +38,7 @@ func (brs *BatchReceiptCacheStorage) SetItem(_, item interface{}) error {
}

brs.receipts = append(brs.receipts, nItem)
monitoring.SetCacheStorageMetrics(monitoring.TypeBatchReceiptCacheStorage, float64(brs.size()))
return nil
}

Expand All @@ -55,6 +57,7 @@ func (brs *BatchReceiptCacheStorage) SetItems(items interface{}) error {
return blocker.NewBlocker(blocker.ValidationErr, "invalid batch receipt item")
}
brs.receipts = nItems
monitoring.SetCacheStorageMetrics(monitoring.TypeBatchReceiptCacheStorage, float64(brs.size()))
return nil
}

Expand Down Expand Up @@ -87,21 +90,25 @@ func (brs *BatchReceiptCacheStorage) RemoveItem(_ interface{}) error {
return nil
}

func (brs *BatchReceiptCacheStorage) GetSize() int64 {
brs.Lock()
defer brs.Unlock()

func (brs *BatchReceiptCacheStorage) size() int {
var size int
for _, cache := range brs.receipts {
size += cache.XXX_Size()
}
return int64(size)
return size
}
func (brs *BatchReceiptCacheStorage) GetSize() int64 {
brs.RLock()
defer brs.RUnlock()

return int64(brs.size())
}

func (brs *BatchReceiptCacheStorage) ClearCache() error {
brs.Lock()
defer brs.Unlock()

brs.receipts = make([]model.BatchReceipt, 0)
monitoring.SetCacheStorageMetrics(monitoring.TypeBatchReceiptCacheStorage, 0)
return nil
}
23 changes: 17 additions & 6 deletions common/storage/mempoolBackupStorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sync"

"github.com/zoobc/zoobc-core/common/blocker"
"github.com/zoobc/zoobc-core/common/monitoring"
)

type (
Expand Down Expand Up @@ -42,6 +43,8 @@ func (m *MempoolBackupStorage) SetItem(key, item interface{}) error {
}

m.mempools[id] = mempoolByte
monitoring.SetCacheStorageMetrics(monitoring.TypeMempoolBackupCacheStorage, float64(m.size()))

return nil
}

Expand All @@ -59,6 +62,8 @@ func (m *MempoolBackupStorage) SetItems(items interface{}) error {
return blocker.NewBlocker(blocker.ValidationErr, "WrongType items")
}
m.mempools = nItems
monitoring.SetCacheStorageMetrics(monitoring.TypeMempoolBackupCacheStorage, float64(m.size()))

return nil
}

Expand Down Expand Up @@ -111,19 +116,24 @@ func (m *MempoolBackupStorage) RemoveItem(key interface{}) error {
return blocker.NewBlocker(blocker.ValidationErr, "WrongType item")
}
delete(m.mempools, id)
monitoring.SetCacheStorageMetrics(monitoring.TypeMempoolBackupCacheStorage, float64(m.size()))
return nil
}

// GetSize get size of MempoolBackupStorage values
func (m *MempoolBackupStorage) GetSize() int64 {
m.Lock()
defer m.Unlock()

func (m *MempoolBackupStorage) size() int {
var size int
for _, v := range m.mempools {
size += len(v)
}
return int64(size)
return size
}

// GetSize get size of MempoolBackupStorage values
func (m *MempoolBackupStorage) GetSize() int64 {
m.RLock()
defer m.RUnlock()

return int64(m.size())
}

// ClearCache clear or remove all items from MempoolBackupStorage
Expand All @@ -132,5 +142,6 @@ func (m *MempoolBackupStorage) ClearCache() error {
defer m.Unlock()

m.mempools = make(map[int64][]byte)
monitoring.SetCacheStorageMetrics(monitoring.TypeMempoolBackupCacheStorage, 0)
return nil
}
19 changes: 15 additions & 4 deletions common/storage/mempoolStorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/zoobc/zoobc-core/common/blocker"
"github.com/zoobc/zoobc-core/common/model"
"github.com/zoobc/zoobc-core/common/monitoring"
)

type (
Expand Down Expand Up @@ -39,6 +40,7 @@ func (m *MempoolCacheStorage) SetItem(key, item interface{}) error {
return blocker.NewBlocker(blocker.ValidationErr, "WrongType item")
}
m.mempoolMap[keyInt64] = mempoolMap
monitoring.SetCacheStorageMetrics(monitoring.TypeMempoolCacheStorage, float64(m.size()))
} else {
return blocker.NewBlocker(blocker.ValidationErr, "WrongType item")
}
Expand Down Expand Up @@ -94,19 +96,28 @@ func (m *MempoolCacheStorage) RemoveItem(keys interface{}) error {
for _, id := range ids {
delete(m.mempoolMap, id)
}
monitoring.SetCacheStorageMetrics(monitoring.TypeMempoolCacheStorage, float64(m.size()))
return nil
}

func (m *MempoolCacheStorage) GetSize() int64 {
var size int64
func (m *MempoolCacheStorage) size() int {
var size int
for _, memObj := range m.mempoolMap {
size += 8 * 3 // key + feePerByte + arrivalTimestamp + blockHeight
size += int64(memObj.TransactionByteSize)
size += 8 * 3
size += int(memObj.TransactionByteSize)
}
return size
}

func (m *MempoolCacheStorage) GetSize() int64 {
m.RLock()
defer m.RUnlock()

return int64(m.size())
}

func (m *MempoolCacheStorage) ClearCache() error {
m.mempoolMap = make(MempoolMap)
monitoring.SetCacheStorageMetrics(monitoring.TypeMempoolCacheStorage, 0)
return nil
}
29 changes: 22 additions & 7 deletions common/storage/nodeAddressInfoStorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strconv"
"sync"

"github.com/zoobc/zoobc-core/common/monitoring"

"github.com/zoobc/zoobc-core/common/blocker"
"github.com/zoobc/zoobc-core/common/model"
)
Expand All @@ -23,7 +25,7 @@ type (
nodeAddressInfoMapByStatus map[model.NodeAddressStatus]map[int64]map[string]bool
awaitedRemoveList map[int64]map[string]bool
}
// NodeAddressInfoStorage represent a key for NodeAddressInfoStorage
// NodeAddressInfoStorageKey represent a key for NodeAddressInfoStorage
NodeAddressInfoStorageKey struct {
NodeID int64
AddressPort string
Expand Down Expand Up @@ -65,6 +67,8 @@ func (nas *NodeAddressInfoStorage) SetItem(key, item interface{}) error {
nas.nodeAddressInfoMapByStatus[nodeAddressInfo.Status][nodeAddressInfo.NodeID] = make(map[string]bool)
}
nas.nodeAddressInfoMapByStatus[nodeAddressInfo.Status][nodeAddressInfo.NodeID][fullAddress] = true

monitoring.SetCacheStorageMetrics(monitoring.TypeNodeAddressInfoCacheStorage, float64(nas.size()))
return nil
}

Expand Down Expand Up @@ -151,21 +155,22 @@ func (nas *NodeAddressInfoStorage) RemoveItem(key interface{}) error {
}

// Remove all waiting node address info on remove list
for nodeID, nodePotitionsByAddressPort := range nas.awaitedRemoveList {
for fullAddress := range nodePotitionsByAddressPort {
for nodeID, nodePositionsByAddressPort := range nas.awaitedRemoveList {
for fullAddress := range nodePositionsByAddressPort {
status := nas.nodeAddressInfoMapByID[nodeID][fullAddress].Status
delete(nas.nodeAddressInfoMapByStatus[status][nodeID], fullAddress)
delete(nas.nodeAddressInfoMapByAddressPort[fullAddress], nodeID)
delete(nas.nodeAddressInfoMapByID[nodeID], fullAddress)
}
}
_ = nas.ClearAwaitedRemoveItems()

monitoring.SetCacheStorageMetrics(monitoring.TypeNodeAddressInfoCacheStorage, float64(nas.size()))

return nil
}

func (nas *NodeAddressInfoStorage) GetSize() int64 {
nas.Lock()
defer nas.Unlock()
func (nas *NodeAddressInfoStorage) size() int {
var (
nasBytes bytes.Buffer
enc = gob.NewEncoder(&nasBytes)
Expand All @@ -174,7 +179,12 @@ func (nas *NodeAddressInfoStorage) GetSize() int64 {
_ = enc.Encode(nas.nodeAddressInfoMapByAddressPort)
_ = enc.Encode(nas.nodeAddressInfoMapByStatus)
_ = enc.Encode(nas.awaitedRemoveList)
return int64(len(nasBytes.Bytes()))
return nasBytes.Len()
}
func (nas *NodeAddressInfoStorage) GetSize() int64 {
nas.RLock()
defer nas.RUnlock()
return int64(nas.size())
}

func (nas *NodeAddressInfoStorage) ClearCache() error {
Expand All @@ -184,6 +194,8 @@ func (nas *NodeAddressInfoStorage) ClearCache() error {
nas.nodeAddressInfoMapByAddressPort = make(map[string]map[int64]bool)
nas.nodeAddressInfoMapByStatus = make(map[model.NodeAddressStatus]map[int64]map[string]bool)
nas.awaitedRemoveList = make(map[int64]map[string]bool)
monitoring.SetCacheStorageMetrics(monitoring.TypeNodeAddressInfoCacheStorage, float64(nas.size()))

return nil
}

Expand All @@ -206,11 +218,14 @@ func (nas *NodeAddressInfoStorage) AddAwaitedRemoveItem(storageKey NodeAddressIn
nas.awaitedRemoveList[storageKey.NodeID][fullAddressPort] = true
}
}
monitoring.SetCacheStorageMetrics(monitoring.TypeNodeAddressInfoCacheStorage, float64(nas.size()))
return nil
}

func (nas *NodeAddressInfoStorage) ClearAwaitedRemoveItems() error {
nas.awaitedRemoveList = make(map[int64]map[string]bool)
monitoring.SetCacheStorageMetrics(monitoring.TypeNodeAddressInfoCacheStorage, float64(nas.size()))

return nil
}

Expand Down
Loading