Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.Original topic: 写写冲突日志问题

TiDB encountered a write-write conflict, but it is impossible to locate which index of which table caused it. I referred to this article, but the log output is not as mentioned in the document. In the document, it shows:
[2020/05/12 15:17:01.568 +08:00] [WARN] [session.go:446] ["commit failed"] [conn=3] ["finished txn"="Txn{state=invalid}"] [error="[kv:9007]Write conflict, txnStartTS=416617006551793665, conflictStartTS=416617018650001409, conflictCommitTS=416617023093080065, key={tableID=47, indexID=1, indexValues={string, }} primary={tableID=47, indexID=1, indexValues={string, }} [try again later]"]
In production, it shows:
[txn.go:83] [RunInNewTxn] ["retry txn"=442462672668854215] ["original txn"=442462672668854215] [error="[kv:9007]Write conflict, txnStartTS=442462672668854215, conflictStartTS=442462672668854858, conflictCommitTS=442462672668855267, key=[]byte{0x6d, 0x4e, 0x65, 0x78, 0x74, 0x47, 0x6c, 0x6f, 0x62, 0xff, 0x61, 0x6c, 0x49, 0x44, 0x0, 0x0, 0x0, 0x0, 0xfb, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x73} primary=[]byte{0x6d, 0x4e, 0x65, 0x78, 0x74, 0x47, 0x6c, 0x6f, 0x62, 0xff, 0x61, 0x6c, 0x49, 0x44, 0x0, 0x0, 0x0, 0x0, 0xfb, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x73} [try again later]"]
I reviewed the code and it seems to have reached the line mKey, mField, err := tablecodec.DecodeMetaKey(key) in the prettyWriteKey function. What does this hexadecimal encoding mean?
byte{0x6d, 0x4e, 0x65, 0x78, 0x74, 0x47, 0x6c, 0x6f, 0x62, 0xff, 0x61, 0x6c, 0x49, 0x44, 0x0, 0x0, 0x0, 0x0, 0xfb, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x73}
func newWriteConflictError(conflict *kvrpcpb.WriteConflict) error {
if conflict == nil {
return kv.ErrWriteConflict
}
var buf bytes.Buffer
prettyWriteKey(&buf, conflict.Key)
buf.WriteString(" primary=")
prettyWriteKey(&buf, conflict.Primary)
return kv.ErrWriteConflict.FastGenByArgs(conflict.StartTs, conflict.ConflictTs, conflict.ConflictCommitTs, buf.String())
}
func prettyWriteKey(buf *bytes.Buffer, key []byte) {
tableID, indexID, indexValues, err := tablecodec.DecodeIndexKey(key)
if err == nil {
_, err1 := fmt.Fprintf(buf, "{tableID=%d, indexID=%d, indexValues={", tableID, indexID)
if err1 != nil {
logutil.BgLogger().Error("error", zap.Error(err1))
}
for _, v := range indexValues {
_, err2 := fmt.Fprintf(buf, "%s, ", v)
if err2 != nil {
logutil.BgLogger().Error("error", zap.Error(err2))
}
}
buf.WriteString("}}")
return
}
tableID, handle, err := tablecodec.DecodeRecordKey(key)
if err == nil {
_, err3 := fmt.Fprintf(buf, "{tableID=%d, handle=%d}", tableID, handle)
if err3 != nil {
logutil.BgLogger().Error("error", zap.Error(err3))
}
return
}
mKey, mField, err := tablecodec.DecodeMetaKey(key)
if err == nil {
_, err3 := fmt.Fprintf(buf, "{metaKey=true, key=%s, field=%s}", string(mKey), string(mField))
if err3 != nil {
logutil.Logger(context.Background()).Error("error", zap.Error(err3))
}
return
}
_, err4 := fmt.Fprintf(buf, "%#v", key)
if err4 != nil {
logutil.BgLogger().Error("error", zap.Error(err4))
}
}