Skip to content

Commit

Permalink
agent,resmgr: merge PodResources{List,Map}, cache list.
Browse files Browse the repository at this point in the history
Merge PodResourceMap into PodResourcesList. Avoid unnecessary
re-listing of pod resources by caching the result of the last
list and trying to reuse it to look up resources.

Signed-off-by: Krisztian Litkey <[email protected]>
  • Loading branch information
klihub authored and askervin committed Dec 17, 2024
1 parent ab71151 commit 3fb1ab2
Show file tree
Hide file tree
Showing 5 changed files with 384 additions and 36 deletions.
6 changes: 3 additions & 3 deletions pkg/agent/pod-resource-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,20 @@ func (a *Agent) GoGetPodResources(ns, pod string, timeout time.Duration) <-chan
}

// ListPodResources lists all pods' resources.
func (a *Agent) ListPodResources(timeout time.Duration) (podresapi.PodResourcesList, error) {
func (a *Agent) ListPodResources(timeout time.Duration) (*podresapi.PodResourcesList, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

return a.podResCli.List(ctx)
}

// GoListPodResources lists all pods' resources asynchronously.
func (a *Agent) GoListPodResources(timeout time.Duration) <-chan podresapi.PodResourcesList {
func (a *Agent) GoListPodResources(timeout time.Duration) <-chan *podresapi.PodResourcesList {
if !a.podResCli.HasClient() {
return nil
}

ch := make(chan podresapi.PodResourcesList, 1)
ch := make(chan *podresapi.PodResourcesList, 1)

go func() {
defer close(ch)
Expand Down
11 changes: 9 additions & 2 deletions pkg/agent/podresapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Client struct {
conn *grpc.ClientConn
cli api.PodResourcesListerClient
noGet bool
cached *PodResourcesList
}

const (
Expand Down Expand Up @@ -105,7 +106,7 @@ func (c *Client) HasClient() bool {
}

// List lists all pods' resources.
func (c *Client) List(ctx context.Context) (PodResourcesList, error) {
func (c *Client) List(ctx context.Context) (*PodResourcesList, error) {
if !c.HasClient() {
return nil, nil
}
Expand All @@ -115,7 +116,9 @@ func (c *Client) List(ctx context.Context) (PodResourcesList, error) {
return nil, fmt.Errorf("failed to get pod resources by list: %w", err)
}

return PodResourcesList(reply.GetPodResources()), nil
c.cached = NewPodResourcesList(reply.GetPodResources())

return c.cached, nil
}

// Get queries the given pod's resources.
Expand Down Expand Up @@ -144,6 +147,10 @@ func (c *Client) Get(ctx context.Context, namespace, pod string) (*PodResources,
c.noGet = true
}

if r := c.cached.GetPodResources(namespace, pod); r != nil {
return r, nil
}

l, err := c.List(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get pod resources: %w", err)
Expand Down
76 changes: 50 additions & 26 deletions pkg/agent/podresapi/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ type ContainerResources struct {
*api.ContainerResources
}

// PodResourcesList is a list of PodResources.
type PodResourcesList []*api.PodResources

// PodResourceMap is a map representation of PodResourcesList.
type PodResourcesMap map[string]map[string]*PodResources
// PodResourcesList containers the result of a pod resources list query.
type PodResourcesList struct {
l []*api.PodResources
m map[string]map[string]*PodResources
}

// GetContainer returns resources for the given container.
func (p *PodResources) GetContainer(ctr string) *ContainerResources {
Expand All @@ -53,41 +53,65 @@ func (p *PodResources) GetContainer(ctr string) *ContainerResources {
return nil
}

// GetPodResources returns resources for the given pod.
func (l PodResourcesList) GetPodResources(ns, pod string) *PodResources {
for _, p := range l {
if p.GetNamespace() == ns && p.GetName() == pod {
return &PodResources{p}
}
func NewPodResourcesList(l []*api.PodResources) *PodResourcesList {
return &PodResourcesList{
l: l,
m: make(map[string]map[string]*PodResources),
}
}

return nil
func (l *PodResourcesList) Len() int {
if l == nil {
return 0
}

cnt := len(l.l)
for _, m := range l.m {
cnt += len(m)
}

return cnt
}

// Map returns a PodResourcesMap for the pod resources list.
func (l PodResourcesList) Map() PodResourcesMap {
m := make(PodResourcesMap)
// GetPodResources returns resources for the given pod.
func (l *PodResourcesList) GetPodResources(ns, pod string) *PodResources {
if l == nil {
return nil
}

if p, ok := l.m[ns][pod]; ok {
return p
}

for i, p := range l.l {
var (
podNs = p.GetNamespace()
podName = p.GetName()
)

for _, p := range l {
podMap, ok := m[p.GetNamespace()]
podMap, ok := l.m[podNs]
if !ok {
podMap = make(map[string]*PodResources)
m[p.GetNamespace()] = podMap
l.m[podNs] = podMap
}

r := &PodResources{p}
podMap[podName] = r

if podNs == ns && podName == pod {
l.l = l.l[i+1:]
return r
}
podMap[p.GetName()] = &PodResources{p}
}

return m
}
l.l = nil

// GetPod returns resources for the given pod.
func (m PodResourcesMap) GetPod(ns, pod string) *PodResources {
return m[ns][pod]
return nil
}

// GetContainer returns resources for the given container.
func (m PodResourcesMap) GetContainer(ns, pod, ctr string) *ContainerResources {
return m.GetPod(ns, pod).GetContainer(ctr)
func (l *PodResourcesList) GetContainer(ns, pod, ctr string) *ContainerResources {
return l.GetPodResources(ns, pod).GetContainer(ctr)
}

// GetDeviceTopologyHints returns topology hints for the given container. checkDenied
Expand Down
Loading

0 comments on commit 3fb1ab2

Please sign in to comment.