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

[Backport release-v1.3] fix yurthub memory leak #1562

Merged
merged 1 commit into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion pkg/yurthub/poolcoordinator/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,9 @@ func (coordinator *coordinator) Run() {
continue
}

klog.Infof("coordinator newCloudLeaseClient success.")
if err := coordinator.poolCacheSyncManager.EnsureStart(); err != nil {
klog.Errorf("failed to sync pool-scoped resource, %v", err)
cancelEtcdStorage()
coordinator.statusInfoChan <- electorStatusInfo
continue
}
Expand All @@ -299,9 +299,11 @@ func (coordinator *coordinator) Run() {
nodeLeaseProxyClient, err := coordinator.newNodeLeaseProxyClient()
if err != nil {
klog.Errorf("cloud not get cloud lease client when becoming leader yurthub, %v", err)
cancelEtcdStorage()
coordinator.statusInfoChan <- electorStatusInfo
continue
}
klog.Infof("coordinator newCloudLeaseClient success.")
coordinator.delegateNodeLeaseManager.EnsureStartWithHandler(cache.FilteringResourceEventHandler{
FilterFunc: ifDelegateHeartBeat,
Handler: cache.ResourceEventHandlerFuncs{
Expand Down
7 changes: 5 additions & 2 deletions pkg/yurthub/storage/etcd/keycache.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,12 @@ func (c *componentKeyCache) Recover() error {

func (c *componentKeyCache) getPoolScopedKeyset() (*keyCache, error) {
keys := &keyCache{m: make(map[schema.GroupVersionResource]storageKeySet)}
for _, gvr := range c.poolScopedResourcesGetter() {
getFunc := func(key string) (*clientv3.GetResponse, error) {
getCtx, cancel := context.WithTimeout(c.ctx, defaultTimeout)
defer cancel()
return c.etcdClient.Get(getCtx, key, clientv3.WithPrefix(), clientv3.WithKeysOnly())
}
for _, gvr := range c.poolScopedResourcesGetter() {
rootKey, err := c.keyFunc(storage.KeyBuildInfo{
Component: coordinatorconstants.DefaultPoolScopedUserAgent,
Group: gvr.Group,
Expand All @@ -120,7 +123,7 @@ func (c *componentKeyCache) getPoolScopedKeyset() (*keyCache, error) {
if err != nil {
return nil, fmt.Errorf("failed to generate keys for %s, %v", gvr.String(), err)
}
getResp, err := c.etcdClient.Get(getCtx, rootKey.Key(), clientv3.WithPrefix(), clientv3.WithKeysOnly())
getResp, err := getFunc(rootKey.Key())
if err != nil {
return nil, fmt.Errorf("failed to get from etcd for %s, %v", gvr.String(), err)
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/yurthub/storage/etcd/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ func NewStorage(ctx context.Context, cfg *EtcdStorageConfig) (storage.Store, err
poolScopedResourcesGetter: resources.GetPoolScopeResources,
}
if err := cache.Recover(); err != nil {
if err := client.Close(); err != nil {
return nil, fmt.Errorf("failed to close etcd client, %v", err)
}
return nil, fmt.Errorf("failed to recover component key cache from %s, %v", cacheFilePath, err)
}
s.localComponentKeyCache = cache
Expand Down Expand Up @@ -182,6 +185,9 @@ func (s *etcdStorage) clientLifeCycleManagement() {
for {
select {
case <-s.ctx.Done():
if err := s.client.Close(); err != nil {
klog.Errorf("failed to close etcd client, %v", err)
}
klog.Info("etcdstorage lifecycle routine exited")
return
default:
Expand Down