-
Notifications
You must be signed in to change notification settings - Fork 40k
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
Add a system modeler to scheduler #5446
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,155 @@ | ||||
/* | ||||
Copyright 2015 Google Inc. All rights reserved. | ||||
|
||||
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 scheduler | ||||
|
||||
import ( | ||||
"fmt" | ||||
"strings" | ||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" | ||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/cache" | ||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels" | ||||
algorithm "github.com/GoogleCloudPlatform/kubernetes/pkg/scheduler" | ||||
|
||||
"github.com/golang/glog" | ||||
) | ||||
|
||||
var ( | ||||
_ = SystemModeler(&FakeModeler{}) | ||||
_ = SystemModeler(&SimpleModeler{}) | ||||
) | ||||
|
||||
// ExtendedPodLister: SimpleModeler needs to be able to check for a pod's | ||||
// existance in addition to listing the pods. | ||||
type ExtendedPodLister interface { | ||||
algorithm.PodLister | ||||
Exists(pod *api.Pod) (bool, error) | ||||
} | ||||
|
||||
// FakeModeler implements the SystemModeler interface. | ||||
type FakeModeler struct { | ||||
AssumePodFunc func(pod *api.Pod) | ||||
} | ||||
|
||||
// AssumePod calls the function variable if it is not nil. | ||||
func (f *FakeModeler) AssumePod(pod *api.Pod) { | ||||
if f.AssumePodFunc != nil { | ||||
f.AssumePodFunc(pod) | ||||
} | ||||
} | ||||
|
||||
// SimpleModeler implements the SystemModeler interface with a timed pod cache. | ||||
type SimpleModeler struct { | ||||
queuedPods ExtendedPodLister | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @lavalamp There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be more specific, I am pointing to this code block:
c.ScheduledPodLister.Store, c.scheduledPodPopulator = framework.NewInformer(
c.createAssignedPodLW(),
...
DeleteFunc: func(obj interface{}) {
c.modeler.LockedAction(func() {
switch t := obj.(type) {
case *api.Pod:
c.modeler.ForgetPod(t)
case cache.DeletedFinalStateUnknown:
c.modeler.ForgetPodByKey(t.Key)
}
}) If a pod is rejected or removed, it's gonna be deleted and notified with delete event. Right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hongchaodeng IIRC, at the time there was a race where a rejected pod could show up in the watch before the binding failure propagated back? Note that this code was written before the informer was created. So this race might not be possible any more. |
||||
scheduledPods ExtendedPodLister | ||||
|
||||
// assumedPods holds the pods that we think we've scheduled, but that | ||||
// haven't yet shown up in the scheduledPods variable. | ||||
// TODO: periodically clear this. | ||||
assumedPods *cache.StoreToPodLister | ||||
} | ||||
|
||||
// NewSimpleModeler returns a new SimpleModeler. | ||||
// queuedPods: a PodLister that will return pods that have not been scheduled yet. | ||||
// scheduledPods: a PodLister that will return pods that we know for sure have been scheduled. | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also comment assumedPods, for completeness? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added documentation in the struct-- the godoc comment isn't a good place to document private members. |
||||
func NewSimpleModeler(queuedPods, scheduledPods ExtendedPodLister) *SimpleModeler { | ||||
return &SimpleModeler{ | ||||
queuedPods: queuedPods, | ||||
scheduledPods: scheduledPods, | ||||
assumedPods: &cache.StoreToPodLister{cache.NewStore(cache.MetaNamespaceKeyFunc)}, | ||||
} | ||||
} | ||||
|
||||
func (s *SimpleModeler) AssumePod(pod *api.Pod) { | ||||
s.assumedPods.Add(pod) | ||||
} | ||||
|
||||
// Extract names for readable logging. | ||||
func podNames(pods []api.Pod) []string { | ||||
out := make([]string, len(pods)) | ||||
for i := range pods { | ||||
out[i] = fmt.Sprintf("'%v/%v (%v)'", pods[i].Namespace, pods[i].Name, pods[i].UID) | ||||
} | ||||
return out | ||||
} | ||||
|
||||
func (s *SimpleModeler) listPods(selector labels.Selector) (pods []api.Pod, err error) { | ||||
assumed, err := s.assumedPods.List(selector) | ||||
if err != nil { | ||||
return nil, err | ||||
} | ||||
// Since the assumed list will be short, just check every one. | ||||
// Goal here is to stop making assumptions about a pod once it shows | ||||
// up in one of these other lists. | ||||
// TODO: there's a possibility that a pod could get deleted at the | ||||
// exact wrong time and linger in assumedPods forever. So we | ||||
// need go through that periodically and check for deleted | ||||
// pods. | ||||
for _, pod := range assumed { | ||||
qExist, err := s.queuedPods.Exists(&pod) | ||||
if err != nil { | ||||
return nil, err | ||||
} | ||||
if qExist { | ||||
s.assumedPods.Store.Delete(&pod) | ||||
continue | ||||
} | ||||
sExist, err := s.scheduledPods.Exists(&pod) | ||||
if err != nil { | ||||
return nil, err | ||||
} | ||||
if sExist { | ||||
s.assumedPods.Store.Delete(&pod) | ||||
continue | ||||
} | ||||
} | ||||
|
||||
scheduled, err := s.scheduledPods.List(selector) | ||||
if err != nil { | ||||
return nil, err | ||||
} | ||||
// re-get in case we deleted any. | ||||
assumed, err = s.assumedPods.List(selector) | ||||
if err != nil { | ||||
return nil, err | ||||
} | ||||
if len(assumed) == 0 { | ||||
return scheduled, nil | ||||
} | ||||
glog.V(2).Infof( | ||||
"listing pods: [%v] assumed to exist in addition to %v known pods.", | ||||
strings.Join(podNames(assumed), ","), | ||||
len(scheduled), | ||||
) | ||||
return append(scheduled, assumed...), nil | ||||
} | ||||
|
||||
// PodLister returns a PodLister that will list pods that we think we have scheduled in | ||||
// addition to pods that we know have been scheduled. | ||||
func (s *SimpleModeler) PodLister() algorithm.PodLister { | ||||
return simpleModelerPods{s} | ||||
} | ||||
|
||||
// simpleModelerPods is an adaptor so that SimpleModeler can be a PodLister. | ||||
type simpleModelerPods struct { | ||||
simpleModeler *SimpleModeler | ||||
} | ||||
|
||||
// List returns pods known and assumed to exist. | ||||
func (s simpleModelerPods) List(selector labels.Selector) (pods []api.Pod, err error) { | ||||
return s.simpleModeler.listPods(selector) | ||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
Copyright 2015 Google Inc. All rights reserved. | ||
|
||
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 scheduler | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" | ||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/cache" | ||
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels" | ||
) | ||
|
||
type nn struct { | ||
namespace, name string | ||
} | ||
|
||
type names []nn | ||
|
||
func (ids names) list() []api.Pod { | ||
out := make([]api.Pod, len(ids)) | ||
for i, id := range ids { | ||
out[i] = api.Pod{ | ||
ObjectMeta: api.ObjectMeta{ | ||
Namespace: id.namespace, | ||
Name: id.name, | ||
}, | ||
} | ||
} | ||
return out | ||
} | ||
|
||
func (ids names) has(pod *api.Pod) bool { | ||
for _, id := range ids { | ||
if pod.Namespace == id.namespace && pod.Name == id.name { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func TestModeler(t *testing.T) { | ||
table := []struct { | ||
queuedPods []api.Pod | ||
scheduledPods []api.Pod | ||
assumedPods []api.Pod | ||
expectPods names | ||
}{ | ||
{ | ||
queuedPods: names{}.list(), | ||
scheduledPods: names{{"default", "foo"}, {"custom", "foo"}}.list(), | ||
assumedPods: names{{"default", "foo"}}.list(), | ||
expectPods: names{{"default", "foo"}, {"custom", "foo"}}, | ||
}, { | ||
queuedPods: names{}.list(), | ||
scheduledPods: names{{"default", "foo"}}.list(), | ||
assumedPods: names{{"default", "foo"}, {"custom", "foo"}}.list(), | ||
expectPods: names{{"default", "foo"}, {"custom", "foo"}}, | ||
}, { | ||
queuedPods: names{{"custom", "foo"}}.list(), | ||
scheduledPods: names{{"default", "foo"}}.list(), | ||
assumedPods: names{{"default", "foo"}, {"custom", "foo"}}.list(), | ||
expectPods: names{{"default", "foo"}}, | ||
}, | ||
} | ||
|
||
for _, item := range table { | ||
q := &cache.StoreToPodLister{cache.NewStore(cache.MetaNamespaceKeyFunc)} | ||
for i := range item.queuedPods { | ||
q.Store.Add(&item.queuedPods[i]) | ||
} | ||
s := &cache.StoreToPodLister{cache.NewStore(cache.MetaNamespaceKeyFunc)} | ||
for i := range item.scheduledPods { | ||
s.Store.Add(&item.scheduledPods[i]) | ||
} | ||
m := NewSimpleModeler(q, s) | ||
for i := range item.assumedPods { | ||
m.AssumePod(&item.assumedPods[i]) | ||
} | ||
|
||
list, err := m.PodLister().List(labels.Everything()) | ||
if err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
} | ||
|
||
found := 0 | ||
for _, pod := range list { | ||
if item.expectPods.has(&pod) { | ||
found++ | ||
} else { | ||
t.Errorf("found unexpected pod %#v", pod) | ||
} | ||
} | ||
if e, a := item.expectPods, found; len(e) != a { | ||
t.Errorf("Expected pods:\n%+v\nFound pods:\n%v\n", e, list) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would drop this comment; it doesn't add much.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
go vet
again... Yeah, I don't know, I try to follow 'godoc style' which requires a comment of this form for all public items. Will leave unless you feel realllly strongly.