Skip to content
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

etcdutil: remove stale client endpoints for healthyChecker (#7227) #7387

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion pkg/utils/etcdutil/etcdutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,18 @@
}

func (checker *healthyChecker) update(eps []string) {
epMap := make(map[string]struct{})
for _, ep := range eps {
epMap[ep] = struct{}{}
}

for ep := range epMap {
// check if client exists, if not, create one, if exists, check if it's offline or disconnected.
if client, ok := checker.Load(ep); ok {
lastHealthy := client.(*healthyClient).lastHealth
if time.Since(lastHealthy) > etcdServerOfflineTimeout {
log.Info("some etcd server maybe offline", zap.String("endpoint", ep))
checker.Delete(ep)
checker.removeClient(ep)

Check warning on line 396 in pkg/utils/etcdutil/etcdutil.go

View check run for this annotation

Codecov / codecov/patch

pkg/utils/etcdutil/etcdutil.go#L396

Added line #L396 was not covered by tests
}
if time.Since(lastHealthy) > etcdServerDisconnectedTimeout {
// try to reset client endpoint to trigger reconnect
Expand All @@ -399,6 +404,16 @@
}
checker.addClient(ep, time.Now())
}

// check if there are some stale clients, if exists, remove them.
checker.Range(func(key, value interface{}) bool {
ep := key.(string)
if _, ok := epMap[ep]; !ok {
log.Info("remove stale etcd client", zap.String("endpoint", ep))
checker.removeClient(ep)
}
return true
})
}

func (checker *healthyChecker) addClient(ep string, lastHealth time.Time) {
Expand All @@ -413,6 +428,15 @@
})
}

func (checker *healthyChecker) removeClient(ep string) {
if client, ok := checker.LoadAndDelete(ep); ok {
err := client.(*healthyClient).Close()
if err != nil {
log.Error("failed to close etcd healthy client", zap.Error(err))
}
}
}

func syncUrls(client *clientv3.Client) []string {
// See https://github.com/etcd-io/etcd/blob/85b640cee793e25f3837c47200089d14a8392dc7/clientv3/client.go#L170-L183
ctx, cancel := context.WithTimeout(clientv3.WithRequireLeader(client.Ctx()), DefaultRequestTimeout)
Expand Down
Loading