diff --git a/server/http_handler.go b/server/http_handler.go index f0ca7856b33e6..edce20faa5cca 100644 --- a/server/http_handler.go +++ b/server/http_handler.go @@ -132,17 +132,33 @@ func (s *Server) newTikvHandlerTool() *tikvHandlerTool { } type mvccKV struct { - Key string `json:"key"` - Value *kvrpcpb.MvccGetByKeyResponse `json:"value"` + Key string `json:"key"` + RegionID uint64 `json:"region_id"` + Value *kvrpcpb.MvccGetByKeyResponse `json:"value"` +} + +func (t *tikvHandlerTool) getRegionIDByKey(encodedKey []byte) (uint64, error) { + keyLocation, err := t.RegionCache.LocateKey(tikv.NewBackoffer(context.Background(), 500), encodedKey) + if err != nil { + return 0, err + } + return keyLocation.Region.GetID(), nil } func (t *tikvHandlerTool) getMvccByHandle(tableID, handle int64) (*mvccKV, error) { encodedKey := tablecodec.EncodeRowKeyWithHandle(tableID, handle) data, err := t.GetMvccByEncodedKey(encodedKey) - return &mvccKV{Key: strings.ToUpper(hex.EncodeToString(encodedKey)), Value: data}, err + if err != nil { + return nil, err + } + regionID, err := t.getRegionIDByKey(encodedKey) + if err != nil { + return nil, err + } + return &mvccKV{Key: strings.ToUpper(hex.EncodeToString(encodedKey)), Value: data, RegionID: regionID}, err } -func (t *tikvHandlerTool) getMvccByStartTs(startTS uint64, startKey, endKey []byte) (*kvrpcpb.MvccGetByStartTsResponse, error) { +func (t *tikvHandlerTool) getMvccByStartTs(startTS uint64, startKey, endKey []byte) (*mvccKV, error) { bo := tikv.NewBackoffer(context.Background(), 5000) for { curRegion, err := t.RegionCache.LocateKey(bo, startKey) @@ -197,7 +213,8 @@ func (t *tikvHandlerTool) getMvccByStartTs(startTS uint64, startKey, endKey []by key := data.GetKey() if len(key) > 0 { - return data, nil + resp := &kvrpcpb.MvccGetByKeyResponse{Info: data.Info, RegionError: data.RegionError, Error: data.Error} + return &mvccKV{Key: strings.ToUpper(hex.EncodeToString(key)), Value: resp, RegionID: curRegion.Region.GetID()}, nil } if len(endKey) > 0 && curRegion.Contains(endKey) { @@ -228,7 +245,14 @@ func (t *tikvHandlerTool) getMvccByIdxValue(idx table.Index, values url.Values, return nil, errors.Trace(err) } data, err := t.GetMvccByEncodedKey(encodedKey) - return &mvccKV{strings.ToUpper(hex.EncodeToString(encodedKey)), data}, err + if err != nil { + return nil, err + } + regionID, err := t.getRegionIDByKey(encodedKey) + if err != nil { + return nil, err + } + return &mvccKV{strings.ToUpper(hex.EncodeToString(encodedKey)), regionID, data}, err } // formValue2DatumRow converts URL query string to a Datum Row. @@ -287,12 +311,20 @@ func (t *tikvHandlerTool) schema() (infoschema.InfoSchema, error) { return domain.GetDomain(session.(sessionctx.Context)).InfoSchema(), nil } -func (t *tikvHandlerTool) handleMvccGetByHex(params map[string]string) (interface{}, error) { +func (t *tikvHandlerTool) handleMvccGetByHex(params map[string]string) (*mvccKV, error) { encodedKey, err := hex.DecodeString(params[pHexKey]) if err != nil { return nil, errors.Trace(err) } - return t.GetMvccByEncodedKey(encodedKey) + data, err := t.GetMvccByEncodedKey(encodedKey) + if err != nil { + return nil, errors.Trace(err) + } + regionID, err := t.getRegionIDByKey(encodedKey) + if err != nil { + return nil, err + } + return &mvccKV{Key: strings.ToUpper(params[pHexKey]), Value: data, RegionID: regionID}, nil } // settingsHandler is the handler for list tidb server settings.