-
Notifications
You must be signed in to change notification settings - Fork 39
/
approver.go
178 lines (155 loc) · 5.21 KB
/
approver.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
package approver
import (
"context"
"sync"
"sync/atomic"
"time"
"github.com/bnb-chain/greenfield-storage-provider/base/gfspapp"
"github.com/bnb-chain/greenfield-storage-provider/core/module"
"github.com/bnb-chain/greenfield-storage-provider/core/rcmgr"
"github.com/bnb-chain/greenfield-storage-provider/core/task"
"github.com/bnb-chain/greenfield-storage-provider/core/taskqueue"
"github.com/bnb-chain/greenfield-storage-provider/pkg/log"
)
const (
DefaultBlockInterval = 3
DefaultApprovalExpiredTimeout = int64(DefaultBlockInterval * 20)
)
var _ module.Approver = &ApprovalModular{}
type managerTasksStats struct {
uploadTaskCount uint32
replicateTaskCount uint32
sealTaskCount uint32
resumableUploadTaskCount uint32
maxUploadingCount uint32
migrateGVGCount uint32
recoveryProcessCount uint32
recoveryFailedList []string
}
func (s *managerTasksStats) totalUploadTasks() uint32 {
return s.uploadTaskCount + s.replicateTaskCount + s.sealTaskCount + s.resumableUploadTaskCount
}
type ApprovalModular struct {
baseApp *gfspapp.GfSpBaseApp
scope rcmgr.ResourceScope
bucketQueue taskqueue.TQueueOnStrategy
objectQueue taskqueue.TQueueOnStrategy
currentBlockHeight uint64
// defines the max bucket number per account, approver refuses the ask approval
// request if account own the bucket number greater the value
accountBucketNumber int64
// defines the creation of bucket/object approval timeout height the approval
// expired height equal to current block height + timeout height
bucketApprovalTimeoutHeight uint64
objectApprovalTimeoutHeight uint64
// the maximum number of GVGs migrating to current SP concurrently is allowed
migrateGVGLimit int
statsMutex sync.RWMutex
tasksStats *managerTasksStats
spID uint32
}
func (a *ApprovalModular) Name() string {
return module.ApprovalModularName
}
func (a *ApprovalModular) Start(ctx context.Context) error {
a.bucketQueue.SetRetireTaskStrategy(a.GCApprovalQueue)
a.objectQueue.SetRetireTaskStrategy(a.GCApprovalQueue)
scope, err := a.baseApp.ResourceManager().OpenService(a.Name())
if err != nil {
return err
}
a.scope = scope
a.tasksStats = &managerTasksStats{}
go a.eventLoop(ctx)
return nil
}
func (a *ApprovalModular) Stop(ctx context.Context) error {
a.scope.Release()
return nil
}
func (a *ApprovalModular) ReserveResource(ctx context.Context, state *rcmgr.ScopeStat) (rcmgr.ResourceScopeSpan, error) {
span, err := a.scope.BeginSpan()
if err != nil {
return nil, err
}
err = span.ReserveResources(state)
if err != nil {
return nil, err
}
return span, nil
}
func (a *ApprovalModular) ReleaseResource(ctx context.Context, span rcmgr.ResourceScopeSpan) {
span.Done()
}
func (a *ApprovalModular) eventLoop(ctx context.Context) {
getCurrentBlockHeightTicker := time.NewTicker(time.Duration(DefaultBlockInterval) * time.Second)
updateManagerTasksStatsTicket := time.NewTicker(time.Duration(DefaultBlockInterval) * time.Second)
for {
select {
case <-ctx.Done():
return
case <-getCurrentBlockHeightTicker.C:
current, err := a.baseApp.Consensus().CurrentHeight(context.Background())
if err != nil {
log.CtxErrorw(ctx, "failed to get current block number", "error", err)
current = atomic.LoadUint64(&a.currentBlockHeight) + 1
}
a.SetCurrentBlockHeight(current)
case <-updateManagerTasksStatsTicket.C:
stats, err := a.baseApp.GfSpClient().GetTasksStats(context.Background())
if err == nil {
a.statsMutex.Lock()
a.tasksStats = &managerTasksStats{
stats.GetUploadCount(),
stats.GetReplicateCount(),
stats.GetSealCount(),
stats.GetResumableUploadCount(),
stats.GetMaxUploading(),
stats.GetMigrateGvgCount(),
stats.GetRecoveryProcessCount(),
stats.GetRecoveryFailedList(),
}
a.statsMutex.Unlock()
}
}
}
}
// GCApprovalQueue defines the strategy of gc approval queue when the queue is full.
// if the approval is expired, it can be deleted.
func (a *ApprovalModular) GCApprovalQueue(qTask task.Task) bool {
approvalTask := qTask.(task.ApprovalTask)
if approvalTask.GetCreateTime()+DefaultApprovalExpiredTimeout < time.Now().Unix() {
log.Debugw("expire approval task", "info", approvalTask.Info())
return true
}
log.Debugw("approval task not expired", "expired_height", approvalTask.GetExpiredHeight(),
"current_height", a.GetCurrentBlockHeight())
return false
}
func (a *ApprovalModular) GetCurrentBlockHeight() uint64 {
return atomic.LoadUint64(&a.currentBlockHeight)
}
func (a *ApprovalModular) SetCurrentBlockHeight(height uint64) {
atomic.StoreUint64(&a.currentBlockHeight, height)
}
func (a *ApprovalModular) getSPID() (uint32, error) {
if a.spID != 0 {
return a.spID, nil
}
spInfo, err := a.baseApp.Consensus().QuerySP(context.Background(), a.baseApp.OperatorAddress())
if err != nil {
return 0, err
}
a.spID = spInfo.GetId()
return a.spID, nil
}
func (a *ApprovalModular) exceedCreateObjectLimit() bool {
a.statsMutex.RLock()
defer a.statsMutex.RUnlock()
return a.tasksStats.totalUploadTasks() >= a.tasksStats.maxUploadingCount
}
func (a *ApprovalModular) exceedMigrateGVGLimit() bool {
a.statsMutex.RLock()
defer a.statsMutex.RUnlock()
return a.tasksStats.migrateGVGCount >= uint32(a.migrateGVGLimit)
}