Skip to content

Commit

Permalink
perfschema: fixed concurrent map bug (pingcap#1160)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicole Nie authored and shenli committed Apr 26, 2016
1 parent bd2b356 commit 36ef582
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion perfschema/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ var setupTimersRecords [][]types.Datum
func (ps *perfSchema) initialize() (err error) {
ps.tables = make(map[string]*model.TableInfo)
ps.mTables = make(map[string]table.Table, len(ps.tables))
ps.stmtHandles = make(map[uint64]int64)
ps.stmtHandles = make([]int64, currentElemMax)

allColDefs := [][]columnInfo{
setupActorsCols,
Expand Down
2 changes: 1 addition & 1 deletion perfschema/perfschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type perfSchema struct {
dbInfo *model.DBInfo
tables map[string]*model.TableInfo
mTables map[string]table.Table // Memory tables for perfSchema
stmtHandles map[uint64]int64
stmtHandles []int64
}

var _ PerfSchema = (*perfSchema)(nil)
Expand Down
8 changes: 5 additions & 3 deletions perfschema/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"fmt"
"reflect"
"runtime"
"sync/atomic"
"time"

"github.com/juju/errors"
Expand Down Expand Up @@ -253,13 +254,14 @@ func (ps *perfSchema) updateEventsStmtsCurrent(connID uint64, record []types.Dat
if tbl == nil {
return nil
}
handle, ok := ps.stmtHandles[connID]
if !ok {
index := connID % uint64(currentElemMax)
handle := atomic.LoadInt64(&ps.stmtHandles[index])
if handle == 0 {
newHandle, err := tbl.AddRecord(nil, record)
if err != nil {
return errors.Trace(err)
}
ps.stmtHandles[connID] = newHandle
atomic.StoreInt64(&ps.stmtHandles[index], newHandle)
return nil
}
err := tbl.UpdateRecord(nil, handle, nil, record, nil)
Expand Down
6 changes: 5 additions & 1 deletion table/tables/bounded_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ const (
invalidRecordID int64 = 0
)

var (
errOpNotSupported = errors.New("Operation not supported")
)

type boundedItem struct {
handle int64
data []types.Datum
Expand Down Expand Up @@ -263,7 +267,7 @@ func (t *BoundedTable) LockRow(ctx context.Context, h int64, forRead bool) error
// RemoveRecord implements table.Table RemoveRecord interface.
func (t *BoundedTable) RemoveRecord(ctx context.Context, h int64, r []types.Datum) error {
// not supported, BoundedTable is TRUNCATE only
return nil
return errOpNotSupported
}

// AllocAutoID implements table.Table AllocAutoID interface.
Expand Down

0 comments on commit 36ef582

Please sign in to comment.