/
expect.go
263 lines (241 loc) · 5.83 KB
/
expect.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
package twelve
import (
"fmt"
"github.com/hootuu/utils/errors"
"github.com/hootuu/utils/sys"
"go.uber.org/zap"
"sync"
"sync/atomic"
"time"
)
type ExpectState int
const (
ExpectInit ExpectState = 0
ExpectWaiting ExpectState = 1
ExpectFinished ExpectState = 2
ExpectTimeout ExpectState = -1
ExpectCanceled ExpectState = -2
)
const (
DefaultExpectVoteCount = 12
DefaultExpectVoteCountRd = 12
DefaultExpectTimeout = 3600 * time.Second
)
type Expect struct {
ID string `json:"id"`
Expect uint32 `json:"expect"`
counter uint32
done chan ExpectState
state ExpectState
timestamp time.Time
replier sync.Map
lock sync.Mutex
}
func ExpectID(peerID string, hash string, forArrow Arrow) string {
return fmt.Sprintf("%s_%s_%d", peerID, hash, forArrow)
}
func NewExpect(id string, expect int) *Expect {
e := &Expect{
ID: id,
Expect: uint32(expect),
counter: 0,
done: make(chan ExpectState),
state: ExpectInit,
timestamp: time.Now(),
}
return e
}
func (e *Expect) IsEnd() bool {
switch e.state {
case ExpectFinished, ExpectTimeout, ExpectCanceled:
return true
}
return false
}
func (e *Expect) SetState(s ExpectState) {
e.lock.Lock()
defer e.lock.Unlock()
e.state = s
e.timestamp = time.Now()
//if sys.RunMode.IsRd() {
// gLogger.Info("expect state change", zap.Int("target", int(s)),
// zap.String("id", e.ID))
//}
}
func (e *Expect) Waiting(doFunc func() *errors.Error, onFunc func()) bool {
//if sys.RunMode.IsRd() {
// fmt.Println("waiting for: ", zap.String("id", e.ID),
// zap.Uint32("expect", e.Expect))
//}
//go func() {
// e.SetState(ExpectWaiting)
// var newCounter = e.counter
// doneResult := ExpectInit
// for {
// end := false
// //if sys.RunMode.IsRd() {
// // gLogger.Info("continue waiting for: ", zap.String("id", e.ID),
// // zap.Uint32("expect", e.Expect), zap.Uint32("counter", newCounter))
// //}
// select {
// case <-e.waitGroup:
// newCounter = atomic.AddUint32(&e.counter, 1)
// //if sys.RunMode.IsRd() {
// // gLogger.Info("done waiting for[get reply]: ", zap.String("id", e.ID),
// // zap.Uint32("expect", e.Expect), zap.Uint32("counter", newCounter))
// //}
// if newCounter >= e.Expect {
// end = true
// doneResult = ExpectFinished
// }
// case result := <-e.done:
// end = true
// doneResult = result
// }
// if end {
// break
// }
// }
// e.confirmDone <- doneResult
//}()
err := doFunc()
if err != nil {
gLogger.Error("expect.doFunc failed",
zap.String("id", e.ID),
zap.Error(err))
e.doCancel()
return false
}
select {
case result := <-e.done:
fmt.Println("when result == E->", ExpectFinished, "<- vs ", result)
success := false
switch result {
case ExpectFinished:
onFunc()
e.SetState(ExpectFinished)
success = true
case ExpectCanceled:
e.SetState(ExpectCanceled)
success = false
case ExpectTimeout:
e.SetState(ExpectTimeout)
success = false
}
return success
case <-time.After(DefaultExpectTimeout):
e.SetState(ExpectTimeout)
return false
}
}
func (e *Expect) Reply(peerID string) {
if sys.RunMode.IsRd() {
gLogger.Info("expect.Reply", zap.String("peerID", peerID),
zap.String("id", e.ID),
zap.Uint32("count", e.counter))
}
_, ok := e.replier.Load(peerID)
if ok {
gLogger.Warn("repeated peerID", zap.String("peerID", peerID))
return
}
e.replier.Store(peerID, struct{}{})
if e.IsEnd() {
return
}
newCounter := atomic.AddUint32(&e.counter, 1)
fmt.Println("newCounter==========>>>>>>>>>>>", newCounter, "/", e.Expect)
if newCounter == e.Expect {
//if _, ok := <-e.done; !ok {
// fmt.Println("通道已关闭=================>>>>>>>>>>>>>>>>>>>")
//} else {
fmt.Println("GO FINISHED=================>>>>>>>>>>>>>>>>>>>")
e.done <- ExpectFinished
//}
}
}
func (e *Expect) doCancel() {
e.done <- ExpectCanceled
}
func (e *Expect) doClose() {
//close(e.waitGroup)
close(e.done)
//close(e.confirmDone)
}
type ExpectFactory struct {
db sync.Map
lock sync.Mutex
}
func NewExpectFactory() *ExpectFactory {
return &ExpectFactory{}
}
func (ef *ExpectFactory) GetOrBuild(peerID string, hash string, forArrow Arrow, expect int) (*Expect, bool, *errors.Error) {
ef.lock.Lock()
defer ef.lock.Unlock()
id := ExpectID(peerID, hash, forArrow)
val, ok := ef.db.Load(id)
if ok {
if e, ok := val.(*Expect); ok {
return e, false, nil
}
gLogger.Error("ExpectFactory.GetOrBuild: it is not the Expect", zap.String("hash", hash))
return nil, false, errors.Sys("Must Be Expect")
}
e := NewExpect(id, expect)
ef.db.Store(id, e)
return e, true, nil
}
func (ef *ExpectFactory) Build(peerID string, hash string, forArrow Arrow, expect int) *Expect {
ef.lock.Lock()
defer ef.lock.Unlock()
id := ExpectID(peerID, hash, forArrow)
val, ok := ef.db.Load(id)
if ok {
if e, ok := val.(*Expect); ok {
return e
}
return nil //todo
}
e := NewExpect(id, expect)
ef.db.Store(id, e)
return e
}
func (ef *ExpectFactory) MustGet(peerID string, hash string, forArrow Arrow) (*Expect, *errors.Error) {
id := ExpectID(peerID, hash, forArrow)
val, ok := ef.db.Load(id)
if ok {
if e, ok := val.(*Expect); ok {
return e, nil
}
return nil, errors.Sys("invalid expect") //todo
}
return nil, errors.Sys("the expect not exists or has been destroyed")
}
func (ef *ExpectFactory) StartGC() {
go func() {
for {
time.Sleep(60 * time.Second)
var count int64 = 0
var needRm []string
ef.db.Range(func(key, value any) bool {
count += 1
id := key.(string)
e := value.(*Expect)
if e.IsEnd() {
return true
}
needRm = append(needRm, id)
return true
})
if len(needRm) > 0 {
for _, id := range needRm {
ef.db.Delete(id)
if sys.RunMode.IsRd() {
gLogger.Info("remove expect: ", zap.String("id", id))
}
}
}
fmt.Println("the EF length:==><==", count)
}
}()
}