diff --git a/client/pd_service_discovery.go b/client/pd_service_discovery.go index 7b39a51fb2b..dd5cd96778b 100644 --- a/client/pd_service_discovery.go +++ b/client/pd_service_discovery.go @@ -23,14 +23,13 @@ import ( "sync/atomic" "time" - "github.com/tikv/pd/client/backoff" - "github.com/pingcap/errors" "github.com/pingcap/failpoint" "github.com/pingcap/kvproto/pkg/pdpb" "github.com/pingcap/log" "github.com/tikv/pd/client/errs" "github.com/tikv/pd/client/grpcutil" + "github.com/tikv/pd/client/retry" "github.com/tikv/pd/client/tlsutil" "go.uber.org/zap" "google.golang.org/grpc" @@ -159,7 +158,7 @@ type pdServiceDiscovery struct { option *option successReConnect chan struct{} - bo *backoff.Backoffer + bo *retry.Backoffer } // newPDServiceDiscovery returns a new PD service discovery-based client. @@ -182,7 +181,7 @@ func newPDServiceDiscovery( keyspaceID: keyspaceID, tlsCfg: tlsCfg, option: option, - bo: backoff.NewBackoffer(ctx, maxRetryTimes), + bo: retry.NewBackoffer(ctx, maxRetryTimes), } pdsd.urls.Store(urls) return pdsd @@ -470,10 +469,11 @@ func (c *pdServiceDiscovery) ScheduleCheckMemberChanged() { select { case c.checkMembershipCh <- struct{}{}: if err := c.waitForReady(); err != nil { - if c.bo.GetBackoffTime(backoff.BoMemberUpdate.String()) >= 10 { + // If backoff times count is greater than 10, reset it. + if c.bo.GetBackoffTimeCnt(retry.BoMemberUpdate.String()) >= 10 { c.bo.Reset() } - e := c.bo.Backoff(backoff.BoMemberUpdate, err) + e := c.bo.Backoff(retry.BoMemberUpdate, err) if e != nil { log.Error("[pd] wait for ready backoff failed", errs.ZapError(e)) return diff --git a/client/backoff/backoff.go b/client/retry/backoff.go similarity index 94% rename from client/backoff/backoff.go rename to client/retry/backoff.go index 6132ecf490b..c5350b7ef72 100644 --- a/client/backoff/backoff.go +++ b/client/retry/backoff.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package backoff +package retry import ( "context" @@ -51,7 +51,7 @@ func NewBackoffer(ctx context.Context, maxSleep int) *Backoffer { // It returns a retryable error if total sleep time exceeds maxSleep. func (b *Backoffer) Backoff(cfg *Config, err error) error { if span := opentracing.SpanFromContext(b.ctx); span != nil && span.Tracer() != nil { - span1 := span.Tracer().StartSpan(fmt.Sprintf("tikv.backoff.%s", cfg), opentracing.ChildOf(span.Context())) + span1 := span.Tracer().StartSpan(fmt.Sprintf("pd.client.backoff.%s", cfg), opentracing.ChildOf(span.Context())) defer span1.Finish() opentracing.ContextWithSpan(b.ctx, span1) } @@ -125,8 +125,8 @@ func (b *Backoffer) GetBackoffTimes() map[string]int { return b.backoffTimes } -// GetBackoffTime returns backoff time count by specific type. -func (b *Backoffer) GetBackoffTime(s string) int { +// GetBackoffTimeCnt returns backoff time count by specific type. +func (b *Backoffer) GetBackoffTimeCnt(s string) int { return b.backoffTimes[s] } diff --git a/client/backoff/backoff_test.go b/client/retry/backoff_test.go similarity index 98% rename from client/backoff/backoff_test.go rename to client/retry/backoff_test.go index 36d68860411..2b9c943b864 100644 --- a/client/backoff/backoff_test.go +++ b/client/retry/backoff_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package backoff +package retry import ( "context" diff --git a/client/backoff/config.go b/client/retry/config.go similarity index 99% rename from client/backoff/config.go rename to client/retry/config.go index e64a662574f..478187f6f18 100644 --- a/client/backoff/config.go +++ b/client/retry/config.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package backoff +package retry import ( "context" diff --git a/tests/integrations/client/client_test.go b/tests/integrations/client/client_test.go index 55feceb3d04..90836ba3a7c 100644 --- a/tests/integrations/client/client_test.go +++ b/tests/integrations/client/client_test.go @@ -97,7 +97,9 @@ func TestClientClusterIDCheck(t *testing.T) { func TestClientLeaderChange(t *testing.T) { re := require.New(t) re.NoError(failpoint.Enable("github.com/tikv/pd/client/acceleratedMemberUpdateInterval", `return(true)`)) - defer failpoint.Disable("github.com/tikv/pd/client/acceleratedMemberUpdateInterval") + defer func() { + failpoint.Disable("github.com/tikv/pd/client/acceleratedMemberUpdateInterval") + }() ctx, cancel := context.WithCancel(context.Background()) defer cancel() cluster, err := tests.NewTestCluster(ctx, 3) @@ -315,7 +317,9 @@ func TestTSOFollowerProxy(t *testing.T) { func TestUnavailableTimeAfterLeaderIsReady(t *testing.T) { re := require.New(t) re.NoError(failpoint.Enable("github.com/tikv/pd/client/acceleratedMemberUpdateInterval", `return(true)`)) - defer failpoint.Disable("github.com/tikv/pd/client/acceleratedMemberUpdateInterval") + defer func() { + failpoint.Disable("github.com/tikv/pd/client/acceleratedMemberUpdateInterval") + }() ctx, cancel := context.WithCancel(context.Background()) defer cancel() cluster, err := tests.NewTestCluster(ctx, 3) @@ -380,7 +384,9 @@ func TestUnavailableTimeAfterLeaderIsReady(t *testing.T) { func TestGlobalAndLocalTSO(t *testing.T) { re := require.New(t) re.NoError(failpoint.Enable("github.com/tikv/pd/client/acceleratedMemberUpdateInterval", `return(true)`)) - defer failpoint.Disable("github.com/tikv/pd/client/acceleratedMemberUpdateInterval") + defer func() { + failpoint.Disable("github.com/tikv/pd/client/acceleratedMemberUpdateInterval") + }() ctx, cancel := context.WithCancel(context.Background()) defer cancel() dcLocationConfig := map[string]string{