-
Notifications
You must be signed in to change notification settings - Fork 39.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57432 from karataliu/azure_vmget_cache
Automatic merge from submit-queue (batch tested with PRs 49856, 56257, 57027, 57695, 57432). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Add cache for VirtualMachinesClient.Get in azure cloud provider **What this PR does / why we need it**: Add a timed cache for 'VirtualMachinesClient.Get' Currently cloud provider will send several get calls to same URL in short period, which is not necessary. **Which issue(s) this PR fixes**: Fixes #57031 **Special notes for your reviewer**: **Release note**: NONE
- Loading branch information
Showing
6 changed files
with
245 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
Copyright 2017 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package azure | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"k8s.io/client-go/tools/cache" | ||
) | ||
|
||
type timedcacheEntry struct { | ||
key string | ||
data interface{} | ||
} | ||
|
||
type timedcache struct { | ||
store cache.Store | ||
lock sync.Mutex | ||
} | ||
|
||
// ttl time.Duration | ||
func newTimedcache(ttl time.Duration) timedcache { | ||
return timedcache{ | ||
store: cache.NewTTLStore(cacheKeyFunc, ttl), | ||
} | ||
} | ||
|
||
func cacheKeyFunc(obj interface{}) (string, error) { | ||
return obj.(*timedcacheEntry).key, nil | ||
} | ||
|
||
func (t *timedcache) GetOrCreate(key string, createFunc func() interface{}) (interface{}, error) { | ||
entry, exists, err := t.store.GetByKey(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if exists { | ||
return (entry.(*timedcacheEntry)).data, nil | ||
} | ||
|
||
t.lock.Lock() | ||
defer t.lock.Unlock() | ||
entry, exists, err = t.store.GetByKey(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if exists { | ||
return (entry.(*timedcacheEntry)).data, nil | ||
} | ||
|
||
if createFunc == nil { | ||
return nil, nil | ||
} | ||
created := createFunc() | ||
t.store.Add(&timedcacheEntry{ | ||
key: key, | ||
data: created, | ||
}) | ||
return created, nil | ||
} | ||
|
||
func (t *timedcache) Delete(key string) { | ||
_ = t.store.Delete(&timedcacheEntry{ | ||
key: key, | ||
}) | ||
} |
96 changes: 96 additions & 0 deletions
96
pkg/cloudprovider/providers/azure/azure_util_cache_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
Copyright 2017 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package azure | ||
|
||
import ( | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestCacheReturnsSameObject(t *testing.T) { | ||
type cacheTestingStruct struct{} | ||
c := newTimedcache(1 * time.Minute) | ||
o1 := cacheTestingStruct{} | ||
get1, _ := c.GetOrCreate("b1", func() interface{} { | ||
return o1 | ||
}) | ||
o2 := cacheTestingStruct{} | ||
get2, _ := c.GetOrCreate("b1", func() interface{} { | ||
return o2 | ||
}) | ||
if get1 != get2 { | ||
t.Error("Get not equal") | ||
} | ||
} | ||
|
||
func TestCacheCallsCreateFuncOnce(t *testing.T) { | ||
var callsCount uint32 | ||
f1 := func() interface{} { | ||
atomic.AddUint32(&callsCount, 1) | ||
return 1 | ||
} | ||
c := newTimedcache(500 * time.Millisecond) | ||
for index := 0; index < 20; index++ { | ||
_, _ = c.GetOrCreate("b1", f1) | ||
} | ||
|
||
if callsCount != 1 { | ||
t.Error("Count not match") | ||
} | ||
time.Sleep(500 * time.Millisecond) | ||
c.GetOrCreate("b1", f1) | ||
if callsCount != 2 { | ||
t.Error("Count not match") | ||
} | ||
} | ||
|
||
func TestCacheExpires(t *testing.T) { | ||
f1 := func() interface{} { | ||
return 1 | ||
} | ||
c := newTimedcache(500 * time.Millisecond) | ||
get1, _ := c.GetOrCreate("b1", f1) | ||
if get1 != 1 { | ||
t.Error("Value not equal") | ||
} | ||
time.Sleep(500 * time.Millisecond) | ||
get1, _ = c.GetOrCreate("b1", nil) | ||
if get1 != nil { | ||
t.Error("value not expired") | ||
} | ||
} | ||
|
||
func TestCacheDelete(t *testing.T) { | ||
f1 := func() interface{} { | ||
return 1 | ||
} | ||
c := newTimedcache(500 * time.Millisecond) | ||
get1, _ := c.GetOrCreate("b1", f1) | ||
if get1 != 1 { | ||
t.Error("Value not equal") | ||
} | ||
get1, _ = c.GetOrCreate("b1", nil) | ||
if get1 != 1 { | ||
t.Error("Value not equal") | ||
} | ||
c.Delete("b1") | ||
get1, _ = c.GetOrCreate("b1", nil) | ||
if get1 != nil { | ||
t.Error("value not deleted") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters