-
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
http: get region's info #2774
http: get region's info #2774
Conversation
server/server.go
Outdated
@@ -250,6 +251,18 @@ func (s *Server) startStatusHTTP() { | |||
}) | |||
// HTTP path for prometheus. | |||
http.Handle("/metrics", prometheus.Handler()) | |||
// default path for regions status info |
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.
use /regions
@AndreMouche CI failed. |
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.
Need test for region handler.
server/region_handler.go
Outdated
"github.com/pingcap/tidb/store/tikv" | ||
"github.com/pingcap/tidb/tablecodec" | ||
"github.com/pingcap/tidb/util/codec" | ||
gContext "golang.org/x/net/context" |
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.
goctx "context"
server/region_handler.go
Outdated
gContext "golang.org/x/net/context" | ||
"math" | ||
"net/http" | ||
"time" |
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.
Take a look at how other files in TiDB organize import paths.
server/server.go
Outdated
@@ -39,6 +39,7 @@ import ( | |||
// For pprof | |||
_ "net/http/pprof" | |||
|
|||
"github.com/gorilla/mux" |
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.
This adds a dependency, we should add this repo in vendor.
server/region_handler.go
Outdated
} | ||
} | ||
|
||
func (req *RegionsHTTPRequest) showResponse(data interface{}, err error) { |
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.
Why not name it writeResponse
?
store/tikv/region_cache.go
Outdated
@@ -163,6 +182,23 @@ func (c *RegionCache) GroupKeysByRegion(bo *Backoffer, keys [][]byte) (map[Regio | |||
return groups, first, nil | |||
} | |||
|
|||
// ListRegionIDsInKeyRange lists ids of regions in [start_key,end_key]. | |||
func (c *RegionCache) ListRegionIDsInKeyRange(bo *Backoffer, startKey, endKey []byte) (regions []uint64, err error) { | |||
regions = make([]uint64, 0, 0) |
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.
Why make a slice with zero capacity?
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.
The variable name should be regionIDs
.
store/tikv/region_cache.go
Outdated
|
||
pdCli, err := pd.NewClient(etcdAddrs) | ||
if err != nil { | ||
if strings.Contains(err.Error(), "i/o timeout") { |
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.
This can be removed, it is useful only for server initialization.
store/tikv/region_cache.go
Outdated
@@ -24,6 +24,7 @@ import ( | |||
"github.com/pingcap/kvproto/pkg/kvrpcpb" | |||
"github.com/pingcap/kvproto/pkg/metapb" | |||
"github.com/pingcap/pd/pd-client" | |||
"strings" |
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.
import path ordering.
server/region_handler.go
Outdated
TableName: tableName, | ||
TableID: tableID, | ||
RowRegions: nil, | ||
Indices: make([]IndexRegions, len(table.Indices()), len(table.Indices())), |
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.
The third parameter in make is not needed.
server/server.go
Outdated
request.Handle() | ||
router.Handle("/metrics", prometheus.Handler()) | ||
|
||
router.HandleFunc("/tables/{db}/{table}/regions", func(w http.ResponseWriter, req *http.Request) { |
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.
I prefer /regions/db/table
… or end_key is not index key
PTAL |
server/region_handler.go
Outdated
} | ||
|
||
// HTTPListTableRegionsHandler is the handler for list table's regions. | ||
type HTTPListTableRegionsHandler struct { |
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.
TableRegionsHandler
server/region_handler.go
Outdated
// HTTPRegionHandler is the common field for http region handler. It contains | ||
// some common functions which would be used in HTTPListTableRegionsHandler and | ||
// HTTPGetRegionByIDHandler. | ||
type HTTPRegionHandler struct { |
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.
You can use RegionHandler to handle /regions/{regionID}
and TableRegionsHandler to handle /tables/db/table
@coocood PTAL |
// get from table's ID directly. Above all, here do dot process like | ||
// `for id in [frameRange.firstTableID,frameRange.endTableID]` | ||
// on [frameRange.firstTableID,frameRange.endTableID] is small enough. | ||
for _, db := range tool.infoSchema.AllSchemas() { |
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 we will add all databases and tables? this may cost lots of memory.
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.
@siddontang Since we need a database's name for each frame, and a table's database name can not get from table's ID directly. So here do dot process like
for id in [frameRange.firstTableID,frameRange.endTableID]
...
on [frameRange.firstTableID,frameRange.endTableID] is small enough.
Above all, it seems iterate all databases and tables cannot be avoided. While with infoSchema
already have all databases with tables in memory, there is no additional memory cost.
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.
can we add a function like GetTableByID in infoshema? @coocood
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.
@siddontang We have GetTableByID
, but the table doesn't know about the DB name.
LGTM |
PTAL @disksing |
store/tikv/region_cache.go
Outdated
@@ -163,6 +192,22 @@ func (c *RegionCache) GroupKeysByRegion(bo *Backoffer, keys [][]byte) (map[Regio | |||
return groups, first, nil | |||
} | |||
|
|||
// ListRegionIDsInKeyRange lists ids of regions in [start_key,end_key]. | |||
func (c *RegionCache) ListRegionIDsInKeyRange(bo *Backoffer, startKey, endKey []byte) (regionIDs []uint64, err error) { | |||
for err == 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.
err will never be nil.
server/region_handler_test.go
Outdated
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// +build !race |
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.
Why disable race detector?
server/region_handler.go
Outdated
} | ||
|
||
// addIndex insert a index into RegionDetail. | ||
func (rt *RegionDetail) addIndex(dbName, tName string, tID int64, indexName string, indexID int64) { |
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.
Please move the methods to the place next to the structure.
// TableRegionsHandler is the handler for list table's regions. | ||
type TableRegionsHandler struct { | ||
RegionHandler | ||
} |
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.
I think we don't need TableRegionsHandler
. How about define 2 methods on RegionHandler
and use HandleFunc
to register them to router?
server/region_handler.go
Outdated
} | ||
} | ||
|
||
func (rh TableRegionsHandler) getRegionsMetaWithRegionIds(rIDs []uint64) ([]RegionMeta, error) { |
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.
Id
should be ID
. Also I think just getRegionMetas(regionIDs []uint64)
is ok.
// prepare checks and prepares for region request. It returns | ||
// regionHandlerTool on success while return an err on any | ||
// error happens. | ||
func (rh *RegionHandler) prepare() (tool *regionHandlerTool, err error) { |
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.
Seems regionHandlerTool
is redundant. I think it would be more clear to return infoSchema, regionCache, error
.
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.
It may cause too many params for the func
? And make regionHandlerTool
as they are common tools used in region handler. @disksing
server/region_handler.go
Outdated
if rh.pdClient == nil { | ||
err = fmt.Errorf("Invalid PdClient: nil") | ||
return | ||
} |
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.
I suppose it won't be nil.
store/tikv/kv.go
Outdated
func ParsePath(path string) (etcdAddrs []string, disableGC bool, err error) { | ||
return parsePath(path) | ||
} | ||
|
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.
Why not just rename 'parsePath' to 'ParsePath'?
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.
Because too many function are using parsePath
. I also think ParsePath
is not a good name used as public function and would like to change someday. @disksing
tablecodec/tablecodec_test.go
Outdated
@@ -22,6 +22,7 @@ import ( | |||
"github.com/pingcap/tidb/util/codec" | |||
"github.com/pingcap/tidb/util/testleak" | |||
"github.com/pingcap/tidb/util/types" | |||
"math" |
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.
The imports are not in good order. We should separate std packages from 3rd-party packages.
store/tikv/region_cache_test.go
Outdated
|
||
regionIDs, err := s.cache.ListRegionIDsInKeyRange(s.bo, []byte("a"), []byte("z")) | ||
c.Assert(err, IsNil) | ||
c.Assert(len(regionIDs), Equals, 2) |
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.
Why only check length? I suppose you can check the IDs are correct.
c.Assert(regionIDs, DeeqEquals, []uint64{s.region1, region2})
server/region_handler_test.go
Outdated
resp, err := http.Get("http://127.0.0.1:10090/tables/information_schema/SCHEMATA/regions") | ||
c.Assert(err, IsNil) | ||
|
||
//c.Assert(resp.StatusCode,Equals,http.StatusOK) |
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.
Why comment it?
server/region_handler_test.go
Outdated
err = decoder.Decode(&data) | ||
c.Assert(err, IsNil) | ||
|
||
c.Assert(len(data.RecordRegions) > 0, IsTrue) |
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.
c.Assert(len(data.RecordRegions), Greater, 0)
@BusyJay PTAL |
LGTM, PTAL @siddontang |
Hi,all,
This PR is used to list regions for table && get region by ID through http request.
@coocood @disksing @siddontang PTAL
Details:
List table's Regions
Requests
Syntax
Request Parameters
Response
Response Elements
Response Sample
GET Region By ID
Requests
Syntax
Request Parameters
Response
Response Elements
Response Sample