Repository: kubernetes/kubernetes
File: staging/src/k8s.io/apiserver/pkg/storage/cacher/delegator.go (Go)

Code before:
		trackingList = append(trackingList, namespaceNameRV{Namespace: obj.GetNamespace(), Name: obj.GetName(), RV: obj.GetResourceVersion()})
	})
	if err != nil {
		return nil, fmt.Errorf("failed calculating cache digest: %w", err)
	}
	etcdDigest, etcdResourceVersion, err := c.calculateStoreDigest(ctx, c.etcd, cacheResourceVersion, storageWatchListPageSize, func(obj metav1.Object) {
		// compare the item in etcd's list against trackingList which is cacher's list
		if foundDiff {
			return
		}
		item := namespaceNameRV{Namespace: obj.GetNamespace(), Name: obj.GetName(), RV: obj.GetResourceVersion()}
		if len(trackingList) == 0 {
			foundDiff = true
			klog.ErrorS(nil, "cache consistency checker found a diff: etcd has more items", "etcdItem", item)
		} else {
			if trackingList[0] != item {
				foundDiff = true
				klog.ErrorS(nil, "cache consistency checker found a diff:", "cacheItem", trackingList[0], "etcdItem", item)
			}
			// This is cheap since it slices existing slice with no copying.
			trackingList = trackingList[1:]
		}
	})
	if len(trackingList) > 0 {
		klog.ErrorS(nil, "cache consistency checker found a diff: cache has more items", "cacheItem", trackingList[0])
	}
	if err != nil {
		return nil, fmt.Errorf("failed calculating etcd digest: %w", err)
	}
	if cacheResourceVersion != etcdResourceVersion {
		return nil, fmt.Errorf("etcd returned different resource version then expected, cache: %q, etcd: %q", cacheResourceVersion, etcdResourceVersion)
	}
	return &storageDigest{
		ResourceVersion: cacheResourceVersion,
		CacheDigest:     cacheDigest,
		EtcdDigest:      etcdDigest,
	}, nil
}

type namespaceNameRV struct {
	Namespace string
	Name      string
	RV        string
}

type storageDigest struct {
	ResourceVersion string
	CacheDigest     string
	EtcdDigest      string
}


Review comment (refactor):
move this after the err check... if we error, we're likely to have spurious items left in trackingList and this message won't really be accurate

Code after:
	if err != nil {
		return nil, fmt.Errorf("failed calculating cache digest: %w", err)
	}
	etcdDigest, etcdResourceVersion, err := c.calculateStoreDigest(ctx, c.etcd, cacheResourceVersion, storageWatchListPageSize, func(obj metav1.Object) {
		// compare the item in etcd's list against cacheItems which is cacher's list
		if foundDiff != nil {
			return
		}
		etcdItem := namespaceNameRV{Namespace: obj.GetNamespace(), Name: obj.GetName(), RV: obj.GetResourceVersion()}
		if len(cacheItems) <= etcdIndex {
			foundDiff = &diffDetail{Index: etcdIndex, EtcdItem: &etcdItem}
			cacheItems = nil // don't need it any more and nil it to allow GC to collect it
			return
		}
		if cacheItem := cacheItems[etcdIndex]; cacheItem != etcdItem {
			foundDiff = &diffDetail{Index: etcdIndex, EtcdItem: &etcdItem, CacheItem: &cacheItem}
			cacheItems = nil // don't need it any more and nil it to allow GC to collect it
			return
		}
		etcdIndex += 1
	})
	if err != nil {
		return nil, fmt.Errorf("failed calculating etcd digest: %w", err)
	}
	if len(cacheItems) > etcdIndex {
		cacheItem := cacheItems[etcdIndex]
		foundDiff = &diffDetail{Index: etcdIndex, CacheItem: &cacheItem}
	}
	if cacheResourceVersion != etcdResourceVersion {
		return nil, fmt.Errorf("etcd returned different resource version then expected, cache: %q, etcd: %q", cacheResourceVersion, etcdResourceVersion)
	}
	return &storageDigest{
		ResourceVersion: cacheResourceVersion,
		CacheDigest:     cacheDigest,
		EtcdDigest:      etcdDigest,
		DiffDetail:      foundDiff,
	}, nil
}

type namespaceNameRV struct {
	Namespace string
	Name      string
	RV        string
}

type diffDetail struct {
	Index     int
	CacheItem *namespaceNameRV
	EtcdItem  *namespaceNameRV
}