-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup_verify.go
245 lines (223 loc) · 6.56 KB
/
setup_verify.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
package rocketmq5Kit
import (
"context"
"fmt"
rmq_client "github.com/apache/rocketmq-clients/golang/v5"
"github.com/apache/rocketmq-clients/golang/v5/protocol/v2"
"github.com/richelieu-yang/chimera/v3/src/core/errorKit"
"github.com/richelieu-yang/chimera/v3/src/core/sliceKit"
"github.com/richelieu-yang/chimera/v3/src/core/strKit"
"github.com/richelieu-yang/chimera/v3/src/file/fileKit"
"github.com/richelieu-yang/chimera/v3/src/idKit"
"github.com/richelieu-yang/chimera/v3/src/log/logrusKit"
"github.com/richelieu-yang/chimera/v3/src/serialize/json/jsonKit"
"github.com/richelieu-yang/chimera/v3/src/time/timeKit"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"io"
"os"
"time"
)
const (
verifySendTimeout = time.Millisecond * 300
// verifyTimeLimit verify操作的最长时间,超过就失败
verifyTimeLimit = time.Second * 3
)
type VerifyConfig struct {
// Topic 用于验证的topic(理论上,此topic仅用于测试,不能同时用于业务,因为测试发的消息无意义).
/*
PS: 为空则不进行验证.
*/
Topic string
// LogPath 用于测试的日志文件路径(如果为空,则输出到控制台).
LogPath string
}
// verify 测试RocketMQ5服务是否正常工作.
/*
@param endpoint 用于测试的RocketMQ5服务的endpoint
@param topic 用于测试的topic(理论上,此topic仅用于测试,不能同时用于业务,因为测试发的消息无意义)
@return 如果为nil,说明 RocketMQ5服务 正常启动
*/
func verify(config *VerifyConfig) error {
if config == nil || strKit.IsEmpty(config.Topic) {
// 不进行验证
return nil
}
/* logger */
var output io.Writer
if strKit.IsEmpty(config.LogPath) {
output = os.Stderr
} else {
if err := fileKit.AssertExistAndIsFile(config.LogPath); err != nil {
return err
}
var err error
output, err = fileKit.CreateInAppendMode(config.LogPath)
if err != nil {
return err
}
}
logger := logrusKit.NewLogger(logrusKit.WithOutput(output),
logrusKit.WithLevel(logrus.DebugLevel),
logrusKit.WithDisableQuote(true),
logrusKit.WithMsgPrefix("[RocketMQ5 VERIFY] "),
)
topic := config.Topic
tag := idKit.NewXid()
consumerGroup := tag
logger.Infof("topic: [%s]", topic)
logger.Infof("tag: [%s]", tag)
logger.Infof("consumer group: [%s]", consumerGroup)
/* texts */
timeStr := timeKit.FormatCurrent(timeKit.FormatEntire)
texts := []string{
fmt.Sprintf("%s_%s", timeStr, "$0"),
fmt.Sprintf("%s_%s", timeStr, "$1"),
fmt.Sprintf("%s_%s", timeStr, "$2"),
fmt.Sprintf("%s_%s", timeStr, "$3"),
fmt.Sprintf("%s_%s", timeStr, "$4"),
fmt.Sprintf("%s_%s", timeStr, "$5"),
}
json, err := jsonKit.MarshalIndentToString(texts, "", " ")
if err != nil {
return err
}
logger.Infof("texts:\n%s\n.", json)
/* (1) producer */
producer, err := NewProducer()
if err != nil {
return err
}
defer producer.GracefulStop()
/* (2) consumer */
consumer, err := NewSimpleConsumer(consumerGroup, map[string]*rmq_client.FilterExpression{
//topic: rmq_client.SUB_ALL,
topic: rmq_client.NewFilterExpression(tag),
})
if err != nil {
return err
}
defer consumer.GracefulStop()
ctx, cancel := context.WithTimeout(context.TODO(), verifyTimeLimit)
defer cancel()
var producerCh = make(chan error, 1)
var consumerCh = make(chan error, 1)
/* (3) producer goroutine */
go func() {
defer func() {
logger.Info("Producer goroutine ends.")
}()
for _, text := range texts {
msg := &rmq_client.Message{
Topic: topic,
Body: []byte(text),
Tag: &tag,
}
ctx, _ := context.WithTimeout(context.TODO(), verifySendTimeout)
_, err := producer.Send(ctx, msg)
if err != nil {
err = errorKit.Wrapf(err, "Producer fails to send message(%s).", text)
producerCh <- err
return
}
logger.WithFields(logrus.Fields{
"text": text,
}).Info("Producer managers to send a message.")
}
logger.Info("Producer managers to send all messages.")
}()
/* (4) consumer goroutine */
textsCopy := sliceKit.Copy(texts)
go func(texts []string) {
defer func() {
logger.Info("Consumer goroutine ends.")
}()
for {
select {
case <-ctx.Done():
consumerCh <- errorKit.Newf("Consumer fails to receive all messages within timeout(%s).", verifyTimeLimit)
return
case <-time.After(time.Millisecond * 100):
// do nothing
}
//time.Sleep(time.Millisecond * 100)
mvs, err := consumer.Receive(context.TODO(), DefaultMaxMessageNum, DefaultInvisibleDuration)
if err != nil {
/* gRPC errors */
if s, ok := status.FromError(err); ok {
switch s.Code() {
case codes.Canceled:
consumerCh <- err
return
//case codes.Canceled:
// /* 提前结束(被取消) */
// break LOOP
//case codes.DeadlineExceeded:
// /* 超时结束 */
// consumerErr = errorKit.Newf("consumer fails to receive all messages(count: %d) within timeout(%s), missing(%d)", len(texts), verifyTimeLimit.String(), len(text1))
// break LOOP
}
}
if errRpcStatus, ok := rmq_client.AsErrRpcStatus(err); ok {
/* RocketMQ5 errors */
switch errRpcStatus.Code {
case int32(v2.Code_MESSAGE_NOT_FOUND):
// 没有新消息
default:
logger.WithFields(logrus.Fields{
"code": errRpcStatus.Code,
"error": err.Error(),
}).Warn("Consumer fails to receive.")
}
} else {
/* other errors */
logger.WithFields(logrus.Fields{
"error": err.Error(),
}).Warn("Consumer fails to receive.")
}
continue
}
// ack message
for _, mv := range mvs {
text := string(mv.GetBody())
err := consumer.Ack(context.TODO(), mv)
if err != nil {
logger.WithFields(logrus.Fields{
"tag": GetTagString(mv.GetTag()),
"text": text,
"error": err.Error(),
}).Error("Consumer fails to ack message.")
continue
}
var ok bool
texts, ok = sliceKit.Remove(texts, text)
left := len(texts)
logger.WithFields(logrus.Fields{
"valid": ok,
"left": left,
"tag": GetTagString(mv.GetTag()),
"text": text,
}).Info("Consumer managers to receive and ack a message.")
if left == 0 {
// 成功收到所有预期消息
logger.Info("Consumer managers to receive and ack all messages.")
consumerCh <- nil
return
}
}
}
}(textsCopy)
select {
case producerErr := <-producerCh:
return producerErr
case consumerCh := <-consumerCh:
if consumerCh != nil {
return consumerCh
}
// 通过验证
return nil
case <-ctx.Done():
return errorKit.Newf("Fail to pass verification within timeout(%s).", verifyTimeLimit)
}
}