-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
213 lines (184 loc) · 4.96 KB
/
metrics.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
/*
Copyright (C) THL A29 Limited, a Tencent company. All rights reserved.
SPDX-License-Identifier: Apache-2.0
*/
package tbft
import (
"encoding/json"
"time"
)
//
// roundMetrics
// @Description: Record each stage status duration and start time
//
type roundMetrics struct {
// current round
round int32
enterNewRoundTime time.Time
enterProposalTime time.Time
enterPrevoteTime time.Time
enterPrecommitTime time.Time
enterCommitTime time.Time
// Status duration for each stage
persistStateDurations map[string][]time.Duration
}
//
// newRoundMetrics
// @Description: Enter a new round, create a new object(*roundMetrics)
// @param round
// @return *roundMetrics
//
func newRoundMetrics(round int32) *roundMetrics {
return &roundMetrics{
round: round,
persistStateDurations: make(map[string][]time.Duration),
}
}
//
// roundMetricsJson
// @Description: json format roundMetrics object
//
type roundMetricsJson struct {
Round int32
Proposal string
Prevote string
Precommit string
PersistStateDurations map[string][]string
}
//
// roundMetricsJson
// @Description: convert *roundMetrics to *roundMetricsJson
// @receiver r
// @return *roundMetricsJson
//
func (r *roundMetrics) roundMetricsJson() *roundMetricsJson {
j := &roundMetricsJson{
Round: r.round,
Proposal: r.enterPrevoteTime.Sub(r.enterProposalTime).String(),
Prevote: r.enterPrecommitTime.Sub(r.enterPrevoteTime).String(),
Precommit: r.enterCommitTime.Sub(r.enterPrecommitTime).String(),
PersistStateDurations: make(map[string][]string),
}
for k, v := range r.persistStateDurations {
for _, d := range v {
j.PersistStateDurations[k] = append(j.PersistStateDurations[k], d.String())
}
}
return j
}
func (r *roundMetrics) SetEnterNewRoundTime() {
r.enterNewRoundTime = time.Now()
}
func (r *roundMetrics) SetEnterProposalTime() {
r.enterProposalTime = time.Now()
}
func (r *roundMetrics) SetEnterPrevoteTime() {
r.enterPrevoteTime = time.Now()
}
func (r *roundMetrics) SetEnterPrecommitTime() {
r.enterPrecommitTime = time.Now()
}
func (r *roundMetrics) SetEnterCommitTime() {
r.enterCommitTime = time.Now()
}
func (r *roundMetrics) AppendPersistStateDuration(step string, d time.Duration) {
r.persistStateDurations[step] = append(r.persistStateDurations[step], d)
}
//
// heightMetrics
// @Description: The time of each stage of a certain height includes all rounds
//
type heightMetrics struct {
height uint64
enterNewHeightTime time.Time
rounds map[int32]*roundMetrics
}
//
// newHeightMetrics
// @Description: enter new height create a *heightMetrics
// @param height
// @return *heightMetrics
//
func newHeightMetrics(height uint64) *heightMetrics {
return &heightMetrics{
height: height,
rounds: make(map[int32]*roundMetrics),
}
}
//
// heightMetricsJson
// @Description: json format heightMetrics object
//
type heightMetricsJson struct {
Height uint64
EnterNewHeightTime string
Rounds map[int32]*roundMetricsJson
}
//
// String
// @Description: convert *heightMetrics to string
// @receiver h
// @return string
//
func (h *heightMetrics) String() string {
j := heightMetricsJson{
Height: h.height,
EnterNewHeightTime: h.enterNewHeightTime.String(),
Rounds: map[int32]*roundMetricsJson{},
}
for k, v := range h.rounds {
j.Rounds[k] = v.roundMetricsJson()
}
byt, _ := json.Marshal(j)
return string(byt)
}
//
// roundString
// @Description: convert a roundMetrics of heightMetrics to string
// @receiver h
// @param num
// @return string
//
func (h *heightMetrics) roundString(num int32) string {
j := heightMetricsJson{
Height: h.height,
EnterNewHeightTime: h.enterNewHeightTime.String(),
Rounds: map[int32]*roundMetricsJson{},
}
j.Rounds[num] = h.rounds[num].roundMetricsJson()
byt, _ := json.Marshal(j)
return string(byt)
}
func (h *heightMetrics) SetEnterNewHeightTime() {
h.enterNewHeightTime = time.Now()
}
func (h *heightMetrics) getRoundMertrics(round int32) *roundMetrics {
if _, ok := h.rounds[round]; !ok {
h.rounds[round] = newRoundMetrics(round)
}
return h.rounds[round]
}
func (h *heightMetrics) SetEnterNewRoundTime(round int32) {
r := h.getRoundMertrics(round)
r.SetEnterNewRoundTime()
}
func (h *heightMetrics) SetEnterProposalTime(round int32) {
r := h.getRoundMertrics(round)
r.SetEnterProposalTime()
}
func (h *heightMetrics) SetEnterPrevoteTime(round int32) {
r := h.getRoundMertrics(round)
r.SetEnterPrevoteTime()
}
func (h *heightMetrics) SetEnterPrecommitTime(round int32) {
r := h.getRoundMertrics(round)
r.SetEnterPrecommitTime()
}
func (h *heightMetrics) SetEnterCommitTime(round int32) {
r := h.getRoundMertrics(round)
r.SetEnterCommitTime()
}
func (h *heightMetrics) AppendPersistStateDuration(round int32, step string, d time.Duration) {
r := h.getRoundMertrics(round)
r.AppendPersistStateDuration(step, d)
}