Skip to content
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

sink(ticdc): asynchronously close the mq producers #5186

Merged
merged 15 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cdc/owner/ddl_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,5 +264,8 @@ func (s *ddlSinkImpl) close(ctx context.Context) (err error) {
err = s.syncPointStore.Close()
}
s.wg.Wait()
return err
if err != nil && errors.Cause(err) != context.Canceled {
return err
}
return nil
}
4 changes: 2 additions & 2 deletions cdc/processor/pipeline/table_actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func NewTableActor(cdcCtx cdcContext.Context,

startTime := time.Now()
log.Info("table actor starting",
zap.String("changefeed", changefeedVars.ID),
zap.String("changefeed", table.changefeedID),
zap.String("tableName", tableName),
zap.Int64("tableID", tableID))
if err := table.start(cctx); err != nil {
Expand All @@ -152,7 +152,7 @@ func NewTableActor(cdcCtx cdcContext.Context,
return nil, errors.Trace(err)
}
log.Info("table actor started",
zap.String("changefeed", changefeedVars.ID),
zap.String("changefeed", table.changefeedID),
zap.String("tableName", tableName),
zap.Int64("tableID", tableID),
zap.Duration("duration", time.Since(startTime)))
Expand Down
13 changes: 3 additions & 10 deletions cdc/sink/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"context"
"sync"
"sync/atomic"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/log"
Expand Down Expand Up @@ -83,18 +82,12 @@ func (m *Manager) Close(ctx context.Context) error {
defer m.tableSinksMu.Unlock()
tableSinkTotalRowsCountCounter.DeleteLabelValues(m.changefeedID)
if m.bufSink != nil {
log.Info("sinkManager try close bufSink",
zap.String("changefeed", m.changefeedID))
start := time.Now()
if err := m.bufSink.Close(ctx); err != nil {
log.Info("close bufSink failed",
if err := m.bufSink.Close(ctx); err != nil && errors.Cause(err) != context.Canceled {
log.Warn("close bufSink failed",
zap.String("changefeed", m.changefeedID),
zap.Duration("duration", time.Since(start)))
zap.Error(err))
return err
}
log.Info("close bufSink success",
zap.String("changefeed", m.changefeedID),
zap.Duration("duration", time.Since(start)))
}
return nil
}
Expand Down
9 changes: 7 additions & 2 deletions cdc/sink/mq.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,13 @@ func (k *mqSink) EmitDDLEvent(ctx context.Context, ddl *model.DDLEvent) error {
}

func (k *mqSink) Close(ctx context.Context) error {
err := k.mqProducer.Close()
return errors.Trace(err)
closedCh := k.mqProducer.AsyncClose()
select {
case <-ctx.Done():
return errors.Trace(ctx.Err())
case err := <-closedCh:
return errors.Trace(err)
}
}

func (k *mqSink) Barrier(cxt context.Context, tableID model.TableID) error {
Expand Down
4 changes: 4 additions & 0 deletions cdc/sink/mq_flush_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ func (m *mockProducer) Close() error {
panic("Not used")
}

func (m *mockProducer) AsyncClose() chan error {
panic("Not used")
}

func (m *mockProducer) InjectError(err error) {
m.mockErr <- err
}
Expand Down
10 changes: 10 additions & 0 deletions cdc/sink/producer/kafka/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,16 @@ func (k *kafkaSaramaProducer) stop() {
close(k.closeCh)
}

// AsyncClose close the produce in an asynchronous way.
func (k *kafkaSaramaProducer) AsyncClose() chan error {
ret := make(chan error, 1)
go func() {
ret <- k.Close()
close(ret)
}()
return ret
}

// Close closes the sync and async clients.
func (k *kafkaSaramaProducer) Close() error {
log.Info("stop the kafka producer", zap.String("changefeed", k.id), zap.Any("role", k.role))
Expand Down
2 changes: 2 additions & 0 deletions cdc/sink/producer/mq_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ type Producer interface {
Flush(ctx context.Context) error
// Close closes the producer and client(s).
Close() error
// AsyncClose close the produce in an asynchronous way.
AsyncClose() chan error
}
10 changes: 10 additions & 0 deletions cdc/sink/producer/pulsar/producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,13 @@ func (p *Producer) Close() error {
p.client.Close()
return nil
}

// AsyncClose close the produce in an asynchronous way.
func (p *Producer) AsyncClose() chan error {
ret := make(chan error, 1)
go func() {
ret <- p.Close()
close(ret)
}()
return ret
}