Merge pull request #15619 from wojtek-t/avoid_unnecessary_copies

Auto commit by PR queue bot
pull/6/head
k8s-merge-robot 2015-10-14 07:32:29 -07:00
commit 7e0e102b15
3 changed files with 11 additions and 6 deletions

View File

@ -323,8 +323,9 @@ func (h *etcdHelper) decodeNodeList(nodes []*etcd.Node, filter storage.FilterFun
trace.Step("Decoding dir " + node.Key + " END") trace.Step("Decoding dir " + node.Key + " END")
continue continue
} }
if obj, found := h.getFromCache(node.ModifiedIndex); found { if obj, found := h.getFromCache(node.ModifiedIndex, filter); found {
if filter(obj) { // obj != nil iff it matches the filter function.
if obj != nil {
v.Set(reflect.Append(v, reflect.ValueOf(obj).Elem())) v.Set(reflect.Append(v, reflect.ValueOf(obj).Elem()))
} }
} else { } else {
@ -498,7 +499,7 @@ func (h *etcdHelper) prefixEtcdKey(key string) string {
// their Node.ModifiedIndex, which is unique across all types. // their Node.ModifiedIndex, which is unique across all types.
// All implementations must be thread-safe. // All implementations must be thread-safe.
type etcdCache interface { type etcdCache interface {
getFromCache(index uint64) (runtime.Object, bool) getFromCache(index uint64, filter storage.FilterFunc) (runtime.Object, bool)
addToCache(index uint64, obj runtime.Object) addToCache(index uint64, obj runtime.Object)
} }
@ -508,18 +509,22 @@ func getTypeName(obj interface{}) string {
return reflect.TypeOf(obj).String() return reflect.TypeOf(obj).String()
} }
func (h *etcdHelper) getFromCache(index uint64) (runtime.Object, bool) { func (h *etcdHelper) getFromCache(index uint64, filter storage.FilterFunc) (runtime.Object, bool) {
startTime := time.Now() startTime := time.Now()
defer func() { defer func() {
metrics.ObserveGetCache(startTime) metrics.ObserveGetCache(startTime)
}() }()
obj, found := h.cache.Get(index) obj, found := h.cache.Get(index)
if found { if found {
if !filter(obj.(runtime.Object)) {
return nil, true
}
// We should not return the object itself to avoid polluting the cache if someone // We should not return the object itself to avoid polluting the cache if someone
// modifies returned values. // modifies returned values.
objCopy, err := h.copier.Copy(obj.(runtime.Object)) objCopy, err := h.copier.Copy(obj.(runtime.Object))
if err != nil { if err != nil {
glog.Errorf("Error during DeepCopy of cached object: %q", err) glog.Errorf("Error during DeepCopy of cached object: %q", err)
// We can't return a copy, thus we report the object as not found.
return nil, false return nil, false
} }
metrics.ObserveCacheHit() metrics.ObserveCacheHit()

View File

@ -208,7 +208,7 @@ func (w *etcdWatcher) translate() {
} }
func (w *etcdWatcher) decodeObject(node *etcd.Node) (runtime.Object, error) { func (w *etcdWatcher) decodeObject(node *etcd.Node) (runtime.Object, error) {
if obj, found := w.cache.getFromCache(node.ModifiedIndex); found { if obj, found := w.cache.getFromCache(node.ModifiedIndex, storage.Everything); found {
return obj, nil return obj, nil
} }

View File

@ -40,7 +40,7 @@ var versioner = APIObjectVersioner{}
// Implements etcdCache interface as empty methods (i.e. does not cache any objects) // Implements etcdCache interface as empty methods (i.e. does not cache any objects)
type fakeEtcdCache struct{} type fakeEtcdCache struct{}
func (f *fakeEtcdCache) getFromCache(index uint64) (runtime.Object, bool) { func (f *fakeEtcdCache) getFromCache(index uint64, filter storage.FilterFunc) (runtime.Object, bool) {
return nil, false return nil, false
} }