-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
ttl: submit ttl scan task to the system table #40422
Changes from all commits
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 |
---|---|---|
|
@@ -1018,16 +1018,16 @@ func (store *MVCCStore) buildPrewriteLock(reqCtx *requestCtx, m *kvrpcpb.Mutatio | |
lock.Op = uint8(kvrpcpb.Op_Put) | ||
} | ||
if rowcodec.IsRowKey(m.Key) && lock.Op == uint8(kvrpcpb.Op_Put) { | ||
if rowcodec.IsNewFormat(m.Value) { | ||
reqCtx.buf = m.Value | ||
} else { | ||
if !rowcodec.IsNewFormat(m.Value) { | ||
reqCtx.buf, err = encodeFromOldRow(m.Value, reqCtx.buf) | ||
if err != nil { | ||
log.Error("encode data failed", zap.Binary("value", m.Value), zap.Binary("key", m.Key), zap.Stringer("op", m.Op), zap.Error(err)) | ||
return nil, err | ||
} | ||
|
||
lock.Value = make([]byte, len(reqCtx.buf)) | ||
copy(lock.Value, reqCtx.buf) | ||
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. I finally find the bug! Reuse the It seems that this bug requires 1PC, and set multiple rows in one transaction (if these rows are in different tables, it will panic), so this problem only occurs in the TTL test, in which we set the session variables to enable 1PC explicitly. It takes me a lot of time to reach here 😢 . |
||
} | ||
lock.Value = reqCtx.buf | ||
} | ||
|
||
lock.ForUpdateTS = req.ForUpdateTs | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
// Copyright 2023 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cache | ||
|
||
import ( | ||
"encoding/json" | ||
"time" | ||
|
||
"github.com/pingcap/tidb/sessionctx" | ||
"github.com/pingcap/tidb/types" | ||
"github.com/pingcap/tidb/util/chunk" | ||
"github.com/pingcap/tidb/util/codec" | ||
) | ||
|
||
const selectFromTTLTask = `SELECT LOW_PRIORITY | ||
job_id, | ||
table_id, | ||
scan_id, | ||
scan_range_start, | ||
scan_range_end, | ||
expire_time, | ||
owner_id, | ||
owner_addr, | ||
owner_hb_time, | ||
status, | ||
status_update_time, | ||
state, | ||
created_time FROM mysql.tidb_ttl_task` | ||
const insertIntoTTLTask = `INSERT LOW_PRIORITY INTO mysql.tidb_ttl_task SET | ||
job_id = %?, | ||
table_id = %?, | ||
scan_id = %?, | ||
scan_range_start = %?, | ||
scan_range_end = %?, | ||
expire_time = %?, | ||
created_time = %?` | ||
|
||
// SelectFromTTLTaskWithID returns an SQL statement to get all tasks of the specified job in mysql.tidb_ttl_task | ||
func SelectFromTTLTaskWithID(jobID string) (string, []interface{}) { | ||
return selectFromTTLTask + " WHERE job_id = %?", []interface{}{jobID} | ||
} | ||
|
||
// InsertIntoTTLTask returns an SQL statement to insert a ttl task into mysql.tidb_ttl_task | ||
func InsertIntoTTLTask(sctx sessionctx.Context, jobID string, tableID int64, scanID int, scanRangeStart []types.Datum, scanRangeEnd []types.Datum, expireTime time.Time, createdTime time.Time) (string, []interface{}, error) { | ||
rangeStart, err := codec.EncodeKey(sctx.GetSessionVars().StmtCtx, []byte{}, scanRangeStart...) | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
rangeEnd, err := codec.EncodeKey(sctx.GetSessionVars().StmtCtx, []byte{}, scanRangeEnd...) | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
return insertIntoTTLTask, []interface{}{jobID, tableID, int64(scanID), rangeStart, rangeEnd, expireTime, createdTime}, nil | ||
} | ||
|
||
// TaskStatus represents the current status of a task | ||
type TaskStatus string | ||
|
||
const ( | ||
// TaskStatusWaiting means the task hasn't started | ||
TaskStatusWaiting TaskStatus = "waiting" | ||
// TaskStatusRunning means this task is running | ||
TaskStatusRunning = "running" | ||
// TaskStatusFinished means this task has finished | ||
TaskStatusFinished = "finished" | ||
) | ||
|
||
// TTLTask is a row recorded in mysql.tidb_ttl_task | ||
type TTLTask struct { | ||
JobID string | ||
TableID int64 | ||
ScanID int64 | ||
ScanRangeStart []types.Datum | ||
ScanRangeEnd []types.Datum | ||
ExpireTime time.Time | ||
OwnerID string | ||
OwnerAddr string | ||
OwnerHBTime time.Time | ||
Status TaskStatus | ||
StatusUpdateTime time.Time | ||
State *TTLTaskState | ||
CreatedTime time.Time | ||
} | ||
|
||
// TTLTaskState records the internal states of the ttl task | ||
type TTLTaskState struct { | ||
TotalRows uint64 `json:"total_rows"` | ||
SuccessRows uint64 `json:"success_rows"` | ||
ErrorRows uint64 `json:"error_rows"` | ||
|
||
ScanTaskErr string `json:"scan_task_err"` | ||
} | ||
|
||
// RowToTTLTask converts a row into TTL task | ||
func RowToTTLTask(sctx sessionctx.Context, row chunk.Row) (*TTLTask, error) { | ||
var err error | ||
timeZone := sctx.GetSessionVars().Location() | ||
|
||
task := &TTLTask{ | ||
JobID: row.GetString(0), | ||
TableID: row.GetInt64(1), | ||
ScanID: row.GetInt64(2), | ||
} | ||
if !row.IsNull(3) { | ||
scanRangeStartBuf := row.GetBytes(3) | ||
// it's still posibble to be nil even this column is not NULL | ||
if scanRangeStartBuf != nil { | ||
task.ScanRangeStart, err = codec.Decode(scanRangeStartBuf, len(scanRangeStartBuf)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
if !row.IsNull(4) { | ||
scanRangeEndBuf := row.GetBytes(4) | ||
// it's still posibble to be nil even this column is not NULL | ||
if scanRangeEndBuf != nil { | ||
task.ScanRangeEnd, err = codec.Decode(scanRangeEndBuf, len(scanRangeEndBuf)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
|
||
task.ExpireTime, err = row.GetTime(5).GoTime(timeZone) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !row.IsNull(6) { | ||
task.OwnerID = row.GetString(6) | ||
} | ||
if !row.IsNull(7) { | ||
task.OwnerAddr = row.GetString(7) | ||
} | ||
if !row.IsNull(8) { | ||
task.OwnerHBTime, err = row.GetTime(8).GoTime(timeZone) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
if !row.IsNull(9) { | ||
status := row.GetString(9) | ||
if len(status) == 0 { | ||
status = "waiting" | ||
} | ||
task.Status = TaskStatus(status) | ||
} | ||
if !row.IsNull(10) { | ||
task.StatusUpdateTime, err = row.GetTime(10).GoTime(timeZone) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
if !row.IsNull(11) { | ||
stateStr := row.GetString(11) | ||
state := &TTLTaskState{} | ||
err = json.Unmarshal([]byte(stateStr), state) | ||
if err != nil { | ||
return nil, err | ||
} | ||
task.State = state | ||
} | ||
|
||
task.CreatedTime, err = row.GetTime(12).GoTime(timeZone) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return task, nil | ||
} |
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.
When
rowcodec.IsNewFormat(m.Value)
, the previous code are deleted?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.
Here it sets
reqCtx.buf = m.Value
and laterlock.Value = reqCtx.buf
. The main purpose is to setlock.Value = m.Value
. In https://github.com/pingcap/tidb/pull/40422/files/f2fe4bde5508cad89d582145709fa286a24bec18#diff-c8058e505bb9be555aca4f1a51fbf20e712ea4d6d89812a09f3ebf02ff99e2f2R896, thelock.Value
has been initialized withm.Value
, so there is no need to assign again.