Skip to content

Commit

Permalink
sink: fix concurrent map access in sink manager (#2249) (#2299)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Jul 19, 2021
1 parent e3cf5d1 commit 578a147
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 2 additions & 2 deletions cdc/sink/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func NewManager(ctx context.Context, backendSink Sink, errCh chan error, checkpo

// CreateTableSink creates a table sink
func (m *Manager) CreateTableSink(tableID model.TableID, checkpointTs model.Ts) Sink {
m.tableSinksMu.Lock()
defer m.tableSinksMu.Unlock()
if _, exist := m.tableSinks[tableID]; exist {
log.Panic("the table sink already exists", zap.Uint64("tableID", uint64(tableID)))
}
Expand All @@ -67,8 +69,6 @@ func (m *Manager) CreateTableSink(tableID model.TableID, checkpointTs model.Ts)
buffer: make([]*model.RowChangedEvent, 0, 128),
emittedTs: checkpointTs,
}
m.tableSinksMu.Lock()
defer m.tableSinksMu.Unlock()
m.tableSinks[tableID] = sink
return sink
}
Expand Down
8 changes: 7 additions & 1 deletion cdc/sink/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,14 @@ func (s *managerSuite) TestManagerRandom(c *check.C) {
var wg sync.WaitGroup
tableSinks := make([]Sink, goroutineNum)
for i := 0; i < goroutineNum; i++ {
tableSinks[i] = manager.CreateTableSink(model.TableID(i), 0)
i := i
wg.Add(1)
go func() {
defer wg.Done()
tableSinks[i] = manager.CreateTableSink(model.TableID(i), 0)
}()
}
wg.Wait()
for i := 0; i < goroutineNum; i++ {
i := i
tableSink := tableSinks[i]
Expand Down

0 comments on commit 578a147

Please sign in to comment.