Skip to content

Commit

Permalink
http: add region-id when getting mvcc by handle (#11436)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lingyu Song authored Aug 1, 2019
1 parent 1846eba commit 7b63293
Showing 1 changed file with 40 additions and 8 deletions.
48 changes: 40 additions & 8 deletions server/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -194,7 +210,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) {
Expand Down Expand Up @@ -225,7 +242,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.
Expand Down Expand Up @@ -284,12 +308,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.
Expand Down

0 comments on commit 7b63293

Please sign in to comment.