Skip to content

Commit f83a13b

Browse files
tiancaiamaojackysp
authored andcommitted
*: add trace support for the AllocAutoIncrementValue function (#11158) (#11230)
1 parent 14d29c3 commit f83a13b

File tree

9 files changed

+33
-39
lines changed

9 files changed

+33
-39
lines changed

executor/executor_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ func checkCases(tests []testCase, ld *executor.LoadDataInfo,
352352
ctx.GetSessionVars().StmtCtx.DupKeyAsWarning = true
353353
ctx.GetSessionVars().StmtCtx.BadNullAsWarning = true
354354
ctx.GetSessionVars().StmtCtx.InLoadDataStmt = true
355-
data, reachLimit, err1 := ld.InsertData(tt.data1, tt.data2)
355+
data, reachLimit, err1 := ld.InsertData(context.Background(), tt.data1, tt.data2)
356356
c.Assert(err1, IsNil)
357357
c.Assert(reachLimit, IsFalse)
358358
if tt.restData == nil {

executor/insert_common.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func (e *InsertValues) insertRows(ctx context.Context, exec func(ctx context.Con
186186
rows := make([][]types.Datum, 0, len(e.Lists))
187187
for i, list := range e.Lists {
188188
e.rowCount++
189-
row, err := e.evalRow(list, i)
189+
row, err := e.evalRow(ctx, list, i)
190190
if err != nil {
191191
return err
192192
}
@@ -228,7 +228,7 @@ func (e *InsertValues) handleErr(col *table.Column, val *types.Datum, rowIdx int
228228

229229
// evalRow evaluates a to-be-inserted row. The value of the column may base on another column,
230230
// so we use setValueForRefColumn to fill the empty row some default values when needFillDefaultValues is true.
231-
func (e *InsertValues) evalRow(list []expression.Expression, rowIdx int) ([]types.Datum, error) {
231+
func (e *InsertValues) evalRow(ctx context.Context, list []expression.Expression, rowIdx int) ([]types.Datum, error) {
232232
rowLen := len(e.Table.Cols())
233233
if e.hasExtraHandle {
234234
rowLen++
@@ -259,7 +259,7 @@ func (e *InsertValues) evalRow(list []expression.Expression, rowIdx int) ([]type
259259
e.evalBuffer.SetDatum(offset, val1)
260260
}
261261

262-
return e.fillRow(row, hasValue)
262+
return e.fillRow(ctx, row, hasValue)
263263
}
264264

265265
// setValueForRefColumn set some default values for the row to eval the row value with other columns,
@@ -320,7 +320,7 @@ func (e *InsertValues) insertRowsFromSelect(ctx context.Context, exec func(ctx c
320320
for innerChunkRow := iter.Begin(); innerChunkRow != iter.End(); innerChunkRow = iter.Next() {
321321
innerRow := types.CloneRow(innerChunkRow.GetDatumRow(fields))
322322
e.rowCount++
323-
row, err := e.getRow(innerRow)
323+
row, err := e.getRow(ctx, innerRow)
324324
if err != nil {
325325
return err
326326
}
@@ -361,7 +361,7 @@ func (e *InsertValues) doBatchInsert(ctx context.Context) error {
361361
// getRow gets the row which from `insert into select from` or `load data`.
362362
// The input values from these two statements are datums instead of
363363
// expressions which are used in `insert into set x=y`.
364-
func (e *InsertValues) getRow(vals []types.Datum) ([]types.Datum, error) {
364+
func (e *InsertValues) getRow(ctx context.Context, vals []types.Datum) ([]types.Datum, error) {
365365
row := make([]types.Datum, len(e.Table.Cols()))
366366
hasValue := make([]bool, len(e.Table.Cols()))
367367
for i, v := range vals {
@@ -375,7 +375,7 @@ func (e *InsertValues) getRow(vals []types.Datum) ([]types.Datum, error) {
375375
hasValue[offset] = true
376376
}
377377

378-
return e.fillRow(row, hasValue)
378+
return e.fillRow(ctx, row, hasValue)
379379
}
380380

381381
func (e *InsertValues) filterErr(err error) error {
@@ -409,10 +409,10 @@ func (e *InsertValues) getColDefaultValue(idx int, col *table.Column) (d types.D
409409
}
410410

411411
// fillColValue fills the column value if it is not set in the insert statement.
412-
func (e *InsertValues) fillColValue(datum types.Datum, idx int, column *table.Column, hasValue bool) (types.Datum,
412+
func (e *InsertValues) fillColValue(ctx context.Context, datum types.Datum, idx int, column *table.Column, hasValue bool) (types.Datum,
413413
error) {
414414
if mysql.HasAutoIncrementFlag(column.Flag) {
415-
d, err := e.adjustAutoIncrementDatum(datum, hasValue, column)
415+
d, err := e.adjustAutoIncrementDatum(ctx, datum, hasValue, column)
416416
if err != nil {
417417
return types.Datum{}, err
418418
}
@@ -430,12 +430,12 @@ func (e *InsertValues) fillColValue(datum types.Datum, idx int, column *table.Co
430430

431431
// fillRow fills generated columns, auto_increment column and empty column.
432432
// For NOT NULL column, it will return error or use zero value based on sql_mode.
433-
func (e *InsertValues) fillRow(row []types.Datum, hasValue []bool) ([]types.Datum, error) {
433+
func (e *InsertValues) fillRow(ctx context.Context, row []types.Datum, hasValue []bool) ([]types.Datum, error) {
434434
gIdx := 0
435435
for i, c := range e.Table.Cols() {
436436
var err error
437437
// Get the default value for all no value columns, the auto increment column is different from the others.
438-
row[i], err = e.fillColValue(row[i], i, c, hasValue[i])
438+
row[i], err = e.fillColValue(ctx, row[i], i, c, hasValue[i])
439439
if err != nil {
440440
return nil, err
441441
}
@@ -462,7 +462,7 @@ func (e *InsertValues) fillRow(row []types.Datum, hasValue []bool) ([]types.Datu
462462
return row, nil
463463
}
464464

465-
func (e *InsertValues) adjustAutoIncrementDatum(d types.Datum, hasValue bool, c *table.Column) (types.Datum, error) {
465+
func (e *InsertValues) adjustAutoIncrementDatum(ctx context.Context, d types.Datum, hasValue bool, c *table.Column) (types.Datum, error) {
466466
retryInfo := e.ctx.GetSessionVars().RetryInfo
467467
if retryInfo.Retrying {
468468
id, err := retryInfo.GetCurrAutoIncrementID()
@@ -501,7 +501,7 @@ func (e *InsertValues) adjustAutoIncrementDatum(d types.Datum, hasValue bool, c
501501
// Change NULL to auto id.
502502
// Change value 0 to auto id, if NoAutoValueOnZero SQL mode is not set.
503503
if d.IsNull() || e.ctx.GetSessionVars().SQLMode&mysql.ModeNoAutoValueOnZero == 0 {
504-
recordID, err = e.Table.AllocAutoIncrementValue(e.ctx)
504+
recordID, err = table.AllocAutoIncrementValue(ctx, e.Table, e.ctx)
505505
if e.filterErr(err) != nil {
506506
return types.Datum{}, err
507507
}

executor/load_data.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func (e *LoadDataInfo) getLine(prevData, curData []byte) ([]byte, []byte, bool)
213213
// If it has the rest of data isn't completed the processing, then it returns without completed data.
214214
// If the number of inserted rows reaches the batchRows, then the second return value is true.
215215
// If prevData isn't nil and curData is nil, there are no other data to deal with and the isEOF is true.
216-
func (e *LoadDataInfo) InsertData(prevData, curData []byte) ([]byte, bool, error) {
216+
func (e *LoadDataInfo) InsertData(ctx context.Context, prevData, curData []byte) ([]byte, bool, error) {
217217
if len(prevData) == 0 && len(curData) == 0 {
218218
return nil, false, nil
219219
}
@@ -252,7 +252,7 @@ func (e *LoadDataInfo) InsertData(prevData, curData []byte) ([]byte, bool, error
252252
if err != nil {
253253
return nil, false, err
254254
}
255-
rows = append(rows, e.colsToRow(cols))
255+
rows = append(rows, e.colsToRow(ctx, cols))
256256
e.rowCount++
257257
if e.maxRowsInBatch != 0 && e.rowCount%e.maxRowsInBatch == 0 {
258258
reachLimit = true
@@ -281,7 +281,7 @@ func (e *LoadDataInfo) SetMessage() {
281281
e.ctx.GetSessionVars().StmtCtx.SetMessage(msg)
282282
}
283283

284-
func (e *LoadDataInfo) colsToRow(cols []field) []types.Datum {
284+
func (e *LoadDataInfo) colsToRow(ctx context.Context, cols []field) []types.Datum {
285285
for i := 0; i < len(e.row); i++ {
286286
if i >= len(cols) {
287287
e.row[i].SetNull()
@@ -295,7 +295,7 @@ func (e *LoadDataInfo) colsToRow(cols []field) []types.Datum {
295295
e.row[i].SetString(string(cols[i].str))
296296
}
297297
}
298-
row, err := e.getRow(e.row)
298+
row, err := e.getRow(ctx, e.row)
299299
if err != nil {
300300
e.handleWarning(err)
301301
return nil

executor/write_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1800,7 +1800,7 @@ func (s *testSuite4) TestLoadData(c *C) {
18001800
// data1 = nil, data2 = nil, fields and lines is default
18011801
ctx.GetSessionVars().StmtCtx.DupKeyAsWarning = true
18021802
ctx.GetSessionVars().StmtCtx.BadNullAsWarning = true
1803-
_, reachLimit, err := ld.InsertData(nil, nil)
1803+
_, reachLimit, err := ld.InsertData(context.Background(), nil, nil)
18041804
c.Assert(err, IsNil)
18051805
c.Assert(reachLimit, IsFalse)
18061806
r := tk.MustQuery(selectSQL)

infoschema/tables.go

-10
Original file line numberDiff line numberDiff line change
@@ -1962,11 +1962,6 @@ func (it *infoschemaTable) UpdateRecord(ctx sessionctx.Context, h int64, oldData
19621962
return table.ErrUnsupportedOp
19631963
}
19641964

1965-
// AllocAutoIncrementValue implements table.Table AllocAutoIncrementValue interface.
1966-
func (it *infoschemaTable) AllocAutoIncrementValue(ctx sessionctx.Context) (int64, error) {
1967-
return 0, table.ErrUnsupportedOp
1968-
}
1969-
19701965
// AllocHandle implements table.Table AllocHandle interface.
19711966
func (it *infoschemaTable) AllocHandle(ctx sessionctx.Context) (int64, error) {
19721967
return 0, table.ErrUnsupportedOp
@@ -2084,11 +2079,6 @@ func (vt *VirtualTable) UpdateRecord(ctx sessionctx.Context, h int64, oldData, n
20842079
return table.ErrUnsupportedOp
20852080
}
20862081

2087-
// AllocAutoIncrementValue implements table.Table AllocAutoIncrementValue interface.
2088-
func (vt *VirtualTable) AllocAutoIncrementValue(ctx sessionctx.Context) (int64, error) {
2089-
return 0, table.ErrUnsupportedOp
2090-
}
2091-
20922082
// AllocHandle implements table.Table AllocHandle interface.
20932083
func (vt *VirtualTable) AllocHandle(ctx sessionctx.Context) (int64, error) {
20942084
return 0, table.ErrUnsupportedOp

server/conn.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,7 @@ func insertDataWithCommit(ctx context.Context, prevData, curData []byte, loadDat
10271027
var err error
10281028
var reachLimit bool
10291029
for {
1030-
prevData, reachLimit, err = loadDataInfo.InsertData(prevData, curData)
1030+
prevData, reachLimit, err = loadDataInfo.InsertData(ctx, prevData, curData)
10311031
if err != nil {
10321032
return nil, err
10331033
}

table/table.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
package table
1919

2020
import (
21+
"context"
22+
23+
"github.com/opentracing/opentracing-go"
2124
"github.com/pingcap/parser/model"
2225
"github.com/pingcap/parser/mysql"
2326
"github.com/pingcap/parser/terror"
@@ -143,9 +146,6 @@ type Table interface {
143146
// RemoveRecord removes a row in the table.
144147
RemoveRecord(ctx sessionctx.Context, h int64, r []types.Datum) error
145148

146-
// AllocAutoIncrementValue allocates an auto_increment value for a new row.
147-
AllocAutoIncrementValue(ctx sessionctx.Context) (int64, error)
148-
149149
// AllocHandle allocates a handle for a new row.
150150
AllocHandle(ctx sessionctx.Context) (int64, error)
151151

@@ -167,6 +167,15 @@ type Table interface {
167167
Type() Type
168168
}
169169

170+
// AllocAutoIncrementValue allocates an auto_increment value for a new row.
171+
func AllocAutoIncrementValue(ctx context.Context, t Table, sctx sessionctx.Context) (int64, error) {
172+
if span := opentracing.SpanFromContext(ctx); span != nil && span.Tracer() != nil {
173+
span1 := span.Tracer().StartSpan("table.AllocAutoIncrementValue", opentracing.ChildOf(span.Context()))
174+
defer span1.Finish()
175+
}
176+
return t.Allocator(sctx).Alloc(t.Meta().ID)
177+
}
178+
170179
// PhysicalTable is an abstraction for two kinds of table representation: partition or non-partitioned table.
171180
// PhysicalID is a ID that can be used to construct a key ranges, all the data in the key range belongs to the corresponding PhysicalTable.
172181
// For a non-partitioned table, its PhysicalID equals to its TableID; For a partition of a partitioned table, its PhysicalID is the partition's ID.

table/tables/tables.go

-5
Original file line numberDiff line numberDiff line change
@@ -914,11 +914,6 @@ func GetColDefaultValue(ctx sessionctx.Context, col *table.Column, defaultVals [
914914
return colVal, nil
915915
}
916916

917-
// AllocAutoIncrementValue implements table.Table AllocAutoIncrementValue interface.
918-
func (t *tableCommon) AllocAutoIncrementValue(ctx sessionctx.Context) (int64, error) {
919-
return t.Allocator(ctx).Alloc(t.tableID)
920-
}
921-
922917
// AllocHandle implements table.Table AllocHandle interface.
923918
func (t *tableCommon) AllocHandle(ctx sessionctx.Context) (int64, error) {
924919
rowID, err := t.Allocator(ctx).Alloc(t.tableID)

table/tables/tables_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (ts *testSuite) TestBasic(c *C) {
9595
c.Assert(string(tb.RecordPrefix()), Not(Equals), "")
9696
c.Assert(tables.FindIndexByColName(tb, "b"), NotNil)
9797

98-
autoid, err := tb.AllocAutoIncrementValue(nil)
98+
autoid, err := table.AllocAutoIncrementValue(context.Background(), tb, nil)
9999
c.Assert(err, IsNil)
100100
c.Assert(autoid, Greater, int64(0))
101101

@@ -247,7 +247,7 @@ func (ts *testSuite) TestUniqueIndexMultipleNullEntries(c *C) {
247247
c.Assert(err, IsNil)
248248
c.Assert(handle, Greater, int64(0))
249249

250-
autoid, err := tb.AllocAutoIncrementValue(nil)
250+
autoid, err := table.AllocAutoIncrementValue(context.Background(), tb, nil)
251251
c.Assert(err, IsNil)
252252
c.Assert(autoid, Greater, int64(0))
253253

0 commit comments

Comments
 (0)