-
Notifications
You must be signed in to change notification settings - Fork 766
server/api: allow follower local region reads #10682
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
Changes from 4 commits
ac95f81
fdcedb5
1bc0a12
661909b
50068ef
56038cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ import ( | |
|
|
||
| "github.com/tikv/pd/pkg/audit" | ||
| "github.com/tikv/pd/pkg/errs" | ||
| "github.com/tikv/pd/pkg/utils/apiutil" | ||
| "github.com/tikv/pd/pkg/utils/requestutil" | ||
| "github.com/tikv/pd/server" | ||
| "github.com/tikv/pd/server/cluster" | ||
|
|
@@ -79,20 +80,36 @@ func (rm *requestInfoMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Reques | |
| } | ||
|
|
||
| type clusterMiddleware struct { | ||
| s *server.Server | ||
| rd *render.Render | ||
| s *server.Server | ||
| rd *render.Render | ||
| allowFollowerSyncedRegion bool | ||
| } | ||
|
|
||
| func newClusterMiddleware(s *server.Server) clusterMiddleware { | ||
| return clusterMiddleware{ | ||
| type clusterMiddlewareOption func(*clusterMiddleware) | ||
|
|
||
| func withFollowerSyncedRegion() clusterMiddlewareOption { | ||
| return func(m *clusterMiddleware) { | ||
| m.allowFollowerSyncedRegion = true | ||
| } | ||
| } | ||
|
|
||
| func newClusterMiddleware(s *server.Server, opts ...clusterMiddlewareOption) clusterMiddleware { | ||
| m := clusterMiddleware{ | ||
| s: s, | ||
| rd: render.New(render.Options{IndentJSON: true}), | ||
| } | ||
| for _, opt := range opts { | ||
| opt(&m) | ||
| } | ||
| return m | ||
| } | ||
|
|
||
| func (m clusterMiddleware) middleware(h http.Handler) http.Handler { | ||
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| rc := m.s.GetRaftCluster() | ||
| if rc == nil { | ||
|
bufferflies marked this conversation as resolved.
|
||
| rc = m.getFollowerSyncedCluster(r) | ||
| } | ||
| if rc == nil { | ||
| m.rd.JSON(w, http.StatusInternalServerError, errs.ErrNotBootstrapped.FastGenByArgs().Error()) | ||
| return | ||
|
|
@@ -102,6 +119,20 @@ func (m clusterMiddleware) middleware(h http.Handler) http.Handler { | |
| }) | ||
| } | ||
|
|
||
| func (m clusterMiddleware) getFollowerSyncedCluster(r *http.Request) *cluster.RaftCluster { | ||
| if r.Method != http.MethodGet || | ||
| !m.allowFollowerSyncedRegion || | ||
| m.s.GetMember().IsServing() || | ||
| r.Header.Get(apiutil.PDAllowFollowerHandleHeader) == "" { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if PDAllowFollowerHandleHeader is another value? |
||
| return nil | ||
| } | ||
| rc := m.s.DirectlyGetRaftCluster() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to change the "Only used for test" comment in DirectlyGetRaftCluster. |
||
| if rc == nil || !rc.GetRegionSyncer().IsRunning() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IsRunning() may be too weak as a readiness gate here. The syncer can become running after the first sync batch, so large clusters may serve partial region cache results before the initial full sync finishes.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For debugging interfaces like this, is it acceptable for them to provide results ahead of synced?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Partial results might be acceptable, but then we should document that these endpoints are eventually consistent and may be incomplete during the initial full sync. Otherwise this needs a stronger readiness signal, because streamingRunning is set after each received batch while the leader may still be sending the rest of the full sync.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tracked this follow-up in #10690. The client-side behavior should avoid sending follower region requests to a PD follower until that follower has completed full region sync; |
||
| return nil | ||
| } | ||
| return rc | ||
| } | ||
|
|
||
| type clusterCtxKey struct{} | ||
|
|
||
| func getCluster(r *http.Request) *cluster.RaftCluster { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.