-
Notifications
You must be signed in to change notification settings - Fork 199
/
miniBlocksPoolsCleaner.go
207 lines (171 loc) · 5.49 KB
/
miniBlocksPoolsCleaner.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
package poolsCleaner
import (
"context"
"fmt"
"sync"
"time"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go-core/core/closing"
"github.com/ElrondNetwork/elrond-go-core/data/block"
"github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/sharding"
"github.com/ElrondNetwork/elrond-go/storage"
)
var log = logger.GetOrCreate("process/block/poolsCleaner")
var _ closing.Closer = (*miniBlocksPoolsCleaner)(nil)
type mbInfo struct {
round int64
senderShardID uint32
receiverShardID uint32
mbType block.Type
}
// miniBlocksPoolsCleaner represents a pools cleaner that checks and cleans miniblocks which should not be in pool anymore
type miniBlocksPoolsCleaner struct {
miniblocksPool storage.Cacher
roundHandler process.RoundHandler
shardCoordinator sharding.Coordinator
mutMapMiniBlocksRounds sync.RWMutex
mapMiniBlocksRounds map[string]*mbInfo
cancelFunc func()
}
// NewMiniBlocksPoolsCleaner will return a new miniblocks pools cleaner
func NewMiniBlocksPoolsCleaner(
miniblocksPool storage.Cacher,
roundHandler process.RoundHandler,
shardCoordinator sharding.Coordinator,
) (*miniBlocksPoolsCleaner, error) {
if check.IfNil(miniblocksPool) {
return nil, process.ErrNilMiniBlockPool
}
if check.IfNil(roundHandler) {
return nil, process.ErrNilRoundHandler
}
if check.IfNil(shardCoordinator) {
return nil, process.ErrNilShardCoordinator
}
mbpc := miniBlocksPoolsCleaner{
miniblocksPool: miniblocksPool,
roundHandler: roundHandler,
shardCoordinator: shardCoordinator,
}
mbpc.mapMiniBlocksRounds = make(map[string]*mbInfo)
mbpc.miniblocksPool.RegisterHandler(mbpc.receivedMiniBlock, core.UniqueIdentifier())
return &mbpc, nil
}
// StartCleaning actually starts the pools cleaning mechanism
func (mbpc *miniBlocksPoolsCleaner) StartCleaning() {
var ctx context.Context
ctx, mbpc.cancelFunc = context.WithCancel(context.Background())
go mbpc.cleanMiniblocksPools(ctx)
}
func (mbpc *miniBlocksPoolsCleaner) cleanMiniblocksPools(ctx context.Context) {
for {
select {
case <-ctx.Done():
log.Debug("miniBlocksPoolsCleaner's go routine is stopping...")
return
case <-time.After(sleepTime):
}
startTime := time.Now()
numMiniblocksInMap := mbpc.cleanMiniblocksPoolsIfNeeded()
elapsedTime := time.Since(startTime)
log.Debug("miniBlocksPoolsCleaner.cleanMiniblocksPools",
"num miniblocks in map", numMiniblocksInMap,
"elapsed time", elapsedTime)
}
}
func (mbpc *miniBlocksPoolsCleaner) receivedMiniBlock(key []byte, value interface{}) {
if key == nil {
return
}
miniBlock, ok := value.(*block.MiniBlock)
if !ok {
log.Warn("miniBlocksPoolsCleaner.receivedMiniBlock",
"error", process.ErrWrongTypeAssertion,
"found type", fmt.Sprintf("%T", value),
)
return
}
log.Trace("miniBlocksPoolsCleaner.receivedMiniBlock", "hash", key)
mbpc.mutMapMiniBlocksRounds.Lock()
defer mbpc.mutMapMiniBlocksRounds.Unlock()
if _, found := mbpc.mapMiniBlocksRounds[string(key)]; !found {
receivedMbInfo := &mbInfo{
round: mbpc.roundHandler.Index(),
senderShardID: miniBlock.SenderShardID,
receiverShardID: miniBlock.ReceiverShardID,
mbType: miniBlock.Type,
}
mbpc.mapMiniBlocksRounds[string(key)] = receivedMbInfo
log.Trace("miniblock has been added",
"hash", key,
"round", receivedMbInfo.round,
"sender", receivedMbInfo.senderShardID,
"receiver", receivedMbInfo.receiverShardID,
"type", receivedMbInfo.mbType)
}
}
func (mbpc *miniBlocksPoolsCleaner) cleanMiniblocksPoolsIfNeeded() int {
numMbsCleaned := 0
hashesToRemove := make(map[string]struct{})
mbpc.mutMapMiniBlocksRounds.Lock()
for hash, mbi := range mbpc.mapMiniBlocksRounds {
_, ok := mbpc.miniblocksPool.Get([]byte(hash))
if !ok {
log.Trace("miniblock not found in pool",
"hash", []byte(hash),
"round", mbi.round,
"sender", mbi.senderShardID,
"receiver", mbi.receiverShardID,
"type", mbi.mbType)
delete(mbpc.mapMiniBlocksRounds, hash)
continue
}
roundDif := mbpc.roundHandler.Index() - mbi.round
if roundDif <= process.MaxRoundsToKeepUnprocessedMiniBlocks {
log.Trace("cleaning miniblock not yet allowed",
"hash", []byte(hash),
"round", mbi.round,
"round dif", roundDif,
"sender", mbi.senderShardID,
"receiver", mbi.receiverShardID,
"type", mbi.mbType)
continue
}
hashesToRemove[hash] = struct{}{}
delete(mbpc.mapMiniBlocksRounds, hash)
numMbsCleaned++
log.Trace("miniblock has been cleaned",
"hash", []byte(hash),
"round", mbi.round,
"sender", mbi.senderShardID,
"receiver", mbi.receiverShardID,
"type", mbi.mbType)
}
numMiniBlocksRounds := len(mbpc.mapMiniBlocksRounds)
mbpc.mutMapMiniBlocksRounds.Unlock()
startTime := time.Now()
for hash := range hashesToRemove {
mbpc.miniblocksPool.Remove([]byte(hash))
}
elapsedTime := time.Since(startTime)
if numMbsCleaned > 0 {
log.Debug("miniBlocksPoolsCleaner.cleanMiniblocksPoolsIfNeeded",
"num mbs cleaned", numMbsCleaned,
"elapsed time to remove mbs from cacher", elapsedTime)
}
return numMiniBlocksRounds
}
// Close will close the endless running go routine
func (mbpc *miniBlocksPoolsCleaner) Close() error {
if mbpc.cancelFunc != nil {
mbpc.cancelFunc()
}
return nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (mbpc *miniBlocksPoolsCleaner) IsInterfaceNil() bool {
return mbpc == nil
}