Skip to content

Commit

Permalink
kgo: sort RequestSharded by broker metadata before returning
Browse files Browse the repository at this point in the history
This can make some usage easier / eliminate a need for sorting.
This is not strictly necessary, but when there are usually <20 brokers
anyway, this is quick.
  • Loading branch information
twmb committed Feb 15, 2021
1 parent 92a2c64 commit 21ddc56
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions pkg/kgo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"math/rand"
"reflect"
"sort"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -578,8 +579,40 @@ type ResponseShard struct {
// that does not exist, all those non-existent pieces are grouped into one
// request to the first seed broker. This will show up as a seed broker node ID
// (min int32) and the response will likely contain purely errors.
//
// The response shards are ordered by broker metadata.
func (cl *Client) RequestSharded(ctx context.Context, req kmsg.Request) []ResponseShard {
resps, _ := cl.shardedRequest(ctx, req)
sort.Slice(resps, func(i, j int) bool {
l := &resps[i].Meta
r := &resps[j].Meta

if l.NodeID < r.NodeID {
return true
}
if r.NodeID < l.NodeID {
return false
}
if l.Host < r.Host {
return true
}
if r.Host < l.Host {
return false
}
if l.Port < r.Port {
return true
}
if r.Port < l.Port {
return false
}
if l.Rack == nil {
return true
}
if r.Rack == nil {
return false
}
return *l.Rack < *r.Rack
})
return resps
}

Expand Down

0 comments on commit 21ddc56

Please sign in to comment.