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

[scheduler cleanup phase 1]: Move CacheComparer to pkg/scheduler/inte… #69317

Merged
merged 1 commit into from
Oct 11, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 1 addition & 4 deletions pkg/scheduler/factory/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = [
"cache_comparer.go",
"factory.go",
"plugins.go",
"signal.go",
Expand All @@ -22,10 +21,10 @@ go_library(
"//pkg/scheduler/algorithm/priorities:go_default_library",
"//pkg/scheduler/api:go_default_library",
"//pkg/scheduler/api/validation:go_default_library",
"//pkg/scheduler/cache:go_default_library",
"//pkg/scheduler/core:go_default_library",
"//pkg/scheduler/core/equivalence:go_default_library",
"//pkg/scheduler/internal/cache:go_default_library",
"//pkg/scheduler/internal/cache/comparer:go_default_library",
"//pkg/scheduler/internal/queue:go_default_library",
"//pkg/scheduler/util:go_default_library",
"//pkg/scheduler/volumebinder:go_default_library",
Expand Down Expand Up @@ -57,7 +56,6 @@ go_library(
go_test(
name = "go_default_test",
srcs = [
"cache_comparer_test.go",
"factory_test.go",
"plugins_test.go",
],
Expand All @@ -78,7 +76,6 @@ go_test(
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
"//staging/src/k8s.io/client-go/informers:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
Expand Down
13 changes: 7 additions & 6 deletions pkg/scheduler/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import (
"k8s.io/kubernetes/pkg/scheduler/core"
"k8s.io/kubernetes/pkg/scheduler/core/equivalence"
schedulerinternalcache "k8s.io/kubernetes/pkg/scheduler/internal/cache"
cachecomparer "k8s.io/kubernetes/pkg/scheduler/internal/cache/comparer"
internalqueue "k8s.io/kubernetes/pkg/scheduler/internal/queue"
"k8s.io/kubernetes/pkg/scheduler/util"
"k8s.io/kubernetes/pkg/scheduler/volumebinder"
Expand Down Expand Up @@ -305,12 +306,12 @@ func NewConfigFactory(args *ConfigFactoryArgs) scheduler.Configurator {
}

// Setup cache comparer
comparer := &cacheComparer{
podLister: args.PodInformer.Lister(),
nodeLister: args.NodeInformer.Lister(),
cache: c.schedulerCache,
podQueue: c.podQueue,
}
comparer := cachecomparer.New(
args.NodeInformer.Lister(),
args.PodInformer.Lister(),
c.schedulerCache,
c.podQueue,
)

ch := make(chan os.Signal, 1)
signal.Notify(ch, compareSignal)
Expand Down
1 change: 1 addition & 0 deletions pkg/scheduler/internal/cache/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//pkg/scheduler/internal/cache/comparer:all-srcs",
"//pkg/scheduler/internal/cache/fake:all-srcs",
],
tags = ["automanaged"],
Expand Down
42 changes: 42 additions & 0 deletions pkg/scheduler/internal/cache/comparer/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
srcs = ["comparer.go"],
importpath = "k8s.io/kubernetes/pkg/scheduler/internal/cache/comparer",
visibility = ["//pkg/scheduler:__subpackages__"],
deps = [
"//pkg/scheduler/cache:go_default_library",
"//pkg/scheduler/internal/cache:go_default_library",
"//pkg/scheduler/internal/queue:go_default_library",
"//staging/src/k8s.io/api/core/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/labels:go_default_library",
"//staging/src/k8s.io/client-go/listers/core/v1:go_default_library",
"//vendor/github.com/golang/glog:go_default_library",
],
)

go_test(
name = "go_default_test",
srcs = ["comparer_test.go"],
embed = [":go_default_library"],
deps = [
"//pkg/scheduler/cache:go_default_library",
"//staging/src/k8s.io/api/core/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
],
)

filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)

filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package factory
package comparer

import (
"sort"
Expand All @@ -29,31 +29,47 @@ import (
internalqueue "k8s.io/kubernetes/pkg/scheduler/internal/queue"
)

type cacheComparer struct {
nodeLister corelisters.NodeLister
podLister corelisters.PodLister
cache schedulerinternalcache.Cache
podQueue internalqueue.SchedulingQueue
compareStrategy
// CacheComparer is an implementation of the Scheduler's cache comparer.
type CacheComparer struct {
NodeLister corelisters.NodeLister
PodLister corelisters.PodLister
Cache schedulerinternalcache.Cache
PodQueue internalqueue.SchedulingQueue
}

func (c *cacheComparer) Compare() error {
// New creates a CacheComparer.
func New(
nodeLister corelisters.NodeLister,
podLister corelisters.PodLister,
cache schedulerinternalcache.Cache,
podQueue internalqueue.SchedulingQueue,
) *CacheComparer {
return &CacheComparer{
NodeLister: nodeLister,
PodLister: podLister,
Cache: cache,
PodQueue: podQueue,
}
}

// Compare compares the nodes and pods of NodeLister with Cache.Snapshot.
func (c *CacheComparer) Compare() error {
glog.V(3).Info("cache comparer started")
defer glog.V(3).Info("cache comparer finished")

nodes, err := c.nodeLister.List(labels.Everything())
nodes, err := c.NodeLister.List(labels.Everything())
if err != nil {
return err
}

pods, err := c.podLister.List(labels.Everything())
pods, err := c.PodLister.List(labels.Everything())
if err != nil {
return err
}

snapshot := c.cache.Snapshot()
snapshot := c.Cache.Snapshot()

waitingPods := c.podQueue.WaitingPods()
waitingPods := c.PodQueue.WaitingPods()

if missed, redundant := c.CompareNodes(nodes, snapshot.Nodes); len(missed)+len(redundant) != 0 {
glog.Warningf("cache mismatch: missed nodes: %s; redundant nodes: %s", missed, redundant)
Expand All @@ -66,10 +82,8 @@ func (c *cacheComparer) Compare() error {
return nil
}

type compareStrategy struct {
}

func (c compareStrategy) CompareNodes(nodes []*v1.Node, nodeinfos map[string]*schedulercache.NodeInfo) (missed, redundant []string) {
// CompareNodes compares actual nodes with cached nodes.
func (c *CacheComparer) CompareNodes(nodes []*v1.Node, nodeinfos map[string]*schedulercache.NodeInfo) (missed, redundant []string) {
actual := []string{}
for _, node := range nodes {
actual = append(actual, node.Name)
Expand All @@ -83,7 +97,8 @@ func (c compareStrategy) CompareNodes(nodes []*v1.Node, nodeinfos map[string]*sc
return compareStrings(actual, cached)
}

func (c compareStrategy) ComparePods(pods, waitingPods []*v1.Pod, nodeinfos map[string]*schedulercache.NodeInfo) (missed, redundant []string) {
// ComparePods compares actual pods with cached pods.
func (c *CacheComparer) ComparePods(pods, waitingPods []*v1.Pod, nodeinfos map[string]*schedulercache.NodeInfo) (missed, redundant []string) {
actual := []string{}
for _, pod := range pods {
actual = append(actual, string(pod.UID))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package factory
package comparer

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's trivial, please change package name to comparer_test in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we should name the package as combiner_test? Sorry, I can't understand the benefits of doing this?

But, even if we do, what should we name our file name? If we don't make any changes of files in combiner_test, we will get:
pkg/scheduler/factory/factory.go:63:2: found packages comparer (comparer.go) and comparer (comparer_test.go)

(I'm sure I have set the package name in comparer.go and comparer_test.go as comparer_test. Perhaps contrary to Go's naming rules?)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is coming from my c++ background, where we try to test via public interfaces. For example, suppose one of the structs in our package has a private field protected by a private sync.Mutex. Nothing prevents a unit test from accessing the field without holding the mutex. (Hopefully go test -race would catch it.)

The point is that direct access to private fields might lead us to test the wrong thing. To follow from the previous example, if we have a strong guarantee that all access to the field is protected by a mutex, we can make certain assumptions about concurrent access.

Regardless of the package name, the file name "comparer_test.go" is appropriate.

/lgtm


import (
"reflect"
Expand Down Expand Up @@ -64,7 +64,7 @@ func TestCompareNodes(t *testing.T) {
}

func testCompareNodes(actual, cached, missing, redundant []string, t *testing.T) {
compare := compareStrategy{}
compare := CacheComparer{}
nodes := []*v1.Node{}
for _, nodeName := range actual {
node := &v1.Node{}
Expand Down Expand Up @@ -155,7 +155,7 @@ func TestComparePods(t *testing.T) {
}

func testComparePods(actual, cached, queued, missing, redundant []string, t *testing.T) {
compare := compareStrategy{}
compare := CacheComparer{}
pods := []*v1.Pod{}
for _, uid := range actual {
pod := &v1.Pod{}
Expand Down