-
Notifications
You must be signed in to change notification settings - Fork 365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
backend: list message concurrent stream send fix #1076
Changes from 3 commits
b285567
40a21dc
2764393
ac6eb61
3803b87
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ package console | |
|
||
import ( | ||
"context" | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
|
||
|
@@ -31,6 +32,8 @@ type streamProgressReporter struct { | |
|
||
messagesConsumed atomic.Int64 | ||
bytesConsumed atomic.Int64 | ||
|
||
writeMutex sync.Mutex | ||
} | ||
|
||
func (p *streamProgressReporter) Start() { | ||
|
@@ -44,41 +47,53 @@ func (p *streamProgressReporter) Start() { | |
// topic it may take some time until there are messages. This go routine is in charge of keeping the user up to | ||
// date about the progress Kowl made streaming the topic | ||
go func() { | ||
ticker := time.NewTicker(1 * time.Second) | ||
|
||
for { | ||
select { | ||
case <-p.ctx.Done(): | ||
ticker.Stop() | ||
return | ||
default: | ||
case <-ticker.C: | ||
p.reportProgress() | ||
} | ||
time.Sleep(1 * time.Second) | ||
} | ||
}() | ||
} | ||
|
||
func (p *streamProgressReporter) reportProgress() { | ||
p.writeMutex.Lock() | ||
defer p.writeMutex.Unlock() | ||
|
||
msg := &v1alpha.ListMessagesResponse_ProgressMessage{ | ||
MessagesConsumed: p.messagesConsumed.Load(), | ||
BytesConsumed: p.bytesConsumed.Load(), | ||
} | ||
|
||
p.stream.Send(&v1alpha.ListMessagesResponse{ | ||
if err := p.stream.Send(&v1alpha.ListMessagesResponse{ | ||
ControlMessage: &v1alpha.ListMessagesResponse_Progress{ | ||
Progress: msg, | ||
}, | ||
}) | ||
}); err != nil { | ||
p.logger.Error("send error in stream reportProgress", zap.Error(err)) | ||
} | ||
} | ||
|
||
func (p *streamProgressReporter) OnPhase(name string) { | ||
p.writeMutex.Lock() | ||
defer p.writeMutex.Unlock() | ||
|
||
msg := &v1alpha.ListMessagesResponse_PhaseMessage{ | ||
Phase: name, | ||
} | ||
|
||
p.stream.Send(&v1alpha.ListMessagesResponse{ | ||
if err := p.stream.Send(&v1alpha.ListMessagesResponse{ | ||
ControlMessage: &v1alpha.ListMessagesResponse_Phase{ | ||
Phase: msg, | ||
}, | ||
}) | ||
}); err != nil { | ||
p.logger.Error("send error in stream OnPhase", zap.Error(err)) | ||
} | ||
} | ||
|
||
func (p *streamProgressReporter) OnMessageConsumed(size int64) { | ||
|
@@ -87,6 +102,13 @@ func (p *streamProgressReporter) OnMessageConsumed(size int64) { | |
} | ||
|
||
func (p *streamProgressReporter) OnMessage(message *kafka.TopicMessage) { | ||
if message == nil { | ||
return | ||
} | ||
Comment on lines
+105
to
+107
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wondering why this is necessary? When would we send nil here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it was just a guard I added as I was debugging. we really shouldn't but might be a good idea. |
||
|
||
p.writeMutex.Lock() | ||
defer p.writeMutex.Unlock() | ||
|
||
headers := make([]*v1alpha.KafkaRecordHeader, 0, len(message.Headers)) | ||
|
||
for _, mh := range message.Headers { | ||
|
@@ -164,36 +186,48 @@ func (p *streamProgressReporter) OnMessage(message *kafka.TopicMessage) { | |
}) | ||
} | ||
|
||
p.stream.Send(&v1alpha.ListMessagesResponse{ | ||
if err := p.stream.Send(&v1alpha.ListMessagesResponse{ | ||
ControlMessage: &v1alpha.ListMessagesResponse_Data{ | ||
Data: data, | ||
}, | ||
}) | ||
}); err != nil { | ||
p.logger.Error("send error in stream OnMessage", zap.Error(err)) | ||
} | ||
} | ||
|
||
func (p *streamProgressReporter) OnComplete(elapsedMs int64, isCancelled bool) { | ||
p.writeMutex.Lock() | ||
defer p.writeMutex.Unlock() | ||
|
||
msg := &v1alpha.ListMessagesResponse_StreamCompletedMessage{ | ||
ElapsedMs: elapsedMs, | ||
IsCancelled: isCancelled, | ||
MessagesConsumed: p.messagesConsumed.Load(), | ||
BytesConsumed: p.bytesConsumed.Load(), | ||
} | ||
|
||
p.stream.Send(&v1alpha.ListMessagesResponse{ | ||
if err := p.stream.Send(&v1alpha.ListMessagesResponse{ | ||
ControlMessage: &v1alpha.ListMessagesResponse_Done{ | ||
Done: msg, | ||
}, | ||
}) | ||
}); err != nil { | ||
p.logger.Error("send error in stream OnComplete", zap.Error(err)) | ||
} | ||
} | ||
|
||
func (p *streamProgressReporter) OnError(message string) { | ||
p.writeMutex.Lock() | ||
defer p.writeMutex.Unlock() | ||
|
||
msg := &v1alpha.ListMessagesResponse_ErrorMessage{ | ||
Message: message, | ||
} | ||
|
||
p.stream.Send(&v1alpha.ListMessagesResponse{ | ||
if err := p.stream.Send(&v1alpha.ListMessagesResponse{ | ||
ControlMessage: &v1alpha.ListMessagesResponse_Error{ | ||
Error: msg, | ||
}, | ||
}) | ||
}); err != nil { | ||
p.logger.Error("send error in stream OnError", zap.Error(err)) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very good to add the reason there 👍