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

Add a caching layer, so we don't pound cloud providers for ip addresses. #1560

Merged
merged 1 commit into from
Oct 3, 2014
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
42 changes: 39 additions & 3 deletions pkg/registry/pod/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ import (
"github.com/golang/glog"
)

type ipCacheEntry struct {
ip string
Copy link
Member

Choose a reason for hiding this comment

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

What about net.IP, which we've discussed using in the API (#1300)?

lastUpdate time.Time
}

type ipCache map[string]ipCacheEntry

type clock interface {
Now() time.Time
}

type realClock struct{}

func (r realClock) Now() time.Time {
return time.Now()
}

// REST implements the RESTStorage interface in terms of a PodRegistry.
type REST struct {
cloudProvider cloudprovider.Interface
Expand All @@ -45,6 +62,8 @@ type REST struct {
podPollPeriod time.Duration
registry Registry
minions client.MinionInterface
ipCache ipCache
clock clock
}

type RESTConfig struct {
Expand All @@ -64,6 +83,8 @@ func NewREST(config *RESTConfig) *REST {
podPollPeriod: time.Second * 10,
registry: config.Registry,
minions: config.Minions,
ipCache: ipCache{},
clock: realClock{},
}
}

Expand Down Expand Up @@ -112,7 +133,7 @@ func (rs *REST) Get(ctx api.Context, id string) (runtime.Object, error) {
}
pod.CurrentState.Status = status
}
pod.CurrentState.HostIP = getInstanceIP(rs.cloudProvider, pod.CurrentState.Host)
pod.CurrentState.HostIP = rs.getInstanceIP(pod.CurrentState.Host)
return pod, err
}

Expand Down Expand Up @@ -144,7 +165,7 @@ func (rs *REST) List(ctx api.Context, label, field labels.Selector) (runtime.Obj
return pod, err
}
pod.CurrentState.Status = status
pod.CurrentState.HostIP = getInstanceIP(rs.cloudProvider, pod.CurrentState.Host)
pod.CurrentState.HostIP = rs.getInstanceIP(pod.CurrentState.Host)
}
}
return pods, err
Expand Down Expand Up @@ -212,7 +233,22 @@ func (rs *REST) fillPodInfo(pod *api.Pod) {
}
}

func getInstanceIP(cloud cloudprovider.Interface, host string) string {
func (rs *REST) getInstanceIP(host string) string {
data, ok := rs.ipCache[host]
now := rs.clock.Now()

if !ok || now.Sub(data.lastUpdate) > (30*time.Second) {
Copy link
Member

Choose a reason for hiding this comment

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

The pod polling interval is 10 seconds. Is 30 seconds long enough?

Anyway, I suppose we can always tune this later, make it configurable, etc.

ip := getInstanceIPFromCloud(rs.cloudProvider, host)
data = ipCacheEntry{
ip: ip,
lastUpdate: now,
}
rs.ipCache[host] = data
}
return data.ip
}

func getInstanceIPFromCloud(cloud cloudprovider.Interface, host string) string {
if cloud == nil {
return ""
}
Expand Down
35 changes: 35 additions & 0 deletions pkg/registry/pod/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ func TestListEmptyPodList(t *testing.T) {
}
}

type fakeClock struct {
t time.Time
}

func (f *fakeClock) Now() time.Time {
return f.t
}

func TestListPodList(t *testing.T) {
podRegistry := registrytest.NewPodRegistry(nil)
podRegistry.Pods = &api.PodList{
Expand All @@ -181,6 +189,8 @@ func TestListPodList(t *testing.T) {
}
storage := REST{
registry: podRegistry,
ipCache: ipCache{},
clock: &fakeClock{},
}
ctx := api.NewContext()
podsObj, err := storage.List(ctx, labels.Everything(), labels.Everything())
Expand Down Expand Up @@ -222,6 +232,8 @@ func TestListPodListSelection(t *testing.T) {
}
storage := REST{
registry: podRegistry,
ipCache: ipCache{},
clock: &fakeClock{},
}
ctx := api.NewContext()

Expand Down Expand Up @@ -311,6 +323,8 @@ func TestGetPod(t *testing.T) {
podRegistry.Pod = &api.Pod{JSONBase: api.JSONBase{ID: "foo"}}
storage := REST{
registry: podRegistry,
ipCache: ipCache{},
clock: &fakeClock{},
}
ctx := api.NewContext()
obj, err := storage.Get(ctx, "foo")
Expand All @@ -328,9 +342,14 @@ func TestGetPodCloud(t *testing.T) {
fakeCloud := &fake_cloud.FakeCloud{}
podRegistry := registrytest.NewPodRegistry(nil)
podRegistry.Pod = &api.Pod{JSONBase: api.JSONBase{ID: "foo"}}

clock := &fakeClock{t: time.Now()}

storage := REST{
registry: podRegistry,
cloudProvider: fakeCloud,
ipCache: ipCache{},
clock: clock,
}
ctx := api.NewContext()
obj, err := storage.Get(ctx, "foo")
Expand All @@ -342,9 +361,25 @@ func TestGetPodCloud(t *testing.T) {
if e, a := podRegistry.Pod, pod; !reflect.DeepEqual(e, a) {
t.Errorf("Unexpected pod. Expected %#v, Got %#v", e, a)
}

// This call should hit the cache, so we expect no additional calls to the cloud
obj, err = storage.Get(ctx, "foo")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if len(fakeCloud.Calls) != 1 || fakeCloud.Calls[0] != "ip-address" {
t.Errorf("Unexpected calls: %#v", fakeCloud.Calls)
}

// Advance the clock, this call should miss the cache, so expect one more call.
clock.t = clock.t.Add(60 * time.Second)
obj, err = storage.Get(ctx, "foo")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if len(fakeCloud.Calls) != 2 || fakeCloud.Calls[1] != "ip-address" {
t.Errorf("Unexpected calls: %#v", fakeCloud.Calls)
}
}

func TestMakePodStatus(t *testing.T) {
Expand Down