-
Notifications
You must be signed in to change notification settings - Fork 3
/
verify.go
203 lines (184 loc) · 5.74 KB
/
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
package pulsarKit
import (
"context"
"fmt"
"github.com/apache/pulsar-client-go/pulsar"
"github.com/richelieu-yang/chimera/v2/src/core/errorKit"
"github.com/richelieu-yang/chimera/v2/src/core/fileKit"
"github.com/richelieu-yang/chimera/v2/src/core/pathKit"
"github.com/richelieu-yang/chimera/v2/src/core/sliceKit"
"github.com/richelieu-yang/chimera/v2/src/core/strKit"
"github.com/richelieu-yang/chimera/v2/src/core/timeKit"
"github.com/richelieu-yang/chimera/v2/src/idKit"
"github.com/richelieu-yang/chimera/v2/src/log/logrusKit"
"github.com/richelieu-yang/chimera/v2/src/operationKit"
"github.com/richelieu-yang/chimera/v2/src/randomKit"
"github.com/sirupsen/logrus"
"time"
)
const (
// connectTimeout 创建Consumer(或Producer)的超时时间
connectTimeout = time.Second * 10
// receiveTimeout 接受消息的超时时间
receiveTimeout = time.Second * 10
// sendTimeout 单次发送消息的超时时间
sendTimeout = time.Second
)
// verify 简单地验证 Pulsar服务 是否启动成功
/*
PS:
可能失败的原因:
(1)pulsar的进程在,但启动报错(存储空间爆了)
*/
func verify(verifyConfig *VerifyConfig) (err error) {
if verifyConfig == nil || strKit.IsBlank(verifyConfig.Topic) {
// 不验证
return nil
}
topic := verifyConfig.Topic
dir, _ := pathKit.GetUniqueTempDir()
timeStr := timeKit.FormatCurrentTime(timeKit.FormatFileName)
consumerLogPath := pathKit.Join(dir, fmt.Sprintf("pulsar_verify_consumer_%s.log", timeStr))
producerLogPath := pathKit.Join(dir, fmt.Sprintf("pulsar_verify_producer_%s.log", timeStr))
// 是否打印日志到控制台?
printFlag := verifyConfig.Print
level := operationKit.Ternary(printFlag, logrus.DebugLevel, logrus.PanicLevel)
cLogger := logrusKit.NewLogger(logrusKit.WithLevel(level))
cLogger.Infof("[Verify] consumerLogPath: [%s].", consumerLogPath)
cLogger.Infof("[Verify] producerLogPath: [%s].", producerLogPath)
defer func() {
if err == nil {
// 验证成功的情况下,删掉客户端日志文件
if err := fileKit.Remove(consumerLogPath); err != nil {
cLogger.WithFields(logrus.Fields{
"error": err.Error(),
}).Error("[Verify] fail to delete consumerLogPath")
} else {
cLogger.Info("[Verify] delete consumerLogPath")
}
if err := fileKit.Remove(producerLogPath); err != nil {
cLogger.WithFields(logrus.Fields{
"error": err.Error(),
}).Error("[Verify] fail to delete producerLogPath")
} else {
cLogger.Info("[Verify] delete producerLogPath")
}
}
}()
err = _verify(cLogger, topic, consumerLogPath, producerLogPath)
return
}
func _verify(logger *logrus.Logger, topic, consumerLogPath, producerLogPath string) error {
ctx0, cancel := context.WithTimeout(context.TODO(), connectTimeout)
defer cancel()
consumer, err := NewConsumer(ctx0, pulsar.ConsumerOptions{
Topic: topic,
SubscriptionName: fmt.Sprintf("verify_%s_%d", idKit.NewUUID(), randomKit.Int(0, 10000000)),
Type: pulsar.Exclusive,
}, consumerLogPath)
if err != nil {
return err
}
defer consumer.Close()
ctx1, cancel := context.WithTimeout(context.TODO(), connectTimeout)
defer cancel()
producer, err := NewProducer(ctx1, pulsar.ProducerOptions{
Topic: topic,
SendTimeout: sendTimeout,
}, producerLogPath)
if err != nil {
return err
}
defer producer.Close()
timeStr := timeKit.FormatCurrentTime()
ulid := idKit.NewULID()
texts := []string{
fmt.Sprintf("%s&&%s&&%s", ulid, timeStr, "$0"),
fmt.Sprintf("%s&&%s&&%s", ulid, timeStr, "$1"),
fmt.Sprintf("%s&&%s&&%s", ulid, timeStr, "$2"),
fmt.Sprintf("%s&&%s&&%s", ulid, timeStr, "$3"),
fmt.Sprintf("%s&&%s&&%s", ulid, timeStr, "$4"),
fmt.Sprintf("%s&&%s&&%s", ulid, timeStr, "$5"),
}
var ch = make(chan struct{}, 1)
var consumerErrCh = make(chan error, 1)
var producerErrCh = make(chan error, 1)
/* consumer */
consumerCtx, cancel := context.WithCancel(context.TODO())
defer cancel()
go func() {
defer func() {
logger.Info("[Pulsar, Verify, Consumer] goroutine ends")
}()
s := sliceKit.Copy(texts)
for {
msg, err := consumer.Receive(consumerCtx)
if err != nil {
logger.WithFields(logrus.Fields{
"error": err.Error(),
}).Info("[Pulsar, Verify, Consumer] fail to receive")
consumerErrCh <- err
break
}
if err := consumer.Ack(msg); err != nil {
logger.WithFields(logrus.Fields{
"error": err.Error(),
}).Info("[Pulsar, Verify, Consumer] fail to ack")
consumerErrCh <- err
break
}
var ok bool
text := string(msg.Payload())
s, ok = sliceKit.Remove(s, text)
left := len(s)
logger.WithFields(logrus.Fields{
"left": left,
"valid": ok,
"text": text,
}).Info("[Pulsar, Verify, Consumer] receive a message")
if ok && left == 0 {
logger.Info("[Pulsar, Verify, Consumer] receive all messages!")
ch <- struct{}{}
break
}
}
}()
/* producer */
go func() {
defer func() {
logger.Info("[Pulsar, Verify, Producer] goroutine ends")
}()
for _, text := range texts {
pMsg := &pulsar.ProducerMessage{
Payload: []byte(text),
}
err := func() error {
ctx, cancel := context.WithTimeout(context.TODO(), sendTimeout)
defer cancel()
_, err := producer.Send(ctx, pMsg)
return err
}()
if err != nil {
logger.WithFields(logrus.Fields{
"text": text,
"error": err.Error(),
}).Error("[Pulsar, Verify, Producer] fail to send")
producerErrCh <- err
break
}
logger.WithFields(logrus.Fields{
"text": text,
}).Info("[Pulsar, Verify, Producer] succeeded to send")
}
}()
select {
case <-ch:
return nil
case err := <-producerErrCh:
return err
case err := <-consumerErrCh:
return err
case <-time.After(receiveTimeout):
return errorKit.New("fail to get all messages within time limit(%s)", receiveTimeout)
}
}