-
Notifications
You must be signed in to change notification settings - Fork 326
/
load_manage_report.go
156 lines (130 loc) · 5.21 KB
/
load_manage_report.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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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 utils
import (
"math"
)
type LocalBrokerData struct {
// URLs to satisfy contract of ServiceLookupData (used by NamespaceService).
WebServiceURL string `json:"webServiceUrl"`
WebServiceURLTLS string `json:"webServiceUrlTls"`
PulsarServiceURL string `json:"pulsarServiceUrl"`
PulsarServiceURLTLS string `json:"pulsarServiceUrlTls"`
PersistentTopicsEnabled bool `json:"persistentTopicsEnabled"`
NonPersistentTopicsEnabled bool `json:"nonPersistentTopicsEnabled"`
// Most recently available system resource usage.
CPU ResourceUsage `json:"cpu"`
Memory ResourceUsage `json:"memory"`
DirectMemory ResourceUsage `json:"directMemory"`
BandwidthIn ResourceUsage `json:"bandwidthIn"`
BandwidthOut ResourceUsage `json:"bandwidthOut"`
// Message data from the most recent namespace bundle stats.
MsgThroughputIn float64 `json:"msgThroughputIn"`
MsgThroughputOut float64 `json:"msgThroughputOut"`
MsgRateIn float64 `json:"msgRateIn"`
MsgRateOut float64 `json:"msgRateOut"`
// Timestamp of last update.
LastUpdate int64 `json:"lastUpdate"`
// The stats given in the most recent invocation of update.
LastStats map[string]*NamespaceBundleStats `json:"lastStats"`
NumTopics int `json:"numTopics"`
NumBundles int `json:"numBundles"`
NumConsumers int `json:"numConsumers"`
NumProducers int `json:"numProducers"`
// All bundles belonging to this broker.
Bundles []string `json:"bundles"`
// The bundles gained since the last invocation of update.
LastBundleGains []string `json:"lastBundleGains"`
// The bundles lost since the last invocation of update.
LastBundleLosses []string `json:"lastBundleLosses"`
// The version string that this broker is running, obtained from the Maven build artifact in the POM
BrokerVersionString string `json:"brokerVersionString"`
// This place-holder requires to identify correct LoadManagerReport type while deserializing
LoadReportType string `json:"loadReportType"`
// the external protocol data advertised by protocol handlers.
Protocols map[string]string `json:"protocols"`
}
func NewLocalBrokerData() LocalBrokerData {
lastStats := make(map[string]*NamespaceBundleStats)
lastStats[""] = NewNamespaceBundleStats()
return LocalBrokerData{
LastStats: lastStats,
}
}
type NamespaceBundleStats struct {
MsgRateIn float64 `json:"msgRateIn"`
MsgThroughputIn float64 `json:"msgThroughputIn"`
MsgRateOut float64 `json:"msgRateOut"`
MsgThroughputOut float64 `json:"msgThroughputOut"`
ConsumerCount int `json:"consumerCount"`
ProducerCount int `json:"producerCount"`
TopicsNum int64 `json:"topics"`
CacheSize int64 `json:"cacheSize"`
// Consider the throughput equal if difference is less than 100 KB/s
ThroughputDifferenceThreshold float64 `json:"throughputDifferenceThreshold"`
// Consider the msgRate equal if the difference is less than 100
MsgRateDifferenceThreshold float64 `json:"msgRateDifferenceThreshold"`
// Consider the total topics/producers/consumers equal if the difference is less than 500
TopicConnectionDifferenceThreshold int64 `json:"topicConnectionDifferenceThreshold"`
// Consider the cache size equal if the difference is less than 100 kb
CacheSizeDifferenceThreshold int64 `json:"cacheSizeDifferenceThreshold"`
}
func NewNamespaceBundleStats() *NamespaceBundleStats {
return &NamespaceBundleStats{
ThroughputDifferenceThreshold: 1e5,
MsgRateDifferenceThreshold: 100,
TopicConnectionDifferenceThreshold: 500,
CacheSizeDifferenceThreshold: 100000,
}
}
type ResourceUsage struct {
Usage float64 `json:"usage"`
Limit float64 `json:"limit"`
}
func (ru *ResourceUsage) Reset() {
ru.Usage = -1
ru.Limit = -1
}
func (ru *ResourceUsage) CompareTo(o *ResourceUsage) int {
required := o.Limit - o.Usage
available := ru.Limit - ru.Usage
return compare(required, available)
}
func (ru *ResourceUsage) PercentUsage() float32 {
var proportion float32
if ru.Limit > 0 {
proportion = float32(ru.Usage) / float32(ru.Limit)
}
return proportion * 100
}
func compare(val1, val2 float64) int {
if val1 < val2 {
return -1
}
if val1 < val2 {
return 1
}
thisBits := math.Float64bits(val1)
anotherBits := math.Float64bits(val2)
if thisBits == anotherBits {
return 0
}
if thisBits < anotherBits {
return -1
}
return 1
}