fix(pd): return PDException body from task/balanceLeaders - #3233
bitflicker64 wants to merge 2 commits into
Conversation
GET /v1/task/balanceLeaders declared "throws PDException" with no handler, so a refused balance (for example within 180 s of balancePartitions, while the balance-shard key is set) surfaced as a bare HTTP 500 and the reason only reached the PD log. Catch it and answer toJSON(e) like patrolStores, patrolPartitions and splitPartitions do. The success body is unchanged. Part 1 of apache#3231.
Codecov Report✅ All modified and coverable lines are covered by tests.
Additional details and impacted files@@ Coverage Diff @@
## master #3233 +/- ##
============================================
- Coverage 41.35% 35.26% -6.09%
+ Complexity 7299 6062 -1237
============================================
Files 802 782 -20
Lines 69688 67372 -2316
Branches 9291 8966 -325
============================================
- Hits 28816 23761 -5055
- Misses 37576 40822 +3246
+ Partials 3296 2789 -507 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
bitflicker64
left a comment
There was a problem hiding this comment.
Blocking: no. Summary: The change fixes the error body of GET /v1/task/balanceLeaders as described: a refused balance now answers {"status":1001,"error":...} like patrolStores, patrolPartitions and splitPartitions, and the success body stays {"1":2}. One minor gap remains: StoreAPI serves the same operation at GET /v1/balanceLeaders and still returns a bare HTTP 500 for the same refusal. Evidence: read TaskAPI, StoreAPI, API.toJSON overloads and TaskScheduleService.balancePartitionLeader at c4e8ab4; confirmed toJSON(leaders) resolves to toJSON(Object) and toJSON(e) to toJSON(PDException); searched hugegraph-pd for @ExceptionHandler and @ControllerAdvice (none); the pd CI job log at this head shows TaskAPIBalanceLeadersTest ran with 2 tests, 0 failures.
StoreAPI serves the same leader balance at GET /v1/balanceLeaders and had the same missing handler, so a refused balance still answered a bare HTTP 500 on that route. Catch it and answer toJSON(e) like the other StoreAPI endpoints. The success body is unchanged. The test now covers both routes and is renamed to BalanceLeadersAPITest.
Purpose of the PR
GET /v1/task/balanceLeadersdeclaresthrows PDExceptionand has no handler. When PD refuses the leader balance, for example within 180 s of abalancePartitionscall while the balance-shard key is set, the caller gets a bare Spring 500 ({"timestamp":...,"status":500,"error":"Internal Server Error","path":"/v1/task/balanceLeaders"}) and the reason (balance shard is processing, please try later!,TaskScheduleService.java:478) only reaches the PD log. The sibling task endpointspatrolStores,patrolPartitionsandsplitPartitionscatch the exception and answertoJSON(e).StoreAPIserves the same operation atGET /v1/balanceLeadersand had the same gap.Part 2 of #3231 (what the task endpoints answer on a follower or on a no-op, and whether the body reports what was done) is out of scope here. It needs a design decision and stays open in the issue. This PR does not change response semantics beyond the error path below.
Main Changes
TaskAPI.balanceLeadersnow catchesPDExceptionand returnstoJSON(e), copying the handler used bypatrolStores,patrolPartitionsandsplitPartitions, includingproduces = APPLICATION_JSON_VALUEand@ResponseBody.{"status":1001,"error":"balance shard is processing, please try later!"}with the same HTTP status the siblings use for their error bodies.API.toJSON(Object)(Jackson), so{"1":2}stays{"1":2}and the content type staysapplication/json.StoreAPI.balanceLeaders(GET /v1/balanceLeaders) gets the same catch, in the form the otherStoreAPIendpoints use, so both routes answer the same error body.BalanceLeadersAPITestinhg-pd-service, next toStoreAPIReadyTest, covering both routes.Verifying these changes
BalanceLeadersAPITest.test{Task,Store}RouteRefusedBalanceReturnsErrorBody: aPDRestServicestub throwsPDException(1001, "balance shard is processing, please try later!"); each route returns a body withstatus1001 and thaterrortext.BalanceLeadersAPITest.test{Task,Store}RouteSuccessfulBalanceKeepsBody: a stub returns{1: 2}; each route returns{"1":2}, the same body as before.mvn test -pl hugegraph-pd/hg-pd-service -am -Dtest='StoreAPIReadyTest,BalanceLeadersAPITest,AuthenticationTest' -DfailIfNoTests=falsegives 17 tests, 0 failures (all test classes inhg-pd-service).mvn editorconfig:check -pl hugegraph-pd/hg-pd-servicepasses.Does this PR potentially affect the following parts?
GET /v1/task/balanceLeadersandGET /v1/balanceLeadersonly; the success body is unchanged)Documentation Status
Select one option and provide the documentation location when applicable.
Doc - TODO: required documentation is pending; complete it before merging.Doc - Done: documentation is included here or linked below.Doc - No Need: no user-visible documentation is affected.Documentation files in this PR or paired hugegraph-doc PR: