/
blockchain.go
353 lines (311 loc) · 8.63 KB
/
blockchain.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
// Copyright 2017 AMIS Technologies
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package metrics
import (
"context"
"fmt"
"math/big"
"sync"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/getamis/istanbul-tools/client"
"github.com/getamis/istanbul-tools/container"
)
type SnapshotStopper func()
type metricsManager struct {
registry *DefaultRegistry
SentTxCounter *Counter
TxErrCounter *Counter
ExcutedTxCounter *Counter
UnknownTxCounter *Counter
ReqMeter *Meter
RespMeter *Meter
TxLatencyTimer *Timer
BlockPeriodTimer *Timer
BlockLatencyTimer *Timer
}
func newMetricsManager() *metricsManager {
r := NewRegistry()
return &metricsManager{
registry: r,
SentTxCounter: r.NewCounter("tx/sent"),
TxErrCounter: r.NewCounter("tx/error"),
ExcutedTxCounter: r.NewCounter("tx/excuted"),
UnknownTxCounter: r.NewCounter("tx/unknown"),
ReqMeter: r.NewMeter("tx/rps"),
RespMeter: r.NewMeter("tx/tps/response"),
TxLatencyTimer: r.NewTimer("tx/latency"),
BlockPeriodTimer: r.NewTimer("block/period"),
BlockLatencyTimer: r.NewTimer("block/latency"),
}
}
func (m *metricsManager) Export() {
m.registry.Export()
}
func (m *metricsManager) SnapshotMeter(meter *Meter, name string, d time.Duration) SnapshotStopper {
mname := fmt.Sprintf("%s/%s", meter.Name(), name)
stop := make(chan struct{})
stopFn := func() {
fmt.Println("Stop snapshot", mname)
close(stop)
}
go func() {
ticker := time.NewTicker(d)
defer ticker.Stop()
for {
select {
case <-ticker.C:
snapshot := meter.Snapshot()
his := m.registry.NewHistogram(mname)
his.Update(int64(snapshot.Rate1()))
case <-stop:
return
}
}
}()
return stopFn
}
// --------------------------------------------------------------------------------------------------
type metricChain struct {
container.Blockchain
eths []container.Ethereum
headCh chan *ethtypes.Header
headSubs []ethereum.Subscription
txStartCh chan *txInfo
txDoneCh chan *txInfo
metricsMgr *metricsManager
wg sync.WaitGroup
quit chan struct{}
}
func NewMetricChain(blockchain container.Blockchain) container.Blockchain {
if blockchain == nil {
return nil
}
mc := &metricChain{
Blockchain: blockchain,
headCh: make(chan *ethtypes.Header, 1000),
txStartCh: make(chan *txInfo, 10000),
txDoneCh: make(chan *txInfo, 10000),
quit: make(chan struct{}),
metricsMgr: newMetricsManager(),
}
mc.eths = mc.getMetricEthereum(mc.Blockchain.Validators())
return mc
}
func (mc *metricChain) AddValidators(numOfValidators int) ([]container.Ethereum, error) {
vals, err := mc.Blockchain.AddValidators(numOfValidators)
if err != nil {
return nil, err
}
mc.eths = mc.getMetricEthereum(vals)
return mc.eths, nil
}
func (mc *metricChain) RemoveValidators(candidates []container.Ethereum, t time.Duration) error {
err := mc.Blockchain.RemoveValidators(candidates, t)
mc.eths = mc.getMetricEthereum(mc.Blockchain.Validators())
return err
}
func (mc *metricChain) Start(strong bool) error {
err := mc.Blockchain.Start(strong)
if err != nil {
return err
}
for _, eth := range mc.eths {
cli := eth.NewClient()
sub, err := cli.SubscribeNewHead(context.Background(), mc.headCh)
if err != nil {
log.Error("Failed to subscribe new head", "err", err)
return err
}
mc.headSubs = append(mc.headSubs, sub)
}
mc.wg.Add(2)
go mc.handleNewHeadEvent()
go mc.updateTxInfo()
return nil
}
func (mc *metricChain) Stop(strong bool) error {
close(mc.quit)
for _, sub := range mc.headSubs {
sub.Unsubscribe()
}
mc.wg.Wait()
mc.Export()
return mc.Blockchain.Stop(strong)
}
func (mc *metricChain) Validators() []container.Ethereum {
return mc.eths
}
func (mc *metricChain) getMetricEthereum(eths []container.Ethereum) []container.Ethereum {
meths := make([]container.Ethereum, len(eths))
for i, eth := range eths {
meths[i] = &metricEthereum{
Ethereum: eth,
txStartCh: mc.txStartCh,
metricsMgr: mc.metricsMgr,
}
}
return meths
}
func (mc *metricChain) handleNewHeadEvent() {
defer mc.wg.Done()
mutex := sync.Mutex{}
var preBlockTime = time.Now()
handledHeads := map[string]*ethtypes.Header{}
for {
select {
case header := <-mc.headCh:
now := time.Now()
go func(header *ethtypes.Header, now time.Time) {
log.Info("New head", "number", header.Number.Int64(), "hash", header.Hash().TerminalString(), "time", header.Time)
hash := header.Hash().String()
// lock hash first
var wasHandled bool
var preBlock *ethtypes.Header
mutex.Lock()
_, wasHandled = handledHeads[hash]
if !wasHandled {
handledHeads[hash] = header
}
preBlock, _ = handledHeads[header.ParentHash.String()]
mutex.Unlock()
if wasHandled {
return
}
var blockPeriod int64
if header.Number.Int64() > 2 && preBlock != nil {
blockPeriod = new(big.Int).Sub(header.Time, preBlock.Time).Int64()
mc.metricsMgr.BlockPeriodTimer.Update(time.Duration(blockPeriod) * time.Second)
}
mc.metricsMgr.BlockLatencyTimer.Update(now.Sub(preBlockTime))
preBlockTime = now
// get block
blockCh := make(chan *ethtypes.Block, len(mc.eths))
ctx, cancel := context.WithCancel(context.Background())
for _, eth := range mc.eths {
cli := eth.NewClient()
go getBlock(ctx, cli, header.Hash(), blockCh)
}
// wait for right block
var headBlock *ethtypes.Block
for i := 0; i < len(mc.eths); i++ {
headBlock = <-blockCh
if headBlock != nil {
break
}
}
// cancel other requests
cancel()
mc.metricsMgr.ExcutedTxCounter.Inc(int64(len(headBlock.Transactions())))
mc.metricsMgr.RespMeter.Mark(int64(len(headBlock.Transactions())))
// update tx info
for _, tx := range headBlock.Transactions() {
go func() {
mc.txDoneCh <- &txInfo{
Hash: tx.Hash().String(),
Time: now,
}
}()
}
}(header, now)
case <-mc.quit:
return
}
}
}
func (mc *metricChain) updateTxInfo() {
defer mc.wg.Done()
// TODO: the completed tx should be deleted from map
// given large space is workaround beacause the some problem between deleting and updating map
txStartMap := make(map[string]time.Time, 0)
txDoneMap := make(map[string]time.Time, 0)
defer func() {
// TODO: debug metric to check incomplete tx
// for _ = range txStartMap {
// mc.metricsMgr.UnknownTxCounter.Inc(1)
// }
// for _ = range txDoneMap {
// mc.metricsMgr.UnknownTxCounter.Inc(1)
// }
}()
updateTxStart := func(hash string, startTime time.Time) {
if done, ok := txDoneMap[hash]; ok {
mc.metricsMgr.TxLatencyTimer.Update(done.Sub(startTime))
return
//delete(txDoneMap, hash)
}
txStartMap[hash] = startTime
}
updateTxDone := func(hash string, doneTime time.Time) {
if start, ok := txStartMap[hash]; ok {
mc.metricsMgr.TxLatencyTimer.Update(doneTime.Sub(start))
return
//delete(txStartMap, hash)
}
txDoneMap[hash] = doneTime
}
for {
select {
case txStart := <-mc.txStartCh:
updateTxStart(txStart.Hash, txStart.Time)
case txDone := <-mc.txDoneCh:
updateTxDone(txDone.Hash, txDone.Time)
case <-mc.quit:
// clear tx start
TX_START:
for {
select {
case txStart := <-mc.txStartCh:
updateTxStart(txStart.Hash, txStart.Time)
default:
break TX_START
}
}
// clear tx done
TX_DONE:
for {
select {
case txDone := <-mc.txDoneCh:
updateTxDone(txDone.Hash, txDone.Time)
default:
break TX_DONE
}
}
return
}
}
}
func getBlock(ctx context.Context, cli client.Client, hash common.Hash, blockCh chan<- *ethtypes.Block) {
resp := make(chan *ethtypes.Block)
go func() {
block, err := cli.BlockByHash(ctx, hash)
if err != nil {
resp <- nil
}
resp <- block
}()
select {
case <-ctx.Done():
// Wait for client.BlockByHash
<-resp
// someone cancelled the request
blockCh <- nil
case r := <-resp:
blockCh <- r
}
}