Skip to content

Commit

Permalink
Update internal/client/v1/client/discoverer/discover.go
Browse files Browse the repository at this point in the history
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Signed-off-by: Yusuke Kato <[email protected]>
Signed-off-by: kpango <[email protected]>
  • Loading branch information
kpango and coderabbitai[bot] committed Nov 8, 2024
1 parent 3f18b2b commit 3b2a458
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions internal/client/v1/client/discoverer/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ func (c *client) discover(ctx context.Context) (err error) {
}

oldAddrs := c.GetAddrs(ctx)
log.Debug("discovered connected IPs: %v", connected)
c.addrs.Store(&connected)
return c.disconnectOldAddrs(ctx, oldAddrs, connected)
}
Expand Down Expand Up @@ -358,11 +359,16 @@ func (c *client) discoverNodes(ctx context.Context) (nodes *payload.Info_Nodes,
return nil, err
}
slices.SortFunc(nodes.Nodes, func(left, right *payload.Info_Node) int {
if left.GetMemory() == nil || right.GetMemory() == nil {
return 0 // Default comparison value; adjust as needed.
}
return cmp.Compare(left.GetMemory().GetUsage(), right.GetMemory().GetUsage())
})
return nodes, nil
}

var dId atomic.Uint64

func (c *client) discoverAddrs(
ctx context.Context, nodes *payload.Info_Nodes,
) (addrs []string, err error) {
Expand All @@ -379,6 +385,9 @@ func (c *client) discoverAddrs(
maxPodLen = l
}
slices.SortFunc(nodes.Nodes[i].Pods.Pods, func(left, right *payload.Info_Pod) int {
if left.GetMemory() == nil || right.GetMemory() == nil {
return 0 // Default comparison value; adjust as needed.
}
return cmp.Compare(left.GetMemory().GetUsage(), right.GetMemory().GetUsage())
})
}
Expand All @@ -387,6 +396,7 @@ func (c *client) discoverAddrs(
if err == nil && nbody != nil {
log.Debug(string(nbody))
}
id := dId.Add(0)
addrs = make([]string, 0, podLength)
for i := 0; i < maxPodLen; i++ {
for _, node := range nodes.GetNodes() {
Expand All @@ -403,13 +413,15 @@ func (c *client) discoverAddrs(
log.Debugf("resource based discovery connect from discoverer API for addr = %s failed %v", addr, errors.ErrAddrCouldNotDiscover(err, addr))
err = nil
} else {
log.Debugf("%d:\tdiscovered healthy pods IP: %s, Node Usage: %f, Pod Usage: %f", id, addr, node.GetMemory().GetUsage(), node.GetPods().GetPods()[i].GetMemory().GetUsage())
addrs = append(addrs, addr)
}

}
}
}
}
log.Debugf("%d:\tdiscovered healthy pods IPs: %v", id, addrs)
return addrs, nil
}

Expand Down

0 comments on commit 3b2a458

Please sign in to comment.